mscms: Accept REG_DWORD values when enumerating registered color profiles.
[wine.git] / dlls / user32 / edit.c
blobee9fb3f4e1911985629fdeb27a11c78d83472d9e
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
45 #include "config.h"
47 #include <stdarg.h>
48 #include <string.h>
49 #include <stdlib.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winnt.h"
54 #include "wownt32.h"
55 #include "win.h"
56 #include "imm.h"
57 #include "wine/winbase16.h"
58 #include "wine/winuser16.h"
59 #include "wine/unicode.h"
60 #include "controls.h"
61 #include "user_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(edit);
65 WINE_DECLARE_DEBUG_CHANNEL(combo);
66 WINE_DECLARE_DEBUG_CHANNEL(relay);
68 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
69 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
70 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
71 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
74 * extra flags for EDITSTATE.flags field
76 #define EF_MODIFIED 0x0001 /* text has been modified */
77 #define EF_FOCUSED 0x0002 /* we have input focus */
78 #define EF_UPDATE 0x0004 /* notify parent of changed state */
79 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
80 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
81 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
82 wrapped line, instead of in front of the next character */
83 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
84 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
85 the text buffer if this is clear. */
86 typedef enum
88 END_0 = 0, /* line ends with terminating '\0' character */
89 END_WRAP, /* line is wrapped */
90 END_HARD, /* line ends with a hard return '\r\n' */
91 END_SOFT, /* line ends with a soft return '\r\r\n' */
92 END_RICH /* line ends with a single '\n' */
93 } LINE_END;
95 typedef struct tagLINEDEF {
96 INT length; /* bruto length of a line in bytes */
97 INT net_length; /* netto length of a line in visible characters */
98 LINE_END ending;
99 INT width; /* width of the line in pixels */
100 INT index; /* line index into the buffer */
101 struct tagLINEDEF *next;
102 } LINEDEF;
104 typedef struct
106 BOOL is_unicode; /* how the control was created */
107 LPWSTR text; /* the actual contents of the control */
108 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
109 UINT buffer_size; /* the size of the buffer in characters */
110 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
111 HFONT font; /* NULL means standard system font */
112 INT x_offset; /* scroll offset for multi lines this is in pixels
113 for single lines it's in characters */
114 INT line_height; /* height of a screen line in pixels */
115 INT char_width; /* average character width in pixels */
116 DWORD style; /* sane version of wnd->dwStyle */
117 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
118 INT undo_insert_count; /* number of characters inserted in sequence */
119 UINT undo_position; /* character index of the insertion and deletion */
120 LPWSTR undo_text; /* deleted text */
121 UINT undo_buffer_size; /* size of the deleted text buffer */
122 INT selection_start; /* == selection_end if no selection */
123 INT selection_end; /* == current caret position */
124 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
125 INT left_margin; /* in pixels */
126 INT right_margin; /* in pixels */
127 RECT format_rect;
128 INT text_width; /* width of the widest line in pixels for multi line controls
129 and just line width for single line controls */
130 INT region_posx; /* Position of cursor relative to region: */
131 INT region_posy; /* -1: to left, 0: within, 1: to right */
132 EDITWORDBREAKPROC16 word_break_proc16;
133 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
134 INT line_count; /* number of lines */
135 INT y_offset; /* scroll offset in number of lines */
136 BOOL bCaptureState; /* flag indicating whether mouse was captured */
137 BOOL bEnableState; /* flag keeping the enable state */
138 HWND hwndSelf; /* the our window handle */
139 HWND hwndParent; /* Handle of parent for sending EN_* messages.
140 Even if parent will change, EN_* messages
141 should be sent to the first parent. */
142 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
144 * only for multi line controls
146 INT lock_count; /* amount of re-entries in the EditWndProc */
147 INT tabs_count;
148 LPINT tabs;
149 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
150 HLOCAL hloc32W; /* our unicode local memory block */
151 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
152 or EM_SETHANDLE16 */
153 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
154 or EM_SETHANDLE */
156 * IME Data
158 UINT composition_len; /* length of composition, 0 == no composition */
159 int composition_start; /* the character position for the composition */
160 } EDITSTATE;
163 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
164 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
166 /* used for disabled or read-only edit control */
167 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
168 do \
169 { /* Notify parent which has created this edit control */ \
170 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
171 SendMessageW(es->hwndParent, WM_COMMAND, \
172 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
173 (LPARAM)(es->hwndSelf)); \
174 } while(0)
177 /*********************************************************************
179 * EM_CANUNDO
182 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
184 return (es->undo_insert_count || strlenW(es->undo_text));
188 /*********************************************************************
190 * EM_EMPTYUNDOBUFFER
193 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
195 es->undo_insert_count = 0;
196 *es->undo_text = '\0';
200 /**********************************************************************
201 * get_app_version
203 * Returns the window version in case Wine emulates a later version
204 * of windows than the application expects.
206 * In a number of cases when windows runs an application that was
207 * designed for an earlier windows version, windows reverts
208 * to "old" behaviour of that earlier version.
210 * An example is a disabled edit control that needs to be painted.
211 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
212 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
213 * applications with an expected version 0f 4.0 or higher.
216 static DWORD get_app_version(void)
218 static DWORD version;
219 if (!version)
221 DWORD dwEmulatedVersion;
222 OSVERSIONINFOW info;
223 DWORD dwProcVersion = GetProcessVersion(0);
225 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
226 GetVersionExW( &info );
227 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
228 /* FIXME: this may not be 100% correct; see discussion on the
229 * wine developer list in Nov 1999 */
230 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
232 return version;
235 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
237 HBRUSH hbrush;
238 UINT msg;
240 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
241 msg = WM_CTLCOLORSTATIC;
242 else
243 msg = WM_CTLCOLOREDIT;
245 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
246 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
247 if (!hbrush)
248 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
249 return hbrush;
253 /*********************************************************************
255 * EDIT_WordBreakProc
257 * Find the beginning of words.
258 * Note: unlike the specs for a WordBreakProc, this function only
259 * allows to be called without linebreaks between s[0] up to
260 * s[count - 1]. Remember it is only called
261 * internally, so we can decide this for ourselves.
264 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
266 INT ret = 0;
268 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
270 if(!s) return 0;
272 switch (action) {
273 case WB_LEFT:
274 if (!count)
275 break;
276 if (index)
277 index--;
278 if (s[index] == ' ') {
279 while (index && (s[index] == ' '))
280 index--;
281 if (index) {
282 while (index && (s[index] != ' '))
283 index--;
284 if (s[index] == ' ')
285 index++;
287 } else {
288 while (index && (s[index] != ' '))
289 index--;
290 if (s[index] == ' ')
291 index++;
293 ret = index;
294 break;
295 case WB_RIGHT:
296 if (!count)
297 break;
298 if (index)
299 index--;
300 if (s[index] == ' ')
301 while ((index < count) && (s[index] == ' ')) index++;
302 else {
303 while (s[index] && (s[index] != ' ') && (index < count))
304 index++;
305 while ((s[index] == ' ') && (index < count)) index++;
307 ret = index;
308 break;
309 case WB_ISDELIMITER:
310 ret = (s[index] == ' ');
311 break;
312 default:
313 ERR("unknown action code, please report !\n");
314 break;
316 return ret;
320 /*********************************************************************
322 * EDIT_CallWordBreakProc
324 * Call appropriate WordBreakProc (internal or external).
326 * Note: The "start" argument should always be an index referring
327 * to es->text. The actual wordbreak proc might be
328 * 16 bit, so we can't always pass any 32 bit LPSTR.
329 * Hence we assume that es->text is the buffer that holds
330 * the string under examination (we can decide this for ourselves).
333 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
335 INT ret;
337 if (es->word_break_proc16) {
338 HGLOBAL16 hglob16;
339 SEGPTR segptr;
340 INT countA;
341 WORD args[5];
342 DWORD result;
344 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
345 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
346 segptr = WOWGlobalLock16(hglob16);
347 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
348 args[4] = SELECTOROF(segptr);
349 args[3] = OFFSETOF(segptr);
350 args[2] = index;
351 args[1] = countA;
352 args[0] = action;
353 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
354 ret = LOWORD(result);
355 GlobalUnlock16(hglob16);
356 GlobalFree16(hglob16);
358 else if (es->word_break_proc)
360 if(es->is_unicode)
362 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
364 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
365 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
366 ret = wbpW(es->text + start, index, count, action);
368 else
370 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
371 INT countA;
372 CHAR *textA;
374 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
375 textA = HeapAlloc(GetProcessHeap(), 0, countA);
376 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
377 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
378 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
379 ret = wbpA(textA, index, countA, action);
380 HeapFree(GetProcessHeap(), 0, textA);
383 else
384 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
386 return ret;
389 /*********************************************************************
391 * EDIT_BuildLineDefs_ML
393 * Build linked list of text lines.
394 * Lines can end with '\0' (last line), a character (if it is wrapped),
395 * a soft return '\r\r\n' or a hard return '\r\n'
398 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
400 HDC dc;
401 HFONT old_font = 0;
402 LPWSTR current_position, cp;
403 INT fw;
404 LINEDEF *current_line;
405 LINEDEF *previous_line;
406 LINEDEF *start_line;
407 INT line_index = 0, nstart_line = 0, nstart_index = 0;
408 INT line_count = es->line_count;
409 INT orig_net_length;
410 RECT rc;
412 if (istart == iend && delta == 0)
413 return;
415 dc = GetDC(es->hwndSelf);
416 if (es->font)
417 old_font = SelectObject(dc, es->font);
419 previous_line = NULL;
420 current_line = es->first_line_def;
422 /* Find starting line. istart must lie inside an existing line or
423 * at the end of buffer */
424 do {
425 if (istart < current_line->index + current_line->length ||
426 current_line->ending == END_0)
427 break;
429 previous_line = current_line;
430 current_line = current_line->next;
431 line_index++;
432 } while (current_line);
434 if (!current_line) /* Error occurred start is not inside previous buffer */
436 FIXME(" modification occurred outside buffer\n");
437 ReleaseDC(es->hwndSelf, dc);
438 return;
441 /* Remember start of modifications in order to calculate update region */
442 nstart_line = line_index;
443 nstart_index = current_line->index;
445 /* We must start to reformat from the previous line since the modifications
446 * may have caused the line to wrap upwards. */
447 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
449 line_index--;
450 current_line = previous_line;
452 start_line = current_line;
454 fw = es->format_rect.right - es->format_rect.left;
455 current_position = es->text + current_line->index;
456 do {
457 if (current_line != start_line)
459 if (!current_line || current_line->index + delta > current_position - es->text)
461 /* The buffer has been expanded, create a new line and
462 insert it into the link list */
463 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
464 new_line->next = previous_line->next;
465 previous_line->next = new_line;
466 current_line = new_line;
467 es->line_count++;
469 else if (current_line->index + delta < current_position - es->text)
471 /* The previous line merged with this line so we delete this extra entry */
472 previous_line->next = current_line->next;
473 HeapFree(GetProcessHeap(), 0, current_line);
474 current_line = previous_line->next;
475 es->line_count--;
476 continue;
478 else /* current_line->index + delta == current_position */
480 if (current_position - es->text > iend)
481 break; /* We reached end of line modifications */
482 /* else recalculate this line */
486 current_line->index = current_position - es->text;
487 orig_net_length = current_line->net_length;
489 /* Find end of line */
490 cp = current_position;
491 while (*cp) {
492 if (*cp == '\n') break;
493 if ((*cp == '\r') && (*(cp + 1) == '\n'))
494 break;
495 cp++;
498 /* Mark type of line termination */
499 if (!(*cp)) {
500 current_line->ending = END_0;
501 current_line->net_length = strlenW(current_position);
502 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
503 current_line->ending = END_SOFT;
504 current_line->net_length = cp - current_position - 1;
505 } else if (*cp == '\n') {
506 current_line->ending = END_RICH;
507 current_line->net_length = cp - current_position;
508 } else {
509 current_line->ending = END_HARD;
510 current_line->net_length = cp - current_position;
513 /* Calculate line width */
514 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
515 current_position, current_line->net_length,
516 es->tabs_count, es->tabs));
518 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
519 if (!(es->style & ES_AUTOHSCROLL)) {
520 if (current_line->width > fw) {
521 INT next = 0;
522 INT prev;
523 do {
524 prev = next;
525 next = EDIT_CallWordBreakProc(es, current_position - es->text,
526 prev + 1, current_line->net_length, WB_RIGHT);
527 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
528 current_position, next, es->tabs_count, es->tabs));
529 } while (current_line->width <= fw);
530 if (!prev) { /* Didn't find a line break so force a break */
531 next = 0;
532 do {
533 prev = next;
534 next++;
535 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
536 current_position, next, es->tabs_count, es->tabs));
537 } while (current_line->width <= fw);
538 if (!prev)
539 prev = 1;
542 /* If the first line we are calculating, wrapped before istart, we must
543 * adjust istart in order for this to be reflected in the update region. */
544 if (current_line->index == nstart_index && istart > current_line->index + prev)
545 istart = current_line->index + prev;
546 /* else if we are updating the previous line before the first line we
547 * are re-calculating and it expanded */
548 else if (current_line == start_line &&
549 current_line->index != nstart_index && orig_net_length < prev)
551 /* Line expanded due to an upwards line wrap so we must partially include
552 * previous line in update region */
553 nstart_line = line_index;
554 nstart_index = current_line->index;
555 istart = current_line->index + orig_net_length;
558 current_line->net_length = prev;
559 current_line->ending = END_WRAP;
560 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
561 current_line->net_length, es->tabs_count, es->tabs));
563 else if (current_line == start_line &&
564 current_line->index != nstart_index &&
565 orig_net_length < current_line->net_length) {
566 /* The previous line expanded but it's still not as wide as the client rect */
567 /* The expansion is due to an upwards line wrap so we must partially include
568 it in the update region */
569 nstart_line = line_index;
570 nstart_index = current_line->index;
571 istart = current_line->index + orig_net_length;
576 /* Adjust length to include line termination */
577 switch (current_line->ending) {
578 case END_SOFT:
579 current_line->length = current_line->net_length + 3;
580 break;
581 case END_RICH:
582 current_line->length = current_line->net_length + 1;
583 break;
584 case END_HARD:
585 current_line->length = current_line->net_length + 2;
586 break;
587 case END_WRAP:
588 case END_0:
589 current_line->length = current_line->net_length;
590 break;
592 es->text_width = max(es->text_width, current_line->width);
593 current_position += current_line->length;
594 previous_line = current_line;
595 current_line = current_line->next;
596 line_index++;
597 } while (previous_line->ending != END_0);
599 /* Finish adjusting line indexes by delta or remove hanging lines */
600 if (previous_line->ending == END_0)
602 LINEDEF *pnext = NULL;
604 previous_line->next = NULL;
605 while (current_line)
607 pnext = current_line->next;
608 HeapFree(GetProcessHeap(), 0, current_line);
609 current_line = pnext;
610 es->line_count--;
613 else if (delta != 0)
615 while (current_line)
617 current_line->index += delta;
618 current_line = current_line->next;
622 /* Calculate rest of modification rectangle */
623 if (hrgn)
625 HRGN tmphrgn;
627 * We calculate two rectangles. One for the first line which may have
628 * an indent with respect to the format rect. The other is a format-width
629 * rectangle that spans the rest of the lines that changed or moved.
631 rc.top = es->format_rect.top + nstart_line * es->line_height -
632 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
633 rc.bottom = rc.top + es->line_height;
634 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
635 rc.left = es->format_rect.left;
636 else
637 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
638 es->text + nstart_index, istart - nstart_index,
639 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
640 rc.right = es->format_rect.right;
641 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
643 rc.top = rc.bottom;
644 rc.left = es->format_rect.left;
645 rc.right = es->format_rect.right;
647 * If lines were added or removed we must re-paint the remainder of the
648 * lines since the remaining lines were either shifted up or down.
650 if (line_count < es->line_count) /* We added lines */
651 rc.bottom = es->line_count * es->line_height;
652 else if (line_count > es->line_count) /* We removed lines */
653 rc.bottom = line_count * es->line_height;
654 else
655 rc.bottom = line_index * es->line_height;
656 rc.bottom += es->format_rect.top;
657 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
658 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
659 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
660 DeleteObject(tmphrgn);
663 if (es->font)
664 SelectObject(dc, old_font);
666 ReleaseDC(es->hwndSelf, dc);
670 static inline UINT get_text_length(EDITSTATE *es)
672 if(es->text_length == (UINT)-1)
673 es->text_length = strlenW(es->text);
674 return es->text_length;
677 /*********************************************************************
679 * EDIT_GetPasswordPointer_SL
681 * note: caller should free the (optionally) allocated buffer
684 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
686 if (es->style & ES_PASSWORD) {
687 INT len = get_text_length(es);
688 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
689 text[len] = '\0';
690 while(len) text[--len] = es->password_char;
691 return text;
692 } else
693 return es->text;
697 /*********************************************************************
699 * EDIT_CalcLineWidth_SL
702 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
704 SIZE size;
705 LPWSTR text;
706 HDC dc;
707 HFONT old_font = 0;
709 text = EDIT_GetPasswordPointer_SL(es);
711 dc = GetDC(es->hwndSelf);
712 if (es->font)
713 old_font = SelectObject(dc, es->font);
715 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
717 if (es->font)
718 SelectObject(dc, old_font);
719 ReleaseDC(es->hwndSelf, dc);
721 if (es->style & ES_PASSWORD)
722 HeapFree(GetProcessHeap(), 0, text);
724 es->text_width = size.cx;
727 /*********************************************************************
729 * EDIT_CharFromPos
731 * Beware: This is not the function called on EM_CHARFROMPOS
732 * The position _can_ be outside the formatting / client
733 * rectangle
734 * The return value is only the character index
737 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
739 INT index;
740 HDC dc;
741 HFONT old_font = 0;
742 INT x_high = 0, x_low = 0;
744 if (es->style & ES_MULTILINE) {
745 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
746 INT line_index = 0;
747 LINEDEF *line_def = es->first_line_def;
748 INT low, high;
749 while ((line > 0) && line_def->next) {
750 line_index += line_def->length;
751 line_def = line_def->next;
752 line--;
754 x += es->x_offset - es->format_rect.left;
755 if (es->style & ES_RIGHT)
756 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
757 else if (es->style & ES_CENTER)
758 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
759 if (x >= line_def->width) {
760 if (after_wrap)
761 *after_wrap = (line_def->ending == END_WRAP);
762 return line_index + line_def->net_length;
764 if (x <= 0) {
765 if (after_wrap)
766 *after_wrap = FALSE;
767 return line_index;
769 dc = GetDC(es->hwndSelf);
770 if (es->font)
771 old_font = SelectObject(dc, es->font);
772 low = line_index;
773 high = line_index + line_def->net_length + 1;
774 while (low < high - 1)
776 INT mid = (low + high) / 2;
777 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
778 if (x_now > x) {
779 high = mid;
780 x_high = x_now;
781 } else {
782 low = mid;
783 x_low = x_now;
786 if (abs(x_high - x) + 1 <= abs(x_low - x))
787 index = high;
788 else
789 index = low;
791 if (after_wrap)
792 *after_wrap = ((index == line_index + line_def->net_length) &&
793 (line_def->ending == END_WRAP));
794 } else {
795 LPWSTR text;
796 SIZE size;
797 if (after_wrap)
798 *after_wrap = FALSE;
799 x -= es->format_rect.left;
800 if (!x)
801 return es->x_offset;
803 if (!es->x_offset)
805 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
806 if (es->style & ES_RIGHT)
807 x -= indent;
808 else if (es->style & ES_CENTER)
809 x -= indent / 2;
812 text = EDIT_GetPasswordPointer_SL(es);
813 dc = GetDC(es->hwndSelf);
814 if (es->font)
815 old_font = SelectObject(dc, es->font);
816 if (x < 0)
818 INT low = 0;
819 INT high = es->x_offset;
820 while (low < high - 1)
822 INT mid = (low + high) / 2;
823 GetTextExtentPoint32W( dc, text + mid,
824 es->x_offset - mid, &size );
825 if (size.cx > -x) {
826 low = mid;
827 x_low = size.cx;
828 } else {
829 high = mid;
830 x_high = size.cx;
833 if (abs(x_high + x) <= abs(x_low + x) + 1)
834 index = high;
835 else
836 index = low;
838 else
840 INT low = es->x_offset;
841 INT high = get_text_length(es) + 1;
842 while (low < high - 1)
844 INT mid = (low + high) / 2;
845 GetTextExtentPoint32W( dc, text + es->x_offset,
846 mid - es->x_offset, &size );
847 if (size.cx > x) {
848 high = mid;
849 x_high = size.cx;
850 } else {
851 low = mid;
852 x_low = size.cx;
855 if (abs(x_high - x) <= abs(x_low - x) + 1)
856 index = high;
857 else
858 index = low;
860 if (es->style & ES_PASSWORD)
861 HeapFree(GetProcessHeap(), 0, text);
863 if (es->font)
864 SelectObject(dc, old_font);
865 ReleaseDC(es->hwndSelf, dc);
866 return index;
870 /*********************************************************************
872 * EDIT_ConfinePoint
874 * adjusts the point to be within the formatting rectangle
875 * (so CharFromPos returns the nearest _visible_ character)
878 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
880 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
881 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
885 /*********************************************************************
887 * EM_LINEFROMCHAR
890 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
892 INT line;
893 LINEDEF *line_def;
895 if (!(es->style & ES_MULTILINE))
896 return 0;
897 if (index > (INT)get_text_length(es))
898 return es->line_count - 1;
899 if (index == -1)
900 index = min(es->selection_start, es->selection_end);
902 line = 0;
903 line_def = es->first_line_def;
904 index -= line_def->length;
905 while ((index >= 0) && line_def->next) {
906 line++;
907 line_def = line_def->next;
908 index -= line_def->length;
910 return line;
914 /*********************************************************************
916 * EM_LINEINDEX
919 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
921 INT line_index;
922 const LINEDEF *line_def;
924 if (!(es->style & ES_MULTILINE))
925 return 0;
926 if (line >= es->line_count)
927 return -1;
929 line_index = 0;
930 line_def = es->first_line_def;
931 if (line == -1) {
932 INT index = es->selection_end - line_def->length;
933 while ((index >= 0) && line_def->next) {
934 line_index += line_def->length;
935 line_def = line_def->next;
936 index -= line_def->length;
938 } else {
939 while (line > 0) {
940 line_index += line_def->length;
941 line_def = line_def->next;
942 line--;
945 return line_index;
949 /*********************************************************************
951 * EM_LINELENGTH
954 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
956 LINEDEF *line_def;
958 if (!(es->style & ES_MULTILINE))
959 return get_text_length(es);
961 if (index == -1) {
962 /* get the number of remaining non-selected chars of selected lines */
963 INT32 l; /* line number */
964 INT32 li; /* index of first char in line */
965 INT32 count;
966 l = EDIT_EM_LineFromChar(es, es->selection_start);
967 /* # chars before start of selection area */
968 count = es->selection_start - EDIT_EM_LineIndex(es, l);
969 l = EDIT_EM_LineFromChar(es, es->selection_end);
970 /* # chars after end of selection */
971 li = EDIT_EM_LineIndex(es, l);
972 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
973 return count;
975 line_def = es->first_line_def;
976 index -= line_def->length;
977 while ((index >= 0) && line_def->next) {
978 line_def = line_def->next;
979 index -= line_def->length;
981 return line_def->net_length;
985 /*********************************************************************
987 * EM_POSFROMCHAR
990 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
992 INT len = get_text_length(es);
993 INT l;
994 INT li;
995 INT x;
996 INT y = 0;
997 INT w;
998 INT lw = 0;
999 INT ll = 0;
1000 HDC dc;
1001 HFONT old_font = 0;
1002 SIZE size;
1003 LINEDEF *line_def;
1005 index = min(index, len);
1006 dc = GetDC(es->hwndSelf);
1007 if (es->font)
1008 old_font = SelectObject(dc, es->font);
1009 if (es->style & ES_MULTILINE) {
1010 l = EDIT_EM_LineFromChar(es, index);
1011 y = (l - es->y_offset) * es->line_height;
1012 li = EDIT_EM_LineIndex(es, l);
1013 if (after_wrap && (li == index) && l) {
1014 INT l2 = l - 1;
1015 line_def = es->first_line_def;
1016 while (l2) {
1017 line_def = line_def->next;
1018 l2--;
1020 if (line_def->ending == END_WRAP) {
1021 l--;
1022 y -= es->line_height;
1023 li = EDIT_EM_LineIndex(es, l);
1027 line_def = es->first_line_def;
1028 while (line_def->index != li)
1029 line_def = line_def->next;
1031 ll = line_def->net_length;
1032 lw = line_def->width;
1034 w = es->format_rect.right - es->format_rect.left;
1035 if (es->style & ES_RIGHT)
1037 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1038 es->tabs_count, es->tabs)) - es->x_offset;
1039 x = w - x;
1041 else if (es->style & ES_CENTER)
1043 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1044 es->tabs_count, es->tabs)) - es->x_offset;
1045 x += (w - lw) / 2;
1047 else /* ES_LEFT */
1049 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1050 es->tabs_count, es->tabs)) - es->x_offset;
1052 } else {
1053 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1054 if (index < es->x_offset) {
1055 GetTextExtentPoint32W(dc, text + index,
1056 es->x_offset - index, &size);
1057 x = -size.cx;
1058 } else {
1059 GetTextExtentPoint32W(dc, text + es->x_offset,
1060 index - es->x_offset, &size);
1061 x = size.cx;
1063 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1065 w = es->format_rect.right - es->format_rect.left;
1066 if (w > es->text_width)
1068 if (es->style & ES_RIGHT)
1069 x += w - es->text_width;
1070 else if (es->style & ES_CENTER)
1071 x += (w - es->text_width) / 2;
1075 y = 0;
1076 if (es->style & ES_PASSWORD)
1077 HeapFree(GetProcessHeap(), 0, text);
1079 x += es->format_rect.left;
1080 y += es->format_rect.top;
1081 if (es->font)
1082 SelectObject(dc, old_font);
1083 ReleaseDC(es->hwndSelf, dc);
1084 return MAKELONG((INT16)x, (INT16)y);
1088 /*********************************************************************
1090 * EDIT_GetLineRect
1092 * Calculates the bounding rectangle for a line from a starting
1093 * column to an ending column.
1096 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1098 INT line_index = EDIT_EM_LineIndex(es, line);
1100 if (es->style & ES_MULTILINE)
1101 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1102 else
1103 rc->top = es->format_rect.top;
1104 rc->bottom = rc->top + es->line_height;
1105 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1106 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1110 static inline void text_buffer_changed(EDITSTATE *es)
1112 es->text_length = (UINT)-1;
1115 /*********************************************************************
1117 * EDIT_LockBuffer
1119 * This acts as a LocalLock16(), but it locks only once. This way
1120 * you can call it whenever you like, without unlocking.
1122 * Initially the edit control allocates a HLOCAL32 buffer
1123 * (32 bit linear memory handler). However, 16 bit application
1124 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1125 * handler). From that moment on we have to keep using this 16 bit memory
1126 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1127 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1128 * conversion.
1131 static void EDIT_LockBuffer(EDITSTATE *es)
1133 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1134 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1136 if (!es->text) {
1137 CHAR *textA = NULL;
1138 UINT countA = 0;
1139 BOOL _16bit = FALSE;
1141 if(es->hloc32W)
1143 if(es->hloc32A)
1145 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1146 textA = LocalLock(es->hloc32A);
1147 countA = strlen(textA) + 1;
1149 else if(es->hloc16)
1151 HANDLE16 oldDS = stack16->ds;
1152 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1153 stack16->ds = hInstance;
1154 textA = MapSL(LocalLock16(es->hloc16));
1155 stack16->ds = oldDS;
1156 countA = strlen(textA) + 1;
1157 _16bit = TRUE;
1160 else {
1161 ERR("no buffer ... please report\n");
1162 return;
1165 if(textA)
1167 HLOCAL hloc32W_new;
1168 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1169 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1170 if(countW_new > es->buffer_size + 1)
1172 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1173 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1174 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1175 if(hloc32W_new)
1177 es->hloc32W = hloc32W_new;
1178 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1179 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1181 else
1182 WARN("FAILED! Will synchronize partially\n");
1186 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1187 es->text = LocalLock(es->hloc32W);
1189 if(textA)
1191 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1192 if(_16bit)
1194 HANDLE16 oldDS = stack16->ds;
1195 stack16->ds = hInstance;
1196 LocalUnlock16(es->hloc16);
1197 stack16->ds = oldDS;
1199 else
1200 LocalUnlock(es->hloc32A);
1203 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1204 es->lock_count++;
1208 /*********************************************************************
1210 * EDIT_UnlockBuffer
1213 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1216 /* Edit window might be already destroyed */
1217 if(!IsWindow(es->hwndSelf))
1219 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1220 return;
1223 if (!es->lock_count) {
1224 ERR("lock_count == 0 ... please report\n");
1225 return;
1227 if (!es->text) {
1228 ERR("es->text == 0 ... please report\n");
1229 return;
1232 if (force || (es->lock_count == 1)) {
1233 if (es->hloc32W) {
1234 CHAR *textA = NULL;
1235 UINT countA = 0;
1236 UINT countW = get_text_length(es) + 1;
1237 STACK16FRAME* stack16 = NULL;
1238 HANDLE16 oldDS = 0;
1240 if(es->hloc32A)
1242 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1243 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1244 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1245 countA = LocalSize(es->hloc32A);
1246 if(countA_new > countA)
1248 HLOCAL hloc32A_new;
1249 UINT alloc_size = ROUND_TO_GROW(countA_new);
1250 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1251 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1252 if(hloc32A_new)
1254 es->hloc32A = hloc32A_new;
1255 countA = LocalSize(hloc32A_new);
1256 TRACE("Real new size %d bytes\n", countA);
1258 else
1259 WARN("FAILED! Will synchronize partially\n");
1261 textA = LocalLock(es->hloc32A);
1263 else if(es->hloc16)
1265 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1267 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1268 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1270 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
1271 oldDS = stack16->ds;
1272 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
1274 countA = LocalSize16(es->hloc16);
1275 if(countA_new > countA)
1277 HLOCAL16 hloc16_new;
1278 UINT alloc_size = ROUND_TO_GROW(countA_new);
1279 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1280 hloc16_new = LocalReAlloc16(es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1281 if(hloc16_new)
1283 es->hloc16 = hloc16_new;
1284 countA = LocalSize16(hloc16_new);
1285 TRACE("Real new size %d bytes\n", countA);
1287 else
1288 WARN("FAILED! Will synchronize partially\n");
1290 textA = MapSL(LocalLock16(es->hloc16));
1293 if(textA)
1295 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
1296 if(stack16)
1297 LocalUnlock16(es->hloc16);
1298 else
1299 LocalUnlock(es->hloc32A);
1302 if (stack16) stack16->ds = oldDS;
1303 LocalUnlock(es->hloc32W);
1304 es->text = NULL;
1306 else {
1307 ERR("no buffer ... please report\n");
1308 return;
1311 es->lock_count--;
1315 /*********************************************************************
1317 * EDIT_MakeFit
1319 * Try to fit size + 1 characters in the buffer.
1321 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1323 HLOCAL hNew32W;
1325 if (size <= es->buffer_size)
1326 return TRUE;
1328 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1330 /* Force edit to unlock it's buffer. es->text now NULL */
1331 EDIT_UnlockBuffer(es, TRUE);
1333 if (es->hloc32W) {
1334 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1335 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1336 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1337 es->hloc32W = hNew32W;
1338 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1342 EDIT_LockBuffer(es);
1344 if (es->buffer_size < size) {
1345 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1346 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1347 return FALSE;
1348 } else {
1349 TRACE("We now have %d+1\n", es->buffer_size);
1350 return TRUE;
1355 /*********************************************************************
1357 * EDIT_MakeUndoFit
1359 * Try to fit size + 1 bytes in the undo buffer.
1362 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1364 UINT alloc_size;
1366 if (size <= es->undo_buffer_size)
1367 return TRUE;
1369 TRACE("trying to ReAlloc to %d+1\n", size);
1371 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1372 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1373 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1374 return TRUE;
1376 else
1378 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1379 return FALSE;
1384 /*********************************************************************
1386 * EDIT_UpdateTextRegion
1389 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1391 if (es->flags & EF_UPDATE) {
1392 es->flags &= ~EF_UPDATE;
1393 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1395 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1399 /*********************************************************************
1401 * EDIT_UpdateText
1404 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1406 if (es->flags & EF_UPDATE) {
1407 es->flags &= ~EF_UPDATE;
1408 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1410 InvalidateRect(es->hwndSelf, rc, bErase);
1413 /*********************************************************************
1415 * EDIT_SL_InvalidateText
1417 * Called from EDIT_InvalidateText().
1418 * Does the job for single-line controls only.
1421 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1423 RECT line_rect;
1424 RECT rc;
1426 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1427 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1428 EDIT_UpdateText(es, &rc, TRUE);
1432 static inline INT get_vertical_line_count(EDITSTATE *es)
1434 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1435 return max(1,vlc);
1438 /*********************************************************************
1440 * EDIT_ML_InvalidateText
1442 * Called from EDIT_InvalidateText().
1443 * Does the job for multi-line controls only.
1446 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1448 INT vlc = get_vertical_line_count(es);
1449 INT sl = EDIT_EM_LineFromChar(es, start);
1450 INT el = EDIT_EM_LineFromChar(es, end);
1451 INT sc;
1452 INT ec;
1453 RECT rc1;
1454 RECT rcWnd;
1455 RECT rcLine;
1456 RECT rcUpdate;
1457 INT l;
1459 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1460 return;
1462 sc = start - EDIT_EM_LineIndex(es, sl);
1463 ec = end - EDIT_EM_LineIndex(es, el);
1464 if (sl < es->y_offset) {
1465 sl = es->y_offset;
1466 sc = 0;
1468 if (el > es->y_offset + vlc) {
1469 el = es->y_offset + vlc;
1470 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1472 GetClientRect(es->hwndSelf, &rc1);
1473 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1474 if (sl == el) {
1475 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1476 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1477 EDIT_UpdateText(es, &rcUpdate, TRUE);
1478 } else {
1479 EDIT_GetLineRect(es, sl, sc,
1480 EDIT_EM_LineLength(es,
1481 EDIT_EM_LineIndex(es, sl)),
1482 &rcLine);
1483 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1484 EDIT_UpdateText(es, &rcUpdate, TRUE);
1485 for (l = sl + 1 ; l < el ; l++) {
1486 EDIT_GetLineRect(es, l, 0,
1487 EDIT_EM_LineLength(es,
1488 EDIT_EM_LineIndex(es, l)),
1489 &rcLine);
1490 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1491 EDIT_UpdateText(es, &rcUpdate, TRUE);
1493 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1494 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1495 EDIT_UpdateText(es, &rcUpdate, TRUE);
1500 /*********************************************************************
1502 * EDIT_InvalidateText
1504 * Invalidate the text from offset start up to, but not including,
1505 * offset end. Useful for (re)painting the selection.
1506 * Regions outside the linewidth are not invalidated.
1507 * end == -1 means end == TextLength.
1508 * start and end need not be ordered.
1511 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1513 if (end == start)
1514 return;
1516 if (end == -1)
1517 end = get_text_length(es);
1519 if (end < start) {
1520 INT tmp = start;
1521 start = end;
1522 end = tmp;
1525 if (es->style & ES_MULTILINE)
1526 EDIT_ML_InvalidateText(es, start, end);
1527 else
1528 EDIT_SL_InvalidateText(es, start, end);
1532 /*********************************************************************
1534 * EDIT_EM_SetSel
1536 * note: unlike the specs say: the order of start and end
1537 * _is_ preserved in Windows. (i.e. start can be > end)
1538 * In other words: this handler is OK
1541 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1543 UINT old_start = es->selection_start;
1544 UINT old_end = es->selection_end;
1545 UINT len = get_text_length(es);
1547 if (start == (UINT)-1) {
1548 start = es->selection_end;
1549 end = es->selection_end;
1550 } else {
1551 start = min(start, len);
1552 end = min(end, len);
1554 es->selection_start = start;
1555 es->selection_end = end;
1556 if (after_wrap)
1557 es->flags |= EF_AFTER_WRAP;
1558 else
1559 es->flags &= ~EF_AFTER_WRAP;
1560 /* Compute the necessary invalidation region. */
1561 /* Note that we don't need to invalidate regions which have
1562 * "never" been selected, or those which are "still" selected.
1563 * In fact, every time we hit a selection boundary, we can
1564 * *toggle* whether we need to invalidate. Thus we can optimize by
1565 * *sorting* the interval endpoints. Let's assume that we sort them
1566 * in this order:
1567 * start <= end <= old_start <= old_end
1568 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1569 * in 5 comparisons; i.e. it is impossible to do better than the
1570 * following: */
1571 ORDER_UINT(end, old_end);
1572 ORDER_UINT(start, old_start);
1573 ORDER_UINT(old_start, old_end);
1574 ORDER_UINT(start, end);
1575 /* Note that at this point 'end' and 'old_start' are not in order, but
1576 * start is definitely the min. and old_end is definitely the max. */
1577 if (end != old_start)
1580 * One can also do
1581 * ORDER_UINT32(end, old_start);
1582 * EDIT_InvalidateText(es, start, end);
1583 * EDIT_InvalidateText(es, old_start, old_end);
1584 * in place of the following if statement.
1585 * (That would complete the optimal five-comparison four-element sort.)
1587 if (old_start > end )
1589 EDIT_InvalidateText(es, start, end);
1590 EDIT_InvalidateText(es, old_start, old_end);
1592 else
1594 EDIT_InvalidateText(es, start, old_start);
1595 EDIT_InvalidateText(es, end, old_end);
1598 else EDIT_InvalidateText(es, start, old_end);
1602 /*********************************************************************
1604 * EDIT_UpdateScrollInfo
1607 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1609 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1611 SCROLLINFO si;
1612 si.cbSize = sizeof(SCROLLINFO);
1613 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1614 si.nMin = 0;
1615 si.nMax = es->line_count - 1;
1616 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1617 si.nPos = es->y_offset;
1618 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1619 si.nMin, si.nMax, si.nPage, si.nPos);
1620 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1623 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1625 SCROLLINFO si;
1626 si.cbSize = sizeof(SCROLLINFO);
1627 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1628 si.nMin = 0;
1629 si.nMax = es->text_width - 1;
1630 si.nPage = es->format_rect.right - es->format_rect.left;
1631 si.nPos = es->x_offset;
1632 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1633 si.nMin, si.nMax, si.nPage, si.nPos);
1634 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1639 /*********************************************************************
1641 * EDIT_EM_LineScroll_internal
1643 * Version of EDIT_EM_LineScroll for internal use.
1644 * It doesn't refuse if ES_MULTILINE is set and assumes that
1645 * dx is in pixels, dy - in lines.
1648 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1650 INT nyoff;
1651 INT x_offset_in_pixels;
1652 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1653 es->line_height;
1655 if (es->style & ES_MULTILINE)
1657 x_offset_in_pixels = es->x_offset;
1659 else
1661 dy = 0;
1662 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1665 if (-dx > x_offset_in_pixels)
1666 dx = -x_offset_in_pixels;
1667 if (dx > es->text_width - x_offset_in_pixels)
1668 dx = es->text_width - x_offset_in_pixels;
1669 nyoff = max(0, es->y_offset + dy);
1670 if (nyoff >= es->line_count - lines_per_page)
1671 nyoff = max(0, es->line_count - lines_per_page);
1672 dy = (es->y_offset - nyoff) * es->line_height;
1673 if (dx || dy) {
1674 RECT rc1;
1675 RECT rc;
1677 es->y_offset = nyoff;
1678 if(es->style & ES_MULTILINE)
1679 es->x_offset += dx;
1680 else
1681 es->x_offset += dx / es->char_width;
1683 GetClientRect(es->hwndSelf, &rc1);
1684 IntersectRect(&rc, &rc1, &es->format_rect);
1685 ScrollWindowEx(es->hwndSelf, -dx, dy,
1686 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1687 /* force scroll info update */
1688 EDIT_UpdateScrollInfo(es);
1690 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1691 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1692 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1693 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1694 return TRUE;
1697 /*********************************************************************
1699 * EM_LINESCROLL
1701 * NOTE: dx is in average character widths, dy - in lines;
1704 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1706 if (!(es->style & ES_MULTILINE))
1707 return FALSE;
1709 dx *= es->char_width;
1710 return EDIT_EM_LineScroll_internal(es, dx, dy);
1714 /*********************************************************************
1716 * EM_SCROLL
1719 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1721 INT dy;
1723 if (!(es->style & ES_MULTILINE))
1724 return (LRESULT)FALSE;
1726 dy = 0;
1728 switch (action) {
1729 case SB_LINEUP:
1730 if (es->y_offset)
1731 dy = -1;
1732 break;
1733 case SB_LINEDOWN:
1734 if (es->y_offset < es->line_count - 1)
1735 dy = 1;
1736 break;
1737 case SB_PAGEUP:
1738 if (es->y_offset)
1739 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1740 break;
1741 case SB_PAGEDOWN:
1742 if (es->y_offset < es->line_count - 1)
1743 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1744 break;
1745 default:
1746 return (LRESULT)FALSE;
1748 if (dy) {
1749 INT vlc = get_vertical_line_count(es);
1750 /* check if we are going to move too far */
1751 if(es->y_offset + dy > es->line_count - vlc)
1752 dy = es->line_count - vlc - es->y_offset;
1754 /* Notification is done in EDIT_EM_LineScroll */
1755 if(dy)
1756 EDIT_EM_LineScroll(es, 0, dy);
1758 return MAKELONG((INT16)dy, (BOOL16)TRUE);
1762 /*********************************************************************
1764 * EDIT_SetCaretPos
1767 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1768 BOOL after_wrap)
1770 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1771 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1772 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1776 /*********************************************************************
1778 * EM_SCROLLCARET
1781 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1783 if (es->style & ES_MULTILINE) {
1784 INT l;
1785 INT vlc;
1786 INT ww;
1787 INT cw = es->char_width;
1788 INT x;
1789 INT dy = 0;
1790 INT dx = 0;
1792 l = EDIT_EM_LineFromChar(es, es->selection_end);
1793 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1794 vlc = get_vertical_line_count(es);
1795 if (l >= es->y_offset + vlc)
1796 dy = l - vlc + 1 - es->y_offset;
1797 if (l < es->y_offset)
1798 dy = l - es->y_offset;
1799 ww = es->format_rect.right - es->format_rect.left;
1800 if (x < es->format_rect.left)
1801 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1802 if (x > es->format_rect.right)
1803 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1804 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1806 /* check if we are going to move too far */
1807 if(es->x_offset + dx + ww > es->text_width)
1808 dx = es->text_width - ww - es->x_offset;
1809 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1810 EDIT_EM_LineScroll_internal(es, dx, dy);
1812 } else {
1813 INT x;
1814 INT goal;
1815 INT format_width;
1817 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1818 format_width = es->format_rect.right - es->format_rect.left;
1819 if (x < es->format_rect.left) {
1820 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1821 do {
1822 es->x_offset--;
1823 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1824 } while ((x < goal) && es->x_offset);
1825 /* FIXME: use ScrollWindow() somehow to improve performance */
1826 EDIT_UpdateText(es, NULL, TRUE);
1827 } else if (x > es->format_rect.right) {
1828 INT x_last;
1829 INT len = get_text_length(es);
1830 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1831 do {
1832 es->x_offset++;
1833 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1834 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1835 } while ((x > goal) && (x_last > es->format_rect.right));
1836 /* FIXME: use ScrollWindow() somehow to improve performance */
1837 EDIT_UpdateText(es, NULL, TRUE);
1841 if(es->flags & EF_FOCUSED)
1842 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1846 /*********************************************************************
1848 * EDIT_MoveBackward
1851 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1853 INT e = es->selection_end;
1855 if (e) {
1856 e--;
1857 if ((es->style & ES_MULTILINE) && e &&
1858 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1859 e--;
1860 if (e && (es->text[e - 1] == '\r'))
1861 e--;
1864 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1865 EDIT_EM_ScrollCaret(es);
1869 /*********************************************************************
1871 * EDIT_MoveDown_ML
1873 * Only for multi line controls
1874 * Move the caret one line down, on a column with the nearest
1875 * x coordinate on the screen (might be a different column).
1878 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1880 INT s = es->selection_start;
1881 INT e = es->selection_end;
1882 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1883 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1884 INT x = (short)LOWORD(pos);
1885 INT y = (short)HIWORD(pos);
1887 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1888 if (!extend)
1889 s = e;
1890 EDIT_EM_SetSel(es, s, e, after_wrap);
1891 EDIT_EM_ScrollCaret(es);
1895 /*********************************************************************
1897 * EDIT_MoveEnd
1900 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1902 BOOL after_wrap = FALSE;
1903 INT e;
1905 /* Pass a high value in x to make sure of receiving the end of the line */
1906 if (!ctrl && (es->style & ES_MULTILINE))
1907 e = EDIT_CharFromPos(es, 0x3fffffff,
1908 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1909 else
1910 e = get_text_length(es);
1911 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1912 EDIT_EM_ScrollCaret(es);
1916 /*********************************************************************
1918 * EDIT_MoveForward
1921 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1923 INT e = es->selection_end;
1925 if (es->text[e]) {
1926 e++;
1927 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1928 if (es->text[e] == '\n')
1929 e++;
1930 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1931 e += 2;
1934 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1935 EDIT_EM_ScrollCaret(es);
1939 /*********************************************************************
1941 * EDIT_MoveHome
1943 * Home key: move to beginning of line.
1946 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1948 INT e;
1950 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1951 if (!ctrl && (es->style & ES_MULTILINE))
1952 e = EDIT_CharFromPos(es, -es->x_offset,
1953 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1954 else
1955 e = 0;
1956 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1957 EDIT_EM_ScrollCaret(es);
1961 /*********************************************************************
1963 * EDIT_MovePageDown_ML
1965 * Only for multi line controls
1966 * Move the caret one page down, on a column with the nearest
1967 * x coordinate on the screen (might be a different column).
1970 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1972 INT s = es->selection_start;
1973 INT e = es->selection_end;
1974 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1975 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1976 INT x = (short)LOWORD(pos);
1977 INT y = (short)HIWORD(pos);
1979 e = EDIT_CharFromPos(es, x,
1980 y + (es->format_rect.bottom - es->format_rect.top),
1981 &after_wrap);
1982 if (!extend)
1983 s = e;
1984 EDIT_EM_SetSel(es, s, e, after_wrap);
1985 EDIT_EM_ScrollCaret(es);
1989 /*********************************************************************
1991 * EDIT_MovePageUp_ML
1993 * Only for multi line controls
1994 * Move the caret one page up, on a column with the nearest
1995 * x coordinate on the screen (might be a different column).
1998 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2000 INT s = es->selection_start;
2001 INT e = es->selection_end;
2002 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2003 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2004 INT x = (short)LOWORD(pos);
2005 INT y = (short)HIWORD(pos);
2007 e = EDIT_CharFromPos(es, x,
2008 y - (es->format_rect.bottom - es->format_rect.top),
2009 &after_wrap);
2010 if (!extend)
2011 s = e;
2012 EDIT_EM_SetSel(es, s, e, after_wrap);
2013 EDIT_EM_ScrollCaret(es);
2017 /*********************************************************************
2019 * EDIT_MoveUp_ML
2021 * Only for multi line controls
2022 * Move the caret one line up, on a column with the nearest
2023 * x coordinate on the screen (might be a different column).
2026 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2028 INT s = es->selection_start;
2029 INT e = es->selection_end;
2030 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2031 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2032 INT x = (short)LOWORD(pos);
2033 INT y = (short)HIWORD(pos);
2035 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2036 if (!extend)
2037 s = e;
2038 EDIT_EM_SetSel(es, s, e, after_wrap);
2039 EDIT_EM_ScrollCaret(es);
2043 /*********************************************************************
2045 * EDIT_MoveWordBackward
2048 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2050 INT s = es->selection_start;
2051 INT e = es->selection_end;
2052 INT l;
2053 INT ll;
2054 INT li;
2056 l = EDIT_EM_LineFromChar(es, e);
2057 ll = EDIT_EM_LineLength(es, e);
2058 li = EDIT_EM_LineIndex(es, l);
2059 if (e - li == 0) {
2060 if (l) {
2061 li = EDIT_EM_LineIndex(es, l - 1);
2062 e = li + EDIT_EM_LineLength(es, li);
2064 } else {
2065 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2067 if (!extend)
2068 s = e;
2069 EDIT_EM_SetSel(es, s, e, FALSE);
2070 EDIT_EM_ScrollCaret(es);
2074 /*********************************************************************
2076 * EDIT_MoveWordForward
2079 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2081 INT s = es->selection_start;
2082 INT e = es->selection_end;
2083 INT l;
2084 INT ll;
2085 INT li;
2087 l = EDIT_EM_LineFromChar(es, e);
2088 ll = EDIT_EM_LineLength(es, e);
2089 li = EDIT_EM_LineIndex(es, l);
2090 if (e - li == ll) {
2091 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2092 e = EDIT_EM_LineIndex(es, l + 1);
2093 } else {
2094 e = li + EDIT_CallWordBreakProc(es,
2095 li, e - li + 1, ll, WB_RIGHT);
2097 if (!extend)
2098 s = e;
2099 EDIT_EM_SetSel(es, s, e, FALSE);
2100 EDIT_EM_ScrollCaret(es);
2104 /*********************************************************************
2106 * EDIT_PaintText
2109 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2111 COLORREF BkColor;
2112 COLORREF TextColor;
2113 LOGFONTW underline_font;
2114 HFONT hUnderline = 0;
2115 HFONT old_font = 0;
2116 INT ret;
2117 INT li;
2118 INT BkMode;
2119 SIZE size;
2121 if (!count)
2122 return 0;
2123 BkMode = GetBkMode(dc);
2124 BkColor = GetBkColor(dc);
2125 TextColor = GetTextColor(dc);
2126 if (rev) {
2127 if (es->composition_len == 0)
2129 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2130 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2131 SetBkMode( dc, OPAQUE);
2133 else
2135 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2136 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2137 underline_font.lfUnderline = TRUE;
2138 hUnderline = CreateFontIndirectW(&underline_font);
2139 old_font = SelectObject(dc,hUnderline);
2142 li = EDIT_EM_LineIndex(es, line);
2143 if (es->style & ES_MULTILINE) {
2144 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2145 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2146 } else {
2147 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2148 TextOutW(dc, x, y, text + li + col, count);
2149 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2150 ret = size.cx;
2151 if (es->style & ES_PASSWORD)
2152 HeapFree(GetProcessHeap(), 0, text);
2154 if (rev) {
2155 if (es->composition_len == 0)
2157 SetBkColor(dc, BkColor);
2158 SetTextColor(dc, TextColor);
2159 SetBkMode( dc, BkMode);
2161 else
2163 if (old_font)
2164 SelectObject(dc,old_font);
2165 if (hUnderline)
2166 DeleteObject(hUnderline);
2169 return ret;
2173 /*********************************************************************
2175 * EDIT_PaintLine
2178 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2180 INT s = es->selection_start;
2181 INT e = es->selection_end;
2182 INT li;
2183 INT ll;
2184 INT x;
2185 INT y;
2186 LRESULT pos;
2188 if (es->style & ES_MULTILINE) {
2189 INT vlc = get_vertical_line_count(es);
2191 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2192 return;
2193 } else if (line)
2194 return;
2196 TRACE("line=%d\n", line);
2198 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2199 x = (short)LOWORD(pos);
2200 y = (short)HIWORD(pos);
2201 li = EDIT_EM_LineIndex(es, line);
2202 ll = EDIT_EM_LineLength(es, li);
2203 s = min(es->selection_start, es->selection_end);
2204 e = max(es->selection_start, es->selection_end);
2205 s = min(li + ll, max(li, s));
2206 e = min(li + ll, max(li, e));
2207 if (rev && (s != e) &&
2208 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2209 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2210 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2211 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2212 } else
2213 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2217 /*********************************************************************
2219 * EDIT_AdjustFormatRect
2221 * Adjusts the format rectangle for the current font and the
2222 * current client rectangle.
2225 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2227 RECT ClientRect;
2229 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2230 if (es->style & ES_MULTILINE)
2232 INT fw, vlc, max_x_offset, max_y_offset;
2234 vlc = get_vertical_line_count(es);
2235 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2237 /* correct es->x_offset */
2238 fw = es->format_rect.right - es->format_rect.left;
2239 max_x_offset = es->text_width - fw;
2240 if(max_x_offset < 0) max_x_offset = 0;
2241 if(es->x_offset > max_x_offset)
2242 es->x_offset = max_x_offset;
2244 /* correct es->y_offset */
2245 max_y_offset = es->line_count - vlc;
2246 if(max_y_offset < 0) max_y_offset = 0;
2247 if(es->y_offset > max_y_offset)
2248 es->y_offset = max_y_offset;
2250 /* force scroll info update */
2251 EDIT_UpdateScrollInfo(es);
2253 else
2254 /* Windows doesn't care to fix text placement for SL controls */
2255 es->format_rect.bottom = es->format_rect.top + es->line_height;
2257 /* Always stay within the client area */
2258 GetClientRect(es->hwndSelf, &ClientRect);
2259 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2261 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2262 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2264 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2268 /*********************************************************************
2270 * EDIT_SetRectNP
2272 * note: this is not (exactly) the handler called on EM_SETRECTNP
2273 * it is also used to set the rect of a single line control
2276 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2278 LONG_PTR ExStyle;
2279 INT bw, bh;
2280 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2282 CopyRect(&es->format_rect, rc);
2284 if (ExStyle & WS_EX_CLIENTEDGE) {
2285 es->format_rect.left++;
2286 es->format_rect.right--;
2288 if (es->format_rect.bottom - es->format_rect.top
2289 >= es->line_height + 2)
2291 es->format_rect.top++;
2292 es->format_rect.bottom--;
2295 else if (es->style & WS_BORDER) {
2296 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2297 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2298 es->format_rect.left += bw;
2299 es->format_rect.right -= bw;
2300 if (es->format_rect.bottom - es->format_rect.top
2301 >= es->line_height + 2 * bh)
2303 es->format_rect.top += bh;
2304 es->format_rect.bottom -= bh;
2308 es->format_rect.left += es->left_margin;
2309 es->format_rect.right -= es->right_margin;
2310 EDIT_AdjustFormatRect(es);
2314 /*********************************************************************
2316 * EM_CHARFROMPOS
2318 * returns line number (not index) in high-order word of result.
2319 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2320 * to Richedit, not to the edit control. Original documentation is valid.
2321 * FIXME: do the specs mean to return -1 if outside client area or
2322 * if outside formatting rectangle ???
2325 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2327 POINT pt;
2328 RECT rc;
2329 INT index;
2331 pt.x = x;
2332 pt.y = y;
2333 GetClientRect(es->hwndSelf, &rc);
2334 if (!PtInRect(&rc, pt))
2335 return -1;
2337 index = EDIT_CharFromPos(es, x, y, NULL);
2338 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2342 /*********************************************************************
2344 * EM_FMTLINES
2346 * Enable or disable soft breaks.
2348 * This means: insert or remove the soft linebreak character (\r\r\n).
2349 * Take care to check if the text still fits the buffer after insertion.
2350 * If not, notify with EN_ERRSPACE.
2353 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2355 es->flags &= ~EF_USE_SOFTBRK;
2356 if (add_eol) {
2357 es->flags |= EF_USE_SOFTBRK;
2358 FIXME("soft break enabled, not implemented\n");
2360 return add_eol;
2364 /*********************************************************************
2366 * EM_GETHANDLE
2368 * Hopefully this won't fire back at us.
2369 * We always start with a fixed buffer in the local heap.
2370 * Despite of the documentation says that the local heap is used
2371 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2372 * buffer on the local heap.
2375 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2377 HLOCAL hLocal;
2379 if (!(es->style & ES_MULTILINE))
2380 return 0;
2382 if(es->is_unicode)
2383 hLocal = es->hloc32W;
2384 else
2386 if(!es->hloc32A)
2388 CHAR *textA;
2389 UINT countA, alloc_size;
2390 TRACE("Allocating 32-bit ANSI alias buffer\n");
2391 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2392 alloc_size = ROUND_TO_GROW(countA);
2393 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2395 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2396 return 0;
2398 textA = LocalLock(es->hloc32A);
2399 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2400 LocalUnlock(es->hloc32A);
2402 hLocal = es->hloc32A;
2405 es->flags |= EF_APP_HAS_HANDLE;
2406 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2407 return hLocal;
2411 /*********************************************************************
2413 * EM_GETHANDLE16
2415 * Hopefully this won't fire back at us.
2416 * We always start with a buffer in 32 bit linear memory.
2417 * However, with this message a 16 bit application requests
2418 * a handle of 16 bit local heap memory, where it expects to find
2419 * the text.
2420 * It's a pitty that from this moment on we have to use this
2421 * local heap, because applications may rely on the handle
2422 * in the future.
2424 * In this function we'll try to switch to local heap.
2426 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2428 CHAR *textA;
2429 UINT countA, alloc_size;
2430 STACK16FRAME* stack16;
2431 HANDLE16 oldDS;
2433 if (!(es->style & ES_MULTILINE))
2434 return 0;
2436 if (es->hloc16)
2437 return es->hloc16;
2439 stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2440 oldDS = stack16->ds;
2441 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2443 if (!LocalHeapSize16()) {
2445 if (!LocalInit16(stack16->ds, 0, GlobalSize16(stack16->ds))) {
2446 ERR("could not initialize local heap\n");
2447 goto done;
2449 TRACE("local heap initialized\n");
2452 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2453 alloc_size = ROUND_TO_GROW(countA);
2455 TRACE("Allocating 16-bit ANSI alias buffer\n");
2456 if (!(es->hloc16 = LocalAlloc16(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2457 ERR("could not allocate new 16 bit buffer\n");
2458 goto done;
2461 if (!(textA = MapSL(LocalLock16( es->hloc16)))) {
2462 ERR("could not lock new 16 bit buffer\n");
2463 LocalFree16(es->hloc16);
2464 es->hloc16 = 0;
2465 goto done;
2468 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2469 LocalUnlock16(es->hloc16);
2470 es->flags |= EF_APP_HAS_HANDLE;
2472 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LocalSize16(es->hloc16));
2474 done:
2475 stack16->ds = oldDS;
2476 return es->hloc16;
2480 /*********************************************************************
2482 * EM_GETLINE
2485 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2487 LPWSTR src;
2488 INT line_len, dst_len;
2489 INT i;
2491 if (es->style & ES_MULTILINE) {
2492 if (line >= es->line_count)
2493 return 0;
2494 } else
2495 line = 0;
2496 i = EDIT_EM_LineIndex(es, line);
2497 src = es->text + i;
2498 line_len = EDIT_EM_LineLength(es, i);
2499 dst_len = *(WORD *)dst;
2500 if(unicode)
2502 if(dst_len <= line_len)
2504 memcpy(dst, src, dst_len * sizeof(WCHAR));
2505 return dst_len;
2507 else /* Append 0 if enough space */
2509 memcpy(dst, src, line_len * sizeof(WCHAR));
2510 dst[line_len] = 0;
2511 return line_len;
2514 else
2516 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2517 if(!ret && line_len) /* Insufficient buffer size */
2518 return dst_len;
2519 if(ret < dst_len) /* Append 0 if enough space */
2520 ((LPSTR)dst)[ret] = 0;
2521 return ret;
2526 /*********************************************************************
2528 * EM_GETSEL
2531 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2533 UINT s = es->selection_start;
2534 UINT e = es->selection_end;
2536 ORDER_UINT(s, e);
2537 if (start)
2538 *start = s;
2539 if (end)
2540 *end = e;
2541 return MAKELONG(s, e);
2545 /*********************************************************************
2547 * EM_REPLACESEL
2549 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2552 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2554 UINT strl = strlenW(lpsz_replace);
2555 UINT tl = get_text_length(es);
2556 UINT utl;
2557 UINT s;
2558 UINT e;
2559 UINT i;
2560 UINT size;
2561 LPWSTR p;
2562 HRGN hrgn = 0;
2563 LPWSTR buf = NULL;
2564 UINT bufl = 0;
2566 TRACE("%s, can_undo %d, send_update %d\n",
2567 debugstr_w(lpsz_replace), can_undo, send_update);
2569 s = es->selection_start;
2570 e = es->selection_end;
2572 if ((s == e) && !strl)
2573 return;
2575 ORDER_UINT(s, e);
2577 size = tl - (e - s) + strl;
2578 if (!size)
2579 es->text_width = 0;
2581 /* Issue the EN_MAXTEXT notification and continue with replacing text
2582 * such that buffer limit is honored. */
2583 if ((honor_limit) && (size > es->buffer_limit)) {
2584 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2585 /* Buffer limit can be smaller than the actual length of text in combobox */
2586 if (es->buffer_limit < (tl - (e-s)))
2587 strl = 0;
2588 else
2589 strl = es->buffer_limit - (tl - (e-s));
2592 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2593 return;
2595 if (e != s) {
2596 /* there is something to be deleted */
2597 TRACE("deleting stuff.\n");
2598 bufl = e - s;
2599 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2600 if (!buf) return;
2601 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2602 buf[bufl] = 0; /* ensure 0 termination */
2603 /* now delete */
2604 strcpyW(es->text + s, es->text + e);
2605 text_buffer_changed(es);
2607 if (strl) {
2608 /* there is an insertion */
2609 tl = get_text_length(es);
2610 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));
2611 for (p = es->text + tl ; p >= es->text + s ; p--)
2612 p[strl] = p[0];
2613 for (i = 0 , p = es->text + s ; i < strl ; i++)
2614 p[i] = lpsz_replace[i];
2615 if(es->style & ES_UPPERCASE)
2616 CharUpperBuffW(p, strl);
2617 else if(es->style & ES_LOWERCASE)
2618 CharLowerBuffW(p, strl);
2619 text_buffer_changed(es);
2621 if (es->style & ES_MULTILINE)
2623 INT st = min(es->selection_start, es->selection_end);
2624 INT vlc = get_vertical_line_count(es);
2626 hrgn = CreateRectRgn(0, 0, 0, 0);
2627 EDIT_BuildLineDefs_ML(es, st, st + strl,
2628 strl - abs(es->selection_end - es->selection_start), hrgn);
2629 /* if text is too long undo all changes */
2630 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2631 if (strl)
2632 strcpyW(es->text + e, es->text + e + strl);
2633 if (e != s)
2634 for (i = 0 , p = es->text ; i < e - s ; i++)
2635 p[i + s] = buf[i];
2636 text_buffer_changed(es);
2637 EDIT_BuildLineDefs_ML(es, s, e,
2638 abs(es->selection_end - es->selection_start) - strl, hrgn);
2639 strl = 0;
2640 e = s;
2641 hrgn = CreateRectRgn(0, 0, 0, 0);
2642 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2645 else {
2646 INT fw = es->format_rect.right - es->format_rect.left;
2647 EDIT_CalcLineWidth_SL(es);
2648 /* remove chars that don't fit */
2649 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2650 while ((es->text_width > fw) && s + strl >= s) {
2651 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2652 strl--;
2653 EDIT_CalcLineWidth_SL(es);
2655 text_buffer_changed(es);
2656 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2660 if (e != s) {
2661 if (can_undo) {
2662 utl = strlenW(es->undo_text);
2663 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2664 /* undo-buffer is extended to the right */
2665 EDIT_MakeUndoFit(es, utl + e - s);
2666 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2667 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2668 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2669 /* undo-buffer is extended to the left */
2670 EDIT_MakeUndoFit(es, utl + e - s);
2671 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2672 p[e - s] = p[0];
2673 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2674 p[i] = buf[i];
2675 es->undo_position = s;
2676 } else {
2677 /* new undo-buffer */
2678 EDIT_MakeUndoFit(es, e - s);
2679 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2680 es->undo_text[e - s] = 0; /* ensure 0 termination */
2681 es->undo_position = s;
2683 /* any deletion makes the old insertion-undo invalid */
2684 es->undo_insert_count = 0;
2685 } else
2686 EDIT_EM_EmptyUndoBuffer(es);
2688 if (strl) {
2689 if (can_undo) {
2690 if ((s == es->undo_position) ||
2691 ((es->undo_insert_count) &&
2692 (s == es->undo_position + es->undo_insert_count)))
2694 * insertion is new and at delete position or
2695 * an extension to either left or right
2697 es->undo_insert_count += strl;
2698 else {
2699 /* new insertion undo */
2700 es->undo_position = s;
2701 es->undo_insert_count = strl;
2702 /* new insertion makes old delete-buffer invalid */
2703 *es->undo_text = '\0';
2705 } else
2706 EDIT_EM_EmptyUndoBuffer(es);
2709 if (bufl)
2710 HeapFree(GetProcessHeap(), 0, buf);
2712 s += strl;
2714 /* If text has been deleted and we're right or center aligned then scroll rightward */
2715 if (es->style & (ES_RIGHT | ES_CENTER))
2717 INT delta = strl - abs(es->selection_end - es->selection_start);
2719 if (delta < 0 && es->x_offset)
2721 if (abs(delta) > es->x_offset)
2722 es->x_offset = 0;
2723 else
2724 es->x_offset += delta;
2728 EDIT_EM_SetSel(es, s, s, FALSE);
2729 es->flags |= EF_MODIFIED;
2730 if (send_update) es->flags |= EF_UPDATE;
2731 if (hrgn)
2733 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2734 DeleteObject(hrgn);
2736 else
2737 EDIT_UpdateText(es, NULL, TRUE);
2739 EDIT_EM_ScrollCaret(es);
2741 /* force scroll info update */
2742 EDIT_UpdateScrollInfo(es);
2745 if(send_update || (es->flags & EF_UPDATE))
2747 es->flags &= ~EF_UPDATE;
2748 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2753 /*********************************************************************
2755 * EM_SETHANDLE
2757 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2760 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2762 if (!(es->style & ES_MULTILINE))
2763 return;
2765 if (!hloc) {
2766 WARN("called with NULL handle\n");
2767 return;
2770 EDIT_UnlockBuffer(es, TRUE);
2772 if(es->hloc16)
2774 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2775 HANDLE16 oldDS = stack16->ds;
2777 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2778 LocalFree16(es->hloc16);
2779 stack16->ds = oldDS;
2780 es->hloc16 = 0;
2783 if(es->is_unicode)
2785 if(es->hloc32A)
2787 LocalFree(es->hloc32A);
2788 es->hloc32A = NULL;
2790 es->hloc32W = hloc;
2792 else
2794 INT countW, countA;
2795 HLOCAL hloc32W_new;
2796 WCHAR *textW;
2797 CHAR *textA;
2799 countA = LocalSize(hloc);
2800 textA = LocalLock(hloc);
2801 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2802 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2804 ERR("Could not allocate new unicode buffer\n");
2805 return;
2807 textW = LocalLock(hloc32W_new);
2808 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2809 LocalUnlock(hloc32W_new);
2810 LocalUnlock(hloc);
2812 if(es->hloc32W)
2813 LocalFree(es->hloc32W);
2815 es->hloc32W = hloc32W_new;
2816 es->hloc32A = hloc;
2819 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2821 es->flags |= EF_APP_HAS_HANDLE;
2822 EDIT_LockBuffer(es);
2824 es->x_offset = es->y_offset = 0;
2825 es->selection_start = es->selection_end = 0;
2826 EDIT_EM_EmptyUndoBuffer(es);
2827 es->flags &= ~EF_MODIFIED;
2828 es->flags &= ~EF_UPDATE;
2829 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2830 EDIT_UpdateText(es, NULL, TRUE);
2831 EDIT_EM_ScrollCaret(es);
2832 /* force scroll info update */
2833 EDIT_UpdateScrollInfo(es);
2837 /*********************************************************************
2839 * EM_SETHANDLE16
2841 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2844 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
2846 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
2847 HINSTANCE16 hInstance = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
2848 HANDLE16 oldDS = stack16->ds;
2849 INT countW, countA;
2850 HLOCAL hloc32W_new;
2851 WCHAR *textW;
2852 CHAR *textA;
2854 if (!(es->style & ES_MULTILINE))
2855 return;
2857 if (!hloc) {
2858 WARN("called with NULL handle\n");
2859 return;
2862 EDIT_UnlockBuffer(es, TRUE);
2864 if(es->hloc32A)
2866 LocalFree(es->hloc32A);
2867 es->hloc32A = NULL;
2870 stack16->ds = hInstance;
2871 countA = LocalSize16(hloc);
2872 textA = MapSL(LocalLock16(hloc));
2873 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2874 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2876 ERR("Could not allocate new unicode buffer\n");
2877 return;
2879 textW = LocalLock(hloc32W_new);
2880 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2881 LocalUnlock(hloc32W_new);
2882 LocalUnlock16(hloc);
2883 stack16->ds = oldDS;
2885 if(es->hloc32W)
2886 LocalFree(es->hloc32W);
2888 es->hloc32W = hloc32W_new;
2889 es->hloc16 = hloc;
2891 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2893 es->flags |= EF_APP_HAS_HANDLE;
2894 EDIT_LockBuffer(es);
2896 es->x_offset = es->y_offset = 0;
2897 es->selection_start = es->selection_end = 0;
2898 EDIT_EM_EmptyUndoBuffer(es);
2899 es->flags &= ~EF_MODIFIED;
2900 es->flags &= ~EF_UPDATE;
2901 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2902 EDIT_UpdateText(es, NULL, TRUE);
2903 EDIT_EM_ScrollCaret(es);
2904 /* force scroll info update */
2905 EDIT_UpdateScrollInfo(es);
2909 /*********************************************************************
2911 * EM_SETLIMITTEXT
2913 * NOTE: this version currently implements WinNT limits
2916 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2918 if (!limit) limit = ~0u;
2919 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2920 es->buffer_limit = limit;
2924 /*********************************************************************
2926 * EM_SETMARGINS
2928 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2929 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2930 * margin according to the textmetrics of the current font.
2932 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2933 * of the char's width as the margin, but this is not how Windows handles this.
2934 * For all other fonts Windows sets the margins to zero.
2936 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2937 * edit control is equal to or larger than a certain size.
2938 * Interestingly if one subtracts both the left and right margins from
2939 * this size one always seems to get an even number. The extents of
2940 * the (four character) string "'**'" match this quite closely, so
2941 * we'll use this until we come up with a better idea.
2943 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2945 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2946 SIZE sz;
2948 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2949 return sz.cx + left + right;
2952 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2953 WORD left, WORD right, BOOL repaint)
2955 TEXTMETRICW tm;
2956 INT default_left_margin = 0; /* in pixels */
2957 INT default_right_margin = 0; /* in pixels */
2959 /* Set the default margins depending on the font */
2960 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2961 HDC dc = GetDC(es->hwndSelf);
2962 HFONT old_font = SelectObject(dc, es->font);
2963 GetTextMetricsW(dc, &tm);
2964 /* The default margins are only non zero for TrueType or Vector fonts */
2965 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2966 int min_size;
2967 RECT rc;
2968 /* This must be calculated more exactly! But how? */
2969 default_left_margin = tm.tmAveCharWidth / 2;
2970 default_right_margin = tm.tmAveCharWidth / 2;
2971 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2972 GetClientRect(es->hwndSelf, &rc);
2973 if(rc.right - rc.left < min_size) {
2974 default_left_margin = es->left_margin;
2975 default_right_margin = es->right_margin;
2978 SelectObject(dc, old_font);
2979 ReleaseDC(es->hwndSelf, dc);
2982 if (action & EC_LEFTMARGIN) {
2983 es->format_rect.left -= es->left_margin;
2984 if (left != EC_USEFONTINFO)
2985 es->left_margin = left;
2986 else
2987 es->left_margin = default_left_margin;
2988 es->format_rect.left += es->left_margin;
2991 if (action & EC_RIGHTMARGIN) {
2992 es->format_rect.right += es->right_margin;
2993 if (right != EC_USEFONTINFO)
2994 es->right_margin = right;
2995 else
2996 es->right_margin = default_right_margin;
2997 es->format_rect.right -= es->right_margin;
3000 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3001 EDIT_AdjustFormatRect(es);
3002 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3005 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3009 /*********************************************************************
3011 * EM_SETPASSWORDCHAR
3014 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3016 LONG style;
3018 if (es->style & ES_MULTILINE)
3019 return;
3021 if (es->password_char == c)
3022 return;
3024 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3025 es->password_char = c;
3026 if (c) {
3027 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3028 es->style |= ES_PASSWORD;
3029 } else {
3030 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3031 es->style &= ~ES_PASSWORD;
3033 EDIT_UpdateText(es, NULL, TRUE);
3037 /*********************************************************************
3039 * EM_SETTABSTOPS
3042 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3044 if (!(es->style & ES_MULTILINE))
3045 return FALSE;
3046 HeapFree(GetProcessHeap(), 0, es->tabs);
3047 es->tabs_count = count;
3048 if (!count)
3049 es->tabs = NULL;
3050 else {
3051 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3052 memcpy(es->tabs, tabs, count * sizeof(INT));
3054 return TRUE;
3058 /*********************************************************************
3060 * EM_SETTABSTOPS16
3063 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, const INT16 *tabs)
3065 if (!(es->style & ES_MULTILINE))
3066 return FALSE;
3067 HeapFree(GetProcessHeap(), 0, es->tabs);
3068 es->tabs_count = count;
3069 if (!count)
3070 es->tabs = NULL;
3071 else {
3072 INT i;
3073 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3074 for (i = 0 ; i < count ; i++)
3075 es->tabs[i] = *tabs++;
3077 return TRUE;
3081 /*********************************************************************
3083 * EM_SETWORDBREAKPROC
3086 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3088 if (es->word_break_proc == wbp)
3089 return;
3091 es->word_break_proc = wbp;
3092 es->word_break_proc16 = NULL;
3094 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3095 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3096 EDIT_UpdateText(es, NULL, TRUE);
3101 /*********************************************************************
3103 * EM_SETWORDBREAKPROC16
3106 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3108 if (es->word_break_proc16 == wbp)
3109 return;
3111 es->word_break_proc = NULL;
3112 es->word_break_proc16 = wbp;
3113 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3114 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3115 EDIT_UpdateText(es, NULL, TRUE);
3120 /*********************************************************************
3122 * EM_UNDO / WM_UNDO
3125 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3127 INT ulength;
3128 LPWSTR utext;
3130 /* As per MSDN spec, for a single-line edit control,
3131 the return value is always TRUE */
3132 if( es->style & ES_READONLY )
3133 return !(es->style & ES_MULTILINE);
3135 ulength = strlenW(es->undo_text);
3137 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3139 strcpyW(utext, es->undo_text);
3141 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3142 es->undo_insert_count, debugstr_w(utext));
3144 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3145 EDIT_EM_EmptyUndoBuffer(es);
3146 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3147 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3148 /* send the notification after the selection start and end are set */
3149 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3150 EDIT_EM_ScrollCaret(es);
3151 HeapFree(GetProcessHeap(), 0, utext);
3153 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3154 es->undo_insert_count, debugstr_w(es->undo_text));
3155 return TRUE;
3159 /* Helper function for WM_CHAR
3161 * According to an MSDN blog article titled "Just because you're a control
3162 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3163 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3164 * a dialog box or not.
3166 static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3168 WND *pParent;
3169 BOOL r = FALSE;
3171 if (es->hwndParent)
3173 pParent = WIN_GetPtr(es->hwndParent);
3174 if (pParent && pParent != WND_OTHER_PROCESS && pParent != WND_DESKTOP)
3176 if (pParent->flags & WIN_ISDIALOG)
3177 r = TRUE;
3178 WIN_ReleasePtr(pParent);
3181 return r;
3185 /*********************************************************************
3187 * WM_PASTE
3190 static void EDIT_WM_Paste(EDITSTATE *es)
3192 HGLOBAL hsrc;
3193 LPWSTR src;
3195 /* Protect read-only edit control from modification */
3196 if(es->style & ES_READONLY)
3197 return;
3199 OpenClipboard(es->hwndSelf);
3200 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3201 src = GlobalLock(hsrc);
3202 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
3203 GlobalUnlock(hsrc);
3205 else if (es->style & ES_PASSWORD) {
3206 /* clear selected text in password edit box even with empty clipboard */
3207 const WCHAR empty_strW[] = { 0 };
3208 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
3210 CloseClipboard();
3214 /*********************************************************************
3216 * WM_COPY
3219 static void EDIT_WM_Copy(EDITSTATE *es)
3221 INT s = min(es->selection_start, es->selection_end);
3222 INT e = max(es->selection_start, es->selection_end);
3223 HGLOBAL hdst;
3224 LPWSTR dst;
3225 DWORD len;
3227 if (e == s) return;
3229 len = e - s;
3230 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3231 dst = GlobalLock(hdst);
3232 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3233 dst[len] = 0; /* ensure 0 termination */
3234 TRACE("%s\n", debugstr_w(dst));
3235 GlobalUnlock(hdst);
3236 OpenClipboard(es->hwndSelf);
3237 EmptyClipboard();
3238 SetClipboardData(CF_UNICODETEXT, hdst);
3239 CloseClipboard();
3243 /*********************************************************************
3245 * WM_CLEAR
3248 static inline void EDIT_WM_Clear(EDITSTATE *es)
3250 static const WCHAR empty_stringW[] = {0};
3252 /* Protect read-only edit control from modification */
3253 if(es->style & ES_READONLY)
3254 return;
3256 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3260 /*********************************************************************
3262 * WM_CUT
3265 static inline void EDIT_WM_Cut(EDITSTATE *es)
3267 EDIT_WM_Copy(es);
3268 EDIT_WM_Clear(es);
3272 /*********************************************************************
3274 * WM_CHAR
3277 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3279 BOOL control;
3281 control = GetKeyState(VK_CONTROL) & 0x8000;
3283 switch (c) {
3284 case '\r':
3285 /* If it's not a multiline edit box, it would be ignored below.
3286 * For multiline edit without ES_WANTRETURN, we have to make a
3287 * special case.
3289 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3290 if (EDIT_IsInsideDialog(es))
3291 break;
3292 case '\n':
3293 if (es->style & ES_MULTILINE) {
3294 if (es->style & ES_READONLY) {
3295 EDIT_MoveHome(es, FALSE, FALSE);
3296 EDIT_MoveDown_ML(es, FALSE);
3297 } else {
3298 static const WCHAR cr_lfW[] = {'\r','\n',0};
3299 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3302 break;
3303 case '\t':
3304 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3306 static const WCHAR tabW[] = {'\t',0};
3307 if (EDIT_IsInsideDialog(es))
3308 break;
3309 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3311 break;
3312 case VK_BACK:
3313 if (!(es->style & ES_READONLY) && !control) {
3314 if (es->selection_start != es->selection_end)
3315 EDIT_WM_Clear(es);
3316 else {
3317 /* delete character left of caret */
3318 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3319 EDIT_MoveBackward(es, TRUE);
3320 EDIT_WM_Clear(es);
3323 break;
3324 case 0x03: /* ^C */
3325 if (!(es->style & ES_PASSWORD))
3326 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3327 break;
3328 case 0x16: /* ^V */
3329 if (!(es->style & ES_READONLY))
3330 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3331 break;
3332 case 0x18: /* ^X */
3333 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3334 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3335 break;
3336 case 0x1A: /* ^Z */
3337 if (!(es->style & ES_READONLY))
3338 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3339 break;
3341 default:
3342 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3343 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3344 break;
3346 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3347 WCHAR str[2];
3348 str[0] = c;
3349 str[1] = '\0';
3350 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3352 break;
3354 return 1;
3358 /*********************************************************************
3360 * WM_COMMAND
3363 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3365 if (code || control)
3366 return;
3368 switch (id) {
3369 case EM_UNDO:
3370 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3371 break;
3372 case WM_CUT:
3373 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3374 break;
3375 case WM_COPY:
3376 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3377 break;
3378 case WM_PASTE:
3379 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3380 break;
3381 case WM_CLEAR:
3382 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3383 break;
3384 case EM_SETSEL:
3385 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3386 EDIT_EM_ScrollCaret(es);
3387 break;
3388 default:
3389 ERR("unknown menu item, please report\n");
3390 break;
3395 /*********************************************************************
3397 * WM_CONTEXTMENU
3399 * Note: the resource files resource/sysres_??.rc cannot define a
3400 * single popup menu. Hence we use a (dummy) menubar
3401 * containing the single popup menu as its first item.
3403 * FIXME: the message identifiers have been chosen arbitrarily,
3404 * hence we use MF_BYPOSITION.
3405 * We might as well use the "real" values (anybody knows ?)
3406 * The menu definition is in resources/sysres_??.rc.
3407 * Once these are OK, we better use MF_BYCOMMAND here
3408 * (as we do in EDIT_WM_Command()).
3411 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3413 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3414 HMENU popup = GetSubMenu(menu, 0);
3415 UINT start = es->selection_start;
3416 UINT end = es->selection_end;
3418 ORDER_UINT(start, end);
3420 /* undo */
3421 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3422 /* cut */
3423 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3424 /* copy */
3425 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3426 /* paste */
3427 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3428 /* delete */
3429 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3430 /* select all */
3431 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3433 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3435 RECT rc;
3436 /* Windows places the menu at the edit's center in this case */
3437 GetClientRect(es->hwndSelf, &rc);
3438 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3439 x = rc.left + (rc.right - rc.left) / 2;
3440 y = rc.top + (rc.bottom - rc.top) / 2;
3443 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
3444 DestroyMenu(menu);
3448 /*********************************************************************
3450 * WM_GETTEXT
3453 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3455 if(!count) return 0;
3457 if(unicode)
3459 lstrcpynW(dst, es->text, count);
3460 return strlenW(dst);
3462 else
3464 LPSTR textA = (LPSTR)dst;
3465 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3466 textA[count - 1] = 0; /* ensure 0 termination */
3467 return strlen(textA);
3471 /*********************************************************************
3473 * EDIT_CheckCombo
3476 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3478 HWND hLBox = es->hwndListBox;
3479 HWND hCombo;
3480 BOOL bDropped;
3481 int nEUI;
3483 if (!hLBox)
3484 return FALSE;
3486 hCombo = GetParent(es->hwndSelf);
3487 bDropped = TRUE;
3488 nEUI = 0;
3490 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3492 if (key == VK_UP || key == VK_DOWN)
3494 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3495 nEUI = 1;
3497 if (msg == WM_KEYDOWN || nEUI)
3498 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3501 switch (msg)
3503 case WM_KEYDOWN:
3504 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3506 /* make sure ComboLBox pops up */
3507 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3508 key = VK_F4;
3509 nEUI = 2;
3512 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3513 break;
3515 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3516 if (nEUI)
3517 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3518 else
3519 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3520 break;
3523 if(nEUI == 2)
3524 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3526 return TRUE;
3530 /*********************************************************************
3532 * WM_KEYDOWN
3534 * Handling of special keys that don't produce a WM_CHAR
3535 * (i.e. non-printable keys) & Backspace & Delete
3538 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3540 BOOL shift;
3541 BOOL control;
3543 if (GetKeyState(VK_MENU) & 0x8000)
3544 return 0;
3546 shift = GetKeyState(VK_SHIFT) & 0x8000;
3547 control = GetKeyState(VK_CONTROL) & 0x8000;
3549 switch (key) {
3550 case VK_F4:
3551 case VK_UP:
3552 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3553 break;
3555 /* fall through */
3556 case VK_LEFT:
3557 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3558 EDIT_MoveUp_ML(es, shift);
3559 else
3560 if (control)
3561 EDIT_MoveWordBackward(es, shift);
3562 else
3563 EDIT_MoveBackward(es, shift);
3564 break;
3565 case VK_DOWN:
3566 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3567 break;
3568 /* fall through */
3569 case VK_RIGHT:
3570 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3571 EDIT_MoveDown_ML(es, shift);
3572 else if (control)
3573 EDIT_MoveWordForward(es, shift);
3574 else
3575 EDIT_MoveForward(es, shift);
3576 break;
3577 case VK_HOME:
3578 EDIT_MoveHome(es, shift, control);
3579 break;
3580 case VK_END:
3581 EDIT_MoveEnd(es, shift, control);
3582 break;
3583 case VK_PRIOR:
3584 if (es->style & ES_MULTILINE)
3585 EDIT_MovePageUp_ML(es, shift);
3586 else
3587 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3588 break;
3589 case VK_NEXT:
3590 if (es->style & ES_MULTILINE)
3591 EDIT_MovePageDown_ML(es, shift);
3592 else
3593 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3594 break;
3595 case VK_DELETE:
3596 if (!(es->style & ES_READONLY) && !(shift && control)) {
3597 if (es->selection_start != es->selection_end) {
3598 if (shift)
3599 EDIT_WM_Cut(es);
3600 else
3601 EDIT_WM_Clear(es);
3602 } else {
3603 if (shift) {
3604 /* delete character left of caret */
3605 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3606 EDIT_MoveBackward(es, TRUE);
3607 EDIT_WM_Clear(es);
3608 } else if (control) {
3609 /* delete to end of line */
3610 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3611 EDIT_MoveEnd(es, TRUE, FALSE);
3612 EDIT_WM_Clear(es);
3613 } else {
3614 /* delete character right of caret */
3615 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3616 EDIT_MoveForward(es, TRUE);
3617 EDIT_WM_Clear(es);
3621 break;
3622 case VK_INSERT:
3623 if (shift) {
3624 if (!(es->style & ES_READONLY))
3625 EDIT_WM_Paste(es);
3626 } else if (control)
3627 EDIT_WM_Copy(es);
3628 break;
3629 case VK_RETURN:
3630 /* If the edit doesn't want the return send a message to the default object */
3631 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3633 HWND hwndParent;
3634 DWORD dw;
3636 if (!EDIT_IsInsideDialog(es)) return 1;
3637 if (control) break;
3638 hwndParent = GetParent(es->hwndSelf);
3639 dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
3640 if (HIWORD(dw) == DC_HASDEFID)
3642 SendMessageW( hwndParent, WM_COMMAND,
3643 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3644 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3646 else
3647 SendMessageW( hwndParent, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndParent, IDOK ) );
3649 break;
3650 case VK_ESCAPE:
3651 if (!(es->style & ES_MULTILINE))
3652 SendMessageW(GetParent(es->hwndSelf), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( GetParent(es->hwndSelf), IDCANCEL ) );
3653 break;
3654 case VK_TAB:
3655 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3656 break;
3658 return 0;
3662 /*********************************************************************
3664 * WM_KILLFOCUS
3667 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3669 es->flags &= ~EF_FOCUSED;
3670 DestroyCaret();
3671 if(!(es->style & ES_NOHIDESEL))
3672 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3673 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3674 return 0;
3678 /*********************************************************************
3680 * WM_LBUTTONDBLCLK
3682 * The caret position has been set on the WM_LBUTTONDOWN message
3685 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3687 INT s;
3688 INT e = es->selection_end;
3689 INT l;
3690 INT li;
3691 INT ll;
3693 es->bCaptureState = TRUE;
3694 SetCapture(es->hwndSelf);
3696 l = EDIT_EM_LineFromChar(es, e);
3697 li = EDIT_EM_LineIndex(es, l);
3698 ll = EDIT_EM_LineLength(es, e);
3699 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3700 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3701 EDIT_EM_SetSel(es, s, e, FALSE);
3702 EDIT_EM_ScrollCaret(es);
3703 es->region_posx = es->region_posy = 0;
3704 SetTimer(es->hwndSelf, 0, 100, NULL);
3705 return 0;
3709 /*********************************************************************
3711 * WM_LBUTTONDOWN
3714 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3716 INT e;
3717 BOOL after_wrap;
3719 es->bCaptureState = TRUE;
3720 SetCapture(es->hwndSelf);
3721 EDIT_ConfinePoint(es, &x, &y);
3722 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3723 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3724 EDIT_EM_ScrollCaret(es);
3725 es->region_posx = es->region_posy = 0;
3726 SetTimer(es->hwndSelf, 0, 100, NULL);
3728 if (!(es->flags & EF_FOCUSED))
3729 SetFocus(es->hwndSelf);
3731 return 0;
3735 /*********************************************************************
3737 * WM_LBUTTONUP
3740 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3742 if (es->bCaptureState) {
3743 KillTimer(es->hwndSelf, 0);
3744 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3746 es->bCaptureState = FALSE;
3747 return 0;
3751 /*********************************************************************
3753 * WM_MBUTTONDOWN
3756 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3758 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3759 return 0;
3763 /*********************************************************************
3765 * WM_MOUSEMOVE
3768 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3770 INT e;
3771 BOOL after_wrap;
3772 INT prex, prey;
3774 /* If the mouse has been captured by process other than the edit control itself,
3775 * the windows edit controls will not select the strings with mouse move.
3777 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3778 return 0;
3781 * FIXME: gotta do some scrolling if outside client
3782 * area. Maybe reset the timer ?
3784 prex = x; prey = y;
3785 EDIT_ConfinePoint(es, &x, &y);
3786 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3787 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3788 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3789 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3790 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3791 return 0;
3795 /*********************************************************************
3797 * WM_PAINT
3800 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3802 PAINTSTRUCT ps;
3803 INT i;
3804 HDC dc;
3805 HFONT old_font = 0;
3806 RECT rc;
3807 RECT rcClient;
3808 RECT rcLine;
3809 RECT rcRgn;
3810 HBRUSH brush;
3811 HBRUSH old_brush;
3812 INT bw, bh;
3813 BOOL rev = es->bEnableState &&
3814 ((es->flags & EF_FOCUSED) ||
3815 (es->style & ES_NOHIDESEL));
3816 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3818 GetClientRect(es->hwndSelf, &rcClient);
3820 /* get the background brush */
3821 brush = EDIT_NotifyCtlColor(es, dc);
3823 /* paint the border and the background */
3824 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3826 if(es->style & WS_BORDER) {
3827 bw = GetSystemMetrics(SM_CXBORDER);
3828 bh = GetSystemMetrics(SM_CYBORDER);
3829 rc = rcClient;
3830 if(es->style & ES_MULTILINE) {
3831 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3832 if(es->style & WS_VSCROLL) rc.right+=bw;
3835 /* Draw the frame. Same code as in nonclient.c */
3836 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3837 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3838 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3839 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3840 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3841 SelectObject(dc, old_brush);
3843 /* Keep the border clean */
3844 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3845 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3848 GetClipBox(dc, &rc);
3849 FillRect(dc, &rc, brush);
3851 IntersectClipRect(dc, es->format_rect.left,
3852 es->format_rect.top,
3853 es->format_rect.right,
3854 es->format_rect.bottom);
3855 if (es->style & ES_MULTILINE) {
3856 rc = rcClient;
3857 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3859 if (es->font)
3860 old_font = SelectObject(dc, es->font);
3862 if (!es->bEnableState)
3863 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3864 GetClipBox(dc, &rcRgn);
3865 if (es->style & ES_MULTILINE) {
3866 INT vlc = get_vertical_line_count(es);
3867 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3868 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3869 if (IntersectRect(&rc, &rcRgn, &rcLine))
3870 EDIT_PaintLine(es, dc, i, rev);
3872 } else {
3873 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3874 if (IntersectRect(&rc, &rcRgn, &rcLine))
3875 EDIT_PaintLine(es, dc, 0, rev);
3877 if (es->font)
3878 SelectObject(dc, old_font);
3880 if (!hdc)
3881 EndPaint(es->hwndSelf, &ps);
3885 /*********************************************************************
3887 * WM_SETFOCUS
3890 static void EDIT_WM_SetFocus(EDITSTATE *es)
3892 es->flags |= EF_FOCUSED;
3894 if (!(es->style & ES_NOHIDESEL))
3895 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3897 /* single line edit updates itself */
3898 if (!(es->style & ES_MULTILINE))
3900 HDC hdc = GetDC(es->hwndSelf);
3901 EDIT_WM_Paint(es, hdc);
3902 ReleaseDC(es->hwndSelf, hdc);
3905 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
3906 EDIT_SetCaretPos(es, es->selection_end,
3907 es->flags & EF_AFTER_WRAP);
3908 ShowCaret(es->hwndSelf);
3909 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3913 /*********************************************************************
3915 * WM_SETFONT
3917 * With Win95 look the margins are set to default font value unless
3918 * the system font (font == 0) is being set, in which case they are left
3919 * unchanged.
3922 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3924 TEXTMETRICW tm;
3925 HDC dc;
3926 HFONT old_font = 0;
3927 RECT clientRect;
3929 es->font = font;
3930 dc = GetDC(es->hwndSelf);
3931 if (font)
3932 old_font = SelectObject(dc, font);
3933 GetTextMetricsW(dc, &tm);
3934 es->line_height = tm.tmHeight;
3935 es->char_width = tm.tmAveCharWidth;
3936 if (font)
3937 SelectObject(dc, old_font);
3938 ReleaseDC(es->hwndSelf, dc);
3940 /* Reset the format rect and the margins */
3941 GetClientRect(es->hwndSelf, &clientRect);
3942 EDIT_SetRectNP(es, &clientRect);
3943 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3944 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3946 if (es->style & ES_MULTILINE)
3947 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3948 else
3949 EDIT_CalcLineWidth_SL(es);
3951 if (redraw)
3952 EDIT_UpdateText(es, NULL, TRUE);
3953 if (es->flags & EF_FOCUSED) {
3954 DestroyCaret();
3955 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
3956 EDIT_SetCaretPos(es, es->selection_end,
3957 es->flags & EF_AFTER_WRAP);
3958 ShowCaret(es->hwndSelf);
3963 /*********************************************************************
3965 * WM_SETTEXT
3967 * NOTES
3968 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3969 * The modified flag is reset. No notifications are sent.
3971 * For single-line controls, reception of WM_SETTEXT triggers:
3972 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3975 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3977 LPWSTR textW = NULL;
3978 if (!unicode && text)
3980 LPCSTR textA = (LPCSTR)text;
3981 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3982 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3983 if (textW)
3984 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3985 text = textW;
3988 if (es->flags & EF_UPDATE)
3989 /* fixed this bug once; complain if we see it about to happen again. */
3990 ERR("SetSel may generate UPDATE message whose handler may reset "
3991 "selection.\n");
3993 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3994 if (text)
3996 TRACE("%s\n", debugstr_w(text));
3997 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3998 if(!unicode)
3999 HeapFree(GetProcessHeap(), 0, textW);
4001 else
4003 static const WCHAR empty_stringW[] = {0};
4004 TRACE("<NULL>\n");
4005 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
4007 es->x_offset = 0;
4008 es->flags &= ~EF_MODIFIED;
4009 EDIT_EM_SetSel(es, 0, 0, FALSE);
4011 /* Send the notification after the selection start and end have been set
4012 * edit control doesn't send notification on WM_SETTEXT
4013 * if it is multiline, or it is part of combobox
4015 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4017 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
4018 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4020 EDIT_EM_ScrollCaret(es);
4021 EDIT_UpdateScrollInfo(es);
4025 /*********************************************************************
4027 * WM_SIZE
4030 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
4032 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4033 RECT rc;
4034 TRACE("width = %d, height = %d\n", width, height);
4035 SetRect(&rc, 0, 0, width, height);
4036 EDIT_SetRectNP(es, &rc);
4037 EDIT_UpdateText(es, NULL, TRUE);
4042 /*********************************************************************
4044 * WM_STYLECHANGED
4046 * This message is sent by SetWindowLong on having changed either the Style
4047 * or the extended style.
4049 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4051 * See also EDIT_WM_NCCreate
4053 * It appears that the Windows version of the edit control allows the style
4054 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4055 * style variable which will generally be different. In this function we
4056 * update the internal style based on what changed in the externally visible
4057 * style.
4059 * Much of this content as based upon the MSDN, especially:
4060 * Platform SDK Documentation -> User Interface Services ->
4061 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4062 * Edit Control Styles
4064 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
4066 if (GWL_STYLE == which) {
4067 DWORD style_change_mask;
4068 DWORD new_style;
4069 /* Only a subset of changes can be applied after the control
4070 * has been created.
4072 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4073 ES_NUMBER;
4074 if (es->style & ES_MULTILINE)
4075 style_change_mask |= ES_WANTRETURN;
4077 new_style = style->styleNew & style_change_mask;
4079 /* Number overrides lowercase overrides uppercase (at least it
4080 * does in Win95). However I'll bet that ES_NUMBER would be
4081 * invalid under Win 3.1.
4083 if (new_style & ES_NUMBER) {
4084 ; /* do not override the ES_NUMBER */
4085 } else if (new_style & ES_LOWERCASE) {
4086 new_style &= ~ES_UPPERCASE;
4089 es->style = (es->style & ~style_change_mask) | new_style;
4090 } else if (GWL_EXSTYLE == which) {
4091 ; /* FIXME - what is needed here */
4092 } else {
4093 WARN ("Invalid style change %ld\n",which);
4096 return 0;
4099 /*********************************************************************
4101 * WM_SYSKEYDOWN
4104 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
4106 if ((key == VK_BACK) && (key_data & 0x2000)) {
4107 if (EDIT_EM_CanUndo(es))
4108 EDIT_EM_Undo(es);
4109 return 0;
4110 } else if (key == VK_UP || key == VK_DOWN) {
4111 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
4112 return 0;
4114 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4118 /*********************************************************************
4120 * WM_TIMER
4123 static void EDIT_WM_Timer(EDITSTATE *es)
4125 if (es->region_posx < 0) {
4126 EDIT_MoveBackward(es, TRUE);
4127 } else if (es->region_posx > 0) {
4128 EDIT_MoveForward(es, TRUE);
4131 * FIXME: gotta do some vertical scrolling here, like
4132 * EDIT_EM_LineScroll(hwnd, 0, 1);
4136 /*********************************************************************
4138 * WM_HSCROLL
4141 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4143 INT dx;
4144 INT fw;
4146 if (!(es->style & ES_MULTILINE))
4147 return 0;
4149 if (!(es->style & ES_AUTOHSCROLL))
4150 return 0;
4152 dx = 0;
4153 fw = es->format_rect.right - es->format_rect.left;
4154 switch (action) {
4155 case SB_LINELEFT:
4156 TRACE("SB_LINELEFT\n");
4157 if (es->x_offset)
4158 dx = -es->char_width;
4159 break;
4160 case SB_LINERIGHT:
4161 TRACE("SB_LINERIGHT\n");
4162 if (es->x_offset < es->text_width)
4163 dx = es->char_width;
4164 break;
4165 case SB_PAGELEFT:
4166 TRACE("SB_PAGELEFT\n");
4167 if (es->x_offset)
4168 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4169 break;
4170 case SB_PAGERIGHT:
4171 TRACE("SB_PAGERIGHT\n");
4172 if (es->x_offset < es->text_width)
4173 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4174 break;
4175 case SB_LEFT:
4176 TRACE("SB_LEFT\n");
4177 if (es->x_offset)
4178 dx = -es->x_offset;
4179 break;
4180 case SB_RIGHT:
4181 TRACE("SB_RIGHT\n");
4182 if (es->x_offset < es->text_width)
4183 dx = es->text_width - es->x_offset;
4184 break;
4185 case SB_THUMBTRACK:
4186 TRACE("SB_THUMBTRACK %d\n", pos);
4187 es->flags |= EF_HSCROLL_TRACK;
4188 if(es->style & WS_HSCROLL)
4189 dx = pos - es->x_offset;
4190 else
4192 INT fw, new_x;
4193 /* Sanity check */
4194 if(pos < 0 || pos > 100) return 0;
4195 /* Assume default scroll range 0-100 */
4196 fw = es->format_rect.right - es->format_rect.left;
4197 new_x = pos * (es->text_width - fw) / 100;
4198 dx = es->text_width ? (new_x - es->x_offset) : 0;
4200 break;
4201 case SB_THUMBPOSITION:
4202 TRACE("SB_THUMBPOSITION %d\n", pos);
4203 es->flags &= ~EF_HSCROLL_TRACK;
4204 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4205 dx = pos - es->x_offset;
4206 else
4208 INT fw, new_x;
4209 /* Sanity check */
4210 if(pos < 0 || pos > 100) return 0;
4211 /* Assume default scroll range 0-100 */
4212 fw = es->format_rect.right - es->format_rect.left;
4213 new_x = pos * (es->text_width - fw) / 100;
4214 dx = es->text_width ? (new_x - es->x_offset) : 0;
4216 if (!dx) {
4217 /* force scroll info update */
4218 EDIT_UpdateScrollInfo(es);
4219 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4221 break;
4222 case SB_ENDSCROLL:
4223 TRACE("SB_ENDSCROLL\n");
4224 break;
4226 * FIXME : the next two are undocumented !
4227 * Are we doing the right thing ?
4228 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4229 * although it's also a regular control message.
4231 case EM_GETTHUMB: /* this one is used by NT notepad */
4232 case EM_GETTHUMB16:
4234 LRESULT ret;
4235 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4236 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4237 else
4239 /* Assume default scroll range 0-100 */
4240 INT fw = es->format_rect.right - es->format_rect.left;
4241 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4243 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4244 return ret;
4246 case EM_LINESCROLL16:
4247 TRACE("EM_LINESCROLL16\n");
4248 dx = pos;
4249 break;
4251 default:
4252 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4253 action, action);
4254 return 0;
4256 if (dx)
4258 INT fw = es->format_rect.right - es->format_rect.left;
4259 /* check if we are going to move too far */
4260 if(es->x_offset + dx + fw > es->text_width)
4261 dx = es->text_width - fw - es->x_offset;
4262 if(dx)
4263 EDIT_EM_LineScroll_internal(es, dx, 0);
4265 return 0;
4269 /*********************************************************************
4271 * WM_VSCROLL
4274 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
4276 INT dy;
4278 if (!(es->style & ES_MULTILINE))
4279 return 0;
4281 if (!(es->style & ES_AUTOVSCROLL))
4282 return 0;
4284 dy = 0;
4285 switch (action) {
4286 case SB_LINEUP:
4287 case SB_LINEDOWN:
4288 case SB_PAGEUP:
4289 case SB_PAGEDOWN:
4290 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4291 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4292 (action == SB_PAGEUP ? "SB_PAGEUP" :
4293 "SB_PAGEDOWN"))));
4294 EDIT_EM_Scroll(es, action);
4295 return 0;
4296 case SB_TOP:
4297 TRACE("SB_TOP\n");
4298 dy = -es->y_offset;
4299 break;
4300 case SB_BOTTOM:
4301 TRACE("SB_BOTTOM\n");
4302 dy = es->line_count - 1 - es->y_offset;
4303 break;
4304 case SB_THUMBTRACK:
4305 TRACE("SB_THUMBTRACK %d\n", pos);
4306 es->flags |= EF_VSCROLL_TRACK;
4307 if(es->style & WS_VSCROLL)
4308 dy = pos - es->y_offset;
4309 else
4311 /* Assume default scroll range 0-100 */
4312 INT vlc, new_y;
4313 /* Sanity check */
4314 if(pos < 0 || pos > 100) return 0;
4315 vlc = get_vertical_line_count(es);
4316 new_y = pos * (es->line_count - vlc) / 100;
4317 dy = es->line_count ? (new_y - es->y_offset) : 0;
4318 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4319 es->line_count, es->y_offset, pos, dy);
4321 break;
4322 case SB_THUMBPOSITION:
4323 TRACE("SB_THUMBPOSITION %d\n", pos);
4324 es->flags &= ~EF_VSCROLL_TRACK;
4325 if(es->style & WS_VSCROLL)
4326 dy = pos - es->y_offset;
4327 else
4329 /* Assume default scroll range 0-100 */
4330 INT vlc, new_y;
4331 /* Sanity check */
4332 if(pos < 0 || pos > 100) return 0;
4333 vlc = get_vertical_line_count(es);
4334 new_y = pos * (es->line_count - vlc) / 100;
4335 dy = es->line_count ? (new_y - es->y_offset) : 0;
4336 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4337 es->line_count, es->y_offset, pos, dy);
4339 if (!dy)
4341 /* force scroll info update */
4342 EDIT_UpdateScrollInfo(es);
4343 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4345 break;
4346 case SB_ENDSCROLL:
4347 TRACE("SB_ENDSCROLL\n");
4348 break;
4350 * FIXME : the next two are undocumented !
4351 * Are we doing the right thing ?
4352 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4353 * although it's also a regular control message.
4355 case EM_GETTHUMB: /* this one is used by NT notepad */
4356 case EM_GETTHUMB16:
4358 LRESULT ret;
4359 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4360 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4361 else
4363 /* Assume default scroll range 0-100 */
4364 INT vlc = get_vertical_line_count(es);
4365 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4367 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4368 return ret;
4370 case EM_LINESCROLL16:
4371 TRACE("EM_LINESCROLL16 %d\n", pos);
4372 dy = pos;
4373 break;
4375 default:
4376 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4377 action, action);
4378 return 0;
4380 if (dy)
4381 EDIT_EM_LineScroll(es, 0, dy);
4382 return 0;
4385 /*********************************************************************
4387 * EM_GETTHUMB
4389 * FIXME: is this right ? (or should it be only VSCROLL)
4390 * (and maybe only for edit controls that really have their
4391 * own scrollbars) (and maybe only for multiline controls ?)
4392 * All in all: very poorly documented
4395 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4397 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
4398 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
4402 /********************************************************************
4404 * The Following code is to handle inline editing from IMEs
4407 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4409 LONG buflen;
4410 LPWSTR lpCompStr = NULL;
4411 LPSTR lpCompStrAttr = NULL;
4412 DWORD dwBufLenAttr;
4414 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4416 if (buflen < 0)
4418 return;
4421 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4422 if (!lpCompStr)
4424 ERR("Unable to allocate IME CompositionString\n");
4425 return;
4428 if (buflen)
4429 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4430 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4432 if (CompFlag & GCS_COMPATTR)
4435 * We do not use the attributes yet. it would tell us what characters
4436 * are in transition and which are converted or decided upon
4438 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4439 if (dwBufLenAttr)
4441 dwBufLenAttr ++;
4442 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4443 if (!lpCompStrAttr)
4445 ERR("Unable to allocate IME Attribute String\n");
4446 HeapFree(GetProcessHeap(),0,lpCompStr);
4447 return;
4449 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4450 dwBufLenAttr);
4451 lpCompStrAttr[dwBufLenAttr] = 0;
4453 else
4454 lpCompStrAttr = NULL;
4457 /* check for change in composition start */
4458 if (es->selection_end < es->composition_start)
4459 es->composition_start = es->selection_end;
4461 /* replace existing selection string */
4462 es->selection_start = es->composition_start;
4464 if (es->composition_len > 0)
4465 es->selection_end = es->composition_start + es->composition_len;
4466 else
4467 es->selection_end = es->selection_start;
4469 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4470 es->composition_len = abs(es->composition_start - es->selection_end);
4472 es->selection_start = es->composition_start;
4473 es->selection_end = es->selection_start + es->composition_len;
4475 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4476 HeapFree(GetProcessHeap(),0,lpCompStr);
4479 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4481 LONG buflen;
4482 LPWSTR lpResultStr;
4484 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4485 if (buflen <= 0)
4487 return;
4490 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4491 if (!lpResultStr)
4493 ERR("Unable to alloc buffer for IME string\n");
4494 return;
4497 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4498 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4500 /* check for change in composition start */
4501 if (es->selection_end < es->composition_start)
4502 es->composition_start = es->selection_end;
4504 es->selection_start = es->composition_start;
4505 es->selection_end = es->composition_start + es->composition_len;
4506 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4507 es->composition_start = es->selection_end;
4508 es->composition_len = 0;
4510 HeapFree(GetProcessHeap(),0,lpResultStr);
4513 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4515 HIMC hIMC;
4516 int cursor;
4518 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4520 static const WCHAR empty_stringW[] = {0};
4521 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4522 es->composition_start = es->selection_end;
4525 hIMC = ImmGetContext(hwnd);
4526 if (!hIMC)
4527 return;
4529 if (CompFlag & GCS_RESULTSTR)
4530 EDIT_GetResultStr(hIMC, es);
4531 if (CompFlag & GCS_COMPSTR)
4532 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4533 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4534 ImmReleaseContext(hwnd, hIMC);
4535 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4539 /*********************************************************************
4541 * WM_NCCREATE
4543 * See also EDIT_WM_StyleChanged
4545 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4547 EDITSTATE *es;
4548 UINT alloc_size;
4550 TRACE("Creating %s edit control, style = %08x\n",
4551 unicode ? "Unicode" : "ANSI", lpcs->style);
4553 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4554 return FALSE;
4555 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4558 * Note: since the EDITSTATE has not been fully initialized yet,
4559 * we can't use any API calls that may send
4560 * WM_XXX messages before WM_NCCREATE is completed.
4563 es->is_unicode = unicode;
4564 es->style = lpcs->style;
4566 es->bEnableState = !(es->style & WS_DISABLED);
4568 es->hwndSelf = hwnd;
4569 /* Save parent, which will be notified by EN_* messages */
4570 es->hwndParent = lpcs->hwndParent;
4572 if (es->style & ES_COMBO)
4573 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4575 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4576 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4578 /* Number overrides lowercase overrides uppercase (at least it
4579 * does in Win95). However I'll bet that ES_NUMBER would be
4580 * invalid under Win 3.1.
4582 if (es->style & ES_NUMBER) {
4583 ; /* do not override the ES_NUMBER */
4584 } else if (es->style & ES_LOWERCASE) {
4585 es->style &= ~ES_UPPERCASE;
4587 if (es->style & ES_MULTILINE) {
4588 es->buffer_limit = BUFLIMIT_INITIAL;
4589 if (es->style & WS_VSCROLL)
4590 es->style |= ES_AUTOVSCROLL;
4591 if (es->style & WS_HSCROLL)
4592 es->style |= ES_AUTOHSCROLL;
4593 es->style &= ~ES_PASSWORD;
4594 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4595 /* Confirmed - RIGHT overrides CENTER */
4596 if (es->style & ES_RIGHT)
4597 es->style &= ~ES_CENTER;
4598 es->style &= ~WS_HSCROLL;
4599 es->style &= ~ES_AUTOHSCROLL;
4601 } else {
4602 es->buffer_limit = BUFLIMIT_INITIAL;
4603 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4604 es->style &= ~ES_CENTER;
4605 es->style &= ~WS_HSCROLL;
4606 es->style &= ~WS_VSCROLL;
4607 if (es->style & ES_PASSWORD)
4608 es->password_char = '*';
4611 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4612 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4613 return FALSE;
4614 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4616 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4617 return FALSE;
4618 es->undo_buffer_size = es->buffer_size;
4620 if (es->style & ES_MULTILINE)
4621 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4622 return FALSE;
4623 es->line_count = 1;
4626 * In Win95 look and feel, the WS_BORDER style is replaced by the
4627 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4628 * control a nonclient area so we don't need to draw the border.
4629 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4630 * a nonclient area and we should handle painting the border ourselves.
4632 * When making modifications please ensure that the code still works
4633 * for edit controls created directly with style 0x50800000, exStyle 0
4634 * (which should have a single pixel border)
4636 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4637 es->style &= ~WS_BORDER;
4638 else if (es->style & WS_BORDER)
4639 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4641 return TRUE;
4645 /*********************************************************************
4647 * WM_CREATE
4650 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4652 RECT clientRect;
4654 TRACE("%s\n", debugstr_w(name));
4656 * To initialize some final structure members, we call some helper
4657 * functions. However, since the EDITSTATE is not consistent (i.e.
4658 * not fully initialized), we should be very careful which
4659 * functions can be called, and in what order.
4661 EDIT_WM_SetFont(es, 0, FALSE);
4662 EDIT_EM_EmptyUndoBuffer(es);
4664 /* We need to calculate the format rect
4665 (applications may send EM_SETMARGINS before the control gets visible) */
4666 GetClientRect(es->hwndSelf, &clientRect);
4667 EDIT_SetRectNP(es, &clientRect);
4669 if (name && *name) {
4670 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4671 /* if we insert text to the editline, the text scrolls out
4672 * of the window, as the caret is placed after the insert
4673 * pos normally; thus we reset es->selection... to 0 and
4674 * update caret
4676 es->selection_start = es->selection_end = 0;
4677 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4678 * Messages are only to be sent when the USER does something to
4679 * change the contents. So I am removing this EN_CHANGE
4681 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4683 EDIT_EM_ScrollCaret(es);
4685 /* force scroll info update */
4686 EDIT_UpdateScrollInfo(es);
4687 /* The rule seems to return 1 here for success */
4688 /* Power Builder masked edit controls will crash */
4689 /* if not. */
4690 /* FIXME: is that in all cases so ? */
4691 return 1;
4695 /*********************************************************************
4697 * WM_DESTROY
4700 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4702 LINEDEF *pc, *pp;
4704 if (es->hloc32W) {
4705 LocalFree(es->hloc32W);
4707 if (es->hloc32A) {
4708 LocalFree(es->hloc32A);
4710 if (es->hloc16) {
4711 STACK16FRAME* stack16 = MapSL(PtrToUlong(NtCurrentTeb()->WOW32Reserved));
4712 HANDLE16 oldDS = stack16->ds;
4714 stack16->ds = GetWindowLongPtrW( es->hwndSelf, GWLP_HINSTANCE );
4715 while (LocalUnlock16(es->hloc16)) ;
4716 LocalFree16(es->hloc16);
4717 stack16->ds = oldDS;
4720 pc = es->first_line_def;
4721 while (pc)
4723 pp = pc->next;
4724 HeapFree(GetProcessHeap(), 0, pc);
4725 pc = pp;
4728 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4729 HeapFree(GetProcessHeap(), 0, es);
4731 return 0;
4735 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4737 if(unicode)
4738 return DefWindowProcW(hwnd, msg, wParam, lParam);
4739 else
4740 return DefWindowProcA(hwnd, msg, wParam, lParam);
4743 /*********************************************************************
4745 * EditWndProc_common
4747 * The messages are in the order of the actual integer values
4748 * (which can be found in include/windows.h)
4749 * Wherever possible the 16 bit versions are converted to
4750 * the 32 bit ones, so that we can 'fall through' to the
4751 * helper functions. These are mostly 32 bit (with a few
4752 * exceptions, clearly indicated by a '16' extension to their
4753 * names).
4756 static LRESULT EditWndProc_common( HWND hwnd, UINT msg,
4757 WPARAM wParam, LPARAM lParam, BOOL unicode )
4759 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4760 LRESULT result = 0;
4762 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4764 if (!es && msg != WM_NCCREATE)
4765 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4767 if (es && (msg != WM_DESTROY)) EDIT_LockBuffer(es);
4769 switch (msg) {
4770 case EM_GETSEL16:
4771 wParam = 0;
4772 lParam = 0;
4773 /* fall through */
4774 case EM_GETSEL:
4775 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4776 break;
4778 case EM_SETSEL16:
4779 if ((short)LOWORD(lParam) == -1)
4780 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4781 else
4782 EDIT_EM_SetSel(es, LOWORD(lParam), HIWORD(lParam), FALSE);
4783 if (!wParam)
4784 EDIT_EM_ScrollCaret(es);
4785 result = 1;
4786 break;
4787 case EM_SETSEL:
4788 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4789 EDIT_EM_ScrollCaret(es);
4790 result = 1;
4791 break;
4793 case EM_GETRECT16:
4794 if (lParam)
4796 RECT16 *r16 = MapSL(lParam);
4797 r16->left = es->format_rect.left;
4798 r16->top = es->format_rect.top;
4799 r16->right = es->format_rect.right;
4800 r16->bottom = es->format_rect.bottom;
4802 break;
4803 case EM_GETRECT:
4804 if (lParam)
4805 CopyRect((LPRECT)lParam, &es->format_rect);
4806 break;
4808 case EM_SETRECT16:
4809 if ((es->style & ES_MULTILINE) && lParam) {
4810 RECT rc;
4811 RECT16 *r16 = MapSL(lParam);
4812 rc.left = r16->left;
4813 rc.top = r16->top;
4814 rc.right = r16->right;
4815 rc.bottom = r16->bottom;
4816 EDIT_SetRectNP(es, &rc);
4817 EDIT_UpdateText(es, NULL, TRUE);
4819 break;
4820 case EM_SETRECT:
4821 if ((es->style & ES_MULTILINE) && lParam) {
4822 EDIT_SetRectNP(es, (LPRECT)lParam);
4823 EDIT_UpdateText(es, NULL, TRUE);
4825 break;
4827 case EM_SETRECTNP16:
4828 if ((es->style & ES_MULTILINE) && lParam) {
4829 RECT rc;
4830 RECT16 *r16 = MapSL(lParam);
4831 rc.left = r16->left;
4832 rc.top = r16->top;
4833 rc.right = r16->right;
4834 rc.bottom = r16->bottom;
4835 EDIT_SetRectNP(es, &rc);
4837 break;
4838 case EM_SETRECTNP:
4839 if ((es->style & ES_MULTILINE) && lParam)
4840 EDIT_SetRectNP(es, (LPRECT)lParam);
4841 break;
4843 case EM_SCROLL16:
4844 case EM_SCROLL:
4845 result = EDIT_EM_Scroll(es, (INT)wParam);
4846 break;
4848 case EM_LINESCROLL16:
4849 wParam = (WPARAM)(INT)(SHORT)HIWORD(lParam);
4850 lParam = (LPARAM)(INT)(SHORT)LOWORD(lParam);
4851 /* fall through */
4852 case EM_LINESCROLL:
4853 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4854 break;
4856 case EM_SCROLLCARET16:
4857 case EM_SCROLLCARET:
4858 EDIT_EM_ScrollCaret(es);
4859 result = 1;
4860 break;
4862 case EM_GETMODIFY16:
4863 case EM_GETMODIFY:
4864 result = ((es->flags & EF_MODIFIED) != 0);
4865 break;
4867 case EM_SETMODIFY16:
4868 case EM_SETMODIFY:
4869 if (wParam)
4870 es->flags |= EF_MODIFIED;
4871 else
4872 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4873 break;
4875 case EM_GETLINECOUNT16:
4876 case EM_GETLINECOUNT:
4877 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4878 break;
4880 case EM_LINEINDEX16:
4881 if ((INT16)wParam == -1)
4882 wParam = (WPARAM)-1;
4883 /* fall through */
4884 case EM_LINEINDEX:
4885 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4886 break;
4888 case EM_SETHANDLE16:
4889 EDIT_EM_SetHandle16(es, (HLOCAL16)wParam);
4890 break;
4891 case EM_SETHANDLE:
4892 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4893 break;
4895 case EM_GETHANDLE16:
4896 result = (LRESULT)EDIT_EM_GetHandle16(es);
4897 break;
4898 case EM_GETHANDLE:
4899 result = (LRESULT)EDIT_EM_GetHandle(es);
4900 break;
4902 case EM_GETTHUMB16:
4903 case EM_GETTHUMB:
4904 result = EDIT_EM_GetThumb(es);
4905 break;
4907 /* these messages missing from specs */
4908 case WM_USER+15:
4909 case 0x00bf:
4910 case WM_USER+16:
4911 case 0x00c0:
4912 case WM_USER+19:
4913 case 0x00c3:
4914 case WM_USER+26:
4915 case 0x00ca:
4916 FIXME("undocumented message 0x%x, please report\n", msg);
4917 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4918 break;
4920 case EM_LINELENGTH16:
4921 case EM_LINELENGTH:
4922 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4923 break;
4925 case EM_REPLACESEL16:
4926 lParam = (LPARAM)MapSL(lParam);
4927 unicode = FALSE; /* 16-bit message is always ascii */
4928 /* fall through */
4929 case EM_REPLACESEL:
4931 LPWSTR textW;
4933 if(unicode)
4934 textW = (LPWSTR)lParam;
4935 else
4937 LPSTR textA = (LPSTR)lParam;
4938 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4939 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4940 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4943 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4944 result = 1;
4946 if(!unicode)
4947 HeapFree(GetProcessHeap(), 0, textW);
4948 break;
4951 case EM_GETLINE16:
4952 lParam = (LPARAM)MapSL(lParam);
4953 unicode = FALSE; /* 16-bit message is always ascii */
4954 /* fall through */
4955 case EM_GETLINE:
4956 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4957 break;
4959 case EM_LIMITTEXT16:
4960 case EM_SETLIMITTEXT:
4961 EDIT_EM_SetLimitText(es, wParam);
4962 break;
4964 case EM_CANUNDO16:
4965 case EM_CANUNDO:
4966 result = (LRESULT)EDIT_EM_CanUndo(es);
4967 break;
4969 case EM_UNDO16:
4970 case EM_UNDO:
4971 case WM_UNDO:
4972 result = (LRESULT)EDIT_EM_Undo(es);
4973 break;
4975 case EM_FMTLINES16:
4976 case EM_FMTLINES:
4977 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4978 break;
4980 case EM_LINEFROMCHAR16:
4981 case EM_LINEFROMCHAR:
4982 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4983 break;
4985 case EM_SETTABSTOPS16:
4986 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
4987 break;
4988 case EM_SETTABSTOPS:
4989 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4990 break;
4992 case EM_SETPASSWORDCHAR16:
4993 unicode = FALSE; /* 16-bit message is always ascii */
4994 /* fall through */
4995 case EM_SETPASSWORDCHAR:
4997 WCHAR charW = 0;
4999 if(unicode)
5000 charW = (WCHAR)wParam;
5001 else
5003 CHAR charA = wParam;
5004 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
5007 EDIT_EM_SetPasswordChar(es, charW);
5008 break;
5011 case EM_EMPTYUNDOBUFFER16:
5012 case EM_EMPTYUNDOBUFFER:
5013 EDIT_EM_EmptyUndoBuffer(es);
5014 break;
5016 case EM_GETFIRSTVISIBLELINE16:
5017 result = es->y_offset;
5018 break;
5019 case EM_GETFIRSTVISIBLELINE:
5020 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
5021 break;
5023 case EM_SETREADONLY16:
5024 case EM_SETREADONLY:
5025 if (wParam) {
5026 SetWindowLongW( hwnd, GWL_STYLE,
5027 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
5028 es->style |= ES_READONLY;
5029 } else {
5030 SetWindowLongW( hwnd, GWL_STYLE,
5031 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
5032 es->style &= ~ES_READONLY;
5034 result = 1;
5035 break;
5037 case EM_SETWORDBREAKPROC16:
5038 EDIT_EM_SetWordBreakProc16(es, (EDITWORDBREAKPROC16)lParam);
5039 break;
5040 case EM_SETWORDBREAKPROC:
5041 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
5042 break;
5044 case EM_GETWORDBREAKPROC16:
5045 result = (LRESULT)es->word_break_proc16;
5046 break;
5047 case EM_GETWORDBREAKPROC:
5048 result = (LRESULT)es->word_break_proc;
5049 break;
5051 case EM_GETPASSWORDCHAR16:
5052 unicode = FALSE; /* 16-bit message is always ascii */
5053 /* fall through */
5054 case EM_GETPASSWORDCHAR:
5056 if(unicode)
5057 result = es->password_char;
5058 else
5060 WCHAR charW = es->password_char;
5061 CHAR charA = 0;
5062 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
5063 result = charA;
5065 break;
5068 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
5070 case EM_SETMARGINS:
5071 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
5072 break;
5074 case EM_GETMARGINS:
5075 result = MAKELONG(es->left_margin, es->right_margin);
5076 break;
5078 case EM_GETLIMITTEXT:
5079 result = es->buffer_limit;
5080 break;
5082 case EM_POSFROMCHAR:
5083 if ((INT)wParam >= get_text_length(es)) result = -1;
5084 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
5085 break;
5087 case EM_CHARFROMPOS:
5088 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5089 break;
5091 /* End of the EM_ messages which were in numerical order; what order
5092 * are these in? vaguely alphabetical?
5095 case WM_NCCREATE:
5096 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
5097 break;
5099 case WM_DESTROY:
5100 result = EDIT_WM_Destroy(es);
5101 es = NULL;
5102 break;
5104 case WM_GETDLGCODE:
5105 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
5107 if (es->style & ES_MULTILINE)
5108 result |= DLGC_WANTALLKEYS;
5110 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
5112 int vk = (int)((LPMSG)lParam)->wParam;
5114 if (es->hwndListBox)
5116 if (vk == VK_RETURN || vk == VK_ESCAPE)
5117 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
5118 result |= DLGC_WANTMESSAGE;
5120 else
5122 switch (vk)
5124 case VK_ESCAPE:
5125 SendMessageW(GetParent(hwnd), WM_CLOSE, 0, 0);
5126 break;
5127 default:
5128 break;
5132 break;
5134 case WM_IME_CHAR:
5135 if (!unicode)
5137 WCHAR charW;
5138 CHAR strng[2];
5140 strng[0] = wParam >> 8;
5141 strng[1] = wParam & 0xff;
5142 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
5143 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
5144 result = EDIT_WM_Char(es, charW);
5145 break;
5147 /* fall through */
5148 case WM_CHAR:
5150 WCHAR charW;
5152 if(unicode)
5153 charW = wParam;
5154 else
5156 CHAR charA = wParam;
5157 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
5160 if (es->hwndListBox)
5162 if (charW == VK_RETURN || charW == VK_ESCAPE)
5164 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
5165 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
5166 break;
5169 result = EDIT_WM_Char(es, charW);
5170 break;
5173 case WM_UNICHAR:
5174 if (unicode)
5176 if (wParam == UNICODE_NOCHAR) return TRUE;
5177 if (wParam <= 0x000fffff)
5179 if(wParam > 0xffff) /* convert to surrogates */
5181 wParam -= 0x10000;
5182 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
5183 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
5185 else EDIT_WM_Char(es, wParam);
5187 return 0;
5189 break;
5191 case WM_CLEAR:
5192 EDIT_WM_Clear(es);
5193 break;
5195 case WM_COMMAND:
5196 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
5197 break;
5199 case WM_CONTEXTMENU:
5200 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5201 break;
5203 case WM_COPY:
5204 EDIT_WM_Copy(es);
5205 break;
5207 case WM_CREATE:
5208 if(unicode)
5209 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
5210 else
5212 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
5213 LPWSTR nameW = NULL;
5214 if(nameA)
5216 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
5217 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
5218 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
5220 result = EDIT_WM_Create(es, nameW);
5221 HeapFree(GetProcessHeap(), 0, nameW);
5223 break;
5225 case WM_CUT:
5226 EDIT_WM_Cut(es);
5227 break;
5229 case WM_ENABLE:
5230 es->bEnableState = (BOOL) wParam;
5231 EDIT_UpdateText(es, NULL, TRUE);
5232 break;
5234 case WM_ERASEBKGND:
5235 /* we do the proper erase in EDIT_WM_Paint */
5236 result = 1;
5237 break;
5239 case WM_GETFONT:
5240 result = (LRESULT)es->font;
5241 break;
5243 case WM_GETTEXT:
5244 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
5245 break;
5247 case WM_GETTEXTLENGTH:
5248 if (unicode) result = get_text_length(es);
5249 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
5250 NULL, 0, NULL, NULL );
5251 break;
5253 case WM_HSCROLL:
5254 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5255 break;
5257 case WM_KEYDOWN:
5258 result = EDIT_WM_KeyDown(es, (INT)wParam);
5259 break;
5261 case WM_KILLFOCUS:
5262 result = EDIT_WM_KillFocus(es);
5263 break;
5265 case WM_LBUTTONDBLCLK:
5266 result = EDIT_WM_LButtonDblClk(es);
5267 break;
5269 case WM_LBUTTONDOWN:
5270 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
5271 break;
5273 case WM_LBUTTONUP:
5274 result = EDIT_WM_LButtonUp(es);
5275 break;
5277 case WM_MBUTTONDOWN:
5278 result = EDIT_WM_MButtonDown(es);
5279 break;
5281 case WM_MOUSEMOVE:
5282 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
5283 break;
5285 case WM_PRINTCLIENT:
5286 case WM_PAINT:
5287 EDIT_WM_Paint(es, (HDC)wParam);
5288 break;
5290 case WM_PASTE:
5291 EDIT_WM_Paste(es);
5292 break;
5294 case WM_SETFOCUS:
5295 EDIT_WM_SetFocus(es);
5296 break;
5298 case WM_SETFONT:
5299 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
5300 break;
5302 case WM_SETREDRAW:
5303 /* FIXME: actually set an internal flag and behave accordingly */
5304 break;
5306 case WM_SETTEXT:
5307 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
5308 result = TRUE;
5309 break;
5311 case WM_SIZE:
5312 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
5313 break;
5315 case WM_STYLECHANGED:
5316 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
5317 break;
5319 case WM_STYLECHANGING:
5320 result = 0; /* See EDIT_WM_StyleChanged */
5321 break;
5323 case WM_SYSKEYDOWN:
5324 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
5325 break;
5327 case WM_TIMER:
5328 EDIT_WM_Timer(es);
5329 break;
5331 case WM_VSCROLL:
5332 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
5333 break;
5335 case WM_MOUSEWHEEL:
5337 int gcWheelDelta = 0;
5338 UINT pulScrollLines = 3;
5339 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
5341 if (wParam & (MK_SHIFT | MK_CONTROL)) {
5342 result = DefWindowProcW(hwnd, msg, wParam, lParam);
5343 break;
5345 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
5346 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
5348 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
5349 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
5350 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
5353 break;
5356 /* IME messages to make the edit control IME aware */
5357 case WM_IME_SETCONTEXT:
5358 break;
5360 case WM_IME_STARTCOMPOSITION:
5361 es->composition_start = es->selection_end;
5362 es->composition_len = 0;
5363 break;
5365 case WM_IME_COMPOSITION:
5366 EDIT_ImeComposition(hwnd, lParam, es);
5367 break;
5369 case WM_IME_ENDCOMPOSITION:
5370 if (es->composition_len > 0)
5372 static const WCHAR empty_stringW[] = {0};
5373 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
5374 es->selection_end = es->selection_start;
5375 es->composition_len= 0;
5377 break;
5379 case WM_IME_COMPOSITIONFULL:
5380 break;
5382 case WM_IME_SELECT:
5383 break;
5385 case WM_IME_CONTROL:
5386 break;
5388 default:
5389 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5390 break;
5393 if (es) EDIT_UnlockBuffer(es, FALSE);
5395 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5397 return result;
5400 /*********************************************************************
5402 * EditWndProc (USER32.@)
5404 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5406 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
5409 /*********************************************************************
5411 * EditWndProcW
5413 static LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5415 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
5418 /*********************************************************************
5419 * edit class descriptor
5421 static const WCHAR editW[] = {'E','d','i','t',0};
5422 const struct builtin_class_descr EDIT_builtin_class =
5424 editW, /* name */
5425 CS_DBLCLKS | CS_PARENTDC, /* style */
5426 EditWndProcA, /* procA */
5427 EditWndProcW, /* procW */
5428 sizeof(EDITSTATE *), /* extra */
5429 IDC_IBEAM, /* cursor */
5430 0 /* brush */