65f36797f636fb93c39b4dc97e0b9168c130fb42
[reactos.git] / reactos / dll / win32 / user32 / controls / edit.c
1 /*
2 * Edit control
3 *
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
7 *
8 *
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.
13 *
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.
18 *
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
22 *
23 * NOTES
24 *
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.
27 *
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.
31 *
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
42 *
43 */
44
45 #include <user32.h>
46
47 #include <wine/debug.h>
48
49 WINE_DEFAULT_DEBUG_CHANNEL(edit);
50 WINE_DECLARE_DEBUG_CHANNEL(combo);
51 WINE_DECLARE_DEBUG_CHANNEL(relay);
52
53 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
54 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
55 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
56 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
57
58 /*
59 * extra flags for EDITSTATE.flags field
60 */
61 #define EF_MODIFIED 0x0001 /* text has been modified */
62 #define EF_FOCUSED 0x0002 /* we have input focus */
63 #define EF_UPDATE 0x0004 /* notify parent of changed state */
64 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
66 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
67 wrapped line, instead of in front of the next character */
68 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
69 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
70 the text buffer if this is clear. */
71 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
72
73 typedef enum
74 {
75 END_0 = 0, /* line ends with terminating '\0' character */
76 END_WRAP, /* line is wrapped */
77 END_HARD, /* line ends with a hard return '\r\n' */
78 END_SOFT, /* line ends with a soft return '\r\r\n' */
79 END_RICH /* line ends with a single '\n' */
80 } LINE_END;
81
82 typedef struct tagLINEDEF {
83 INT length; /* bruto length of a line in bytes */
84 INT net_length; /* netto length of a line in visible characters */
85 LINE_END ending;
86 INT width; /* width of the line in pixels */
87 INT index; /* line index into the buffer */
88 struct tagLINEDEF *next;
89 } LINEDEF;
90
91 typedef struct
92 {
93 BOOL is_unicode; /* how the control was created */
94 LPWSTR text; /* the actual contents of the control */
95 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
96 UINT buffer_size; /* the size of the buffer in characters */
97 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
98 HFONT font; /* NULL means standard system font */
99 INT x_offset; /* scroll offset for multi lines this is in pixels
100 for single lines it's in characters */
101 INT line_height; /* height of a screen line in pixels */
102 INT char_width; /* average character width in pixels */
103 DWORD style; /* sane version of wnd->dwStyle */
104 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
105 INT undo_insert_count; /* number of characters inserted in sequence */
106 UINT undo_position; /* character index of the insertion and deletion */
107 LPWSTR undo_text; /* deleted text */
108 UINT undo_buffer_size; /* size of the deleted text buffer */
109 INT selection_start; /* == selection_end if no selection */
110 INT selection_end; /* == current caret position */
111 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
112 INT left_margin; /* in pixels */
113 INT right_margin; /* in pixels */
114 RECT format_rect;
115 INT text_width; /* width of the widest line in pixels for multi line controls
116 and just line width for single line controls */
117 INT region_posx; /* Position of cursor relative to region: */
118 INT region_posy; /* -1: to left, 0: within, 1: to right */
119 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
120 INT line_count; /* number of lines */
121 INT y_offset; /* scroll offset in number of lines */
122 BOOL bCaptureState; /* flag indicating whether mouse was captured */
123 BOOL bEnableState; /* flag keeping the enable state */
124 HWND hwndSelf; /* the our window handle */
125 HWND hwndParent; /* Handle of parent for sending EN_* messages.
126 Even if parent will change, EN_* messages
127 should be sent to the first parent. */
128 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
129 /*
130 * only for multi line controls
131 */
132 INT lock_count; /* amount of re-entries in the EditWndProc */
133 INT tabs_count;
134 LPINT tabs;
135 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
136 HLOCAL hloc32W; /* our unicode local memory block */
137 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
138 or EM_SETHANDLE */
139 /*
140 * IME Data
141 */
142 UINT composition_len; /* length of composition, 0 == no composition */
143 int composition_start; /* the character position for the composition */
144 } EDITSTATE;
145
146
147 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
148 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
149
150 /* used for disabled or read-only edit control */
151 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
152 do \
153 { /* Notify parent which has created this edit control */ \
154 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
155 SendMessageW(es->hwndParent, WM_COMMAND, \
156 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
157 (LPARAM)(es->hwndSelf)); \
158 } while(0)
159
160 static const WCHAR empty_stringW[] = {0};
161
162 /*********************************************************************
163 *
164 * EM_CANUNDO
165 *
166 */
167 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
168 {
169 return (es->undo_insert_count || strlenW(es->undo_text));
170 }
171
172
173 /*********************************************************************
174 *
175 * EM_EMPTYUNDOBUFFER
176 *
177 */
178 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
179 {
180 es->undo_insert_count = 0;
181 *es->undo_text = '\0';
182 }
183
184
185 /**********************************************************************
186 * get_app_version
187 *
188 * Returns the window version in case Wine emulates a later version
189 * of windows than the application expects.
190 *
191 * In a number of cases when windows runs an application that was
192 * designed for an earlier windows version, windows reverts
193 * to "old" behaviour of that earlier version.
194 *
195 * An example is a disabled edit control that needs to be painted.
196 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
197 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
198 * applications with an expected version 0f 4.0 or higher.
199 *
200 */
201 static DWORD get_app_version(void)
202 {
203 static DWORD version;
204 if (!version)
205 {
206 DWORD dwEmulatedVersion;
207 OSVERSIONINFOW info;
208 DWORD dwProcVersion = GetProcessVersion(0);
209
210 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
211 GetVersionExW( &info );
212 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
213 /* FIXME: this may not be 100% correct; see discussion on the
214 * wine developer list in Nov 1999 */
215 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
216 }
217 return version;
218 }
219
220 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
221 {
222 HBRUSH hbrush;
223 UINT msg;
224
225 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
226 msg = WM_CTLCOLORSTATIC;
227 else
228 msg = WM_CTLCOLOREDIT;
229
230 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
231 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
232 if (!hbrush)
233 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
234 return hbrush;
235 }
236
237
238 /*********************************************************************
239 *
240 * EDIT_WordBreakProc
241 *
242 * Find the beginning of words.
243 * Note: unlike the specs for a WordBreakProc, this function only
244 * allows to be called without linebreaks between s[0] up to
245 * s[count - 1]. Remember it is only called
246 * internally, so we can decide this for ourselves.
247 *
248 */
249 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
250 {
251 INT ret = 0;
252
253 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
254
255 if(!s) return 0;
256
257 switch (action) {
258 case WB_LEFT:
259 if (!count)
260 break;
261 if (index)
262 index--;
263 if (s[index] == ' ') {
264 while (index && (s[index] == ' '))
265 index--;
266 if (index) {
267 while (index && (s[index] != ' '))
268 index--;
269 if (s[index] == ' ')
270 index++;
271 }
272 } else {
273 while (index && (s[index] != ' '))
274 index--;
275 if (s[index] == ' ')
276 index++;
277 }
278 ret = index;
279 break;
280 case WB_RIGHT:
281 if (!count)
282 break;
283 if (index)
284 index--;
285 if (s[index] == ' ')
286 while ((index < count) && (s[index] == ' ')) index++;
287 else {
288 while (s[index] && (s[index] != ' ') && (index < count))
289 index++;
290 while ((s[index] == ' ') && (index < count)) index++;
291 }
292 ret = index;
293 break;
294 case WB_ISDELIMITER:
295 ret = (s[index] == ' ');
296 break;
297 default:
298 ERR("unknown action code, please report !\n");
299 break;
300 }
301 return ret;
302 }
303
304
305 /*********************************************************************
306 *
307 * EDIT_CallWordBreakProc
308 *
309 * Call appropriate WordBreakProc (internal or external).
310 *
311 * Note: The "start" argument should always be an index referring
312 * to es->text. The actual wordbreak proc might be
313 * 16 bit, so we can't always pass any 32 bit LPSTR.
314 * Hence we assume that es->text is the buffer that holds
315 * the string under examination (we can decide this for ourselves).
316 *
317 */
318 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
319 {
320 INT ret;
321
322 if (es->word_break_proc)
323 {
324 if(es->is_unicode)
325 {
326 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
327
328 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
329 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
330 ret = wbpW(es->text + start, index, count, action);
331 }
332 else
333 {
334 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
335 INT countA;
336 CHAR *textA;
337
338 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
339 textA = HeapAlloc(GetProcessHeap(), 0, countA);
340 if (textA == NULL) return 0;
341 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
342 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
343 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
344 ret = wbpA(textA, index, countA, action);
345 HeapFree(GetProcessHeap(), 0, textA);
346 }
347 }
348 else
349 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
350
351 return ret;
352 }
353
354 /*********************************************************************
355 *
356 * EDIT_BuildLineDefs_ML
357 *
358 * Build linked list of text lines.
359 * Lines can end with '\0' (last line), a character (if it is wrapped),
360 * a soft return '\r\r\n' or a hard return '\r\n'
361 *
362 */
363 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
364 {
365 HDC dc;
366 HFONT old_font = 0;
367 LPWSTR current_position, cp;
368 INT fw;
369 LINEDEF *current_line;
370 LINEDEF *previous_line;
371 LINEDEF *start_line;
372 INT line_index = 0, nstart_line = 0, nstart_index = 0;
373 INT line_count = es->line_count;
374 INT orig_net_length;
375 RECT rc;
376
377 if (istart == iend && delta == 0)
378 return;
379
380 dc = GetDC(es->hwndSelf);
381 if (es->font)
382 old_font = SelectObject(dc, es->font);
383
384 previous_line = NULL;
385 current_line = es->first_line_def;
386
387 /* Find starting line. istart must lie inside an existing line or
388 * at the end of buffer */
389 do {
390 if (istart < current_line->index + current_line->length ||
391 current_line->ending == END_0)
392 break;
393
394 previous_line = current_line;
395 current_line = current_line->next;
396 line_index++;
397 } while (current_line);
398
399 if (!current_line) /* Error occurred start is not inside previous buffer */
400 {
401 FIXME(" modification occurred outside buffer\n");
402 ReleaseDC(es->hwndSelf, dc);
403 return;
404 }
405
406 /* Remember start of modifications in order to calculate update region */
407 nstart_line = line_index;
408 nstart_index = current_line->index;
409
410 /* We must start to reformat from the previous line since the modifications
411 * may have caused the line to wrap upwards. */
412 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
413 {
414 line_index--;
415 current_line = previous_line;
416 }
417 start_line = current_line;
418
419 fw = es->format_rect.right - es->format_rect.left;
420 current_position = es->text + current_line->index;
421 do {
422 if (current_line != start_line)
423 {
424 if (!current_line || current_line->index + delta > current_position - es->text)
425 {
426 /* The buffer has been expanded, create a new line and
427 insert it into the link list */
428 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
429 if (new_line == NULL) // reactos r33509
430 break; // reactos r33509
431 new_line->next = previous_line->next;
432 previous_line->next = new_line;
433 current_line = new_line;
434 es->line_count++;
435 }
436 else if (current_line->index + delta < current_position - es->text)
437 {
438 /* The previous line merged with this line so we delete this extra entry */
439 previous_line->next = current_line->next;
440 HeapFree(GetProcessHeap(), 0, current_line);
441 current_line = previous_line->next;
442 es->line_count--;
443 continue;
444 }
445 else /* current_line->index + delta == current_position */
446 {
447 if (current_position - es->text > iend)
448 break; /* We reached end of line modifications */
449 /* else recalculate this line */
450 }
451 }
452
453 current_line->index = current_position - es->text;
454 orig_net_length = current_line->net_length;
455
456 /* Find end of line */
457 cp = current_position;
458 while (*cp) {
459 if (*cp == '\n') break;
460 if ((*cp == '\r') && (*(cp + 1) == '\n'))
461 break;
462 cp++;
463 }
464
465 /* Mark type of line termination */
466 if (!(*cp)) {
467 current_line->ending = END_0;
468 current_line->net_length = strlenW(current_position);
469 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
470 current_line->ending = END_SOFT;
471 current_line->net_length = cp - current_position - 1;
472 } else if (*cp == '\n') {
473 current_line->ending = END_RICH;
474 current_line->net_length = cp - current_position;
475 } else {
476 current_line->ending = END_HARD;
477 current_line->net_length = cp - current_position;
478 }
479
480 /* Calculate line width */
481 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
482 current_position, current_line->net_length,
483 es->tabs_count, es->tabs));
484
485 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
486 if (!(es->style & ES_AUTOHSCROLL)) {
487 if (current_line->width > fw) {
488 INT next = 0;
489 INT prev;
490 do {
491 prev = next;
492 next = EDIT_CallWordBreakProc(es, current_position - es->text,
493 prev + 1, current_line->net_length, WB_RIGHT);
494 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
495 current_position, next, es->tabs_count, es->tabs));
496 } while (current_line->width <= fw);
497 if (!prev) { /* Didn't find a line break so force a break */
498 next = 0;
499 do {
500 prev = next;
501 next++;
502 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
503 current_position, next, es->tabs_count, es->tabs));
504 } while (current_line->width <= fw);
505 if (!prev)
506 prev = 1;
507 }
508
509 /* If the first line we are calculating, wrapped before istart, we must
510 * adjust istart in order for this to be reflected in the update region. */
511 if (current_line->index == nstart_index && istart > current_line->index + prev)
512 istart = current_line->index + prev;
513 /* else if we are updating the previous line before the first line we
514 * are re-calculating and it expanded */
515 else if (current_line == start_line &&
516 current_line->index != nstart_index && orig_net_length < prev)
517 {
518 /* Line expanded due to an upwards line wrap so we must partially include
519 * previous line in update region */
520 nstart_line = line_index;
521 nstart_index = current_line->index;
522 istart = current_line->index + orig_net_length;
523 }
524
525 current_line->net_length = prev;
526 current_line->ending = END_WRAP;
527 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
528 current_line->net_length, es->tabs_count, es->tabs));
529 }
530 else if (current_line == start_line &&
531 current_line->index != nstart_index &&
532 orig_net_length < current_line->net_length) {
533 /* The previous line expanded but it's still not as wide as the client rect */
534 /* The expansion is due to an upwards line wrap so we must partially include
535 it in the update region */
536 nstart_line = line_index;
537 nstart_index = current_line->index;
538 istart = current_line->index + orig_net_length;
539 }
540 }
541
542
543 /* Adjust length to include line termination */
544 switch (current_line->ending) {
545 case END_SOFT:
546 current_line->length = current_line->net_length + 3;
547 break;
548 case END_RICH:
549 current_line->length = current_line->net_length + 1;
550 break;
551 case END_HARD:
552 current_line->length = current_line->net_length + 2;
553 break;
554 case END_WRAP:
555 case END_0:
556 current_line->length = current_line->net_length;
557 break;
558 }
559 es->text_width = max(es->text_width, current_line->width);
560 current_position += current_line->length;
561 previous_line = current_line;
562 current_line = current_line->next;
563 line_index++;
564 } while (previous_line->ending != END_0);
565
566 /* Finish adjusting line indexes by delta or remove hanging lines */
567 if (previous_line->ending == END_0)
568 {
569 LINEDEF *pnext = NULL;
570
571 previous_line->next = NULL;
572 while (current_line)
573 {
574 pnext = current_line->next;
575 HeapFree(GetProcessHeap(), 0, current_line);
576 current_line = pnext;
577 es->line_count--;
578 }
579 }
580 else if (delta != 0)
581 {
582 while (current_line)
583 {
584 current_line->index += delta;
585 current_line = current_line->next;
586 }
587 }
588
589 /* Calculate rest of modification rectangle */
590 if (hrgn)
591 {
592 HRGN tmphrgn;
593 /*
594 * We calculate two rectangles. One for the first line which may have
595 * an indent with respect to the format rect. The other is a format-width
596 * rectangle that spans the rest of the lines that changed or moved.
597 */
598 rc.top = es->format_rect.top + nstart_line * es->line_height -
599 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
600 rc.bottom = rc.top + es->line_height;
601 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
602 rc.left = es->format_rect.left;
603 else
604 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
605 es->text + nstart_index, istart - nstart_index,
606 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
607 rc.right = es->format_rect.right;
608 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
609
610 rc.top = rc.bottom;
611 rc.left = es->format_rect.left;
612 rc.right = es->format_rect.right;
613 /*
614 * If lines were added or removed we must re-paint the remainder of the
615 * lines since the remaining lines were either shifted up or down.
616 */
617 if (line_count < es->line_count) /* We added lines */
618 rc.bottom = es->line_count * es->line_height;
619 else if (line_count > es->line_count) /* We removed lines */
620 rc.bottom = line_count * es->line_height;
621 else
622 rc.bottom = line_index * es->line_height;
623 rc.bottom += es->format_rect.top;
624 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
625 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
626 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
627 DeleteObject(tmphrgn);
628 }
629
630 if (es->font)
631 SelectObject(dc, old_font);
632
633 ReleaseDC(es->hwndSelf, dc);
634 }
635
636
637 static inline UINT get_text_length(EDITSTATE *es)
638 {
639 if(es->text_length == (UINT)-1)
640 es->text_length = strlenW(es->text);
641 return es->text_length;
642 }
643
644 /*********************************************************************
645 *
646 * EDIT_GetPasswordPointer_SL
647 *
648 * note: caller should free the (optionally) allocated buffer
649 *
650 */
651 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
652 {
653 if (es->style & ES_PASSWORD) {
654 INT len = get_text_length(es);
655 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
656 if (text == NULL)
657 return NULL;
658 text[len] = '\0';
659 while(len) text[--len] = es->password_char;
660 return text;
661 } else
662 return es->text;
663 }
664
665
666 /*********************************************************************
667 *
668 * EDIT_CalcLineWidth_SL
669 *
670 */
671 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
672 {
673 SIZE size;
674 LPWSTR text;
675 HDC dc;
676 HFONT old_font = 0;
677
678 text = EDIT_GetPasswordPointer_SL(es);
679
680 dc = GetDC(es->hwndSelf);
681 if (es->font)
682 old_font = SelectObject(dc, es->font);
683
684 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
685
686 if (es->font)
687 SelectObject(dc, old_font);
688 ReleaseDC(es->hwndSelf, dc);
689
690 if (es->style & ES_PASSWORD)
691 HeapFree(GetProcessHeap(), 0, text);
692
693 es->text_width = size.cx;
694 }
695
696 /*********************************************************************
697 *
698 * EDIT_CharFromPos
699 *
700 * Beware: This is not the function called on EM_CHARFROMPOS
701 * The position _can_ be outside the formatting / client
702 * rectangle
703 * The return value is only the character index
704 *
705 */
706 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
707 {
708 INT index;
709 HDC dc;
710 HFONT old_font = 0;
711 INT x_high = 0, x_low = 0;
712
713 if (es->style & ES_MULTILINE) {
714 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
715 INT line_index = 0;
716 LINEDEF *line_def = es->first_line_def;
717 INT low, high;
718 while ((line > 0) && line_def->next) {
719 line_index += line_def->length;
720 line_def = line_def->next;
721 line--;
722 }
723 x += es->x_offset - es->format_rect.left;
724 if (es->style & ES_RIGHT)
725 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
726 else if (es->style & ES_CENTER)
727 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
728 if (x >= line_def->width) {
729 if (after_wrap)
730 *after_wrap = (line_def->ending == END_WRAP);
731 return line_index + line_def->net_length;
732 }
733 if (x <= 0) {
734 if (after_wrap)
735 *after_wrap = FALSE;
736 return line_index;
737 }
738 dc = GetDC(es->hwndSelf);
739 if (es->font)
740 old_font = SelectObject(dc, es->font);
741 low = line_index;
742 high = line_index + line_def->net_length + 1;
743 while (low < high - 1)
744 {
745 INT mid = (low + high) / 2;
746 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
747 if (x_now > x) {
748 high = mid;
749 x_high = x_now;
750 } else {
751 low = mid;
752 x_low = x_now;
753 }
754 }
755 if (abs(x_high - x) + 1 <= abs(x_low - x))
756 index = high;
757 else
758 index = low;
759
760 if (after_wrap)
761 *after_wrap = ((index == line_index + line_def->net_length) &&
762 (line_def->ending == END_WRAP));
763 } else {
764 LPWSTR text;
765 SIZE size;
766 if (after_wrap)
767 *after_wrap = FALSE;
768 x -= es->format_rect.left;
769 if (!x)
770 return es->x_offset;
771
772 if (!es->x_offset)
773 {
774 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
775 if (es->style & ES_RIGHT)
776 x -= indent;
777 else if (es->style & ES_CENTER)
778 x -= indent / 2;
779 }
780
781 text = EDIT_GetPasswordPointer_SL(es);
782 dc = GetDC(es->hwndSelf);
783 if (es->font)
784 old_font = SelectObject(dc, es->font);
785 if (x < 0)
786 {
787 INT low = 0;
788 INT high = es->x_offset;
789 while (low < high - 1)
790 {
791 INT mid = (low + high) / 2;
792 GetTextExtentPoint32W( dc, text + mid,
793 es->x_offset - mid, &size );
794 if (size.cx > -x) {
795 low = mid;
796 x_low = size.cx;
797 } else {
798 high = mid;
799 x_high = size.cx;
800 }
801 }
802 if (abs(x_high + x) <= abs(x_low + x) + 1)
803 index = high;
804 else
805 index = low;
806 }
807 else
808 {
809 INT low = es->x_offset;
810 INT high = get_text_length(es) + 1;
811 while (low < high - 1)
812 {
813 INT mid = (low + high) / 2;
814 GetTextExtentPoint32W( dc, text + es->x_offset,
815 mid - es->x_offset, &size );
816 if (size.cx > x) {
817 high = mid;
818 x_high = size.cx;
819 } else {
820 low = mid;
821 x_low = size.cx;
822 }
823 }
824 if (abs(x_high - x) <= abs(x_low - x) + 1)
825 index = high;
826 else
827 index = low;
828 }
829 if (es->style & ES_PASSWORD)
830 HeapFree(GetProcessHeap(), 0, text);
831 }
832 if (es->font)
833 SelectObject(dc, old_font);
834 ReleaseDC(es->hwndSelf, dc);
835 return index;
836 }
837
838
839 /*********************************************************************
840 *
841 * EDIT_ConfinePoint
842 *
843 * adjusts the point to be within the formatting rectangle
844 * (so CharFromPos returns the nearest _visible_ character)
845 *
846 */
847 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
848 {
849 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
850 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
851 }
852
853
854 /*********************************************************************
855 *
856 * EM_LINEFROMCHAR
857 *
858 */
859 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
860 {
861 INT line;
862 LINEDEF *line_def;
863
864 if (!(es->style & ES_MULTILINE))
865 return 0;
866 if (index > (INT)get_text_length(es))
867 return es->line_count - 1;
868 if (index == -1)
869 index = min(es->selection_start, es->selection_end);
870
871 line = 0;
872 line_def = es->first_line_def;
873 index -= line_def->length;
874 while ((index >= 0) && line_def->next) {
875 line++;
876 line_def = line_def->next;
877 index -= line_def->length;
878 }
879 return line;
880 }
881
882
883 /*********************************************************************
884 *
885 * EM_LINEINDEX
886 *
887 */
888 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
889 {
890 INT line_index;
891 const LINEDEF *line_def;
892
893 if (!(es->style & ES_MULTILINE))
894 return 0;
895 if (line >= es->line_count)
896 return -1;
897
898 line_index = 0;
899 line_def = es->first_line_def;
900 if (line == -1) {
901 INT index = es->selection_end - line_def->length;
902 while ((index >= 0) && line_def->next) {
903 line_index += line_def->length;
904 line_def = line_def->next;
905 index -= line_def->length;
906 }
907 } else {
908 while (line > 0) {
909 line_index += line_def->length;
910 line_def = line_def->next;
911 line--;
912 }
913 }
914 return line_index;
915 }
916
917
918 /*********************************************************************
919 *
920 * EM_LINELENGTH
921 *
922 */
923 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
924 {
925 LINEDEF *line_def;
926
927 if (!(es->style & ES_MULTILINE))
928 return get_text_length(es);
929
930 if (index == -1) {
931 /* get the number of remaining non-selected chars of selected lines */
932 INT32 l; /* line number */
933 INT32 li; /* index of first char in line */
934 INT32 count;
935 l = EDIT_EM_LineFromChar(es, es->selection_start);
936 /* # chars before start of selection area */
937 count = es->selection_start - EDIT_EM_LineIndex(es, l);
938 l = EDIT_EM_LineFromChar(es, es->selection_end);
939 /* # chars after end of selection */
940 li = EDIT_EM_LineIndex(es, l);
941 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
942 return count;
943 }
944 line_def = es->first_line_def;
945 index -= line_def->length;
946 while ((index >= 0) && line_def->next) {
947 line_def = line_def->next;
948 index -= line_def->length;
949 }
950 return line_def->net_length;
951 }
952
953
954 /*********************************************************************
955 *
956 * EM_POSFROMCHAR
957 *
958 */
959 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
960 {
961 INT len = get_text_length(es);
962 INT l;
963 INT li;
964 INT x;
965 INT y = 0;
966 INT w;
967 INT lw = 0;
968 INT ll = 0;
969 HDC dc;
970 HFONT old_font = 0;
971 SIZE size;
972 LINEDEF *line_def;
973
974 index = min(index, len);
975 dc = GetDC(es->hwndSelf);
976 if (es->font)
977 old_font = SelectObject(dc, es->font);
978 if (es->style & ES_MULTILINE) {
979 l = EDIT_EM_LineFromChar(es, index);
980 y = (l - es->y_offset) * es->line_height;
981 li = EDIT_EM_LineIndex(es, l);
982 if (after_wrap && (li == index) && l) {
983 INT l2 = l - 1;
984 line_def = es->first_line_def;
985 while (l2) {
986 line_def = line_def->next;
987 l2--;
988 }
989 if (line_def->ending == END_WRAP) {
990 l--;
991 y -= es->line_height;
992 li = EDIT_EM_LineIndex(es, l);
993 }
994 }
995
996 line_def = es->first_line_def;
997 while (line_def->index != li)
998 line_def = line_def->next;
999
1000 ll = line_def->net_length;
1001 lw = line_def->width;
1002
1003 w = es->format_rect.right - es->format_rect.left;
1004 if (es->style & ES_RIGHT)
1005 {
1006 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1007 es->tabs_count, es->tabs)) - es->x_offset;
1008 x = w - x;
1009 }
1010 else if (es->style & ES_CENTER)
1011 {
1012 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1013 es->tabs_count, es->tabs)) - es->x_offset;
1014 x += (w - lw) / 2;
1015 }
1016 else /* ES_LEFT */
1017 {
1018 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1019 es->tabs_count, es->tabs)) - es->x_offset;
1020 }
1021 } else {
1022 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1023 if (index < es->x_offset) {
1024 GetTextExtentPoint32W(dc, text + index,
1025 es->x_offset - index, &size);
1026 x = -size.cx;
1027 } else {
1028 GetTextExtentPoint32W(dc, text + es->x_offset,
1029 index - es->x_offset, &size);
1030 x = size.cx;
1031
1032 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1033 {
1034 w = es->format_rect.right - es->format_rect.left;
1035 if (w > es->text_width)
1036 {
1037 if (es->style & ES_RIGHT)
1038 x += w - es->text_width;
1039 else if (es->style & ES_CENTER)
1040 x += (w - es->text_width) / 2;
1041 }
1042 }
1043 }
1044 y = 0;
1045 if (es->style & ES_PASSWORD)
1046 HeapFree(GetProcessHeap(), 0, text);
1047 }
1048 x += es->format_rect.left;
1049 y += es->format_rect.top;
1050 if (es->font)
1051 SelectObject(dc, old_font);
1052 ReleaseDC(es->hwndSelf, dc);
1053 return MAKELONG((INT16)x, (INT16)y);
1054 }
1055
1056
1057 /*********************************************************************
1058 *
1059 * EDIT_GetLineRect
1060 *
1061 * Calculates the bounding rectangle for a line from a starting
1062 * column to an ending column.
1063 *
1064 */
1065 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1066 {
1067 INT line_index = EDIT_EM_LineIndex(es, line);
1068
1069 if (es->style & ES_MULTILINE)
1070 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1071 else
1072 rc->top = es->format_rect.top;
1073 rc->bottom = rc->top + es->line_height;
1074 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1075 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1076 }
1077
1078
1079 static inline void text_buffer_changed(EDITSTATE *es)
1080 {
1081 es->text_length = (UINT)-1;
1082 }
1083
1084 /*********************************************************************
1085 * EDIT_LockBuffer
1086 *
1087 */
1088 static void EDIT_LockBuffer(EDITSTATE *es)
1089 {
1090 if (!es->text) {
1091
1092 CHAR *textA = NULL; // ReactOS Hacked!
1093 UINT countA = 0;
1094
1095 if(es->hloc32W)
1096 {
1097 if(es->hloc32A)
1098 {
1099 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1100 textA = LocalLock(es->hloc32A);
1101 countA = strlen(textA) + 1;
1102 }
1103 }
1104 else {
1105 ERR("no buffer ... please report\n");
1106 return;
1107 }
1108
1109 if(textA)
1110 {
1111 HLOCAL hloc32W_new;
1112 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1113 if(countW_new > es->buffer_size + 1)
1114 {
1115 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1116 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1117 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1118 if(hloc32W_new)
1119 {
1120 es->hloc32W = hloc32W_new;
1121 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1122 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1123 }
1124 else
1125 WARN("FAILED! Will synchronize partially\n");
1126 }
1127 es->text = LocalLock(es->hloc32W);
1128 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1129 LocalUnlock(es->hloc32A);
1130 }
1131 else es->text = LocalLock(es->hloc32W);
1132 }
1133 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1134 es->lock_count++;
1135 }
1136
1137
1138 /*********************************************************************
1139 *
1140 * EDIT_UnlockBuffer
1141 *
1142 */
1143 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1144 {
1145
1146 /* Edit window might be already destroyed */
1147 if(!IsWindow(es->hwndSelf))
1148 {
1149 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1150 return;
1151 }
1152
1153 if (!es->lock_count) {
1154 ERR("lock_count == 0 ... please report\n");
1155 return;
1156 }
1157 if (!es->text) {
1158 ERR("es->text == 0 ... please report\n");
1159 return;
1160 }
1161
1162 if (force || (es->lock_count == 1)) {
1163 if (es->hloc32W) {
1164 UINT countA = 0;
1165 UINT countW = get_text_length(es) + 1;
1166
1167 if(es->hloc32A)
1168 {
1169 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1170 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1171 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1172 countA = LocalSize(es->hloc32A);
1173 if(countA_new > countA)
1174 {
1175 HLOCAL hloc32A_new;
1176 UINT alloc_size = ROUND_TO_GROW(countA_new);
1177 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1178 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1179 if(hloc32A_new)
1180 {
1181 es->hloc32A = hloc32A_new;
1182 countA = LocalSize(hloc32A_new);
1183 TRACE("Real new size %d bytes\n", countA);
1184 }
1185 else
1186 WARN("FAILED! Will synchronize partially\n");
1187 }
1188 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1189 LocalLock(es->hloc32A), countA, NULL, NULL);
1190 LocalUnlock(es->hloc32A);
1191 }
1192
1193 LocalUnlock(es->hloc32W);
1194 es->text = NULL;
1195 }
1196 else {
1197 ERR("no buffer ... please report\n");
1198 return;
1199 }
1200 }
1201 es->lock_count--;
1202 }
1203
1204
1205 /*********************************************************************
1206 *
1207 * EDIT_MakeFit
1208 *
1209 * Try to fit size + 1 characters in the buffer.
1210 */
1211 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1212 {
1213 HLOCAL hNew32W;
1214
1215 if (size <= es->buffer_size)
1216 return TRUE;
1217
1218 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1219
1220 /* Force edit to unlock it's buffer. es->text now NULL */
1221 EDIT_UnlockBuffer(es, TRUE);
1222
1223 if (es->hloc32W) {
1224 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1225 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1226 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1227 es->hloc32W = hNew32W;
1228 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1229 }
1230 }
1231
1232 EDIT_LockBuffer(es);
1233
1234 if (es->buffer_size < size) {
1235 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1236 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1237 return FALSE;
1238 } else {
1239 TRACE("We now have %d+1\n", es->buffer_size);
1240 return TRUE;
1241 }
1242 }
1243
1244
1245 /*********************************************************************
1246 *
1247 * EDIT_MakeUndoFit
1248 *
1249 * Try to fit size + 1 bytes in the undo buffer.
1250 *
1251 */
1252 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1253 {
1254 UINT alloc_size;
1255
1256 if (size <= es->undo_buffer_size)
1257 return TRUE;
1258
1259 TRACE("trying to ReAlloc to %d+1\n", size);
1260
1261 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1262 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1263 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1264 return TRUE;
1265 }
1266 else
1267 {
1268 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1269 return FALSE;
1270 }
1271 }
1272
1273
1274 /*********************************************************************
1275 *
1276 * EDIT_UpdateTextRegion
1277 *
1278 */
1279 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1280 {
1281 if (es->flags & EF_UPDATE) {
1282 es->flags &= ~EF_UPDATE;
1283 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1284 }
1285 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1286 }
1287
1288
1289 /*********************************************************************
1290 *
1291 * EDIT_UpdateText
1292 *
1293 */
1294 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1295 {
1296 if (es->flags & EF_UPDATE) {
1297 es->flags &= ~EF_UPDATE;
1298 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1299 }
1300 InvalidateRect(es->hwndSelf, rc, bErase);
1301 }
1302
1303 /*********************************************************************
1304 *
1305 * EDIT_SL_InvalidateText
1306 *
1307 * Called from EDIT_InvalidateText().
1308 * Does the job for single-line controls only.
1309 *
1310 */
1311 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1312 {
1313 RECT line_rect;
1314 RECT rc;
1315
1316 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1317 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1318 EDIT_UpdateText(es, &rc, TRUE);
1319 }
1320
1321
1322 static inline INT get_vertical_line_count(EDITSTATE *es)
1323 {
1324 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1325 return max(1,vlc);
1326 }
1327
1328 /*********************************************************************
1329 *
1330 * EDIT_ML_InvalidateText
1331 *
1332 * Called from EDIT_InvalidateText().
1333 * Does the job for multi-line controls only.
1334 *
1335 */
1336 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1337 {
1338 INT vlc = get_vertical_line_count(es);
1339 INT sl = EDIT_EM_LineFromChar(es, start);
1340 INT el = EDIT_EM_LineFromChar(es, end);
1341 INT sc;
1342 INT ec;
1343 RECT rc1;
1344 RECT rcWnd;
1345 RECT rcLine;
1346 RECT rcUpdate;
1347 INT l;
1348
1349 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1350 return;
1351
1352 sc = start - EDIT_EM_LineIndex(es, sl);
1353 ec = end - EDIT_EM_LineIndex(es, el);
1354 if (sl < es->y_offset) {
1355 sl = es->y_offset;
1356 sc = 0;
1357 }
1358 if (el > es->y_offset + vlc) {
1359 el = es->y_offset + vlc;
1360 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1361 }
1362 GetClientRect(es->hwndSelf, &rc1);
1363 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1364 if (sl == el) {
1365 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1366 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1367 EDIT_UpdateText(es, &rcUpdate, TRUE);
1368 } else {
1369 EDIT_GetLineRect(es, sl, sc,
1370 EDIT_EM_LineLength(es,
1371 EDIT_EM_LineIndex(es, sl)),
1372 &rcLine);
1373 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1374 EDIT_UpdateText(es, &rcUpdate, TRUE);
1375 for (l = sl + 1 ; l < el ; l++) {
1376 EDIT_GetLineRect(es, l, 0,
1377 EDIT_EM_LineLength(es,
1378 EDIT_EM_LineIndex(es, l)),
1379 &rcLine);
1380 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1381 EDIT_UpdateText(es, &rcUpdate, TRUE);
1382 }
1383 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1384 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1385 EDIT_UpdateText(es, &rcUpdate, TRUE);
1386 }
1387 }
1388
1389
1390 /*********************************************************************
1391 *
1392 * EDIT_InvalidateText
1393 *
1394 * Invalidate the text from offset start up to, but not including,
1395 * offset end. Useful for (re)painting the selection.
1396 * Regions outside the linewidth are not invalidated.
1397 * end == -1 means end == TextLength.
1398 * start and end need not be ordered.
1399 *
1400 */
1401 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1402 {
1403 if (end == start)
1404 return;
1405
1406 if (end == -1)
1407 end = get_text_length(es);
1408
1409 if (end < start) {
1410 INT tmp = start;
1411 start = end;
1412 end = tmp;
1413 }
1414
1415 if (es->style & ES_MULTILINE)
1416 EDIT_ML_InvalidateText(es, start, end);
1417 else
1418 EDIT_SL_InvalidateText(es, start, end);
1419 }
1420
1421
1422 /*********************************************************************
1423 *
1424 * EDIT_EM_SetSel
1425 *
1426 * note: unlike the specs say: the order of start and end
1427 * _is_ preserved in Windows. (i.e. start can be > end)
1428 * In other words: this handler is OK
1429 *
1430 */
1431 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1432 {
1433 UINT old_start = es->selection_start;
1434 UINT old_end = es->selection_end;
1435 UINT len = get_text_length(es);
1436
1437 if (start == (UINT)-1) {
1438 start = es->selection_end;
1439 end = es->selection_end;
1440 } else {
1441 start = min(start, len);
1442 end = min(end, len);
1443 }
1444 es->selection_start = start;
1445 es->selection_end = end;
1446 if (after_wrap)
1447 es->flags |= EF_AFTER_WRAP;
1448 else
1449 es->flags &= ~EF_AFTER_WRAP;
1450 /* Compute the necessary invalidation region. */
1451 /* Note that we don't need to invalidate regions which have
1452 * "never" been selected, or those which are "still" selected.
1453 * In fact, every time we hit a selection boundary, we can
1454 * *toggle* whether we need to invalidate. Thus we can optimize by
1455 * *sorting* the interval endpoints. Let's assume that we sort them
1456 * in this order:
1457 * start <= end <= old_start <= old_end
1458 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1459 * in 5 comparisons; i.e. it is impossible to do better than the
1460 * following: */
1461 ORDER_UINT(end, old_end);
1462 ORDER_UINT(start, old_start);
1463 ORDER_UINT(old_start, old_end);
1464 ORDER_UINT(start, end);
1465 /* Note that at this point 'end' and 'old_start' are not in order, but
1466 * start is definitely the min. and old_end is definitely the max. */
1467 if (end != old_start)
1468 {
1469 /*
1470 * One can also do
1471 * ORDER_UINT32(end, old_start);
1472 * EDIT_InvalidateText(es, start, end);
1473 * EDIT_InvalidateText(es, old_start, old_end);
1474 * in place of the following if statement.
1475 * (That would complete the optimal five-comparison four-element sort.)
1476 */
1477 if (old_start > end )
1478 {
1479 EDIT_InvalidateText(es, start, end);
1480 EDIT_InvalidateText(es, old_start, old_end);
1481 }
1482 else
1483 {
1484 EDIT_InvalidateText(es, start, old_start);
1485 EDIT_InvalidateText(es, end, old_end);
1486 }
1487 }
1488 else EDIT_InvalidateText(es, start, old_end);
1489 }
1490
1491
1492 /*********************************************************************
1493 *
1494 * EDIT_UpdateScrollInfo
1495 *
1496 */
1497 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1498 {
1499 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1500 {
1501 SCROLLINFO si;
1502 si.cbSize = sizeof(SCROLLINFO);
1503 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1504 si.nMin = 0;
1505 si.nMax = es->line_count - 1;
1506 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1507 si.nPos = es->y_offset;
1508 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1509 si.nMin, si.nMax, si.nPage, si.nPos);
1510 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1511 }
1512
1513 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1514 {
1515 SCROLLINFO si;
1516 si.cbSize = sizeof(SCROLLINFO);
1517 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1518 si.nMin = 0;
1519 si.nMax = es->text_width - 1;
1520 si.nPage = es->format_rect.right - es->format_rect.left;
1521 si.nPos = es->x_offset;
1522 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1523 si.nMin, si.nMax, si.nPage, si.nPos);
1524 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1525 }
1526 }
1527
1528
1529 /*********************************************************************
1530 *
1531 * EDIT_EM_LineScroll_internal
1532 *
1533 * Version of EDIT_EM_LineScroll for internal use.
1534 * It doesn't refuse if ES_MULTILINE is set and assumes that
1535 * dx is in pixels, dy - in lines.
1536 *
1537 */
1538 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1539 {
1540 INT nyoff;
1541 INT x_offset_in_pixels;
1542 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1543 es->line_height;
1544
1545 if (es->style & ES_MULTILINE)
1546 {
1547 x_offset_in_pixels = es->x_offset;
1548 }
1549 else
1550 {
1551 dy = 0;
1552 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1553 }
1554
1555 if (-dx > x_offset_in_pixels)
1556 dx = -x_offset_in_pixels;
1557 if (dx > es->text_width - x_offset_in_pixels)
1558 dx = es->text_width - x_offset_in_pixels;
1559 nyoff = max(0, es->y_offset + dy);
1560 if (nyoff >= es->line_count - lines_per_page)
1561 nyoff = max(0, es->line_count - lines_per_page);
1562 dy = (es->y_offset - nyoff) * es->line_height;
1563 if (dx || dy) {
1564 RECT rc1;
1565 RECT rc;
1566
1567 es->y_offset = nyoff;
1568 if(es->style & ES_MULTILINE)
1569 es->x_offset += dx;
1570 else
1571 es->x_offset += dx / es->char_width;
1572
1573 GetClientRect(es->hwndSelf, &rc1);
1574 IntersectRect(&rc, &rc1, &es->format_rect);
1575 ScrollWindowEx(es->hwndSelf, -dx, dy,
1576 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1577 /* force scroll info update */
1578 EDIT_UpdateScrollInfo(es);
1579 }
1580 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1581 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1582 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1583 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1584 return TRUE;
1585 }
1586
1587 /*********************************************************************
1588 *
1589 * EM_LINESCROLL
1590 *
1591 * NOTE: dx is in average character widths, dy - in lines;
1592 *
1593 */
1594 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1595 {
1596 if (!(es->style & ES_MULTILINE))
1597 return FALSE;
1598
1599 dx *= es->char_width;
1600 return EDIT_EM_LineScroll_internal(es, dx, dy);
1601 }
1602
1603
1604 /*********************************************************************
1605 *
1606 * EM_SCROLL
1607 *
1608 */
1609 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1610 {
1611 INT dy;
1612
1613 if (!(es->style & ES_MULTILINE))
1614 return (LRESULT)FALSE;
1615
1616 dy = 0;
1617
1618 switch (action) {
1619 case SB_LINEUP:
1620 if (es->y_offset)
1621 dy = -1;
1622 break;
1623 case SB_LINEDOWN:
1624 if (es->y_offset < es->line_count - 1)
1625 dy = 1;
1626 break;
1627 case SB_PAGEUP:
1628 if (es->y_offset)
1629 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1630 break;
1631 case SB_PAGEDOWN:
1632 if (es->y_offset < es->line_count - 1)
1633 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1634 break;
1635 default:
1636 return (LRESULT)FALSE;
1637 }
1638 if (dy) {
1639 INT vlc = get_vertical_line_count(es);
1640 /* check if we are going to move too far */
1641 if(es->y_offset + dy > es->line_count - vlc)
1642 dy = max(es->line_count - vlc, 0) - es->y_offset;
1643
1644 /* Notification is done in EDIT_EM_LineScroll */
1645 if(dy) {
1646 EDIT_EM_LineScroll(es, 0, dy);
1647 return MAKELONG(dy, TRUE);
1648 }
1649
1650 }
1651 return (LRESULT)FALSE;
1652 }
1653
1654
1655 /*********************************************************************
1656 *
1657 * EDIT_SetCaretPos
1658 *
1659 */
1660 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1661 BOOL after_wrap)
1662 {
1663 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1664 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1665 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1666 }
1667
1668
1669 /*********************************************************************
1670 *
1671 * EM_SCROLLCARET
1672 *
1673 */
1674 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1675 {
1676 if (es->style & ES_MULTILINE) {
1677 INT l;
1678 INT vlc;
1679 INT ww;
1680 INT cw = es->char_width;
1681 INT x;
1682 INT dy = 0;
1683 INT dx = 0;
1684
1685 l = EDIT_EM_LineFromChar(es, es->selection_end);
1686 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1687 vlc = get_vertical_line_count(es);
1688 if (l >= es->y_offset + vlc)
1689 dy = l - vlc + 1 - es->y_offset;
1690 if (l < es->y_offset)
1691 dy = l - es->y_offset;
1692 ww = es->format_rect.right - es->format_rect.left;
1693 if (x < es->format_rect.left)
1694 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1695 if (x > es->format_rect.right)
1696 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1697 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1698 {
1699 /* check if we are going to move too far */
1700 if(es->x_offset + dx + ww > es->text_width)
1701 dx = es->text_width - ww - es->x_offset;
1702 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1703 EDIT_EM_LineScroll_internal(es, dx, dy);
1704 }
1705 } else {
1706 INT x;
1707 INT goal;
1708 INT format_width;
1709
1710 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1711 format_width = es->format_rect.right - es->format_rect.left;
1712 if (x < es->format_rect.left) {
1713 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1714 do {
1715 es->x_offset--;
1716 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1717 } while ((x < goal) && es->x_offset);
1718 /* FIXME: use ScrollWindow() somehow to improve performance */
1719 EDIT_UpdateText(es, NULL, TRUE);
1720 } else if (x > es->format_rect.right) {
1721 INT x_last;
1722 INT len = get_text_length(es);
1723 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1724 do {
1725 es->x_offset++;
1726 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1727 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1728 } while ((x > goal) && (x_last > es->format_rect.right));
1729 /* FIXME: use ScrollWindow() somehow to improve performance */
1730 EDIT_UpdateText(es, NULL, TRUE);
1731 }
1732 }
1733
1734 if(es->flags & EF_FOCUSED)
1735 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1736 }
1737
1738
1739 /*********************************************************************
1740 *
1741 * EDIT_MoveBackward
1742 *
1743 */
1744 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1745 {
1746 INT e = es->selection_end;
1747
1748 if (e) {
1749 e--;
1750 if ((es->style & ES_MULTILINE) && e &&
1751 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1752 e--;
1753 if (e && (es->text[e - 1] == '\r'))
1754 e--;
1755 }
1756 }
1757 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1758 EDIT_EM_ScrollCaret(es);
1759 }
1760
1761
1762 /*********************************************************************
1763 *
1764 * EDIT_MoveDown_ML
1765 *
1766 * Only for multi line controls
1767 * Move the caret one line down, on a column with the nearest
1768 * x coordinate on the screen (might be a different column).
1769 *
1770 */
1771 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1772 {
1773 INT s = es->selection_start;
1774 INT e = es->selection_end;
1775 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1776 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1777 INT x = (short)LOWORD(pos);
1778 INT y = (short)HIWORD(pos);
1779
1780 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1781 if (!extend)
1782 s = e;
1783 EDIT_EM_SetSel(es, s, e, after_wrap);
1784 EDIT_EM_ScrollCaret(es);
1785 }
1786
1787
1788 /*********************************************************************
1789 *
1790 * EDIT_MoveEnd
1791 *
1792 */
1793 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1794 {
1795 BOOL after_wrap = FALSE;
1796 INT e;
1797
1798 /* Pass a high value in x to make sure of receiving the end of the line */
1799 if (!ctrl && (es->style & ES_MULTILINE))
1800 e = EDIT_CharFromPos(es, 0x3fffffff,
1801 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1802 else
1803 e = get_text_length(es);
1804 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1805 EDIT_EM_ScrollCaret(es);
1806 }
1807
1808
1809 /*********************************************************************
1810 *
1811 * EDIT_MoveForward
1812 *
1813 */
1814 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1815 {
1816 INT e = es->selection_end;
1817
1818 if (es->text[e]) {
1819 e++;
1820 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1821 if (es->text[e] == '\n')
1822 e++;
1823 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1824 e += 2;
1825 }
1826 }
1827 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1828 EDIT_EM_ScrollCaret(es);
1829 }
1830
1831
1832 /*********************************************************************
1833 *
1834 * EDIT_MoveHome
1835 *
1836 * Home key: move to beginning of line.
1837 *
1838 */
1839 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1840 {
1841 INT e;
1842
1843 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1844 if (!ctrl && (es->style & ES_MULTILINE))
1845 e = EDIT_CharFromPos(es, -es->x_offset,
1846 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1847 else
1848 e = 0;
1849 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1850 EDIT_EM_ScrollCaret(es);
1851 }
1852
1853
1854 /*********************************************************************
1855 *
1856 * EDIT_MovePageDown_ML
1857 *
1858 * Only for multi line controls
1859 * Move the caret one page down, on a column with the nearest
1860 * x coordinate on the screen (might be a different column).
1861 *
1862 */
1863 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1864 {
1865 INT s = es->selection_start;
1866 INT e = es->selection_end;
1867 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1868 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1869 INT x = (short)LOWORD(pos);
1870 INT y = (short)HIWORD(pos);
1871
1872 e = EDIT_CharFromPos(es, x,
1873 y + (es->format_rect.bottom - es->format_rect.top),
1874 &after_wrap);
1875 if (!extend)
1876 s = e;
1877 EDIT_EM_SetSel(es, s, e, after_wrap);
1878 EDIT_EM_ScrollCaret(es);
1879 }
1880
1881
1882 /*********************************************************************
1883 *
1884 * EDIT_MovePageUp_ML
1885 *
1886 * Only for multi line controls
1887 * Move the caret one page up, on a column with the nearest
1888 * x coordinate on the screen (might be a different column).
1889 *
1890 */
1891 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1892 {
1893 INT s = es->selection_start;
1894 INT e = es->selection_end;
1895 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1896 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1897 INT x = (short)LOWORD(pos);
1898 INT y = (short)HIWORD(pos);
1899
1900 e = EDIT_CharFromPos(es, x,
1901 y - (es->format_rect.bottom - es->format_rect.top),
1902 &after_wrap);
1903 if (!extend)
1904 s = e;
1905 EDIT_EM_SetSel(es, s, e, after_wrap);
1906 EDIT_EM_ScrollCaret(es);
1907 }
1908
1909
1910 /*********************************************************************
1911 *
1912 * EDIT_MoveUp_ML
1913 *
1914 * Only for multi line controls
1915 * Move the caret one line up, on a column with the nearest
1916 * x coordinate on the screen (might be a different column).
1917 *
1918 */
1919 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1920 {
1921 INT s = es->selection_start;
1922 INT e = es->selection_end;
1923 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1924 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1925 INT x = (short)LOWORD(pos);
1926 INT y = (short)HIWORD(pos);
1927
1928 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1929 if (!extend)
1930 s = e;
1931 EDIT_EM_SetSel(es, s, e, after_wrap);
1932 EDIT_EM_ScrollCaret(es);
1933 }
1934
1935
1936 /*********************************************************************
1937 *
1938 * EDIT_MoveWordBackward
1939 *
1940 */
1941 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1942 {
1943 INT s = es->selection_start;
1944 INT e = es->selection_end;
1945 INT l;
1946 INT ll;
1947 INT li;
1948
1949 l = EDIT_EM_LineFromChar(es, e);
1950 ll = EDIT_EM_LineLength(es, e);
1951 li = EDIT_EM_LineIndex(es, l);
1952 if (e - li == 0) {
1953 if (l) {
1954 li = EDIT_EM_LineIndex(es, l - 1);
1955 e = li + EDIT_EM_LineLength(es, li);
1956 }
1957 } else {
1958 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
1959 }
1960 if (!extend)
1961 s = e;
1962 EDIT_EM_SetSel(es, s, e, FALSE);
1963 EDIT_EM_ScrollCaret(es);
1964 }
1965
1966
1967 /*********************************************************************
1968 *
1969 * EDIT_MoveWordForward
1970 *
1971 */
1972 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1973 {
1974 INT s = es->selection_start;
1975 INT e = es->selection_end;
1976 INT l;
1977 INT ll;
1978 INT li;
1979
1980 l = EDIT_EM_LineFromChar(es, e);
1981 ll = EDIT_EM_LineLength(es, e);
1982 li = EDIT_EM_LineIndex(es, l);
1983 if (e - li == ll) {
1984 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1985 e = EDIT_EM_LineIndex(es, l + 1);
1986 } else {
1987 e = li + EDIT_CallWordBreakProc(es,
1988 li, e - li + 1, ll, WB_RIGHT);
1989 }
1990 if (!extend)
1991 s = e;
1992 EDIT_EM_SetSel(es, s, e, FALSE);
1993 EDIT_EM_ScrollCaret(es);
1994 }
1995
1996
1997 /*********************************************************************
1998 *
1999 * EDIT_PaintText
2000 *
2001 */
2002 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2003 {
2004 COLORREF BkColor;
2005 COLORREF TextColor;
2006 LOGFONTW underline_font;
2007 HFONT hUnderline = 0;
2008 HFONT old_font = 0;
2009 INT ret;
2010 INT li;
2011 INT BkMode;
2012 SIZE size;
2013
2014 if (!count)
2015 return 0;
2016 BkMode = GetBkMode(dc);
2017 BkColor = GetBkColor(dc);
2018 TextColor = GetTextColor(dc);
2019 if (rev) {
2020 if (es->composition_len == 0)
2021 {
2022 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2023 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2024 SetBkMode( dc, OPAQUE);
2025 }
2026 else
2027 {
2028 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2029 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2030 underline_font.lfUnderline = TRUE;
2031 hUnderline = CreateFontIndirectW(&underline_font);
2032 old_font = SelectObject(dc,hUnderline);
2033 }
2034 }
2035 li = EDIT_EM_LineIndex(es, line);
2036 if (es->style & ES_MULTILINE) {
2037 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2038 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2039 } else {
2040 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2041 TextOutW(dc, x, y, text + li + col, count);
2042 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2043 ret = size.cx;
2044 if (es->style & ES_PASSWORD)
2045 HeapFree(GetProcessHeap(), 0, text);
2046 }
2047 if (rev) {
2048 if (es->composition_len == 0)
2049 {
2050 SetBkColor(dc, BkColor);
2051 SetTextColor(dc, TextColor);
2052 SetBkMode( dc, BkMode);
2053 }
2054 else
2055 {
2056 if (old_font)
2057 SelectObject(dc,old_font);
2058 if (hUnderline)
2059 DeleteObject(hUnderline);
2060 }
2061 }
2062 return ret;
2063 }
2064
2065
2066 /*********************************************************************
2067 *
2068 * EDIT_PaintLine
2069 *
2070 */
2071 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2072 {
2073 INT s = es->selection_start;
2074 INT e = es->selection_end;
2075 INT li;
2076 INT ll;
2077 INT x;
2078 INT y;
2079 LRESULT pos;
2080
2081 if (es->style & ES_MULTILINE) {
2082 INT vlc = get_vertical_line_count(es);
2083
2084 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2085 return;
2086 } else if (line)
2087 return;
2088
2089 TRACE("line=%d\n", line);
2090
2091 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2092 x = (short)LOWORD(pos);
2093 y = (short)HIWORD(pos);
2094 li = EDIT_EM_LineIndex(es, line);
2095 ll = EDIT_EM_LineLength(es, li);
2096 s = min(es->selection_start, es->selection_end);
2097 e = max(es->selection_start, es->selection_end);
2098 s = min(li + ll, max(li, s));
2099 e = min(li + ll, max(li, e));
2100 if (rev && (s != e) &&
2101 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2102 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2103 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2104 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2105 } else
2106 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2107 }
2108
2109
2110 /*********************************************************************
2111 *
2112 * EDIT_AdjustFormatRect
2113 *
2114 * Adjusts the format rectangle for the current font and the
2115 * current client rectangle.
2116 *
2117 */
2118 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2119 {
2120 RECT ClientRect;
2121
2122 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2123 if (es->style & ES_MULTILINE)
2124 {
2125 INT fw, vlc, max_x_offset, max_y_offset;
2126
2127 vlc = get_vertical_line_count(es);
2128 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2129
2130 /* correct es->x_offset */
2131 fw = es->format_rect.right - es->format_rect.left;
2132 max_x_offset = es->text_width - fw;
2133 if(max_x_offset < 0) max_x_offset = 0;
2134 if(es->x_offset > max_x_offset)
2135 es->x_offset = max_x_offset;
2136
2137 /* correct es->y_offset */
2138 max_y_offset = es->line_count - vlc;
2139 if(max_y_offset < 0) max_y_offset = 0;
2140 if(es->y_offset > max_y_offset)
2141 es->y_offset = max_y_offset;
2142
2143 /* force scroll info update */
2144 EDIT_UpdateScrollInfo(es);
2145 }
2146 else
2147 /* Windows doesn't care to fix text placement for SL controls */
2148 es->format_rect.bottom = es->format_rect.top + es->line_height;
2149
2150 /* Always stay within the client area */
2151 GetClientRect(es->hwndSelf, &ClientRect);
2152 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2153
2154 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2155 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2156
2157 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2158 }
2159
2160
2161 /*********************************************************************
2162 *
2163 * EDIT_SetRectNP
2164 *
2165 * note: this is not (exactly) the handler called on EM_SETRECTNP
2166 * it is also used to set the rect of a single line control
2167 *
2168 */
2169 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2170 {
2171 LONG_PTR ExStyle;
2172 INT bw, bh;
2173 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2174
2175 CopyRect(&es->format_rect, rc);
2176
2177 if (ExStyle & WS_EX_CLIENTEDGE) {
2178 es->format_rect.left++;
2179 es->format_rect.right--;
2180
2181 if (es->format_rect.bottom - es->format_rect.top
2182 >= es->line_height + 2)
2183 {
2184 es->format_rect.top++;
2185 es->format_rect.bottom--;
2186 }
2187 }
2188 else if (es->style & WS_BORDER) {
2189 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2190 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2191 es->format_rect.left += bw;
2192 es->format_rect.right -= bw;
2193 if (es->format_rect.bottom - es->format_rect.top
2194 >= es->line_height + 2 * bh)
2195 {
2196 es->format_rect.top += bh;
2197 es->format_rect.bottom -= bh;
2198 }
2199 }
2200
2201 es->format_rect.left += es->left_margin;
2202 es->format_rect.right -= es->right_margin;
2203 EDIT_AdjustFormatRect(es);
2204 }
2205
2206
2207 /*********************************************************************
2208 *
2209 * EM_CHARFROMPOS
2210 *
2211 * returns line number (not index) in high-order word of result.
2212 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2213 * to Richedit, not to the edit control. Original documentation is valid.
2214 * FIXME: do the specs mean to return -1 if outside client area or
2215 * if outside formatting rectangle ???
2216 *
2217 */
2218 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2219 {
2220 POINT pt;
2221 RECT rc;
2222 INT index;
2223
2224 pt.x = x;
2225 pt.y = y;
2226 GetClientRect(es->hwndSelf, &rc);
2227 if (!PtInRect(&rc, pt))
2228 return -1;
2229
2230 index = EDIT_CharFromPos(es, x, y, NULL);
2231 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2232 }
2233
2234
2235 /*********************************************************************
2236 *
2237 * EM_FMTLINES
2238 *
2239 * Enable or disable soft breaks.
2240 *
2241 * This means: insert or remove the soft linebreak character (\r\r\n).
2242 * Take care to check if the text still fits the buffer after insertion.
2243 * If not, notify with EN_ERRSPACE.
2244 *
2245 */
2246 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2247 {
2248 es->flags &= ~EF_USE_SOFTBRK;
2249 if (add_eol) {
2250 es->flags |= EF_USE_SOFTBRK;
2251 FIXME("soft break enabled, not implemented\n");
2252 }
2253 return add_eol;
2254 }
2255
2256
2257 /*********************************************************************
2258 *
2259 * EM_GETHANDLE
2260 *
2261 * Hopefully this won't fire back at us.
2262 * We always start with a fixed buffer in the local heap.
2263 * Despite of the documentation says that the local heap is used
2264 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2265 * buffer on the local heap.
2266 *
2267 */
2268 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2269 {
2270 HLOCAL hLocal;
2271
2272 if (!(es->style & ES_MULTILINE))
2273 return 0;
2274
2275 if(es->is_unicode)
2276 hLocal = es->hloc32W;
2277 else
2278 {
2279 if(!es->hloc32A)
2280 {
2281 CHAR *textA;
2282 UINT countA, alloc_size;
2283 TRACE("Allocating 32-bit ANSI alias buffer\n");
2284 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2285 alloc_size = ROUND_TO_GROW(countA);
2286 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2287 {
2288 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2289 return 0;
2290 }
2291 textA = LocalLock(es->hloc32A);
2292 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2293 LocalUnlock(es->hloc32A);
2294 }
2295 hLocal = es->hloc32A;
2296 }
2297
2298 es->flags |= EF_APP_HAS_HANDLE;
2299 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2300 return hLocal;
2301 }
2302
2303
2304 /*********************************************************************
2305 *
2306 * EM_GETLINE
2307 *
2308 */
2309 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2310 {
2311 LPWSTR src;
2312 INT line_len, dst_len;
2313 INT i;
2314
2315 if (es->style & ES_MULTILINE) {
2316 if (line >= es->line_count)
2317 return 0;
2318 } else
2319 line = 0;
2320 i = EDIT_EM_LineIndex(es, line);
2321 src = es->text + i;
2322 line_len = EDIT_EM_LineLength(es, i);
2323 dst_len = *(WORD *)dst;
2324 if(unicode)
2325 {
2326 if(dst_len <= line_len)
2327 {
2328 memcpy(dst, src, dst_len * sizeof(WCHAR));
2329 return dst_len;
2330 }
2331 else /* Append 0 if enough space */
2332 {
2333 memcpy(dst, src, line_len * sizeof(WCHAR));
2334 dst[line_len] = 0;
2335 return line_len;
2336 }
2337 }
2338 else
2339 {
2340 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2341 if(!ret && line_len) /* Insufficient buffer size */
2342 return dst_len;
2343 if(ret < dst_len) /* Append 0 if enough space */
2344 ((LPSTR)dst)[ret] = 0;
2345 return ret;
2346 }
2347 }
2348
2349
2350 /*********************************************************************
2351 *
2352 * EM_GETSEL
2353 *
2354 */
2355 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2356 {
2357 UINT s = es->selection_start;
2358 UINT e = es->selection_end;
2359
2360 ORDER_UINT(s, e);
2361 if (start)
2362 *start = s;
2363 if (end)
2364 *end = e;
2365 return MAKELONG(s, e);
2366 }
2367
2368
2369 /*********************************************************************
2370 *
2371 * EM_REPLACESEL
2372 *
2373 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2374 *
2375 */
2376 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2377 {
2378 UINT strl = strlenW(lpsz_replace);
2379 UINT tl = get_text_length(es);
2380 UINT utl;
2381 UINT s;
2382 UINT e;
2383 UINT i;
2384 UINT size;
2385 LPWSTR p;
2386 HRGN hrgn = 0;
2387 LPWSTR buf = NULL;
2388 UINT bufl = 0;
2389
2390 TRACE("%s, can_undo %d, send_update %d\n",
2391 debugstr_w(lpsz_replace), can_undo, send_update);
2392
2393 s = es->selection_start;
2394 e = es->selection_end;
2395
2396 if ((s == e) && !strl)
2397 return;
2398
2399 ORDER_UINT(s, e);
2400
2401 size = tl - (e - s) + strl;
2402 if (!size)
2403 es->text_width = 0;
2404
2405 /* Issue the EN_MAXTEXT notification and continue with replacing text
2406 * such that buffer limit is honored. */
2407 if ((honor_limit) && (size > es->buffer_limit)) {
2408 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2409 /* Buffer limit can be smaller than the actual length of text in combobox */
2410 if (es->buffer_limit < (tl - (e-s)))
2411 strl = 0;
2412 else
2413 strl = es->buffer_limit - (tl - (e-s));
2414 }
2415
2416 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2417 return;
2418
2419 if (e != s) {
2420 /* there is something to be deleted */
2421 TRACE("deleting stuff.\n");
2422 bufl = e - s;
2423 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2424 if (!buf) return;
2425 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2426 buf[bufl] = 0; /* ensure 0 termination */
2427 /* now delete */
2428 strcpyW(es->text + s, es->text + e);
2429 text_buffer_changed(es);
2430 }
2431 if (strl) {
2432 /* there is an insertion */
2433 tl = get_text_length(es);
2434 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));
2435 for (p = es->text + tl ; p >= es->text + s ; p--)
2436 p[strl] = p[0];
2437 for (i = 0 , p = es->text + s ; i < strl ; i++)
2438 p[i] = lpsz_replace[i];
2439 if(es->style & ES_UPPERCASE)
2440 CharUpperBuffW(p, strl);
2441 else if(es->style & ES_LOWERCASE)
2442 CharLowerBuffW(p, strl);
2443 text_buffer_changed(es);
2444 }
2445 if (es->style & ES_MULTILINE)
2446 {
2447 INT st = min(es->selection_start, es->selection_end);
2448 INT vlc = get_vertical_line_count(es);
2449
2450 hrgn = CreateRectRgn(0, 0, 0, 0);
2451 EDIT_BuildLineDefs_ML(es, st, st + strl,
2452 strl - abs(es->selection_end - es->selection_start), hrgn);
2453 /* if text is too long undo all changes */
2454 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2455 if (strl)
2456 strcpyW(es->text + e, es->text + e + strl);
2457 if (e != s)
2458 for (i = 0 , p = es->text ; i < e - s ; i++)
2459 p[i + s] = buf[i];
2460 text_buffer_changed(es);
2461 EDIT_BuildLineDefs_ML(es, s, e,
2462 abs(es->selection_end - es->selection_start) - strl, hrgn);
2463 strl = 0;
2464 e = s;
2465 hrgn = CreateRectRgn(0, 0, 0, 0);
2466 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2467 }
2468 }
2469 else {
2470 INT fw = es->format_rect.right - es->format_rect.left;
2471 EDIT_CalcLineWidth_SL(es);
2472 /* remove chars that don't fit */
2473 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2474 while ((es->text_width > fw) && s + strl >= s) {
2475 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2476 strl--;
2477 EDIT_CalcLineWidth_SL(es);
2478 }
2479 text_buffer_changed(es);
2480 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2481 }
2482 }
2483
2484 if (e != s) {
2485 if (can_undo) {
2486 utl = strlenW(es->undo_text);
2487 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2488 /* undo-buffer is extended to the right */
2489 EDIT_MakeUndoFit(es, utl + e - s);
2490 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2491 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2492 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2493 /* undo-buffer is extended to the left */
2494 EDIT_MakeUndoFit(es, utl + e - s);
2495 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2496 p[e - s] = p[0];
2497 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2498 p[i] = buf[i];
2499 es->undo_position = s;
2500 } else {
2501 /* new undo-buffer */
2502 EDIT_MakeUndoFit(es, e - s);
2503 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2504 es->undo_text[e - s] = 0; /* ensure 0 termination */
2505 es->undo_position = s;
2506 }
2507 /* any deletion makes the old insertion-undo invalid */
2508 es->undo_insert_count = 0;
2509 } else
2510 EDIT_EM_EmptyUndoBuffer(es);
2511 }
2512 if (strl) {
2513 if (can_undo) {
2514 if ((s == es->undo_position) ||
2515 ((es->undo_insert_count) &&
2516 (s == es->undo_position + es->undo_insert_count)))
2517 /*
2518 * insertion is new and at delete position or
2519 * an extension to either left or right
2520 */
2521 es->undo_insert_count += strl;
2522 else {
2523 /* new insertion undo */
2524 es->undo_position = s;
2525 es->undo_insert_count = strl;
2526 /* new insertion makes old delete-buffer invalid */
2527 *es->undo_text = '\0';
2528 }
2529 } else
2530 EDIT_EM_EmptyUndoBuffer(es);
2531 }
2532
2533 if (bufl)
2534 HeapFree(GetProcessHeap(), 0, buf);
2535
2536 s += strl;
2537
2538 /* If text has been deleted and we're right or center aligned then scroll rightward */
2539 if (es->style & (ES_RIGHT | ES_CENTER))
2540 {
2541 INT delta = strl - abs(es->selection_end - es->selection_start);
2542
2543 if (delta < 0 && es->x_offset)
2544 {
2545 if (abs(delta) > es->x_offset)
2546 es->x_offset = 0;
2547 else
2548 es->x_offset += delta;
2549 }
2550 }
2551
2552 EDIT_EM_SetSel(es, s, s, FALSE);
2553 es->flags |= EF_MODIFIED;
2554 if (send_update) es->flags |= EF_UPDATE;
2555 if (hrgn)
2556 {
2557 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2558 DeleteObject(hrgn);
2559 }
2560 else
2561 EDIT_UpdateText(es, NULL, TRUE);
2562
2563 EDIT_EM_ScrollCaret(es);
2564
2565 /* force scroll info update */
2566 EDIT_UpdateScrollInfo(es);
2567
2568
2569 if(send_update || (es->flags & EF_UPDATE))
2570 {
2571 es->flags &= ~EF_UPDATE;
2572 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2573 }
2574 }
2575
2576
2577 /*********************************************************************
2578 *
2579 * EM_SETHANDLE
2580 *
2581 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2582 *
2583 */
2584 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2585 {
2586 if (!(es->style & ES_MULTILINE))
2587 return;
2588
2589 if (!hloc) {
2590 WARN("called with NULL handle\n");
2591 return;
2592 }
2593
2594 EDIT_UnlockBuffer(es, TRUE);
2595
2596 if(es->is_unicode)
2597 {
2598 if(es->hloc32A)
2599 {
2600 LocalFree(es->hloc32A);
2601 es->hloc32A = NULL;
2602 }
2603 es->hloc32W = hloc;
2604 }
2605 else
2606 {
2607 INT countW, countA;
2608 HLOCAL hloc32W_new;
2609 WCHAR *textW;
2610 CHAR *textA;
2611
2612 countA = LocalSize(hloc);
2613 textA = LocalLock(hloc);
2614 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2615 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2616 {
2617 ERR("Could not allocate new unicode buffer\n");
2618 return;
2619 }
2620 textW = LocalLock(hloc32W_new);
2621 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2622 LocalUnlock(hloc32W_new);
2623 LocalUnlock(hloc);
2624
2625 if(es->hloc32W)
2626 LocalFree(es->hloc32W);
2627
2628 es->hloc32W = hloc32W_new;
2629 es->hloc32A = hloc;
2630 }
2631
2632 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2633
2634 es->flags |= EF_APP_HAS_HANDLE;
2635 EDIT_LockBuffer(es);
2636
2637 es->x_offset = es->y_offset = 0;
2638 es->selection_start = es->selection_end = 0;
2639 EDIT_EM_EmptyUndoBuffer(es);
2640 es->flags &= ~EF_MODIFIED;
2641 es->flags &= ~EF_UPDATE;
2642 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2643 EDIT_UpdateText(es, NULL, TRUE);
2644 EDIT_EM_ScrollCaret(es);
2645 /* force scroll info update */
2646 EDIT_UpdateScrollInfo(es);
2647 }
2648
2649
2650 /*********************************************************************
2651 *
2652 * EM_SETLIMITTEXT
2653 *
2654 * NOTE: this version currently implements WinNT limits
2655 *
2656 */
2657 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2658 {
2659 if (!limit) limit = ~0u;
2660 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2661 es->buffer_limit = limit;
2662 }
2663
2664
2665 /*********************************************************************
2666 *
2667 * EM_SETMARGINS
2668 *
2669 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2670 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2671 * margin according to the textmetrics of the current font.
2672 *
2673 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2674 * of the char's width as the margin, but this is not how Windows handles this.
2675 * For all other fonts Windows sets the margins to zero.
2676 *
2677 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2678 * edit control is equal to or larger than a certain size.
2679 * Interestingly if one subtracts both the left and right margins from
2680 * this size one always seems to get an even number. The extents of
2681 * the (four character) string "'**'" match this quite closely, so
2682 * we'll use this until we come up with a better idea.
2683 */
2684 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2685 {
2686 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2687 SIZE sz;
2688
2689 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2690 return sz.cx + left + right;
2691 }
2692
2693 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2694 WORD left, WORD right, BOOL repaint)
2695 {
2696 TEXTMETRICW tm;
2697 INT default_left_margin = 0; /* in pixels */
2698 INT default_right_margin = 0; /* in pixels */
2699
2700 /* Set the default margins depending on the font */
2701 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2702 HDC dc = GetDC(es->hwndSelf);
2703 HFONT old_font = SelectObject(dc, es->font);
2704 GetTextMetricsW(dc, &tm);
2705 /* The default margins are only non zero for TrueType or Vector fonts */
2706 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2707 int min_size;
2708 RECT rc;
2709 /* This must be calculated more exactly! But how? */
2710 default_left_margin = tm.tmAveCharWidth / 2;
2711 default_right_margin = tm.tmAveCharWidth / 2;
2712 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2713 GetClientRect(es->hwndSelf, &rc);
2714 if(rc.right - rc.left < min_size) {
2715 default_left_margin = es->left_margin;
2716 default_right_margin = es->right_margin;
2717 }
2718 }
2719 SelectObject(dc, old_font);
2720 ReleaseDC(es->hwndSelf, dc);
2721 }
2722
2723 if (action & EC_LEFTMARGIN) {
2724 es->format_rect.left -= es->left_margin;
2725 if (left != EC_USEFONTINFO)
2726 es->left_margin = left;
2727 else
2728 es->left_margin = default_left_margin;
2729 es->format_rect.left += es->left_margin;
2730 }
2731
2732 if (action & EC_RIGHTMARGIN) {
2733 es->format_rect.right += es->right_margin;
2734 if (right != EC_USEFONTINFO)
2735 es->right_margin = right;
2736 else
2737 es->right_margin = default_right_margin;
2738 es->format_rect.right -= es->right_margin;
2739 }
2740
2741 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2742 EDIT_AdjustFormatRect(es);
2743 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2744 }
2745
2746 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2747 }
2748
2749
2750 /*********************************************************************
2751 *
2752 * EM_SETPASSWORDCHAR
2753 *
2754 */
2755 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2756 {
2757 LONG style;
2758
2759 if (es->style & ES_MULTILINE)
2760 return;
2761
2762 if (es->password_char == c)
2763 return;
2764
2765 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2766 es->password_char = c;
2767 if (c) {
2768 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2769 es->style |= ES_PASSWORD;
2770 } else {
2771 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2772 es->style &= ~ES_PASSWORD;
2773 }
2774 EDIT_UpdateText(es, NULL, TRUE);
2775 }
2776
2777
2778 /*********************************************************************
2779 *
2780 * EM_SETTABSTOPS
2781 *
2782 */
2783 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2784 {
2785 if (!(es->style & ES_MULTILINE))
2786 return FALSE;
2787 HeapFree(GetProcessHeap(), 0, es->tabs);
2788 es->tabs_count = count;
2789 if (!count)
2790 es->tabs = NULL;
2791 else {
2792 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2793 if (es->tabs == NULL) // reactos r33503
2794 {
2795 es->tabs_count = 0;
2796 return FALSE;
2797 } // reactos r33503
2798 memcpy(es->tabs, tabs, count * sizeof(INT));
2799 }
2800 return TRUE;
2801 }
2802
2803
2804 /*********************************************************************
2805 *
2806 * EM_SETWORDBREAKPROC
2807 *
2808 */
2809 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2810 {
2811 if (es->word_break_proc == wbp)
2812 return;
2813
2814 es->word_break_proc = wbp;
2815
2816 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2817 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2818 EDIT_UpdateText(es, NULL, TRUE);
2819 }
2820 }
2821
2822
2823 /*********************************************************************
2824 *
2825 * EM_UNDO / WM_UNDO
2826 *
2827 */
2828 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2829 {
2830 INT ulength;
2831 LPWSTR utext;
2832
2833 /* As per MSDN spec, for a single-line edit control,
2834 the return value is always TRUE */
2835 if( es->style & ES_READONLY )
2836 return !(es->style & ES_MULTILINE);
2837
2838 ulength = strlenW(es->undo_text);
2839
2840 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
2841 if (utext == NULL) // reactos r33503
2842 return FALSE; // reactos r33503
2843
2844 strcpyW(utext, es->undo_text);
2845
2846 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2847 es->undo_insert_count, debugstr_w(utext));
2848
2849 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2850 EDIT_EM_EmptyUndoBuffer(es);
2851 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
2852 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2853 /* send the notification after the selection start and end are set */
2854 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2855 EDIT_EM_ScrollCaret(es);
2856 HeapFree(GetProcessHeap(), 0, utext);
2857
2858 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2859 es->undo_insert_count, debugstr_w(es->undo_text));
2860 return TRUE;
2861 }
2862
2863
2864 /* Helper function for WM_CHAR
2865 *
2866 * According to an MSDN blog article titled "Just because you're a control
2867 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2868 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2869 * a dialog box or not.
2870 */
2871 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2872 {
2873 return (es->flags & EF_DIALOGMODE);
2874 }
2875
2876
2877 /*********************************************************************
2878 *
2879 * WM_PASTE
2880 *
2881 */
2882 static void EDIT_WM_Paste(EDITSTATE *es)
2883 {
2884 HGLOBAL hsrc;
2885 LPWSTR src;
2886
2887 /* Protect read-only edit control from modification */
2888 if(es->style & ES_READONLY)
2889 return;
2890
2891 OpenClipboard(es->hwndSelf);
2892 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2893 src = GlobalLock(hsrc);
2894 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
2895 GlobalUnlock(hsrc);
2896 }
2897 else if (es->style & ES_PASSWORD) {
2898 /* clear selected text in password edit box even with empty clipboard */
2899 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2900 }
2901 CloseClipboard();
2902 }
2903
2904
2905 /*********************************************************************
2906 *
2907 * WM_COPY
2908 *
2909 */
2910 static void EDIT_WM_Copy(EDITSTATE *es)
2911 {
2912 INT s = min(es->selection_start, es->selection_end);
2913 INT e = max(es->selection_start, es->selection_end);
2914 HGLOBAL hdst;
2915 LPWSTR dst;
2916 DWORD len;
2917
2918 if (e == s) return;
2919
2920 len = e - s;
2921 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2922 dst = GlobalLock(hdst);
2923 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2924 dst[len] = 0; /* ensure 0 termination */
2925 TRACE("%s\n", debugstr_w(dst));
2926 GlobalUnlock(hdst);
2927 OpenClipboard(es->hwndSelf);
2928 EmptyClipboard();
2929 SetClipboardData(CF_UNICODETEXT, hdst);
2930 CloseClipboard();
2931 }
2932
2933
2934 /*********************************************************************
2935 *
2936 * WM_CLEAR
2937 *
2938 */
2939 static inline void EDIT_WM_Clear(EDITSTATE *es)
2940 {
2941 /* Protect read-only edit control from modification */
2942 if(es->style & ES_READONLY)
2943 return;
2944
2945 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2946 }
2947
2948
2949 /*********************************************************************
2950 *
2951 * WM_CUT
2952 *
2953 */
2954 static inline void EDIT_WM_Cut(EDITSTATE *es)
2955 {
2956 EDIT_WM_Copy(es);
2957 EDIT_WM_Clear(es);
2958 }
2959
2960
2961 /*********************************************************************
2962 *
2963 * WM_CHAR
2964 *
2965 */
2966 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2967 {
2968 BOOL control;
2969
2970 control = GetKeyState(VK_CONTROL) & 0x8000;
2971
2972 switch (c) {
2973 case '\r':
2974 /* If it's not a multiline edit box, it would be ignored below.
2975 * For multiline edit without ES_WANTRETURN, we have to make a
2976 * special case.
2977 */
2978 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2979 if (EDIT_IsInsideDialog(es))
2980 break;
2981 case '\n':
2982 if (es->style & ES_MULTILINE) {
2983 if (es->style & ES_READONLY) {
2984 EDIT_MoveHome(es, FALSE, FALSE);
2985 EDIT_MoveDown_ML(es, FALSE);
2986 } else {
2987 static const WCHAR cr_lfW[] = {'\r','\n',0};
2988 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
2989 }
2990 }
2991 break;
2992 case '\t':
2993 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2994 {
2995 static const WCHAR tabW[] = {'\t',0};
2996 if (EDIT_IsInsideDialog(es))
2997 break;
2998 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
2999 }
3000 break;
3001 case VK_BACK:
3002 if (!(es->style & ES_READONLY) && !control) {
3003 if (es->selection_start != es->selection_end)
3004 EDIT_WM_Clear(es);
3005 else {
3006 /* delete character left of caret */
3007 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3008 EDIT_MoveBackward(es, TRUE);
3009 EDIT_WM_Clear(es);
3010 }
3011 }
3012 break;
3013 case 0x03: /* ^C */
3014 if (!(es->style & ES_PASSWORD))
3015 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3016 break;
3017 case 0x16: /* ^V */
3018 if (!(es->style & ES_READONLY))
3019 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3020 break;
3021 case 0x18: /* ^X */
3022 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3023 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3024 break;
3025 case 0x1A: /* ^Z */
3026 if (!(es->style & ES_READONLY))
3027 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3028 break;
3029
3030 default:
3031 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3032 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3033 break;
3034
3035 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3036 WCHAR str[2];
3037 str[0] = c;
3038 str[1] = '\0';
3039 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3040 }
3041 break;
3042 }
3043 return 1;
3044 }
3045
3046 #if 0 // Removed see Revision 43925 comments.
3047 /*********************************************************************
3048 *
3049 * WM_COMMAND
3050 *
3051 */
3052 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3053 {
3054 if (code || control)
3055 return;
3056
3057 switch (id) {
3058 case EM_UNDO:
3059 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3060 break;
3061 case WM_CUT:
3062 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3063 break;
3064 case WM_COPY:
3065 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3066 break;
3067 case WM_PASTE:
3068 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3069 break;
3070 case WM_CLEAR:
3071 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3072 break;
3073 case EM_SETSEL:
3074 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3075 EDIT_EM_ScrollCaret(es);
3076 break;
3077 default:
3078 ERR("unknown menu item, please report\n");
3079 break;
3080 }
3081 }
3082 #endif
3083
3084
3085 /*********************************************************************
3086 *
3087 * WM_CONTEXTMENU
3088 *
3089 * Note: the resource files resource/sysres_??.rc cannot define a
3090 * single popup menu. Hence we use a (dummy) menubar
3091 * containing the single popup menu as its first item.
3092 *
3093 * FIXME: the message identifiers have been chosen arbitrarily,
3094 * hence we use MF_BYPOSITION.
3095 * We might as well use the "real" values (anybody knows ?)
3096 * The menu definition is in resources/sysres_??.rc.
3097 * Once these are OK, we better use MF_BYCOMMAND here
3098 * (as we do in EDIT_WM_Command()).
3099 *
3100 */
3101 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3102 {
3103 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3104 HMENU popup = GetSubMenu(menu, 0);
3105 UINT start = es->selection_start;
3106 UINT end = es->selection_end;
3107
3108 BOOL selectedItem;
3109 ORDER_UINT(start, end);
3110
3111 /* undo */
3112 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3113 /* cut */
3114 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3115 /* copy */
3116 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3117 /* paste */
3118 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3119 /* delete */
3120 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3121 /* select all */
3122 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3123
3124 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3125 {
3126 RECT rc;
3127 /* Windows places the menu at the edit's center in this case */
3128 GetClientRect(es->hwndSelf, &rc);
3129 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3130 x = rc.left + (rc.right - rc.left) / 2;
3131 y = rc.top + (rc.bottom - rc.top) / 2;
3132 }
3133
3134 if (!(es->flags & EF_FOCUSED))
3135 SetFocus(es->hwndSelf);
3136
3137 #ifdef __REACTOS__
3138 selectedItem = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, es->hwndSelf, NULL);
3139 // Added see Revision 43925 comments.
3140 switch (selectedItem) {
3141 case EM_UNDO:
3142 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3143 break;
3144 case WM_CUT:
3145 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3146 break;
3147 case WM_COPY:
3148 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3149 break;
3150 case WM_PASTE:
3151 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3152 break;
3153 case WM_CLEAR:
3154 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3155 break;
3156 case EM_SETSEL:
3157 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3158 EDIT_EM_ScrollCaret(es);
3159 break;
3160 default:
3161 ERR("unknown menu item, please report\n");
3162 break;
3163 }
3164 #endif
3165 DestroyMenu(menu);
3166 }
3167
3168
3169 /*********************************************************************
3170 *
3171 * WM_GETTEXT
3172 *
3173 */
3174 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3175 {
3176 if(!count) return 0;
3177
3178 if(unicode)
3179 {
3180 lstrcpynW(dst, es->text, count);
3181 return strlenW(dst);
3182 }
3183 else
3184 {
3185 LPSTR textA = (LPSTR)dst;
3186 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3187 textA[count - 1] = 0; /* ensure 0 termination */
3188 return strlen(textA);
3189 }
3190 }
3191
3192 /*********************************************************************
3193 *
3194 * EDIT_CheckCombo
3195 *
3196 */
3197 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3198 {
3199 HWND hLBox = es->hwndListBox;
3200 HWND hCombo;
3201 BOOL bDropped;
3202 int nEUI;
3203
3204 if (!hLBox)
3205 return FALSE;
3206
3207 hCombo = GetParent(es->hwndSelf);
3208 bDropped = TRUE;
3209 nEUI = 0;
3210
3211 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3212
3213 if (key == VK_UP || key == VK_DOWN)
3214 {
3215 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3216 nEUI = 1;
3217
3218 if (msg == WM_KEYDOWN || nEUI)
3219 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3220 }
3221
3222 switch (msg)
3223 {
3224 case WM_KEYDOWN:
3225 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3226 {
3227 /* make sure ComboLBox pops up */
3228 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3229 key = VK_F4;
3230 nEUI = 2;
3231 }
3232
3233 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3234 break;
3235
3236 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3237 if (nEUI)
3238 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3239 else
3240 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3241 break;
3242 }
3243
3244 if(nEUI == 2)
3245 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3246
3247 return TRUE;
3248 }
3249
3250
3251 /*********************************************************************
3252 *
3253 * WM_KEYDOWN
3254 *
3255 * Handling of special keys that don't produce a WM_CHAR
3256 * (i.e. non-printable keys) & Backspace & Delete
3257 *
3258 */
3259 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3260 {
3261 BOOL shift;
3262 BOOL control;
3263
3264 if (GetKeyState(VK_MENU) & 0x8000)
3265 return 0;
3266
3267 shift = GetKeyState(VK_SHIFT) & 0x8000;
3268 control = GetKeyState(VK_CONTROL) & 0x8000;
3269
3270 switch (key) {
3271 case VK_F4:
3272 case VK_UP:
3273 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3274 break;
3275
3276 /* fall through */
3277 case VK_LEFT:
3278 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3279 EDIT_MoveUp_ML(es, shift);
3280 else
3281 if (control)
3282 EDIT_MoveWordBackward(es, shift);
3283 else
3284 EDIT_MoveBackward(es, shift);
3285 break;
3286 case VK_DOWN:
3287 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3288 break;
3289 /* fall through */
3290 case VK_RIGHT:
3291 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3292 EDIT_MoveDown_ML(es, shift);
3293 else if (control)
3294 EDIT_MoveWordForward(es, shift);
3295 else
3296 EDIT_MoveForward(es, shift);
3297 break;
3298 case VK_HOME:
3299 EDIT_MoveHome(es, shift, control);
3300 break;
3301 case VK_END:
3302 EDIT_MoveEnd(es, shift, control);
3303 break;
3304 case VK_PRIOR:
3305 if (es->style & ES_MULTILINE)
3306 EDIT_MovePageUp_ML(es, shift);
3307 else
3308 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3309 break;
3310 case VK_NEXT:
3311 if (es->style & ES_MULTILINE)
3312 EDIT_MovePageDown_ML(es, shift);
3313 else
3314 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3315 break;
3316 case VK_DELETE:
3317 if (!(es->style & ES_READONLY) && !(shift && control)) {
3318 if (es->selection_start != es->selection_end) {
3319 if (shift)
3320 EDIT_WM_Cut(es);
3321 else
3322 EDIT_WM_Clear(es);
3323 } else {
3324 if (shift) {
3325 /* delete character left of caret */
3326 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3327 EDIT_MoveBackward(es, TRUE);
3328 EDIT_WM_Clear(es);
3329 } else if (control) {
3330 /* delete to end of line */
3331 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3332 EDIT_MoveEnd(es, TRUE, FALSE);
3333 EDIT_WM_Clear(es);
3334 } else {
3335 /* delete character right of caret */
3336 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3337 EDIT_MoveForward(es, TRUE);
3338 EDIT_WM_Clear(es);
3339 }
3340 }
3341 }
3342 break;
3343 case VK_INSERT:
3344 if (shift) {
3345 if (!(es->style & ES_READONLY))
3346 EDIT_WM_Paste(es);
3347 } else if (control)
3348 EDIT_WM_Copy(es);
3349 break;
3350 case VK_RETURN:
3351 /* If the edit doesn't want the return send a message to the default object */
3352 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3353 {
3354 DWORD dw;
3355
3356 if (!EDIT_IsInsideDialog(es)) break;
3357 if (control) break;
3358 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3359 if (HIWORD(dw) == DC_HASDEFID)
3360 {
3361 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3362 if (hwDefCtrl)
3363 {
3364 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3365 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3366 }
3367 }
3368 }
3369 break;
3370 case VK_ESCAPE:
3371 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3372 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3373 break;
3374 case VK_TAB:
3375 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3376 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3377 break;
3378 }
3379 return TRUE;
3380 }
3381
3382
3383 /*********************************************************************
3384 *
3385 * WM_KILLFOCUS
3386 *
3387 */
3388 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3389 {
3390 es->flags &= ~EF_FOCUSED;
3391 DestroyCaret();
3392 if(!(es->style & ES_NOHIDESEL))
3393 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3394 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3395 return 0;
3396 }
3397
3398
3399 /*********************************************************************
3400 *
3401 * WM_LBUTTONDBLCLK
3402 *
3403 * The caret position has been set on the WM_LBUTTONDOWN message
3404 *
3405 */
3406 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3407 {
3408 INT s;
3409 INT e = es->selection_end;
3410 INT l;
3411 INT li;
3412 INT ll;
3413
3414 es->bCaptureState = TRUE;
3415 SetCapture(es->hwndSelf);
3416
3417 l = EDIT_EM_LineFromChar(es, e);
3418 li = EDIT_EM_LineIndex(es, l);
3419 ll = EDIT_EM_LineLength(es, e);
3420 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3421 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3422 EDIT_EM_SetSel(es, s, e, FALSE);
3423 EDIT_EM_ScrollCaret(es);
3424 es->region_posx = es->region_posy = 0;
3425 SetTimer(es->hwndSelf, 0, 100, NULL);
3426 return 0;
3427 }
3428
3429
3430 /*********************************************************************
3431 *
3432 * WM_LBUTTONDOWN
3433 *
3434 */
3435 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3436 {
3437 INT e;
3438 BOOL after_wrap;
3439
3440 es->bCaptureState = TRUE;
3441 SetCapture(es->hwndSelf);
3442 EDIT_ConfinePoint(es, &x, &y);
3443 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3444 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3445 EDIT_EM_ScrollCaret(es);
3446 es->region_posx = es->region_posy = 0;
3447 SetTimer(es->hwndSelf, 0, 100, NULL);
3448
3449 if (!(es->flags & EF_FOCUSED))
3450 SetFocus(es->hwndSelf);
3451
3452 return 0;
3453 }
3454
3455
3456 /*********************************************************************
3457 *
3458 * WM_LBUTTONUP
3459 *
3460 */
3461 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es