7c16e44de6663122109db20c8e41b0a0aeb5ce3a
[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_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
72
73 typedef enum
74 {
75 END_0 = 0, /* line ends with terminating '\0' character */
76 END_WRAP, /* line is wrapped */
77 END_HARD, /* line ends with a hard return '\r\n' */
78 END_SOFT, /* line ends with a soft return '\r\r\n' */
79 END_RICH /* line ends with a single '\n' */
80 } LINE_END;
81
82 typedef struct tagLINEDEF {
83 INT length; /* bruto length of a line in bytes */
84 INT net_length; /* netto length of a line in visible characters */
85 LINE_END ending;
86 INT width; /* width of the line in pixels */
87 INT index; /* line index into the buffer */
88 SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
89 struct tagLINEDEF *next;
90 } LINEDEF;
91
92 typedef struct
93 {
94 BOOL is_unicode; /* how the control was created */
95 LPWSTR text; /* the actual contents of the control */
96 UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
97 UINT buffer_size; /* the size of the buffer in characters */
98 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
99 HFONT font; /* NULL means standard system font */
100 INT x_offset; /* scroll offset for multi lines this is in pixels
101 for single lines it's in characters */
102 INT line_height; /* height of a screen line in pixels */
103 INT char_width; /* average character width in pixels */
104 DWORD style; /* sane version of wnd->dwStyle */
105 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
106 INT undo_insert_count; /* number of characters inserted in sequence */
107 UINT undo_position; /* character index of the insertion and deletion */
108 LPWSTR undo_text; /* deleted text */
109 UINT undo_buffer_size; /* size of the deleted text buffer */
110 INT selection_start; /* == selection_end if no selection */
111 INT selection_end; /* == current caret position */
112 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
113 INT left_margin; /* in pixels */
114 INT right_margin; /* in pixels */
115 RECT format_rect;
116 INT text_width; /* width of the widest line in pixels for multi line controls
117 and just line width for single line controls */
118 INT region_posx; /* Position of cursor relative to region: */
119 INT region_posy; /* -1: to left, 0: within, 1: to right */
120 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
121 INT line_count; /* number of lines */
122 INT y_offset; /* scroll offset in number of lines */
123 BOOL bCaptureState; /* flag indicating whether mouse was captured */
124 BOOL bEnableState; /* flag keeping the enable state */
125 HWND hwndSelf; /* the our window handle */
126 HWND hwndParent; /* Handle of parent for sending EN_* messages.
127 Even if parent will change, EN_* messages
128 should be sent to the first parent. */
129 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
130 INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
131 /*
132 * only for multi line controls
133 */
134 INT lock_count; /* amount of re-entries in the EditWndProc */
135 INT tabs_count;
136 LPINT tabs;
137 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
138 HLOCAL hloc32W; /* our unicode local memory block */
139 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
140 or EM_SETHANDLE */
141 HLOCAL hlocapp; /* The text buffer handle belongs to the app */
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 #ifdef __REACTOS__
241 /* ReactOS r54259 */
242 hbrush = GetControlBrush(es->hwndSelf, hdc, msg);
243 #else
244 hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
245 if (!hbrush)
246 hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
247 #endif
248 return hbrush;
249 }
250
251
252 static inline UINT get_text_length(EDITSTATE *es)
253 {
254 if(es->text_length == (UINT)-1)
255 es->text_length = strlenW(es->text);
256 return es->text_length;
257 }
258
259
260 /*********************************************************************
261 *
262 * EDIT_WordBreakProc
263 *
264 * Find the beginning of words.
265 * Note: unlike the specs for a WordBreakProc, this function can
266 * only be called without linebreaks between s[0] up to
267 * s[count - 1]. Remember it is only called
268 * internally, so we can decide this for ourselves.
269 * Additionally we will always be breaking the full string.
270 *
271 */
272 static INT EDIT_WordBreakProc(EDITSTATE *es, LPWSTR s, INT index, INT count, INT action)
273 {
274 INT ret = 0;
275
276 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
277
278 if(!s) return 0;
279
280 if (!es->logAttr)
281 {
282 SCRIPT_ANALYSIS psa;
283
284 memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
285 psa.eScript = SCRIPT_UNDEFINED;
286
287 es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
288 ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
289 }
290
291 switch (action) {
292 case WB_LEFT:
293 if (index)
294 index--;
295 while (index && !es->logAttr[index].fSoftBreak)
296 index--;
297 ret = index;
298 break;
299 case WB_RIGHT:
300 if (!count)
301 break;
302 while (s[index] && index < count && !es->logAttr[index].fSoftBreak)
303 index++;
304 ret = index;
305 break;
306 case WB_ISDELIMITER:
307 ret = es->logAttr[index].fWhiteSpace;
308 break;
309 default:
310 ERR("unknown action code, please report !\n");
311 break;
312 }
313 return ret;
314 }
315
316
317 /*********************************************************************
318 *
319 * EDIT_CallWordBreakProc
320 *
321 * Call appropriate WordBreakProc (internal or external).
322 *
323 * Note: The "start" argument should always be an index referring
324 * to es->text. The actual wordbreak proc might be
325 * 16 bit, so we can't always pass any 32 bit LPSTR.
326 * Hence we assume that es->text is the buffer that holds
327 * the string under examination (we can decide this for ourselves).
328 *
329 */
330 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
331 {
332 INT ret;
333
334 if (es->word_break_proc)
335 {
336 if(es->is_unicode)
337 {
338 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
339
340 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
341 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
342 ret = wbpW(es->text + start, index, count, action);
343 }
344 else
345 {
346 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
347 INT countA;
348 CHAR *textA;
349
350 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
351 textA = HeapAlloc(GetProcessHeap(), 0, countA);
352 #ifdef __REACTOS__
353 /* ReactOS r33503 */
354 if (textA == NULL) return 0;
355 #endif
356 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
357 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
358 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
359 ret = wbpA(textA, index, countA, action);
360 HeapFree(GetProcessHeap(), 0, textA);
361 }
362 }
363 else
364 ret = EDIT_WordBreakProc(es, es->text, index+start, count+start, action) - start;
365
366 return ret;
367 }
368
369 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
370 {
371 if (line_def->ssa)
372 {
373 ScriptStringFree(&line_def->ssa);
374 line_def->ssa = NULL;
375 }
376 }
377
378 static inline void EDIT_InvalidateUniscribeData(EDITSTATE *es)
379 {
380 LINEDEF *line_def = es->first_line_def;
381 while (line_def)
382 {
383 EDIT_InvalidateUniscribeData_linedef(line_def);
384 line_def = line_def->next;
385 }
386 if (es->ssa)
387 {
388 ScriptStringFree(&es->ssa);
389 es->ssa = NULL;
390 }
391 }
392
393 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData_linedef(EDITSTATE *es, HDC dc, LINEDEF *line_def)
394 {
395 if (!line_def)
396 return NULL;
397
398 if (line_def->net_length && !line_def->ssa)
399 {
400 int index = line_def->index;
401 HFONT old_font = NULL;
402 HDC udc = dc;
403 SCRIPT_TABDEF tabdef;
404 HRESULT hr;
405
406 if (!udc)
407 udc = GetDC(es->hwndSelf);
408 if (es->font)
409 old_font = SelectObject(udc, es->font);
410
411 tabdef.cTabStops = es->tabs_count;
412 tabdef.iScale = 0;
413 tabdef.pTabStops = es->tabs;
414 tabdef.iTabOrigin = 0;
415
416 hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
417 #ifdef __REACTOS__
418 /* ReactOS r57679 */
419 (3*line_def->net_length/2+16), -1,
420 #else
421 (1.5*line_def->net_length+16), -1,
422 #endif
423 SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_TAB, -1,
424 NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
425 if (FAILED(hr))
426 {
427 WARN("ScriptStringAnalyse failed (%x)\n",hr);
428 line_def->ssa = NULL;
429 }
430
431 if (es->font)
432 SelectObject(udc, old_font);
433 if (udc != dc)
434 ReleaseDC(es->hwndSelf, udc);
435 }
436
437 return line_def->ssa;
438 }
439
440 static SCRIPT_STRING_ANALYSIS EDIT_UpdateUniscribeData(EDITSTATE *es, HDC dc, INT line)
441 {
442 LINEDEF *line_def;
443
444 if (!(es->style & ES_MULTILINE))
445 {
446 if (!es->ssa)
447 {
448 INT length = get_text_length(es);
449 HFONT old_font = NULL;
450 HDC udc = dc;
451
452 if (!udc)
453 udc = GetDC(es->hwndSelf);
454 if (es->font)
455 old_font = SelectObject(udc, es->font);
456
457 if (es->style & ES_PASSWORD)
458 #ifdef __REACTOS__
459 /* ReactOS r57677 */
460 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);
461 #else
462 ScriptStringAnalyse(udc, &es->password_char, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS|SSA_PASSWORD, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
463 #endif
464 else
465 #ifdef __REACTOS__
466 /* ReactOS r57677 */
467 ScriptStringAnalyse(udc, es->text, length, (3*length/2+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
468 #else
469 ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
470 #endif
471
472 if (es->font)
473 SelectObject(udc, old_font);
474 if (udc != dc)
475 ReleaseDC(es->hwndSelf, udc);
476 }
477 return es->ssa;
478 }
479 else
480 {
481 line_def = es->first_line_def;
482 while (line_def && line)
483 {
484 line_def = line_def->next;
485 line--;
486 }
487
488 return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
489 }
490 }
491
492 static inline INT get_vertical_line_count(EDITSTATE *es)
493 {
494 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
495 return max(1,vlc);
496 }
497
498 /*********************************************************************
499 *
500 * EDIT_BuildLineDefs_ML
501 *
502 * Build linked list of text lines.
503 * Lines can end with '\0' (last line), a character (if it is wrapped),
504 * a soft return '\r\r\n' or a hard return '\r\n'
505 *
506 */
507 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
508 {
509 LPWSTR current_position, cp;
510 INT fw;
511 LINEDEF *current_line;
512 LINEDEF *previous_line;
513 LINEDEF *start_line;
514 INT line_index = 0, nstart_line = 0, nstart_index = 0;
515 INT line_count = es->line_count;
516 INT orig_net_length;
517 RECT rc;
518 INT vlc;
519
520 if (istart == iend && delta == 0)
521 return;
522
523 previous_line = NULL;
524 current_line = es->first_line_def;
525
526 /* Find starting line. istart must lie inside an existing line or
527 * at the end of buffer */
528 do {
529 if (istart < current_line->index + current_line->length ||
530 current_line->ending == END_0)
531 break;
532
533 previous_line = current_line;
534 current_line = current_line->next;
535 line_index++;
536 } while (current_line);
537
538 if (!current_line) /* Error occurred start is not inside previous buffer */
539 {
540 FIXME(" modification occurred outside buffer\n");
541 return;
542 }
543
544 /* Remember start of modifications in order to calculate update region */
545 nstart_line = line_index;
546 nstart_index = current_line->index;
547
548 /* We must start to reformat from the previous line since the modifications
549 * may have caused the line to wrap upwards. */
550 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
551 {
552 line_index--;
553 current_line = previous_line;
554 }
555 start_line = current_line;
556
557 fw = es->format_rect.right - es->format_rect.left;
558 current_position = es->text + current_line->index;
559 vlc = get_vertical_line_count(es);
560 do {
561 if (current_line != start_line)
562 {
563 if (!current_line || current_line->index + delta > current_position - es->text)
564 {
565 /* The buffer has been expanded, create a new line and
566 insert it into the link list */
567 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
568 #ifdef __REACTOS__
569 /* ReactOS r33509 */
570 if (new_line == NULL)
571 break;
572 #endif
573 new_line->next = previous_line->next;
574 previous_line->next = new_line;
575 current_line = new_line;
576 es->line_count++;
577 }
578 else if (current_line->index + delta < current_position - es->text)
579 {
580 /* The previous line merged with this line so we delete this extra entry */
581 previous_line->next = current_line->next;
582 HeapFree(GetProcessHeap(), 0, current_line);
583 current_line = previous_line->next;
584 es->line_count--;
585 continue;
586 }
587 else /* current_line->index + delta == current_position */
588 {
589 if (current_position - es->text > iend)
590 break; /* We reached end of line modifications */
591 /* else recalculate this line */
592 }
593 }
594
595 current_line->index = current_position - es->text;
596 orig_net_length = current_line->net_length;
597
598 /* Find end of line */
599 cp = current_position;
600 while (*cp) {
601 if (*cp == '\n') break;
602 if ((*cp == '\r') && (*(cp + 1) == '\n'))
603 break;
604 cp++;
605 }
606
607 /* Mark type of line termination */
608 if (!(*cp)) {
609 current_line->ending = END_0;
610 current_line->net_length = strlenW(current_position);
611 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
612 current_line->ending = END_SOFT;
613 current_line->net_length = cp - current_position - 1;
614 } else if (*cp == '\n') {
615 current_line->ending = END_RICH;
616 current_line->net_length = cp - current_position;
617 } else {
618 current_line->ending = END_HARD;
619 current_line->net_length = cp - current_position;
620 }
621
622 if (current_line->net_length)
623 {
624 const SIZE *sz;
625 EDIT_InvalidateUniscribeData_linedef(current_line);
626 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
627 if (current_line->ssa)
628 {
629 sz = ScriptString_pSize(current_line->ssa);
630 /* Calculate line width */
631 current_line->width = sz->cx;
632 }
633 else current_line->width = es->char_width * current_line->net_length;
634 }
635 else current_line->width = 0;
636
637 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
638
639 /* Line breaks just look back from the end and find the next break and try that. */
640
641 if (!(es->style & ES_AUTOHSCROLL)) {
642 if (current_line->width > fw && fw > es->char_width) {
643
644 INT prev, next;
645 int w;
646 const SIZE *sz;
647 float d;
648
649 prev = current_line->net_length - 1;
650 w = current_line->net_length;
651 d = (float)current_line->width/(float)fw;
652 if (d > 1.2) d -= 0.2;
653 next = prev/d;
654 if (next >= prev) next = prev-1;
655 do {
656 prev = EDIT_CallWordBreakProc(es, current_position - es->text,
657 next, current_line->net_length, WB_LEFT);
658 current_line->net_length = prev;
659 EDIT_InvalidateUniscribeData_linedef(current_line);
660 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
661 if (current_line->ssa)
662 sz = ScriptString_pSize(current_line->ssa);
663 else sz = 0;
664 if (sz)
665 current_line->width = sz->cx;
666 else
667 prev = 0;
668 next = prev - 1;
669 } while (prev && current_line->width > fw);
670 current_line->net_length = w;
671
672 if (prev == 0) { /* Didn't find a line break so force a break */
673 INT *piDx;
674 const INT *count;
675
676 EDIT_InvalidateUniscribeData_linedef(current_line);
677 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
678
679 if (current_line->ssa)
680 {
681 count = ScriptString_pcOutChars(current_line->ssa);
682 piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
683 ScriptStringGetLogicalWidths(current_line->ssa,piDx);
684
685 prev = current_line->net_length-1;
686 do {
687 current_line->width -= piDx[prev];
688 prev--;
689 } while ( prev > 0 && current_line->width > fw);
690 if (prev<=0)
691 prev = 1;
692 HeapFree(GetProcessHeap(),0,piDx);
693 }
694 else
695 prev = (fw / es->char_width);
696 }
697
698 /* If the first line we are calculating, wrapped before istart, we must
699 * adjust istart in order for this to be reflected in the update region. */
700 if (current_line->index == nstart_index && istart > current_line->index + prev)
701 istart = current_line->index + prev;
702 /* else if we are updating the previous line before the first line we
703 * are re-calculating and it expanded */
704 else if (current_line == start_line &&
705 current_line->index != nstart_index && orig_net_length < prev)
706 {
707 /* Line expanded due to an upwards line wrap so we must partially include
708 * previous line in update region */
709 nstart_line = line_index;
710 nstart_index = current_line->index;
711 istart = current_line->index + orig_net_length;
712 }
713
714 current_line->net_length = prev;
715 current_line->ending = END_WRAP;
716
717 if (current_line->net_length > 0)
718 {
719 EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
720 if (current_line->ssa)
721 {
722 sz = ScriptString_pSize(current_line->ssa);
723 current_line->width = sz->cx;
724 }
725 else
726 current_line->width = 0;
727 }
728 else current_line->width = 0;
729 }
730 else if (current_line == start_line &&
731 current_line->index != nstart_index &&
732 orig_net_length < current_line->net_length) {
733 /* The previous line expanded but it's still not as wide as the client rect */
734 /* The expansion is due to an upwards line wrap so we must partially include
735 it in the update region */
736 nstart_line = line_index;
737 nstart_index = current_line->index;
738 istart = current_line->index + orig_net_length;
739 }
740 }
741
742
743 /* Adjust length to include line termination */
744 switch (current_line->ending) {
745 case END_SOFT:
746 current_line->length = current_line->net_length + 3;
747 break;
748 case END_RICH:
749 current_line->length = current_line->net_length + 1;
750 break;
751 case END_HARD:
752 current_line->length = current_line->net_length + 2;
753 break;
754 case END_WRAP:
755 case END_0:
756 current_line->length = current_line->net_length;
757 break;
758 }
759 es->text_width = max(es->text_width, current_line->width);
760 current_position += current_line->length;
761 previous_line = current_line;
762
763 /* Discard data for non-visible lines. It will be calculated as needed */
764 if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
765 EDIT_InvalidateUniscribeData_linedef(current_line);
766
767 current_line = current_line->next;
768 line_index++;
769 } while (previous_line->ending != END_0);
770
771 /* Finish adjusting line indexes by delta or remove hanging lines */
772 if (previous_line->ending == END_0)
773 {
774 LINEDEF *pnext = NULL;
775
776 previous_line->next = NULL;
777 while (current_line)
778 {
779 pnext = current_line->next;
780 EDIT_InvalidateUniscribeData_linedef(current_line);
781 HeapFree(GetProcessHeap(), 0, current_line);
782 current_line = pnext;
783 es->line_count--;
784 }
785 }
786 else if (delta != 0)
787 {
788 while (current_line)
789 {
790 current_line->index += delta;
791 current_line = current_line->next;
792 }
793 }
794
795 /* Calculate rest of modification rectangle */
796 if (hrgn)
797 {
798 HRGN tmphrgn;
799 /*
800 * We calculate two rectangles. One for the first line which may have
801 * an indent with respect to the format rect. The other is a format-width
802 * rectangle that spans the rest of the lines that changed or moved.
803 */
804 rc.top = es->format_rect.top + nstart_line * es->line_height -
805 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
806 rc.bottom = rc.top + es->line_height;
807 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
808 rc.left = es->format_rect.left;
809 else
810 rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
811 rc.right = es->format_rect.right;
812 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
813
814 rc.top = rc.bottom;
815 rc.left = es->format_rect.left;
816 rc.right = es->format_rect.right;
817 /*
818 * If lines were added or removed we must re-paint the remainder of the
819 * lines since the remaining lines were either shifted up or down.
820 */
821 if (line_count < es->line_count) /* We added lines */
822 rc.bottom = es->line_count * es->line_height;
823 else if (line_count > es->line_count) /* We removed lines */
824 rc.bottom = line_count * es->line_height;
825 else
826 rc.bottom = line_index * es->line_height;
827 rc.bottom += es->format_rect.top;
828 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
829 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
830 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
831 DeleteObject(tmphrgn);
832 }
833 }
834
835 /*********************************************************************
836 *
837 * EDIT_CalcLineWidth_SL
838 *
839 */
840 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
841 {
842 EDIT_UpdateUniscribeData(es, NULL, 0);
843 if (es->ssa)
844 {
845 const SIZE *size;
846 size = ScriptString_pSize(es->ssa);
847 es->text_width = size->cx;
848 }
849 else
850 es->text_width = 0;
851 }
852
853 /*********************************************************************
854 *
855 * EDIT_CharFromPos
856 *
857 * Beware: This is not the function called on EM_CHARFROMPOS
858 * The position _can_ be outside the formatting / client
859 * rectangle
860 * The return value is only the character index
861 *
862 */
863 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
864 {
865 INT index;
866
867 if (es->style & ES_MULTILINE) {
868 int trailing;
869 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
870 INT line_index = 0;
871 LINEDEF *line_def = es->first_line_def;
872 EDIT_UpdateUniscribeData(es, NULL, line);
873 while ((line > 0) && line_def->next) {
874 line_index += line_def->length;
875 line_def = line_def->next;
876 line--;
877 }
878
879 x += es->x_offset - es->format_rect.left;
880 if (es->style & ES_RIGHT)
881 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
882 else if (es->style & ES_CENTER)
883 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
884 if (x >= line_def->width) {
885 if (after_wrap)
886 *after_wrap = (line_def->ending == END_WRAP);
887 return line_index + line_def->net_length;
888 }
889 if (x <= 0 || !line_def->ssa) {
890 if (after_wrap)
891 *after_wrap = FALSE;
892 return line_index;
893 }
894
895 ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
896 if (trailing) index++;
897 index += line_index;
898 if (after_wrap)
899 *after_wrap = ((index == line_index + line_def->net_length) &&
900 (line_def->ending == END_WRAP));
901 } else {
902 INT xoff = 0;
903 INT trailing;
904 if (after_wrap)
905 *after_wrap = FALSE;
906 x -= es->format_rect.left;
907 if (!x)
908 return es->x_offset;
909
910 if (!es->x_offset)
911 {
912 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
913 if (es->style & ES_RIGHT)
914 x -= indent;
915 else if (es->style & ES_CENTER)
916 x -= indent / 2;
917 }
918
919 EDIT_UpdateUniscribeData(es, NULL, 0);
920 if (es->x_offset)
921 {
922 if (es->ssa)
923 {
924 if (es->x_offset>= get_text_length(es))
925 {
926 const SIZE *size;
927 size = ScriptString_pSize(es->ssa);
928 xoff = size->cx;
929 }
930 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
931 }
932 else
933 xoff = 0;
934 }
935 if (x < 0)
936 {
937 if (x + xoff > 0 || !es->ssa)
938 {
939 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
940 if (trailing) index++;
941 }
942 else
943 index = 0;
944 }
945 else
946 {
947 if (x)
948 {
949 const SIZE *size = NULL;
950 if (es->ssa)
951 size = ScriptString_pSize(es->ssa);
952 if (!size)
953 index = 0;
954 else if (x > size->cx)
955 index = get_text_length(es);
956 else if (es->ssa)
957 {
958 ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
959 if (trailing) index++;
960 }
961 else
962 index = 0;
963 }
964 else
965 index = es->x_offset;
966 }
967 }
968 return index;
969 }
970
971
972 /*********************************************************************
973 *
974 * EDIT_ConfinePoint
975 *
976 * adjusts the point to be within the formatting rectangle
977 * (so CharFromPos returns the nearest _visible_ character)
978 *
979 */
980 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
981 {
982 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
983 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
984 }
985
986
987 /*********************************************************************
988 *
989 * EM_LINEFROMCHAR
990 *
991 */
992 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
993 {
994 INT line;
995 LINEDEF *line_def;
996
997 if (!(es->style & ES_MULTILINE))
998 return 0;
999 if (index > (INT)get_text_length(es))
1000 return es->line_count - 1;
1001 if (index == -1)
1002 index = min(es->selection_start, es->selection_end);
1003
1004 line = 0;
1005 line_def = es->first_line_def;
1006 index -= line_def->length;
1007 while ((index >= 0) && line_def->next) {
1008 line++;
1009 line_def = line_def->next;
1010 index -= line_def->length;
1011 }
1012 return line;
1013 }
1014
1015
1016 /*********************************************************************
1017 *
1018 * EM_LINEINDEX
1019 *
1020 */
1021 static INT EDIT_EM_LineIndex(const EDITSTATE *es, INT line)
1022 {
1023 INT line_index;
1024 const LINEDEF *line_def;
1025
1026 if (!(es->style & ES_MULTILINE))
1027 return 0;
1028 if (line >= es->line_count)
1029 return -1;
1030
1031 line_index = 0;
1032 line_def = es->first_line_def;
1033 if (line == -1) {
1034 INT index = es->selection_end - line_def->length;
1035 while ((index >= 0) && line_def->next) {
1036 line_index += line_def->length;
1037 line_def = line_def->next;
1038 index -= line_def->length;
1039 }
1040 } else {
1041 while (line > 0) {
1042 line_index += line_def->length;
1043 line_def = line_def->next;
1044 line--;
1045 }
1046 }
1047 return line_index;
1048 }
1049
1050
1051 /*********************************************************************
1052 *
1053 * EM_LINELENGTH
1054 *
1055 */
1056 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
1057 {
1058 LINEDEF *line_def;
1059
1060 if (!(es->style & ES_MULTILINE))
1061 return get_text_length(es);
1062
1063 if (index == -1) {
1064 /* get the number of remaining non-selected chars of selected lines */
1065 INT32 l; /* line number */
1066 INT32 li; /* index of first char in line */
1067 INT32 count;
1068 l = EDIT_EM_LineFromChar(es, es->selection_start);
1069 /* # chars before start of selection area */
1070 count = es->selection_start - EDIT_EM_LineIndex(es, l);
1071 l = EDIT_EM_LineFromChar(es, es->selection_end);
1072 /* # chars after end of selection */
1073 li = EDIT_EM_LineIndex(es, l);
1074 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1075 return count;
1076 }
1077 line_def = es->first_line_def;
1078 index -= line_def->length;
1079 while ((index >= 0) && line_def->next) {
1080 line_def = line_def->next;
1081 index -= line_def->length;
1082 }
1083 return line_def->net_length;
1084 }
1085
1086
1087 /*********************************************************************
1088 *
1089 * EM_POSFROMCHAR
1090 *
1091 */
1092 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
1093 {
1094 INT len = get_text_length(es);
1095 INT l;
1096 INT li;
1097 INT x = 0;
1098 INT y = 0;
1099 INT w;
1100 INT lw = 0;
1101 LINEDEF *line_def;
1102
1103 index = min(index, len);
1104 if (es->style & ES_MULTILINE) {
1105 l = EDIT_EM_LineFromChar(es, index);
1106 EDIT_UpdateUniscribeData(es, NULL, l);
1107
1108 y = (l - es->y_offset) * es->line_height;
1109 li = EDIT_EM_LineIndex(es, l);
1110 if (after_wrap && (li == index) && l) {
1111 INT l2 = l - 1;
1112 line_def = es->first_line_def;
1113 while (l2) {
1114 line_def = line_def->next;
1115 l2--;
1116 }
1117 if (line_def->ending == END_WRAP) {
1118 l--;
1119 y -= es->line_height;
1120 li = EDIT_EM_LineIndex(es, l);
1121 }
1122 }
1123
1124 line_def = es->first_line_def;
1125 while (line_def->index != li)
1126 line_def = line_def->next;
1127
1128 lw = line_def->width;
1129 w = es->format_rect.right - es->format_rect.left;
1130 if (line_def->ssa)
1131 {
1132 ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1133 x -= es->x_offset;
1134 }
1135 else
1136 x = es->x_offset;
1137
1138 if (es->style & ES_RIGHT)
1139 x = w - (lw - x);
1140 else if (es->style & ES_CENTER)
1141 x += (w - lw) / 2;
1142 } else {
1143 INT xoff = 0;
1144 INT xi = 0;
1145 EDIT_UpdateUniscribeData(es, NULL, 0);
1146 if (es->x_offset)
1147 {
1148 if (es->ssa)
1149 {
1150 if (es->x_offset >= get_text_length(es))
1151 {
1152 int leftover = es->x_offset - get_text_length(es);
1153 if (es->ssa)
1154 {
1155 const SIZE *size;
1156 size = ScriptString_pSize(es->ssa);
1157 xoff = size->cx;
1158 }
1159 else
1160 xoff = 0;
1161 xoff += es->char_width * leftover;
1162 }
1163 else
1164 ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1165 }
1166 else
1167 xoff = 0;
1168 }
1169 if (index)
1170 {
1171 if (index >= get_text_length(es))
1172 {
1173 if (es->ssa)
1174 {
1175 const SIZE *size;
1176 size = ScriptString_pSize(es->ssa);
1177 xi = size->cx;
1178 }
1179 else
1180 xi = 0;
1181 }
1182 else if (es->ssa)
1183 ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1184 else
1185 xi = 0;
1186 }
1187 x = xi - xoff;
1188
1189 if (index >= es->x_offset) {
1190 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1191 {
1192 w = es->format_rect.right - es->format_rect.left;
1193 if (w > es->text_width)
1194 {
1195 if (es->style & ES_RIGHT)
1196 x += w - es->text_width;
1197 else if (es->style & ES_CENTER)
1198 x += (w - es->text_width) / 2;
1199 }
1200 }
1201 }
1202 y = 0;
1203 }
1204 x += es->format_rect.left;
1205 y += es->format_rect.top;
1206 return MAKELONG((INT16)x, (INT16)y);
1207 }
1208
1209
1210 /*********************************************************************
1211 *
1212 * EDIT_GetLineRect
1213 *
1214 * Calculates the bounding rectangle for a line from a starting
1215 * column to an ending column.
1216 *
1217 */
1218 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1219 {
1220 SCRIPT_STRING_ANALYSIS ssa;
1221 INT line_index = 0;
1222 INT pt1, pt2, pt3;
1223
1224 if (es->style & ES_MULTILINE)
1225 {
1226 const LINEDEF *line_def = NULL;
1227 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1228 if (line >= es->line_count)
1229 return;
1230
1231 line_def = es->first_line_def;
1232 if (line == -1) {
1233 INT index = es->selection_end - line_def->length;
1234 while ((index >= 0) && line_def->next) {
1235 line_index += line_def->length;
1236 line_def = line_def->next;
1237 index -= line_def->length;
1238 }
1239 } else {
1240 while (line > 0) {
1241 line_index += line_def->length;
1242 line_def = line_def->next;
1243 line--;
1244 }
1245 }
1246 ssa = line_def->ssa;
1247 }
1248 else
1249 {
1250 line_index = 0;
1251 rc->top = es->format_rect.top;
1252 ssa = es->ssa;
1253 }
1254
1255 rc->bottom = rc->top + es->line_height;
1256 pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1257 pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1258 if (ssa)
1259 {
1260 ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1261 pt3+=es->format_rect.left;
1262 }
1263 else pt3 = pt1;
1264 rc->right = max(max(pt1 , pt2),pt3);
1265 rc->left = min(min(pt1, pt2),pt3);
1266 }
1267
1268
1269 static inline void text_buffer_changed(EDITSTATE *es)
1270 {
1271 es->text_length = (UINT)-1;
1272
1273 HeapFree( GetProcessHeap(), 0, es->logAttr );
1274 es->logAttr = NULL;
1275 EDIT_InvalidateUniscribeData(es);
1276 }
1277
1278 /*********************************************************************
1279 * EDIT_LockBuffer
1280 *
1281 */
1282 static void EDIT_LockBuffer(EDITSTATE *es)
1283 {
1284 if (es->hlocapp) return;
1285
1286 if (!es->text) {
1287
1288 #ifdef __REACTOS__
1289 /* FIXME: What is this ? */
1290 CHAR *textA = NULL; // ReactOS Hacked! r45670
1291 //UINT countA = 0;
1292
1293 if(es->hloc32W)
1294 {
1295 if(es->hloc32A)
1296 {
1297 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1298 textA = LocalLock(es->hloc32A);
1299 //countA = strlen(textA) + 1;
1300 }
1301 }
1302 else {
1303 ERR("no buffer ... please report\n");
1304 return;
1305 }
1306
1307 if (textA) //// ReactOS
1308 {
1309 #else
1310 if(!es->hloc32W) return;
1311
1312 if(es->hloc32A)
1313 {
1314 CHAR *textA = LocalLock(es->hloc32A);
1315 #endif
1316 HLOCAL hloc32W_new;
1317 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1318 if(countW_new > es->buffer_size + 1)
1319 {
1320 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1321 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1322 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1323 if(hloc32W_new)
1324 {
1325 es->hloc32W = hloc32W_new;
1326 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1327 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1328 }
1329 else
1330 WARN("FAILED! Will synchronize partially\n");
1331 }
1332 es->text = LocalLock(es->hloc32W);
1333 MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1334 LocalUnlock(es->hloc32A);
1335 }
1336 else es->text = LocalLock(es->hloc32W);
1337 }
1338 es->lock_count++;
1339 }
1340
1341
1342 /*********************************************************************
1343 *
1344 * EDIT_UnlockBuffer
1345 *
1346 */
1347 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1348 {
1349 if (es->hlocapp) return;
1350
1351 /* Edit window might be already destroyed */
1352 if(!IsWindow(es->hwndSelf))
1353 {
1354 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1355 return;
1356 }
1357
1358 if (!es->lock_count) {
1359 ERR("lock_count == 0 ... please report\n");
1360 return;
1361 }
1362 if (!es->text) {
1363 ERR("es->text == 0 ... please report\n");
1364 return;
1365 }
1366
1367 if (force || (es->lock_count == 1)) {
1368 if (es->hloc32W) {
1369 UINT countA = 0;
1370 UINT countW = get_text_length(es) + 1;
1371
1372 if(es->hloc32A)
1373 {
1374 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1375 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1376 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1377 countA = LocalSize(es->hloc32A);
1378 if(countA_new > countA)
1379 {
1380 HLOCAL hloc32A_new;
1381 UINT alloc_size = ROUND_TO_GROW(countA_new);
1382 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1383 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1384 if(hloc32A_new)
1385 {
1386 es->hloc32A = hloc32A_new;
1387 countA = LocalSize(hloc32A_new);
1388 TRACE("Real new size %d bytes\n", countA);
1389 }
1390 else
1391 WARN("FAILED! Will synchronize partially\n");
1392 }
1393 WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1394 LocalLock(es->hloc32A), countA, NULL, NULL);
1395 LocalUnlock(es->hloc32A);
1396 }
1397
1398 LocalUnlock(es->hloc32W);
1399 es->text = NULL;
1400 }
1401 else {
1402 ERR("no buffer ... please report\n");
1403 return;
1404 }
1405 }
1406 es->lock_count--;
1407 }
1408
1409
1410 /*********************************************************************
1411 *
1412 * EDIT_MakeFit
1413 *
1414 * Try to fit size + 1 characters in the buffer.
1415 */
1416 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1417 {
1418 HLOCAL hNew32W;
1419
1420 if (size <= es->buffer_size)
1421 return TRUE;
1422
1423 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1424
1425 /* Force edit to unlock its buffer. es->text now NULL */
1426 EDIT_UnlockBuffer(es, TRUE);
1427
1428 if (es->hloc32W) {
1429 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1430 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1431 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1432 es->hloc32W = hNew32W;
1433 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1434 }
1435 }
1436
1437 EDIT_LockBuffer(es);
1438
1439 if (es->buffer_size < size) {
1440 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1441 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1442 return FALSE;
1443 } else {
1444 TRACE("We now have %d+1\n", es->buffer_size);
1445 return TRUE;
1446 }
1447 }
1448
1449
1450 /*********************************************************************
1451 *
1452 * EDIT_MakeUndoFit
1453 *
1454 * Try to fit size + 1 bytes in the undo buffer.
1455 *
1456 */
1457 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1458 {
1459 UINT alloc_size;
1460
1461 if (size <= es->undo_buffer_size)
1462 return TRUE;
1463
1464 TRACE("trying to ReAlloc to %d+1\n", size);
1465
1466 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1467 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1468 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1469 return TRUE;
1470 }
1471 else
1472 {
1473 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1474 return FALSE;
1475 }
1476 }
1477
1478
1479 /*********************************************************************
1480 *
1481 * EDIT_UpdateTextRegion
1482 *
1483 */
1484 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1485 {
1486 if (es->flags & EF_UPDATE) {
1487 es->flags &= ~EF_UPDATE;
1488 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1489 }
1490 InvalidateRgn(es->hwndSelf, hrgn, bErase);
1491 }
1492
1493
1494 /*********************************************************************
1495 *
1496 * EDIT_UpdateText
1497 *
1498 */
1499 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1500 {
1501 if (es->flags & EF_UPDATE) {
1502 es->flags &= ~EF_UPDATE;
1503 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
1504 }
1505 InvalidateRect(es->hwndSelf, rc, bErase);
1506 }
1507
1508 /*********************************************************************
1509 *
1510 * EDIT_SL_InvalidateText
1511 *
1512 * Called from EDIT_InvalidateText().
1513 * Does the job for single-line controls only.
1514 *
1515 */
1516 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1517 {
1518 RECT line_rect;
1519 RECT rc;
1520
1521 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1522 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1523 EDIT_UpdateText(es, &rc, TRUE);
1524 }
1525
1526 /*********************************************************************
1527 *
1528 * EDIT_ML_InvalidateText
1529 *
1530 * Called from EDIT_InvalidateText().
1531 * Does the job for multi-line controls only.
1532 *
1533 */
1534 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1535 {
1536 INT vlc = get_vertical_line_count(es);
1537 INT sl = EDIT_EM_LineFromChar(es, start);
1538 INT el = EDIT_EM_LineFromChar(es, end);
1539 INT sc;
1540 INT ec;
1541 RECT rc1;
1542 RECT rcWnd;
1543 RECT rcLine;
1544 RECT rcUpdate;
1545 INT l;
1546
1547 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1548 return;
1549
1550 sc = start - EDIT_EM_LineIndex(es, sl);
1551 ec = end - EDIT_EM_LineIndex(es, el);
1552 if (sl < es->y_offset) {
1553 sl = es->y_offset;
1554 sc = 0;
1555 }
1556 if (el > es->y_offset + vlc) {
1557 el = es->y_offset + vlc;
1558 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1559 }
1560 GetClientRect(es->hwndSelf, &rc1);
1561 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1562 if (sl == el) {
1563 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1564 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1565 EDIT_UpdateText(es, &rcUpdate, TRUE);
1566 } else {
1567 EDIT_GetLineRect(es, sl, sc,
1568 EDIT_EM_LineLength(es,
1569 EDIT_EM_LineIndex(es, sl)),
1570 &rcLine);
1571 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1572 EDIT_UpdateText(es, &rcUpdate, TRUE);
1573 for (l = sl + 1 ; l < el ; l++) {
1574 EDIT_GetLineRect(es, l, 0,
1575 EDIT_EM_LineLength(es,
1576 EDIT_EM_LineIndex(es, l)),
1577 &rcLine);
1578 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1579 EDIT_UpdateText(es, &rcUpdate, TRUE);
1580 }
1581 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1582 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1583 EDIT_UpdateText(es, &rcUpdate, TRUE);
1584 }
1585 }
1586
1587
1588 /*********************************************************************
1589 *
1590 * EDIT_InvalidateText
1591 *
1592 * Invalidate the text from offset start up to, but not including,
1593 * offset end. Useful for (re)painting the selection.
1594 * Regions outside the linewidth are not invalidated.
1595 * end == -1 means end == TextLength.
1596 * start and end need not be ordered.
1597 *
1598 */
1599 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1600 {
1601 if (end == start)
1602 return;
1603
1604 if (end == -1)
1605 end = get_text_length(es);
1606
1607 if (end < start) {
1608 INT tmp = start;
1609 start = end;
1610 end = tmp;
1611 }
1612
1613 if (es->style & ES_MULTILINE)
1614 EDIT_ML_InvalidateText(es, start, end);
1615 else
1616 EDIT_SL_InvalidateText(es, start, end);
1617 }
1618
1619
1620 /*********************************************************************
1621 *
1622 * EDIT_EM_SetSel
1623 *
1624 * note: unlike the specs say: the order of start and end
1625 * _is_ preserved in Windows. (i.e. start can be > end)
1626 * In other words: this handler is OK
1627 *
1628 */
1629 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1630 {
1631 UINT old_start = es->selection_start;
1632 UINT old_end = es->selection_end;
1633 UINT len = get_text_length(es);
1634
1635 if (start == (UINT)-1) {
1636 start = es->selection_end;
1637 end = es->selection_end;
1638 } else {
1639 start = min(start, len);
1640 end = min(end, len);
1641 }
1642 es->selection_start = start;
1643 es->selection_end = end;
1644 if (after_wrap)
1645 es->flags |= EF_AFTER_WRAP;
1646 else
1647 es->flags &= ~EF_AFTER_WRAP;
1648 /* Compute the necessary invalidation region. */
1649 /* Note that we don't need to invalidate regions which have
1650 * "never" been selected, or those which are "still" selected.
1651 * In fact, every time we hit a selection boundary, we can
1652 * *toggle* whether we need to invalidate. Thus we can optimize by
1653 * *sorting* the interval endpoints. Let's assume that we sort them
1654 * in this order:
1655 * start <= end <= old_start <= old_end
1656 * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1657 * in 5 comparisons; i.e. it is impossible to do better than the
1658 * following: */
1659 ORDER_UINT(end, old_end);
1660 ORDER_UINT(start, old_start);
1661 ORDER_UINT(old_start, old_end);
1662 ORDER_UINT(start, end);
1663 /* Note that at this point 'end' and 'old_start' are not in order, but
1664 * start is definitely the min. and old_end is definitely the max. */
1665 if (end != old_start)
1666 {
1667 /*
1668 * One can also do
1669 * ORDER_UINT32(end, old_start);
1670 * EDIT_InvalidateText(es, start, end);
1671 * EDIT_InvalidateText(es, old_start, old_end);
1672 * in place of the following if statement.
1673 * (That would complete the optimal five-comparison four-element sort.)
1674 */
1675 if (old_start > end )
1676 {
1677 EDIT_InvalidateText(es, start, end);
1678 EDIT_InvalidateText(es, old_start, old_end);
1679 }
1680 else
1681 {
1682 EDIT_InvalidateText(es, start, old_start);
1683 EDIT_InvalidateText(es, end, old_end);
1684 }
1685 }
1686 else EDIT_InvalidateText(es, start, old_end);
1687 }
1688
1689
1690 /*********************************************************************
1691 *
1692 * EDIT_UpdateScrollInfo
1693 *
1694 */
1695 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
1696 {
1697 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1698 {
1699 SCROLLINFO si;
1700 si.cbSize = sizeof(SCROLLINFO);
1701 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1702 si.nMin = 0;
1703 si.nMax = es->line_count - 1;
1704 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1705 si.nPos = es->y_offset;
1706 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1707 si.nMin, si.nMax, si.nPage, si.nPos);
1708 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1709 }
1710
1711 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1712 {
1713 SCROLLINFO si;
1714 si.cbSize = sizeof(SCROLLINFO);
1715 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
1716 si.nMin = 0;
1717 si.nMax = es->text_width - 1;
1718 si.nPage = es->format_rect.right - es->format_rect.left;
1719 si.nPos = es->x_offset;
1720 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1721 si.nMin, si.nMax, si.nPage, si.nPos);
1722 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1723 }
1724 }
1725
1726
1727 /*********************************************************************
1728 *
1729 * EDIT_EM_LineScroll_internal
1730 *
1731 * Version of EDIT_EM_LineScroll for internal use.
1732 * It doesn't refuse if ES_MULTILINE is set and assumes that
1733 * dx is in pixels, dy - in lines.
1734 *
1735 */
1736 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
1737 {
1738 INT nyoff;
1739 INT x_offset_in_pixels;
1740 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1741 es->line_height;
1742
1743 if (es->style & ES_MULTILINE)
1744 {
1745 x_offset_in_pixels = es->x_offset;
1746 }
1747 else
1748 {
1749 dy = 0;
1750 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1751 }
1752
1753 if (-dx > x_offset_in_pixels)
1754 dx = -x_offset_in_pixels;
1755 if (dx > es->text_width - x_offset_in_pixels)
1756 dx = es->text_width - x_offset_in_pixels;
1757 nyoff = max(0, es->y_offset + dy);
1758 if (nyoff >= es->line_count - lines_per_page)
1759 nyoff = max(0, es->line_count - lines_per_page);
1760 dy = (es->y_offset - nyoff) * es->line_height;
1761 if (dx || dy) {
1762 RECT rc1;
1763 RECT rc;
1764
1765 es->y_offset = nyoff;
1766 if(es->style & ES_MULTILINE)
1767 es->x_offset += dx;
1768 else
1769 es->x_offset += dx / es->char_width;
1770
1771 GetClientRect(es->hwndSelf, &rc1);
1772 IntersectRect(&rc, &rc1, &es->format_rect);
1773 ScrollWindowEx(es->hwndSelf, -dx, dy,
1774 NULL, &rc, NULL, NULL, SW_INVALIDATE);
1775 /* force scroll info update */
1776 EDIT_UpdateScrollInfo(es);
1777 }
1778 if (dx && !(es->flags & EF_HSCROLL_TRACK))
1779 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
1780 if (dy && !(es->flags & EF_VSCROLL_TRACK))
1781 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
1782 return TRUE;
1783 }
1784
1785 /*********************************************************************
1786 *
1787 * EM_LINESCROLL
1788 *
1789 * NOTE: dx is in average character widths, dy - in lines;
1790 *
1791 */
1792 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
1793 {
1794 if (!(es->style & ES_MULTILINE))
1795 return FALSE;
1796
1797 dx *= es->char_width;
1798 return EDIT_EM_LineScroll_internal(es, dx, dy);
1799 }
1800
1801
1802 /*********************************************************************
1803 *
1804 * EM_SCROLL
1805 *
1806 */
1807 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
1808 {
1809 INT dy;
1810
1811 if (!(es->style & ES_MULTILINE))
1812 return (LRESULT)FALSE;
1813
1814 dy = 0;
1815
1816 switch (action) {
1817 case SB_LINEUP:
1818 if (es->y_offset)
1819 dy = -1;
1820 break;
1821 case SB_LINEDOWN:
1822 if (es->y_offset < es->line_count - 1)
1823 dy = 1;
1824 break;
1825 case SB_PAGEUP:
1826 if (es->y_offset)
1827 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1828 break;
1829 case SB_PAGEDOWN:
1830 if (es->y_offset < es->line_count - 1)
1831 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1832 break;
1833 default:
1834 return (LRESULT)FALSE;
1835 }
1836 if (dy) {
1837 INT vlc = get_vertical_line_count(es);
1838 /* check if we are going to move too far */
1839 if(es->y_offset + dy > es->line_count - vlc)
1840 dy = max(es->line_count - vlc, 0) - es->y_offset;
1841
1842 /* Notification is done in EDIT_EM_LineScroll */
1843 if(dy) {
1844 EDIT_EM_LineScroll(es, 0, dy);
1845 return MAKELONG(dy, TRUE);
1846 }
1847
1848 }
1849 return (LRESULT)FALSE;
1850 }
1851
1852
1853 /*********************************************************************
1854 *
1855 * EDIT_SetCaretPos
1856 *
1857 */
1858 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
1859 BOOL after_wrap)
1860 {
1861 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1862 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1863 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1864 }
1865
1866
1867 /*********************************************************************
1868 *
1869 * EM_SCROLLCARET
1870 *
1871 */
1872 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
1873 {
1874 if (es->style & ES_MULTILINE) {
1875 INT l;
1876 INT vlc;
1877 INT ww;
1878 INT cw = es->char_width;
1879 INT x;
1880 INT dy = 0;
1881 INT dx = 0;
1882
1883 l = EDIT_EM_LineFromChar(es, es->selection_end);
1884 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1885 vlc = get_vertical_line_count(es);
1886 if (l >= es->y_offset + vlc)
1887 dy = l - vlc + 1 - es->y_offset;
1888 if (l < es->y_offset)
1889 dy = l - es->y_offset;
1890 ww = es->format_rect.right - es->format_rect.left;
1891 if (x < es->format_rect.left)
1892 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1893 if (x > es->format_rect.right)
1894 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1895 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1896 {
1897 /* check if we are going to move too far */
1898 if(es->x_offset + dx + ww > es->text_width)
1899 dx = es->text_width - ww - es->x_offset;
1900 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1901 EDIT_EM_LineScroll_internal(es, dx, dy);
1902 }
1903 } else {
1904 INT x;
1905 INT goal;
1906 INT format_width;
1907
1908 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1909 format_width = es->format_rect.right - es->format_rect.left;
1910 if (x < es->format_rect.left) {
1911 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1912 do {
1913 es->x_offset--;
1914 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1915 } while ((x < goal) && es->x_offset);
1916 /* FIXME: use ScrollWindow() somehow to improve performance */
1917 EDIT_UpdateText(es, NULL, TRUE);
1918 } else if (x > es->format_rect.right) {
1919 INT x_last;
1920 INT len = get_text_length(es);
1921 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1922 do {
1923 es->x_offset++;
1924 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1925 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1926 } while ((x > goal) && (x_last > es->format_rect.right));
1927 /* FIXME: use ScrollWindow() somehow to improve performance */
1928 EDIT_UpdateText(es, NULL, TRUE);
1929 }
1930 }
1931
1932 if(es->flags & EF_FOCUSED)
1933 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1934 }
1935
1936
1937 /*********************************************************************
1938 *
1939 * EDIT_MoveBackward
1940 *
1941 */
1942 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1943 {
1944 INT e = es->selection_end;
1945
1946 if (e) {
1947 e--;
1948 if ((es->style & ES_MULTILINE) && e &&
1949 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1950 e--;
1951 if (e && (es->text[e - 1] == '\r'))
1952 e--;
1953 }
1954 }
1955 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1956 EDIT_EM_ScrollCaret(es);
1957 }
1958
1959
1960 /*********************************************************************
1961 *
1962 * EDIT_MoveDown_ML
1963 *
1964 * Only for multi line controls
1965 * Move the caret one line down, on a column with the nearest
1966 * x coordinate on the screen (might be a different column).
1967 *
1968 */
1969 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1970 {
1971 INT s = es->selection_start;
1972 INT e = es->selection_end;
1973 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1974 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1975 INT x = (short)LOWORD(pos);
1976 INT y = (short)HIWORD(pos);
1977
1978 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1979 if (!extend)
1980 s = e;
1981 EDIT_EM_SetSel(es, s, e, after_wrap);
1982 EDIT_EM_ScrollCaret(es);
1983 }
1984
1985
1986 /*********************************************************************
1987 *
1988 * EDIT_MoveEnd
1989 *
1990 */
1991 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1992 {
1993 BOOL after_wrap = FALSE;
1994 INT e;
1995
1996 /* Pass a high value in x to make sure of receiving the end of the line */
1997 if (!ctrl && (es->style & ES_MULTILINE))
1998 e = EDIT_CharFromPos(es, 0x3fffffff,
1999 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2000 else
2001 e = get_text_length(es);
2002 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2003 EDIT_EM_ScrollCaret(es);
2004 }
2005
2006
2007 /*********************************************************************
2008 *
2009 * EDIT_MoveForward
2010 *
2011 */
2012 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2013 {
2014 INT e = es->selection_end;
2015
2016 if (es->text[e]) {
2017 e++;
2018 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2019 if (es->text[e] == '\n')
2020 e++;
2021 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2022 e += 2;
2023 }
2024 }
2025 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2026 EDIT_EM_ScrollCaret(es);
2027 }
2028
2029
2030 /*********************************************************************
2031 *
2032 * EDIT_MoveHome
2033 *
2034 * Home key: move to beginning of line.
2035 *
2036 */
2037 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
2038 {
2039 INT e;
2040
2041 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2042 if (!ctrl && (es->style & ES_MULTILINE))
2043 e = EDIT_CharFromPos(es, -es->x_offset,
2044 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2045 else
2046 e = 0;
2047 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2048 EDIT_EM_ScrollCaret(es);
2049 }
2050
2051
2052 /*********************************************************************
2053 *
2054 * EDIT_MovePageDown_ML
2055 *
2056 * Only for multi line controls
2057 * Move the caret one page down, on a column with the nearest
2058 * x coordinate on the screen (might be a different column).
2059 *
2060 */
2061 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2062 {
2063 INT s = es->selection_start;
2064 INT e = es->selection_end;
2065 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2066 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2067 INT x = (short)LOWORD(pos);
2068 INT y = (short)HIWORD(pos);
2069
2070 e = EDIT_CharFromPos(es, x,
2071 y + (es->format_rect.bottom - es->format_rect.top),
2072 &after_wrap);
2073 if (!extend)
2074 s = e;
2075 EDIT_EM_SetSel(es, s, e, after_wrap);
2076 EDIT_EM_ScrollCaret(es);
2077 }
2078
2079
2080 /*********************************************************************
2081 *
2082 * EDIT_MovePageUp_ML
2083 *
2084 * Only for multi line controls
2085 * Move the caret one page up, on a column with the nearest
2086 * x coordinate on the screen (might be a different column).
2087 *
2088 */
2089 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2090 {
2091 INT s = es->selection_start;
2092 INT e = es->selection_end;
2093 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2094 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2095 INT x = (short)LOWORD(pos);
2096 INT y = (short)HIWORD(pos);
2097
2098 e = EDIT_CharFromPos(es, x,
2099 y - (es->format_rect.bottom - es->format_rect.top),
2100 &after_wrap);
2101 if (!extend)
2102 s = e;
2103 EDIT_EM_SetSel(es, s, e, after_wrap);
2104 EDIT_EM_ScrollCaret(es);
2105 }
2106
2107
2108 /*********************************************************************
2109 *
2110 * EDIT_MoveUp_ML
2111 *
2112 * Only for multi line controls
2113 * Move the caret one line up, on a column with the nearest
2114 * x coordinate on the screen (might be a different column).
2115 *
2116 */
2117 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2118 {
2119 INT s = es->selection_start;
2120 INT e = es->selection_end;
2121 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2122 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2123 INT x = (short)LOWORD(pos);
2124 INT y = (short)HIWORD(pos);
2125
2126 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2127 if (!extend)
2128 s = e;
2129 EDIT_EM_SetSel(es, s, e, after_wrap);
2130 EDIT_EM_ScrollCaret(es);
2131 }
2132
2133
2134 /*********************************************************************
2135 *
2136 * EDIT_MoveWordBackward
2137 *
2138 */
2139 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2140 {
2141 INT s = es->selection_start;
2142 INT e = es->selection_end;
2143 INT l;
2144 INT ll;
2145 INT li;
2146
2147 l = EDIT_EM_LineFromChar(es, e);
2148 ll = EDIT_EM_LineLength(es, e);
2149 li = EDIT_EM_LineIndex(es, l);
2150 if (e - li == 0) {
2151 if (l) {
2152 li = EDIT_EM_LineIndex(es, l - 1);
2153 e = li + EDIT_EM_LineLength(es, li);
2154 }
2155 } else {
2156 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2157 }
2158 if (!extend)
2159 s = e;
2160 EDIT_EM_SetSel(es, s, e, FALSE);
2161 EDIT_EM_ScrollCaret(es);
2162 }
2163
2164
2165 /*********************************************************************
2166 *
2167 * EDIT_MoveWordForward
2168 *
2169 */
2170 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2171 {
2172 INT s = es->selection_start;
2173 INT e = es->selection_end;
2174 INT l;
2175 INT ll;
2176 INT li;
2177
2178 l = EDIT_EM_LineFromChar(es, e);
2179 ll = EDIT_EM_LineLength(es, e);
2180 li = EDIT_EM_LineIndex(es, l);
2181 if (e - li == ll) {
2182 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2183 e = EDIT_EM_LineIndex(es, l + 1);
2184 } else {
2185 e = li + EDIT_CallWordBreakProc(es,
2186 li, e - li + 1, ll, WB_RIGHT);
2187 }
2188 if (!extend)
2189 s = e;
2190 EDIT_EM_SetSel(es, s, e, FALSE);
2191 EDIT_EM_ScrollCaret(es);
2192 }
2193
2194
2195 /*********************************************************************
2196 *
2197 * EDIT_PaintText
2198 *
2199 */
2200 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2201 {
2202 COLORREF BkColor;
2203 COLORREF TextColor;
2204 LOGFONTW underline_font;
2205 HFONT hUnderline = 0;
2206 HFONT old_font = 0;
2207 INT ret;
2208 INT li;
2209 INT BkMode;
2210 SIZE size;
2211
2212 if (!count)
2213 return 0;
2214 BkMode = GetBkMode(dc);
2215 BkColor = GetBkColor(dc);
2216 TextColor = GetTextColor(dc);
2217 if (rev) {
2218 if (es->composition_len == 0)
2219 {
2220 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2221 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2222 SetBkMode( dc, OPAQUE);
2223 }
2224 else
2225 {
2226 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2227 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2228 underline_font.lfUnderline = TRUE;
2229 hUnderline = CreateFontIndirectW(&underline_font);
2230 old_font = SelectObject(dc,hUnderline);
2231 }
2232 }
2233 li = EDIT_EM_LineIndex(es, line);
2234 if (es->style & ES_MULTILINE) {
2235 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2236 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2237 } else {
2238 LPWSTR text = es->text;
2239 TextOutW(dc, x, y, text + li + col, count);
2240 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2241 ret = size.cx;
2242 if (es->style & ES_PASSWORD)
2243 HeapFree(GetProcessHeap(), 0, text);
2244 }
2245 if (rev) {
2246 if (es->composition_len == 0)
2247 {
2248 SetBkColor(dc, BkColor);
2249 SetTextColor(dc, TextColor);
2250 SetBkMode( dc, BkMode);
2251 }
2252 else
2253 {
2254 if (old_font)
2255 SelectObject(dc,old_font);
2256 if (hUnderline)
2257 DeleteObject(hUnderline);
2258 }
2259 }
2260 return ret;
2261 }
2262
2263
2264 /*********************************************************************
2265 *
2266 * EDIT_PaintLine
2267 *
2268 */
2269 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2270 {
2271 INT s = 0;
2272 INT e = 0;
2273 INT li = 0;
2274 INT ll = 0;
2275 INT x;
2276 INT y;
2277 LRESULT pos;
2278 SCRIPT_STRING_ANALYSIS ssa;
2279
2280 if (es->style & ES_MULTILINE) {
2281 INT vlc = get_vertical_line_count(es);
2282
2283 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2284 return;
2285 } else if (line)
2286 return;
2287
2288 TRACE("line=%d\n", line);
2289
2290 ssa = EDIT_UpdateUniscribeData(es, dc, line);
2291 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2292 x = (short)LOWORD(pos);
2293 y = (short)HIWORD(pos);
2294
2295 if (es->style & ES_MULTILINE)
2296 {
2297 int line_idx = line;
2298 x = -es->x_offset;
2299 if (es->style & ES_RIGHT || es->style & ES_CENTER)
2300 {
2301 LINEDEF *line_def = es->first_line_def;
2302 int w, lw;
2303
2304 while (line_def && line_idx)
2305 {
2306 line_def = line_def->next;
2307 line_idx--;
2308 }
2309 w = es->format_rect.right - es->format_rect.left;
2310 lw = line_def->width;
2311
2312 if (es->style & ES_RIGHT)
2313 x = w - (lw - x);
2314 else if (es->style & ES_CENTER)
2315 x += (w - lw) / 2;
2316 }
2317 x += es->format_rect.left;
2318 }
2319
2320 if (rev)
2321 {
2322 li = EDIT_EM_LineIndex(es, line);
2323 ll = EDIT_EM_LineLength(es, li);
2324 s = min(es->selection_start, es->selection_end);
2325 e = max(es->selection_start, es->selection_end);
2326 s = min(li + ll, max(li, s));
2327 e = min(li + ll, max(li, e));
2328 }
2329
2330 if (ssa)
2331 ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2332 else if (rev && (s != e) &&
2333 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2334 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2335 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2336 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2337 } else
2338 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2339 }
2340
2341
2342 /*********************************************************************
2343 *
2344 * EDIT_AdjustFormatRect
2345 *
2346 * Adjusts the format rectangle for the current font and the
2347 * current client rectangle.
2348 *
2349 */
2350 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2351 {
2352 RECT ClientRect;
2353
2354 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2355 if (es->style & ES_MULTILINE)
2356 {
2357 INT fw, vlc, max_x_offset, max_y_offset;
2358
2359 vlc = get_vertical_line_count(es);
2360 es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2361
2362 /* correct es->x_offset */
2363 fw = es->format_rect.right - es->format_rect.left;
2364 max_x_offset = es->text_width - fw;
2365 if(max_x_offset < 0) max_x_offset = 0;
2366 if(es->x_offset > max_x_offset)
2367 es->x_offset = max_x_offset;
2368
2369 /* correct es->y_offset */
2370 max_y_offset = es->line_count - vlc;
2371 if(max_y_offset < 0) max_y_offset = 0;
2372 if(es->y_offset > max_y_offset)
2373 es->y_offset = max_y_offset;
2374
2375 /* force scroll info update */
2376 EDIT_UpdateScrollInfo(es);
2377 }
2378 else
2379 /* Windows doesn't care to fix text placement for SL controls */
2380 es->format_rect.bottom = es->format_rect.top + es->line_height;
2381
2382 /* Always stay within the client area */
2383 GetClientRect(es->hwndSelf, &ClientRect);
2384 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2385
2386 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2387 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2388
2389 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2390 }
2391
2392
2393 /*********************************************************************
2394 *
2395 * EDIT_SetRectNP
2396 *
2397 * note: this is not (exactly) the handler called on EM_SETRECTNP
2398 * it is also used to set the rect of a single line control
2399 *
2400 */
2401 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2402 {
2403 LONG_PTR ExStyle;
2404 INT bw, bh;
2405 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2406
2407 CopyRect(&es->format_rect, rc);
2408
2409 if (ExStyle & WS_EX_CLIENTEDGE) {
2410 es->format_rect.left++;
2411 es->format_rect.right--;
2412
2413 if (es->format_rect.bottom - es->format_rect.top
2414 >= es->line_height + 2)
2415 {
2416 es->format_rect.top++;
2417 es->format_rect.bottom--;
2418 }
2419 }
2420 else if (es->style & WS_BORDER) {
2421 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2422 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2423 es->format_rect.left += bw;
2424 es->format_rect.right -= bw;
2425 if (es->format_rect.bottom - es->format_rect.top
2426 >= es->line_height + 2 * bh)
2427 {
2428 es->format_rect.top += bh;
2429 es->format_rect.bottom -= bh;
2430 }
2431 }
2432
2433 es->format_rect.left += es->left_margin;
2434 es->format_rect.right -= es->right_margin;
2435 EDIT_AdjustFormatRect(es);
2436 }
2437
2438
2439 /*********************************************************************
2440 *
2441 * EM_CHARFROMPOS
2442 *
2443 * returns line number (not index) in high-order word of result.
2444 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2445 * to Richedit, not to the edit control. Original documentation is valid.
2446 * FIXME: do the specs mean to return -1 if outside client area or
2447 * if outside formatting rectangle ???
2448 *
2449 */
2450 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2451 {
2452 POINT pt;
2453 RECT rc;
2454 INT index;
2455
2456 pt.x = x;
2457 pt.y = y;
2458 GetClientRect(es->hwndSelf, &rc);
2459 if (!PtInRect(&rc, pt))
2460 return -1;
2461
2462 index = EDIT_CharFromPos(es, x, y, NULL);
2463 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2464 }
2465
2466
2467 /*********************************************************************
2468 *
2469 * EM_FMTLINES
2470 *
2471 * Enable or disable soft breaks.
2472 *
2473 * This means: insert or remove the soft linebreak character (\r\r\n).
2474 * Take care to check if the text still fits the buffer after insertion.
2475 * If not, notify with EN_ERRSPACE.
2476 *
2477 */
2478 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2479 {
2480 es->flags &= ~EF_USE_SOFTBRK;
2481 if (add_eol) {
2482 es->flags |= EF_USE_SOFTBRK;
2483 FIXME("soft break enabled, not implemented\n");
2484 }
2485 return add_eol;
2486 }
2487
2488
2489 /*********************************************************************
2490 *
2491 * EM_GETHANDLE
2492 *
2493 * Hopefully this won't fire back at us.
2494 * We always start with a fixed buffer in the local heap.
2495 * Despite of the documentation says that the local heap is used
2496 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2497 * buffer on the local heap.
2498 *
2499 */
2500 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2501 {
2502 HLOCAL hLocal;
2503
2504 if (!(es->style & ES_MULTILINE))
2505 return 0;
2506
2507 if(es->is_unicode)
2508 hLocal = es->hloc32W;
2509 else
2510 {
2511 if(!es->hloc32A)
2512 {
2513 CHAR *textA;
2514 UINT countA, alloc_size;
2515 TRACE("Allocating 32-bit ANSI alias buffer\n");
2516 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2517 alloc_size = ROUND_TO_GROW(countA);
2518 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2519 {
2520 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2521 return 0;
2522 }
2523 textA = LocalLock(es->hloc32A);
2524 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2525 LocalUnlock(es->hloc32A);
2526 }
2527 hLocal = es->hloc32A;
2528 }
2529
2530 EDIT_UnlockBuffer(es, TRUE);
2531
2532 /* The text buffer handle belongs to the app */
2533 es->hlocapp = hLocal;
2534
2535 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2536 return hLocal;
2537 }
2538
2539
2540 /*********************************************************************
2541 *
2542 * EM_GETLINE
2543 *
2544 */
2545 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2546 {
2547 LPWSTR src;
2548 INT line_len, dst_len;
2549 INT i;
2550
2551 if (es->style & ES_MULTILINE) {
2552 if (line >= es->line_count)
2553 return 0;
2554 } else
2555 line = 0;
2556 i = EDIT_EM_LineIndex(es, line);
2557 src = es->text + i;
2558 line_len = EDIT_EM_LineLength(es, i);
2559 dst_len = *(WORD *)dst;
2560 if(unicode)
2561 {
2562 if(dst_len <= line_len)
2563 {
2564 memcpy(dst, src, dst_len * sizeof(WCHAR));
2565 return dst_len;
2566 }
2567 else /* Append 0 if enough space */
2568 {
2569 memcpy(dst, src, line_len * sizeof(WCHAR));
2570 dst[line_len] = 0;
2571 return line_len;
2572 }
2573 }
2574 else
2575 {
2576 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2577 if(!ret && line_len) /* Insufficient buffer size */
2578 return dst_len;
2579 if(ret < dst_len) /* Append 0 if enough space */
2580 ((LPSTR)dst)[ret] = 0;
2581 return ret;
2582 }
2583 }
2584
2585
2586 /*********************************************************************
2587 *
2588 * EM_GETSEL
2589 *
2590 */
2591 static LRESULT EDIT_EM_GetSel(const EDITSTATE *es, PUINT start, PUINT end)
2592 {
2593 UINT s = es->selection_start;
2594 UINT e = es->selection_end;
2595
2596 ORDER_UINT(s, e);
2597 if (start)
2598 *start = s;
2599 if (end)
2600 *end = e;
2601 return MAKELONG(s, e);
2602 }
2603
2604
2605 /*********************************************************************
2606 *
2607 * EM_REPLACESEL
2608 *
2609 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2610 *
2611 */
2612 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2613 {
2614 UINT strl = strlenW(lpsz_replace);
2615 UINT tl = get_text_length(es);
2616 UINT utl;
2617 UINT s;
2618 UINT e;
2619 UINT i;
2620 UINT size;
2621 LPWSTR p;
2622 HRGN hrgn = 0;
2623 LPWSTR buf = NULL;
2624 UINT bufl = 0;
2625
2626 TRACE("%s, can_undo %d, send_update %d\n",
2627 debugstr_w(lpsz_replace), can_undo, send_update);
2628
2629 s = es->selection_start;
2630 e = es->selection_end;
2631
2632 EDIT_InvalidateUniscribeData(es);
2633 if ((s == e) && !strl)
2634 return;
2635
2636 ORDER_UINT(s, e);
2637
2638 size = tl - (e - s) + strl;
2639 if (!size)
2640 es->text_width = 0;
2641
2642 /* Issue the EN_MAXTEXT notification and continue with replacing text
2643 * so that buffer limit is honored. */
2644 if ((honor_limit) && (size > es->buffer_limit)) {
2645 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2646 /* Buffer limit can be smaller than the actual length of text in combobox */
2647 if (es->buffer_limit < (tl - (e-s)))
2648 strl = 0;
2649 else
2650 strl = es->buffer_limit - (tl - (e-s));
2651 }
2652
2653 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2654 return;
2655
2656 if (e != s) {
2657 /* there is something to be deleted */
2658 TRACE("deleting stuff.\n");
2659 bufl = e - s;
2660 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2661 if (!buf) return;
2662 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2663 buf[bufl] = 0; /* ensure 0 termination */
2664 /* now delete */
2665 strcpyW(es->text + s, es->text + e);
2666 text_buffer_changed(es);
2667 }
2668 if (strl) {
2669 /* there is an insertion */
2670 tl = get_text_length(es);
2671 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));
2672 for (p = es->text + tl ; p >= es->text + s ; p--)
2673 p[strl] = p[0];
2674 for (i = 0 , p = es->text + s ; i < strl ; i++)
2675 p[i] = lpsz_replace[i];
2676 if(es->style & ES_UPPERCASE)
2677 CharUpperBuffW(p, strl);
2678 else if(es->style & ES_LOWERCASE)
2679 CharLowerBuffW(p, strl);
2680 text_buffer_changed(es);
2681 }
2682 if (es->style & ES_MULTILINE)
2683 {
2684 INT st = min(es->selection_start, es->selection_end);
2685 INT vlc = get_vertical_line_count(es);
2686
2687 hrgn = CreateRectRgn(0, 0, 0, 0);
2688 EDIT_BuildLineDefs_ML(es, st, st + strl,
2689 strl - abs(es->selection_end - es->selection_start), hrgn);
2690 /* if text is too long undo all changes */
2691 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2692 if (strl)
2693 strcpyW(es->text + e, es->text + e + strl);
2694 if (e != s)
2695 for (i = 0 , p = es->text ; i < e - s ; i++)
2696 p[i + s] = buf[i];
2697 text_buffer_changed(es);
2698 EDIT_BuildLineDefs_ML(es, s, e,
2699 abs(es->selection_end - es->selection_start) - strl, hrgn);
2700 strl = 0;
2701 e = s;
2702 hrgn = CreateRectRgn(0, 0, 0, 0);
2703 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2704 }
2705 }
2706 else {
2707 INT fw = es->format_rect.right - es->format_rect.left;
2708 EDIT_InvalidateUniscribeData(es);
2709 EDIT_CalcLineWidth_SL(es);
2710 /* remove chars that don't fit */
2711 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2712 while ((es->text_width > fw) && s + strl >= s) {
2713 strcpyW(es->text + s + strl - 1, es->text + s + strl);
2714 strl--;
2715 es->text_length = -1;
2716 EDIT_InvalidateUniscribeData(es);
2717 EDIT_CalcLineWidth_SL(es);
2718 }
2719 text_buffer_changed(es);
2720 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
2721 }
2722 }
2723
2724 if (e != s) {
2725 if (can_undo) {
2726 utl = strlenW(es->undo_text);
2727 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2728 /* undo-buffer is extended to the right */
2729 EDIT_MakeUndoFit(es, utl + e - s);
2730 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2731 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2732 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2733 /* undo-buffer is extended to the left */
2734 EDIT_MakeUndoFit(es, utl + e - s);
2735 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2736 p[e - s] = p[0];
2737 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2738 p[i] = buf[i];
2739 es->undo_position = s;
2740 } else {
2741 /* new undo-buffer */
2742 EDIT_MakeUndoFit(es, e - s);
2743 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2744 es->undo_text[e - s] = 0; /* ensure 0 termination */
2745 es->undo_position = s;
2746 }
2747 /* any deletion makes the old insertion-undo invalid */
2748 es->undo_insert_count = 0;
2749 } else
2750 EDIT_EM_EmptyUndoBuffer(es);
2751 }
2752 if (strl) {
2753 if (can_undo) {
2754 if ((s == es->undo_position) ||
2755 ((es->undo_insert_count) &&
2756 (s == es->undo_position + es->undo_insert_count)))
2757 /*
2758 * insertion is new and at delete position or
2759 * an extension to either left or right
2760 */
2761 es->undo_insert_count += strl;
2762 else {
2763 /* new insertion undo */
2764 es->undo_position = s;
2765 es->undo_insert_count = strl;
2766 /* new insertion makes old delete-buffer invalid */
2767 *es->undo_text = '\0';
2768 }
2769 } else
2770 EDIT_EM_EmptyUndoBuffer(es);
2771 }
2772
2773 if (bufl)
2774 HeapFree(GetProcessHeap(), 0, buf);
2775
2776 s += strl;
2777
2778 /* If text has been deleted and we're right or center aligned then scroll rightward */
2779 if (es->style & (ES_RIGHT | ES_CENTER))
2780 {
2781 INT delta = strl - abs(es->selection_end - es->selection_start);
2782
2783 if (delta < 0 && es->x_offset)
2784 {
2785 if (abs(delta) > es->x_offset)
2786 es->x_offset = 0;
2787 else
2788 es->x_offset += delta;
2789 }
2790 }
2791
2792 EDIT_EM_SetSel(es, s, s, FALSE);
2793 es->flags |= EF_MODIFIED;
2794 if (send_update) es->flags |= EF_UPDATE;
2795 if (hrgn)
2796 {
2797 EDIT_UpdateTextRegion(es, hrgn, TRUE);
2798 DeleteObject(hrgn);
2799 }
2800 else
2801 EDIT_UpdateText(es, NULL, TRUE);
2802
2803 EDIT_EM_ScrollCaret(es);
2804
2805 /* force scroll info update */
2806 EDIT_UpdateScrollInfo(es);
2807
2808
2809 if(send_update || (es->flags & EF_UPDATE))
2810 {
2811 es->flags &= ~EF_UPDATE;
2812 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
2813 }
2814 EDIT_InvalidateUniscribeData(es);
2815 }
2816
2817
2818 /*********************************************************************
2819 *
2820 * EM_SETHANDLE
2821 *
2822 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2823 *
2824 */
2825 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
2826 {
2827 if (!(es->style & ES_MULTILINE))
2828 return;
2829
2830 if (!hloc) {
2831 WARN("called with NULL handle\n");
2832 return;
2833 }
2834
2835 EDIT_UnlockBuffer(es, TRUE);
2836
2837 if(es->is_unicode)
2838 {
2839 if(es->hloc32A)
2840 {
2841 LocalFree(es->hloc32A);
2842 es->hloc32A = NULL;
2843 }
2844 es->hloc32W = hloc;
2845 }
2846 else
2847 {
2848 INT countW, countA;
2849 HLOCAL hloc32W_new;
2850 WCHAR *textW;
2851 CHAR *textA;
2852
2853 countA = LocalSize(hloc);
2854 textA = LocalLock(hloc);
2855 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2856 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2857 {
2858 ERR("Could not allocate new unicode buffer\n");
2859 return;
2860 }
2861 textW = LocalLock(hloc32W_new);
2862 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2863 LocalUnlock(hloc32W_new);
2864 LocalUnlock(hloc);
2865
2866 if(es->hloc32W)
2867 LocalFree(es->hloc32W);
2868
2869 es->hloc32W = hloc32W_new;
2870 es->hloc32A = hloc;
2871 }
2872
2873 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2874
2875 /* The text buffer handle belongs to the control */
2876 es->hlocapp = NULL;
2877
2878 EDIT_LockBuffer(es);
2879 text_buffer_changed(es);
2880
2881 es->x_offset = es->y_offset = 0;
2882 es->selection_start = es->selection_end = 0;
2883 EDIT_EM_EmptyUndoBuffer(es);
2884 es->flags &= ~EF_MODIFIED;
2885 es->flags &= ~EF_UPDATE;
2886 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2887 EDIT_UpdateText(es, NULL, TRUE);
2888 EDIT_EM_ScrollCaret(es);
2889 /* force scroll info update */
2890 EDIT_UpdateScrollInfo(es);
2891 }
2892
2893
2894 /*********************************************************************
2895 *
2896 * EM_SETLIMITTEXT
2897 *
2898 * NOTE: this version currently implements WinNT limits
2899 *
2900 */
2901 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
2902 {
2903 if (!limit) limit = ~0u;
2904 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2905 es->buffer_limit = limit;
2906 }
2907
2908
2909 /*********************************************************************
2910 *
2911 * EM_SETMARGINS
2912 *
2913 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2914 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2915 * margin according to the textmetrics of the current font.
2916 *
2917 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
2918 * of the char's width as the margin, but this is not how Windows handles this.
2919 * For all other fonts Windows sets the margins to zero.
2920 *
2921 * FIXME - When EC_USEFONTINFO is used the margins only change if the
2922 * edit control is equal to or larger than a certain size.
2923 * Interestingly if one subtracts both the left and right margins from
2924 * this size one always seems to get an even number. The extents of
2925 * the (four character) string "'**'" match this quite closely, so
2926 * we'll use this until we come up with a better idea.
2927 */
2928 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
2929 {
2930 WCHAR magic_string[] = {'\'','*','*','\'', 0};
2931 SIZE sz;
2932
2933 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
2934 return sz.cx + left + right;
2935 }
2936
2937 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
2938 WORD left, WORD right, BOOL repaint)
2939 {
2940 TEXTMETRICW tm;
2941 INT default_left_margin = 0; /* in pixels */
2942 INT default_right_margin = 0; /* in pixels */
2943
2944 /* Set the default margins depending on the font */
2945 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2946 HDC dc = GetDC(es->hwndSelf);
2947 HFONT old_font = SelectObject(dc, es->font);
2948 GetTextMetricsW(dc, &tm);
2949 /* The default margins are only non zero for TrueType or Vector fonts */
2950 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2951 int min_size;
2952 RECT rc;
2953 /* This must be calculated more exactly! But how? */
2954 default_left_margin = tm.tmAveCharWidth / 2;
2955 default_right_margin = tm.tmAveCharWidth / 2;
2956 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
2957 GetClientRect(es->hwndSelf, &rc);
2958 if (!IsRectEmpty(&rc) && (rc.right - rc.left < min_size)) {
2959 default_left_margin = es->left_margin;
2960 default_right_margin = es->right_margin;
2961 }
2962 }
2963 SelectObject(dc, old_font);
2964 ReleaseDC(es->hwndSelf, dc);
2965 }
2966
2967 if (action & EC_LEFTMARGIN) {
2968 es->format_rect.left -= es->left_margin;
2969 if (left != EC_USEFONTINFO)
2970 es->left_margin = left;
2971 else
2972 es->left_margin = default_left_margin;
2973 es->format_rect.left += es->left_margin;
2974 }
2975
2976 if (action & EC_RIGHTMARGIN) {
2977 es->format_rect.right += es->right_margin;
2978 if (right != EC_USEFONTINFO)
2979 es->right_margin = right;
2980 else
2981 es->right_margin = default_right_margin;
2982 es->format_rect.right -= es->right_margin;
2983 }
2984
2985 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2986 EDIT_AdjustFormatRect(es);
2987 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2988 }
2989
2990 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2991 }
2992
2993
2994 /*********************************************************************
2995 *
2996 * EM_SETPASSWORDCHAR
2997 *
2998 */
2999 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3000 {
3001 LONG style;
3002
3003 if (es->style & ES_MULTILINE)
3004 return;
3005
3006 if (es->password_char == c)
3007 return;
3008
3009 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3010 es->password_char = c;
3011 if (c) {
3012 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3013 es->style |= ES_PASSWORD;
3014 } else {
3015 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3016 es->style &= ~ES_PASSWORD;
3017 }
3018 EDIT_InvalidateUniscribeData(es);
3019 EDIT_UpdateText(es, NULL, TRUE);
3020 }
3021
3022
3023 /*********************************************************************
3024 *
3025 * EM_SETTABSTOPS
3026 *
3027 */
3028 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, const INT *tabs)
3029 {
3030 if (!(es->style & ES_MULTILINE))
3031 return FALSE;
3032 HeapFree(GetProcessHeap(), 0, es->tabs);
3033 es->tabs_count = count;
3034 if (!count)
3035 es->tabs = NULL;
3036 else {
3037 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3038 #ifdef __REACTOS__
3039 /* ReactOS r33503 */
3040 if (es->tabs == NULL)
3041 {
3042 es->tabs_count = 0;
3043 return FALSE;
3044 }
3045 #endif
3046 memcpy(es->tabs, tabs, count * sizeof(INT));
3047 }
3048 EDIT_InvalidateUniscribeData(es);
3049 return TRUE;
3050 }
3051
3052
3053 /*********************************************************************
3054 *
3055 * EM_SETWORDBREAKPROC
3056 *
3057 */
3058 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3059 {
3060 if (es->word_break_proc == wbp)
3061 return;
3062
3063 es->word_break_proc = wbp;
3064
3065 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3066 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3067 EDIT_UpdateText(es, NULL, TRUE);
3068 }
3069 }
3070
3071
3072 /*********************************************************************
3073 *
3074 * EM_UNDO / WM_UNDO
3075 *
3076 */
3077 static BOOL EDIT_EM_Undo(EDITSTATE *es)
3078 {
3079 INT ulength;
3080 LPWSTR utext;
3081
3082 /* As per MSDN spec, for a single-line edit control,
3083 the return value is always TRUE */
3084 if( es->style & ES_READONLY )
3085 return !(es->style & ES_MULTILINE);
3086
3087 ulength = strlenW(es->undo_text);
3088
3089 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3090 #ifdef __REACTOS__
3091 /* ReactOS r33503 */
3092 if (utext == NULL)
3093 return FALSE;
3094 #endif
3095
3096 strcpyW(utext, es->undo_text);
3097
3098 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3099 es->undo_insert_count, debugstr_w(utext));
3100
3101 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3102 EDIT_EM_EmptyUndoBuffer(es);
3103 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3104 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3105 /* send the notification after the selection start and end are set */
3106 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3107 EDIT_EM_ScrollCaret(es);
3108 HeapFree(GetProcessHeap(), 0, utext);
3109
3110 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3111 es->undo_insert_count, debugstr_w(es->undo_text));
3112 return TRUE;
3113 }
3114
3115
3116 /* Helper function for WM_CHAR
3117 *
3118 * According to an MSDN blog article titled "Just because you're a control
3119 * doesn't mean that you're necessarily inside a dialog box," multiline edit
3120 * controls without ES_WANTRETURN would attempt to detect whether it is inside
3121 * a dialog box or not.
3122 */
3123 static inline BOOL EDIT_IsInsideDialog(EDITSTATE *es)
3124 {
3125 return (es->flags & EF_DIALOGMODE);
3126 }
3127
3128
3129 /*********************************************************************
3130 *
3131 * WM_PASTE
3132 *
3133 */
3134 static void EDIT_WM_Paste(EDITSTATE *es)
3135 {
3136 HGLOBAL hsrc;
3137 LPWSTR src;
3138
3139 /* Protect read-only edit control from modification */
3140 if(es->style & ES_READONLY)
3141 return;
3142
3143 OpenClipboard(es->hwndSelf);
3144 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3145 src = GlobalLock(hsrc);
3146 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
3147 GlobalUnlock(hsrc);
3148 }
3149 else if (es->style & ES_PASSWORD) {
3150 /* clear selected text in password edit box even with empty clipboard */
3151 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3152 }
3153 CloseClipboard();
3154 }
3155
3156
3157 /*********************************************************************
3158 *
3159 * WM_COPY
3160 *
3161 */
3162 static void EDIT_WM_Copy(EDITSTATE *es)
3163 {
3164 INT s = min(es->selection_start, es->selection_end);
3165 INT e = max(es->selection_start, es->selection_end);
3166 HGLOBAL hdst;
3167 LPWSTR dst;
3168 DWORD len;
3169
3170 if (e == s) return;
3171
3172 len = e - s;
3173 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3174 dst = GlobalLock(hdst);
3175 memcpy(dst, es->text + s, len * sizeof(WCHAR));
3176 dst[len] = 0; /* ensure 0 termination */
3177 TRACE("%s\n", debugstr_w(dst));
3178 GlobalUnlock(hdst);
3179 OpenClipboard(es->hwndSelf);
3180 EmptyClipboard();
3181 SetClipboardData(CF_UNICODETEXT, hdst);
3182 CloseClipboard();
3183 }
3184
3185
3186 /*********************************************************************
3187 *
3188 * WM_CLEAR
3189 *
3190 */
3191 static inline void EDIT_WM_Clear(EDITSTATE *es)
3192 {
3193 /* Protect read-only edit control from modification */
3194 if(es->style & ES_READONLY)
3195 return;
3196
3197 EDIT_EM_ReplaceSel(es, TRUE, empty_stringW, TRUE, TRUE);
3198 }
3199
3200
3201 /*********************************************************************
3202 *
3203 * WM_CUT
3204 *
3205 */
3206 static inline void EDIT_WM_Cut(EDITSTATE *es)
3207 {
3208 EDIT_WM_Copy(es);
3209 EDIT_WM_Clear(es);
3210 }
3211
3212
3213 /*********************************************************************
3214 *
3215 * WM_CHAR
3216 *
3217 */
3218 static LRESULT EDIT_WM_Char(EDITSTATE *es, WCHAR c)
3219 {
3220 BOOL control;
3221
3222 control = GetKeyState(VK_CONTROL) & 0x8000;
3223
3224 switch (c) {
3225 case '\r':
3226 /* If it's not a multiline edit box, it would be ignored below.
3227 * For multiline edit without ES_WANTRETURN, we have to make a
3228 * special case.
3229 */
3230 if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3231 if (EDIT_IsInsideDialog(es))
3232 break;
3233 case '\n':
3234 if (es->style & ES_MULTILINE) {
3235 if (es->style & ES_READONLY) {
3236 EDIT_MoveHome(es, FALSE, FALSE);
3237 EDIT_MoveDown_ML(es, FALSE);
3238 } else {
3239 static const WCHAR cr_lfW[] = {'\r','\n',0};
3240 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3241 }
3242 }
3243 break;
3244 case '\t':
3245 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3246 {
3247 static const WCHAR tabW[] = {'\t',0};
3248 if (EDIT_IsInsideDialog(es))
3249 break;
3250 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3251 }
3252 break;
3253 case VK_BACK:
3254 if (!(es->style & ES_READONLY) && !control) {
3255 if (es->selection_start != es->selection_end)
3256 EDIT_WM_Clear(es);
3257 else {
3258 /* delete character left of caret */
3259 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3260 EDIT_MoveBackward(es, TRUE);
3261 EDIT_WM_Clear(es);
3262 }
3263 }
3264 break;
3265 case 0x03: /* ^C */
3266 if (!(es->style & ES_PASSWORD))
3267 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3268 break;
3269 case 0x16: /* ^V */
3270 if (!(es->style & ES_READONLY))
3271 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3272 break;
3273 case 0x18: /* ^X */
3274 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3275 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3276 break;
3277 case 0x1A: /* ^Z */
3278 if (!(es->style & ES_READONLY))
3279 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3280 break;
3281
3282 default:
3283 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3284 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3285 break;
3286
3287 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3288 WCHAR str[2];
3289 str[0] = c;
3290 str[1] = '\0';
3291 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
3292 }
3293 break;
3294 }
3295 return 1;
3296 }
3297
3298
3299 /*********************************************************************
3300 *
3301 * EDIT_ContextMenuCommand
3302 *
3303 */
3304 static void EDIT_ContextMenuCommand(EDITSTATE *es, UINT id)
3305 {
3306 switch (id) {
3307 case EM_UNDO:
3308 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3309 break;
3310 case WM_CUT:
3311 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3312 break;
3313 case WM_COPY:
3314 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3315 break;
3316 case WM_PASTE:
3317 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3318 break;
3319 case WM_CLEAR:
3320 SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3321 break;
3322 case EM_SETSEL:
3323 SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3324 break;
3325 default:
3326 ERR("unknown menu item, please report\n");
3327 break;
3328 }
3329 }
3330
3331
3332 /*********************************************************************
3333 *
3334 * WM_CONTEXTMENU
3335 *
3336 * Note: the resource files resource/sysres_??.rc cannot define a
3337 * single popup menu. Hence we use a (dummy) menubar
3338 * containing the single popup menu as its first item.
3339 *
3340 * FIXME: the message identifiers have been chosen arbitrarily,
3341 * hence we use MF_BYPOSITION.
3342 * We might as well use the "real" values (anybody knows ?)
3343 * The menu definition is in resources/sysres_??.rc.
3344 * Once these are OK, we better use MF_BYCOMMAND here
3345 * (as we do in EDIT_WM_Command()).
3346 *
3347 */
3348 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
3349 {
3350 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3351 HMENU popup = GetSubMenu(menu, 0);
3352 UINT start = es->selection_start;
3353 UINT end = es->selection_end;
3354 UINT cmd;
3355
3356 ORDER_UINT(start, end);
3357
3358 /* undo */
3359 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3360 /* cut */
3361 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3362 /* copy */
3363 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3364 /* paste */
3365 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3366 /* delete */
3367 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3368 /* select all */
3369 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
3370
3371 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3372 {
3373 RECT rc;
3374 /* Windows places the menu at the edit's center in this case */
3375 #ifdef __REACTOS__
3376 /* ReactOS r55202 */
3377 GetClientRect(es->hwndSelf, &rc);
3378 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3379 #else
3380 WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3381 #endif
3382 x = rc.left + (rc.right - rc.left) / 2;
3383 y = rc.top + (rc.bottom - rc.top) / 2;
3384 }
3385
3386 if (!(es->flags & EF_FOCUSED))
3387 SetFocus(es->hwndSelf);
3388
3389 cmd = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
3390 x, y, 0, es->hwndSelf, NULL);
3391
3392 if (cmd)
3393 EDIT_ContextMenuCommand(es, cmd);
3394
3395 DestroyMenu(menu);
3396 }
3397
3398
3399 /*********************************************************************
3400 *
3401 * WM_GETTEXT
3402 *
3403 */
3404 static INT EDIT_WM_GetText(const EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
3405 {
3406 if(!count) return 0;
3407
3408 if(unicode)
3409 {
3410 lstrcpynW(dst, es->text, count);
3411 return strlenW(dst);
3412 }
3413 else
3414 {
3415 LPSTR textA = (LPSTR)dst;
3416 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3417 textA[count - 1] = 0; /* ensure 0 termination */
3418 return strlen(textA);
3419 }
3420 }
3421
3422 /*********************************************************************
3423 *
3424 * EDIT_CheckCombo
3425 *
3426 */
3427 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
3428 {
3429 HWND hLBox = es->hwndListBox;
3430 HWND hCombo;
3431 BOOL bDropped;
3432 int nEUI;
3433
3434 if (!hLBox)
3435 return FALSE;
3436