winepulse: Set pulse master volume to 0 when session is muted.
[wine.git] / dlls / comctl32 / edit.c
blobd9354a107b157b40820e6b59b1d4f713ad223cbf
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
7 * Copyright Frank Richter, 2005
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * TODO:
25 * - EDITBALLOONTIP structure
26 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
27 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
28 * - EM_GETIMESTATUS, EM_SETIMESTATUS
29 * - EN_ALIGN_LTR_EC
30 * - EN_ALIGN_RTL_EC
31 * - ES_OEMCONVERT
35 #include <stdarg.h>
36 #include <string.h>
37 #include <stdlib.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnt.h"
42 #include "imm.h"
43 #include "usp10.h"
44 #include "commctrl.h"
45 #include "uxtheme.h"
46 #include "vsstyle.h"
47 #include "wine/debug.h"
48 #include "wine/heap.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(edit);
52 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
53 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
54 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
55 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
58 * extra flags for EDITSTATE.flags field
60 #define EF_MODIFIED 0x0001 /* text has been modified */
61 #define EF_FOCUSED 0x0002 /* we have input focus */
62 #define EF_UPDATE 0x0004 /* notify parent of changed state */
63 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
64 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
66 wrapped line, instead of in front of the next character */
67 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
68 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
70 #define ID_CB_LISTBOX 1000
72 typedef enum
74 END_0 = 0, /* line ends with terminating '\0' character */
75 END_WRAP, /* line is wrapped */
76 END_HARD, /* line ends with a hard return '\r\n' */
77 END_SOFT, /* line ends with a soft return '\r\r\n' */
78 END_RICH /* line ends with a single '\n' */
79 } LINE_END;
81 typedef struct tagLINEDEF {
82 INT length; /* bruto length of a line in bytes */
83 INT net_length; /* netto length of a line in visible characters */
84 LINE_END ending;
85 INT width; /* width of the line in pixels */
86 INT index; /* line index into the buffer */
87 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
88 struct tagLINEDEF *next;
89 } LINEDEF;
91 typedef struct
93 LPWSTR text; /* the actual contents of the control */
94 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
95 UINT buffer_size; /* the size of the buffer in characters */
96 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
97 HFONT font; /* NULL means standard system font */
98 INT x_offset; /* scroll offset for multi lines this is in pixels
99 for single lines it's in characters */
100 INT line_height; /* height of a screen line in pixels */
101 INT char_width; /* average character width in pixels */
102 DWORD style; /* sane version of wnd->dwStyle */
103 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
104 INT undo_insert_count; /* number of characters inserted in sequence */
105 UINT undo_position; /* character index of the insertion and deletion */
106 LPWSTR undo_text; /* deleted text */
107 UINT undo_buffer_size; /* size of the deleted text buffer */
108 INT selection_start; /* == selection_end if no selection */
109 INT selection_end; /* == current caret position */
110 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
111 INT left_margin; /* in pixels */
112 INT right_margin; /* in pixels */
113 RECT format_rect;
114 INT text_width; /* width of the widest line in pixels for multi line controls
115 and just line width for single line controls */
116 INT region_posx; /* Position of cursor relative to region: */
117 INT region_posy; /* -1: to left, 0: within, 1: to right */
118 EDITWORDBREAKPROCW word_break_proc;
119 INT line_count; /* number of lines */
120 INT y_offset; /* scroll offset in number of lines */
121 BOOL bCaptureState; /* flag indicating whether mouse was captured */
122 BOOL bEnableState; /* flag keeping the enable state */
123 HWND hwndSelf; /* the our window handle */
124 HWND hwndParent; /* Handle of parent for sending EN_* messages.
125 Even if parent will change, EN_* messages
126 should be sent to the first parent. */
127 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
128 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
129 WCHAR *cue_banner_text;
130 BOOL cue_banner_draw_focused;
133 * only for multi line controls
135 INT lock_count; /* amount of re-entries in the EditWndProc */
136 INT tabs_count;
137 LPINT tabs;
138 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
139 HLOCAL hloc32W; /* our unicode local memory block */
140 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
142 * IME Data
144 UINT composition_len; /* length of composition, 0 == no composition */
145 int composition_start; /* the character position for the composition */
147 * Uniscribe Data
149 SCRIPT_LOGATTR *logAttr;
150 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
151 } EDITSTATE;
154 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
155 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
157 /* used for disabled or read-only edit control */
158 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
159 do \
160 { /* Notify parent which has created this edit control */ \
161 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
162 SendMessageW(es->hwndParent, WM_COMMAND, \
163 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
164 (LPARAM)(es->hwndSelf)); \
165 } while(0)
167 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
169 /*********************************************************************
171 * EM_CANUNDO
174 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
176 return (es->undo_insert_count || lstrlenW(es->undo_text));
180 /*********************************************************************
182 * EM_EMPTYUNDOBUFFER
185 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
187 es->undo_insert_count = 0;
188 *es->undo_text = '\0';
191 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
193 HBRUSH hbrush;
194 UINT msg;
196 if ((!es->bEnableState || (es->style & ES_READONLY)))
197 msg = WM_CTLCOLORSTATIC;
198 else
199 msg = WM_CTLCOLOREDIT;
201 /* Why do we notify to es->hwndParent, and we send this one to GetParent()? */
202 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
203 if (!hbrush)
204 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
205 return hbrush;
209 static inline UINT get_text_length(EDITSTATE *es)
211 if(es->text_length == (UINT)-1)
212 es->text_length = lstrlenW(es->text);
213 return es->text_length;
217 /*********************************************************************
219 * EDIT_WordBreakProc
221 * Find the beginning of words.
222 * Note: unlike the specs for a WordBreakProc, this function can
223 * only be called without linebreaks between s[0] up to
224 * s[count - 1]. Remember it is only called
225 * internally, so we can decide this for ourselves.
226 * Additionally we will always be breaking the full string.
229 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
231 INT ret = 0;
233 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
235 if(!s) return 0;
237 if (!es->logAttr)
239 SCRIPT_ANALYSIS psa;
241 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
242 psa.eScript = SCRIPT_UNDEFINED;
244 es->logAttr = heap_alloc(sizeof(SCRIPT_LOGATTR) * get_text_length(es));
245 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
248 switch (action) {
249 case WB_LEFT:
250 if (index)
251 index--;
252 while (index && !es->logAttr[index].fSoftBreak)
253 index--;
254 ret = index;
255 break;
256 case WB_RIGHT:
257 if (!count)
258 break;
259 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
260 index++;
261 ret = index;
262 break;
263 case WB_ISDELIMITER:
264 ret = es->logAttr[index].fWhiteSpace;
265 break;
266 default:
267 ERR("unknown action code, please report !\n");
268 break;
270 return ret;
274 /*********************************************************************
276 * EDIT_CallWordBreakProc
278 * Call appropriate WordBreakProc (internal or external).
280 * Note: The "start" argument should always be an index referring
281 * to es->text. The actual wordbreak proc might be
282 * 16 bit, so we can't always pass any 32 bit LPSTR.
283 * Hence we assume that es->text is the buffer that holds
284 * the string under examination (we can decide this for ourselves).
287 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
289 INT ret;
291 if (es->word_break_proc)
292 ret = es->word_break_proc(es->text + start, index, count, action);
293 else
294 ret = EDIT_WordBreakProc(es, es->text, index + start, count + start, action) - start;
296 return ret;
299 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
301 if (line_def->ssa)
303 ScriptStringFree(&line_def->ssa);
304 line_def->ssa = NULL;
308 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
310 LINEDEF *line_def = es->first_line_def;
311 while (line_def)
313 EDIT_InvalidateUniscribeData_linedef(line_def);
314 line_def = line_def->next;
316 if (es->ssa)
318 ScriptStringFree(&es->ssa);
319 es->ssa = NULL;
323 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
325 if (!line_def)
326 return NULL;
328 if (line_def->net_length && !line_def->ssa)
330 int index = line_def->index;
331 HFONT old_font = NULL;
332 HDC udc = dc;
333 SCRIPT_TABDEF tabdef;
334 HRESULT hr;
336 if (!udc)
337 udc = GetDC(es->hwndSelf);
338 if (es->font)
339 old_font = SelectObject(udc, es->font);
341 tabdef.cTabStops = es->tabs_count;
342 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
343 tabdef.pTabStops = es->tabs;
344 tabdef.iTabOrigin = 0;
346 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
347 (1.5*line_def->net_length+16), -1,
348 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
349 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
350 if (FAILED(hr))
352 WARN("ScriptStringAnalyse failed (%x)\n",hr);
353 line_def->ssa = NULL;
356 if (es->font)
357 SelectObject(udc, old_font);
358 if (udc != dc)
359 ReleaseDC(es->hwndSelf, udc);
362 return line_def->ssa;
365 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
367 LINEDEF *line_def;
369 if (!(es->style & ES_MULTILINE))
371 if (!es->ssa)
373 INT length = get_text_length(es);
374 HFONT old_font = NULL;
375 HDC udc = dc;
377 if (!udc)
378 udc = GetDC(es->hwndSelf);
379 if (es->font)
380 old_font = SelectObject(udc, es->font);
382 if (es->style & ES_PASSWORD)
383 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);
384 else
385 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
387 if (es->font)
388 SelectObject(udc, old_font);
389 if (udc != dc)
390 ReleaseDC(es->hwndSelf, udc);
392 return es->ssa;
394 else
396 line_def = es->first_line_def;
397 while (line_def && line)
399 line_def = line_def->next;
400 line--;
403 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
407 static inline INT get_vertical_line_count(EDITSTATE *es)
409 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
410 return max(1,vlc);
413 /*********************************************************************
415 * EDIT_BuildLineDefs_ML
417 * Build linked list of text lines.
418 * Lines can end with '\0' (last line), a character (if it is wrapped),
419 * a soft return '\r\r\n' or a hard return '\r\n'
422 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
424 LPWSTR current_position, cp;
425 INT fw;
426 LINEDEF *current_line;
427 LINEDEF *previous_line;
428 LINEDEF *start_line;
429 INT line_index = 0, nstart_line, nstart_index;
430 INT line_count = es->line_count;
431 INT orig_net_length;
432 RECT rc;
433 INT vlc;
435 if (istart == iend && delta == 0)
436 return;
438 previous_line = NULL;
439 current_line = es->first_line_def;
441 /* Find starting line. istart must lie inside an existing line or
442 * at the end of buffer */
443 do {
444 if (istart < current_line->index + current_line->length ||
445 current_line->ending == END_0)
446 break;
448 previous_line = current_line;
449 current_line = current_line->next;
450 line_index++;
451 } while (current_line);
453 if (!current_line) /* Error occurred start is not inside previous buffer */
455 FIXME(" modification occurred outside buffer\n");
456 return;
459 /* Remember start of modifications in order to calculate update region */
460 nstart_line = line_index;
461 nstart_index = current_line->index;
463 /* We must start to reformat from the previous line since the modifications
464 * may have caused the line to wrap upwards. */
465 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
467 line_index--;
468 current_line = previous_line;
470 start_line = current_line;
472 fw = es->format_rect.right - es->format_rect.left;
473 current_position = es->text + current_line->index;
474 vlc = get_vertical_line_count(es);
475 do {
476 if (current_line != start_line)
478 if (!current_line || current_line->index + delta > current_position - es->text)
480 /* The buffer has been expanded, create a new line and
481 insert it into the link list */
482 LINEDEF *new_line = heap_alloc_zero(sizeof(*new_line));
483 new_line->next = previous_line->next;
484 previous_line->next = new_line;
485 current_line = new_line;
486 es->line_count++;
488 else if (current_line->index + delta < current_position - es->text)
490 /* The previous line merged with this line so we delete this extra entry */
491 previous_line->next = current_line->next;
492 heap_free(current_line);
493 current_line = previous_line->next;
494 es->line_count--;
495 continue;
497 else /* current_line->index + delta == current_position */
499 if (current_position - es->text > iend)
500 break; /* We reached end of line modifications */
501 /* else recalculate this line */
505 current_line->index = current_position - es->text;
506 orig_net_length = current_line->net_length;
508 /* Find end of line */
509 cp = current_position;
510 while (*cp) {
511 if (*cp == '\n') break;
512 if ((*cp == '\r') && (*(cp + 1) == '\n'))
513 break;
514 cp++;
517 /* Mark type of line termination */
518 if (!(*cp)) {
519 current_line->ending = END_0;
520 current_line->net_length = lstrlenW(current_position);
521 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
522 current_line->ending = END_SOFT;
523 current_line->net_length = cp - current_position - 1;
524 } else if (*cp == '\n') {
525 current_line->ending = END_RICH;
526 current_line->net_length = cp - current_position;
527 } else {
528 current_line->ending = END_HARD;
529 current_line->net_length = cp - current_position;
532 if (current_line->net_length)
534 const SIZE *sz;
535 EDIT_InvalidateUniscribeData_linedef(current_line);
536 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
537 if (current_line->ssa)
539 sz = ScriptString_pSize(current_line->ssa);
540 /* Calculate line width */
541 current_line->width = sz->cx;
543 else current_line->width = es->char_width * current_line->net_length;
545 else current_line->width = 0;
547 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
549 /* Line breaks just look back from the end and find the next break and try that. */
551 if (!(es->style & ES_AUTOHSCROLL)) {
552 if (current_line->width > fw && fw > es->char_width) {
554 INT prev, next;
555 int w;
556 const SIZE *sz;
557 float d;
559 prev = current_line->net_length - 1;
560 w = current_line->net_length;
561 d = (float)current_line->width/(float)fw;
562 if (d > 1.2f) d -= 0.2f;
563 next = prev/d;
564 if (next >= prev) next = prev-1;
565 do {
566 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
567 next, current_line->net_length, WB_LEFT);
568 current_line->net_length = prev;
569 EDIT_InvalidateUniscribeData_linedef(current_line);
570 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
571 if (current_line->ssa)
572 sz = ScriptString_pSize(current_line->ssa);
573 else sz = 0;
574 if (sz)
575 current_line->width = sz->cx;
576 else
577 prev = 0;
578 next = prev - 1;
579 } while (prev && current_line->width > fw);
580 current_line->net_length = w;
582 if (prev == 0) { /* Didn't find a line break so force a break */
583 INT *piDx;
584 const INT *count;
586 EDIT_InvalidateUniscribeData_linedef(current_line);
587 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
589 if (current_line->ssa)
591 count = ScriptString_pcOutChars(current_line->ssa);
592 piDx = heap_alloc(sizeof(INT) * (*count));
593 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
595 prev = current_line->net_length-1;
596 do {
597 current_line->width -= piDx[prev];
598 prev--;
599 } while ( prev > 0 && current_line->width > fw);
600 if (prev<=0)
601 prev = 1;
602 heap_free(piDx);
604 else
605 prev = (fw / es->char_width);
608 /* If the first line we are calculating, wrapped before istart, we must
609 * adjust istart in order for this to be reflected in the update region. */
610 if (current_line->index == nstart_index && istart > current_line->index + prev)
611 istart = current_line->index + prev;
612 /* else if we are updating the previous line before the first line we
613 * are re-calculating and it expanded */
614 else if (current_line == start_line &&
615 current_line->index != nstart_index && orig_net_length < prev)
617 /* Line expanded due to an upwards line wrap so we must partially include
618 * previous line in update region */
619 nstart_line = line_index;
620 nstart_index = current_line->index;
621 istart = current_line->index + orig_net_length;
624 current_line->net_length = prev;
625 current_line->ending = END_WRAP;
627 if (current_line->net_length > 0)
629 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
630 if (current_line->ssa)
632 sz = ScriptString_pSize(current_line->ssa);
633 current_line->width = sz->cx;
635 else
636 current_line->width = 0;
638 else current_line->width = 0;
640 else if (current_line == start_line &&
641 current_line->index != nstart_index &&
642 orig_net_length < current_line->net_length) {
643 /* The previous line expanded but it's still not as wide as the client rect */
644 /* The expansion is due to an upwards line wrap so we must partially include
645 it in the update region */
646 nstart_line = line_index;
647 nstart_index = current_line->index;
648 istart = current_line->index + orig_net_length;
653 /* Adjust length to include line termination */
654 switch (current_line->ending) {
655 case END_SOFT:
656 current_line->length = current_line->net_length + 3;
657 break;
658 case END_RICH:
659 current_line->length = current_line->net_length + 1;
660 break;
661 case END_HARD:
662 current_line->length = current_line->net_length + 2;
663 break;
664 case END_WRAP:
665 case END_0:
666 current_line->length = current_line->net_length;
667 break;
669 es->text_width = max(es->text_width, current_line->width);
670 current_position += current_line->length;
671 previous_line = current_line;
673 /* Discard data for non-visible lines. It will be calculated as needed */
674 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
675 EDIT_InvalidateUniscribeData_linedef(current_line);
677 current_line = current_line->next;
678 line_index++;
679 } while (previous_line->ending != END_0);
681 /* Finish adjusting line indexes by delta or remove hanging lines */
682 if (previous_line->ending == END_0)
684 LINEDEF *pnext = NULL;
686 previous_line->next = NULL;
687 while (current_line)
689 pnext = current_line->next;
690 EDIT_InvalidateUniscribeData_linedef(current_line);
691 heap_free(current_line);
692 current_line = pnext;
693 es->line_count--;
696 else if (delta != 0)
698 while (current_line)
700 current_line->index += delta;
701 current_line = current_line->next;
705 /* Calculate rest of modification rectangle */
706 if (hrgn)
708 HRGN tmphrgn;
710 * We calculate two rectangles. One for the first line which may have
711 * an indent with respect to the format rect. The other is a format-width
712 * rectangle that spans the rest of the lines that changed or moved.
714 rc.top = es->format_rect.top + nstart_line * es->line_height -
715 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
716 rc.bottom = rc.top + es->line_height;
717 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
718 rc.left = es->format_rect.left;
719 else
720 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
721 rc.right = es->format_rect.right;
722 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
724 rc.top = rc.bottom;
725 rc.left = es->format_rect.left;
726 rc.right = es->format_rect.right;
728 * If lines were added or removed we must re-paint the remainder of the
729 * lines since the remaining lines were either shifted up or down.
731 if (line_count < es->line_count) /* We added lines */
732 rc.bottom = es->line_count * es->line_height;
733 else if (line_count > es->line_count) /* We removed lines */
734 rc.bottom = line_count * es->line_height;
735 else
736 rc.bottom = line_index * es->line_height;
737 rc.bottom += es->format_rect.top;
738 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
739 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
740 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
741 DeleteObject(tmphrgn);
745 /*********************************************************************
747 * EDIT_CalcLineWidth_SL
750 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
752 EDIT_UpdateUniscribeData(es, NULL, 0);
753 if (es->ssa)
755 const SIZE *size;
756 size = ScriptString_pSize(es->ssa);
757 es->text_width = size->cx;
759 else
760 es->text_width = 0;
763 /*********************************************************************
765 * EDIT_CharFromPos
767 * Beware: This is not the function called on EM_CHARFROMPOS
768 * The position _can_ be outside the formatting / client
769 * rectangle
770 * The return value is only the character index
773 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
775 INT index;
777 if (es->style & ES_MULTILINE) {
778 int trailing;
779 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
780 INT line_index = 0;
781 LINEDEF *line_def = es->first_line_def;
782 EDIT_UpdateUniscribeData(es, NULL, line);
783 while ((line > 0) && line_def->next) {
784 line_index += line_def->length;
785 line_def = line_def->next;
786 line--;
789 x += es->x_offset - es->format_rect.left;
790 if (es->style & ES_RIGHT)
791 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
792 else if (es->style & ES_CENTER)
793 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
794 if (x >= line_def->width) {
795 if (after_wrap)
796 *after_wrap = (line_def->ending == END_WRAP);
797 return line_index + line_def->net_length;
799 if (x <= 0 || !line_def->ssa) {
800 if (after_wrap)
801 *after_wrap = FALSE;
802 return line_index;
805 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
806 if (trailing) index++;
807 index += line_index;
808 if (after_wrap)
809 *after_wrap = ((index == line_index + line_def->net_length) &&
810 (line_def->ending == END_WRAP));
811 } else {
812 INT xoff = 0;
813 INT trailing;
814 if (after_wrap)
815 *after_wrap = FALSE;
816 x -= es->format_rect.left;
817 if (!x)
818 return es->x_offset;
820 if (!es->x_offset)
822 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
823 if (es->style & ES_RIGHT)
824 x -= indent;
825 else if (es->style & ES_CENTER)
826 x -= indent / 2;
829 EDIT_UpdateUniscribeData(es, NULL, 0);
830 if (es->x_offset)
832 if (es->ssa)
834 if (es->x_offset>= get_text_length(es))
836 const SIZE *size;
837 size = ScriptString_pSize(es->ssa);
838 xoff = size->cx;
840 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
842 else
843 xoff = 0;
845 if (x < 0)
847 if (x + xoff > 0 || !es->ssa)
849 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
850 if (trailing) index++;
852 else
853 index = 0;
855 else
857 if (x)
859 const SIZE *size = NULL;
860 if (es->ssa)
861 size = ScriptString_pSize(es->ssa);
862 if (!size)
863 index = 0;
864 else if (x > size->cx)
865 index = get_text_length(es);
866 else if (es->ssa)
868 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
869 if (trailing) index++;
871 else
872 index = 0;
874 else
875 index = es->x_offset;
878 return index;
882 /*********************************************************************
884 * EDIT_ConfinePoint
886 * adjusts the point to be within the formatting rectangle
887 * (so CharFromPos returns the nearest _visible_ character)
890 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
892 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
893 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
897 /*********************************************************************
899 * EM_LINEFROMCHAR
902 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
904 INT line;
905 LINEDEF *line_def;
907 if (!(es->style & ES_MULTILINE))
908 return 0;
909 if (index > (INT)get_text_length(es))
910 return es->line_count - 1;
911 if (index == -1)
912 index = min(es->selection_start, es->selection_end);
914 line = 0;
915 line_def = es->first_line_def;
916 index -= line_def->length;
917 while ((index >= 0) && line_def->next) {
918 line++;
919 line_def = line_def->next;
920 index -= line_def->length;
922 return line;
926 /*********************************************************************
928 * EM_LINEINDEX
931 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
933 INT line_index;
934 const LINEDEF *line_def;
936 if (!(es->style & ES_MULTILINE))
937 return 0;
938 if (line >= es->line_count)
939 return -1;
941 line_index = 0;
942 line_def = es->first_line_def;
943 if (line == -1) {
944 INT index = es->selection_end - line_def->length;
945 while ((index >= 0) && line_def->next) {
946 line_index += line_def->length;
947 line_def = line_def->next;
948 index -= line_def->length;
950 } else {
951 while (line > 0) {
952 line_index += line_def->length;
953 line_def = line_def->next;
954 line--;
957 return line_index;
961 /*********************************************************************
963 * EM_LINELENGTH
966 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
968 LINEDEF *line_def;
970 if (!(es->style & ES_MULTILINE))
971 return get_text_length(es);
973 if (index == -1) {
974 /* get the number of remaining non-selected chars of selected lines */
975 INT32 l; /* line number */
976 INT32 li; /* index of first char in line */
977 INT32 count;
978 l = EDIT_EM_LineFromChar(es, es->selection_start);
979 /* # chars before start of selection area */
980 count = es->selection_start - EDIT_EM_LineIndex(es, l);
981 l = EDIT_EM_LineFromChar(es, es->selection_end);
982 /* # chars after end of selection */
983 li = EDIT_EM_LineIndex(es, l);
984 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
985 return count;
987 line_def = es->first_line_def;
988 index -= line_def->length;
989 while ((index >= 0) && line_def->next) {
990 line_def = line_def->next;
991 index -= line_def->length;
993 return line_def->net_length;
997 /*********************************************************************
999 * EM_POSFROMCHAR
1002 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1004 INT len = get_text_length(es);
1005 INT l;
1006 INT li;
1007 INT x = 0;
1008 INT y = 0;
1009 INT w;
1010 INT lw;
1011 LINEDEF *line_def;
1013 index = min(index, len);
1014 if (es->style & ES_MULTILINE) {
1015 l = EDIT_EM_LineFromChar(es, index);
1016 EDIT_UpdateUniscribeData(es, NULL, l);
1018 y = (l - es->y_offset) * es->line_height;
1019 li = EDIT_EM_LineIndex(es, l);
1020 if (after_wrap && (li == index) && l) {
1021 INT l2 = l - 1;
1022 line_def = es->first_line_def;
1023 while (l2) {
1024 line_def = line_def->next;
1025 l2--;
1027 if (line_def->ending == END_WRAP) {
1028 l--;
1029 y -= es->line_height;
1030 li = EDIT_EM_LineIndex(es, l);
1034 line_def = es->first_line_def;
1035 while (line_def->index != li)
1036 line_def = line_def->next;
1038 lw = line_def->width;
1039 w = es->format_rect.right - es->format_rect.left;
1040 if (line_def->ssa)
1041 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1042 x -= es->x_offset;
1044 if (es->style & ES_RIGHT)
1045 x = w - (lw - x);
1046 else if (es->style & ES_CENTER)
1047 x += (w - lw) / 2;
1048 } else {
1049 INT xoff = 0;
1050 INT xi = 0;
1051 EDIT_UpdateUniscribeData(es, NULL, 0);
1052 if (es->x_offset)
1054 if (es->ssa)
1056 if (es->x_offset >= get_text_length(es))
1058 int leftover = es->x_offset - get_text_length(es);
1059 if (es->ssa)
1061 const SIZE *size;
1062 size = ScriptString_pSize(es->ssa);
1063 xoff = size->cx;
1065 else
1066 xoff = 0;
1067 xoff += es->char_width * leftover;
1069 else
1070 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1072 else
1073 xoff = 0;
1075 if (index)
1077 if (index >= get_text_length(es))
1079 if (es->ssa)
1081 const SIZE *size;
1082 size = ScriptString_pSize(es->ssa);
1083 xi = size->cx;
1085 else
1086 xi = 0;
1088 else if (es->ssa)
1089 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1090 else
1091 xi = 0;
1093 x = xi - xoff;
1095 if (index >= es->x_offset) {
1096 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1098 w = es->format_rect.right - es->format_rect.left;
1099 if (w > es->text_width)
1101 if (es->style & ES_RIGHT)
1102 x += w - es->text_width;
1103 else if (es->style & ES_CENTER)
1104 x += (w - es->text_width) / 2;
1108 y = 0;
1110 x += es->format_rect.left;
1111 y += es->format_rect.top;
1112 return MAKELONG((INT16)x, (INT16)y);
1116 /*********************************************************************
1118 * EDIT_GetLineRect
1120 * Calculates the bounding rectangle for a line from a starting
1121 * column to an ending column.
1124 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1126 SCRIPT_STRING_ANALYSIS ssa;
1127 INT line_index = 0;
1128 INT pt1, pt2, pt3;
1130 if (es->style & ES_MULTILINE)
1132 const LINEDEF *line_def = NULL;
1133 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1134 if (line >= es->line_count)
1135 return;
1137 line_def = es->first_line_def;
1138 if (line == -1) {
1139 INT index = es->selection_end - line_def->length;
1140 while ((index >= 0) && line_def->next) {
1141 line_index += line_def->length;
1142 line_def = line_def->next;
1143 index -= line_def->length;
1145 } else {
1146 while (line > 0) {
1147 line_index += line_def->length;
1148 line_def = line_def->next;
1149 line--;
1152 ssa = line_def->ssa;
1154 else
1156 line_index = 0;
1157 rc->top = es->format_rect.top;
1158 ssa = es->ssa;
1161 rc->bottom = rc->top + es->line_height;
1162 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1163 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1164 if (ssa)
1166 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1167 pt3+=es->format_rect.left;
1169 else pt3 = pt1;
1170 rc->right = max(max(pt1 , pt2),pt3);
1171 rc->left = min(min(pt1, pt2),pt3);
1175 static inline void text_buffer_changed(EDITSTATE *es)
1177 es->text_length = (UINT)-1;
1179 heap_free( es->logAttr );
1180 es->logAttr = NULL;
1181 EDIT_InvalidateUniscribeData(es);
1184 /*********************************************************************
1185 * EDIT_LockBuffer
1188 static void EDIT_LockBuffer(EDITSTATE *es)
1190 if (!es->text)
1192 if (!es->hloc32W)
1193 return;
1195 es->text = LocalLock(es->hloc32W);
1198 es->lock_count++;
1202 /*********************************************************************
1204 * EDIT_UnlockBuffer
1207 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1209 /* Edit window might be already destroyed */
1210 if (!IsWindow(es->hwndSelf))
1212 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1213 return;
1216 if (!es->lock_count)
1218 ERR("lock_count == 0 ... please report\n");
1219 return;
1222 if (!es->text)
1224 ERR("es->text == 0 ... please report\n");
1225 return;
1228 if (force || (es->lock_count == 1))
1230 if (es->hloc32W)
1232 LocalUnlock(es->hloc32W);
1233 es->text = NULL;
1235 else
1237 ERR("no buffer ... please report\n");
1238 return;
1243 es->lock_count--;
1247 /*********************************************************************
1249 * EDIT_MakeFit
1251 * Try to fit size + 1 characters in the buffer.
1253 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1255 HLOCAL hNew32W;
1257 if (size <= es->buffer_size)
1258 return TRUE;
1260 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1262 /* Force edit to unlock its buffer. es->text now NULL */
1263 EDIT_UnlockBuffer(es, TRUE);
1265 if (es->hloc32W) {
1266 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1267 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1268 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1269 es->hloc32W = hNew32W;
1270 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1274 EDIT_LockBuffer(es);
1276 if (es->buffer_size < size) {
1277 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1278 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1279 return FALSE;
1280 } else {
1281 TRACE("We now have %d+1\n", es->buffer_size);
1282 return TRUE;
1287 /*********************************************************************
1289 * EDIT_MakeUndoFit
1291 * Try to fit size + 1 bytes in the undo buffer.
1294 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1296 UINT alloc_size;
1298 if (size <= es->undo_buffer_size)
1299 return TRUE;
1301 TRACE("trying to ReAlloc to %d+1\n", size);
1303 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1304 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1305 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1306 return TRUE;
1308 else
1310 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1311 return FALSE;
1316 /*********************************************************************
1318 * EDIT_UpdateTextRegion
1321 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1323 if (es->flags & EF_UPDATE) {
1324 es->flags &= ~EF_UPDATE;
1325 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1327 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1331 /*********************************************************************
1333 * EDIT_UpdateText
1336 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1338 if (es->flags & EF_UPDATE) {
1339 es->flags &= ~EF_UPDATE;
1340 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1342 InvalidateRect(es->hwndSelf, rc, bErase);
1345 /*********************************************************************
1347 * EDIT_SL_InvalidateText
1349 * Called from EDIT_InvalidateText().
1350 * Does the job for single-line controls only.
1353 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1355 RECT line_rect;
1356 RECT rc;
1358 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1359 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1360 EDIT_UpdateText(es, &rc, TRUE);
1363 /*********************************************************************
1365 * EDIT_ML_InvalidateText
1367 * Called from EDIT_InvalidateText().
1368 * Does the job for multi-line controls only.
1371 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1373 INT vlc = get_vertical_line_count(es);
1374 INT sl = EDIT_EM_LineFromChar(es, start);
1375 INT el = EDIT_EM_LineFromChar(es, end);
1376 INT sc;
1377 INT ec;
1378 RECT rc1;
1379 RECT rcWnd;
1380 RECT rcLine;
1381 RECT rcUpdate;
1382 INT l;
1384 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1385 return;
1387 sc = start - EDIT_EM_LineIndex(es, sl);
1388 ec = end - EDIT_EM_LineIndex(es, el);
1389 if (sl < es->y_offset) {
1390 sl = es->y_offset;
1391 sc = 0;
1393 if (el > es->y_offset + vlc) {
1394 el = es->y_offset + vlc;
1395 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1397 GetClientRect(es->hwndSelf, &rc1);
1398 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1399 if (sl == el) {
1400 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1401 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1402 EDIT_UpdateText(es, &rcUpdate, TRUE);
1403 } else {
1404 EDIT_GetLineRect(es, sl, sc,
1405 EDIT_EM_LineLength(es,
1406 EDIT_EM_LineIndex(es, sl)),
1407 &rcLine);
1408 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1409 EDIT_UpdateText(es, &rcUpdate, TRUE);
1410 for (l = sl + 1 ; l < el ; l++) {
1411 EDIT_GetLineRect(es, l, 0,
1412 EDIT_EM_LineLength(es,
1413 EDIT_EM_LineIndex(es, l)),
1414 &rcLine);
1415 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1416 EDIT_UpdateText(es, &rcUpdate, TRUE);
1418 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1419 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1420 EDIT_UpdateText(es, &rcUpdate, TRUE);
1425 /*********************************************************************
1427 * EDIT_InvalidateText
1429 * Invalidate the text from offset start up to, but not including,
1430 * offset end. Useful for (re)painting the selection.
1431 * Regions outside the linewidth are not invalidated.
1432 * end == -1 means end == TextLength.
1433 * start and end need not be ordered.
1436 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1438 if (end == start)
1439 return;
1441 if (end == -1)
1442 end = get_text_length(es);
1444 if (end < start) {
1445 INT tmp = start;
1446 start = end;
1447 end = tmp;
1450 if (es->style & ES_MULTILINE)
1451 EDIT_ML_InvalidateText(es, start, end);
1452 else
1453 EDIT_SL_InvalidateText(es, start, end);
1457 /*********************************************************************
1459 * EDIT_EM_SetSel
1461 * note: unlike the specs say: the order of start and end
1462 * _is_ preserved in Windows. (i.e. start can be > end)
1463 * In other words: this handler is OK
1466 static BOOL EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1468 UINT old_start = es->selection_start;
1469 UINT old_end = es->selection_end;
1470 UINT len = get_text_length(es);
1472 if (start == old_start && end == old_end)
1473 return FALSE;
1475 if (start == (UINT)-1) {
1476 start = es->selection_end;
1477 end = es->selection_end;
1478 } else {
1479 start = min(start, len);
1480 end = min(end, len);
1482 es->selection_start = start;
1483 es->selection_end = end;
1484 if (after_wrap)
1485 es->flags |= EF_AFTER_WRAP;
1486 else
1487 es->flags &= ~EF_AFTER_WRAP;
1488 /* Compute the necessary invalidation region. */
1489 /* Note that we don't need to invalidate regions which have
1490 * "never" been selected, or those which are "still" selected.
1491 * In fact, every time we hit a selection boundary, we can
1492 * *toggle* whether we need to invalidate. Thus we can optimize by
1493 * *sorting* the interval endpoints. Let's assume that we sort them
1494 * in this order:
1495 * start <= end <= old_start <= old_end
1496 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1497 * in 5 comparisons; i.e. it is impossible to do better than the
1498 * following: */
1499 ORDER_UINT(end, old_end);
1500 ORDER_UINT(start, old_start);
1501 ORDER_UINT(old_start, old_end);
1502 ORDER_UINT(start, end);
1503 /* Note that at this point 'end' and 'old_start' are not in order, but
1504 * start is definitely the min. and old_end is definitely the max. */
1505 if (end != old_start)
1508 * One can also do
1509 * ORDER_UINT32(end, old_start);
1510 * EDIT_InvalidateText(es, start, end);
1511 * EDIT_InvalidateText(es, old_start, old_end);
1512 * in place of the following if statement.
1513 * (That would complete the optimal five-comparison four-element sort.)
1515 if (old_start > end )
1517 EDIT_InvalidateText(es, start, end);
1518 EDIT_InvalidateText(es, old_start, old_end);
1520 else
1522 EDIT_InvalidateText(es, start, old_start);
1523 EDIT_InvalidateText(es, end, old_end);
1526 else EDIT_InvalidateText(es, start, old_end);
1528 return TRUE;
1532 /*********************************************************************
1534 * EDIT_UpdateScrollInfo
1537 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1539 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1541 SCROLLINFO si;
1542 si.cbSize = sizeof(SCROLLINFO);
1543 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1544 si.nMin = 0;
1545 si.nMax = es->line_count - 1;
1546 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1547 si.nPos = es->y_offset;
1548 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1549 si.nMin, si.nMax, si.nPage, si.nPos);
1550 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1553 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1555 SCROLLINFO si;
1556 si.cbSize = sizeof(SCROLLINFO);
1557 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1558 si.nMin = 0;
1559 si.nMax = es->text_width - 1;
1560 si.nPage = es->format_rect.right - es->format_rect.left;
1561 si.nPos = es->x_offset;
1562 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1563 si.nMin, si.nMax, si.nPage, si.nPos);
1564 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1569 /*********************************************************************
1571 * EDIT_EM_LineScroll_internal
1573 * Version of EDIT_EM_LineScroll for internal use.
1574 * It doesn't refuse if ES_MULTILINE is set and assumes that
1575 * dx is in pixels, dy - in lines.
1578 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1580 INT nyoff;
1581 INT x_offset_in_pixels;
1582 INT lines_per_page;
1584 if (!es->line_height || !es->char_width)
1585 return TRUE;
1587 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1589 if (es->style & ES_MULTILINE)
1591 x_offset_in_pixels = es->x_offset;
1593 else
1595 dy = 0;
1596 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1599 if (-dx > x_offset_in_pixels)
1600 dx = -x_offset_in_pixels;
1601 if (dx > es->text_width - x_offset_in_pixels)
1602 dx = es->text_width - x_offset_in_pixels;
1603 nyoff = max(0, es->y_offset + dy);
1604 if (nyoff >= es->line_count - lines_per_page)
1605 nyoff = max(0, es->line_count - lines_per_page);
1606 dy = (es->y_offset - nyoff) * es->line_height;
1607 if (dx || dy) {
1608 RECT rc1;
1609 RECT rc;
1611 es->y_offset = nyoff;
1612 if(es->style & ES_MULTILINE)
1613 es->x_offset += dx;
1614 else
1615 es->x_offset += dx / es->char_width;
1617 GetClientRect(es->hwndSelf, &rc1);
1618 IntersectRect(&rc, &rc1, &es->format_rect);
1619 ScrollWindowEx(es->hwndSelf, -dx, dy,
1620 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1621 /* force scroll info update */
1622 EDIT_UpdateScrollInfo(es);
1624 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1625 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1626 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1627 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1628 return TRUE;
1631 /*********************************************************************
1633 * EM_LINESCROLL
1635 * NOTE: dx is in average character widths, dy - in lines;
1638 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1640 if (!(es->style & ES_MULTILINE))
1641 return FALSE;
1643 dx *= es->char_width;
1644 return EDIT_EM_LineScroll_internal(es, dx, dy);
1648 /*********************************************************************
1650 * EM_SCROLL
1653 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1655 INT dy;
1657 if (!(es->style & ES_MULTILINE))
1658 return (LRESULT)FALSE;
1660 dy = 0;
1662 switch (action) {
1663 case SB_LINEUP:
1664 if (es->y_offset)
1665 dy = -1;
1666 break;
1667 case SB_LINEDOWN:
1668 if (es->y_offset < es->line_count - 1)
1669 dy = 1;
1670 break;
1671 case SB_PAGEUP:
1672 if (es->y_offset)
1673 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1674 break;
1675 case SB_PAGEDOWN:
1676 if (es->y_offset < es->line_count - 1)
1677 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1678 break;
1679 default:
1680 return (LRESULT)FALSE;
1682 if (dy) {
1683 INT vlc = get_vertical_line_count(es);
1684 /* check if we are going to move too far */
1685 if(es->y_offset + dy > es->line_count - vlc)
1686 dy = max(es->line_count - vlc, 0) - es->y_offset;
1688 /* Notification is done in EDIT_EM_LineScroll */
1689 if(dy) {
1690 EDIT_EM_LineScroll(es, 0, dy);
1691 return MAKELONG(dy, TRUE);
1695 return (LRESULT)FALSE;
1699 /*********************************************************************
1701 * EDIT_SetCaretPos
1704 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1705 BOOL after_wrap)
1707 LRESULT res;
1709 if (es->flags & EF_FOCUSED)
1711 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1712 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1713 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1718 /*********************************************************************
1720 * EM_SCROLLCARET
1723 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1725 if (es->style & ES_MULTILINE) {
1726 INT l;
1727 INT vlc;
1728 INT ww;
1729 INT cw = es->char_width;
1730 INT x;
1731 INT dy = 0;
1732 INT dx = 0;
1734 l = EDIT_EM_LineFromChar(es, es->selection_end);
1735 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1736 vlc = get_vertical_line_count(es);
1737 if (l >= es->y_offset + vlc)
1738 dy = l - vlc + 1 - es->y_offset;
1739 if (l < es->y_offset)
1740 dy = l - es->y_offset;
1741 ww = es->format_rect.right - es->format_rect.left;
1742 if (x < es->format_rect.left)
1743 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1744 if (x > es->format_rect.right)
1745 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1746 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1748 /* check if we are going to move too far */
1749 if(es->x_offset + dx + ww > es->text_width)
1750 dx = es->text_width - ww - es->x_offset;
1751 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1752 EDIT_EM_LineScroll_internal(es, dx, dy);
1754 } else {
1755 INT x;
1756 INT goal;
1757 INT format_width;
1759 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1760 format_width = es->format_rect.right - es->format_rect.left;
1761 if (x < es->format_rect.left) {
1762 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1763 do {
1764 es->x_offset--;
1765 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1766 } while ((x < goal) && es->x_offset);
1767 /* FIXME: use ScrollWindow() somehow to improve performance */
1768 EDIT_UpdateText(es, NULL, TRUE);
1769 } else if (x > es->format_rect.right) {
1770 INT x_last;
1771 INT len = get_text_length(es);
1772 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1773 do {
1774 es->x_offset++;
1775 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1776 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1777 } while ((x > goal) && (x_last > es->format_rect.right));
1778 /* FIXME: use ScrollWindow() somehow to improve performance */
1779 EDIT_UpdateText(es, NULL, TRUE);
1783 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1787 /*********************************************************************
1789 * EDIT_MoveBackward
1792 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1794 INT e = es->selection_end;
1796 if (e) {
1797 e--;
1798 if ((es->style & ES_MULTILINE) && e &&
1799 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1800 e--;
1801 if (e && (es->text[e - 1] == '\r'))
1802 e--;
1805 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1806 EDIT_EM_ScrollCaret(es);
1810 /*********************************************************************
1812 * EDIT_MoveDown_ML
1814 * Only for multi line controls
1815 * Move the caret one line down, on a column with the nearest
1816 * x coordinate on the screen (might be a different column).
1819 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1821 INT s = es->selection_start;
1822 INT e = es->selection_end;
1823 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1824 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1825 INT x = (short)LOWORD(pos);
1826 INT y = (short)HIWORD(pos);
1828 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1829 if (!extend)
1830 s = e;
1831 EDIT_EM_SetSel(es, s, e, after_wrap);
1832 EDIT_EM_ScrollCaret(es);
1836 /*********************************************************************
1838 * EDIT_MoveEnd
1841 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1843 BOOL after_wrap = FALSE;
1844 INT e;
1846 /* Pass a high value in x to make sure of receiving the end of the line */
1847 if (!ctrl && (es->style & ES_MULTILINE))
1848 e = EDIT_CharFromPos(es, 0x3fffffff,
1849 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1850 else
1851 e = get_text_length(es);
1852 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1853 EDIT_EM_ScrollCaret(es);
1857 /*********************************************************************
1859 * EDIT_MoveForward
1862 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1864 INT e = es->selection_end;
1866 if (es->text[e]) {
1867 e++;
1868 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1869 if (es->text[e] == '\n')
1870 e++;
1871 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1872 e += 2;
1875 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1876 EDIT_EM_ScrollCaret(es);
1880 /*********************************************************************
1882 * EDIT_MoveHome
1884 * Home key: move to beginning of line.
1887 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1889 INT e;
1891 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1892 if (!ctrl && (es->style & ES_MULTILINE))
1893 e = EDIT_CharFromPos(es, -es->x_offset,
1894 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1895 else
1896 e = 0;
1897 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1898 EDIT_EM_ScrollCaret(es);
1902 /*********************************************************************
1904 * EDIT_MovePageDown_ML
1906 * Only for multi line controls
1907 * Move the caret one page down, on a column with the nearest
1908 * x coordinate on the screen (might be a different column).
1911 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1913 INT s = es->selection_start;
1914 INT e = es->selection_end;
1915 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1916 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1917 INT x = (short)LOWORD(pos);
1918 INT y = (short)HIWORD(pos);
1920 e = EDIT_CharFromPos(es, x,
1921 y + (es->format_rect.bottom - es->format_rect.top),
1922 &after_wrap);
1923 if (!extend)
1924 s = e;
1925 EDIT_EM_SetSel(es, s, e, after_wrap);
1926 EDIT_EM_ScrollCaret(es);
1930 /*********************************************************************
1932 * EDIT_MovePageUp_ML
1934 * Only for multi line controls
1935 * Move the caret one page up, on a column with the nearest
1936 * x coordinate on the screen (might be a different column).
1939 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1941 INT s = es->selection_start;
1942 INT e = es->selection_end;
1943 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1944 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1945 INT x = (short)LOWORD(pos);
1946 INT y = (short)HIWORD(pos);
1948 e = EDIT_CharFromPos(es, x,
1949 y - (es->format_rect.bottom - es->format_rect.top),
1950 &after_wrap);
1951 if (!extend)
1952 s = e;
1953 EDIT_EM_SetSel(es, s, e, after_wrap);
1954 EDIT_EM_ScrollCaret(es);
1958 /*********************************************************************
1960 * EDIT_MoveUp_ML
1962 * Only for multi line controls
1963 * Move the caret one line up, on a column with the nearest
1964 * x coordinate on the screen (might be a different column).
1967 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1969 INT s = es->selection_start;
1970 INT e = es->selection_end;
1971 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1972 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1973 INT x = (short)LOWORD(pos);
1974 INT y = (short)HIWORD(pos);
1976 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1977 if (!extend)
1978 s = e;
1979 EDIT_EM_SetSel(es, s, e, after_wrap);
1980 EDIT_EM_ScrollCaret(es);
1984 /*********************************************************************
1986 * EDIT_MoveWordBackward
1989 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1991 INT s = es->selection_start;
1992 INT e = es->selection_end;
1993 INT l;
1994 INT ll;
1995 INT li;
1997 l = EDIT_EM_LineFromChar(es, e);
1998 ll = EDIT_EM_LineLength(es, e);
1999 li = EDIT_EM_LineIndex(es, l);
2000 if (e - li == 0) {
2001 if (l) {
2002 li = EDIT_EM_LineIndex(es, l - 1);
2003 e = li + EDIT_EM_LineLength(es, li);
2005 } else {
2006 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2008 if (!extend)
2009 s = e;
2010 EDIT_EM_SetSel(es, s, e, FALSE);
2011 EDIT_EM_ScrollCaret(es);
2015 /*********************************************************************
2017 * EDIT_MoveWordForward
2020 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2022 INT s = es->selection_start;
2023 INT e = es->selection_end;
2024 INT l;
2025 INT ll;
2026 INT li;
2028 l = EDIT_EM_LineFromChar(es, e);
2029 ll = EDIT_EM_LineLength(es, e);
2030 li = EDIT_EM_LineIndex(es, l);
2031 if (e - li == ll) {
2032 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2033 e = EDIT_EM_LineIndex(es, l + 1);
2034 } else {
2035 e = li + EDIT_CallWordBreakProc(es,
2036 li, e - li + 1, ll, WB_RIGHT);
2038 if (!extend)
2039 s = e;
2040 EDIT_EM_SetSel(es, s, e, FALSE);
2041 EDIT_EM_ScrollCaret(es);
2045 /*********************************************************************
2047 * EDIT_PaintText
2050 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2052 COLORREF BkColor;
2053 COLORREF TextColor;
2054 LOGFONTW underline_font;
2055 HFONT hUnderline = 0;
2056 HFONT old_font = 0;
2057 INT ret;
2058 INT li;
2059 INT BkMode;
2060 SIZE size;
2062 if (!count)
2063 return 0;
2064 BkMode = GetBkMode(dc);
2065 BkColor = GetBkColor(dc);
2066 TextColor = GetTextColor(dc);
2067 if (rev) {
2068 if (es->composition_len == 0)
2070 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2071 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2072 SetBkMode( dc, OPAQUE);
2074 else
2076 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2077 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2078 underline_font.lfUnderline = TRUE;
2079 hUnderline = CreateFontIndirectW(&underline_font);
2080 old_font = SelectObject(dc,hUnderline);
2083 li = EDIT_EM_LineIndex(es, line);
2084 if (es->style & ES_MULTILINE) {
2085 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2086 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2087 } else {
2088 TextOutW(dc, x, y, es->text + li + col, count);
2089 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2090 ret = size.cx;
2092 if (rev) {
2093 if (es->composition_len == 0)
2095 SetBkColor(dc, BkColor);
2096 SetTextColor(dc, TextColor);
2097 SetBkMode( dc, BkMode);
2099 else
2101 if (old_font)
2102 SelectObject(dc,old_font);
2103 if (hUnderline)
2104 DeleteObject(hUnderline);
2107 return ret;
2111 /*********************************************************************
2113 * EDIT_PaintLine
2116 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2118 INT s = 0;
2119 INT e = 0;
2120 INT li = 0;
2121 INT ll = 0;
2122 INT x;
2123 INT y;
2124 LRESULT pos;
2125 SCRIPT_STRING_ANALYSIS ssa;
2127 if (es->style & ES_MULTILINE) {
2128 INT vlc = get_vertical_line_count(es);
2130 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2131 return;
2132 } else if (line)
2133 return;
2135 TRACE("line=%d\n", line);
2137 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2138 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2139 x = (short)LOWORD(pos);
2140 y = (short)HIWORD(pos);
2142 if (es->style & ES_MULTILINE)
2144 int line_idx = line;
2145 x = -es->x_offset;
2146 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2148 LINEDEF *line_def = es->first_line_def;
2149 int w, lw;
2151 while (line_def && line_idx)
2153 line_def = line_def->next;
2154 line_idx--;
2156 w = es->format_rect.right - es->format_rect.left;
2157 lw = line_def->width;
2159 if (es->style & ES_RIGHT)
2160 x = w - (lw - x);
2161 else if (es->style & ES_CENTER)
2162 x += (w - lw) / 2;
2164 x += es->format_rect.left;
2167 if (rev)
2169 li = EDIT_EM_LineIndex(es, line);
2170 ll = EDIT_EM_LineLength(es, li);
2171 s = min(es->selection_start, es->selection_end);
2172 e = max(es->selection_start, es->selection_end);
2173 s = min(li + ll, max(li, s));
2174 e = min(li + ll, max(li, e));
2177 if (ssa)
2178 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2179 else if (rev && (s != e) &&
2180 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2181 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2182 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2183 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2184 } else
2185 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2187 if (es->cue_banner_text && es->text_length == 0 && (!(es->flags & EF_FOCUSED) || es->cue_banner_draw_focused))
2189 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
2190 TextOutW(dc, x, y, es->cue_banner_text, lstrlenW(es->cue_banner_text));
2195 /*********************************************************************
2197 * EDIT_AdjustFormatRect
2199 * Adjusts the format rectangle for the current font and the
2200 * current client rectangle.
2203 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2205 RECT ClientRect;
2207 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2208 if (es->style & ES_MULTILINE)
2210 INT fw, vlc, max_x_offset, max_y_offset;
2212 vlc = get_vertical_line_count(es);
2213 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2215 /* correct es->x_offset */
2216 fw = es->format_rect.right - es->format_rect.left;
2217 max_x_offset = es->text_width - fw;
2218 if(max_x_offset < 0) max_x_offset = 0;
2219 if(es->x_offset > max_x_offset)
2220 es->x_offset = max_x_offset;
2222 /* correct es->y_offset */
2223 max_y_offset = es->line_count - vlc;
2224 if(max_y_offset < 0) max_y_offset = 0;
2225 if(es->y_offset > max_y_offset)
2226 es->y_offset = max_y_offset;
2228 /* force scroll info update */
2229 EDIT_UpdateScrollInfo(es);
2231 else
2232 /* Windows doesn't care to fix text placement for SL controls */
2233 es->format_rect.bottom = es->format_rect.top + es->line_height;
2235 /* Always stay within the client area */
2236 GetClientRect(es->hwndSelf, &ClientRect);
2237 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2239 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2240 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2242 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2246 /*********************************************************************
2248 * EDIT_SetRectNP
2250 * note: this is not (exactly) the handler called on EM_SETRECTNP
2251 * it is also used to set the rect of a single line control
2254 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2256 LONG_PTR ExStyle;
2257 INT bw, bh;
2258 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2260 CopyRect(&es->format_rect, rc);
2262 if (ExStyle & WS_EX_CLIENTEDGE) {
2263 es->format_rect.left++;
2264 es->format_rect.right--;
2266 if (es->format_rect.bottom - es->format_rect.top
2267 >= es->line_height + 2)
2269 es->format_rect.top++;
2270 es->format_rect.bottom--;
2273 else if (es->style & WS_BORDER) {
2274 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2275 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2276 InflateRect(&es->format_rect, -bw, 0);
2277 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2278 InflateRect(&es->format_rect, 0, -bh);
2281 es->format_rect.left += es->left_margin;
2282 es->format_rect.right -= es->right_margin;
2283 EDIT_AdjustFormatRect(es);
2287 /*********************************************************************
2289 * EM_CHARFROMPOS
2291 * returns line number (not index) in high-order word of result.
2292 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2293 * to Richedit, not to the edit control. Original documentation is valid.
2294 * FIXME: do the specs mean to return -1 if outside client area or
2295 * if outside formatting rectangle ???
2298 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2300 POINT pt;
2301 RECT rc;
2302 INT index;
2304 pt.x = x;
2305 pt.y = y;
2306 GetClientRect(es->hwndSelf, &rc);
2307 if (!PtInRect(&rc, pt))
2308 return -1;
2310 index = EDIT_CharFromPos(es, x, y, NULL);
2311 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2315 /*********************************************************************
2317 * EM_FMTLINES
2319 * Enable or disable soft breaks.
2321 * This means: insert or remove the soft linebreak character (\r\r\n).
2322 * Take care to check if the text still fits the buffer after insertion.
2323 * If not, notify with EN_ERRSPACE.
2326 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2328 es->flags &= ~EF_USE_SOFTBRK;
2329 if (add_eol) {
2330 es->flags |= EF_USE_SOFTBRK;
2331 FIXME("soft break enabled, not implemented\n");
2333 return add_eol;
2337 /*********************************************************************
2339 * EM_GETHANDLE
2341 * Hopefully this won't fire back at us.
2342 * We always start with a fixed buffer in the local heap.
2343 * Despite of the documentation says that the local heap is used
2344 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2345 * buffer on the local heap.
2348 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2350 if (!(es->style & ES_MULTILINE))
2351 return 0;
2353 EDIT_UnlockBuffer(es, TRUE);
2355 /* The text buffer handle belongs to the app */
2356 es->hlocapp = es->hloc32W;
2358 TRACE("Returning %p, LocalSize() = %ld\n", es->hlocapp, LocalSize(es->hlocapp));
2359 return es->hlocapp;
2363 /*********************************************************************
2365 * EM_GETLINE
2368 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst)
2370 INT line_len, dst_len;
2371 LPWSTR src;
2372 INT i;
2374 if (es->style & ES_MULTILINE)
2376 if (line >= es->line_count)
2377 return 0;
2379 else
2380 line = 0;
2382 i = EDIT_EM_LineIndex(es, line);
2383 src = es->text + i;
2384 line_len = EDIT_EM_LineLength(es, i);
2385 dst_len = *(WORD *)dst;
2387 if (dst_len <= line_len)
2389 memcpy(dst, src, dst_len * sizeof(WCHAR));
2390 return dst_len;
2392 else /* Append 0 if enough space */
2394 memcpy(dst, src, line_len * sizeof(WCHAR));
2395 dst[line_len] = 0;
2396 return line_len;
2401 /*********************************************************************
2403 * EM_GETSEL
2406 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2408 UINT s = es->selection_start;
2409 UINT e = es->selection_end;
2411 ORDER_UINT(s, e);
2412 if (start)
2413 *start = s;
2414 if (end)
2415 *end = e;
2416 return MAKELONG(s, e);
2420 /*********************************************************************
2422 * EM_REPLACESEL
2424 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2427 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2428 BOOL send_update, BOOL honor_limit)
2430 UINT tl = get_text_length(es);
2431 UINT utl;
2432 UINT s;
2433 UINT e;
2434 UINT i;
2435 UINT size;
2436 LPWSTR p;
2437 HRGN hrgn = 0;
2438 LPWSTR buf = NULL;
2439 UINT bufl;
2441 TRACE("%s, can_undo %d, send_update %d\n",
2442 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2444 s = es->selection_start;
2445 e = es->selection_end;
2447 EDIT_InvalidateUniscribeData(es);
2448 if ((s == e) && !strl)
2449 return;
2451 ORDER_UINT(s, e);
2453 size = tl - (e - s) + strl;
2454 if (!size)
2455 es->text_width = 0;
2457 /* Issue the EN_MAXTEXT notification and continue with replacing text
2458 * so that buffer limit is honored. */
2459 if ((honor_limit) && (size > es->buffer_limit)) {
2460 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2461 /* Buffer limit can be smaller than the actual length of text in combobox */
2462 if (es->buffer_limit < (tl - (e-s)))
2463 strl = 0;
2464 else
2465 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2468 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2469 return;
2471 if (e != s) {
2472 /* there is something to be deleted */
2473 TRACE("deleting stuff.\n");
2474 bufl = e - s;
2475 buf = heap_alloc((bufl + 1) * sizeof(WCHAR));
2476 if (!buf) return;
2477 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2478 buf[bufl] = 0; /* ensure 0 termination */
2479 /* now delete */
2480 lstrcpyW(es->text + s, es->text + e);
2481 text_buffer_changed(es);
2483 if (strl) {
2484 /* there is an insertion */
2485 tl = get_text_length(es);
2486 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));
2487 for (p = es->text + tl ; p >= es->text + s ; p--)
2488 p[strl] = p[0];
2489 for (i = 0 , p = es->text + s ; i < strl ; i++)
2490 p[i] = lpsz_replace[i];
2491 if(es->style & ES_UPPERCASE)
2492 CharUpperBuffW(p, strl);
2493 else if(es->style & ES_LOWERCASE)
2494 CharLowerBuffW(p, strl);
2495 text_buffer_changed(es);
2497 if (es->style & ES_MULTILINE)
2499 INT st = min(es->selection_start, es->selection_end);
2500 INT vlc = get_vertical_line_count(es);
2502 hrgn = CreateRectRgn(0, 0, 0, 0);
2503 EDIT_BuildLineDefs_ML(es, st, st + strl,
2504 strl - abs(es->selection_end - es->selection_start), hrgn);
2505 /* if text is too long undo all changes */
2506 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2507 if (strl)
2508 lstrcpyW(es->text + e, es->text + e + strl);
2509 if (e != s)
2510 for (i = 0 , p = es->text ; i < e - s ; i++)
2511 p[i + s] = buf[i];
2512 text_buffer_changed(es);
2513 EDIT_BuildLineDefs_ML(es, s, e,
2514 abs(es->selection_end - es->selection_start) - strl, hrgn);
2515 strl = 0;
2516 e = s;
2517 SetRectRgn(hrgn, 0, 0, 0, 0);
2518 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2521 else {
2522 INT fw = es->format_rect.right - es->format_rect.left;
2523 EDIT_InvalidateUniscribeData(es);
2524 EDIT_CalcLineWidth_SL(es);
2525 /* remove chars that don't fit */
2526 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2527 while ((es->text_width > fw) && s + strl >= s) {
2528 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2529 strl--;
2530 es->text_length = -1;
2531 EDIT_InvalidateUniscribeData(es);
2532 EDIT_CalcLineWidth_SL(es);
2534 text_buffer_changed(es);
2535 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2539 if (e != s) {
2540 if (can_undo) {
2541 utl = lstrlenW(es->undo_text);
2542 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2543 /* undo-buffer is extended to the right */
2544 EDIT_MakeUndoFit(es, utl + e - s);
2545 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2546 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2547 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2548 /* undo-buffer is extended to the left */
2549 EDIT_MakeUndoFit(es, utl + e - s);
2550 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2551 p[e - s] = p[0];
2552 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2553 p[i] = buf[i];
2554 es->undo_position = s;
2555 } else {
2556 /* new undo-buffer */
2557 EDIT_MakeUndoFit(es, e - s);
2558 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2559 es->undo_text[e - s] = 0; /* ensure 0 termination */
2560 es->undo_position = s;
2562 /* any deletion makes the old insertion-undo invalid */
2563 es->undo_insert_count = 0;
2564 } else
2565 EDIT_EM_EmptyUndoBuffer(es);
2567 if (strl) {
2568 if (can_undo) {
2569 if ((s == es->undo_position) ||
2570 ((es->undo_insert_count) &&
2571 (s == es->undo_position + es->undo_insert_count)))
2573 * insertion is new and at delete position or
2574 * an extension to either left or right
2576 es->undo_insert_count += strl;
2577 else {
2578 /* new insertion undo */
2579 es->undo_position = s;
2580 es->undo_insert_count = strl;
2581 /* new insertion makes old delete-buffer invalid */
2582 *es->undo_text = '\0';
2584 } else
2585 EDIT_EM_EmptyUndoBuffer(es);
2588 heap_free(buf);
2590 s += strl;
2592 /* If text has been deleted and we're right or center aligned then scroll rightward */
2593 if (es->style & (ES_RIGHT | ES_CENTER))
2595 INT delta = strl - abs(es->selection_end - es->selection_start);
2597 if (delta < 0 && es->x_offset)
2599 if (abs(delta) > es->x_offset)
2600 es->x_offset = 0;
2601 else
2602 es->x_offset += delta;
2606 EDIT_EM_SetSel(es, s, s, FALSE);
2607 es->flags |= EF_MODIFIED;
2608 if (send_update) es->flags |= EF_UPDATE;
2609 if (hrgn)
2611 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2612 DeleteObject(hrgn);
2614 else
2615 EDIT_UpdateText(es, NULL, TRUE);
2617 EDIT_EM_ScrollCaret(es);
2619 /* force scroll info update */
2620 EDIT_UpdateScrollInfo(es);
2623 if(send_update || (es->flags & EF_UPDATE))
2625 es->flags &= ~EF_UPDATE;
2626 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2628 EDIT_InvalidateUniscribeData(es);
2632 /*********************************************************************
2634 * EM_SETHANDLE
2636 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2639 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2641 if (!(es->style & ES_MULTILINE))
2642 return;
2644 if (!hloc)
2645 return;
2647 EDIT_UnlockBuffer(es, TRUE);
2649 es->hloc32W = hloc;
2650 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2652 /* The text buffer handle belongs to the control */
2653 es->hlocapp = NULL;
2655 EDIT_LockBuffer(es);
2656 text_buffer_changed(es);
2658 es->x_offset = es->y_offset = 0;
2659 es->selection_start = es->selection_end = 0;
2660 EDIT_EM_EmptyUndoBuffer(es);
2661 es->flags &= ~EF_MODIFIED;
2662 es->flags &= ~EF_UPDATE;
2663 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2664 EDIT_UpdateText(es, NULL, TRUE);
2665 EDIT_EM_ScrollCaret(es);
2666 /* force scroll info update */
2667 EDIT_UpdateScrollInfo(es);
2671 /*********************************************************************
2673 * EM_SETLIMITTEXT
2675 * NOTE: this version currently implements WinNT limits
2678 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2680 if (!limit) limit = ~0u;
2681 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2682 es->buffer_limit = limit;
2685 static BOOL is_cjk(HDC dc)
2687 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2688 FONTSIGNATURE fs;
2690 switch (GdiGetCodePage(dc)) {
2691 case 932: case 936: case 949: case 950: case 1361:
2692 return TRUE;
2693 default:
2694 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2695 (fs.fsCsb[0] & FS_DBCS_MASK));
2699 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2701 int margin;
2702 if (side_bearing < 0)
2703 margin = min(-side_bearing, width/2);
2704 else
2705 margin = 0;
2706 return margin;
2709 struct char_width_info {
2710 INT min_lsb, min_rsb, unknown;
2713 /* Undocumented gdi32 export */
2714 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2716 /*********************************************************************
2718 * EM_SETMARGINS
2720 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2721 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2722 * margin according to the textmetrics of the current font.
2724 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2725 * equal to or larger than a certain size. The empty client rect is treated as
2726 * 80 pixels width.
2728 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2729 WORD left, WORD right, BOOL repaint)
2731 TEXTMETRICW tm;
2732 INT default_left_margin = 0; /* in pixels */
2733 INT default_right_margin = 0; /* in pixels */
2735 /* Set the default margins depending on the font */
2736 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2737 HDC dc = GetDC(es->hwndSelf);
2738 HFONT old_font = SelectObject(dc, es->font);
2739 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2740 RECT rc;
2742 /* The default margins are only non zero for TrueType or Vector fonts */
2743 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2744 struct char_width_info width_info;
2746 if (is_cjk(dc) && GetCharWidthInfo(dc, &width_info))
2748 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2749 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2751 else
2753 default_left_margin = width / 2;
2754 default_right_margin = width / 2;
2757 GetClientRect(es->hwndSelf, &rc);
2758 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2759 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2760 default_left_margin = es->left_margin;
2761 default_right_margin = es->right_margin;
2764 SelectObject(dc, old_font);
2765 ReleaseDC(es->hwndSelf, dc);
2768 if (action & EC_LEFTMARGIN) {
2769 es->format_rect.left -= es->left_margin;
2770 if (left != EC_USEFONTINFO)
2771 es->left_margin = left;
2772 else
2773 es->left_margin = default_left_margin;
2774 es->format_rect.left += es->left_margin;
2777 if (action & EC_RIGHTMARGIN) {
2778 es->format_rect.right += es->right_margin;
2779 if (right != EC_USEFONTINFO)
2780 es->right_margin = right;
2781 else
2782 es->right_margin = default_right_margin;
2783 es->format_rect.right -= es->right_margin;
2786 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2787 EDIT_AdjustFormatRect(es);
2788 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2791 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2795 /*********************************************************************
2797 * EM_SETPASSWORDCHAR
2800 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2802 LONG style;
2804 if (es->style & ES_MULTILINE)
2805 return;
2807 if (es->password_char == c)
2808 return;
2810 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2811 es->password_char = c;
2812 if (c) {
2813 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2814 es->style |= ES_PASSWORD;
2815 } else {
2816 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2817 es->style &= ~ES_PASSWORD;
2819 EDIT_InvalidateUniscribeData(es);
2820 EDIT_UpdateText(es, NULL, TRUE);
2824 /*********************************************************************
2826 * EM_SETTABSTOPS
2829 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2831 if (!(es->style & ES_MULTILINE))
2832 return FALSE;
2833 heap_free(es->tabs);
2834 es->tabs_count = count;
2835 if (!count)
2836 es->tabs = NULL;
2837 else {
2838 es->tabs = heap_alloc(count * sizeof(INT));
2839 memcpy(es->tabs, tabs, count * sizeof(INT));
2841 EDIT_InvalidateUniscribeData(es);
2842 return TRUE;
2846 /*********************************************************************
2848 * EM_SETWORDBREAKPROC
2851 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, EDITWORDBREAKPROCW wbp)
2853 if (es->word_break_proc == wbp)
2854 return;
2856 es->word_break_proc = wbp;
2858 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2859 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2860 EDIT_UpdateText(es, NULL, TRUE);
2865 /*********************************************************************
2867 * EM_UNDO / WM_UNDO
2870 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2872 INT ulength;
2873 LPWSTR utext;
2875 /* As per MSDN spec, for a single-line edit control,
2876 the return value is always TRUE */
2877 if( es->style & ES_READONLY )
2878 return !(es->style & ES_MULTILINE);
2880 ulength = lstrlenW(es->undo_text);
2882 utext = heap_alloc((ulength + 1) * sizeof(WCHAR));
2884 lstrcpyW(utext, es->undo_text);
2886 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2887 es->undo_insert_count, debugstr_w(utext));
2889 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2890 EDIT_EM_EmptyUndoBuffer(es);
2891 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
2892 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2893 /* send the notification after the selection start and end are set */
2894 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2895 EDIT_EM_ScrollCaret(es);
2896 heap_free(utext);
2898 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2899 es->undo_insert_count, debugstr_w(es->undo_text));
2900 return TRUE;
2904 /* Helper function for WM_CHAR
2906 * According to an MSDN blog article titled "Just because you're a control
2907 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2908 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2909 * a dialog box or not.
2911 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2913 return (es->flags & EF_DIALOGMODE);
2917 /*********************************************************************
2919 * WM_PASTE
2922 static void EDIT_WM_Paste(EDITSTATE *es)
2924 HGLOBAL hsrc;
2925 LPWSTR src, ptr;
2926 int len;
2928 /* Protect read-only edit control from modification */
2929 if(es->style & ES_READONLY)
2930 return;
2932 OpenClipboard(es->hwndSelf);
2933 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2934 src = GlobalLock(hsrc);
2935 len = lstrlenW(src);
2936 /* Protect single-line edit against pasting new line character */
2937 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
2938 len = ptr - src;
2939 if (len && src[len - 1] == '\r')
2940 --len;
2942 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
2943 GlobalUnlock(hsrc);
2945 else if (es->style & ES_PASSWORD) {
2946 /* clear selected text in password edit box even with empty clipboard */
2947 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2949 CloseClipboard();
2953 /*********************************************************************
2955 * WM_COPY
2958 static void EDIT_WM_Copy(EDITSTATE *es)
2960 INT s = min(es->selection_start, es->selection_end);
2961 INT e = max(es->selection_start, es->selection_end);
2962 HGLOBAL hdst;
2963 LPWSTR dst;
2964 DWORD len;
2966 if (e == s) return;
2968 len = e - s;
2969 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2970 dst = GlobalLock(hdst);
2971 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2972 dst[len] = 0; /* ensure 0 termination */
2973 TRACE("%s\n", debugstr_w(dst));
2974 GlobalUnlock(hdst);
2975 OpenClipboard(es->hwndSelf);
2976 EmptyClipboard();
2977 SetClipboardData(CF_UNICODETEXT, hdst);
2978 CloseClipboard();
2982 /*********************************************************************
2984 * WM_CLEAR
2987 static inline void EDIT_WM_Clear(EDITSTATE *es)
2989 /* Protect read-only edit control from modification */
2990 if(es->style & ES_READONLY)
2991 return;
2993 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2997 /*********************************************************************
2999 * WM_CUT
3002 static inline void EDIT_WM_Cut(EDITSTATE *es)
3004 EDIT_WM_Copy(es);
3005 EDIT_WM_Clear(es);
3009 /*********************************************************************
3011 * WM_CHAR
3014 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3016 BOOL control;
3018 control = GetKeyState(VK_CONTROL) & 0x8000;
3020 switch (c) {
3021 case '\r':
3022 /* If it's not a multiline edit box, it would be ignored below.
3023 * For multiline edit without ES_WANTRETURN, we have to make a
3024 * special case.
3026 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3027 if (EDIT_IsInsideDialog(es))
3028 break;
3029 case '\n':
3030 if (es->style & ES_MULTILINE) {
3031 if (es->style & ES_READONLY) {
3032 EDIT_MoveHome(es, FALSE, FALSE);
3033 EDIT_MoveDown_ML(es, FALSE);
3034 } else
3035 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3037 break;
3038 case '\t':
3039 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3041 if (EDIT_IsInsideDialog(es))
3042 break;
3043 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3045 break;
3046 case VK_BACK:
3047 if (!(es->style & ES_READONLY) && !control) {
3048 if (es->selection_start != es->selection_end)
3049 EDIT_WM_Clear(es);
3050 else {
3051 /* delete character left of caret */
3052 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3053 EDIT_MoveBackward(es, TRUE);
3054 EDIT_WM_Clear(es);
3057 break;
3058 case 0x03: /* ^C */
3059 if (!(es->style & ES_PASSWORD))
3060 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3061 break;
3062 case 0x16: /* ^V */
3063 if (!(es->style & ES_READONLY))
3064 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3065 break;
3066 case 0x18: /* ^X */
3067 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3068 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3069 break;
3070 case 0x1A: /* ^Z */
3071 if (!(es->style & ES_READONLY))
3072 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3073 break;
3075 default:
3076 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3077 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3078 break;
3080 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3081 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3082 break;
3084 return 1;
3088 /*********************************************************************
3090 * EDIT_ContextMenuCommand
3093 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3095 switch (id) {
3096 case EM_UNDO:
3097 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3098 break;
3099 case WM_CUT:
3100 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3101 break;
3102 case WM_COPY:
3103 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3104 break;
3105 case WM_PASTE:
3106 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3107 break;
3108 case WM_CLEAR:
3109 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3110 break;
3111 case EM_SETSEL:
3112 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3113 break;
3114 default:
3115 ERR("unknown menu item, please report\n");
3116 break;
3121 /*********************************************************************
3123 * WM_CONTEXTMENU
3125 * Note: the resource files resource/sysres_??.rc cannot define a
3126 * single popup menu. Hence we use a (dummy) menubar
3127 * containing the single popup menu as its first item.
3129 * FIXME: the message identifiers have been chosen arbitrarily,
3130 * hence we use MF_BYPOSITION.
3131 * We might as well use the "real" values (anybody knows ?)
3132 * The menu definition is in resources/sysres_??.rc.
3133 * Once these are OK, we better use MF_BYCOMMAND here
3134 * (as we do in EDIT_WM_Command()).
3137 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3139 HMENU menu = LoadMenuA(GetModuleHandleA("user32.dll"), "EDITMENU");
3140 HMENU popup = GetSubMenu(menu, 0);
3141 UINT start = es->selection_start;
3142 UINT end = es->selection_end;
3143 UINT cmd;
3144 POINT pt;
3146 ORDER_UINT(start, end);
3148 /* undo */
3149 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3150 /* cut */
3151 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3152 /* copy */
3153 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3154 /* paste */
3155 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3156 /* delete */
3157 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3158 /* select all */
3159 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3161 pt.x = x;
3162 pt.y = y;
3164 if (pt.x == -1 && pt.y == -1) /* passed via VK_APPS press/release */
3166 RECT rc;
3168 /* Windows places the menu at the edit's center in this case */
3169 GetClientRect(es->hwndSelf, &rc);
3170 pt.x = rc.left + (rc.right - rc.left) / 2;
3171 pt.y = rc.top + (rc.bottom - rc.top) / 2;
3172 ClientToScreen(es->hwndSelf, &pt);
3175 if (!(es->flags & EF_FOCUSED))
3176 SetFocus(es->hwndSelf);
3178 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3179 pt.x, pt.y, 0, es->hwndSelf, NULL);
3181 if (cmd)
3182 EDIT_ContextMenuCommand(es, cmd);
3184 DestroyMenu(menu);
3188 /*********************************************************************
3190 * WM_GETTEXT
3193 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst)
3195 if (!count)
3196 return 0;
3198 lstrcpynW(dst, es->text, count);
3199 return lstrlenW(dst);
3202 /*********************************************************************
3204 * EDIT_CheckCombo
3207 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3209 HWND hLBox = es->hwndListBox;
3210 HWND hCombo;
3211 BOOL bDropped;
3212 int nEUI;
3214 if (!hLBox)
3215 return FALSE;
3217 hCombo = GetParent(es->hwndSelf);
3218 bDropped = TRUE;
3219 nEUI = 0;
3221 TRACE("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3223 if (key == VK_UP || key == VK_DOWN)
3225 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3226 nEUI = 1;
3228 if (msg == WM_KEYDOWN || nEUI)
3229 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3232 switch (msg)
3234 case WM_KEYDOWN:
3235 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3237 /* make sure ComboLBox pops up */
3238 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3239 key = VK_F4;
3240 nEUI = 2;
3243 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3244 break;
3246 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3247 if (nEUI)
3248 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3249 else
3250 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3251 break;
3254 if (nEUI == 2)
3255 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3257 return TRUE;
3261 /*********************************************************************
3263 * WM_KEYDOWN
3265 * Handling of special keys that don't produce a WM_CHAR
3266 * (i.e. non-printable keys) & Backspace & Delete
3269 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3271 BOOL shift;
3272 BOOL control;
3274 if (GetKeyState(VK_MENU) & 0x8000)
3275 return 0;
3277 shift = GetKeyState(VK_SHIFT) & 0x8000;
3278 control = GetKeyState(VK_CONTROL) & 0x8000;
3280 switch (key) {
3281 case VK_F4:
3282 case VK_UP:
3283 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3284 break;
3286 /* fall through */
3287 case VK_LEFT:
3288 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3289 EDIT_MoveUp_ML(es, shift);
3290 else
3291 if (control)
3292 EDIT_MoveWordBackward(es, shift);
3293 else
3294 EDIT_MoveBackward(es, shift);
3295 break;
3296 case VK_DOWN:
3297 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3298 break;
3299 /* fall through */
3300 case VK_RIGHT:
3301 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3302 EDIT_MoveDown_ML(es, shift);
3303 else if (control)
3304 EDIT_MoveWordForward(es, shift);
3305 else
3306 EDIT_MoveForward(es, shift);
3307 break;
3308 case VK_HOME:
3309 EDIT_MoveHome(es, shift, control);
3310 break;
3311 case VK_END:
3312 EDIT_MoveEnd(es, shift, control);
3313 break;
3314 case VK_PRIOR:
3315 if (es->style & ES_MULTILINE)
3316 EDIT_MovePageUp_ML(es, shift);
3317 else
3318 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3319 break;
3320 case VK_NEXT:
3321 if (es->style & ES_MULTILINE)
3322 EDIT_MovePageDown_ML(es, shift);
3323 else
3324 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3325 break;
3326 case VK_DELETE:
3327 if (!(es->style & ES_READONLY) && !(shift && control)) {
3328 if (es->selection_start != es->selection_end) {
3329 if (shift)
3330 EDIT_WM_Cut(es);
3331 else
3332 EDIT_WM_Clear(es);
3333 } else {
3334 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3335 if (shift)
3336 /* delete character left of caret */
3337 EDIT_MoveBackward(es, TRUE);
3338 else if (control)
3339 /* delete to end of line */
3340 EDIT_MoveEnd(es, TRUE, FALSE);
3341 else
3342 /* delete character right of caret */
3343 EDIT_MoveForward(es, TRUE);
3344 EDIT_WM_Clear(es);
3347 break;
3348 case VK_INSERT:
3349 if (shift) {
3350 if (!(es->style & ES_READONLY))
3351 EDIT_WM_Paste(es);
3352 } else if (control)
3353 EDIT_WM_Copy(es);
3354 break;
3355 case VK_RETURN:
3356 /* If the edit doesn't want the return send a message to the default object */
3357 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3359 DWORD dw;
3361 if (!EDIT_IsInsideDialog(es)) break;
3362 if (control) break;
3363 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3364 if (HIWORD(dw) == DC_HASDEFID)
3366 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3367 if (hwDefCtrl)
3369 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3370 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3374 break;
3375 case VK_ESCAPE:
3376 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3377 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3378 break;
3379 case VK_TAB:
3380 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3381 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3382 break;
3383 case 'A':
3384 if (control)
3386 if (EDIT_EM_SetSel(es, 0, get_text_length(es), FALSE))
3388 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3389 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3392 break;
3394 return TRUE;
3398 /*********************************************************************
3400 * WM_KILLFOCUS
3403 static LRESULT EDIT_WM_KillFocus(HTHEME theme, EDITSTATE *es)
3405 UINT flags = RDW_INVALIDATE | RDW_UPDATENOW;
3407 es->flags &= ~EF_FOCUSED;
3408 DestroyCaret();
3409 if (!(es->style & ES_NOHIDESEL))
3410 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3411 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3412 /* Throw away left over scroll when we lose focus */
3413 es->wheelDeltaRemainder = 0;
3415 if (theme)
3416 flags |= RDW_FRAME;
3418 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3419 return 0;
3423 /*********************************************************************
3425 * WM_LBUTTONDBLCLK
3427 * The caret position has been set on the WM_LBUTTONDOWN message
3430 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3432 INT s;
3433 INT e = es->selection_end;
3434 INT l;
3435 INT li;
3436 INT ll;
3438 es->bCaptureState = TRUE;
3439 SetCapture(es->hwndSelf);
3441 l = EDIT_EM_LineFromChar(es, e);
3442 li = EDIT_EM_LineIndex(es, l);
3443 ll = EDIT_EM_LineLength(es, e);
3444 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3445 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3446 EDIT_EM_SetSel(es, s, e, FALSE);
3447 EDIT_EM_ScrollCaret(es);
3448 es->region_posx = es->region_posy = 0;
3449 SetTimer(es->hwndSelf, 0, 100, NULL);
3450 return 0;
3454 /*********************************************************************
3456 * WM_LBUTTONDOWN
3459 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3461 INT e;
3462 BOOL after_wrap;
3464 es->bCaptureState = TRUE;
3465 SetCapture(es->hwndSelf);
3466 EDIT_ConfinePoint(es, &x, &y);
3467 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3468 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3469 EDIT_EM_ScrollCaret(es);
3470 es->region_posx = es->region_posy = 0;
3471 SetTimer(es->hwndSelf, 0, 100, NULL);
3473 if (!(es->flags & EF_FOCUSED))
3474 SetFocus(es->hwndSelf);
3476 return 0;
3480 /*********************************************************************
3482 * WM_LBUTTONUP
3485 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3487 if (es->bCaptureState) {
3488 KillTimer(es->hwndSelf, 0);
3489 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3491 es->bCaptureState = FALSE;
3492 return 0;
3496 /*********************************************************************
3498 * WM_MBUTTONDOWN
3501 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3503 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3504 return 0;
3508 /*********************************************************************
3510 * WM_MOUSEMOVE
3513 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3515 INT e;
3516 BOOL after_wrap;
3517 INT prex, prey;
3519 /* If the mouse has been captured by process other than the edit control itself,
3520 * the windows edit controls will not select the strings with mouse move.
3522 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3523 return 0;
3526 * FIXME: gotta do some scrolling if outside client
3527 * area. Maybe reset the timer ?
3529 prex = x; prey = y;
3530 EDIT_ConfinePoint(es, &x, &y);
3531 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3532 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3533 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3534 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3535 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3536 return 0;
3540 /*********************************************************************
3542 * WM_PAINT
3545 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3547 PAINTSTRUCT ps;
3548 INT i;
3549 HDC dc;
3550 HFONT old_font = 0;
3551 RECT rc;
3552 RECT rcClient;
3553 RECT rcLine;
3554 RECT rcRgn;
3555 HBRUSH brush;
3556 HBRUSH old_brush;
3557 INT bw, bh;
3558 BOOL rev = es->bEnableState &&
3559 ((es->flags & EF_FOCUSED) ||
3560 (es->style & ES_NOHIDESEL));
3561 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3563 /* The dc we use for calculating may not be the one we paint into.
3564 This is the safest action. */
3565 EDIT_InvalidateUniscribeData(es);
3566 GetClientRect(es->hwndSelf, &rcClient);
3568 /* get the background brush */
3569 brush = EDIT_NotifyCtlColor(es, dc);
3571 /* paint the border and the background */
3572 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3574 if(es->style & WS_BORDER) {
3575 bw = GetSystemMetrics(SM_CXBORDER);
3576 bh = GetSystemMetrics(SM_CYBORDER);
3577 rc = rcClient;
3578 if(es->style & ES_MULTILINE) {
3579 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3580 if(es->style & WS_VSCROLL) rc.right+=bw;
3583 /* Draw the frame. Same code as in nonclient.c */
3584 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3585 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3586 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3587 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3588 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3589 SelectObject(dc, old_brush);
3591 /* Keep the border clean */
3592 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3593 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3596 GetClipBox(dc, &rc);
3597 FillRect(dc, &rc, brush);
3599 IntersectClipRect(dc, es->format_rect.left,
3600 es->format_rect.top,
3601 es->format_rect.right,
3602 es->format_rect.bottom);
3603 if (es->style & ES_MULTILINE) {
3604 rc = rcClient;
3605 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3607 if (es->font)
3608 old_font = SelectObject(dc, es->font);
3610 if (!es->bEnableState)
3611 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3612 GetClipBox(dc, &rcRgn);
3613 if (es->style & ES_MULTILINE) {
3614 INT vlc = get_vertical_line_count(es);
3615 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3616 EDIT_UpdateUniscribeData(es, dc, i);
3617 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3618 if (IntersectRect(&rc, &rcRgn, &rcLine))
3619 EDIT_PaintLine(es, dc, i, rev);
3621 } else {
3622 EDIT_UpdateUniscribeData(es, dc, 0);
3623 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3624 if (IntersectRect(&rc, &rcRgn, &rcLine))
3625 EDIT_PaintLine(es, dc, 0, rev);
3627 if (es->font)
3628 SelectObject(dc, old_font);
3630 if (!hdc)
3631 EndPaint(es->hwndSelf, &ps);
3634 static void EDIT_WM_NCPaint(HWND hwnd, HRGN region)
3636 DWORD exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3637 HTHEME theme = GetWindowTheme(hwnd);
3638 HRGN cliprgn = region;
3640 if (theme && exStyle & WS_EX_CLIENTEDGE)
3642 HDC dc;
3643 RECT r;
3644 int cxEdge = GetSystemMetrics(SM_CXEDGE),
3645 cyEdge = GetSystemMetrics(SM_CYEDGE);
3646 const int part = EP_EDITTEXT;
3647 int state = ETS_NORMAL;
3648 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3650 if (!IsWindowEnabled(hwnd))
3651 state = ETS_DISABLED;
3652 else if (dwStyle & ES_READONLY)
3653 state = ETS_READONLY;
3654 else if (GetFocus() == hwnd)
3655 state = ETS_FOCUSED;
3657 GetWindowRect(hwnd, &r);
3659 /* New clipping region passed to default proc to exclude border */
3660 cliprgn = CreateRectRgn(r.left + cxEdge, r.top + cyEdge,
3661 r.right - cxEdge, r.bottom - cyEdge);
3662 if (region != (HRGN)1)
3663 CombineRgn(cliprgn, cliprgn, region, RGN_AND);
3664 OffsetRect(&r, -r.left, -r.top);
3666 dc = GetDCEx(hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
3668 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
3669 DrawThemeParentBackground(hwnd, dc, &r);
3670 DrawThemeBackground(theme, dc, part, state, &r, 0);
3671 ReleaseDC(hwnd, dc);
3674 /* Call default proc to get the scrollbars etc. also painted */
3675 DefWindowProcW (hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
3676 if (cliprgn != region)
3677 DeleteObject(cliprgn);
3680 /*********************************************************************
3682 * WM_SETFOCUS
3685 static void EDIT_WM_SetFocus(HTHEME theme, EDITSTATE *es)
3687 UINT flags = RDW_INVALIDATE | RDW_UPDATENOW;
3689 es->flags |= EF_FOCUSED;
3691 if (!(es->style & ES_NOHIDESEL))
3692 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3694 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3695 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3696 ShowCaret(es->hwndSelf);
3697 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3699 if (theme)
3700 flags |= RDW_FRAME | RDW_ERASE;
3702 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3706 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm)
3708 ABC abc[256];
3709 SHORT left, right;
3710 UINT i;
3712 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3713 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3715 if (!is_cjk(hdc))
3716 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3718 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3719 return 0;
3721 left = right = 0;
3722 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3723 if (-abc[i].abcA > right) right = -abc[i].abcA;
3724 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3726 return MAKELONG(left, right);
3729 /*********************************************************************
3731 * WM_SETFONT
3733 * With Win95 look the margins are set to default font value unless
3734 * the system font (font == 0) is being set, in which case they are left
3735 * unchanged.
3738 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3740 TEXTMETRICW tm;
3741 HDC dc;
3742 HFONT old_font = 0;
3743 RECT clientRect;
3744 DWORD margins;
3746 es->font = font;
3747 EDIT_InvalidateUniscribeData(es);
3748 dc = GetDC(es->hwndSelf);
3749 if (font)
3750 old_font = SelectObject(dc, font);
3751 GetTextMetricsW(dc, &tm);
3752 es->line_height = tm.tmHeight;
3753 es->char_width = tm.tmAveCharWidth;
3754 margins = get_font_margins(dc, &tm);
3755 if (font)
3756 SelectObject(dc, old_font);
3757 ReleaseDC(es->hwndSelf, dc);
3759 /* Reset the format rect and the margins */
3760 GetClientRect(es->hwndSelf, &clientRect);
3761 EDIT_SetRectNP(es, &clientRect);
3762 if (margins)
3763 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3764 LOWORD(margins), HIWORD(margins), FALSE);
3766 if (es->style & ES_MULTILINE)
3767 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3768 else
3769 EDIT_CalcLineWidth_SL(es);
3771 if (redraw)
3772 EDIT_UpdateText(es, NULL, TRUE);
3773 if (es->flags & EF_FOCUSED) {
3774 DestroyCaret();
3775 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3776 EDIT_SetCaretPos(es, es->selection_end,
3777 es->flags & EF_AFTER_WRAP);
3778 ShowCaret(es->hwndSelf);
3783 /*********************************************************************
3785 * WM_SETTEXT
3787 * NOTES
3788 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3789 * The modified flag is reset. No notifications are sent.
3791 * For single-line controls, reception of WM_SETTEXT triggers:
3792 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3795 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text)
3797 if (es->flags & EF_UPDATE)
3798 /* fixed this bug once; complain if we see it about to happen again. */
3799 ERR("SetSel may generate UPDATE message whose handler may reset "
3800 "selection.\n");
3802 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3803 if (text)
3805 TRACE("%s\n", debugstr_w(text));
3806 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3808 else
3810 TRACE("<NULL>\n");
3811 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3813 es->x_offset = 0;
3814 es->flags &= ~EF_MODIFIED;
3815 EDIT_EM_SetSel(es, 0, 0, FALSE);
3817 /* Send the notification after the selection start and end have been set
3818 * edit control doesn't send notification on WM_SETTEXT
3819 * if it is multiline, or it is part of combobox
3821 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3823 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3824 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3826 EDIT_EM_ScrollCaret(es);
3827 EDIT_UpdateScrollInfo(es);
3828 EDIT_InvalidateUniscribeData(es);
3832 /*********************************************************************
3834 * WM_SIZE
3837 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3839 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3840 RECT rc;
3841 GetClientRect(es->hwndSelf, &rc);
3842 EDIT_SetRectNP(es, &rc);
3843 EDIT_UpdateText(es, NULL, TRUE);
3848 /*********************************************************************
3850 * WM_STYLECHANGED
3852 * This message is sent by SetWindowLong on having changed either the Style
3853 * or the extended style.
3855 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3857 * See also EDIT_WM_NCCreate
3859 * It appears that the Windows version of the edit control allows the style
3860 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3861 * style variable which will generally be different. In this function we
3862 * update the internal style based on what changed in the externally visible
3863 * style.
3865 * Much of this content as based upon the MSDN, especially:
3866 * Platform SDK Documentation -> User Interface Services ->
3867 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3868 * Edit Control Styles
3870 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3872 if (GWL_STYLE == which) {
3873 DWORD style_change_mask;
3874 DWORD new_style;
3875 /* Only a subset of changes can be applied after the control
3876 * has been created.
3878 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3879 ES_NUMBER;
3880 if (es->style & ES_MULTILINE)
3881 style_change_mask |= ES_WANTRETURN;
3883 new_style = style->styleNew & style_change_mask;
3885 /* Number overrides lowercase overrides uppercase (at least it
3886 * does in Win95). However I'll bet that ES_NUMBER would be
3887 * invalid under Win 3.1.
3889 if (new_style & ES_NUMBER) {
3890 ; /* do not override the ES_NUMBER */
3891 } else if (new_style & ES_LOWERCASE) {
3892 new_style &= ~ES_UPPERCASE;
3895 es->style = (es->style & ~style_change_mask) | new_style;
3896 } else if (GWL_EXSTYLE == which) {
3897 ; /* FIXME - what is needed here */
3898 } else {
3899 WARN ("Invalid style change %ld\n",which);
3902 return 0;
3905 /*********************************************************************
3907 * WM_SYSKEYDOWN
3910 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3912 if ((key == VK_BACK) && (key_data & 0x2000)) {
3913 if (EDIT_EM_CanUndo(es))
3914 EDIT_EM_Undo(es);
3915 return 0;
3916 } else if (key == VK_UP || key == VK_DOWN) {
3917 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3918 return 0;
3920 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3924 /*********************************************************************
3926 * WM_TIMER
3929 static void EDIT_WM_Timer(EDITSTATE *es)
3931 if (es->region_posx < 0) {
3932 EDIT_MoveBackward(es, TRUE);
3933 } else if (es->region_posx > 0) {
3934 EDIT_MoveForward(es, TRUE);
3937 * FIXME: gotta do some vertical scrolling here, like
3938 * EDIT_EM_LineScroll(hwnd, 0, 1);
3942 /*********************************************************************
3944 * WM_HSCROLL
3947 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3949 INT dx;
3950 INT fw;
3952 if (!(es->style & ES_MULTILINE))
3953 return 0;
3955 if (!(es->style & ES_AUTOHSCROLL))
3956 return 0;
3958 dx = 0;
3959 fw = es->format_rect.right - es->format_rect.left;
3960 switch (action) {
3961 case SB_LINELEFT:
3962 TRACE("SB_LINELEFT\n");
3963 if (es->x_offset)
3964 dx = -es->char_width;
3965 break;
3966 case SB_LINERIGHT:
3967 TRACE("SB_LINERIGHT\n");
3968 if (es->x_offset < es->text_width)
3969 dx = es->char_width;
3970 break;
3971 case SB_PAGELEFT:
3972 TRACE("SB_PAGELEFT\n");
3973 if (es->x_offset)
3974 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3975 break;
3976 case SB_PAGERIGHT:
3977 TRACE("SB_PAGERIGHT\n");
3978 if (es->x_offset < es->text_width)
3979 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3980 break;
3981 case SB_LEFT:
3982 TRACE("SB_LEFT\n");
3983 if (es->x_offset)
3984 dx = -es->x_offset;
3985 break;
3986 case SB_RIGHT:
3987 TRACE("SB_RIGHT\n");
3988 if (es->x_offset < es->text_width)
3989 dx = es->text_width - es->x_offset;
3990 break;
3991 case SB_THUMBTRACK:
3992 TRACE("SB_THUMBTRACK %d\n", pos);
3993 es->flags |= EF_HSCROLL_TRACK;
3994 if(es->style & WS_HSCROLL)
3995 dx = pos - es->x_offset;
3996 else
3998 INT fw, new_x;
3999 /* Sanity check */
4000 if(pos < 0 || pos > 100) return 0;
4001 /* Assume default scroll range 0-100 */
4002 fw = es->format_rect.right - es->format_rect.left;
4003 new_x = pos * (es->text_width - fw) / 100;
4004 dx = es->text_width ? (new_x - es->x_offset) : 0;
4006 break;
4007 case SB_THUMBPOSITION:
4008 TRACE("SB_THUMBPOSITION %d\n", pos);
4009 es->flags &= ~EF_HSCROLL_TRACK;
4010 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4011 dx = pos - es->x_offset;
4012 else
4014 INT fw, new_x;
4015 /* Sanity check */
4016 if(pos < 0 || pos > 100) return 0;
4017 /* Assume default scroll range 0-100 */
4018 fw = es->format_rect.right - es->format_rect.left;
4019 new_x = pos * (es->text_width - fw) / 100;
4020 dx = es->text_width ? (new_x - es->x_offset) : 0;
4022 if (!dx) {
4023 /* force scroll info update */
4024 EDIT_UpdateScrollInfo(es);
4025 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4027 break;
4028 case SB_ENDSCROLL:
4029 TRACE("SB_ENDSCROLL\n");
4030 break;
4032 * FIXME : the next two are undocumented !
4033 * Are we doing the right thing ?
4034 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4035 * although it's also a regular control message.
4037 case EM_GETTHUMB: /* this one is used by NT notepad */
4039 LRESULT ret;
4040 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4041 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4042 else
4044 /* Assume default scroll range 0-100 */
4045 INT fw = es->format_rect.right - es->format_rect.left;
4046 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4048 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4049 return ret;
4051 case EM_LINESCROLL:
4052 TRACE("EM_LINESCROLL16\n");
4053 dx = pos;
4054 break;
4056 default:
4057 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4058 action, action);
4059 return 0;
4061 if (dx)
4063 INT fw = es->format_rect.right - es->format_rect.left;
4064 /* check if we are going to move too far */
4065 if(es->x_offset + dx + fw > es->text_width)
4066 dx = es->text_width - fw - es->x_offset;
4067 if(dx)
4068 EDIT_EM_LineScroll_internal(es, dx, 0);
4070 return 0;
4074 /*********************************************************************
4076 * WM_VSCROLL
4079 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4081 INT dy;
4083 if (!(es->style & ES_MULTILINE))
4084 return 0;
4086 if (!(es->style & ES_AUTOVSCROLL))
4087 return 0;
4089 dy = 0;
4090 switch (action) {
4091 case SB_LINEUP:
4092 case SB_LINEDOWN:
4093 case SB_PAGEUP:
4094 case SB_PAGEDOWN:
4095 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4096 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4097 (action == SB_PAGEUP ? "SB_PAGEUP" :
4098 "SB_PAGEDOWN"))));
4099 EDIT_EM_Scroll(es, action);
4100 return 0;
4101 case SB_TOP:
4102 TRACE("SB_TOP\n");
4103 dy = -es->y_offset;
4104 break;
4105 case SB_BOTTOM:
4106 TRACE("SB_BOTTOM\n");
4107 dy = es->line_count - 1 - es->y_offset;
4108 break;
4109 case SB_THUMBTRACK:
4110 TRACE("SB_THUMBTRACK %d\n", pos);
4111 es->flags |= EF_VSCROLL_TRACK;
4112 if(es->style & WS_VSCROLL)
4113 dy = pos - es->y_offset;
4114 else
4116 /* Assume default scroll range 0-100 */
4117 INT vlc, new_y;
4118 /* Sanity check */
4119 if(pos < 0 || pos > 100) return 0;
4120 vlc = get_vertical_line_count(es);
4121 new_y = pos * (es->line_count - vlc) / 100;
4122 dy = es->line_count ? (new_y - es->y_offset) : 0;
4123 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4124 es->line_count, es->y_offset, pos, dy);
4126 break;
4127 case SB_THUMBPOSITION:
4128 TRACE("SB_THUMBPOSITION %d\n", pos);
4129 es->flags &= ~EF_VSCROLL_TRACK;
4130 if(es->style & WS_VSCROLL)
4131 dy = pos - es->y_offset;
4132 else
4134 /* Assume default scroll range 0-100 */
4135 INT vlc, new_y;
4136 /* Sanity check */
4137 if(pos < 0 || pos > 100) return 0;
4138 vlc = get_vertical_line_count(es);
4139 new_y = pos * (es->line_count - vlc) / 100;
4140 dy = es->line_count ? (new_y - es->y_offset) : 0;
4141 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4142 es->line_count, es->y_offset, pos, dy);
4144 if (!dy)
4146 /* force scroll info update */
4147 EDIT_UpdateScrollInfo(es);
4148 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4150 break;
4151 case SB_ENDSCROLL:
4152 TRACE("SB_ENDSCROLL\n");
4153 break;
4155 * FIXME : the next two are undocumented !
4156 * Are we doing the right thing ?
4157 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4158 * although it's also a regular control message.
4160 case EM_GETTHUMB: /* this one is used by NT notepad */
4162 LRESULT ret;
4163 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4164 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4165 else
4167 /* Assume default scroll range 0-100 */
4168 INT vlc = get_vertical_line_count(es);
4169 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4171 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4172 return ret;
4174 case EM_LINESCROLL:
4175 TRACE("EM_LINESCROLL %d\n", pos);
4176 dy = pos;
4177 break;
4179 default:
4180 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4181 action, action);
4182 return 0;
4184 if (dy)
4185 EDIT_EM_LineScroll(es, 0, dy);
4186 return 0;
4189 /*********************************************************************
4191 * EM_GETTHUMB
4193 * FIXME: is this right ? (or should it be only VSCROLL)
4194 * (and maybe only for edit controls that really have their
4195 * own scrollbars) (and maybe only for multiline controls ?)
4196 * All in all: very poorly documented
4199 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4201 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4202 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4205 static inline WCHAR *heap_strdupW(const WCHAR *str)
4207 int len = lstrlenW(str) + 1;
4208 WCHAR *ret = heap_alloc(len * sizeof(WCHAR));
4209 lstrcpyW(ret, str);
4210 return ret;
4213 /*********************************************************************
4215 * EM_SETCUEBANNER
4218 static BOOL EDIT_EM_SetCueBanner(EDITSTATE *es, BOOL draw_focused, const WCHAR *cue_text)
4220 if (es->style & ES_MULTILINE || !cue_text)
4221 return FALSE;
4223 heap_free(es->cue_banner_text);
4224 es->cue_banner_text = heap_strdupW(cue_text);
4225 es->cue_banner_draw_focused = draw_focused;
4227 return TRUE;
4230 /*********************************************************************
4232 * EM_GETCUEBANNER
4235 static BOOL EDIT_EM_GetCueBanner(EDITSTATE *es, WCHAR *buf, DWORD size)
4237 if (es->style & ES_MULTILINE)
4238 return FALSE;
4240 if (!es->cue_banner_text)
4242 if (buf && size)
4243 *buf = 0;
4244 return FALSE;
4246 else
4248 if (buf)
4249 lstrcpynW(buf, es->cue_banner_text, size);
4250 return TRUE;
4255 /********************************************************************
4257 * The Following code is to handle inline editing from IMEs
4260 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4262 LONG buflen;
4263 LPWSTR lpCompStr;
4264 LPSTR lpCompStrAttr = NULL;
4265 DWORD dwBufLenAttr;
4267 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4269 if (buflen < 0)
4271 return;
4274 lpCompStr = heap_alloc(buflen);
4275 if (!lpCompStr)
4277 ERR("Unable to allocate IME CompositionString\n");
4278 return;
4281 if (buflen)
4282 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4284 if (CompFlag & GCS_COMPATTR)
4287 * We do not use the attributes yet. it would tell us what characters
4288 * are in transition and which are converted or decided upon
4290 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4291 if (dwBufLenAttr)
4293 dwBufLenAttr ++;
4294 lpCompStrAttr = heap_alloc(dwBufLenAttr + 1);
4295 if (!lpCompStrAttr)
4297 ERR("Unable to allocate IME Attribute String\n");
4298 heap_free(lpCompStr);
4299 return;
4301 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4302 dwBufLenAttr);
4303 lpCompStrAttr[dwBufLenAttr] = 0;
4307 /* check for change in composition start */
4308 if (es->selection_end < es->composition_start)
4309 es->composition_start = es->selection_end;
4311 /* replace existing selection string */
4312 es->selection_start = es->composition_start;
4314 if (es->composition_len > 0)
4315 es->selection_end = es->composition_start + es->composition_len;
4316 else
4317 es->selection_end = es->selection_start;
4319 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4320 es->composition_len = abs(es->composition_start - es->selection_end);
4322 es->selection_start = es->composition_start;
4323 es->selection_end = es->selection_start + es->composition_len;
4325 heap_free(lpCompStrAttr);
4326 heap_free(lpCompStr);
4329 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4331 LONG buflen;
4332 LPWSTR lpResultStr;
4334 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4335 if (buflen <= 0)
4337 return;
4340 lpResultStr = heap_alloc(buflen);
4341 if (!lpResultStr)
4343 ERR("Unable to alloc buffer for IME string\n");
4344 return;
4347 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4349 /* check for change in composition start */
4350 if (es->selection_end < es->composition_start)
4351 es->composition_start = es->selection_end;
4353 es->selection_start = es->composition_start;
4354 es->selection_end = es->composition_start + es->composition_len;
4355 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4356 es->composition_start = es->selection_end;
4357 es->composition_len = 0;
4359 heap_free(lpResultStr);
4362 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4364 HIMC hIMC;
4365 int cursor;
4367 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4369 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4370 es->composition_start = es->selection_end;
4373 hIMC = ImmGetContext(hwnd);
4374 if (!hIMC)
4375 return;
4377 if (CompFlag & GCS_RESULTSTR)
4379 EDIT_GetResultStr(hIMC, es);
4380 cursor = 0;
4382 else
4384 if (CompFlag & GCS_COMPSTR)
4385 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4386 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4388 ImmReleaseContext(hwnd, hIMC);
4389 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4393 /*********************************************************************
4395 * WM_NCCREATE
4397 * See also EDIT_WM_StyleChanged
4399 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
4401 EDITSTATE *es;
4402 UINT alloc_size;
4404 TRACE("Creating edit control, style = %08x\n", lpcs->style);
4406 if (!(es = heap_alloc_zero(sizeof(*es))))
4407 return FALSE;
4408 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4411 * Note: since the EDITSTATE has not been fully initialized yet,
4412 * we can't use any API calls that may send
4413 * WM_XXX messages before WM_NCCREATE is completed.
4416 es->style = lpcs->style;
4418 es->bEnableState = !(es->style & WS_DISABLED);
4420 es->hwndSelf = hwnd;
4421 /* Save parent, which will be notified by EN_* messages */
4422 es->hwndParent = lpcs->hwndParent;
4424 if (es->style & ES_COMBO)
4425 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4427 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4428 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4430 /* Number overrides lowercase overrides uppercase (at least it
4431 * does in Win95). However I'll bet that ES_NUMBER would be
4432 * invalid under Win 3.1.
4434 if (es->style & ES_NUMBER) {
4435 ; /* do not override the ES_NUMBER */
4436 } else if (es->style & ES_LOWERCASE) {
4437 es->style &= ~ES_UPPERCASE;
4439 if (es->style & ES_MULTILINE) {
4440 es->buffer_limit = BUFLIMIT_INITIAL;
4441 if (es->style & WS_VSCROLL)
4442 es->style |= ES_AUTOVSCROLL;
4443 if (es->style & WS_HSCROLL)
4444 es->style |= ES_AUTOHSCROLL;
4445 es->style &= ~ES_PASSWORD;
4446 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4447 /* Confirmed - RIGHT overrides CENTER */
4448 if (es->style & ES_RIGHT)
4449 es->style &= ~ES_CENTER;
4450 es->style &= ~WS_HSCROLL;
4451 es->style &= ~ES_AUTOHSCROLL;
4453 } else {
4454 es->buffer_limit = BUFLIMIT_INITIAL;
4455 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4456 es->style &= ~ES_CENTER;
4457 es->style &= ~WS_HSCROLL;
4458 es->style &= ~WS_VSCROLL;
4459 if (es->style & ES_PASSWORD)
4460 es->password_char = '*';
4463 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4464 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4465 goto cleanup;
4466 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4468 if (!(es->undo_text = heap_alloc_zero((es->buffer_size + 1) * sizeof(WCHAR))))
4469 goto cleanup;
4470 es->undo_buffer_size = es->buffer_size;
4472 if (es->style & ES_MULTILINE)
4473 if (!(es->first_line_def = heap_alloc_zero(sizeof(LINEDEF))))
4474 goto cleanup;
4475 es->line_count = 1;
4478 * In Win95 look and feel, the WS_BORDER style is replaced by the
4479 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4480 * control a nonclient area so we don't need to draw the border.
4481 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4482 * a nonclient area and we should handle painting the border ourselves.
4484 * When making modifications please ensure that the code still works
4485 * for edit controls created directly with style 0x50800000, exStyle 0
4486 * (which should have a single pixel border)
4488 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4489 es->style &= ~WS_BORDER;
4490 else if (es->style & WS_BORDER)
4491 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4493 return TRUE;
4495 cleanup:
4496 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4497 EDIT_InvalidateUniscribeData(es);
4498 heap_free(es->first_line_def);
4499 heap_free(es->undo_text);
4500 if (es->hloc32W) LocalFree(es->hloc32W);
4501 heap_free(es->logAttr);
4502 heap_free(es);
4503 return FALSE;
4507 /*********************************************************************
4509 * WM_CREATE
4512 static LRESULT EDIT_WM_Create(EDITSTATE *es, const WCHAR *name)
4514 RECT clientRect;
4516 TRACE("%s\n", debugstr_w(name));
4519 * To initialize some final structure members, we call some helper
4520 * functions. However, since the EDITSTATE is not consistent (i.e.
4521 * not fully initialized), we should be very careful which
4522 * functions can be called, and in what order.
4524 EDIT_WM_SetFont(es, 0, FALSE);
4525 EDIT_EM_EmptyUndoBuffer(es);
4527 /* We need to calculate the format rect
4528 (applications may send EM_SETMARGINS before the control gets visible) */
4529 GetClientRect(es->hwndSelf, &clientRect);
4530 EDIT_SetRectNP(es, &clientRect);
4532 if (name && *name)
4534 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4535 /* if we insert text to the editline, the text scrolls out
4536 * of the window, as the caret is placed after the insert
4537 * pos normally; thus we reset es->selection... to 0 and
4538 * update caret
4540 es->selection_start = es->selection_end = 0;
4541 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4542 * Messages are only to be sent when the USER does something to
4543 * change the contents. So I am removing this EN_CHANGE
4545 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4547 EDIT_EM_ScrollCaret(es);
4550 /* force scroll info update */
4551 EDIT_UpdateScrollInfo(es);
4552 OpenThemeData(es->hwndSelf, WC_EDITW);
4554 /* The rule seems to return 1 here for success */
4555 /* Power Builder masked edit controls will crash */
4556 /* if not. */
4557 /* FIXME: is that in all cases so ? */
4558 return 1;
4562 /*********************************************************************
4564 * WM_NCDESTROY
4567 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4569 LINEDEF *pc, *pp;
4570 HTHEME theme;
4572 theme = GetWindowTheme(es->hwndSelf);
4573 CloseThemeData(theme);
4575 /* The app can own the text buffer handle */
4576 if (es->hloc32W && (es->hloc32W != es->hlocapp))
4577 LocalFree(es->hloc32W);
4579 EDIT_InvalidateUniscribeData(es);
4581 pc = es->first_line_def;
4582 while (pc)
4584 pp = pc->next;
4585 heap_free(pc);
4586 pc = pp;
4589 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4590 heap_free(es->undo_text);
4591 heap_free(es->cue_banner_text);
4592 heap_free(es);
4594 return 0;
4597 static LRESULT CALLBACK EDIT_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
4599 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW(hwnd, 0);
4600 LRESULT result = 0;
4601 RECT *rect;
4603 TRACE("hwnd=%p msg=%#x wparam=%lx lparam=%lx\n", hwnd, msg, wParam, lParam);
4605 if (!es && msg != WM_NCCREATE)
4606 return DefWindowProcW(hwnd, msg, wParam, lParam);
4608 if (es && (msg != WM_NCDESTROY))
4609 EDIT_LockBuffer(es);
4611 switch (msg)
4613 case EM_GETSEL:
4614 result = EDIT_EM_GetSel(es, (UINT *)wParam, (UINT *)lParam);
4615 break;
4617 case EM_SETSEL:
4618 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4619 EDIT_EM_ScrollCaret(es);
4620 result = 1;
4621 break;
4623 case EM_GETRECT:
4624 rect = (RECT *)lParam;
4625 if (rect)
4626 *rect = es->format_rect;
4627 break;
4629 case EM_SETRECT:
4630 if ((es->style & ES_MULTILINE) && lParam)
4632 EDIT_SetRectNP(es, (RECT *)lParam);
4633 EDIT_UpdateText(es, NULL, TRUE);
4635 break;
4637 case EM_SETRECTNP:
4638 if ((es->style & ES_MULTILINE) && lParam)
4639 EDIT_SetRectNP(es, (LPRECT)lParam);
4640 break;
4642 case EM_SCROLL:
4643 result = EDIT_EM_Scroll(es, (INT)wParam);
4644 break;
4646 case EM_LINESCROLL:
4647 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4648 break;
4650 case EM_SCROLLCARET:
4651 EDIT_EM_ScrollCaret(es);
4652 result = 1;
4653 break;
4655 case EM_GETMODIFY:
4656 result = ((es->flags & EF_MODIFIED) != 0);
4657 break;
4659 case EM_SETMODIFY:
4660 if (wParam)
4661 es->flags |= EF_MODIFIED;
4662 else
4663 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4664 break;
4666 case EM_GETLINECOUNT:
4667 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4668 break;
4670 case EM_LINEINDEX:
4671 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4672 break;
4674 case EM_SETHANDLE:
4675 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4676 break;
4678 case EM_GETHANDLE:
4679 result = (LRESULT)EDIT_EM_GetHandle(es);
4680 break;
4682 case EM_GETTHUMB:
4683 result = EDIT_EM_GetThumb(es);
4684 break;
4686 /* these messages missing from specs */
4687 case 0x00bf:
4688 case 0x00c0:
4689 case 0x00c3:
4690 case 0x00ca:
4691 FIXME("undocumented message 0x%x, please report\n", msg);
4692 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4693 break;
4695 case EM_LINELENGTH:
4696 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4697 break;
4699 case EM_REPLACESEL:
4701 const WCHAR *textW = (const WCHAR *)lParam;
4703 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4704 result = 1;
4705 break;
4708 case EM_GETLINE:
4709 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
4710 break;
4712 case EM_SETLIMITTEXT:
4713 EDIT_EM_SetLimitText(es, wParam);
4714 break;
4716 case EM_CANUNDO:
4717 result = (LRESULT)EDIT_EM_CanUndo(es);
4718 break;
4720 case EM_UNDO:
4721 case WM_UNDO:
4722 result = (LRESULT)EDIT_EM_Undo(es);
4723 break;
4725 case EM_FMTLINES:
4726 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4727 break;
4729 case EM_LINEFROMCHAR:
4730 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4731 break;
4733 case EM_SETTABSTOPS:
4734 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4735 break;
4737 case EM_SETPASSWORDCHAR:
4738 EDIT_EM_SetPasswordChar(es, wParam);
4739 break;
4741 case EM_EMPTYUNDOBUFFER:
4742 EDIT_EM_EmptyUndoBuffer(es);
4743 break;
4745 case EM_GETFIRSTVISIBLELINE:
4746 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4747 break;
4749 case EM_SETREADONLY:
4751 DWORD old_style = es->style;
4753 if (wParam)
4755 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) | ES_READONLY);
4756 es->style |= ES_READONLY;
4758 else
4760 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & ~ES_READONLY);
4761 es->style &= ~ES_READONLY;
4764 if (old_style ^ es->style)
4765 InvalidateRect(es->hwndSelf, NULL, TRUE);
4767 result = 1;
4768 break;
4771 case EM_SETWORDBREAKPROC:
4772 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4773 result = 1;
4774 break;
4776 case EM_GETWORDBREAKPROC:
4777 result = (LRESULT)es->word_break_proc;
4778 break;
4780 case EM_GETPASSWORDCHAR:
4781 result = es->password_char;
4782 break;
4784 case EM_SETMARGINS:
4785 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4786 break;
4788 case EM_GETMARGINS:
4789 result = MAKELONG(es->left_margin, es->right_margin);
4790 break;
4792 case EM_GETLIMITTEXT:
4793 result = es->buffer_limit;
4794 break;
4796 case EM_POSFROMCHAR:
4797 if ((INT)wParam >= get_text_length(es)) result = -1;
4798 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4799 break;
4801 case EM_CHARFROMPOS:
4802 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4803 break;
4805 case EM_SETCUEBANNER:
4806 result = EDIT_EM_SetCueBanner(es, (BOOL)wParam, (const WCHAR *)lParam);
4807 break;
4809 case EM_GETCUEBANNER:
4810 result = EDIT_EM_GetCueBanner(es, (WCHAR *)wParam, (DWORD)lParam);
4811 break;
4813 /* End of the EM_ messages which were in numerical order; what order
4814 * are these in? vaguely alphabetical?
4817 case WM_NCCREATE:
4818 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
4819 break;
4821 case WM_NCDESTROY:
4822 result = EDIT_WM_NCDestroy(es);
4823 es = NULL;
4824 break;
4826 case WM_GETDLGCODE:
4827 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4829 if (es->style & ES_MULTILINE)
4830 result |= DLGC_WANTALLKEYS;
4832 if (lParam)
4834 MSG *msg = (MSG *)lParam;
4835 es->flags |= EF_DIALOGMODE;
4837 if (msg->message == WM_KEYDOWN)
4839 int vk = (int)msg->wParam;
4841 if (es->hwndListBox)
4843 if (vk == VK_RETURN || vk == VK_ESCAPE)
4844 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4845 result |= DLGC_WANTMESSAGE;
4849 break;
4851 case WM_IME_CHAR:
4852 case WM_CHAR:
4854 WCHAR charW = wParam;
4856 if (es->hwndListBox)
4858 if (charW == VK_RETURN || charW == VK_ESCAPE)
4860 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4861 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4862 break;
4865 result = EDIT_WM_Char(es, charW);
4866 break;
4869 case WM_UNICHAR:
4870 if (wParam == UNICODE_NOCHAR) return TRUE;
4871 if (wParam <= 0x000fffff)
4873 if (wParam > 0xffff) /* convert to surrogates */
4875 wParam -= 0x10000;
4876 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4877 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4879 else
4880 EDIT_WM_Char(es, wParam);
4882 return 0;
4884 case WM_CLEAR:
4885 EDIT_WM_Clear(es);
4886 break;
4888 case WM_CONTEXTMENU:
4889 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4890 break;
4892 case WM_COPY:
4893 EDIT_WM_Copy(es);
4894 break;
4896 case WM_CREATE:
4897 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4898 break;
4900 case WM_CUT:
4901 EDIT_WM_Cut(es);
4902 break;
4904 case WM_ENABLE:
4905 es->bEnableState = (BOOL) wParam;
4906 EDIT_UpdateText(es, NULL, TRUE);
4907 if (GetWindowTheme(hwnd))
4908 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
4909 break;
4911 case WM_ERASEBKGND:
4912 /* we do the proper erase in EDIT_WM_Paint */
4913 result = 1;
4914 break;
4916 case WM_GETFONT:
4917 result = (LRESULT)es->font;
4918 break;
4920 case WM_GETTEXT:
4921 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam);
4922 break;
4924 case WM_GETTEXTLENGTH:
4925 result = get_text_length(es);
4926 break;
4928 case WM_HSCROLL:
4929 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4930 break;
4932 case WM_KEYDOWN:
4933 result = EDIT_WM_KeyDown(es, (INT)wParam);
4934 break;
4936 case WM_KILLFOCUS:
4937 result = EDIT_WM_KillFocus(GetWindowTheme(hwnd), es);
4938 break;
4940 case WM_LBUTTONDBLCLK:
4941 result = EDIT_WM_LButtonDblClk(es);
4942 break;
4944 case WM_LBUTTONDOWN:
4945 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4946 break;
4948 case WM_LBUTTONUP:
4949 result = EDIT_WM_LButtonUp(es);
4950 break;
4952 case WM_MBUTTONDOWN:
4953 result = EDIT_WM_MButtonDown(es);
4954 break;
4956 case WM_MOUSEMOVE:
4957 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4958 break;
4960 case WM_PRINTCLIENT:
4961 case WM_PAINT:
4962 EDIT_WM_Paint(es, (HDC)wParam);
4963 break;
4965 case WM_NCPAINT:
4966 EDIT_WM_NCPaint(hwnd, (HRGN)wParam);
4967 break;
4969 case WM_PASTE:
4970 EDIT_WM_Paste(es);
4971 break;
4973 case WM_SETFOCUS:
4974 EDIT_WM_SetFocus(GetWindowTheme(hwnd), es);
4975 break;
4977 case WM_SETFONT:
4978 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4979 break;
4981 case WM_SETREDRAW:
4982 /* FIXME: actually set an internal flag and behave accordingly */
4983 break;
4985 case WM_SETTEXT:
4986 EDIT_WM_SetText(es, (const WCHAR *)lParam);
4987 result = TRUE;
4988 break;
4990 case WM_SIZE:
4991 EDIT_WM_Size(es, (UINT)wParam);
4992 break;
4994 case WM_STYLECHANGED:
4995 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4996 break;
4998 case WM_STYLECHANGING:
4999 result = 0; /* See EDIT_WM_StyleChanged */
5000 break;
5002 case WM_SYSKEYDOWN:
5003 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5004 break;
5006 case WM_TIMER:
5007 EDIT_WM_Timer(es);
5008 break;
5010 case WM_VSCROLL:
5011 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5012 break;
5014 case WM_MOUSEWHEEL:
5016 int wheelDelta;
5017 INT pulScrollLines = 3;
5018 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5020 if (wParam & (MK_SHIFT | MK_CONTROL))
5022 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5023 break;
5026 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
5027 /* if scrolling changes direction, ignore left overs */
5028 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
5029 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
5030 es->wheelDeltaRemainder += wheelDelta;
5031 else
5032 es->wheelDeltaRemainder = wheelDelta;
5034 if (es->wheelDeltaRemainder && pulScrollLines)
5036 int cLineScroll;
5037 pulScrollLines = min(es->line_count, pulScrollLines);
5038 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
5039 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
5040 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
5042 break;
5045 /* IME messages to make the edit control IME aware */
5046 case WM_IME_SETCONTEXT:
5047 break;
5049 case WM_IME_STARTCOMPOSITION:
5050 es->composition_start = es->selection_end;
5051 es->composition_len = 0;
5052 break;
5054 case WM_IME_COMPOSITION:
5055 EDIT_ImeComposition(hwnd, lParam, es);
5056 break;
5058 case WM_IME_ENDCOMPOSITION:
5059 if (es->composition_len > 0)
5061 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
5062 es->selection_end = es->selection_start;
5063 es->composition_len= 0;
5065 break;
5067 case WM_IME_COMPOSITIONFULL:
5068 break;
5070 case WM_IME_SELECT:
5071 break;
5073 case WM_IME_CONTROL:
5074 break;
5076 case WM_IME_REQUEST:
5077 switch (wParam)
5079 case IMR_QUERYCHARPOSITION:
5081 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
5082 LRESULT pos;
5084 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
5085 chpos->pt.x = LOWORD(pos);
5086 chpos->pt.y = HIWORD(pos);
5087 chpos->cLineHeight = es->line_height;
5088 chpos->rcDocument = es->format_rect;
5089 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
5090 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
5091 result = 1;
5092 break;
5095 break;
5097 case WM_THEMECHANGED:
5098 CloseThemeData(GetWindowTheme(hwnd));
5099 OpenThemeData(hwnd, WC_EDITW);
5100 InvalidateRect(hwnd, NULL, TRUE);
5101 break;
5103 default:
5104 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5105 break;
5108 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5109 EDIT_UnlockBuffer(es, FALSE);
5111 TRACE("hwnd=%p msg=%x -- 0x%08lx\n", hwnd, msg, result);
5113 return result;
5116 void EDIT_Register(void)
5118 WNDCLASSW wndClass;
5120 memset(&wndClass, 0, sizeof(wndClass));
5121 wndClass.style = CS_PARENTDC | CS_GLOBALCLASS | CS_DBLCLKS;
5122 wndClass.lpfnWndProc = EDIT_WindowProc;
5123 wndClass.cbClsExtra = 0;
5124 #ifdef __i386__
5125 wndClass.cbWndExtra = sizeof(EDITSTATE *) + sizeof(WORD);
5126 #else
5127 wndClass.cbWndExtra = sizeof(EDITSTATE *);
5128 #endif
5129 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_IBEAM);
5130 wndClass.hbrBackground = NULL;
5131 wndClass.lpszClassName = WC_EDITW;
5132 RegisterClassW(&wndClass);