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