widl: Properly align name table entries.
[wine.git] / dlls / comctl32 / edit.c
blob4b950412617271e5647597c037b9ada37590df23
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
7 * Copyright Frank Richter, 2005
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * TODO:
25 * - EDITBALLOONTIP structure
26 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
27 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
28 * - EN_ALIGN_LTR_EC
29 * - EN_ALIGN_RTL_EC
30 * - ES_OEMCONVERT
34 #include <stdarg.h>
35 #include <string.h>
36 #include <stdlib.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnt.h"
41 #include "wingdi.h"
42 #include "imm.h"
43 #include "usp10.h"
44 #include "commctrl.h"
45 #include "uxtheme.h"
46 #include "vsstyle.h"
47 #include "comctl32.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(edit);
52 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
53 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
54 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
55 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
58 * extra flags for EDITSTATE.flags field
60 #define EF_MODIFIED 0x0001 /* text has been modified */
61 #define EF_FOCUSED 0x0002 /* we have input focus */
62 #define EF_UPDATE 0x0004 /* notify parent of changed state */
63 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
64 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
66 wrapped line, instead of in front of the next character */
67 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
68 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
70 #define ID_CB_LISTBOX 1000
72 typedef enum
74 END_0 = 0, /* line ends with terminating '\0' character */
75 END_WRAP, /* line is wrapped */
76 END_HARD, /* line ends with a hard return '\r\n' */
77 END_SOFT, /* line ends with a soft return '\r\r\n' */
78 END_RICH /* line ends with a single '\n' */
79 } LINE_END;
81 typedef struct tagLINEDEF {
82 INT length; /* bruto length of a line in bytes */
83 INT net_length; /* netto length of a line in visible characters */
84 LINE_END ending;
85 INT width; /* width of the line in pixels */
86 INT index; /* line index into the buffer */
87 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
88 struct tagLINEDEF *next;
89 } LINEDEF;
91 typedef struct
93 LPWSTR text; /* the actual contents of the control */
94 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
95 UINT buffer_size; /* the size of the buffer in characters */
96 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
97 HFONT font; /* NULL means standard system font */
98 INT x_offset; /* scroll offset for multi lines this is in pixels
99 for single lines it's in characters */
100 INT line_height; /* height of a screen line in pixels */
101 INT char_width; /* average character width in pixels */
102 DWORD style; /* sane version of wnd->dwStyle */
103 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
104 INT undo_insert_count; /* number of characters inserted in sequence */
105 UINT undo_position; /* character index of the insertion and deletion */
106 LPWSTR undo_text; /* deleted text */
107 UINT undo_buffer_size; /* size of the deleted text buffer */
108 INT selection_start; /* == selection_end if no selection */
109 INT selection_end; /* == current caret position */
110 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
111 INT left_margin; /* in pixels */
112 INT right_margin; /* in pixels */
113 RECT format_rect;
114 INT text_width; /* width of the widest line in pixels for multi line controls
115 and just line width for single line controls */
116 EDITWORDBREAKPROCW word_break_proc;
117 INT line_count; /* number of lines */
118 INT y_offset; /* scroll offset in number of lines */
119 BOOL bCaptureState; /* flag indicating whether mouse was captured */
120 BOOL bEnableState; /* flag keeping the enable state */
121 HWND hwndSelf; /* the our window handle */
122 HWND hwndParent; /* Handle of parent for sending EN_* messages.
123 Even if parent will change, EN_* messages
124 should be sent to the first parent. */
125 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
126 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
127 WCHAR *cue_banner_text;
128 BOOL cue_banner_draw_focused;
131 * only for multi line controls
133 INT lock_count; /* amount of re-entries in the EditWndProc */
134 INT tabs_count;
135 LPINT tabs;
136 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
137 HLOCAL hloc32W; /* our unicode local memory block */
138 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
140 * IME Data
142 UINT ime_status; /* IME status flag */
145 * Uniscribe Data
147 SCRIPT_LOGATTR *logAttr;
148 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
149 } EDITSTATE;
152 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
153 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
155 static inline BOOL notify_parent(const EDITSTATE *es, INT code)
157 HWND hwnd = es->hwndSelf;
158 TRACE("notification %d sent to %p.\n", code, es->hwndParent);
159 SendMessageW(es->hwndParent, WM_COMMAND, MAKEWPARAM(GetWindowLongPtrW(es->hwndSelf, GWLP_ID), code), (LPARAM)es->hwndSelf);
160 return IsWindow(hwnd);
163 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
165 /*********************************************************************
167 * EM_CANUNDO
170 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
172 return (es->undo_insert_count || lstrlenW(es->undo_text));
176 /*********************************************************************
178 * EM_EMPTYUNDOBUFFER
181 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
183 es->undo_insert_count = 0;
184 *es->undo_text = '\0';
187 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
189 HBRUSH hbrush;
190 UINT msg;
192 if ((!es->bEnableState || (es->style & ES_READONLY)))
193 msg = WM_CTLCOLORSTATIC;
194 else
195 msg = WM_CTLCOLOREDIT;
197 /* Why do we notify to es->hwndParent, and we send this one to GetParent()? */
198 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
199 if (!hbrush)
200 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
201 return hbrush;
205 static inline UINT get_text_length(EDITSTATE *es)
207 if(es->text_length == (UINT)-1)
208 es->text_length = lstrlenW(es->text);
209 return es->text_length;
213 /*********************************************************************
215 * EDIT_WordBreakProc
217 * Find the beginning of words.
218 * Note: unlike the specs for a WordBreakProc, this function can
219 * only be called without linebreaks between s[0] up to
220 * s[count - 1]. Remember it is only called
221 * internally, so we can decide this for ourselves.
222 * Additionally we will always be breaking the full string.
225 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
227 INT ret = 0;
229 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
231 if(!s) return 0;
233 if (!es->logAttr)
235 SCRIPT_ANALYSIS psa;
237 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
238 psa.eScript = SCRIPT_UNDEFINED;
240 es->logAttr = Alloc(sizeof(SCRIPT_LOGATTR) * get_text_length(es));
241 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
244 switch (action) {
245 case WB_LEFT:
246 if (index)
247 index--;
248 while (index && !es->logAttr[index].fSoftBreak)
249 index--;
250 ret = index;
251 break;
252 case WB_RIGHT:
253 if (!count)
254 break;
255 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
256 index++;
257 ret = index;
258 break;
259 case WB_ISDELIMITER:
260 ret = es->logAttr[index].fWhiteSpace;
261 break;
262 default:
263 ERR("unknown action code, please report !\n");
264 break;
266 return ret;
270 /*********************************************************************
272 * EDIT_CallWordBreakProc
274 * Call appropriate WordBreakProc (internal or external).
276 * Note: The "start" argument should always be an index referring
277 * to es->text. The actual wordbreak proc might be
278 * 16 bit, so we can't always pass any 32 bit LPSTR.
279 * Hence we assume that es->text is the buffer that holds
280 * the string under examination (we can decide this for ourselves).
283 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
285 INT ret;
287 if (es->word_break_proc)
288 ret = es->word_break_proc(es->text + start, index, count, action);
289 else
290 ret = EDIT_WordBreakProc(es, es->text, index + start, count + start, action) - start;
292 return ret;
295 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
297 if (line_def->ssa)
299 ScriptStringFree(&line_def->ssa);
300 line_def->ssa = NULL;
304 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
306 LINEDEF *line_def = es->first_line_def;
307 while (line_def)
309 EDIT_InvalidateUniscribeData_linedef(line_def);
310 line_def = line_def->next;
312 if (es->ssa)
314 ScriptStringFree(&es->ssa);
315 es->ssa = NULL;
319 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
321 if (!line_def)
322 return NULL;
324 if (line_def->net_length && !line_def->ssa)
326 int index = line_def->index;
327 HFONT old_font = NULL;
328 HDC udc = dc;
329 SCRIPT_TABDEF tabdef;
330 HRESULT hr;
332 if (!udc)
333 udc = GetDC(es->hwndSelf);
334 if (es->font)
335 old_font = SelectObject(udc, es->font);
337 tabdef.cTabStops = es->tabs_count;
338 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
339 tabdef.pTabStops = es->tabs;
340 tabdef.iTabOrigin = 0;
342 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
343 (1.5*line_def->net_length+16), -1,
344 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
345 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
346 if (FAILED(hr))
348 WARN("ScriptStringAnalyse failed, hr %#lx.\n", hr);
349 line_def->ssa = NULL;
352 if (es->font)
353 SelectObject(udc, old_font);
354 if (udc != dc)
355 ReleaseDC(es->hwndSelf, udc);
358 return line_def->ssa;
361 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
363 LINEDEF *line_def;
365 if (!(es->style & ES_MULTILINE))
367 if (!es->ssa)
369 INT length = get_text_length(es);
370 HFONT old_font = NULL;
371 HDC udc = dc;
373 if (!udc)
374 udc = GetDC(es->hwndSelf);
375 if (es->font)
376 old_font = SelectObject(udc, es->font);
378 if (es->style & ES_PASSWORD)
379 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);
380 else
381 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
383 if (es->font)
384 SelectObject(udc, old_font);
385 if (udc != dc)
386 ReleaseDC(es->hwndSelf, udc);
388 return es->ssa;
390 else
392 line_def = es->first_line_def;
393 while (line_def && line)
395 line_def = line_def->next;
396 line--;
399 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
403 static inline INT get_vertical_line_count(EDITSTATE *es)
405 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
406 return max(1,vlc);
409 /*********************************************************************
411 * EDIT_BuildLineDefs_ML
413 * Build linked list of text lines.
414 * Lines can end with '\0' (last line), a character (if it is wrapped),
415 * a soft return '\r\r\n' or a hard return '\r\n'
418 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
420 LPWSTR current_position, cp;
421 INT fw;
422 LINEDEF *current_line;
423 LINEDEF *previous_line;
424 LINEDEF *start_line;
425 INT line_index = 0, nstart_line, nstart_index;
426 INT line_count = es->line_count;
427 INT orig_net_length;
428 RECT rc;
429 INT vlc;
431 if (istart == iend && delta == 0)
432 return;
434 previous_line = NULL;
435 current_line = es->first_line_def;
437 /* Find starting line. istart must lie inside an existing line or
438 * at the end of buffer */
439 do {
440 if (istart < current_line->index + current_line->length ||
441 current_line->ending == END_0)
442 break;
444 previous_line = current_line;
445 current_line = current_line->next;
446 line_index++;
447 } while (current_line);
449 if (!current_line) /* Error occurred start is not inside previous buffer */
451 FIXME(" modification occurred outside buffer\n");
452 return;
455 /* Remember start of modifications in order to calculate update region */
456 nstart_line = line_index;
457 nstart_index = current_line->index;
459 /* We must start to reformat from the previous line since the modifications
460 * may have caused the line to wrap upwards. */
461 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
463 line_index--;
464 current_line = previous_line;
466 start_line = current_line;
468 fw = es->format_rect.right - es->format_rect.left;
469 current_position = es->text + current_line->index;
470 vlc = get_vertical_line_count(es);
471 do {
472 if (current_line != start_line)
474 if (!current_line || current_line->index + delta > current_position - es->text)
476 /* The buffer has been expanded, create a new line and
477 insert it into the link list */
478 LINEDEF *new_line = Alloc(sizeof(*new_line));
479 new_line->next = previous_line->next;
480 previous_line->next = new_line;
481 current_line = new_line;
482 es->line_count++;
484 else if (current_line->index + delta < current_position - es->text)
486 /* The previous line merged with this line so we delete this extra entry */
487 previous_line->next = current_line->next;
488 Free(current_line);
489 current_line = previous_line->next;
490 es->line_count--;
491 continue;
493 else /* current_line->index + delta == current_position */
495 if (current_position - es->text > iend)
496 break; /* We reached end of line modifications */
497 /* else recalculate this line */
501 current_line->index = current_position - es->text;
502 orig_net_length = current_line->net_length;
504 /* Find end of line */
505 cp = current_position;
506 while (*cp) {
507 if (*cp == '\n') break;
508 if ((*cp == '\r') && (*(cp + 1) == '\n'))
509 break;
510 cp++;
513 /* Mark type of line termination */
514 if (!(*cp)) {
515 current_line->ending = END_0;
516 current_line->net_length = lstrlenW(current_position);
517 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
518 current_line->ending = END_SOFT;
519 current_line->net_length = cp - current_position - 1;
520 } else if (*cp == '\n') {
521 current_line->ending = END_RICH;
522 current_line->net_length = cp - current_position;
523 } else {
524 current_line->ending = END_HARD;
525 current_line->net_length = cp - current_position;
528 if (current_line->net_length)
530 const SIZE *sz;
531 EDIT_InvalidateUniscribeData_linedef(current_line);
532 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
533 if (current_line->ssa)
535 sz = ScriptString_pSize(current_line->ssa);
536 /* Calculate line width */
537 current_line->width = sz->cx;
539 else current_line->width = es->char_width * current_line->net_length;
541 else current_line->width = 0;
543 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
545 /* Line breaks just look back from the end and find the next break and try that. */
547 if (!(es->style & ES_AUTOHSCROLL)) {
548 if (current_line->width > fw && fw > es->char_width) {
550 INT prev, next;
551 int w;
552 const SIZE *sz;
553 float d;
555 prev = current_line->net_length - 1;
556 w = current_line->net_length;
557 d = (float)current_line->width/(float)fw;
558 if (d > 1.2f) d -= 0.2f;
559 next = prev/d;
560 if (next >= prev) next = prev-1;
561 do {
562 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
563 next, current_line->net_length, WB_LEFT);
564 current_line->net_length = prev;
565 EDIT_InvalidateUniscribeData_linedef(current_line);
566 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
567 if (current_line->ssa)
568 sz = ScriptString_pSize(current_line->ssa);
569 else sz = 0;
570 if (sz)
571 current_line->width = sz->cx;
572 else
573 prev = 0;
574 next = prev - 1;
575 } while (prev && current_line->width > fw);
576 current_line->net_length = w;
578 if (prev == 0) { /* Didn't find a line break so force a break */
579 INT *piDx;
580 const INT *count;
582 EDIT_InvalidateUniscribeData_linedef(current_line);
583 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
585 if (current_line->ssa)
587 count = ScriptString_pcOutChars(current_line->ssa);
588 piDx = Alloc(sizeof(INT) * (*count));
589 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
591 prev = current_line->net_length-1;
592 do {
593 current_line->width -= piDx[prev];
594 prev--;
595 } while ( prev > 0 && current_line->width > fw);
596 if (prev<=0)
597 prev = 1;
598 Free(piDx);
600 else
601 prev = (fw / es->char_width);
604 /* If the first line we are calculating, wrapped before istart, we must
605 * adjust istart in order for this to be reflected in the update region. */
606 if (current_line->index == nstart_index && istart > current_line->index + prev)
607 istart = current_line->index + prev;
608 /* else if we are updating the previous line before the first line we
609 * are re-calculating and it expanded */
610 else if (current_line == start_line &&
611 current_line->index != nstart_index && orig_net_length < prev)
613 /* Line expanded due to an upwards line wrap so we must partially include
614 * previous line in update region */
615 nstart_line = line_index;
616 nstart_index = current_line->index;
617 istart = current_line->index + orig_net_length;
620 current_line->net_length = prev;
621 current_line->ending = END_WRAP;
623 if (current_line->net_length > 0)
625 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
626 if (current_line->ssa)
628 sz = ScriptString_pSize(current_line->ssa);
629 current_line->width = sz->cx;
631 else
632 current_line->width = 0;
634 else current_line->width = 0;
636 else if (current_line == start_line &&
637 current_line->index != nstart_index &&
638 orig_net_length < current_line->net_length) {
639 /* The previous line expanded but it's still not as wide as the client rect */
640 /* The expansion is due to an upwards line wrap so we must partially include
641 it in the update region */
642 nstart_line = line_index;
643 nstart_index = current_line->index;
644 istart = current_line->index + orig_net_length;
649 /* Adjust length to include line termination */
650 switch (current_line->ending) {
651 case END_SOFT:
652 current_line->length = current_line->net_length + 3;
653 break;
654 case END_RICH:
655 current_line->length = current_line->net_length + 1;
656 break;
657 case END_HARD:
658 current_line->length = current_line->net_length + 2;
659 break;
660 case END_WRAP:
661 case END_0:
662 current_line->length = current_line->net_length;
663 break;
665 es->text_width = max(es->text_width, current_line->width);
666 current_position += current_line->length;
667 previous_line = current_line;
669 /* Discard data for non-visible lines. It will be calculated as needed */
670 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
671 EDIT_InvalidateUniscribeData_linedef(current_line);
673 current_line = current_line->next;
674 line_index++;
675 } while (previous_line->ending != END_0);
677 /* Finish adjusting line indexes by delta or remove hanging lines */
678 if (previous_line->ending == END_0)
680 LINEDEF *pnext = NULL;
682 previous_line->next = NULL;
683 while (current_line)
685 pnext = current_line->next;
686 EDIT_InvalidateUniscribeData_linedef(current_line);
687 Free(current_line);
688 current_line = pnext;
689 es->line_count--;
692 else if (delta != 0)
694 while (current_line)
696 current_line->index += delta;
697 current_line = current_line->next;
701 /* Calculate rest of modification rectangle */
702 if (hrgn)
704 HRGN tmphrgn;
706 * We calculate two rectangles. One for the first line which may have
707 * an indent with respect to the format rect. The other is a format-width
708 * rectangle that spans the rest of the lines that changed or moved.
710 rc.top = es->format_rect.top + nstart_line * es->line_height -
711 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
712 rc.bottom = rc.top + es->line_height;
713 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
714 rc.left = es->format_rect.left;
715 else
716 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
717 rc.right = es->format_rect.right;
718 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
720 rc.top = rc.bottom;
721 rc.left = es->format_rect.left;
722 rc.right = es->format_rect.right;
724 * If lines were added or removed we must re-paint the remainder of the
725 * lines since the remaining lines were either shifted up or down.
727 if (line_count < es->line_count) /* We added lines */
728 rc.bottom = es->line_count * es->line_height;
729 else if (line_count > es->line_count) /* We removed lines */
730 rc.bottom = line_count * es->line_height;
731 else
732 rc.bottom = line_index * es->line_height;
733 rc.bottom += es->format_rect.top;
734 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
735 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
736 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
737 DeleteObject(tmphrgn);
741 /*********************************************************************
743 * EDIT_CalcLineWidth_SL
746 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
748 EDIT_UpdateUniscribeData(es, NULL, 0);
749 if (es->ssa)
751 const SIZE *size;
752 size = ScriptString_pSize(es->ssa);
753 es->text_width = size->cx;
755 else
756 es->text_width = 0;
759 /*********************************************************************
761 * EDIT_CharFromPos
763 * Beware: This is not the function called on EM_CHARFROMPOS
764 * The position _can_ be outside the formatting / client
765 * rectangle
766 * The return value is only the character index
769 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
771 INT index;
773 if (es->style & ES_MULTILINE) {
774 int trailing;
775 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
776 INT line_index = 0;
777 LINEDEF *line_def = es->first_line_def;
778 EDIT_UpdateUniscribeData(es, NULL, line);
779 while ((line > 0) && line_def->next) {
780 line_index += line_def->length;
781 line_def = line_def->next;
782 line--;
785 x += es->x_offset - es->format_rect.left;
786 if (es->style & ES_RIGHT)
787 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
788 else if (es->style & ES_CENTER)
789 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
790 if (x >= line_def->width) {
791 if (after_wrap)
792 *after_wrap = (line_def->ending == END_WRAP);
793 return line_index + line_def->net_length;
795 if (x <= 0 || !line_def->ssa) {
796 if (after_wrap)
797 *after_wrap = FALSE;
798 return line_index;
801 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
802 if (trailing) index++;
803 index += line_index;
804 if (after_wrap)
805 *after_wrap = ((index == line_index + line_def->net_length) &&
806 (line_def->ending == END_WRAP));
807 } else {
808 INT xoff = 0;
809 INT trailing;
810 if (after_wrap)
811 *after_wrap = FALSE;
812 x -= es->format_rect.left;
813 if (!x)
814 return es->x_offset;
816 if (!es->x_offset)
818 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
819 if (es->style & ES_RIGHT)
820 x -= indent;
821 else if (es->style & ES_CENTER)
822 x -= indent / 2;
825 EDIT_UpdateUniscribeData(es, NULL, 0);
826 if (es->x_offset)
828 if (es->ssa)
830 if (es->x_offset>= get_text_length(es))
832 const SIZE *size;
833 size = ScriptString_pSize(es->ssa);
834 xoff = size->cx;
836 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
838 else
839 xoff = 0;
841 if (x < 0)
843 if (x + xoff > 0 || !es->ssa)
845 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
846 if (trailing) index++;
848 else
849 index = 0;
851 else
853 if (x)
855 const SIZE *size = NULL;
856 if (es->ssa)
857 size = ScriptString_pSize(es->ssa);
858 if (!size)
859 index = 0;
860 else if (x > size->cx)
861 index = get_text_length(es);
862 else if (es->ssa)
864 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
865 if (trailing) index++;
867 else
868 index = 0;
870 else
871 index = es->x_offset;
874 return index;
878 /*********************************************************************
880 * EDIT_ConfinePoint
882 * adjusts the point to be within the formatting rectangle
883 * (so CharFromPos returns the nearest _visible_ character)
886 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
888 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
889 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
893 /*********************************************************************
895 * EM_LINEFROMCHAR
898 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
900 INT line;
901 LINEDEF *line_def;
903 if (!(es->style & ES_MULTILINE))
904 return 0;
905 if (index > (INT)get_text_length(es))
906 return es->line_count - 1;
907 if (index == -1)
908 index = min(es->selection_start, es->selection_end);
910 line = 0;
911 line_def = es->first_line_def;
912 index -= line_def->length;
913 while ((index >= 0) && line_def->next) {
914 line++;
915 line_def = line_def->next;
916 index -= line_def->length;
918 return line;
922 /*********************************************************************
924 * EM_LINEINDEX
927 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
929 INT line_index;
930 const LINEDEF *line_def;
932 if (!(es->style & ES_MULTILINE))
933 return 0;
934 if (line >= es->line_count)
935 return -1;
937 line_index = 0;
938 line_def = es->first_line_def;
939 if (line == -1) {
940 INT index = es->selection_end - line_def->length;
941 while ((index >= 0) && line_def->next) {
942 line_index += line_def->length;
943 line_def = line_def->next;
944 index -= line_def->length;
946 } else {
947 while (line > 0) {
948 line_index += line_def->length;
949 line_def = line_def->next;
950 line--;
953 return line_index;
957 /*********************************************************************
959 * EM_LINELENGTH
962 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
964 LINEDEF *line_def;
966 if (!(es->style & ES_MULTILINE))
967 return get_text_length(es);
969 if (index == -1) {
970 /* get the number of remaining non-selected chars of selected lines */
971 INT32 l; /* line number */
972 INT32 li; /* index of first char in line */
973 INT32 count;
974 l = EDIT_EM_LineFromChar(es, es->selection_start);
975 /* # chars before start of selection area */
976 count = es->selection_start - EDIT_EM_LineIndex(es, l);
977 l = EDIT_EM_LineFromChar(es, es->selection_end);
978 /* # chars after end of selection */
979 li = EDIT_EM_LineIndex(es, l);
980 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
981 return count;
983 line_def = es->first_line_def;
984 index -= line_def->length;
985 while ((index >= 0) && line_def->next) {
986 line_def = line_def->next;
987 index -= line_def->length;
989 return line_def->net_length;
993 /*********************************************************************
995 * EM_POSFROMCHAR
998 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1000 INT len = get_text_length(es);
1001 INT l;
1002 INT li;
1003 INT x = 0;
1004 INT y = 0;
1005 INT w;
1006 INT lw;
1007 LINEDEF *line_def;
1009 index = min(index, len);
1010 if (es->style & ES_MULTILINE) {
1011 l = EDIT_EM_LineFromChar(es, index);
1012 EDIT_UpdateUniscribeData(es, NULL, l);
1014 y = (l - es->y_offset) * es->line_height;
1015 li = EDIT_EM_LineIndex(es, l);
1016 if (after_wrap && (li == index) && l) {
1017 INT l2 = l - 1;
1018 line_def = es->first_line_def;
1019 while (l2) {
1020 line_def = line_def->next;
1021 l2--;
1023 if (line_def->ending == END_WRAP) {
1024 l--;
1025 y -= es->line_height;
1026 li = EDIT_EM_LineIndex(es, l);
1030 line_def = es->first_line_def;
1031 while (line_def->index != li)
1032 line_def = line_def->next;
1034 lw = line_def->width;
1035 w = es->format_rect.right - es->format_rect.left;
1036 if (line_def->ssa)
1037 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1038 x -= es->x_offset;
1040 if (es->style & ES_RIGHT)
1041 x = w - (lw - x);
1042 else if (es->style & ES_CENTER)
1043 x += (w - lw) / 2;
1044 } else {
1045 INT xoff = 0;
1046 INT xi = 0;
1047 EDIT_UpdateUniscribeData(es, NULL, 0);
1048 if (es->x_offset)
1050 if (es->ssa)
1052 if (es->x_offset >= get_text_length(es))
1054 int leftover = es->x_offset - get_text_length(es);
1055 if (es->ssa)
1057 const SIZE *size;
1058 size = ScriptString_pSize(es->ssa);
1059 xoff = size->cx;
1061 else
1062 xoff = 0;
1063 xoff += es->char_width * leftover;
1065 else
1066 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1068 else
1069 xoff = 0;
1071 if (index)
1073 if (index >= get_text_length(es))
1075 if (es->ssa)
1077 const SIZE *size;
1078 size = ScriptString_pSize(es->ssa);
1079 xi = size->cx;
1081 else
1082 xi = 0;
1084 else if (es->ssa)
1085 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1086 else
1087 xi = 0;
1089 x = xi - xoff;
1091 if (index >= es->x_offset) {
1092 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1094 w = es->format_rect.right - es->format_rect.left;
1095 if (w > es->text_width)
1097 if (es->style & ES_RIGHT)
1098 x += w - es->text_width;
1099 else if (es->style & ES_CENTER)
1100 x += (w - es->text_width) / 2;
1104 y = 0;
1106 x += es->format_rect.left;
1107 y += es->format_rect.top;
1108 return MAKELONG((INT16)x, (INT16)y);
1112 /*********************************************************************
1114 * EDIT_GetLineRect
1116 * Calculates the bounding rectangle for a line from a starting
1117 * column to an ending column.
1120 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1122 SCRIPT_STRING_ANALYSIS ssa;
1123 INT line_index = 0;
1124 INT pt1, pt2, pt3;
1126 if (es->style & ES_MULTILINE)
1128 const LINEDEF *line_def = NULL;
1129 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1130 if (line >= es->line_count)
1131 return;
1133 line_def = es->first_line_def;
1134 if (line == -1) {
1135 INT index = es->selection_end - line_def->length;
1136 while ((index >= 0) && line_def->next) {
1137 line_index += line_def->length;
1138 line_def = line_def->next;
1139 index -= line_def->length;
1141 } else {
1142 while (line > 0) {
1143 line_index += line_def->length;
1144 line_def = line_def->next;
1145 line--;
1148 ssa = line_def->ssa;
1150 else
1152 line_index = 0;
1153 rc->top = es->format_rect.top;
1154 ssa = es->ssa;
1157 rc->bottom = rc->top + es->line_height;
1158 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1159 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1160 if (ssa)
1162 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1163 pt3+=es->format_rect.left;
1165 else pt3 = pt1;
1166 rc->right = max(max(pt1 , pt2),pt3);
1167 rc->left = min(min(pt1, pt2),pt3);
1171 static inline void text_buffer_changed(EDITSTATE *es)
1173 es->text_length = (UINT)-1;
1175 Free(es->logAttr);
1176 es->logAttr = NULL;
1177 EDIT_InvalidateUniscribeData(es);
1180 /*********************************************************************
1181 * EDIT_LockBuffer
1184 static void EDIT_LockBuffer(EDITSTATE *es)
1186 if (!es->text)
1188 if (!es->hloc32W)
1189 return;
1191 es->text = LocalLock(es->hloc32W);
1194 es->lock_count++;
1198 /*********************************************************************
1200 * EDIT_UnlockBuffer
1203 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1205 /* Edit window might be already destroyed */
1206 if (!IsWindow(es->hwndSelf))
1208 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1209 return;
1212 if (!es->lock_count)
1214 ERR("lock_count == 0 ... please report\n");
1215 return;
1218 if (!es->text)
1220 ERR("es->text == 0 ... please report\n");
1221 return;
1224 if (force || (es->lock_count == 1))
1226 if (es->hloc32W)
1228 LocalUnlock(es->hloc32W);
1229 es->text = NULL;
1231 else
1233 ERR("no buffer ... please report\n");
1234 return;
1239 es->lock_count--;
1243 /*********************************************************************
1245 * EDIT_MakeFit
1247 * Try to fit size + 1 characters in the buffer.
1249 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1251 HLOCAL hNew32W;
1253 if (size <= es->buffer_size)
1254 return TRUE;
1256 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1258 /* Force edit to unlock its buffer. es->text now NULL */
1259 EDIT_UnlockBuffer(es, TRUE);
1261 if (es->hloc32W) {
1262 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1263 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1264 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1265 es->hloc32W = hNew32W;
1266 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1270 EDIT_LockBuffer(es);
1272 if (es->buffer_size < size) {
1273 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1274 notify_parent(es, EN_ERRSPACE);
1275 return FALSE;
1276 } else {
1277 TRACE("We now have %d+1\n", es->buffer_size);
1278 return TRUE;
1283 /*********************************************************************
1285 * EDIT_MakeUndoFit
1287 * Try to fit size + 1 bytes in the undo buffer.
1290 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1292 UINT alloc_size;
1293 WCHAR *new_undo_text;
1295 if (size <= es->undo_buffer_size)
1296 return TRUE;
1298 TRACE("trying to ReAlloc to %d+1\n", size);
1300 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1301 if ((new_undo_text = ReAlloc(es->undo_text, alloc_size))) {
1302 memset(new_undo_text + es->undo_buffer_size, 0, alloc_size - es->undo_buffer_size * sizeof(WCHAR));
1303 es->undo_text = new_undo_text;
1304 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1305 return TRUE;
1307 else
1309 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1310 return FALSE;
1315 /*********************************************************************
1317 * EDIT_UpdateTextRegion
1320 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1322 if (es->flags & EF_UPDATE) {
1323 es->flags &= ~EF_UPDATE;
1324 if (!notify_parent(es, EN_UPDATE)) return;
1326 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1330 /*********************************************************************
1332 * EDIT_UpdateText
1335 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1337 if (es->flags & EF_UPDATE) {
1338 es->flags &= ~EF_UPDATE;
1339 if (!notify_parent(es, EN_UPDATE)) return;
1341 InvalidateRect(es->hwndSelf, rc, bErase);
1344 /*********************************************************************
1346 * EDIT_SL_InvalidateText
1348 * Called from EDIT_InvalidateText().
1349 * Does the job for single-line controls only.
1352 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1354 RECT line_rect;
1355 RECT rc;
1357 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1358 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1359 EDIT_UpdateText(es, &rc, TRUE);
1362 /*********************************************************************
1364 * EDIT_ML_InvalidateText
1366 * Called from EDIT_InvalidateText().
1367 * Does the job for multi-line controls only.
1370 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1372 INT vlc = get_vertical_line_count(es);
1373 INT sl = EDIT_EM_LineFromChar(es, start);
1374 INT el = EDIT_EM_LineFromChar(es, end);
1375 INT sc;
1376 INT ec;
1377 RECT rc1;
1378 RECT rcWnd;
1379 RECT rcLine;
1380 RECT rcUpdate;
1381 INT l;
1383 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1384 return;
1386 sc = start - EDIT_EM_LineIndex(es, sl);
1387 ec = end - EDIT_EM_LineIndex(es, el);
1388 if (sl < es->y_offset) {
1389 sl = es->y_offset;
1390 sc = 0;
1392 if (el > es->y_offset + vlc) {
1393 el = es->y_offset + vlc;
1394 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1396 GetClientRect(es->hwndSelf, &rc1);
1397 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1398 if (sl == el) {
1399 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1400 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1401 EDIT_UpdateText(es, &rcUpdate, TRUE);
1402 } else {
1403 EDIT_GetLineRect(es, sl, sc,
1404 EDIT_EM_LineLength(es,
1405 EDIT_EM_LineIndex(es, sl)),
1406 &rcLine);
1407 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1408 EDIT_UpdateText(es, &rcUpdate, TRUE);
1409 for (l = sl + 1 ; l < el ; l++) {
1410 EDIT_GetLineRect(es, l, 0,
1411 EDIT_EM_LineLength(es,
1412 EDIT_EM_LineIndex(es, l)),
1413 &rcLine);
1414 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1415 EDIT_UpdateText(es, &rcUpdate, TRUE);
1417 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1418 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1419 EDIT_UpdateText(es, &rcUpdate, TRUE);
1424 /*********************************************************************
1426 * EDIT_InvalidateText
1428 * Invalidate the text from offset start up to, but not including,
1429 * offset end. Useful for (re)painting the selection.
1430 * Regions outside the linewidth are not invalidated.
1431 * end == -1 means end == TextLength.
1432 * start and end need not be ordered.
1435 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1437 if (end == start)
1438 return;
1440 if (end == -1)
1441 end = get_text_length(es);
1443 if (end < start) {
1444 INT tmp = start;
1445 start = end;
1446 end = tmp;
1449 if (es->style & ES_MULTILINE)
1450 EDIT_ML_InvalidateText(es, start, end);
1451 else
1452 EDIT_SL_InvalidateText(es, start, end);
1456 /*********************************************************************
1458 * EDIT_EM_SetSel
1460 * note: unlike the specs say: the order of start and end
1461 * _is_ preserved in Windows. (i.e. start can be > end)
1462 * In other words: this handler is OK
1465 static BOOL EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1467 UINT old_start = es->selection_start;
1468 UINT old_end = es->selection_end;
1469 UINT len = get_text_length(es);
1471 if (start == old_start && end == old_end)
1472 return FALSE;
1474 if (start == (UINT)-1) {
1475 start = es->selection_end;
1476 end = es->selection_end;
1477 } else {
1478 start = min(start, len);
1479 end = min(end, len);
1481 es->selection_start = start;
1482 es->selection_end = end;
1483 if (after_wrap)
1484 es->flags |= EF_AFTER_WRAP;
1485 else
1486 es->flags &= ~EF_AFTER_WRAP;
1487 /* Compute the necessary invalidation region. */
1488 /* Note that we don't need to invalidate regions which have
1489 * "never" been selected, or those which are "still" selected.
1490 * In fact, every time we hit a selection boundary, we can
1491 * *toggle* whether we need to invalidate. Thus we can optimize by
1492 * *sorting* the interval endpoints. Let's assume that we sort them
1493 * in this order:
1494 * start <= end <= old_start <= old_end
1495 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1496 * in 5 comparisons; i.e. it is impossible to do better than the
1497 * following: */
1498 ORDER_UINT(end, old_end);
1499 ORDER_UINT(start, old_start);
1500 ORDER_UINT(old_start, old_end);
1501 ORDER_UINT(start, end);
1502 /* Note that at this point 'end' and 'old_start' are not in order, but
1503 * start is definitely the min. and old_end is definitely the max. */
1504 if (end != old_start)
1507 * One can also do
1508 * ORDER_UINT32(end, old_start);
1509 * EDIT_InvalidateText(es, start, end);
1510 * EDIT_InvalidateText(es, old_start, old_end);
1511 * in place of the following if statement.
1512 * (That would complete the optimal five-comparison four-element sort.)
1514 if (old_start > end )
1516 EDIT_InvalidateText(es, start, end);
1517 EDIT_InvalidateText(es, old_start, old_end);
1519 else
1521 EDIT_InvalidateText(es, start, old_start);
1522 EDIT_InvalidateText(es, end, old_end);
1525 else EDIT_InvalidateText(es, start, old_end);
1527 return TRUE;
1531 /*********************************************************************
1533 * EDIT_UpdateScrollInfo
1536 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1538 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1540 SCROLLINFO si;
1541 si.cbSize = sizeof(SCROLLINFO);
1542 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1543 si.nMin = 0;
1544 si.nMax = es->line_count - 1;
1545 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1546 si.nPos = es->y_offset;
1547 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1548 si.nMin, si.nMax, si.nPage, si.nPos);
1549 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1552 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1554 SCROLLINFO si;
1555 si.cbSize = sizeof(SCROLLINFO);
1556 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1557 si.nMin = 0;
1558 si.nMax = es->text_width - 1;
1559 si.nPage = es->format_rect.right - es->format_rect.left;
1560 si.nPos = es->x_offset;
1561 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1562 si.nMin, si.nMax, si.nPage, si.nPos);
1563 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1568 /*********************************************************************
1570 * EDIT_EM_LineScroll_internal
1572 * Version of EDIT_EM_LineScroll for internal use.
1573 * It doesn't refuse if ES_MULTILINE is set and assumes that
1574 * dx is in pixels, dy - in lines.
1577 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1579 INT nyoff;
1580 INT x_offset_in_pixels;
1581 INT lines_per_page;
1583 if (!es->line_height || !es->char_width)
1584 return TRUE;
1586 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1588 if (es->style & ES_MULTILINE)
1590 x_offset_in_pixels = es->x_offset;
1592 else
1594 dy = 0;
1595 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1598 if (-dx > x_offset_in_pixels)
1599 dx = -x_offset_in_pixels;
1600 if (dx > es->text_width - x_offset_in_pixels)
1601 dx = es->text_width - x_offset_in_pixels;
1602 nyoff = max(0, es->y_offset + dy);
1603 if (nyoff >= es->line_count - lines_per_page)
1604 nyoff = max(0, es->line_count - lines_per_page);
1605 dy = (es->y_offset - nyoff) * es->line_height;
1606 if (dx || dy) {
1607 RECT rc1;
1608 RECT rc;
1610 es->y_offset = nyoff;
1611 if(es->style & ES_MULTILINE)
1612 es->x_offset += dx;
1613 else
1614 es->x_offset += dx / es->char_width;
1616 GetClientRect(es->hwndSelf, &rc1);
1617 IntersectRect(&rc, &rc1, &es->format_rect);
1618 ScrollWindowEx(es->hwndSelf, -dx, dy,
1619 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1620 /* force scroll info update */
1621 EDIT_UpdateScrollInfo(es);
1623 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1624 notify_parent(es, EN_HSCROLL);
1625 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1626 notify_parent(es, EN_VSCROLL);
1627 return TRUE;
1630 /*********************************************************************
1632 * EM_LINESCROLL
1634 * NOTE: dx is in average character widths, dy - in lines;
1637 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1639 if (!(es->style & ES_MULTILINE))
1640 return FALSE;
1642 dx *= es->char_width;
1643 return EDIT_EM_LineScroll_internal(es, dx, dy);
1647 /*********************************************************************
1649 * EM_SCROLL
1652 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1654 INT dy;
1656 if (!(es->style & ES_MULTILINE))
1657 return (LRESULT)FALSE;
1659 dy = 0;
1661 switch (action) {
1662 case SB_LINEUP:
1663 if (es->y_offset)
1664 dy = -1;
1665 break;
1666 case SB_LINEDOWN:
1667 if (es->y_offset < es->line_count - 1)
1668 dy = 1;
1669 break;
1670 case SB_PAGEUP:
1671 if (es->y_offset)
1672 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1673 break;
1674 case SB_PAGEDOWN:
1675 if (es->y_offset < es->line_count - 1)
1676 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1677 break;
1678 default:
1679 return (LRESULT)FALSE;
1681 if (dy) {
1682 INT vlc = get_vertical_line_count(es);
1683 /* check if we are going to move too far */
1684 if(es->y_offset + dy > es->line_count - vlc)
1685 dy = max(es->line_count - vlc, 0) - es->y_offset;
1687 /* Notification is done in EDIT_EM_LineScroll */
1688 if(dy) {
1689 EDIT_EM_LineScroll(es, 0, dy);
1690 return MAKELONG(dy, TRUE);
1694 return (LRESULT)FALSE;
1698 static void EDIT_UpdateImmCompositionWindow(EDITSTATE *es, UINT x, UINT y)
1700 COMPOSITIONFORM form =
1702 .dwStyle = CFS_RECT,
1703 .ptCurrentPos = {.x = x, .y = y},
1704 .rcArea = es->format_rect,
1706 HIMC himc = ImmGetContext(es->hwndSelf);
1707 ImmSetCompositionWindow(himc, &form);
1708 ImmReleaseContext(es->hwndSelf, himc);
1712 /*********************************************************************
1714 * EDIT_SetCaretPos
1717 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1718 BOOL after_wrap)
1720 LRESULT res;
1722 if (es->flags & EF_FOCUSED)
1724 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1725 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1726 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1727 EDIT_UpdateImmCompositionWindow(es, (short)LOWORD(res), (short)HIWORD(res));
1732 /*********************************************************************
1734 * EM_SCROLLCARET
1737 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1739 if (es->style & ES_MULTILINE) {
1740 INT l;
1741 INT vlc;
1742 INT ww;
1743 INT cw = es->char_width;
1744 INT x;
1745 INT dy = 0;
1746 INT dx = 0;
1748 l = EDIT_EM_LineFromChar(es, es->selection_end);
1749 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1750 vlc = get_vertical_line_count(es);
1751 if (l >= es->y_offset + vlc)
1752 dy = l - vlc + 1 - es->y_offset;
1753 if (l < es->y_offset)
1754 dy = l - es->y_offset;
1755 ww = es->format_rect.right - es->format_rect.left;
1756 if (x < es->format_rect.left)
1757 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1758 if (x > es->format_rect.right)
1759 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1760 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1762 /* check if we are going to move too far */
1763 if(es->x_offset + dx + ww > es->text_width)
1764 dx = es->text_width - ww - es->x_offset;
1765 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1766 EDIT_EM_LineScroll_internal(es, dx, dy);
1768 } else {
1769 INT x;
1770 INT goal;
1771 INT format_width;
1773 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1774 format_width = es->format_rect.right - es->format_rect.left;
1775 if (x < es->format_rect.left) {
1776 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1777 do {
1778 es->x_offset--;
1779 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1780 } while ((x < goal) && es->x_offset);
1781 /* FIXME: use ScrollWindow() somehow to improve performance */
1782 EDIT_UpdateText(es, NULL, TRUE);
1783 } else if (x > es->format_rect.right) {
1784 INT x_last;
1785 INT len = get_text_length(es);
1786 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1787 do {
1788 es->x_offset++;
1789 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1790 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1791 } while ((x > goal) && (x_last > es->format_rect.right));
1792 /* FIXME: use ScrollWindow() somehow to improve performance */
1793 EDIT_UpdateText(es, NULL, TRUE);
1797 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1801 /*********************************************************************
1803 * EDIT_MoveBackward
1806 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1808 INT e = es->selection_end;
1810 if (e) {
1811 e--;
1812 if ((es->style & ES_MULTILINE) && e &&
1813 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1814 e--;
1815 if (e && (es->text[e - 1] == '\r'))
1816 e--;
1819 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1820 EDIT_EM_ScrollCaret(es);
1824 /*********************************************************************
1826 * EDIT_MoveDown_ML
1828 * Only for multi line controls
1829 * Move the caret one line down, on a column with the nearest
1830 * x coordinate on the screen (might be a different column).
1833 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1835 INT s = es->selection_start;
1836 INT e = es->selection_end;
1837 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1838 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1839 INT x = (short)LOWORD(pos);
1840 INT y = (short)HIWORD(pos);
1842 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1843 if (!extend)
1844 s = e;
1845 EDIT_EM_SetSel(es, s, e, after_wrap);
1846 EDIT_EM_ScrollCaret(es);
1850 /*********************************************************************
1852 * EDIT_MoveEnd
1855 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1857 BOOL after_wrap = FALSE;
1858 INT e;
1860 /* Pass a high value in x to make sure of receiving the end of the line */
1861 if (!ctrl && (es->style & ES_MULTILINE))
1862 e = EDIT_CharFromPos(es, 0x3fffffff,
1863 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1864 else
1865 e = get_text_length(es);
1866 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1867 EDIT_EM_ScrollCaret(es);
1871 /*********************************************************************
1873 * EDIT_MoveForward
1876 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1878 INT e = es->selection_end;
1880 if (es->text[e]) {
1881 e++;
1882 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1883 if (es->text[e] == '\n')
1884 e++;
1885 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1886 e += 2;
1889 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1890 EDIT_EM_ScrollCaret(es);
1894 /*********************************************************************
1896 * EDIT_MoveHome
1898 * Home key: move to beginning of line.
1901 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1903 INT e;
1905 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1906 if (!ctrl && (es->style & ES_MULTILINE))
1907 e = EDIT_CharFromPos(es, -es->x_offset,
1908 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1909 else
1910 e = 0;
1911 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1912 EDIT_EM_ScrollCaret(es);
1916 /*********************************************************************
1918 * EDIT_MovePageDown_ML
1920 * Only for multi line controls
1921 * Move the caret one page down, on a column with the nearest
1922 * x coordinate on the screen (might be a different column).
1925 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1927 INT s = es->selection_start;
1928 INT e = es->selection_end;
1929 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1930 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1931 INT x = (short)LOWORD(pos);
1932 INT y = (short)HIWORD(pos);
1934 e = EDIT_CharFromPos(es, x,
1935 y + (es->format_rect.bottom - es->format_rect.top),
1936 &after_wrap);
1937 if (!extend)
1938 s = e;
1939 EDIT_EM_SetSel(es, s, e, after_wrap);
1940 EDIT_EM_ScrollCaret(es);
1944 /*********************************************************************
1946 * EDIT_MovePageUp_ML
1948 * Only for multi line controls
1949 * Move the caret one page up, on a column with the nearest
1950 * x coordinate on the screen (might be a different column).
1953 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1955 INT s = es->selection_start;
1956 INT e = es->selection_end;
1957 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1958 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1959 INT x = (short)LOWORD(pos);
1960 INT y = (short)HIWORD(pos);
1962 e = EDIT_CharFromPos(es, x,
1963 y - (es->format_rect.bottom - es->format_rect.top),
1964 &after_wrap);
1965 if (!extend)
1966 s = e;
1967 EDIT_EM_SetSel(es, s, e, after_wrap);
1968 EDIT_EM_ScrollCaret(es);
1972 /*********************************************************************
1974 * EDIT_MoveUp_ML
1976 * Only for multi line controls
1977 * Move the caret one line up, on a column with the nearest
1978 * x coordinate on the screen (might be a different column).
1981 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1983 INT s = es->selection_start;
1984 INT e = es->selection_end;
1985 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1986 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1987 INT x = (short)LOWORD(pos);
1988 INT y = (short)HIWORD(pos);
1990 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1991 if (!extend)
1992 s = e;
1993 EDIT_EM_SetSel(es, s, e, after_wrap);
1994 EDIT_EM_ScrollCaret(es);
1998 /*********************************************************************
2000 * EDIT_MoveWordBackward
2003 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2005 INT s = es->selection_start;
2006 INT e = es->selection_end;
2007 INT l;
2008 INT ll;
2009 INT li;
2011 l = EDIT_EM_LineFromChar(es, e);
2012 ll = EDIT_EM_LineLength(es, e);
2013 li = EDIT_EM_LineIndex(es, l);
2014 if (e - li == 0) {
2015 if (l) {
2016 li = EDIT_EM_LineIndex(es, l - 1);
2017 e = li + EDIT_EM_LineLength(es, li);
2019 } else {
2020 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2022 if (!extend)
2023 s = e;
2024 EDIT_EM_SetSel(es, s, e, FALSE);
2025 EDIT_EM_ScrollCaret(es);
2029 /*********************************************************************
2031 * EDIT_MoveWordForward
2034 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2036 INT s = es->selection_start;
2037 INT e = es->selection_end;
2038 INT l;
2039 INT ll;
2040 INT li;
2042 l = EDIT_EM_LineFromChar(es, e);
2043 ll = EDIT_EM_LineLength(es, e);
2044 li = EDIT_EM_LineIndex(es, l);
2045 if (e - li == ll) {
2046 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2047 e = EDIT_EM_LineIndex(es, l + 1);
2048 } else {
2049 e = li + EDIT_CallWordBreakProc(es,
2050 li, e - li + 1, ll, WB_RIGHT);
2052 if (!extend)
2053 s = e;
2054 EDIT_EM_SetSel(es, s, e, FALSE);
2055 EDIT_EM_ScrollCaret(es);
2059 /*********************************************************************
2061 * EDIT_PaintText
2064 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2066 COLORREF BkColor;
2067 COLORREF TextColor;
2068 INT ret;
2069 INT li;
2070 INT BkMode;
2071 SIZE size;
2073 if (!count)
2074 return 0;
2075 BkMode = GetBkMode(dc);
2076 BkColor = GetBkColor(dc);
2077 TextColor = GetTextColor(dc);
2078 if (rev) {
2079 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2080 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2081 SetBkMode(dc, OPAQUE);
2083 li = EDIT_EM_LineIndex(es, line);
2084 if (es->style & ES_MULTILINE) {
2085 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2086 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2087 } else {
2088 TextOutW(dc, x, y, es->text + li + col, count);
2089 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2090 ret = size.cx;
2092 if (rev) {
2093 SetBkColor(dc, BkColor);
2094 SetTextColor(dc, TextColor);
2095 SetBkMode(dc, BkMode);
2097 return ret;
2101 /*********************************************************************
2103 * EDIT_PaintLine
2106 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2108 INT s = 0;
2109 INT e = 0;
2110 INT li = 0;
2111 INT ll = 0;
2112 INT x;
2113 INT y;
2114 LRESULT pos;
2115 SCRIPT_STRING_ANALYSIS ssa;
2117 if (es->style & ES_MULTILINE) {
2118 INT vlc = get_vertical_line_count(es);
2120 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2121 return;
2122 } else if (line)
2123 return;
2125 TRACE("line=%d\n", line);
2127 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2128 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2129 x = (short)LOWORD(pos);
2130 y = (short)HIWORD(pos);
2132 if (es->style & ES_MULTILINE)
2134 int line_idx = line;
2135 x = -es->x_offset;
2136 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2138 LINEDEF *line_def = es->first_line_def;
2139 int w, lw;
2141 while (line_def && line_idx)
2143 line_def = line_def->next;
2144 line_idx--;
2146 w = es->format_rect.right - es->format_rect.left;
2147 lw = line_def->width;
2149 if (es->style & ES_RIGHT)
2150 x = w - (lw - x);
2151 else if (es->style & ES_CENTER)
2152 x += (w - lw) / 2;
2154 x += es->format_rect.left;
2157 if (rev)
2159 li = EDIT_EM_LineIndex(es, line);
2160 ll = EDIT_EM_LineLength(es, li);
2161 s = min(es->selection_start, es->selection_end);
2162 e = max(es->selection_start, es->selection_end);
2163 s = min(li + ll, max(li, s));
2164 e = min(li + ll, max(li, e));
2167 if (ssa)
2168 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2169 else if (rev && (s != e) &&
2170 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2171 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2172 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2173 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2174 } else
2175 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2177 if (es->cue_banner_text && es->text_length == 0 && (!(es->flags & EF_FOCUSED) || es->cue_banner_draw_focused))
2179 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
2180 TextOutW(dc, x, y, es->cue_banner_text, lstrlenW(es->cue_banner_text));
2185 /*********************************************************************
2187 * EDIT_AdjustFormatRect
2189 * Adjusts the format rectangle for the current font and the
2190 * current client rectangle.
2193 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2195 RECT ClientRect;
2197 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2198 if (es->style & ES_MULTILINE)
2200 INT fw, vlc, max_x_offset, max_y_offset;
2202 vlc = get_vertical_line_count(es);
2203 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2205 /* correct es->x_offset */
2206 fw = es->format_rect.right - es->format_rect.left;
2207 max_x_offset = es->text_width - fw;
2208 if(max_x_offset < 0) max_x_offset = 0;
2209 if(es->x_offset > max_x_offset)
2210 es->x_offset = max_x_offset;
2212 /* correct es->y_offset */
2213 max_y_offset = es->line_count - vlc;
2214 if(max_y_offset < 0) max_y_offset = 0;
2215 if(es->y_offset > max_y_offset)
2216 es->y_offset = max_y_offset;
2218 /* force scroll info update */
2219 EDIT_UpdateScrollInfo(es);
2221 else
2222 /* Windows doesn't care to fix text placement for SL controls */
2223 es->format_rect.bottom = es->format_rect.top + es->line_height;
2225 /* Always stay within the client area */
2226 GetClientRect(es->hwndSelf, &ClientRect);
2227 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2229 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2230 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2232 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2236 /*********************************************************************
2238 * EDIT_SetRectNP
2240 * note: this is not (exactly) the handler called on EM_SETRECTNP
2241 * it is also used to set the rect of a single line control
2244 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2246 LONG_PTR ExStyle;
2247 INT bw, bh;
2248 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2250 CopyRect(&es->format_rect, rc);
2252 if (ExStyle & WS_EX_CLIENTEDGE) {
2253 es->format_rect.left++;
2254 es->format_rect.right--;
2256 if (es->format_rect.bottom - es->format_rect.top
2257 >= es->line_height + 2)
2259 es->format_rect.top++;
2260 es->format_rect.bottom--;
2263 else if (es->style & WS_BORDER) {
2264 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2265 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2266 InflateRect(&es->format_rect, -bw, 0);
2267 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2268 InflateRect(&es->format_rect, 0, -bh);
2271 es->format_rect.left += es->left_margin;
2272 es->format_rect.right -= es->right_margin;
2273 EDIT_AdjustFormatRect(es);
2277 /*********************************************************************
2279 * EM_CHARFROMPOS
2281 * returns line number (not index) in high-order word of result.
2282 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2283 * to Richedit, not to the edit control. Original documentation is valid.
2284 * FIXME: do the specs mean to return -1 if outside client area or
2285 * if outside formatting rectangle ???
2288 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2290 POINT pt;
2291 RECT rc;
2292 INT index;
2294 pt.x = x;
2295 pt.y = y;
2296 GetClientRect(es->hwndSelf, &rc);
2297 if (!PtInRect(&rc, pt))
2298 return -1;
2300 index = EDIT_CharFromPos(es, x, y, NULL);
2301 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2305 /*********************************************************************
2307 * EM_FMTLINES
2309 * Enable or disable soft breaks.
2311 * This means: insert or remove the soft linebreak character (\r\r\n).
2312 * Take care to check if the text still fits the buffer after insertion.
2313 * If not, notify with EN_ERRSPACE.
2316 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2318 es->flags &= ~EF_USE_SOFTBRK;
2319 if (add_eol) {
2320 es->flags |= EF_USE_SOFTBRK;
2321 FIXME("soft break enabled, not implemented\n");
2323 return add_eol;
2327 /*********************************************************************
2329 * EM_GETHANDLE
2331 * Hopefully this won't fire back at us.
2332 * We always start with a fixed buffer in the local heap.
2333 * Despite of the documentation says that the local heap is used
2334 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2335 * buffer on the local heap.
2338 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2340 if (!(es->style & ES_MULTILINE))
2341 return 0;
2343 EDIT_UnlockBuffer(es, TRUE);
2345 /* The text buffer handle belongs to the app */
2346 es->hlocapp = es->hloc32W;
2348 TRACE("Returning %p, LocalSize() = %Id\n", es->hlocapp, LocalSize(es->hlocapp));
2349 return es->hlocapp;
2353 /*********************************************************************
2355 * EM_GETLINE
2358 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst)
2360 INT line_len, dst_len;
2361 LPWSTR src;
2362 INT i;
2364 if (es->style & ES_MULTILINE)
2366 if (line >= es->line_count)
2367 return 0;
2369 else
2370 line = 0;
2372 i = EDIT_EM_LineIndex(es, line);
2373 src = es->text + i;
2374 line_len = EDIT_EM_LineLength(es, i);
2375 dst_len = *(WORD *)dst;
2377 if (dst_len <= line_len)
2379 memcpy(dst, src, dst_len * sizeof(WCHAR));
2380 return dst_len;
2382 else /* Append 0 if enough space */
2384 memcpy(dst, src, line_len * sizeof(WCHAR));
2385 dst[line_len] = 0;
2386 return line_len;
2391 /*********************************************************************
2393 * EM_GETSEL
2396 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2398 UINT s = es->selection_start;
2399 UINT e = es->selection_end;
2401 ORDER_UINT(s, e);
2402 if (start)
2403 *start = s;
2404 if (end)
2405 *end = e;
2406 return MAKELONG(s, e);
2410 /*********************************************************************
2412 * EM_REPLACESEL
2414 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2417 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2418 BOOL send_update, BOOL honor_limit)
2420 UINT tl = get_text_length(es);
2421 UINT utl;
2422 UINT s;
2423 UINT e;
2424 UINT i;
2425 UINT size;
2426 LPWSTR p;
2427 HRGN hrgn = 0;
2428 LPWSTR buf = NULL;
2429 UINT bufl;
2431 TRACE("%s, can_undo %d, send_update %d\n",
2432 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2434 s = es->selection_start;
2435 e = es->selection_end;
2437 EDIT_InvalidateUniscribeData(es);
2438 if ((s == e) && !strl)
2439 return;
2441 ORDER_UINT(s, e);
2443 size = tl - (e - s) + strl;
2444 if (!size)
2445 es->text_width = 0;
2447 /* Issue the EN_MAXTEXT notification and continue with replacing text
2448 * so that buffer limit is honored. */
2449 if ((honor_limit) && (size > es->buffer_limit))
2451 if (!notify_parent(es, EN_MAXTEXT)) return;
2452 /* Buffer limit can be smaller than the actual length of text in combobox */
2453 if (es->buffer_limit < (tl - (e-s)))
2454 strl = 0;
2455 else
2456 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2459 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2460 return;
2462 if (e != s) {
2463 /* there is something to be deleted */
2464 TRACE("deleting stuff.\n");
2465 bufl = e - s;
2466 buf = Alloc((bufl + 1) * sizeof(WCHAR));
2467 if (!buf) return;
2468 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2469 buf[bufl] = 0; /* ensure 0 termination */
2470 /* now delete */
2471 lstrcpyW(es->text + s, es->text + e);
2472 text_buffer_changed(es);
2474 if (strl) {
2475 /* there is an insertion */
2476 tl = get_text_length(es);
2477 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));
2478 for (p = es->text + tl ; p >= es->text + s ; p--)
2479 p[strl] = p[0];
2480 for (i = 0 , p = es->text + s ; i < strl ; i++)
2481 p[i] = lpsz_replace[i];
2482 if(es->style & ES_UPPERCASE)
2483 CharUpperBuffW(p, strl);
2484 else if(es->style & ES_LOWERCASE)
2485 CharLowerBuffW(p, strl);
2486 text_buffer_changed(es);
2488 if (es->style & ES_MULTILINE)
2490 INT st = min(es->selection_start, es->selection_end);
2491 INT vlc = get_vertical_line_count(es);
2493 hrgn = CreateRectRgn(0, 0, 0, 0);
2494 EDIT_BuildLineDefs_ML(es, st, st + strl,
2495 strl - abs(es->selection_end - es->selection_start), hrgn);
2496 /* if text is too long undo all changes */
2497 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2498 if (strl)
2499 lstrcpyW(es->text + e, es->text + e + strl);
2500 if (e != s)
2501 for (i = 0 , p = es->text ; i < e - s ; i++)
2502 p[i + s] = buf[i];
2503 text_buffer_changed(es);
2504 EDIT_BuildLineDefs_ML(es, s, e,
2505 abs(es->selection_end - es->selection_start) - strl, hrgn);
2506 strl = 0;
2507 e = s;
2508 SetRectRgn(hrgn, 0, 0, 0, 0);
2509 if (!notify_parent(es, EN_MAXTEXT)) return;
2512 else {
2513 INT fw = es->format_rect.right - es->format_rect.left;
2514 EDIT_InvalidateUniscribeData(es);
2515 EDIT_CalcLineWidth_SL(es);
2516 /* remove chars that don't fit */
2517 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2518 while ((es->text_width > fw) && s + strl >= s) {
2519 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2520 strl--;
2521 es->text_length = -1;
2522 EDIT_InvalidateUniscribeData(es);
2523 EDIT_CalcLineWidth_SL(es);
2525 text_buffer_changed(es);
2526 if (!notify_parent(es, EN_MAXTEXT)) return;
2530 if (e != s) {
2531 if (can_undo) {
2532 utl = lstrlenW(es->undo_text);
2533 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2534 /* undo-buffer is extended to the right */
2535 EDIT_MakeUndoFit(es, utl + e - s);
2536 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2537 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2538 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2539 /* undo-buffer is extended to the left */
2540 EDIT_MakeUndoFit(es, utl + e - s);
2541 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2542 p[e - s] = p[0];
2543 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2544 p[i] = buf[i];
2545 es->undo_position = s;
2546 } else {
2547 /* new undo-buffer */
2548 EDIT_MakeUndoFit(es, e - s);
2549 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2550 es->undo_text[e - s] = 0; /* ensure 0 termination */
2551 es->undo_position = s;
2553 /* any deletion makes the old insertion-undo invalid */
2554 es->undo_insert_count = 0;
2555 } else
2556 EDIT_EM_EmptyUndoBuffer(es);
2558 if (strl) {
2559 if (can_undo) {
2560 if ((s == es->undo_position) ||
2561 ((es->undo_insert_count) &&
2562 (s == es->undo_position + es->undo_insert_count)))
2564 * insertion is new and at delete position or
2565 * an extension to either left or right
2567 es->undo_insert_count += strl;
2568 else {
2569 /* new insertion undo */
2570 es->undo_position = s;
2571 es->undo_insert_count = strl;
2572 /* new insertion makes old delete-buffer invalid */
2573 *es->undo_text = '\0';
2575 } else
2576 EDIT_EM_EmptyUndoBuffer(es);
2579 Free(buf);
2581 s += strl;
2583 /* If text has been deleted and we're right or center aligned then scroll rightward */
2584 if (es->style & (ES_RIGHT | ES_CENTER))
2586 INT delta = strl - abs(es->selection_end - es->selection_start);
2588 if (delta < 0 && es->x_offset)
2590 if (abs(delta) > es->x_offset)
2591 es->x_offset = 0;
2592 else
2593 es->x_offset += delta;
2597 EDIT_EM_SetSel(es, s, s, FALSE);
2598 es->flags |= EF_MODIFIED;
2599 if (send_update) es->flags |= EF_UPDATE;
2600 if (hrgn)
2602 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2603 DeleteObject(hrgn);
2605 else
2606 EDIT_UpdateText(es, NULL, TRUE);
2608 EDIT_EM_ScrollCaret(es);
2610 /* force scroll info update */
2611 EDIT_UpdateScrollInfo(es);
2614 if(send_update || (es->flags & EF_UPDATE))
2616 es->flags &= ~EF_UPDATE;
2617 if (!notify_parent(es, EN_CHANGE)) return;
2619 EDIT_InvalidateUniscribeData(es);
2623 /*********************************************************************
2625 * EM_SETHANDLE
2627 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2630 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2632 if (!(es->style & ES_MULTILINE))
2633 return;
2635 if (!hloc)
2636 return;
2638 EDIT_UnlockBuffer(es, TRUE);
2640 es->hloc32W = hloc;
2641 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2643 /* The text buffer handle belongs to the control */
2644 es->hlocapp = NULL;
2646 EDIT_LockBuffer(es);
2647 text_buffer_changed(es);
2649 es->x_offset = es->y_offset = 0;
2650 es->selection_start = es->selection_end = 0;
2651 EDIT_EM_EmptyUndoBuffer(es);
2652 es->flags &= ~EF_MODIFIED;
2653 es->flags &= ~EF_UPDATE;
2654 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2655 EDIT_UpdateText(es, NULL, TRUE);
2656 EDIT_EM_ScrollCaret(es);
2657 /* force scroll info update */
2658 EDIT_UpdateScrollInfo(es);
2662 /*********************************************************************
2664 * EM_SETLIMITTEXT
2666 * NOTE: this version currently implements WinNT limits
2669 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2671 if (!limit) limit = ~0u;
2672 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2673 es->buffer_limit = limit;
2676 static BOOL is_cjk(HDC dc)
2678 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2679 FONTSIGNATURE fs;
2681 switch (GdiGetCodePage(dc)) {
2682 case 932: case 936: case 949: case 950: case 1361:
2683 return TRUE;
2684 default:
2685 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2686 (fs.fsCsb[0] & FS_DBCS_MASK));
2690 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2692 int margin;
2693 if (side_bearing < 0)
2694 margin = min(-side_bearing, width/2);
2695 else
2696 margin = 0;
2697 return margin;
2700 struct char_width_info {
2701 INT min_lsb, min_rsb, unknown;
2704 /* Undocumented gdi32 export */
2705 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2707 /*********************************************************************
2709 * EM_SETMARGINS
2711 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2712 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2713 * margin according to the textmetrics of the current font.
2715 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2716 * equal to or larger than a certain size. The empty client rect is treated as
2717 * 80 pixels width.
2719 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2720 WORD left, WORD right, BOOL repaint)
2722 TEXTMETRICW tm;
2723 INT default_left_margin = 0; /* in pixels */
2724 INT default_right_margin = 0; /* in pixels */
2726 /* Set the default margins depending on the font */
2727 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2728 HDC dc = GetDC(es->hwndSelf);
2729 HFONT old_font = SelectObject(dc, es->font);
2730 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2731 RECT rc;
2733 /* The default margins are only non zero for TrueType or Vector fonts */
2734 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2735 struct char_width_info width_info;
2737 if (is_cjk(dc) && GetCharWidthInfo(dc, &width_info))
2739 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2740 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2742 else
2744 default_left_margin = width / 2;
2745 default_right_margin = width / 2;
2748 GetClientRect(es->hwndSelf, &rc);
2749 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2750 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2751 default_left_margin = es->left_margin;
2752 default_right_margin = es->right_margin;
2755 SelectObject(dc, old_font);
2756 ReleaseDC(es->hwndSelf, dc);
2759 if (action & EC_LEFTMARGIN) {
2760 es->format_rect.left -= es->left_margin;
2761 if (left != EC_USEFONTINFO)
2762 es->left_margin = left;
2763 else
2764 es->left_margin = default_left_margin;
2765 es->format_rect.left += es->left_margin;
2768 if (action & EC_RIGHTMARGIN) {
2769 es->format_rect.right += es->right_margin;
2770 if (right != EC_USEFONTINFO)
2771 es->right_margin = right;
2772 else
2773 es->right_margin = default_right_margin;
2774 es->format_rect.right -= es->right_margin;
2777 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2778 EDIT_AdjustFormatRect(es);
2779 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2782 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2786 /*********************************************************************
2788 * EM_SETPASSWORDCHAR
2791 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2793 LONG style;
2795 if (es->style & ES_MULTILINE)
2796 return;
2798 if (es->password_char == c)
2799 return;
2801 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2802 es->password_char = c;
2803 if (c) {
2804 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2805 es->style |= ES_PASSWORD;
2806 } else {
2807 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2808 es->style &= ~ES_PASSWORD;
2810 EDIT_InvalidateUniscribeData(es);
2811 EDIT_UpdateText(es, NULL, TRUE);
2815 /*********************************************************************
2817 * EM_SETTABSTOPS
2820 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2822 if (!(es->style & ES_MULTILINE))
2823 return FALSE;
2824 Free(es->tabs);
2825 es->tabs_count = count;
2826 if (!count)
2827 es->tabs = NULL;
2828 else {
2829 es->tabs = Alloc(count * sizeof(INT));
2830 memcpy(es->tabs, tabs, count * sizeof(INT));
2832 EDIT_InvalidateUniscribeData(es);
2833 return TRUE;
2837 /*********************************************************************
2839 * EM_SETWORDBREAKPROC
2842 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, EDITWORDBREAKPROCW wbp)
2844 if (es->word_break_proc == wbp)
2845 return;
2847 es->word_break_proc = wbp;
2849 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2850 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2851 EDIT_UpdateText(es, NULL, TRUE);
2856 /*********************************************************************
2858 * EM_UNDO / WM_UNDO
2861 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2863 INT ulength;
2864 LPWSTR utext;
2866 /* As per MSDN spec, for a single-line edit control,
2867 the return value is always TRUE */
2868 if( es->style & ES_READONLY )
2869 return !(es->style & ES_MULTILINE);
2871 ulength = lstrlenW(es->undo_text);
2873 utext = Alloc((ulength + 1) * sizeof(WCHAR));
2875 lstrcpyW(utext, es->undo_text);
2877 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2878 es->undo_insert_count, debugstr_w(utext));
2880 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2881 EDIT_EM_EmptyUndoBuffer(es);
2882 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
2883 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2884 /* send the notification after the selection start and end are set */
2885 if (!notify_parent(es, EN_CHANGE)) return TRUE;
2886 EDIT_EM_ScrollCaret(es);
2887 Free(utext);
2889 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2890 es->undo_insert_count, debugstr_w(es->undo_text));
2891 return TRUE;
2895 /* Helper function for WM_CHAR
2897 * According to an MSDN blog article titled "Just because you're a control
2898 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2899 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2900 * a dialog box or not.
2902 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2904 return (es->flags & EF_DIALOGMODE);
2908 /*********************************************************************
2910 * WM_PASTE
2913 static void EDIT_WM_Paste(EDITSTATE *es)
2915 HGLOBAL hsrc;
2916 LPWSTR src, ptr;
2917 int len;
2919 /* Protect read-only edit control from modification */
2920 if(es->style & ES_READONLY)
2921 return;
2923 OpenClipboard(es->hwndSelf);
2924 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2925 src = GlobalLock(hsrc);
2926 len = lstrlenW(src);
2927 /* Protect single-line edit against pasting new line character */
2928 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
2929 len = ptr - src;
2930 if (len && src[len - 1] == '\r')
2931 --len;
2933 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
2934 GlobalUnlock(hsrc);
2936 else if (es->style & ES_PASSWORD) {
2937 /* clear selected text in password edit box even with empty clipboard */
2938 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2940 CloseClipboard();
2944 /*********************************************************************
2946 * WM_COPY
2949 static void EDIT_WM_Copy(EDITSTATE *es)
2951 INT s = min(es->selection_start, es->selection_end);
2952 INT e = max(es->selection_start, es->selection_end);
2953 HGLOBAL hdst;
2954 LPWSTR dst;
2955 DWORD len;
2957 if (e == s) return;
2959 len = e - s;
2960 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2961 dst = GlobalLock(hdst);
2962 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2963 dst[len] = 0; /* ensure 0 termination */
2964 TRACE("%s\n", debugstr_w(dst));
2965 GlobalUnlock(hdst);
2966 OpenClipboard(es->hwndSelf);
2967 EmptyClipboard();
2968 SetClipboardData(CF_UNICODETEXT, hdst);
2969 CloseClipboard();
2973 /*********************************************************************
2975 * WM_CLEAR
2978 static inline void EDIT_WM_Clear(EDITSTATE *es)
2980 /* Protect read-only edit control from modification */
2981 if(es->style & ES_READONLY)
2982 return;
2984 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2988 /*********************************************************************
2990 * WM_CUT
2993 static inline void EDIT_WM_Cut(EDITSTATE *es)
2995 EDIT_WM_Copy(es);
2996 EDIT_WM_Clear(es);
3000 /*********************************************************************
3002 * WM_CHAR
3005 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3007 BOOL control;
3009 control = GetKeyState(VK_CONTROL) & 0x8000;
3011 switch (c) {
3012 case '\r':
3013 /* If it's not a multiline edit box, it would be ignored below.
3014 * For multiline edit without ES_WANTRETURN, we have to make a
3015 * special case.
3017 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3018 if (EDIT_IsInsideDialog(es))
3019 break;
3020 case '\n':
3021 if (es->style & ES_MULTILINE) {
3022 if (es->style & ES_READONLY) {
3023 EDIT_MoveHome(es, FALSE, FALSE);
3024 EDIT_MoveDown_ML(es, FALSE);
3025 } else
3026 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3028 break;
3029 case '\t':
3030 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3032 if (EDIT_IsInsideDialog(es))
3033 break;
3034 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3036 break;
3037 case VK_BACK:
3038 if (!(es->style & ES_READONLY) && !control) {
3039 if (es->selection_start != es->selection_end)
3040 EDIT_WM_Clear(es);
3041 else {
3042 /* delete character left of caret */
3043 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3044 EDIT_MoveBackward(es, TRUE);
3045 EDIT_WM_Clear(es);
3048 break;
3049 case 0x03: /* ^C */
3050 if (!(es->style & ES_PASSWORD))
3051 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3052 break;
3053 case 0x16: /* ^V */
3054 if (!(es->style & ES_READONLY))
3055 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3056 break;
3057 case 0x18: /* ^X */
3058 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3059 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3060 break;
3061 case 0x1A: /* ^Z */
3062 if (!(es->style & ES_READONLY))
3063 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3064 break;
3066 default:
3067 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3068 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3069 break;
3071 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3072 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3073 break;
3075 return 1;
3079 /*********************************************************************
3081 * EDIT_ContextMenuCommand
3084 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3086 switch (id) {
3087 case EM_UNDO:
3088 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3089 break;
3090 case WM_CUT:
3091 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3092 break;
3093 case WM_COPY:
3094 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3095 break;
3096 case WM_PASTE:
3097 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3098 break;
3099 case WM_CLEAR:
3100 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3101 break;
3102 case EM_SETSEL:
3103 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3104 break;
3105 default:
3106 ERR("unknown menu item, please report\n");
3107 break;
3112 /*********************************************************************
3114 * WM_CONTEXTMENU
3116 * Note: the resource files resource/sysres_??.rc cannot define a
3117 * single popup menu. Hence we use a (dummy) menubar
3118 * containing the single popup menu as its first item.
3120 * FIXME: the message identifiers have been chosen arbitrarily,
3121 * hence we use MF_BYPOSITION.
3122 * We might as well use the "real" values (anybody knows ?)
3123 * The menu definition is in resources/sysres_??.rc.
3124 * Once these are OK, we better use MF_BYCOMMAND here
3125 * (as we do in EDIT_WM_Command()).
3128 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3130 HMENU menu = LoadMenuA(GetModuleHandleA("user32.dll"), "EDITMENU");
3131 HMENU popup = GetSubMenu(menu, 0);
3132 UINT start = es->selection_start;
3133 UINT end = es->selection_end;
3134 UINT cmd;
3135 POINT pt;
3137 ORDER_UINT(start, end);
3139 /* undo */
3140 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3141 /* cut */
3142 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3143 /* copy */
3144 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3145 /* paste */
3146 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3147 /* delete */
3148 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3149 /* select all */
3150 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3152 pt.x = x;
3153 pt.y = y;
3155 if (pt.x == -1 && pt.y == -1) /* passed via VK_APPS press/release */
3157 RECT rc;
3159 /* Windows places the menu at the edit's center in this case */
3160 GetClientRect(es->hwndSelf, &rc);
3161 pt.x = rc.left + (rc.right - rc.left) / 2;
3162 pt.y = rc.top + (rc.bottom - rc.top) / 2;
3163 ClientToScreen(es->hwndSelf, &pt);
3166 if (!(es->flags & EF_FOCUSED))
3167 SetFocus(es->hwndSelf);
3169 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3170 pt.x, pt.y, 0, es->hwndSelf, NULL);
3172 if (cmd)
3173 EDIT_ContextMenuCommand(es, cmd);
3175 DestroyMenu(menu);
3179 /*********************************************************************
3181 * WM_GETTEXT
3184 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst)
3186 if (!count)
3187 return 0;
3189 lstrcpynW(dst, es->text, count);
3190 return lstrlenW(dst);
3193 /*********************************************************************
3195 * EDIT_CheckCombo
3198 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3200 HWND hLBox = es->hwndListBox;
3201 HWND hCombo;
3202 BOOL bDropped;
3203 int nEUI;
3205 if (!hLBox)
3206 return FALSE;
3208 hCombo = GetParent(es->hwndSelf);
3209 bDropped = TRUE;
3210 nEUI = 0;
3212 TRACE("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3214 if (key == VK_UP || key == VK_DOWN)
3216 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3217 nEUI = 1;
3219 if (msg == WM_KEYDOWN || nEUI)
3220 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3223 switch (msg)
3225 case WM_KEYDOWN:
3226 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3228 /* make sure ComboLBox pops up */
3229 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3230 key = VK_F4;
3231 nEUI = 2;
3234 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3235 break;
3237 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3238 if (nEUI)
3239 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3240 else
3241 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3242 break;
3245 if (nEUI == 2)
3246 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3248 return TRUE;
3252 /*********************************************************************
3254 * WM_KEYDOWN
3256 * Handling of special keys that don't produce a WM_CHAR
3257 * (i.e. non-printable keys) & Backspace & Delete
3260 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3262 BOOL shift;
3263 BOOL control;
3265 if (GetKeyState(VK_MENU) & 0x8000)
3266 return 0;
3268 shift = GetKeyState(VK_SHIFT) & 0x8000;
3269 control = GetKeyState(VK_CONTROL) & 0x8000;
3271 switch (key) {
3272 case VK_F4:
3273 case VK_UP:
3274 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3275 break;
3277 /* fall through */
3278 case VK_LEFT:
3279 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3280 EDIT_MoveUp_ML(es, shift);
3281 else
3282 if (control)
3283 EDIT_MoveWordBackward(es, shift);
3284 else
3285 EDIT_MoveBackward(es, shift);
3286 break;
3287 case VK_DOWN:
3288 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3289 break;
3290 /* fall through */
3291 case VK_RIGHT:
3292 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3293 EDIT_MoveDown_ML(es, shift);
3294 else if (control)
3295 EDIT_MoveWordForward(es, shift);
3296 else
3297 EDIT_MoveForward(es, shift);
3298 break;
3299 case VK_HOME:
3300 EDIT_MoveHome(es, shift, control);
3301 break;
3302 case VK_END:
3303 EDIT_MoveEnd(es, shift, control);
3304 break;
3305 case VK_PRIOR:
3306 if (es->style & ES_MULTILINE)
3307 EDIT_MovePageUp_ML(es, shift);
3308 else
3309 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3310 break;
3311 case VK_NEXT:
3312 if (es->style & ES_MULTILINE)
3313 EDIT_MovePageDown_ML(es, shift);
3314 else
3315 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3316 break;
3317 case VK_DELETE:
3318 if (!(es->style & ES_READONLY) && !(shift && control)) {
3319 if (es->selection_start != es->selection_end) {
3320 if (shift)
3321 EDIT_WM_Cut(es);
3322 else
3323 EDIT_WM_Clear(es);
3324 } else {
3325 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3326 if (shift)
3327 /* delete character left of caret */
3328 EDIT_MoveBackward(es, TRUE);
3329 else if (control)
3330 /* delete to end of line */
3331 EDIT_MoveEnd(es, TRUE, FALSE);
3332 else
3333 /* delete character right of caret */
3334 EDIT_MoveForward(es, TRUE);
3335 EDIT_WM_Clear(es);
3338 break;
3339 case VK_INSERT:
3340 if (shift) {
3341 if (!(es->style & ES_READONLY))
3342 EDIT_WM_Paste(es);
3343 } else if (control)
3344 EDIT_WM_Copy(es);
3345 break;
3346 case VK_RETURN:
3347 /* If the edit doesn't want the return send a message to the default object */
3348 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3350 DWORD dw;
3352 if (!EDIT_IsInsideDialog(es)) break;
3353 if (control) break;
3354 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3355 if (HIWORD(dw) == DC_HASDEFID)
3357 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3358 if (hwDefCtrl)
3360 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3361 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3365 break;
3366 case VK_ESCAPE:
3367 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3368 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3369 break;
3370 case VK_TAB:
3371 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3372 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3373 break;
3374 case 'A':
3375 if (control)
3377 if (EDIT_EM_SetSel(es, 0, get_text_length(es), FALSE))
3379 if (!notify_parent(es, EN_UPDATE)) break;
3380 notify_parent(es, EN_CHANGE);
3383 break;
3385 return TRUE;
3389 /*********************************************************************
3391 * WM_KILLFOCUS
3394 static LRESULT EDIT_WM_KillFocus(HTHEME theme, EDITSTATE *es)
3396 UINT flags = RDW_INVALIDATE;
3398 es->flags &= ~EF_FOCUSED;
3399 DestroyCaret();
3400 if (!(es->style & ES_NOHIDESEL))
3401 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3402 if (!notify_parent(es, EN_KILLFOCUS)) return 0;
3403 /* Throw away left over scroll when we lose focus */
3404 es->wheelDeltaRemainder = 0;
3406 if (theme)
3407 flags |= RDW_FRAME;
3409 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3410 return 0;
3414 /*********************************************************************
3416 * WM_LBUTTONDBLCLK
3418 * The caret position has been set on the WM_LBUTTONDOWN message
3421 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3423 INT s;
3424 INT e = es->selection_end;
3425 INT l;
3426 INT li;
3427 INT ll;
3429 es->bCaptureState = TRUE;
3430 SetCapture(es->hwndSelf);
3432 l = EDIT_EM_LineFromChar(es, e);
3433 li = EDIT_EM_LineIndex(es, l);
3434 ll = EDIT_EM_LineLength(es, e);
3435 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3436 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3437 EDIT_EM_SetSel(es, s, e, FALSE);
3438 EDIT_EM_ScrollCaret(es);
3439 return 0;
3443 /*********************************************************************
3445 * WM_LBUTTONDOWN
3448 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3450 INT e;
3451 BOOL after_wrap;
3453 es->bCaptureState = TRUE;
3454 SetCapture(es->hwndSelf);
3455 EDIT_ConfinePoint(es, &x, &y);
3456 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3457 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3458 EDIT_EM_ScrollCaret(es);
3460 if (!(es->flags & EF_FOCUSED))
3461 SetFocus(es->hwndSelf);
3463 return 0;
3467 /*********************************************************************
3469 * WM_LBUTTONUP
3472 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3474 if (es->bCaptureState) {
3475 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3477 es->bCaptureState = FALSE;
3478 return 0;
3482 /*********************************************************************
3484 * WM_MBUTTONDOWN
3487 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3489 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3490 return 0;
3494 /*********************************************************************
3496 * WM_MOUSEMOVE
3499 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3501 INT e;
3502 BOOL after_wrap;
3504 /* If the mouse has been captured by process other than the edit control itself,
3505 * the windows edit controls will not select the strings with mouse move.
3507 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3508 return 0;
3510 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3511 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3512 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3513 EDIT_EM_ScrollCaret(es);
3514 return 0;
3518 /*********************************************************************
3520 * WM_PAINT
3523 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3525 PAINTSTRUCT ps;
3526 INT i;
3527 HDC dc;
3528 HFONT old_font = 0;
3529 RECT rc;
3530 RECT rcClient;
3531 RECT rcLine;
3532 RECT rcRgn;
3533 HBRUSH brush;
3534 HBRUSH old_brush;
3535 INT bw, bh;
3536 BOOL rev = es->bEnableState &&
3537 ((es->flags & EF_FOCUSED) ||
3538 (es->style & ES_NOHIDESEL));
3539 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3541 /* The dc we use for calculating may not be the one we paint into.
3542 This is the safest action. */
3543 EDIT_InvalidateUniscribeData(es);
3544 GetClientRect(es->hwndSelf, &rcClient);
3546 /* get the background brush */
3547 brush = EDIT_NotifyCtlColor(es, dc);
3549 /* paint the border and the background */
3550 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3552 if(es->style & WS_BORDER) {
3553 bw = GetSystemMetrics(SM_CXBORDER);
3554 bh = GetSystemMetrics(SM_CYBORDER);
3555 rc = rcClient;
3556 if(es->style & ES_MULTILINE) {
3557 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3558 if(es->style & WS_VSCROLL) rc.right+=bw;
3561 /* Draw the frame. Same code as in nonclient.c */
3562 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3563 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3564 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3565 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3566 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3567 SelectObject(dc, old_brush);
3569 /* Keep the border clean */
3570 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3571 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3574 GetClipBox(dc, &rc);
3575 FillRect(dc, &rc, brush);
3577 IntersectClipRect(dc, es->format_rect.left,
3578 es->format_rect.top,
3579 es->format_rect.right,
3580 es->format_rect.bottom);
3581 if (es->style & ES_MULTILINE) {
3582 rc = rcClient;
3583 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3585 if (es->font)
3586 old_font = SelectObject(dc, es->font);
3588 if (!es->bEnableState)
3589 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3590 GetClipBox(dc, &rcRgn);
3591 if (es->style & ES_MULTILINE) {
3592 INT vlc = get_vertical_line_count(es);
3593 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3594 EDIT_UpdateUniscribeData(es, dc, i);
3595 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3596 if (IntersectRect(&rc, &rcRgn, &rcLine))
3597 EDIT_PaintLine(es, dc, i, rev);
3599 } else {
3600 EDIT_UpdateUniscribeData(es, dc, 0);
3601 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3602 if (IntersectRect(&rc, &rcRgn, &rcLine))
3603 EDIT_PaintLine(es, dc, 0, rev);
3605 if (es->font)
3606 SelectObject(dc, old_font);
3608 if (!hdc)
3609 EndPaint(es->hwndSelf, &ps);
3612 static void EDIT_WM_NCPaint(HWND hwnd, HRGN region)
3614 DWORD exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3615 HTHEME theme = GetWindowTheme(hwnd);
3616 HRGN cliprgn = region;
3618 if (theme && exStyle & WS_EX_CLIENTEDGE)
3620 HDC dc;
3621 RECT r;
3622 int cxEdge = GetSystemMetrics(SM_CXEDGE),
3623 cyEdge = GetSystemMetrics(SM_CYEDGE);
3624 const int part = EP_EDITTEXT;
3625 int state = ETS_NORMAL;
3626 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3628 if (!IsWindowEnabled(hwnd))
3629 state = ETS_DISABLED;
3630 else if (dwStyle & ES_READONLY)
3631 state = ETS_READONLY;
3632 else if (GetFocus() == hwnd)
3633 state = ETS_FOCUSED;
3635 GetWindowRect(hwnd, &r);
3637 /* New clipping region passed to default proc to exclude border */
3638 cliprgn = CreateRectRgn(r.left + cxEdge, r.top + cyEdge,
3639 r.right - cxEdge, r.bottom - cyEdge);
3640 if (region != (HRGN)1)
3641 CombineRgn(cliprgn, cliprgn, region, RGN_AND);
3642 OffsetRect(&r, -r.left, -r.top);
3644 dc = GetDCEx(hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
3646 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
3647 DrawThemeParentBackground(hwnd, dc, &r);
3648 DrawThemeBackground(theme, dc, part, state, &r, 0);
3649 ReleaseDC(hwnd, dc);
3652 /* Call default proc to get the scrollbars etc. also painted */
3653 DefWindowProcW (hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
3654 if (cliprgn != region)
3655 DeleteObject(cliprgn);
3658 /*********************************************************************
3660 * WM_SETFOCUS
3663 static void EDIT_WM_SetFocus(HTHEME theme, EDITSTATE *es)
3665 UINT flags = RDW_INVALIDATE;
3667 es->flags |= EF_FOCUSED;
3669 if (!(es->style & ES_NOHIDESEL))
3670 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3672 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3673 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3674 ShowCaret(es->hwndSelf);
3675 if (!notify_parent(es, EN_SETFOCUS)) return;
3677 if (theme)
3678 flags |= RDW_FRAME | RDW_ERASE;
3680 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3684 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm)
3686 ABC abc[256];
3687 SHORT left, right;
3688 UINT i;
3690 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3691 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3693 if (!is_cjk(hdc))
3694 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3696 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3697 return 0;
3699 left = right = 0;
3700 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3701 if (-abc[i].abcA > right) right = -abc[i].abcA;
3702 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3704 return MAKELONG(left, right);
3707 static void EDIT_UpdateImmCompositionFont(EDITSTATE *es)
3709 LOGFONTW composition_font;
3710 HIMC himc = ImmGetContext(es->hwndSelf);
3711 GetObjectW(es->font, sizeof(LOGFONTW), &composition_font);
3712 ImmSetCompositionFontW(himc, &composition_font);
3713 ImmReleaseContext(es->hwndSelf, himc);
3716 /*********************************************************************
3718 * WM_SETFONT
3720 * With Win95 look the margins are set to default font value unless
3721 * the system font (font == 0) is being set, in which case they are left
3722 * unchanged.
3725 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3727 TEXTMETRICW tm;
3728 HDC dc;
3729 HFONT old_font = 0;
3730 RECT clientRect;
3731 DWORD margins;
3733 es->font = font;
3734 EDIT_InvalidateUniscribeData(es);
3735 dc = GetDC(es->hwndSelf);
3736 if (font)
3737 old_font = SelectObject(dc, font);
3738 GetTextMetricsW(dc, &tm);
3739 es->line_height = tm.tmHeight;
3740 es->char_width = tm.tmAveCharWidth;
3741 margins = get_font_margins(dc, &tm);
3742 if (font)
3743 SelectObject(dc, old_font);
3744 ReleaseDC(es->hwndSelf, dc);
3746 /* Reset the format rect and the margins */
3747 GetClientRect(es->hwndSelf, &clientRect);
3748 EDIT_SetRectNP(es, &clientRect);
3749 if (margins)
3750 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3751 LOWORD(margins), HIWORD(margins), FALSE);
3753 if (es->style & ES_MULTILINE)
3754 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3755 else
3756 EDIT_CalcLineWidth_SL(es);
3758 if (redraw)
3759 EDIT_UpdateText(es, NULL, TRUE);
3760 if (es->flags & EF_FOCUSED) {
3761 DestroyCaret();
3762 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3763 EDIT_SetCaretPos(es, es->selection_end,
3764 es->flags & EF_AFTER_WRAP);
3765 ShowCaret(es->hwndSelf);
3768 EDIT_UpdateImmCompositionFont(es);
3772 /*********************************************************************
3774 * WM_SETTEXT
3776 * NOTES
3777 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3778 * The modified flag is reset. No notifications are sent.
3780 * For single-line controls, reception of WM_SETTEXT triggers:
3781 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3784 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text)
3786 if (es->flags & EF_UPDATE)
3787 /* fixed this bug once; complain if we see it about to happen again. */
3788 ERR("SetSel may generate UPDATE message whose handler may reset "
3789 "selection.\n");
3791 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3792 if (text)
3794 TRACE("%s\n", debugstr_w(text));
3795 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3797 else
3799 TRACE("<NULL>\n");
3800 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3802 es->x_offset = 0;
3803 es->flags &= ~EF_MODIFIED;
3804 EDIT_EM_SetSel(es, 0, 0, FALSE);
3806 /* Send the notification after the selection start and end have been set
3807 * edit control doesn't send notification on WM_SETTEXT
3808 * if it is multiline, or it is part of combobox
3810 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3812 if (!notify_parent(es, EN_UPDATE)) return;
3813 if (!notify_parent(es, EN_CHANGE)) return;
3815 EDIT_EM_ScrollCaret(es);
3816 EDIT_UpdateScrollInfo(es);
3817 EDIT_InvalidateUniscribeData(es);
3821 /*********************************************************************
3823 * WM_SIZE
3826 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3828 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3829 RECT rc;
3830 GetClientRect(es->hwndSelf, &rc);
3831 EDIT_SetRectNP(es, &rc);
3832 EDIT_UpdateText(es, NULL, TRUE);
3837 /*********************************************************************
3839 * WM_STYLECHANGED
3841 * This message is sent by SetWindowLong on having changed either the Style
3842 * or the extended style.
3844 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3846 * See also EDIT_WM_NCCreate
3848 * It appears that the Windows version of the edit control allows the style
3849 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3850 * style variable which will generally be different. In this function we
3851 * update the internal style based on what changed in the externally visible
3852 * style.
3854 * Much of this content as based upon the MSDN, especially:
3855 * Platform SDK Documentation -> User Interface Services ->
3856 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3857 * Edit Control Styles
3859 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3861 if (GWL_STYLE == which) {
3862 DWORD style_change_mask;
3863 DWORD new_style;
3864 /* Only a subset of changes can be applied after the control
3865 * has been created.
3867 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3868 ES_NUMBER;
3869 if (es->style & ES_MULTILINE)
3870 style_change_mask |= ES_WANTRETURN;
3872 new_style = style->styleNew & style_change_mask;
3874 /* Number overrides lowercase overrides uppercase (at least it
3875 * does in Win95). However I'll bet that ES_NUMBER would be
3876 * invalid under Win 3.1.
3878 if (new_style & ES_NUMBER) {
3879 ; /* do not override the ES_NUMBER */
3880 } else if (new_style & ES_LOWERCASE) {
3881 new_style &= ~ES_UPPERCASE;
3884 es->style = (es->style & ~style_change_mask) | new_style;
3885 } else if (GWL_EXSTYLE == which) {
3886 ; /* FIXME - what is needed here */
3887 } else {
3888 WARN ("Invalid style change %#Ix.\n", which);
3891 return 0;
3894 /*********************************************************************
3896 * WM_SYSKEYDOWN
3899 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3901 if ((key == VK_BACK) && (key_data & 0x2000)) {
3902 if (EDIT_EM_CanUndo(es))
3903 EDIT_EM_Undo(es);
3904 return 0;
3905 } else if (key == VK_UP || key == VK_DOWN) {
3906 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3907 return 0;
3909 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3912 /*********************************************************************
3914 * WM_HSCROLL
3917 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3919 INT dx;
3920 INT fw;
3922 if (!(es->style & ES_MULTILINE))
3923 return 0;
3925 if (!(es->style & ES_AUTOHSCROLL))
3926 return 0;
3928 dx = 0;
3929 fw = es->format_rect.right - es->format_rect.left;
3930 switch (action) {
3931 case SB_LINELEFT:
3932 TRACE("SB_LINELEFT\n");
3933 if (es->x_offset)
3934 dx = -es->char_width;
3935 break;
3936 case SB_LINERIGHT:
3937 TRACE("SB_LINERIGHT\n");
3938 if (es->x_offset < es->text_width)
3939 dx = es->char_width;
3940 break;
3941 case SB_PAGELEFT:
3942 TRACE("SB_PAGELEFT\n");
3943 if (es->x_offset)
3944 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3945 break;
3946 case SB_PAGERIGHT:
3947 TRACE("SB_PAGERIGHT\n");
3948 if (es->x_offset < es->text_width)
3949 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3950 break;
3951 case SB_LEFT:
3952 TRACE("SB_LEFT\n");
3953 if (es->x_offset)
3954 dx = -es->x_offset;
3955 break;
3956 case SB_RIGHT:
3957 TRACE("SB_RIGHT\n");
3958 if (es->x_offset < es->text_width)
3959 dx = es->text_width - es->x_offset;
3960 break;
3961 case SB_THUMBTRACK:
3962 TRACE("SB_THUMBTRACK %d\n", pos);
3963 es->flags |= EF_HSCROLL_TRACK;
3964 if(es->style & WS_HSCROLL)
3965 dx = pos - es->x_offset;
3966 else
3968 INT fw, new_x;
3969 /* Sanity check */
3970 if(pos < 0 || pos > 100) return 0;
3971 /* Assume default scroll range 0-100 */
3972 fw = es->format_rect.right - es->format_rect.left;
3973 new_x = pos * (es->text_width - fw) / 100;
3974 dx = es->text_width ? (new_x - es->x_offset) : 0;
3976 break;
3977 case SB_THUMBPOSITION:
3978 TRACE("SB_THUMBPOSITION %d\n", pos);
3979 es->flags &= ~EF_HSCROLL_TRACK;
3980 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3981 dx = pos - es->x_offset;
3982 else
3984 INT fw, new_x;
3985 /* Sanity check */
3986 if(pos < 0 || pos > 100) return 0;
3987 /* Assume default scroll range 0-100 */
3988 fw = es->format_rect.right - es->format_rect.left;
3989 new_x = pos * (es->text_width - fw) / 100;
3990 dx = es->text_width ? (new_x - es->x_offset) : 0;
3992 if (!dx) {
3993 /* force scroll info update */
3994 EDIT_UpdateScrollInfo(es);
3995 notify_parent(es, EN_HSCROLL);
3997 break;
3998 case SB_ENDSCROLL:
3999 TRACE("SB_ENDSCROLL\n");
4000 break;
4002 * FIXME : the next two are undocumented !
4003 * Are we doing the right thing ?
4004 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4005 * although it's also a regular control message.
4007 case EM_GETTHUMB: /* this one is used by NT notepad */
4009 LRESULT ret;
4010 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4011 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4012 else
4014 /* Assume default scroll range 0-100 */
4015 INT fw = es->format_rect.right - es->format_rect.left;
4016 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4018 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4019 return ret;
4021 case EM_LINESCROLL:
4022 TRACE("EM_LINESCROLL16\n");
4023 dx = pos;
4024 break;
4026 default:
4027 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4028 action, action);
4029 return 0;
4031 if (dx)
4033 INT fw = es->format_rect.right - es->format_rect.left;
4034 /* check if we are going to move too far */
4035 if(es->x_offset + dx + fw > es->text_width)
4036 dx = es->text_width - fw - es->x_offset;
4037 if(dx)
4038 EDIT_EM_LineScroll_internal(es, dx, 0);
4040 return 0;
4044 /*********************************************************************
4046 * WM_VSCROLL
4049 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4051 INT dy;
4053 if (!(es->style & ES_MULTILINE))
4054 return 0;
4056 if (!(es->style & ES_AUTOVSCROLL))
4057 return 0;
4059 dy = 0;
4060 switch (action) {
4061 case SB_LINEUP:
4062 case SB_LINEDOWN:
4063 case SB_PAGEUP:
4064 case SB_PAGEDOWN:
4065 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4066 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4067 (action == SB_PAGEUP ? "SB_PAGEUP" :
4068 "SB_PAGEDOWN"))));
4069 EDIT_EM_Scroll(es, action);
4070 return 0;
4071 case SB_TOP:
4072 TRACE("SB_TOP\n");
4073 dy = -es->y_offset;
4074 break;
4075 case SB_BOTTOM:
4076 TRACE("SB_BOTTOM\n");
4077 dy = es->line_count - 1 - es->y_offset;
4078 break;
4079 case SB_THUMBTRACK:
4080 TRACE("SB_THUMBTRACK %d\n", pos);
4081 es->flags |= EF_VSCROLL_TRACK;
4082 if(es->style & WS_VSCROLL)
4083 dy = pos - es->y_offset;
4084 else
4086 /* Assume default scroll range 0-100 */
4087 INT vlc, new_y;
4088 /* Sanity check */
4089 if(pos < 0 || pos > 100) return 0;
4090 vlc = get_vertical_line_count(es);
4091 new_y = pos * (es->line_count - vlc) / 100;
4092 dy = es->line_count ? (new_y - es->y_offset) : 0;
4093 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4094 es->line_count, es->y_offset, pos, dy);
4096 break;
4097 case SB_THUMBPOSITION:
4098 TRACE("SB_THUMBPOSITION %d\n", pos);
4099 es->flags &= ~EF_VSCROLL_TRACK;
4100 if(es->style & WS_VSCROLL)
4101 dy = pos - es->y_offset;
4102 else
4104 /* Assume default scroll range 0-100 */
4105 INT vlc, new_y;
4106 /* Sanity check */
4107 if(pos < 0 || pos > 100) return 0;
4108 vlc = get_vertical_line_count(es);
4109 new_y = pos * (es->line_count - vlc) / 100;
4110 dy = es->line_count ? (new_y - es->y_offset) : 0;
4111 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4112 es->line_count, es->y_offset, pos, dy);
4114 if (!dy)
4116 /* force scroll info update */
4117 EDIT_UpdateScrollInfo(es);
4118 notify_parent(es, EN_VSCROLL);
4120 break;
4121 case SB_ENDSCROLL:
4122 TRACE("SB_ENDSCROLL\n");
4123 break;
4125 * FIXME : the next two are undocumented !
4126 * Are we doing the right thing ?
4127 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4128 * although it's also a regular control message.
4130 case EM_GETTHUMB: /* this one is used by NT notepad */
4132 LRESULT ret;
4133 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4134 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4135 else
4137 /* Assume default scroll range 0-100 */
4138 INT vlc = get_vertical_line_count(es);
4139 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4141 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4142 return ret;
4144 case EM_LINESCROLL:
4145 TRACE("EM_LINESCROLL %d\n", pos);
4146 dy = pos;
4147 break;
4149 default:
4150 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4151 action, action);
4152 return 0;
4154 if (dy)
4155 EDIT_EM_LineScroll(es, 0, dy);
4156 return 0;
4159 /*********************************************************************
4161 * EM_GETTHUMB
4163 * FIXME: is this right ? (or should it be only VSCROLL)
4164 * (and maybe only for edit controls that really have their
4165 * own scrollbars) (and maybe only for multiline controls ?)
4166 * All in all: very poorly documented
4169 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4171 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4172 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4175 /*********************************************************************
4177 * EM_SETCUEBANNER
4180 static BOOL EDIT_EM_SetCueBanner(EDITSTATE *es, BOOL draw_focused, const WCHAR *cue_text)
4182 if (es->style & ES_MULTILINE || !cue_text)
4183 return FALSE;
4185 Free(es->cue_banner_text);
4186 es->cue_banner_text = wcsdup(cue_text);
4187 es->cue_banner_draw_focused = draw_focused;
4189 return TRUE;
4192 /*********************************************************************
4194 * EM_GETCUEBANNER
4197 static BOOL EDIT_EM_GetCueBanner(EDITSTATE *es, WCHAR *buf, DWORD size)
4199 if (es->style & ES_MULTILINE)
4200 return FALSE;
4202 if (!es->cue_banner_text)
4204 if (buf && size)
4205 *buf = 0;
4206 return FALSE;
4208 else
4210 if (buf)
4211 lstrcpynW(buf, es->cue_banner_text, size);
4212 return TRUE;
4217 /********************************************************************
4219 * The Following code is to handle inline editing from IMEs
4222 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4224 LONG buflen;
4225 LPWSTR lpResultStr;
4227 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4228 if (buflen <= 0)
4230 return;
4233 lpResultStr = Alloc(buflen);
4234 if (!lpResultStr)
4236 ERR("Unable to alloc buffer for IME string\n");
4237 return;
4240 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4241 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4242 Free(lpResultStr);
4245 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4247 HIMC hIMC;
4249 hIMC = ImmGetContext(hwnd);
4250 if (!hIMC)
4251 return;
4253 if (CompFlag & GCS_RESULTSTR)
4254 EDIT_GetResultStr(hIMC, es);
4256 ImmReleaseContext(hwnd, hIMC);
4260 /*********************************************************************
4262 * WM_NCCREATE
4264 * See also EDIT_WM_StyleChanged
4266 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
4268 EDITSTATE *es;
4269 UINT alloc_size;
4271 TRACE("Creating edit control, style = %#lx\n", lpcs->style);
4273 if (!(es = Alloc(sizeof(*es))))
4274 return FALSE;
4275 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4278 * Note: since the EDITSTATE has not been fully initialized yet,
4279 * we can't use any API calls that may send
4280 * WM_XXX messages before WM_NCCREATE is completed.
4283 es->style = lpcs->style;
4285 es->bEnableState = !(es->style & WS_DISABLED);
4287 es->hwndSelf = hwnd;
4288 /* Save parent, which will be notified by EN_* messages */
4289 es->hwndParent = lpcs->hwndParent;
4291 if (es->style & ES_COMBO)
4292 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4294 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4295 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4297 /* Number overrides lowercase overrides uppercase (at least it
4298 * does in Win95). However I'll bet that ES_NUMBER would be
4299 * invalid under Win 3.1.
4301 if (es->style & ES_NUMBER) {
4302 ; /* do not override the ES_NUMBER */
4303 } else if (es->style & ES_LOWERCASE) {
4304 es->style &= ~ES_UPPERCASE;
4306 if (es->style & ES_MULTILINE) {
4307 es->buffer_limit = BUFLIMIT_INITIAL;
4308 if (es->style & WS_VSCROLL)
4309 es->style |= ES_AUTOVSCROLL;
4310 if (es->style & WS_HSCROLL)
4311 es->style |= ES_AUTOHSCROLL;
4312 es->style &= ~ES_PASSWORD;
4313 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4314 /* Confirmed - RIGHT overrides CENTER */
4315 if (es->style & ES_RIGHT)
4316 es->style &= ~ES_CENTER;
4317 es->style &= ~WS_HSCROLL;
4318 es->style &= ~ES_AUTOHSCROLL;
4320 } else {
4321 es->buffer_limit = BUFLIMIT_INITIAL;
4322 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4323 es->style &= ~ES_CENTER;
4324 es->style &= ~WS_HSCROLL;
4325 es->style &= ~WS_VSCROLL;
4326 if (es->style & ES_PASSWORD)
4327 es->password_char = '*';
4330 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4331 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4332 goto cleanup;
4333 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4335 if (!(es->undo_text = Alloc((es->buffer_size + 1) * sizeof(WCHAR))))
4336 goto cleanup;
4337 es->undo_buffer_size = es->buffer_size;
4339 if (es->style & ES_MULTILINE)
4340 if (!(es->first_line_def = Alloc(sizeof(LINEDEF))))
4341 goto cleanup;
4342 es->line_count = 1;
4345 * In Win95 look and feel, the WS_BORDER style is replaced by the
4346 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4347 * control a nonclient area so we don't need to draw the border.
4348 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4349 * a nonclient area and we should handle painting the border ourselves.
4351 * When making modifications please ensure that the code still works
4352 * for edit controls created directly with style 0x50800000, exStyle 0
4353 * (which should have a single pixel border)
4355 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4356 es->style &= ~WS_BORDER;
4357 else if (es->style & WS_BORDER)
4358 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4360 return TRUE;
4362 cleanup:
4363 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4364 EDIT_InvalidateUniscribeData(es);
4365 Free(es->first_line_def);
4366 Free(es->undo_text);
4367 if (es->hloc32W) LocalFree(es->hloc32W);
4368 Free(es->logAttr);
4369 Free(es);
4370 return FALSE;
4374 /*********************************************************************
4376 * WM_CREATE
4379 static LRESULT EDIT_WM_Create(EDITSTATE *es, const WCHAR *name)
4381 RECT clientRect;
4383 TRACE("%s\n", debugstr_w(name));
4386 * To initialize some final structure members, we call some helper
4387 * functions. However, since the EDITSTATE is not consistent (i.e.
4388 * not fully initialized), we should be very careful which
4389 * functions can be called, and in what order.
4391 EDIT_WM_SetFont(es, 0, FALSE);
4392 EDIT_EM_EmptyUndoBuffer(es);
4394 /* We need to calculate the format rect
4395 (applications may send EM_SETMARGINS before the control gets visible) */
4396 GetClientRect(es->hwndSelf, &clientRect);
4397 EDIT_SetRectNP(es, &clientRect);
4399 if (name && *name)
4401 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4402 /* if we insert text to the editline, the text scrolls out
4403 * of the window, as the caret is placed after the insert
4404 * pos normally; thus we reset es->selection... to 0 and
4405 * update caret
4407 es->selection_start = es->selection_end = 0;
4408 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4409 * Messages are only to be sent when the USER does something to
4410 * change the contents. So I am removing this EN_CHANGE
4412 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4414 EDIT_EM_ScrollCaret(es);
4417 /* force scroll info update */
4418 EDIT_UpdateScrollInfo(es);
4419 OpenThemeData(es->hwndSelf, WC_EDITW);
4421 /* The rule seems to return 1 here for success */
4422 /* Power Builder masked edit controls will crash */
4423 /* if not. */
4424 /* FIXME: is that in all cases so ? */
4425 return 1;
4429 /*********************************************************************
4431 * WM_NCDESTROY
4434 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4436 LINEDEF *pc, *pp;
4437 HTHEME theme;
4439 theme = GetWindowTheme(es->hwndSelf);
4440 CloseThemeData(theme);
4442 /* The app can own the text buffer handle */
4443 if (es->hloc32W && (es->hloc32W != es->hlocapp))
4444 LocalFree(es->hloc32W);
4446 EDIT_InvalidateUniscribeData(es);
4448 pc = es->first_line_def;
4449 while (pc)
4451 pp = pc->next;
4452 Free(pc);
4453 pc = pp;
4456 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4457 Free(es->undo_text);
4458 Free(es->cue_banner_text);
4459 Free(es);
4461 return 0;
4464 static LRESULT CALLBACK EDIT_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
4466 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW(hwnd, 0);
4467 LRESULT result = 0;
4468 RECT *rect;
4469 POINT pt;
4471 TRACE("hwnd %p, msg %#x, wparam %Ix, lparam %Ix\n", hwnd, msg, wParam, lParam);
4473 if (!es && msg != WM_NCCREATE)
4474 return DefWindowProcW(hwnd, msg, wParam, lParam);
4476 if (es && (msg != WM_NCDESTROY))
4477 EDIT_LockBuffer(es);
4479 switch (msg)
4481 case EM_GETSEL:
4482 result = EDIT_EM_GetSel(es, (UINT *)wParam, (UINT *)lParam);
4483 break;
4485 case EM_SETSEL:
4486 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4487 EDIT_EM_ScrollCaret(es);
4488 result = 1;
4489 break;
4491 case EM_GETRECT:
4492 rect = (RECT *)lParam;
4493 if (rect)
4494 *rect = es->format_rect;
4495 break;
4497 case EM_SETRECT:
4498 if ((es->style & ES_MULTILINE) && lParam)
4500 EDIT_SetRectNP(es, (RECT *)lParam);
4501 EDIT_UpdateText(es, NULL, TRUE);
4503 break;
4505 case EM_SETRECTNP:
4506 if ((es->style & ES_MULTILINE) && lParam)
4507 EDIT_SetRectNP(es, (LPRECT)lParam);
4508 break;
4510 case EM_SCROLL:
4511 result = EDIT_EM_Scroll(es, (INT)wParam);
4512 break;
4514 case EM_LINESCROLL:
4515 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4516 break;
4518 case EM_SCROLLCARET:
4519 EDIT_EM_ScrollCaret(es);
4520 result = 1;
4521 break;
4523 case EM_GETMODIFY:
4524 result = ((es->flags & EF_MODIFIED) != 0);
4525 break;
4527 case EM_SETMODIFY:
4528 if (wParam)
4529 es->flags |= EF_MODIFIED;
4530 else
4531 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4532 break;
4534 case EM_GETLINECOUNT:
4535 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4536 break;
4538 case EM_LINEINDEX:
4539 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4540 break;
4542 case EM_SETHANDLE:
4543 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4544 break;
4546 case EM_GETHANDLE:
4547 result = (LRESULT)EDIT_EM_GetHandle(es);
4548 break;
4550 case EM_GETTHUMB:
4551 result = EDIT_EM_GetThumb(es);
4552 break;
4554 /* these messages missing from specs */
4555 case 0x00bf:
4556 case 0x00c0:
4557 case 0x00c3:
4558 case 0x00ca:
4559 FIXME("undocumented message 0x%x, please report\n", msg);
4560 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4561 break;
4563 case EM_LINELENGTH:
4564 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4565 break;
4567 case EM_REPLACESEL:
4569 const WCHAR *textW = (const WCHAR *)lParam;
4571 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4572 result = 1;
4573 break;
4576 case EM_GETLINE:
4577 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
4578 break;
4580 case EM_SETLIMITTEXT:
4581 EDIT_EM_SetLimitText(es, wParam);
4582 break;
4584 case EM_CANUNDO:
4585 result = (LRESULT)EDIT_EM_CanUndo(es);
4586 break;
4588 case EM_UNDO:
4589 case WM_UNDO:
4590 result = (LRESULT)EDIT_EM_Undo(es);
4591 break;
4593 case EM_FMTLINES:
4594 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4595 break;
4597 case EM_LINEFROMCHAR:
4598 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4599 break;
4601 case EM_SETTABSTOPS:
4602 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4603 break;
4605 case EM_SETPASSWORDCHAR:
4606 EDIT_EM_SetPasswordChar(es, wParam);
4607 break;
4609 case EM_EMPTYUNDOBUFFER:
4610 EDIT_EM_EmptyUndoBuffer(es);
4611 break;
4613 case EM_GETFIRSTVISIBLELINE:
4614 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4615 break;
4617 case EM_SETREADONLY:
4619 DWORD old_style = es->style;
4621 if (wParam)
4623 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) | ES_READONLY);
4624 es->style |= ES_READONLY;
4626 else
4628 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & ~ES_READONLY);
4629 es->style &= ~ES_READONLY;
4632 if (old_style ^ es->style)
4633 InvalidateRect(es->hwndSelf, NULL, TRUE);
4635 result = 1;
4636 break;
4639 case EM_SETWORDBREAKPROC:
4640 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4641 result = 1;
4642 break;
4644 case EM_GETWORDBREAKPROC:
4645 result = (LRESULT)es->word_break_proc;
4646 break;
4648 case EM_GETPASSWORDCHAR:
4649 result = es->password_char;
4650 break;
4652 case EM_SETMARGINS:
4653 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4654 break;
4656 case EM_GETMARGINS:
4657 result = MAKELONG(es->left_margin, es->right_margin);
4658 break;
4660 case EM_GETLIMITTEXT:
4661 result = es->buffer_limit;
4662 break;
4664 case EM_POSFROMCHAR:
4665 if ((INT)wParam >= get_text_length(es)) result = -1;
4666 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4667 break;
4669 case EM_CHARFROMPOS:
4670 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4671 break;
4673 case EM_SETCUEBANNER:
4674 result = EDIT_EM_SetCueBanner(es, (BOOL)wParam, (const WCHAR *)lParam);
4675 break;
4677 case EM_GETCUEBANNER:
4678 result = EDIT_EM_GetCueBanner(es, (WCHAR *)wParam, (DWORD)lParam);
4679 break;
4681 case EM_SETIMESTATUS:
4682 if (wParam == EMSIS_COMPOSITIONSTRING)
4683 es->ime_status = lParam & 0xFFFF;
4685 result = 1;
4686 break;
4688 case EM_GETIMESTATUS:
4689 result = wParam == EMSIS_COMPOSITIONSTRING ? es->ime_status : 1;
4690 break;
4692 /* End of the EM_ messages which were in numerical order; what order
4693 * are these in? vaguely alphabetical?
4696 case WM_NCCREATE:
4697 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
4698 break;
4700 case WM_NCDESTROY:
4701 result = EDIT_WM_NCDestroy(es);
4702 es = NULL;
4703 break;
4705 case WM_GETDLGCODE:
4706 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4708 if (es->style & ES_MULTILINE)
4709 result |= DLGC_WANTALLKEYS;
4711 if (lParam)
4713 MSG *msg = (MSG *)lParam;
4714 es->flags |= EF_DIALOGMODE;
4716 if (msg->message == WM_KEYDOWN)
4718 int vk = (int)msg->wParam;
4720 if (es->hwndListBox)
4722 if (vk == VK_RETURN || vk == VK_ESCAPE)
4723 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4724 result |= DLGC_WANTMESSAGE;
4728 break;
4730 case WM_CHAR:
4732 WCHAR charW = wParam;
4734 if (es->hwndListBox)
4736 if (charW == VK_RETURN || charW == VK_ESCAPE)
4738 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4739 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4740 break;
4743 result = EDIT_WM_Char(es, charW);
4744 break;
4747 case WM_UNICHAR:
4748 if (wParam == UNICODE_NOCHAR) return TRUE;
4749 if (wParam <= 0x000fffff)
4751 if (wParam > 0xffff) /* convert to surrogates */
4753 wParam -= 0x10000;
4754 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4755 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4757 else
4758 EDIT_WM_Char(es, wParam);
4760 return 0;
4762 case WM_CLEAR:
4763 EDIT_WM_Clear(es);
4764 break;
4766 case WM_CONTEXTMENU:
4767 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4768 break;
4770 case WM_COPY:
4771 EDIT_WM_Copy(es);
4772 break;
4774 case WM_CREATE:
4775 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4776 break;
4778 case WM_CUT:
4779 EDIT_WM_Cut(es);
4780 break;
4782 case WM_ENABLE:
4783 es->bEnableState = (BOOL) wParam;
4784 EDIT_UpdateText(es, NULL, TRUE);
4785 if (GetWindowTheme(hwnd))
4786 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
4787 break;
4789 case WM_ERASEBKGND:
4790 /* we do the proper erase in EDIT_WM_Paint */
4791 result = 1;
4792 break;
4794 case WM_GETFONT:
4795 result = (LRESULT)es->font;
4796 break;
4798 case WM_GETTEXT:
4799 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam);
4800 break;
4802 case WM_GETTEXTLENGTH:
4803 result = get_text_length(es);
4804 break;
4806 case WM_HSCROLL:
4807 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4808 break;
4810 case WM_KEYDOWN:
4811 result = EDIT_WM_KeyDown(es, (INT)wParam);
4812 break;
4814 case WM_KILLFOCUS:
4815 result = EDIT_WM_KillFocus(GetWindowTheme(hwnd), es);
4816 break;
4818 case WM_LBUTTONDBLCLK:
4819 result = EDIT_WM_LButtonDblClk(es);
4820 break;
4822 case WM_LBUTTONDOWN:
4823 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4824 break;
4826 case WM_LBUTTONUP:
4827 result = EDIT_WM_LButtonUp(es);
4828 break;
4830 case WM_MBUTTONDOWN:
4831 result = EDIT_WM_MButtonDown(es);
4832 break;
4834 case WM_MOUSEMOVE:
4835 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4836 break;
4838 case WM_PRINTCLIENT:
4839 case WM_PAINT:
4840 EDIT_WM_Paint(es, (HDC)wParam);
4841 break;
4843 case WM_NCPAINT:
4844 EDIT_WM_NCPaint(hwnd, (HRGN)wParam);
4845 break;
4847 case WM_PASTE:
4848 EDIT_WM_Paste(es);
4849 break;
4851 case WM_SETFOCUS:
4852 EDIT_WM_SetFocus(GetWindowTheme(hwnd), es);
4853 break;
4855 case WM_SETFONT:
4856 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4857 break;
4859 case WM_SETREDRAW:
4860 /* FIXME: actually set an internal flag and behave accordingly */
4861 break;
4863 case WM_SETTEXT:
4864 EDIT_WM_SetText(es, (const WCHAR *)lParam);
4865 result = TRUE;
4866 break;
4868 case WM_SIZE:
4869 EDIT_WM_Size(es, (UINT)wParam);
4870 break;
4872 case WM_STYLECHANGED:
4873 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4874 break;
4876 case WM_STYLECHANGING:
4877 result = 0; /* See EDIT_WM_StyleChanged */
4878 break;
4880 case WM_SYSKEYDOWN:
4881 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4882 break;
4884 case WM_VSCROLL:
4885 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4886 break;
4888 case WM_MOUSEWHEEL:
4890 int wheelDelta;
4891 INT pulScrollLines = 3;
4892 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4894 if (wParam & (MK_SHIFT | MK_CONTROL))
4896 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4897 break;
4900 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4901 /* if scrolling changes direction, ignore left overs */
4902 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
4903 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
4904 es->wheelDeltaRemainder += wheelDelta;
4905 else
4906 es->wheelDeltaRemainder = wheelDelta;
4908 if (es->wheelDeltaRemainder && pulScrollLines)
4910 int cLineScroll;
4911 pulScrollLines = min(es->line_count, pulScrollLines);
4912 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
4913 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
4914 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
4916 break;
4919 /* IME messages to make the edit control IME aware */
4920 case WM_IME_SETCONTEXT:
4921 GetCaretPos(&pt);
4922 EDIT_UpdateImmCompositionWindow(es, pt.x, pt.y);
4923 EDIT_UpdateImmCompositionFont(es);
4924 break;
4926 case WM_IME_COMPOSITION:
4927 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4928 if ((lParam & GCS_RESULTSTR) && (es->ime_status & EIMES_GETCOMPSTRATONCE))
4929 EDIT_ImeComposition(hwnd, lParam, es);
4930 return DefWindowProcW(hwnd, msg, wParam, lParam);
4932 case WM_IME_SELECT:
4933 break;
4935 case WM_IME_CONTROL:
4936 break;
4938 case WM_IME_REQUEST:
4939 switch (wParam)
4941 case IMR_QUERYCHARPOSITION:
4943 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
4944 LRESULT pos;
4946 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
4947 chpos->pt.x = LOWORD(pos);
4948 chpos->pt.y = HIWORD(pos);
4949 chpos->cLineHeight = es->line_height;
4950 chpos->rcDocument = es->format_rect;
4951 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
4952 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
4953 result = 1;
4954 break;
4957 break;
4959 case WM_THEMECHANGED:
4960 CloseThemeData(GetWindowTheme(hwnd));
4961 OpenThemeData(hwnd, WC_EDITW);
4962 InvalidateRect(hwnd, NULL, TRUE);
4963 break;
4965 default:
4966 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4967 break;
4970 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
4971 EDIT_UnlockBuffer(es, FALSE);
4973 TRACE("hwnd=%p msg=%x -- %#Ix\n", hwnd, msg, result);
4975 return result;
4978 void EDIT_Register(void)
4980 WNDCLASSW wndClass;
4982 memset(&wndClass, 0, sizeof(wndClass));
4983 wndClass.style = CS_PARENTDC | CS_GLOBALCLASS | CS_DBLCLKS;
4984 wndClass.lpfnWndProc = EDIT_WindowProc;
4985 wndClass.cbClsExtra = 0;
4986 #ifdef __i386__
4987 wndClass.cbWndExtra = sizeof(EDITSTATE *) + sizeof(WORD);
4988 #else
4989 wndClass.cbWndExtra = sizeof(EDITSTATE *);
4990 #endif
4991 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_IBEAM);
4992 wndClass.hbrBackground = NULL;
4993 wndClass.lpszClassName = WC_EDITW;
4994 RegisterClassW(&wndClass);