Merging r37048, r37051, r37052, r37055 from the-real-msvc 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 BOOL 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 result = 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 result = 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 if (new_line == NULL)
1226 break;
1227 new_line->next = previous_line->next;
1228 previous_line->next = new_line;
1229 current_line = new_line;
1230 es->line_count++;
1231 }
1232 else if (current_line->index + delta < current_position - es->text)
1233 {
1234 /* The previous line merged with this line so we delete this extra entry */
1235 previous_line->next = current_line->next;
1236 HeapFree(GetProcessHeap(), 0, current_line);
1237 current_line = previous_line->next;
1238 es->line_count--;
1239 continue;
1240 }
1241 else /* current_line->index + delta == current_position */
1242 {
1243 if (current_position - es->text > iend)
1244 break; /* We reached end of line modifications */
1245 /* else recalulate this line */
1246 }
1247 }
1248
1249 current_line->index = current_position - es->text;
1250 orig_net_length = current_line->net_length;
1251
1252 /* Find end of line */
1253 cp = current_position;
1254 while (*cp) {
1255 if (*cp == '\n') break;
1256 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1257 break;
1258 cp++;
1259 }
1260
1261 /* Mark type of line termination */
1262 if (!(*cp)) {
1263 current_line->ending = END_0;
1264 current_line->net_length = strlenW(current_position);
1265 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1266 current_line->ending = END_SOFT;
1267 current_line->net_length = cp - current_position - 1;
1268 } else if (*cp == '\n') {
1269 current_line->ending = END_RICH;
1270 current_line->net_length = cp - current_position;
1271 } else {
1272 current_line->ending = END_HARD;
1273 current_line->net_length = cp - current_position;
1274 }
1275
1276 /* Calculate line width */
1277 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1278 current_position, current_line->net_length,
1279 es->tabs_count, es->tabs));
1280
1281 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1282 if (!(es->style & ES_AUTOHSCROLL)) {
1283 if (current_line->width > fw) {
1284 INT next = 0;
1285 INT prev;
1286 do {
1287 prev = next;
1288 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1289 prev + 1, current_line->net_length, WB_RIGHT);
1290 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1291 current_position, next, es->tabs_count, es->tabs));
1292 } while (current_line->width <= fw);
1293 if (!prev) { /* Didn't find a line break so force a break */
1294 next = 0;
1295 do {
1296 prev = next;
1297 next++;
1298 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1299 current_position, next, es->tabs_count, es->tabs));
1300 } while (current_line->width <= fw);
1301 if (!prev)
1302 prev = 1;
1303 }
1304
1305 /* If the first line we are calculating, wrapped before istart, we must
1306 * adjust istart in order for this to be reflected in the update region. */
1307 if (current_line->index == nstart_index && istart > current_line->index + prev)
1308 istart = current_line->index + prev;
1309 /* else if we are updating the previous line before the first line we
1310 * are re-calculating and it expanded */
1311 else if (current_line == start_line &&
1312 current_line->index != nstart_index && orig_net_length < prev)
1313 {
1314 /* Line expanded due to an upwards line wrap so we must partially include
1315 * previous line in update region */
1316 nstart_line = line_index;
1317 nstart_index = current_line->index;
1318 istart = current_line->index + orig_net_length;
1319 }
1320
1321 current_line->net_length = prev;
1322 current_line->ending = END_WRAP;
1323 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1324 current_line->net_length, es->tabs_count, es->tabs));
1325 }
1326 else if (orig_net_length < current_line->net_length &&
1327 current_line == start_line &&
1328 current_line->index != nstart_index) {
1329 /* The previous line expanded but it's still not as wide as the client rect */
1330 /* The expansion is due to an upwards line wrap so we must partially include
1331 it in the update region */
1332 nstart_line = line_index;
1333 nstart_index = current_line->index;
1334 istart = current_line->index + orig_net_length;
1335 }
1336 }
1337
1338
1339 /* Adjust length to include line termination */
1340 switch (current_line->ending) {
1341 case END_SOFT:
1342 current_line->length = current_line->net_length + 3;
1343 break;
1344 case END_RICH:
1345 current_line->length = current_line->net_length + 1;
1346 break;
1347 case END_HARD:
1348 current_line->length = current_line->net_length + 2;
1349 break;
1350 case END_WRAP:
1351 case END_0:
1352 current_line->length = current_line->net_length;
1353 break;
1354 }
1355 es->text_width = max(es->text_width, current_line->width);
1356 current_position += current_line->length;
1357 previous_line = current_line;
1358 current_line = current_line->next;
1359 line_index++;
1360 } while (previous_line->ending != END_0);
1361
1362 /* Finish adjusting line indexes by delta or remove hanging lines */
1363 if (previous_line->ending == END_0)
1364 {
1365 LINEDEF *pnext = NULL;
1366
1367 previous_line->next = NULL;
1368 while (current_line)
1369 {
1370 pnext = current_line->next;
1371 HeapFree(GetProcessHeap(), 0, current_line);
1372 current_line = pnext;
1373 es->line_count--;
1374 }
1375 }
1376 else if (delta != 0)
1377 {
1378 while (current_line)
1379 {
1380 current_line->index += delta;
1381 current_line = current_line->next;
1382 }
1383 }
1384
1385 /* Calculate rest of modification rectangle */
1386 if (hrgn)
1387 {
1388 HRGN tmphrgn;
1389 /*
1390 * We calculate two rectangles. One for the first line which may have
1391 * an indent with respect to the format rect. The other is a format-width
1392 * rectangle that spans the rest of the lines that changed or moved.
1393 */
1394 rc.top = es->format_rect.top + nstart_line * es->line_height -
1395 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1396 rc.bottom = rc.top + es->line_height;
1397 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT))
1398 rc.left = es->format_rect.left;
1399 else
1400 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1401 es->text + nstart_index, istart - nstart_index,
1402 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1403 rc.right = es->format_rect.right;
1404 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1405
1406 rc.top = rc.bottom;
1407 rc.left = es->format_rect.left;
1408 rc.right = es->format_rect.right;
1409 /*
1410 * If lines were added or removed we must re-paint the remainder of the
1411 * lines since the remaining lines were either shifted up or down.
1412 */
1413 if (line_count < es->line_count) /* We added lines */
1414 rc.bottom = es->line_count * es->line_height;
1415 else if (line_count > es->line_count) /* We removed lines */
1416 rc.bottom = line_count * es->line_height;
1417 else
1418 rc.bottom = line_index * es->line_height;
1419 rc.bottom += es->format_rect.top;
1420 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1421 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1422 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1423 DeleteObject(tmphrgn);
1424 }
1425
1426 if (es->font)
1427 SelectObject(dc, old_font);
1428
1429 ReleaseDC(es->hwndSelf, dc);
1430 }
1431
1432 /*********************************************************************
1433 *
1434 * EDIT_CalcLineWidth_SL
1435 *
1436 */
1437 static void EDIT_CalcLineWidth_SL(EDITSTATE *es)
1438 {
1439 SIZE size;
1440 LPWSTR text;
1441 HDC dc;
1442 HFONT old_font = 0;
1443
1444 text = EDIT_GetPasswordPointer_SL(es);
1445
1446 dc = GetDC(es->hwndSelf);
1447 if (es->font)
1448 old_font = SelectObject(dc, es->font);
1449
1450 GetTextExtentPoint32W(dc, text, strlenW(text), &size);
1451
1452 if (es->font)
1453 SelectObject(dc, old_font);
1454 ReleaseDC(es->hwndSelf, dc);
1455
1456 if (es->style & ES_PASSWORD)
1457 HeapFree(GetProcessHeap(), 0, text);
1458
1459 es->text_width = size.cx;
1460 }
1461
1462 /*********************************************************************
1463 *
1464 * EDIT_CallWordBreakProc
1465 *
1466 * Call appropriate WordBreakProc (internal or external).
1467 *
1468 * Note: The "start" argument should always be an index referring
1469 * to es->text. The actual wordbreak proc might be
1470 * 16 bit, so we can't always pass any 32 bit LPSTR.
1471 * Hence we assume that es->text is the buffer that holds
1472 * the string under examination (we can decide this for ourselves).
1473 *
1474 */
1475 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1476 {
1477 INT ret;
1478
1479 #ifndef __REACTOS__
1480 if (es->word_break_proc16) {
1481 HGLOBAL16 hglob16;
1482 SEGPTR segptr;
1483 INT countA;
1484 WORD args[5];
1485 DWORD result;
1486
1487 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1488 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1489 segptr = K32WOWGlobalLock16(hglob16);
1490 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1491 args[4] = SELECTOROF(segptr);
1492 args[3] = OFFSETOF(segptr);
1493 args[2] = index;
1494 args[1] = countA;
1495 args[0] = action;
1496 WOWCallback16Ex((DWORD)es->word_break_proc16, WCB16_PASCAL, sizeof(args), args, &result);
1497 ret = LOWORD(result);
1498 GlobalUnlock16(hglob16);
1499 GlobalFree16(hglob16);
1500 }
1501 else if (es->word_break_proc)
1502 #else
1503 if (es->word_break_proc)
1504 #endif
1505 {
1506 if(es->is_unicode)
1507 {
1508 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1509
1510 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1511 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1512 ret = wbpW(es->text + start, index, count, action);
1513 }
1514 else
1515 {
1516 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1517 INT countA;
1518 CHAR *textA;
1519
1520 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1521 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1522 if (textA == NULL)
1523 return 0;
1524 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1525 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1526 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1527 ret = wbpA(textA, index, countA, action);
1528 HeapFree(GetProcessHeap(), 0, textA);
1529 }
1530 }
1531 else
1532 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1533
1534 return ret;
1535 }
1536
1537
1538 /*********************************************************************
1539 *
1540 * EDIT_CharFromPos
1541 *
1542 * Beware: This is not the function called on EM_CHARFROMPOS
1543 * The position _can_ be outside the formatting / client
1544 * rectangle
1545 * The return value is only the character index
1546 *
1547 */
1548 static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1549 {
1550 INT index;
1551 HDC dc;
1552 HFONT old_font = 0;
1553 INT x_high = 0, x_low = 0;
1554
1555 if (es->style & ES_MULTILINE) {
1556 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1557 INT line_index = 0;
1558 LINEDEF *line_def = es->first_line_def;
1559 INT low, high;
1560 while ((line > 0) && line_def->next) {
1561 line_index += line_def->length;
1562 line_def = line_def->next;
1563 line--;
1564 }
1565 x += es->x_offset - es->format_rect.left;
1566 if (es->style & ES_RIGHT)
1567 x -= (es->format_rect.right - es->format_rect.left) - line_def->width;
1568 else if (es->style & ES_CENTER)
1569 x -= ((es->format_rect.right - es->format_rect.left) - line_def->width) / 2;
1570 if (x >= line_def->width) {
1571 if (after_wrap)
1572 *after_wrap = (line_def->ending == END_WRAP);
1573 return line_index + line_def->net_length;
1574 }
1575 if (x <= 0) {
1576 if (after_wrap)
1577 *after_wrap = FALSE;
1578 return line_index;
1579 }
1580 dc = GetDC(es->hwndSelf);
1581 if (es->font)
1582 old_font = SelectObject(dc, es->font);
1583 low = line_index;
1584 high = line_index + line_def->net_length + 1;
1585 while (low < high - 1)
1586 {
1587 INT mid = (low + high) / 2;
1588 INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
1589 if (x_now > x) {
1590 high = mid;
1591 x_high = x_now;
1592 } else {
1593 low = mid;
1594 x_low = x_now;
1595 }
1596 }
1597 if (abs(x_high - x) + 1 <= abs(x_low - x))
1598 index = high;
1599 else
1600 index = low;
1601
1602 if (after_wrap)
1603 *after_wrap = ((index == line_index + line_def->net_length) &&
1604 (line_def->ending == END_WRAP));
1605 } else {
1606 LPWSTR text;
1607 SIZE size;
1608 if (after_wrap)
1609 *after_wrap = FALSE;
1610 x -= es->format_rect.left;
1611 if (!x)
1612 return es->x_offset;
1613
1614 if (!es->x_offset)
1615 {
1616 INT indent = (es->format_rect.right - es->format_rect.left) - es->text_width;
1617 if (es->style & ES_RIGHT)
1618 x -= indent;
1619 else if (es->style & ES_CENTER)
1620 x -= indent / 2;
1621 }
1622
1623 text = EDIT_GetPasswordPointer_SL(es);
1624 dc = GetDC(es->hwndSelf);
1625 if (es->font)
1626 old_font = SelectObject(dc, es->font);
1627 if (x < 0)
1628 {
1629 INT low = 0;
1630 INT high = es->x_offset;
1631 while (low < high - 1)
1632 {
1633 INT mid = (low + high) / 2;
1634 GetTextExtentPoint32W( dc, text + mid,
1635 es->x_offset - mid, &size );
1636 if (size.cx > -x) {
1637 low = mid;
1638 x_low = size.cx;
1639 } else {
1640 high = mid;
1641 x_high = size.cx;
1642 }
1643 }
1644 if (abs(x_high + x) <= abs(x_low + x) + 1)
1645 index = high;
1646 else
1647 index = low;
1648 }
1649 else
1650 {
1651 INT low = es->x_offset;
1652 INT high = get_text_length(es) + 1;
1653 while (low < high - 1)
1654 {
1655 INT mid = (low + high) / 2;
1656 GetTextExtentPoint32W( dc, text + es->x_offset,
1657 mid - es->x_offset, &size );
1658 if (size.cx > x) {
1659 high = mid;
1660 x_high = size.cx;
1661 } else {
1662 low = mid;
1663 x_low = size.cx;
1664 }
1665 }
1666 if (abs(x_high - x) <= abs(x_low - x) + 1)
1667 index = high;
1668 else
1669 index = low;
1670 }
1671 if (es->style & ES_PASSWORD)
1672 HeapFree(GetProcessHeap(), 0, text);
1673 }
1674 if (es->font)
1675 SelectObject(dc, old_font);
1676 ReleaseDC(es->hwndSelf, dc);
1677 return index;
1678 }
1679
1680
1681 /*********************************************************************
1682 *
1683 * EDIT_ConfinePoint
1684 *
1685 * adjusts the point to be within the formatting rectangle
1686 * (so CharFromPos returns the nearest _visible_ character)
1687 *
1688 */
1689 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1690 {
1691 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1692 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1693 }
1694
1695
1696 /*********************************************************************
1697 *
1698 * EDIT_GetLineRect
1699 *
1700 * Calculates the bounding rectangle for a line from a starting
1701 * column to an ending column.
1702 *
1703 */
1704 static void EDIT_GetLineRect(EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1705 {
1706 INT line_index = EDIT_EM_LineIndex(es, line);
1707
1708 if (es->style & ES_MULTILINE)
1709 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1710 else
1711 rc->top = es->format_rect.top;
1712 rc->bottom = rc->top + es->line_height;
1713 rc->left = (scol == 0) ? es->format_rect.left : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + scol, TRUE));
1714 rc->right = (ecol == -1) ? es->format_rect.right : (short)LOWORD(EDIT_EM_PosFromChar(es, line_index + ecol, TRUE));
1715 }
1716
1717
1718 /*********************************************************************
1719 *
1720 * EDIT_GetPasswordPointer_SL
1721 *
1722 * note: caller should free the (optionally) allocated buffer
1723 *
1724 */
1725 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1726 {
1727 if (es->style & ES_PASSWORD) {
1728 INT len = get_text_length(es);
1729 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1730 if (text == NULL)
1731 return NULL;
1732 text[len] = '\0';
1733 while(len) text[--len] = es->password_char;
1734 return text;
1735 } else
1736 return es->text;
1737 }
1738
1739
1740 /*********************************************************************
1741 *
1742 * EDIT_LockBuffer
1743 *
1744 * This acts as a LOCAL_Lock(), but it locks only once. This way
1745 * you can call it whenever you like, without unlocking.
1746 *
1747 * Initially the edit control allocates a HLOCAL32 buffer
1748 * (32 bit linear memory handler). However, 16 bit application
1749 * might send an EM_GETHANDLE message and expect a HLOCAL16 (16 bit SEG:OFF
1750 * handler). From that moment on we have to keep using this 16 bit memory
1751 * handler, because it is supposed to be valid at all times after EM_GETHANDLE.
1752 * What we do is create a HLOCAL16 buffer, copy the text, and do pointer
1753 * conversion.
1754 *
1755 */
1756 static void EDIT_LockBuffer(EDITSTATE *es)
1757 {
1758 #ifndef __REACTOS__
1759 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
1760 #endif
1761 if (!es->text) {
1762 CHAR *textA = NULL;
1763 UINT countA = 0;
1764 #ifndef __REACTOS__
1765 BOOL _16bit = FALSE;
1766 #endif
1767
1768 if(es->hloc32W)
1769 {
1770 if(es->hloc32A)
1771 {
1772 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1773 textA = LocalLock(es->hloc32A);
1774 countA = strlen(textA) + 1;
1775 }
1776 #ifndef __REACTOS__
1777 else if(es->hloc16)
1778 {
1779 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1780 textA = LOCAL_Lock(hInstance, es->hloc16);
1781 countA = strlen(textA) + 1;
1782 _16bit = TRUE;
1783 }
1784 #endif
1785 }
1786 else {
1787 ERR("no buffer ... please report\n");
1788 return;
1789 }
1790
1791 if(textA)
1792 {
1793 HLOCAL hloc32W_new;
1794 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1795 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1796 if(countW_new > es->buffer_size + 1)
1797 {
1798 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1799 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1800 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1801 if(hloc32W_new)
1802 {
1803 es->hloc32W = hloc32W_new;
1804 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1805 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1806 }
1807 else
1808 WARN("FAILED! Will synchronize partially\n");
1809 }
1810 }
1811
1812 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1813 es->text = LocalLock(es->hloc32W);
1814
1815 if(textA)
1816 {
1817 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1818 #ifndef __REACTOS__
1819 if(_16bit)
1820 LOCAL_Unlock(hInstance, es->hloc16);
1821 else
1822 #endif
1823 LocalUnlock(es->hloc32A);
1824 }
1825 }
1826 if(es->flags & EF_APP_HAS_HANDLE) text_buffer_changed(es);
1827 es->lock_count++;
1828 }
1829
1830
1831 /*********************************************************************
1832 *
1833 * EDIT_SL_InvalidateText
1834 *
1835 * Called from EDIT_InvalidateText().
1836 * Does the job for single-line controls only.
1837 *
1838 */
1839 static void EDIT_SL_InvalidateText(EDITSTATE *es, INT start, INT end)
1840 {
1841 RECT line_rect;
1842 RECT rc;
1843
1844 EDIT_GetLineRect(es, 0, start, end, &line_rect);
1845 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1846 EDIT_UpdateText(es, &rc, TRUE);
1847 }
1848
1849
1850 /*********************************************************************
1851 *
1852 * EDIT_ML_InvalidateText
1853 *
1854 * Called from EDIT_InvalidateText().
1855 * Does the job for multi-line controls only.
1856 *
1857 */
1858 static void EDIT_ML_InvalidateText(EDITSTATE *es, INT start, INT end)
1859 {
1860 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1861 INT sl = EDIT_EM_LineFromChar(es, start);
1862 INT el = EDIT_EM_LineFromChar(es, end);
1863 INT sc;
1864 INT ec;
1865 RECT rc1;
1866 RECT rcWnd;
1867 RECT rcLine;
1868 RECT rcUpdate;
1869 INT l;
1870
1871 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1872 return;
1873
1874 sc = start - EDIT_EM_LineIndex(es, sl);
1875 ec = end - EDIT_EM_LineIndex(es, el);
1876 if (sl < es->y_offset) {
1877 sl = es->y_offset;
1878 sc = 0;
1879 }
1880 if (el > es->y_offset + vlc) {
1881 el = es->y_offset + vlc;
1882 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1883 }
1884 GetClientRect(es->hwndSelf, &rc1);
1885 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1886 if (sl == el) {
1887 EDIT_GetLineRect(es, sl, sc, ec, &rcLine);
1888 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1889 EDIT_UpdateText(es, &rcUpdate, TRUE);
1890 } else {
1891 EDIT_GetLineRect(es, sl, sc,
1892 EDIT_EM_LineLength(es,
1893 EDIT_EM_LineIndex(es, sl)),
1894 &rcLine);
1895 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1896 EDIT_UpdateText(es, &rcUpdate, TRUE);
1897 for (l = sl + 1 ; l < el ; l++) {
1898 EDIT_GetLineRect(es, l, 0,
1899 EDIT_EM_LineLength(es,
1900 EDIT_EM_LineIndex(es, l)),
1901 &rcLine);
1902 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1903 EDIT_UpdateText(es, &rcUpdate, TRUE);
1904 }
1905 EDIT_GetLineRect(es, el, 0, ec, &rcLine);
1906 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1907 EDIT_UpdateText(es, &rcUpdate, TRUE);
1908 }
1909 }
1910
1911
1912 /*********************************************************************
1913 *
1914 * EDIT_InvalidateText
1915 *
1916 * Invalidate the text from offset start up to, but not including,
1917 * offset end. Useful for (re)painting the selection.
1918 * Regions outside the linewidth are not invalidated.
1919 * end == -1 means end == TextLength.
1920 * start and end need not be ordered.
1921 *
1922 */
1923 static void EDIT_InvalidateText(EDITSTATE *es, INT start, INT end)
1924 {
1925 if (end == start)
1926 return;
1927
1928 if (end == -1)
1929 end = get_text_length(es);
1930
1931 if (end < start) {
1932 INT tmp = start;
1933 start = end;
1934 end = tmp;
1935 }
1936
1937 if (es->style & ES_MULTILINE)
1938 EDIT_ML_InvalidateText(es, start, end);
1939 else
1940 EDIT_SL_InvalidateText(es, start, end);
1941 }
1942
1943
1944 /*********************************************************************
1945 *
1946 * EDIT_MakeFit
1947 *
1948 * Try to fit size + 1 characters in the buffer.
1949 */
1950 static BOOL EDIT_MakeFit(EDITSTATE *es, UINT size)
1951 {
1952 HLOCAL hNew32W;
1953
1954 if (size <= es->buffer_size)
1955 return TRUE;
1956
1957 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1958
1959 /* Force edit to unlock it's buffer. es->text now NULL */
1960 EDIT_UnlockBuffer(es, TRUE);
1961
1962 if (es->hloc32W) {
1963 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1964 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1965 TRACE("Old 32 bit handle %p, new handle %p\n", es->hloc32W, hNew32W);
1966 es->hloc32W = hNew32W;
1967 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1968 }
1969 }
1970
1971 EDIT_LockBuffer(es);
1972
1973 if (es->buffer_size < size) {
1974 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1975 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE);
1976 return FALSE;
1977 } else {
1978 TRACE("We now have %d+1\n", es->buffer_size);
1979 return TRUE;
1980 }
1981 }
1982
1983
1984 /*********************************************************************
1985 *
1986 * EDIT_MakeUndoFit
1987 *
1988 * Try to fit size + 1 bytes in the undo buffer.
1989 *
1990 */
1991 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1992 {
1993 UINT alloc_size;
1994
1995 if (size <= es->undo_buffer_size)
1996 return TRUE;
1997
1998 TRACE("trying to ReAlloc to %d+1\n", size);
1999
2000 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
2001 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
2002 es->undo_buffer_size = alloc_size/sizeof(WCHAR) - 1;
2003 return TRUE;
2004 }
2005 else
2006 {
2007 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
2008 return FALSE;
2009 }
2010 }
2011
2012
2013 /*********************************************************************
2014 *
2015 * EDIT_MoveBackward
2016 *
2017 */
2018 static void EDIT_MoveBackward(EDITSTATE *es, BOOL extend)
2019 {
2020 INT e = es->selection_end;
2021
2022 if (e) {
2023 e--;
2024 if ((es->style & ES_MULTILINE) && e &&
2025 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
2026 e--;
2027 if (e && (es->text[e - 1] == '\r'))
2028 e--;
2029 }
2030 }
2031 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2032 EDIT_EM_ScrollCaret(es);
2033 }
2034
2035
2036 /*********************************************************************
2037 *
2038 * EDIT_MoveDown_ML
2039 *
2040 * Only for multi line controls
2041 * Move the caret one line down, on a column with the nearest
2042 * x coordinate on the screen (might be a different column).
2043 *
2044 */
2045 static void EDIT_MoveDown_ML(EDITSTATE *es, BOOL extend)
2046 {
2047 INT s = es->selection_start;
2048 INT e = es->selection_end;
2049 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2050 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2051 INT x = (short)LOWORD(pos);
2052 INT y = (short)HIWORD(pos);
2053
2054 e = EDIT_CharFromPos(es, x, y + es->line_height, &after_wrap);
2055 if (!extend)
2056 s = e;
2057 EDIT_EM_SetSel(es, s, e, after_wrap);
2058 EDIT_EM_ScrollCaret(es);
2059 }
2060
2061
2062 /*********************************************************************
2063 *
2064 * EDIT_MoveEnd
2065 *
2066 */
2067 static void EDIT_MoveEnd(EDITSTATE *es, BOOL extend)
2068 {
2069 BOOL after_wrap = FALSE;
2070 INT e;
2071
2072 /* Pass a high value in x to make sure of receiving the end of the line */
2073 if (es->style & ES_MULTILINE)
2074 e = EDIT_CharFromPos(es, 0x3fffffff,
2075 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
2076 else
2077 e = get_text_length(es);
2078 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2079 EDIT_EM_ScrollCaret(es);
2080 }
2081
2082
2083 /*********************************************************************
2084 *
2085 * EDIT_MoveEndOfText
2086 *
2087 * Handler for Ctrl+End. Move to end of text. Only for multiline.
2088 *
2089 */
2090 static void EDIT_MoveEndOfText(EDITSTATE *es, BOOL extend)
2091 {
2092 BOOL after_wrap = FALSE;
2093 INT e;
2094
2095 assert(es->style & ES_MULTILINE);
2096
2097 /* Just give it impossible high x&y to get the index of the last char */
2098 e = EDIT_CharFromPos(es, 0x3fffffff, 0x3fffffff, &after_wrap);
2099
2100 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, after_wrap);
2101 EDIT_EM_ScrollCaret(es);
2102 }
2103
2104
2105 /*********************************************************************
2106 *
2107 * EDIT_MoveForward
2108 *
2109 */
2110 static void EDIT_MoveForward(EDITSTATE *es, BOOL extend)
2111 {
2112 INT e = es->selection_end;
2113
2114 if (es->text[e]) {
2115 e++;
2116 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
2117 if (es->text[e] == '\n')
2118 e++;
2119 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
2120 e += 2;
2121 }
2122 }
2123 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2124 EDIT_EM_ScrollCaret(es);
2125 }
2126
2127
2128 /*********************************************************************
2129 *
2130 * EDIT_MoveStartOfText
2131 *
2132 * Handler for Ctr+Home. Move to start of text. Only for multiline.
2133 *
2134 */
2135 static void EDIT_MoveStartOfText(EDITSTATE *es, BOOL extend)
2136 {
2137 BOOL after_wrap = FALSE;
2138
2139 assert(es->style & ES_MULTILINE);
2140
2141 /* use CharFromPos instead of just plain zero, to get the wrap_flag */
2142 EDIT_CharFromPos(es, 0, 0, &after_wrap);
2143
2144
2145 EDIT_EM_SetSel(es, 0, extend ? es->selection_end : 0, FALSE);
2146 EDIT_EM_ScrollCaret(es);
2147 }
2148
2149
2150 /*********************************************************************
2151 *
2152 * EDIT_MoveHome
2153 *
2154 * Home key: move to beginning of line.
2155 *
2156 */
2157 static void EDIT_MoveHome(EDITSTATE *es, BOOL extend)
2158 {
2159 INT e;
2160
2161 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2162 if (es->style & ES_MULTILINE)
2163 e = EDIT_CharFromPos(es, -es->x_offset,
2164 HIWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2165 else
2166 e = 0;
2167 EDIT_EM_SetSel(es, extend ? es->selection_start : e, e, FALSE);
2168 EDIT_EM_ScrollCaret(es);
2169 }
2170
2171
2172 /*********************************************************************
2173 *
2174 * EDIT_MovePageDown_ML
2175 *
2176 * Only for multi line controls
2177 * Move the caret one page down, on a column with the nearest
2178 * x coordinate on the screen (might be a different column).
2179 *
2180 */
2181 static void EDIT_MovePageDown_ML(EDITSTATE *es, BOOL extend)
2182 {
2183 INT s = es->selection_start;
2184 INT e = es->selection_end;
2185 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2186 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2187 INT x = (short)LOWORD(pos);
2188 INT y = (short)HIWORD(pos);
2189
2190 e = EDIT_CharFromPos(es, x,
2191 y + (es->format_rect.bottom - es->format_rect.top),
2192 &after_wrap);
2193 if (!extend)
2194 s = e;
2195 EDIT_EM_SetSel(es, s, e, after_wrap);
2196 EDIT_EM_ScrollCaret(es);
2197 }
2198
2199
2200 /*********************************************************************
2201 *
2202 * EDIT_MovePageUp_ML
2203 *
2204 * Only for multi line controls
2205 * Move the caret one page up, on a column with the nearest
2206 * x coordinate on the screen (might be a different column).
2207 *
2208 */
2209 static void EDIT_MovePageUp_ML(EDITSTATE *es, BOOL extend)
2210 {
2211 INT s = es->selection_start;
2212 INT e = es->selection_end;
2213 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2214 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2215 INT x = (short)LOWORD(pos);
2216 INT y = (short)HIWORD(pos);
2217
2218 e = EDIT_CharFromPos(es, x,
2219 y - (es->format_rect.bottom - es->format_rect.top),
2220 &after_wrap);
2221 if (!extend)
2222 s = e;
2223 EDIT_EM_SetSel(es, s, e, after_wrap);
2224 EDIT_EM_ScrollCaret(es);
2225 }
2226
2227
2228 /*********************************************************************
2229 *
2230 * EDIT_MoveUp_ML
2231 *
2232 * Only for multi line controls
2233 * Move the caret one line up, on a column with the nearest
2234 * x coordinate on the screen (might be a different column).
2235 *
2236 */
2237 static void EDIT_MoveUp_ML(EDITSTATE *es, BOOL extend)
2238 {
2239 INT s = es->selection_start;
2240 INT e = es->selection_end;
2241 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2242 LRESULT pos = EDIT_EM_PosFromChar(es, e, after_wrap);
2243 INT x = (short)LOWORD(pos);
2244 INT y = (short)HIWORD(pos);
2245
2246 e = EDIT_CharFromPos(es, x, y - es->line_height, &after_wrap);
2247 if (!extend)
2248 s = e;
2249 EDIT_EM_SetSel(es, s, e, after_wrap);
2250 EDIT_EM_ScrollCaret(es);
2251 }
2252
2253
2254 /*********************************************************************
2255 *
2256 * EDIT_MoveWordBackward
2257 *
2258 */
2259 static void EDIT_MoveWordBackward(EDITSTATE *es, BOOL extend)
2260 {
2261 INT s = es->selection_start;
2262 INT e = es->selection_end;
2263 INT l;
2264 INT ll;
2265 INT li;
2266
2267 l = EDIT_EM_LineFromChar(es, e);
2268 ll = EDIT_EM_LineLength(es, e);
2269 li = EDIT_EM_LineIndex(es, l);
2270 if (e - li == 0) {
2271 if (l) {
2272 li = EDIT_EM_LineIndex(es, l - 1);
2273 e = li + EDIT_EM_LineLength(es, li);
2274 }
2275 } else {
2276 e = li + (INT)EDIT_CallWordBreakProc(es,
2277 li, e - li, ll, WB_LEFT);
2278 }
2279 if (!extend)
2280 s = e;
2281 EDIT_EM_SetSel(es, s, e, FALSE);
2282 EDIT_EM_ScrollCaret(es);
2283 }
2284
2285
2286 /*********************************************************************
2287 *
2288 * EDIT_MoveWordForward
2289 *
2290 */
2291 static void EDIT_MoveWordForward(EDITSTATE *es, BOOL extend)
2292 {
2293 INT s = es->selection_start;
2294 INT e = es->selection_end;
2295 INT l;
2296 INT ll;
2297 INT li;
2298
2299 l = EDIT_EM_LineFromChar(es, e);
2300 ll = EDIT_EM_LineLength(es, e);
2301 li = EDIT_EM_LineIndex(es, l);
2302 if (e - li == ll) {
2303 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2304 e = EDIT_EM_LineIndex(es, l + 1);
2305 } else {
2306 e = li + EDIT_CallWordBreakProc(es,
2307 li, e - li + 1, ll, WB_RIGHT);
2308 }
2309 if (!extend)
2310 s = e;
2311 EDIT_EM_SetSel(es, s, e, FALSE);
2312 EDIT_EM_ScrollCaret(es);
2313 }
2314
2315
2316 /*********************************************************************
2317 *
2318 * EDIT_PaintLine
2319 *
2320 */
2321 static void EDIT_PaintLine(EDITSTATE *es, HDC dc, INT line, BOOL rev)
2322 {
2323 INT s = es->selection_start;
2324 INT e = es->selection_end;
2325 INT li;
2326 INT ll;
2327 INT x;
2328 INT y;
2329 LRESULT pos;
2330
2331 if (es->style & ES_MULTILINE) {
2332 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2333 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2334 return;
2335 } else if (line)
2336 return;
2337
2338 TRACE("line=%d\n", line);
2339
2340 pos = EDIT_EM_PosFromChar(es, EDIT_EM_LineIndex(es, line), FALSE);
2341 x = (short)LOWORD(pos);
2342 y = (short)HIWORD(pos);
2343 li = EDIT_EM_LineIndex(es, line);
2344 ll = EDIT_EM_LineLength(es, li);
2345 s = min(es->selection_start, es->selection_end);
2346 e = max(es->selection_start, es->selection_end);
2347 s = min(li + ll, max(li, s));
2348 e = min(li + ll, max(li, e));
2349 if (rev && (s != e) &&
2350 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2351 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2352 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2353 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2354 } else
2355 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2356 }
2357
2358
2359 /*********************************************************************
2360 *
2361 * EDIT_PaintText
2362 *
2363 */
2364 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2365 {
2366 COLORREF BkColor;
2367 COLORREF TextColor;
2368 LOGFONTW underline_font;
2369 HFONT hUnderline = 0;
2370 HFONT old_font = 0;
2371 INT ret;
2372 INT li;
2373 INT BkMode;
2374 SIZE size;
2375
2376 if (!count)
2377 return 0;
2378 BkMode = GetBkMode(dc);
2379 BkColor = GetBkColor(dc);
2380 TextColor = GetTextColor(dc);
2381 if (rev) {
2382 if (es->composition_len == 0)
2383 {
2384 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2385 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2386 SetBkMode( dc, OPAQUE);
2387 }
2388 else
2389 {
2390 HFONT current = GetCurrentObject(dc,OBJ_FONT);
2391 GetObjectW(current,sizeof(LOGFONTW),&underline_font);
2392 underline_font.lfUnderline = TRUE;
2393 hUnderline = CreateFontIndirectW(&underline_font);
2394 old_font = SelectObject(dc,hUnderline);
2395 }
2396 }
2397 li = EDIT_EM_LineIndex(es, line);
2398 if (es->style & ES_MULTILINE) {
2399 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2400 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2401 } else {
2402 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2403 TextOutW(dc, x, y, text + li + col, count);
2404 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2405 ret = size.cx;
2406 if (es->style & ES_PASSWORD)
2407 HeapFree(GetProcessHeap(), 0, text);
2408 }
2409 if (rev) {
2410 if (es->composition_len == 0)
2411 {
2412 SetBkColor(dc, BkColor);
2413 SetTextColor(dc, TextColor);
2414 SetBkMode( dc, BkMode);
2415 }
2416 else
2417 {
2418 if (old_font)
2419 SelectObject(dc,old_font);
2420 if (hUnderline)
2421 DeleteObject(hUnderline);
2422 }
2423 }
2424 return ret;
2425 }
2426
2427
2428 /*********************************************************************
2429 *
2430 * EDIT_SetCaretPos
2431 *
2432 */
2433 static void EDIT_SetCaretPos(EDITSTATE *es, INT pos,
2434 BOOL after_wrap)
2435 {
2436 LRESULT res = EDIT_EM_PosFromChar(es, pos, after_wrap);
2437 TRACE("%d - %dx%d\n", pos, (short)LOWORD(res), (short)HIWORD(res));
2438 SetCaretPos((short)LOWORD(res), (short)HIWORD(res));
2439 }
2440
2441
2442 /*********************************************************************
2443 *
2444 * EDIT_AdjustFormatRect
2445 *
2446 * Adjusts the format rectangle for the current font and the
2447 * current client rectangle.
2448 *
2449 */
2450 static void EDIT_AdjustFormatRect(EDITSTATE *es)
2451 {
2452 RECT ClientRect;
2453
2454 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2455 if (es->style & ES_MULTILINE)
2456 {
2457 INT fw, vlc, max_x_offset, max_y_offset;
2458
2459 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2460 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2461
2462 /* correct es->x_offset */
2463 fw = es->format_rect.right - es->format_rect.left;
2464 max_x_offset = es->text_width - fw;
2465 if(max_x_offset < 0) max_x_offset = 0;
2466 if(es->x_offset > max_x_offset)
2467 es->x_offset = max_x_offset;
2468
2469 /* correct es->y_offset */
2470 max_y_offset = es->line_count - vlc;
2471 if(max_y_offset < 0) max_y_offset = 0;
2472 if(es->y_offset > max_y_offset)
2473 es->y_offset = max_y_offset;
2474
2475 /* force scroll info update */
2476 EDIT_UpdateScrollInfo(es);
2477 }
2478 else
2479 /* Windows doesn't care to fix text placement for SL controls */
2480 es->format_rect.bottom = es->format_rect.top + es->line_height;
2481
2482 /* Always stay within the client area */
2483 GetClientRect(es->hwndSelf, &ClientRect);
2484 es->format_rect.bottom = min(es->format_rect.bottom, ClientRect.bottom);
2485
2486 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2487 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
2488
2489 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
2490 }
2491
2492
2493 /*********************************************************************
2494 *
2495 * EDIT_SetRectNP
2496 *
2497 * note: this is not (exactly) the handler called on EM_SETRECTNP
2498 * it is also used to set the rect of a single line control
2499 *
2500 */
2501 static void EDIT_SetRectNP(EDITSTATE *es, LPRECT rc)
2502 {
2503 LONG_PTR ExStyle;
2504 INT bw, bh;
2505 ExStyle = GetWindowLongPtrW(es->hwndSelf, GWL_EXSTYLE);
2506
2507 CopyRect(&es->format_rect, rc);
2508
2509 if (ExStyle & WS_EX_CLIENTEDGE) {
2510 es->format_rect.left++;
2511 es->format_rect.right--;
2512
2513 if (es->format_rect.bottom - es->format_rect.top
2514 >= es->line_height + 2)
2515 {
2516 es->format_rect.top++;
2517 es->format_rect.bottom--;
2518 }
2519 }
2520 else if (es->style & WS_BORDER) {
2521 bw = GetSystemMetrics(SM_CXBORDER) + 1;
2522 bh = GetSystemMetrics(SM_CYBORDER) + 1;
2523 es->format_rect.left += bw;
2524 es->format_rect.right -= bw;
2525 if (es->format_rect.bottom - es->format_rect.top
2526 >= es->line_height + 2 * bh)
2527 {
2528 es->format_rect.top += bh;
2529 es->format_rect.bottom -= bh;
2530 }
2531 }
2532
2533 es->format_rect.left += es->left_margin;
2534 es->format_rect.right -= es->right_margin;
2535 EDIT_AdjustFormatRect(es);
2536 }
2537
2538
2539 /*********************************************************************
2540 *
2541 * EDIT_UnlockBuffer
2542 *
2543 */
2544 static void EDIT_UnlockBuffer(EDITSTATE *es, BOOL force)
2545 {
2546
2547 /* Edit window might be already destroyed */
2548 if(!IsWindow(es->hwndSelf))
2549 {
2550 WARN("edit hwnd %p already destroyed\n", es->hwndSelf);
2551 return;
2552 }
2553
2554 if (!es->lock_count) {
2555 ERR("lock_count == 0 ... please report\n");
2556 return;
2557 }
2558 if (!es->text) {
2559 ERR("es->text == 0 ... please report\n");
2560 return;
2561 }
2562
2563 if (force || (es->lock_count == 1)) {
2564 if (es->hloc32W) {
2565 CHAR *textA = NULL;
2566 UINT countA = 0;
2567 UINT countW = get_text_length(es) + 1;
2568 #ifndef __REACTOS__
2569 STACK16FRAME* stack16 = NULL;
2570 HANDLE16 oldDS = 0;
2571 #endif
2572
2573 if(es->hloc32A)
2574 {
2575 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2576 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2577 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2578 countA = LocalSize(es->hloc32A);
2579 if(countA_new > countA)
2580 {
2581 HLOCAL hloc32A_new;
2582 UINT alloc_size = ROUND_TO_GROW(countA_new);
2583 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2584 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2585 if(hloc32A_new)
2586 {
2587 es->hloc32A = hloc32A_new;
2588 countA = LocalSize(hloc32A_new);
2589 TRACE("Real new size %d bytes\n", countA);
2590 }
2591 else
2592 WARN("FAILED! Will synchronize partially\n");
2593 }
2594 textA = LocalLock(es->hloc32A);
2595 }
2596 #ifndef __REACTOS__
2597 else if(es->hloc16)
2598 {
2599 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2600 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2601 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2602 countA = LOCAL_Size(hInstance, es->hloc16);
2603 if(countA_new > countA)
2604 {
2605 HLOCAL16 hloc16_new;
2606 UINT alloc_size = ROUND_TO_GROW(countA_new);
2607 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2608 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2609 if(hloc16_new)
2610 {
2611 es->hloc16 = hloc16_new;
2612 countA = LOCAL_Size(hInstance, hloc16_new);
2613 TRACE("Real new size %d bytes\n", countA);
2614 }
2615 else
2616 WARN("FAILED! Will synchronize partially\n");
2617 }
2618 textA = LOCAL_Lock(hInstance, es->hloc16);
2619 _16bit = TRUE;
2620 }
2621 #endif
2622
2623 if(textA)
2624 {
2625 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2626 #ifndef __REACTOS__
2627 if(_16bit)
2628 LOCAL_Unlock(hInstance, es->hloc16);
2629 else
2630 #endif
2631 LocalUnlock(es->hloc32A);
2632 }
2633
2634 LocalUnlock(es->hloc32W);
2635 es->text = NULL;
2636 }
2637 else {
2638 ERR("no buffer ... please report\n");
2639 return;
2640 }
2641 }
2642 es->lock_count--;
2643 }
2644
2645
2646 /*********************************************************************
2647 *
2648 * EDIT_UpdateScrollInfo
2649 *
2650 */
2651 static void EDIT_UpdateScrollInfo(EDITSTATE *es)
2652 {
2653 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2654 {
2655 SCROLLINFO si;
2656 si.cbSize = sizeof(SCROLLINFO);
2657 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2658 si.nMin = 0;
2659 si.nMax = es->line_count - 1;
2660 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2661 si.nPos = es->y_offset;
2662 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2663 si.nMin, si.nMax, si.nPage, si.nPos);
2664 SetScrollInfo(es->hwndSelf, SB_VERT, &si, TRUE);
2665 }
2666
2667 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2668 {
2669 SCROLLINFO si;
2670 si.cbSize = sizeof(SCROLLINFO);
2671 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2672 si.nMin = 0;
2673 si.nMax = es->text_width - 1;
2674 si.nPage = es->format_rect.right - es->format_rect.left;
2675 si.nPos = es->x_offset;
2676 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2677 si.nMin, si.nMax, si.nPage, si.nPos);
2678 SetScrollInfo(es->hwndSelf, SB_HORZ, &si, TRUE);
2679 }
2680 }
2681
2682 /*********************************************************************
2683 *
2684 * EDIT_WordBreakProc
2685 *
2686 * Find the beginning of words.
2687 * Note: unlike the specs for a WordBreakProc, this function only
2688 * allows to be called without linebreaks between s[0] up to
2689 * s[count - 1]. Remember it is only called
2690 * internally, so we can decide this for ourselves.
2691 *
2692 */
2693 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2694 {
2695 INT ret = 0;
2696
2697 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2698
2699 if(!s) return 0;
2700
2701 switch (action) {
2702 case WB_LEFT:
2703 if (!count)
2704 break;
2705 if (index)
2706 index--;
2707 if (s[index] == ' ') {
2708 while (index && (s[index] == ' '))
2709 index--;
2710 if (index) {
2711 while (index && (s[index] != ' '))
2712 index--;
2713 if (s[index] == ' ')
2714 index++;
2715 }
2716 } else {
2717 while (index && (s[index] != ' '))
2718 index--;
2719 if (s[index] == ' ')
2720 index++;
2721 }
2722 ret = index;
2723 break;
2724 case WB_RIGHT:
2725 if (!count)
2726 break;
2727 if (index)
2728 index--;
2729 if (s[index] == ' ')
2730 while ((index < count) && (s[index] == ' ')) index++;
2731 else {
2732 while (s[index] && (s[index] != ' ') && (index < count))
2733 index++;
2734 while ((s[index] == ' ') && (index < count)) index++;
2735 }
2736 ret = index;
2737 break;
2738 case WB_ISDELIMITER:
2739 ret = (s[index] == ' ');
2740 break;
2741 default:
2742 ERR("unknown action code, please report !\n");
2743 break;
2744 }
2745 return ret;
2746 }
2747
2748
2749 /*********************************************************************
2750 *
2751 * EM_CHARFROMPOS
2752 *
2753 * returns line number (not index) in high-order word of result.
2754 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2755 * to Richedit, not to the edit control. Original documentation is valid.
2756 * FIXME: do the specs mean to return -1 if outside client area or
2757 * if outside formatting rectangle ???
2758 *
2759 */
2760 static LRESULT EDIT_EM_CharFromPos(EDITSTATE *es, INT x, INT y)
2761 {
2762 POINT pt;
2763 RECT rc;
2764 INT index;
2765
2766 pt.x = x;
2767 pt.y = y;
2768 GetClientRect(es->hwndSelf, &rc);
2769 if (!PtInRect(&rc, pt))
2770 return -1;
2771
2772 index = EDIT_CharFromPos(es, x, y, NULL);
2773 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2774 }
2775
2776
2777 /*********************************************************************
2778 *
2779 * EM_FMTLINES
2780 *
2781 * Enable or disable soft breaks.
2782 *
2783 * This means: insert or remove the soft linebreak character (\r\r\n).
2784 * Take care to check if the text still fits the buffer after insertion.
2785 * If not, notify with EN_ERRSPACE.
2786 *
2787 */
2788 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2789 {
2790 es->flags &= ~EF_USE_SOFTBRK;
2791 if (add_eol) {
2792 es->flags |= EF_USE_SOFTBRK;
2793 FIXME("soft break enabled, not implemented\n");
2794 }
2795 return add_eol;
2796 }
2797
2798
2799 /*********************************************************************
2800 *
2801 * EM_GETHANDLE
2802 *
2803 * Hopefully this won't fire back at us.
2804 * We always start with a fixed buffer in the local heap.
2805 * Despite of the documentation says that the local heap is used
2806 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2807 * buffer on the local heap.
2808 *
2809 */
2810 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2811 {
2812 HLOCAL hLocal;
2813
2814 if (!(es->style & ES_MULTILINE))
2815 return 0;
2816
2817 if(es->is_unicode)
2818 hLocal = es->hloc32W;
2819 else
2820 {
2821 if(!es->hloc32A)
2822 {
2823 CHAR *textA;
2824 UINT countA, alloc_size;
2825 TRACE("Allocating 32-bit ANSI alias buffer\n");
2826 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2827 alloc_size = ROUND_TO_GROW(countA);
2828 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2829 {
2830 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2831 return 0;
2832 }
2833 textA = LocalLock(es->hloc32A);
2834 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2835 LocalUnlock(es->hloc32A);
2836 }
2837 hLocal = es->hloc32A;
2838 }
2839
2840 TRACE("Returning %p, LocalSize() = %ld\n", hLocal, LocalSize(hLocal));
2841 return hLocal;
2842 }
2843
2844
2845 #ifndef __REACTOS__
2846 /*********************************************************************
2847 *
2848 * EM_GETHANDLE16
2849 *
2850 * Hopefully this won't fire back at us.
2851 * We always start with a buffer in 32 bit linear memory.
2852 * However, with this message a 16 bit application requests
2853 * a handle of 16 bit local heap memory, where it expects to find
2854 * the text.
2855 * It's a pitty that from this moment on we have to use this
2856 * local heap, because applications may rely on the handle
2857 * in the future.
2858 *
2859 * In this function we'll try to switch to local heap.
2860 */
2861 static HLOCAL16 EDIT_EM_GetHandle16(EDITSTATE *es)
2862 {
2863 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
2864 CHAR *textA;
2865 UINT countA, alloc_size;
2866
2867 if (!(es->style & ES_MULTILINE))
2868 return 0;
2869
2870 if (es->hloc16)
2871 return es->hloc16;
2872
2873 if (!LOCAL_HeapSize(hInstance)) {
2874 if (!LocalInit16(hInstance, 0,
2875 GlobalSize16(hInstance))) {
2876 ERR("could not initialize local heap\n");
2877 return 0;
2878 }
2879 TRACE("local heap initialized\n");
2880 }
2881
2882 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2883 alloc_size = ROUND_TO_GROW(countA);
2884
2885 TRACE("Allocating 16-bit ANSI alias buffer\n");
2886 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2887 ERR("could not allocate new 16 bit buffer\n");
2888 return 0;
2889 }
2890
2891 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2892 ERR("could not lock new 16 bit buffer\n");
2893 LOCAL_Free(hInstance, es->hloc16);
2894 es->hloc16 = 0;
2895 return 0;
2896 }
2897
2898 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2899 LOCAL_Unlock(hInstance, es->hloc16);
2900
2901 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2902 return es->hloc16;
2903 }
2904 #endif
2905
2906
2907 /*********************************************************************
2908 *
2909 * EM_GETLINE
2910 *
2911 */
2912 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPWSTR dst, BOOL unicode)
2913 {
2914 LPWSTR src;
2915 INT line_len, dst_len;
2916 INT i;
2917
2918 if (es->style & ES_MULTILINE) {
2919 if (line >= es->line_count)
2920 return 0;
2921 } else
2922 line = 0;
2923 i = EDIT_EM_LineIndex(es, line);
2924 src = es->text + i;
2925 line_len = EDIT_EM_LineLength(es, i);
2926 dst_len = *(WORD *)dst;
2927 if(unicode)
2928 {
2929 if(dst_len <= line_len)
2930 {
2931 memcpy(dst, src, dst_len * sizeof(WCHAR));
2932 return dst_len;
2933 }
2934 else /* Append 0 if enough space */
2935 {
2936 memcpy(dst, src, line_len * sizeof(WCHAR));
2937 dst[line_len] = 0;
2938 return line_len;
2939 }
2940 }
2941 else
2942 {
2943 INT ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, (LPSTR)dst, dst_len, NULL, NULL);
2944 if(!ret && line_len) /* Insufficient buffer size */
2945 return dst_len;
2946 if(ret < dst_len) /* Append 0 if enough space */
2947 ((LPSTR)dst)[ret] = 0;
2948 return ret;
2949 }
2950 }
2951
2952
2953 /*********************************************************************
2954 *
2955 * EM_GETSEL
2956 *
2957 */
2958 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, PUINT start, PUINT end)
2959 {
2960 UINT s = es->selection_start;
2961 UINT e = es->selection_end;
2962
2963 ORDER_UINT(s, e);
2964 if (start)
2965 *start = s;
2966 if (end)
2967 *end = e;
2968 return MAKELONG(s, e);
2969 }
2970
2971
2972 /*********************************************************************
2973 *
2974 * EM_GETTHUMB
2975 *
2976 * FIXME: is this right ? (or should it be only VSCROLL)
2977 * (and maybe only for edit controls that really have their
2978 * own scrollbars) (and maybe only for multiline controls ?)
2979 * All in all: very poorly documented
2980 *
2981 */
2982 static LRESULT EDIT_EM_GetThumb(EDITSTATE *es)
2983 {
2984 #ifdef __REACTOS__
2985 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB, 0),
2986 EDIT_WM_HScroll(es, EM_GETTHUMB, 0));
2987 #else
2988 return MAKELONG(EDIT_WM_VScroll(es, EM_GETTHUMB16, 0),
2989 EDIT_WM_HScroll(es, EM_GETTHUMB16, 0));
2990 #endif
2991 }
2992
2993
2994 /*********************************************************************
2995 *
2996 * EM_LINEFROMCHAR
2997 *
2998 */
2999 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
3000 {
3001 INT line;
3002 LINEDEF *line_def;
3003
3004 if (!(es->style & ES_MULTILINE))
3005 return 0;
3006 if (index > (INT)get_text_length(es))
3007 return es->line_count - 1;
3008 if (index == -1)
3009 index = min(es->selection_start, es->selection_end);
3010
3011 line = 0;
3012 line_def = es->first_line_def;
3013 index -= line_def->length;
3014 while ((index >= 0) && line_def->next) {
3015 line++;
3016 line_def = line_def->next;
3017 index -= line_def->length;
3018 }
3019 return line;
3020 }
3021
3022
3023 /*********************************************************************
3024 *
3025 * EM_LINEINDEX
3026 *
3027 */
3028 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
3029 {
3030 INT line_index;
3031 LINEDEF *line_def;
3032
3033 if (!(es->style & ES_MULTILINE))
3034 return 0;
3035 if (line >= es->line_count)
3036 return -1;
3037
3038 line_index = 0;
3039 line_def = es->first_line_def;
3040 if (line == -1) {
3041 INT index = es->selection_end - line_def->length;
3042 while ((index >= 0) && line_def->next) {
3043 line_index += line_def->length;
3044 line_def = line_def->next;
3045 index -= line_def->length;
3046 }
3047 } else {
3048 while (line > 0) {
3049 line_index += line_def->length;
3050 line_def = line_def->next;
3051 line--;
3052 }
3053 }
3054 return line_index;
3055 }
3056
3057
3058 /*********************************************************************
3059 *
3060 * EM_LINELENGTH
3061 *
3062 */
3063 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
3064 {
3065 LINEDEF *line_def;
3066
3067 if (!(es->style & ES_MULTILINE))
3068 return get_text_length(es);
3069
3070 if (index == -1) {
3071 /* get the number of remaining non-selected chars of selected lines */
3072 INT32 l; /* line number */
3073 INT32 li; /* index of first char in line */
3074 INT32 count;
3075 l = EDIT_EM_LineFromChar(es, es->selection_start);
3076 /* # chars before start of selection area */
3077 count = es->selection_start - EDIT_EM_LineIndex(es, l);
3078 l = EDIT_EM_LineFromChar(es, es->selection_end);
3079 /* # chars after end of selection */
3080 li = EDIT_EM_LineIndex(es, l);
3081 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
3082 return count;
3083 }
3084 line_def = es->first_line_def;
3085 index -= line_def->length;
3086 while ((index >= 0) && line_def->next) {
3087 line_def = line_def->next;
3088 index -= line_def->length;
3089 }
3090 return line_def->net_length;
3091 }
3092
3093
3094 /*********************************************************************
3095 *
3096 * EM_LINESCROLL
3097 *
3098 * NOTE: dx is in average character widths, dy - in lines;
3099 *
3100 */
3101 static BOOL EDIT_EM_LineScroll(EDITSTATE *es, INT dx, INT dy)
3102 {
3103 if (!(es->style & ES_MULTILINE))
3104 return FALSE;
3105
3106 dx *= es->char_width;
3107 return EDIT_EM_LineScroll_internal(es, dx, dy);
3108 }
3109
3110 /*********************************************************************
3111 *
3112 * EDIT_EM_LineScroll_internal
3113 *
3114 * Version of EDIT_EM_LineScroll for internal use.
3115 * It doesn't refuse if ES_MULTILINE is set and assumes that
3116 * dx is in pixels, dy - in lines.
3117 *
3118 */
3119 static BOOL EDIT_EM_LineScroll_internal(EDITSTATE *es, INT dx, INT dy)
3120 {
3121 INT nyoff;
3122 INT x_offset_in_pixels;
3123 INT lines_per_page = (es->format_rect.bottom - es->format_rect.top) /
3124 es->line_height;
3125
3126 if (es->style & ES_MULTILINE)
3127 {
3128 x_offset_in_pixels = es->x_offset;
3129 }
3130 else
3131 {
3132 dy = 0;
3133 x_offset_in_pixels = (short)LOWORD(EDIT_EM_PosFromChar(es, es->x_offset, FALSE));
3134 }
3135
3136 if (-dx > x_offset_in_pixels)
3137 dx = -x_offset_in_pixels;
3138 if (dx > es->text_width - x_offset_in_pixels)
3139 dx = es->text_width - x_offset_in_pixels;
3140 nyoff = max(0, es->y_offset + dy);
3141 if (nyoff >= es->line_count - lines_per_page)
3142 nyoff = max(0, es->line_count - lines_per_page);
3143 dy = (es->y_offset - nyoff) * es->line_height;
3144 if (dx || dy) {
3145 RECT rc1;
3146 RECT rc;
3147
3148 es->y_offset = nyoff;
3149 if(es->style & ES_MULTILINE)
3150 es->x_offset += dx;
3151 else
3152 es->x_offset += dx / es->char_width;
3153
3154 GetClientRect(es->hwndSelf, &rc1);
3155 IntersectRect(&rc, &rc1, &es->format_rect);
3156 ScrollWindowEx(es->hwndSelf, -dx, dy,
3157 NULL, &rc, NULL, NULL, SW_INVALIDATE);
3158 /* force scroll info update */
3159 EDIT_UpdateScrollInfo(es);
3160 }
3161 if (dx && !(es->flags & EF_HSCROLL_TRACK))
3162 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
3163 if (dy && !(es->flags & EF_VSCROLL_TRACK))
3164 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
3165 return TRUE;
3166 }
3167
3168
3169 /*********************************************************************
3170 *
3171 * EM_POSFROMCHAR
3172 *
3173 */
3174 static LRESULT EDIT_EM_PosFromChar(EDITSTATE *es, INT index, BOOL after_wrap)
3175 {
3176 INT len = get_text_length(es);
3177 INT l;
3178 INT li;
3179 INT x;
3180 INT y = 0;
3181 INT w;
3182 INT lw = 0;
3183 INT ll = 0;
3184 HDC dc;
3185 HFONT old_font = 0;
3186 SIZE size;
3187 LINEDEF *line_def;
3188
3189 index = min(index, len);
3190 dc = GetDC(es->hwndSelf);
3191 if (es->font)
3192 old_font = SelectObject(dc, es->font);
3193 if (es->style & ES_MULTILINE) {
3194 l = EDIT_EM_LineFromChar(es, index);
3195 y = (l - es->y_offset) * es->line_height;
3196 li = EDIT_EM_LineIndex(es, l);
3197 if (after_wrap && (li == index) && l) {
3198 INT l2 = l - 1;
3199 line_def = es->first_line_def;
3200 while (l2) {
3201 line_def = line_def->next;
3202 l2--;
3203 }
3204 if (line_def->ending == END_WRAP) {
3205 l--;
3206 y -= es->line_height;
3207 li = EDIT_EM_LineIndex(es, l);
3208 }
3209 }
3210
3211 line_def = es->first_line_def;
3212 while (line_def->index != li)
3213 line_def = line_def->next;
3214
3215 ll = line_def->net_length;
3216 lw = line_def->width;
3217
3218 w = es->format_rect.right - es->format_rect.left;
3219 if (es->style & ES_RIGHT)
3220 {
3221 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li + (index - li), ll - (index - li),
3222 es->tabs_count, es->tabs)) - es->x_offset;
3223 x = w - x;
3224 }
3225 else if (es->style & ES_CENTER)
3226 {
3227 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3228 es->tabs_count, es->tabs)) - es->x_offset;
3229 x += (w - lw) / 2;
3230 }
3231 else /* ES_LEFT */
3232 {
3233 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
3234 es->tabs_count, es->tabs)) - es->x_offset;
3235 }
3236 } else {
3237 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
3238 if (index < es->x_offset) {
3239 GetTextExtentPoint32W(dc, text + index,
3240 es->x_offset - index, &size);
3241 x = -size.cx;
3242 } else {
3243 GetTextExtentPoint32W(dc, text + es->x_offset,
3244 index - es->x_offset, &size);
3245 x = size.cx;
3246
3247 if (!es->x_offset && (es->style & (ES_RIGHT | ES_CENTER)))
3248 {
3249 w = es->format_rect.right - es->format_rect.left;
3250 if (w > es->text_width)
3251 {
3252 if (es->style & ES_RIGHT)
3253 x += w - es->text_width;
3254 else if (es->style & ES_CENTER)
3255 x += (w - es->text_width) / 2;
3256 }
3257 }
3258 }
3259 y = 0;
3260 if (es->style & ES_PASSWORD)
3261 HeapFree(GetProcessHeap(), 0, text);
3262 }
3263 x += es->format_rect.left;
3264 y += es->format_rect.top;
3265 if (es->font)
3266 SelectObject(dc, old_font);
3267 ReleaseDC(es->hwndSelf, dc);
3268 return MAKELONG((INT16)x, (INT16)y);
3269 }
3270
3271
3272 /*********************************************************************
3273 *
3274 * EM_REPLACESEL
3275 *
3276 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3277 *
3278 */
3279 static void EDIT_EM_ReplaceSel(EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update, BOOL honor_limit)
3280 {
3281 UINT strl = strlenW(lpsz_replace);
3282 UINT tl = get_text_length(es);
3283 UINT utl;
3284 UINT s;
3285 UINT e;
3286 UINT i;
3287 UINT size;
3288 LPWSTR p;
3289 HRGN hrgn = 0;
3290 LPWSTR buf = NULL;
3291 UINT bufl = 0;
3292
3293 TRACE("%s, can_undo %d, send_update %d\n",
3294 debugstr_w(lpsz_replace), can_undo, send_update);
3295
3296 s = es->selection_start;
3297 e = es->selection_end;
3298
3299 if ((s == e) && !strl)
3300 return;
3301
3302 ORDER_UINT(s, e);
3303
3304 size = tl - (e - s) + strl;
3305 if (!size)
3306 es->text_width = 0;
3307
3308 /* Issue the EN_MAXTEXT notification and continue with replacing text
3309 * such that buffer limit is honored. */
3310 if ((honor_limit) && (size > es->buffer_limit)) {
3311 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3312 /* Buffer limit can be smaller than the actual length of text in combobox */
3313 if (es->buffer_limit < (tl - (e-s)))
3314 strl = 0;
3315 else
3316 strl = es->buffer_limit - (tl - (e-s));
3317 }
3318
3319 if (!EDIT_MakeFit(es, tl - (e - s) + strl))
3320 return;
3321
3322 if (e != s) {
3323 /* there is something to be deleted */
3324 TRACE("deleting stuff.\n");
3325 bufl = e - s;
3326 buf = HeapAlloc(GetProcessHeap(), 0, (bufl + 1) * sizeof(WCHAR));
3327 if (!buf) return;
3328 memcpy(buf, es->text + s, bufl * sizeof(WCHAR));
3329 buf[bufl] = 0; /* ensure 0 termination */
3330 /* now delete */
3331 strcpyW(es->text + s, es->text + e);
3332 text_buffer_changed(es);
3333 }
3334 if (strl) {
3335 /* there is an insertion */
3336 tl = get_text_length(es);
3337 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));
3338 for (p = es->text + tl ; p >= es->text + s ; p--)
3339 p[strl] = p[0];
3340 for (i = 0 , p = es->text + s ; i < strl ; i++)
3341 p[i] = lpsz_replace[i];
3342 if(es->style & ES_UPPERCASE)
3343 CharUpperBuffW(p, strl);
3344 else if(es->style & ES_LOWERCASE)
3345 CharLowerBuffW(p, strl);
3346 text_buffer_changed(es);
3347 }
3348 if (es->style & ES_MULTILINE)
3349 {
3350 INT st = min(es->selection_start, es->selection_end);
3351 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3352
3353 hrgn = CreateRectRgn(0, 0, 0, 0);
3354 EDIT_BuildLineDefs_ML(es, st, st + strl,
3355 strl - abs(es->selection_end - es->selection_start), hrgn);
3356 /* if text is too long undo all changes */
3357 if (honor_limit && !(es->style & ES_AUTOVSCROLL) && (es->line_count > vlc)) {
3358 if (strl)
3359 strcpyW(es->text + e, es->text + e + strl);
3360 if (e != s)
3361 for (i = 0 , p = es->text ; i < e - s ; i++)
3362 p[i + s] = buf[i];
3363 text_buffer_changed(es);
3364 EDIT_BuildLineDefs_ML(es, s, e,
3365 abs(es->selection_end - es->selection_start) - strl, hrgn);
3366 strl = 0;
3367 e = s;
3368 hrgn = CreateRectRgn(0, 0, 0, 0);
3369 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3370 }
3371 }
3372 else {
3373 INT fw = es->format_rect.right - es->format_rect.left;
3374 EDIT_CalcLineWidth_SL(es);
3375 /* remove chars that don't fit */
3376 if (honor_limit && !(es->style & ES_AUTOHSCROLL) && (es->text_width > fw)) {
3377 while ((es->text_width > fw) && s + strl >= s) {
3378 strcpyW(es->text + s + strl - 1, es->text + s + strl);
3379 strl--;
3380 EDIT_CalcLineWidth_SL(es);
3381 }
3382 text_buffer_changed(es);
3383 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT);
3384 }
3385 }
3386
3387 if (e != s) {
3388 if (can_undo) {
3389 utl = strlenW(es->undo_text);
3390 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3391 /* undo-buffer is extended to the right */
3392 EDIT_MakeUndoFit(es, utl + e - s);
3393 memcpy(es->undo_text + utl, buf, (e - s)*sizeof(WCHAR));
3394 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3395 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3396 /* undo-buffer is extended to the left */
3397 EDIT_MakeUndoFit(es, utl + e - s);
3398 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3399 p[e - s] = p[0];
3400 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3401 p[i] = buf[i];
3402 es->undo_position = s;
3403 } else {
3404 /* new undo-buffer */
3405 EDIT_MakeUndoFit(es, e - s);
3406 memcpy(es->undo_text, buf, (e - s)*sizeof(WCHAR));
3407 es->undo_text[e - s] = 0; /* ensure 0 termination */
3408 es->undo_position = s;
3409 }
3410 /* any deletion makes the old insertion-undo invalid */
3411 es->undo_insert_count = 0;
3412 } else
3413 EDIT_EM_EmptyUndoBuffer(es);
3414 }
3415 if (strl) {
3416 if (can_undo) {
3417 if ((s == es->undo_position) ||
3418 ((es->undo_insert_count) &&
3419 (s == es->undo_position + es->undo_insert_count)))
3420 /*
3421 * insertion is new and at delete position or
3422 * an extension to either left or right
3423 */
3424 es->undo_insert_count += strl;
3425 else {
3426 /* new insertion undo */
3427 es->undo_position = s;
3428 es->undo_insert_count = strl;
3429 /* new insertion makes old delete-buffer invalid */
3430 *es->undo_text = '\0';
3431 }
3432 } else
3433 EDIT_EM_EmptyUndoBuffer(es);
3434 }
3435
3436 if (bufl)
3437 HeapFree(GetProcessHeap(), 0, buf);
3438
3439 s += strl;
3440
3441 /* If text has been deleted and we're right or center aligned then scroll rightward */
3442 if (es->style & (ES_RIGHT | ES_CENTER))
3443 {
3444 INT delta = strl - abs(es->selection_end - es->selection_start);
3445
3446 if (delta < 0 && es->x_offset)
3447 {
3448 if (abs(delta) > es->x_offset)
3449 es->x_offset = 0;
3450 else
3451 es->x_offset += delta;
3452 }
3453 }
3454
3455 EDIT_EM_SetSel(es, s, s, FALSE);
3456 es->flags |= EF_MODIFIED;
3457 if (send_update) es->flags |= EF_UPDATE;
3458 if (hrgn)
3459 {
3460 EDIT_UpdateTextRegion(es, hrgn, TRUE);
3461 DeleteObject(hrgn);
3462 }
3463 else
3464 EDIT_UpdateText(es, NULL, TRUE);
3465
3466 EDIT_EM_ScrollCaret(es);
3467
3468 /* force scroll info update */
3469 EDIT_UpdateScrollInfo(es);
3470
3471
3472 if(send_update || (es->flags & EF_UPDATE))
3473 {
3474 es->flags &= ~EF_UPDATE;
3475 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
3476 }
3477 }
3478
3479
3480 /*********************************************************************
3481 *
3482 * EM_SCROLL
3483 *
3484 */
3485 static LRESULT EDIT_EM_Scroll(EDITSTATE *es, INT action)
3486 {
3487 INT dy;
3488
3489 if (!(es->style & ES_MULTILINE))
3490 return (LRESULT)FALSE;
3491
3492 dy = 0;
3493
3494 switch (action) {
3495 case SB_LINEUP:
3496 if (es->y_offset)
3497 dy = -1;
3498 break;
3499 case SB_LINEDOWN:
3500 if (es->y_offset < es->line_count - 1)
3501 dy = 1;
3502 break;
3503 case SB_PAGEUP:
3504 if (es->y_offset)
3505 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3506 break;
3507 case SB_PAGEDOWN:
3508 if (es->y_offset < es->line_count - 1)
3509 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3510 break;
3511 default:
3512 return (LRESULT)FALSE;
3513 }
3514 if (dy) {
3515 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3516 /* check if we are going to move too far */
3517 if(es->y_offset + dy > es->line_count - vlc)
3518 dy = es->line_count - vlc - es->y_offset;
3519
3520 /* Notification is done in EDIT_EM_LineScroll */
3521 if(dy)
3522 EDIT_EM_LineScroll(es, 0, dy);
3523 }
3524 #ifdef __REACTOS__
3525 return MAKELONG((SHORT)dy, (BOOL)TRUE);
3526 #else
3527 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3528 #endif
3529 }
3530
3531
3532 /*********************************************************************
3533 *
3534 * EM_SCROLLCARET
3535 *
3536 */
3537 static void EDIT_EM_ScrollCaret(EDITSTATE *es)
3538 {
3539 if (es->style & ES_MULTILINE) {
3540 INT l;
3541 INT li;
3542 INT vlc;
3543 INT ww;
3544 INT cw = es->char_width;
3545 INT x;
3546 INT dy = 0;
3547 INT dx = 0;
3548
3549 l = EDIT_EM_LineFromChar(es, es->selection_end);
3550 li = EDIT_EM_LineIndex(es, l);
3551 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, es->flags & EF_AFTER_WRAP));
3552 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3553 if (l >= es->y_offset + vlc)
3554 dy = l - vlc + 1 - es->y_offset;
3555 if (l < es->y_offset)
3556 dy = l - es->y_offset;
3557 ww = es->format_rect.right - es->format_rect.left;
3558 if (x < es->format_rect.left)
3559 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3560 if (x > es->format_rect.right)
3561 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3562 if (dy || dx || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3563 {
3564 /* check if we are going to move too far */
3565 if(es->x_offset + dx + ww > es->text_width)
3566 dx = es->text_width - ww - es->x_offset;
3567 if(dx || dy || (es->y_offset && (es->line_count - es->y_offset < vlc)))
3568 EDIT_EM_LineScroll_internal(es, dx, dy);
3569 }
3570 } else {
3571 INT x;
3572 INT goal;
3573 INT format_width;
3574
3575 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3576 format_width = es->format_rect.right - es->format_rect.left;
3577 if (x < es->format_rect.left) {
3578 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3579 do {
3580 es->x_offset--;
3581 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3582 } while ((x < goal) && es->x_offset);
3583 /* FIXME: use ScrollWindow() somehow to improve performance */
3584 EDIT_UpdateText(es, NULL, TRUE);
3585 } else if (x > es->format_rect.right) {
3586 INT x_last;
3587 INT len = get_text_length(es);
3588 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3589 do {
3590 es->x_offset++;
3591 x = (short)LOWORD(EDIT_EM_PosFromChar(es, es->selection_end, FALSE));
3592 x_last = (short)LOWORD(EDIT_EM_PosFromChar(es, len, FALSE));
3593 } while ((x > goal) && (x_last > es->format_rect.right));
3594 /* FIXME: use ScrollWindow() somehow to improve performance */
3595 EDIT_UpdateText(es, NULL, TRUE);
3596 }
3597 }
3598
3599 if(es->flags & EF_FOCUSED)
3600 EDIT_SetCaretPos(es, es->selection_end, es->flags & EF_AFTER_WRAP);
3601 }
3602
3603
3604 /*********************************************************************
3605 *
3606 * EM_SETHANDLE
3607 *
3608 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3609 *
3610 */
3611 static void EDIT_EM_SetHandle(EDITSTATE *es, HLOCAL hloc)
3612 {
3613 if (!(es->style & ES_MULTILINE))
3614 return;
3615
3616 if (!hloc) {
3617 WARN("called with NULL handle\n");
3618 return;
3619 }
3620
3621 EDIT_UnlockBuffer(es, TRUE);
3622
3623 #ifndef __REACTOS__
3624 if(es->hloc16)
3625 {
3626 LOCAL_Free(hInstance, es->hloc16);
3627 es->hloc16 = (HLOCAL16)NULL;
3628 }
3629 #endif
3630
3631 if(es->is_unicode)
3632 {
3633 if(es->hloc32A)
3634 {
3635 LocalFree(es->hloc32A);
3636 es->hloc32A = NULL;
3637 }
3638 es->hloc32W = hloc;
3639 }
3640 else
3641 {
3642 INT countW, countA;
3643 HLOCAL hloc32W_new;
3644 WCHAR *textW;
3645 CHAR *textA;
3646
3647 countA = LocalSize(hloc);
3648 textA = LocalLock(hloc);
3649 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3650 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3651 {
3652 ERR("Could not allocate new unicode buffer\n");
3653 return;
3654 }
3655 textW = LocalLock(hloc32W_new);
3656 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3657 LocalUnlock(hloc32W_new);
3658 LocalUnlock(hloc);
3659
3660 if(es->hloc32W)
3661 LocalFree(es->hloc32W);
3662
3663 es->hloc32W = hloc32W_new;
3664 es->hloc32A = hloc;
3665 }
3666
3667 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3668
3669 es->flags |= EF_APP_HAS_HANDLE;
3670 EDIT_LockBuffer(es);
3671
3672 es->x_offset = es->y_offset = 0;
3673 es->selection_start = es->selection_end = 0;
3674 EDIT_EM_EmptyUndoBuffer(es);
3675 es->flags &= ~EF_MODIFIED;
3676 es->flags &= ~EF_UPDATE;
3677 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3678 EDIT_UpdateText(es, NULL, TRUE);
3679 EDIT_EM_ScrollCaret(es);
3680 /* force scroll info update */
3681 EDIT_UpdateScrollInfo(es);
3682 }
3683
3684
3685 #ifndef __REACTOS__
3686 /*********************************************************************
3687 *
3688 * EM_SETHANDLE16
3689 *
3690 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3691 *
3692 */
3693 static void EDIT_EM_SetHandle16(EDITSTATE *es, HLOCAL16 hloc)
3694 {
3695 HINSTANCE16 hInstance = GetWindowLongW( es->hwndSelf, GWL_HINSTANCE );
3696 INT countW, countA;
3697 HLOCAL hloc32W_new;
3698 WCHAR *textW;
3699 CHAR *textA;
3700
3701 if (!(es->style & ES_MULTILINE))
3702 return;
3703
3704 if (!hloc) {
3705 WARN("called with NULL handle\n");
3706 return;
3707 }
3708
3709 EDIT_UnlockBuffer(es, TRUE);
3710
3711 if(es->hloc32A)
3712 {
3713 LocalFree(es->hloc32A);
3714 es->hloc32A = NULL;
3715 }
3716
3717 countA = LOCAL_Size(hInstance, hloc);
3718 textA = LOCAL_Lock(hInstance, hloc);
3719 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3720 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3721 {
3722 ERR("Could not allocate new unicode buffer\n");
3723 return;
3724 }
3725 textW = LocalLock(hloc32W_new);
3726 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3727 LocalUnlock(hloc32W_new);
3728 LOCAL_Unlock(hInstance, hloc);
3729
3730 if(es->hloc32W)
3731 LocalFree(es->hloc32W);
3732
3733 es->hloc32W = hloc32W_new;
3734 es->hloc16 = hloc;
3735
3736 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3737
3738 EDIT_LockBuffer(es);
3739
3740 es->x_offset = es->y_offset = 0;
3741 es->selection_start = es->selection_end = 0;
3742 EDIT_EM_EmptyUndoBuffer(es);
3743 es->flags &= ~EF_MODIFIED;
3744 es->flags &= ~EF_UPDATE;
3745 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
3746 EDIT_UpdateText(es, NULL, TRUE);
3747 EDIT_EM_ScrollCaret(es);
3748 /* force scroll info update */
3749 EDIT_UpdateScrollInfo(es);
3750 }
3751 #endif
3752
3753
3754 /*********************************************************************
3755 *
3756 * EM_SETLIMITTEXT
3757 *
3758 * NOTE: this version currently implements WinNT limits
3759 *
3760 */
3761 static void EDIT_EM_SetLimitText(EDITSTATE *es, UINT limit)
3762 {
3763 if (!limit) limit = ~0u;
3764 if (!(es->style & ES_MULTILINE)) limit = min(limit, 0x7ffffffe);
3765 es->buffer_limit = limit;
3766 }
3767
3768
3769 /*********************************************************************
3770 *
3771 * EM_SETMARGINS
3772 *
3773 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3774 * action wParam despite what the docs say. EC_USEFONTINFO calculates the
3775 * margin according to the textmetrics of the current font.
3776 *
3777 * FIXME - With TrueType or vector fonts EC_USEFONTINFO currently sets one third
3778 * of the char's width as the margin, but this is not how Windows handles this.
3779 * For all other fonts Windows sets the margins to zero.
3780 *
3781 * FIXME - When EC_USEFONTINFO is used the margins only change if the
3782 * edit control is equal to or larger than a certain size.
3783 * Interestingly if one subtracts both the left and right margins from
3784 * this size one always seems to get an even number. The extents of
3785 * the (four character) string "'**'" match this quite closely, so
3786 * we'll use this until we come up with a better idea.
3787 */
3788 static int calc_min_set_margin_size(HDC dc, INT left, INT right)
3789 {
3790 WCHAR magic_string[] = {'\'','*','*','\'', 0};
3791 SIZE sz;
3792
3793 GetTextExtentPointW(dc, magic_string, sizeof(magic_string)/sizeof(WCHAR) - 1, &sz);
3794 return sz.cx + left + right;
3795 }
3796
3797 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3798 WORD left, WORD right, BOOL repaint)
3799 {
3800 TEXTMETRICW tm;
3801 INT default_left_margin = 0; /* in pixels */
3802 INT default_right_margin = 0; /* in pixels */
3803
3804 /* Set the default margins depending on the font */
3805 if (es->font && (left == EC_USEFONTINFO || right == EC_USEFONTINFO)) {
3806 HDC dc = GetDC(es->hwndSelf);
3807 HFONT old_font = SelectObject(dc, es->font);
3808 GetTextMetricsW(dc, &tm);
3809 /* The default margins are only non zero for TrueType or Vector fonts */
3810 if (tm.tmPitchAndFamily & ( TMPF_VECTOR | TMPF_TRUETYPE )) {
3811 int min_size;
3812 RECT rc;
3813 /* This must be calculated more exactly! But how? */
3814 default_left_margin = tm.tmAveCharWidth / 2;
3815 default_right_margin = tm.tmAveCharWidth / 2;
3816 min_size = calc_min_set_margin_size(dc, default_left_margin, default_right_margin);
3817 GetClientRect(es->hwndSelf, &rc);
3818 if(rc.right - rc.left < min_size) {
3819 default_left_margin = es->left_margin;
3820 default_right_margin = es->right_margin;
3821 }
3822 }
3823 SelectObject(dc, old_font);
3824 ReleaseDC(es->hwndSelf, dc);
3825 }
3826
3827 if (action & EC_LEFTMARGIN) {
3828 es->format_rect.left -= es->left_margin;
3829 if (left != EC_USEFONTINFO)
3830 es->left_margin = left;
3831 else
3832 es->left_margin = default_left_margin;
3833 es->format_rect.left += es->left_margin;
3834 }
3835
3836 if (action & EC_RIGHTMARGIN) {
3837 es->format_rect.right += es->right_margin;
3838 if (right != EC_USEFONTINFO)
3839 es->right_margin = right;
3840 else
3841 es->right_margin = default_right_margin;
3842 es->format_rect.right -= es->right_margin;
3843 }
3844
3845 if (action & (EC_LEFTMARGIN | EC_RIGHTMARGIN)) {
3846 EDIT_AdjustFormatRect(es);
3847 if (repaint) EDIT_UpdateText(es, NULL, TRUE);
3848 }
3849
3850 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3851 }
3852
3853
3854 /*********************************************************************
3855 *
3856 * EM_SETPASSWORDCHAR
3857 *
3858 */
3859 static void EDIT_EM_SetPasswordChar(EDITSTATE *es, WCHAR c)
3860 {
3861 LONG style;
3862
3863 if (es->style & ES_MULTILINE)
3864 return;
3865
3866 if (es->password_char == c)
3867 return;
3868
3869 style = GetWindowLongW( es->hwndSelf, GWL_STYLE );
3870 es->password_char = c;
3871 if (c) {
3872 SetWindowLongW( es->hwndSelf, GWL_STYLE, style | ES_PASSWORD );
3873 es->style |= ES_PASSWORD;
3874 } else {
3875 SetWindowLongW( es->hwndSelf, GWL_STYLE, style & ~ES_PASSWORD );
3876 es->style &= ~ES_PASSWORD;
3877 }
3878 EDIT_UpdateText(es, NULL, TRUE);
3879 }
3880
3881
3882 /*********************************************************************
3883 *
3884 * EDIT_EM_SetSel
3885 *
3886 * note: unlike the specs say: the order of start and end
3887 * _is_ preserved in Windows. (i.e. start can be > end)
3888 * In other words: this handler is OK
3889 *
3890 */
3891 static void EDIT_EM_SetSel(EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3892 {
3893 UINT old_start = es->selection_start;
3894 UINT old_end = es->selection_end;
3895 UINT len = get_text_length(es);
3896
3897 if (start == (UINT)-1) {
3898 start = es->selection_end;
3899 end = es->selection_end;
3900 } else {
3901 start = min(start, len);
3902 end = min(end, len);
3903 }
3904 es->selection_start = start;
3905 es->selection_end = end;
3906 if (after_wrap)
3907 es->flags |= EF_AFTER_WRAP;
3908 else
3909 es->flags &= ~EF_AFTER_WRAP;
3910 /* Compute the necessary invalidation region. */
3911 /* Note that we don't need to invalidate regions which have
3912 * "never" been selected, or those which are "still" selected.
3913 * In fact, every time we hit a selection boundary, we can
3914 * *toggle* whether we need to invalidate. Thus we can optimize by
3915 * *sorting* the interval endpoints. Let's assume that we sort them
3916 * in this order:
3917 * start <= end <= old_start <= old_end
3918 * Knuth 5.3.1 (p 183) asssures us that this can be done optimally
3919 * in 5 comparisons; ie it's impossible to do better than the
3920 * following: */
3921 ORDER_UINT(end, old_end);
3922 ORDER_UINT(start, old_start);
3923 ORDER_UINT(old_start, old_end);
3924 ORDER_UINT(start, end);
3925 /* Note that at this point 'end' and 'old_start' are not in order, but
3926 * start is definitely the min. and old_end is definitely the max. */
3927 if (end != old_start)
3928 {
3929 /*
3930 * One can also do
3931 * ORDER_UINT32(end, old_start);
3932 * EDIT_InvalidateText(es, start, end);
3933 * EDIT_InvalidateText(es, old_start, old_end);
3934 * in place of the following if statement.
3935 * (That would complete the optimal five-comparison four-element sort.)
3936 */
3937 if (old_start > end )
3938 {
3939 EDIT_InvalidateText(es, start, end);
3940 EDIT_InvalidateText(es, old_start, old_end);
3941 }
3942 else
3943 {
3944 EDIT_InvalidateText(es, start, old_start);
3945 EDIT_InvalidateText(es, end, old_end);
3946 }
3947 }
3948 else EDIT_InvalidateText(es, start, old_end);
3949 }
3950
3951
3952 /*********************************************************************
3953 *
3954 * EM_SETTABSTOPS
3955 *
3956 */
3957 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3958 {
3959 if (!(es->style & ES_MULTILINE))
3960 return FALSE;
3961 HeapFree(GetProcessHeap(), 0, es->tabs);
3962 es->tabs_count = count;
3963 if (!count)
3964 es->tabs = NULL;
3965 else {
3966 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3967 if (es->tabs == NULL)
3968 {
3969 es->tabs_count = 0;
3970 return FALSE;
3971 }
3972 memcpy(es->tabs, tabs, count * sizeof(INT));
3973 }
3974 return TRUE;
3975 }
3976
3977
3978 #ifndef __REACTOS__
3979 /*********************************************************************
3980 *
3981 * EM_SETTABSTOPS16
3982 *
3983 */
3984 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3985 {
3986 if (!(es->style & ES_MULTILINE))
3987 return FALSE;
3988 if (es->tabs)
3989 HeapFree(GetProcessHeap(), 0, es->tabs);
3990 es->tabs_count = count;
3991 if (!count)
3992 es->tabs = NULL;
3993 else {
3994 INT i;
3995 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3996 for (i = 0 ; i < count ; i++)
3997 es->tabs[i] = *tabs++;
3998 }
3999 return TRUE;
4000 }
4001 #endif
4002
4003
4004 /*********************************************************************
4005 *
4006 * EM_SETWORDBREAKPROC
4007 *
4008 */
4009 static void EDIT_EM_SetWordBreakProc(EDITSTATE *es, void *wbp)
4010 {
4011 if (es->word_break_proc == wbp)
4012 return;
4013
4014 es->word_break_proc = wbp;
4015 #ifndef __REACTOS__
4016 es->word_break_proc16 = NULL;
4017 #endif
4018
4019 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
4020 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
4021 EDIT_UpdateText(es, NULL, TRUE);
4022 }
4023 }
4024
4025
4026 #ifndef __REACTOS__
4027 /*********************************************************************
4028 *
4029 * EM_SETWORDBREAKPROC16
4030 *
4031 */
4032 static void EDIT_EM_SetWordBreakProc16(EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
4033 {
4034 if (es->word_break_proc16 == wbp)
4035 return;
4036
4037 es->word_break_proc = NULL;
4038 es->word_break_proc16 = wbp;
4039 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
4040 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
4041 EDIT_UpdateText(es, NULL, TRUE);
4042 }
4043 }
4044 #endif
4045
4046
4047 /*********************************************************************
4048 *
4049 * EM_UNDO / WM_UNDO
4050 *
4051 */
4052 static BOOL EDIT_EM_Undo(EDITSTATE *es)
4053 {
4054 INT ulength;
4055 LPWSTR utext;
4056
4057 /* As per MSDN spec, for a single-line edit control,
4058 the return value is always TRUE */
4059 if( es->style & ES_READONLY )
4060 return !(es->style & ES_MULTILINE);
4061
4062 ulength = strlenW(es->undo_text);
4063
4064 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
4065 if (utext == NULL)
4066 return FALSE;
4067
4068 strcpyW(utext, es->undo_text);
4069
4070 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
4071 es->undo_insert_count, debugstr_w(utext));
4072
4073 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
4074 EDIT_EM_EmptyUndoBuffer(es);
4075 EDIT_EM_ReplaceSel(es, TRUE, utext, TRUE, TRUE);
4076 EDIT_EM_SetSel(es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
4077 /* send the notification after the selection start and end are set */
4078 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4079 EDIT_EM_ScrollCaret(es);
4080 HeapFree(GetProcessHeap(), 0, utext);
4081
4082 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
4083 es->undo_insert_count, debugstr_w(es->undo_text));
4084 return TRUE;
4085 }
4086
4087 /* Helper function for WM_CHAR
4088 *
4089 * According to an MSDN blog article titled "Just because you're a control
4090 * doesn't mean that you're necessarily inside a dialog box," multiline edit
4091 * controls without ES_WANTRETURN would attempt to detect whether it is inside
4092 * a dialog box or not.
4093 */
4094 static BOOL EDIT_IsInsideDialog(EDITSTATE *es)
4095 {
4096 if (es->hwndParent && es->hwndParent != GetDesktopWindow())
4097 {
4098 if (GetClassLongW (es->hwndParent, GCW_ATOM) == (DWORD)MAKEINTATOM(32770))
4099 return TRUE;
4100 }
4101 return FALSE;
4102 }
4103
4104 /*********************************************************************
4105 *
4106 * WM_CHAR
4107 *
4108 */
4109 static BOOL EDIT_WM_Char(EDITSTATE *es, WCHAR c)
4110 {
4111 BOOL control;
4112
4113 control = GetKeyState(VK_CONTROL) & 0x8000;
4114
4115 switch (c) {
4116 case '\r':
4117 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
4118 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
4119 break;
4120 case '\n':
4121 if (es->style & ES_MULTILINE) {
4122 if (es->style & ES_READONLY) {
4123 EDIT_MoveHome(es, FALSE);
4124 EDIT_MoveDown_ML(es, FALSE);
4125 } else {
4126 static const WCHAR cr_lfW[] = {'\r','\n',0};
4127 EDIT_EM_ReplaceSel(es, TRUE, cr_lfW, TRUE, TRUE);
4128 }
4129 }
4130 break;
4131 case '\t':
4132 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
4133 {
4134 static const WCHAR tabW[] = {'\t',0};
4135 if (EDIT_IsInsideDialog(es))
4136 break;
4137 EDIT_EM_ReplaceSel(es, TRUE, tabW, TRUE, TRUE);
4138 }
4139 break;
4140 case VK_BACK:
4141 if (!(es->style & ES_READONLY) && !control) {
4142 if (es->selection_start != es->selection_end)
4143 EDIT_WM_Clear(es);
4144 else {
4145 /* delete character left of caret */
4146 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4147 EDIT_MoveBackward(es, TRUE);
4148 EDIT_WM_Clear(es);
4149 }
4150 }
4151 break;
4152 case 0x03: /* ^C */
4153 if (!(es->style & ES_PASSWORD))
4154 SendMessageW(es->hwndSelf, WM_COPY, 0, 0);
4155 break;
4156 case 0x16: /* ^V */
4157 if (!(es->style & ES_READONLY))
4158 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4159 break;
4160 case 0x18: /* ^X */
4161 if (!((es->style & ES_READONLY) || (es->style & ES_PASSWORD)))
4162 SendMessageW(es->hwndSelf, WM_CUT, 0, 0);
4163 break;
4164 case 0x1A: /* ^Z */
4165 if (!(es->style & ES_READONLY))
4166 SendMessageW(es->hwndSelf, WM_UNDO, 0, 0);
4167 break;
4168
4169 default:
4170 /*If Edit control style is ES_NUMBER allow users to key in only numeric values*/
4171 if( (es->style & ES_NUMBER) && !( c >= '0' && c <= '9') )
4172 break;
4173
4174 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
4175 WCHAR str[2];
4176 str[0] = c;
4177 str[1] = '\0';
4178 EDIT_EM_ReplaceSel(es, TRUE, str, TRUE, TRUE);
4179 }
4180 break;
4181 }
4182
4183 return TRUE;
4184 }
4185
4186
4187 /*********************************************************************
4188 *
4189 * WM_COMMAND
4190 *
4191 */
4192 static void EDIT_WM_Command(EDITSTATE *es, INT code, INT id, HWND control)
4193 {
4194 if (code || control)
4195 return;
4196
4197 switch (id) {
4198 case EM_UNDO:
4199 EDIT_EM_Undo(es);
4200 break;
4201 case WM_CUT:
4202 EDIT_WM_Cut(es);
4203 break;
4204 case WM_COPY:
4205 EDIT_WM_Copy(es);
4206 break;
4207 case WM_PASTE:
4208 EDIT_WM_Paste(es);
4209 break;
4210 case WM_CLEAR:
4211 EDIT_WM_Clear(es);
4212 break;
4213 case EM_SETSEL:
4214 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
4215 EDIT_EM_ScrollCaret(es);
4216 break;
4217 default:
4218 ERR("unknown menu item, please report\n");
4219 break;
4220 }
4221 }
4222
4223
4224 /*********************************************************************
4225 *
4226 * WM_CONTEXTMENU
4227 *
4228 * Note: the resource files resource/sysres_??.rc cannot define a
4229 * single popup menu. Hence we use a (dummy) menubar
4230 * containing the single popup menu as its first item.
4231 *
4232 * FIXME: the message identifiers have been chosen arbitrarily,
4233 * hence we use MF_BYPOSITION.
4234 * We might as well use the "real" values (anybody knows ?)
4235 * The menu definition is in resources/sysres_??.rc.
4236 * Once these are OK, we better use MF_BYCOMMAND here
4237 * (as we do in EDIT_WM_Command()).
4238 *
4239 */
4240 static void EDIT_WM_ContextMenu(EDITSTATE *es, INT x, INT y)
4241 {
4242 #ifdef __REACTOS__
4243 HMENU menu = LoadMenuA(User32Instance, "EDITMENU");
4244 #else
4245 HMENU menu = LoadMenuA(user32_module, "EDITMENU");
4246 #endif
4247 HMENU popup = GetSubMenu(menu, 0);
4248 UINT start = es->selection_start;
4249 UINT end = es->selection_end;
4250
4251 ORDER_UINT(start, end);
4252
4253 /* undo */
4254 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4255 /* cut */
4256 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4257 /* copy */
4258 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
4259 /* paste */
4260 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4261 /* delete */
4262 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
4263 /* select all */
4264 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != get_text_length(es)) ? MF_ENABLED : MF_GRAYED));
4265
4266 if (x == -1 && y == -1) /* passed via VK_APPS press/release */
4267 {
4268 RECT rc;
4269 /* Windows places the menu at the edit's center in this case */
4270 GetClientRect(es->hwndSelf, &rc);
4271 MapWindowPoints(es->hwndSelf, 0, (POINT *)&rc, 2);
4272 x = rc.left + (rc.right - rc.left) / 2;
4273 y = rc.top + (rc.bottom - rc.top) / 2;
4274 }
4275
4276 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, es->hwndSelf, NULL);
4277 DestroyMenu(menu);
4278 }
4279
4280
4281 /*********************************************************************
4282 *
4283 * WM_COPY
4284 *
4285 */
4286 static void EDIT_WM_Copy(EDITSTATE *es)
4287 {
4288 INT s = min(es->selection_start, es->selection_end);
4289 INT e = max(es->selection_start, es->selection_end);
4290 HGLOBAL hdst;
4291 LPWSTR dst;
4292 DWORD len;
4293
4294 if (e == s) return;
4295
4296 len = e - s;
4297 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
4298 dst = GlobalLock(hdst);
4299 memcpy(dst, es->text + s, len * sizeof(WCHAR));
4300 dst[len] = 0; /* ensure 0 termination */
4301 TRACE("%s\n", debugstr_w(dst));
4302 GlobalUnlock(hdst);
4303 OpenClipboard(es->hwndSelf);
4304 EmptyClipboard();
4305 SetClipboardData(CF_UNICODETEXT, hdst);
4306 CloseClipboard();
4307 }
4308
4309
4310 /*********************************************************************
4311 *
4312 * WM_CREATE
4313 *
4314 */
4315 static LRESULT EDIT_WM_Create(EDITSTATE *es, LPCWSTR name)
4316 {
4317 RECT clientRect;
4318
4319 TRACE("%s\n", debugstr_w(name));
4320 /*
4321 * To initialize some final structure members, we call some helper
4322 * functions. However, since the EDITSTATE is not consistent (i.e.
4323 * not fully initialized), we should be very careful which
4324 * functions can be called, and in what order.
4325 */
4326 EDIT_WM_SetFont(es, 0, FALSE);
4327 EDIT_EM_EmptyUndoBuffer(es);
4328
4329 /* We need to calculate the format rect
4330 (applications may send EM_SETMARGINS before the control gets visible) */
4331 GetClientRect(es->hwndSelf, &clientRect);
4332 EDIT_SetRectNP(es, &clientRect);
4333
4334 if (name && *name) {
4335 EDIT_EM_ReplaceSel(es, FALSE, name, FALSE, FALSE);
4336 /* if we insert text to the editline, the text scrolls out
4337 * of the window, as the caret is placed after the insert
4338 * pos normally; thus we reset es->selection... to 0 and
4339 * update caret
4340 */
4341 es->selection_start = es->selection_end = 0;
4342 /* Adobe Photoshop does NOT like this. and MSDN says that EN_CHANGE
4343 * Messages are only to be sent when the USER does something to
4344 * change the contents. So I am removing this EN_CHANGE
4345 *
4346 * EDIT_NOTIFY_PARENT(es, EN_CHANGE);
4347 */
4348 EDIT_EM_ScrollCaret(es);
4349 }
4350 /* force scroll info update */
4351 EDIT_UpdateScrollInfo(es);
4352 /* The rule seems to return 1 here for success */
4353 /* Power Builder masked edit controls will crash */
4354 /* if not. */
4355 /* FIXME: is that in all cases so ? */
4356 return 1;
4357 }
4358
4359
4360 /*********************************************************************
4361 *
4362 * WM_DESTROY
4363 *
4364 */
4365 static LRESULT EDIT_WM_Destroy(EDITSTATE *es)
4366 {
4367 LINEDEF *pc, *pp;
4368
4369 if (es->hloc32W) {
4370 while (LocalUnlock(es->hloc32W)) ;
4371 LocalFree(es->hloc32W);
4372 }
4373 if (es->hloc32A) {
4374 while (LocalUnlock(es->hloc32A)) ;
4375 LocalFree(es->hloc32A);
4376 }
4377 #ifndef __REACTOS__
4378 if (es->hloc16) {
4379 HINSTANCE16 hInstance = GetWindowWord( es->hwndSelf, GWL_HINSTANCE );
4380 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
4381 LOCAL_Free(hInstance, es->hloc16);
4382 }
4383 #endif
4384
4385 pc = es->first_line_def;
4386 while (pc)
4387 {
4388 pp = pc->next;
4389 HeapFree(GetProcessHeap(), 0, pc);
4390 pc = pp;
4391 }
4392
4393 SetWindowLongPtrW( es->hwndSelf, 0, 0 );
4394 HeapFree(GetProcessHeap(), 0, es);
4395
4396 return 0;
4397 }
4398
4399
4400 /*********************************************************************
4401 *
4402 * WM_ERASEBKGND
4403 *
4404 */
4405 static LRESULT EDIT_WM_EraseBkGnd(EDITSTATE *es, HDC dc)
4406 {
4407 /* we do the proper erase in EDIT_WM_Paint */
4408 return 1;
4409 }
4410
4411
4412 /*********************************************************************
4413 *
4414 * WM_GETTEXT
4415 *
4416 */
4417 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPWSTR dst, BOOL unicode)
4418 {
4419 if(!count) return 0;
4420
4421 if(unicode)
4422 {
4423 lstrcpynW(dst, es->text, count);
4424 return strlenW(dst);
4425 }
4426 else
4427 {
4428 LPSTR textA = (LPSTR)dst;
4429 if (!WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL))
4430 textA[count - 1] = 0; /* ensure 0 termination */
4431 return strlen(textA);
4432 }
4433 }
4434
4435 /*********************************************************************
4436 *
4437 * WM_HSCROLL
4438 *
4439 */
4440 static LRESULT EDIT_WM_HScroll(EDITSTATE *es, INT action, INT pos)
4441 {
4442 INT dx;
4443 INT fw;
4444
4445 if (!(es->style & ES_MULTILINE))
4446 return 0;
4447
4448 if (!(es->style & ES_AUTOHSCROLL))
4449 return 0;
4450
4451 dx = 0;
4452 fw = es->format_rect.right - es->format_rect.left;
4453 switch (action) {
4454 case SB_LINELEFT:
4455 TRACE("SB_LINELEFT\n");
4456 if (es->x_offset)
4457 dx = -es->char_width;
4458 break;
4459 case SB_LINERIGHT:
4460 TRACE("SB_LINERIGHT\n");
4461 if (es->x_offset < es->text_width)
4462 dx = es->char_width;
4463 break;
4464 case SB_PAGELEFT:
4465 TRACE("SB_PAGELEFT\n");
4466 if (es->x_offset)
4467 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4468 break;
4469 case SB_PAGERIGHT:
4470 TRACE("SB_PAGERIGHT\n");
4471 if (es->x_offset < es->text_width)
4472 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4473 break;
4474 case SB_LEFT:
4475 TRACE("SB_LEFT\n");
4476 if (es->x_offset)
4477 dx = -es->x_offset;
4478 break;
4479 case SB_RIGHT:
4480 TRACE("SB_RIGHT\n");
4481 if (es->x_offset < es->text_width)
4482 dx = es->text_width - es->x_offset;
4483 break;
4484 case SB_THUMBTRACK:
4485 TRACE("SB_THUMBTRACK %d\n", pos);
4486 es->flags |= EF_HSCROLL_TRACK;
4487 if(es->style & WS_HSCROLL)
4488 dx = pos - es->x_offset;
4489 else
4490 {
4491 INT fw, new_x;
4492 /* Sanity check */
4493 if(pos < 0 || pos > 100) return 0;
4494 /* Assume default scroll range 0-100 */
4495 fw = es->format_rect.right - es->format_rect.left;
4496 new_x = pos * (es->text_width - fw) / 100;
4497 dx = es->text_width ? (new_x - es->x_offset) : 0;
4498 }
4499 break;
4500 case SB_THUMBPOSITION:
4501 TRACE("SB_THUMBPOSITION %d\n", pos);
4502 es->flags &= ~EF_HSCROLL_TRACK;
4503 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4504 dx = pos - es->x_offset;
4505 else
4506 {
4507 INT fw, new_x;
4508 /* Sanity check */
4509 if(pos < 0 || pos > 100) return 0;
4510 /* Assume default scroll range 0-100 */
4511 fw = es->format_rect.right - es->format_rect.left;
4512 new_x = pos * (es->text_width - fw) / 100;
4513 dx = es->text_width ? (new_x - es->x_offset) : 0;
4514 }
4515 if (!dx) {
4516 /* force scroll info update */
4517 EDIT_UpdateScrollInfo(es);
4518 EDIT_NOTIFY_PARENT(es, EN_HSCROLL);
4519 }
4520 break;
4521 case SB_ENDSCROLL:
4522 TRACE("SB_ENDSCROLL\n");
4523 break;
4524 /*
4525 * FIXME : the next two are undocumented !
4526 * Are we doing the right thing ?
4527 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4528 * although it's also a regular control message.
4529 */
4530 case EM_GETTHUMB: /* this one is used by NT notepad */
4531 #ifndef __REACTOS__
4532 case EM_GETTHUMB16:
4533 #endif
4534 {
4535 LRESULT ret;
4536 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_HSCROLL)
4537 ret = GetScrollPos(es->hwndSelf, SB_HORZ);
4538 else
4539 {
4540 /* Assume default scroll range 0-100 */
4541 INT fw = es->format_rect.right - es->format_rect.left;
4542 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4543 }
4544 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4545 return ret;
4546 }
4547 #ifndef __REACTOS__
4548 case EM_LINESCROLL16:
4549 TRACE("EM_LINESCROLL16\n");
4550 dx = pos;
4551 break;
4552 #endif
4553
4554 default:
4555 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4556 action, action);
4557 return 0;
4558 }
4559 if (dx)
4560 {
4561 INT fw = es->format_rect.right - es->format_rect.left;
4562 /* check if we are going to move too far */
4563 if(es->x_offset + dx + fw > es->text_width)
4564 dx = es->text_width - fw - es->x_offset;
4565 if(dx)
4566 EDIT_EM_LineScroll_internal(es, dx, 0);
4567 }
4568 return 0;
4569 }
4570
4571
4572 /*********************************************************************
4573 *
4574 * EDIT_CheckCombo
4575 *
4576 */
4577 static BOOL EDIT_CheckCombo(EDITSTATE *es, UINT msg, INT key)
4578 {
4579 HWND hLBox = es->hwndListBox;
4580 HWND hCombo;
4581 BOOL bDropped;
4582 int nEUI;
4583
4584 if (!hLBox)
4585 return FALSE;
4586
4587 hCombo = GetParent(es->hwndSelf);
4588 bDropped = TRUE;
4589 nEUI = 0;
4590
4591 TRACE_(combo)("[%p]: handling msg %x (%x)\n", es->hwndSelf, msg, key);
4592
4593 if (key == VK_UP || key == VK_DOWN)
4594 {
4595 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4596 nEUI = 1;
4597
4598 if (msg == WM_KEYDOWN || nEUI)
4599 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4600 }
4601
4602 switch (msg)
4603 {
4604 case WM_KEYDOWN:
4605 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4606 {
4607 /* make sure ComboLBox pops up */
4608 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4609 key = VK_F4;
4610 nEUI = 2;
4611 }
4612
4613 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4614 break;
4615
4616 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4617 if (nEUI)
4618 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4619 else
4620 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4621 break;
4622 }
4623
4624 if(nEUI == 2)
4625 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4626
4627 return TRUE;
4628 }
4629
4630
4631 /*********************************************************************
4632 *
4633 * WM_KEYDOWN
4634 *
4635 * Handling of special keys that don't produce a WM_CHAR
4636 * (i.e. non-printable keys) & Backspace & Delete
4637 *
4638 */
4639 static LRESULT EDIT_WM_KeyDown(EDITSTATE *es, INT key)
4640 {
4641 BOOL shift;
4642 BOOL control;
4643
4644 if (GetKeyState(VK_MENU) & 0x8000)
4645 return 0;
4646
4647 shift = GetKeyState(VK_SHIFT) & 0x8000;
4648 control = GetKeyState(VK_CONTROL) & 0x8000;
4649
4650 switch (key) {
4651 case VK_F4:
4652 case VK_UP:
4653 if (EDIT_CheckCombo(es, WM_KEYDOWN, key) || key == VK_F4)
4654 break;
4655
4656 /* fall through */
4657 case VK_LEFT:
4658 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4659 EDIT_MoveUp_ML(es, shift);
4660 else
4661 if (control)
4662 EDIT_MoveWordBackward(es, shift);
4663 else
4664 EDIT_MoveBackward(es, shift);
4665 break;
4666 case VK_DOWN:
4667 if (EDIT_CheckCombo(es, WM_KEYDOWN, key))
4668 break;
4669 /* fall through */
4670 case VK_RIGHT:
4671 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4672 EDIT_MoveDown_ML(es, shift);
4673 else if (control)
4674 EDIT_MoveWordForward(es, shift);
4675 else
4676 EDIT_MoveForward(es, shift);
4677 break;
4678 case VK_HOME:
4679 if (control && (es->style & ES_MULTILINE)) {
4680 EDIT_MoveStartOfText(es, shift);
4681 } else {
4682 EDIT_MoveHome(es, shift); /* start of line */
4683 }
4684 break;
4685 case VK_END:
4686 if (control && (es->style & ES_MULTILINE)) {
4687 EDIT_MoveEndOfText(es, shift);
4688 } else {
4689 EDIT_MoveEnd(es, shift); /* end of line */
4690 }
4691 break;
4692 case VK_PRIOR:
4693 if (es->style & ES_MULTILINE)
4694 EDIT_MovePageUp_ML(es, shift);
4695 else
4696 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4697 break;
4698 case VK_NEXT:
4699 if (es->style & ES_MULTILINE)
4700 EDIT_MovePageDown_ML(es, shift);
4701 else
4702 EDIT_CheckCombo(es, WM_KEYDOWN, key);
4703 break;
4704 case VK_DELETE:
4705 if (!(es->style & ES_READONLY) && !(shift && control)) {
4706 if (es->selection_start != es->selection_end) {
4707 if (shift)
4708 EDIT_WM_Cut(es);
4709 else
4710 EDIT_WM_Clear(es);
4711 } else {
4712 if (shift) {
4713 /* delete character left of caret */
4714 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4715 EDIT_MoveBackward(es, TRUE);
4716 EDIT_WM_Clear(es);
4717 } else if (control) {
4718 /* delete to end of line */
4719 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4720 EDIT_MoveEnd(es, TRUE);
4721 EDIT_WM_Clear(es);
4722 } else {
4723 /* delete character right of caret */
4724 EDIT_EM_SetSel(es, (UINT)-1, 0, FALSE);
4725 EDIT_MoveForward(es, TRUE);
4726 EDIT_WM_Clear(es);
4727 }
4728 }
4729 }
4730 break;
4731 case VK_INSERT:
4732 if (shift) {
4733 if (!(es->style & ES_READONLY))
4734 EDIT_WM_Paste(es);
4735 } else if (control)
4736 EDIT_WM_Copy(es);
4737 break;
4738 case VK_RETURN:
4739 /* If the edit doesn't want the return send a message to the default object */
4740 if(!(es->style & ES_MULTILINE) || !(es->style & ES_WANTRETURN))
4741 {
4742 HWND hwndParent;
4743 DWORD dw;
4744
4745 if (!EDIT_IsInsideDialog(es)) return 1;
4746 if (control) break;
4747
4748 hwndParent = GetParent(es->hwndSelf);
4749 dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4750 if (HIWORD(dw) == DC_HASDEFID)
4751 {
4752 SendMessageW( hwndParent, WM_COMMAND,
4753 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4754 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4755 }
4756 else
4757 SendMessageW( hwndParent, WM_COMMAND, IDOK, (LPARAM)GetDlgItem( hwndParent, IDOK ) );
4758 }
4759 break;
4760 case VK_ESCAPE:
4761 if (!(es->style & ES_MULTILINE))
4762 SendMessageW(GetParent(es->hwndSelf), WM_COMMAND, IDCANCEL, (LPARAM)GetDlgItem( GetParent(es->hwndSelf), IDCANCEL ) );
4763 break;
4764 case VK_TAB:
4765 SendMessageW(es->hwndParent, WM_NEXTDLGCTL, shift, 0);
4766 break;
4767 }
4768 return 0;
4769 }
4770
4771
4772 /*********************************************************************
4773 *
4774 * WM_KILLFOCUS
4775 *
4776 */
4777 static LRESULT EDIT_WM_KillFocus(EDITSTATE *es)
4778 {
4779 es->flags &= ~EF_FOCUSED;
4780 DestroyCaret();
4781 if(!(es->style & ES_NOHIDESEL))
4782 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
4783 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS);
4784 return 0;
4785 }
4786
4787
4788 /*********************************************************************
4789 *
4790 * WM_LBUTTONDBLCLK
4791 *
4792 * The caret position has been set on the WM_LBUTTONDOWN message
4793 *
4794 */
4795 static LRESULT EDIT_WM_LButtonDblClk(EDITSTATE *es)
4796 {
4797 INT s;
4798 INT e = es->selection_end;
4799 INT l;
4800 INT li;
4801 INT ll;
4802
4803 es->bCaptureState = TRUE;
4804 SetCapture(es->hwndSelf);
4805
4806 l = EDIT_EM_LineFromChar(es, e);
4807 li = EDIT_EM_LineIndex(es, l);
4808 ll = EDIT_EM_LineLength(es, e);
4809 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4810 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4811 EDIT_EM_SetSel(es, s, e, FALSE);
4812 EDIT_EM_ScrollCaret(es);
4813 es->region_posx = es->region_posy = 0;
4814 SetTimer(es->hwndSelf, 0, 100, NULL);
4815 return 0;
4816 }
4817
4818
4819 /*********************************************************************
4820 *
4821 * WM_LBUTTONDOWN
4822 *
4823 */
4824 static LRESULT EDIT_WM_LButtonDown(EDITSTATE *es, DWORD keys, INT x, INT y)
4825 {
4826 INT e;
4827 BOOL after_wrap;
4828
4829 es->bCaptureState = TRUE;
4830 SetCapture(es->hwndSelf);
4831 EDIT_ConfinePoint(es, &x, &y);
4832 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4833 EDIT_EM_SetSel(es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4834 EDIT_EM_ScrollCaret(es);
4835 es->region_posx = es->region_posy = 0;
4836 SetTimer(es->hwndSelf, 0, 100, NULL);
4837
4838 if (!(es->flags & EF_FOCUSED))
4839 SetFocus(es->hwndSelf);
4840
4841 return 0;
4842 }
4843
4844
4845 /*********************************************************************
4846 *
4847 * WM_LBUTTONUP
4848 *
4849 */
4850 static LRESULT EDIT_WM_LButtonUp(EDITSTATE *es)
4851 {
4852 if (es->bCaptureState) {
4853 KillTimer(es->hwndSelf, 0);
4854 if (GetCapture() == es->hwndSelf) ReleaseCapture();
4855 }
4856 es->bCaptureState = FALSE;
4857 return 0;
4858 }
4859
4860
4861 /*********************************************************************
4862 *
4863 * WM_MBUTTONDOWN
4864 *
4865 */
4866 static LRESULT EDIT_WM_MButtonDown(EDITSTATE *es)
4867 {
4868 SendMessageW(es->hwndSelf, WM_PASTE, 0, 0);
4869 return 0;
4870 }
4871
4872
4873 /*********************************************************************
4874 *
4875 * WM_MOUSEMOVE
4876 *
4877 */
4878 static LRESULT EDIT_WM_MouseMove(EDITSTATE *es, INT x, INT y)
4879 {
4880 INT e;
4881 BOOL after_wrap;
4882 INT prex, prey;
4883
4884 /* If the mouse has been captured by process other than the edit control itself,
4885 * the windows edit controls will not select the strings with mouse move.
4886 */
4887 if (!es->bCaptureState || GetCapture() != es->hwndSelf)
4888 return 0;
4889
4890 /*
4891 * FIXME: gotta do some scrolling if outside client
4892 * area. Maybe reset the timer ?
4893 */
4894 prex = x; prey = y;
4895 EDIT_ConfinePoint(es, &x, &y);
4896 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4897 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4898 e = EDIT_CharFromPos(es, x, y, &after_wrap);
4899 EDIT_EM_SetSel(es, es->selection_start, e, after_wrap);
4900 EDIT_SetCaretPos(es,es->selection_end,es->flags & EF_AFTER_WRAP);
4901 return 0;
4902 }
4903
4904
4905 /*********************************************************************
4906 *
4907 * WM_NCCREATE
4908 *
4909 * See also EDIT_WM_StyleChanged
4910 */
4911 static LRESULT EDIT_WM_NCCreate(HWND hwnd, LPCREATESTRUCTW lpcs, BOOL unicode)
4912 {
4913 EDITSTATE *es;
4914 UINT alloc_size;
4915
4916 TRACE("Creating %s edit control, style = %08lx\n",
4917 unicode ? "Unicode" : "ANSI", lpcs->style);
4918
4919 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4920 return FALSE;
4921 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)es );
4922
4923 /*
4924 * Note: since the EDITSTATE has not been fully initialized yet,
4925 * we can't use any API calls that may send
4926 * WM_XXX messages before WM_NCCREATE is completed.
4927 */
4928
4929 es->is_unicode = unicode;
4930 es->style = lpcs->style;
4931
4932 es->bEnableState = !(es->style & WS_DISABLED);
4933
4934 es->hwndSelf = hwnd;
4935 /* Save parent, which will be notified by EN_* messages */
4936 es->hwndParent = lpcs->hwndParent;
4937
4938 if (es->style & ES_COMBO)
4939 es->hwndListBox = GetDlgItem(es->hwndParent, ID_CB_LISTBOX);
4940
4941 /* Number overrides lowercase overrides uppercase (at least it
4942 * does in Win95). However I'll bet that ES_NUMBER would be
4943 * invalid under Win 3.1.
4944 */
4945 if (es->style & ES_NUMBER) {
4946 ; /* do not override the ES_NUMBER */
4947 } else if (es->style & ES_LOWERCASE) {
4948 es->style &= ~ES_UPPERCASE;
4949 }
4950 if (es->style & ES_MULTILINE) {
4951 es->buffer_limit = BUFLIMIT_INITIAL;
4952 if (es->style & WS_VSCROLL)
4953 es->style |= ES_AUTOVSCROLL;
4954 if (es->style & WS_HSCROLL)
4955 es->style |= ES_AUTOHSCROLL;
4956 es->style &= ~ES_PASSWORD;
4957 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4958 /* Confirmed - RIGHT overrides CENTER */
4959 if (es->style & ES_RIGHT)
4960 es->style &= ~ES_CENTER;
4961 es->style &= ~WS_HSCROLL;
4962 es->style &= ~ES_AUTOHSCROLL;
4963 }
4964 } else {
4965 es->buffer_limit = BUFLIMIT_INITIAL;
4966 if ((es->style & ES_RIGHT) && (es->style & ES_CENTER))
4967 es->style &= ~ES_CENTER;
4968 es->style &= ~WS_HSCROLL;
4969 es->style &= ~WS_VSCROLL;
4970 if (es->style & ES_PASSWORD)
4971 es->password_char = '*';
4972 }
4973
4974 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4975 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4976 return FALSE;
4977 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4978
4979 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4980 return FALSE;
4981 es->undo_buffer_size = es->buffer_size;
4982
4983 if (es->style & ES_MULTILINE)
4984 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4985 return FALSE;
4986 es->line_count = 1;
4987
4988 /*
4989 * In Win95 look and feel, the WS_BORDER style is replaced by the
4990 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4991 * control a nonclient area so we don't need to draw the border.
4992 * If WS_BORDER without WS_EX_CLIENTEDGE is specified we shouldn't have
4993 * a nonclient area and we should handle painting the border ourselves.
4994 *
4995 * When making modifications please ensure that the code still works
4996 * for edit controls created directly with style 0x50800000, exStyle 0
4997 * (which should have a single pixel border)
4998 */
4999 if (lpcs->dwExStyle & WS_EX_CLIENTEDGE)
5000 es->style &= ~WS_BORDER;
5001 else if (es->style & WS_BORDER)
5002 SetWindowLongW(hwnd, GWL_STYLE, es->style & ~WS_BORDER);
5003
5004 return TRUE;
5005 }
5006
5007 /*********************************************************************
5008 *
5009 * WM_PAINT
5010 *
5011 */
5012 static void EDIT_WM_Paint(EDITSTATE *es, HDC hdc)
5013 {
5014 PAINTSTRUCT ps;
5015 INT i;
5016 HDC dc;
5017 HFONT old_font = 0;
5018 RECT rc;
5019 RECT rcClient;
5020 RECT rcLine;
5021 RECT rcRgn;
5022 HBRUSH brush;
5023 HBRUSH old_brush;
5024 INT bw, bh;
5025 BOOL rev = es->bEnableState &&
5026 ((es->flags & EF_FOCUSED) ||
5027 (es->style & ES_NOHIDESEL));
5028 dc = hdc ? hdc : BeginPaint(es->hwndSelf, &ps);
5029
5030 GetClientRect(es->hwndSelf, &rcClient);
5031
5032 /* get the background brush */
5033 brush = EDIT_NotifyCtlColor(es, dc);
5034
5035 /* paint the border and the background */
5036 IntersectClipRect(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5037
5038 if(es->style & WS_BORDER) {
5039 bw = GetSystemMetrics(SM_CXBORDER);
5040 bh = GetSystemMetrics(SM_CYBORDER);
5041 rc = rcClient;
5042 if(es->style & ES_MULTILINE) {
5043 if(es->style & WS_HSCROLL) rc.bottom+=bh;
5044 if(es->style & WS_VSCROLL) rc.right+=bw;
5045 }
5046
5047 /* Draw the frame. Same code as in nonclient.c */
5048 old_brush = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
5049 PatBlt(dc, rc.left, rc.top, rc.right - rc.left, bh, PATCOPY);
5050 PatBlt(dc, rc.left, rc.top, bw, rc.bottom - rc.top, PATCOPY);
5051 PatBlt(dc, rc.left, rc.bottom - 1, rc.right - rc.left, -bw, PATCOPY);
5052 PatBlt(dc, rc.right - 1, rc.top, -bw, rc.bottom - rc.top, PATCOPY);
5053 SelectObject(dc, old_brush);
5054
5055 /* Keep the border clean */
5056 IntersectClipRect(dc, rc.left+bw, rc.top+bh,
5057 max(rc.right-bw, rc.left+bw), max(rc.bottom-bh, rc.top+bh));
5058 }
5059
5060 GetClipBox(dc, &rc);
5061 FillRect(dc, &rc, brush);
5062
5063 IntersectClipRect(dc, es->format_rect.left,
5064 es->format_rect.top,
5065 es->format_rect.right,
5066 es->format_rect.bottom);
5067 if (es->style & ES_MULTILINE) {
5068 rc = rcClient;
5069 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
5070 }
5071 if (es->font)
5072 old_font = SelectObject(dc, es->font);
5073
5074 if (!es->bEnableState)
5075 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
5076 GetClipBox(dc, &rcRgn);
5077 if (es->style & ES_MULTILINE) {
5078 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5079 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
5080 EDIT_GetLineRect(es, i, 0, -1, &rcLine);
5081 if (IntersectRect(&rc, &rcRgn, &rcLine))
5082 EDIT_PaintLine(es, dc, i, rev);
5083 }
5084 } else {
5085 EDIT_GetLineRect(es, 0, 0, -1, &rcLine);
5086 if (IntersectRect(&rc, &rcRgn, &rcLine))
5087 EDIT_PaintLine(es, dc, 0, rev);
5088 }
5089 if (es->font)
5090 SelectObject(dc, old_font);
5091
5092 if (!hdc)
5093 EndPaint(es->hwndSelf, &ps);
5094 }
5095
5096
5097 /*********************************************************************
5098 *
5099 * WM_PASTE
5100 *
5101 */
5102 static void EDIT_WM_Paste(EDITSTATE *es)
5103 {
5104 HGLOBAL hsrc;
5105 LPWSTR src;
5106
5107 /* Protect read-only edit control from modification */
5108 if(es->style & ES_READONLY)
5109 return;
5110
5111 OpenClipboard(es->hwndSelf);
5112 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
5113 src = (LPWSTR)GlobalLock(hsrc);
5114 EDIT_EM_ReplaceSel(es, TRUE, src, TRUE, TRUE);
5115 GlobalUnlock(hsrc);
5116 }
5117 else if (es->style & ES_PASSWORD) {
5118 /* clear selected text in password edit box even with empty clipboard */
5119 const WCHAR empty_strW[] = { 0 };
5120 EDIT_EM_ReplaceSel(es, TRUE, empty_strW, TRUE, TRUE);
5121 }
5122 CloseClipboard();
5123 }
5124
5125
5126 /*********************************************************************
5127 *
5128 * WM_SETFOCUS
5129 *
5130 */
5131 static void EDIT_WM_SetFocus(EDITSTATE *es)
5132 {
5133 es->flags |= EF_FOCUSED;
5134
5135 if (!(es->style & ES_NOHIDESEL))
5136 EDIT_InvalidateText(es, es->selection_start, es->selection_end);
5137
5138 /* single line edit updates itself */
5139 if (!(es->style & ES_MULTILINE))
5140 {
5141 HDC hdc = GetDC(es->hwndSelf);
5142 EDIT_WM_Paint(es, hdc);
5143 ReleaseDC(es->hwndSelf, hdc);
5144 }
5145
5146 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5147 EDIT_SetCaretPos(es, es->selection_end,
5148 es->flags & EF_AFTER_WRAP);
5149 ShowCaret(es->hwndSelf);
5150 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS);
5151 }
5152
5153
5154 /*********************************************************************
5155 *
5156 * WM_SETFONT
5157 *
5158 * With Win95 look the margins are set to default font value unless
5159 * the system font (font == 0) is being set, in which case they are left
5160 * unchanged.
5161 *
5162 */
5163 static void EDIT_WM_SetFont(EDITSTATE *es, HFONT font, BOOL redraw)
5164 {
5165 TEXTMETRICW tm;
5166 HDC dc;
5167 HFONT old_font = 0;
5168 RECT clientRect;
5169
5170 es->font = font;
5171 dc = GetDC(es->hwndSelf);
5172 if (font)
5173 old_font = SelectObject(dc, font);
5174 GetTextMetricsW(dc, &tm);
5175 es->line_height = tm.tmHeight;
5176 es->char_width = tm.tmAveCharWidth;
5177 if (font)
5178 SelectObject(dc, old_font);
5179 ReleaseDC(es->hwndSelf, dc);
5180
5181 /* Reset the format rect and the margins */
5182 GetClientRect(es->hwndSelf, &clientRect);
5183 EDIT_SetRectNP(es, &clientRect);
5184 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
5185 EC_USEFONTINFO, EC_USEFONTINFO, FALSE);
5186
5187 if (es->style & ES_MULTILINE)
5188 EDIT_BuildLineDefs_ML(es, 0, get_text_length(es), 0, NULL);
5189 else
5190 EDIT_CalcLineWidth_SL(es);
5191
5192 if (redraw)
5193 EDIT_UpdateText(es, NULL, TRUE);
5194 if (es->flags & EF_FOCUSED) {
5195 DestroyCaret();
5196 CreateCaret(es->hwndSelf, 0, 2, es->line_height);
5197 EDIT_SetCaretPos(es, es->selection_end,
5198 es->flags & EF_AFTER_WRAP);
5199 ShowCaret(es->hwndSelf);
5200 }
5201 }
5202
5203
5204 /*********************************************************************
5205 *
5206 * WM_SETTEXT
5207 *
5208 * NOTES
5209 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
5210 * The modified flag is reset. No notifications are sent.
5211 *
5212 * For single-line controls, reception of WM_SETTEXT triggers:
5213 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
5214 *
5215 */
5216 static void EDIT_WM_SetText(EDITSTATE *es, LPCWSTR text, BOOL unicode)
5217 {
5218 LPWSTR textW = NULL;
5219 if (!unicode && text)
5220 {
5221 LPCSTR textA = (LPCSTR)text;
5222 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
5223 textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR));
5224 if (textW)
5225 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
5226 text = textW;
5227 }
5228
5229 if (es->flags & EF_UPDATE)
5230 /* fixed this bug once; complain if we see it about to happen again. */
5231 ERR("SetSel may generate UPDATE message whose handler may reset "
5232 "selection.\n");
5233
5234 EDIT_EM_SetSel(es, 0, (UINT)-1, FALSE);
5235 if (text)
5236 {
5237 TRACE("%s\n", debugstr_w(text));
5238 EDIT_EM_ReplaceSel(es, FALSE, text, FALSE, FALSE);
5239 if(!unicode)
5240 HeapFree(GetProcessHeap(), 0, textW);
5241 }
5242 else
5243 {
5244 static const WCHAR empty_stringW[] = {0};
5245 TRACE("<NULL>\n");
5246 EDIT_EM_ReplaceSel(es, FALSE, empty_stringW, FALSE, FALSE);
5247 }
5248 es->x_offset = 0;
5249 es->flags &= ~EF_MODIFIED;
5250 EDIT_EM_SetSel(es, 0, 0, FALSE);
5251
5252 /* Send the notification after the selection start and end have been set
5253 * edit control doesn't send notification on WM_SETTEXT
5254 * if it is multiline, or it is part of combobox
5255 */
5256 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
5257 {
5258 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5259 EDIT_NOTIFY_PARENT(es, EN_CHANGE);
5260 }
5261 EDIT_EM_ScrollCaret(es);
5262 EDIT_UpdateScrollInfo(es);
5263 }
5264
5265
5266 /*********************************************************************
5267 *
5268 * WM_SIZE
5269 *
5270 */
5271 static void EDIT_WM_Size(EDITSTATE *es, UINT action, INT width, INT height)
5272 {
5273 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
5274 RECT rc;
5275 TRACE("width = %d, height = %d\n", width, height);
5276 SetRect(&rc, 0, 0, width, height);
5277 EDIT_SetRectNP(es, &rc);
5278 EDIT_UpdateText(es, NULL, TRUE);
5279 }
5280 }
5281
5282
5283 /*********************************************************************
5284 *
5285 * WM_STYLECHANGED
5286 *
5287 * This message is sent by SetWindowLong on having changed either the Style
5288 * or the extended style.
5289 *
5290 * We ensure that the window's version of the styles and the EDITSTATE's agree.
5291 *
5292 * See also EDIT_WM_NCCreate
5293 *
5294 * It appears that the Windows version of the edit control allows the style
5295 * (as retrieved by GetWindowLong) to be any value and maintains an internal
5296 * style variable which will generally be different. In this function we
5297 * update the internal style based on what changed in the externally visible
5298 * style.
5299 *
5300 * Much of this content as based upon the MSDN, especially:
5301 * Platform SDK Documentation -> User Interface Services ->
5302 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
5303 * Edit Control Styles
5304 */
5305 static LRESULT EDIT_WM_StyleChanged ( EDITSTATE *es, WPARAM which, const STYLESTRUCT *style)
5306 {
5307 if (GWL_STYLE == which) {
5308 DWORD style_change_mask;
5309 DWORD new_style;
5310 /* Only a subset of changes can be applied after the control
5311 * has been created.
5312 */
5313 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
5314 ES_NUMBER;
5315 if (es->style & ES_MULTILINE)
5316 style_change_mask |= ES_WANTRETURN;
5317
5318 new_style = style->styleNew & style_change_mask;
5319
5320 /* Number overrides lowercase overrides uppercase (at least it
5321 * does in Win95). However I'll bet that ES_NUMBER would be
5322 * invalid under Win 3.1.
5323 */
5324 if (new_style & ES_NUMBER) {
5325 ; /* do not override the ES_NUMBER */
5326 } else if (new_style & ES_LOWERCASE) {
5327 new_style &= ~ES_UPPERCASE;
5328 }
5329
5330 es->style = (es->style & ~style_change_mask) | new_style;
5331 } else if (GWL_EXSTYLE == which) {
5332 ; /* FIXME - what is needed here */
5333 } else {
5334 WARN ("Invalid style change %ld\n",which);
5335 }
5336
5337 return 0;
5338 }
5339
5340 /*********************************************************************
5341 *
5342 * WM_SYSKEYDOWN
5343 *
5344 */
5345 static LRESULT EDIT_WM_SysKeyDown(EDITSTATE *es, INT key, DWORD key_data)
5346 {
5347 if ((key == VK_BACK) && (key_data & 0x2000)) {
5348 if (EDIT_EM_CanUndo(es))
5349 EDIT_EM_Undo(es);
5350 return 0;
5351 } else if (key == VK_UP || key == VK_DOWN) {
5352 if (EDIT_CheckCombo(es, WM_SYSKEYDOWN, key))
5353 return 0;
5354 }
5355 return DefWindowProcW(es->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
5356 }
5357
5358
5359 /*********************************************************************
5360 *
5361 * WM_TIMER
5362 *
5363 */
5364 static void EDIT_WM_Timer(EDITSTATE *es)
5365 {
5366 if (es->region_posx < 0) {
5367 EDIT_MoveBackward(es, TRUE);
5368 } else if (es->region_posx > 0) {
5369 EDIT_MoveForward(es, TRUE);
5370 }
5371 /*
5372 * FIXME: gotta do some vertical scrolling here, like
5373 * EDIT_EM_LineScroll(hwnd, 0, 1);
5374 */
5375 }
5376
5377 /*********************************************************************
5378 *
5379 * WM_VSCROLL
5380 *
5381 */
5382 static LRESULT EDIT_WM_VScroll(EDITSTATE *es, INT action, INT pos)
5383 {
5384 INT dy;
5385
5386 if (!(es->style & ES_MULTILINE))
5387 return 0;
5388
5389 if (!(es->style & ES_AUTOVSCROLL))
5390 return 0;
5391
5392 dy = 0;
5393 switch (action) {
5394 case SB_LINEUP:
5395 case SB_LINEDOWN:
5396 case SB_PAGEUP:
5397 case SB_PAGEDOWN:
5398 TRACE("action %d (%s)\n", action, (action == SB_LINEUP ? "SB_LINEUP" :
5399 (action == SB_LINEDOWN ? "SB_LINEDOWN" :
5400 (action == SB_PAGEUP ? "SB_PAGEUP" :
5401 "SB_PAGEDOWN"))));
5402 EDIT_EM_Scroll(es, action);
5403 return 0;
5404 case SB_TOP:
5405 TRACE("SB_TOP\n");
5406 dy = -es->y_offset;
5407 break;
5408 case SB_BOTTOM:
5409 TRACE("SB_BOTTOM\n");
5410 dy = es->line_count - 1 - es->y_offset;
5411 break;
5412 case SB_THUMBTRACK:
5413 TRACE("SB_THUMBTRACK %d\n", pos);
5414 es->flags |= EF_VSCROLL_TRACK;
5415 if(es->style & WS_VSCROLL)
5416 dy = pos - es->y_offset;
5417 else
5418 {
5419 /* Assume default scroll range 0-100 */
5420 INT vlc, new_y;
5421 /* Sanity check */
5422 if(pos < 0 || pos > 100) return 0;
5423 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5424 new_y = pos * (es->line_count - vlc) / 100;
5425 dy = es->line_count ? (new_y - es->y_offset) : 0;
5426 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5427 es->line_count, es->y_offset, pos, dy);
5428 }
5429 break;
5430 case SB_THUMBPOSITION:
5431 TRACE("SB_THUMBPOSITION %d\n", pos);
5432 es->flags &= ~EF_VSCROLL_TRACK;
5433 if(es->style & WS_VSCROLL)
5434 dy = pos - es->y_offset;
5435 else
5436 {
5437 /* Assume default scroll range 0-100 */
5438 INT vlc, new_y;
5439 /* Sanity check */
5440 if(pos < 0 || pos > 100) return 0;
5441 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5442 new_y = pos * (es->line_count - vlc) / 100;
5443 dy = es->line_count ? (new_y - es->y_offset) : 0;
5444 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
5445 es->line_count, es->y_offset, pos, dy);
5446 }
5447 if (!dy)
5448 {
5449 /* force scroll info update */
5450 EDIT_UpdateScrollInfo(es);
5451 EDIT_NOTIFY_PARENT(es, EN_VSCROLL);
5452 }
5453 break;
5454 case SB_ENDSCROLL:
5455 TRACE("SB_ENDSCROLL\n");
5456 break;
5457 /*
5458 * FIXME : the next two are undocumented !
5459 * Are we doing the right thing ?
5460 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
5461 * although it's also a regular control message.
5462 */
5463 case EM_GETTHUMB: /* this one is used by NT notepad */
5464 #ifndef __REACTOS__
5465 case EM_GETTHUMB16:
5466 #endif
5467 {
5468 LRESULT ret;
5469 if(GetWindowLongW( es->hwndSelf, GWL_STYLE ) & WS_VSCROLL)
5470 ret = GetScrollPos(es->hwndSelf, SB_VERT);
5471 else
5472 {
5473 /* Assume default scroll range 0-100 */
5474 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
5475 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
5476 }
5477 TRACE("EM_GETTHUMB: returning %ld\n", ret);
5478 return ret;
5479 }
5480 #ifndef __REACTOS__
5481 case EM_LINESCROLL16:
5482 TRACE("EM_LINESCROLL16 %d\n", pos);
5483 dy = pos;
5484 break;
5485 #endif
5486
5487 default:
5488 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
5489 action, action);
5490 return 0;
5491 }
5492 if (dy)
5493 EDIT_EM_LineScroll(es, 0, dy);
5494 return 0;
5495 }
5496
5497 /*********************************************************************
5498 *
5499 * EDIT_UpdateText
5500 *
5501 */
5502 static void EDIT_UpdateTextRegion(EDITSTATE *es, HRGN hrgn, BOOL bErase)
5503 {
5504 if (es->flags & EF_UPDATE) {
5505 es->flags &= ~EF_UPDATE;
5506 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5507 }
5508 InvalidateRgn(es->hwndSelf, hrgn, bErase);
5509 }
5510
5511
5512 /*********************************************************************
5513 *
5514 * EDIT_UpdateText
5515 *
5516 */
5517 static void EDIT_UpdateText(EDITSTATE *es, LPRECT rc, BOOL bErase)
5518 {
5519 if (es->flags & EF_UPDATE) {
5520 es->flags &= ~EF_UPDATE;
5521 EDIT_NOTIFY_PARENT(es, EN_UPDATE);
5522 }
5523 NtUserInvalidateRect(es->hwndSelf, rc, bErase);
5524 }
5525
5526 /********************************************************************
5527 *
5528 * The Following code is to handle inline editing from IMEs
5529 */
5530
5531 static void EDIT_GetCompositionStr(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5532 {
5533 DWORD dwBufLen;
5534 LPWSTR lpCompStr = NULL;
5535 HIMC hIMC;
5536 LPSTR lpCompStrAttr = NULL;
5537 DWORD dwBufLenAttr;
5538
5539 if (!(hIMC = ImmGetContext(hwnd)))
5540 return;
5541
5542 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
5543
5544 if (dwBufLen <= 0)
5545 {
5546 ImmReleaseContext(hwnd, hIMC);
5547 return;
5548 }
5549
5550 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen);
5551 if (!lpCompStr)
5552 {
5553 ERR("Unable to allocate IME CompositionString\n");
5554 ImmReleaseContext(hwnd,hIMC);
5555 return;
5556 }
5557
5558 if (dwBufLen)
5559 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
5560 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
5561
5562 if (CompFlag & GCS_COMPATTR)
5563 {
5564 /*
5565 * We do not use the attributes yet. it would tell us what characters
5566 * are in transition and which are converted or decided upon
5567 */
5568 dwBufLenAttr = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
5569 if (dwBufLenAttr)
5570 {
5571 dwBufLenAttr ++;
5572 lpCompStrAttr = HeapAlloc(GetProcessHeap(),0,dwBufLenAttr);
5573 if (!lpCompStrAttr)
5574 {
5575 ERR("Unable to allocate IME Attribute String\n");
5576 HeapFree(GetProcessHeap(),0,lpCompStr);
5577 ImmReleaseContext(hwnd,hIMC);
5578 return;
5579 }
5580 ImmGetCompositionStringW(hIMC,GCS_COMPATTR, lpCompStrAttr,
5581 dwBufLenAttr);
5582 lpCompStrAttr[dwBufLenAttr] = 0;
5583 }
5584 else
5585 lpCompStrAttr = NULL;
5586 }
5587
5588 /* check for change in composition start */
5589 if (es->selection_end < es->composition_start)
5590 es->composition_start = es->selection_end;
5591
5592 /* replace existing selection string */
5593 es->selection_start = es->composition_start;
5594
5595 if (es->composition_len > 0)
5596 es->selection_end = es->composition_start + es->composition_len;
5597 else
5598 es->selection_end = es->selection_start;
5599
5600 EDIT_EM_ReplaceSel(es, FALSE, lpCompStr, TRUE, TRUE);
5601 es->composition_len = abs(es->composition_start - es->selection_end);
5602
5603 es->selection_start = es->composition_start;
5604 es->selection_end = es->selection_start + es->composition_len;
5605
5606 HeapFree(GetProcessHeap(),0,lpCompStrAttr);
5607 HeapFree(GetProcessHeap(),0,lpCompStr);
5608 ImmReleaseContext(hwnd,hIMC);
5609 }
5610
5611 static void EDIT_GetResultStr(HWND hwnd, EDITSTATE *es)
5612 {
5613 DWORD dwBufLen;
5614 LPWSTR lpResultStr;
5615 HIMC hIMC;
5616
5617 if ( !(hIMC = ImmGetContext(hwnd)))
5618 return;
5619
5620 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
5621 if (dwBufLen <= 0)
5622 {
5623 ImmReleaseContext(hwnd, hIMC);
5624 return;
5625 }
5626
5627 lpResultStr = HeapAlloc(GetProcessHeap(),0, dwBufLen);
5628 if (!lpResultStr)
5629 {
5630 ERR("Unable to alloc buffer for IME string\n");
5631 ImmReleaseContext(hwnd, hIMC);
5632 return;
5633 }
5634
5635 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpResultStr, dwBufLen);
5636 lpResultStr[dwBufLen/sizeof(WCHAR)] = 0;
5637
5638 /* check for change in composition start */
5639 if (es->selection_end < es->composition_start)
5640 es->composition_start = es->selection_end;
5641
5642 es->selection_start = es->composition_start;
5643 es->selection_end = es->composition_start + es->composition_len;
5644 EDIT_EM_ReplaceSel(es, TRUE, lpResultStr, TRUE, TRUE);
5645 es->composition_start = es->selection_end;
5646 es->composition_len = 0;
5647
5648 HeapFree(GetProcessHeap(),0,lpResultStr);
5649 ImmReleaseContext(hwnd, hIMC);
5650 }
5651
5652 static void EDIT_ImeComposition(HWND hwnd, LPARAM CompFlag, EDITSTATE *es)
5653 {
5654 if (CompFlag & GCS_RESULTSTR)
5655 EDIT_GetResultStr(hwnd,es);
5656 if (CompFlag & GCS_COMPSTR)
5657 EDIT_GetCompositionStr(hwnd, CompFlag, es);
5658 }