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