dmime: Remove DECLSPEC_HIDDEN usage.
[wine.git] / dlls / comctl32 / edit.c
blob24ef8a43c17248e61cfc85609a640e8efb611df3
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 INT region_posx; /* Position of cursor relative to region: */
117 INT region_posy; /* -1: to left, 0: within, 1: to right */
118 EDITWORDBREAKPROCW word_break_proc;
119 INT line_count; /* number of lines */
120 INT y_offset; /* scroll offset in number of lines */
121 BOOL bCaptureState; /* flag indicating whether mouse was captured */
122 BOOL bEnableState; /* flag keeping the enable state */
123 HWND hwndSelf; /* the our window handle */
124 HWND hwndParent; /* Handle of parent for sending EN_* messages.
125 Even if parent will change, EN_* messages
126 should be sent to the first parent. */
127 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
128 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
129 WCHAR *cue_banner_text;
130 BOOL cue_banner_draw_focused;
133 * only for multi line controls
135 INT lock_count; /* amount of re-entries in the EditWndProc */
136 INT tabs_count;
137 LPINT tabs;
138 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
139 HLOCAL hloc32W; /* our unicode local memory block */
140 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
142 * IME Data
144 UINT ime_status; /* IME status flag */
147 * Uniscribe Data
149 SCRIPT_LOGATTR *logAttr;
150 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
151 } EDITSTATE;
154 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
155 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
157 static inline BOOL notify_parent(const EDITSTATE *es, INT code)
159 HWND hwnd = es->hwndSelf;
160 TRACE("notification %d sent to %p.\n", code, es->hwndParent);
161 SendMessageW(es->hwndParent, WM_COMMAND, MAKEWPARAM(GetWindowLongPtrW(es->hwndSelf, GWLP_ID), code), (LPARAM)es->hwndSelf);
162 return IsWindow(hwnd);
165 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
167 /*********************************************************************
169 * EM_CANUNDO
172 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
174 return (es->undo_insert_count || lstrlenW(es->undo_text));
178 /*********************************************************************
180 * EM_EMPTYUNDOBUFFER
183 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
185 es->undo_insert_count = 0;
186 *es->undo_text = '\0';
189 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
191 HBRUSH hbrush;
192 UINT msg;
194 if ((!es->bEnableState || (es->style & ES_READONLY)))
195 msg = WM_CTLCOLORSTATIC;
196 else
197 msg = WM_CTLCOLOREDIT;
199 /* Why do we notify to es->hwndParent, and we send this one to GetParent()? */
200 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
201 if (!hbrush)
202 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
203 return hbrush;
207 static inline UINT get_text_length(EDITSTATE *es)
209 if(es->text_length == (UINT)-1)
210 es->text_length = lstrlenW(es->text);
211 return es->text_length;
215 /*********************************************************************
217 * EDIT_WordBreakProc
219 * Find the beginning of words.
220 * Note: unlike the specs for a WordBreakProc, this function can
221 * only be called without linebreaks between s[0] up to
222 * s[count - 1]. Remember it is only called
223 * internally, so we can decide this for ourselves.
224 * Additionally we will always be breaking the full string.
227 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
229 INT ret = 0;
231 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
233 if(!s) return 0;
235 if (!es->logAttr)
237 SCRIPT_ANALYSIS psa;
239 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
240 psa.eScript = SCRIPT_UNDEFINED;
242 es->logAttr = Alloc(sizeof(SCRIPT_LOGATTR) * get_text_length(es));
243 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
246 switch (action) {
247 case WB_LEFT:
248 if (index)
249 index--;
250 while (index && !es->logAttr[index].fSoftBreak)
251 index--;
252 ret = index;
253 break;
254 case WB_RIGHT:
255 if (!count)
256 break;
257 while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
258 index++;
259 ret = index;
260 break;
261 case WB_ISDELIMITER:
262 ret = es->logAttr[index].fWhiteSpace;
263 break;
264 default:
265 ERR("unknown action code, please report !\n");
266 break;
268 return ret;
272 /*********************************************************************
274 * EDIT_CallWordBreakProc
276 * Call appropriate WordBreakProc (internal or external).
278 * Note: The "start" argument should always be an index referring
279 * to es->text. The actual wordbreak proc might be
280 * 16 bit, so we can't always pass any 32 bit LPSTR.
281 * Hence we assume that es->text is the buffer that holds
282 * the string under examination (we can decide this for ourselves).
285 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
287 INT ret;
289 if (es->word_break_proc)
290 ret = es->word_break_proc(es->text + start, index, count, action);
291 else
292 ret = EDIT_WordBreakProc(es, es->text, index + start, count + start, action) - start;
294 return ret;
297 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
299 if (line_def->ssa)
301 ScriptStringFree(&line_def->ssa);
302 line_def->ssa = NULL;
306 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
308 LINEDEF *line_def = es->first_line_def;
309 while (line_def)
311 EDIT_InvalidateUniscribeData_linedef(line_def);
312 line_def = line_def->next;
314 if (es->ssa)
316 ScriptStringFree(&es->ssa);
317 es->ssa = NULL;
321 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
323 if (!line_def)
324 return NULL;
326 if (line_def->net_length && !line_def->ssa)
328 int index = line_def->index;
329 HFONT old_font = NULL;
330 HDC udc = dc;
331 SCRIPT_TABDEF tabdef;
332 HRESULT hr;
334 if (!udc)
335 udc = GetDC(es->hwndSelf);
336 if (es->font)
337 old_font = SelectObject(udc, es->font);
339 tabdef.cTabStops = es->tabs_count;
340 tabdef.iScale = GdiGetCharDimensions(udc, NULL, NULL);
341 tabdef.pTabStops = es->tabs;
342 tabdef.iTabOrigin = 0;
344 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
345 (1.5*line_def->net_length+16), -1,
346 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
347 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
348 if (FAILED(hr))
350 WARN("ScriptStringAnalyse failed, hr %#lx.\n", hr);
351 line_def->ssa = NULL;
354 if (es->font)
355 SelectObject(udc, old_font);
356 if (udc != dc)
357 ReleaseDC(es->hwndSelf, udc);
360 return line_def->ssa;
363 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
365 LINEDEF *line_def;
367 if (!(es->style & ES_MULTILINE))
369 if (!es->ssa)
371 INT length = get_text_length(es);
372 HFONT old_font = NULL;
373 HDC udc = dc;
375 if (!udc)
376 udc = GetDC(es->hwndSelf);
377 if (es->font)
378 old_font = SelectObject(udc, es->font);
380 if (es->style & ES_PASSWORD)
381 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);
382 else
383 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
385 if (es->font)
386 SelectObject(udc, old_font);
387 if (udc != dc)
388 ReleaseDC(es->hwndSelf, udc);
390 return es->ssa;
392 else
394 line_def = es->first_line_def;
395 while (line_def && line)
397 line_def = line_def->next;
398 line--;
401 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
405 static inline INT get_vertical_line_count(EDITSTATE *es)
407 INT vlc = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
408 return max(1,vlc);
411 /*********************************************************************
413 * EDIT_BuildLineDefs_ML
415 * Build linked list of text lines.
416 * Lines can end with '\0' (last line), a character (if it is wrapped),
417 * a soft return '\r\r\n' or a hard return '\r\n'
420 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
422 LPWSTR current_position, cp;
423 INT fw;
424 LINEDEF *current_line;
425 LINEDEF *previous_line;
426 LINEDEF *start_line;
427 INT line_index = 0, nstart_line, nstart_index;
428 INT line_count = es->line_count;
429 INT orig_net_length;
430 RECT rc;
431 INT vlc;
433 if (istart == iend && delta == 0)
434 return;
436 previous_line = NULL;
437 current_line = es->first_line_def;
439 /* Find starting line. istart must lie inside an existing line or
440 * at the end of buffer */
441 do {
442 if (istart < current_line->index + current_line->length ||
443 current_line->ending == END_0)
444 break;
446 previous_line = current_line;
447 current_line = current_line->next;
448 line_index++;
449 } while (current_line);
451 if (!current_line) /* Error occurred start is not inside previous buffer */
453 FIXME(" modification occurred outside buffer\n");
454 return;
457 /* Remember start of modifications in order to calculate update region */
458 nstart_line = line_index;
459 nstart_index = current_line->index;
461 /* We must start to reformat from the previous line since the modifications
462 * may have caused the line to wrap upwards. */
463 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
465 line_index--;
466 current_line = previous_line;
468 start_line = current_line;
470 fw = es->format_rect.right - es->format_rect.left;
471 current_position = es->text + current_line->index;
472 vlc = get_vertical_line_count(es);
473 do {
474 if (current_line != start_line)
476 if (!current_line || current_line->index + delta > current_position - es->text)
478 /* The buffer has been expanded, create a new line and
479 insert it into the link list */
480 LINEDEF *new_line = Alloc(sizeof(*new_line));
481 new_line->next = previous_line->next;
482 previous_line->next = new_line;
483 current_line = new_line;
484 es->line_count++;
486 else if (current_line->index + delta < current_position - es->text)
488 /* The previous line merged with this line so we delete this extra entry */
489 previous_line->next = current_line->next;
490 Free(current_line);
491 current_line = previous_line->next;
492 es->line_count--;
493 continue;
495 else /* current_line->index + delta == current_position */
497 if (current_position - es->text > iend)
498 break; /* We reached end of line modifications */
499 /* else recalculate this line */
503 current_line->index = current_position - es->text;
504 orig_net_length = current_line->net_length;
506 /* Find end of line */
507 cp = current_position;
508 while (*cp) {
509 if (*cp == '\n') break;
510 if ((*cp == '\r') && (*(cp + 1) == '\n'))
511 break;
512 cp++;
515 /* Mark type of line termination */
516 if (!(*cp)) {
517 current_line->ending = END_0;
518 current_line->net_length = lstrlenW(current_position);
519 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
520 current_line->ending = END_SOFT;
521 current_line->net_length = cp - current_position - 1;
522 } else if (*cp == '\n') {
523 current_line->ending = END_RICH;
524 current_line->net_length = cp - current_position;
525 } else {
526 current_line->ending = END_HARD;
527 current_line->net_length = cp - current_position;
530 if (current_line->net_length)
532 const SIZE *sz;
533 EDIT_InvalidateUniscribeData_linedef(current_line);
534 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
535 if (current_line->ssa)
537 sz = ScriptString_pSize(current_line->ssa);
538 /* Calculate line width */
539 current_line->width = sz->cx;
541 else current_line->width = es->char_width * current_line->net_length;
543 else current_line->width = 0;
545 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
547 /* Line breaks just look back from the end and find the next break and try that. */
549 if (!(es->style & ES_AUTOHSCROLL)) {
550 if (current_line->width > fw && fw > es->char_width) {
552 INT prev, next;
553 int w;
554 const SIZE *sz;
555 float d;
557 prev = current_line->net_length - 1;
558 w = current_line->net_length;
559 d = (float)current_line->width/(float)fw;
560 if (d > 1.2f) d -= 0.2f;
561 next = prev/d;
562 if (next >= prev) next = prev-1;
563 do {
564 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
565 next, current_line->net_length, WB_LEFT);
566 current_line->net_length = prev;
567 EDIT_InvalidateUniscribeData_linedef(current_line);
568 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
569 if (current_line->ssa)
570 sz = ScriptString_pSize(current_line->ssa);
571 else sz = 0;
572 if (sz)
573 current_line->width = sz->cx;
574 else
575 prev = 0;
576 next = prev - 1;
577 } while (prev && current_line->width > fw);
578 current_line->net_length = w;
580 if (prev == 0) { /* Didn't find a line break so force a break */
581 INT *piDx;
582 const INT *count;
584 EDIT_InvalidateUniscribeData_linedef(current_line);
585 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
587 if (current_line->ssa)
589 count = ScriptString_pcOutChars(current_line->ssa);
590 piDx = Alloc(sizeof(INT) * (*count));
591 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
593 prev = current_line->net_length-1;
594 do {
595 current_line->width -= piDx[prev];
596 prev--;
597 } while ( prev > 0 && current_line->width > fw);
598 if (prev<=0)
599 prev = 1;
600 Free(piDx);
602 else
603 prev = (fw / es->char_width);
606 /* If the first line we are calculating, wrapped before istart, we must
607 * adjust istart in order for this to be reflected in the update region. */
608 if (current_line->index == nstart_index && istart > current_line->index + prev)
609 istart = current_line->index + prev;
610 /* else if we are updating the previous line before the first line we
611 * are re-calculating and it expanded */
612 else if (current_line == start_line &&
613 current_line->index != nstart_index && orig_net_length < prev)
615 /* Line expanded due to an upwards line wrap so we must partially include
616 * previous line in update region */
617 nstart_line = line_index;
618 nstart_index = current_line->index;
619 istart = current_line->index + orig_net_length;
622 current_line->net_length = prev;
623 current_line->ending = END_WRAP;
625 if (current_line->net_length > 0)
627 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
628 if (current_line->ssa)
630 sz = ScriptString_pSize(current_line->ssa);
631 current_line->width = sz->cx;
633 else
634 current_line->width = 0;
636 else current_line->width = 0;
638 else if (current_line == start_line &&
639 current_line->index != nstart_index &&
640 orig_net_length < current_line->net_length) {
641 /* The previous line expanded but it's still not as wide as the client rect */
642 /* The expansion is due to an upwards line wrap so we must partially include
643 it in the update region */
644 nstart_line = line_index;
645 nstart_index = current_line->index;
646 istart = current_line->index + orig_net_length;
651 /* Adjust length to include line termination */
652 switch (current_line->ending) {
653 case END_SOFT:
654 current_line->length = current_line->net_length + 3;
655 break;
656 case END_RICH:
657 current_line->length = current_line->net_length + 1;
658 break;
659 case END_HARD:
660 current_line->length = current_line->net_length + 2;
661 break;
662 case END_WRAP:
663 case END_0:
664 current_line->length = current_line->net_length;
665 break;
667 es->text_width = max(es->text_width, current_line->width);
668 current_position += current_line->length;
669 previous_line = current_line;
671 /* Discard data for non-visible lines. It will be calculated as needed */
672 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
673 EDIT_InvalidateUniscribeData_linedef(current_line);
675 current_line = current_line->next;
676 line_index++;
677 } while (previous_line->ending != END_0);
679 /* Finish adjusting line indexes by delta or remove hanging lines */
680 if (previous_line->ending == END_0)
682 LINEDEF *pnext = NULL;
684 previous_line->next = NULL;
685 while (current_line)
687 pnext = current_line->next;
688 EDIT_InvalidateUniscribeData_linedef(current_line);
689 Free(current_line);
690 current_line = pnext;
691 es->line_count--;
694 else if (delta != 0)
696 while (current_line)
698 current_line->index += delta;
699 current_line = current_line->next;
703 /* Calculate rest of modification rectangle */
704 if (hrgn)
706 HRGN tmphrgn;
708 * We calculate two rectangles. One for the first line which may have
709 * an indent with respect to the format rect. The other is a format-width
710 * rectangle that spans the rest of the lines that changed or moved.
712 rc.top = es->format_rect.top + nstart_line * es->line_height -
713 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
714 rc.bottom = rc.top + es->line_height;
715 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
716 rc.left = es->format_rect.left;
717 else
718 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
719 rc.right = es->format_rect.right;
720 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
722 rc.top = rc.bottom;
723 rc.left = es->format_rect.left;
724 rc.right = es->format_rect.right;
726 * If lines were added or removed we must re-paint the remainder of the
727 * lines since the remaining lines were either shifted up or down.
729 if (line_count < es->line_count) /* We added lines */
730 rc.bottom = es->line_count * es->line_height;
731 else if (line_count > es->line_count) /* We removed lines */
732 rc.bottom = line_count * es->line_height;
733 else
734 rc.bottom = line_index * es->line_height;
735 rc.bottom += es->format_rect.top;
736 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
737 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
738 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
739 DeleteObject(tmphrgn);
743 /*********************************************************************
745 * EDIT_CalcLineWidth_SL
748 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
750 EDIT_UpdateUniscribeData(es, NULL, 0);
751 if (es->ssa)
753 const SIZE *size;
754 size = ScriptString_pSize(es->ssa);
755 es->text_width = size->cx;
757 else
758 es->text_width = 0;
761 /*********************************************************************
763 * EDIT_CharFromPos
765 * Beware: This is not the function called on EM_CHARFROMPOS
766 * The position _can_ be outside the formatting / client
767 * rectangle
768 * The return value is only the character index
771 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
773 INT index;
775 if (es->style & ES_MULTILINE) {
776 int trailing;
777 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
778 INT line_index = 0;
779 LINEDEF *line_def = es->first_line_def;
780 EDIT_UpdateUniscribeData(es, NULL, line);
781 while ((line > 0) && line_def->next) {
782 line_index += line_def->length;
783 line_def = line_def->next;
784 line--;
787 x += es->x_offset - es->format_rect.left;
788 if (es->style & ES_RIGHT)
789 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
790 else if (es->style & ES_CENTER)
791 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
792 if (x >= line_def->width) {
793 if (after_wrap)
794 *after_wrap = (line_def->ending == END_WRAP);
795 return line_index + line_def->net_length;
797 if (x <= 0 || !line_def->ssa) {
798 if (after_wrap)
799 *after_wrap = FALSE;
800 return line_index;
803 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
804 if (trailing) index++;
805 index += line_index;
806 if (after_wrap)
807 *after_wrap = ((index == line_index + line_def->net_length) &&
808 (line_def->ending == END_WRAP));
809 } else {
810 INT xoff = 0;
811 INT trailing;
812 if (after_wrap)
813 *after_wrap = FALSE;
814 x -= es->format_rect.left;
815 if (!x)
816 return es->x_offset;
818 if (!es->x_offset)
820 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
821 if (es->style & ES_RIGHT)
822 x -= indent;
823 else if (es->style & ES_CENTER)
824 x -= indent / 2;
827 EDIT_UpdateUniscribeData(es, NULL, 0);
828 if (es->x_offset)
830 if (es->ssa)
832 if (es->x_offset>= get_text_length(es))
834 const SIZE *size;
835 size = ScriptString_pSize(es->ssa);
836 xoff = size->cx;
838 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
840 else
841 xoff = 0;
843 if (x < 0)
845 if (x + xoff > 0 || !es->ssa)
847 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
848 if (trailing) index++;
850 else
851 index = 0;
853 else
855 if (x)
857 const SIZE *size = NULL;
858 if (es->ssa)
859 size = ScriptString_pSize(es->ssa);
860 if (!size)
861 index = 0;
862 else if (x > size->cx)
863 index = get_text_length(es);
864 else if (es->ssa)
866 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
867 if (trailing) index++;
869 else
870 index = 0;
872 else
873 index = es->x_offset;
876 return index;
880 /*********************************************************************
882 * EDIT_ConfinePoint
884 * adjusts the point to be within the formatting rectangle
885 * (so CharFromPos returns the nearest _visible_ character)
888 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
890 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
891 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
895 /*********************************************************************
897 * EM_LINEFROMCHAR
900 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
902 INT line;
903 LINEDEF *line_def;
905 if (!(es->style & ES_MULTILINE))
906 return 0;
907 if (index > (INT)get_text_length(es))
908 return es->line_count - 1;
909 if (index == -1)
910 index = min(es->selection_start, es->selection_end);
912 line = 0;
913 line_def = es->first_line_def;
914 index -= line_def->length;
915 while ((index >= 0) && line_def->next) {
916 line++;
917 line_def = line_def->next;
918 index -= line_def->length;
920 return line;
924 /*********************************************************************
926 * EM_LINEINDEX
929 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
931 INT line_index;
932 const LINEDEF *line_def;
934 if (!(es->style & ES_MULTILINE))
935 return 0;
936 if (line >= es->line_count)
937 return -1;
939 line_index = 0;
940 line_def = es->first_line_def;
941 if (line == -1) {
942 INT index = es->selection_end - line_def->length;
943 while ((index >= 0) && line_def->next) {
944 line_index += line_def->length;
945 line_def = line_def->next;
946 index -= line_def->length;
948 } else {
949 while (line > 0) {
950 line_index += line_def->length;
951 line_def = line_def->next;
952 line--;
955 return line_index;
959 /*********************************************************************
961 * EM_LINELENGTH
964 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
966 LINEDEF *line_def;
968 if (!(es->style & ES_MULTILINE))
969 return get_text_length(es);
971 if (index == -1) {
972 /* get the number of remaining non-selected chars of selected lines */
973 INT32 l; /* line number */
974 INT32 li; /* index of first char in line */
975 INT32 count;
976 l = EDIT_EM_LineFromChar(es, es->selection_start);
977 /* # chars before start of selection area */
978 count = es->selection_start - EDIT_EM_LineIndex(es, l);
979 l = EDIT_EM_LineFromChar(es, es->selection_end);
980 /* # chars after end of selection */
981 li = EDIT_EM_LineIndex(es, l);
982 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
983 return count;
985 line_def = es->first_line_def;
986 index -= line_def->length;
987 while ((index >= 0) && line_def->next) {
988 line_def = line_def->next;
989 index -= line_def->length;
991 return line_def->net_length;
995 /*********************************************************************
997 * EM_POSFROMCHAR
1000 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1002 INT len = get_text_length(es);
1003 INT l;
1004 INT li;
1005 INT x = 0;
1006 INT y = 0;
1007 INT w;
1008 INT lw;
1009 LINEDEF *line_def;
1011 index = min(index, len);
1012 if (es->style & ES_MULTILINE) {
1013 l = EDIT_EM_LineFromChar(es, index);
1014 EDIT_UpdateUniscribeData(es, NULL, l);
1016 y = (l - es->y_offset) * es->line_height;
1017 li = EDIT_EM_LineIndex(es, l);
1018 if (after_wrap && (li == index) && l) {
1019 INT l2 = l - 1;
1020 line_def = es->first_line_def;
1021 while (l2) {
1022 line_def = line_def->next;
1023 l2--;
1025 if (line_def->ending == END_WRAP) {
1026 l--;
1027 y -= es->line_height;
1028 li = EDIT_EM_LineIndex(es, l);
1032 line_def = es->first_line_def;
1033 while (line_def->index != li)
1034 line_def = line_def->next;
1036 lw = line_def->width;
1037 w = es->format_rect.right - es->format_rect.left;
1038 if (line_def->ssa)
1039 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1040 x -= es->x_offset;
1042 if (es->style & ES_RIGHT)
1043 x = w - (lw - x);
1044 else if (es->style & ES_CENTER)
1045 x += (w - lw) / 2;
1046 } else {
1047 INT xoff = 0;
1048 INT xi = 0;
1049 EDIT_UpdateUniscribeData(es, NULL, 0);
1050 if (es->x_offset)
1052 if (es->ssa)
1054 if (es->x_offset >= get_text_length(es))
1056 int leftover = es->x_offset - get_text_length(es);
1057 if (es->ssa)
1059 const SIZE *size;
1060 size = ScriptString_pSize(es->ssa);
1061 xoff = size->cx;
1063 else
1064 xoff = 0;
1065 xoff += es->char_width * leftover;
1067 else
1068 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1070 else
1071 xoff = 0;
1073 if (index)
1075 if (index >= get_text_length(es))
1077 if (es->ssa)
1079 const SIZE *size;
1080 size = ScriptString_pSize(es->ssa);
1081 xi = size->cx;
1083 else
1084 xi = 0;
1086 else if (es->ssa)
1087 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1088 else
1089 xi = 0;
1091 x = xi - xoff;
1093 if (index >= es->x_offset) {
1094 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1096 w = es->format_rect.right - es->format_rect.left;
1097 if (w > es->text_width)
1099 if (es->style & ES_RIGHT)
1100 x += w - es->text_width;
1101 else if (es->style & ES_CENTER)
1102 x += (w - es->text_width) / 2;
1106 y = 0;
1108 x += es->format_rect.left;
1109 y += es->format_rect.top;
1110 return MAKELONG((INT16)x, (INT16)y);
1114 /*********************************************************************
1116 * EDIT_GetLineRect
1118 * Calculates the bounding rectangle for a line from a starting
1119 * column to an ending column.
1122 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1124 SCRIPT_STRING_ANALYSIS ssa;
1125 INT line_index = 0;
1126 INT pt1, pt2, pt3;
1128 if (es->style & ES_MULTILINE)
1130 const LINEDEF *line_def = NULL;
1131 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1132 if (line >= es->line_count)
1133 return;
1135 line_def = es->first_line_def;
1136 if (line == -1) {
1137 INT index = es->selection_end - line_def->length;
1138 while ((index >= 0) && line_def->next) {
1139 line_index += line_def->length;
1140 line_def = line_def->next;
1141 index -= line_def->length;
1143 } else {
1144 while (line > 0) {
1145 line_index += line_def->length;
1146 line_def = line_def->next;
1147 line--;
1150 ssa = line_def->ssa;
1152 else
1154 line_index = 0;
1155 rc->top = es->format_rect.top;
1156 ssa = es->ssa;
1159 rc->bottom = rc->top + es->line_height;
1160 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1161 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1162 if (ssa)
1164 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1165 pt3+=es->format_rect.left;
1167 else pt3 = pt1;
1168 rc->right = max(max(pt1 , pt2),pt3);
1169 rc->left = min(min(pt1, pt2),pt3);
1173 static inline void text_buffer_changed(EDITSTATE *es)
1175 es->text_length = (UINT)-1;
1177 Free(es->logAttr);
1178 es->logAttr = NULL;
1179 EDIT_InvalidateUniscribeData(es);
1182 /*********************************************************************
1183 * EDIT_LockBuffer
1186 static void EDIT_LockBuffer(EDITSTATE *es)
1188 if (!es->text)
1190 if (!es->hloc32W)
1191 return;
1193 es->text = LocalLock(es->hloc32W);
1196 es->lock_count++;
1200 /*********************************************************************
1202 * EDIT_UnlockBuffer
1205 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1207 /* Edit window might be already destroyed */
1208 if (!IsWindow(es->hwndSelf))
1210 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1211 return;
1214 if (!es->lock_count)
1216 ERR("lock_count == 0 ... please report\n");
1217 return;
1220 if (!es->text)
1222 ERR("es->text == 0 ... please report\n");
1223 return;
1226 if (force || (es->lock_count == 1))
1228 if (es->hloc32W)
1230 LocalUnlock(es->hloc32W);
1231 es->text = NULL;
1233 else
1235 ERR("no buffer ... please report\n");
1236 return;
1241 es->lock_count--;
1245 /*********************************************************************
1247 * EDIT_MakeFit
1249 * Try to fit size + 1 characters in the buffer.
1251 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1253 HLOCAL hNew32W;
1255 if (size <= es->buffer_size)
1256 return TRUE;
1258 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1260 /* Force edit to unlock its buffer. es->text now NULL */
1261 EDIT_UnlockBuffer(es, TRUE);
1263 if (es->hloc32W) {
1264 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1265 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1266 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1267 es->hloc32W = hNew32W;
1268 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1272 EDIT_LockBuffer(es);
1274 if (es->buffer_size < size) {
1275 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1276 notify_parent(es, EN_ERRSPACE);
1277 return FALSE;
1278 } else {
1279 TRACE("We now have %d+1\n", es->buffer_size);
1280 return TRUE;
1285 /*********************************************************************
1287 * EDIT_MakeUndoFit
1289 * Try to fit size + 1 bytes in the undo buffer.
1292 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1294 UINT alloc_size;
1295 WCHAR *new_undo_text;
1297 if (size <= es->undo_buffer_size)
1298 return TRUE;
1300 TRACE("trying to ReAlloc to %d+1\n", size);
1302 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1303 if ((new_undo_text = ReAlloc(es->undo_text, alloc_size))) {
1304 memset(new_undo_text + es->undo_buffer_size, 0, alloc_size - es->undo_buffer_size * sizeof(WCHAR));
1305 es->undo_text = new_undo_text;
1306 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1307 return TRUE;
1309 else
1311 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1312 return FALSE;
1317 /*********************************************************************
1319 * EDIT_UpdateTextRegion
1322 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1324 if (es->flags & EF_UPDATE) {
1325 es->flags &= ~EF_UPDATE;
1326 if (!notify_parent(es, EN_UPDATE)) return;
1328 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1332 /*********************************************************************
1334 * EDIT_UpdateText
1337 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1339 if (es->flags & EF_UPDATE) {
1340 es->flags &= ~EF_UPDATE;
1341 if (!notify_parent(es, EN_UPDATE)) return;
1343 InvalidateRect(es->hwndSelf, rc, bErase);
1346 /*********************************************************************
1348 * EDIT_SL_InvalidateText
1350 * Called from EDIT_InvalidateText().
1351 * Does the job for single-line controls only.
1354 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1356 RECT line_rect;
1357 RECT rc;
1359 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1360 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1361 EDIT_UpdateText(es, &rc, TRUE);
1364 /*********************************************************************
1366 * EDIT_ML_InvalidateText
1368 * Called from EDIT_InvalidateText().
1369 * Does the job for multi-line controls only.
1372 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1374 INT vlc = get_vertical_line_count(es);
1375 INT sl = EDIT_EM_LineFromChar(es, start);
1376 INT el = EDIT_EM_LineFromChar(es, end);
1377 INT sc;
1378 INT ec;
1379 RECT rc1;
1380 RECT rcWnd;
1381 RECT rcLine;
1382 RECT rcUpdate;
1383 INT l;
1385 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1386 return;
1388 sc = start - EDIT_EM_LineIndex(es, sl);
1389 ec = end - EDIT_EM_LineIndex(es, el);
1390 if (sl < es->y_offset) {
1391 sl = es->y_offset;
1392 sc = 0;
1394 if (el > es->y_offset + vlc) {
1395 el = es->y_offset + vlc;
1396 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1398 GetClientRect(es->hwndSelf, &rc1);
1399 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1400 if (sl == el) {
1401 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1402 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1403 EDIT_UpdateText(es, &rcUpdate, TRUE);
1404 } else {
1405 EDIT_GetLineRect(es, sl, sc,
1406 EDIT_EM_LineLength(es,
1407 EDIT_EM_LineIndex(es, sl)),
1408 &rcLine);
1409 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1410 EDIT_UpdateText(es, &rcUpdate, TRUE);
1411 for (l = sl + 1 ; l < el ; l++) {
1412 EDIT_GetLineRect(es, l, 0,
1413 EDIT_EM_LineLength(es,
1414 EDIT_EM_LineIndex(es, l)),
1415 &rcLine);
1416 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1417 EDIT_UpdateText(es, &rcUpdate, TRUE);
1419 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1420 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1421 EDIT_UpdateText(es, &rcUpdate, TRUE);
1426 /*********************************************************************
1428 * EDIT_InvalidateText
1430 * Invalidate the text from offset start up to, but not including,
1431 * offset end. Useful for (re)painting the selection.
1432 * Regions outside the linewidth are not invalidated.
1433 * end == -1 means end == TextLength.
1434 * start and end need not be ordered.
1437 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1439 if (end == start)
1440 return;
1442 if (end == -1)
1443 end = get_text_length(es);
1445 if (end < start) {
1446 INT tmp = start;
1447 start = end;
1448 end = tmp;
1451 if (es->style & ES_MULTILINE)
1452 EDIT_ML_InvalidateText(es, start, end);
1453 else
1454 EDIT_SL_InvalidateText(es, start, end);
1458 /*********************************************************************
1460 * EDIT_EM_SetSel
1462 * note: unlike the specs say: the order of start and end
1463 * _is_ preserved in Windows. (i.e. start can be > end)
1464 * In other words: this handler is OK
1467 static BOOL EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1469 UINT old_start = es->selection_start;
1470 UINT old_end = es->selection_end;
1471 UINT len = get_text_length(es);
1473 if (start == old_start && end == old_end)
1474 return FALSE;
1476 if (start == (UINT)-1) {
1477 start = es->selection_end;
1478 end = es->selection_end;
1479 } else {
1480 start = min(start, len);
1481 end = min(end, len);
1483 es->selection_start = start;
1484 es->selection_end = end;
1485 if (after_wrap)
1486 es->flags |= EF_AFTER_WRAP;
1487 else
1488 es->flags &= ~EF_AFTER_WRAP;
1489 /* Compute the necessary invalidation region. */
1490 /* Note that we don't need to invalidate regions which have
1491 * "never" been selected, or those which are "still" selected.
1492 * In fact, every time we hit a selection boundary, we can
1493 * *toggle* whether we need to invalidate. Thus we can optimize by
1494 * *sorting* the interval endpoints. Let's assume that we sort them
1495 * in this order:
1496 * start <= end <= old_start <= old_end
1497 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1498 * in 5 comparisons; i.e. it is impossible to do better than the
1499 * following: */
1500 ORDER_UINT(end, old_end);
1501 ORDER_UINT(start, old_start);
1502 ORDER_UINT(old_start, old_end);
1503 ORDER_UINT(start, end);
1504 /* Note that at this point 'end' and 'old_start' are not in order, but
1505 * start is definitely the min. and old_end is definitely the max. */
1506 if (end != old_start)
1509 * One can also do
1510 * ORDER_UINT32(end, old_start);
1511 * EDIT_InvalidateText(es, start, end);
1512 * EDIT_InvalidateText(es, old_start, old_end);
1513 * in place of the following if statement.
1514 * (That would complete the optimal five-comparison four-element sort.)
1516 if (old_start > end )
1518 EDIT_InvalidateText(es, start, end);
1519 EDIT_InvalidateText(es, old_start, old_end);
1521 else
1523 EDIT_InvalidateText(es, start, old_start);
1524 EDIT_InvalidateText(es, end, old_end);
1527 else EDIT_InvalidateText(es, start, old_end);
1529 return TRUE;
1533 /*********************************************************************
1535 * EDIT_UpdateScrollInfo
1538 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1540 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1542 SCROLLINFO si;
1543 si.cbSize = sizeof(SCROLLINFO);
1544 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1545 si.nMin = 0;
1546 si.nMax = es->line_count - 1;
1547 si.nPage = es->line_height ? (es->format_rect.bottom - es->format_rect.top) / es->line_height : 0;
1548 si.nPos = es->y_offset;
1549 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1550 si.nMin, si.nMax, si.nPage, si.nPos);
1551 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1554 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1556 SCROLLINFO si;
1557 si.cbSize = sizeof(SCROLLINFO);
1558 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1559 si.nMin = 0;
1560 si.nMax = es->text_width - 1;
1561 si.nPage = es->format_rect.right - es->format_rect.left;
1562 si.nPos = es->x_offset;
1563 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1564 si.nMin, si.nMax, si.nPage, si.nPos);
1565 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1570 /*********************************************************************
1572 * EDIT_EM_LineScroll_internal
1574 * Version of EDIT_EM_LineScroll for internal use.
1575 * It doesn't refuse if ES_MULTILINE is set and assumes that
1576 * dx is in pixels, dy - in lines.
1579 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1581 INT nyoff;
1582 INT x_offset_in_pixels;
1583 INT lines_per_page;
1585 if (!es->line_height || !es->char_width)
1586 return TRUE;
1588 lines_per_page = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1590 if (es->style & ES_MULTILINE)
1592 x_offset_in_pixels = es->x_offset;
1594 else
1596 dy = 0;
1597 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1600 if (-dx > x_offset_in_pixels)
1601 dx = -x_offset_in_pixels;
1602 if (dx > es->text_width - x_offset_in_pixels)
1603 dx = es->text_width - x_offset_in_pixels;
1604 nyoff = max(0, es->y_offset + dy);
1605 if (nyoff >= es->line_count - lines_per_page)
1606 nyoff = max(0, es->line_count - lines_per_page);
1607 dy = (es->y_offset - nyoff) * es->line_height;
1608 if (dx || dy) {
1609 RECT rc1;
1610 RECT rc;
1612 es->y_offset = nyoff;
1613 if(es->style & ES_MULTILINE)
1614 es->x_offset += dx;
1615 else
1616 es->x_offset += dx / es->char_width;
1618 GetClientRect(es->hwndSelf, &rc1);
1619 IntersectRect(&rc, &rc1, &es->format_rect);
1620 ScrollWindowEx(es->hwndSelf, -dx, dy,
1621 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1622 /* force scroll info update */
1623 EDIT_UpdateScrollInfo(es);
1625 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1626 notify_parent(es, EN_HSCROLL);
1627 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1628 notify_parent(es, EN_VSCROLL);
1629 return TRUE;
1632 /*********************************************************************
1634 * EM_LINESCROLL
1636 * NOTE: dx is in average character widths, dy - in lines;
1639 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1641 if (!(es->style & ES_MULTILINE))
1642 return FALSE;
1644 dx *= es->char_width;
1645 return EDIT_EM_LineScroll_internal(es, dx, dy);
1649 /*********************************************************************
1651 * EM_SCROLL
1654 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1656 INT dy;
1658 if (!(es->style & ES_MULTILINE))
1659 return (LRESULT)FALSE;
1661 dy = 0;
1663 switch (action) {
1664 case SB_LINEUP:
1665 if (es->y_offset)
1666 dy = -1;
1667 break;
1668 case SB_LINEDOWN:
1669 if (es->y_offset < es->line_count - 1)
1670 dy = 1;
1671 break;
1672 case SB_PAGEUP:
1673 if (es->y_offset)
1674 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1675 break;
1676 case SB_PAGEDOWN:
1677 if (es->y_offset < es->line_count - 1)
1678 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1679 break;
1680 default:
1681 return (LRESULT)FALSE;
1683 if (dy) {
1684 INT vlc = get_vertical_line_count(es);
1685 /* check if we are going to move too far */
1686 if(es->y_offset + dy > es->line_count - vlc)
1687 dy = max(es->line_count - vlc, 0) - es->y_offset;
1689 /* Notification is done in EDIT_EM_LineScroll */
1690 if(dy) {
1691 EDIT_EM_LineScroll(es, 0, dy);
1692 return MAKELONG(dy, TRUE);
1696 return (LRESULT)FALSE;
1700 static void EDIT_UpdateImmCompositionWindow(EDITSTATE *es, UINT x, UINT y)
1702 COMPOSITIONFORM form =
1704 .dwStyle = CFS_RECT,
1705 .ptCurrentPos = {.x = x, .y = y},
1706 .rcArea = es->format_rect,
1708 HIMC himc = ImmGetContext(es->hwndSelf);
1709 ImmSetCompositionWindow(himc, &form);
1710 ImmReleaseContext(es->hwndSelf, himc);
1714 /*********************************************************************
1716 * EDIT_SetCaretPos
1719 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1720 BOOL after_wrap)
1722 LRESULT res;
1724 if (es->flags & EF_FOCUSED)
1726 res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1727 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1728 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1729 EDIT_UpdateImmCompositionWindow(es, (short)LOWORD(res), (short)HIWORD(res));
1734 /*********************************************************************
1736 * EM_SCROLLCARET
1739 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1741 if (es->style & ES_MULTILINE) {
1742 INT l;
1743 INT vlc;
1744 INT ww;
1745 INT cw = es->char_width;
1746 INT x;
1747 INT dy = 0;
1748 INT dx = 0;
1750 l = EDIT_EM_LineFromChar(es, es->selection_end);
1751 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1752 vlc = get_vertical_line_count(es);
1753 if (l >= es->y_offset + vlc)
1754 dy = l - vlc + 1 - es->y_offset;
1755 if (l < es->y_offset)
1756 dy = l - es->y_offset;
1757 ww = es->format_rect.right - es->format_rect.left;
1758 if (x < es->format_rect.left)
1759 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1760 if (x > es->format_rect.right)
1761 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1762 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1764 /* check if we are going to move too far */
1765 if(es->x_offset + dx + ww > es->text_width)
1766 dx = es->text_width - ww - es->x_offset;
1767 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1768 EDIT_EM_LineScroll_internal(es, dx, dy);
1770 } else {
1771 INT x;
1772 INT goal;
1773 INT format_width;
1775 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1776 format_width = es->format_rect.right - es->format_rect.left;
1777 if (x < es->format_rect.left) {
1778 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1779 do {
1780 es->x_offset--;
1781 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1782 } while ((x < goal) && es->x_offset);
1783 /* FIXME: use ScrollWindow() somehow to improve performance */
1784 EDIT_UpdateText(es, NULL, TRUE);
1785 } else if (x > es->format_rect.right) {
1786 INT x_last;
1787 INT len = get_text_length(es);
1788 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1789 do {
1790 es->x_offset++;
1791 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1792 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1793 } while ((x > goal) && (x_last > es->format_rect.right));
1794 /* FIXME: use ScrollWindow() somehow to improve performance */
1795 EDIT_UpdateText(es, NULL, TRUE);
1799 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1803 /*********************************************************************
1805 * EDIT_MoveBackward
1808 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1810 INT e = es->selection_end;
1812 if (e) {
1813 e--;
1814 if ((es->style & ES_MULTILINE) && e &&
1815 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1816 e--;
1817 if (e && (es->text[e - 1] == '\r'))
1818 e--;
1821 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1822 EDIT_EM_ScrollCaret(es);
1826 /*********************************************************************
1828 * EDIT_MoveDown_ML
1830 * Only for multi line controls
1831 * Move the caret one line down, on a column with the nearest
1832 * x coordinate on the screen (might be a different column).
1835 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1837 INT s = es->selection_start;
1838 INT e = es->selection_end;
1839 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1840 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1841 INT x = (short)LOWORD(pos);
1842 INT y = (short)HIWORD(pos);
1844 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1845 if (!extend)
1846 s = e;
1847 EDIT_EM_SetSel(es, s, e, after_wrap);
1848 EDIT_EM_ScrollCaret(es);
1852 /*********************************************************************
1854 * EDIT_MoveEnd
1857 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1859 BOOL after_wrap = FALSE;
1860 INT e;
1862 /* Pass a high value in x to make sure of receiving the end of the line */
1863 if (!ctrl && (es->style & ES_MULTILINE))
1864 e = EDIT_CharFromPos(es, 0x3fffffff,
1865 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1866 else
1867 e = get_text_length(es);
1868 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1869 EDIT_EM_ScrollCaret(es);
1873 /*********************************************************************
1875 * EDIT_MoveForward
1878 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1880 INT e = es->selection_end;
1882 if (es->text[e]) {
1883 e++;
1884 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1885 if (es->text[e] == '\n')
1886 e++;
1887 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1888 e += 2;
1891 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1892 EDIT_EM_ScrollCaret(es);
1896 /*********************************************************************
1898 * EDIT_MoveHome
1900 * Home key: move to beginning of line.
1903 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1905 INT e;
1907 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1908 if (!ctrl && (es->style & ES_MULTILINE))
1909 e = EDIT_CharFromPos(es, -es->x_offset,
1910 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1911 else
1912 e = 0;
1913 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1914 EDIT_EM_ScrollCaret(es);
1918 /*********************************************************************
1920 * EDIT_MovePageDown_ML
1922 * Only for multi line controls
1923 * Move the caret one page down, on a column with the nearest
1924 * x coordinate on the screen (might be a different column).
1927 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1929 INT s = es->selection_start;
1930 INT e = es->selection_end;
1931 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1932 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1933 INT x = (short)LOWORD(pos);
1934 INT y = (short)HIWORD(pos);
1936 e = EDIT_CharFromPos(es, x,
1937 y + (es->format_rect.bottom - es->format_rect.top),
1938 &after_wrap);
1939 if (!extend)
1940 s = e;
1941 EDIT_EM_SetSel(es, s, e, after_wrap);
1942 EDIT_EM_ScrollCaret(es);
1946 /*********************************************************************
1948 * EDIT_MovePageUp_ML
1950 * Only for multi line controls
1951 * Move the caret one page up, on a column with the nearest
1952 * x coordinate on the screen (might be a different column).
1955 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1957 INT s = es->selection_start;
1958 INT e = es->selection_end;
1959 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1960 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1961 INT x = (short)LOWORD(pos);
1962 INT y = (short)HIWORD(pos);
1964 e = EDIT_CharFromPos(es, x,
1965 y - (es->format_rect.bottom - es->format_rect.top),
1966 &after_wrap);
1967 if (!extend)
1968 s = e;
1969 EDIT_EM_SetSel(es, s, e, after_wrap);
1970 EDIT_EM_ScrollCaret(es);
1974 /*********************************************************************
1976 * EDIT_MoveUp_ML
1978 * Only for multi line controls
1979 * Move the caret one line up, on a column with the nearest
1980 * x coordinate on the screen (might be a different column).
1983 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1985 INT s = es->selection_start;
1986 INT e = es->selection_end;
1987 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1988 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1989 INT x = (short)LOWORD(pos);
1990 INT y = (short)HIWORD(pos);
1992 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1993 if (!extend)
1994 s = e;
1995 EDIT_EM_SetSel(es, s, e, after_wrap);
1996 EDIT_EM_ScrollCaret(es);
2000 /*********************************************************************
2002 * EDIT_MoveWordBackward
2005 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2007 INT s = es->selection_start;
2008 INT e = es->selection_end;
2009 INT l;
2010 INT ll;
2011 INT li;
2013 l = EDIT_EM_LineFromChar(es, e);
2014 ll = EDIT_EM_LineLength(es, e);
2015 li = EDIT_EM_LineIndex(es, l);
2016 if (e - li == 0) {
2017 if (l) {
2018 li = EDIT_EM_LineIndex(es, l - 1);
2019 e = li + EDIT_EM_LineLength(es, li);
2021 } else {
2022 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2024 if (!extend)
2025 s = e;
2026 EDIT_EM_SetSel(es, s, e, FALSE);
2027 EDIT_EM_ScrollCaret(es);
2031 /*********************************************************************
2033 * EDIT_MoveWordForward
2036 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2038 INT s = es->selection_start;
2039 INT e = es->selection_end;
2040 INT l;
2041 INT ll;
2042 INT li;
2044 l = EDIT_EM_LineFromChar(es, e);
2045 ll = EDIT_EM_LineLength(es, e);
2046 li = EDIT_EM_LineIndex(es, l);
2047 if (e - li == ll) {
2048 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2049 e = EDIT_EM_LineIndex(es, l + 1);
2050 } else {
2051 e = li + EDIT_CallWordBreakProc(es,
2052 li, e - li + 1, ll, WB_RIGHT);
2054 if (!extend)
2055 s = e;
2056 EDIT_EM_SetSel(es, s, e, FALSE);
2057 EDIT_EM_ScrollCaret(es);
2061 /*********************************************************************
2063 * EDIT_PaintText
2066 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2068 COLORREF BkColor;
2069 COLORREF TextColor;
2070 INT ret;
2071 INT li;
2072 INT BkMode;
2073 SIZE size;
2075 if (!count)
2076 return 0;
2077 BkMode = GetBkMode(dc);
2078 BkColor = GetBkColor(dc);
2079 TextColor = GetTextColor(dc);
2080 if (rev) {
2081 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2082 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2083 SetBkMode(dc, OPAQUE);
2085 li = EDIT_EM_LineIndex(es, line);
2086 if (es->style & ES_MULTILINE) {
2087 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2088 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2089 } else {
2090 TextOutW(dc, x, y, es->text + li + col, count);
2091 GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2092 ret = size.cx;
2094 if (rev) {
2095 SetBkColor(dc, BkColor);
2096 SetTextColor(dc, TextColor);
2097 SetBkMode(dc, BkMode);
2099 return ret;
2103 /*********************************************************************
2105 * EDIT_PaintLine
2108 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2110 INT s = 0;
2111 INT e = 0;
2112 INT li = 0;
2113 INT ll = 0;
2114 INT x;
2115 INT y;
2116 LRESULT pos;
2117 SCRIPT_STRING_ANALYSIS ssa;
2119 if (es->style & ES_MULTILINE) {
2120 INT vlc = get_vertical_line_count(es);
2122 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2123 return;
2124 } else if (line)
2125 return;
2127 TRACE("line=%d\n", line);
2129 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2130 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2131 x = (short)LOWORD(pos);
2132 y = (short)HIWORD(pos);
2134 if (es->style & ES_MULTILINE)
2136 int line_idx = line;
2137 x = -es->x_offset;
2138 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2140 LINEDEF *line_def = es->first_line_def;
2141 int w, lw;
2143 while (line_def && line_idx)
2145 line_def = line_def->next;
2146 line_idx--;
2148 w = es->format_rect.right - es->format_rect.left;
2149 lw = line_def->width;
2151 if (es->style & ES_RIGHT)
2152 x = w - (lw - x);
2153 else if (es->style & ES_CENTER)
2154 x += (w - lw) / 2;
2156 x += es->format_rect.left;
2159 if (rev)
2161 li = EDIT_EM_LineIndex(es, line);
2162 ll = EDIT_EM_LineLength(es, li);
2163 s = min(es->selection_start, es->selection_end);
2164 e = max(es->selection_start, es->selection_end);
2165 s = min(li + ll, max(li, s));
2166 e = min(li + ll, max(li, e));
2169 if (ssa)
2170 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2171 else if (rev && (s != e) &&
2172 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2173 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2174 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2175 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2176 } else
2177 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2179 if (es->cue_banner_text && es->text_length == 0 && (!(es->flags & EF_FOCUSED) || es->cue_banner_draw_focused))
2181 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
2182 TextOutW(dc, x, y, es->cue_banner_text, lstrlenW(es->cue_banner_text));
2187 /*********************************************************************
2189 * EDIT_AdjustFormatRect
2191 * Adjusts the format rectangle for the current font and the
2192 * current client rectangle.
2195 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2197 RECT ClientRect;
2199 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2200 if (es->style & ES_MULTILINE)
2202 INT fw, vlc, max_x_offset, max_y_offset;
2204 vlc = get_vertical_line_count(es);
2205 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2207 /* correct es->x_offset */
2208 fw = es->format_rect.right - es->format_rect.left;
2209 max_x_offset = es->text_width - fw;
2210 if(max_x_offset < 0) max_x_offset = 0;
2211 if(es->x_offset > max_x_offset)
2212 es->x_offset = max_x_offset;
2214 /* correct es->y_offset */
2215 max_y_offset = es->line_count - vlc;
2216 if(max_y_offset < 0) max_y_offset = 0;
2217 if(es->y_offset > max_y_offset)
2218 es->y_offset = max_y_offset;
2220 /* force scroll info update */
2221 EDIT_UpdateScrollInfo(es);
2223 else
2224 /* Windows doesn't care to fix text placement for SL controls */
2225 es->format_rect.bottom = es->format_rect.top + es->line_height;
2227 /* Always stay within the client area */
2228 GetClientRect(es->hwndSelf, &ClientRect);
2229 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2231 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2232 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2234 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2238 /*********************************************************************
2240 * EDIT_SetRectNP
2242 * note: this is not (exactly) the handler called on EM_SETRECTNP
2243 * it is also used to set the rect of a single line control
2246 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2248 LONG_PTR ExStyle;
2249 INT bw, bh;
2250 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2252 CopyRect(&es->format_rect, rc);
2254 if (ExStyle & WS_EX_CLIENTEDGE) {
2255 es->format_rect.left++;
2256 es->format_rect.right--;
2258 if (es->format_rect.bottom - es->format_rect.top
2259 >= es->line_height + 2)
2261 es->format_rect.top++;
2262 es->format_rect.bottom--;
2265 else if (es->style & WS_BORDER) {
2266 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2267 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2268 InflateRect(&es->format_rect, -bw, 0);
2269 if (es->format_rect.bottom - es->format_rect.top >= es->line_height + 2 * bh)
2270 InflateRect(&es->format_rect, 0, -bh);
2273 es->format_rect.left += es->left_margin;
2274 es->format_rect.right -= es->right_margin;
2275 EDIT_AdjustFormatRect(es);
2279 /*********************************************************************
2281 * EM_CHARFROMPOS
2283 * returns line number (not index) in high-order word of result.
2284 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2285 * to Richedit, not to the edit control. Original documentation is valid.
2286 * FIXME: do the specs mean to return -1 if outside client area or
2287 * if outside formatting rectangle ???
2290 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2292 POINT pt;
2293 RECT rc;
2294 INT index;
2296 pt.x = x;
2297 pt.y = y;
2298 GetClientRect(es->hwndSelf, &rc);
2299 if (!PtInRect(&rc, pt))
2300 return -1;
2302 index = EDIT_CharFromPos(es, x, y, NULL);
2303 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2307 /*********************************************************************
2309 * EM_FMTLINES
2311 * Enable or disable soft breaks.
2313 * This means: insert or remove the soft linebreak character (\r\r\n).
2314 * Take care to check if the text still fits the buffer after insertion.
2315 * If not, notify with EN_ERRSPACE.
2318 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2320 es->flags &= ~EF_USE_SOFTBRK;
2321 if (add_eol) {
2322 es->flags |= EF_USE_SOFTBRK;
2323 FIXME("soft break enabled, not implemented\n");
2325 return add_eol;
2329 /*********************************************************************
2331 * EM_GETHANDLE
2333 * Hopefully this won't fire back at us.
2334 * We always start with a fixed buffer in the local heap.
2335 * Despite of the documentation says that the local heap is used
2336 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2337 * buffer on the local heap.
2340 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2342 if (!(es->style & ES_MULTILINE))
2343 return 0;
2345 EDIT_UnlockBuffer(es, TRUE);
2347 /* The text buffer handle belongs to the app */
2348 es->hlocapp = es->hloc32W;
2350 TRACE("Returning %p, LocalSize() = %Id\n", es->hlocapp, LocalSize(es->hlocapp));
2351 return es->hlocapp;
2355 /*********************************************************************
2357 * EM_GETLINE
2360 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst)
2362 INT line_len, dst_len;
2363 LPWSTR src;
2364 INT i;
2366 if (es->style & ES_MULTILINE)
2368 if (line >= es->line_count)
2369 return 0;
2371 else
2372 line = 0;
2374 i = EDIT_EM_LineIndex(es, line);
2375 src = es->text + i;
2376 line_len = EDIT_EM_LineLength(es, i);
2377 dst_len = *(WORD *)dst;
2379 if (dst_len <= line_len)
2381 memcpy(dst, src, dst_len * sizeof(WCHAR));
2382 return dst_len;
2384 else /* Append 0 if enough space */
2386 memcpy(dst, src, line_len * sizeof(WCHAR));
2387 dst[line_len] = 0;
2388 return line_len;
2393 /*********************************************************************
2395 * EM_GETSEL
2398 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2400 UINT s = es->selection_start;
2401 UINT e = es->selection_end;
2403 ORDER_UINT(s, e);
2404 if (start)
2405 *start = s;
2406 if (end)
2407 *end = e;
2408 return MAKELONG(s, e);
2412 /*********************************************************************
2414 * EM_REPLACESEL
2416 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2419 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, const WCHAR *lpsz_replace, UINT strl,
2420 BOOL send_update, BOOL honor_limit)
2422 UINT tl = get_text_length(es);
2423 UINT utl;
2424 UINT s;
2425 UINT e;
2426 UINT i;
2427 UINT size;
2428 LPWSTR p;
2429 HRGN hrgn = 0;
2430 LPWSTR buf = NULL;
2431 UINT bufl;
2433 TRACE("%s, can_undo %d, send_update %d\n",
2434 debugstr_wn(lpsz_replace, strl), can_undo, send_update);
2436 s = es->selection_start;
2437 e = es->selection_end;
2439 EDIT_InvalidateUniscribeData(es);
2440 if ((s == e) && !strl)
2441 return;
2443 ORDER_UINT(s, e);
2445 size = tl - (e - s) + strl;
2446 if (!size)
2447 es->text_width = 0;
2449 /* Issue the EN_MAXTEXT notification and continue with replacing text
2450 * so that buffer limit is honored. */
2451 if ((honor_limit) && (size > es->buffer_limit))
2453 if (!notify_parent(es, EN_MAXTEXT)) return;
2454 /* Buffer limit can be smaller than the actual length of text in combobox */
2455 if (es->buffer_limit < (tl - (e-s)))
2456 strl = 0;
2457 else
2458 strl = min(strl, es->buffer_limit - (tl - (e-s)));
2461 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2462 return;
2464 if (e != s) {
2465 /* there is something to be deleted */
2466 TRACE("deleting stuff.\n");
2467 bufl = e - s;
2468 buf = Alloc((bufl + 1) * sizeof(WCHAR));
2469 if (!buf) return;
2470 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2471 buf[bufl] = 0; /* ensure 0 termination */
2472 /* now delete */
2473 lstrcpyW(es->text + s, es->text + e);
2474 text_buffer_changed(es);
2476 if (strl) {
2477 /* there is an insertion */
2478 tl = get_text_length(es);
2479 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));
2480 for (p = es->text + tl ; p >= es->text + s ; p--)
2481 p[strl] = p[0];
2482 for (i = 0 , p = es->text + s ; i < strl ; i++)
2483 p[i] = lpsz_replace[i];
2484 if(es->style & ES_UPPERCASE)
2485 CharUpperBuffW(p, strl);
2486 else if(es->style & ES_LOWERCASE)
2487 CharLowerBuffW(p, strl);
2488 text_buffer_changed(es);
2490 if (es->style & ES_MULTILINE)
2492 INT st = min(es->selection_start, es->selection_end);
2493 INT vlc = get_vertical_line_count(es);
2495 hrgn = CreateRectRgn(0, 0, 0, 0);
2496 EDIT_BuildLineDefs_ML(es, st, st + strl,
2497 strl - abs(es->selection_end - es->selection_start), hrgn);
2498 /* if text is too long undo all changes */
2499 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2500 if (strl)
2501 lstrcpyW(es->text + e, es->text + e + strl);
2502 if (e != s)
2503 for (i = 0 , p = es->text ; i < e - s ; i++)
2504 p[i + s] = buf[i];
2505 text_buffer_changed(es);
2506 EDIT_BuildLineDefs_ML(es, s, e,
2507 abs(es->selection_end - es->selection_start) - strl, hrgn);
2508 strl = 0;
2509 e = s;
2510 SetRectRgn(hrgn, 0, 0, 0, 0);
2511 if (!notify_parent(es, EN_MAXTEXT)) return;
2514 else {
2515 INT fw = es->format_rect.right - es->format_rect.left;
2516 EDIT_InvalidateUniscribeData(es);
2517 EDIT_CalcLineWidth_SL(es);
2518 /* remove chars that don't fit */
2519 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2520 while ((es->text_width > fw) && s + strl >= s) {
2521 lstrcpyW(es->text + s + strl - 1, es->text + s + strl);
2522 strl--;
2523 es->text_length = -1;
2524 EDIT_InvalidateUniscribeData(es);
2525 EDIT_CalcLineWidth_SL(es);
2527 text_buffer_changed(es);
2528 if (!notify_parent(es, EN_MAXTEXT)) return;
2532 if (e != s) {
2533 if (can_undo) {
2534 utl = lstrlenW(es->undo_text);
2535 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2536 /* undo-buffer is extended to the right */
2537 EDIT_MakeUndoFit(es, utl + e - s);
2538 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2539 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2540 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2541 /* undo-buffer is extended to the left */
2542 EDIT_MakeUndoFit(es, utl + e - s);
2543 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2544 p[e - s] = p[0];
2545 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2546 p[i] = buf[i];
2547 es->undo_position = s;
2548 } else {
2549 /* new undo-buffer */
2550 EDIT_MakeUndoFit(es, e - s);
2551 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2552 es->undo_text[e - s] = 0; /* ensure 0 termination */
2553 es->undo_position = s;
2555 /* any deletion makes the old insertion-undo invalid */
2556 es->undo_insert_count = 0;
2557 } else
2558 EDIT_EM_EmptyUndoBuffer(es);
2560 if (strl) {
2561 if (can_undo) {
2562 if ((s == es->undo_position) ||
2563 ((es->undo_insert_count) &&
2564 (s == es->undo_position + es->undo_insert_count)))
2566 * insertion is new and at delete position or
2567 * an extension to either left or right
2569 es->undo_insert_count += strl;
2570 else {
2571 /* new insertion undo */
2572 es->undo_position = s;
2573 es->undo_insert_count = strl;
2574 /* new insertion makes old delete-buffer invalid */
2575 *es->undo_text = '\0';
2577 } else
2578 EDIT_EM_EmptyUndoBuffer(es);
2581 Free(buf);
2583 s += strl;
2585 /* If text has been deleted and we're right or center aligned then scroll rightward */
2586 if (es->style & (ES_RIGHT | ES_CENTER))
2588 INT delta = strl - abs(es->selection_end - es->selection_start);
2590 if (delta < 0 && es->x_offset)
2592 if (abs(delta) > es->x_offset)
2593 es->x_offset = 0;
2594 else
2595 es->x_offset += delta;
2599 EDIT_EM_SetSel(es, s, s, FALSE);
2600 es->flags |= EF_MODIFIED;
2601 if (send_update) es->flags |= EF_UPDATE;
2602 if (hrgn)
2604 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2605 DeleteObject(hrgn);
2607 else
2608 EDIT_UpdateText(es, NULL, TRUE);
2610 EDIT_EM_ScrollCaret(es);
2612 /* force scroll info update */
2613 EDIT_UpdateScrollInfo(es);
2616 if(send_update || (es->flags & EF_UPDATE))
2618 es->flags &= ~EF_UPDATE;
2619 if (!notify_parent(es, EN_CHANGE)) return;
2621 EDIT_InvalidateUniscribeData(es);
2625 /*********************************************************************
2627 * EM_SETHANDLE
2629 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2632 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2634 if (!(es->style & ES_MULTILINE))
2635 return;
2637 if (!hloc)
2638 return;
2640 EDIT_UnlockBuffer(es, TRUE);
2642 es->hloc32W = hloc;
2643 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2645 /* The text buffer handle belongs to the control */
2646 es->hlocapp = NULL;
2648 EDIT_LockBuffer(es);
2649 text_buffer_changed(es);
2651 es->x_offset = es->y_offset = 0;
2652 es->selection_start = es->selection_end = 0;
2653 EDIT_EM_EmptyUndoBuffer(es);
2654 es->flags &= ~EF_MODIFIED;
2655 es->flags &= ~EF_UPDATE;
2656 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2657 EDIT_UpdateText(es, NULL, TRUE);
2658 EDIT_EM_ScrollCaret(es);
2659 /* force scroll info update */
2660 EDIT_UpdateScrollInfo(es);
2664 /*********************************************************************
2666 * EM_SETLIMITTEXT
2668 * NOTE: this version currently implements WinNT limits
2671 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2673 if (!limit) limit = ~0u;
2674 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2675 es->buffer_limit = limit;
2678 static BOOL is_cjk(HDC dc)
2680 const DWORD FS_DBCS_MASK = FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB;
2681 FONTSIGNATURE fs;
2683 switch (GdiGetCodePage(dc)) {
2684 case 932: case 936: case 949: case 950: case 1361:
2685 return TRUE;
2686 default:
2687 return (GetTextCharsetInfo(dc, &fs, 0) != DEFAULT_CHARSET &&
2688 (fs.fsCsb[0] & FS_DBCS_MASK));
2692 static int get_cjk_fontinfo_margin(int width, int side_bearing)
2694 int margin;
2695 if (side_bearing < 0)
2696 margin = min(-side_bearing, width/2);
2697 else
2698 margin = 0;
2699 return margin;
2702 struct char_width_info {
2703 INT min_lsb, min_rsb, unknown;
2706 /* Undocumented gdi32 export */
2707 extern BOOL WINAPI GetCharWidthInfo(HDC, struct char_width_info *);
2709 /*********************************************************************
2711 * EM_SETMARGINS
2713 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2714 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2715 * margin according to the textmetrics of the current font.
2717 * When EC_USEFONTINFO is used, the margins only change if the edit control is
2718 * equal to or larger than a certain size. The empty client rect is treated as
2719 * 80 pixels width.
2721 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2722 WORD left, WORD right, BOOL repaint)
2724 TEXTMETRICW tm;
2725 INT default_left_margin = 0; /* in pixels */
2726 INT default_right_margin = 0; /* in pixels */
2728 /* Set the default margins depending on the font */
2729 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2730 HDC dc = GetDC(es->hwndSelf);
2731 HFONT old_font = SelectObject(dc, es->font);
2732 LONG width = GdiGetCharDimensions(dc, &tm, NULL), rc_width;
2733 RECT rc;
2735 /* The default margins are only non zero for TrueType or Vector fonts */
2736 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2737 struct char_width_info width_info;
2739 if (is_cjk(dc) && GetCharWidthInfo(dc, &width_info))
2741 default_left_margin = get_cjk_fontinfo_margin(width, width_info.min_lsb);
2742 default_right_margin = get_cjk_fontinfo_margin(width, width_info.min_rsb);
2744 else
2746 default_left_margin = width / 2;
2747 default_right_margin = width / 2;
2750 GetClientRect(es->hwndSelf, &rc);
2751 rc_width = !IsRectEmpty(&rc) ? rc.right - rc.left : 80;
2752 if (rc_width < default_left_margin + default_right_margin + width * 2) {
2753 default_left_margin = es->left_margin;
2754 default_right_margin = es->right_margin;
2757 SelectObject(dc, old_font);
2758 ReleaseDC(es->hwndSelf, dc);
2761 if (action & EC_LEFTMARGIN) {
2762 es->format_rect.left -= es->left_margin;
2763 if (left != EC_USEFONTINFO)
2764 es->left_margin = left;
2765 else
2766 es->left_margin = default_left_margin;
2767 es->format_rect.left += es->left_margin;
2770 if (action & EC_RIGHTMARGIN) {
2771 es->format_rect.right += es->right_margin;
2772 if (right != EC_USEFONTINFO)
2773 es->right_margin = right;
2774 else
2775 es->right_margin = default_right_margin;
2776 es->format_rect.right -= es->right_margin;
2779 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2780 EDIT_AdjustFormatRect(es);
2781 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2784 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2788 /*********************************************************************
2790 * EM_SETPASSWORDCHAR
2793 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2795 LONG style;
2797 if (es->style & ES_MULTILINE)
2798 return;
2800 if (es->password_char == c)
2801 return;
2803 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2804 es->password_char = c;
2805 if (c) {
2806 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2807 es->style |= ES_PASSWORD;
2808 } else {
2809 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2810 es->style &= ~ES_PASSWORD;
2812 EDIT_InvalidateUniscribeData(es);
2813 EDIT_UpdateText(es, NULL, TRUE);
2817 /*********************************************************************
2819 * EM_SETTABSTOPS
2822 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2824 if (!(es->style & ES_MULTILINE))
2825 return FALSE;
2826 Free(es->tabs);
2827 es->tabs_count = count;
2828 if (!count)
2829 es->tabs = NULL;
2830 else {
2831 es->tabs = Alloc(count * sizeof(INT));
2832 memcpy(es->tabs, tabs, count * sizeof(INT));
2834 EDIT_InvalidateUniscribeData(es);
2835 return TRUE;
2839 /*********************************************************************
2841 * EM_SETWORDBREAKPROC
2844 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, EDITWORDBREAKPROCW wbp)
2846 if (es->word_break_proc == wbp)
2847 return;
2849 es->word_break_proc = wbp;
2851 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2852 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2853 EDIT_UpdateText(es, NULL, TRUE);
2858 /*********************************************************************
2860 * EM_UNDO / WM_UNDO
2863 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2865 INT ulength;
2866 LPWSTR utext;
2868 /* As per MSDN spec, for a single-line edit control,
2869 the return value is always TRUE */
2870 if( es->style & ES_READONLY )
2871 return !(es->style & ES_MULTILINE);
2873 ulength = lstrlenW(es->undo_text);
2875 utext = Alloc((ulength + 1) * sizeof(WCHAR));
2877 lstrcpyW(utext, es->undo_text);
2879 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2880 es->undo_insert_count, debugstr_w(utext));
2882 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2883 EDIT_EM_EmptyUndoBuffer(es);
2884 EDIT_EM_ReplaceSel(es, TRUE, utext, ulength, TRUE, TRUE);
2885 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2886 /* send the notification after the selection start and end are set */
2887 if (!notify_parent(es, EN_CHANGE)) return TRUE;
2888 EDIT_EM_ScrollCaret(es);
2889 Free(utext);
2891 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2892 es->undo_insert_count, debugstr_w(es->undo_text));
2893 return TRUE;
2897 /* Helper function for WM_CHAR
2899 * According to an MSDN blog article titled "Just because you're a control
2900 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2901 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2902 * a dialog box or not.
2904 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2906 return (es->flags & EF_DIALOGMODE);
2910 /*********************************************************************
2912 * WM_PASTE
2915 static void EDIT_WM_Paste(EDITSTATE *es)
2917 HGLOBAL hsrc;
2918 LPWSTR src, ptr;
2919 int len;
2921 /* Protect read-only edit control from modification */
2922 if(es->style & ES_READONLY)
2923 return;
2925 OpenClipboard(es->hwndSelf);
2926 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2927 src = GlobalLock(hsrc);
2928 len = lstrlenW(src);
2929 /* Protect single-line edit against pasting new line character */
2930 if (!(es->style & ES_MULTILINE) && ((ptr = wcschr(src, '\n')))) {
2931 len = ptr - src;
2932 if (len && src[len - 1] == '\r')
2933 --len;
2935 EDIT_EM_ReplaceSel(es, TRUE, src, len, TRUE, TRUE);
2936 GlobalUnlock(hsrc);
2938 else if (es->style & ES_PASSWORD) {
2939 /* clear selected text in password edit box even with empty clipboard */
2940 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2942 CloseClipboard();
2946 /*********************************************************************
2948 * WM_COPY
2951 static void EDIT_WM_Copy(EDITSTATE *es)
2953 INT s = min(es->selection_start, es->selection_end);
2954 INT e = max(es->selection_start, es->selection_end);
2955 HGLOBAL hdst;
2956 LPWSTR dst;
2957 DWORD len;
2959 if (e == s) return;
2961 len = e - s;
2962 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2963 dst = GlobalLock(hdst);
2964 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2965 dst[len] = 0; /* ensure 0 termination */
2966 TRACE("%s\n", debugstr_w(dst));
2967 GlobalUnlock(hdst);
2968 OpenClipboard(es->hwndSelf);
2969 EmptyClipboard();
2970 SetClipboardData(CF_UNICODETEXT, hdst);
2971 CloseClipboard();
2975 /*********************************************************************
2977 * WM_CLEAR
2980 static inline void EDIT_WM_Clear(EDITSTATE *es)
2982 /* Protect read-only edit control from modification */
2983 if(es->style & ES_READONLY)
2984 return;
2986 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
2990 /*********************************************************************
2992 * WM_CUT
2995 static inline void EDIT_WM_Cut(EDITSTATE *es)
2997 EDIT_WM_Copy(es);
2998 EDIT_WM_Clear(es);
3002 /*********************************************************************
3004 * WM_CHAR
3007 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3009 BOOL control;
3011 control = GetKeyState(VK_CONTROL) & 0x8000;
3013 switch (c) {
3014 case '\r':
3015 /* If it's not a multiline edit box, it would be ignored below.
3016 * For multiline edit without ES_WANTRETURN, we have to make a
3017 * special case.
3019 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3020 if (EDIT_IsInsideDialog(es))
3021 break;
3022 case '\n':
3023 if (es->style & ES_MULTILINE) {
3024 if (es->style & ES_READONLY) {
3025 EDIT_MoveHome(es, FALSE, FALSE);
3026 EDIT_MoveDown_ML(es, FALSE);
3027 } else
3028 EDIT_EM_ReplaceSel(es, TRUE, L"\r\n", 2, TRUE, TRUE);
3030 break;
3031 case '\t':
3032 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3034 if (EDIT_IsInsideDialog(es))
3035 break;
3036 EDIT_EM_ReplaceSel(es, TRUE, L"\t", 1, TRUE, TRUE);
3038 break;
3039 case VK_BACK:
3040 if (!(es->style & ES_READONLY) && !control) {
3041 if (es->selection_start != es->selection_end)
3042 EDIT_WM_Clear(es);
3043 else {
3044 /* delete character left of caret */
3045 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3046 EDIT_MoveBackward(es, TRUE);
3047 EDIT_WM_Clear(es);
3050 break;
3051 case 0x03: /* ^C */
3052 if (!(es->style & ES_PASSWORD))
3053 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3054 break;
3055 case 0x16: /* ^V */
3056 if (!(es->style & ES_READONLY))
3057 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3058 break;
3059 case 0x18: /* ^X */
3060 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3061 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3062 break;
3063 case 0x1A: /* ^Z */
3064 if (!(es->style & ES_READONLY))
3065 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3066 break;
3068 default:
3069 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3070 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3071 break;
3073 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127))
3074 EDIT_EM_ReplaceSel(es, TRUE, &c, 1, TRUE, TRUE);
3075 break;
3077 return 1;
3081 /*********************************************************************
3083 * EDIT_ContextMenuCommand
3086 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3088 switch (id) {
3089 case EM_UNDO:
3090 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3091 break;
3092 case WM_CUT:
3093 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3094 break;
3095 case WM_COPY:
3096 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3097 break;
3098 case WM_PASTE:
3099 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3100 break;
3101 case WM_CLEAR:
3102 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3103 break;
3104 case EM_SETSEL:
3105 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3106 break;
3107 default:
3108 ERR("unknown menu item, please report\n");
3109 break;
3114 /*********************************************************************
3116 * WM_CONTEXTMENU
3118 * Note: the resource files resource/sysres_??.rc cannot define a
3119 * single popup menu. Hence we use a (dummy) menubar
3120 * containing the single popup menu as its first item.
3122 * FIXME: the message identifiers have been chosen arbitrarily,
3123 * hence we use MF_BYPOSITION.
3124 * We might as well use the "real" values (anybody knows ?)
3125 * The menu definition is in resources/sysres_??.rc.
3126 * Once these are OK, we better use MF_BYCOMMAND here
3127 * (as we do in EDIT_WM_Command()).
3130 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3132 HMENU menu = LoadMenuA(GetModuleHandleA("user32.dll"), "EDITMENU");
3133 HMENU popup = GetSubMenu(menu, 0);
3134 UINT start = es->selection_start;
3135 UINT end = es->selection_end;
3136 UINT cmd;
3137 POINT pt;
3139 ORDER_UINT(start, end);
3141 /* undo */
3142 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3143 /* cut */
3144 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3145 /* copy */
3146 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3147 /* paste */
3148 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3149 /* delete */
3150 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3151 /* select all */
3152 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3154 pt.x = x;
3155 pt.y = y;
3157 if (pt.x == -1 && pt.y == -1) /* passed via VK_APPS press/release */
3159 RECT rc;
3161 /* Windows places the menu at the edit's center in this case */
3162 GetClientRect(es->hwndSelf, &rc);
3163 pt.x = rc.left + (rc.right - rc.left) / 2;
3164 pt.y = rc.top + (rc.bottom - rc.top) / 2;
3165 ClientToScreen(es->hwndSelf, &pt);
3168 if (!(es->flags & EF_FOCUSED))
3169 SetFocus(es->hwndSelf);
3171 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3172 pt.x, pt.y, 0, es->hwndSelf, NULL);
3174 if (cmd)
3175 EDIT_ContextMenuCommand(es, cmd);
3177 DestroyMenu(menu);
3181 /*********************************************************************
3183 * WM_GETTEXT
3186 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst)
3188 if (!count)
3189 return 0;
3191 lstrcpynW(dst, es->text, count);
3192 return lstrlenW(dst);
3195 /*********************************************************************
3197 * EDIT_CheckCombo
3200 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3202 HWND hLBox = es->hwndListBox;
3203 HWND hCombo;
3204 BOOL bDropped;
3205 int nEUI;
3207 if (!hLBox)
3208 return FALSE;
3210 hCombo = GetParent(es->hwndSelf);
3211 bDropped = TRUE;
3212 nEUI = 0;
3214 TRACE("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3216 if (key == VK_UP || key == VK_DOWN)
3218 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3219 nEUI = 1;
3221 if (msg == WM_KEYDOWN || nEUI)
3222 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3225 switch (msg)
3227 case WM_KEYDOWN:
3228 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3230 /* make sure ComboLBox pops up */
3231 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3232 key = VK_F4;
3233 nEUI = 2;
3236 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3237 break;
3239 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3240 if (nEUI)
3241 SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3242 else
3243 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3244 break;
3247 if (nEUI == 2)
3248 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3250 return TRUE;
3254 /*********************************************************************
3256 * WM_KEYDOWN
3258 * Handling of special keys that don't produce a WM_CHAR
3259 * (i.e. non-printable keys) & Backspace & Delete
3262 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3264 BOOL shift;
3265 BOOL control;
3267 if (GetKeyState(VK_MENU) & 0x8000)
3268 return 0;
3270 shift = GetKeyState(VK_SHIFT) & 0x8000;
3271 control = GetKeyState(VK_CONTROL) & 0x8000;
3273 switch (key) {
3274 case VK_F4:
3275 case VK_UP:
3276 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3277 break;
3279 /* fall through */
3280 case VK_LEFT:
3281 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3282 EDIT_MoveUp_ML(es, shift);
3283 else
3284 if (control)
3285 EDIT_MoveWordBackward(es, shift);
3286 else
3287 EDIT_MoveBackward(es, shift);
3288 break;
3289 case VK_DOWN:
3290 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3291 break;
3292 /* fall through */
3293 case VK_RIGHT:
3294 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3295 EDIT_MoveDown_ML(es, shift);
3296 else if (control)
3297 EDIT_MoveWordForward(es, shift);
3298 else
3299 EDIT_MoveForward(es, shift);
3300 break;
3301 case VK_HOME:
3302 EDIT_MoveHome(es, shift, control);
3303 break;
3304 case VK_END:
3305 EDIT_MoveEnd(es, shift, control);
3306 break;
3307 case VK_PRIOR:
3308 if (es->style & ES_MULTILINE)
3309 EDIT_MovePageUp_ML(es, shift);
3310 else
3311 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3312 break;
3313 case VK_NEXT:
3314 if (es->style & ES_MULTILINE)
3315 EDIT_MovePageDown_ML(es, shift);
3316 else
3317 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3318 break;
3319 case VK_DELETE:
3320 if (!(es->style & ES_READONLY) && !(shift && control)) {
3321 if (es->selection_start != es->selection_end) {
3322 if (shift)
3323 EDIT_WM_Cut(es);
3324 else
3325 EDIT_WM_Clear(es);
3326 } else {
3327 EDIT_EM_SetSel(es, ~0u, 0, FALSE);
3328 if (shift)
3329 /* delete character left of caret */
3330 EDIT_MoveBackward(es, TRUE);
3331 else if (control)
3332 /* delete to end of line */
3333 EDIT_MoveEnd(es, TRUE, FALSE);
3334 else
3335 /* delete character right of caret */
3336 EDIT_MoveForward(es, TRUE);
3337 EDIT_WM_Clear(es);
3340 break;
3341 case VK_INSERT:
3342 if (shift) {
3343 if (!(es->style & ES_READONLY))
3344 EDIT_WM_Paste(es);
3345 } else if (control)
3346 EDIT_WM_Copy(es);
3347 break;
3348 case VK_RETURN:
3349 /* If the edit doesn't want the return send a message to the default object */
3350 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3352 DWORD dw;
3354 if (!EDIT_IsInsideDialog(es)) break;
3355 if (control) break;
3356 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3357 if (HIWORD(dw) == DC_HASDEFID)
3359 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3360 if (hwDefCtrl)
3362 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3363 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3367 break;
3368 case VK_ESCAPE:
3369 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3370 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3371 break;
3372 case VK_TAB:
3373 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3374 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3375 break;
3376 case 'A':
3377 if (control)
3379 if (EDIT_EM_SetSel(es, 0, get_text_length(es), FALSE))
3381 if (!notify_parent(es, EN_UPDATE)) break;
3382 notify_parent(es, EN_CHANGE);
3385 break;
3387 return TRUE;
3391 /*********************************************************************
3393 * WM_KILLFOCUS
3396 static LRESULT EDIT_WM_KillFocus(HTHEME theme, EDITSTATE *es)
3398 UINT flags = RDW_INVALIDATE;
3400 es->flags &= ~EF_FOCUSED;
3401 DestroyCaret();
3402 if (!(es->style & ES_NOHIDESEL))
3403 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3404 if (!notify_parent(es, EN_KILLFOCUS)) return 0;
3405 /* Throw away left over scroll when we lose focus */
3406 es->wheelDeltaRemainder = 0;
3408 if (theme)
3409 flags |= RDW_FRAME;
3411 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3412 return 0;
3416 /*********************************************************************
3418 * WM_LBUTTONDBLCLK
3420 * The caret position has been set on the WM_LBUTTONDOWN message
3423 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3425 INT s;
3426 INT e = es->selection_end;
3427 INT l;
3428 INT li;
3429 INT ll;
3431 es->bCaptureState = TRUE;
3432 SetCapture(es->hwndSelf);
3434 l = EDIT_EM_LineFromChar(es, e);
3435 li = EDIT_EM_LineIndex(es, l);
3436 ll = EDIT_EM_LineLength(es, e);
3437 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3438 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3439 EDIT_EM_SetSel(es, s, e, FALSE);
3440 EDIT_EM_ScrollCaret(es);
3441 es->region_posx = es->region_posy = 0;
3442 SetTimer(es->hwndSelf, 0, 100, NULL);
3443 return 0;
3447 /*********************************************************************
3449 * WM_LBUTTONDOWN
3452 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3454 INT e;
3455 BOOL after_wrap;
3457 es->bCaptureState = TRUE;
3458 SetCapture(es->hwndSelf);
3459 EDIT_ConfinePoint(es, &x, &y);
3460 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3461 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3462 EDIT_EM_ScrollCaret(es);
3463 es->region_posx = es->region_posy = 0;
3464 SetTimer(es->hwndSelf, 0, 100, NULL);
3466 if (!(es->flags & EF_FOCUSED))
3467 SetFocus(es->hwndSelf);
3469 return 0;
3473 /*********************************************************************
3475 * WM_LBUTTONUP
3478 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3480 if (es->bCaptureState) {
3481 KillTimer(es->hwndSelf, 0);
3482 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3484 es->bCaptureState = FALSE;
3485 return 0;
3489 /*********************************************************************
3491 * WM_MBUTTONDOWN
3494 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3496 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3497 return 0;
3501 /*********************************************************************
3503 * WM_MOUSEMOVE
3506 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3508 INT e;
3509 BOOL after_wrap;
3510 INT prex, prey;
3512 /* If the mouse has been captured by process other than the edit control itself,
3513 * the windows edit controls will not select the strings with mouse move.
3515 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3516 return 0;
3519 * FIXME: gotta do some scrolling if outside client
3520 * area. Maybe reset the timer ?
3522 prex = x; prey = y;
3523 EDIT_ConfinePoint(es, &x, &y);
3524 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3525 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3526 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3527 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3528 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3529 return 0;
3533 /*********************************************************************
3535 * WM_PAINT
3538 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3540 PAINTSTRUCT ps;
3541 INT i;
3542 HDC dc;
3543 HFONT old_font = 0;
3544 RECT rc;
3545 RECT rcClient;
3546 RECT rcLine;
3547 RECT rcRgn;
3548 HBRUSH brush;
3549 HBRUSH old_brush;
3550 INT bw, bh;
3551 BOOL rev = es->bEnableState &&
3552 ((es->flags & EF_FOCUSED) ||
3553 (es->style & ES_NOHIDESEL));
3554 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3556 /* The dc we use for calculating may not be the one we paint into.
3557 This is the safest action. */
3558 EDIT_InvalidateUniscribeData(es);
3559 GetClientRect(es->hwndSelf, &rcClient);
3561 /* get the background brush */
3562 brush = EDIT_NotifyCtlColor(es, dc);
3564 /* paint the border and the background */
3565 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3567 if(es->style & WS_BORDER) {
3568 bw = GetSystemMetrics(SM_CXBORDER);
3569 bh = GetSystemMetrics(SM_CYBORDER);
3570 rc = rcClient;
3571 if(es->style & ES_MULTILINE) {
3572 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3573 if(es->style & WS_VSCROLL) rc.right+=bw;
3576 /* Draw the frame. Same code as in nonclient.c */
3577 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3578 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3579 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3580 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3581 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3582 SelectObject(dc, old_brush);
3584 /* Keep the border clean */
3585 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3586 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3589 GetClipBox(dc, &rc);
3590 FillRect(dc, &rc, brush);
3592 IntersectClipRect(dc, es->format_rect.left,
3593 es->format_rect.top,
3594 es->format_rect.right,
3595 es->format_rect.bottom);
3596 if (es->style & ES_MULTILINE) {
3597 rc = rcClient;
3598 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3600 if (es->font)
3601 old_font = SelectObject(dc, es->font);
3603 if (!es->bEnableState)
3604 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3605 GetClipBox(dc, &rcRgn);
3606 if (es->style & ES_MULTILINE) {
3607 INT vlc = get_vertical_line_count(es);
3608 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3609 EDIT_UpdateUniscribeData(es, dc, i);
3610 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3611 if (IntersectRect(&rc, &rcRgn, &rcLine))
3612 EDIT_PaintLine(es, dc, i, rev);
3614 } else {
3615 EDIT_UpdateUniscribeData(es, dc, 0);
3616 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3617 if (IntersectRect(&rc, &rcRgn, &rcLine))
3618 EDIT_PaintLine(es, dc, 0, rev);
3620 if (es->font)
3621 SelectObject(dc, old_font);
3623 if (!hdc)
3624 EndPaint(es->hwndSelf, &ps);
3627 static void EDIT_WM_NCPaint(HWND hwnd, HRGN region)
3629 DWORD exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3630 HTHEME theme = GetWindowTheme(hwnd);
3631 HRGN cliprgn = region;
3633 if (theme && exStyle & WS_EX_CLIENTEDGE)
3635 HDC dc;
3636 RECT r;
3637 int cxEdge = GetSystemMetrics(SM_CXEDGE),
3638 cyEdge = GetSystemMetrics(SM_CYEDGE);
3639 const int part = EP_EDITTEXT;
3640 int state = ETS_NORMAL;
3641 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3643 if (!IsWindowEnabled(hwnd))
3644 state = ETS_DISABLED;
3645 else if (dwStyle & ES_READONLY)
3646 state = ETS_READONLY;
3647 else if (GetFocus() == hwnd)
3648 state = ETS_FOCUSED;
3650 GetWindowRect(hwnd, &r);
3652 /* New clipping region passed to default proc to exclude border */
3653 cliprgn = CreateRectRgn(r.left + cxEdge, r.top + cyEdge,
3654 r.right - cxEdge, r.bottom - cyEdge);
3655 if (region != (HRGN)1)
3656 CombineRgn(cliprgn, cliprgn, region, RGN_AND);
3657 OffsetRect(&r, -r.left, -r.top);
3659 dc = GetDCEx(hwnd, region, DCX_WINDOW|DCX_INTERSECTRGN);
3661 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
3662 DrawThemeParentBackground(hwnd, dc, &r);
3663 DrawThemeBackground(theme, dc, part, state, &r, 0);
3664 ReleaseDC(hwnd, dc);
3667 /* Call default proc to get the scrollbars etc. also painted */
3668 DefWindowProcW (hwnd, WM_NCPAINT, (WPARAM)cliprgn, 0);
3669 if (cliprgn != region)
3670 DeleteObject(cliprgn);
3673 /*********************************************************************
3675 * WM_SETFOCUS
3678 static void EDIT_WM_SetFocus(HTHEME theme, EDITSTATE *es)
3680 UINT flags = RDW_INVALIDATE;
3682 es->flags |= EF_FOCUSED;
3684 if (!(es->style & ES_NOHIDESEL))
3685 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3687 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3688 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3689 ShowCaret(es->hwndSelf);
3690 if (!notify_parent(es, EN_SETFOCUS)) return;
3692 if (theme)
3693 flags |= RDW_FRAME | RDW_ERASE;
3695 RedrawWindow(es->hwndSelf, NULL, NULL, flags);
3699 static DWORD get_font_margins(HDC hdc, const TEXTMETRICW *tm)
3701 ABC abc[256];
3702 SHORT left, right;
3703 UINT i;
3705 if (!(tm->tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE)))
3706 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3708 if (!is_cjk(hdc))
3709 return MAKELONG(EC_USEFONTINFO, EC_USEFONTINFO);
3711 if (!GetCharABCWidthsW(hdc, 0, 255, abc))
3712 return 0;
3714 left = right = 0;
3715 for (i = 0; i < ARRAY_SIZE(abc); i++) {
3716 if (-abc[i].abcA > right) right = -abc[i].abcA;
3717 if (-abc[i].abcC > left ) left = -abc[i].abcC;
3719 return MAKELONG(left, right);
3722 static void EDIT_UpdateImmCompositionFont(EDITSTATE *es)
3724 LOGFONTW composition_font;
3725 HIMC himc = ImmGetContext(es->hwndSelf);
3726 GetObjectW(es->font, sizeof(LOGFONTW), &composition_font);
3727 ImmSetCompositionFontW(himc, &composition_font);
3728 ImmReleaseContext(es->hwndSelf, himc);
3731 /*********************************************************************
3733 * WM_SETFONT
3735 * With Win95 look the margins are set to default font value unless
3736 * the system font (font == 0) is being set, in which case they are left
3737 * unchanged.
3740 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3742 TEXTMETRICW tm;
3743 HDC dc;
3744 HFONT old_font = 0;
3745 RECT clientRect;
3746 DWORD margins;
3748 es->font = font;
3749 EDIT_InvalidateUniscribeData(es);
3750 dc = GetDC(es->hwndSelf);
3751 if (font)
3752 old_font = SelectObject(dc, font);
3753 GetTextMetricsW(dc, &tm);
3754 es->line_height = tm.tmHeight;
3755 es->char_width = tm.tmAveCharWidth;
3756 margins = get_font_margins(dc, &tm);
3757 if (font)
3758 SelectObject(dc, old_font);
3759 ReleaseDC(es->hwndSelf, dc);
3761 /* Reset the format rect and the margins */
3762 GetClientRect(es->hwndSelf, &clientRect);
3763 EDIT_SetRectNP(es, &clientRect);
3764 if (margins)
3765 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3766 LOWORD(margins), HIWORD(margins), FALSE);
3768 if (es->style & ES_MULTILINE)
3769 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3770 else
3771 EDIT_CalcLineWidth_SL(es);
3773 if (redraw)
3774 EDIT_UpdateText(es, NULL, TRUE);
3775 if (es->flags & EF_FOCUSED) {
3776 DestroyCaret();
3777 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3778 EDIT_SetCaretPos(es, es->selection_end,
3779 es->flags & EF_AFTER_WRAP);
3780 ShowCaret(es->hwndSelf);
3783 EDIT_UpdateImmCompositionFont(es);
3787 /*********************************************************************
3789 * WM_SETTEXT
3791 * NOTES
3792 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3793 * The modified flag is reset. No notifications are sent.
3795 * For single-line controls, reception of WM_SETTEXT triggers:
3796 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3799 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text)
3801 if (es->flags & EF_UPDATE)
3802 /* fixed this bug once; complain if we see it about to happen again. */
3803 ERR("SetSel may generate UPDATE message whose handler may reset "
3804 "selection.\n");
3806 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3807 if (text)
3809 TRACE("%s\n", debugstr_w(text));
3810 EDIT_EM_ReplaceSel(es, FALSE, text, lstrlenW(text), FALSE, FALSE);
3812 else
3814 TRACE("<NULL>\n");
3815 EDIT_EM_ReplaceSel(es, FALSE, NULL, 0, FALSE, FALSE);
3817 es->x_offset = 0;
3818 es->flags &= ~EF_MODIFIED;
3819 EDIT_EM_SetSel(es, 0, 0, FALSE);
3821 /* Send the notification after the selection start and end have been set
3822 * edit control doesn't send notification on WM_SETTEXT
3823 * if it is multiline, or it is part of combobox
3825 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3827 if (!notify_parent(es, EN_UPDATE)) return;
3828 if (!notify_parent(es, EN_CHANGE)) return;
3830 EDIT_EM_ScrollCaret(es);
3831 EDIT_UpdateScrollInfo(es);
3832 EDIT_InvalidateUniscribeData(es);
3836 /*********************************************************************
3838 * WM_SIZE
3841 static void EDIT_WM_Size(EDITSTATE *es, UINT action)
3843 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3844 RECT rc;
3845 GetClientRect(es->hwndSelf, &rc);
3846 EDIT_SetRectNP(es, &rc);
3847 EDIT_UpdateText(es, NULL, TRUE);
3852 /*********************************************************************
3854 * WM_STYLECHANGED
3856 * This message is sent by SetWindowLong on having changed either the Style
3857 * or the extended style.
3859 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3861 * See also EDIT_WM_NCCreate
3863 * It appears that the Windows version of the edit control allows the style
3864 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3865 * style variable which will generally be different. In this function we
3866 * update the internal style based on what changed in the externally visible
3867 * style.
3869 * Much of this content as based upon the MSDN, especially:
3870 * Platform SDK Documentation -> User Interface Services ->
3871 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3872 * Edit Control Styles
3874 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3876 if (GWL_STYLE == which) {
3877 DWORD style_change_mask;
3878 DWORD new_style;
3879 /* Only a subset of changes can be applied after the control
3880 * has been created.
3882 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3883 ES_NUMBER;
3884 if (es->style & ES_MULTILINE)
3885 style_change_mask |= ES_WANTRETURN;
3887 new_style = style->styleNew & style_change_mask;
3889 /* Number overrides lowercase overrides uppercase (at least it
3890 * does in Win95). However I'll bet that ES_NUMBER would be
3891 * invalid under Win 3.1.
3893 if (new_style & ES_NUMBER) {
3894 ; /* do not override the ES_NUMBER */
3895 } else if (new_style & ES_LOWERCASE) {
3896 new_style &= ~ES_UPPERCASE;
3899 es->style = (es->style & ~style_change_mask) | new_style;
3900 } else if (GWL_EXSTYLE == which) {
3901 ; /* FIXME - what is needed here */
3902 } else {
3903 WARN ("Invalid style change %#Ix.\n", which);
3906 return 0;
3909 /*********************************************************************
3911 * WM_SYSKEYDOWN
3914 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3916 if ((key == VK_BACK) && (key_data & 0x2000)) {
3917 if (EDIT_EM_CanUndo(es))
3918 EDIT_EM_Undo(es);
3919 return 0;
3920 } else if (key == VK_UP || key == VK_DOWN) {
3921 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3922 return 0;
3924 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3928 /*********************************************************************
3930 * WM_TIMER
3933 static void EDIT_WM_Timer(EDITSTATE *es)
3935 if (es->region_posx < 0) {
3936 EDIT_MoveBackward(es, TRUE);
3937 } else if (es->region_posx > 0) {
3938 EDIT_MoveForward(es, TRUE);
3941 * FIXME: gotta do some vertical scrolling here, like
3942 * EDIT_EM_LineScroll(hwnd, 0, 1);
3946 /*********************************************************************
3948 * WM_HSCROLL
3951 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3953 INT dx;
3954 INT fw;
3956 if (!(es->style & ES_MULTILINE))
3957 return 0;
3959 if (!(es->style & ES_AUTOHSCROLL))
3960 return 0;
3962 dx = 0;
3963 fw = es->format_rect.right - es->format_rect.left;
3964 switch (action) {
3965 case SB_LINELEFT:
3966 TRACE("SB_LINELEFT\n");
3967 if (es->x_offset)
3968 dx = -es->char_width;
3969 break;
3970 case SB_LINERIGHT:
3971 TRACE("SB_LINERIGHT\n");
3972 if (es->x_offset < es->text_width)
3973 dx = es->char_width;
3974 break;
3975 case SB_PAGELEFT:
3976 TRACE("SB_PAGELEFT\n");
3977 if (es->x_offset)
3978 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3979 break;
3980 case SB_PAGERIGHT:
3981 TRACE("SB_PAGERIGHT\n");
3982 if (es->x_offset < es->text_width)
3983 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3984 break;
3985 case SB_LEFT:
3986 TRACE("SB_LEFT\n");
3987 if (es->x_offset)
3988 dx = -es->x_offset;
3989 break;
3990 case SB_RIGHT:
3991 TRACE("SB_RIGHT\n");
3992 if (es->x_offset < es->text_width)
3993 dx = es->text_width - es->x_offset;
3994 break;
3995 case SB_THUMBTRACK:
3996 TRACE("SB_THUMBTRACK %d\n", pos);
3997 es->flags |= EF_HSCROLL_TRACK;
3998 if(es->style & WS_HSCROLL)
3999 dx = pos - es->x_offset;
4000 else
4002 INT fw, new_x;
4003 /* Sanity check */
4004 if(pos < 0 || pos > 100) return 0;
4005 /* Assume default scroll range 0-100 */
4006 fw = es->format_rect.right - es->format_rect.left;
4007 new_x = pos * (es->text_width - fw) / 100;
4008 dx = es->text_width ? (new_x - es->x_offset) : 0;
4010 break;
4011 case SB_THUMBPOSITION:
4012 TRACE("SB_THUMBPOSITION %d\n", pos);
4013 es->flags &= ~EF_HSCROLL_TRACK;
4014 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4015 dx = pos - es->x_offset;
4016 else
4018 INT fw, new_x;
4019 /* Sanity check */
4020 if(pos < 0 || pos > 100) return 0;
4021 /* Assume default scroll range 0-100 */
4022 fw = es->format_rect.right - es->format_rect.left;
4023 new_x = pos * (es->text_width - fw) / 100;
4024 dx = es->text_width ? (new_x - es->x_offset) : 0;
4026 if (!dx) {
4027 /* force scroll info update */
4028 EDIT_UpdateScrollInfo(es);
4029 notify_parent(es, EN_HSCROLL);
4031 break;
4032 case SB_ENDSCROLL:
4033 TRACE("SB_ENDSCROLL\n");
4034 break;
4036 * FIXME : the next two are undocumented !
4037 * Are we doing the right thing ?
4038 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4039 * although it's also a regular control message.
4041 case EM_GETTHUMB: /* this one is used by NT notepad */
4043 LRESULT ret;
4044 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4045 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4046 else
4048 /* Assume default scroll range 0-100 */
4049 INT fw = es->format_rect.right - es->format_rect.left;
4050 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4052 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4053 return ret;
4055 case EM_LINESCROLL:
4056 TRACE("EM_LINESCROLL16\n");
4057 dx = pos;
4058 break;
4060 default:
4061 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4062 action, action);
4063 return 0;
4065 if (dx)
4067 INT fw = es->format_rect.right - es->format_rect.left;
4068 /* check if we are going to move too far */
4069 if(es->x_offset + dx + fw > es->text_width)
4070 dx = es->text_width - fw - es->x_offset;
4071 if(dx)
4072 EDIT_EM_LineScroll_internal(es, dx, 0);
4074 return 0;
4078 /*********************************************************************
4080 * WM_VSCROLL
4083 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4085 INT dy;
4087 if (!(es->style & ES_MULTILINE))
4088 return 0;
4090 if (!(es->style & ES_AUTOVSCROLL))
4091 return 0;
4093 dy = 0;
4094 switch (action) {
4095 case SB_LINEUP:
4096 case SB_LINEDOWN:
4097 case SB_PAGEUP:
4098 case SB_PAGEDOWN:
4099 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4100 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4101 (action == SB_PAGEUP ? "SB_PAGEUP" :
4102 "SB_PAGEDOWN"))));
4103 EDIT_EM_Scroll(es, action);
4104 return 0;
4105 case SB_TOP:
4106 TRACE("SB_TOP\n");
4107 dy = -es->y_offset;
4108 break;
4109 case SB_BOTTOM:
4110 TRACE("SB_BOTTOM\n");
4111 dy = es->line_count - 1 - es->y_offset;
4112 break;
4113 case SB_THUMBTRACK:
4114 TRACE("SB_THUMBTRACK %d\n", pos);
4115 es->flags |= EF_VSCROLL_TRACK;
4116 if(es->style & WS_VSCROLL)
4117 dy = pos - es->y_offset;
4118 else
4120 /* Assume default scroll range 0-100 */
4121 INT vlc, new_y;
4122 /* Sanity check */
4123 if(pos < 0 || pos > 100) return 0;
4124 vlc = get_vertical_line_count(es);
4125 new_y = pos * (es->line_count - vlc) / 100;
4126 dy = es->line_count ? (new_y - es->y_offset) : 0;
4127 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4128 es->line_count, es->y_offset, pos, dy);
4130 break;
4131 case SB_THUMBPOSITION:
4132 TRACE("SB_THUMBPOSITION %d\n", pos);
4133 es->flags &= ~EF_VSCROLL_TRACK;
4134 if(es->style & WS_VSCROLL)
4135 dy = pos - es->y_offset;
4136 else
4138 /* Assume default scroll range 0-100 */
4139 INT vlc, new_y;
4140 /* Sanity check */
4141 if(pos < 0 || pos > 100) return 0;
4142 vlc = get_vertical_line_count(es);
4143 new_y = pos * (es->line_count - vlc) / 100;
4144 dy = es->line_count ? (new_y - es->y_offset) : 0;
4145 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4146 es->line_count, es->y_offset, pos, dy);
4148 if (!dy)
4150 /* force scroll info update */
4151 EDIT_UpdateScrollInfo(es);
4152 notify_parent(es, EN_VSCROLL);
4154 break;
4155 case SB_ENDSCROLL:
4156 TRACE("SB_ENDSCROLL\n");
4157 break;
4159 * FIXME : the next two are undocumented !
4160 * Are we doing the right thing ?
4161 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4162 * although it's also a regular control message.
4164 case EM_GETTHUMB: /* this one is used by NT notepad */
4166 LRESULT ret;
4167 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4168 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4169 else
4171 /* Assume default scroll range 0-100 */
4172 INT vlc = get_vertical_line_count(es);
4173 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4175 TRACE("EM_GETTHUMB: returning %Id\n", ret);
4176 return ret;
4178 case EM_LINESCROLL:
4179 TRACE("EM_LINESCROLL %d\n", pos);
4180 dy = pos;
4181 break;
4183 default:
4184 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4185 action, action);
4186 return 0;
4188 if (dy)
4189 EDIT_EM_LineScroll(es, 0, dy);
4190 return 0;
4193 /*********************************************************************
4195 * EM_GETTHUMB
4197 * FIXME: is this right ? (or should it be only VSCROLL)
4198 * (and maybe only for edit controls that really have their
4199 * own scrollbars) (and maybe only for multiline controls ?)
4200 * All in all: very poorly documented
4203 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4205 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4206 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4209 /*********************************************************************
4211 * EM_SETCUEBANNER
4214 static BOOL EDIT_EM_SetCueBanner(EDITSTATE *es, BOOL draw_focused, const WCHAR *cue_text)
4216 if (es->style & ES_MULTILINE || !cue_text)
4217 return FALSE;
4219 Free(es->cue_banner_text);
4220 es->cue_banner_text = wcsdup(cue_text);
4221 es->cue_banner_draw_focused = draw_focused;
4223 return TRUE;
4226 /*********************************************************************
4228 * EM_GETCUEBANNER
4231 static BOOL EDIT_EM_GetCueBanner(EDITSTATE *es, WCHAR *buf, DWORD size)
4233 if (es->style & ES_MULTILINE)
4234 return FALSE;
4236 if (!es->cue_banner_text)
4238 if (buf && size)
4239 *buf = 0;
4240 return FALSE;
4242 else
4244 if (buf)
4245 lstrcpynW(buf, es->cue_banner_text, size);
4246 return TRUE;
4251 /********************************************************************
4253 * The Following code is to handle inline editing from IMEs
4256 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4258 LONG buflen;
4259 LPWSTR lpResultStr;
4261 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4262 if (buflen <= 0)
4264 return;
4267 lpResultStr = Alloc(buflen);
4268 if (!lpResultStr)
4270 ERR("Unable to alloc buffer for IME string\n");
4271 return;
4274 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4275 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, buflen / sizeof(WCHAR), TRUE, TRUE);
4276 Free(lpResultStr);
4279 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4281 HIMC hIMC;
4283 hIMC = ImmGetContext(hwnd);
4284 if (!hIMC)
4285 return;
4287 if (CompFlag & GCS_RESULTSTR)
4288 EDIT_GetResultStr(hIMC, es);
4290 ImmReleaseContext(hwnd, hIMC);
4294 /*********************************************************************
4296 * WM_NCCREATE
4298 * See also EDIT_WM_StyleChanged
4300 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
4302 EDITSTATE *es;
4303 UINT alloc_size;
4305 TRACE("Creating edit control, style = %#lx\n", lpcs->style);
4307 if (!(es = Alloc(sizeof(*es))))
4308 return FALSE;
4309 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4312 * Note: since the EDITSTATE has not been fully initialized yet,
4313 * we can't use any API calls that may send
4314 * WM_XXX messages before WM_NCCREATE is completed.
4317 es->style = lpcs->style;
4319 es->bEnableState = !(es->style & WS_DISABLED);
4321 es->hwndSelf = hwnd;
4322 /* Save parent, which will be notified by EN_* messages */
4323 es->hwndParent = lpcs->hwndParent;
4325 if (es->style & ES_COMBO)
4326 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4328 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4329 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4331 /* Number overrides lowercase overrides uppercase (at least it
4332 * does in Win95). However I'll bet that ES_NUMBER would be
4333 * invalid under Win 3.1.
4335 if (es->style & ES_NUMBER) {
4336 ; /* do not override the ES_NUMBER */
4337 } else if (es->style & ES_LOWERCASE) {
4338 es->style &= ~ES_UPPERCASE;
4340 if (es->style & ES_MULTILINE) {
4341 es->buffer_limit = BUFLIMIT_INITIAL;
4342 if (es->style & WS_VSCROLL)
4343 es->style |= ES_AUTOVSCROLL;
4344 if (es->style & WS_HSCROLL)
4345 es->style |= ES_AUTOHSCROLL;
4346 es->style &= ~ES_PASSWORD;
4347 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4348 /* Confirmed - RIGHT overrides CENTER */
4349 if (es->style & ES_RIGHT)
4350 es->style &= ~ES_CENTER;
4351 es->style &= ~WS_HSCROLL;
4352 es->style &= ~ES_AUTOHSCROLL;
4354 } else {
4355 es->buffer_limit = BUFLIMIT_INITIAL;
4356 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4357 es->style &= ~ES_CENTER;
4358 es->style &= ~WS_HSCROLL;
4359 es->style &= ~WS_VSCROLL;
4360 if (es->style & ES_PASSWORD)
4361 es->password_char = '*';
4364 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4365 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4366 goto cleanup;
4367 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4369 if (!(es->undo_text = Alloc((es->buffer_size + 1) * sizeof(WCHAR))))
4370 goto cleanup;
4371 es->undo_buffer_size = es->buffer_size;
4373 if (es->style & ES_MULTILINE)
4374 if (!(es->first_line_def = Alloc(sizeof(LINEDEF))))
4375 goto cleanup;
4376 es->line_count = 1;
4379 * In Win95 look and feel, the WS_BORDER style is replaced by the
4380 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4381 * control a nonclient area so we don't need to draw the border.
4382 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4383 * a nonclient area and we should handle painting the border ourselves.
4385 * When making modifications please ensure that the code still works
4386 * for edit controls created directly with style 0x50800000, exStyle 0
4387 * (which should have a single pixel border)
4389 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4390 es->style &= ~WS_BORDER;
4391 else if (es->style & WS_BORDER)
4392 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4394 return TRUE;
4396 cleanup:
4397 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4398 EDIT_InvalidateUniscribeData(es);
4399 Free(es->first_line_def);
4400 Free(es->undo_text);
4401 if (es->hloc32W) LocalFree(es->hloc32W);
4402 Free(es->logAttr);
4403 Free(es);
4404 return FALSE;
4408 /*********************************************************************
4410 * WM_CREATE
4413 static LRESULT EDIT_WM_Create(EDITSTATE *es, const WCHAR *name)
4415 RECT clientRect;
4417 TRACE("%s\n", debugstr_w(name));
4420 * To initialize some final structure members, we call some helper
4421 * functions. However, since the EDITSTATE is not consistent (i.e.
4422 * not fully initialized), we should be very careful which
4423 * functions can be called, and in what order.
4425 EDIT_WM_SetFont(es, 0, FALSE);
4426 EDIT_EM_EmptyUndoBuffer(es);
4428 /* We need to calculate the format rect
4429 (applications may send EM_SETMARGINS before the control gets visible) */
4430 GetClientRect(es->hwndSelf, &clientRect);
4431 EDIT_SetRectNP(es, &clientRect);
4433 if (name && *name)
4435 EDIT_EM_ReplaceSel(es, FALSE, name, lstrlenW(name), FALSE, FALSE);
4436 /* if we insert text to the editline, the text scrolls out
4437 * of the window, as the caret is placed after the insert
4438 * pos normally; thus we reset es->selection... to 0 and
4439 * update caret
4441 es->selection_start = es->selection_end = 0;
4442 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4443 * Messages are only to be sent when the USER does something to
4444 * change the contents. So I am removing this EN_CHANGE
4446 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4448 EDIT_EM_ScrollCaret(es);
4451 /* force scroll info update */
4452 EDIT_UpdateScrollInfo(es);
4453 OpenThemeData(es->hwndSelf, WC_EDITW);
4455 /* The rule seems to return 1 here for success */
4456 /* Power Builder masked edit controls will crash */
4457 /* if not. */
4458 /* FIXME: is that in all cases so ? */
4459 return 1;
4463 /*********************************************************************
4465 * WM_NCDESTROY
4468 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4470 LINEDEF *pc, *pp;
4471 HTHEME theme;
4473 theme = GetWindowTheme(es->hwndSelf);
4474 CloseThemeData(theme);
4476 /* The app can own the text buffer handle */
4477 if (es->hloc32W && (es->hloc32W != es->hlocapp))
4478 LocalFree(es->hloc32W);
4480 EDIT_InvalidateUniscribeData(es);
4482 pc = es->first_line_def;
4483 while (pc)
4485 pp = pc->next;
4486 Free(pc);
4487 pc = pp;
4490 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4491 Free(es->undo_text);
4492 Free(es->cue_banner_text);
4493 Free(es);
4495 return 0;
4498 static LRESULT CALLBACK EDIT_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
4500 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW(hwnd, 0);
4501 LRESULT result = 0;
4502 RECT *rect;
4503 POINT pt;
4505 TRACE("hwnd %p, msg %#x, wparam %Ix, lparam %Ix\n", hwnd, msg, wParam, lParam);
4507 if (!es && msg != WM_NCCREATE)
4508 return DefWindowProcW(hwnd, msg, wParam, lParam);
4510 if (es && (msg != WM_NCDESTROY))
4511 EDIT_LockBuffer(es);
4513 switch (msg)
4515 case EM_GETSEL:
4516 result = EDIT_EM_GetSel(es, (UINT *)wParam, (UINT *)lParam);
4517 break;
4519 case EM_SETSEL:
4520 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4521 EDIT_EM_ScrollCaret(es);
4522 result = 1;
4523 break;
4525 case EM_GETRECT:
4526 rect = (RECT *)lParam;
4527 if (rect)
4528 *rect = es->format_rect;
4529 break;
4531 case EM_SETRECT:
4532 if ((es->style & ES_MULTILINE) && lParam)
4534 EDIT_SetRectNP(es, (RECT *)lParam);
4535 EDIT_UpdateText(es, NULL, TRUE);
4537 break;
4539 case EM_SETRECTNP:
4540 if ((es->style & ES_MULTILINE) && lParam)
4541 EDIT_SetRectNP(es, (LPRECT)lParam);
4542 break;
4544 case EM_SCROLL:
4545 result = EDIT_EM_Scroll(es, (INT)wParam);
4546 break;
4548 case EM_LINESCROLL:
4549 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4550 break;
4552 case EM_SCROLLCARET:
4553 EDIT_EM_ScrollCaret(es);
4554 result = 1;
4555 break;
4557 case EM_GETMODIFY:
4558 result = ((es->flags & EF_MODIFIED) != 0);
4559 break;
4561 case EM_SETMODIFY:
4562 if (wParam)
4563 es->flags |= EF_MODIFIED;
4564 else
4565 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4566 break;
4568 case EM_GETLINECOUNT:
4569 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4570 break;
4572 case EM_LINEINDEX:
4573 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4574 break;
4576 case EM_SETHANDLE:
4577 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4578 break;
4580 case EM_GETHANDLE:
4581 result = (LRESULT)EDIT_EM_GetHandle(es);
4582 break;
4584 case EM_GETTHUMB:
4585 result = EDIT_EM_GetThumb(es);
4586 break;
4588 /* these messages missing from specs */
4589 case 0x00bf:
4590 case 0x00c0:
4591 case 0x00c3:
4592 case 0x00ca:
4593 FIXME("undocumented message 0x%x, please report\n", msg);
4594 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4595 break;
4597 case EM_LINELENGTH:
4598 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4599 break;
4601 case EM_REPLACESEL:
4603 const WCHAR *textW = (const WCHAR *)lParam;
4605 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, lstrlenW(textW), TRUE, TRUE);
4606 result = 1;
4607 break;
4610 case EM_GETLINE:
4611 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam);
4612 break;
4614 case EM_SETLIMITTEXT:
4615 EDIT_EM_SetLimitText(es, wParam);
4616 break;
4618 case EM_CANUNDO:
4619 result = (LRESULT)EDIT_EM_CanUndo(es);
4620 break;
4622 case EM_UNDO:
4623 case WM_UNDO:
4624 result = (LRESULT)EDIT_EM_Undo(es);
4625 break;
4627 case EM_FMTLINES:
4628 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4629 break;
4631 case EM_LINEFROMCHAR:
4632 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4633 break;
4635 case EM_SETTABSTOPS:
4636 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4637 break;
4639 case EM_SETPASSWORDCHAR:
4640 EDIT_EM_SetPasswordChar(es, wParam);
4641 break;
4643 case EM_EMPTYUNDOBUFFER:
4644 EDIT_EM_EmptyUndoBuffer(es);
4645 break;
4647 case EM_GETFIRSTVISIBLELINE:
4648 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4649 break;
4651 case EM_SETREADONLY:
4653 DWORD old_style = es->style;
4655 if (wParam)
4657 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) | ES_READONLY);
4658 es->style |= ES_READONLY;
4660 else
4662 SetWindowLongW(hwnd, GWL_STYLE, GetWindowLongW(hwnd, GWL_STYLE) & ~ES_READONLY);
4663 es->style &= ~ES_READONLY;
4666 if (old_style ^ es->style)
4667 InvalidateRect(es->hwndSelf, NULL, TRUE);
4669 result = 1;
4670 break;
4673 case EM_SETWORDBREAKPROC:
4674 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4675 result = 1;
4676 break;
4678 case EM_GETWORDBREAKPROC:
4679 result = (LRESULT)es->word_break_proc;
4680 break;
4682 case EM_GETPASSWORDCHAR:
4683 result = es->password_char;
4684 break;
4686 case EM_SETMARGINS:
4687 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4688 break;
4690 case EM_GETMARGINS:
4691 result = MAKELONG(es->left_margin, es->right_margin);
4692 break;
4694 case EM_GETLIMITTEXT:
4695 result = es->buffer_limit;
4696 break;
4698 case EM_POSFROMCHAR:
4699 if ((INT)wParam >= get_text_length(es)) result = -1;
4700 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4701 break;
4703 case EM_CHARFROMPOS:
4704 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4705 break;
4707 case EM_SETCUEBANNER:
4708 result = EDIT_EM_SetCueBanner(es, (BOOL)wParam, (const WCHAR *)lParam);
4709 break;
4711 case EM_GETCUEBANNER:
4712 result = EDIT_EM_GetCueBanner(es, (WCHAR *)wParam, (DWORD)lParam);
4713 break;
4715 case EM_SETIMESTATUS:
4716 if (wParam == EMSIS_COMPOSITIONSTRING)
4717 es->ime_status = lParam & 0xFFFF;
4719 result = 1;
4720 break;
4722 case EM_GETIMESTATUS:
4723 result = wParam == EMSIS_COMPOSITIONSTRING ? es->ime_status : 1;
4724 break;
4726 /* End of the EM_ messages which were in numerical order; what order
4727 * are these in? vaguely alphabetical?
4730 case WM_NCCREATE:
4731 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
4732 break;
4734 case WM_NCDESTROY:
4735 result = EDIT_WM_NCDestroy(es);
4736 es = NULL;
4737 break;
4739 case WM_GETDLGCODE:
4740 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4742 if (es->style & ES_MULTILINE)
4743 result |= DLGC_WANTALLKEYS;
4745 if (lParam)
4747 MSG *msg = (MSG *)lParam;
4748 es->flags |= EF_DIALOGMODE;
4750 if (msg->message == WM_KEYDOWN)
4752 int vk = (int)msg->wParam;
4754 if (es->hwndListBox)
4756 if (vk == VK_RETURN || vk == VK_ESCAPE)
4757 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4758 result |= DLGC_WANTMESSAGE;
4762 break;
4764 case WM_CHAR:
4766 WCHAR charW = wParam;
4768 if (es->hwndListBox)
4770 if (charW == VK_RETURN || charW == VK_ESCAPE)
4772 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4773 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4774 break;
4777 result = EDIT_WM_Char(es, charW);
4778 break;
4781 case WM_UNICHAR:
4782 if (wParam == UNICODE_NOCHAR) return TRUE;
4783 if (wParam <= 0x000fffff)
4785 if (wParam > 0xffff) /* convert to surrogates */
4787 wParam -= 0x10000;
4788 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4789 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4791 else
4792 EDIT_WM_Char(es, wParam);
4794 return 0;
4796 case WM_CLEAR:
4797 EDIT_WM_Clear(es);
4798 break;
4800 case WM_CONTEXTMENU:
4801 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4802 break;
4804 case WM_COPY:
4805 EDIT_WM_Copy(es);
4806 break;
4808 case WM_CREATE:
4809 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4810 break;
4812 case WM_CUT:
4813 EDIT_WM_Cut(es);
4814 break;
4816 case WM_ENABLE:
4817 es->bEnableState = (BOOL) wParam;
4818 EDIT_UpdateText(es, NULL, TRUE);
4819 if (GetWindowTheme(hwnd))
4820 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
4821 break;
4823 case WM_ERASEBKGND:
4824 /* we do the proper erase in EDIT_WM_Paint */
4825 result = 1;
4826 break;
4828 case WM_GETFONT:
4829 result = (LRESULT)es->font;
4830 break;
4832 case WM_GETTEXT:
4833 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam);
4834 break;
4836 case WM_GETTEXTLENGTH:
4837 result = get_text_length(es);
4838 break;
4840 case WM_HSCROLL:
4841 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4842 break;
4844 case WM_KEYDOWN:
4845 result = EDIT_WM_KeyDown(es, (INT)wParam);
4846 break;
4848 case WM_KILLFOCUS:
4849 result = EDIT_WM_KillFocus(GetWindowTheme(hwnd), es);
4850 break;
4852 case WM_LBUTTONDBLCLK:
4853 result = EDIT_WM_LButtonDblClk(es);
4854 break;
4856 case WM_LBUTTONDOWN:
4857 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4858 break;
4860 case WM_LBUTTONUP:
4861 result = EDIT_WM_LButtonUp(es);
4862 break;
4864 case WM_MBUTTONDOWN:
4865 result = EDIT_WM_MButtonDown(es);
4866 break;
4868 case WM_MOUSEMOVE:
4869 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4870 break;
4872 case WM_PRINTCLIENT:
4873 case WM_PAINT:
4874 EDIT_WM_Paint(es, (HDC)wParam);
4875 break;
4877 case WM_NCPAINT:
4878 EDIT_WM_NCPaint(hwnd, (HRGN)wParam);
4879 break;
4881 case WM_PASTE:
4882 EDIT_WM_Paste(es);
4883 break;
4885 case WM_SETFOCUS:
4886 EDIT_WM_SetFocus(GetWindowTheme(hwnd), es);
4887 break;
4889 case WM_SETFONT:
4890 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4891 break;
4893 case WM_SETREDRAW:
4894 /* FIXME: actually set an internal flag and behave accordingly */
4895 break;
4897 case WM_SETTEXT:
4898 EDIT_WM_SetText(es, (const WCHAR *)lParam);
4899 result = TRUE;
4900 break;
4902 case WM_SIZE:
4903 EDIT_WM_Size(es, (UINT)wParam);
4904 break;
4906 case WM_STYLECHANGED:
4907 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4908 break;
4910 case WM_STYLECHANGING:
4911 result = 0; /* See EDIT_WM_StyleChanged */
4912 break;
4914 case WM_SYSKEYDOWN:
4915 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4916 break;
4918 case WM_TIMER:
4919 EDIT_WM_Timer(es);
4920 break;
4922 case WM_VSCROLL:
4923 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4924 break;
4926 case WM_MOUSEWHEEL:
4928 int wheelDelta;
4929 INT pulScrollLines = 3;
4930 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4932 if (wParam & (MK_SHIFT | MK_CONTROL))
4934 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4935 break;
4938 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4939 /* if scrolling changes direction, ignore left overs */
4940 if ((wheelDelta < 0 && es->wheelDeltaRemainder < 0) ||
4941 (wheelDelta > 0 && es->wheelDeltaRemainder > 0))
4942 es->wheelDeltaRemainder += wheelDelta;
4943 else
4944 es->wheelDeltaRemainder = wheelDelta;
4946 if (es->wheelDeltaRemainder && pulScrollLines)
4948 int cLineScroll;
4949 pulScrollLines = min(es->line_count, pulScrollLines);
4950 cLineScroll = pulScrollLines * es->wheelDeltaRemainder / WHEEL_DELTA;
4951 es->wheelDeltaRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
4952 result = EDIT_EM_LineScroll(es, 0, -cLineScroll);
4954 break;
4957 /* IME messages to make the edit control IME aware */
4958 case WM_IME_SETCONTEXT:
4959 GetCaretPos(&pt);
4960 EDIT_UpdateImmCompositionWindow(es, pt.x, pt.y);
4961 EDIT_UpdateImmCompositionFont(es);
4962 break;
4964 case WM_IME_COMPOSITION:
4965 EDIT_EM_ReplaceSel(es, TRUE, NULL, 0, TRUE, TRUE);
4966 if ((lParam & GCS_RESULTSTR) && (es->ime_status & EIMES_GETCOMPSTRATONCE))
4967 EDIT_ImeComposition(hwnd, lParam, es);
4968 return DefWindowProcW(hwnd, msg, wParam, lParam);
4970 case WM_IME_SELECT:
4971 break;
4973 case WM_IME_CONTROL:
4974 break;
4976 case WM_IME_REQUEST:
4977 switch (wParam)
4979 case IMR_QUERYCHARPOSITION:
4981 IMECHARPOSITION *chpos = (IMECHARPOSITION *)lParam;
4982 LRESULT pos;
4984 pos = EDIT_EM_PosFromChar(es, es->selection_start + chpos->dwCharPos, FALSE);
4985 chpos->pt.x = LOWORD(pos);
4986 chpos->pt.y = HIWORD(pos);
4987 chpos->cLineHeight = es->line_height;
4988 chpos->rcDocument = es->format_rect;
4989 MapWindowPoints(hwnd, 0, &chpos->pt, 1);
4990 MapWindowPoints(hwnd, 0, (POINT*)&chpos->rcDocument, 2);
4991 result = 1;
4992 break;
4995 break;
4997 case WM_THEMECHANGED:
4998 CloseThemeData(GetWindowTheme(hwnd));
4999 OpenThemeData(hwnd, WC_EDITW);
5000 InvalidateRect(hwnd, NULL, TRUE);
5001 break;
5003 default:
5004 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5005 break;
5008 if (IsWindow(hwnd) && es && msg != EM_GETHANDLE)
5009 EDIT_UnlockBuffer(es, FALSE);
5011 TRACE("hwnd=%p msg=%x -- %#Ix\n", hwnd, msg, result);
5013 return result;
5016 void EDIT_Register(void)
5018 WNDCLASSW wndClass;
5020 memset(&wndClass, 0, sizeof(wndClass));
5021 wndClass.style = CS_PARENTDC | CS_GLOBALCLASS | CS_DBLCLKS;
5022 wndClass.lpfnWndProc = EDIT_WindowProc;
5023 wndClass.cbClsExtra = 0;
5024 #ifdef __i386__
5025 wndClass.cbWndExtra = sizeof(EDITSTATE *) + sizeof(WORD);
5026 #else
5027 wndClass.cbWndExtra = sizeof(EDITSTATE *);
5028 #endif
5029 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_IBEAM);
5030 wndClass.hbrBackground = NULL;
5031 wndClass.lpszClassName = WC_EDITW;
5032 RegisterClassW(&wndClass);