usp10: Avoid leaking "open_stack" and "stack_index" in case of 0 "pair_count" in...
[wine.git] / dlls / comctl32 / edit.c
blob10ff5fb0f3e1b2bf9a8d059edcd6496e6d4b4389
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_GETCUEBANNER/Edit_GetCueBannerText
27 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
28 * - EM_SETCUEBANNER/Edit_SetCueBannerText
29 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
30 * - EM_GETIMESTATUS, EM_SETIMESTATUS
31 * - EN_ALIGN_LTR_EC
32 * - EN_ALIGN_RTL_EC
33 * - ES_OEMCONVERT
37 #include "config.h"
39 #include <stdarg.h>
40 #include <string.h>
41 #include <stdlib.h>
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnt.h"
46 #include "imm.h"
47 #include "usp10.h"
48 #include "commctrl.h"
49 #include "uxtheme.h"
50 #include "vsstyle.h"
51 #include "wine/unicode.h"
52 #include "wine/debug.h"
53 #include "wine/heap.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(edit);
57 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
58 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
59 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
60 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
63 * extra flags for EDITSTATE.flags field
65 #define EF_MODIFIED 0x0001 /* text has been modified */
66 #define EF_FOCUSED 0x0002 /* we have input focus */
67 #define EF_UPDATE 0x0004 /* notify parent of changed state */
68 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
69 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
70 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
71 wrapped line, instead of in front of the next character */
72 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
73 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
75 #define ID_CB_LISTBOX 1000
77 typedef enum
79 END_0 = 0, /* line ends with terminating '\0' character */
80 END_WRAP, /* line is wrapped */
81 END_HARD, /* line ends with a hard return '\r\n' */
82 END_SOFT, /* line ends with a soft return '\r\r\n' */
83 END_RICH /* line ends with a single '\n' */
84 } LINE_END;
86 typedef struct tagLINEDEF {
87 INT length; /* bruto length of a line in bytes */
88 INT net_length; /* netto length of a line in visible characters */
89 LINE_END ending;
90 INT width; /* width of the line in pixels */
91 INT index; /* line index into the buffer */
92 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
93 struct tagLINEDEF *next;
94 } LINEDEF;
96 typedef struct
98 LPWSTR text; /* the actual contents of the control */
99 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
100 UINT buffer_size; /* the size of the buffer in characters */
101 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
102 HFONT font; /* NULL means standard system font */
103 INT x_offset; /* scroll offset for multi lines this is in pixels
104 for single lines it's in characters */
105 INT line_height; /* height of a screen line in pixels */
106 INT char_width; /* average character width in pixels */
107 DWORD style; /* sane version of wnd->dwStyle */
108 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
109 INT undo_insert_count; /* number of characters inserted in sequence */
110 UINT undo_position; /* character index of the insertion and deletion */
111 LPWSTR undo_text; /* deleted text */
112 UINT undo_buffer_size; /* size of the deleted text buffer */
113 INT selection_start; /* == selection_end if no selection */
114 INT selection_end; /* == current caret position */
115 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
116 INT left_margin; /* in pixels */
117 INT right_margin; /* in pixels */
118 RECT format_rect;
119 INT text_width; /* width of the widest line in pixels for multi line controls
120 and just line width for single line controls */
121 INT region_posx; /* Position of cursor relative to region: */
122 INT region_posy; /* -1: to left, 0: within, 1: to right */
123 EDITWORDBREAKPROCW word_break_proc;
124 INT line_count; /* number of lines */
125 INT y_offset; /* scroll offset in number of lines */
126 BOOL bCaptureState; /* flag indicating whether mouse was captured */
127 BOOL bEnableState; /* flag keeping the enable state */
128 HWND hwndSelf; /* the our window handle */
129 HWND hwndParent; /* Handle of parent for sending EN_* messages.
130 Even if parent will change, EN_* messages
131 should be sent to the first parent. */
132 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
133 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
135 * only for multi line controls
137 INT lock_count; /* amount of re-entries in the EditWndProc */
138 INT tabs_count;
139 LPINT tabs;
140 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
141 HLOCAL hloc32W; /* our unicode local memory block */
142 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
144 * IME Data
146 UINT composition_len; /* length of composition, 0 == no composition */
147 int composition_start; /* the character position for the composition */
149 * Uniscribe Data
151 SCRIPT_LOGATTR *logAttr;
152 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
153 } EDITSTATE;
156 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
157 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
159 /* used for disabled or read-only edit control */
160 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
161 do \
162 { /* Notify parent which has created this edit control */ \
163 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
164 SendMessageW(es->hwndParent, WM_COMMAND, \
165 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
166 (LPARAM)(es->hwndSelf)); \
167 } while(0)
169 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
171 /*********************************************************************
173 * EM_CANUNDO
176 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
178 return (es->undo_insert_count || strlenW(es->undo_text));
182 /*********************************************************************
184 * EM_EMPTYUNDOBUFFER
187 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
189 es->undo_insert_count = 0;
190 *es->undo_text = '\0';
193 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
195 HBRUSH hbrush;
196 UINT msg;
198 if ((!es->bEnableState || (es->style & ES_READONLY)))
199 msg = WM_CTLCOLORSTATIC;
200 else
201 msg = WM_CTLCOLOREDIT;
203 /* Why do we notify to es->hwndParent, and we send this one to GetParent()? */
204 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
205 if (!hbrush)
206 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
207 return hbrush;
211 static inline UINT get_text_length(EDITSTATE *es)
213 if(es->text_length == (UINT)-1)
214 es->text_length = strlenW(es->text);
215 return es->text_length;
219 /*********************************************************************
221 * EDIT_WordBreakProc
223 * Find the beginning of words.
224 * Note: unlike the specs for a WordBreakProc, this function can
225 * only be called without linebreaks between s[0] up to
226 * s[count - 1]. Remember it is only called
227 * internally, so we can decide this for ourselves.
228 * Additionally we will always be breaking the full string.
231 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
233 INT ret = 0;
235 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
237 if(!s) return 0;
239 if (!es->logAttr)
241 SCRIPT_ANALYSIS psa;
243 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
244 psa.eScript = SCRIPT_UNDEFINED;
246 es->logAttr = heap_alloc(sizeof(SCRIPT_LOGATTR) * get_text_length(es));
247 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
250 switch (action) {
251 case WB_LEFT:
252 if (index)
253 index--;
254 while (index && !es->logAttr[index].fSoftBreak)
255 index--;
256 ret = index;
257 break;
258 case WB_RIGHT:
259 if (!count)
260 break;
261 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
262 index++;
263 ret = index;
264 break;
265 case WB_ISDELIMITER:
266 ret = es->logAttr[index].fWhiteSpace;
267 break;
268 default:
269 ERR("unknown action code, please report !\n");
270 break;
272 return ret;
276 /*********************************************************************
278 * EDIT_CallWordBreakProc
280 * Call appropriate WordBreakProc (internal or external).
282 * Note: The "start" argument should always be an index referring
283 * to es->text. The actual wordbreak proc might be
284 * 16 bit, so we can't always pass any 32 bit LPSTR.
285 * Hence we assume that es->text is the buffer that holds
286 * the string under examination (we can decide this for ourselves).
289 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
291 INT ret;
293 if (es->word_break_proc)
294 ret = es->word_break_proc(es->text + start, index, count, action);
295 else
296 ret = EDIT_WordBreakProc(es, es->text, index + start, count + start, action) - start;
298 return ret;
301 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
303 if (line_def->ssa)
305 ScriptStringFree(&line_def->ssa);
306 line_def->ssa = NULL;
310 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
312 LINEDEF *line_def = es->first_line_def;
313 while (line_def)
315 EDIT_InvalidateUniscribeData_linedef(line_def);
316 line_def = line_def->next;
318 if (es->ssa)
320 ScriptStringFree(&es->ssa);
321 es->ssa = NULL;
325 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
327 if (!line_def)
328 return NULL;
330 if (line_def->net_length && !line_def->ssa)
332 int index = line_def->index;
333 HFONT old_font = NULL;
334 HDC udc = dc;
335 SCRIPT_TABDEF tabdef;
336 HRESULT hr;
338 if (!udc)
339 udc = GetDC(es->hwndSelf);
340 if (es->font)
341 old_font = SelectObject(udc, es->font);
343 tabdef.cTabStops = es->tabs_count;
344 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
345 tabdef.pTabStops = es->tabs;
346 tabdef.iTabOrigin = 0;
348 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
349 (1.5*line_def->net_length+16), -1,
350 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
351 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
352 if (FAILED(hr))
354 WARN("ScriptStringAnalyse failed (%x)\n",hr);
355 line_def->ssa = NULL;
358 if (es->font)
359 SelectObject(udc, old_font);
360 if (udc != dc)
361 ReleaseDC(es->hwndSelf, udc);
364 return line_def->ssa;
367 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
369 LINEDEF *line_def;
371 if (!(es->style & ES_MULTILINE))
373 if (!es->ssa)
375 INT length = get_text_length(es);
376 HFONT old_font = NULL;
377 HDC udc = dc;
379 if (!udc)
380 udc = GetDC(es->hwndSelf);
381 if (es->font)
382 old_font = SelectObject(udc, es->font);
384 if (es->style & ES_PASSWORD)
385 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);
386 else
387 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
389 if (es->font)
390 SelectObject(udc, old_font);
391 if (udc != dc)
392 ReleaseDC(es->hwndSelf, udc);
394 return es->ssa;
396 else
398 line_def = es->first_line_def;
399 while (line_def && line)
401 line_def = line_def->next;
402 line--;
405 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
409 static inline INT get_vertical_line_count(EDITSTATE *es)
411 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
412 return max(1,vlc);
415 /*********************************************************************
417 * EDIT_BuildLineDefs_ML
419 * Build linked list of text lines.
420 * Lines can end with '\0' (last line), a character (if it is wrapped),
421 * a soft return '\r\r\n' or a hard return '\r\n'
424 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
426 LPWSTR current_position, cp;
427 INT fw;
428 LINEDEF *current_line;
429 LINEDEF *previous_line;
430 LINEDEF *start_line;
431 INT line_index = 0, nstart_line, nstart_index;
432 INT line_count = es->line_count;
433 INT orig_net_length;
434 RECT rc;
435 INT vlc;
437 if (istart == iend && delta == 0)
438 return;
440 previous_line = NULL;
441 current_line = es->first_line_def;
443 /* Find starting line. istart must lie inside an existing line or
444 * at the end of buffer */
445 do {
446 if (istart < current_line->index + current_line->length ||
447 current_line->ending == END_0)
448 break;
450 previous_line = current_line;
451 current_line = current_line->next;
452 line_index++;
453 } while (current_line);
455 if (!current_line) /* Error occurred start is not inside previous buffer */
457 FIXME(" modification occurred outside buffer\n");
458 return;
461 /* Remember start of modifications in order to calculate update region */
462 nstart_line = line_index;
463 nstart_index = current_line->index;
465 /* We must start to reformat from the previous line since the modifications
466 * may have caused the line to wrap upwards. */
467 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
469 line_index--;
470 current_line = previous_line;
472 start_line = current_line;
474 fw = es->format_rect.right - es->format_rect.left;
475 current_position = es->text + current_line->index;
476 vlc = get_vertical_line_count(es);
477 do {
478 if (current_line != start_line)
480 if (!current_line || current_line->index + delta > current_position - es->text)
482 /* The buffer has been expanded, create a new line and
483 insert it into the link list */
484 LINEDEF *new_line = heap_alloc_zero(sizeof(*new_line));
485 new_line->next = previous_line->next;
486 previous_line->next = new_line;
487 current_line = new_line;
488 es->line_count++;
490 else if (current_line->index + delta < current_position - es->text)
492 /* The previous line merged with this line so we delete this extra entry */
493 previous_line->next = current_line->next;
494 heap_free(current_line);
495 current_line = previous_line->next;
496 es->line_count--;
497 continue;
499 else /* current_line->index + delta == current_position */
501 if (current_position - es->text > iend)
502 break; /* We reached end of line modifications */
503 /* else recalculate this line */
507 current_line->index = current_position - es->text;
508 orig_net_length = current_line->net_length;
510 /* Find end of line */
511 cp = current_position;
512 while (*cp) {
513 if (*cp == '\n') break;
514 if ((*cp == '\r') && (*(cp + 1) == '\n'))
515 break;
516 cp++;
519 /* Mark type of line termination */
520 if (!(*cp)) {
521 current_line->ending = END_0;
522 current_line->net_length = strlenW(current_position);
523 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
524 current_line->ending = END_SOFT;
525 current_line->net_length = cp - current_position - 1;
526 } else if (*cp == '\n') {
527 current_line->ending = END_RICH;
528 current_line->net_length = cp - current_position;
529 } else {
530 current_line->ending = END_HARD;
531 current_line->net_length = cp - current_position;
534 if (current_line->net_length)
536 const SIZE *sz;
537 EDIT_InvalidateUniscribeData_linedef(current_line);
538 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
539 if (current_line->ssa)
541 sz = ScriptString_pSize(current_line->ssa);
542 /* Calculate line width */
543 current_line->width = sz->cx;
545 else current_line->width = es->char_width * current_line->net_length;
547 else current_line->width = 0;
549 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
551 /* Line breaks just look back from the end and find the next break and try that. */
553 if (!(es->style & ES_AUTOHSCROLL)) {
554 if (current_line->width > fw && fw > es->char_width) {
556 INT prev, next;
557 int w;
558 const SIZE *sz;
559 float d;
561 prev = current_line->net_length - 1;
562 w = current_line->net_length;
563 d = (float)current_line->width/(float)fw;
564 if (d > 1.2f) d -= 0.2f;
565 next = prev/d;
566 if (next >= prev) next = prev-1;
567 do {
568 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
569 next, current_line->net_length, WB_LEFT);
570 current_line->net_length = prev;
571 EDIT_InvalidateUniscribeData_linedef(current_line);
572 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
573 if (current_line->ssa)
574 sz = ScriptString_pSize(current_line->ssa);
575 else sz = 0;
576 if (sz)
577 current_line->width = sz->cx;
578 else
579 prev = 0;
580 next = prev - 1;
581 } while (prev && current_line->width > fw);
582 current_line->net_length = w;
584 if (prev == 0) { /* Didn't find a line break so force a break */
585 INT *piDx;
586 const INT *count;
588 EDIT_InvalidateUniscribeData_linedef(current_line);
589 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
591 if (current_line->ssa)
593 count = ScriptString_pcOutChars(current_line->ssa);
594 piDx = heap_alloc(sizeof(INT) * (*count));
595 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
597 prev = current_line->net_length-1;
598 do {
599 current_line->width -= piDx[prev];
600 prev--;
601 } while ( prev > 0 && current_line->width > fw);
602 if (prev<=0)
603 prev = 1;
604 heap_free(piDx);
606 else
607 prev = (fw / es->char_width);
610 /* If the first line we are calculating, wrapped before istart, we must
611 * adjust istart in order for this to be reflected in the update region. */
612 if (current_line->index == nstart_index && istart > current_line->index + prev)
613 istart = current_line->index + prev;
614 /* else if we are updating the previous line before the first line we
615 * are re-calculating and it expanded */
616 else if (current_line == start_line &&
617 current_line->index != nstart_index && orig_net_length < prev)
619 /* Line expanded due to an upwards line wrap so we must partially include
620 * previous line in update region */
621 nstart_line = line_index;
622 nstart_index = current_line->index;
623 istart = current_line->index + orig_net_length;
626 current_line->net_length = prev;
627 current_line->ending = END_WRAP;
629 if (current_line->net_length > 0)
631 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
632 if (current_line->ssa)
634 sz = ScriptString_pSize(current_line->ssa);
635 current_line->width = sz->cx;
637 else
638 current_line->width = 0;
640 else current_line->width = 0;
642 else if (current_line == start_line &&
643 current_line->index != nstart_index &&
644 orig_net_length < current_line->net_length) {
645 /* The previous line expanded but it's still not as wide as the client rect */
646 /* The expansion is due to an upwards line wrap so we must partially include
647 it in the update region */
648 nstart_line = line_index;
649 nstart_index = current_line->index;
650 istart = current_line->index + orig_net_length;
655 /* Adjust length to include line termination */
656 switch (current_line->ending) {
657 case END_SOFT:
658 current_line->length = current_line->net_length + 3;
659 break;
660 case END_RICH:
661 current_line->length = current_line->net_length + 1;
662 break;
663 case END_HARD:
664 current_line->length = current_line->net_length + 2;
665 break;
666 case END_WRAP:
667 case END_0:
668 current_line->length = current_line->net_length;
669 break;
671 es->text_width = max(es->text_width, current_line->width);
672 current_position += current_line->length;
673 previous_line = current_line;
675 /* Discard data for non-visible lines. It will be calculated as needed */
676 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
677 EDIT_InvalidateUniscribeData_linedef(current_line);
679 current_line = current_line->next;
680 line_index++;
681 } while (previous_line->ending != END_0);
683 /* Finish adjusting line indexes by delta or remove hanging lines */
684 if (previous_line->ending == END_0)
686 LINEDEF *pnext = NULL;
688 previous_line->next = NULL;
689 while (current_line)
691 pnext = current_line->next;
692 EDIT_InvalidateUniscribeData_linedef(current_line);
693 heap_free(current_line);
694 current_line = pnext;
695 es->line_count--;
698 else if (delta != 0)
700 while (current_line)
702 current_line->index += delta;
703 current_line = current_line->next;
707 /* Calculate rest of modification rectangle */
708 if (hrgn)
710 HRGN tmphrgn;
712 * We calculate two rectangles. One for the first line which may have
713 * an indent with respect to the format rect. The other is a format-width
714 * rectangle that spans the rest of the lines that changed or moved.
716 rc.top = es->format_rect.top + nstart_line * es->line_height -
717 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
718 rc.bottom = rc.top + es->line_height;
719 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
720 rc.left = es->format_rect.left;
721 else
722 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
723 rc.right = es->format_rect.right;
724 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
726 rc.top = rc.bottom;
727 rc.left = es->format_rect.left;
728 rc.right = es->format_rect.right;
730 * If lines were added or removed we must re-paint the remainder of the
731 * lines since the remaining lines were either shifted up or down.
733 if (line_count < es->line_count) /* We added lines */
734 rc.bottom = es->line_count * es->line_height;
735 else if (line_count > es->line_count) /* We removed lines */
736 rc.bottom = line_count * es->line_height;
737 else
738 rc.bottom = line_index * es->line_height;
739 rc.bottom += es->format_rect.top;
740 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
741 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
742 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
743 DeleteObject(tmphrgn);
747 /*********************************************************************
749 * EDIT_CalcLineWidth_SL
752 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
754 EDIT_UpdateUniscribeData(es, NULL, 0);
755 if (es->ssa)
757 const SIZE *size;
758 size = ScriptString_pSize(es->ssa);
759 es->text_width = size->cx;
761 else
762 es->text_width = 0;
765 /*********************************************************************
767 * EDIT_CharFromPos
769 * Beware: This is not the function called on EM_CHARFROMPOS
770 * The position _can_ be outside the formatting / client
771 * rectangle
772 * The return value is only the character index
775 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
777 INT index;
779 if (es->style & ES_MULTILINE) {
780 int trailing;
781 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
782 INT line_index = 0;
783 LINEDEF *line_def = es->first_line_def;
784 EDIT_UpdateUniscribeData(es, NULL, line);
785 while ((line > 0) && line_def->next) {
786 line_index += line_def->length;
787 line_def = line_def->next;
788 line--;
791 x += es->x_offset - es->format_rect.left;
792 if (es->style & ES_RIGHT)
793 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
794 else if (es->style & ES_CENTER)
795 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
796 if (x >= line_def->width) {
797 if (after_wrap)
798 *after_wrap = (line_def->ending == END_WRAP);
799 return line_index + line_def->net_length;
801 if (x <= 0 || !line_def->ssa) {
802 if (after_wrap)
803 *after_wrap = FALSE;
804 return line_index;
807 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
808 if (trailing) index++;
809 index += line_index;
810 if (after_wrap)
811 *after_wrap = ((index == line_index + line_def->net_length) &&
812 (line_def->ending == END_WRAP));
813 } else {
814 INT xoff = 0;
815 INT trailing;
816 if (after_wrap)
817 *after_wrap = FALSE;
818 x -= es->format_rect.left;
819 if (!x)
820 return es->x_offset;
822 if (!es->x_offset)
824 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
825 if (es->style & ES_RIGHT)
826 x -= indent;
827 else if (es->style & ES_CENTER)
828 x -= indent / 2;
831 EDIT_UpdateUniscribeData(es, NULL, 0);
832 if (es->x_offset)
834 if (es->ssa)
836 if (es->x_offset>= get_text_length(es))
838 const SIZE *size;
839 size = ScriptString_pSize(es->ssa);
840 xoff = size->cx;
842 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
844 else
845 xoff = 0;
847 if (x < 0)
849 if (x + xoff > 0 || !es->ssa)
851 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
852 if (trailing) index++;
854 else
855 index = 0;
857 else
859 if (x)
861 const SIZE *size = NULL;
862 if (es->ssa)
863 size = ScriptString_pSize(es->ssa);
864 if (!size)
865 index = 0;
866 else if (x > size->cx)
867 index = get_text_length(es);
868 else if (es->ssa)
870 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
871 if (trailing) index++;
873 else
874 index = 0;
876 else
877 index = es->x_offset;
880 return index;
884 /*********************************************************************
886 * EDIT_ConfinePoint
888 * adjusts the point to be within the formatting rectangle
889 * (so CharFromPos returns the nearest _visible_ character)
892 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
894 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
895 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
899 /*********************************************************************
901 * EM_LINEFROMCHAR
904 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
906 INT line;
907 LINEDEF *line_def;
909 if (!(es->style & ES_MULTILINE))
910 return 0;
911 if (index > (INT)get_text_length(es))
912 return es->line_count - 1;
913 if (index == -1)
914 index = min(es->selection_start, es->selection_end);
916 line = 0;
917 line_def = es->first_line_def;
918 index -= line_def->length;
919 while ((index >= 0) && line_def->next) {
920 line++;
921 line_def = line_def->next;
922 index -= line_def->length;
924 return line;
928 /*********************************************************************
930 * EM_LINEINDEX
933 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
935 INT line_index;
936 const LINEDEF *line_def;
938 if (!(es->style & ES_MULTILINE))
939 return 0;
940 if (line >= es->line_count)
941 return -1;
943 line_index = 0;
944 line_def = es->first_line_def;
945 if (line == -1) {
946 INT index = es->selection_end - line_def->length;
947 while ((index >= 0) && line_def->next) {
948 line_index += line_def->length;
949 line_def = line_def->next;
950 index -= line_def->length;
952 } else {
953 while (line > 0) {
954 line_index += line_def->length;
955 line_def = line_def->next;
956 line--;
959 return line_index;
963 /*********************************************************************
965 * EM_LINELENGTH
968 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
970 LINEDEF *line_def;
972 if (!(es->style & ES_MULTILINE))
973 return get_text_length(es);
975 if (index == -1) {
976 /* get the number of remaining non-selected chars of selected lines */
977 INT32 l; /* line number */
978 INT32 li; /* index of first char in line */
979 INT32 count;
980 l = EDIT_EM_LineFromChar(es, es->selection_start);
981 /* # chars before start of selection area */
982 count = es->selection_start - EDIT_EM_LineIndex(es, l);
983 l = EDIT_EM_LineFromChar(es, es->selection_end);
984 /* # chars after end of selection */
985 li = EDIT_EM_LineIndex(es, l);
986 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
987 return count;
989 line_def = es->first_line_def;
990 index -= line_def->length;
991 while ((index >= 0) && line_def->next) {
992 line_def = line_def->next;
993 index -= line_def->length;
995 return line_def->net_length;
999 /*********************************************************************
1001 * EM_POSFROMCHAR
1004 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1006 INT len = get_text_length(es);
1007 INT l;
1008 INT li;
1009 INT x = 0;
1010 INT y = 0;
1011 INT w;
1012 INT lw;
1013 LINEDEF *line_def;
1015 index = min(index, len);
1016 if (es->style & ES_MULTILINE) {
1017 l = EDIT_EM_LineFromChar(es, index);
1018 EDIT_UpdateUniscribeData(es, NULL, l);
1020 y = (l - es->y_offset) * es->line_height;
1021 li = EDIT_EM_LineIndex(es, l);
1022 if (after_wrap && (li == index) && l) {
1023 INT l2 = l - 1;
1024 line_def = es->first_line_def;
1025 while (l2) {
1026 line_def = line_def->next;
1027 l2--;
1029 if (line_def->ending == END_WRAP) {
1030 l--;
1031 y -= es->line_height;
1032 li = EDIT_EM_LineIndex(es, l);
1036 line_def = es->first_line_def;
1037 while (line_def->index != li)
1038 line_def = line_def->next;
1040 lw = line_def->width;
1041 w = es->format_rect.right - es->format_rect.left;
1042 if (line_def->ssa)
1044 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1045 x -= es->x_offset;
1047 else
1048 x = es->x_offset;
1050 if (es->style & ES_RIGHT)
1051 x = w - (lw - x);
1052 else if (es->style & ES_CENTER)
1053 x += (w - lw) / 2;
1054 } else {
1055 INT xoff = 0;
1056 INT xi = 0;
1057 EDIT_UpdateUniscribeData(es, NULL, 0);
1058 if (es->x_offset)
1060 if (es->ssa)
1062 if (es->x_offset >= get_text_length(es))
1064 int leftover = es->x_offset - get_text_length(es);
1065 if (es->ssa)
1067 const SIZE *size;
1068 size = ScriptString_pSize(es->ssa);
1069 xoff = size->cx;
1071 else
1072 xoff = 0;
1073 xoff += es->char_width * leftover;
1075 else
1076 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1078 else
1079 xoff = 0;
1081 if (index)
1083 if (index >= get_text_length(es))
1085 if (es->ssa)
1087 const SIZE *size;
1088 size = ScriptString_pSize(es->ssa);
1089 xi = size->cx;
1091 else
1092 xi = 0;
1094 else if (es->ssa)
1095 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1096 else
1097 xi = 0;
1099 x = xi - xoff;
1101 if (index >= es->x_offset) {
1102 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1104 w = es->format_rect.right - es->format_rect.left;
1105 if (w > es->text_width)
1107 if (es->style & ES_RIGHT)
1108 x += w - es->text_width;
1109 else if (es->style & ES_CENTER)
1110 x += (w - es->text_width) / 2;
1114 y = 0;
1116 x += es->format_rect.left;
1117 y += es->format_rect.top;
1118 return MAKELONG((INT16)x, (INT16)y);
1122 /*********************************************************************
1124 * EDIT_GetLineRect
1126 * Calculates the bounding rectangle for a line from a starting
1127 * column to an ending column.
1130 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1132 SCRIPT_STRING_ANALYSIS ssa;
1133 INT line_index = 0;
1134 INT pt1, pt2, pt3;
1136 if (es->style & ES_MULTILINE)
1138 const LINEDEF *line_def = NULL;
1139 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1140 if (line >= es->line_count)
1141 return;
1143 line_def = es->first_line_def;
1144 if (line == -1) {
1145 INT index = es->selection_end - line_def->length;
1146 while ((index >= 0) && line_def->next) {
1147 line_index += line_def->length;
1148 line_def = line_def->next;
1149 index -= line_def->length;
1151 } else {
1152 while (line > 0) {
1153 line_index += line_def->length;
1154 line_def = line_def->next;
1155 line--;
1158 ssa = line_def->ssa;
1160 else
1162 line_index = 0;
1163 rc->top = es->format_rect.top;
1164 ssa = es->ssa;
1167 rc->bottom = rc->top + es->line_height;
1168 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1169 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1170 if (ssa)
1172 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1173 pt3+=es->format_rect.left;
1175 else pt3 = pt1;
1176 rc->right = max(max(pt1 , pt2),pt3);
1177 rc->left = min(min(pt1, pt2),pt3);
1181 static inline void text_buffer_changed(EDITSTATE *es)
1183 es->text_length = (UINT)-1;
1185 heap_free( es->logAttr );
1186 es->logAttr = NULL;
1187 EDIT_InvalidateUniscribeData(es);
1190 /*********************************************************************
1191 * EDIT_LockBuffer
1194 static void EDIT_LockBuffer(EDITSTATE *es)
1196 if (!es->text)
1198 if (!es->hloc32W)
1199 return;
1201 es->text = LocalLock(es->hloc32W);
1204 es->lock_count++;
1208 /*********************************************************************
1210 * EDIT_UnlockBuffer
1213 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1215 /* Edit window might be already destroyed */
1216 if (!IsWindow(es->hwndSelf))
1218 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1219 return;
1222 if (!es->lock_count)
1224 ERR("lock_count == 0 ... please report\n");
1225 return;
1228 if (!es->text)
1230 ERR("es->text == 0 ... please report\n");
1231 return;
1234 if (force || (es->lock_count == 1))
1236 if (es->hloc32W)
1238 LocalUnlock(es->hloc32W);
1239 es->text = NULL;
1241 else
1243 ERR("no buffer ... please report\n");
1244 return;
1249 es->lock_count--;
1253 /*********************************************************************
1255 * EDIT_MakeFit
1257 * Try to fit size + 1 characters in the buffer.
1259 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1261 HLOCAL hNew32W;
1263 if (size <= es->buffer_size)
1264 return TRUE;
1266 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1268 /* Force edit to unlock its buffer. es->text now NULL */
1269 EDIT_UnlockBuffer(es, TRUE);
1271 if (es->hloc32W) {
1272 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1273 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1274 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1275 es->hloc32W = hNew32W;
1276 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1280 EDIT_LockBuffer(es);
1282 if (es->buffer_size < size) {
1283 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1284 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1285 return FALSE;
1286 } else {
1287 TRACE("We now have %d+1\n", es->buffer_size);
1288 return TRUE;
1293 /*********************************************************************
1295 * EDIT_MakeUndoFit
1297 * Try to fit size + 1 bytes in the undo buffer.
1300 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1302 UINT alloc_size;
1304 if (size <= es->undo_buffer_size)
1305 return TRUE;
1307 TRACE("trying to ReAlloc to %d+1\n", size);
1309 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1310 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1311 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1312 return TRUE;
1314 else
1316 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1317 return FALSE;
1322 /*********************************************************************
1324 * EDIT_UpdateTextRegion
1327 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1329 if (es->flags & EF_UPDATE) {
1330 es->flags &= ~EF_UPDATE;
1331 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1333 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1337 /*********************************************************************
1339 * EDIT_UpdateText
1342 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1344 if (es->flags & EF_UPDATE) {
1345 es->flags &= ~EF_UPDATE;
1346 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1348 InvalidateRect(es->hwndSelf, rc, bErase);
1351 /*********************************************************************
1353 * EDIT_SL_InvalidateText
1355 * Called from EDIT_InvalidateText().
1356 * Does the job for single-line controls only.
1359 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1361 RECT line_rect;
1362 RECT rc;
1364 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1365 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1366 EDIT_UpdateText(es, &rc, TRUE);
1369 /*********************************************************************
1371 * EDIT_ML_InvalidateText
1373 * Called from EDIT_InvalidateText().
1374 * Does the job for multi-line controls only.
1377 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1379 INT vlc = get_vertical_line_count(es);
1380 INT sl = EDIT_EM_LineFromChar(es, start);
1381 INT el = EDIT_EM_LineFromChar(es, end);
1382 INT sc;
1383 INT ec;
1384 RECT rc1;
1385 RECT rcWnd;
1386 RECT rcLine;
1387 RECT rcUpdate;
1388 INT l;
1390 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1391 return;
1393 sc = start - EDIT_EM_LineIndex(es, sl);
1394 ec = end - EDIT_EM_LineIndex(es, el);
1395 if (sl < es->y_offset) {
1396 sl = es->y_offset;
1397 sc = 0;
1399 if (el > es->y_offset + vlc) {
1400 el = es->y_offset + vlc;
1401 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1403 GetClientRect(es->hwndSelf, &rc1);
1404 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1405 if (sl == el) {
1406 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1407 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1408 EDIT_UpdateText(es, &rcUpdate, TRUE);
1409 } else {
1410 EDIT_GetLineRect(es, sl, sc,
1411 EDIT_EM_LineLength(es,
1412 EDIT_EM_LineIndex(es, sl)),
1413 &rcLine);
1414 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1415 EDIT_UpdateText(es, &rcUpdate, TRUE);
1416 for (l = sl + 1 ; l < el ; l++) {
1417 EDIT_GetLineRect(es, l, 0,
1418 EDIT_EM_LineLength(es,
1419 EDIT_EM_LineIndex(es, l)),
1420 &rcLine);
1421 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1422 EDIT_UpdateText(es, &rcUpdate, TRUE);
1424 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1425 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1426 EDIT_UpdateText(es, &rcUpdate, TRUE);
1431 /*********************************************************************
1433 * EDIT_InvalidateText
1435 * Invalidate the text from offset start up to, but not including,
1436 * offset end. Useful for (re)painting the selection.
1437 * Regions outside the linewidth are not invalidated.
1438 * end == -1 means end == TextLength.
1439 * start and end need not be ordered.
1442 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1444 if (end == start)
1445 return;
1447 if (end == -1)
1448 end = get_text_length(es);
1450 if (end < start) {
1451 INT tmp = start;
1452 start = end;
1453 end = tmp;
1456 if (es->style & ES_MULTILINE)
1457 EDIT_ML_InvalidateText(es, start, end);
1458 else
1459 EDIT_SL_InvalidateText(es, start, end);
1463 /*********************************************************************
1465 * EDIT_EM_SetSel
1467 * note: unlike the specs say: the order of start and end
1468 * _is_ preserved in Windows. (i.e. start can be > end)
1469 * In other words: this handler is OK
1472 static BOOL EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1474 UINT old_start = es->selection_start;
1475 UINT old_end = es->selection_end;
1476 UINT len = get_text_length(es);
1478 if (start == old_start && end == old_end)
1479 return FALSE;
1481 if (start == (UINT)-1) {
1482 start = es->selection_end;
1483 end = es->selection_end;
1484 } else {
1485 start = min(start, len);
1486 end = min(end, len);
1488 es->selection_start = start;
1489 es->selection_end = end;
1490 if (after_wrap)
1491 es->flags |= EF_AFTER_WRAP;
1492 else
1493 es->flags &= ~EF_AFTER_WRAP;
1494 /* Compute the necessary invalidation region. */
1495 /* Note that we don't need to invalidate regions which have
1496 * "never" been selected, or those which are "still" selected.
1497 * In fact, every time we hit a selection boundary, we can
1498 * *toggle* whether we need to invalidate. Thus we can optimize by
1499 * *sorting* the interval endpoints. Let's assume that we sort them
1500 * in this order:
1501 * start <= end <= old_start <= old_end
1502 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1503 * in 5 comparisons; i.e. it is impossible to do better than the
1504 * following: */
1505 ORDER_UINT(end, old_end);
1506 ORDER_UINT(start, old_start);
1507 ORDER_UINT(old_start, old_end);
1508 ORDER_UINT(start, end);
1509 /* Note that at this point 'end' and 'old_start' are not in order, but
1510 * start is definitely the min. and old_end is definitely the max. */
1511 if (end != old_start)
1514 * One can also do
1515 * ORDER_UINT32(end, old_start);
1516 * EDIT_InvalidateText(es, start, end);
1517 * EDIT_InvalidateText(es, old_start, old_end);
1518 * in place of the following if statement.
1519 * (That would complete the optimal five-comparison four-element sort.)
1521 if (old_start > end )
1523 EDIT_InvalidateText(es, start, end);
1524 EDIT_InvalidateText(es, old_start, old_end);
1526 else
1528 EDIT_InvalidateText(es, start, old_start);
1529 EDIT_InvalidateText(es, end, old_end);
1532 else EDIT_InvalidateText(es, start, old_end);
1534 return TRUE;
1538 /*********************************************************************
1540 * EDIT_UpdateScrollInfo
1543 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1545 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1547 SCROLLINFO si;
1548 si.cbSize = sizeof(SCROLLINFO);
1549 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1550 si.nMin = 0;
1551 si.nMax = es->line_count - 1;
1552 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1553 si.nPos = es->y_offset;
1554 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1555 si.nMin, si.nMax, si.nPage, si.nPos);
1556 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1559 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1561 SCROLLINFO si;
1562 si.cbSize = sizeof(SCROLLINFO);
1563 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1564 si.nMin = 0;
1565 si.nMax = es->text_width - 1;
1566 si.nPage = es->format_rect.right - es->format_rect.left;
1567 si.nPos = es->x_offset;
1568 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1569 si.nMin, si.nMax, si.nPage, si.nPos);
1570 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1575 /*********************************************************************
1577 * EDIT_EM_LineScroll_internal
1579 * Version of EDIT_EM_LineScroll for internal use.
1580 * It doesn't refuse if ES_MULTILINE is set and assumes that
1581 * dx is in pixels, dy - in lines.
1584 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1586 INT nyoff;
1587 INT x_offset_in_pixels;
1588 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1589 es->line_height;
1591 if (es->style & ES_MULTILINE)
1593 x_offset_in_pixels = es->x_offset;
1595 else
1597 dy = 0;
1598 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1601 if (-dx > x_offset_in_pixels)
1602 dx = -x_offset_in_pixels;
1603 if (dx > es->text_width - x_offset_in_pixels)
1604 dx = es->text_width - x_offset_in_pixels;
1605 nyoff = max(0, es->y_offset + dy);
1606 if (nyoff >= es->line_count - lines_per_page)
1607 nyoff = max(0, es->line_count - lines_per_page);
1608 dy = (es->y_offset - nyoff) * es->line_height;
1609 if (dx || dy) {
1610 RECT rc1;
1611 RECT rc;
1613 es->y_offset = nyoff;
1614 if(es->style & ES_MULTILINE)
1615 es->x_offset += dx;
1616 else
1617 es->x_offset += dx / es->char_width;
1619 GetClientRect(es->hwndSelf, &rc1);
1620 IntersectRect(&rc, &rc1, &es->format_rect);
1621 ScrollWindowEx(es->hwndSelf, -dx, dy,
1622 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1623 /* force scroll info update */
1624 EDIT_UpdateScrollInfo(es);
1626 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1627 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1628 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1629 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1630 return TRUE;
1633 /*********************************************************************
1635 * EM_LINESCROLL
1637 * NOTE: dx is in average character widths, dy - in lines;
1640 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1642 if (!(es->style & ES_MULTILINE))
1643 return FALSE;
1645 dx *= es->char_width;
1646 return EDIT_EM_LineScroll_internal(es, dx, dy);
1650 /*********************************************************************
1652 * EM_SCROLL
1655 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1657 INT dy;
1659 if (!(es->style & ES_MULTILINE))
1660 return (LRESULT)FALSE;
1662 dy = 0;
1664 switch (action) {
1665 case SB_LINEUP:
1666 if (es->y_offset)
1667 dy = -1;
1668 break;
1669 case SB_LINEDOWN:
1670 if (es->y_offset < es->line_count - 1)
1671 dy = 1;
1672 break;
1673 case SB_PAGEUP:
1674 if (es->y_offset)
1675 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1676 break;
1677 case SB_PAGEDOWN:
1678 if (es->y_offset < es->line_count - 1)
1679 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1680 break;
1681 default:
1682 return (LRESULT)FALSE;
1684 if (dy) {
1685 INT vlc = get_vertical_line_count(es);
1686 /* check if we are going to move too far */
1687 if(es->y_offset + dy > es->line_count - vlc)
1688 dy = max(es->line_count - vlc, 0) - es->y_offset;
1690 /* Notification is done in EDIT_EM_LineScroll */
1691 if(dy) {
1692 EDIT_EM_LineScroll(es, 0, dy);
1693 return MAKELONG(dy, TRUE);
1697 return (LRESULT)FALSE;
1701 /*********************************************************************
1703 * EDIT_SetCaretPos
1706 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1707 BOOL after_wrap)
1709 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1710 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1711 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1715 /*********************************************************************
1717 * EM_SCROLLCARET
1720 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1722 if (es->style & ES_MULTILINE) {
1723 INT l;
1724 INT vlc;
1725 INT ww;
1726 INT cw = es->char_width;
1727 INT x;
1728 INT dy = 0;
1729 INT dx = 0;
1731 l = EDIT_EM_LineFromChar(es, es->selection_end);
1732 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1733 vlc = get_vertical_line_count(es);
1734 if (l >= es->y_offset + vlc)
1735 dy = l - vlc + 1 - es->y_offset;
1736 if (l < es->y_offset)
1737 dy = l - es->y_offset;
1738 ww = es->format_rect.right - es->format_rect.left;
1739 if (x < es->format_rect.left)
1740 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1741 if (x > es->format_rect.right)
1742 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1743 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1745 /* check if we are going to move too far */
1746 if(es->x_offset + dx + ww > es->text_width)
1747 dx = es->text_width - ww - es->x_offset;
1748 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1749 EDIT_EM_LineScroll_internal(es, dx, dy);
1751 } else {
1752 INT x;
1753 INT goal;
1754 INT format_width;
1756 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1757 format_width = es->format_rect.right - es->format_rect.left;
1758 if (x < es->format_rect.left) {
1759 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1760 do {
1761 es->x_offset--;
1762 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1763 } while ((x < goal) && es->x_offset);
1764 /* FIXME: use ScrollWindow() somehow to improve performance */
1765 EDIT_UpdateText(es, NULL, TRUE);
1766 } else if (x > es->format_rect.right) {
1767 INT x_last;
1768 INT len = get_text_length(es);
1769 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1770 do {
1771 es->x_offset++;
1772 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1773 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1774 } while ((x > goal) && (x_last > es->format_rect.right));
1775 /* FIXME: use ScrollWindow() somehow to improve performance */
1776 EDIT_UpdateText(es, NULL, TRUE);
1780 if(es->flags & EF_FOCUSED)
1781 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1785 /*********************************************************************
1787 * EDIT_MoveBackward
1790 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1792 INT e = es->selection_end;
1794 if (e) {
1795 e--;
1796 if ((es->style & ES_MULTILINE) && e &&
1797 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1798 e--;
1799 if (e && (es->text[e - 1] == '\r'))
1800 e--;
1803 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1804 EDIT_EM_ScrollCaret(es);
1808 /*********************************************************************
1810 * EDIT_MoveDown_ML
1812 * Only for multi line controls
1813 * Move the caret one line down, on a column with the nearest
1814 * x coordinate on the screen (might be a different column).
1817 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1819 INT s = es->selection_start;
1820 INT e = es->selection_end;
1821 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1822 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1823 INT x = (short)LOWORD(pos);
1824 INT y = (short)HIWORD(pos);
1826 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1827 if (!extend)
1828 s = e;
1829 EDIT_EM_SetSel(es, s, e, after_wrap);
1830 EDIT_EM_ScrollCaret(es);
1834 /*********************************************************************
1836 * EDIT_MoveEnd
1839 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1841 BOOL after_wrap = FALSE;
1842 INT e;
1844 /* Pass a high value in x to make sure of receiving the end of the line */
1845 if (!ctrl && (es->style & ES_MULTILINE))
1846 e = EDIT_CharFromPos(es, 0x3fffffff,
1847 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1848 else
1849 e = get_text_length(es);
1850 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1851 EDIT_EM_ScrollCaret(es);
1855 /*********************************************************************
1857 * EDIT_MoveForward
1860 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1862 INT e = es->selection_end;
1864 if (es->text[e]) {
1865 e++;
1866 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1867 if (es->text[e] == '\n')
1868 e++;
1869 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1870 e += 2;
1873 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1874 EDIT_EM_ScrollCaret(es);
1878 /*********************************************************************
1880 * EDIT_MoveHome
1882 * Home key: move to beginning of line.
1885 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1887 INT e;
1889 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1890 if (!ctrl && (es->style & ES_MULTILINE))
1891 e = EDIT_CharFromPos(es, -es->x_offset,
1892 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1893 else
1894 e = 0;
1895 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1896 EDIT_EM_ScrollCaret(es);
1900 /*********************************************************************
1902 * EDIT_MovePageDown_ML
1904 * Only for multi line controls
1905 * Move the caret one page down, on a column with the nearest
1906 * x coordinate on the screen (might be a different column).
1909 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1911 INT s = es->selection_start;
1912 INT e = es->selection_end;
1913 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1914 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1915 INT x = (short)LOWORD(pos);
1916 INT y = (short)HIWORD(pos);
1918 e = EDIT_CharFromPos(es, x,
1919 y + (es->format_rect.bottom - es->format_rect.top),
1920 &after_wrap);
1921 if (!extend)
1922 s = e;
1923 EDIT_EM_SetSel(es, s, e, after_wrap);
1924 EDIT_EM_ScrollCaret(es);
1928 /*********************************************************************
1930 * EDIT_MovePageUp_ML
1932 * Only for multi line controls
1933 * Move the caret one page up, on a column with the nearest
1934 * x coordinate on the screen (might be a different column).
1937 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1939 INT s = es->selection_start;
1940 INT e = es->selection_end;
1941 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1942 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1943 INT x = (short)LOWORD(pos);
1944 INT y = (short)HIWORD(pos);
1946 e = EDIT_CharFromPos(es, x,
1947 y - (es->format_rect.bottom - es->format_rect.top),
1948 &after_wrap);
1949 if (!extend)
1950 s = e;
1951 EDIT_EM_SetSel(es, s, e, after_wrap);
1952 EDIT_EM_ScrollCaret(es);
1956 /*********************************************************************
1958 * EDIT_MoveUp_ML
1960 * Only for multi line controls
1961 * Move the caret one line up, on a column with the nearest
1962 * x coordinate on the screen (might be a different column).
1965 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1967 INT s = es->selection_start;
1968 INT e = es->selection_end;
1969 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1970 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1971 INT x = (short)LOWORD(pos);
1972 INT y = (short)HIWORD(pos);
1974 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1975 if (!extend)
1976 s = e;
1977 EDIT_EM_SetSel(es, s, e, after_wrap);
1978 EDIT_EM_ScrollCaret(es);
1982 /*********************************************************************
1984 * EDIT_MoveWordBackward
1987 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1989 INT s = es->selection_start;
1990 INT e = es->selection_end;
1991 INT l;
1992 INT ll;
1993 INT li;
1995 l = EDIT_EM_LineFromChar(es, e);
1996 ll = EDIT_EM_LineLength(es, e);
1997 li = EDIT_EM_LineIndex(es, l);
1998 if (e - li == 0) {
1999 if (l) {
2000 li = EDIT_EM_LineIndex(es, l - 1);
2001 e = li + EDIT_EM_LineLength(es, li);
2003 } else {
2004 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2006 if (!extend)
2007 s = e;
2008 EDIT_EM_SetSel(es, s, e, FALSE);
2009 EDIT_EM_ScrollCaret(es);
2013 /*********************************************************************
2015 * EDIT_MoveWordForward
2018 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2020 INT s = es->selection_start;
2021 INT e = es->selection_end;
2022 INT l;
2023 INT ll;
2024 INT li;
2026 l = EDIT_EM_LineFromChar(es, e);
2027 ll = EDIT_EM_LineLength(es, e);
2028 li = EDIT_EM_LineIndex(es, l);
2029 if (e - li == ll) {
2030 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2031 e = EDIT_EM_LineIndex(es, l + 1);
2032 } else {
2033 e = li + EDIT_CallWordBreakProc(es,
2034 li, e - li + 1, ll, WB_RIGHT);
2036 if (!extend)
2037 s = e;
2038 EDIT_EM_SetSel(es, s, e, FALSE);
2039 EDIT_EM_ScrollCaret(es);
2043 /*********************************************************************
2045 * EDIT_PaintText
2048 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2050 COLORREF BkColor;
2051 COLORREF TextColor;
2052 LOGFONTW underline_font;
2053 HFONT hUnderline = 0;
2054 HFONT old_font = 0;
2055 INT ret;
2056 INT li;
2057 INT BkMode;
2058 SIZE size;
2060 if (!count)
2061 return 0;
2062 BkMode = GetBkMode(dc);
2063 BkColor = GetBkColor(dc);
2064 TextColor = GetTextColor(dc);
2065 if (rev) {
2066 if (es->composition_len == 0)
2068 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2069 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2070 SetBkMode( dc, OPAQUE);
2072 else
2074 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2075 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2076 underline_font.lfUnderline = TRUE;
2077 hUnderline = CreateFontIndirectW(&underline_font);
2078 old_font = SelectObject(dc,hUnderline);
2081 li = EDIT_EM_LineIndex(es, line);
2082 if (es->style & ES_MULTILINE) {
2083 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2084 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2085 } else {
2086 TextOutW(dc, x, y, es->text + li + col, count);
2087 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2088 ret = size.cx;
2090 if (rev) {
2091 if (es->composition_len == 0)
2093 SetBkColor(dc, BkColor);
2094 SetTextColor(dc, TextColor);
2095 SetBkMode( dc, BkMode);
2097 else
2099 if (old_font)
2100 SelectObject(dc,old_font);
2101 if (hUnderline)
2102 DeleteObject(hUnderline);
2105 return ret;
2109 /*********************************************************************
2111 * EDIT_PaintLine
2114 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2116 INT s = 0;
2117 INT e = 0;
2118 INT li = 0;
2119 INT ll = 0;
2120 INT x;
2121 INT y;
2122 LRESULT pos;
2123 SCRIPT_STRING_ANALYSIS ssa;
2125 if (es->style & ES_MULTILINE) {
2126 INT vlc = get_vertical_line_count(es);
2128 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2129 return;
2130 } else if (line)
2131 return;
2133 TRACE("line=%d\n", line);
2135 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2136 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2137 x = (short)LOWORD(pos);
2138 y = (short)HIWORD(pos);
2140 if (es->style & ES_MULTILINE)
2142 int line_idx = line;
2143 x = -es->x_offset;
2144 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2146 LINEDEF *line_def = es->first_line_def;
2147 int w, lw;
2149 while (line_def && line_idx)
2151 line_def = line_def->next;
2152 line_idx--;
2154 w = es->format_rect.right - es->format_rect.left;
2155 lw = line_def->width;
2157 if (es->style & ES_RIGHT)
2158 x = w - (lw - x);
2159 else if (es->style & ES_CENTER)
2160 x += (w - lw) / 2;
2162 x += es->format_rect.left;
2165 if (rev)
2167 li = EDIT_EM_LineIndex(es, line);
2168 ll = EDIT_EM_LineLength(es, li);
2169 s = min(es->selection_start, es->selection_end);
2170 e = max(es->selection_start, es->selection_end);
2171 s = min(li + ll, max(li, s));
2172 e = min(li + ll, max(li, e));
2175 if (ssa)
2176 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2177 else if (rev && (s != e) &&
2178 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2179 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2180 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2181 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2182 } else
2183 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2187 /*********************************************************************
2189 * EDIT_AdjustFormatRect
2191 * Adjusts the format rectangle for the current font and the
2192 * current client rectangle.
2195 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2197 RECT ClientRect;
2199 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2200 if (es->style & ES_MULTILINE)
2202 INT fw, vlc, max_x_offset, max_y_offset;
2204 vlc = get_vertical_line_count(es);
2205 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2207 /* correct es->x_offset */
2208 fw = es->format_rect.right - es->format_rect.left;
2209 max_x_offset = es->text_width - fw;
2210 if(max_x_offset < 0) max_x_offset = 0;
2211 if(es->x_offset > max_x_offset)
2212 es->x_offset = max_x_offset;
2214 /* correct es->y_offset */
2215 max_y_offset = es->line_count - vlc;
2216 if(max_y_offset < 0) max_y_offset = 0;
2217 if(es->y_offset > max_y_offset)
2218 es->y_offset = max_y_offset;
2220 /* force scroll info update */
2221 EDIT_UpdateScrollInfo(es);
2223 else
2224 /* Windows doesn't care to fix text placement for SL controls */
2225 es->format_rect.bottom = es->format_rect.top + es->line_height;
2227 /* Always stay within the client area */
2228 GetClientRect(es->hwndSelf, &ClientRect);
2229 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2231 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2232 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2234 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2238 /*********************************************************************
2240 * EDIT_SetRectNP
2242 * note: this is not (exactly) the handler called on EM_SETRECTNP
2243 * it is also used to set the rect of a single line control
2246 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2248 LONG_PTR ExStyle;
2249 INT bw, bh;
2250 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2252 CopyRect(&es->format_rect, rc);
2254 if (ExStyle & WS_EX_CLIENTEDGE) {
2255 es->format_rect.left++;
2256 es->format_rect.right--;
2258 if (es->format_rect.bottom - es->format_rect.top
2259 >= es->line_height + 2)
2261 es->format_rect.top++;
2262 es->format_rect.bottom--;
2265 else if (es->style & WS_BORDER) {
2266 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2267 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2268 InflateRect(&es->format_rect, -bw, 0);
2269 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2270 InflateRect(&es->format_rect, 0, -bh);
2273 es->format_rect.left += es->left_margin;
2274 es->format_rect.right -= es->right_margin;
2275 EDIT_AdjustFormatRect(es);
2279 /*********************************************************************
2281 * EM_CHARFROMPOS
2283 * returns line number (not index) in high-order word of result.
2284 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2285 * to Richedit, not to the edit control. Original documentation is valid.
2286 * FIXME: do the specs mean to return -1 if outside client area or
2287 * if outside formatting rectangle ???
2290 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2292 POINT pt;
2293 RECT rc;
2294 INT index;
2296 pt.x = x;
2297 pt.y = y;
2298 GetClientRect(es->hwndSelf, &rc);
2299 if (!PtInRect(&rc, pt))
2300 return -1;
2302 index = EDIT_CharFromPos(es, x, y, NULL);
2303 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2307 /*********************************************************************
2309 * EM_FMTLINES
2311 * Enable or disable soft breaks.
2313 * This means: insert or remove the soft linebreak character (\r\r\n).
2314 * Take care to check if the text still fits the buffer after insertion.
2315 * If not, notify with EN_ERRSPACE.
2318 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2320 es->flags &= ~EF_USE_SOFTBRK;
2321 if (add_eol) {
2322 es->flags |= EF_USE_SOFTBRK;
2323 FIXME("soft break enabled, not implemented\n");
2325 return add_eol;
2329 /*********************************************************************
2331 * EM_GETHANDLE
2333 * Hopefully this won't fire back at us.
2334 * We always start with a fixed buffer in the local heap.
2335 * Despite of the documentation says that the local heap is used
2336 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2337 * buffer on the local heap.
2340 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2342 if (!(es->style & ES_MULTILINE))
2343 return 0;
2345 EDIT_UnlockBuffer(es, TRUE);
2347 /* The text buffer handle belongs to the app */
2348 es->hlocapp = es->hloc32W;
2350 TRACE("Returning %p, LocalSize() = %ld\n", es->hlocapp, LocalSize(es->hlocapp));
2351 return es->hlocapp;
2355 /*********************************************************************
2357 * EM_GETLINE
2360 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst)
2362 INT line_len, dst_len;
2363 LPWSTR src;
2364 INT i;
2366 if (es->style & ES_MULTILINE)
2368 if (line >= es->line_count)
2369 return 0;
2371 else
2372 line = 0;
2374 i = EDIT_EM_LineIndex(es, line);
2375 src = es->text + i;
2376 line_len = EDIT_EM_LineLength(es, i);
2377 dst_len = *(WORD *)dst;
2379 if (dst_len <= line_len)
2381 memcpy(dst, src, dst_len * sizeof(WCHAR));
2382 return dst_len;
2384 else /* Append 0 if enough space */
2386 memcpy(dst, src, line_len * sizeof(WCHAR));
2387 dst[line_len] = 0;
2388 return line_len;
2393 /*********************************************************************
2395 * EM_GETSEL
2398 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2400 UINT s = es->selection_start;
2401 UINT e = es->selection_end;
2403 ORDER_UINT(s, e);
2404 if (start)
2405 *start = s;
2406 if (end)
2407 *end = e;
2408 return MAKELONG(s, e);
2412 /*********************************************************************
2414 * EM_REPLACESEL
2416 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2419 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2420 BOOL send_update, BOOL honor_limit)
2422 UINT tl = get_text_length(es);
2423 UINT utl;
2424 UINT s;
2425 UINT e;
2426 UINT i;
2427 UINT size;
2428 LPWSTR p;
2429 HRGN hrgn = 0;
2430 LPWSTR buf = NULL;
2431 UINT bufl;
2433 TRACE("%s, can_undo %d, send_update %d\n",
2434 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2436 s = es->selection_start;
2437 e = es->selection_end;
2439 EDIT_InvalidateUniscribeData(es);
2440 if ((s == e) && !strl)
2441 return;
2443 ORDER_UINT(s, e);
2445 size = tl - (e - s) + strl;
2446 if (!size)
2447 es->text_width = 0;
2449 /* Issue the EN_MAXTEXT notification and continue with replacing text
2450 * so that buffer limit is honored. */
2451 if ((honor_limit) && (size > es->buffer_limit)) {
2452 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2453 /* Buffer limit can be smaller than the actual length of text in combobox */
2454 if (es->buffer_limit < (tl - (e-s)))
2455 strl = 0;
2456 else
2457 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2460 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2461 return;
2463 if (e != s) {
2464 /* there is something to be deleted */
2465 TRACE("deleting stuff.\n");
2466 bufl = e - s;
2467 buf = heap_alloc((bufl + 1) * sizeof(WCHAR));
2468 if (!buf) return;
2469 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2470 buf[bufl] = 0; /* ensure 0 termination */
2471 /* now delete */
2472 strcpyW(es->text + s, es->text + e);
2473 text_buffer_changed(es);
2475 if (strl) {
2476 /* there is an insertion */
2477 tl = get_text_length(es);
2478 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));
2479 for (p = es->text + tl ; p >= es->text + s ; p--)
2480 p[strl] = p[0];
2481 for (i = 0 , p = es->text + s ; i < strl ; i++)
2482 p[i] = lpsz_replace[i];
2483 if(es->style & ES_UPPERCASE)
2484 CharUpperBuffW(p, strl);
2485 else if(es->style & ES_LOWERCASE)
2486 CharLowerBuffW(p, strl);
2487 text_buffer_changed(es);
2489 if (es->style & ES_MULTILINE)
2491 INT st = min(es->selection_start, es->selection_end);
2492 INT vlc = get_vertical_line_count(es);
2494 hrgn = CreateRectRgn(0, 0, 0, 0);
2495 EDIT_BuildLineDefs_ML(es, st, st + strl,
2496 strl - abs(es->selection_end - es->selection_start), hrgn);
2497 /* if text is too long undo all changes */
2498 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2499 if (strl)
2500 strcpyW(es->text + e, es->text + e + strl);
2501 if (e != s)
2502 for (i = 0 , p = es->text ; i < e - s ; i++)
2503 p[i + s] = buf[i];
2504 text_buffer_changed(es);
2505 EDIT_BuildLineDefs_ML(es, s, e,
2506 abs(es->selection_end - es->selection_start) - strl, hrgn);
2507 strl = 0;
2508 e = s;
2509 hrgn = CreateRectRgn(0, 0, 0, 0);
2510 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2513 else {
2514 INT fw = es->format_rect.right - es->format_rect.left;
2515 EDIT_InvalidateUniscribeData(es);
2516 EDIT_CalcLineWidth_SL(es);
2517 /* remove chars that don't fit */
2518 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2519 while ((es->text_width > fw) && s + strl >= s) {
2520 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2521 strl--;
2522 es->text_length = -1;
2523 EDIT_InvalidateUniscribeData(es);
2524 EDIT_CalcLineWidth_SL(es);
2526 text_buffer_changed(es);
2527 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2531 if (e != s) {
2532 if (can_undo) {
2533 utl = strlenW(es->undo_text);
2534 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2535 /* undo-buffer is extended to the right */
2536 EDIT_MakeUndoFit(es, utl + e - s);
2537 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2538 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2539 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2540 /* undo-buffer is extended to the left */
2541 EDIT_MakeUndoFit(es, utl + e - s);
2542 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2543 p[e - s] = p[0];
2544 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2545 p[i] = buf[i];
2546 es->undo_position = s;
2547 } else {
2548 /* new undo-buffer */
2549 EDIT_MakeUndoFit(es, e - s);
2550 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2551 es->undo_text[e - s] = 0; /* ensure 0 termination */
2552 es->undo_position = s;
2554 /* any deletion makes the old insertion-undo invalid */
2555 es->undo_insert_count = 0;
2556 } else
2557 EDIT_EM_EmptyUndoBuffer(es);
2559 if (strl) {
2560 if (can_undo) {
2561 if ((s == es->undo_position) ||
2562 ((es->undo_insert_count) &&
2563 (s == es->undo_position + es->undo_insert_count)))
2565 * insertion is new and at delete position or
2566 * an extension to either left or right
2568 es->undo_insert_count += strl;
2569 else {
2570 /* new insertion undo */
2571 es->undo_position = s;
2572 es->undo_insert_count = strl;
2573 /* new insertion makes old delete-buffer invalid */
2574 *es->undo_text = '\0';
2576 } else
2577 EDIT_EM_EmptyUndoBuffer(es);
2580 heap_free(buf);
2582 s += strl;
2584 /* If text has been deleted and we're right or center aligned then scroll rightward */
2585 if (es->style & (ES_RIGHT | ES_CENTER))
2587 INT delta = strl - abs(es->selection_end - es->selection_start);
2589 if (delta < 0 && es->x_offset)
2591 if (abs(delta) > es->x_offset)
2592 es->x_offset = 0;
2593 else
2594 es->x_offset += delta;
2598 EDIT_EM_SetSel(es, s, s, FALSE);
2599 es->flags |= EF_MODIFIED;
2600 if (send_update) es->flags |= EF_UPDATE;
2601 if (hrgn)
2603 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2604 DeleteObject(hrgn);
2606 else
2607 EDIT_UpdateText(es, NULL, TRUE);
2609 EDIT_EM_ScrollCaret(es);
2611 /* force scroll info update */
2612 EDIT_UpdateScrollInfo(es);
2615 if(send_update || (es->flags & EF_UPDATE))
2617 es->flags &= ~EF_UPDATE;
2618 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2620 EDIT_InvalidateUniscribeData(es);
2624 /*********************************************************************
2626 * EM_SETHANDLE
2628 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2631 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2633 if (!(es->style & ES_MULTILINE))
2634 return;
2636 if (!hloc)
2637 return;
2639 EDIT_UnlockBuffer(es, TRUE);
2641 es->hloc32W = hloc;
2642 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2644 /* The text buffer handle belongs to the control */
2645 es->hlocapp = NULL;
2647 EDIT_LockBuffer(es);
2648 text_buffer_changed(es);
2650 es->x_offset = es->y_offset = 0;
2651 es->selection_start = es->selection_end = 0;
2652 EDIT_EM_EmptyUndoBuffer(es);
2653 es->flags &= ~EF_MODIFIED;
2654 es->flags &= ~EF_UPDATE;
2655 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2656 EDIT_UpdateText(es, NULL, TRUE);
2657 EDIT_EM_ScrollCaret(es);
2658 /* force scroll info update */
2659 EDIT_UpdateScrollInfo(es);
2663 /*********************************************************************
2665 * EM_SETLIMITTEXT
2667 * NOTE: this version currently implements WinNT limits
2670 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2672 if (!limit) limit = ~0u;
2673 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2674 es->buffer_limit = limit;
2678 /*********************************************************************
2680 * EM_SETMARGINS
2682 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2683 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2684 * margin according to the textmetrics of the current font.
2686 * When EC_USEFONTINFO is used in the non_cjk case the margins only
2687 * change if the edit control is equal to or larger than a certain
2688 * size. Though there is an exception for the empty client rect case
2689 * with small font sizes.
2691 static BOOL is_cjk(UINT charset)
2693 switch(charset)
2695 case SHIFTJIS_CHARSET:
2696 case HANGUL_CHARSET:
2697 case GB2312_CHARSET:
2698 case CHINESEBIG5_CHARSET:
2699 return TRUE;
2701 /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2702 * not by other versions including Win 10. */
2703 return FALSE;
2706 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2707 WORD left, WORD right, BOOL repaint)
2709 TEXTMETRICW tm;
2710 INT default_left_margin = 0; /* in pixels */
2711 INT default_right_margin = 0; /* in pixels */
2713 /* Set the default margins depending on the font */
2714 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2715 HDC dc = GetDC(es->hwndSelf);
2716 HFONT old_font = SelectObject(dc, es->font);
2717 LONG width = GdiGetCharDimensions(dc, &tm, NULL);
2718 RECT rc;
2720 /* The default margins are only non zero for TrueType or Vector fonts */
2721 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2722 if (!is_cjk(tm.tmCharSet)) {
2723 default_left_margin = width / 2;
2724 default_right_margin = width / 2;
2726 GetClientRect(es->hwndSelf, &rc);
2727 if (rc.right - rc.left < (width / 2 + width) * 2 &&
2728 (width >= 28 || !IsRectEmpty(&rc)) ) {
2729 default_left_margin = es->left_margin;
2730 default_right_margin = es->right_margin;
2732 } else {
2733 /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2734 default_left_margin = width / 2;
2735 default_right_margin = width / 2;
2738 SelectObject(dc, old_font);
2739 ReleaseDC(es->hwndSelf, dc);
2742 if (action & EC_LEFTMARGIN) {
2743 es->format_rect.left -= es->left_margin;
2744 if (left != EC_USEFONTINFO)
2745 es->left_margin = left;
2746 else
2747 es->left_margin = default_left_margin;
2748 es->format_rect.left += es->left_margin;
2751 if (action & EC_RIGHTMARGIN) {
2752 es->format_rect.right += es->right_margin;
2753 if (right != EC_USEFONTINFO)
2754 es->right_margin = right;
2755 else
2756 es->right_margin = default_right_margin;
2757 es->format_rect.right -= es->right_margin;
2760 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2761 EDIT_AdjustFormatRect(es);
2762 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2765 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2769 /*********************************************************************
2771 * EM_SETPASSWORDCHAR
2774 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2776 LONG style;
2778 if (es->style & ES_MULTILINE)
2779 return;
2781 if (es->password_char == c)
2782 return;
2784 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2785 es->password_char = c;
2786 if (c) {
2787 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2788 es->style |= ES_PASSWORD;
2789 } else {
2790 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2791 es->style &= ~ES_PASSWORD;
2793 EDIT_InvalidateUniscribeData(es);
2794 EDIT_UpdateText(es, NULL, TRUE);
2798 /*********************************************************************
2800 * EM_SETTABSTOPS
2803 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2805 if (!(es->style & ES_MULTILINE))
2806 return FALSE;
2807 heap_free(es->tabs);
2808 es->tabs_count = count;
2809 if (!count)
2810 es->tabs = NULL;
2811 else {
2812 es->tabs = heap_alloc(count * sizeof(INT));
2813 memcpy(es->tabs, tabs, count * sizeof(INT));
2815 EDIT_InvalidateUniscribeData(es);
2816 return TRUE;
2820 /*********************************************************************
2822 * EM_SETWORDBREAKPROC
2825 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, EDITWORDBREAKPROCW wbp)
2827 if (es->word_break_proc == wbp)
2828 return;
2830 es->word_break_proc = wbp;
2832 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2833 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2834 EDIT_UpdateText(es, NULL, TRUE);
2839 /*********************************************************************
2841 * EM_UNDO / WM_UNDO
2844 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2846 INT ulength;
2847 LPWSTR utext;
2849 /* As per MSDN spec, for a single-line edit control,
2850 the return value is always TRUE */
2851 if( es->style & ES_READONLY )
2852 return !(es->style & ES_MULTILINE);
2854 ulength = strlenW(es->undo_text);
2856 utext = heap_alloc((ulength + 1) * sizeof(WCHAR));
2858 strcpyW(utext, es->undo_text);
2860 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2861 es->undo_insert_count, debugstr_w(utext));
2863 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2864 EDIT_EM_EmptyUndoBuffer(es);
2865 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
2866 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2867 /* send the notification after the selection start and end are set */
2868 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2869 EDIT_EM_ScrollCaret(es);
2870 heap_free(utext);
2872 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2873 es->undo_insert_count, debugstr_w(es->undo_text));
2874 return TRUE;
2878 /* Helper function for WM_CHAR
2880 * According to an MSDN blog article titled "Just because you're a control
2881 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2882 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2883 * a dialog box or not.
2885 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2887 return (es->flags & EF_DIALOGMODE);
2891 /*********************************************************************
2893 * WM_PASTE
2896 static void EDIT_WM_Paste(EDITSTATE *es)
2898 HGLOBAL hsrc;
2899 LPWSTR src, ptr;
2900 int len;
2902 /* Protect read-only edit control from modification */
2903 if(es->style & ES_READONLY)
2904 return;
2906 OpenClipboard(es->hwndSelf);
2907 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2908 src = GlobalLock(hsrc);
2909 len = strlenW(src);
2910 /* Protect single-line edit against pasting new line character */
2911 if (!(es->style & ES_MULTILINE) && ((ptr = strchrW(src, '\n')))) {
2912 len = ptr - src;
2913 if (len && src[len - 1] == '\r')
2914 --len;
2916 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
2917 GlobalUnlock(hsrc);
2919 else if (es->style & ES_PASSWORD) {
2920 /* clear selected text in password edit box even with empty clipboard */
2921 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2923 CloseClipboard();
2927 /*********************************************************************
2929 * WM_COPY
2932 static void EDIT_WM_Copy(EDITSTATE *es)
2934 INT s = min(es->selection_start, es->selection_end);
2935 INT e = max(es->selection_start, es->selection_end);
2936 HGLOBAL hdst;
2937 LPWSTR dst;
2938 DWORD len;
2940 if (e == s) return;
2942 len = e - s;
2943 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2944 dst = GlobalLock(hdst);
2945 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2946 dst[len] = 0; /* ensure 0 termination */
2947 TRACE("%s\n", debugstr_w(dst));
2948 GlobalUnlock(hdst);
2949 OpenClipboard(es->hwndSelf);
2950 EmptyClipboard();
2951 SetClipboardData(CF_UNICODETEXT, hdst);
2952 CloseClipboard();
2956 /*********************************************************************
2958 * WM_CLEAR
2961 static inline void EDIT_WM_Clear(EDITSTATE *es)
2963 /* Protect read-only edit control from modification */
2964 if(es->style & ES_READONLY)
2965 return;
2967 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2971 /*********************************************************************
2973 * WM_CUT
2976 static inline void EDIT_WM_Cut(EDITSTATE *es)
2978 EDIT_WM_Copy(es);
2979 EDIT_WM_Clear(es);
2983 /*********************************************************************
2985 * WM_CHAR
2988 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2990 BOOL control;
2992 control = GetKeyState(VK_CONTROL) & 0x8000;
2994 switch (c) {
2995 case '\r':
2996 /* If it's not a multiline edit box, it would be ignored below.
2997 * For multiline edit without ES_WANTRETURN, we have to make a
2998 * special case.
3000 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3001 if (EDIT_IsInsideDialog(es))
3002 break;
3003 case '\n':
3004 if (es->style & ES_MULTILINE) {
3005 if (es->style & ES_READONLY) {
3006 EDIT_MoveHome(es, FALSE, FALSE);
3007 EDIT_MoveDown_ML(es, FALSE);
3008 } else {
3009 static const WCHAR cr_lfW[] = {'\r','\n'};
3010 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, 2, TRUE, TRUE);
3013 break;
3014 case '\t':
3015 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3017 static const WCHAR tabW[] = {'\t'};
3018 if (EDIT_IsInsideDialog(es))
3019 break;
3020 EDIT_EM_ReplaceSel(es, TRUE, tabW, 1, TRUE, TRUE);
3022 break;
3023 case VK_BACK:
3024 if (!(es->style & ES_READONLY) && !control) {
3025 if (es->selection_start != es->selection_end)
3026 EDIT_WM_Clear(es);
3027 else {
3028 /* delete character left of caret */
3029 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3030 EDIT_MoveBackward(es, TRUE);
3031 EDIT_WM_Clear(es);
3034 break;
3035 case 0x03: /* ^C */
3036 if (!(es->style & ES_PASSWORD))
3037 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3038 break;
3039 case 0x16: /* ^V */
3040 if (!(es->style & ES_READONLY))
3041 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3042 break;
3043 case 0x18: /* ^X */
3044 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3045 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3046 break;
3047 case 0x1A: /* ^Z */
3048 if (!(es->style & ES_READONLY))
3049 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3050 break;
3052 default:
3053 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3054 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3055 break;
3057 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3058 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3059 break;
3061 return 1;
3065 /*********************************************************************
3067 * EDIT_ContextMenuCommand
3070 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3072 switch (id) {
3073 case EM_UNDO:
3074 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3075 break;
3076 case WM_CUT:
3077 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3078 break;
3079 case WM_COPY:
3080 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3081 break;
3082 case WM_PASTE:
3083 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3084 break;
3085 case WM_CLEAR:
3086 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3087 break;
3088 case EM_SETSEL:
3089 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3090 break;
3091 default:
3092 ERR("unknown menu item, please report\n");
3093 break;
3098 /*********************************************************************
3100 * WM_CONTEXTMENU
3102 * Note: the resource files resource/sysres_??.rc cannot define a
3103 * single popup menu. Hence we use a (dummy) menubar
3104 * containing the single popup menu as its first item.
3106 * FIXME: the message identifiers have been chosen arbitrarily,
3107 * hence we use MF_BYPOSITION.
3108 * We might as well use the "real" values (anybody knows ?)
3109 * The menu definition is in resources/sysres_??.rc.
3110 * Once these are OK, we better use MF_BYCOMMAND here
3111 * (as we do in EDIT_WM_Command()).
3114 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3116 HMENU menu = LoadMenuA(GetModuleHandleA("user32.dll"), "EDITMENU");
3117 HMENU popup = GetSubMenu(menu, 0);
3118 UINT start = es->selection_start;
3119 UINT end = es->selection_end;
3120 UINT cmd;
3121 POINT pt;
3123 ORDER_UINT(start, end);
3125 /* undo */
3126 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3127 /* cut */
3128 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3129 /* copy */
3130 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3131 /* paste */
3132 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3133 /* delete */
3134 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3135 /* select all */
3136 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3138 pt.x = x;
3139 pt.y = y;
3141 if (pt.x == -1 && pt.y == -1) /* passed via VK_APPS press/release */
3143 RECT rc;
3145 /* Windows places the menu at the edit's center in this case */
3146 GetClientRect(es->hwndSelf, &rc);
3147 pt.x = rc.left + (rc.right - rc.left) / 2;
3148 pt.y = rc.top + (rc.bottom - rc.top) / 2;
3149 ClientToScreen(es->hwndSelf, &pt);
3152 if (!(es->flags & EF_FOCUSED))
3153 SetFocus(es->hwndSelf);
3155 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3156 pt.x, pt.y, 0, es->hwndSelf, NULL);
3158 if (cmd)
3159 EDIT_ContextMenuCommand(es, cmd);
3161 DestroyMenu(menu);
3165 /*********************************************************************
3167 * WM_GETTEXT
3170 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst)
3172 if (!count)
3173 return 0;
3175 lstrcpynW(dst, es->text, count);
3176 return strlenW(dst);
3179 /*********************************************************************
3181 * EDIT_CheckCombo
3184 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3186 HWND hLBox = es->hwndListBox;
3187 HWND hCombo;
3188 BOOL bDropped;
3189 int nEUI;
3191 if (!hLBox)
3192 return FALSE;
3194 hCombo = GetParent(es->hwndSelf);
3195 bDropped = TRUE;
3196 nEUI = 0;
3198 TRACE("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3200 if (key == VK_UP || key == VK_DOWN)
3202 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3203 nEUI = 1;
3205 if (msg == WM_KEYDOWN || nEUI)
3206 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3209 switch (msg)
3211 case WM_KEYDOWN:
3212 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3214 /* make sure ComboLBox pops up */
3215 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3216 key = VK_F4;
3217 nEUI = 2;
3220 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3221 break;
3223 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3224 if (nEUI)
3225 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3226 else
3227 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3228 break;
3231 if (nEUI == 2)
3232 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3234 return TRUE;
3238 /*********************************************************************
3240 * WM_KEYDOWN
3242 * Handling of special keys that don't produce a WM_CHAR
3243 * (i.e. non-printable keys) & Backspace & Delete
3246 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3248 BOOL shift;
3249 BOOL control;
3251 if (GetKeyState(VK_MENU) & 0x8000)
3252 return 0;
3254 shift = GetKeyState(VK_SHIFT) & 0x8000;
3255 control = GetKeyState(VK_CONTROL) & 0x8000;
3257 switch (key) {
3258 case VK_F4:
3259 case VK_UP:
3260 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3261 break;
3263 /* fall through */
3264 case VK_LEFT:
3265 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3266 EDIT_MoveUp_ML(es, shift);
3267 else
3268 if (control)
3269 EDIT_MoveWordBackward(es, shift);
3270 else
3271 EDIT_MoveBackward(es, shift);
3272 break;
3273 case VK_DOWN:
3274 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3275 break;
3276 /* fall through */
3277 case VK_RIGHT:
3278 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3279 EDIT_MoveDown_ML(es, shift);
3280 else if (control)
3281 EDIT_MoveWordForward(es, shift);
3282 else
3283 EDIT_MoveForward(es, shift);
3284 break;
3285 case VK_HOME:
3286 EDIT_MoveHome(es, shift, control);
3287 break;
3288 case VK_END:
3289 EDIT_MoveEnd(es, shift, control);
3290 break;
3291 case VK_PRIOR:
3292 if (es->style & ES_MULTILINE)
3293 EDIT_MovePageUp_ML(es, shift);
3294 else
3295 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3296 break;
3297 case VK_NEXT:
3298 if (es->style & ES_MULTILINE)
3299 EDIT_MovePageDown_ML(es, shift);
3300 else
3301 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3302 break;
3303 case VK_DELETE:
3304 if (!(es->style & ES_READONLY) && !(shift && control)) {
3305 if (es->selection_start != es->selection_end) {
3306 if (shift)
3307 EDIT_WM_Cut(es);
3308 else
3309 EDIT_WM_Clear(es);
3310 } else {
3311 if (shift) {
3312 /* delete character left of caret */
3313 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3314 EDIT_MoveBackward(es, TRUE);
3315 EDIT_WM_Clear(es);
3316 } else if (control) {
3317 /* delete to end of line */
3318 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3319 EDIT_MoveEnd(es, TRUE, FALSE);
3320 EDIT_WM_Clear(es);
3321 } else {
3322 /* delete character right of caret */
3323 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3324 EDIT_MoveForward(es, TRUE);
3325 EDIT_WM_Clear(es);
3329 break;
3330 case VK_INSERT:
3331 if (shift) {
3332 if (!(es->style & ES_READONLY))
3333 EDIT_WM_Paste(es);
3334 } else if (control)
3335 EDIT_WM_Copy(es);
3336 break;
3337 case VK_RETURN:
3338 /* If the edit doesn't want the return send a message to the default object */
3339 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3341 DWORD dw;
3343 if (!EDIT_IsInsideDialog(es)) break;
3344 if (control) break;
3345 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3346 if (HIWORD(dw) == DC_HASDEFID)
3348 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3349 if (hwDefCtrl)
3351 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3352 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3356 break;
3357 case VK_ESCAPE:
3358 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3359 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3360 break;
3361 case VK_TAB:
3362 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3363 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3364 break;
3365 case 'A':
3366 if (control)
3368 if (EDIT_EM_SetSel(es, 0, get_text_length(es), FALSE))
3370 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3371 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3374 break;
3376 return TRUE;
3380 /*********************************************************************
3382 * WM_KILLFOCUS
3385 static LRESULT EDIT_WM_KillFocus(HTHEME theme, EDITSTATE *es)
3387 UINT flags = RDW_INVALIDATE | RDW_UPDATENOW;
3389 es->flags &= ~EF_FOCUSED;
3390 DestroyCaret();
3391 if (!(es->style & ES_NOHIDESEL))
3392 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3393 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3394 /* Throw away left over scroll when we lose focus */
3395 es->wheelDeltaRemainder = 0;
3397 if (theme)
3398 flags |= RDW_FRAME;
3400 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3401 return 0;
3405 /*********************************************************************
3407 * WM_LBUTTONDBLCLK
3409 * The caret position has been set on the WM_LBUTTONDOWN message
3412 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3414 INT s;
3415 INT e = es->selection_end;
3416 INT l;
3417 INT li;
3418 INT ll;
3420 es->bCaptureState = TRUE;
3421 SetCapture(es->hwndSelf);
3423 l = EDIT_EM_LineFromChar(es, e);
3424 li = EDIT_EM_LineIndex(es, l);
3425 ll = EDIT_EM_LineLength(es, e);
3426 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3427 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3428 EDIT_EM_SetSel(es, s, e, FALSE);
3429 EDIT_EM_ScrollCaret(es);
3430 es->region_posx = es->region_posy = 0;
3431 SetTimer(es->hwndSelf, 0, 100, NULL);
3432 return 0;
3436 /*********************************************************************
3438 * WM_LBUTTONDOWN
3441 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3443 INT e;
3444 BOOL after_wrap;
3446 es->bCaptureState = TRUE;
3447 SetCapture(es->hwndSelf);
3448 EDIT_ConfinePoint(es, &x, &y);
3449 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3450 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3451 EDIT_EM_ScrollCaret(es);
3452 es->region_posx = es->region_posy = 0;
3453 SetTimer(es->hwndSelf, 0, 100, NULL);
3455 if (!(es->flags & EF_FOCUSED))
3456 SetFocus(es->hwndSelf);
3458 return 0;
3462 /*********************************************************************
3464 * WM_LBUTTONUP
3467 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3469 if (es->bCaptureState) {
3470 KillTimer(es->hwndSelf, 0);
3471 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3473 es->bCaptureState = FALSE;
3474 return 0;
3478 /*********************************************************************
3480 * WM_MBUTTONDOWN
3483 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3485 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3486 return 0;
3490 /*********************************************************************
3492 * WM_MOUSEMOVE
3495 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3497 INT e;
3498 BOOL after_wrap;
3499 INT prex, prey;
3501 /* If the mouse has been captured by process other than the edit control itself,
3502 * the windows edit controls will not select the strings with mouse move.
3504 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3505 return 0;
3508 * FIXME: gotta do some scrolling if outside client
3509 * area. Maybe reset the timer ?
3511 prex = x; prey = y;
3512 EDIT_ConfinePoint(es, &x, &y);
3513 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3514 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3515 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3516 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3517 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3518 return 0;
3522 /*********************************************************************
3524 * WM_PAINT
3527 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3529 PAINTSTRUCT ps;
3530 INT i;
3531 HDC dc;
3532 HFONT old_font = 0;
3533 RECT rc;
3534 RECT rcClient;
3535 RECT rcLine;
3536 RECT rcRgn;
3537 HBRUSH brush;
3538 HBRUSH old_brush;
3539 INT bw, bh;
3540 BOOL rev = es->bEnableState &&
3541 ((es->flags & EF_FOCUSED) ||
3542 (es->style & ES_NOHIDESEL));
3543 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3545 /* The dc we use for calculating may not be the one we paint into.
3546 This is the safest action. */
3547 EDIT_InvalidateUniscribeData(es);
3548 GetClientRect(es->hwndSelf, &rcClient);
3550 /* get the background brush */
3551 brush = EDIT_NotifyCtlColor(es, dc);
3553 /* paint the border and the background */
3554 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3556 if(es->style & WS_BORDER) {
3557 bw = GetSystemMetrics(SM_CXBORDER);
3558 bh = GetSystemMetrics(SM_CYBORDER);
3559 rc = rcClient;
3560 if(es->style & ES_MULTILINE) {
3561 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3562 if(es->style & WS_VSCROLL) rc.right+=bw;
3565 /* Draw the frame. Same code as in nonclient.c */
3566 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3567 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3568 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3569 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3570 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3571 SelectObject(dc, old_brush);
3573 /* Keep the border clean */
3574 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3575 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3578 GetClipBox(dc, &rc);
3579 FillRect(dc, &rc, brush);
3581 IntersectClipRect(dc, es->format_rect.left,
3582 es->format_rect.top,
3583 es->format_rect.right,
3584 es->format_rect.bottom);
3585 if (es->style & ES_MULTILINE) {
3586 rc = rcClient;
3587 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3589 if (es->font)
3590 old_font = SelectObject(dc, es->font);
3592 if (!es->bEnableState)
3593 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3594 GetClipBox(dc, &rcRgn);
3595 if (es->style & ES_MULTILINE) {
3596 INT vlc = get_vertical_line_count(es);
3597 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3598 EDIT_UpdateUniscribeData(es, dc, i);
3599 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3600 if (IntersectRect(&rc, &rcRgn, &rcLine))
3601 EDIT_PaintLine(es, dc, i, rev);
3603 } else {
3604 EDIT_UpdateUniscribeData(es, dc, 0);
3605 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3606 if (IntersectRect(&rc, &rcRgn, &rcLine))
3607 EDIT_PaintLine(es, dc, 0, rev);
3609 if (es->font)
3610 SelectObject(dc, old_font);
3612 if (!hdc)
3613 EndPaint(es->hwndSelf, &ps);
3616 static void EDIT_WM_NCPaint(HWND hwnd, HRGN region)
3618 DWORD exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3619 HTHEME theme = GetWindowTheme(hwnd);
3620 HRGN cliprgn = region;
3622 if (theme && exStyle & WS_EX_CLIENTEDGE)
3624 HDC dc;
3625 RECT r;
3626 int cxEdge = GetSystemMetrics(SM_CXEDGE),
3627 cyEdge = GetSystemMetrics(SM_CYEDGE);
3628 const int part = EP_EDITTEXT;
3629 int state = ETS_NORMAL;
3630 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3632 if (!IsWindowEnabled(hwnd))
3633 state = ETS_DISABLED;
3634 else if (dwStyle & ES_READONLY)
3635 state = ETS_READONLY;
3636 else if (GetFocus() == hwnd)
3637 state = ETS_FOCUSED;
3639 GetWindowRect(hwnd, &r);
3641 /* New clipping region passed to default proc to exclude border */
3642 cliprgn = CreateRectRgn(r.left + cxEdge, r.top + cyEdge,
3643 r.right - cxEdge, r.bottom - cyEdge);
3644 if (region != (HRGN)1)
3645 CombineRgn(cliprgn, cliprgn, region, RGN_AND);
3646 OffsetRect(&r, -r.left, -r.top);
3648 dc = GetDCEx(hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
3649 OffsetRect(&r, -r.left, -r.top);
3651 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
3652 DrawThemeParentBackground(hwnd, dc, &r);
3653 DrawThemeBackground(theme, dc, part, state, &r, 0);
3654 ReleaseDC(hwnd, dc);
3657 /* Call default proc to get the scrollbars etc. also painted */
3658 DefWindowProcW (hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
3661 /*********************************************************************
3663 * WM_SETFOCUS
3666 static void EDIT_WM_SetFocus(HTHEME theme, EDITSTATE *es)
3668 UINT flags = RDW_INVALIDATE | RDW_UPDATENOW;
3670 es->flags |= EF_FOCUSED;
3672 if (!(es->style & ES_NOHIDESEL))
3673 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3675 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3676 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3677 ShowCaret(es->hwndSelf);
3678 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3680 if (theme)
3681 flags |= RDW_FRAME | RDW_ERASE;
3683 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3687 /*********************************************************************
3689 * WM_SETFONT
3691 * With Win95 look the margins are set to default font value unless
3692 * the system font (font == 0) is being set, in which case they are left
3693 * unchanged.
3696 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3698 TEXTMETRICW tm;
3699 HDC dc;
3700 HFONT old_font = 0;
3701 RECT clientRect;
3703 es->font = font;
3704 EDIT_InvalidateUniscribeData(es);
3705 dc = GetDC(es->hwndSelf);
3706 if (font)
3707 old_font = SelectObject(dc, font);
3708 GetTextMetricsW(dc, &tm);
3709 es->line_height = tm.tmHeight;
3710 es->char_width = tm.tmAveCharWidth;
3711 if (font)
3712 SelectObject(dc, old_font);
3713 ReleaseDC(es->hwndSelf, dc);
3715 /* Reset the format rect and the margins */
3716 GetClientRect(es->hwndSelf, &clientRect);
3717 EDIT_SetRectNP(es, &clientRect);
3718 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3719 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3721 if (es->style & ES_MULTILINE)
3722 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3723 else
3724 EDIT_CalcLineWidth_SL(es);
3726 if (redraw)
3727 EDIT_UpdateText(es, NULL, TRUE);
3728 if (es->flags & EF_FOCUSED) {
3729 DestroyCaret();
3730 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3731 EDIT_SetCaretPos(es, es->selection_end,
3732 es->flags & EF_AFTER_WRAP);
3733 ShowCaret(es->hwndSelf);
3738 /*********************************************************************
3740 * WM_SETTEXT
3742 * NOTES
3743 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3744 * The modified flag is reset. No notifications are sent.
3746 * For single-line controls, reception of WM_SETTEXT triggers:
3747 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3750 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text)
3752 if (es->flags & EF_UPDATE)
3753 /* fixed this bug once; complain if we see it about to happen again. */
3754 ERR("SetSel may generate UPDATE message whose handler may reset "
3755 "selection.\n");
3757 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3758 if (text)
3760 TRACE("%s\n", debugstr_w(text));
3761 EDIT_EM_ReplaceSel(es, FALSE, text, strlenW(text), FALSE, FALSE);
3763 else
3765 TRACE("<NULL>\n");
3766 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3768 es->x_offset = 0;
3769 es->flags &= ~EF_MODIFIED;
3770 EDIT_EM_SetSel(es, 0, 0, FALSE);
3772 /* Send the notification after the selection start and end have been set
3773 * edit control doesn't send notification on WM_SETTEXT
3774 * if it is multiline, or it is part of combobox
3776 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3778 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3779 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3781 EDIT_EM_ScrollCaret(es);
3782 EDIT_UpdateScrollInfo(es);
3783 EDIT_InvalidateUniscribeData(es);
3787 /*********************************************************************
3789 * WM_SIZE
3792 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3794 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3795 RECT rc;
3796 GetClientRect(es->hwndSelf, &rc);
3797 EDIT_SetRectNP(es, &rc);
3798 EDIT_UpdateText(es, NULL, TRUE);
3803 /*********************************************************************
3805 * WM_STYLECHANGED
3807 * This message is sent by SetWindowLong on having changed either the Style
3808 * or the extended style.
3810 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3812 * See also EDIT_WM_NCCreate
3814 * It appears that the Windows version of the edit control allows the style
3815 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3816 * style variable which will generally be different. In this function we
3817 * update the internal style based on what changed in the externally visible
3818 * style.
3820 * Much of this content as based upon the MSDN, especially:
3821 * Platform SDK Documentation -> User Interface Services ->
3822 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3823 * Edit Control Styles
3825 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3827 if (GWL_STYLE == which) {
3828 DWORD style_change_mask;
3829 DWORD new_style;
3830 /* Only a subset of changes can be applied after the control
3831 * has been created.
3833 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3834 ES_NUMBER;
3835 if (es->style & ES_MULTILINE)
3836 style_change_mask |= ES_WANTRETURN;
3838 new_style = style->styleNew & style_change_mask;
3840 /* Number overrides lowercase overrides uppercase (at least it
3841 * does in Win95). However I'll bet that ES_NUMBER would be
3842 * invalid under Win 3.1.
3844 if (new_style & ES_NUMBER) {
3845 ; /* do not override the ES_NUMBER */
3846 } else if (new_style & ES_LOWERCASE) {
3847 new_style &= ~ES_UPPERCASE;
3850 es->style = (es->style & ~style_change_mask) | new_style;
3851 } else if (GWL_EXSTYLE == which) {
3852 ; /* FIXME - what is needed here */
3853 } else {
3854 WARN ("Invalid style change %ld\n",which);
3857 return 0;
3860 /*********************************************************************
3862 * WM_SYSKEYDOWN
3865 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3867 if ((key == VK_BACK) && (key_data & 0x2000)) {
3868 if (EDIT_EM_CanUndo(es))
3869 EDIT_EM_Undo(es);
3870 return 0;
3871 } else if (key == VK_UP || key == VK_DOWN) {
3872 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3873 return 0;
3875 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3879 /*********************************************************************
3881 * WM_TIMER
3884 static void EDIT_WM_Timer(EDITSTATE *es)
3886 if (es->region_posx < 0) {
3887 EDIT_MoveBackward(es, TRUE);
3888 } else if (es->region_posx > 0) {
3889 EDIT_MoveForward(es, TRUE);
3892 * FIXME: gotta do some vertical scrolling here, like
3893 * EDIT_EM_LineScroll(hwnd, 0, 1);
3897 /*********************************************************************
3899 * WM_HSCROLL
3902 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3904 INT dx;
3905 INT fw;
3907 if (!(es->style & ES_MULTILINE))
3908 return 0;
3910 if (!(es->style & ES_AUTOHSCROLL))
3911 return 0;
3913 dx = 0;
3914 fw = es->format_rect.right - es->format_rect.left;
3915 switch (action) {
3916 case SB_LINELEFT:
3917 TRACE("SB_LINELEFT\n");
3918 if (es->x_offset)
3919 dx = -es->char_width;
3920 break;
3921 case SB_LINERIGHT:
3922 TRACE("SB_LINERIGHT\n");
3923 if (es->x_offset < es->text_width)
3924 dx = es->char_width;
3925 break;
3926 case SB_PAGELEFT:
3927 TRACE("SB_PAGELEFT\n");
3928 if (es->x_offset)
3929 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3930 break;
3931 case SB_PAGERIGHT:
3932 TRACE("SB_PAGERIGHT\n");
3933 if (es->x_offset < es->text_width)
3934 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3935 break;
3936 case SB_LEFT:
3937 TRACE("SB_LEFT\n");
3938 if (es->x_offset)
3939 dx = -es->x_offset;
3940 break;
3941 case SB_RIGHT:
3942 TRACE("SB_RIGHT\n");
3943 if (es->x_offset < es->text_width)
3944 dx = es->text_width - es->x_offset;
3945 break;
3946 case SB_THUMBTRACK:
3947 TRACE("SB_THUMBTRACK %d\n", pos);
3948 es->flags |= EF_HSCROLL_TRACK;
3949 if(es->style & WS_HSCROLL)
3950 dx = pos - es->x_offset;
3951 else
3953 INT fw, new_x;
3954 /* Sanity check */
3955 if(pos < 0 || pos > 100) return 0;
3956 /* Assume default scroll range 0-100 */
3957 fw = es->format_rect.right - es->format_rect.left;
3958 new_x = pos * (es->text_width - fw) / 100;
3959 dx = es->text_width ? (new_x - es->x_offset) : 0;
3961 break;
3962 case SB_THUMBPOSITION:
3963 TRACE("SB_THUMBPOSITION %d\n", pos);
3964 es->flags &= ~EF_HSCROLL_TRACK;
3965 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3966 dx = pos - es->x_offset;
3967 else
3969 INT fw, new_x;
3970 /* Sanity check */
3971 if(pos < 0 || pos > 100) return 0;
3972 /* Assume default scroll range 0-100 */
3973 fw = es->format_rect.right - es->format_rect.left;
3974 new_x = pos * (es->text_width - fw) / 100;
3975 dx = es->text_width ? (new_x - es->x_offset) : 0;
3977 if (!dx) {
3978 /* force scroll info update */
3979 EDIT_UpdateScrollInfo(es);
3980 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3982 break;
3983 case SB_ENDSCROLL:
3984 TRACE("SB_ENDSCROLL\n");
3985 break;
3987 * FIXME : the next two are undocumented !
3988 * Are we doing the right thing ?
3989 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3990 * although it's also a regular control message.
3992 case EM_GETTHUMB: /* this one is used by NT notepad */
3994 LRESULT ret;
3995 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3996 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3997 else
3999 /* Assume default scroll range 0-100 */
4000 INT fw = es->format_rect.right - es->format_rect.left;
4001 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4003 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4004 return ret;
4006 case EM_LINESCROLL:
4007 TRACE("EM_LINESCROLL16\n");
4008 dx = pos;
4009 break;
4011 default:
4012 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4013 action, action);
4014 return 0;
4016 if (dx)
4018 INT fw = es->format_rect.right - es->format_rect.left;
4019 /* check if we are going to move too far */
4020 if(es->x_offset + dx + fw > es->text_width)
4021 dx = es->text_width - fw - es->x_offset;
4022 if(dx)
4023 EDIT_EM_LineScroll_internal(es, dx, 0);
4025 return 0;
4029 /*********************************************************************
4031 * WM_VSCROLL
4034 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4036 INT dy;
4038 if (!(es->style & ES_MULTILINE))
4039 return 0;
4041 if (!(es->style & ES_AUTOVSCROLL))
4042 return 0;
4044 dy = 0;
4045 switch (action) {
4046 case SB_LINEUP:
4047 case SB_LINEDOWN:
4048 case SB_PAGEUP:
4049 case SB_PAGEDOWN:
4050 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4051 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4052 (action == SB_PAGEUP ? "SB_PAGEUP" :
4053 "SB_PAGEDOWN"))));
4054 EDIT_EM_Scroll(es, action);
4055 return 0;
4056 case SB_TOP:
4057 TRACE("SB_TOP\n");
4058 dy = -es->y_offset;
4059 break;
4060 case SB_BOTTOM:
4061 TRACE("SB_BOTTOM\n");
4062 dy = es->line_count - 1 - es->y_offset;
4063 break;
4064 case SB_THUMBTRACK:
4065 TRACE("SB_THUMBTRACK %d\n", pos);
4066 es->flags |= EF_VSCROLL_TRACK;
4067 if(es->style & WS_VSCROLL)
4068 dy = pos - es->y_offset;
4069 else
4071 /* Assume default scroll range 0-100 */
4072 INT vlc, new_y;
4073 /* Sanity check */
4074 if(pos < 0 || pos > 100) return 0;
4075 vlc = get_vertical_line_count(es);
4076 new_y = pos * (es->line_count - vlc) / 100;
4077 dy = es->line_count ? (new_y - es->y_offset) : 0;
4078 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4079 es->line_count, es->y_offset, pos, dy);
4081 break;
4082 case SB_THUMBPOSITION:
4083 TRACE("SB_THUMBPOSITION %d\n", pos);
4084 es->flags &= ~EF_VSCROLL_TRACK;
4085 if(es->style & WS_VSCROLL)
4086 dy = pos - es->y_offset;
4087 else
4089 /* Assume default scroll range 0-100 */
4090 INT vlc, new_y;
4091 /* Sanity check */
4092 if(pos < 0 || pos > 100) return 0;
4093 vlc = get_vertical_line_count(es);
4094 new_y = pos * (es->line_count - vlc) / 100;
4095 dy = es->line_count ? (new_y - es->y_offset) : 0;
4096 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4097 es->line_count, es->y_offset, pos, dy);
4099 if (!dy)
4101 /* force scroll info update */
4102 EDIT_UpdateScrollInfo(es);
4103 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4105 break;
4106 case SB_ENDSCROLL:
4107 TRACE("SB_ENDSCROLL\n");
4108 break;
4110 * FIXME : the next two are undocumented !
4111 * Are we doing the right thing ?
4112 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4113 * although it's also a regular control message.
4115 case EM_GETTHUMB: /* this one is used by NT notepad */
4117 LRESULT ret;
4118 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4119 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4120 else
4122 /* Assume default scroll range 0-100 */
4123 INT vlc = get_vertical_line_count(es);
4124 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4126 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4127 return ret;
4129 case EM_LINESCROLL:
4130 TRACE("EM_LINESCROLL %d\n", pos);
4131 dy = pos;
4132 break;
4134 default:
4135 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4136 action, action);
4137 return 0;
4139 if (dy)
4140 EDIT_EM_LineScroll(es, 0, dy);
4141 return 0;
4144 /*********************************************************************
4146 * EM_GETTHUMB
4148 * FIXME: is this right ? (or should it be only VSCROLL)
4149 * (and maybe only for edit controls that really have their
4150 * own scrollbars) (and maybe only for multiline controls ?)
4151 * All in all: very poorly documented
4154 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4156 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4157 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4161 /********************************************************************
4163 * The Following code is to handle inline editing from IMEs
4166 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4168 LONG buflen;
4169 LPWSTR lpCompStr;
4170 LPSTR lpCompStrAttr = NULL;
4171 DWORD dwBufLenAttr;
4173 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4175 if (buflen < 0)
4177 return;
4180 lpCompStr = heap_alloc(buflen);
4181 if (!lpCompStr)
4183 ERR("Unable to allocate IME CompositionString\n");
4184 return;
4187 if (buflen)
4188 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4190 if (CompFlag & GCS_COMPATTR)
4193 * We do not use the attributes yet. it would tell us what characters
4194 * are in transition and which are converted or decided upon
4196 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4197 if (dwBufLenAttr)
4199 dwBufLenAttr ++;
4200 lpCompStrAttr = heap_alloc(dwBufLenAttr + 1);
4201 if (!lpCompStrAttr)
4203 ERR("Unable to allocate IME Attribute String\n");
4204 heap_free(lpCompStr);
4205 return;
4207 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4208 dwBufLenAttr);
4209 lpCompStrAttr[dwBufLenAttr] = 0;
4213 /* check for change in composition start */
4214 if (es->selection_end < es->composition_start)
4215 es->composition_start = es->selection_end;
4217 /* replace existing selection string */
4218 es->selection_start = es->composition_start;
4220 if (es->composition_len > 0)
4221 es->selection_end = es->composition_start + es->composition_len;
4222 else
4223 es->selection_end = es->selection_start;
4225 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4226 es->composition_len = abs(es->composition_start - es->selection_end);
4228 es->selection_start = es->composition_start;
4229 es->selection_end = es->selection_start + es->composition_len;
4231 heap_free(lpCompStrAttr);
4232 heap_free(lpCompStr);
4235 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4237 LONG buflen;
4238 LPWSTR lpResultStr;
4240 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4241 if (buflen <= 0)
4243 return;
4246 lpResultStr = heap_alloc(buflen);
4247 if (!lpResultStr)
4249 ERR("Unable to alloc buffer for IME string\n");
4250 return;
4253 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4255 /* check for change in composition start */
4256 if (es->selection_end < es->composition_start)
4257 es->composition_start = es->selection_end;
4259 es->selection_start = es->composition_start;
4260 es->selection_end = es->composition_start + es->composition_len;
4261 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4262 es->composition_start = es->selection_end;
4263 es->composition_len = 0;
4265 heap_free(lpResultStr);
4268 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4270 HIMC hIMC;
4271 int cursor;
4273 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4275 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4276 es->composition_start = es->selection_end;
4279 hIMC = ImmGetContext(hwnd);
4280 if (!hIMC)
4281 return;
4283 if (CompFlag & GCS_RESULTSTR)
4285 EDIT_GetResultStr(hIMC, es);
4286 cursor = 0;
4288 else
4290 if (CompFlag & GCS_COMPSTR)
4291 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4292 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4294 ImmReleaseContext(hwnd, hIMC);
4295 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4299 /*********************************************************************
4301 * WM_NCCREATE
4303 * See also EDIT_WM_StyleChanged
4305 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
4307 EDITSTATE *es;
4308 UINT alloc_size;
4310 TRACE("Creating edit control, style = %08x\n", lpcs->style);
4312 if (!(es = heap_alloc_zero(sizeof(*es))))
4313 return FALSE;
4314 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4317 * Note: since the EDITSTATE has not been fully initialized yet,
4318 * we can't use any API calls that may send
4319 * WM_XXX messages before WM_NCCREATE is completed.
4322 es->style = lpcs->style;
4324 es->bEnableState = !(es->style & WS_DISABLED);
4326 es->hwndSelf = hwnd;
4327 /* Save parent, which will be notified by EN_* messages */
4328 es->hwndParent = lpcs->hwndParent;
4330 if (es->style & ES_COMBO)
4331 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4333 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4334 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4336 /* Number overrides lowercase overrides uppercase (at least it
4337 * does in Win95). However I'll bet that ES_NUMBER would be
4338 * invalid under Win 3.1.
4340 if (es->style & ES_NUMBER) {
4341 ; /* do not override the ES_NUMBER */
4342 } else if (es->style & ES_LOWERCASE) {
4343 es->style &= ~ES_UPPERCASE;
4345 if (es->style & ES_MULTILINE) {
4346 es->buffer_limit = BUFLIMIT_INITIAL;
4347 if (es->style & WS_VSCROLL)
4348 es->style |= ES_AUTOVSCROLL;
4349 if (es->style & WS_HSCROLL)
4350 es->style |= ES_AUTOHSCROLL;
4351 es->style &= ~ES_PASSWORD;
4352 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4353 /* Confirmed - RIGHT overrides CENTER */
4354 if (es->style & ES_RIGHT)
4355 es->style &= ~ES_CENTER;
4356 es->style &= ~WS_HSCROLL;
4357 es->style &= ~ES_AUTOHSCROLL;
4359 } else {
4360 es->buffer_limit = BUFLIMIT_INITIAL;
4361 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4362 es->style &= ~ES_CENTER;
4363 es->style &= ~WS_HSCROLL;
4364 es->style &= ~WS_VSCROLL;
4365 if (es->style & ES_PASSWORD)
4366 es->password_char = '*';
4369 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4370 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4371 goto cleanup;
4372 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4374 if (!(es->undo_text = heap_alloc_zero((es->buffer_size + 1) * sizeof(WCHAR))))
4375 goto cleanup;
4376 es->undo_buffer_size = es->buffer_size;
4378 if (es->style & ES_MULTILINE)
4379 if (!(es->first_line_def = heap_alloc_zero(sizeof(LINEDEF))))
4380 goto cleanup;
4381 es->line_count = 1;
4384 * In Win95 look and feel, the WS_BORDER style is replaced by the
4385 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4386 * control a nonclient area so we don't need to draw the border.
4387 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4388 * a nonclient area and we should handle painting the border ourselves.
4390 * When making modifications please ensure that the code still works
4391 * for edit controls created directly with style 0x50800000, exStyle 0
4392 * (which should have a single pixel border)
4394 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4395 es->style &= ~WS_BORDER;
4396 else if (es->style & WS_BORDER)
4397 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4399 return TRUE;
4401 cleanup:
4402 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4403 EDIT_InvalidateUniscribeData(es);
4404 heap_free(es->first_line_def);
4405 heap_free(es->undo_text);
4406 if (es->hloc32W) LocalFree(es->hloc32W);
4407 heap_free(es->logAttr);
4408 heap_free(es);
4409 return FALSE;
4413 /*********************************************************************
4415 * WM_CREATE
4418 static LRESULT EDIT_WM_Create(EDITSTATE *es, const WCHAR *name)
4420 RECT clientRect;
4422 TRACE("%s\n", debugstr_w(name));
4425 * To initialize some final structure members, we call some helper
4426 * functions. However, since the EDITSTATE is not consistent (i.e.
4427 * not fully initialized), we should be very careful which
4428 * functions can be called, and in what order.
4430 EDIT_WM_SetFont(es, 0, FALSE);
4431 EDIT_EM_EmptyUndoBuffer(es);
4433 /* We need to calculate the format rect
4434 (applications may send EM_SETMARGINS before the control gets visible) */
4435 GetClientRect(es->hwndSelf, &clientRect);
4436 EDIT_SetRectNP(es, &clientRect);
4438 if (name && *name)
4440 EDIT_EM_ReplaceSel(es, FALSE, name, strlenW(name), FALSE, FALSE);
4441 /* if we insert text to the editline, the text scrolls out
4442 * of the window, as the caret is placed after the insert
4443 * pos normally; thus we reset es->selection... to 0 and
4444 * update caret
4446 es->selection_start = es->selection_end = 0;
4447 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4448 * Messages are only to be sent when the USER does something to
4449 * change the contents. So I am removing this EN_CHANGE
4451 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4453 EDIT_EM_ScrollCaret(es);
4456 /* force scroll info update */
4457 EDIT_UpdateScrollInfo(es);
4458 OpenThemeData(es->hwndSelf, WC_EDITW);
4460 /* The rule seems to return 1 here for success */
4461 /* Power Builder masked edit controls will crash */
4462 /* if not. */
4463 /* FIXME: is that in all cases so ? */
4464 return 1;
4468 /*********************************************************************
4470 * WM_NCDESTROY
4473 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4475 LINEDEF *pc, *pp;
4476 HTHEME theme;
4478 theme = GetWindowTheme(es->hwndSelf);
4479 CloseThemeData(theme);
4481 /* The app can own the text buffer handle */
4482 if (es->hloc32W && (es->hloc32W != es->hlocapp))
4483 LocalFree(es->hloc32W);
4485 EDIT_InvalidateUniscribeData(es);
4487 pc = es->first_line_def;
4488 while (pc)
4490 pp = pc->next;
4491 heap_free(pc);
4492 pc = pp;
4495 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4496 heap_free(es->undo_text);
4497 heap_free(es);
4499 return 0;
4502 static LRESULT CALLBACK EDIT_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
4504 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW(hwnd, 0);
4505 HTHEME theme = GetWindowTheme(hwnd);
4506 LRESULT result = 0;
4507 RECT *rect;
4509 TRACE("hwnd=%p msg=%#x wparam=%lx lparam=%lx\n", hwnd, msg, wParam, lParam);
4511 if (!es && msg != WM_NCCREATE)
4512 return DefWindowProcW(hwnd, msg, wParam, lParam);
4514 if (es && (msg != WM_NCDESTROY))
4515 EDIT_LockBuffer(es);
4517 switch (msg)
4519 case EM_GETSEL:
4520 result = EDIT_EM_GetSel(es, (UINT *)wParam, (UINT *)lParam);
4521 break;
4523 case EM_SETSEL:
4524 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4525 EDIT_EM_ScrollCaret(es);
4526 result = 1;
4527 break;
4529 case EM_GETRECT:
4530 rect = (RECT *)lParam;
4531 if (rect)
4532 *rect = es->format_rect;
4533 break;
4535 case EM_SETRECT:
4536 if ((es->style & ES_MULTILINE) && lParam)
4538 EDIT_SetRectNP(es, (RECT *)lParam);
4539 EDIT_UpdateText(es, NULL, TRUE);
4541 break;
4543 case EM_SETRECTNP:
4544 if ((es->style & ES_MULTILINE) && lParam)
4545 EDIT_SetRectNP(es, (LPRECT)lParam);
4546 break;
4548 case EM_SCROLL:
4549 result = EDIT_EM_Scroll(es, (INT)wParam);
4550 break;
4552 case EM_LINESCROLL:
4553 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4554 break;
4556 case EM_SCROLLCARET:
4557 EDIT_EM_ScrollCaret(es);
4558 result = 1;
4559 break;
4561 case EM_GETMODIFY:
4562 result = ((es->flags & EF_MODIFIED) != 0);
4563 break;
4565 case EM_SETMODIFY:
4566 if (wParam)
4567 es->flags |= EF_MODIFIED;
4568 else
4569 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4570 break;
4572 case EM_GETLINECOUNT:
4573 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4574 break;
4576 case EM_LINEINDEX:
4577 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4578 break;
4580 case EM_SETHANDLE:
4581 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4582 break;
4584 case EM_GETHANDLE:
4585 result = (LRESULT)EDIT_EM_GetHandle(es);
4586 break;
4588 case EM_GETTHUMB:
4589 result = EDIT_EM_GetThumb(es);
4590 break;
4592 /* these messages missing from specs */
4593 case 0x00bf:
4594 case 0x00c0:
4595 case 0x00c3:
4596 case 0x00ca:
4597 FIXME("undocumented message 0x%x, please report\n", msg);
4598 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4599 break;
4601 case EM_LINELENGTH:
4602 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4603 break;
4605 case EM_REPLACESEL:
4607 const WCHAR *textW = (const WCHAR *)lParam;
4609 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, strlenW(textW), TRUE, TRUE);
4610 result = 1;
4611 break;
4614 case EM_GETLINE:
4615 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
4616 break;
4618 case EM_SETLIMITTEXT:
4619 EDIT_EM_SetLimitText(es, wParam);
4620 break;
4622 case EM_CANUNDO:
4623 result = (LRESULT)EDIT_EM_CanUndo(es);
4624 break;
4626 case EM_UNDO:
4627 case WM_UNDO:
4628 result = (LRESULT)EDIT_EM_Undo(es);
4629 break;
4631 case EM_FMTLINES:
4632 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4633 break;
4635 case EM_LINEFROMCHAR:
4636 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4637 break;
4639 case EM_SETTABSTOPS:
4640 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4641 break;
4643 case EM_SETPASSWORDCHAR:
4644 EDIT_EM_SetPasswordChar(es, wParam);
4645 break;
4647 case EM_EMPTYUNDOBUFFER:
4648 EDIT_EM_EmptyUndoBuffer(es);
4649 break;
4651 case EM_GETFIRSTVISIBLELINE:
4652 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4653 break;
4655 case EM_SETREADONLY:
4657 DWORD old_style = es->style;
4659 if (wParam)
4661 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) | ES_READONLY);
4662 es->style |= ES_READONLY;
4664 else
4666 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & ~ES_READONLY);
4667 es->style &= ~ES_READONLY;
4670 if (old_style ^ es->style)
4671 InvalidateRect(es->hwndSelf, NULL, TRUE);
4673 result = 1;
4674 break;
4677 case EM_SETWORDBREAKPROC:
4678 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4679 result = 1;
4680 break;
4682 case EM_GETWORDBREAKPROC:
4683 result = (LRESULT)es->word_break_proc;
4684 break;
4686 case EM_GETPASSWORDCHAR:
4687 result = es->password_char;
4688 break;
4690 case EM_SETMARGINS:
4691 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4692 break;
4694 case EM_GETMARGINS:
4695 result = MAKELONG(es->left_margin, es->right_margin);
4696 break;
4698 case EM_GETLIMITTEXT:
4699 result = es->buffer_limit;
4700 break;
4702 case EM_POSFROMCHAR:
4703 if ((INT)wParam >= get_text_length(es)) result = -1;
4704 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4705 break;
4707 case EM_CHARFROMPOS:
4708 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4709 break;
4711 /* End of the EM_ messages which were in numerical order; what order
4712 * are these in? vaguely alphabetical?
4715 case WM_NCCREATE:
4716 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
4717 break;
4719 case WM_NCDESTROY:
4720 result = EDIT_WM_NCDestroy(es);
4721 es = NULL;
4722 break;
4724 case WM_GETDLGCODE:
4725 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4727 if (es->style & ES_MULTILINE)
4728 result |= DLGC_WANTALLKEYS;
4730 if (lParam)
4732 MSG *msg = (MSG *)lParam;
4733 es->flags |= EF_DIALOGMODE;
4735 if (msg->message == WM_KEYDOWN)
4737 int vk = (int)msg->wParam;
4739 if (es->hwndListBox)
4741 if (vk == VK_RETURN || vk == VK_ESCAPE)
4742 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4743 result |= DLGC_WANTMESSAGE;
4747 break;
4749 case WM_IME_CHAR:
4750 case WM_CHAR:
4752 WCHAR charW = wParam;
4754 if (es->hwndListBox)
4756 if (charW == VK_RETURN || charW == VK_ESCAPE)
4758 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4759 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4760 break;
4763 result = EDIT_WM_Char(es, charW);
4764 break;
4767 case WM_UNICHAR:
4768 if (wParam == UNICODE_NOCHAR) return TRUE;
4769 if (wParam <= 0x000fffff)
4771 if (wParam > 0xffff) /* convert to surrogates */
4773 wParam -= 0x10000;
4774 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4775 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4777 else
4778 EDIT_WM_Char(es, wParam);
4780 return 0;
4782 case WM_CLEAR:
4783 EDIT_WM_Clear(es);
4784 break;
4786 case WM_CONTEXTMENU:
4787 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4788 break;
4790 case WM_COPY:
4791 EDIT_WM_Copy(es);
4792 break;
4794 case WM_CREATE:
4795 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4796 break;
4798 case WM_CUT:
4799 EDIT_WM_Cut(es);
4800 break;
4802 case WM_ENABLE:
4803 es->bEnableState = (BOOL) wParam;
4804 EDIT_UpdateText(es, NULL, TRUE);
4805 if (theme)
4806 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
4807 break;
4809 case WM_ERASEBKGND:
4810 /* we do the proper erase in EDIT_WM_Paint */
4811 result = 1;
4812 break;
4814 case WM_GETFONT:
4815 result = (LRESULT)es->font;
4816 break;
4818 case WM_GETTEXT:
4819 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam);
4820 break;
4822 case WM_GETTEXTLENGTH:
4823 result = get_text_length(es);
4824 break;
4826 case WM_HSCROLL:
4827 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4828 break;
4830 case WM_KEYDOWN:
4831 result = EDIT_WM_KeyDown(es, (INT)wParam);
4832 break;
4834 case WM_KILLFOCUS:
4835 result = EDIT_WM_KillFocus(theme, es);
4836 break;
4838 case WM_LBUTTONDBLCLK:
4839 result = EDIT_WM_LButtonDblClk(es);
4840 break;
4842 case WM_LBUTTONDOWN:
4843 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4844 break;
4846 case WM_LBUTTONUP:
4847 result = EDIT_WM_LButtonUp(es);
4848 break;
4850 case WM_MBUTTONDOWN:
4851 result = EDIT_WM_MButtonDown(es);
4852 break;
4854 case WM_MOUSEMOVE:
4855 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4856 break;
4858 case WM_PRINTCLIENT:
4859 case WM_PAINT:
4860 EDIT_WM_Paint(es, (HDC)wParam);
4861 break;
4863 case WM_NCPAINT:
4864 EDIT_WM_NCPaint(hwnd, (HRGN)wParam);
4865 break;
4867 case WM_PASTE:
4868 EDIT_WM_Paste(es);
4869 break;
4871 case WM_SETFOCUS:
4872 EDIT_WM_SetFocus(theme, es);
4873 break;
4875 case WM_SETFONT:
4876 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4877 break;
4879 case WM_SETREDRAW:
4880 /* FIXME: actually set an internal flag and behave accordingly */
4881 break;
4883 case WM_SETTEXT:
4884 EDIT_WM_SetText(es, (const WCHAR *)lParam);
4885 result = TRUE;
4886 break;
4888 case WM_SIZE:
4889 EDIT_WM_Size(es, (UINT)wParam);
4890 break;
4892 case WM_STYLECHANGED:
4893 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4894 break;
4896 case WM_STYLECHANGING:
4897 result = 0; /* See EDIT_WM_StyleChanged */
4898 break;
4900 case WM_SYSKEYDOWN:
4901 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4902 break;
4904 case WM_TIMER:
4905 EDIT_WM_Timer(es);
4906 break;
4908 case WM_VSCROLL:
4909 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4910 break;
4912 case WM_MOUSEWHEEL:
4914 int wheelDelta;
4915 UINT pulScrollLines = 3;
4916 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4918 if (wParam & (MK_SHIFT | MK_CONTROL))
4920 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4921 break;
4924 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4925 /* if scrolling changes direction, ignore left overs */
4926 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
4927 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
4928 es->wheelDeltaRemainder += wheelDelta;
4929 else
4930 es->wheelDeltaRemainder = wheelDelta;
4932 if (es->wheelDeltaRemainder && pulScrollLines)
4934 int cLineScroll;
4935 pulScrollLines = (int) min((UINT) es->line_count, pulScrollLines);
4936 cLineScroll = pulScrollLines * (float)es->wheelDeltaRemainder / WHEEL_DELTA;
4937 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
4938 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
4940 break;
4943 /* IME messages to make the edit control IME aware */
4944 case WM_IME_SETCONTEXT:
4945 break;
4947 case WM_IME_STARTCOMPOSITION:
4948 es->composition_start = es->selection_end;
4949 es->composition_len = 0;
4950 break;
4952 case WM_IME_COMPOSITION:
4953 EDIT_ImeComposition(hwnd, lParam, es);
4954 break;
4956 case WM_IME_ENDCOMPOSITION:
4957 if (es->composition_len > 0)
4959 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4960 es->selection_end = es->selection_start;
4961 es->composition_len= 0;
4963 break;
4965 case WM_IME_COMPOSITIONFULL:
4966 break;
4968 case WM_IME_SELECT:
4969 break;
4971 case WM_IME_CONTROL:
4972 break;
4974 case WM_IME_REQUEST:
4975 switch (wParam)
4977 case IMR_QUERYCHARPOSITION:
4979 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
4980 LRESULT pos;
4982 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
4983 chpos->pt.x = LOWORD(pos);
4984 chpos->pt.y = HIWORD(pos);
4985 chpos->cLineHeight = es->line_height;
4986 chpos->rcDocument = es->format_rect;
4987 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
4988 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
4989 result = 1;
4990 break;
4993 break;
4995 case WM_THEMECHANGED:
4996 CloseThemeData (theme);
4997 OpenThemeData(hwnd, WC_EDITW);
4998 break;
5000 default:
5001 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5002 break;
5005 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5006 EDIT_UnlockBuffer(es, FALSE);
5008 TRACE("hwnd=%p msg=%x -- 0x%08lx\n", hwnd, msg, result);
5010 return result;
5013 void EDIT_Register(void)
5015 WNDCLASSW wndClass;
5017 memset(&wndClass, 0, sizeof(wndClass));
5018 wndClass.style = CS_PARENTDC | CS_GLOBALCLASS | CS_DBLCLKS;
5019 wndClass.lpfnWndProc = EDIT_WindowProc;
5020 wndClass.cbClsExtra = 0;
5021 #ifdef __i386__
5022 wndClass.cbWndExtra = sizeof(EDITSTATE *) + sizeof(WORD);
5023 #else
5024 wndClass.cbWndExtra = sizeof(EDITSTATE *);
5025 #endif
5026 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_IBEAM);
5027 wndClass.hbrBackground = NULL;
5028 wndClass.lpszClassName = WC_EDITW;
5029 RegisterClassW(&wndClass);