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