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