b484c842ea066e87de72210fd253a57c0bb36b32
[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)
430 break;
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, countA, NULL, 0);
1113 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1114 if(countW_new > es->buffer_size + 1)
1115 {
1116 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1117 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1118 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1119 if(hloc32W_new)
1120 {
1121 es->hloc32W = hloc32W_new;
1122 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1123 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1124 }
1125 else
1126 WARN("FAILED! Will synchronize partially\n");
1127 }
1128 }
1129 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1130 es->text = LocalLock(es->hloc32W);
1131 if(textA)
1132 {
1133 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1134 LocalUnlock(es->hloc32A);
1135 }
1136 }
1137 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1138 es->lock_count++;
1139 }
1140
1141
1142 /*********************************************************************
1143 *
1144 * EDIT_UnlockBuffer
1145 *
1146 */
1147 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1148 {
1149
1150 /* Edit window might be already destroyed */
1151 if(!IsWindow(es->hwndSelf))
1152 {
1153 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1154 return;
1155 }
1156
1157 if (!es->lock_count) {
1158 ERR("lock_count == 0 ... please report\n");
1159 return;
1160 }
1161 if (!es->text) {
1162 ERR("es->text == 0 ... please report\n");
1163 return;
1164 }
1165
1166 if (force || (es->lock_count == 1)) {
1167 if (es->hloc32W) {
1168 CHAR *textA = NULL; // ReactOS hacked!
1169 UINT countA = 0;
1170 UINT countW = get_text_length(es) + 1;
1171
1172 if(es->hloc32A)
1173 {
1174 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1175 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1176 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1177 countA = LocalSize(es->hloc32A);
1178 if(countA_new > countA)
1179 {
1180 HLOCAL hloc32A_new;
1181 UINT alloc_size = ROUND_TO_GROW(countA_new);
1182 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1183 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1184 if(hloc32A_new)
1185 {
1186 es->hloc32A = hloc32A_new;
1187 countA = LocalSize(hloc32A_new);
1188 TRACE("Real new size %d bytes\n", countA);
1189 }
1190 else
1191 WARN("FAILED! Will synchronize partially\n");
1192 }
1193 textA = LocalLock(es->hloc32A);
1194 }
1195
1196 if(textA)
1197 {
1198 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
1199 LocalUnlock(es->hloc32A);
1200 }
1201
1202 LocalUnlock(es->hloc32W);
1203 es->text = NULL;
1204 }
1205 else {
1206 ERR("no buffer ... please report\n");
1207 return;
1208 }
1209 }
1210 es->lock_count--;
1211 }
1212
1213
1214 /*********************************************************************
1215 *
1216 * EDIT_MakeFit
1217 *
1218 * Try to fit size + 1 characters in the buffer.
1219 */
1220 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1221 {
1222 HLOCAL hNew32W;
1223
1224 if (size <= es->buffer_size)
1225 return TRUE;
1226
1227 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1228
1229 /* Force edit to unlock it's buffer. es->text now NULL */
1230 EDIT_UnlockBuffer(es, TRUE);
1231
1232 if (es->hloc32W) {
1233 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1234 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1235 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1236 es->hloc32W = hNew32W;
1237 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1238 }
1239 }
1240
1241 EDIT_LockBuffer(es);
1242
1243 if (es->buffer_size < size) {
1244 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1245 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1246 return FALSE;
1247 } else {
1248 TRACE("We now have %d+1\n", es->buffer_size);
1249 return TRUE;
1250 }
1251 }
1252
1253
1254 /*********************************************************************
1255 *
1256 * EDIT_MakeUndoFit
1257 *
1258 * Try to fit size + 1 bytes in the undo buffer.
1259 *
1260 */
1261 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1262 {
1263 UINT alloc_size;
1264
1265 if (size <= es->undo_buffer_size)
1266 return TRUE;
1267
1268 TRACE("trying to ReAlloc to %d+1\n", size);
1269
1270 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1271 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1272 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1273 return TRUE;
1274 }
1275 else
1276 {
1277 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1278 return FALSE;
1279 }
1280 }
1281
1282
1283 /*********************************************************************
1284 *
1285 * EDIT_UpdateTextRegion
1286 *
1287 */
1288 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1289 {
1290 if (es->flags & EF_UPDATE) {
1291 es->flags &= ~EF_UPDATE;
1292 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1293 }
1294 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1295 }
1296
1297
1298 /*********************************************************************
1299 *
1300 * EDIT_UpdateText
1301 *
1302 */
1303 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1304 {
1305 if (es->flags & EF_UPDATE) {
1306 es->flags &= ~EF_UPDATE;
1307 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1308 }
1309 InvalidateRect(es->hwndSelf, rc, bErase);
1310 }
1311
1312 /*********************************************************************
1313 *
1314 * EDIT_SL_InvalidateText
1315 *
1316 * Called from EDIT_InvalidateText().
1317 * Does the job for single-line controls only.
1318 *
1319 */
1320 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1321 {
1322 RECT line_rect;
1323 RECT rc;
1324
1325 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1326 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1327 EDIT_UpdateText(es, &rc, TRUE);
1328 }
1329
1330
1331 static inline INT get_vertical_line_count(EDITSTATE *es)
1332 {
1333 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1334 return max(1,vlc);
1335 }
1336
1337 /*********************************************************************
1338 *
1339 * EDIT_ML_InvalidateText
1340 *
1341 * Called from EDIT_InvalidateText().
1342 * Does the job for multi-line controls only.
1343 *
1344 */
1345 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1346 {
1347 INT vlc = get_vertical_line_count(es);
1348 INT sl = EDIT_EM_LineFromChar(es, start);
1349 INT el = EDIT_EM_LineFromChar(es, end);
1350 INT sc;
1351 INT ec;
1352 RECT rc1;
1353 RECT rcWnd;
1354 RECT rcLine;
1355 RECT rcUpdate;
1356 INT l;
1357
1358 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1359 return;
1360
1361 sc = start - EDIT_EM_LineIndex(es, sl);
1362 ec = end - EDIT_EM_LineIndex(es, el);
1363 if (sl < es->y_offset) {
1364 sl = es->y_offset;
1365 sc = 0;
1366 }
1367 if (el > es->y_offset + vlc) {
1368 el = es->y_offset + vlc;
1369 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1370 }
1371 GetClientRect(es->hwndSelf, &rc1);
1372 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1373 if (sl == el) {
1374 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1375 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1376 EDIT_UpdateText(es, &rcUpdate, TRUE);
1377 } else {
1378 EDIT_GetLineRect(es, sl, sc,
1379 EDIT_EM_LineLength(es,
1380 EDIT_EM_LineIndex(es, sl)),
1381 &rcLine);
1382 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1383 EDIT_UpdateText(es, &rcUpdate, TRUE);
1384 for (l = sl + 1 ; l < el ; l++) {
1385 EDIT_GetLineRect(es, l, 0,
1386 EDIT_EM_LineLength(es,
1387 EDIT_EM_LineIndex(es, l)),
1388 &rcLine);
1389 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1390 EDIT_UpdateText(es, &rcUpdate, TRUE);
1391 }
1392 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1393 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1394 EDIT_UpdateText(es, &rcUpdate, TRUE);
1395 }
1396 }
1397
1398
1399 /*********************************************************************
1400 *
1401 * EDIT_InvalidateText
1402 *
1403 * Invalidate the text from offset start up to, but not including,
1404 * offset end. Useful for (re)painting the selection.
1405 * Regions outside the linewidth are not invalidated.
1406 * end == -1 means end == TextLength.
1407 * start and end need not be ordered.
1408 *
1409 */
1410 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1411 {
1412 if (end == start)
1413 return;
1414
1415 if (end == -1)
1416 end = get_text_length(es);
1417
1418 if (end < start) {
1419 INT tmp = start;
1420 start = end;
1421 end = tmp;
1422 }
1423
1424 if (es->style & ES_MULTILINE)
1425 EDIT_ML_InvalidateText(es, start, end);
1426 else
1427 EDIT_SL_InvalidateText(es, start, end);
1428 }
1429
1430
1431 /*********************************************************************
1432 *
1433 * EDIT_EM_SetSel
1434 *
1435 * note: unlike the specs say: the order of start and end
1436 * _is_ preserved in Windows. (i.e. start can be > end)
1437 * In other words: this handler is OK
1438 *
1439 */
1440 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1441 {
1442 UINT old_start = es->selection_start;
1443 UINT old_end = es->selection_end;
1444 UINT len = get_text_length(es);
1445
1446 if (start == (UINT)-1) {
1447 start = es->selection_end;
1448 end = es->selection_end;
1449 } else {
1450 start = min(start, len);
1451 end = min(end, len);
1452 }
1453 es->selection_start = start;
1454 es->selection_end = end;
1455 if (after_wrap)
1456 es->flags |= EF_AFTER_WRAP;
1457 else
1458 es->flags &= ~EF_AFTER_WRAP;
1459 /* Compute the necessary invalidation region. */
1460 /* Note that we don't need to invalidate regions which have
1461 * "never" been selected, or those which are "still" selected.
1462 * In fact, every time we hit a selection boundary, we can
1463 * *toggle* whether we need to invalidate. Thus we can optimize by
1464 * *sorting* the interval endpoints. Let's assume that we sort them
1465 * in this order:
1466 * start <= end <= old_start <= old_end
1467 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1468 * in 5 comparisons; i.e. it is impossible to do better than the
1469 * following: */
1470 ORDER_UINT(end, old_end);
1471 ORDER_UINT(start, old_start);
1472 ORDER_UINT(old_start, old_end);
1473 ORDER_UINT(start, end);
1474 /* Note that at this point 'end' and 'old_start' are not in order, but
1475 * start is definitely the min. and old_end is definitely the max. */
1476 if (end != old_start)
1477 {
1478 /*
1479 * One can also do
1480 * ORDER_UINT32(end, old_start);
1481 * EDIT_InvalidateText(es, start, end);
1482 * EDIT_InvalidateText(es, old_start, old_end);
1483 * in place of the following if statement.
1484 * (That would complete the optimal five-comparison four-element sort.)
1485 */
1486 if (old_start > end )
1487 {
1488 EDIT_InvalidateText(es, start, end);
1489 EDIT_InvalidateText(es, old_start, old_end);
1490 }
1491 else
1492 {
1493 EDIT_InvalidateText(es, start, old_start);
1494 EDIT_InvalidateText(es, end, old_end);
1495 }
1496 }
1497 else EDIT_InvalidateText(es, start, old_end);
1498 }
1499
1500
1501 /*********************************************************************
1502 *
1503 * EDIT_UpdateScrollInfo
1504 *
1505 */
1506 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1507 {
1508 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1509 {
1510 SCROLLINFO si;
1511 si.cbSize = sizeof(SCROLLINFO);
1512 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1513 si.nMin = 0;
1514 si.nMax = es->line_count - 1;
1515 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1516 si.nPos = es->y_offset;
1517 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1518 si.nMin, si.nMax, si.nPage, si.nPos);
1519 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1520 }
1521
1522 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1523 {
1524 SCROLLINFO si;
1525 si.cbSize = sizeof(SCROLLINFO);
1526 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1527 si.nMin = 0;
1528 si.nMax = es->text_width - 1;
1529 si.nPage = es->format_rect.right - es->format_rect.left;
1530 si.nPos = es->x_offset;
1531 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1532 si.nMin, si.nMax, si.nPage, si.nPos);
1533 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1534 }
1535 }
1536
1537
1538 /*********************************************************************
1539 *
1540 * EDIT_EM_LineScroll_internal
1541 *
1542 * Version of EDIT_EM_LineScroll for internal use.
1543 * It doesn't refuse if ES_MULTILINE is set and assumes that
1544 * dx is in pixels, dy - in lines.
1545 *
1546 */
1547 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1548 {
1549 INT nyoff;
1550 INT x_offset_in_pixels;
1551 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1552 es->line_height;
1553
1554 if (es->style & ES_MULTILINE)
1555 {
1556 x_offset_in_pixels = es->x_offset;
1557 }
1558 else
1559 {
1560 dy = 0;
1561 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1562 }
1563
1564 if (-dx > x_offset_in_pixels)
1565 dx = -x_offset_in_pixels;
1566 if (dx > es->text_width - x_offset_in_pixels)
1567 dx = es->text_width - x_offset_in_pixels;
1568 nyoff = max(0, es->y_offset + dy);
1569 if (nyoff >= es->line_count - lines_per_page)
1570 nyoff = max(0, es->line_count - lines_per_page);
1571 dy = (es->y_offset - nyoff) * es->line_height;
1572 if (dx || dy) {
1573 RECT rc1;
1574 RECT rc;
1575
1576 es->y_offset = nyoff;
1577 if(es->style & ES_MULTILINE)
1578 es->x_offset += dx;
1579 else
1580 es->x_offset += dx / es->char_width;
1581
1582 GetClientRect(es->hwndSelf, &rc1);
1583 IntersectRect(&rc, &rc1, &es->format_rect);
1584 ScrollWindowEx(es->hwndSelf, -dx, dy,
1585 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1586 /* force scroll info update */
1587 EDIT_UpdateScrollInfo(es);
1588 }
1589 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1590 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1591 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1592 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1593 return TRUE;
1594 }
1595
1596 /*********************************************************************
1597 *
1598 * EM_LINESCROLL
1599 *
1600 * NOTE: dx is in average character widths, dy - in lines;
1601 *
1602 */
1603 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1604 {
1605 if (!(es->style & ES_MULTILINE))
1606 return FALSE;
1607
1608 dx *= es->char_width;
1609 return EDIT_EM_LineScroll_internal(es, dx, dy);
1610 }
1611
1612
1613 /*********************************************************************
1614 *
1615 * EM_SCROLL
1616 *
1617 */
1618 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1619 {
1620 INT dy;
1621
1622 if (!(es->style & ES_MULTILINE))
1623 return (LRESULT)FALSE;
1624
1625 dy = 0;
1626
1627 switch (action) {
1628 case SB_LINEUP:
1629 if (es->y_offset)
1630 dy = -1;
1631 break;
1632 case SB_LINEDOWN:
1633 if (es->y_offset < es->line_count - 1)
1634 dy = 1;
1635 break;
1636 case SB_PAGEUP:
1637 if (es->y_offset)
1638 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1639 break;
1640 case SB_PAGEDOWN:
1641 if (es->y_offset < es->line_count - 1)
1642 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1643 break;
1644 default:
1645 return (LRESULT)FALSE;
1646 }
1647 if (dy) {
1648 INT vlc = get_vertical_line_count(es);
1649 /* check if we are going to move too far */
1650 if(es->y_offset + dy > es->line_count - vlc)
1651 dy = max(es->line_count - vlc, 0) - es->y_offset;
1652
1653 /* Notification is done in EDIT_EM_LineScroll */
1654 if(dy) {
1655 EDIT_EM_LineScroll(es, 0, dy);
1656 return MAKELONG((SHORT)dy, (BOOL)TRUE);
1657 }
1658
1659 }
1660 return (LRESULT)FALSE;
1661 }
1662
1663
1664 /*********************************************************************
1665 *
1666 * EDIT_SetCaretPos
1667 *
1668 */
1669 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1670 BOOL after_wrap)
1671 {
1672 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1673 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1674 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1675 }
1676
1677
1678 /*********************************************************************
1679 *
1680 * EM_SCROLLCARET
1681 *
1682 */
1683 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1684 {
1685 if (es->style & ES_MULTILINE) {
1686 INT l;
1687 INT vlc;
1688 INT ww;
1689 INT cw = es->char_width;
1690 INT x;
1691 INT dy = 0;
1692 INT dx = 0;
1693
1694 l = EDIT_EM_LineFromChar(es, es->selection_end);
1695 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1696 vlc = get_vertical_line_count(es);
1697 if (l >= es->y_offset + vlc)
1698 dy = l - vlc + 1 - es->y_offset;
1699 if (l < es->y_offset)
1700 dy = l - es->y_offset;
1701 ww = es->format_rect.right - es->format_rect.left;
1702 if (x < es->format_rect.left)
1703 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1704 if (x > es->format_rect.right)
1705 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1706 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1707 {
1708 /* check if we are going to move too far */
1709 if(es->x_offset + dx + ww > es->text_width)
1710 dx = es->text_width - ww - es->x_offset;
1711 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1712 EDIT_EM_LineScroll_internal(es, dx, dy);
1713 }
1714 } else {
1715 INT x;
1716 INT goal;
1717 INT format_width;
1718
1719 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1720 format_width = es->format_rect.right - es->format_rect.left;
1721 if (x < es->format_rect.left) {
1722 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1723 do {
1724 es->x_offset--;
1725 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1726 } while ((x < goal) && es->x_offset);
1727 /* FIXME: use ScrollWindow() somehow to improve performance */
1728 EDIT_UpdateText(es, NULL, TRUE);
1729 } else if (x > es->format_rect.right) {
1730 INT x_last;
1731 INT len = get_text_length(es);
1732 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1733 do {
1734 es->x_offset++;
1735 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1736 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1737 } while ((x > goal) && (x_last > es->format_rect.right));
1738 /* FIXME: use ScrollWindow() somehow to improve performance */
1739 EDIT_UpdateText(es, NULL, TRUE);
1740 }
1741 }
1742
1743 if(es->flags & EF_FOCUSED)
1744 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1745 }
1746
1747
1748 /*********************************************************************
1749 *
1750 * EDIT_MoveBackward
1751 *
1752 */
1753 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1754 {
1755 INT e = es->selection_end;
1756
1757 if (e) {
1758 e--;
1759 if ((es->style & ES_MULTILINE) && e &&
1760 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1761 e--;
1762 if (e && (es->text[e - 1] == '\r'))
1763 e--;
1764 }
1765 }
1766 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1767 EDIT_EM_ScrollCaret(es);
1768 }
1769
1770
1771 /*********************************************************************
1772 *
1773 * EDIT_MoveDown_ML
1774 *
1775 * Only for multi line controls
1776 * Move the caret one line down, on a column with the nearest
1777 * x coordinate on the screen (might be a different column).
1778 *
1779 */
1780 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1781 {
1782 INT s = es->selection_start;
1783 INT e = es->selection_end;
1784 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1785 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1786 INT x = (short)LOWORD(pos);
1787 INT y = (short)HIWORD(pos);
1788
1789 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1790 if (!extend)
1791 s = e;
1792 EDIT_EM_SetSel(es, s, e, after_wrap);
1793 EDIT_EM_ScrollCaret(es);
1794 }
1795
1796
1797 /*********************************************************************
1798 *
1799 * EDIT_MoveEnd
1800 *
1801 */
1802 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1803 {
1804 BOOL after_wrap = FALSE;
1805 INT e;
1806
1807 /* Pass a high value in x to make sure of receiving the end of the line */
1808 if (!ctrl && (es->style & ES_MULTILINE))
1809 e = EDIT_CharFromPos(es, 0x3fffffff,
1810 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1811 else
1812 e = get_text_length(es);
1813 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1814 EDIT_EM_ScrollCaret(es);
1815 }
1816
1817
1818 /*********************************************************************
1819 *
1820 * EDIT_MoveForward
1821 *
1822 */
1823 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1824 {
1825 INT e = es->selection_end;
1826
1827 if (es->text[e]) {
1828 e++;
1829 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1830 if (es->text[e] == '\n')
1831 e++;
1832 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1833 e += 2;
1834 }
1835 }
1836 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1837 EDIT_EM_ScrollCaret(es);
1838 }
1839
1840
1841 /*********************************************************************
1842 *
1843 * EDIT_MoveHome
1844 *
1845 * Home key: move to beginning of line.
1846 *
1847 */
1848 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1849 {
1850 INT e;
1851
1852 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1853 if (!ctrl && (es->style & ES_MULTILINE))
1854 e = EDIT_CharFromPos(es, -es->x_offset,
1855 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1856 else
1857 e = 0;
1858 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1859 EDIT_EM_ScrollCaret(es);
1860 }
1861
1862
1863 /*********************************************************************
1864 *
1865 * EDIT_MovePageDown_ML
1866 *
1867 * Only for multi line controls
1868 * Move the caret one page down, on a column with the nearest
1869 * x coordinate on the screen (might be a different column).
1870 *
1871 */
1872 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1873 {
1874 INT s = es->selection_start;
1875 INT e = es->selection_end;
1876 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1877 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1878 INT x = (short)LOWORD(pos);
1879 INT y = (short)HIWORD(pos);
1880
1881 e = EDIT_CharFromPos(es, x,
1882 y + (es->format_rect.bottom - es->format_rect.top),
1883 &after_wrap);
1884 if (!extend)
1885 s = e;
1886 EDIT_EM_SetSel(es, s, e, after_wrap);
1887 EDIT_EM_ScrollCaret(es);
1888 }
1889
1890
1891 /*********************************************************************
1892 *
1893 * EDIT_MovePageUp_ML
1894 *
1895 * Only for multi line controls
1896 * Move the caret one page up, on a column with the nearest
1897 * x coordinate on the screen (might be a different column).
1898 *
1899 */
1900 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1901 {
1902 INT s = es->selection_start;
1903 INT e = es->selection_end;
1904 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1905 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1906 INT x = (short)LOWORD(pos);
1907 INT y = (short)HIWORD(pos);
1908
1909 e = EDIT_CharFromPos(es, x,
1910 y - (es->format_rect.bottom - es->format_rect.top),
1911 &after_wrap);
1912 if (!extend)
1913 s = e;
1914 EDIT_EM_SetSel(es, s, e, after_wrap);
1915 EDIT_EM_ScrollCaret(es);
1916 }
1917
1918
1919 /*********************************************************************
1920 *
1921 * EDIT_MoveUp_ML
1922 *
1923 * Only for multi line controls
1924 * Move the caret one line up, on a column with the nearest
1925 * x coordinate on the screen (might be a different column).
1926 *
1927 */
1928 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1929 {
1930 INT s = es->selection_start;
1931 INT e = es->selection_end;
1932 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1933 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1934 INT x = (short)LOWORD(pos);
1935 INT y = (short)HIWORD(pos);
1936
1937 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1938 if (!extend)
1939 s = e;
1940 EDIT_EM_SetSel(es, s, e, after_wrap);
1941 EDIT_EM_ScrollCaret(es);
1942 }
1943
1944
1945 /*********************************************************************
1946 *
1947 * EDIT_MoveWordBackward
1948 *
1949 */
1950 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1951 {
1952 INT s = es->selection_start;
1953 INT e = es->selection_end;
1954 INT l;
1955 INT ll;
1956 INT li;
1957
1958 l = EDIT_EM_LineFromChar(es, e);
1959 ll = EDIT_EM_LineLength(es, e);
1960 li = EDIT_EM_LineIndex(es, l);
1961 if (e - li == 0) {
1962 if (l) {
1963 li = EDIT_EM_LineIndex(es, l - 1);
1964 e = li + EDIT_EM_LineLength(es, li);
1965 }
1966 } else {
1967 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
1968 }
1969 if (!extend)
1970 s = e;
1971 EDIT_EM_SetSel(es, s, e, FALSE);
1972 EDIT_EM_ScrollCaret(es);
1973 }
1974
1975
1976 /*********************************************************************
1977 *
1978 * EDIT_MoveWordForward
1979 *
1980 */
1981 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1982 {
1983 INT s = es->selection_start;
1984 INT e = es->selection_end;
1985 INT l;
1986 INT ll;
1987 INT li;
1988
1989 l = EDIT_EM_LineFromChar(es, e);
1990 ll = EDIT_EM_LineLength(es, e);
1991 li = EDIT_EM_LineIndex(es, l);
1992 if (e - li == ll) {
1993 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1994 e = EDIT_EM_LineIndex(es, l + 1);
1995 } else {
1996 e = li + EDIT_CallWordBreakProc(es,
1997 li, e - li + 1, ll, WB_RIGHT);
1998 }
1999 if (!extend)
2000 s = e;
2001 EDIT_EM_SetSel(es, s, e, FALSE);
2002 EDIT_EM_ScrollCaret(es);
2003 }
2004
2005
2006 /*********************************************************************
2007 *
2008 * EDIT_PaintText
2009 *
2010 */
2011 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2012 {
2013 COLORREF BkColor;
2014 COLORREF TextColor;
2015 LOGFONTW underline_font;
2016 HFONT hUnderline = 0;
2017 HFONT old_font = 0;
2018 INT ret;
2019 INT li;
2020 INT BkMode;
2021 SIZE size;
2022
2023 if (!count)
2024 return 0;
2025 BkMode = GetBkMode(dc);
2026 BkColor = GetBkColor(dc);
2027 TextColor = GetTextColor(dc);
2028 if (rev) {
2029 if (es->composition_len == 0)
2030 {
2031 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2032 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2033 SetBkMode( dc, OPAQUE);
2034 }
2035 else
2036 {
2037 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2038 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2039 underline_font.lfUnderline = TRUE;
2040 hUnderline = CreateFontIndirectW(&underline_font);
2041 old_font = SelectObject(dc,hUnderline);
2042 }
2043 }
2044 li = EDIT_EM_LineIndex(es, line);
2045 if (es->style & ES_MULTILINE) {
2046 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2047 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2048 } else {
2049 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2050 TextOutW(dc, x, y, text + li + col, count);
2051 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2052 ret = size.cx;
2053 if (es->style & ES_PASSWORD)
2054 HeapFree(GetProcessHeap(), 0, text);
2055 }
2056 if (rev) {
2057 if (es->composition_len == 0)
2058 {
2059 SetBkColor(dc, BkColor);
2060 SetTextColor(dc, TextColor);
2061 SetBkMode( dc, BkMode);
2062 }
2063 else
2064 {
2065 if (old_font)
2066 SelectObject(dc,old_font);
2067 if (hUnderline)
2068 DeleteObject(hUnderline);
2069 }
2070 }
2071 return ret;
2072 }
2073
2074
2075 /*********************************************************************
2076 *
2077 * EDIT_PaintLine
2078 *
2079 */
2080 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2081 {
2082 INT s = es->selection_start;
2083 INT e = es->selection_end;
2084 INT li;
2085 INT ll;
2086 INT x;
2087 INT y;
2088 LRESULT pos;
2089
2090 if (es->style & ES_MULTILINE) {
2091 INT vlc = get_vertical_line_count(es);
2092
2093 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2094 return;
2095 } else if (line)
2096 return;
2097
2098 TRACE("line=%d\n", line);
2099
2100 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2101 x = (short)LOWORD(pos);
2102 y = (short)HIWORD(pos);
2103 li = EDIT_EM_LineIndex(es, line);
2104 ll = EDIT_EM_LineLength(es, li);
2105 s = min(es->selection_start, es->selection_end);
2106 e = max(es->selection_start, es->selection_end);
2107 s = min(li + ll, max(li, s));
2108 e = min(li + ll, max(li, e));
2109 if (rev && (s != e) &&
2110 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2111 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2112 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2113 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2114 } else
2115 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2116 }
2117
2118
2119 /*********************************************************************
2120 *
2121 * EDIT_AdjustFormatRect
2122 *
2123 * Adjusts the format rectangle for the current font and the
2124 * current client rectangle.
2125 *
2126 */
2127 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2128 {
2129 RECT ClientRect;
2130
2131 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2132 if (es->style & ES_MULTILINE)
2133 {
2134 INT fw, vlc, max_x_offset, max_y_offset;
2135
2136 vlc = get_vertical_line_count(es);
2137 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2138
2139 /* correct es->x_offset */
2140 fw = es->format_rect.right - es->format_rect.left;
2141 max_x_offset = es->text_width - fw;
2142 if(max_x_offset < 0) max_x_offset = 0;
2143 if(es->x_offset > max_x_offset)
2144 es->x_offset = max_x_offset;
2145
2146 /* correct es->y_offset */
2147 max_y_offset = es->line_count - vlc;
2148 if(max_y_offset < 0) max_y_offset = 0;
2149 if(es->y_offset > max_y_offset)
2150 es->y_offset = max_y_offset;
2151
2152 /* force scroll info update */
2153 EDIT_UpdateScrollInfo(es);
2154 }
2155 else
2156 /* Windows doesn't care to fix text placement for SL controls */
2157 es->format_rect.bottom = es->format_rect.top + es->line_height;
2158
2159 /* Always stay within the client area */
2160 GetClientRect(es->hwndSelf, &ClientRect);
2161 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2162
2163 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2164 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2165
2166 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2167 }
2168
2169
2170 /*********************************************************************
2171 *
2172 * EDIT_SetRectNP
2173 *
2174 * note: this is not (exactly) the handler called on EM_SETRECTNP
2175 * it is also used to set the rect of a single line control
2176 *
2177 */
2178 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2179 {
2180 LONG_PTR ExStyle;
2181 INT bw, bh;
2182 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2183
2184 CopyRect(&es->format_rect, rc);
2185
2186 if (ExStyle & WS_EX_CLIENTEDGE) {
2187 es->format_rect.left++;
2188 es->format_rect.right--;
2189
2190 if (es->format_rect.bottom - es->format_rect.top
2191 >= es->line_height + 2)
2192 {
2193 es->format_rect.top++;
2194 es->format_rect.bottom--;
2195 }
2196 }
2197 else if (es->style & WS_BORDER) {
2198 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2199 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2200 es->format_rect.left += bw;
2201 es->format_rect.right -= bw;
2202 if (es->format_rect.bottom - es->format_rect.top
2203 >= es->line_height + 2 * bh)
2204 {
2205 es->format_rect.top += bh;
2206 es->format_rect.bottom -= bh;
2207 }
2208 }
2209
2210 es->format_rect.left += es->left_margin;
2211 es->format_rect.right -= es->right_margin;
2212 EDIT_AdjustFormatRect(es);
2213 }
2214
2215
2216 /*********************************************************************
2217 *
2218 * EM_CHARFROMPOS
2219 *
2220 * returns line number (not index) in high-order word of result.
2221 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2222 * to Richedit, not to the edit control. Original documentation is valid.
2223 * FIXME: do the specs mean to return -1 if outside client area or
2224 * if outside formatting rectangle ???
2225 *
2226 */
2227 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2228 {
2229 POINT pt;
2230 RECT rc;
2231 INT index;
2232
2233 pt.x = x;
2234 pt.y = y;
2235 GetClientRect(es->hwndSelf, &rc);
2236 if (!PtInRect(&rc, pt))
2237 return -1;
2238
2239 index = EDIT_CharFromPos(es, x, y, NULL);
2240 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2241 }
2242
2243
2244 /*********************************************************************
2245 *
2246 * EM_FMTLINES
2247 *
2248 * Enable or disable soft breaks.
2249 *
2250 * This means: insert or remove the soft linebreak character (\r\r\n).
2251 * Take care to check if the text still fits the buffer after insertion.
2252 * If not, notify with EN_ERRSPACE.
2253 *
2254 */
2255 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2256 {
2257 es->flags &= ~EF_USE_SOFTBRK;
2258 if (add_eol) {
2259 es->flags |= EF_USE_SOFTBRK;
2260 FIXME("soft break enabled, not implemented\n");
2261 }
2262 return add_eol;
2263 }
2264
2265
2266 /*********************************************************************
2267 *
2268 * EM_GETHANDLE
2269 *
2270 * Hopefully this won't fire back at us.
2271 * We always start with a fixed buffer in the local heap.
2272 * Despite of the documentation says that the local heap is used
2273 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2274 * buffer on the local heap.
2275 *
2276 */
2277 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2278 {
2279 HLOCAL hLocal;
2280
2281 if (!(es->style & ES_MULTILINE))
2282 return 0;
2283
2284 if(es->is_unicode)
2285 hLocal = es->hloc32W;
2286 else
2287 {
2288 if(!es->hloc32A)
2289 {
2290 CHAR *textA;
2291 UINT countA, alloc_size;
2292 TRACE("Allocating 32-bit ANSI alias buffer\n");
2293 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2294 alloc_size = ROUND_TO_GROW(countA);
2295 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2296 {
2297 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2298 return 0;
2299 }
2300 textA = LocalLock(es->hloc32A);
2301 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2302 LocalUnlock(es->hloc32A);
2303 }
2304 hLocal = es->hloc32A;
2305 }
2306
2307 es->flags |= EF_APP_HAS_HANDLE;
2308 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2309 return hLocal;
2310 }
2311
2312
2313 /*********************************************************************
2314 *
2315 * EM_GETLINE
2316 *
2317 */
2318 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2319 {
2320 LPWSTR src;
2321 INT line_len, dst_len;
2322 INT i;
2323
2324 if (es->style & ES_MULTILINE) {
2325 if (line >= es->line_count)
2326 return 0;
2327 } else
2328 line = 0;
2329 i = EDIT_EM_LineIndex(es, line);
2330 src = es->text + i;
2331 line_len = EDIT_EM_LineLength(es, i);
2332 dst_len = *(WORD *)dst;
2333 if(unicode)
2334 {
2335 if(dst_len <= line_len)
2336 {
2337 memcpy(dst, src, dst_len * sizeof(WCHAR));
2338 return dst_len;
2339 }
2340 else /* Append 0 if enough space */
2341 {
2342 memcpy(dst, src, line_len * sizeof(WCHAR));
2343 dst[line_len] = 0;
2344 return line_len;
2345 }
2346 }
2347 else
2348 {
2349 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2350 if(!ret && line_len) /* Insufficient buffer size */
2351 return dst_len;
2352 if(ret < dst_len) /* Append 0 if enough space */
2353 ((LPSTR)dst)[ret] = 0;
2354 return ret;
2355 }
2356 }
2357
2358
2359 /*********************************************************************
2360 *
2361 * EM_GETSEL
2362 *
2363 */
2364 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2365 {
2366 UINT s = es->selection_start;
2367 UINT e = es->selection_end;
2368
2369 ORDER_UINT(s, e);
2370 if (start)
2371 *start = s;
2372 if (end)
2373 *end = e;
2374 return MAKELONG(s, e);
2375 }
2376
2377
2378 /*********************************************************************
2379 *
2380 * EM_REPLACESEL
2381 *
2382 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2383 *
2384 */
2385 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2386 {
2387 UINT strl = strlenW(lpsz_replace);
2388 UINT tl = get_text_length(es);
2389 UINT utl;
2390 UINT s;
2391 UINT e;
2392 UINT i;
2393 UINT size;
2394 LPWSTR p;
2395 HRGN hrgn = 0;
2396 LPWSTR buf = NULL;
2397 UINT bufl = 0;
2398
2399 TRACE("%s, can_undo %d, send_update %d\n",
2400 debugstr_w(lpsz_replace), can_undo, send_update);
2401
2402 s = es->selection_start;
2403 e = es->selection_end;
2404
2405 if ((s == e) && !strl)
2406 return;
2407
2408 ORDER_UINT(s, e);
2409
2410 size = tl - (e - s) + strl;
2411 if (!size)
2412 es->text_width = 0;
2413
2414 /* Issue the EN_MAXTEXT notification and continue with replacing text
2415 * such that buffer limit is honored. */
2416 if ((honor_limit) && (size > es->buffer_limit)) {
2417 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2418 /* Buffer limit can be smaller than the actual length of text in combobox */
2419 if (es->buffer_limit < (tl - (e-s)))
2420 strl = 0;
2421 else
2422 strl = es->buffer_limit - (tl - (e-s));
2423 }
2424
2425 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2426 return;
2427
2428 if (e != s) {
2429 /* there is something to be deleted */
2430 TRACE("deleting stuff.\n");
2431 bufl = e - s;
2432 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2433 if (!buf) return;
2434 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2435 buf[bufl] = 0; /* ensure 0 termination */
2436 /* now delete */
2437 strcpyW(es->text + s, es->text + e);
2438 text_buffer_changed(es);
2439 }
2440 if (strl) {
2441 /* there is an insertion */
2442 tl = get_text_length(es);
2443 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));
2444 for (p = es->text + tl ; p >= es->text + s ; p--)
2445 p[strl] = p[0];
2446 for (i = 0 , p = es->text + s ; i < strl ; i++)
2447 p[i] = lpsz_replace[i];
2448 if(es->style & ES_UPPERCASE)
2449 CharUpperBuffW(p, strl);
2450 else if(es->style & ES_LOWERCASE)
2451 CharLowerBuffW(p, strl);
2452 text_buffer_changed(es);
2453 }
2454 if (es->style & ES_MULTILINE)
2455 {
2456 INT st = min(es->selection_start, es->selection_end);
2457 INT vlc = get_vertical_line_count(es);
2458
2459 hrgn = CreateRectRgn(0, 0, 0, 0);
2460 EDIT_BuildLineDefs_ML(es, st, st + strl,
2461 strl - abs(es->selection_end - es->selection_start), hrgn);
2462 /* if text is too long undo all changes */
2463 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2464 if (strl)
2465 strcpyW(es->text + e, es->text + e + strl);
2466 if (e != s)
2467 for (i = 0 , p = es->text ; i < e - s ; i++)
2468 p[i + s] = buf[i];
2469 text_buffer_changed(es);
2470 EDIT_BuildLineDefs_ML(es, s, e,
2471 abs(es->selection_end - es->selection_start) - strl, hrgn);
2472 strl = 0;
2473 e = s;
2474 hrgn = CreateRectRgn(0, 0, 0, 0);
2475 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2476 }
2477 }
2478 else {
2479 INT fw = es->format_rect.right - es->format_rect.left;
2480 EDIT_CalcLineWidth_SL(es);
2481 /* remove chars that don't fit */
2482 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2483 while ((es->text_width > fw) && s + strl >= s) {
2484 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2485 strl--;
2486 EDIT_CalcLineWidth_SL(es);
2487 }
2488 text_buffer_changed(es);
2489 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2490 }
2491 }
2492
2493 if (e != s) {
2494 if (can_undo) {
2495 utl = strlenW(es->undo_text);
2496 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2497 /* undo-buffer is extended to the right */
2498 EDIT_MakeUndoFit(es, utl + e - s);
2499 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2500 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2501 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2502 /* undo-buffer is extended to the left */
2503 EDIT_MakeUndoFit(es, utl + e - s);
2504 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2505 p[e - s] = p[0];
2506 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2507 p[i] = buf[i];
2508 es->undo_position = s;
2509 } else {
2510 /* new undo-buffer */
2511 EDIT_MakeUndoFit(es, e - s);
2512 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2513 es->undo_text[e - s] = 0; /* ensure 0 termination */
2514 es->undo_position = s;
2515 }
2516 /* any deletion makes the old insertion-undo invalid */
2517 es->undo_insert_count = 0;
2518 } else
2519 EDIT_EM_EmptyUndoBuffer(es);
2520 }
2521 if (strl) {
2522 if (can_undo) {
2523 if ((s == es->undo_position) ||
2524 ((es->undo_insert_count) &&
2525 (s == es->undo_position + es->undo_insert_count)))
2526 /*
2527 * insertion is new and at delete position or
2528 * an extension to either left or right
2529 */
2530 es->undo_insert_count += strl;
2531 else {
2532 /* new insertion undo */
2533 es->undo_position = s;
2534 es->undo_insert_count = strl;
2535 /* new insertion makes old delete-buffer invalid */
2536 *es->undo_text = '\0';
2537 }
2538 } else
2539 EDIT_EM_EmptyUndoBuffer(es);
2540 }
2541
2542 if (bufl)
2543 HeapFree(GetProcessHeap(), 0, buf);
2544
2545 s += strl;
2546
2547 /* If text has been deleted and we're right or center aligned then scroll rightward */
2548 if (es->style & (ES_RIGHT | ES_CENTER))
2549 {
2550 INT delta = strl - abs(es->selection_end - es->selection_start);
2551
2552 if (delta < 0 && es->x_offset)
2553 {
2554 if (abs(delta) > es->x_offset)
2555 es->x_offset = 0;
2556 else
2557 es->x_offset += delta;
2558 }
2559 }
2560
2561 EDIT_EM_SetSel(es, s, s, FALSE);
2562 es->flags |= EF_MODIFIED;
2563 if (send_update) es->flags |= EF_UPDATE;
2564 if (hrgn)
2565 {
2566 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2567 DeleteObject(hrgn);
2568 }
2569 else
2570 EDIT_UpdateText(es, NULL, TRUE);
2571
2572 EDIT_EM_ScrollCaret(es);
2573
2574 /* force scroll info update */
2575 EDIT_UpdateScrollInfo(es);
2576
2577
2578 if(send_update || (es->flags & EF_UPDATE))
2579 {
2580 es->flags &= ~EF_UPDATE;
2581 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2582 }
2583 }
2584
2585
2586 /*********************************************************************
2587 *
2588 * EM_SETHANDLE
2589 *
2590 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2591 *
2592 */
2593 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2594 {
2595 if (!(es->style & ES_MULTILINE))
2596 return;
2597
2598 if (!hloc) {
2599 WARN("called with NULL handle\n");
2600 return;
2601 }
2602
2603 EDIT_UnlockBuffer(es, TRUE);
2604
2605 if(es->is_unicode)
2606 {
2607 if(es->hloc32A)
2608 {
2609 LocalFree(es->hloc32A);
2610 es->hloc32A = NULL;
2611 }
2612 es->hloc32W = hloc;
2613 }
2614 else
2615 {
2616 INT countW, countA;
2617 HLOCAL hloc32W_new;
2618 WCHAR *textW;
2619 CHAR *textA;
2620
2621 countA = LocalSize(hloc);
2622 textA = LocalLock(hloc);
2623 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2624 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2625 {
2626 ERR("Could not allocate new unicode buffer\n");
2627 return;
2628 }
2629 textW = LocalLock(hloc32W_new);
2630 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2631 LocalUnlock(hloc32W_new);
2632 LocalUnlock(hloc);
2633
2634 if(es->hloc32W)
2635 LocalFree(es->hloc32W);
2636
2637 es->hloc32W = hloc32W_new;
2638 es->hloc32A = hloc;
2639 }
2640
2641 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2642
2643 es->flags |= EF_APP_HAS_HANDLE;
2644 EDIT_LockBuffer(es);
2645
2646 es->x_offset = es->y_offset = 0;
2647 es->selection_start = es->selection_end = 0;
2648 EDIT_EM_EmptyUndoBuffer(es);
2649 es->flags &= ~EF_MODIFIED;
2650 es->flags &= ~EF_UPDATE;
2651 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2652 EDIT_UpdateText(es, NULL, TRUE);
2653 EDIT_EM_ScrollCaret(es);
2654 /* force scroll info update */
2655 EDIT_UpdateScrollInfo(es);
2656 }
2657
2658
2659 /*********************************************************************
2660 *
2661 * EM_SETLIMITTEXT
2662 *
2663 * NOTE: this version currently implements WinNT limits
2664 *
2665 */
2666 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2667 {
2668 if (!limit) limit = ~0u;
2669 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2670 es->buffer_limit = limit;
2671 }
2672
2673
2674 /*********************************************************************
2675 *
2676 * EM_SETMARGINS
2677 *
2678 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2679 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2680 * margin according to the textmetrics of the current font.
2681 *
2682 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2683 * of the char's width as the margin, but this is not how Windows handles this.
2684 * For all other fonts Windows sets the margins to zero.
2685 *
2686 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2687 * edit control is equal to or larger than a certain size.
2688 * Interestingly if one subtracts both the left and right margins from
2689 * this size one always seems to get an even number. The extents of
2690 * the (four character) string "'**'" match this quite closely, so
2691 * we'll use this until we come up with a better idea.
2692 */
2693 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2694 {
2695 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2696 SIZE sz;
2697
2698 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2699 return sz.cx + left + right;
2700 }
2701
2702 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2703 WORD left, WORD right, BOOL repaint)
2704 {
2705 TEXTMETRICW tm;
2706 INT default_left_margin = 0; /* in pixels */
2707 INT default_right_margin = 0; /* in pixels */
2708
2709 /* Set the default margins depending on the font */
2710 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2711 HDC dc = GetDC(es->hwndSelf);
2712 HFONT old_font = SelectObject(dc, es->font);
2713 GetTextMetricsW(dc, &tm);
2714 /* The default margins are only non zero for TrueType or Vector fonts */
2715 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2716 int min_size;
2717 RECT rc;
2718 /* This must be calculated more exactly! But how? */
2719 default_left_margin = tm.tmAveCharWidth / 2;
2720 default_right_margin = tm.tmAveCharWidth / 2;
2721 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2722 GetClientRect(es->hwndSelf, &rc);
2723 if(rc.right - rc.left < min_size) {
2724 default_left_margin = es->left_margin;
2725 default_right_margin = es->right_margin;
2726 }
2727 }
2728 SelectObject(dc, old_font);
2729 ReleaseDC(es->hwndSelf, dc);
2730 }
2731
2732 if (action & EC_LEFTMARGIN) {
2733 es->format_rect.left -= es->left_margin;
2734 if (left != EC_USEFONTINFO)
2735 es->left_margin = left;
2736 else
2737 es->left_margin = default_left_margin;
2738 es->format_rect.left += es->left_margin;
2739 }
2740
2741 if (action & EC_RIGHTMARGIN) {
2742 es->format_rect.right += es->right_margin;
2743 if (right != EC_USEFONTINFO)
2744 es->right_margin = right;
2745 else
2746 es->right_margin = default_right_margin;
2747 es->format_rect.right -= es->right_margin;
2748 }
2749
2750 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2751 EDIT_AdjustFormatRect(es);
2752 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2753 }
2754
2755 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2756 }
2757
2758
2759 /*********************************************************************
2760 *
2761 * EM_SETPASSWORDCHAR
2762 *
2763 */
2764 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2765 {
2766 LONG style;
2767
2768 if (es->style & ES_MULTILINE)
2769 return;
2770
2771 if (es->password_char == c)
2772 return;
2773
2774 style = GetWindowLongPtrW( es->hwndSelf, GWL_STYLE );
2775 es->password_char = c;
2776 if (c) {
2777 SetWindowLongPtrW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2778 es->style |= ES_PASSWORD;
2779 } else {
2780 SetWindowLongPtrW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2781 es->style &= ~ES_PASSWORD;
2782 }
2783 EDIT_UpdateText(es, NULL, TRUE);
2784 }
2785
2786
2787 /*********************************************************************
2788 *
2789 * EM_SETTABSTOPS
2790 *
2791 */
2792 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2793 {
2794 if (!(es->style & ES_MULTILINE))
2795 return FALSE;
2796 HeapFree(GetProcessHeap(), 0, es->tabs);
2797 es->tabs_count = count;
2798 if (!count)
2799 es->tabs = NULL;
2800 else {
2801 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2802 if (es->tabs == NULL)
2803 {
2804 es->tabs_count = 0;
2805 return FALSE;
2806 }
2807 memcpy(es->tabs, tabs, count * sizeof(INT));
2808 }
2809 return TRUE;
2810 }
2811
2812
2813 /*********************************************************************
2814 *
2815 * EM_SETWORDBREAKPROC
2816 *
2817 */
2818 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2819 {
2820 if (es->word_break_proc == wbp)
2821 return;
2822
2823 es->word_break_proc = wbp;
2824
2825 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2826 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2827 EDIT_UpdateText(es, NULL, TRUE);
2828 }
2829 }
2830
2831
2832 /*********************************************************************
2833 *
2834 * EM_UNDO / WM_UNDO
2835 *
2836 */
2837 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2838 {
2839 INT ulength;
2840 LPWSTR utext;
2841
2842 /* As per MSDN spec, for a single-line edit control,
2843 the return value is always TRUE */
2844 if( es->style & ES_READONLY )
2845 return !(es->style & ES_MULTILINE);
2846
2847 ulength = strlenW(es->undo_text);
2848
2849 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
2850 if (utext == NULL)
2851 return FALSE;
2852
2853 strcpyW(utext, es->undo_text);
2854
2855 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2856 es->undo_insert_count, debugstr_w(utext));
2857
2858 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2859 EDIT_EM_EmptyUndoBuffer(es);
2860 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
2861 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2862 /* send the notification after the selection start and end are set */
2863 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2864 EDIT_EM_ScrollCaret(es);
2865 HeapFree(GetProcessHeap(), 0, utext);
2866
2867 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2868 es->undo_insert_count, debugstr_w(es->undo_text));
2869 return TRUE;
2870 }
2871
2872
2873 /* Helper function for WM_CHAR
2874 *
2875 * According to an MSDN blog article titled "Just because you're a control
2876 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2877 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2878 * a dialog box or not.
2879 */
2880 static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2881 {
2882 return (es->flags & EF_DIALOGMODE);
2883 }
2884
2885
2886 /*********************************************************************
2887 *
2888 * WM_PASTE
2889 *
2890 */
2891 static void EDIT_WM_Paste(EDITSTATE *es)
2892 {
2893 HGLOBAL hsrc;
2894 LPWSTR src;
2895
2896 /* Protect read-only edit control from modification */
2897 if(es->style & ES_READONLY)
2898 return;
2899
2900 OpenClipboard(es->hwndSelf);
2901 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2902 src = GlobalLock(hsrc);
2903 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
2904 GlobalUnlock(hsrc);
2905 }
2906 else if (es->style & ES_PASSWORD) {
2907 /* clear selected text in password edit box even with empty clipboard */
2908 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2909 }
2910 CloseClipboard();
2911 }
2912
2913
2914 /*********************************************************************
2915 *
2916 * WM_COPY
2917 *
2918 */
2919 static void EDIT_WM_Copy(EDITSTATE *es)
2920 {
2921 INT s = min(es->selection_start, es->selection_end);
2922 INT e = max(es->selection_start, es->selection_end);
2923 HGLOBAL hdst;
2924 LPWSTR dst;
2925 DWORD len;
2926
2927 if (e == s) return;
2928
2929 len = e - s;
2930 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2931 dst = GlobalLock(hdst);
2932 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2933 dst[len] = 0; /* ensure 0 termination */
2934 TRACE("%s\n", debugstr_w(dst));
2935 GlobalUnlock(hdst);
2936 OpenClipboard(es->hwndSelf);
2937 EmptyClipboard();
2938 SetClipboardData(CF_UNICODETEXT, hdst);
2939 CloseClipboard();
2940 }
2941
2942
2943 /*********************************************************************
2944 *
2945 * WM_CLEAR
2946 *
2947 */
2948 static inline void EDIT_WM_Clear(EDITSTATE *es)
2949 {
2950 /* Protect read-only edit control from modification */
2951 if(es->style & ES_READONLY)
2952 return;
2953
2954 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2955 }
2956
2957
2958 /*********************************************************************
2959 *
2960 * WM_CUT
2961 *
2962 */
2963 static inline void EDIT_WM_Cut(EDITSTATE *es)
2964 {
2965 EDIT_WM_Copy(es);
2966 EDIT_WM_Clear(es);
2967 }
2968
2969
2970 /*********************************************************************
2971 *
2972 * WM_CHAR
2973 *
2974 */
2975 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2976 {
2977 BOOL control;
2978
2979 control = GetKeyState(VK_CONTROL) & 0x8000;
2980
2981 switch (c) {
2982 case '\r':
2983 /* If it's not a multiline edit box, it would be ignored below.
2984 * For multiline edit without ES_WANTRETURN, we have to make a
2985 * special case.
2986 */
2987 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2988 if (EDIT_IsInsideDialog(es))
2989 break;
2990 case '\n':
2991 if (es->style & ES_MULTILINE) {
2992 if (es->style & ES_READONLY) {
2993 EDIT_MoveHome(es, FALSE, FALSE);
2994 EDIT_MoveDown_ML(es, FALSE);
2995 } else {
2996 static const WCHAR cr_lfW[] = {'\r','\n',0};
2997 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
2998 }
2999 }
3000 break;
3001 case '\t':
3002 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3003 {
3004 static const WCHAR tabW[] = {'\t',0};
3005 if (EDIT_IsInsideDialog(es))
3006 break;
3007 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3008 }
3009 break;
3010 case VK_BACK:
3011 if (!(es->style & ES_READONLY) && !control) {
3012 if (es->selection_start != es->selection_end)
3013 EDIT_WM_Clear(es);
3014 else {
3015 /* delete character left of caret */
3016 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3017 EDIT_MoveBackward(es, TRUE);
3018 EDIT_WM_Clear(es);
3019 }
3020 }
3021 break;
3022 case 0x03: /* ^C */
3023 if (!(es->style & ES_PASSWORD))
3024 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3025 break;
3026 case 0x16: /* ^V */
3027 if (!(es->style & ES_READONLY))
3028 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3029 break;
3030 case 0x18: /* ^X */
3031 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3032 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3033 break;
3034 case 0x1A: /* ^Z */
3035 if (!(es->style & ES_READONLY))
3036 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3037 break;
3038
3039 default:
3040 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3041 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3042 break;
3043
3044 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3045 WCHAR str[2];
3046 str[0] = c;
3047 str[1] = '\0';
3048 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3049 }
3050 break;
3051 }
3052 return 1;
3053 }
3054
3055 #if 0 // Removed see Revision 43925 comments.
3056 /*********************************************************************
3057 *
3058 * WM_COMMAND
3059 *
3060 */
3061 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3062 {
3063 if (code || control)
3064 return;
3065
3066 switch (id) {
3067 case EM_UNDO:
3068 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3069 break;
3070 case WM_CUT:
3071 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3072 break;
3073 case WM_COPY:
3074 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3075 break;
3076 case WM_PASTE:
3077 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3078 break;
3079 case WM_CLEAR:
3080 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3081 break;
3082 case EM_SETSEL:
3083 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3084 EDIT_EM_ScrollCaret(es);
3085 break;
3086 default:
3087 ERR("unknown menu item, please report\n");
3088 break;
3089 }
3090 }
3091 #endif
3092
3093 /*********************************************************************
3094 *
3095 * WM_CONTEXTMENU
3096 *
3097 * Note: the resource files resource/sysres_??.rc cannot define a
3098 * single popup menu. Hence we use a (dummy) menubar
3099 * containing the single popup menu as its first item.
3100 *
3101 * FIXME: the message identifiers have been chosen arbitrarily,
3102 * hence we use MF_BYPOSITION.
3103 * We might as well use the "real" values (anybody knows ?)
3104 * The menu definition is in resources/sysres_??.rc.
3105 * Once these are OK, we better use MF_BYCOMMAND here
3106 * (as we do in EDIT_WM_Command()).
3107 *
3108 */
3109 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3110 {
3111 #ifdef __REACTOS__
3112 HMENU menu = LoadMenuA(User32Instance, "EDITMENU");
3113 #else
3114 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3115 #endif
3116 HMENU popup = GetSubMenu(menu, 0);
3117 UINT start = es->selection_start;
3118 UINT end = es->selection_end;
3119
3120 BOOL selectedItem;
3121 ORDER_UINT(start, end);
3122
3123 /* undo */
3124 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3125 /* cut */
3126 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3127 /* copy */
3128 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3129 /* paste */
3130 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3131 /* delete */
3132 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3133 /* select all */
3134 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3135
3136 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3137 {
3138 RECT rc;
3139 /* Windows places the menu at the edit's center in this case */
3140 GetClientRect(es->hwndSelf, &rc);
3141 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3142 x = rc.left + (rc.right - rc.left) / 2;
3143 y = rc.top + (rc.bottom - rc.top) / 2;
3144 }
3145
3146 selectedItem = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, es->hwndSelf, NULL);
3147 // Added see Revision 43925 comments.
3148 switch (selectedItem) {
3149 case EM_UNDO:
3150 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3151 break;
3152 case WM_CUT:
3153 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3154 break;
3155 case WM_COPY:
3156 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3157 break;
3158 case WM_PASTE:
3159 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3160 break;
3161 case WM_CLEAR:
3162 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3163 break;
3164 case EM_SETSEL:
3165 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3166 EDIT_EM_ScrollCaret(es);
3167 break;
3168 default:
3169 ERR("unknown menu item, please report\n");
3170 break;
3171 }
3172
3173 DestroyMenu(menu);
3174 }
3175
3176
3177 /*********************************************************************
3178 *
3179 * WM_GETTEXT
3180 *
3181 */
3182 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3183 {
3184 if(!count) return 0;
3185
3186 if(unicode)
3187 {
3188 lstrcpynW(dst, es->text, count);
3189 return strlenW(dst);
3190 }
3191 else
3192 {
3193 LPSTR textA = (LPSTR)dst;
3194 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3195 textA[count - 1] = 0; /* ensure 0 termination */
3196 return strlen(textA);
3197 }
3198 }
3199
3200 /*********************************************************************
3201 *
3202 * EDIT_CheckCombo
3203 *
3204 */
3205 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3206 {
3207 HWND hLBox = es->hwndListBox;
3208 HWND hCombo;
3209 BOOL bDropped;
3210 int nEUI;
3211
3212 if (!hLBox)
3213 return FALSE;
3214
3215 hCombo = GetParent(es->hwndSelf);
3216 bDropped = TRUE;
3217 nEUI = 0;
3218
3219 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3220
3221 if (key == VK_UP || key == VK_DOWN)
3222 {
3223 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3224 nEUI = 1;
3225
3226 if (msg == WM_KEYDOWN || nEUI)
3227 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3228 }
3229
3230 switch (msg)
3231 {
3232 case WM_KEYDOWN:
3233 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3234 {
3235 /* make sure ComboLBox pops up */
3236 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3237 key = VK_F4;
3238 nEUI = 2;
3239 }
3240
3241 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3242 break;
3243
3244 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3245 if (nEUI)
3246 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3247 else
3248 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
3249 break;
3250 }
3251
3252 if(nEUI == 2)
3253 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3254
3255 return TRUE;
3256 }
3257
3258
3259 /*********************************************************************
3260 *
3261 * WM_KEYDOWN
3262 *
3263 * Handling of special keys that don't produce a WM_CHAR
3264 * (i.e. non-printable keys) & Backspace & Delete
3265 *
3266 */
3267 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3268 {
3269 BOOL shift;
3270 BOOL control;
3271
3272 if (GetKeyState(VK_MENU) & 0x8000)
3273 return 0;
3274
3275 shift = GetKeyState(VK_SHIFT) & 0x8000;
3276 control = GetKeyState(VK_CONTROL) & 0x8000;
3277
3278 switch (key) {
3279 case VK_F4:
3280 case VK_UP:
3281 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3282 break;
3283
3284 /* fall through */
3285 case VK_LEFT:
3286 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3287 EDIT_MoveUp_ML(es, shift);
3288 else
3289 if (control)
3290 EDIT_MoveWordBackward(es, shift);
3291 else
3292 EDIT_MoveBackward(es, shift);
3293 break;
3294 case VK_DOWN:
3295 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3296 break;
3297 /* fall through */
3298 case VK_RIGHT:
3299 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3300 EDIT_MoveDown_ML(es, shift);
3301 else if (control)
3302 EDIT_MoveWordForward(es, shift);
3303 else
3304 EDIT_MoveForward(es, shift);
3305 break;
3306 case VK_HOME:
3307 EDIT_MoveHome(es, shift, control);
3308 break;
3309 case VK_END:
3310 EDIT_MoveEnd(es, shift, control);
3311 break;
3312 case VK_PRIOR:
3313 if (es->style & ES_MULTILINE)
3314 EDIT_MovePageUp_ML(es, shift);
3315 else
3316 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3317 break;
3318 case VK_NEXT:
3319 if (es->style & ES_MULTILINE)
3320 EDIT_MovePageDown_ML(es, shift);
3321 else
3322 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3323 break;
3324 case VK_DELETE:
3325 if (!(es->style & ES_READONLY) && !(shift && control)) {
3326 if (es->selection_start != es->selection_end) {
3327 if (shift)
3328 EDIT_WM_Cut(es);
3329 else
3330 EDIT_WM_Clear(es);
3331 } else {
3332 if (shift) {
3333 /* delete character left of caret */
3334 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3335 EDIT_MoveBackward(es, TRUE);
3336 EDIT_WM_Clear(es);
3337 } else if (control) {
3338 /* delete to end of line */
3339 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3340 EDIT_MoveEnd(es, TRUE, FALSE);
3341 EDIT_WM_Clear(es);
3342 } else {
3343 /* delete character right of caret */
3344 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3345 EDIT_MoveForward(es, TRUE);
3346 EDIT_WM_Clear(es);
3347 }
3348 }
3349 }
3350 break;
3351 case VK_INSERT:
3352 if (shift) {
3353 if (!(es->style & ES_READONLY))
3354 EDIT_WM_Paste(es);
3355 } else if (control)
3356 EDIT_WM_Copy(es);
3357 break;
3358 case VK_RETURN:
3359 /* If the edit doesn't want the return send a message to the default object */
3360 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3361 {
3362 DWORD dw;
3363
3364 if (!EDIT_IsInsideDialog(es)) break;
3365 if (control) break;
3366 dw = SendMessageW( es->hwndParent, DM_GETDEFID, 0, 0 );
3367 if (HIWORD(dw) == DC_HASDEFID)
3368 {
3369 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3370 if (hwDefCtrl)
3371 {
3372 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, (LPARAM)TRUE);
3373 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3374 }
3375 }
3376 }
3377 break;
3378 case VK_ESCAPE:
3379 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3380 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3381 break;
3382 case VK_TAB:
3383 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3384 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3385 break;
3386 }
3387 return TRUE;
3388 }
3389
3390
3391 /*********************************************************************
3392 *
3393 * WM_KILLFOCUS
3394 *
3395 */
3396 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3397 {
3398 es->flags &= ~EF_FOCUSED;
3399 DestroyCaret();
3400 if(!(es->style & ES_NOHIDESEL))
3401 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3402 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3403 return 0;
3404 }
3405
3406
3407 /*********************************************************************
3408 *
3409 * WM_LBUTTONDBLCLK
3410 *
3411 * The caret position has been set on the WM_LBUTTONDOWN message
3412 *
3413 */
3414 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3415 {
3416 INT s;
3417 INT e = es->selection_end;
3418 INT l;
3419 INT li;
3420 INT ll;
3421
3422 es->bCaptureState = TRUE;
3423 SetCapture(es->hwndSelf);
3424
3425 l = EDIT_EM_LineFromChar(es, e);
3426 li = EDIT_EM_LineIndex(es, l);
3427 ll = EDIT_EM_LineLength(es, e);
3428 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3429 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3430 EDIT_EM_SetSel(es, s, e, FALSE);
3431 EDIT_EM_ScrollCaret(es);
3432 es->region_posx = es->region_posy = 0;
3433 SetTimer(es->hwndSelf, 0, 100, NULL);
3434 return 0;
3435 }
3436
3437
3438 /*********************************************************************
3439 *
3440 * WM_LBUTTONDOWN
3441 *
3442 */
3443 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3444 {
3445 INT e;
3446 BOOL after_wrap;
3447
3448 es->bCaptureState = TRUE;
3449 SetCapture(es->hwndSelf);
3450 EDIT_ConfinePoint(es, &x, &y);
3451 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3452 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3453 EDIT_EM_ScrollCaret(es);
3454 es->region_posx = es->region_posy = 0;
3455 SetTimer(es->hwndSelf, 0, 100, NULL);
3456
3457 if (!(es->flags & EF_FOCUSED))
3458 SetFocus(es->hwndSelf);
3459
3460 return 0;