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