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