[USER32]
[reactos.git] / reactos / dll / win32 / user32 / controls / edit.c
1 /*
2 * Edit control
3 *
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
7 *
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 * NOTES
24 *
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 8, 2004, by Dimitrie O. Paun.
27 *
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
31 *
32 * TODO:
33 * - EDITBALLOONTIP structure
34 * - EM_GETCUEBANNER/Edit_GetCueBannerText
35 * - EM_HIDEBALLOONTIP/Edit_HideBalloonTip
36 * - EM_SETCUEBANNER/Edit_SetCueBannerText
37 * - EM_SHOWBALLOONTIP/Edit_ShowBalloonTip
38 * - EM_GETIMESTATUS, EM_SETIMESTATUS
39 * - EN_ALIGN_LTR_EC
40 * - EN_ALIGN_RTL_EC
41 * - ES_OEMCONVERT
42 *
43 */
44
45 #include <user32.h>
46
47 #include <wine/debug.h>
48
49 WINE_DEFAULT_DEBUG_CHANNEL(edit);
50 WINE_DECLARE_DEBUG_CHANNEL(combo);
51 WINE_DECLARE_DEBUG_CHANNEL(relay);
52
53 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
54 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
55 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
56 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
57
58 /*
59 * extra flags for EDITSTATE.flags field
60 */
61 #define EF_MODIFIED 0x0001 /* text has been modified */
62 #define EF_FOCUSED 0x0002 /* we have input focus */
63 #define EF_UPDATE 0x0004 /* notify parent of changed state */
64 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
66 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
67 wrapped line, instead of in front of the next character */
68 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
69 #define EF_APP_HAS_HANDLE 0x0200 /* Set when an app sends EM_[G|S]ETHANDLE. We are in sole control of
70 the text buffer if this is clear. */
71 #define EF_DIALOGMODE 0x0400 /* Indicates that we are inside a dialog window */
72
73 typedef enum
74 {
75 END_0 = 0, /* line ends with terminating '\0' character */
76 END_WRAP, /* line is wrapped */
77 END_HARD, /* line ends with a hard return '\r\n' */
78 END_SOFT, /* line ends with a soft return '\r\r\n' */
79 END_RICH /* line ends with a single '\n' */
80 } LINE_END;
81
82 typedef struct tagLINEDEF {
83 INT length; /* bruto length of a line in bytes */
84 INT net_length; /* netto length of a line in visible characters */
85 LINE_END ending;
86 INT width; /* width of the line in pixels */
87 INT index; /* line index into the buffer */
88 struct tagLINEDEF *next;
89 } LINEDEF;
90
91 typedef struct
92 {
93 BOOL is_unicode; /* how the control was created */
94 LPWSTR text; /* the actual contents of the control */
95 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
96 UINT buffer_size; /* the size of the buffer in characters */
97 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
98 HFONT font; /* NULL means standard system font */
99 INT x_offset; /* scroll offset for multi lines this is in pixels
100 for single lines it's in characters */
101 INT line_height; /* height of a screen line in pixels */
102 INT char_width; /* average character width in pixels */
103 DWORD style; /* sane version of wnd->dwStyle */
104 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
105 INT undo_insert_count; /* number of characters inserted in sequence */
106 UINT undo_position; /* character index of the insertion and deletion */
107 LPWSTR undo_text; /* deleted text */
108 UINT undo_buffer_size; /* size of the deleted text buffer */
109 INT selection_start; /* == selection_end if no selection */
110 INT selection_end; /* == current caret position */
111 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
112 INT left_margin; /* in pixels */
113 INT right_margin; /* in pixels */
114 RECT format_rect;
115 INT text_width; /* width of the widest line in pixels for multi line controls
116 and just line width for single line controls */
117 INT region_posx; /* Position of cursor relative to region: */
118 INT region_posy; /* -1: to left, 0: within, 1: to right */
119 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
120 INT line_count; /* number of lines */
121 INT y_offset; /* scroll offset in number of lines */
122 BOOL bCaptureState; /* flag indicating whether mouse was captured */
123 BOOL bEnableState; /* flag keeping the enable state */
124 HWND hwndSelf; /* the our window handle */
125 HWND hwndParent; /* Handle of parent for sending EN_* messages.
126 Even if parent will change, EN_* messages
127 should be sent to the first parent. */
128 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
129 /*
130 * only for multi line controls
131 */
132 INT lock_count; /* amount of re-entries in the EditWndProc */
133 INT tabs_count;
134 LPINT tabs;
135 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
136 HLOCAL hloc32W; /* our unicode local memory block */
137 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
138 or EM_SETHANDLE */
139 /*
140 * IME Data
141 */
142 UINT composition_len; /* length of composition, 0 == no composition */
143 int composition_start; /* the character position for the composition */
144 } EDITSTATE;
145
146
147 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
148 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
149
150 /* used for disabled or read-only edit control */
151 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
152 do \
153 { /* Notify parent which has created this edit control */ \
154 TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
155 SendMessageW(es->hwndParent, WM_COMMAND, \
156 MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
157 (LPARAM)(es->hwndSelf)); \
158 } while(0)
159
160 static const WCHAR empty_stringW[] = {0};
161
162 /*********************************************************************
163 *
164 * EM_CANUNDO
165 *
166 */
167 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
168 {
169 return (es->undo_insert_count || strlenW(es->undo_text));
170 }
171
172
173 /*********************************************************************
174 *
175 * EM_EMPTYUNDOBUFFER
176 *
177 */
178 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
179 {
180 es->undo_insert_count = 0;
181 *es->undo_text = '\0';
182 }
183
184
185 /**********************************************************************
186 * get_app_version
187 *
188 * Returns the window version in case Wine emulates a later version
189 * of windows than the application expects.
190 *
191 * In a number of cases when windows runs an application that was
192 * designed for an earlier windows version, windows reverts
193 * to "old" behaviour of that earlier version.
194 *
195 * An example is a disabled edit control that needs to be painted.
196 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
197 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
198 * applications with an expected version 0f 4.0 or higher.
199 *
200 */
201 static DWORD get_app_version(void)
202 {
203 static DWORD version;
204 if (!version)
205 {
206 DWORD dwEmulatedVersion;
207 OSVERSIONINFOW info;
208 DWORD dwProcVersion = GetProcessVersion(0);
209
210 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
211 GetVersionExW( &info );
212 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
213 /* FIXME: this may not be 100% correct; see discussion on the
214 * wine developer list in Nov 1999 */
215 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
216 }
217 return version;
218 }
219
220 static HBRUSH EDIT_NotifyCtlColor(EDITSTATE *es, HDC hdc)
221 {
222 HBRUSH hbrush;
223 UINT msg;
224
225 if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
226 msg = WM_CTLCOLORSTATIC;
227 else
228 msg = WM_CTLCOLOREDIT;
229
230 /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
231 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
232 if (!hbrush)
233 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
234 return hbrush;
235 }
236
237
238 /*********************************************************************
239 *
240 * EDIT_WordBreakProc
241 *
242 * Find the beginning of words.
243 * Note: unlike the specs for a WordBreakProc, this function only
244 * allows to be called without linebreaks between s[0] up to
245 * s[count - 1]. Remember it is only called
246 * internally, so we can decide this for ourselves.
247 *
248 */
249 static INT EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
250 {
251 INT ret = 0;
252
253 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
254
255 if(!s) return 0;
256
257 switch (action) {
258 case WB_LEFT:
259 if (!count)
260 break;
261 if (index)
262 index--;
263 if (s[index] == ' ') {
264 while (index && (s[index] == ' '))
265 index--;
266 if (index) {
267 while (index && (s[index] != ' '))
268 index--;
269 if (s[index] == ' ')
270 index++;
271 }
272 } else {
273 while (index && (s[index] != ' '))
274 index--;
275 if (s[index] == ' ')
276 index++;
277 }
278 ret = index;
279 break;
280 case WB_RIGHT:
281 if (!count)
282 break;
283 if (index)
284 index--;
285 if (s[index] == ' ')
286 while ((index < count) && (s[index] == ' ')) index++;
287 else {
288 while (s[index] && (s[index] != ' ') && (index < count))
289 index++;
290 while ((s[index] == ' ') && (index < count)) index++;
291 }
292 ret = index;
293 break;
294 case WB_ISDELIMITER:
295 ret = (s[index] == ' ');
296 break;
297 default:
298 ERR("unknown action code, please report !\n");
299 break;
300 }
301 return ret;
302 }
303
304
305 /*********************************************************************
306 *
307 * EDIT_CallWordBreakProc
308 *
309 * Call appropriate WordBreakProc (internal or external).
310 *
311 * Note: The "start" argument should always be an index referring
312 * to es->text. The actual wordbreak proc might be
313 * 16 bit, so we can't always pass any 32 bit LPSTR.
314 * Hence we assume that es->text is the buffer that holds
315 * the string under examination (we can decide this for ourselves).
316 *
317 */
318 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
319 {
320 INT ret;
321
322 if (es->word_break_proc)
323 {
324 if(es->is_unicode)
325 {
326 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
327
328 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
329 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
330 ret = wbpW(es->text + start, index, count, action);
331 }
332 else
333 {
334 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
335 INT countA;
336 CHAR *textA;
337
338 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
339 textA = HeapAlloc(GetProcessHeap(), 0, countA);
340 if (textA == NULL) return 0;
341 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
342 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
343 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
344 ret = wbpA(textA, index, countA, action);
345 HeapFree(GetProcessHeap(), 0, textA);
346 }
347 }
348 else
349 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
350
351 return ret;
352 }
353
354 /*********************************************************************
355 *
356 * EDIT_BuildLineDefs_ML
357 *
358 * Build linked list of text lines.
359 * Lines can end with '\0' (last line), a character (if it is wrapped),
360 * a soft return '\r\r\n' or a hard return '\r\n'
361 *
362 */
363 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
364 {
365 HDC dc;
366 HFONT old_font = 0;
367 LPWSTR current_position, cp;
368 INT fw;
369 LINEDEF *current_line;
370 LINEDEF *previous_line;
371 LINEDEF *start_line;
372 INT line_index = 0, nstart_line = 0, nstart_index = 0;
373 INT line_count = es->line_count;
374 INT orig_net_length;
375 RECT rc;
376
377 if (istart == iend && delta == 0)
378 return;
379
380 dc = GetDC(es->hwndSelf);
381 if (es->font)
382 old_font = SelectObject(dc, es->font);
383
384 previous_line = NULL;
385 current_line = es->first_line_def;
386
387 /* Find starting line. istart must lie inside an existing line or
388 * at the end of buffer */
389 do {
390 if (istart < current_line->index + current_line->length ||
391 current_line->ending == END_0)
392 break;
393
394 previous_line = current_line;
395 current_line = current_line->next;
396 line_index++;
397 } while (current_line);
398
399 if (!current_line) /* Error occurred start is not inside previous buffer */
400 {
401 FIXME(" modification occurred outside buffer\n");
402 ReleaseDC(es->hwndSelf, dc);
403 return;
404 }
405
406 /* Remember start of modifications in order to calculate update region */
407 nstart_line = line_index;
408 nstart_index = current_line->index;
409
410 /* We must start to reformat from the previous line since the modifications
411 * may have caused the line to wrap upwards. */
412 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
413 {
414 line_index--;
415 current_line = previous_line;
416 }
417 start_line = current_line;
418
419 fw = es->format_rect.right - es->format_rect.left;
420 current_position = es->text + current_line->index;
421 do {
422 if (current_line != start_line)
423 {
424 if (!current_line || current_line->index + delta > current_position - es->text)
425 {
426 /* The buffer has been expanded, create a new line and
427 insert it into the link list */
428 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
429 if (new_line == NULL) // reactos r33509
430 break; // reactos r33509
431 new_line->next = previous_line->next;
432 previous_line->next = new_line;
433 current_line = new_line;
434 es->line_count++;
435 }
436 else if (current_line->index + delta < current_position - es->text)
437 {
438 /* The previous line merged with this line so we delete this extra entry */
439 previous_line->next = current_line->next;
440 HeapFree(GetProcessHeap(), 0, current_line);
441 current_line = previous_line->next;
442 es->line_count--;
443 continue;
444 }
445 else /* current_line->index + delta == current_position */
446 {
447 if (current_position - es->text > iend)
448 break; /* We reached end of line modifications */
449 /* else recalculate this line */
450 }
451 }
452
453 current_line->index = current_position - es->text;
454 orig_net_length = current_line->net_length;
455
456 /* Find end of line */
457 cp = current_position;
458 while (*cp) {
459 if (*cp == '\n') break;
460 if ((*cp == '\r') && (*(cp + 1) == '\n'))
461 break;
462 cp++;
463 }
464
465 /* Mark type of line termination */
466 if (!(*cp)) {
467 current_line->ending = END_0;
468 current_line->net_length = strlenW(current_position);
469 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
470 current_line->ending = END_SOFT;
471 current_line->net_length = cp - current_position - 1;
472 } else if (*cp == '\n') {
473 current_line->ending = END_RICH;
474 current_line->net_length = cp - current_position;
475 } else {
476 current_line->ending = END_HARD;
477 current_line->net_length = cp - current_position;
478 }
479
480 /* Calculate line width */
481 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
482 current_position, current_line->net_length,
483 es->tabs_count, es->tabs));
484
485 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
486 if (!(es->style & ES_AUTOHSCROLL)) {
487 if (current_line->width > fw) {
488 INT next = 0;
489 INT prev;
490 do {
491 prev = next;
492 next = EDIT_CallWordBreakProc(es, current_position - es->text,
493 prev + 1, current_line->net_length, WB_RIGHT);
494 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
495 current_position, next, es->tabs_count, es->tabs));
496 } while (current_line->width <= fw);
497 if (!prev) { /* Didn't find a line break so force a break */
498 next = 0;
499 do {
500 prev = next;
501 next++;
502 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
503 current_position, next, es->tabs_count, es->tabs));
504 } while (current_line->width <= fw);
505 if (!prev)
506 prev = 1;
507 }
508
509 /* If the first line we are calculating, wrapped before istart, we must
510 * adjust istart in order for this to be reflected in the update region. */
511 if (current_line->index == nstart_index && istart > current_line->index + prev)
512 istart = current_line->index + prev;
513 /* else if we are updating the previous line before the first line we
514 * are re-calculating and it expanded */
515 else if (current_line == start_line &&
516 current_line->index != nstart_index && orig_net_length < prev)
517 {
518 /* Line expanded due to an upwards line wrap so we must partially include
519 * previous line in update region */
520 nstart_line = line_index;
521 nstart_index = current_line->index;
522 istart = current_line->index + orig_net_length;
523 }
524
525 current_line->net_length = prev;
526 current_line->ending = END_WRAP;
527 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
528 current_line->net_length, es->tabs_count, es->tabs));
529 }
530 else if (current_line == start_line &&
531 current_line->index != nstart_index &&
532 orig_net_length < current_line->net_length) {
533 /* The previous line expanded but it's still not as wide as the client rect */
534 /* The expansion is due to an upwards line wrap so we must partially include
535 it in the update region */
536 nstart_line = line_index;
537 nstart_index = current_line->index;
538 istart = current_line->index + orig_net_length;
539 }
540 }
541
542
543 /* Adjust length to include line termination */
544 switch (current_line->ending) {
545 case END_SOFT:
546 current_line->length = current_line->net_length + 3;
547 break;
548 case END_RICH:
549 current_line->length = current_line->net_length + 1;
550 break;
551 case END_HARD:
552 current_line->length = current_line->net_length + 2;
553 break;
554 case END_WRAP:
555 case END_0:
556 current_line->length = current_line->net_length;
557 break;
558 }
559 es->text_width = max(es->text_width, current_line->width);
560 current_position += current_line->length;
561 previous_line = current_line;
562 current_line = current_line->next;
563 line_index++;
564 } while (previous_line->ending != END_0);
565
566 /* Finish adjusting line indexes by delta or remove hanging lines */
567 if (previous_line->ending == END_0)
568 {
569 LINEDEF *pnext = NULL;
570
571 previous_line->next = NULL;
572 while (current_line)
573 {
574 pnext = current_line->next;
575 HeapFree(GetProcessHeap(), 0, current_line);
576 current_line = pnext;
577 es->line_count--;
578 }
579 }
580 else if (delta != 0)
581 {
582 while (current_line)
583 {
584 current_line->index += delta;
585 current_line = current_line->next;
586 }
587 }
588
589 /* Calculate rest of modification rectangle */
590 if (hrgn)
591 {
592 HRGN tmphrgn;
593 /*
594 * We calculate two rectangles. One for the first line which may have
595 * an indent with respect to the format rect. The other is a format-width
596 * rectangle that spans the rest of the lines that changed or moved.
597 */
598 rc.top = es->format_rect.top + nstart_line * es->line_height -
599 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
600 rc.bottom = rc.top + es->line_height;
601 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
602 rc.left = es->format_rect.left;
603 else
604 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
605 es->text + nstart_index, istart - nstart_index,
606 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
607 rc.right = es->format_rect.right;
608 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
609
610 rc.top = rc.bottom;
611 rc.left = es->format_rect.left;
612 rc.right = es->format_rect.right;
613 /*
614 * If lines were added or removed we must re-paint the remainder of the
615 * lines since the remaining lines were either shifted up or down.
616 */
617 if (line_count < es->line_count) /* We added lines */
618 rc.bottom = es->line_count * es->line_height;
619 else if (line_count > es->line_count) /* We removed lines */
620 rc.bottom = line_count * es->line_height;
621 else
622 rc.bottom = line_index * es->line_height;
623 rc.bottom += es->format_rect.top;
624 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
625 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
626 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
627 DeleteObject(tmphrgn);
628 }
629
630 if (es->font)
631 SelectObject(dc, old_font);
632
633 ReleaseDC(es->hwndSelf, dc);
634 }
635
636
637 static inline UINT get_text_length(EDITSTATE *es)
638 {
639 if(es->text_length == (UINT)-1)
640 es->text_length = strlenW(es->text);
641 return es->text_length;
642 }
643
644 /*********************************************************************
645 *
646 * EDIT_GetPasswordPointer_SL
647 *
648 * note: caller should free the (optionally) allocated buffer
649 *
650 */
651 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
652 {
653 if (es->style & ES_PASSWORD) {
654 INT len = get_text_length(es);
655 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
656 if (text == NULL)
657 return NULL;
658 text[len] = '\0';
659 while(len) text[--len] = es->password_char;
660 return text;
661 } else
662 return es->text;
663 }
664
665
666 /*********************************************************************
667 *
668 * EDIT_CalcLineWidth_SL
669 *
670 */
671 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
672 {
673 SIZE size;
674 LPWSTR text;
675 HDC dc;
676 HFONT old_font = 0;
677
678 text = EDIT_GetPasswordPointer_SL(es);
679
680 dc = GetDC(es->hwndSelf);
681 if (es->font)
682 old_font = SelectObject(dc, es->font);
683
684 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
685
686 if (es->font)
687 SelectObject(dc, old_font);
688 ReleaseDC(es->hwndSelf, dc);
689
690 if (es->style & ES_PASSWORD)
691 HeapFree(GetProcessHeap(), 0, text);
692
693 es->text_width = size.cx;
694 }
695
696 /*********************************************************************
697 *
698 * EDIT_CharFromPos
699 *
700 * Beware: This is not the function called on EM_CHARFROMPOS
701 * The position _can_ be outside the formatting / client
702 * rectangle
703 * The return value is only the character index
704 *
705 */
706 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
707 {
708 INT index;
709 HDC dc;
710 HFONT old_font = 0;
711 INT x_high = 0, x_low = 0;
712
713 if (es->style & ES_MULTILINE) {
714 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
715 INT line_index = 0;
716 LINEDEF *line_def = es->first_line_def;
717 INT low, high;
718 while ((line > 0) && line_def->next) {
719 line_index += line_def->length;
720 line_def = line_def->next;
721 line--;
722 }
723 x += es->x_offset - es->format_rect.left;
724 if (es->style & ES_RIGHT)
725 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
726 else if (es->style & ES_CENTER)
727 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
728 if (x >= line_def->width) {
729 if (after_wrap)
730 *after_wrap = (line_def->ending == END_WRAP);
731 return line_index + line_def->net_length;
732 }
733 if (x <= 0) {
734 if (after_wrap)
735 *after_wrap = FALSE;
736 return line_index;
737 }
738 dc = GetDC(es->hwndSelf);
739 if (es->font)
740 old_font = SelectObject(dc, es->font);
741 low = line_index;
742 high = line_index + line_def->net_length + 1;
743 while (low < high - 1)
744 {
745 INT mid = (low + high) / 2;
746 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
747 if (x_now > x) {
748 high = mid;
749 x_high = x_now;
750 } else {
751 low = mid;
752 x_low = x_now;
753 }
754 }
755 if (abs(x_high - x) + 1 <= abs(x_low - x))
756 index = high;
757 else
758 index = low;
759
760 if (after_wrap)
761 *after_wrap = ((index == line_index + line_def->net_length) &&
762 (line_def->ending == END_WRAP));
763 } else {
764 LPWSTR text;
765 SIZE size;
766 if (after_wrap)
767 *after_wrap = FALSE;
768 x -= es->format_rect.left;
769 if (!x)
770 return es->x_offset;
771
772 if (!es->x_offset)
773 {
774 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
775 if (es->style & ES_RIGHT)
776 x -= indent;
777 else if (es->style & ES_CENTER)
778 x -= indent / 2;
779 }
780
781 text = EDIT_GetPasswordPointer_SL(es);
782 dc = GetDC(es->hwndSelf);
783 if (es->font)
784 old_font = SelectObject(dc, es->font);
785 if (x < 0)
786 {
787 INT low = 0;
788 INT high = es->x_offset;
789 while (low < high - 1)
790 {
791 INT mid = (low + high) / 2;
792 GetTextExtentPoint32W( dc, text + mid,
793 es->x_offset - mid, &size );
794 if (size.cx > -x) {
795 low = mid;
796 x_low = size.cx;
797 } else {
798 high = mid;
799 x_high = size.cx;
800 }
801 }
802 if (abs(x_high + x) <= abs(x_low + x) + 1)
803 index = high;
804 else
805 index = low;
806 }
807 else
808 {
809 INT low = es->x_offset;
810 INT high = get_text_length(es) + 1;
811 while (low < high - 1)
812 {
813 INT mid = (low + high) / 2;
814 GetTextExtentPoint32W( dc, text + es->x_offset,
815 mid - es->x_offset, &size );
816 if (size.cx > x) {
817 high = mid;
818 x_high = size.cx;
819 } else {
820 low = mid;
821 x_low = size.cx;
822 }
823 }
824 if (abs(x_high - x) <= abs(x_low - x) + 1)
825 index = high;
826 else
827 index = low;
828 }
829 if (es->style & ES_PASSWORD)
830 HeapFree(GetProcessHeap(), 0, text);
831 }
832 if (es->font)
833 SelectObject(dc, old_font);
834 ReleaseDC(es->hwndSelf, dc);
835 return index;
836 }
837
838
839 /*********************************************************************
840 *
841 * EDIT_ConfinePoint
842 *
843 * adjusts the point to be within the formatting rectangle
844 * (so CharFromPos returns the nearest _visible_ character)
845 *
846 */
847 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
848 {
849 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
850 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
851 }
852
853
854 /*********************************************************************
855 *
856 * EM_LINEFROMCHAR
857 *
858 */
859 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
860 {
861 INT line;
862 LINEDEF *line_def;
863
864 if (!(es->style & ES_MULTILINE))
865 return 0;
866 if (index > (INT)get_text_length(es))
867 return es->line_count - 1;
868 if (index == -1)
869 index = min(es->selection_start, es->selection_end);
870
871 line = 0;
872 line_def = es->first_line_def;
873 index -= line_def->length;
874 while ((index >= 0) && line_def->next) {
875 line++;
876 line_def = line_def->next;
877 index -= line_def->length;
878 }
879 return line;
880 }
881
882
883 /*********************************************************************
884 *
885 * EM_LINEINDEX
886 *
887 */
888 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
889 {
890 INT line_index;
891 const LINEDEF *line_def;
892
893 if (!(es->style & ES_MULTILINE))
894 return 0;
895 if (line >= es->line_count)
896 return -1;
897
898 line_index = 0;
899 line_def = es->first_line_def;
900 if (line == -1) {
901 INT index = es->selection_end - line_def->length;
902 while ((index >= 0) && line_def->next) {
903 line_index += line_def->length;
904 line_def = line_def->next;
905 index -= line_def->length;
906 }
907 } else {
908 while (line > 0) {
909 line_index += line_def->length;
910 line_def = line_def->next;
911 line--;
912 }
913 }
914 return line_index;
915 }
916
917
918 /*********************************************************************
919 *
920 * EM_LINELENGTH
921 *
922 */
923 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
924 {
925 LINEDEF *line_def;
926
927 if (!(es->style & ES_MULTILINE))
928 return get_text_length(es);
929
930 if (index == -1) {
931 /* get the number of remaining non-selected chars of selected lines */
932 INT32 l; /* line number */
933 INT32 li; /* index of first char in line */
934 INT32 count;
935 l = EDIT_EM_LineFromChar(es, es->selection_start);
936 /* # chars before start of selection area */
937 count = es->selection_start - EDIT_EM_LineIndex(es, l);
938 l = EDIT_EM_LineFromChar(es, es->selection_end);
939 /* # chars after end of selection */
940 li = EDIT_EM_LineIndex(es, l);
941 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
942 return count;
943 }
944 line_def = es->first_line_def;
945 index -= line_def->length;
946 while ((index >= 0) && line_def->next) {
947 line_def = line_def->next;
948 index -= line_def->length;
949 }
950 return line_def->net_length;
951 }
952
953
954 /*********************************************************************
955 *
956 * EM_POSFROMCHAR
957 *
958 */
959 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
960 {
961 INT len = get_text_length(es);
962 INT l;
963 INT li;
964 INT x;
965 INT y = 0;
966 INT w;
967 INT lw = 0;
968 INT ll = 0;
969 HDC dc;
970 HFONT old_font = 0;
971 SIZE size;
972 LINEDEF *line_def;
973
974 index = min(index, len);
975 dc = GetDC(es->hwndSelf);
976 if (es->font)
977 old_font = SelectObject(dc, es->font);
978 if (es->style & ES_MULTILINE) {
979 l = EDIT_EM_LineFromChar(es, index);
980 y = (l - es->y_offset) * es->line_height;
981 li = EDIT_EM_LineIndex(es, l);
982 if (after_wrap && (li == index) && l) {
983 INT l2 = l - 1;
984 line_def = es->first_line_def;
985 while (l2) {
986 line_def = line_def->next;
987 l2--;
988 }
989 if (line_def->ending == END_WRAP) {
990 l--;
991 y -= es->line_height;
992 li = EDIT_EM_LineIndex(es, l);
993 }
994 }
995
996 line_def = es->first_line_def;
997 while (line_def->index != li)
998 line_def = line_def->next;
999
1000 ll = line_def->net_length;
1001 lw = line_def->width;
1002
1003 w = es->format_rect.right - es->format_rect.left;
1004 if (es->style & ES_RIGHT)
1005 {
1006 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
1007 es->tabs_count, es->tabs)) - es->x_offset;
1008 x = w - x;
1009 }
1010 else if (es->style & ES_CENTER)
1011 {
1012 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1013 es->tabs_count, es->tabs)) - es->x_offset;
1014 x += (w - lw) / 2;
1015 }
1016 else /* ES_LEFT */
1017 {
1018 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
1019 es->tabs_count, es->tabs)) - es->x_offset;
1020 }
1021 } else {
1022 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
1023 if (index < es->x_offset) {
1024 GetTextExtentPoint32W(dc, text + index,
1025 es->x_offset - index, &size);
1026 x = -size.cx;
1027 } else {
1028 GetTextExtentPoint32W(dc, text + es->x_offset,
1029 index - es->x_offset, &size);
1030 x = size.cx;
1031
1032 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1033 {
1034 w = es->format_rect.right - es->format_rect.left;
1035 if (w > es->text_width)
1036 {
1037 if (es->style & ES_RIGHT)
1038 x += w - es->text_width;
1039 else if (es->style & ES_CENTER)
1040 x += (w - es->text_width) / 2;
1041 }
1042 }
1043 }
1044 y = 0;
1045 if (es->style & ES_PASSWORD)
1046 HeapFree(GetProcessHeap(), 0, text);
1047 }
1048 x += es->format_rect.left;
1049 y += es->format_rect.top;
1050 if (es->font)
1051 SelectObject(dc, old_font);
1052 ReleaseDC(es->hwndSelf, dc);
1053 return MAKELONG((INT16)x, (INT16)y);
1054 }
1055
1056
1057 /*********************************************************************
1058 *
1059 * EDIT_GetLineRect
1060 *
1061 * Calculates the bounding rectangle for a line from a starting
1062 * column to an ending column.
1063 *
1064 */
1065 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1066 {
1067 INT line_index = EDIT_EM_LineIndex(es, line);
1068
1069 if (es->style & ES_MULTILINE)
1070 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1071 else
1072 rc->top = es->format_rect.top;
1073 rc->bottom = rc->top + es->line_height;
1074 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1075 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1076 }
1077
1078
1079 static inline void text_buffer_changed(EDITSTATE *es)
1080 {
1081 es->text_length = (UINT)-1;
1082 }
1083
1084 /*********************************************************************
1085 * EDIT_LockBuffer
1086 *
1087 */
1088 static void EDIT_LockBuffer(EDITSTATE *es)
1089 {
1090 if (!es->text) {
1091
1092 CHAR *textA = NULL; // ReactOS Hacked!
1093 UINT countA = 0;
1094
1095 if(es->hloc32W)
1096 {
1097 if(es->hloc32A)
1098 {
1099 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1100 textA = LocalLock(es->hloc32A);
1101 countA = strlen(textA) + 1;
1102 }
1103 }
1104 else {
1105 ERR("no buffer ... please report\n");
1106 return;
1107 }
1108
1109 if(textA)
1110 {
1111 HLOCAL hloc32W_new;
1112 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1113 if(countW_new > es->buffer_size + 1)
1114 {
1115 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1116 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1117 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1118 if(hloc32W_new)
1119 {
1120 es->hloc32W = hloc32W_new;
1121 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1122 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1123 }
1124 else
1125 WARN("FAILED! Will synchronize partially\n");
1126 }
1127 es->text = LocalLock(es->hloc32W);
1128 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1129 LocalUnlock(es->hloc32A);
1130 }
1131 else es->text = LocalLock(es->hloc32W);
1132 }
1133 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1134 es->lock_count++;
1135 }
1136
1137
1138 /*********************************************************************
1139 *
1140 * EDIT_UnlockBuffer
1141 *
1142 */
1143 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1144 {
1145
1146 /* Edit window might be already destroyed */
1147 if(!IsWindow(es->hwndSelf))
1148 {
1149 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1150 return;
1151 }
1152
1153 if (!es->lock_count) {
1154 ERR("lock_count == 0 ... please report\n");
1155 return;
1156 }
1157 if (!es->text) {
1158 ERR("es->text == 0 ... please report\n");
1159 return;
1160 }
1161
1162 if (force || (es->lock_count == 1)) {
1163 if (es->hloc32W) {
1164 UINT countA = 0;
1165 UINT countW = get_text_length(es) + 1;
1166
1167 if(es->hloc32A)
1168 {
1169 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1170 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1171 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1172 countA = LocalSize(es->hloc32A);
1173 if(countA_new > countA)
1174 {
1175 HLOCAL hloc32A_new;
1176 UINT alloc_size = ROUND_TO_GROW(countA_new);
1177 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1178 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1179 if(hloc32A_new)
1180 {
1181 es->hloc32A = hloc32A_new;
1182 countA = LocalSize(hloc32A_new);
1183 TRACE("Real new size %d bytes\n", countA);
1184 }
1185 else
1186 WARN("FAILED! Will synchronize partially\n");
1187 }
1188 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1189 LocalLock(es->hloc32A), countA, NULL, NULL);
1190 LocalUnlock(es->hloc32A);
1191 }
1192
1193 LocalUnlock(es->hloc32W);
1194 es->text = NULL;
1195 }
1196 else {
1197 ERR("no buffer ... please report\n");
1198 return;
1199 }
1200 }
1201 es->lock_count--;
1202 }
1203
1204
1205 /*********************************************************************
1206 *
1207 * EDIT_MakeFit
1208 *
1209 * Try to fit size + 1 characters in the buffer.
1210 */
1211 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1212 {
1213 HLOCAL hNew32W;
1214
1215 if (size <= es->buffer_size)
1216 return TRUE;
1217
1218 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1219
1220 /* Force edit to unlock it's buffer. es->text now NULL */
1221 EDIT_UnlockBuffer(es, TRUE);
1222
1223 if (es->hloc32W) {
1224 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1225 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1226 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1227 es->hloc32W = hNew32W;
1228 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1229 }
1230 }
1231
1232 EDIT_LockBuffer(es);
1233
1234 if (es->buffer_size < size) {
1235 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1236 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1237 return FALSE;
1238 } else {
1239 TRACE("We now have %d+1\n", es->buffer_size);
1240 return TRUE;
1241 }
1242 }
1243
1244
1245 /*********************************************************************
1246 *
1247 * EDIT_MakeUndoFit
1248 *
1249 * Try to fit size + 1 bytes in the undo buffer.
1250 *
1251 */
1252 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1253 {
1254 UINT alloc_size;
1255
1256 if (size <= es->undo_buffer_size)
1257 return TRUE;
1258
1259 TRACE("trying to ReAlloc to %d+1\n", size);
1260
1261 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1262 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1263 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1264 return TRUE;
1265 }
1266 else
1267 {
1268 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1269 return FALSE;
1270 }
1271 }
1272
1273
1274 /*********************************************************************
1275 *
1276 * EDIT_UpdateTextRegion
1277 *
1278 */
1279 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1280 {
1281 if (es->flags & EF_UPDATE) {
1282 es->flags &= ~EF_UPDATE;
1283 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1284 }
1285 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1286 }
1287
1288
1289 /*********************************************************************
1290 *
1291 * EDIT_UpdateText
1292 *
1293 */
1294 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1295 {
1296 if (es->flags & EF_UPDATE) {
1297 es->flags &= ~EF_UPDATE;
1298 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1299 }
1300 InvalidateRect(es->hwndSelf, rc, bErase);
1301 }
1302
1303 /*********************************************************************
1304 *
1305 * EDIT_SL_InvalidateText
1306 *
1307 * Called from EDIT_InvalidateText().
1308 * Does the job for single-line controls only.
1309 *
1310 */
1311 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1312 {
1313 RECT line_rect;
1314 RECT rc;
1315
1316 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1317 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1318 EDIT_UpdateText(es, &rc, TRUE);
1319 }
1320
1321
1322 static inline INT get_vertical_line_count(EDITSTATE *es)
1323 {
1324 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1325 return max(1,vlc);
1326 }
1327
1328 /*********************************************************************
1329 *
1330 * EDIT_ML_InvalidateText
1331 *
1332 * Called from EDIT_InvalidateText().
1333 * Does the job for multi-line controls only.
1334 *
1335 */
1336 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1337 {
1338 INT vlc = get_vertical_line_count(es);
1339 INT sl = EDIT_EM_LineFromChar(es, start);
1340 INT el = EDIT_EM_LineFromChar(es, end);
1341 INT sc;
1342 INT ec;
1343 RECT rc1;
1344 RECT rcWnd;
1345 RECT rcLine;
1346 RECT rcUpdate;
1347 INT l;
1348
1349 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1350 return;
1351
1352 sc = start - EDIT_EM_LineIndex(es, sl);
1353 ec = end - EDIT_EM_LineIndex(es, el);
1354 if (sl < es->y_offset) {
1355 sl = es->y_offset;
1356 sc = 0;
1357 }
1358 if (el > es->y_offset + vlc) {
1359 el = es->y_offset + vlc;
1360 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1361 }
1362 GetClientRect(es->hwndSelf, &rc1);
1363 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1364 if (sl == el) {
1365 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1366 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1367 EDIT_UpdateText(es, &rcUpdate, TRUE);
1368 } else {
1369 EDIT_GetLineRect(es, sl, sc,
1370 EDIT_EM_LineLength(es,
1371 EDIT_EM_LineIndex(es, sl)),
1372 &rcLine);
1373 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1374 EDIT_UpdateText(es, &rcUpdate, TRUE);
1375 for (l = sl + 1 ; l < el ; l++) {
1376 EDIT_GetLineRect(es, l, 0,
1377 EDIT_EM_LineLength(es,
1378 EDIT_EM_LineIndex(es, l)),
1379 &rcLine);
1380 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1381 EDIT_UpdateText(es, &rcUpdate, TRUE);
1382 }
1383 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1384 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1385 EDIT_UpdateText(es, &rcUpdate, TRUE);
1386 }
1387 }
1388
1389
1390 /*********************************************************************
1391 *
1392 * EDIT_InvalidateText
1393 *
1394 * Invalidate the text from offset start up to, but not including,
1395 * offset end. Useful for (re)painting the selection.
1396 * Regions outside the linewidth are not invalidated.
1397 * end == -1 means end == TextLength.
1398 * start and end need not be ordered.
1399 *
1400 */
1401 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1402 {
1403 if (end == start)
1404 return;
1405
1406 if (end == -1)
1407 end = get_text_length(es);
1408
1409 if (end < start) {
1410 INT tmp = start;
1411 start = end;
1412 end = tmp;
1413 }
1414
1415 if (es->style & ES_MULTILINE)
1416 EDIT_ML_InvalidateText(es, start, end);
1417 else
1418 EDIT_SL_InvalidateText(es, start, end);
1419 }
1420
1421
1422 /*********************************************************************
1423 *
1424 * EDIT_EM_SetSel
1425 *
1426 * note: unlike the specs say: the order of start and end
1427 * _is_ preserved in Windows. (i.e. start can be > end)
1428 * In other words: this handler is OK
1429 *
1430 */
1431 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1432 {
1433 UINT old_start = es->selection_start;
1434 UINT old_end = es->selection_end;
1435 UINT len = get_text_length(es);
1436
1437 if (start == (UINT)-1) {
1438 start = es->selection_end;
1439 end = es->selection_end;
1440 } else {
1441 start = min(start, len);
1442 end = min(end, len);
1443 }
1444 es->selection_start = start;
1445 es->selection_end = end;
1446 if (after_wrap)
1447 es->flags |= EF_AFTER_WRAP;
1448 else
1449 es->flags &= ~EF_AFTER_WRAP;
1450 /* Compute the necessary invalidation region. */
1451 /* Note that we don't need to invalidate regions which have
1452 * "never" been selected, or those which are "still" selected.
1453 * In fact, every time we hit a selection boundary, we can
1454 * *toggle* whether we need to invalidate. Thus we can optimize by
1455 * *sorting* the interval endpoints. Let's assume that we sort them
1456 * in this order:
1457 * start <= end <= old_start <= old_end
1458 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1459 * in 5 comparisons; i.e. it is impossible to do better than the
1460 * following: */
1461 ORDER_UINT(end, old_end);
1462 ORDER_UINT(start, old_start);
1463 ORDER_UINT(old_start, old_end);
1464 ORDER_UINT(start, end);
1465 /* Note that at this point 'end' and 'old_start' are not in order, but
1466 * start is definitely the min. and old_end is definitely the max. */
1467 if (end != old_start)
1468 {
1469 /*
1470 * One can also do
1471 * ORDER_UINT32(end, old_start);
1472 * EDIT_InvalidateText(es, start, end);
1473 * EDIT_InvalidateText(es, old_start, old_end);
1474 * in place of the following if statement.
1475 * (That would complete the optimal five-comparison four-element sort.)
1476 */
1477 if (old_start > end )
1478 {
1479 EDIT_InvalidateText(es, start, end);
1480 EDIT_InvalidateText(es, old_start, old_end);
1481 }
1482 else
1483 {
1484 EDIT_InvalidateText(es, start, old_start);
1485 EDIT_InvalidateText(es, end, old_end);
1486 }
1487 }
1488 else EDIT_InvalidateText(es, start, old_end);
1489 }
1490
1491
1492 /*********************************************************************
1493 *
1494 * EDIT_UpdateScrollInfo
1495 *
1496 */
1497 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1498 {
1499 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1500 {
1501 SCROLLINFO si;
1502 si.cbSize = sizeof(SCROLLINFO);
1503 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1504 si.nMin = 0;
1505 si.nMax = es->line_count - 1;
1506 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1507 si.nPos = es->y_offset;
1508 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1509 si.nMin, si.nMax, si.nPage, si.nPos);
1510 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1511 }
1512
1513 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1514 {
1515 SCROLLINFO si;
1516 si.cbSize = sizeof(SCROLLINFO);
1517 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1518 si.nMin = 0;
1519 si.nMax = es->text_width - 1;
1520 si.nPage = es->format_rect.right - es->format_rect.left;
1521 si.nPos = es->x_offset;
1522 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1523 si.nMin, si.nMax, si.nPage, si.nPos);
1524 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1525 }
1526 }
1527
1528
1529 /*********************************************************************
1530 *
1531 * EDIT_EM_LineScroll_internal
1532 *
1533 * Version of EDIT_EM_LineScroll for internal use.
1534 * It doesn't refuse if ES_MULTILINE is set and assumes that
1535 * dx is in pixels, dy - in lines.
1536 *
1537 */
1538 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1539 {
1540 INT nyoff;
1541 INT x_offset_in_pixels;
1542 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1543 es->line_height;
1544
1545 if (es->style & ES_MULTILINE)
1546 {
1547 x_offset_in_pixels = es->x_offset;
1548 }
1549 else
1550 {
1551 dy = 0;
1552 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1553 }
1554
1555 if (-dx > x_offset_in_pixels)
1556 dx = -x_offset_in_pixels;
1557 if (dx > es->text_width - x_offset_in_pixels)
1558 dx = es->text_width - x_offset_in_pixels;
1559 nyoff = max(0, es->y_offset + dy);
1560 if (nyoff >= es->line_count - lines_per_page)
1561 nyoff = max(0, es->line_count - lines_per_page);
1562 dy = (es->y_offset - nyoff) * es->line_height;
1563 if (dx || dy) {
1564 RECT rc1;
1565 RECT rc;
1566
1567 es->y_offset = nyoff;
1568 if(es->style & ES_MULTILINE)
1569 es->x_offset += dx;
1570 else
1571 es->x_offset += dx / es->char_width;
1572
1573 GetClientRect(es->hwndSelf, &rc1);
1574 IntersectRect(&rc, &rc1, &es->format_rect);
1575 ScrollWindowEx(es->hwndSelf, -dx, dy,
1576 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1577 /* force scroll info update */
1578 EDIT_UpdateScrollInfo(es);
1579 }
1580 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1581 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1582 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1583 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1584 return TRUE;
1585 }
1586
1587 /*********************************************************************
1588 *
1589 * EM_LINESCROLL
1590 *
1591 * NOTE: dx is in average character widths, dy - in lines;
1592 *
1593 */
1594 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1595 {
1596 if (!(es->style & ES_MULTILINE))
1597 return FALSE;
1598
1599 dx *= es->char_width;
1600 return EDIT_EM_LineScroll_internal(es, dx, dy);
1601 }
1602
1603
1604 /*********************************************************************
1605 *
1606 * EM_SCROLL
1607 *
1608 */
1609 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1610 {
1611 INT dy;
1612
1613 if (!(es->style & ES_MULTILINE))
1614 return (LRESULT)FALSE;
1615
1616 dy = 0;
1617
1618 switch (action) {
1619 case SB_LINEUP:
1620 if (es->y_offset)
1621 dy = -1;
1622 break;
1623 case SB_LINEDOWN:
1624 if (es->y_offset < es->line_count - 1)
1625 dy = 1;
1626 break;
1627 case SB_PAGEUP:
1628 if (es->y_offset)
1629 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1630 break;
1631 case SB_PAGEDOWN:
1632 if (es->y_offset < es->line_count - 1)
1633 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1634 break;
1635 default:
1636 return (LRESULT)FALSE;
1637 }
1638 if (dy) {
1639 INT vlc = get_vertical_line_count(es);
1640 /* check if we are going to move too far */
1641 if(es->y_offset + dy > es->line_count - vlc)
1642 dy = max(es->line_count - vlc, 0) - es->y_offset;
1643
1644 /* Notification is done in EDIT_EM_LineScroll */
1645 if(dy) {
1646 EDIT_EM_LineScroll(es, 0, dy);
1647 return MAKELONG(dy, TRUE);
1648 }
1649
1650 }
1651 return (LRESULT)FALSE;
1652 }
1653
1654
1655 /*********************************************************************
1656 *
1657 * EDIT_SetCaretPos
1658 *
1659 */
1660 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1661 BOOL after_wrap)
1662 {
1663 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1664 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1665 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1666 }
1667
1668
1669 /*********************************************************************
1670 *
1671 * EM_SCROLLCARET
1672 *
1673 */
1674 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1675 {
1676 if (es->style & ES_MULTILINE) {
1677 INT l;
1678 INT vlc;
1679 INT ww;
1680 INT cw = es->char_width;
1681 INT x;
1682 INT dy = 0;
1683 INT dx = 0;
1684
1685 l = EDIT_EM_LineFromChar(es, es->selection_end);
1686 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1687 vlc = get_vertical_line_count(es);
1688 if (l >= es->y_offset + vlc)
1689 dy = l - vlc + 1 - es->y_offset;
1690 if (l < es->y_offset)
1691 dy = l - es->y_offset;
1692 ww = es->format_rect.right - es->format_rect.left;
1693 if (x < es->format_rect.left)
1694 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1695 if (x > es->format_rect.right)
1696 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1697 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1698 {
1699 /* check if we are going to move too far */
1700 if(es->x_offset + dx + ww > es->text_width)
1701 dx = es->text_width - ww - es->x_offset;
1702 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1703 EDIT_EM_LineScroll_internal(es, dx, dy);
1704 }
1705 } else {
1706 INT x;
1707 INT goal;
1708 INT format_width;
1709
1710 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1711 format_width = es->format_rect.right - es->format_rect.left;
1712 if (x < es->format_rect.left) {
1713 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1714 do {
1715 es->x_offset--;
1716 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1717 } while ((x < goal) && es->x_offset);
1718 /* FIXME: use ScrollWindow() somehow to improve performance */
1719 EDIT_UpdateText(es, NULL, TRUE);
1720 } else if (x > es->format_rect.right) {
1721 INT x_last;
1722 INT len = get_text_length(es);
1723 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1724 do {
1725 es->x_offset++;
1726 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1727 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1728 } while ((x > goal) && (x_last > es->format_rect.right));
1729 /* FIXME: use ScrollWindow() somehow to improve performance */
1730 EDIT_UpdateText(es, NULL, TRUE);
1731 }
1732 }
1733
1734 if(es->flags & EF_FOCUSED)
1735 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1736 }
1737
1738
1739 /*********************************************************************
1740 *
1741 * EDIT_MoveBackward
1742 *
1743 */
1744 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1745 {
1746 INT e = es->selection_end;
1747
1748 if (e) {
1749 e--;
1750 if ((es->style & ES_MULTILINE) && e &&
1751 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1752 e--;
1753 if (e && (es->text[e - 1] == '\r'))
1754 e--;
1755 }
1756 }
1757 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1758 EDIT_EM_ScrollCaret(es);
1759 }
1760
1761
1762 /*********************************************************************
1763 *
1764 * EDIT_MoveDown_ML
1765 *
1766 * Only for multi line controls
1767 * Move the caret one line down, on a column with the nearest
1768 * x coordinate on the screen (might be a different column).
1769 *
1770 */
1771 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1772 {
1773 INT s = es->selection_start;
1774 INT e = es->selection_end;
1775 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1776 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1777 INT x = (short)LOWORD(pos);
1778 INT y = (short)HIWORD(pos);
1779
1780 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1781 if (!extend)
1782 s = e;
1783 EDIT_EM_SetSel(es, s, e, after_wrap);
1784 EDIT_EM_ScrollCaret(es);
1785 }
1786
1787
1788 /*********************************************************************
1789 *
1790 * EDIT_MoveEnd
1791 *
1792 */
1793 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1794 {
1795 BOOL after_wrap = FALSE;
1796 INT e;
1797
1798 /* Pass a high value in x to make sure of receiving the end of the line */
1799 if (!ctrl && (es->style & ES_MULTILINE))
1800 e = EDIT_CharFromPos(es, 0x3fffffff,
1801 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1802 else
1803 e = get_text_length(es);
1804 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
1805 EDIT_EM_ScrollCaret(es);
1806 }
1807
1808
1809 /*********************************************************************
1810 *
1811 * EDIT_MoveForward
1812 *
1813 */
1814 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
1815 {
1816 INT e = es->selection_end;
1817
1818 if (es->text[e]) {
1819 e++;
1820 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1821 if (es->text[e] == '\n')
1822 e++;
1823 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1824 e += 2;
1825 }
1826 }
1827 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1828 EDIT_EM_ScrollCaret(es);
1829 }
1830
1831
1832 /*********************************************************************
1833 *
1834 * EDIT_MoveHome
1835 *
1836 * Home key: move to beginning of line.
1837 *
1838 */
1839 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
1840 {
1841 INT e;
1842
1843 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1844 if (!ctrl && (es->style & ES_MULTILINE))
1845 e = EDIT_CharFromPos(es, -es->x_offset,
1846 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1847 else
1848 e = 0;
1849 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1850 EDIT_EM_ScrollCaret(es);
1851 }
1852
1853
1854 /*********************************************************************
1855 *
1856 * EDIT_MovePageDown_ML
1857 *
1858 * Only for multi line controls
1859 * Move the caret one page down, on a column with the nearest
1860 * x coordinate on the screen (might be a different column).
1861 *
1862 */
1863 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
1864 {
1865 INT s = es->selection_start;
1866 INT e = es->selection_end;
1867 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1868 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1869 INT x = (short)LOWORD(pos);
1870 INT y = (short)HIWORD(pos);
1871
1872 e = EDIT_CharFromPos(es, x,
1873 y + (es->format_rect.bottom - es->format_rect.top),
1874 &after_wrap);
1875 if (!extend)
1876 s = e;
1877 EDIT_EM_SetSel(es, s, e, after_wrap);
1878 EDIT_EM_ScrollCaret(es);
1879 }
1880
1881
1882 /*********************************************************************
1883 *
1884 * EDIT_MovePageUp_ML
1885 *
1886 * Only for multi line controls
1887 * Move the caret one page up, on a column with the nearest
1888 * x coordinate on the screen (might be a different column).
1889 *
1890 */
1891 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
1892 {
1893 INT s = es->selection_start;
1894 INT e = es->selection_end;
1895 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1896 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1897 INT x = (short)LOWORD(pos);
1898 INT y = (short)HIWORD(pos);
1899
1900 e = EDIT_CharFromPos(es, x,
1901 y - (es->format_rect.bottom - es->format_rect.top),
1902 &after_wrap);
1903 if (!extend)
1904 s = e;
1905 EDIT_EM_SetSel(es, s, e, after_wrap);
1906 EDIT_EM_ScrollCaret(es);
1907 }
1908
1909
1910 /*********************************************************************
1911 *
1912 * EDIT_MoveUp_ML
1913 *
1914 * Only for multi line controls
1915 * Move the caret one line up, on a column with the nearest
1916 * x coordinate on the screen (might be a different column).
1917 *
1918 */
1919 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
1920 {
1921 INT s = es->selection_start;
1922 INT e = es->selection_end;
1923 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1924 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1925 INT x = (short)LOWORD(pos);
1926 INT y = (short)HIWORD(pos);
1927
1928 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
1929 if (!extend)
1930 s = e;
1931 EDIT_EM_SetSel(es, s, e, after_wrap);
1932 EDIT_EM_ScrollCaret(es);
1933 }
1934
1935
1936 /*********************************************************************
1937 *
1938 * EDIT_MoveWordBackward
1939 *
1940 */
1941 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
1942 {
1943 INT s = es->selection_start;
1944 INT e = es->selection_end;
1945 INT l;
1946 INT ll;
1947 INT li;
1948
1949 l = EDIT_EM_LineFromChar(es, e);
1950 ll = EDIT_EM_LineLength(es, e);
1951 li = EDIT_EM_LineIndex(es, l);
1952 if (e - li == 0) {
1953 if (l) {
1954 li = EDIT_EM_LineIndex(es, l - 1);
1955 e = li + EDIT_EM_LineLength(es, li);
1956 }
1957 } else {
1958 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
1959 }
1960 if (!extend)
1961 s = e;
1962 EDIT_EM_SetSel(es, s, e, FALSE);
1963 EDIT_EM_ScrollCaret(es);
1964 }
1965
1966
1967 /*********************************************************************
1968 *
1969 * EDIT_MoveWordForward
1970 *
1971 */
1972 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
1973 {
1974 INT s = es->selection_start;
1975 INT e = es->selection_end;
1976 INT l;
1977 INT ll;
1978 INT li;
1979
1980 l = EDIT_EM_LineFromChar(es, e);
1981 ll = EDIT_EM_LineLength(es, e);
1982 li = EDIT_EM_LineIndex(es, l);
1983 if (e - li == ll) {
1984 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1985 e = EDIT_EM_LineIndex(es, l + 1);
1986 } else {
1987 e = li + EDIT_CallWordBreakProc(es,
1988 li, e - li + 1, ll, WB_RIGHT);
1989 }
1990 if (!extend)
1991 s = e;
1992 EDIT_EM_SetSel(es, s, e, FALSE);
1993 EDIT_EM_ScrollCaret(es);
1994 }
1995
1996
1997 /*********************************************************************
1998 *
1999 * EDIT_PaintText
2000 *
2001 */
2002 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2003 {
2004 COLORREF BkColor;
2005 COLORREF TextColor;
2006 LOGFONTW underline_font;
2007 HFONT hUnderline = 0;
2008 HFONT old_font = 0;
2009 INT ret;
2010 INT li;
2011 INT BkMode;
2012 SIZE size;
2013
2014 if (!count)
2015 return 0;
2016 BkMode = GetBkMode(dc);
2017 BkColor = GetBkColor(dc);
2018 TextColor = GetTextColor(dc);
2019 if (rev) {
2020 if (es->composition_len == 0)
2021 {
2022 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2023 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2024 SetBkMode( dc, OPAQUE);
2025 }
2026 else
2027 {
2028 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2029 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2030 underline_font.lfUnderline = TRUE;
2031 hUnderline = CreateFontIndirectW(&underline_font);
2032 old_font = SelectObject(dc,hUnderline);
2033 }
2034 }
2035 li = EDIT_EM_LineIndex(es, line);
2036 if (es->style & ES_MULTILINE) {
2037 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2038 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2039 } else {
2040 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2041 TextOutW(dc, x, y, text + li + col, count);
2042 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2043 ret = size.cx;
2044 if (es->style & ES_PASSWORD)
2045 HeapFree(GetProcessHeap(), 0, text);
2046 }
2047 if (rev) {
2048 if (es->composition_len == 0)
2049 {
2050 SetBkColor(dc, BkColor);
2051 SetTextColor(dc, TextColor);
2052 SetBkMode( dc, BkMode);
2053 }
2054 else
2055 {
2056 if (old_font)
2057 SelectObject(dc,old_font);
2058 if (hUnderline)
2059 DeleteObject(hUnderline);
2060 }
2061 }
2062 return ret;
2063 }
2064
2065
2066 /*********************************************************************
2067 *
2068 * EDIT_PaintLine
2069 *
2070 */
2071 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2072 {
2073 INT s = es->selection_start;
2074 INT e = es->selection_end;
2075 INT li;
2076 INT ll;
2077 INT x;
2078 INT y;
2079 LRESULT pos;
2080
2081 if (es->style & ES_MULTILINE) {
2082 INT vlc = get_vertical_line_count(es);
2083
2084 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2085 return;
2086 } else if (line)
2087 return;
2088
2089 TRACE("line=%d\n", line);
2090
2091 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2092 x = (short)LOWORD(pos);
2093 y = (short)HIWORD(pos);
2094 li = EDIT_EM_LineIndex(es, line);
2095 ll = EDIT_EM_LineLength(es, li);
2096 s = min(es->selection_start, es->selection_end);
2097 e = max(es->selection_start, es->selection_end);
2098 s = min(li + ll, max(li, s));
2099 e = min(li + ll, max(li, e));
2100 if (rev && (s != e) &&
2101 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2102 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2103 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2104 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2105 } else
2106 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2107 }
2108
2109
2110 /*********************************************************************
2111 *
2112 * EDIT_AdjustFormatRect
2113 *
2114 * Adjusts the format rectangle for the current font and the
2115 * current client rectangle.
2116 *
2117 */
2118 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2119 {
2120 RECT ClientRect;
2121
2122 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2123 if (es->style & ES_MULTILINE)
2124 {
2125 INT fw, vlc, max_x_offset, max_y_offset;
2126
2127 vlc = get_vertical_line_count(es);
2128 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2129
2130 /* correct es->x_offset */
2131 fw = es->format_rect.right - es->format_rect.left;
2132 max_x_offset = es->text_width - fw;
2133 if(max_x_offset < 0) max_x_offset = 0;
2134 if(es->x_offset > max_x_offset)
2135 es->x_offset = max_x_offset;
2136
2137 /* correct es->y_offset */
2138 max_y_offset = es->line_count - vlc;
2139 if(max_y_offset < 0) max_y_offset = 0;
2140 if(es->y_offset > max_y_offset)
2141 es->y_offset = max_y_offset;
2142
2143 /* force scroll info update */
2144 EDIT_UpdateScrollInfo(es);
2145 }
2146 else
2147 /* Windows doesn't care to fix text placement for SL controls */
2148 es->format_rect.bottom = es->format_rect.top + es->line_height;
2149
2150 /* Always stay within the client area */
2151 GetClientRect(es->hwndSelf, &ClientRect);
2152 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2153
2154 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2155 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2156
2157 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2158 }
2159
2160
2161 /*********************************************************************
2162 *
2163 * EDIT_SetRectNP
2164 *
2165 * note: this is not (exactly) the handler called on EM_SETRECTNP
2166 * it is also used to set the rect of a single line control
2167 *
2168 */
2169 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2170 {
2171 LONG_PTR ExStyle;
2172 INT bw, bh;
2173 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2174
2175 CopyRect(&es->format_rect, rc);
2176
2177 if (ExStyle & WS_EX_CLIENTEDGE) {
2178 es->format_rect.left++;
2179 es->format_rect.right--;
2180
2181 if (es->format_rect.bottom - es->format_rect.top
2182 >= es->line_height + 2)
2183 {
2184 es->format_rect.top++;
2185 es->format_rect.bottom--;
2186 }
2187 }
2188 else if (es->style & WS_BORDER) {
2189 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2190 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2191 es->format_rect.left += bw;
2192 es->format_rect.right -= bw;
2193 if (es->format_rect.bottom - es->format_rect.top
2194 >= es->line_height + 2 * bh)
2195 {
2196 es->format_rect.top += bh;
2197 es->format_rect.bottom -= bh;
2198 }
2199 }
2200
2201 es->format_rect.left += es->left_margin;
2202 es->format_rect.right -= es->right_margin;
2203 EDIT_AdjustFormatRect(es);
2204 }
2205
2206
2207 /*********************************************************************
2208 *
2209 * EM_CHARFROMPOS
2210 *
2211 * returns line number (not index) in high-order word of result.
2212 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2213 * to Richedit, not to the edit control. Original documentation is valid.
2214 * FIXME: do the specs mean to return -1 if outside client area or
2215 * if outside formatting rectangle ???
2216 *
2217 */
2218 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2219 {
2220 POINT pt;
2221 RECT rc;
2222 INT index;
2223
2224 pt.x = x;
2225 pt.y = y;
2226 GetClientRect(es->hwndSelf, &rc);
2227 if (!PtInRect(&rc, pt))
2228 return -1;
2229
2230 index = EDIT_CharFromPos(es, x, y, NULL);
2231 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2232 }
2233
2234
2235 /*********************************************************************
2236 *
2237 * EM_FMTLINES
2238 *
2239 * Enable or disable soft breaks.
2240 *
2241 * This means: insert or remove the soft linebreak character (\r\r\n).
2242 * Take care to check if the text still fits the buffer after insertion.
2243 * If not, notify with EN_ERRSPACE.
2244 *
2245 */
2246 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2247 {
2248 es->flags &= ~EF_USE_SOFTBRK;
2249 if (add_eol) {
2250 es->flags |= EF_USE_SOFTBRK;
2251 FIXME("soft break enabled, not implemented\n");
2252 }
2253 return add_eol;
2254 }
2255
2256
2257 /*********************************************************************
2258 *
2259 * EM_GETHANDLE
2260 *
2261 * Hopefully this won't fire back at us.
2262 * We always start with a fixed buffer in the local heap.
2263 * Despite of the documentation says that the local heap is used
2264 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2265 * buffer on the local heap.
2266 *
2267 */
2268 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2269 {
2270 HLOCAL hLocal;
2271
2272 if (!(es->style & ES_MULTILINE))
2273 return 0;
2274
2275 if(es->is_unicode)
2276 hLocal = es->hloc32W;
2277 else
2278 {
2279 if(!es->hloc32A)
2280 {
2281 CHAR *textA;
2282 UINT countA, alloc_size;
2283 TRACE("Allocating 32-bit ANSI alias buffer\n");
2284 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2285 alloc_size = ROUND_TO_GROW(countA);
2286 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2287 {
2288 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2289 return 0;
2290 }
2291 textA = LocalLock(es->hloc32A);
2292 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2293 LocalUnlock(es->hloc32A);
2294 }
2295 hLocal = es->hloc32A;
2296 }
2297
2298 es->flags |= EF_APP_HAS_HANDLE;
2299 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2300 return hLocal;
2301 }
2302
2303
2304 /*********************************************************************
2305 *
2306 * EM_GETLINE
2307 *
2308 */
2309 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2310 {
2311 LPWSTR src;
2312 INT line_len, dst_len;
2313 INT i;
2314
2315 if (es->style & ES_MULTILINE) {
2316 if (line >= es->line_count)
2317 return 0;
2318 } else
2319 line = 0;
2320 i = EDIT_EM_LineIndex(es, line);
2321 src = es->text + i;
2322 line_len = EDIT_EM_LineLength(es, i);
2323 dst_len = *(WORD *)dst;
2324 if(unicode)
2325 {
2326 if(dst_len <= line_len)
2327 {
2328 memcpy(dst, src, dst_len * sizeof(WCHAR));
2329 return dst_len;
2330 }
2331 else /* Append 0 if enough space */
2332 {
2333 memcpy(dst, src, line_len * sizeof(WCHAR));
2334 dst[line_len] = 0;
2335 return line_len;
2336 }
2337 }
2338 else
2339 {
2340 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2341 if(!ret && line_len) /* Insufficient buffer size */
2342 return dst_len;
2343 if(ret < dst_len) /* Append 0 if enough space */
2344 ((LPSTR)dst)[ret] = 0;
2345 return ret;
2346 }
2347 }
2348
2349
2350 /*********************************************************************
2351 *
2352 * EM_GETSEL
2353 *
2354 */
2355 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2356 {
2357 UINT s = es->selection_start;
2358 UINT e = es->selection_end;
2359
2360 ORDER_UINT(s, e);
2361 if (start)
2362 *start = s;
2363 if (end)
2364 *end = e;
2365 return MAKELONG(s, e);
2366 }
2367
2368
2369 /*********************************************************************
2370 *
2371 * EM_REPLACESEL
2372 *
2373 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2374 *
2375 */
2376 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2377 {
2378 UINT strl = strlenW(lpsz_replace);
2379 UINT tl = get_text_length(es);
2380 UINT utl;
2381 UINT s;
2382 UINT e;
2383 UINT i;
2384 UINT size;
2385 LPWSTR p;
2386 HRGN hrgn = 0;
2387 LPWSTR buf = NULL;
2388 UINT bufl = 0;
2389
2390 TRACE("%s, can_undo %d, send_update %d\n",
2391 debugstr_w(lpsz_replace), can_undo, send_update);
2392
2393 s = es->selection_start;
2394 e = es->selection_end;
2395
2396 if ((s == e) && !strl)
2397 return;
2398
2399 ORDER_UINT(s, e);
2400
2401 size = tl - (e - s) + strl;
2402 if (!size)
2403 es->text_width = 0;
2404
2405 /* Issue the EN_MAXTEXT notification and continue with replacing text
2406 * such that buffer limit is honored. */
2407 if ((honor_limit) && (size > es->buffer_limit)) {
2408 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2409 /* Buffer limit can be smaller than the actual length of text in combobox */
2410 if (es->buffer_limit < (tl - (e-s)))
2411 strl = 0;
2412 else
2413 strl = es->buffer_limit - (tl - (e-s));
2414 }
2415
2416 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2417 return;
2418
2419 if (e != s) {
2420 /* there is something to be deleted */
2421 TRACE("deleting stuff.\n");
2422 bufl = e - s;
2423 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2424 if (!buf) return;
2425 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2426 buf[bufl] = 0; /* ensure 0 termination */
2427 /* now delete */
2428 strcpyW(es->text + s, es->text + e);
2429 text_buffer_changed(es);
2430 }
2431 if (strl) {
2432 /* there is an insertion */
2433 tl = get_text_length(es);
2434 TRACE("inserting stuff (tl %d, strl %d, selstart %d (%s), text %s)\n", tl, strl, s, debugstr_w(es->text + s), debugstr_w(es->text));
2435 for (p = es->text + tl ; p >= es->text + s ; p--)
2436 p[strl] = p[0];
2437 for (i = 0 , p = es->text + s ; i < strl ; i++)
2438 p[i] = lpsz_replace[i];
2439 if(es->style & ES_UPPERCASE)
2440 CharUpperBuffW(p, strl);
2441 else if(es->style & ES_LOWERCASE)
2442 CharLowerBuffW(p, strl);
2443 text_buffer_changed(es);
2444 }
2445 if (es->style & ES_MULTILINE)
2446 {
2447 INT st = min(es->selection_start, es->selection_end);
2448 INT vlc = get_vertical_line_count(es);
2449
2450 hrgn = CreateRectRgn(0, 0, 0, 0);
2451 EDIT_BuildLineDefs_ML(es, st, st + strl,
2452 strl - abs(es->selection_end - es->selection_start), hrgn);
2453 /* if text is too long undo all changes */
2454 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2455 if (strl)
2456 strcpyW(es->text + e, es->text + e + strl);
2457 if (e != s)
2458 for (i = 0 , p = es->text ; i < e - s ; i++)
2459 p[i + s] = buf[i];
2460 text_buffer_changed(es);
2461 EDIT_BuildLineDefs_ML(es, s, e,
2462 abs(es->selection_end - es->selection_start) - strl, hrgn);
2463 strl = 0;
2464 e = s;
2465 hrgn = CreateRectRgn(0, 0, 0, 0);
2466 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2467 }
2468 }
2469 else {
2470 INT fw = es->format_rect.right - es->format_rect.left;
2471 EDIT_CalcLineWidth_SL(es);
2472 /* remove chars that don't fit */
2473 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2474 while ((es->text_width > fw) && s + strl >= s) {
2475 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2476 strl--;
2477 EDIT_CalcLineWidth_SL(es);
2478 }
2479 text_buffer_changed(es);
2480 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2481 }
2482 }
2483
2484 if (e != s) {
2485 if (can_undo) {
2486 utl = strlenW(es->undo_text);
2487 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2488 /* undo-buffer is extended to the right */
2489 EDIT_MakeUndoFit(es, utl + e - s);
2490 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2491 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2492 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2493 /* undo-buffer is extended to the left */
2494 EDIT_MakeUndoFit(es, utl + e - s);
2495 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2496 p[e - s] = p[0];
2497 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2498 p[i] = buf[i];
2499 es->undo_position = s;
2500 } else {
2501 /* new undo-buffer */
2502 EDIT_MakeUndoFit(es, e - s);
2503 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2504 es->undo_text[e - s] = 0; /* ensure 0 termination */
2505 es->undo_position = s;
2506 }
2507 /* any deletion makes the old insertion-undo invalid */
2508 es->undo_insert_count = 0;
2509 } else
2510 EDIT_EM_EmptyUndoBuffer(es);
2511 }
2512 if (strl) {
2513 if (can_undo) {
2514 if ((s == es->undo_position) ||
2515 ((es->undo_insert_count) &&
2516 (s == es->undo_position + es->undo_insert_count)))
2517 /*
2518 * insertion is new and at delete position or
2519 * an extension to either left or right
2520 */
2521 es->undo_insert_count += strl;
2522 else {
2523 /* new insertion undo */
2524 es->undo_position = s;
2525 es->undo_insert_count = strl;
2526 /* new insertion makes old delete-buffer invalid */
2527 *es->undo_text = '\0';
2528 }
2529 } else
2530 EDIT_EM_EmptyUndoBuffer(es);
2531 }
2532
2533 if (bufl)
2534 HeapFree(GetProcessHeap(), 0, buf);
2535
2536 s += strl;
2537
2538 /* If text has been deleted and we're right or center aligned then scroll rightward */
2539 if (es->style & (ES_RIGHT | ES_CENTER))
2540 {
2541 INT delta = strl - abs(es->selection_end - es->selection_start);
2542
2543 if (delta < 0 && es->x_offset)
2544 {
2545 if (abs(delta) > es->x_offset)
2546 es->x_offset = 0;
2547 else
2548 es->x_offset += delta;
2549 }
2550 }
2551
2552 EDIT_EM_SetSel(es, s, s, FALSE);
2553 es->flags |= EF_MODIFIED;
2554 if (send_update) es->flags |= EF_UPDATE;
2555 if (hrgn)
2556 {
2557 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2558 DeleteObject(hrgn);
2559 }
2560 else
2561 EDIT_UpdateText(es, NULL, TRUE);
2562
2563 EDIT_EM_ScrollCaret(es);
2564
2565 /* force scroll info update */
2566 EDIT_UpdateScrollInfo(es);
2567
2568
2569 if(send_update || (es->flags & EF_UPDATE))
2570 {
2571 es->flags &= ~EF_UPDATE;
2572 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2573 }
2574 }
2575
2576
2577 /*********************************************************************
2578 *
2579 * EM_SETHANDLE
2580 *
2581 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2582 *
2583 */
2584 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2585 {
2586 if (!(es->style & ES_MULTILINE))
2587 return;
2588
2589 if (!hloc) {
2590 WARN("called with NULL handle\n");
2591 return;
2592 }
2593
2594 EDIT_UnlockBuffer(es, TRUE);
2595
2596 if(es->is_unicode)
2597 {
2598 if(es->hloc32A)
2599 {
2600 LocalFree(es->hloc32A);
2601 es->hloc32A = NULL;
2602 }
2603 es->hloc32W = hloc;
2604 }
2605 else
2606 {
2607 INT countW, countA;
2608 HLOCAL hloc32W_new;
2609 WCHAR *textW;
2610 CHAR *textA;
2611
2612 countA = LocalSize(hloc);
2613 textA = LocalLock(hloc);
2614 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2615 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2616 {
2617 ERR("Could not allocate new unicode buffer\n");
2618 return;
2619 }
2620 textW = LocalLock(hloc32W_new);
2621 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2622 LocalUnlock(hloc32W_new);
2623 LocalUnlock(hloc);
2624
2625 if(es->hloc32W)
2626 LocalFree(es->hloc32W);
2627
2628 es->hloc32W = hloc32W_new;
2629 es->hloc32A = hloc;
2630 }
2631
2632 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2633
2634 es->flags |= EF_APP_HAS_HANDLE;
2635 EDIT_LockBuffer(es);
2636
2637 es->x_offset = es->y_offset = 0;
2638 es->selection_start = es->selection_end = 0;
2639 EDIT_EM_EmptyUndoBuffer(es);
2640 es->flags &= ~EF_MODIFIED;
2641 es->flags &= ~EF_UPDATE;
2642 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2643 EDIT_UpdateText(es, NULL, TRUE);
2644 EDIT_EM_ScrollCaret(es);
2645 /* force scroll info update */
2646 EDIT_UpdateScrollInfo(es);
2647 }
2648
2649
2650 /*********************************************************************
2651 *
2652 * EM_SETLIMITTEXT
2653 *
2654 * NOTE: this version currently implements WinNT limits
2655 *
2656 */
2657 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2658 {
2659 if (!limit) limit = ~0u;
2660 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2661 es->buffer_limit = limit;
2662 }
2663
2664
2665 /*********************************************************************
2666 *
2667 * EM_SETMARGINS
2668 *
2669 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2670 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2671 * margin according to the textmetrics of the current font.
2672 *
2673 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2674 * of the char's width as the margin, but this is not how Windows handles this.
2675 * For all other fonts Windows sets the margins to zero.
2676 *
2677 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2678 * edit control is equal to or larger than a certain size.
2679 * Interestingly if one subtracts both the left and right margins from
2680 * this size one always seems to get an even number. The extents of
2681 * the (four character) string "'**'" match this quite closely, so
2682 * we'll use this until we come up with a better idea.
2683 */
2684 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2685 {
2686 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2687 SIZE sz;
2688
2689 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2690 return sz.cx + left + right;
2691 }
2692
2693 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2694 WORD left, WORD right, BOOL repaint)
2695 {
2696 TEXTMETRICW tm;
2697 INT default_left_margin = 0; /* in pixels */
2698 INT default_right_margin = 0; /* in pixels */
2699
2700 /* Set the default margins depending on the font */
2701 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2702 HDC dc = GetDC(es->hwndSelf);
2703 HFONT old_font = SelectObject(dc, es->font);
2704 GetTextMetricsW(dc, &tm);
2705 /* The default margins are only non zero for TrueType or Vector fonts */
2706 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2707 int min_size;
2708 RECT rc;
2709 /* This must be calculated more exactly! But how? */
2710 default_left_margin = tm.tmAveCharWidth / 2;
2711 default_right_margin = tm.tmAveCharWidth / 2;
2712 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2713 GetClientRect(es->hwndSelf, &rc);
2714 if(rc.right - rc.left < min_size) {
2715 default_left_margin = es->left_margin;
2716 default_right_margin = es->right_margin;
2717 }
2718 }
2719 SelectObject(dc, old_font);
2720 ReleaseDC(es->hwndSelf, dc);
2721 }
2722
2723 if (action & EC_LEFTMARGIN) {
2724 es->format_rect.left -= es->left_margin;
2725 if (left != EC_USEFONTINFO)
2726 es->left_margin = left;
2727 else
2728 es->left_margin = default_left_margin;
2729 es->format_rect.left += es->left_margin;
2730 }
2731
2732 if (action & EC_RIGHTMARGIN) {
2733 es->format_rect.right += es->right_margin;
2734 if (right != EC_USEFONTINFO)
2735 es->right_margin = right;
2736 else
2737 es->right_margin = default_right_margin;
2738 es->format_rect.right -= es->right_margin;
2739 }
2740
2741 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2742 EDIT_AdjustFormatRect(es);
2743 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2744 }
2745
2746 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2747 }
2748
2749
2750 /*********************************************************************
2751 *
2752 * EM_SETPASSWORDCHAR
2753 *
2754 */
2755 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
2756 {
2757 LONG style;
2758
2759 if (es->style & ES_MULTILINE)
2760 return;
2761
2762 if (es->password_char == c)
2763 return;
2764
2765 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
2766 es->password_char = c;
2767 if (c) {
2768 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
2769 es->style |= ES_PASSWORD;
2770 } else {
2771 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
2772 es->style &= ~ES_PASSWORD;
2773 }
2774 EDIT_UpdateText(es, NULL, TRUE);
2775 }
2776
2777
2778 /*********************************************************************
2779 *
2780 * EM_SETTABSTOPS
2781 *
2782 */
2783 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
2784 {
2785 if (!(es->style & ES_MULTILINE))
2786 return FALSE;
2787 HeapFree(GetProcessHeap(), 0, es->tabs);
2788 es->tabs_count = count;
2789 if (!count)
2790 es->tabs = NULL;
2791 else {
2792 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
2793 if (es->tabs == NULL) // reactos r33503
2794 {
2795 es->tabs_count = 0;
2796 return FALSE;
2797 } // reactos r33503
2798 memcpy(es->tabs, tabs, count * sizeof(INT));
2799 }
2800 return TRUE;
2801 }
2802
2803
2804 /*********************************************************************
2805 *
2806 * EM_SETWORDBREAKPROC
2807 *
2808 */
2809 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
2810 {
2811 if (es->word_break_proc == wbp)
2812 return;
2813
2814 es->word_break_proc = wbp;
2815
2816 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2817 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2818 EDIT_UpdateText(es, NULL, TRUE);
2819 }
2820 }
2821
2822
2823 /*********************************************************************
2824 *
2825 * EM_UNDO / WM_UNDO
2826 *
2827 */
2828 static BOOL EDIT_EM_Undo(EDITSTATE *es)
2829 {
2830 INT ulength;
2831 LPWSTR utext;
2832
2833 /* As per MSDN spec, for a single-line edit control,
2834 the return value is always TRUE */
2835 if( es->style & ES_READONLY )
2836 return !(es->style & ES_MULTILINE);
2837
2838 ulength = strlenW(es->undo_text);
2839
2840 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
2841 if (utext == NULL) // reactos r33503
2842 return FALSE; // reactos r33503
2843
2844 strcpyW(utext, es->undo_text);
2845
2846 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2847 es->undo_insert_count, debugstr_w(utext));
2848
2849 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2850 EDIT_EM_EmptyUndoBuffer(es);
2851 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
2852 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2853 /* send the notification after the selection start and end are set */
2854 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2855 EDIT_EM_ScrollCaret(es);
2856 HeapFree(GetProcessHeap(), 0, utext);
2857
2858 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2859 es->undo_insert_count, debugstr_w(es->undo_text));
2860 return TRUE;
2861 }
2862
2863
2864 /* Helper function for WM_CHAR
2865 *
2866 * According to an MSDN blog article titled "Just because you're a control
2867 * doesn't mean that you're necessarily inside a dialog box," multiline edit
2868 * controls without ES_WANTRETURN would attempt to detect whether it is inside
2869 * a dialog box or not.
2870 */
2871 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
2872 {
2873 return (es->flags & EF_DIALOGMODE);
2874 }
2875
2876
2877 /*********************************************************************
2878 *
2879 * WM_PASTE
2880 *
2881 */
2882 static void EDIT_WM_Paste(EDITSTATE *es)
2883 {
2884 HGLOBAL hsrc;
2885 LPWSTR src;
2886
2887 /* Protect read-only edit control from modification */
2888 if(es->style & ES_READONLY)
2889 return;
2890
2891 OpenClipboard(es->hwndSelf);
2892 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
2893 src = GlobalLock(hsrc);
2894 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
2895 GlobalUnlock(hsrc);
2896 }
2897 else if (es->style & ES_PASSWORD) {
2898 /* clear selected text in password edit box even with empty clipboard */
2899 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2900 }
2901 CloseClipboard();
2902 }
2903
2904
2905 /*********************************************************************
2906 *
2907 * WM_COPY
2908 *
2909 */
2910 static void EDIT_WM_Copy(EDITSTATE *es)
2911 {
2912 INT s = min(es->selection_start, es->selection_end);
2913 INT e = max(es->selection_start, es->selection_end);
2914 HGLOBAL hdst;
2915 LPWSTR dst;
2916 DWORD len;
2917
2918 if (e == s) return;
2919
2920 len = e - s;
2921 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
2922 dst = GlobalLock(hdst);
2923 memcpy(dst, es->text + s, len * sizeof(WCHAR));
2924 dst[len] = 0; /* ensure 0 termination */
2925 TRACE("%s\n", debugstr_w(dst));
2926 GlobalUnlock(hdst);
2927 OpenClipboard(es->hwndSelf);
2928 EmptyClipboard();
2929 SetClipboardData(CF_UNICODETEXT, hdst);
2930 CloseClipboard();
2931 }
2932
2933
2934 /*********************************************************************
2935 *
2936 * WM_CLEAR
2937 *
2938 */
2939 static inline void EDIT_WM_Clear(EDITSTATE *es)
2940 {
2941 /* Protect read-only edit control from modification */
2942 if(es->style & ES_READONLY)
2943 return;
2944
2945 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
2946 }
2947
2948
2949 /*********************************************************************
2950 *
2951 * WM_CUT
2952 *
2953 */
2954 static inline void EDIT_WM_Cut(EDITSTATE *es)
2955 {
2956 EDIT_WM_Copy(es);
2957 EDIT_WM_Clear(es);
2958 }
2959
2960
2961 /*********************************************************************
2962 *
2963 * WM_CHAR
2964 *
2965 */
2966 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
2967 {
2968 BOOL control;
2969
2970 control = GetKeyState(VK_CONTROL) & 0x8000;
2971
2972 switch (c) {
2973 case '\r':
2974 /* If it's not a multiline edit box, it would be ignored below.
2975 * For multiline edit without ES_WANTRETURN, we have to make a
2976 * special case.
2977 */
2978 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2979 if (EDIT_IsInsideDialog(es))
2980 break;
2981 case '\n':
2982 if (es->style & ES_MULTILINE) {
2983 if (es->style & ES_READONLY) {
2984 EDIT_MoveHome(es, FALSE, FALSE);
2985 EDIT_MoveDown_ML(es, FALSE);
2986 } else {
2987 static const WCHAR cr_lfW[] = {'\r','\n',0};
2988 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
2989 }
2990 }
2991 break;
2992 case '\t':
2993 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2994 {
2995 static const WCHAR tabW[] = {'\t',0};
2996 if (EDIT_IsInsideDialog(es))
2997 break;
2998 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
2999 }
3000 break;
3001 case VK_BACK:
3002 if (!(es->style & ES_READONLY) && !control) {
3003 if (es->selection_start != es->selection_end)
3004 EDIT_WM_Clear(es);
3005 else {
3006 /* delete character left of caret */
3007 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3008 EDIT_MoveBackward(es, TRUE);
3009 EDIT_WM_Clear(es);
3010 }
3011 }
3012 break;
3013 case 0x03: /* ^C */
3014 if (!(es->style & ES_PASSWORD))
3015 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3016 break;
3017 case 0x16: /* ^V */
3018 if (!(es->style & ES_READONLY))
3019 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3020 break;
3021 case 0x18: /* ^X */
3022 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3023 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3024 break;
3025 case 0x1A: /* ^Z */
3026 if (!(es->style & ES_READONLY))
3027 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3028 break;
3029
3030 default:
3031 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3032 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3033 break;
3034
3035 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3036 WCHAR str[2];
3037 str[0] = c;
3038 str[1] = '\0';
3039 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3040 }
3041 break;
3042 }
3043 return 1;
3044 }
3045
3046 #if 0 // Removed see Revision 43925 comments.
3047 /*********************************************************************
3048 *
3049 * WM_COMMAND
3050 *
3051 */
3052 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
3053 {
3054 if (code || control)
3055 return;
3056
3057 switch (id) {
3058 case EM_UNDO:
3059 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3060 break;
3061 case WM_CUT:
3062 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3063 break;
3064 case WM_COPY:
3065 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3066 break;
3067 case WM_PASTE:
3068 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3069 break;
3070 case WM_CLEAR:
3071 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3072 break;
3073 case EM_SETSEL:
3074 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3075 EDIT_EM_ScrollCaret(es);
3076 break;
3077 default:
3078 ERR("unknown menu item, please report\n");
3079 break;
3080 }
3081 }
3082 #endif
3083
3084
3085 /*********************************************************************
3086 *
3087 * WM_CONTEXTMENU
3088 *
3089 * Note: the resource files resource/sysres_??.rc cannot define a
3090 * single popup menu. Hence we use a (dummy) menubar
3091 * containing the single popup menu as its first item.
3092 *
3093 * FIXME: the message identifiers have been chosen arbitrarily,
3094 * hence we use MF_BYPOSITION.
3095 * We might as well use the "real" values (anybody knows ?)
3096 * The menu definition is in resources/sysres_??.rc.
3097 * Once these are OK, we better use MF_BYCOMMAND here
3098 * (as we do in EDIT_WM_Command()).
3099 *
3100 */
3101 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3102 {
3103 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3104 HMENU popup = GetSubMenu(menu, 0);
3105 UINT start = es->selection_start;
3106 UINT end = es->selection_end;
3107
3108 BOOL selectedItem;
3109 ORDER_UINT(start, end);
3110
3111 /* undo */
3112 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3113 /* cut */
3114 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3115 /* copy */
3116 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3117 /* paste */
3118 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3119 /* delete */
3120 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3121 /* select all */
3122 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3123
3124 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3125 {
3126 RECT rc;
3127 /* Windows places the menu at the edit's center in this case */
3128 GetClientRect(es->hwndSelf, &rc);
3129 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3130 x = rc.left + (rc.right - rc.left) / 2;
3131 y = rc.top + (rc.bottom - rc.top) / 2;
3132 }
3133
3134 if (!(es->flags & EF_FOCUSED))
3135 SetFocus(es->hwndSelf);
3136
3137 #ifdef __REACTOS__
3138 selectedItem = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, es->hwndSelf, NULL);
3139 // Added see Revision 43925 comments.
3140 switch (selectedItem) {
3141 case EM_UNDO:
3142 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3143 break;
3144 case WM_CUT:
3145 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3146 break;
3147 case WM_COPY:
3148 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3149 break;
3150 case WM_PASTE:
3151 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3152 break;
3153 case WM_CLEAR:
3154 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3155 break;
3156 case EM_SETSEL:
3157 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3158 EDIT_EM_ScrollCaret(es);
3159 break;
3160 default:
3161 ERR("unknown menu item, please report\n");
3162 break;
3163 }
3164 #endif
3165 DestroyMenu(menu);
3166 }
3167
3168
3169 /*********************************************************************
3170 *
3171 * WM_GETTEXT
3172 *
3173 */
3174 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3175 {
3176 if(!count) return 0;
3177
3178 if(unicode)
3179 {
3180 lstrcpynW(dst, es->text, count);
3181 return strlenW(dst);
3182 }
3183 else
3184 {
3185 LPSTR textA = (LPSTR)dst;
3186 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3187 textA[count - 1] = 0; /* ensure 0 termination */
3188 return strlen(textA);
3189 }
3190 }
3191
3192 /*********************************************************************
3193 *
3194 * EDIT_CheckCombo
3195 *
3196 */
3197 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3198 {
3199 HWND hLBox = es->hwndListBox;
3200 HWND hCombo;
3201 BOOL bDropped;
3202 int nEUI;
3203
3204 if (!hLBox)
3205 return FALSE;
3206
3207 hCombo = GetParent(es->hwndSelf);
3208 bDropped = TRUE;
3209 nEUI = 0;
3210
3211 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3212
3213 if (key == VK_UP || key == VK_DOWN)
3214 {
3215 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3216 nEUI = 1;
3217
3218 if (msg == WM_KEYDOWN || nEUI)
3219 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3220 }
3221
3222 switch (msg)
3223 {
3224 case WM_KEYDOWN:
3225 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3226 {
3227 /* make sure ComboLBox pops up */
3228 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3229 key = VK_F4;
3230 nEUI = 2;
3231 }
3232
3233 SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3234 break;
3235
3236 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3237 if (nEUI)
3238 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
3239 else
3240 SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3241 break;
3242 }
3243
3244 if(nEUI == 2)
3245 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3246
3247 return TRUE;
3248 }
3249
3250
3251 /*********************************************************************
3252 *
3253 * WM_KEYDOWN
3254 *
3255 * Handling of special keys that don't produce a WM_CHAR
3256 * (i.e. non-printable keys) & Backspace & Delete
3257 *
3258 */
3259 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
3260 {
3261 BOOL shift;
3262 BOOL control;
3263
3264 if (GetKeyState(VK_MENU) & 0x8000)
3265 return 0;
3266
3267 shift = GetKeyState(VK_SHIFT) & 0x8000;
3268 control = GetKeyState(VK_CONTROL) & 0x8000;
3269
3270 switch (key) {
3271 case VK_F4:
3272 case VK_UP:
3273 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3274 break;
3275
3276 /* fall through */
3277 case VK_LEFT:
3278 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3279 EDIT_MoveUp_ML(es, shift);
3280 else
3281 if (control)
3282 EDIT_MoveWordBackward(es, shift);
3283 else
3284 EDIT_MoveBackward(es, shift);
3285 break;
3286 case VK_DOWN:
3287 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
3288 break;
3289 /* fall through */
3290 case VK_RIGHT:
3291 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3292 EDIT_MoveDown_ML(es, shift);
3293 else if (control)
3294 EDIT_MoveWordForward(es, shift);
3295 else
3296 EDIT_MoveForward(es, shift);
3297 break;
3298 case VK_HOME:
3299 EDIT_MoveHome(es, shift, control);
3300 break;
3301 case VK_END:
3302 EDIT_MoveEnd(es, shift, control);
3303 break;
3304 case VK_PRIOR:
3305 if (es->style & ES_MULTILINE)
3306 EDIT_MovePageUp_ML(es, shift);
3307 else
3308 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3309 break;
3310 case VK_NEXT:
3311 if (es->style & ES_MULTILINE)
3312 EDIT_MovePageDown_ML(es, shift);
3313 else
3314 EDIT_CheckCombo(es, WM_KEYDOWN, key);
3315 break;
3316 case VK_DELETE:
3317 if (!(es->style & ES_READONLY) && !(shift && control)) {
3318 if (es->selection_start != es->selection_end) {
3319 if (shift)
3320 EDIT_WM_Cut(es);
3321 else
3322 EDIT_WM_Clear(es);
3323 } else {
3324 if (shift) {
3325 /* delete character left of caret */
3326 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3327 EDIT_MoveBackward(es, TRUE);
3328 EDIT_WM_Clear(es);
3329 } else if (control) {
3330 /* delete to end of line */
3331 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3332 EDIT_MoveEnd(es, TRUE, FALSE);
3333 EDIT_WM_Clear(es);
3334 } else {
3335 /* delete character right of caret */
3336 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3337 EDIT_MoveForward(es, TRUE);
3338 EDIT_WM_Clear(es);
3339 }
3340 }
3341 }
3342 break;
3343 case VK_INSERT:
3344 if (shift) {
3345 if (!(es->style & ES_READONLY))
3346 EDIT_WM_Paste(es);
3347 } else if (control)
3348 EDIT_WM_Copy(es);
3349 break;
3350 case VK_RETURN:
3351 /* If the edit doesn't want the return send a message to the default object */
3352 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3353 {
3354 DWORD dw;
3355
3356 if (!EDIT_IsInsideDialog(es)) break;
3357 if (control) break;
3358 dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3359 if (HIWORD(dw) == DC_HASDEFID)
3360 {
3361 HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3362 if (hwDefCtrl)
3363 {
3364 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3365 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3366 }
3367 }
3368 }
3369 break;
3370 case VK_ESCAPE:
3371 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3372 PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3373 break;
3374 case VK_TAB:
3375 if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3376 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3377 break;
3378 }
3379 return TRUE;
3380 }
3381
3382
3383 /*********************************************************************
3384 *
3385 * WM_KILLFOCUS
3386 *
3387 */
3388 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
3389 {
3390 es->flags &= ~EF_FOCUSED;
3391 DestroyCaret();
3392 if(!(es->style & ES_NOHIDESEL))
3393 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3394 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
3395 return 0;
3396 }
3397
3398
3399 /*********************************************************************
3400 *
3401 * WM_LBUTTONDBLCLK
3402 *
3403 * The caret position has been set on the WM_LBUTTONDOWN message
3404 *
3405 */
3406 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
3407 {
3408 INT s;
3409 INT e = es->selection_end;
3410 INT l;
3411 INT li;
3412 INT ll;
3413
3414 es->bCaptureState = TRUE;
3415 SetCapture(es->hwndSelf);
3416
3417 l = EDIT_EM_LineFromChar(es, e);
3418 li = EDIT_EM_LineIndex(es, l);
3419 ll = EDIT_EM_LineLength(es, e);
3420 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3421 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3422 EDIT_EM_SetSel(es, s, e, FALSE);
3423 EDIT_EM_ScrollCaret(es);
3424 es->region_posx = es->region_posy = 0;
3425 SetTimer(es->hwndSelf, 0, 100, NULL);
3426 return 0;
3427 }
3428
3429
3430 /*********************************************************************
3431 *
3432 * WM_LBUTTONDOWN
3433 *
3434 */
3435 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
3436 {
3437 INT e;
3438 BOOL after_wrap;
3439
3440 es->bCaptureState = TRUE;
3441 SetCapture(es->hwndSelf);
3442 EDIT_ConfinePoint(es, &x, &y);
3443 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3444 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3445 EDIT_EM_ScrollCaret(es);
3446 es->region_posx = es->region_posy = 0;
3447 SetTimer(es->hwndSelf, 0, 100, NULL);
3448
3449 if (!(es->flags & EF_FOCUSED))
3450 SetFocus(es->hwndSelf);
3451
3452 return 0;
3453 }
3454
3455
3456 /*********************************************************************
3457 *
3458 * WM_LBUTTONUP
3459 *
3460 */
3461 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
3462 {
3463 if (es->bCaptureState) {
3464 KillTimer(es->hwndSelf, 0);
3465 if (GetCapture() == es->hwndSelf) ReleaseCapture();
3466 }
3467 es->bCaptureState = FALSE;
3468 return 0;
3469 }
3470
3471
3472 /*********************************************************************
3473 *
3474 * WM_MBUTTONDOWN
3475 *
3476 */
3477 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
3478 {
3479 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3480 return 0;
3481 }
3482
3483
3484 /*********************************************************************
3485 *
3486 * WM_MOUSEMOVE
3487 *
3488 */
3489 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
3490 {
3491 INT e;
3492 BOOL after_wrap;
3493 INT prex, prey;
3494
3495 /* If the mouse has been captured by process other than the edit control itself,
3496 * the windows edit controls will not select the strings with mouse move.
3497 */
3498 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3499 return 0;
3500
3501 /*
3502 * FIXME: gotta do some scrolling if outside client
3503 * area. Maybe reset the timer ?
3504 */
3505 prex = x; prey = y;
3506 EDIT_ConfinePoint(es, &x, &y);
3507 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3508 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3509 e = EDIT_CharFromPos(es, x, y, &after_wrap);
3510 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3511 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3512 return 0;
3513 }
3514
3515
3516 /*********************************************************************
3517 *
3518 * WM_PAINT
3519 *
3520 */
3521 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
3522 {
3523 PAINTSTRUCT ps;
3524 INT i;
3525 HDC dc;
3526 HFONT old_font = 0;
3527 RECT rc;
3528 RECT rcClient;
3529 RECT rcLine;
3530 RECT rcRgn;
3531 HBRUSH brush;
3532 HBRUSH old_brush;
3533 INT bw, bh;
3534 BOOL rev = es->bEnableState &&
3535 ((es->flags & EF_FOCUSED) ||
3536 (es->style & ES_NOHIDESEL));
3537 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3538
3539 GetClientRect(es->hwndSelf, &rcClient);
3540
3541 /* get the background brush */
3542 brush = EDIT_NotifyCtlColor(es, dc);
3543
3544 /* paint the border and the background */
3545 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3546
3547 if(es->style & WS_BORDER) {
3548 bw = GetSystemMetrics(SM_CXBORDER);
3549 bh = GetSystemMetrics(SM_CYBORDER);
3550 rc = rcClient;
3551 if(es->style & ES_MULTILINE) {
3552 if(es->style & WS_HSCROLL) rc.bottom+=bh;
3553 if(es->style & WS_VSCROLL) rc.right+=bw;
3554 }
3555
3556 /* Draw the frame. Same code as in nonclient.c */
3557 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
3558 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3559 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3560 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3561 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3562 SelectObject(dc, old_brush);
3563
3564 /* Keep the border clean */
3565 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3566 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3567 }
3568
3569 GetClipBox(dc, &rc);
3570 FillRect(dc, &rc, brush);
3571
3572 IntersectClipRect(dc, es->format_rect.left,
3573 es->format_rect.top,
3574 es->format_rect.right,
3575 es->format_rect.bottom);
3576 if (es->style & ES_MULTILINE) {
3577 rc = rcClient;
3578 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3579 }
3580 if (es->font)
3581 old_font = SelectObject(dc, es->font);
3582
3583 if (!es->bEnableState)
3584 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3585 GetClipBox(dc, &rcRgn);
3586 if (es->style & ES_MULTILINE) {
3587 INT vlc = get_vertical_line_count(es);
3588 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3589 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3590 if (IntersectRect(&rc, &rcRgn, &rcLine))
3591 EDIT_PaintLine(es, dc, i, rev);
3592 }
3593 } else {
3594 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3595 if (IntersectRect(&rc, &rcRgn, &rcLine))
3596 EDIT_PaintLine(es, dc, 0, rev);
3597 }
3598 if (es->font)
3599 SelectObject(dc, old_font);
3600
3601 if (!hdc)
3602 EndPaint(es->hwndSelf, &ps);
3603 }
3604
3605
3606 /*********************************************************************
3607 *
3608 * WM_SETFOCUS
3609 *
3610 */
3611 static void EDIT_WM_SetFocus(EDITSTATE *es)
3612 {
3613 es->flags |= EF_FOCUSED;
3614
3615 if (!(es->style & ES_NOHIDESEL))
3616 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3617
3618 /* single line edit updates itself */
3619 if (!(es->style & ES_MULTILINE))
3620 {
3621 HDC hdc = GetDC(es->hwndSelf);
3622 EDIT_WM_Paint(es, hdc);
3623 ReleaseDC(es->hwndSelf, hdc);
3624 }
3625
3626 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3627 EDIT_SetCaretPos(es, es->selection_end,
3628 es->flags & EF_AFTER_WRAP);
3629 ShowCaret(es->hwndSelf);
3630 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
3631 }
3632
3633
3634 /*********************************************************************
3635 *
3636 * WM_SETFONT
3637 *
3638 * With Win95 look the margins are set to default font value unless
3639 * the system font (font == 0) is being set, in which case they are left
3640 * unchanged.
3641 *
3642 */
3643 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
3644 {
3645 TEXTMETRICW tm;
3646 HDC dc;
3647 HFONT old_font = 0;
3648 RECT clientRect;
3649
3650 es->font = font;
3651 dc = GetDC(es->hwndSelf);
3652 if (font)
3653 old_font = SelectObject(dc, font);
3654 GetTextMetricsW(dc, &tm);
3655 es->line_height = tm.tmHeight;
3656 es->char_width = tm.tmAveCharWidth;
3657 if (font)
3658 SelectObject(dc, old_font);
3659 ReleaseDC(es->hwndSelf, dc);
3660
3661 /* Reset the format rect and the margins */
3662 GetClientRect(es->hwndSelf, &clientRect);
3663 EDIT_SetRectNP(es, &clientRect);
3664 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3665 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
3666
3667 if (es->style & ES_MULTILINE)
3668 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3669 else
3670 EDIT_CalcLineWidth_SL(es);
3671
3672 if (redraw)
3673 EDIT_UpdateText(es, NULL, TRUE);
3674 if (es->flags & EF_FOCUSED) {
3675 DestroyCaret();
3676 CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3677 EDIT_SetCaretPos(es, es->selection_end,
3678 es->flags & EF_AFTER_WRAP);
3679 ShowCaret(es->hwndSelf);
3680 }
3681 }
3682
3683
3684 /*********************************************************************
3685 *
3686 * WM_SETTEXT
3687 *
3688 * NOTES
3689 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3690 * The modified flag is reset. No notifications are sent.
3691 *
3692 * For single-line controls, reception of WM_SETTEXT triggers:
3693 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3694 *
3695 */
3696 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3697 {
3698 LPWSTR textW = NULL;
3699 if (!unicode && text)
3700 {
3701 LPCSTR textA = (LPCSTR)text;
3702 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3703 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3704 if (textW)
3705 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3706 text = textW;
3707 }
3708
3709 if (es->flags & EF_UPDATE)
3710 /* fixed this bug once; complain if we see it about to happen again. */
3711 ERR("SetSel may generate UPDATE message whose handler may reset "
3712 "selection.\n");
3713
3714 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3715 if (text)
3716 {
3717 TRACE("%s\n", debugstr_w(text));
3718 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
3719 if(!unicode)
3720 HeapFree(GetProcessHeap(), 0, textW);
3721 }
3722 else
3723 {
3724 TRACE("<NULL>\n");
3725 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
3726 }
3727 es->x_offset = 0;
3728 es->flags &= ~EF_MODIFIED;
3729 EDIT_EM_SetSel(es, 0, 0, FALSE);
3730
3731 /* Send the notification after the selection start and end have been set
3732 * edit control doesn't send notification on WM_SETTEXT
3733 * if it is multiline, or it is part of combobox
3734 */
3735 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
3736 {
3737 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
3738 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3739 }
3740 EDIT_EM_ScrollCaret(es);
3741 EDIT_UpdateScrollInfo(es);
3742 }
3743
3744
3745 /*********************************************************************
3746 *
3747 * WM_SIZE
3748 *
3749 */
3750 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
3751 {
3752 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3753 RECT rc;
3754 TRACE("width = %d, height = %d\n", width, height);
3755 SetRect(&rc, 0, 0, width, height);
3756 EDIT_SetRectNP(es, &rc);
3757 EDIT_UpdateText(es, NULL, TRUE);
3758 }
3759 }
3760
3761
3762 /*********************************************************************
3763 *
3764 * WM_STYLECHANGED
3765 *
3766 * This message is sent by SetWindowLong on having changed either the Style
3767 * or the extended style.
3768 *
3769 * We ensure that the window's version of the styles and the EDITSTATE's agree.
3770 *
3771 * See also EDIT_WM_NCCreate
3772 *
3773 * It appears that the Windows version of the edit control allows the style
3774 * (as retrieved by GetWindowLong) to be any value and maintains an internal
3775 * style variable which will generally be different. In this function we
3776 * update the internal style based on what changed in the externally visible
3777 * style.
3778 *
3779 * Much of this content as based upon the MSDN, especially:
3780 * Platform SDK Documentation -> User Interface Services ->
3781 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
3782 * Edit Control Styles
3783 */
3784 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
3785 {
3786 if (GWL_STYLE == which) {
3787 DWORD style_change_mask;
3788 DWORD new_style;
3789 /* Only a subset of changes can be applied after the control
3790 * has been created.
3791 */
3792 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
3793 ES_NUMBER;
3794 if (es->style & ES_MULTILINE)
3795 style_change_mask |= ES_WANTRETURN;
3796
3797 new_style = style->styleNew & style_change_mask;
3798
3799 /* Number overrides lowercase overrides uppercase (at least it
3800 * does in Win95). However I'll bet that ES_NUMBER would be
3801 * invalid under Win 3.1.
3802 */
3803 if (new_style & ES_NUMBER) {
3804 ; /* do not override the ES_NUMBER */
3805 } else if (new_style & ES_LOWERCASE) {
3806 new_style &= ~ES_UPPERCASE;
3807 }
3808
3809 es->style = (es->style & ~style_change_mask) | new_style;
3810 } else if (GWL_EXSTYLE == which) {
3811 ; /* FIXME - what is needed here */
3812 } else {
3813 WARN ("Invalid style change %ld\n",which);
3814 }
3815
3816 return 0;
3817 }
3818
3819 /*********************************************************************
3820 *
3821 * WM_SYSKEYDOWN
3822 *
3823 */
3824 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
3825 {
3826 if ((key == VK_BACK) && (key_data & 0x2000)) {
3827 if (EDIT_EM_CanUndo(es))
3828 EDIT_EM_Undo(es);
3829 return 0;
3830 } else if (key == VK_UP || key == VK_DOWN) {
3831 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
3832 return 0;
3833 }
3834 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
3835 }
3836
3837
3838 /*********************************************************************
3839 *
3840 * WM_TIMER
3841 *
3842 */
3843 static void EDIT_WM_Timer(EDITSTATE *es)
3844 {
3845 if (es->region_posx < 0) {
3846 EDIT_MoveBackward(es, TRUE);
3847 } else if (es->region_posx > 0) {
3848 EDIT_MoveForward(es, TRUE);
3849 }
3850 /*
3851 * FIXME: gotta do some vertical scrolling here, like
3852 * EDIT_EM_LineScroll(hwnd, 0, 1);
3853 */
3854 }
3855
3856 /*********************************************************************
3857 *
3858 * WM_HSCROLL
3859 *
3860 */
3861 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
3862 {
3863 INT dx;
3864 INT fw;
3865
3866 if (!(es->style & ES_MULTILINE))
3867 return 0;
3868
3869 if (!(es->style & ES_AUTOHSCROLL))
3870 return 0;
3871
3872 dx = 0;
3873 fw = es->format_rect.right - es->format_rect.left;
3874 switch (action) {
3875 case SB_LINELEFT:
3876 TRACE("SB_LINELEFT\n");
3877 if (es->x_offset)
3878 dx = -es->char_width;
3879 break;
3880 case SB_LINERIGHT:
3881 TRACE("SB_LINERIGHT\n");
3882 if (es->x_offset < es->text_width)
3883 dx = es->char_width;
3884 break;
3885 case SB_PAGELEFT:
3886 TRACE("SB_PAGELEFT\n");
3887 if (es->x_offset)
3888 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3889 break;
3890 case SB_PAGERIGHT:
3891 TRACE("SB_PAGERIGHT\n");
3892 if (es->x_offset < es->text_width)
3893 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3894 break;
3895 case SB_LEFT:
3896 TRACE("SB_LEFT\n");
3897 if (es->x_offset)
3898 dx = -es->x_offset;
3899 break;
3900 case SB_RIGHT:
3901 TRACE("SB_RIGHT\n");
3902 if (es->x_offset < es->text_width)
3903 dx = es->text_width - es->x_offset;
3904 break;
3905 case SB_THUMBTRACK:
3906 TRACE("SB_THUMBTRACK %d\n", pos);
3907 es->flags |= EF_HSCROLL_TRACK;
3908 if(es->style & WS_HSCROLL)
3909 dx = pos - es->x_offset;
3910 else
3911 {
3912 INT fw, new_x;
3913 /* Sanity check */
3914 if(pos < 0 || pos > 100) return 0;
3915 /* Assume default scroll range 0-100 */
3916 fw = es->format_rect.right - es->format_rect.left;
3917 new_x = pos * (es->text_width - fw) / 100;
3918 dx = es->text_width ? (new_x - es->x_offset) : 0;
3919 }
3920 break;
3921 case SB_THUMBPOSITION:
3922 TRACE("SB_THUMBPOSITION %d\n", pos);
3923 es->flags &= ~EF_HSCROLL_TRACK;
3924 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3925 dx = pos - es->x_offset;
3926 else
3927 {
3928 INT fw, new_x;
3929 /* Sanity check */
3930 if(pos < 0 || pos > 100) return 0;
3931 /* Assume default scroll range 0-100 */
3932 fw = es->format_rect.right - es->format_rect.left;
3933 new_x = pos * (es->text_width - fw) / 100;
3934 dx = es->text_width ? (new_x - es->x_offset) : 0;
3935 }
3936 if (!dx) {
3937 /* force scroll info update */
3938 EDIT_UpdateScrollInfo(es);
3939 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3940 }
3941 break;
3942 case SB_ENDSCROLL:
3943 TRACE("SB_ENDSCROLL\n");
3944 break;
3945 /*
3946 * FIXME : the next two are undocumented !
3947 * Are we doing the right thing ?
3948 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3949 * although it's also a regular control message.
3950 */
3951 case EM_GETTHUMB: /* this one is used by NT notepad */
3952 {
3953 LRESULT ret;
3954 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
3955 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
3956 else
3957 {
3958 /* Assume default scroll range 0-100 */
3959 INT fw = es->format_rect.right - es->format_rect.left;
3960 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
3961 }
3962 TRACE("EM_GETTHUMB: returning %ld\n", ret);
3963 return ret;
3964 }
3965 case EM_LINESCROLL:
3966 TRACE("EM_LINESCROLL16\n");
3967 dx = pos;
3968 break;
3969
3970 default:
3971 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
3972 action, action);
3973 return 0;
3974 }
3975 if (dx)
3976 {
3977 INT fw = es->format_rect.right - es->format_rect.left;
3978 /* check if we are going to move too far */
3979 if(es->x_offset + dx + fw > es->text_width)
3980 dx = es->text_width - fw - es->x_offset;
3981 if(dx)
3982 EDIT_EM_LineScroll_internal(es, dx, 0);
3983 }
3984 return 0;
3985 }
3986
3987
3988 /*********************************************************************
3989 *
3990 * WM_VSCROLL
3991 *
3992 */
3993 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
3994 {
3995 INT dy;
3996
3997 if (!(es->style & ES_MULTILINE))
3998 return 0;
3999
4000 if (!(es->style & ES_AUTOVSCROLL))
4001 return 0;
4002
4003 dy = 0;
4004 switch (action) {
4005 case SB_LINEUP:
4006 case SB_LINEDOWN:
4007 case SB_PAGEUP:
4008 case SB_PAGEDOWN:
4009 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
4010 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
4011 (action == SB_PAGEUP ? "SB_PAGEUP" :
4012 "SB_PAGEDOWN"))));
4013 EDIT_EM_Scroll(es, action);
4014 return 0;
4015 case SB_TOP:
4016 TRACE("SB_TOP\n");
4017 dy = -es->y_offset;
4018 break;
4019 case SB_BOTTOM:
4020 TRACE("SB_BOTTOM\n");
4021 dy = es->line_count - 1 - es->y_offset;
4022 break;
4023 case SB_THUMBTRACK:
4024 TRACE("SB_THUMBTRACK %d\n", pos);
4025 es->flags |= EF_VSCROLL_TRACK;
4026 if(es->style & WS_VSCROLL)
4027 dy = pos - es->y_offset;
4028 else
4029 {
4030 /* Assume default scroll range 0-100 */
4031 INT vlc, new_y;
4032 /* Sanity check */
4033 if(pos < 0 || pos > 100) return 0;
4034 vlc = get_vertical_line_count(es);
4035 new_y = pos * (es->line_count - vlc) / 100;
4036 dy = es->line_count ? (new_y - es->y_offset) : 0;
4037 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4038 es->line_count, es->y_offset, pos, dy);
4039 }
4040 break;
4041 case SB_THUMBPOSITION:
4042 TRACE("SB_THUMBPOSITION %d\n", pos);
4043 es->flags &= ~EF_VSCROLL_TRACK;
4044 if(es->style & WS_VSCROLL)
4045 dy = pos - es->y_offset;
4046 else
4047 {
4048 /* Assume default scroll range 0-100 */
4049 INT vlc, new_y;
4050 /* Sanity check */
4051 if(pos < 0 || pos > 100) return 0;
4052 vlc = get_vertical_line_count(es);
4053 new_y = pos * (es->line_count - vlc) / 100;
4054 dy = es->line_count ? (new_y - es->y_offset) : 0;
4055 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4056 es->line_count, es->y_offset, pos, dy);
4057 }
4058 if (!dy)
4059 {
4060 /* force scroll info update */
4061 EDIT_UpdateScrollInfo(es);
4062 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
4063 }
4064 break;
4065 case SB_ENDSCROLL:
4066 TRACE("SB_ENDSCROLL\n");
4067 break;
4068 /*
4069 * FIXME : the next two are undocumented !
4070 * Are we doing the right thing ?
4071 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4072 * although it's also a regular control message.
4073 */
4074 case EM_GETTHUMB: /* this one is used by NT notepad */
4075 {
4076 LRESULT ret;
4077 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
4078 ret = GetScrollPos(es->hwndSelf, SB_VERT);
4079 else
4080 {
4081 /* Assume default scroll range 0-100 */
4082 INT vlc = get_vertical_line_count(es);
4083 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4084 }
4085 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4086 return ret;
4087 }
4088 case EM_LINESCROLL:
4089 TRACE("EM_LINESCROLL %d\n", pos);
4090 dy = pos;
4091 break;
4092
4093 default:
4094 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4095 action, action);
4096 return 0;
4097 }
4098 if (dy)
4099 EDIT_EM_LineScroll(es, 0, dy);
4100 return 0;
4101 }
4102
4103 /*********************************************************************
4104 *
4105 * EM_GETTHUMB
4106 *
4107 * FIXME: is this right ? (or should it be only VSCROLL)
4108 * (and maybe only for edit controls that really have their
4109 * own scrollbars) (and maybe only for multiline controls ?)
4110 * All in all: very poorly documented
4111 *
4112 */
4113 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
4114 {
4115 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
4116 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
4117 }
4118
4119
4120 /********************************************************************
4121 *
4122 * The Following code is to handle inline editing from IMEs
4123 */
4124
4125 static void EDIT_GetCompositionStr(HIMC hIMC, LPARAM CompFlag, EDITSTATE *es)
4126 {
4127 LONG buflen;
4128 LPWSTR lpCompStr = NULL;
4129 LPSTR lpCompStrAttr = NULL;
4130 DWORD dwBufLenAttr;
4131
4132 buflen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4133
4134 if (buflen < 0)
4135 {
4136 return;
4137 }
4138
4139 lpCompStr = HeapAlloc(GetProcessHeap(),0,buflen + sizeof(WCHAR));
4140 if (!lpCompStr)
4141 {
4142 ERR("Unable to allocate IME CompositionString\n");
4143 return;
4144 }
4145
4146 if (buflen)
4147 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, buflen);
4148 lpCompStr[buflen/sizeof(WCHAR)] = 0;
4149
4150 if (CompFlag & GCS_COMPATTR)
4151 {
4152 /*
4153 * We do not use the attributes yet. it would tell us what characters
4154 * are in transition and which are converted or decided upon
4155 */
4156 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
4157 if (dwBufLenAttr)
4158 {
4159 dwBufLenAttr ++;
4160 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr+1);
4161 if (!lpCompStrAttr)
4162 {
4163 ERR("Unable to allocate IME Attribute String\n");
4164 HeapFree(GetProcessHeap(),0,lpCompStr);
4165 return;
4166 }
4167 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
4168 dwBufLenAttr);
4169 lpCompStrAttr[dwBufLenAttr] = 0;
4170 }
4171 else
4172 lpCompStrAttr = NULL;
4173 }
4174
4175 /* check for change in composition start */
4176 if (es->selection_end < es->composition_start)
4177 es->composition_start = es->selection_end;
4178
4179 /* replace existing selection string */
4180 es->selection_start = es->composition_start;
4181
4182 if (es->composition_len > 0)
4183 es->selection_end = es->composition_start + es->composition_len;
4184 else
4185 es->selection_end = es->selection_start;
4186
4187 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
4188 es->composition_len = abs(es->composition_start - es->selection_end);
4189
4190 es->selection_start = es->composition_start;
4191 es->selection_end = es->selection_start + es->composition_len;
4192
4193 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
4194 HeapFree(GetProcessHeap(),0,lpCompStr);
4195 }
4196
4197 static void EDIT_GetResultStr(HIMC hIMC, EDITSTATE *es)
4198 {
4199 LONG buflen;
4200 LPWSTR lpResultStr;
4201
4202 buflen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4203 if (buflen <= 0)
4204 {
4205 return;
4206 }
4207
4208 lpResultStr = HeapAlloc(GetProcessHeap(),0, buflen+sizeof(WCHAR));
4209 if (!lpResultStr)
4210 {
4211 ERR("Unable to alloc buffer for IME string\n");
4212 return;
4213 }
4214
4215 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, buflen);
4216 lpResultStr[buflen/sizeof(WCHAR)] = 0;
4217
4218 /* check for change in composition start */
4219 if (es->selection_end < es->composition_start)
4220 es->composition_start = es->selection_end;
4221
4222 es->selection_start = es->composition_start;
4223 es->selection_end = es->composition_start + es->composition_len;
4224 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
4225 es->composition_start = es->selection_end;
4226 es->composition_len = 0;
4227
4228 HeapFree(GetProcessHeap(),0,lpResultStr);
4229 }
4230
4231 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
4232 {
4233 HIMC hIMC;
4234 int cursor;
4235
4236 if (es->composition_len == 0 && es->selection_start != es->selection_end)
4237 {
4238 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4239 es->composition_start = es->selection_end;
4240 }
4241
4242 hIMC = ImmGetContext(hwnd);
4243 if (!hIMC)
4244 return;
4245
4246 if (CompFlag & GCS_RESULTSTR)
4247 EDIT_GetResultStr(hIMC, es);
4248 if (CompFlag & GCS_COMPSTR)
4249 EDIT_GetCompositionStr(hIMC, CompFlag, es);
4250 cursor = ImmGetCompositionStringW(hIMC, GCS_CURSORPOS, 0, 0);
4251 ImmReleaseContext(hwnd, hIMC);
4252 EDIT_SetCaretPos(es, es->selection_start + cursor, es->flags & EF_AFTER_WRAP);
4253 }
4254
4255
4256 /*********************************************************************
4257 *
4258 * WM_NCCREATE
4259 *
4260 * See also EDIT_WM_StyleChanged
4261 */
4262 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4263 {
4264 EDITSTATE *es;
4265 UINT alloc_size;
4266
4267 TRACE("Creating %s edit control, style = %08x\n",
4268 unicode ? "Unicode" : "ANSI", lpcs->style);
4269
4270 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4271 return FALSE;
4272 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4273
4274 /*
4275 * Note: since the EDITSTATE has not been fully initialized yet,
4276 * we can't use any API calls that may send
4277 * WM_XXX messages before WM_NCCREATE is completed.
4278 */
4279
4280 es->is_unicode = unicode;
4281 es->style = lpcs->style;
4282
4283 es->bEnableState = !(es->style & WS_DISABLED);
4284
4285 es->hwndSelf = hwnd;
4286 /* Save parent, which will be notified by EN_* messages */
4287 es->hwndParent = lpcs->hwndParent;
4288
4289 if (es->style & ES_COMBO)
4290 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4291
4292 /* FIXME: should we handle changes to WS_EX_RIGHT style after creation? */
4293 if (lpcs->dwExStyle & WS_EX_RIGHT) es->style |= ES_RIGHT;
4294
4295 /* Number overrides lowercase overrides uppercase (at least it
4296 * does in Win95). However I'll bet that ES_NUMBER would be
4297 * invalid under Win 3.1.
4298 */
4299 if (es->style & ES_NUMBER) {
4300 ; /* do not override the ES_NUMBER */
4301 } else if (es->style & ES_LOWERCASE) {
4302 es->style &= ~ES_UPPERCASE;
4303 }
4304 if (es->style & ES_MULTILINE) {
4305 es->buffer_limit = BUFLIMIT_INITIAL;
4306 if (es->style & WS_VSCROLL)
4307 es->style |= ES_AUTOVSCROLL;
4308 if (es->style & WS_HSCROLL)
4309 es->style |= ES_AUTOHSCROLL;
4310 es->style &= ~ES_PASSWORD;
4311 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4312 /* Confirmed - RIGHT overrides CENTER */
4313 if (es->style & ES_RIGHT)
4314 es->style &= ~ES_CENTER;
4315 es->style &= ~WS_HSCROLL;
4316 es->style &= ~ES_AUTOHSCROLL;
4317 }
4318 } else {
4319 es->buffer_limit = BUFLIMIT_INITIAL;
4320 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4321 es->style &= ~ES_CENTER;
4322 es->style &= ~WS_HSCROLL;
4323 es->style &= ~WS_VSCROLL;
4324 if (es->style & ES_PASSWORD)
4325 es->password_char = '*';
4326 }
4327
4328 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4329 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4330 goto cleanup;
4331 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4332
4333 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4334 goto cleanup;
4335 es->undo_buffer_size = es->buffer_size;
4336
4337 if (es->style & ES_MULTILINE)
4338 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4339 goto cleanup;
4340 es->line_count = 1;
4341
4342 /*
4343 * In Win95 look and feel, the WS_BORDER style is replaced by the
4344 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4345 * control a nonclient area so we don't need to draw the border.
4346 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4347 * a nonclient area and we should handle painting the border ourselves.
4348 *
4349 * When making modifications please ensure that the code still works
4350 * for edit controls created directly with style 0x50800000, exStyle 0
4351 * (which should have a single pixel border)
4352 */
4353 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
4354 es->style &= ~WS_BORDER;
4355 else if (es->style & WS_BORDER)
4356 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
4357
4358 return TRUE;
4359
4360 cleanup:
4361 SetWindowLongPtrW(es->hwndSelf, 0, 0);
4362 HeapFree(GetProcessHeap(), 0, es->first_line_def);
4363 HeapFree(GetProcessHeap(), 0, es->undo_text);
4364 if (es->hloc32W) LocalFree(es->hloc32W);
4365 HeapFree(GetProcessHeap(), 0, es);
4366 return FALSE;
4367 }
4368
4369
4370 /*********************************************************************
4371 *
4372 * WM_CREATE
4373 *
4374 */
4375 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4376 {
4377 RECT clientRect;
4378
4379 TRACE("%s\n", debugstr_w(name));
4380 /*
4381 * To initialize some final structure members, we call some helper
4382 * functions. However, since the EDITSTATE is not consistent (i.e.
4383 * not fully initialized), we should be very careful which
4384 * functions can be called, and in what order.
4385 */
4386 EDIT_WM_SetFont(es, 0, FALSE);
4387 EDIT_EM_EmptyUndoBuffer(es);
4388
4389 /* We need to calculate the format rect
4390 (applications may send EM_SETMARGINS before the control gets visible) */
4391 GetClientRect(es->hwndSelf, &clientRect);
4392 EDIT_SetRectNP(es, &clientRect);
4393
4394 if (name && *name) {
4395 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4396 /* if we insert text to the editline, the text scrolls out
4397 * of the window, as the caret is placed after the insert
4398 * pos normally; thus we reset es->selection... to 0 and
4399 * update caret
4400 */
4401 es->selection_start = es->selection_end = 0;
4402 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4403 * Messages are only to be sent when the USER does something to
4404 * change the contents. So I am removing this EN_CHANGE
4405 *
4406 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4407 */
4408 EDIT_EM_ScrollCaret(es);
4409 }
4410 /* force scroll info update */
4411 EDIT_UpdateScrollInfo(es);
4412 /* The rule seems to return 1 here for success */
4413 /* Power Builder masked edit controls will crash */
4414 /* if not. */
4415 /* FIXME: is that in all cases so ? */
4416 return 1;
4417 }
4418
4419
4420 /*********************************************************************
4421 *
4422 * WM_NCDESTROY
4423 *
4424 */
4425 static LRESULT EDIT_WM_NCDestroy(EDITSTATE *es)
4426 {
4427 LINEDEF *pc, *pp;
4428
4429 if (es->hloc32W) {
4430 LocalFree(es->hloc32W);
4431 }
4432 if (es->hloc32A) {
4433 LocalFree(es->hloc32A);
4434 }
4435 pc = es->first_line_def;
4436 while (pc)
4437 {
4438 pp = pc->next;
4439 HeapFree(GetProcessHeap(), 0, pc);
4440 pc = pp;
4441 }
4442
4443 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4444 HeapFree(GetProcessHeap(), 0, es->undo_text);
4445 HeapFree(GetProcessHeap(), 0, es);
4446
4447 return 0;
4448 }
4449
4450
4451 static inline LRESULT DefWindowProcT(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode)
4452 {
4453 if(unicode)
4454 return DefWindowProcW(hwnd, msg, wParam, lParam);
4455 else
4456 return DefWindowProcA(hwnd, msg, wParam, lParam);
4457 }
4458
4459 /*********************************************************************
4460 *
4461 * EditWndProc_common
4462 *
4463 * The messages are in the order of the actual integer values
4464 * (which can be found in include/windows.h)
4465 */
4466 LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
4467 {
4468 EDITSTATE *es = (EDITSTATE *)GetWindowLongPtrW( hwnd, 0 );
4469 LRESULT result = 0;
4470 #ifdef __REACTOS__
4471 PWND pWnd;
4472
4473 pWnd = ValidateHwnd(hwnd);
4474 if (pWnd)
4475 {
4476 if (!pWnd->fnid)
4477 {
4478 NtUserSetWindowFNID(hwnd, FNID_EDIT);
4479 }
4480 }
4481 #endif
4482
4483 TRACE("hwnd=%p msg=%x (%s) wparam=%lx lparam=%lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), wParam, lParam);
4484
4485 if (!es && msg != WM_NCCREATE)
4486 return DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
4487
4488 if (es && (msg != WM_NCDESTROY)) EDIT_LockBuffer(es);
4489
4490 switch (msg) {
4491 case EM_GETSEL:
4492 result = EDIT_EM_GetSel(es, (PUINT)wParam, (PUINT)lParam);
4493 break;
4494
4495 case EM_SETSEL:
4496 EDIT_EM_SetSel(es, wParam, lParam, FALSE);
4497 EDIT_EM_ScrollCaret(es);
4498 result = 1;
4499 break;
4500
4501 case EM_GETRECT:
4502 if (lParam)
4503 CopyRect((LPRECT)lParam, &es->format_rect);
4504 break;
4505
4506 case EM_SETRECT:
4507 if ((es->style & ES_MULTILINE) && lParam) {
4508 EDIT_SetRectNP(es, (LPRECT)lParam);
4509 EDIT_UpdateText(es, NULL, TRUE);
4510 }
4511 break;
4512
4513 case EM_SETRECTNP:
4514 if ((es->style & ES_MULTILINE) && lParam)
4515 EDIT_SetRectNP(es, (LPRECT)lParam);
4516 break;
4517
4518 case EM_SCROLL:
4519 result = EDIT_EM_Scroll(es, (INT)wParam);
4520 break;
4521
4522 case EM_LINESCROLL:
4523 result = (LRESULT)EDIT_EM_LineScroll(es, (INT)wParam, (INT)lParam);
4524 break;
4525
4526 case EM_SCROLLCARET:
4527 EDIT_EM_ScrollCaret(es);
4528 result = 1;
4529 break;
4530
4531 case EM_GETMODIFY:
4532 result = ((es->flags & EF_MODIFIED) != 0);
4533 break;
4534
4535 case EM_SETMODIFY:
4536 if (wParam)
4537 es->flags |= EF_MODIFIED;
4538 else
4539 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
4540 break;
4541
4542 case EM_GETLINECOUNT:
4543 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
4544 break;
4545
4546 case EM_LINEINDEX:
4547 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
4548 break;
4549
4550 case EM_SETHANDLE:
4551 EDIT_EM_SetHandle(es, (HLOCAL)wParam);
4552 break;
4553
4554 case EM_GETHANDLE:
4555 result = (LRESULT)EDIT_EM_GetHandle(es);
4556 break;
4557
4558 case EM_GETTHUMB:
4559 result = EDIT_EM_GetThumb(es);
4560 break;
4561
4562 /* these messages missing from specs */
4563 case 0x00bf:
4564 case 0x00c0:
4565 case 0x00c3:
4566 case 0x00ca:
4567 FIXME("undocumented message 0x%x, please report\n", msg);
4568 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4569 break;
4570
4571 case EM_LINELENGTH:
4572 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
4573 break;
4574
4575 case EM_REPLACESEL:
4576 {
4577 LPWSTR textW;
4578
4579 if(unicode)
4580 textW = (LPWSTR)lParam;
4581 else
4582 {
4583 LPSTR textA = (LPSTR)lParam;
4584 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4585 if (!(textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR)))) break;
4586 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
4587 }
4588
4589 EDIT_EM_ReplaceSel(es, (BOOL)wParam, textW, TRUE, TRUE);
4590 result = 1;
4591
4592 if(!unicode)
4593 HeapFree(GetProcessHeap(), 0, textW);
4594 break;
4595 }
4596
4597 case EM_GETLINE:
4598 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, (LPWSTR)lParam, unicode);
4599 break;
4600
4601 case EM_SETLIMITTEXT:
4602 EDIT_EM_SetLimitText(es, wParam);
4603 break;
4604
4605 case EM_CANUNDO:
4606 result = (LRESULT)EDIT_EM_CanUndo(es);
4607 break;
4608
4609 case EM_UNDO:
4610 case WM_UNDO:
4611 result = (LRESULT)EDIT_EM_Undo(es);
4612 break;
4613
4614 case EM_FMTLINES:
4615 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
4616 break;
4617
4618 case EM_LINEFROMCHAR:
4619 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
4620 break;
4621
4622 case EM_SETTABSTOPS:
4623 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
4624 break;
4625
4626 case EM_SETPASSWORDCHAR:
4627 {
4628 WCHAR charW = 0;
4629
4630 if(unicode)
4631 charW = (WCHAR)wParam;
4632 else
4633 {
4634 CHAR charA = wParam;
4635 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4636 }
4637
4638 EDIT_EM_SetPasswordChar(es, charW);
4639 break;
4640 }
4641
4642 case EM_EMPTYUNDOBUFFER:
4643 EDIT_EM_EmptyUndoBuffer(es);
4644 break;
4645
4646 case EM_GETFIRSTVISIBLELINE:
4647 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
4648 break;
4649
4650 case EM_SETREADONLY:
4651 {
4652 DWORD old_style = es->style;
4653
4654 if (wParam) {
4655 SetWindowLongW( hwnd, GWL_STYLE,
4656 GetWindowLongW( hwnd, GWL_STYLE ) | ES_READONLY );
4657 es->style |= ES_READONLY;
4658 } else {
4659 SetWindowLongW( hwnd, GWL_STYLE,
4660 GetWindowLongW( hwnd, GWL_STYLE ) & ~ES_READONLY );
4661 es->style &= ~ES_READONLY;
4662 }
4663
4664 if (old_style ^ es->style)
4665 InvalidateRect(es->hwndSelf, NULL, TRUE);
4666
4667 result = 1;
4668 break;
4669 }
4670
4671 case EM_SETWORDBREAKPROC:
4672 EDIT_EM_SetWordBreakProc(es, (void *)lParam);
4673 break;
4674
4675 case EM_GETWORDBREAKPROC:
4676 result = (LRESULT)es->word_break_proc;
4677 break;
4678
4679 case EM_GETPASSWORDCHAR:
4680 {
4681 if(unicode)
4682 result = es->password_char;
4683 else
4684 {
4685 WCHAR charW = es->password_char;
4686 CHAR charA = 0;
4687 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
4688 result = charA;
4689 }
4690 break;
4691 }
4692
4693 case EM_SETMARGINS:
4694 EDIT_EM_SetMargins(es, (INT)wParam, LOWORD(lParam), HIWORD(lParam), TRUE);
4695 break;
4696
4697 case EM_GETMARGINS:
4698 result = MAKELONG(es->left_margin, es->right_margin);
4699 break;
4700
4701 case EM_GETLIMITTEXT:
4702 result = es->buffer_limit;
4703 break;
4704
4705 case EM_POSFROMCHAR:
4706 if ((INT)wParam >= get_text_length(es)) result = -1;
4707 else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
4708 break;
4709
4710 case EM_CHARFROMPOS:
4711 result = EDIT_EM_CharFromPos(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4712 break;
4713
4714 /* End of the EM_ messages which were in numerical order; what order
4715 * are these in? vaguely alphabetical?
4716 */
4717
4718 case WM_NCCREATE:
4719 result = EDIT_WM_NCCreate(hwnd, (LPCREATESTRUCTW)lParam, unicode);
4720 break;
4721
4722 case WM_NCDESTROY:
4723 result = EDIT_WM_NCDestroy(es);
4724 // es = NULL; reactos
4725 #ifdef __REACTOS__
4726 NtUserSetWindowFNID(hwnd, FNID_DESTROY);
4727 #endif
4728 break;
4729
4730 case WM_GETDLGCODE:
4731 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
4732
4733 if (es->style & ES_MULTILINE)
4734 result |= DLGC_WANTALLKEYS;
4735
4736 if (lParam)
4737 {
4738 es->flags|=EF_DIALOGMODE;
4739
4740 if (((LPMSG)lParam)->message == WM_KEYDOWN)
4741 {
4742 int vk = (int)((LPMSG)lParam)->wParam;
4743
4744 if (es->hwndListBox)
4745 {
4746 if (vk == VK_RETURN || vk == VK_ESCAPE)
4747 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4748 result |= DLGC_WANTMESSAGE;
4749 }
4750 }
4751 }
4752 break;
4753
4754 case WM_IME_CHAR:
4755 if (!unicode)
4756 {
4757 WCHAR charW;
4758 CHAR strng[2];
4759
4760 strng[0] = wParam >> 8;
4761 strng[1] = wParam & 0xff;
4762 if (strng[0]) MultiByteToWideChar(CP_ACP, 0, strng, 2, &charW, 1);
4763 else MultiByteToWideChar(CP_ACP, 0, &strng[1], 1, &charW, 1);
4764 result = EDIT_WM_Char(es, charW);
4765 break;
4766 }
4767 /* fall through */
4768 case WM_CHAR:
4769 {
4770 WCHAR charW;
4771
4772 if(unicode)
4773 charW = wParam;
4774 else
4775 {
4776 CHAR charA = wParam;
4777 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
4778 }
4779
4780 if (es->hwndListBox)
4781 {
4782 if (charW == VK_RETURN || charW == VK_ESCAPE)
4783 {
4784 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
4785 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
4786 break;
4787 }
4788 }
4789 result = EDIT_WM_Char(es, charW);
4790 break;
4791 }
4792
4793 case WM_UNICHAR:
4794 if (unicode)
4795 {
4796 if (wParam == UNICODE_NOCHAR) return TRUE;
4797 if (wParam <= 0x000fffff)
4798 {
4799 if(wParam > 0xffff) /* convert to surrogates */
4800 {
4801 wParam -= 0x10000;
4802 EDIT_WM_Char(es, (wParam >> 10) + 0xd800);
4803 EDIT_WM_Char(es, (wParam & 0x03ff) + 0xdc00);
4804 }
4805 else EDIT_WM_Char(es, wParam);
4806 }
4807 return 0;
4808 }
4809 break;
4810
4811 case WM_CLEAR:
4812 EDIT_WM_Clear(es);
4813 break;
4814 #if 0 // Removed see Revision 43925 comments.
4815 case WM_COMMAND:
4816 EDIT_WM_Command(es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
4817 break;
4818 #endif
4819 case WM_CONTEXTMENU:
4820 EDIT_WM_ContextMenu(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4821 break;
4822
4823 case WM_COPY:
4824 EDIT_WM_Copy(es);
4825 break;
4826
4827 case WM_CREATE:
4828 if(unicode)
4829 result = EDIT_WM_Create(es, ((LPCREATESTRUCTW)lParam)->lpszName);
4830 else
4831 {
4832 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
4833 LPWSTR nameW = NULL;
4834 if(nameA)
4835 {
4836 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
4837 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4838 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
4839 }
4840 result = EDIT_WM_Create(es, nameW);
4841 HeapFree(GetProcessHeap(), 0, nameW);
4842 }
4843 break;
4844
4845 case WM_CUT:
4846 EDIT_WM_Cut(es);
4847 break;
4848
4849 case WM_ENABLE:
4850 es->bEnableState = (BOOL) wParam;
4851 EDIT_UpdateText(es, NULL, TRUE);
4852 break;
4853
4854 case WM_ERASEBKGND:
4855 /* we do the proper erase in EDIT_WM_Paint */
4856 result = 1;
4857 break;
4858
4859 case WM_GETFONT:
4860 result = (LRESULT)es->font;
4861 break;
4862
4863 case WM_GETTEXT:
4864 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, (LPWSTR)lParam, unicode);
4865 break;
4866
4867 case WM_GETTEXTLENGTH:
4868 if (unicode) result = get_text_length(es);
4869 else result = WideCharToMultiByte( CP_ACP, 0, es->text, get_text_length(es),
4870 NULL, 0, NULL, NULL );
4871 break;
4872
4873 case WM_HSCROLL:
4874 result = EDIT_WM_HScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4875 break;
4876
4877 case WM_KEYDOWN:
4878 result = EDIT_WM_KeyDown(es, (INT)wParam);
4879 break;
4880
4881 case WM_KILLFOCUS:
4882 result = EDIT_WM_KillFocus(es);
4883 break;
4884
4885 case WM_LBUTTONDBLCLK:
4886 result = EDIT_WM_LButtonDblClk(es);
4887 break;
4888
4889 case WM_LBUTTONDOWN:
4890 result = EDIT_WM_LButtonDown(es, wParam, (short)LOWORD(lParam), (short)HIWORD(lParam));
4891 break;
4892
4893 case WM_LBUTTONUP:
4894 result = EDIT_WM_LButtonUp(es);
4895 break;
4896
4897 case WM_MBUTTONDOWN:
4898 result = EDIT_WM_MButtonDown(es);
4899 break;
4900
4901 case WM_MOUSEMOVE:
4902 result = EDIT_WM_MouseMove(es, (short)LOWORD(lParam), (short)HIWORD(lParam));
4903 break;
4904
4905 case WM_PRINTCLIENT:
4906 case WM_PAINT:
4907 EDIT_WM_Paint(es, (HDC)wParam);
4908 break;
4909
4910 case WM_PASTE:
4911 EDIT_WM_Paste(es);
4912 break;
4913
4914 case WM_SETFOCUS:
4915 EDIT_WM_SetFocus(es);
4916 break;
4917
4918 case WM_SETFONT:
4919 EDIT_WM_SetFont(es, (HFONT)wParam, LOWORD(lParam) != 0);
4920 break;
4921
4922 case WM_SETREDRAW:
4923 /* FIXME: actually set an internal flag and behave accordingly */
4924 break;
4925
4926 case WM_SETTEXT:
4927 EDIT_WM_SetText(es, (LPCWSTR)lParam, unicode);
4928 result = TRUE;
4929 break;
4930
4931 case WM_SIZE:
4932 EDIT_WM_Size(es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
4933 break;
4934
4935 case WM_STYLECHANGED:
4936 result = EDIT_WM_StyleChanged(es, wParam, (const STYLESTRUCT *)lParam);
4937 break;
4938
4939 case WM_STYLECHANGING:
4940 result = 0; /* See EDIT_WM_StyleChanged */
4941 break;
4942
4943 case WM_SYSKEYDOWN:
4944 result = EDIT_WM_SysKeyDown(es, (INT)wParam, (DWORD)lParam);
4945 break;
4946
4947 case WM_TIMER:
4948 EDIT_WM_Timer(es);
4949 break;
4950
4951 case WM_VSCROLL:
4952 result = EDIT_WM_VScroll(es, LOWORD(wParam), (short)HIWORD(wParam));
4953 break;
4954
4955 case WM_MOUSEWHEEL:
4956 {
4957 int gcWheelDelta = 0;
4958 UINT pulScrollLines = 3;
4959 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4960
4961 if (wParam & (MK_SHIFT | MK_CONTROL)) {
4962 result = DefWindowProcW(hwnd, msg, wParam, lParam);
4963 break;
4964 }
4965 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
4966 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
4967 {
4968 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
4969 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
4970 result = EDIT_EM_LineScroll(es, 0, cLineScroll);
4971 }
4972 }
4973 break;
4974
4975
4976 /* IME messages to make the edit control IME aware */
4977 case WM_IME_SETCONTEXT:
4978 break;
4979
4980 case WM_IME_STARTCOMPOSITION:
4981 es->composition_start = es->selection_end;
4982 es->composition_len = 0;
4983 break;
4984
4985 case WM_IME_COMPOSITION:
4986 EDIT_ImeComposition(hwnd, lParam, es);
4987 break;
4988
4989 case WM_IME_ENDCOMPOSITION:
4990 if (es->composition_len > 0)
4991 {
4992 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
4993 es->selection_end = es->selection_start;
4994 es->composition_len= 0;
4995 }
4996 break;
4997
4998 case WM_IME_COMPOSITIONFULL:
4999 break;
5000
5001 case WM_IME_SELECT:
5002 break;
5003
5004 case WM_IME_CONTROL:
5005 break;
5006
5007 default:
5008 result = DefWindowProcT(hwnd, msg, wParam, lParam, unicode);
5009 break;
5010 }
5011
5012 /* reactos: check GetWindowLong in case es has been destroyed during processing */
5013 if (IsWindow(hwnd) && es && GetWindowLongPtrW(hwnd, 0))
5014 EDIT_UnlockBuffer(es, FALSE);
5015
5016 TRACE("hwnd=%p msg=%x (%s) -- 0x%08lx\n", hwnd, msg, SPY_GetMsgName(msg, hwnd), result);
5017
5018 return result;
5019 }
5020
5021 /*********************************************************************
5022 *
5023 * EditWndProc (USER32.@)
5024 */
5025 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5026 {
5027 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
5028 }
5029
5030 /*********************************************************************
5031 *
5032 * EditWndProcW (USER32.@)
5033 */
5034 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5035 {
5036 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
5037 }
5038
5039 /*********************************************************************
5040 * edit class descriptor
5041 */
5042 static const WCHAR editW[] = {'E','d','i','t',0};
5043 const struct builtin_class_descr EDIT_builtin_class =
5044 {
5045 editW, /* name */
5046 CS_DBLCLKS | CS_PARENTDC, /* style */
5047 EditWndProcA, /* procA */
5048 EditWndProcW, /* procW */
5049 #ifndef _WIN64
5050 sizeof(EDITSTATE *) + sizeof(WORD), /* extra */
5051 #else
5052 sizeof(EDITSTATE *), /* extra */
5053 #endif
5054 IDC_IBEAM, /* cursor */
5055 0 /* brush */
5056 };