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