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