ReactOS  0.4.14-dev-49-gfb4591c
edit.c
Go to the documentation of this file.
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 
52 
53 #define BUFLIMIT_INITIAL 30000 /* initial buffer size */
54 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
55 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
56 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
57 
58 /*
59  * extra flags for EDITSTATE.flags field
60  */
61 #define EF_MODIFIED 0x0001 /* text has been modified */
62 #define EF_FOCUSED 0x0002 /* we have input focus */
63 #define EF_UPDATE 0x0004 /* notify parent of changed state */
64 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
65 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
66 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
67  wrapped line, instead of in front of the next character */
68 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
69 #define EF_DIALOGMODE 0x0200 /* Indicates that we are inside a dialog window */
70 
71 typedef enum
72 {
73  END_0 = 0, /* line ends with terminating '\0' character */
74  END_WRAP, /* line is wrapped */
75  END_HARD, /* line ends with a hard return '\r\n' */
76  END_SOFT, /* line ends with a soft return '\r\r\n' */
77  END_RICH /* line ends with a single '\n' */
78 } LINE_END;
79 
80 typedef struct tagLINEDEF {
81  INT length; /* bruto length of a line in bytes */
82  INT net_length; /* netto length of a line in visible characters */
84  INT width; /* width of the line in pixels */
85  INT index; /* line index into the buffer */
86  SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data */
87  struct tagLINEDEF *next;
88 } LINEDEF;
89 
90 typedef struct
91 {
92  BOOL is_unicode; /* how the control was created */
93  LPWSTR text; /* the actual contents of the control */
94  UINT text_length; /* cached length of text buffer (in WCHARs) - use get_text_length() to retrieve */
95  UINT buffer_size; /* the size of the buffer in characters */
96  UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
97  HFONT font; /* NULL means standard system font */
98  INT x_offset; /* scroll offset for multi lines this is in pixels
99  for single lines it's in characters */
100  INT line_height; /* height of a screen line in pixels */
101  INT char_width; /* average character width in pixels */
102  DWORD style; /* sane version of wnd->dwStyle */
103  WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
104  INT undo_insert_count; /* number of characters inserted in sequence */
105  UINT undo_position; /* character index of the insertion and deletion */
106  LPWSTR undo_text; /* deleted text */
107  UINT undo_buffer_size; /* size of the deleted text buffer */
108  INT selection_start; /* == selection_end if no selection */
109  INT selection_end; /* == current caret position */
110  WCHAR password_char; /* == 0 if no password char, and for multi line controls */
111  INT left_margin; /* in pixels */
112  INT right_margin; /* in pixels */
113  RECT format_rect;
114  INT text_width; /* width of the widest line in pixels for multi line controls
115  and just line width for single line controls */
116  INT region_posx; /* Position of cursor relative to region: */
117  INT region_posy; /* -1: to left, 0: within, 1: to right */
118  void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
119  INT line_count; /* number of lines */
120  INT y_offset; /* scroll offset in number of lines */
121  BOOL bCaptureState; /* flag indicating whether mouse was captured */
122  BOOL bEnableState; /* flag keeping the enable state */
123  HWND hwndSelf; /* the our window handle */
124  HWND hwndParent; /* Handle of parent for sending EN_* messages.
125  Even if parent will change, EN_* messages
126  should be sent to the first parent. */
127  HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
128  INT wheelDeltaRemainder; /* scroll wheel delta left over after scrolling whole lines */
129  /*
130  * only for multi line controls
131  */
132  INT lock_count; /* amount of re-entries in the EditWndProc */
133  INT tabs_count;
134  LPINT tabs;
135  LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
136  HLOCAL hloc32W; /* our unicode local memory block */
137  HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
138  or EM_SETHANDLE */
139  HLOCAL hlocapp; /* The text buffer handle belongs to the app */
140  /*
141  * IME Data
142  */
143  UINT composition_len; /* length of composition, 0 == no composition */
144  int composition_start; /* the character position for the composition */
145  /*
146  * Uniscribe Data
147  */
148  SCRIPT_LOGATTR *logAttr;
149  SCRIPT_STRING_ANALYSIS ssa; /* Uniscribe Data for single line controls */
150 } EDITSTATE;
151 
152 
153 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
154 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
155 
156 /* used for disabled or read-only edit control */
157 #define EDIT_NOTIFY_PARENT(es, wNotifyCode) \
158  do \
159  { /* Notify parent which has created this edit control */ \
160  TRACE("notification " #wNotifyCode " sent to hwnd=%p\n", es->hwndParent); \
161  SendMessageW(es->hwndParent, WM_COMMAND, \
162  MAKEWPARAM(GetWindowLongPtrW((es->hwndSelf),GWLP_ID), wNotifyCode), \
163  (LPARAM)(es->hwndSelf)); \
164  } while(0)
165 
166 static const WCHAR empty_stringW[] = {0};
167 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap);
168 
169 /*********************************************************************
170  *
171  * EM_CANUNDO
172  *
173  */
174 static inline BOOL EDIT_EM_CanUndo(const EDITSTATE *es)
175 {
176  return (es->undo_insert_count || strlenW(es->undo_text));
177 }
178 
179 
180 /*********************************************************************
181  *
182  * EM_EMPTYUNDOBUFFER
183  *
184  */
186 {
187  es->undo_insert_count = 0;
188  *es->undo_text = '\0';
189 }
190 
191 
192 /**********************************************************************
193  * get_app_version
194  *
195  * Returns the window version in case Wine emulates a later version
196  * of windows than the application expects.
197  *
198  * In a number of cases when windows runs an application that was
199  * designed for an earlier windows version, windows reverts
200  * to "old" behaviour of that earlier version.
201  *
202  * An example is a disabled edit control that needs to be painted.
203  * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
204  * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
205  * applications with an expected version 0f 4.0 or higher.
206  *
207  */
208 static DWORD get_app_version(void)
209 {
210  static DWORD version;
211  if (!version)
212  {
213  DWORD dwEmulatedVersion;
215  DWORD dwProcVersion = GetProcessVersion(0);
216 
217  info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
218  GetVersionExW( &info );
219  dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
220  /* FIXME: this may not be 100% correct; see discussion on the
221  * wine developer list in Nov 1999 */
222  version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
223  }
224  return version;
225 }
226 
228 {
229  HBRUSH hbrush;
230  UINT msg;
231 
232  if ( get_app_version() >= 0x40000 && (!es->bEnableState || (es->style & ES_READONLY)))
234  else
236 
237  /* why do we notify to es->hwndParent, and we send this one to GetParent()? */
238 #ifdef __REACTOS__
239  /* ReactOS r54259 */
240  hbrush = GetControlBrush(es->hwndSelf, hdc, msg);
241 #else
242  hbrush = (HBRUSH)SendMessageW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
243  if (!hbrush)
244  hbrush = (HBRUSH)DefWindowProcW(GetParent(es->hwndSelf), msg, (WPARAM)hdc, (LPARAM)es->hwndSelf);
245 #endif
246  return hbrush;
247 }
248 
249 
251 {
252  if(es->text_length == (UINT)-1)
253  es->text_length = strlenW(es->text);
254  return es->text_length;
255 }
256 
257 
258 /*********************************************************************
259  *
260  * EDIT_WordBreakProc
261  *
262  * Find the beginning of words.
263  * Note: unlike the specs for a WordBreakProc, this function can
264  * only be called without linebreaks between s[0] up to
265  * s[count - 1]. Remember it is only called
266  * internally, so we can decide this for ourselves.
267  * Additionally we will always be breaking the full string.
268  *
269  */
271 {
272  INT ret = 0;
273 
274  TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
275 
276  if(!s) return 0;
277 
278  if (!es->logAttr)
279  {
281 
282  memset(&psa,0,sizeof(SCRIPT_ANALYSIS));
283  psa.eScript = SCRIPT_UNDEFINED;
284 
285  es->logAttr = HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR) * get_text_length(es));
286  ScriptBreak(es->text, get_text_length(es), &psa, es->logAttr);
287  }
288 
289  switch (action) {
290  case WB_LEFT:
291  if (index)
292  index--;
293  while (index && !es->logAttr[index].fSoftBreak)
294  index--;
295  ret = index;
296  break;
297  case WB_RIGHT:
298  if (!count)
299  break;
300  while (index < count && s[index] && !es->logAttr[index].fSoftBreak)
301  index++;
302  ret = index;
303  break;
304  case WB_ISDELIMITER:
305  ret = es->logAttr[index].fWhiteSpace;
306  break;
307  default:
308  ERR("unknown action code, please report !\n");
309  break;
310  }
311  return ret;
312 }
313 
314 
315 /*********************************************************************
316  *
317  * EDIT_CallWordBreakProc
318  *
319  * Call appropriate WordBreakProc (internal or external).
320  *
321  * Note: The "start" argument should always be an index referring
322  * to es->text. The actual wordbreak proc might be
323  * 16 bit, so we can't always pass any 32 bit LPSTR.
324  * Hence we assume that es->text is the buffer that holds
325  * the string under examination (we can decide this for ourselves).
326  *
327  */
329 {
330  INT ret;
331 
332  if (es->word_break_proc)
333  {
334  if(es->is_unicode)
335  {
336  EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
337 
338  TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
339  es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
340  ret = wbpW(es->text + start, index, count, action);
341  }
342  else
343  {
344  EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
345  INT countA;
346  CHAR *textA;
347 
348  countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
349  textA = HeapAlloc(GetProcessHeap(), 0, countA);
350 #ifdef __REACTOS__
351  /* ReactOS r33503 */
352  if (textA == NULL) return 0;
353 #endif
354  WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
355  TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
356  es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
357  ret = wbpA(textA, index, countA, action);
359  }
360  }
361  else
363 
364  return ret;
365 }
366 
367 static inline void EDIT_InvalidateUniscribeData_linedef(LINEDEF *line_def)
368 {
369  if (line_def->ssa)
370  {
371  ScriptStringFree(&line_def->ssa);
372  line_def->ssa = NULL;
373  }
374 }
375 
377 {
378  LINEDEF *line_def = es->first_line_def;
379  while (line_def)
380  {
382  line_def = line_def->next;
383  }
384  if (es->ssa)
385  {
386  ScriptStringFree(&es->ssa);
387  es->ssa = NULL;
388  }
389 }
390 
392 {
393  if (!line_def)
394  return NULL;
395 
396  if (line_def->net_length && !line_def->ssa)
397  {
398  int index = line_def->index;
399  HFONT old_font = NULL;
400  HDC udc = dc;
401  SCRIPT_TABDEF tabdef;
402  HRESULT hr;
403 
404  if (!udc)
405  udc = GetDC(es->hwndSelf);
406  if (es->font)
407  old_font = SelectObject(udc, es->font);
408 
409  tabdef.cTabStops = es->tabs_count;
410  tabdef.iScale = 0;
411  tabdef.pTabStops = es->tabs;
412  tabdef.iTabOrigin = 0;
413 
414  hr = ScriptStringAnalyse(udc, &es->text[index], line_def->net_length,
415 #ifdef __REACTOS__
416  /* ReactOS r57679 */
417  (3*line_def->net_length/2+16), -1,
418 #else
419  (1.5*line_def->net_length+16), -1,
420 #endif
422  NULL, NULL, NULL, &tabdef, NULL, &line_def->ssa);
423  if (FAILED(hr))
424  {
425  WARN("ScriptStringAnalyse failed (%x)\n",hr);
426  line_def->ssa = NULL;
427  }
428 
429  if (es->font)
430  SelectObject(udc, old_font);
431  if (udc != dc)
432  ReleaseDC(es->hwndSelf, udc);
433  }
434 
435  return line_def->ssa;
436 }
437 
439 {
440  LINEDEF *line_def;
441 
442  if (!(es->style & ES_MULTILINE))
443  {
444  if (!es->ssa)
445  {
447  HFONT old_font = NULL;
448  HDC udc = dc;
449 
450  if (!udc)
451  udc = GetDC(es->hwndSelf);
452  if (es->font)
453  old_font = SelectObject(udc, es->font);
454 
455  if (es->style & ES_PASSWORD)
456 #ifdef __REACTOS__
457  /* ReactOS r57677 */
458  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);
459 #else
460  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);
461 #endif
462  else
463 #ifdef __REACTOS__
464  /* ReactOS r57677 */
465  ScriptStringAnalyse(udc, es->text, length, (3*length/2+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
466 #else
467  ScriptStringAnalyse(udc, es->text, length, (1.5*length+16), -1, SSA_LINK|SSA_FALLBACK|SSA_GLYPHS, -1, NULL, NULL, NULL, NULL, NULL, &es->ssa);
468 #endif
469 
470  if (es->font)
471  SelectObject(udc, old_font);
472  if (udc != dc)
473  ReleaseDC(es->hwndSelf, udc);
474  }
475  return es->ssa;
476  }
477  else
478  {
479  line_def = es->first_line_def;
480  while (line_def && line)
481  {
482  line_def = line_def->next;
483  line--;
484  }
485 
486  return EDIT_UpdateUniscribeData_linedef(es,dc,line_def);
487  }
488 }
489 
491 {
492  INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
493  return max(1,vlc);
494 }
495 
496 /*********************************************************************
497  *
498  * EDIT_BuildLineDefs_ML
499  *
500  * Build linked list of text lines.
501  * Lines can end with '\0' (last line), a character (if it is wrapped),
502  * a soft return '\r\r\n' or a hard return '\r\n'
503  *
504  */
505 static void EDIT_BuildLineDefs_ML(EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
506 {
507  LPWSTR current_position, cp;
508  INT fw;
509  LINEDEF *current_line;
510  LINEDEF *previous_line;
511  LINEDEF *start_line;
512  INT line_index = 0, nstart_line, nstart_index;
513  INT line_count = es->line_count;
514  INT orig_net_length;
515  RECT rc;
516  INT vlc;
517 
518  if (istart == iend && delta == 0)
519  return;
520 
521  previous_line = NULL;
522  current_line = es->first_line_def;
523 
524  /* Find starting line. istart must lie inside an existing line or
525  * at the end of buffer */
526  do {
527  if (istart < current_line->index + current_line->length ||
528  current_line->ending == END_0)
529  break;
530 
531  previous_line = current_line;
532  current_line = current_line->next;
533  line_index++;
534  } while (current_line);
535 
536  if (!current_line) /* Error occurred start is not inside previous buffer */
537  {
538  FIXME(" modification occurred outside buffer\n");
539  return;
540  }
541 
542  /* Remember start of modifications in order to calculate update region */
543  nstart_line = line_index;
544  nstart_index = current_line->index;
545 
546  /* We must start to reformat from the previous line since the modifications
547  * may have caused the line to wrap upwards. */
548  if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
549  {
550  line_index--;
551  current_line = previous_line;
552  }
553  start_line = current_line;
554 
555  fw = es->format_rect.right - es->format_rect.left;
556  current_position = es->text + current_line->index;
558  do {
559  if (current_line != start_line)
560  {
561  if (!current_line || current_line->index + delta > current_position - es->text)
562  {
563  /* The buffer has been expanded, create a new line and
564  insert it into the link list */
565  LINEDEF *new_line = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF));
566 #ifdef __REACTOS__
567  /* ReactOS r33509 */
568  if (new_line == NULL)
569  break;
570 #endif
571  new_line->next = previous_line->next;
572  previous_line->next = new_line;
573  current_line = new_line;
574  es->line_count++;
575  }
576  else if (current_line->index + delta < current_position - es->text)
577  {
578  /* The previous line merged with this line so we delete this extra entry */
579  previous_line->next = current_line->next;
580  HeapFree(GetProcessHeap(), 0, current_line);
581  current_line = previous_line->next;
582  es->line_count--;
583  continue;
584  }
585  else /* current_line->index + delta == current_position */
586  {
587  if (current_position - es->text > iend)
588  break; /* We reached end of line modifications */
589  /* else recalculate this line */
590  }
591  }
592 
593  current_line->index = current_position - es->text;
594  orig_net_length = current_line->net_length;
595 
596  /* Find end of line */
597  cp = current_position;
598  while (*cp) {
599  if (*cp == '\n') break;
600  if ((*cp == '\r') && (*(cp + 1) == '\n'))
601  break;
602  cp++;
603  }
604 
605  /* Mark type of line termination */
606  if (!(*cp)) {
607  current_line->ending = END_0;
608  current_line->net_length = strlenW(current_position);
609  } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
610  current_line->ending = END_SOFT;
611  current_line->net_length = cp - current_position - 1;
612  } else if (*cp == '\n') {
613  current_line->ending = END_RICH;
614  current_line->net_length = cp - current_position;
615  } else {
616  current_line->ending = END_HARD;
617  current_line->net_length = cp - current_position;
618  }
619 
620  if (current_line->net_length)
621  {
622  const SIZE *sz;
624  EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
625  if (current_line->ssa)
626  {
627  sz = ScriptString_pSize(current_line->ssa);
628  /* Calculate line width */
629  current_line->width = sz->cx;
630  }
631  else current_line->width = es->char_width * current_line->net_length;
632  }
633  else current_line->width = 0;
634 
635  /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
636 
637 /* Line breaks just look back from the end and find the next break and try that. */
638 
639  if (!(es->style & ES_AUTOHSCROLL)) {
640  if (current_line->width > fw && fw > es->char_width) {
641 
642  INT prev, next;
643  int w;
644  const SIZE *sz;
645  float d;
646 
647  prev = current_line->net_length - 1;
648  w = current_line->net_length;
649  d = (float)current_line->width/(float)fw;
650  if (d > 1.2f) d -= 0.2f;
651  next = prev/d;
652  if (next >= prev) next = prev-1;
653  do {
654  prev = EDIT_CallWordBreakProc(es, current_position - es->text,
655  next, current_line->net_length, WB_LEFT);
656  current_line->net_length = prev;
658  EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
659  if (current_line->ssa)
660  sz = ScriptString_pSize(current_line->ssa);
661  else sz = 0;
662  if (sz)
663  current_line->width = sz->cx;
664  else
665  prev = 0;
666  next = prev - 1;
667  } while (prev && current_line->width > fw);
668  current_line->net_length = w;
669 
670  if (prev == 0) { /* Didn't find a line break so force a break */
671  INT *piDx;
672  const INT *count;
673 
675  EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
676 
677  if (current_line->ssa)
678  {
679  count = ScriptString_pcOutChars(current_line->ssa);
680  piDx = HeapAlloc(GetProcessHeap(),0,sizeof(INT) * (*count));
681  ScriptStringGetLogicalWidths(current_line->ssa,piDx);
682 
683  prev = current_line->net_length-1;
684  do {
685  current_line->width -= piDx[prev];
686  prev--;
687  } while ( prev > 0 && current_line->width > fw);
688  if (prev<=0)
689  prev = 1;
690  HeapFree(GetProcessHeap(),0,piDx);
691  }
692  else
693  prev = (fw / es->char_width);
694  }
695 
696  /* If the first line we are calculating, wrapped before istart, we must
697  * adjust istart in order for this to be reflected in the update region. */
698  if (current_line->index == nstart_index && istart > current_line->index + prev)
699  istart = current_line->index + prev;
700  /* else if we are updating the previous line before the first line we
701  * are re-calculating and it expanded */
702  else if (current_line == start_line &&
703  current_line->index != nstart_index && orig_net_length < prev)
704  {
705  /* Line expanded due to an upwards line wrap so we must partially include
706  * previous line in update region */
707  nstart_line = line_index;
708  nstart_index = current_line->index;
709  istart = current_line->index + orig_net_length;
710  }
711 
712  current_line->net_length = prev;
713  current_line->ending = END_WRAP;
714 
715  if (current_line->net_length > 0)
716  {
717  EDIT_UpdateUniscribeData_linedef(es, NULL, current_line);
718  if (current_line->ssa)
719  {
720  sz = ScriptString_pSize(current_line->ssa);
721  current_line->width = sz->cx;
722  }
723  else
724  current_line->width = 0;
725  }
726  else current_line->width = 0;
727  }
728  else if (current_line == start_line &&
729  current_line->index != nstart_index &&
730  orig_net_length < current_line->net_length) {
731  /* The previous line expanded but it's still not as wide as the client rect */
732  /* The expansion is due to an upwards line wrap so we must partially include
733  it in the update region */
734  nstart_line = line_index;
735  nstart_index = current_line->index;
736  istart = current_line->index + orig_net_length;
737  }
738  }
739 
740 
741  /* Adjust length to include line termination */
742  switch (current_line->ending) {
743  case END_SOFT:
744  current_line->length = current_line->net_length + 3;
745  break;
746  case END_RICH:
747  current_line->length = current_line->net_length + 1;
748  break;
749  case END_HARD:
750  current_line->length = current_line->net_length + 2;
751  break;
752  case END_WRAP:
753  case END_0:
754  current_line->length = current_line->net_length;
755  break;
756  }
757  es->text_width = max(es->text_width, current_line->width);
758  current_position += current_line->length;
759  previous_line = current_line;
760 
761  /* Discard data for non-visible lines. It will be calculated as needed */
762  if ((line_index < es->y_offset) || (line_index > es->y_offset + vlc))
764 
765  current_line = current_line->next;
766  line_index++;
767  } while (previous_line->ending != END_0);
768 
769  /* Finish adjusting line indexes by delta or remove hanging lines */
770  if (previous_line->ending == END_0)
771  {
772  LINEDEF *pnext = NULL;
773 
774  previous_line->next = NULL;
775  while (current_line)
776  {
777  pnext = current_line->next;
779  HeapFree(GetProcessHeap(), 0, current_line);
780  current_line = pnext;
781  es->line_count--;
782  }
783  }
784  else if (delta != 0)
785  {
786  while (current_line)
787  {
788  current_line->index += delta;
789  current_line = current_line->next;
790  }
791  }
792 
793  /* Calculate rest of modification rectangle */
794  if (hrgn)
795  {
796  HRGN tmphrgn;
797  /*
798  * We calculate two rectangles. One for the first line which may have
799  * an indent with respect to the format rect. The other is a format-width
800  * rectangle that spans the rest of the lines that changed or moved.
801  */
802  rc.top = es->format_rect.top + nstart_line * es->line_height -
803  (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
804  rc.bottom = rc.top + es->line_height;
805  if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
806  rc.left = es->format_rect.left;
807  else
808 #ifdef __REACTOS__ /* CORE-11475 */
809  rc.left = (short)LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
810 #else
811  rc.left = LOWORD(EDIT_EM_PosFromChar(es, nstart_index, FALSE));
812 #endif
813  rc.right = es->format_rect.right;
814  SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
815 
816  rc.top = rc.bottom;
817  rc.left = es->format_rect.left;
818  rc.right = es->format_rect.right;
819  /*
820  * If lines were added or removed we must re-paint the remainder of the
821  * lines since the remaining lines were either shifted up or down.
822  */
823  if (line_count < es->line_count) /* We added lines */
824  rc.bottom = es->line_count * es->line_height;
825  else if (line_count > es->line_count) /* We removed lines */
826  rc.bottom = line_count * es->line_height;
827  else
828  rc.bottom = line_index * es->line_height;
829  rc.bottom += es->format_rect.top;
830  rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
831  tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
832  CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
833  DeleteObject(tmphrgn);
834  }
835 }
836 
837 /*********************************************************************
838  *
839  * EDIT_CalcLineWidth_SL
840  *
841  */
843 {
845  if (es->ssa)
846  {
847  const SIZE *size;
848  size = ScriptString_pSize(es->ssa);
849  es->text_width = size->cx;
850  }
851  else
852  es->text_width = 0;
853 }
854 
855 /*********************************************************************
856  *
857  * EDIT_CharFromPos
858  *
859  * Beware: This is not the function called on EM_CHARFROMPOS
860  * The position _can_ be outside the formatting / client
861  * rectangle
862  * The return value is only the character index
863  *
864  */
865 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
866 {
867  INT index;
868 
869  if (es->style & ES_MULTILINE) {
870  int trailing;
871  INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
872  INT line_index = 0;
873  LINEDEF *line_def = es->first_line_def;
875  while ((line > 0) && line_def->next) {
876  line_index += line_def->length;
877  line_def = line_def->next;
878  line--;
879  }
880 
881  x += es->x_offset - es->format_rect.left;
882  if (es->style & ES_RIGHT)
883  x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
884  else if (es->style & ES_CENTER)
885  x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
886  if (x >= line_def->width) {
887  if (after_wrap)
888  *after_wrap = (line_def->ending == END_WRAP);
889  return line_index + line_def->net_length;
890  }
892  if (after_wrap)
893  *after_wrap = FALSE;
894  return line_index;
895  }
896 
897  ScriptStringXtoCP(line_def->ssa, x , &index, &trailing);
898  if (trailing) index++;
899  index += line_index;
900  if (after_wrap)
901  *after_wrap = ((index == line_index + line_def->net_length) &&
902  (line_def->ending == END_WRAP));
903  } else {
904  INT xoff = 0;
905  INT trailing;
906  if (after_wrap)
907  *after_wrap = FALSE;
908  x -= es->format_rect.left;
909  if (!x)
910  return es->x_offset;
911 
912  if (!es->x_offset)
913  {
914  INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
915  if (es->style & ES_RIGHT)
916  x -= indent;
917  else if (es->style & ES_CENTER)
918  x -= indent / 2;
919  }
920 
922  if (es->x_offset)
923  {
924  if (es->ssa)
925  {
926  if (es->x_offset>= get_text_length(es))
927  {
928  const SIZE *size;
929  size = ScriptString_pSize(es->ssa);
930  xoff = size->cx;
931  }
932  ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
933  }
934  else
935  xoff = 0;
936  }
937  if (x < 0)
938  {
939  if (x + xoff > 0 || !es->ssa)
940  {
941  ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
942  if (trailing) index++;
943  }
944  else
945  index = 0;
946  }
947  else
948  {
949  if (x)
950  {
951  const SIZE *size = NULL;
952  if (es->ssa)
953  size = ScriptString_pSize(es->ssa);
954  if (!size)
955  index = 0;
956  else if (x > size->cx)
958  else if (es->ssa)
959  {
960  ScriptStringXtoCP(es->ssa, x+xoff, &index, &trailing);
961  if (trailing) index++;
962  }
963  else
964  index = 0;
965  }
966  else
967  index = es->x_offset;
968  }
969  }
970  return index;
971 }
972 
973 
974 /*********************************************************************
975  *
976  * EDIT_ConfinePoint
977  *
978  * adjusts the point to be within the formatting rectangle
979  * (so CharFromPos returns the nearest _visible_ character)
980  *
981  */
982 static void EDIT_ConfinePoint(const EDITSTATE *es, LPINT x, LPINT y)
983 {
984  *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
985  *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
986 }
987 
988 
989 /*********************************************************************
990  *
991  * EM_LINEFROMCHAR
992  *
993  */
995 {
996  INT line;
997  LINEDEF *line_def;
998 
999  if (!(es->style & ES_MULTILINE))
1000  return 0;
1001  if (index > (INT)get_text_length(es))
1002  return es->line_count - 1;
1003  if (index == -1)
1004  index = min(es->selection_start, es->selection_end);
1005 
1006  line = 0;
1007  line_def = es->first_line_def;
1008  index -= line_def->length;
1009  while ((index >= 0) && line_def->next) {
1010  line++;
1011  line_def = line_def->next;
1012  index -= line_def->length;
1013  }
1014  return line;
1015 }
1016 
1017 
1018 /*********************************************************************
1019  *
1020  * EM_LINEINDEX
1021  *
1022  */
1024 {
1025  INT line_index;
1026  const LINEDEF *line_def;
1027 
1028  if (!(es->style & ES_MULTILINE))
1029  return 0;
1030  if (line >= es->line_count)
1031  return -1;
1032 
1033  line_index = 0;
1034  line_def = es->first_line_def;
1035  if (line == -1) {
1036  INT index = es->selection_end - line_def->length;
1037  while ((index >= 0) && line_def->next) {
1038  line_index += line_def->length;
1039  line_def = line_def->next;
1040  index -= line_def->length;
1041  }
1042  } else {
1043  while (line > 0) {
1044  line_index += line_def->length;
1045  line_def = line_def->next;
1046  line--;
1047  }
1048  }
1049  return line_index;
1050 }
1051 
1052 
1053 /*********************************************************************
1054  *
1055  * EM_LINELENGTH
1056  *
1057  */
1059 {
1060  LINEDEF *line_def;
1061 
1062  if (!(es->style & ES_MULTILINE))
1063  return get_text_length(es);
1064 
1065  if (index == -1) {
1066  /* get the number of remaining non-selected chars of selected lines */
1067  INT32 l; /* line number */
1068  INT32 li; /* index of first char in line */
1069  INT32 count;
1070  l = EDIT_EM_LineFromChar(es, es->selection_start);
1071  /* # chars before start of selection area */
1072  count = es->selection_start - EDIT_EM_LineIndex(es, l);
1073  l = EDIT_EM_LineFromChar(es, es->selection_end);
1074  /* # chars after end of selection */
1075  li = EDIT_EM_LineIndex(es, l);
1076  count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
1077  return count;
1078  }
1079  line_def = es->first_line_def;
1080  index -= line_def->length;
1081  while ((index >= 0) && line_def->next) {
1082  line_def = line_def->next;
1083  index -= line_def->length;
1084  }
1085  return line_def->net_length;
1086 }
1087 
1088 
1089 /*********************************************************************
1090  *
1091  * EM_POSFROMCHAR
1092  *
1093  */
1095 {
1097  INT l;
1098  INT li;
1099  INT x = 0;
1100  INT y = 0;
1101  INT w;
1102  INT lw;
1103  LINEDEF *line_def;
1104 
1105  index = min(index, len);
1106  if (es->style & ES_MULTILINE) {
1109 
1110  y = (l - es->y_offset) * es->line_height;
1111  li = EDIT_EM_LineIndex(es, l);
1112  if (after_wrap && (li == index) && l) {
1113  INT l2 = l - 1;
1114  line_def = es->first_line_def;
1115  while (l2) {
1116  line_def = line_def->next;
1117  l2--;
1118  }
1119  if (line_def->ending == END_WRAP) {
1120  l--;
1121  y -= es->line_height;
1122  li = EDIT_EM_LineIndex(es, l);
1123  }
1124  }
1125 
1126  line_def = es->first_line_def;
1127  while (line_def->index != li)
1128  line_def = line_def->next;
1129 
1130  lw = line_def->width;
1131  w = es->format_rect.right - es->format_rect.left;
1132  if (line_def->ssa)
1133  {
1134  ScriptStringCPtoX(line_def->ssa, (index - 1) - li, TRUE, &x);
1135  x -= es->x_offset;
1136  }
1137  else
1138 #ifdef __REACTOS__ /* CORE-15780 */
1139  x = (lw > 0 ? es->x_offset : x - es->x_offset);
1140 #else
1141  x = es->x_offset;
1142 #endif
1143 
1144  if (es->style & ES_RIGHT)
1145  x = w - (lw - x);
1146  else if (es->style & ES_CENTER)
1147  x += (w - lw) / 2;
1148  } else {
1149  INT xoff = 0;
1150  INT xi = 0;
1152  if (es->x_offset)
1153  {
1154  if (es->ssa)
1155  {
1156  if (es->x_offset >= get_text_length(es))
1157  {
1158  int leftover = es->x_offset - get_text_length(es);
1159  if (es->ssa)
1160  {
1161  const SIZE *size;
1162  size = ScriptString_pSize(es->ssa);
1163  xoff = size->cx;
1164  }
1165  else
1166  xoff = 0;
1167  xoff += es->char_width * leftover;
1168  }
1169  else
1170  ScriptStringCPtoX(es->ssa, es->x_offset, FALSE, &xoff);
1171  }
1172  else
1173  xoff = 0;
1174  }
1175  if (index)
1176  {
1177  if (index >= get_text_length(es))
1178  {
1179  if (es->ssa)
1180  {
1181  const SIZE *size;
1182  size = ScriptString_pSize(es->ssa);
1183  xi = size->cx;
1184  }
1185  else
1186  xi = 0;
1187  }
1188  else if (es->ssa)
1189  ScriptStringCPtoX(es->ssa, index, FALSE, &xi);
1190  else
1191  xi = 0;
1192  }
1193  x = xi - xoff;
1194 
1195  if (index >= es->x_offset) {
1196  if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
1197  {
1198  w = es->format_rect.right - es->format_rect.left;
1199  if (w > es->text_width)
1200  {
1201  if (es->style & ES_RIGHT)
1202  x += w - es->text_width;
1203  else if (es->style & ES_CENTER)
1204  x += (w - es->text_width) / 2;
1205  }
1206  }
1207  }
1208  y = 0;
1209  }
1210  x += es->format_rect.left;
1211  y += es->format_rect.top;
1212  return MAKELONG((INT16)x, (INT16)y);
1213 }
1214 
1215 
1216 /*********************************************************************
1217  *
1218  * EDIT_GetLineRect
1219  *
1220  * Calculates the bounding rectangle for a line from a starting
1221  * column to an ending column.
1222  *
1223  */
1224 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1225 {
1227  INT line_index = 0;
1228  INT pt1, pt2, pt3;
1229 
1230  if (es->style & ES_MULTILINE)
1231  {
1232  const LINEDEF *line_def = NULL;
1233  rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1234  if (line >= es->line_count)
1235  return;
1236 
1237  line_def = es->first_line_def;
1238  if (line == -1) {
1239  INT index = es->selection_end - line_def->length;
1240  while ((index >= 0) && line_def->next) {
1241  line_index += line_def->length;
1242  line_def = line_def->next;
1243  index -= line_def->length;
1244  }
1245  } else {
1246  while (line > 0) {
1247  line_index += line_def->length;
1248  line_def = line_def->next;
1249  line--;
1250  }
1251  }
1252  ssa = line_def->ssa;
1253  }
1254  else
1255  {
1256  line_index = 0;
1257  rc->top = es->format_rect.top;
1258  ssa = es->ssa;
1259  }
1260 
1261  rc->bottom = rc->top + es->line_height;
1262  pt1 = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1263  pt2 = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1264  if (ssa)
1265  {
1266  ScriptStringCPtoX(ssa, scol, FALSE, &pt3);
1267  pt3+=es->format_rect.left;
1268  }
1269  else pt3 = pt1;
1270  rc->right = max(max(pt1 , pt2),pt3);
1271  rc->left = min(min(pt1, pt2),pt3);
1272 }
1273 
1274 
1275 static inline void text_buffer_changed(EDITSTATE *es)
1276 {
1277  es->text_length = (UINT)-1;
1278 
1279  HeapFree( GetProcessHeap(), 0, es->logAttr );
1280  es->logAttr = NULL;
1282 }
1283 
1284 /*********************************************************************
1285  * EDIT_LockBuffer
1286  *
1287  */
1289 {
1290  if (es->hlocapp) return;
1291 
1292  if (!es->text) {
1293 
1294 #ifdef __REACTOS__
1295 /* FIXME: What is this ? */
1296  CHAR *textA = NULL; // ReactOS Hacked! r45670
1297  //UINT countA = 0;
1298 
1299  if(es->hloc32W)
1300  {
1301  if(es->hloc32A)
1302  {
1303  TRACE("Synchronizing with 32-bit ANSI buffer\n");
1304  textA = LocalLock(es->hloc32A);
1305  //countA = strlen(textA) + 1;
1306  }
1307  }
1308  else {
1309  ERR("no buffer ... please report\n");
1310  return;
1311  }
1312 
1313  if (textA)
1314  {
1315 #else
1316  if(!es->hloc32W) return;
1317 
1318  if(es->hloc32A)
1319  {
1320  CHAR *textA = LocalLock(es->hloc32A);
1321 #endif
1322  HLOCAL hloc32W_new;
1323  UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
1324  if(countW_new > es->buffer_size + 1)
1325  {
1326  UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1327  TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1328  hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1329  if(hloc32W_new)
1330  {
1331  es->hloc32W = hloc32W_new;
1332  es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1333  TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1334  }
1335  else
1336  WARN("FAILED! Will synchronize partially\n");
1337  }
1338  es->text = LocalLock(es->hloc32W);
1339  MultiByteToWideChar(CP_ACP, 0, textA, -1, es->text, es->buffer_size + 1);
1340  LocalUnlock(es->hloc32A);
1341  }
1342  else es->text = LocalLock(es->hloc32W);
1343  }
1344  es->lock_count++;
1345 }
1346 
1347 
1348 /*********************************************************************
1349  *
1350  * EDIT_UnlockBuffer
1351  *
1352  */
1353 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
1354 {
1355  if (es->hlocapp) return;
1356 
1357  /* Edit window might be already destroyed */
1358  if(!IsWindow(es->hwndSelf))
1359  {
1360  WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
1361  return;
1362  }
1363 
1364  if (!es->lock_count) {
1365  ERR("lock_count == 0 ... please report\n");
1366  return;
1367  }
1368  if (!es->text) {
1369  ERR("es->text == 0 ... please report\n");
1370  return;
1371  }
1372 
1373  if (force || (es->lock_count == 1)) {
1374  if (es->hloc32W) {
1375  UINT countA = 0;
1376  UINT countW = get_text_length(es) + 1;
1377 
1378  if(es->hloc32A)
1379  {
1380  UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
1381  TRACE("Synchronizing with 32-bit ANSI buffer\n");
1382  TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
1383  countA = LocalSize(es->hloc32A);
1384  if(countA_new > countA)
1385  {
1386  HLOCAL hloc32A_new;
1387  UINT alloc_size = ROUND_TO_GROW(countA_new);
1388  TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
1389  hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1390  if(hloc32A_new)
1391  {
1392  es->hloc32A = hloc32A_new;
1393  countA = LocalSize(hloc32A_new);
1394  TRACE("Real new size %d bytes\n", countA);
1395  }
1396  else
1397  WARN("FAILED! Will synchronize partially\n");
1398  }
1399  WideCharToMultiByte(CP_ACP, 0, es->text, countW,
1400  LocalLock(es->hloc32A), countA, NULL, NULL);
1401  LocalUnlock(es->hloc32A);
1402  }
1403 
1404  LocalUnlock(es->hloc32W);
1405  es->text = NULL;
1406  }
1407  else {
1408  ERR("no buffer ... please report\n");
1409  return;
1410  }
1411  }
1412  es->lock_count--;
1413 }
1414 
1415 
1416 /*********************************************************************
1417  *
1418  * EDIT_MakeFit
1419  *
1420  * Try to fit size + 1 characters in the buffer.
1421  */
1423 {
1424  HLOCAL hNew32W;
1425 
1427  return TRUE;
1428 
1429  TRACE("trying to ReAlloc to %d+1 characters\n", size);
1430 
1431  /* Force edit to unlock its buffer. es->text now NULL */
1433 
1434  if (es->hloc32W) {
1435  UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1436  if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1437  TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1438  es->hloc32W = hNew32W;
1439  es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1440  }
1441  }
1442 
1444 
1445  if (es->buffer_size < size) {
1446  WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1448  return FALSE;
1449  } else {
1450  TRACE("We now have %d+1\n", es->buffer_size);
1451  return TRUE;
1452  }
1453 }
1454 
1455 
1456 /*********************************************************************
1457  *
1458  * EDIT_MakeUndoFit
1459  *
1460  * Try to fit size + 1 bytes in the undo buffer.
1461  *
1462  */
1464 {
1465  UINT alloc_size;
1466 
1467  if (size <= es->undo_buffer_size)
1468  return TRUE;
1469 
1470  TRACE("trying to ReAlloc to %d+1\n", size);
1471 
1472  alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1473  if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1474  es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
1475  return TRUE;
1476  }
1477  else
1478  {
1479  WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1480  return FALSE;
1481  }
1482 }
1483 
1484 
1485 /*********************************************************************
1486  *
1487  * EDIT_UpdateTextRegion
1488  *
1489  */
1490 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
1491 {
1492  if (es->flags & EF_UPDATE) {
1493  es->flags &= ~EF_UPDATE;
1495  }
1496  InvalidateRgn(es->hwndSelf, hrgn, bErase);
1497 }
1498 
1499 
1500 /*********************************************************************
1501  *
1502  * EDIT_UpdateText
1503  *
1504  */
1505 static void EDIT_UpdateText(EDITSTATE *es, const RECT *rc, BOOL bErase)
1506 {
1507  if (es->flags & EF_UPDATE) {
1508  es->flags &= ~EF_UPDATE;
1510  }
1511  InvalidateRect(es->hwndSelf, rc, bErase);
1512 }
1513 
1514 /*********************************************************************
1515  *
1516  * EDIT_SL_InvalidateText
1517  *
1518  * Called from EDIT_InvalidateText().
1519  * Does the job for single-line controls only.
1520  *
1521  */
1523 {
1524  RECT line_rect;
1525  RECT rc;
1526 
1527  EDIT_GetLineRect(es, 0, start, end, &line_rect);
1528  if (IntersectRect(&rc, &line_rect, &es->format_rect))
1529  EDIT_UpdateText(es, &rc, TRUE);
1530 }
1531 
1532 /*********************************************************************
1533  *
1534  * EDIT_ML_InvalidateText
1535  *
1536  * Called from EDIT_InvalidateText().
1537  * Does the job for multi-line controls only.
1538  *
1539  */
1541 {
1544  INT el = EDIT_EM_LineFromChar(es, end);
1545  INT sc;
1546  INT ec;
1547  RECT rc1;
1548  RECT rcWnd;
1549  RECT rcLine;
1550  RECT rcUpdate;
1551  INT l;
1552 
1553  if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1554  return;
1555 
1556  sc = start - EDIT_EM_LineIndex(es, sl);
1557  ec = end - EDIT_EM_LineIndex(es, el);
1558  if (sl < es->y_offset) {
1559  sl = es->y_offset;
1560  sc = 0;
1561  }
1562  if (el > es->y_offset + vlc) {
1563  el = es->y_offset + vlc;
1565  }
1566  GetClientRect(es->hwndSelf, &rc1);
1567  IntersectRect(&rcWnd, &rc1, &es->format_rect);
1568  if (sl == el) {
1569  EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1570  if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1571  EDIT_UpdateText(es, &rcUpdate, TRUE);
1572  } else {
1573  EDIT_GetLineRect(es, sl, sc,
1575  EDIT_EM_LineIndex(es, sl)),
1576  &rcLine);
1577  if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1578  EDIT_UpdateText(es, &rcUpdate, TRUE);
1579  for (l = sl + 1 ; l < el ; l++) {
1580  EDIT_GetLineRect(es, l, 0,
1582  EDIT_EM_LineIndex(es, l)),
1583  &rcLine);
1584  if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1585  EDIT_UpdateText(es, &rcUpdate, TRUE);
1586  }
1587  EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1588  if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1589  EDIT_UpdateText(es, &rcUpdate, TRUE);
1590  }
1591 }
1592 
1593 
1594 /*********************************************************************
1595  *
1596  * EDIT_InvalidateText
1597  *
1598  * Invalidate the text from offset start up to, but not including,
1599  * offset end. Useful for (re)painting the selection.
1600  * Regions outside the linewidth are not invalidated.
1601  * end == -1 means end == TextLength.
1602  * start and end need not be ordered.
1603  *
1604  */
1606 {
1607  if (end == start)
1608  return;
1609 
1610  if (end == -1)
1611  end = get_text_length(es);
1612 
1613  if (end < start) {
1614  INT tmp = start;
1615  start = end;
1616  end = tmp;
1617  }
1618 
1619  if (es->style & ES_MULTILINE)
1621  else
1623 }
1624 
1625 
1626 /*********************************************************************
1627  *
1628  * EDIT_EM_SetSel
1629  *
1630  * note: unlike the specs say: the order of start and end
1631  * _is_ preserved in Windows. (i.e. start can be > end)
1632  * In other words: this handler is OK
1633  *
1634  */
1635 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
1636 {
1637  UINT old_start = es->selection_start;
1638  UINT old_end = es->selection_end;
1640 
1641  if (start == (UINT)-1) {
1642  start = es->selection_end;
1643  end = es->selection_end;
1644  } else {
1645  start = min(start, len);
1646  end = min(end, len);
1647  }
1648  es->selection_start = start;
1649  es->selection_end = end;
1650  if (after_wrap)
1651  es->flags |= EF_AFTER_WRAP;
1652  else
1653  es->flags &= ~EF_AFTER_WRAP;
1654  /* Compute the necessary invalidation region. */
1655  /* Note that we don't need to invalidate regions which have
1656  * "never" been selected, or those which are "still" selected.
1657  * In fact, every time we hit a selection boundary, we can
1658  * *toggle* whether we need to invalidate. Thus we can optimize by
1659  * *sorting* the interval endpoints. Let's assume that we sort them
1660  * in this order:
1661  * start <= end <= old_start <= old_end
1662  * Knuth 5.3.1 (p 183) assures us that this can be done optimally
1663  * in 5 comparisons; i.e. it is impossible to do better than the
1664  * following: */
1665  ORDER_UINT(end, old_end);
1666  ORDER_UINT(start, old_start);
1667  ORDER_UINT(old_start, old_end);
1668  ORDER_UINT(start, end);
1669  /* Note that at this point 'end' and 'old_start' are not in order, but
1670  * start is definitely the min. and old_end is definitely the max. */
1671  if (end != old_start)
1672  {
1673 /*
1674  * One can also do
1675  * ORDER_UINT32(end, old_start);
1676  * EDIT_InvalidateText(es, start, end);
1677  * EDIT_InvalidateText(es, old_start, old_end);
1678  * in place of the following if statement.
1679  * (That would complete the optimal five-comparison four-element sort.)
1680  */
1681  if (old_start > end )
1682  {
1684  EDIT_InvalidateText(es, old_start, old_end);
1685  }
1686  else
1687  {
1688  EDIT_InvalidateText(es, start, old_start);
1689  EDIT_InvalidateText(es, end, old_end);
1690  }
1691  }
1692  else EDIT_InvalidateText(es, start, old_end);
1693 }
1694 
1695 
1696 /*********************************************************************
1697  *
1698  * EDIT_UpdateScrollInfo
1699  *
1700  */
1702 {
1703  if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
1704  {
1705  SCROLLINFO si;
1706  si.cbSize = sizeof(SCROLLINFO);
1708  si.nMin = 0;
1709  si.nMax = es->line_count - 1;
1710  si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1711  si.nPos = es->y_offset;
1712  TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1713  si.nMin, si.nMax, si.nPage, si.nPos);
1714  SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
1715  }
1716 
1717  if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
1718  {
1719  SCROLLINFO si;
1720  si.cbSize = sizeof(SCROLLINFO);
1722  si.nMin = 0;
1723  si.nMax = es->text_width - 1;
1724  si.nPage = es->format_rect.right - es->format_rect.left;
1725  si.nPos = es->x_offset;
1726  TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
1727  si.nMin, si.nMax, si.nPage, si.nPos);
1728  SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
1729  }
1730 }
1731 
1732 
1733 /*********************************************************************
1734  *
1735  * EDIT_EM_LineScroll_internal
1736  *
1737  * Version of EDIT_EM_LineScroll for internal use.
1738  * It doesn't refuse if ES_MULTILINE is set and assumes that
1739  * dx is in pixels, dy - in lines.
1740  *
1741  */
1743 {
1744  INT nyoff;
1745  INT x_offset_in_pixels;
1746  INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
1747  es->line_height;
1748 
1749  if (es->style & ES_MULTILINE)
1750  {
1751  x_offset_in_pixels = es->x_offset;
1752  }
1753  else
1754  {
1755  dy = 0;
1756  x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
1757  }
1758 
1759  if (-dx > x_offset_in_pixels)
1760  dx = -x_offset_in_pixels;
1761  if (dx > es->text_width - x_offset_in_pixels)
1762  dx = es->text_width - x_offset_in_pixels;
1763  nyoff = max(0, es->y_offset + dy);
1764  if (nyoff >= es->line_count - lines_per_page)
1765  nyoff = max(0, es->line_count - lines_per_page);
1766  dy = (es->y_offset - nyoff) * es->line_height;
1767  if (dx || dy) {
1768  RECT rc1;
1769  RECT rc;
1770 
1771  es->y_offset = nyoff;
1772  if(es->style & ES_MULTILINE)
1773  es->x_offset += dx;
1774  else
1775  es->x_offset += dx / es->char_width;
1776 
1777  GetClientRect(es->hwndSelf, &rc1);
1778  IntersectRect(&rc, &rc1, &es->format_rect);
1779  ScrollWindowEx(es->hwndSelf, -dx, dy,
1780  NULL, &rc, NULL, NULL, SW_INVALIDATE);
1781  /* force scroll info update */
1783  }
1784  if (dx && !(es->flags & EF_HSCROLL_TRACK))
1786  if (dy && !(es->flags & EF_VSCROLL_TRACK))
1788  return TRUE;
1789 }
1790 
1791 /*********************************************************************
1792  *
1793  * EM_LINESCROLL
1794  *
1795  * NOTE: dx is in average character widths, dy - in lines;
1796  *
1797  */
1799 {
1800  if (!(es->style & ES_MULTILINE))
1801  return FALSE;
1802 
1803  dx *= es->char_width;
1804  return EDIT_EM_LineScroll_internal(es, dx, dy);
1805 }
1806 
1807 
1808 /*********************************************************************
1809  *
1810  * EM_SCROLL
1811  *
1812  */
1814 {
1815  INT dy;
1816 
1817  if (!(es->style & ES_MULTILINE))
1818  return (LRESULT)FALSE;
1819 
1820  dy = 0;
1821 
1822  switch (action) {
1823  case SB_LINEUP:
1824  if (es->y_offset)
1825  dy = -1;
1826  break;
1827  case SB_LINEDOWN:
1828  if (es->y_offset < es->line_count - 1)
1829  dy = 1;
1830  break;
1831  case SB_PAGEUP:
1832  if (es->y_offset)
1833  dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
1834  break;
1835  case SB_PAGEDOWN:
1836  if (es->y_offset < es->line_count - 1)
1837  dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1838  break;
1839  default:
1840  return (LRESULT)FALSE;
1841  }
1842  if (dy) {
1844  /* check if we are going to move too far */
1845  if(es->y_offset + dy > es->line_count - vlc)
1846  dy = max(es->line_count - vlc, 0) - es->y_offset;
1847 
1848  /* Notification is done in EDIT_EM_LineScroll */
1849  if(dy) {
1850  EDIT_EM_LineScroll(es, 0, dy);
1851  return MAKELONG(dy, TRUE);
1852  }
1853 
1854  }
1855  return (LRESULT)FALSE;
1856 }
1857 
1858 
1859 /*********************************************************************
1860  *
1861  * EDIT_SetCaretPos
1862  *
1863  */
1865  BOOL after_wrap)
1866 {
1867  LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
1868  TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
1869  SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
1870 }
1871 
1872 
1873 /*********************************************************************
1874  *
1875  * EM_SCROLLCARET
1876  *
1877  */
1879 {
1880  if (es->style & ES_MULTILINE) {
1881  INT l;
1882  INT vlc;
1883  INT ww;
1884  INT cw = es->char_width;
1885  INT x;
1886  INT dy = 0;
1887  INT dx = 0;
1888 
1889  l = EDIT_EM_LineFromChar(es, es->selection_end);
1890  x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
1891  vlc = get_vertical_line_count(es);
1892  if (l >= es->y_offset + vlc)
1893  dy = l - vlc + 1 - es->y_offset;
1894  if (l < es->y_offset)
1895  dy = l - es->y_offset;
1896  ww = es->format_rect.right - es->format_rect.left;
1897  if (x < es->format_rect.left)
1898  dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1899  if (x > es->format_rect.right)
1900  dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1901  if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1902  {
1903  /* check if we are going to move too far */
1904  if(es->x_offset + dx + ww > es->text_width)
1905  dx = es->text_width - ww - es->x_offset;
1906  if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
1908  }
1909  } else {
1910  INT x;
1911  INT goal;
1912  INT format_width;
1913 
1914  x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1915  format_width = es->format_rect.right - es->format_rect.left;
1916  if (x < es->format_rect.left) {
1917  goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1918  do {
1919  es->x_offset--;
1920  x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1921  } while ((x < goal) && es->x_offset);
1922  /* FIXME: use ScrollWindow() somehow to improve performance */
1924  } else if (x > es->format_rect.right) {
1925  INT x_last;
1927  goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1928  do {
1929  es->x_offset++;
1930  x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
1931  x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
1932  } while ((x > goal) && (x_last > es->format_rect.right));
1933  /* FIXME: use ScrollWindow() somehow to improve performance */
1935  }
1936  }
1937 
1938  if(es->flags & EF_FOCUSED)
1939  EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
1940 }
1941 
1942 
1943 /*********************************************************************
1944  *
1945  * EDIT_MoveBackward
1946  *
1947  */
1948 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
1949 {
1950  INT e = es->selection_end;
1951 
1952  if (e) {
1953  e--;
1954  if ((es->style & ES_MULTILINE) && e &&
1955  (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1956  e--;
1957  if (e && (es->text[e - 1] == '\r'))
1958  e--;
1959  }
1960  }
1961  EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
1963 }
1964 
1965 
1966 /*********************************************************************
1967  *
1968  * EDIT_MoveDown_ML
1969  *
1970  * Only for multi line controls
1971  * Move the caret one line down, on a column with the nearest
1972  * x coordinate on the screen (might be a different column).
1973  *
1974  */
1975 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
1976 {
1977  INT s = es->selection_start;
1978  INT e = es->selection_end;
1979  BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1980  LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
1981  INT x = (short)LOWORD(pos);
1982  INT y = (short)HIWORD(pos);
1983 
1984  e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
1985  if (!extend)
1986  s = e;
1987  EDIT_EM_SetSel(es, s, e, after_wrap);
1989 }
1990 
1991 
1992 /*********************************************************************
1993  *
1994  * EDIT_MoveEnd
1995  *
1996  */
1997 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend, BOOL ctrl)
1998 {
1999  BOOL after_wrap = FALSE;
2000  INT e;
2001 
2002  /* Pass a high value in x to make sure of receiving the end of the line */
2003  if (!ctrl && (es->style & ES_MULTILINE))
2004  e = EDIT_CharFromPos(es, 0x3fffffff,
2005  HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2006  else
2007  e = get_text_length(es);
2008  EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2010 }
2011 
2012 
2013 /*********************************************************************
2014  *
2015  * EDIT_MoveForward
2016  *
2017  */
2018 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2019 {
2020  INT e = es->selection_end;
2021 
2022  if (es->text[e]) {
2023  e++;
2024  if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2025  if (es->text[e] == '\n')
2026  e++;
2027  else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2028  e += 2;
2029  }
2030  }
2031  EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2033 }
2034 
2035 
2036 /*********************************************************************
2037  *
2038  * EDIT_MoveHome
2039  *
2040  * Home key: move to beginning of line.
2041  *
2042  */
2043 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend, BOOL ctrl)
2044 {
2045  INT e;
2046 
2047  /* Pass the x_offset in x to make sure of receiving the first position of the line */
2048  if (!ctrl && (es->style & ES_MULTILINE))
2049  e = EDIT_CharFromPos(es, -es->x_offset,
2050  HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2051  else
2052  e = 0;
2053  EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2055 }
2056 
2057 
2058 /*********************************************************************
2059  *
2060  * EDIT_MovePageDown_ML
2061  *
2062  * Only for multi line controls
2063  * Move the caret one page down, on a column with the nearest
2064  * x coordinate on the screen (might be a different column).
2065  *
2066  */
2067 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2068 {
2069  INT s = es->selection_start;
2070  INT e = es->selection_end;
2071  BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2072  LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2073  INT x = (short)LOWORD(pos);
2074  INT y = (short)HIWORD(pos);
2075 
2076  e = EDIT_CharFromPos(es, x,
2077  y + (es->format_rect.bottom - es->format_rect.top),
2078  &after_wrap);
2079  if (!extend)
2080  s = e;
2081  EDIT_EM_SetSel(es, s, e, after_wrap);
2083 }
2084 
2085 
2086 /*********************************************************************
2087  *
2088  * EDIT_MovePageUp_ML
2089  *
2090  * Only for multi line controls
2091  * Move the caret one page up, on a column with the nearest
2092  * x coordinate on the screen (might be a different column).
2093  *
2094  */
2095 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2096 {
2097  INT s = es->selection_start;
2098  INT e = es->selection_end;
2099  BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2100  LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2101  INT x = (short)LOWORD(pos);
2102  INT y = (short)HIWORD(pos);
2103 
2104  e = EDIT_CharFromPos(es, x,
2105  y - (es->format_rect.bottom - es->format_rect.top),
2106  &after_wrap);
2107  if (!extend)
2108  s = e;
2109  EDIT_EM_SetSel(es, s, e, after_wrap);
2111 }
2112 
2113 
2114 /*********************************************************************
2115  *
2116  * EDIT_MoveUp_ML
2117  *
2118  * Only for multi line controls
2119  * Move the caret one line up, on a column with the nearest
2120  * x coordinate on the screen (might be a different column).
2121  *
2122  */
2123 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2124 {
2125  INT s = es->selection_start;
2126  INT e = es->selection_end;
2127  BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2128  LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2129  INT x = (short)LOWORD(pos);
2130  INT y = (short)HIWORD(pos);
2131 
2132  e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2133  if (!extend)
2134  s = e;
2135  EDIT_EM_SetSel(es, s, e, after_wrap);
2137 }
2138 
2139 
2140 /*********************************************************************
2141  *
2142  * EDIT_MoveWordBackward
2143  *
2144  */
2146 {
2147  INT s = es->selection_start;
2148  INT e = es->selection_end;
2149  INT l;
2150  INT ll;
2151  INT li;
2152 
2153  l = EDIT_EM_LineFromChar(es, e);
2154  ll = EDIT_EM_LineLength(es, e);
2155  li = EDIT_EM_LineIndex(es, l);
2156  if (e - li == 0) {
2157  if (l) {
2158  li = EDIT_EM_LineIndex(es, l - 1);
2159  e = li + EDIT_EM_LineLength(es, li);
2160  }
2161  } else {
2162  e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
2163  }
2164  if (!extend)
2165  s = e;
2166  EDIT_EM_SetSel(es, s, e, FALSE);
2168 }
2169 
2170 
2171 /*********************************************************************
2172  *
2173  * EDIT_MoveWordForward
2174  *
2175  */
2176 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2177 {
2178  INT s = es->selection_start;
2179  INT e = es->selection_end;
2180  INT l;
2181  INT ll;
2182  INT li;
2183 
2184  l = EDIT_EM_LineFromChar(es, e);
2185  ll = EDIT_EM_LineLength(es, e);
2186  li = EDIT_EM_LineIndex(es, l);
2187  if (e - li == ll) {
2188  if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2189  e = EDIT_EM_LineIndex(es, l + 1);
2190  } else {
2191  e = li + EDIT_CallWordBreakProc(es,
2192  li, e - li + 1, ll, WB_RIGHT);
2193  }
2194  if (!extend)
2195  s = e;
2196  EDIT_EM_SetSel(es, s, e, FALSE);
2198 }
2199 
2200 
2201 /*********************************************************************
2202  *
2203  * EDIT_PaintText
2204  *
2205  */
2207 {
2208  COLORREF BkColor;
2209  COLORREF TextColor;
2210  LOGFONTW underline_font;
2211  HFONT hUnderline = 0;
2212  HFONT old_font = 0;
2213  INT ret;
2214  INT li;
2215  INT BkMode;
2216  SIZE size;
2217 
2218  if (!count)
2219  return 0;
2220  BkMode = GetBkMode(dc);
2221  BkColor = GetBkColor(dc);
2222  TextColor = GetTextColor(dc);
2223  if (rev) {
2224  if (es->composition_len == 0)
2225  {
2228  SetBkMode( dc, OPAQUE);
2229  }
2230  else
2231  {
2233  GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2234  underline_font.lfUnderline = TRUE;
2235  hUnderline = CreateFontIndirectW(&underline_font);
2236  old_font = SelectObject(dc,hUnderline);
2237  }
2238  }
2239  li = EDIT_EM_LineIndex(es, line);
2240  if (es->style & ES_MULTILINE) {
2241  ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2242  es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2243  } else {
2244  TextOutW(dc, x, y, es->text + li + col, count);
2245  GetTextExtentPoint32W(dc, es->text + li + col, count, &size);
2246  ret = size.cx;
2247  }
2248  if (rev) {
2249  if (es->composition_len == 0)
2250  {
2251  SetBkColor(dc, BkColor);
2252  SetTextColor(dc, TextColor);
2253  SetBkMode( dc, BkMode);
2254  }
2255  else
2256  {
2257  if (old_font)
2258  SelectObject(dc,old_font);
2259  if (hUnderline)
2260  DeleteObject(hUnderline);
2261  }
2262  }
2263  return ret;
2264 }
2265 
2266 
2267 /*********************************************************************
2268  *
2269  * EDIT_PaintLine
2270  *
2271  */
2273 {
2274  INT s = 0;
2275  INT e = 0;
2276  INT li = 0;
2277  INT ll = 0;
2278  INT x;
2279  INT y;
2280  LRESULT pos;
2282 
2283  if (es->style & ES_MULTILINE) {
2285 
2286  if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2287  return;
2288  } else if (line)
2289  return;
2290 
2291  TRACE("line=%d\n", line);
2292 
2295  x = (short)LOWORD(pos);
2296  y = (short)HIWORD(pos);
2297 
2298  if (es->style & ES_MULTILINE)
2299  {
2300  int line_idx = line;
2301  x = -es->x_offset;
2302  if (es->style & ES_RIGHT || es->style & ES_CENTER)
2303  {
2304  LINEDEF *line_def = es->first_line_def;
2305  int w, lw;
2306 
2307  while (line_def && line_idx)
2308  {
2309  line_def = line_def->next;
2310  line_idx--;
2311  }
2312  w = es->format_rect.right - es->format_rect.left;
2313  lw = line_def->width;
2314 
2315  if (es->style & ES_RIGHT)
2316  x = w - (lw - x);
2317  else if (es->style & ES_CENTER)
2318  x += (w - lw) / 2;
2319  }
2320  x += es->format_rect.left;
2321  }
2322 
2323  if (rev)
2324  {
2325  li = EDIT_EM_LineIndex(es, line);
2326  ll = EDIT_EM_LineLength(es, li);
2327  s = min(es->selection_start, es->selection_end);
2328  e = max(es->selection_start, es->selection_end);
2329  s = min(li + ll, max(li, s));
2330  e = min(li + ll, max(li, e));
2331  }
2332 
2333  if (ssa)
2334  ScriptStringOut(ssa, x, y, 0, &es->format_rect, s - li, e - li, FALSE);
2335  else if (rev && (s != e) &&
2336  ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2337  x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2338  x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2339  x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2340  } else
2341  x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2342 }
2343 
2344 
2345 /*********************************************************************
2346  *
2347  * EDIT_AdjustFormatRect
2348  *
2349  * Adjusts the format rectangle for the current font and the
2350  * current client rectangle.
2351  *
2352  */
2354 {
2355  RECT ClientRect;
2356 
2357  es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2358  if (es->style & ES_MULTILINE)
2359  {
2360  INT fw, vlc, max_x_offset, max_y_offset;
2361 
2362  vlc = get_vertical_line_count(es);
2363  es->format_rect.bottom = es->format_rect.top + vlc * es->line_height;
2364 
2365  /* correct es->x_offset */
2366  fw = es->format_rect.right - es->format_rect.left;
2367  max_x_offset = es->text_width - fw;
2368  if(max_x_offset < 0) max_x_offset = 0;
2369  if(es->x_offset > max_x_offset)
2370  es->x_offset = max_x_offset;
2371 
2372  /* correct es->y_offset */
2373  max_y_offset = es->line_count - vlc;
2374  if(max_y_offset < 0) max_y_offset = 0;
2375  if(es->y_offset > max_y_offset)
2376  es->y_offset = max_y_offset;
2377 
2378  /* force scroll info update */
2380  }
2381  else
2382  /* Windows doesn't care to fix text placement for SL controls */
2383  es->format_rect.bottom = es->format_rect.top + es->line_height;
2384 
2385  /* Always stay within the client area */
2386  GetClientRect(es->hwndSelf, &ClientRect);
2387  es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2388 
2389  if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2391 
2392  EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2393 }
2394 
2395 
2396 /*********************************************************************
2397  *
2398  * EDIT_SetRectNP
2399  *
2400  * note: this is not (exactly) the handler called on EM_SETRECTNP
2401  * it is also used to set the rect of a single line control
2402  *
2403  */
2404 static void EDIT_SetRectNP(EDITSTATE *es, const RECT *rc)
2405 {
2406  LONG_PTR ExStyle;
2407  INT bw, bh;
2408  ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2409 
2410  CopyRect(&es->format_rect, rc);
2411 
2412  if (ExStyle & WS_EX_CLIENTEDGE) {
2413  es->format_rect.left++;
2414  es->format_rect.right--;
2415 
2416  if (es->format_rect.bottom - es->format_rect.top
2417  >= es->line_height + 2)
2418  {
2419  es->format_rect.top++;
2420  es->format_rect.bottom--;
2421  }
2422  }
2423  else if (es->style & WS_BORDER) {
2425  bh = GetSystemMetrics(SM_CYBORDER) + 1;
2426  es->format_rect.left += bw;
2427  es->format_rect.right -= bw;
2428  if (es->format_rect.bottom - es->format_rect.top
2429  >= es->line_height + 2 * bh)
2430  {
2431  es->format_rect.top += bh;
2432  es->format_rect.bottom -= bh;
2433  }
2434  }
2435 
2436  es->format_rect.left += es->left_margin;
2437  es->format_rect.right -= es->right_margin;
2439 }
2440 
2441 
2442 /*********************************************************************
2443  *
2444  * EM_CHARFROMPOS
2445  *
2446  * returns line number (not index) in high-order word of result.
2447  * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2448  * to Richedit, not to the edit control. Original documentation is valid.
2449  * FIXME: do the specs mean to return -1 if outside client area or
2450  * if outside formatting rectangle ???
2451  *
2452  */
2454 {
2455  POINT pt;
2456  RECT rc;
2457  INT index;
2458 
2459  pt.x = x;
2460  pt.y = y;
2461  GetClientRect(es->hwndSelf, &rc);
2462  if (!PtInRect(&rc, pt))
2463  return -1;
2464 
2465  index = EDIT_CharFromPos(es, x, y, NULL);
2467 }
2468 
2469 
2470 /*********************************************************************
2471  *
2472  * EM_FMTLINES
2473  *
2474  * Enable or disable soft breaks.
2475  *
2476  * This means: insert or remove the soft linebreak character (\r\r\n).
2477  * Take care to check if the text still fits the buffer after insertion.
2478  * If not, notify with EN_ERRSPACE.
2479  *
2480  */
2482 {
2483  es->flags &= ~EF_USE_SOFTBRK;
2484  if (add_eol) {
2485  es->flags |= EF_USE_SOFTBRK;
2486  FIXME("soft break enabled, not implemented\n");
2487  }
2488  return add_eol;
2489 }
2490 
2491 
2492 /*********************************************************************
2493  *
2494  * EM_GETHANDLE
2495  *
2496  * Hopefully this won't fire back at us.
2497  * We always start with a fixed buffer in the local heap.
2498  * Despite of the documentation says that the local heap is used
2499  * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2500  * buffer on the local heap.
2501  *
2502  */
2504 {
2505  HLOCAL hLocal;
2506 
2507  if (!(es->style & ES_MULTILINE))
2508  return 0;
2509 
2510  if(es->is_unicode)
2511  hLocal = es->hloc32W;
2512  else
2513  {
2514  if(!es->hloc32A)
2515  {
2516  CHAR *textA;
2517  UINT countA, alloc_size;
2518  TRACE("Allocating 32-bit ANSI alias buffer\n");
2519  countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2520  alloc_size = ROUND_TO_GROW(countA);
2521  if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2522  {
2523  ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2524  return 0;
2525  }
2526  textA = LocalLock(es->hloc32A);
2527  WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2528  LocalUnlock(es->hloc32A);
2529  }
2530  hLocal = es->hloc32A;
2531  }
2532 
2534 
2535  /* The text buffer handle belongs to the app */
2536  es->hlocapp = hLocal;
2537 
2538  TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2539  return hLocal;
2540 }
2541 
2542 
2543 /*********************************************************************
2544  *
2545  * EM_GETLINE
2546  *
2547  */
2549 {
2550  LPWSTR src;
2551  INT line_len, dst_len;
2552  INT i;
2553 
2554  if (es->style & ES_MULTILINE) {
2555  if (line >= es->line_count)
2556  return 0;
2557  } else
2558  line = 0;
2559  i = EDIT_EM_LineIndex(es, line);
2560  src = es->text + i;
2561  line_len = EDIT_EM_LineLength(es, i);
2562  dst_len = *(WORD *)dst;
2563  if(unicode)
2564  {
2565  if(dst_len <= line_len)
2566  {
2567  memcpy(dst, src, dst_len * sizeof(WCHAR));
2568  return dst_len;
2569  }
2570  else /* Append 0 if enough space */
2571  {
2572  memcpy(dst, src, line_len * sizeof(WCHAR));
2573  dst[line_len] = 0;
2574  return line_len;
2575  }
2576  }
2577  else
2578  {
2579  INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2580  if(!ret && line_len) /* Insufficient buffer size */
2581  return dst_len;
2582  if(ret < dst_len) /* Append 0 if enough space */
2583  ((LPSTR)dst)[ret] = 0;
2584  return ret;
2585  }
2586 }
2587 
2588 
2589 /*********************************************************************
2590  *
2591  * EM_GETSEL
2592  *
2593  */
2595 {
2596  UINT s = es->selection_start;
2597  UINT e = es->selection_end;
2598 
2599  ORDER_UINT(s, e);
2600  if (start)
2601  *start = s;
2602  if (end)
2603  *end = e;
2604  return MAKELONG(s, e);
2605 }
2606 
2607 
2608 /*********************************************************************
2609  *
2610  * EM_REPLACESEL
2611  *
2612  * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2613  *
2614  */
2615 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
2616 {
2617  UINT strl = strlenW(lpsz_replace);
2618  UINT tl = get_text_length(es);
2619  UINT utl;
2620  UINT s;
2621  UINT e;
2622  UINT i;
2623  UINT size;
2624  LPWSTR p;
2625  HRGN hrgn = 0;
2626  LPWSTR buf = NULL;
2627  UINT bufl;
2628 
2629  TRACE("%s, can_undo %d, send_update %d\n",
2630  debugstr_w(lpsz_replace), can_undo, send_update);
2631 
2632  s = es->selection_start;
2633  e = es->selection_end;
2634 
2636  if ((s == e) && !strl)
2637  return;
2638 
2639  ORDER_UINT(s, e);
2640 
2641  size = tl - (e - s) + strl;
2642  if (!size)
2643  es->text_width = 0;
2644 
2645  /* Issue the EN_MAXTEXT notification and continue with replacing text
2646  * so that buffer limit is honored. */
2647  if ((honor_limit) && (size > es->buffer_limit)) {
2649  /* Buffer limit can be smaller than the actual length of text in combobox */
2650  if (es->buffer_limit < (tl - (e-s)))
2651  strl = 0;
2652  else
2653  strl = min(strl, es->buffer_limit - (tl - (e-s)));
2654  }
2655 
2656  if (!EDIT_MakeFit(es, tl - (e - s) + strl))
2657  return;
2658 
2659  if (e != s) {
2660  /* there is something to be deleted */
2661  TRACE("deleting stuff.\n");
2662  bufl = e - s;
2663  buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
2664  if (!buf) return;
2665  memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
2666  buf[bufl] = 0; /* ensure 0 termination */
2667  /* now delete */
2668  strcpyW(es->text + s, es->text + e);
2670  }
2671  if (strl) {
2672  /* there is an insertion */
2673  tl = get_text_length(es);
2674  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));
2675  for (p = es->text + tl ; p >= es->text + s ; p--)
2676  p[strl] = p[0];
2677  for (i = 0 , p = es->text + s ; i < strl ; i++)
2678  p[i] = lpsz_replace[i];
2679  if(es->style & ES_UPPERCASE)
2680  CharUpperBuffW(p, strl);
2681  else if(es->style & ES_LOWERCASE)
2682  CharLowerBuffW(p, strl);
2684  }
2685  if (es->style & ES_MULTILINE)
2686  {
2687  INT st = min(es->selection_start, es->selection_end);
2689 
2690  hrgn = CreateRectRgn(0, 0, 0, 0);
2691  EDIT_BuildLineDefs_ML(es, st, st + strl,
2692  strl - abs(es->selection_end - es->selection_start), hrgn);
2693  /* if text is too long undo all changes */
2694  if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
2695  if (strl)
2696  strcpyW(es->text + e, es->text + e + strl);
2697  if (e != s)
2698  for (i = 0 , p = es->text ; i < e - s ; i++)
2699  p[i + s] = buf[i];
2702  abs(es->selection_end - es->selection_start) - strl, hrgn);
2703  strl = 0;
2704  e = s;
2705  hrgn = CreateRectRgn(0, 0, 0, 0);
2707  }
2708  }
2709  else {
2710  INT fw = es->format_rect.right - es->format_rect.left;
2713  /* remove chars that don't fit */
2714  if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
2715  while ((es->text_width > fw) && s + strl >= s) {
2716  strcpyW(es->text + s + strl - 1, es->text + s + strl);
2717  strl--;
2718  es->text_length = -1;
2721  }
2724  }
2725  }
2726 
2727  if (e != s) {
2728  if (can_undo) {
2729  utl = strlenW(es->undo_text);
2730  if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2731  /* undo-buffer is extended to the right */
2732  EDIT_MakeUndoFit(es, utl + e - s);
2733  memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
2734  (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
2735  } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2736  /* undo-buffer is extended to the left */
2737  EDIT_MakeUndoFit(es, utl + e - s);
2738  for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2739  p[e - s] = p[0];
2740  for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2741  p[i] = buf[i];
2742  es->undo_position = s;
2743  } else {
2744  /* new undo-buffer */
2745  EDIT_MakeUndoFit(es, e - s);
2746  memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
2747  es->undo_text[e - s] = 0; /* ensure 0 termination */
2748  es->undo_position = s;
2749  }
2750  /* any deletion makes the old insertion-undo invalid */
2751  es->undo_insert_count = 0;
2752  } else
2754  }
2755  if (strl) {
2756  if (can_undo) {
2757  if ((s == es->undo_position) ||
2758  ((es->undo_insert_count) &&
2759  (s == es->undo_position + es->undo_insert_count)))
2760  /*
2761  * insertion is new and at delete position or
2762  * an extension to either left or right
2763  */
2764  es->undo_insert_count += strl;
2765  else {
2766  /* new insertion undo */
2767  es->undo_position = s;
2768  es->undo_insert_count = strl;
2769  /* new insertion makes old delete-buffer invalid */
2770  *es->undo_text = '\0';
2771  }
2772  } else
2774  }
2775 
2776  HeapFree(GetProcessHeap(), 0, buf);
2777 
2778  s += strl;
2779 
2780  /* If text has been deleted and we're right or center aligned then scroll rightward */
2781  if (es->style & (ES_RIGHT | ES_CENTER))
2782  {
2783  INT delta = strl - abs(es->selection_end - es->selection_start);
2784 
2785  if (delta < 0 && es->x_offset)
2786  {
2787  if (abs(delta) > es->x_offset)
2788  es->x_offset = 0;
2789  else
2790  es->x_offset += delta;
2791  }
2792  }
2793 
2794  EDIT_EM_SetSel(es, s, s, FALSE);
2795  es->flags |= EF_MODIFIED;
2796  if (send_update) es->flags |= EF_UPDATE;
2797  if (hrgn)
2798  {
2800  DeleteObject(hrgn);
2801  }
2802  else
2804 
2806 
2807  /* force scroll info update */
2809 
2810 
2811  if(send_update || (es->flags & EF_UPDATE))
2812  {
2813  es->flags &= ~EF_UPDATE;
2815  }
2817 }
2818 
2819 
2820 /*********************************************************************
2821  *
2822  * EM_SETHANDLE
2823  *
2824  * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2825  *
2826  */
2828 {
2829  if (!(es->style & ES_MULTILINE))
2830  return;
2831 
2832  if (!hloc) {
2833  WARN("called with NULL handle\n");
2834  return;
2835  }
2836 
2838 
2839  if(es->is_unicode)
2840  {
2841  if(es->hloc32A)
2842  {
2843  LocalFree(es->hloc32A);
2844  es->hloc32A = NULL;
2845  }
2846  es->hloc32W = hloc;
2847  }
2848  else
2849  {
2850  INT countW, countA;
2851  HLOCAL hloc32W_new;
2852  WCHAR *textW;
2853  CHAR *textA;
2854 
2855  countA = LocalSize(hloc);
2856  textA = LocalLock(hloc);
2857  countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
2858  if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
2859  {
2860  ERR("Could not allocate new unicode buffer\n");
2861  return;
2862  }
2863  textW = LocalLock(hloc32W_new);
2864  MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
2865  LocalUnlock(hloc32W_new);
2866  LocalUnlock(hloc);
2867 
2868  if(es->hloc32W)
2869  LocalFree(es->hloc32W);
2870 
2871  es->hloc32W = hloc32W_new;
2872  es->hloc32A = hloc;
2873  }
2874 
2875  es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
2876 
2877  /* The text buffer handle belongs to the control */
2878  es->hlocapp = NULL;
2879 
2882 
2883  es->x_offset = es->y_offset = 0;
2884  es->selection_start = es->selection_end = 0;
2886  es->flags &= ~EF_MODIFIED;
2887  es->flags &= ~EF_UPDATE;
2891  /* force scroll info update */
2893 }
2894 
2895 
2896 /*********************************************************************
2897  *
2898  * EM_SETLIMITTEXT
2899  *
2900  * NOTE: this version currently implements WinNT limits
2901  *
2902  */
2904 {
2905  if (!limit) limit = ~0u;
2906  if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
2907  es->buffer_limit = limit;
2908 }
2909 
2910 
2911 /*********************************************************************
2912  *
2913  * EM_SETMARGINS
2914  *
2915  * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2916  * action wParam despite what the docs say. EC_USEFONTINFO calculates the
2917  * margin according to the textmetrics of the current font.
2918  *
2919  * When EC_USEFONTINFO is used in the non_cjk case the margins only
2920  * change if the edit control is equal to or larger than a certain
2921  * size. Though there is an exception for the empty client rect case
2922  * with small font sizes.
2923  */
2925 {
2926  switch(charset)
2927  {
2928  case SHIFTJIS_CHARSET:
2929  case HANGUL_CHARSET:
2930  case GB2312_CHARSET:
2931  case CHINESEBIG5_CHARSET:
2932  return TRUE;
2933  }
2934  /* HANGUL_CHARSET is strange, though treated as CJK by Win 8, it is
2935  * not by other versions including Win 10. */
2936  return FALSE;
2937 }
2938 
2940  WORD left, WORD right, BOOL repaint)
2941 {
2942  TEXTMETRICW tm;
2943  INT default_left_margin = 0; /* in pixels */
2944  INT default_right_margin = 0; /* in pixels */
2945 
2946  /* Set the default margins depending on the font */
2947  if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
2948  HDC dc = GetDC(es->hwndSelf);
2949  HFONT old_font = SelectObject(dc, es->font);
2951  RECT rc;
2952 
2953  /* The default margins are only non zero for TrueType or Vector fonts */
2954  if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
2955  if (!is_cjk(tm.tmCharSet)) {
2956  default_left_margin = width / 2;
2957  default_right_margin = width / 2;
2958 
2959  GetClientRect(es->hwndSelf, &rc);
2960  if (rc.right - rc.left < (width / 2 + width) * 2 &&
2961  (width >= 28 || !IsRectEmpty(&rc)) ) {
2962  default_left_margin = es->left_margin;
2963  default_right_margin = es->right_margin;
2964  }
2965  } else {
2966  /* FIXME: figure out the CJK values. They are not affected by the client rect. */
2967  default_left_margin = width / 2;
2968  default_right_margin = width / 2;
2969  }
2970  }
2971  SelectObject(dc, old_font);
2972  ReleaseDC(es->hwndSelf, dc);
2973  }
2974 
2975  if (action & EC_LEFTMARGIN) {
2976  es->format_rect.left -= es->left_margin;
2977  if (left != EC_USEFONTINFO)
2978  es->left_margin = left;
2979  else
2980  es->left_margin = default_left_margin;
2981  es->format_rect.left += es->left_margin;
2982  }
2983 
2984  if (action & EC_RIGHTMARGIN) {
2985  es->format_rect.right += es->right_margin;
2986  if (right != EC_USEFONTINFO)
2987  es->right_margin = right;
2988  else
2989  es->right_margin = default_right_margin;
2990  es->format_rect.right -= es->right_margin;
2991  }
2992 
2993  if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
2995  if (repaint) EDIT_UpdateText(es, NULL, TRUE);
2996  }
2997 
2998  TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2999 }
3000 
3001 
3002 /*********************************************************************
3003  *
3004  * EM_SETPASSWORDCHAR
3005  *
3006  */
3008 {
3009  LONG style;
3010 
3011  if (es->style & ES_MULTILINE)
3012  return;
3013 
3014  if (es->password_char == c)
3015  return;
3016 
3017  style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3018  es->password_char = c;
3019  if (c) {
3020  SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3021  es->style |= ES_PASSWORD;
3022  } else {
3023  SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3024  es->style &= ~ES_PASSWORD;
3025  }
3028 }
3029 
3030 
3031 /*********************************************************************
3032  *
3033  * EM_SETTABSTOPS
3034  *
3035  */
3037 {
3038  if (!(es->style & ES_MULTILINE))
3039  return FALSE;
3040  HeapFree(GetProcessHeap(), 0, es->tabs);
3041  es->tabs_count = count;
3042  if (!count)
3043  es->tabs = NULL;
3044  else {
3045  es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3046 #ifdef __REACTOS__
3047  /* ReactOS r33503 */
3048  if (es->tabs == NULL)
3049  {
3050  es->tabs_count = 0;
3051  return FALSE;
3052  }
3053 #endif
3054  memcpy(es->tabs, tabs, count * sizeof(INT));
3055  }
3057  return TRUE;
3058 }
3059 
3060 
3061 /*********************************************************************
3062  *
3063  * EM_SETWORDBREAKPROC
3064  *
3065  */
3066 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
3067 {
3068  if (es->word_break_proc == wbp)
3069  return;
3070 
3071  es->word_break_proc = wbp;
3072 
3073  if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3076  }
3077 }
3078 
3079 
3080 /*********************************************************************
3081  *
3082  * EM_UNDO / WM_UNDO
3083  *
3084  */
3086 {
3087  INT ulength;
3088  LPWSTR utext;
3089 
3090  /* As per MSDN spec, for a single-line edit control,
3091  the return value is always TRUE */
3092  if( es->style & ES_READONLY )
3093  return !(es->style & ES_MULTILINE);
3094 
3095  ulength = strlenW(es->undo_text);
3096 
3097  utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3098 #ifdef __REACTOS__
3099  /* ReactOS r33503 */
3100  if (utext == NULL)
3101  return FALSE;
3102 #endif
3103 
3104  strcpyW(utext, es->undo_text);
3105 
3106  TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3107  es->undo_insert_count, debugstr_w(utext));
3108 
3109  EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3111  EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
3112  EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3113  /* send the notification after the selection start and end are set */
3116  HeapFree(GetProcessHeap(), 0, utext);
3117 
3118  TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3119  es->undo_insert_count, debugstr_w(es->undo_text));
3120  return TRUE;
3121 }
3122 
3123 
3124 /* Helper function for WM_CHAR
3125  *
3126  * According to an MSDN blog article titled "Just because you're a control
3127  * doesn't mean that you're necessarily inside a dialog box," multiline edit
3128  * controls without ES_WANTRETURN would attempt to detect whether it is inside
3129  * a dialog box or not.
3130  */
3132 {
3133  return (es->flags & EF_DIALOGMODE);
3134 }
3135 
3136 
3137 /*********************************************************************
3138  *
3139  * WM_PASTE
3140  *
3141  */
3143 {
3144  HGLOBAL hsrc;
3145  LPWSTR src;
3146 
3147  /* Protect read-only edit control from modification */
3148  if(es->style & ES_READONLY)
3149  return;
3150 
3151  OpenClipboard(es->hwndSelf);
3152  if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
3153  src = GlobalLock(hsrc);
3155  GlobalUnlock(hsrc);
3156  }
3157  else if (es->style & ES_PASSWORD) {
3158  /* clear selected text in password edit box even with empty clipboard */
3160  }
3161  CloseClipboard();
3162 }
3163 
3164 
3165 /*********************************************************************
3166  *
3167  * WM_COPY
3168  *
3169  */
3171 {
3172  INT s = min(es->selection_start, es->selection_end);
3173  INT e = max(es->selection_start, es->selection_end);
3174  HGLOBAL hdst;
3175  LPWSTR dst;
3176  DWORD len;
3177 
3178  if (e == s) return;
3179 
3180  len = e - s;
3181  hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
3182  dst = GlobalLock(hdst);
3183  memcpy(dst, es->text + s, len * sizeof(WCHAR));
3184  dst[len] = 0; /* ensure 0 termination */
3185  TRACE("%s\n", debugstr_w(dst));
3186  GlobalUnlock(hdst);
3187  OpenClipboard(es->hwndSelf);
3188  EmptyClipboard();
3190  CloseClipboard();
3191 }
3192 
3193 
3194 /*********************************************************************
3195  *
3196  * WM_CLEAR
3197  *
3198  */
3199 static inline void EDIT_WM_Clear(EDITSTATE *es)
3200 {
3201  /* Protect read-only edit control from modification */
3202  if(es->style & ES_READONLY)
3203  return;
3204 
3206 }
3207 
3208 
3209 /*********************************************************************
3210  *
3211  * WM_CUT
3212  *
3213  */
3214 static inline void EDIT_WM_Cut(EDITSTATE *es)
3215 {
3216  EDIT_WM_Copy(es);
3217  EDIT_WM_Clear(es);
3218 }
3219 
3220 
3221 /*********************************************************************
3222  *
3223  * WM_CHAR
3224  *
3225  */
3227 {
3228  BOOL control;
3229 
3230  control = GetKeyState(VK_CONTROL) & 0x8000;
3231 
3232  switch (c) {
3233  case '\r':
3234  /* If it's not a multiline edit box, it would be ignored below.
3235  * For multiline edit without ES_WANTRETURN, we have to make a
3236  * special case.
3237  */
3238  if ((es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3239  if (EDIT_IsInsideDialog(es))
3240  break;
3241  case '\n':
3242  if (es->style & ES_MULTILINE) {
3243  if (es->style & ES_READONLY) {
3246  } else {
3247  static const WCHAR cr_lfW[] = {'\r','\n',0};
3248  EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
3249  }
3250  }
3251  break;
3252  case '\t':
3253  if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3254  {
3255  static const WCHAR tabW[] = {'\t',0};
3256  if (EDIT_IsInsideDialog(es))
3257  break;
3258  EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
3259  }
3260  break;
3261  case VK_BACK:
3262  if (!(es->style & ES_READONLY) && !control) {
3263  if (es->selection_start != es->selection_end)
3264  EDIT_WM_Clear(es);
3265  else {
3266  /* delete character left of caret */
3267  EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3269  EDIT_WM_Clear(es);
3270  }
3271  }
3272  break;
3273  case 0x03: /* ^C */
3274  if (!(es->style & ES_PASSWORD))
3275  SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3276  break;
3277  case 0x16: /* ^V */
3278  if (!(es->style & ES_READONLY))
3279  SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3280  break;
3281  case 0x18: /* ^X */
3282  if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
3283  SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3284  break;
3285  case 0x1A: /* ^Z */
3286  if (!(es->style & ES_READONLY))
3287  SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3288  break;
3289 
3290  default:
3291  /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
3292  if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
3293  break;
3294 
3295  if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3296  WCHAR str[2];
3297  str[0] = c;
3298  str[1] = '\0';
3300  }
3301  break;
3302  }
3303  return 1;
3304 }
3305 
3306 
3307 /*********************************************************************
3308  *
3309  * EDIT_ContextMenuCommand
3310  *
3311  */
3313 {
3314  switch (id) {
3315  case EM_UNDO:
3316  SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
3317  break;
3318  case WM_CUT:
3319  SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
3320  break;
3321  case WM_COPY:
3322  SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
3323  break;
3324  case WM_PASTE:
3325  SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3326  break;
3327  case WM_CLEAR:
3328  SendMessageW(es->hwndSelf, WM_CLEAR, 0, 0);
3329  break;
3330  case EM_SETSEL:
3331  SendMessageW(es->hwndSelf, EM_SETSEL, 0, -1);
3332  break;
3333  default:
3334  ERR("unknown menu item, please report\n");
3335  break;
3336  }
3337 }
3338 
3339 
3340 /*********************************************************************
3341  *
3342  * WM_CONTEXTMENU
3343  *
3344  * Note: the resource files resource/sysres_??.rc cannot define a
3345  * single popup menu. Hence we use a (dummy) menubar
3346  * containing the single popup menu as its first item.
3347  *
3348  * FIXME: the message identifiers have been chosen arbitrarily,
3349  * hence we use MF_BYPOSITION.
3350  * We might as well use the "real" values (anybody knows ?)
3351  * The menu definition is in resources/sysres_??.rc.
3352  * Once these are OK, we better use MF_BYCOMMAND here
3353  * (as we do in EDIT_WM_Command()).
3354  *
3355  */
3357 {
3358  HMENU menu = LoadMenuA(user32_module, "EDITMENU");
3359  HMENU popup = GetSubMenu(menu, 0);
3360  UINT start = es->selection_start;
3361  UINT end = es->selection_end;
3362  UINT cmd;
3363 
3364  ORDER_UINT(start, end);
3365 
3366  /* undo */
3367  EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3368  /* cut */
3369  EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3370  /* copy */
3371  EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3372  /* paste */
3374  /* delete */
3375  EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3376  /* select all */
3378 
3379  if (x == -1 && y == -1) /* passed via VK_APPS press/release */
3380  {
3381  RECT rc;
3382  /* Windows places the menu at the edit's center in this case */
3383 #ifdef __REACTOS__
3384  /* ReactOS r55202 */
3385  GetClientRect(es->hwndSelf, &rc);
3386  MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
3387 #else
3388  WIN_GetRectangles( es->hwndSelf, COORDS_SCREEN, NULL, &rc );
3389 #endif
3390  x = rc.left + (rc.right - rc.left) / 2;
3391  y = rc.top + (rc.bottom - rc.top) / 2;
3392  }
3393 
3394  if (!(es->flags & EF_FOCUSED))
3395  SetFocus(es->hwndSelf);
3396 
3398  x, y, 0, es->hwndSelf, NULL);
3399 
3400  if (cmd)
3402 
3403  DestroyMenu(menu);
3404 }
3405 
3406 
3407 /*********************************************************************
3408  *
3409  * WM_GETTEXT
3410  *
3411  */
3413 {
3414  if(!count) return 0;
3415 
3416  if(unicode)
3417  {
3418  lstrcpynW(dst, es->text, count);
3419  return strlenW(dst);
3420  }
3421  else
3422  {
3423  LPSTR textA = (LPSTR)dst;
3424  if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
3425  textA[count - 1] = 0; /* ensure 0 termination */
3426  return strlen(textA);
3427  }
3428 }
3429 
3430 /*********************************************************************
3431  *
3432  * EDIT_CheckCombo
3433  *
3434  */
3436 {
3437  HWND hLBox = es->hwndListBox;
3438  HWND hCombo;
3439  BOOL bDropped;
3440  int nEUI;
3441 
3442  if (!hLBox)
3443  return FALSE;
3444 
3445  hCombo = GetParent(es->hwndSelf);
3446  bDropped = TRUE;
3447  nEUI = 0;
3448 
3449  TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
3450 
3451  if (key == VK_UP || key == VK_DOWN)
3452  {
3453  if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
3454  nEUI = 1;
3455 
3456  if (msg == WM_KEYDOWN || nEUI)
3457  bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3458  }
3459 
3460  switch (msg)
3461  {
3462  case WM_KEYDOWN:
3463  if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
3464  {
3465  /* make sure ComboLBox pops up */
3466  SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
3467  key = VK_F4;
3468  nEUI = 2;
3469  }
3470 
3471  SendMessageW(hLBox, WM_KEYDOWN, key, 0);
3472  break;
3473 
3474  case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3475  if (nEUI)
3476  SendMessageW(hCombo, CB_SHOWDROPDOWN, !bDropped, 0);
3477  else
3478  SendMessageW(hLBox, WM_KEYDOWN, VK_F4, 0);
3479  break;
3480  }
3481 
3482  if(nEUI == 2)
3483  SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
3484 
3485  return TRUE;
3486 }
3487 
3488 
3489 /*********************************************************************
3490  *
3491  * WM_KEYDOWN
3492  *
3493  * Handling of special keys that don't produce a WM_CHAR
3494  * (i.e. non-printable keys) & Backspace & Delete
3495  *
3496  */
3498 {
3499  BOOL shift;
3500  BOOL control;
3501 
3502  if (GetKeyState(VK_MENU) & 0x8000)
3503  return 0;
3504 
3505  shift = GetKeyState(VK_SHIFT) & 0x8000;
3506  control = GetKeyState(VK_CONTROL) & 0x8000;
3507 
3508  switch (key) {
3509  case VK_F4:
3510  case VK_UP:
3511  if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
3512  break;
3513 
3514  /* fall through */
3515  case VK_LEFT:
3516  if ((es->style & ES_MULTILINE) && (key == VK_UP))
3518  else
3519  if (control)
3521  else
3523  break;
3524  case VK_DOWN:
3526  break;
3527  /* fall through */
3528  case VK_RIGHT:
3529  if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3531  else if (control)
3533  else
3535  break;
3536  case VK_HOME:
3537  EDIT_MoveHome(es, shift, control);
3538  break;
3539  case VK_END:
3540  EDIT_MoveEnd(es, shift, control);
3541  break;
3542  case VK_PRIOR:
3543  if (es->style & ES_MULTILINE)
3545  else
3547  break;
3548  case VK_NEXT:
3549  if (es->style & ES_MULTILINE)
3551  else
3553  break;
3554  case VK_DELETE:
3555  if (!(es->style & ES_READONLY) && !(shift && control)) {
3556  if (es->selection_start != es->selection_end) {
3557  if (shift)
3558  EDIT_WM_Cut(es);
3559  else
3560  EDIT_WM_Clear(es);
3561  } else {
3562  if (shift) {
3563  /* delete character left of caret */
3564  EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3566  EDIT_WM_Clear(es);
3567  } else if (control) {
3568  /* delete to end of line */
3569  EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3571  EDIT_WM_Clear(es);
3572  } else {
3573  /* delete character right of caret */
3574  EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
3576  EDIT_WM_Clear(es);
3577  }
3578  }
3579  }
3580  break;
3581  case VK_INSERT:
3582  if (shift) {
3583  if (!(es->style & ES_READONLY))
3584  EDIT_WM_Paste(es);
3585  } else if (control)
3586  EDIT_WM_Copy(es);
3587  break;
3588  case VK_RETURN:
3589  /* If the edit doesn't want the return send a message to the default object */
3590  if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
3591  {
3592  DWORD dw;
3593 
3594  if (!EDIT_IsInsideDialog(es)) break;
3595  if (control) break;
3596  dw = SendMessageW(es->hwndParent, DM_GETDEFID, 0, 0);
3597  if (HIWORD(dw) == DC_HASDEFID)
3598  {
3599  HWND hwDefCtrl = GetDlgItem(es->hwndParent, LOWORD(dw));
3600  if (hwDefCtrl)
3601  {
3602  SendMessageW(es->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
3603  PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
3604  }
3605  }
3606  }
3607  break;
3608  case VK_ESCAPE:
3609  if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3610  PostMessageW(es->hwndParent, WM_CLOSE, 0, 0);
3611  break;
3612  case VK_TAB:
3613  if ((es->style & ES_MULTILINE) && EDIT_IsInsideDialog(es))
3614  SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
3615  break;
3616 #ifdef __REACTOS__
3617  /* ReactOS CORE-1419 */
3618  case VK_BACK:
3619  if (control)
3620  {
3621  FIXME("Ctrl+Backspace\n");
3622  }
3623  break;
3624 #endif
3625  }
3626  return TRUE;
3627 }
3628 
3629 
3630 /*********************************************************************
3631  *
3632  * WM_KILLFOCUS
3633  *
3634  */
3636 {
3637  HWND hCombo;
3638  LONG lStyles;
3639 
3640  es->flags &= ~EF_FOCUSED;
3641  DestroyCaret();
3642  if(!(es->style & ES_NOHIDESEL))
3643  EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3644 
3645  /* throw away left over scroll when we lose focus */
3646  es->wheelDeltaRemainder = 0;
3647 
3648  if (es->hwndListBox == NULL)
3650  else
3651  { /* send the undocumented WM_CBLOSTTEXTFOCUS message to combobox */
3652  hCombo = GetParent(es->hwndSelf);
3653  lStyles = GetWindowLong(hCombo, GWL_STYLE);
3654  if ((lStyles & CBS_DROPDOWN) || (lStyles & CBS_SIMPLE))
3655  SendMessage(hCombo, WM_CBLOSTTEXTFOCUS, 0, 0);
3656  }
3657 
3658  return 0;
3659 }
3660 
3661 
3662 /*********************************************************************
3663  *
3664  * WM_LBUTTONDBLCLK
3665  *
3666  * The caret position has been set on the WM_LBUTTONDOWN message
3667  *
3668  */
3670 {
3671  INT s;
3672  INT e = es->selection_end;
3673  INT l;
3674  INT li;
3675  INT ll;
3676 
3677  es->bCaptureState = TRUE;
3678  SetCapture(es->hwndSelf);
3679 
3680  l = EDIT_EM_LineFromChar(es, e);
3681  li = EDIT_EM_LineIndex(es, l);
3682  ll = EDIT_EM_LineLength(es, e);
3683  s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
3684  e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
3685  EDIT_EM_SetSel(es, s, e, FALSE);
3687  es->region_posx = es->region_posy = 0;
3688  SetTimer(es->hwndSelf, 0, 100, NULL);
3689  return 0;
3690 }
3691 
3692 
3693 /*********************************************************************
3694  *
3695  * WM_LBUTTONDOWN
3696  *
3697  */
3699 {
3700  INT e;
3701  BOOL after_wrap;
3702 
3703  es->bCaptureState = TRUE;
3704  SetCapture(es->hwndSelf);
3705  EDIT_ConfinePoint(es, &x, &y);
3706  e = EDIT_CharFromPos(es, x, y, &after_wrap);
3707  EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3709  es->region_posx = es->region_posy = 0;
3710  SetTimer(es->hwndSelf, 0, 100, NULL);
3711 
3712  if (!(es->flags & EF_FOCUSED))
3713  SetFocus(es->hwndSelf);
3714 
3715  return 0;
3716 }
3717 
3718 
3719 /*********************************************************************
3720  *
3721  * WM_LBUTTONUP
3722  *
3723  */
3725 {
3726  if (es->bCaptureState) {
3727  KillTimer(es->hwndSelf, 0);
3728  if (GetCapture() == es->hwndSelf) ReleaseCapture();
3729  }
3730  es->bCaptureState = FALSE;
3731  return 0;
3732 }
3733 
3734 
3735 /*********************************************************************
3736  *
3737  * WM_MBUTTONDOWN
3738  *
3739  */
3741 {
3742  SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
3743  return 0;
3744 }
3745 
3746 
3747 /*********************************************************************
3748  *
3749  * WM_MOUSEMOVE
3750  *
3751  */
3753 {
3754  INT e;
3755  BOOL after_wrap;
3756  INT prex, prey;
3757 
3758  /* If the mouse has been captured by process other than the edit control itself,
3759  * the windows edit controls will not select the strings with mouse move.
3760  */
3761  if (!es->bCaptureState || GetCapture() != es->hwndSelf)
3762  return 0;
3763 
3764  /*
3765  * FIXME: gotta do some scrolling if outside client
3766  * area. Maybe reset the timer ?
3767  */
3768  prex = x; prey = y;
3769  EDIT_ConfinePoint(es, &x, &y);
3770  es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3771  es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3772  e = EDIT_CharFromPos(es, x, y, &after_wrap);
3773  EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
3774  EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
3775  return 0;
3776 }
3777 
3778 
3779 /*********************************************************************
3780  *
3781  * WM_PAINT
3782  *
3783  */
3785 {
3786  PAINTSTRUCT ps;
3787  INT i;
3788  HDC dc;
3789  HFONT old_font = 0;
3790  RECT rc;
3791  RECT rcClient;
3792  RECT rcLine;
3793  RECT rcRgn;
3794  HBRUSH brush;
3795  HBRUSH old_brush;
3796  INT bw, bh;
3797  BOOL rev = es->bEnableState &&
3798  ((es->flags & EF_FOCUSED) ||
3799  (es->style & ES_NOHIDESEL));
3800  dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
3801 
3802  /* The dc we use for calculating may not be the one we paint into.
3803  This is the safest action. */
3805  GetClientRect(es->hwndSelf, &rcClient);
3806 
3807  /* get the background brush */
3808  brush = EDIT_NotifyCtlColor(es, dc);
3809 
3810  /* paint the border and the background */
3811  IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
3812 
3813  if(es->style & WS_BORDER) {
3816  rc = rcClient;
3817  if(es->style & ES_MULTILINE) {
3818  if(es->style & WS_HSCROLL) rc.bottom+=bh;
3819  if(es->style & WS_VSCROLL) rc.right+=bw;
3820  }
3821 
3822  /* Draw the frame. Same code as in nonclient.c */
3824  PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
3825  PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
3826  PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
3827  PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
3828  SelectObject(dc, old_brush);
3829 
3830  /* Keep the border clean */
3831  IntersectClipRect(dc, rc.left+bw, rc.top+bh,
3832  max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
3833  }
3834 
3835  GetClipBox(dc, &rc);
3836  FillRect(dc, &rc, brush);
3837 
3838  IntersectClipRect(dc, es->format_rect.left,
3839  es->format_rect.top,
3840  es->format_rect.right,
3841  es->format_rect.bottom);
3842  if (es->style & ES_MULTILINE) {
3843  rc = rcClient;
3844  IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3845  }
3846  if (es->font)
3847  old_font = SelectObject(dc, es->font);
3848 
3849  if (!es->bEnableState)
3851  GetClipBox(dc, &rcRgn);
3852  if (es->style & ES_MULTILINE) {
3854  for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3856  EDIT_GetLineRect(es, i, 0, -1, &rcLine);
3857  if (IntersectRect(&rc, &rcRgn, &rcLine))
3858  EDIT_PaintLine(es, dc, i, rev);
3859  }
3860  } else {
3862  EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
3863  if (IntersectRect(&rc, &rcRgn, &rcLine))
3864  EDIT_PaintLine(es, dc, 0, rev);
3865  }
3866  if (es->font)
3867  SelectObject(dc, old_font);
3868 
3869  if (!hdc)
3870  EndPaint(es->hwndSelf, &ps);
3871 }
3872 
3873 
3874 /*********************************************************************
3875  *
3876  * WM_SETFOCUS
3877  *
3878  */
3880 {
3881  es->flags |= EF_FOCUSED;
3882 
3883  if (!(es->style & ES_NOHIDESEL))
3884  EDIT_InvalidateText(es, es->selection_start, es->selection_end);
3885 
3886  /* single line edit updates itself */
3887  if (IsWindowVisible(es->hwndSelf) && !(es->style & ES_MULTILINE))
3888  {
3889  HDC hdc = GetDC(es->hwndSelf);
3890  EDIT_WM_Paint(es, hdc);
3891  ReleaseDC(es->hwndSelf, hdc);
3892  }
3893 
3894  CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3895  EDIT_SetCaretPos(es, es->selection_end,
3896  es->flags & EF_AFTER_WRAP);
3897  ShowCaret(es->hwndSelf);
3899 }
3900 
3901 
3902 /*********************************************************************
3903  *
3904  * WM_SETFONT
3905  *
3906  * With Win95 look the margins are set to default font value unless
3907  * the system font (font == 0) is being set, in which case they are left
3908  * unchanged.
3909  *
3910  */
3912 {
3913  TEXTMETRICW tm;
3914  HDC dc;
3915  HFONT old_font = 0;
3916  RECT clientRect;
3917 
3918  es->font = font;
3920  dc = GetDC(es->hwndSelf);
3921  if (font)
3922  old_font = SelectObject(dc, font);
3923  GetTextMetricsW(dc, &tm);
3924  es->line_height = tm.tmHeight;
3925  es->char_width = tm.tmAveCharWidth;
3926  if (font)
3927  SelectObject(dc, old_font);
3928  ReleaseDC(es->hwndSelf, dc);
3929 
3930  /* Reset the format rect and the margins */
3931  GetClientRect(es->hwndSelf, &clientRect);
3932  EDIT_SetRectNP(es, &clientRect);
3935 
3936  if (es->style & ES_MULTILINE)
3938  else
3940 
3941  if (redraw)
3943  if (es->flags & EF_FOCUSED) {
3944  DestroyCaret();
3945  CreateCaret(es->hwndSelf, 0, 1, es->line_height);
3946  EDIT_SetCaretPos(es, es->selection_end,
3947  es->flags & EF_AFTER_WRAP);
3948  ShowCaret(es->hwndSelf);
3949  }
3950 }
3951 
3952 
3953 /*********************************************************************
3954  *
3955  * WM_SETTEXT
3956  *
3957  * NOTES
3958  * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3959  * The modified flag is reset. No notifications are sent.
3960  *
3961  * For single-line controls, reception of WM_SETTEXT triggers:
3962  * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3963  *
3964  */
3965 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
3966 {
3967  LPWSTR textW = NULL;
3968  if (!unicode && text)
3969  {
3970  LPCSTR textA = (LPCSTR)text;
3971  INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
3972  textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
3973  if (textW)
3974  MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
3975  text = textW;
3976  }
3977 
3978  if (es->flags & EF_UPDATE)
3979  /* fixed this bug once; complain if we see it about to happen again. */
3980  ERR("SetSel may generate UPDATE message whose handler may reset "
3981  "selection.\n");
3982 
3983  EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
3984  if (text)
3985  {
3986  TRACE("%s\n", debugstr_w(text));
3988  if(!unicode)
3989  HeapFree(GetProcessHeap(), 0, textW);
3990  }
3991  else
3992  {
3993  TRACE("<NULL>\n");
3995  }
3996  es->x_offset = 0;
3997  es->flags &= ~EF_MODIFIED;
3998  EDIT_EM_SetSel(es, 0, 0, FALSE);
3999 
4000  /* Send the notification after the selection start and end have been set
4001  * edit control doesn't send notification on WM_SETTEXT
4002  * if it is multiline, or it is part of combobox
4003  */
4004  if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4005  {
4008  }
4012 }
4013 
4014 
4015 /*********************************************************************
4016  *
4017  * WM_SIZE
4018  *
4019  */
4021 {
4022  if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4023  RECT rc;
4024  GetClientRect(es->hwndSelf, &rc);
4025  EDIT_SetRectNP(es, &rc);
4027  }
4028 }
4029 
4030 
4031 /*********************************************************************
4032  *
4033  * WM_STYLECHANGED
4034  *
4035  * This message is sent by SetWindowLong on having changed either the Style
4036  * or the extended style.
4037  *
4038  * We ensure that the window's version of the styles and the EDITSTATE's agree.
4039  *
4040  * See also EDIT_WM_NCCreate
4041  *
4042  * It appears that the Windows version of the edit control allows the style
4043  * (as retrieved by GetWindowLong) to be any value and maintains an internal
4044  * style variable which will generally be different. In this function we
4045  * update the internal style based on what changed in the externally visible
4046  * style.
4047  *
4048  * Much of this content as based upon the MSDN, especially:
4049  * Platform SDK Documentation -> User Interface Services ->
4050  * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4051  * Edit Control Styles
4052  */
4054 {
4055  if (GWL_STYLE == which) {
4056  DWORD style_change_mask;
4057  DWORD new_style;
4058  /* Only a subset of changes can be applied after the control
4059  * has been created.
4060  */
4061  style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4062  ES_NUMBER;
4063  if (es->style & ES_MULTILINE)
4064  style_change_mask |= ES_WANTRETURN;
4065 
4066  new_style = style->styleNew & style_change_mask;
4067 
4068  /* Number overrides lowercase overrides uppercase (at least it
4069  * does in Win95). However I'll bet that ES_NUMBER would be
4070  * invalid under Win 3.1.
4071  */
4072  if (new_style & ES_NUMBER) {
4073  ; /* do not override the ES_NUMBER */
4074  } else if (new_style & ES_LOWERCASE) {
4075  new_style &= ~ES_UPPERCASE;
4076  }
4077 
4078  es->style = (es->style & ~style_change_mask) | new_style;
4079  } else if (GWL_EXSTYLE == which) {
4080  ; /* FIXME - what is needed here */
4081  } else {
4082  WARN ("Invalid style change %ld\n",which);
4083  }
4084 
4085  return 0;
4086 }
4087 
4088 /*********************************************************************
4089  *
4090  * WM_SYSKEYDOWN
4091  *
4092  */
4094 {
4095  if ((key == VK_BACK) && (key_data & 0x2000)) {
4096  if (EDIT_EM_CanUndo(es))
4097  EDIT_EM_Undo(es);
4098  return 0;
4099  } else if (key == VK_UP || key == VK_DOWN) {
4101  return 0;
4102  }
4103  return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, key, key_data);
4104 }
4105 
4106 
4107 /*********************************************************************
4108  *
4109  * WM_TIMER
4110  *
4111  */
4113 {
4114  if (es->region_posx < 0) {
4116  } else if (es->region_posx > 0) {
4118  }
4119 /*
4120  * FIXME: gotta do some vertical scrolling here, like
4121  * EDIT_EM_LineScroll(hwnd, 0, 1);
4122  */
4123 }
4124 
4125 /*********************************************************************
4126  *
4127  * WM_HSCROLL
4128  *
4129  */
4131 {
4132  INT dx;
4133  INT fw;
4134 
4135  if (!(es->style & ES_MULTILINE))
4136  return 0;
4137 
4138  if (!(es->style & ES_AUTOHSCROLL))
4139  return 0;
4140 
4141  dx = 0;
4142  fw = es->format_rect.right - es->format_rect.left;
4143  switch (action) {
4144  case SB_LINELEFT:
4145  TRACE("SB_LINELEFT\n");
4146  if (es->x_offset)
4147  dx = -es->char_width;
4148  break;
4149  case SB_LINERIGHT:
4150  TRACE("SB_LINERIGHT\n");
4151  if (es->x_offset < es->text_width)
4152  dx = es->char_width;
4153  break;
4154  case SB_PAGELEFT:
4155  TRACE("SB_PAGELEFT\n");
4156  if (es->x_offset)
4157  dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4158  break;
4159  case SB_PAGERIGHT:
4160  TRACE("SB_PAGERIGHT\n");
4161  if (es->x_offset < es->text_width)
4162  dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4163  break;
4164  case SB_LEFT:
4165  TRACE("SB_LEFT\n");
4166  if (es->x_offset)
4167  dx = -es->x_offset;
4168  break;
4169  case SB_RIGHT:
4170  TRACE("SB_RIGHT\n");
4171  if (es->x_offset < es->text_width)
4172  dx = es->text_width - es->x_offset;
4173  break;
4174  case SB_THUMBTRACK:
4175  TRACE("SB_THUMBTRACK %d\n", pos);
4176  es->flags |= EF_HSCROLL_TRACK;
4177  if(es->style & WS_HSCROLL)
4178  dx = pos - es->x_offset;
4179  else
4180  {
4181  INT fw, new_x;
4182  /* Sanity check */
4183  if(pos < 0 || pos > 100) return 0;
4184  /* Assume default scroll range 0-100 */
4185