Rewrite GetCommState16 using GetCommState.
[wine/wine-kai.git] / controls / edit.c
blob717c7a9c4ca7d19214fecb3cd417d57c79300f1c
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
8 */
11 * please read EDIT.TODO (and update it when you change things)
14 #include "config.h"
16 #include <string.h>
17 #include <stdlib.h>
19 #include "winbase.h"
20 #include "winnt.h"
21 #include "win.h"
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
24 #include "wine/unicode.h"
25 #include "controls.h"
26 #include "local.h"
27 #include "user.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(edit);
31 DECLARE_DEBUG_CHANNEL(combo);
32 DECLARE_DEBUG_CHANNEL(relay);
34 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
35 FIXME: BTW, new specs say 65535 (do you dare ???) */
36 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
37 #define GROWLENGTH 32 /* buffers granularity in bytes: must be power of 2 */
38 #define ROUND_TO_GROW(size) (((size) + (GROWLENGTH - 1)) & ~(GROWLENGTH - 1))
39 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
42 * extra flags for EDITSTATE.flags field
44 #define EF_MODIFIED 0x0001 /* text has been modified */
45 #define EF_FOCUSED 0x0002 /* we have input focus */
46 #define EF_UPDATE 0x0004 /* notify parent of changed state */
47 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
48 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
49 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
50 wrapped line, instead of in front of the next character */
51 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
53 typedef enum
55 END_0 = 0, /* line ends with terminating '\0' character */
56 END_WRAP, /* line is wrapped */
57 END_HARD, /* line ends with a hard return '\r\n' */
58 END_SOFT /* line ends with a soft return '\r\r\n' */
59 } LINE_END;
61 typedef struct tagLINEDEF {
62 INT length; /* bruto length of a line in bytes */
63 INT net_length; /* netto length of a line in visible characters */
64 LINE_END ending;
65 INT width; /* width of the line in pixels */
66 INT index; /* line index into the buffer */
67 struct tagLINEDEF *next;
68 } LINEDEF;
70 typedef struct
72 BOOL is_unicode; /* how the control was created */
73 LPWSTR text; /* the actual contents of the control */
74 UINT buffer_size; /* the size of the buffer in characters */
75 UINT buffer_limit; /* the maximum size to which the buffer may grow in characters */
76 HFONT font; /* NULL means standard system font */
77 INT x_offset; /* scroll offset for multi lines this is in pixels
78 for single lines it's in characters */
79 INT line_height; /* height of a screen line in pixels */
80 INT char_width; /* average character width in pixels */
81 DWORD style; /* sane version of wnd->dwStyle */
82 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
83 INT undo_insert_count; /* number of characters inserted in sequence */
84 UINT undo_position; /* character index of the insertion and deletion */
85 LPWSTR undo_text; /* deleted text */
86 UINT undo_buffer_size; /* size of the deleted text buffer */
87 INT selection_start; /* == selection_end if no selection */
88 INT selection_end; /* == current caret position */
89 WCHAR password_char; /* == 0 if no password char, and for multi line controls */
90 INT left_margin; /* in pixels */
91 INT right_margin; /* in pixels */
92 RECT format_rect;
93 INT text_width; /* width of the widest line in pixels for multi line controls
94 and just line width for single line controls */
95 INT region_posx; /* Position of cursor relative to region: */
96 INT region_posy; /* -1: to left, 0: within, 1: to right */
97 EDITWORDBREAKPROC16 word_break_proc16;
98 void *word_break_proc; /* 32-bit word break proc: ANSI or Unicode */
99 INT line_count; /* number of lines */
100 INT y_offset; /* scroll offset in number of lines */
101 BOOL bCaptureState; /* flag indicating whether mouse was captured */
102 BOOL bEnableState; /* flag keeping the enable state */
103 HWND hwndParent; /* Handle of parent for sending EN_* messages.
104 Even if parent will change, EN_* messages
105 should be sent to the first parent. */
106 HWND hwndListBox; /* handle of ComboBox's listbox or NULL */
108 * only for multi line controls
110 INT lock_count; /* amount of re-entries in the EditWndProc */
111 INT tabs_count;
112 LPINT tabs;
113 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
114 HLOCAL hloc32W; /* our unicode local memory block */
115 HLOCAL16 hloc16; /* alias for 16-bit control receiving EM_GETHANDLE16
116 or EM_SETHANDLE16 */
117 HLOCAL hloc32A; /* alias for ANSI control receiving EM_GETHANDLE
118 or EM_SETHANDLE */
119 } EDITSTATE;
122 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
123 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
125 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
126 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
128 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
129 do {TRACE("notification " str " sent to hwnd=%08x\n", \
130 (UINT)(hwnd));} while(0)
132 /* used for disabled or read-only edit control */
133 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
134 (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
135 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
136 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
137 (SendMessageW((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
138 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
139 #define EDIT_NOTIFY_PARENT(es, wNotifyCode, str) \
140 do \
141 { /* Notify parent which has created this edit control */ \
142 DPRINTF_EDIT_NOTIFY((es)->hwndParent, str); \
143 SendMessageW((es)->hwndParent, WM_COMMAND, \
144 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
145 (LPARAM)(wnd)->hwndSelf); \
146 } while(0)
147 #define DPRINTF_EDIT_MSG16(str) \
148 TRACE(\
149 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
150 (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
151 #define DPRINTF_EDIT_MSG32(str) \
152 TRACE(\
153 "32 bit %c : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
154 unicode ? 'W' : 'A', \
155 (UINT)wnd->hwndSelf, (UINT)wParam, (UINT)lParam)
157 /*********************************************************************
159 * Declarations
164 * These functions have trivial implementations
165 * We still like to call them internally
166 * "static inline" makes them more like macro's
168 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es);
169 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es);
170 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
171 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
174 * Helper functions only valid for one type of control
176 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
177 static void EDIT_CalcLineWidth_SL(WND *wnd, EDITSTATE *es);
178 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
179 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
180 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
181 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
182 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
184 * Helper functions valid for both single line _and_ multi line controls
186 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action);
187 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
188 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y);
189 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
190 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
191 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
192 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size);
193 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
194 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
195 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
196 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
197 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
198 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
199 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
200 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
201 static INT EDIT_PaintText(EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
202 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
203 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
204 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
205 static void EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es);
206 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action);
208 * EM_XXX message handlers
210 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
211 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol);
212 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es);
213 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
214 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode);
215 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end);
216 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
217 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index);
218 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line);
219 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index);
220 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
221 static BOOL EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy);
222 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
223 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
224 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
225 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
226 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
227 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
228 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit);
229 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action, INT left, INT right);
230 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c);
231 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
232 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs);
233 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs);
234 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam);
235 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
236 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
238 * WM_XXX message handlers
240 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c);
241 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
242 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y);
243 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
244 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name);
245 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
246 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
247 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
248 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
249 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key);
250 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es);
251 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es);
252 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
253 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
254 static LRESULT EDIT_WM_MButtonDown(WND *wnd);
255 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y);
256 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode);
257 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam);
258 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
259 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es);
260 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
261 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
262 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
263 static LRESULT EDIT_WM_StyleChanged (WND *wnd, EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
264 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
265 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es);
266 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos);
267 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase);
268 static void EDIT_UpdateTextRegion(WND *wnd, HRGN hrgn, BOOL bErase);
270 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
271 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
273 /*********************************************************************
274 * edit class descriptor
276 const struct builtin_class_descr EDIT_builtin_class =
278 "Edit", /* name */
279 CS_GLOBALCLASS | CS_DBLCLKS /*| CS_PARENTDC*/, /* style */
280 EditWndProcA, /* procA */
281 EditWndProcW, /* procW */
282 sizeof(EDITSTATE *), /* extra */
283 IDC_IBEAMA, /* cursor */
284 0 /* brush */
288 /*********************************************************************
290 * EM_CANUNDO
293 static inline BOOL EDIT_EM_CanUndo(EDITSTATE *es)
295 return (es->undo_insert_count || strlenW(es->undo_text));
299 /*********************************************************************
301 * EM_EMPTYUNDOBUFFER
304 static inline void EDIT_EM_EmptyUndoBuffer(EDITSTATE *es)
306 es->undo_insert_count = 0;
307 *es->undo_text = '\0';
311 /*********************************************************************
313 * WM_CLEAR
316 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
318 static const WCHAR empty_stringW[] = {0};
320 /* Protect read-only edit control from modification */
321 if(es->style & ES_READONLY)
322 return;
324 EDIT_EM_ReplaceSel(wnd, es, TRUE, empty_stringW, TRUE);
328 /*********************************************************************
330 * WM_CUT
333 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
335 EDIT_WM_Copy(wnd, es);
336 EDIT_WM_Clear(wnd, es);
340 /**********************************************************************
341 * get_app_version
343 * Returns the window version in case Wine emulates a later version
344 * of windows then the application expects.
346 * In a number of cases when windows runs an application that was
347 * designed for an earlier windows version, windows reverts
348 * to "old" behaviour of that earlier version.
350 * An example is a disabled edit control that needs to be painted.
351 * Old style behaviour is to send a WM_CTLCOLOREDIT message. This was
352 * changed in Win95, NT4.0 by a WM_CTLCOLORSTATIC message _only_ for
353 * applications with an expected version 0f 4.0 or higher.
356 static DWORD get_app_version(void)
358 static DWORD version;
359 if (!version)
361 DWORD dwEmulatedVersion;
362 OSVERSIONINFOW info;
363 DWORD dwProcVersion = GetProcessVersion(0);
365 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
366 GetVersionExW( &info );
367 dwEmulatedVersion = MAKELONG( info.dwMinorVersion, info.dwMajorVersion );
368 /* fixme: this may not be 100% correct; see discussion on the
369 * wine developer list in Nov 1999 */
370 version = dwProcVersion < dwEmulatedVersion ? dwProcVersion : dwEmulatedVersion;
372 return version;
376 /*********************************************************************
378 * EditWndProc_locked
380 * The messages are in the order of the actual integer values
381 * (which can be found in include/windows.h)
382 * Wherever possible the 16 bit versions are converted to
383 * the 32 bit ones, so that we can 'fall through' to the
384 * helper functions. These are mostly 32 bit (with a few
385 * exceptions, clearly indicated by a '16' extension to their
386 * names).
389 static LRESULT WINAPI EditWndProc_locked( WND *wnd, UINT msg,
390 WPARAM wParam, LPARAM lParam, BOOL unicode )
392 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
393 LRESULT result = 0;
395 switch (msg) {
396 case WM_DESTROY:
397 DPRINTF_EDIT_MSG32("WM_DESTROY");
398 EDIT_WM_Destroy(wnd, es);
399 result = 0;
400 goto END;
402 case WM_NCCREATE:
403 DPRINTF_EDIT_MSG32("WM_NCCREATE");
404 if(unicode)
406 LPCREATESTRUCTW cs = (LPCREATESTRUCTW)lParam;
407 result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, TRUE);
409 else
411 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
412 result = EDIT_WM_NCCreate(wnd, cs->style, cs->hwndParent, FALSE);
414 goto END;
417 if (!es)
419 if(unicode)
420 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
421 else
422 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
423 goto END;
427 EDIT_LockBuffer(wnd, es);
428 switch (msg) {
429 case EM_GETSEL16:
430 DPRINTF_EDIT_MSG16("EM_GETSEL");
431 wParam = 0;
432 lParam = 0;
433 /* fall through */
434 case EM_GETSEL:
435 DPRINTF_EDIT_MSG32("EM_GETSEL");
436 result = EDIT_EM_GetSel(es, (LPUINT)wParam, (LPUINT)lParam);
437 break;
439 case EM_SETSEL16:
440 DPRINTF_EDIT_MSG16("EM_SETSEL");
441 if (SLOWORD(lParam) == -1)
442 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
443 else
444 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
445 if (!wParam)
446 EDIT_EM_ScrollCaret(wnd, es);
447 result = 1;
448 break;
449 case EM_SETSEL:
450 DPRINTF_EDIT_MSG32("EM_SETSEL");
451 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
452 EDIT_EM_ScrollCaret(wnd, es);
453 result = 1;
454 break;
456 case EM_GETRECT16:
457 DPRINTF_EDIT_MSG16("EM_GETRECT");
458 if (lParam)
459 CONV_RECT32TO16(&es->format_rect, MapSL(lParam));
460 break;
461 case EM_GETRECT:
462 DPRINTF_EDIT_MSG32("EM_GETRECT");
463 if (lParam)
464 CopyRect((LPRECT)lParam, &es->format_rect);
465 break;
467 case EM_SETRECT16:
468 DPRINTF_EDIT_MSG16("EM_SETRECT");
469 if ((es->style & ES_MULTILINE) && lParam) {
470 RECT rc;
471 CONV_RECT16TO32(MapSL(lParam), &rc);
472 EDIT_SetRectNP(wnd, es, &rc);
473 EDIT_UpdateText(wnd, NULL, TRUE);
475 break;
476 case EM_SETRECT:
477 DPRINTF_EDIT_MSG32("EM_SETRECT");
478 if ((es->style & ES_MULTILINE) && lParam) {
479 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
480 EDIT_UpdateText(wnd, NULL, TRUE);
482 break;
484 case EM_SETRECTNP16:
485 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
486 if ((es->style & ES_MULTILINE) && lParam) {
487 RECT rc;
488 CONV_RECT16TO32(MapSL(lParam), &rc);
489 EDIT_SetRectNP(wnd, es, &rc);
491 break;
492 case EM_SETRECTNP:
493 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
494 if ((es->style & ES_MULTILINE) && lParam)
495 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
496 break;
498 case EM_SCROLL16:
499 DPRINTF_EDIT_MSG16("EM_SCROLL");
500 /* fall through */
501 case EM_SCROLL:
502 DPRINTF_EDIT_MSG32("EM_SCROLL");
503 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
504 break;
506 case EM_LINESCROLL16:
507 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
508 wParam = (WPARAM)(INT)SHIWORD(lParam);
509 lParam = (LPARAM)(INT)SLOWORD(lParam);
510 /* fall through */
511 case EM_LINESCROLL:
512 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
513 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
514 break;
516 case EM_SCROLLCARET16:
517 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
518 /* fall through */
519 case EM_SCROLLCARET:
520 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
521 EDIT_EM_ScrollCaret(wnd, es);
522 result = 1;
523 break;
525 case EM_GETMODIFY16:
526 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
527 /* fall through */
528 case EM_GETMODIFY:
529 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
530 result = ((es->flags & EF_MODIFIED) != 0);
531 break;
533 case EM_SETMODIFY16:
534 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
535 /* fall through */
536 case EM_SETMODIFY:
537 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
538 if (wParam)
539 es->flags |= EF_MODIFIED;
540 else
541 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
542 break;
544 case EM_GETLINECOUNT16:
545 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
546 /* fall through */
547 case EM_GETLINECOUNT:
548 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
549 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
550 break;
552 case EM_LINEINDEX16:
553 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
554 if ((INT16)wParam == -1)
555 wParam = (WPARAM)-1;
556 /* fall through */
557 case EM_LINEINDEX:
558 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
559 result = (LRESULT)EDIT_EM_LineIndex(es, (INT)wParam);
560 break;
562 case EM_SETHANDLE16:
563 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
564 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
565 break;
566 case EM_SETHANDLE:
567 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
568 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
569 break;
571 case EM_GETHANDLE16:
572 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
573 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
574 break;
575 case EM_GETHANDLE:
576 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
577 result = (LRESULT)EDIT_EM_GetHandle(es);
578 break;
580 case EM_GETTHUMB16:
581 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
582 /* fall through */
583 case EM_GETTHUMB:
584 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
585 result = EDIT_EM_GetThumb(wnd, es);
586 break;
588 /* messages 0x00bf and 0x00c0 missing from specs */
590 case WM_USER+15:
591 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
592 /* fall through */
593 case 0x00bf:
594 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
595 if(unicode)
596 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
597 else
598 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
599 break;
601 case WM_USER+16:
602 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
603 /* fall through */
604 case 0x00c0:
605 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
606 if(unicode)
607 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
608 else
609 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
610 break;
612 case EM_LINELENGTH16:
613 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
614 /* fall through */
615 case EM_LINELENGTH:
616 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
617 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
618 break;
620 case EM_REPLACESEL16:
621 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
622 lParam = (LPARAM)MapSL(lParam);
623 unicode = FALSE; /* 16-bit message is always ascii */
624 /* fall through */
625 case EM_REPLACESEL:
627 LPWSTR textW;
628 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
630 if(unicode)
631 textW = (LPWSTR)lParam;
632 else
634 LPSTR textA = (LPSTR)lParam;
635 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
636 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
637 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
640 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, textW, TRUE);
641 result = 1;
643 if(!unicode)
644 HeapFree(GetProcessHeap(), 0, textW);
645 break;
647 /* message 0x00c3 missing from specs */
649 case WM_USER+19:
650 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
651 /* fall through */
652 case 0x00c3:
653 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
654 if(unicode)
655 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
656 else
657 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
658 break;
660 case EM_GETLINE16:
661 DPRINTF_EDIT_MSG16("EM_GETLINE");
662 lParam = (LPARAM)MapSL(lParam);
663 unicode = FALSE; /* 16-bit message is always ascii */
664 /* fall through */
665 case EM_GETLINE:
666 DPRINTF_EDIT_MSG32("EM_GETLINE");
667 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
668 break;
670 case EM_LIMITTEXT16:
671 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
672 /* fall through */
673 case EM_SETLIMITTEXT:
674 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
675 EDIT_EM_SetLimitText(es, (INT)wParam);
676 break;
678 case EM_CANUNDO16:
679 DPRINTF_EDIT_MSG16("EM_CANUNDO");
680 /* fall through */
681 case EM_CANUNDO:
682 DPRINTF_EDIT_MSG32("EM_CANUNDO");
683 result = (LRESULT)EDIT_EM_CanUndo(es);
684 break;
686 case EM_UNDO16:
687 DPRINTF_EDIT_MSG16("EM_UNDO");
688 /* fall through */
689 case EM_UNDO:
690 /* fall through */
691 case WM_UNDO:
692 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
693 result = (LRESULT)EDIT_EM_Undo(wnd, es);
694 break;
696 case EM_FMTLINES16:
697 DPRINTF_EDIT_MSG16("EM_FMTLINES");
698 /* fall through */
699 case EM_FMTLINES:
700 DPRINTF_EDIT_MSG32("EM_FMTLINES");
701 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
702 break;
704 case EM_LINEFROMCHAR16:
705 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
706 /* fall through */
707 case EM_LINEFROMCHAR:
708 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
709 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
710 break;
712 /* message 0x00ca missing from specs */
714 case WM_USER+26:
715 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
716 /* fall through */
717 case 0x00ca:
718 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
719 if(unicode)
720 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
721 else
722 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
723 break;
725 case EM_SETTABSTOPS16:
726 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
727 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
728 break;
729 case EM_SETTABSTOPS:
730 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
731 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
732 break;
734 case EM_SETPASSWORDCHAR16:
735 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
736 unicode = FALSE; /* 16-bit message is always ascii */
737 /* fall through */
738 case EM_SETPASSWORDCHAR:
740 WCHAR charW = 0;
741 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
743 if(unicode)
744 charW = (WCHAR)wParam;
745 else
747 CHAR charA = wParam;
748 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
751 EDIT_EM_SetPasswordChar(wnd, es, charW);
752 break;
755 case EM_EMPTYUNDOBUFFER16:
756 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
757 /* fall through */
758 case EM_EMPTYUNDOBUFFER:
759 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
760 EDIT_EM_EmptyUndoBuffer(es);
761 break;
763 case EM_GETFIRSTVISIBLELINE16:
764 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
765 result = es->y_offset;
766 break;
767 case EM_GETFIRSTVISIBLELINE:
768 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
769 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
770 break;
772 case EM_SETREADONLY16:
773 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
774 /* fall through */
775 case EM_SETREADONLY:
776 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
777 if (wParam) {
778 wnd->dwStyle |= ES_READONLY;
779 es->style |= ES_READONLY;
780 } else {
781 wnd->dwStyle &= ~ES_READONLY;
782 es->style &= ~ES_READONLY;
784 result = 1;
785 break;
787 case EM_SETWORDBREAKPROC16:
788 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
789 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
790 break;
791 case EM_SETWORDBREAKPROC:
792 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
793 EDIT_EM_SetWordBreakProc(wnd, es, lParam);
794 break;
796 case EM_GETWORDBREAKPROC16:
797 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
798 result = (LRESULT)es->word_break_proc16;
799 break;
800 case EM_GETWORDBREAKPROC:
801 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
802 result = (LRESULT)es->word_break_proc;
803 break;
805 case EM_GETPASSWORDCHAR16:
806 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
807 unicode = FALSE; /* 16-bit message is always ascii */
808 /* fall through */
809 case EM_GETPASSWORDCHAR:
811 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
813 if(unicode)
814 result = es->password_char;
815 else
817 WCHAR charW = es->password_char;
818 CHAR charA = 0;
819 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
820 result = charA;
822 break;
825 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
827 case EM_SETMARGINS:
828 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
829 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
830 break;
832 case EM_GETMARGINS:
833 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
834 result = MAKELONG(es->left_margin, es->right_margin);
835 break;
837 case EM_GETLIMITTEXT:
838 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
839 result = es->buffer_limit;
840 break;
842 case EM_POSFROMCHAR:
843 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
844 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
845 break;
847 case EM_CHARFROMPOS:
848 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
849 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
850 break;
852 /* End of the EM_ messages which were in numerical order; what order
853 * are these in? vaguely alphabetical?
856 case WM_GETDLGCODE:
857 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
858 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
860 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
862 int vk = (int)((LPMSG)lParam)->wParam;
864 if ((wnd->dwStyle & ES_WANTRETURN) && vk == VK_RETURN)
866 result |= DLGC_WANTMESSAGE;
868 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
870 if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
871 result |= DLGC_WANTMESSAGE;
874 break;
876 case WM_CHAR:
878 WCHAR charW;
879 DPRINTF_EDIT_MSG32("WM_CHAR");
881 if(unicode)
882 charW = wParam;
883 else
885 CHAR charA = wParam;
886 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
889 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
891 if (SendMessageW(wnd->parent->hwndSelf, CB_GETDROPPEDSTATE, 0, 0))
892 SendMessageW(wnd->parent->hwndSelf, WM_KEYDOWN, charW, 0);
893 break;
895 EDIT_WM_Char(wnd, es, charW);
896 break;
899 case WM_CLEAR:
900 DPRINTF_EDIT_MSG32("WM_CLEAR");
901 EDIT_WM_Clear(wnd, es);
902 break;
904 case WM_COMMAND:
905 DPRINTF_EDIT_MSG32("WM_COMMAND");
906 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
907 break;
909 case WM_CONTEXTMENU:
910 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
911 EDIT_WM_ContextMenu(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
912 break;
914 case WM_COPY:
915 DPRINTF_EDIT_MSG32("WM_COPY");
916 EDIT_WM_Copy(wnd, es);
917 break;
919 case WM_CREATE:
920 DPRINTF_EDIT_MSG32("WM_CREATE");
921 if(unicode)
922 result = EDIT_WM_Create(wnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
923 else
925 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
926 LPWSTR nameW = NULL;
927 if(nameA)
929 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
930 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
931 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
933 result = EDIT_WM_Create(wnd, es, nameW);
934 if(nameW)
935 HeapFree(GetProcessHeap(), 0, nameW);
937 break;
939 case WM_CUT:
940 DPRINTF_EDIT_MSG32("WM_CUT");
941 EDIT_WM_Cut(wnd, es);
942 break;
944 case WM_ENABLE:
945 DPRINTF_EDIT_MSG32("WM_ENABLE");
946 es->bEnableState = (BOOL) wParam;
947 EDIT_UpdateText(wnd, NULL, TRUE);
948 break;
950 case WM_ERASEBKGND:
951 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
952 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
953 break;
955 case WM_GETFONT:
956 DPRINTF_EDIT_MSG32("WM_GETFONT");
957 result = (LRESULT)es->font;
958 break;
960 case WM_GETTEXT:
961 DPRINTF_EDIT_MSG32("WM_GETTEXT");
962 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
963 break;
965 case WM_GETTEXTLENGTH:
966 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
967 result = strlenW(es->text);
968 break;
970 case WM_HSCROLL:
971 DPRINTF_EDIT_MSG32("WM_HSCROLL");
972 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
973 break;
975 case WM_KEYDOWN:
976 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
977 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam);
978 break;
980 case WM_KILLFOCUS:
981 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
982 result = EDIT_WM_KillFocus(wnd, es);
983 break;
985 case WM_LBUTTONDBLCLK:
986 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
987 result = EDIT_WM_LButtonDblClk(wnd, es);
988 break;
990 case WM_LBUTTONDOWN:
991 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
992 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
993 break;
995 case WM_LBUTTONUP:
996 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
997 result = EDIT_WM_LButtonUp(wnd->hwndSelf, es);
998 break;
1000 case WM_MBUTTONDOWN:
1001 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
1002 result = EDIT_WM_MButtonDown(wnd);
1003 break;
1005 case WM_MOUSEACTIVATE:
1007 * FIXME: maybe DefWindowProc() screws up, but it seems that
1008 * modeless dialog boxes need this. If we don't do this, the focus
1009 * will _not_ be set by DefWindowProc() for edit controls in a
1010 * modeless dialog box ???
1012 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
1013 SetFocus(wnd->hwndSelf);
1014 result = MA_ACTIVATE;
1015 break;
1017 case WM_MOUSEMOVE:
1019 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1021 result = EDIT_WM_MouseMove(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
1022 break;
1024 case WM_PAINT:
1025 DPRINTF_EDIT_MSG32("WM_PAINT");
1026 EDIT_WM_Paint(wnd, es, wParam);
1027 break;
1029 case WM_PASTE:
1030 DPRINTF_EDIT_MSG32("WM_PASTE");
1031 EDIT_WM_Paste(wnd, es);
1032 break;
1034 case WM_SETFOCUS:
1035 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1036 EDIT_WM_SetFocus(wnd, es);
1037 break;
1039 case WM_SETFONT:
1040 DPRINTF_EDIT_MSG32("WM_SETFONT");
1041 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1042 break;
1044 case WM_SETREDRAW:
1045 /* FIXME: actually set an internal flag and behave accordingly */
1046 break;
1048 case WM_SETTEXT:
1049 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1050 EDIT_WM_SetText(wnd, es, lParam, unicode);
1051 result = TRUE;
1052 break;
1054 case WM_SIZE:
1055 DPRINTF_EDIT_MSG32("WM_SIZE");
1056 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1057 break;
1059 case WM_STYLECHANGED:
1060 DPRINTF_EDIT_MSG32("WM_STYLECHANGED");
1061 result = EDIT_WM_StyleChanged (wnd, es, wParam, (const STYLESTRUCT *)lParam);
1062 break;
1064 case WM_STYLECHANGING:
1065 DPRINTF_EDIT_MSG32("WM_STYLECHANGING");
1066 result = 0; /* See EDIT_WM_StyleChanged */
1067 break;
1069 case WM_SYSKEYDOWN:
1070 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1071 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
1072 break;
1074 case WM_TIMER:
1075 DPRINTF_EDIT_MSG32("WM_TIMER");
1076 EDIT_WM_Timer(wnd, es);
1077 break;
1079 case WM_VSCROLL:
1080 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1081 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam));
1082 break;
1084 case WM_MOUSEWHEEL:
1086 int gcWheelDelta = 0;
1087 UINT pulScrollLines = 3;
1088 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1090 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1091 if(unicode)
1092 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1093 else
1094 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1095 break;
1097 gcWheelDelta -= SHIWORD(wParam);
1098 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1100 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1101 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1102 result = EDIT_EM_LineScroll(wnd, es, 0, cLineScroll);
1105 break;
1106 default:
1107 if(unicode)
1108 result = DefWindowProcW(wnd->hwndSelf, msg, wParam, lParam);
1109 else
1110 result = DefWindowProcA(wnd->hwndSelf, msg, wParam, lParam);
1111 break;
1113 EDIT_UnlockBuffer(wnd, es, FALSE);
1114 END:
1115 return result;
1118 /*********************************************************************
1120 * EditWndProcW (USER32.@)
1122 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1124 LRESULT res;
1125 WND *wndPtr = WIN_FindWndPtr(hWnd);
1127 res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, TRUE);
1129 WIN_ReleaseWndPtr(wndPtr);
1130 return res;
1133 /*********************************************************************
1135 * EditWndProc (USER32.@)
1137 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1139 LRESULT res;
1140 WND *wndPtr = WIN_FindWndPtr(hWnd);
1142 res = EditWndProc_locked(wndPtr, uMsg, wParam, lParam, FALSE);
1144 WIN_ReleaseWndPtr(wndPtr);
1145 return res;
1148 /*********************************************************************
1150 * EDIT_BuildLineDefs_ML
1152 * Build linked list of text lines.
1153 * Lines can end with '\0' (last line), a character (if it is wrapped),
1154 * a soft return '\r\r\n' or a hard return '\r\n'
1157 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1159 HDC dc;
1160 HFONT old_font = 0;
1161 LPWSTR current_position, cp;
1162 INT fw;
1163 LINEDEF *current_line;
1164 LINEDEF *previous_line;
1165 LINEDEF *start_line;
1166 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1167 INT line_count = es->line_count;
1168 INT orig_net_length;
1169 RECT rc;
1171 if (istart == iend && delta == 0)
1172 return;
1174 dc = GetDC(wnd->hwndSelf);
1175 if (es->font)
1176 old_font = SelectObject(dc, es->font);
1178 previous_line = NULL;
1179 current_line = es->first_line_def;
1181 /* Find starting line. istart must lie inside an existing line or
1182 * at the end of buffer */
1183 do {
1184 if (istart < current_line->index + current_line->length ||
1185 current_line->ending == END_0)
1186 break;
1188 previous_line = current_line;
1189 current_line = current_line->next;
1190 line_index++;
1191 } while (current_line);
1193 if (!current_line) /* Error occurred start is not inside previous buffer */
1195 FIXME(" modification occurred outside buffer\n");
1196 return;
1199 /* Remember start of modifications in order to calculate update region */
1200 nstart_line = line_index;
1201 nstart_index = current_line->index;
1203 /* We must start to reformat from the previous line since the modifications
1204 * may have caused the line to wrap upwards. */
1205 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1207 line_index--;
1208 current_line = previous_line;
1210 start_line = current_line;
1212 fw = es->format_rect.right - es->format_rect.left;
1213 current_position = es->text + current_line->index;
1214 do {
1215 if (current_line != start_line)
1217 if (!current_line || current_line->index + delta > current_position - es->text)
1219 /* The buffer has been expanded, create a new line and
1220 insert it into the link list */
1221 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1222 new_line->next = previous_line->next;
1223 previous_line->next = new_line;
1224 current_line = new_line;
1225 es->line_count++;
1227 else if (current_line->index + delta < current_position - es->text)
1229 /* The previous line merged with this line so we delete this extra entry */
1230 previous_line->next = current_line->next;
1231 HeapFree(GetProcessHeap(), 0, current_line);
1232 current_line = previous_line->next;
1233 es->line_count--;
1234 continue;
1236 else /* current_line->index + delta == current_position */
1238 if (current_position - es->text > iend)
1239 break; /* We reached end of line modifications */
1240 /* else recalulate this line */
1244 current_line->index = current_position - es->text;
1245 orig_net_length = current_line->net_length;
1247 /* Find end of line */
1248 cp = current_position;
1249 while (*cp) {
1250 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1251 break;
1252 cp++;
1255 /* Mark type of line termination */
1256 if (!(*cp)) {
1257 current_line->ending = END_0;
1258 current_line->net_length = strlenW(current_position);
1259 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1260 current_line->ending = END_SOFT;
1261 current_line->net_length = cp - current_position - 1;
1262 } else {
1263 current_line->ending = END_HARD;
1264 current_line->net_length = cp - current_position;
1267 /* Calculate line width */
1268 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1269 current_position, current_line->net_length,
1270 es->tabs_count, es->tabs));
1272 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1273 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1274 INT next = 0;
1275 INT prev;
1276 do {
1277 prev = next;
1278 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1279 prev + 1, current_line->net_length, WB_RIGHT);
1280 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1281 current_position, next, es->tabs_count, es->tabs));
1282 } while (current_line->width <= fw);
1283 if (!prev) { /* Didn't find a line break so force a break */
1284 next = 0;
1285 do {
1286 prev = next;
1287 next++;
1288 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1289 current_position, next, es->tabs_count, es->tabs));
1290 } while (current_line->width <= fw);
1291 if (!prev)
1292 prev = 1;
1295 /* If the first line we are calculating, wrapped before istart, we must
1296 * adjust istart in order for this to be reflected in the update region. */
1297 if (current_line->index == nstart_index && istart > current_line->index + prev)
1298 istart = current_line->index + prev;
1299 /* else if we are updating the previous line before the first line we
1300 * are re-caulculating and it expanded */
1301 else if (current_line == start_line &&
1302 current_line->index != nstart_index && orig_net_length < prev)
1304 /* Line expanded due to an upwards line wrap so we must partially include
1305 * previous line in update region */
1306 nstart_line = line_index;
1307 nstart_index = current_line->index;
1308 istart = current_line->index + orig_net_length;
1311 current_line->net_length = prev;
1312 current_line->ending = END_WRAP;
1313 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1314 current_line->net_length, es->tabs_count, es->tabs));
1318 /* Adjust length to include line termination */
1319 switch (current_line->ending) {
1320 case END_SOFT:
1321 current_line->length = current_line->net_length + 3;
1322 break;
1323 case END_HARD:
1324 current_line->length = current_line->net_length + 2;
1325 break;
1326 case END_WRAP:
1327 case END_0:
1328 current_line->length = current_line->net_length;
1329 break;
1331 es->text_width = max(es->text_width, current_line->width);
1332 current_position += current_line->length;
1333 previous_line = current_line;
1334 current_line = current_line->next;
1335 line_index++;
1336 } while (previous_line->ending != END_0);
1338 /* Finish adjusting line index's by delta or remove hanging lines */
1339 if (previous_line->ending == END_0)
1341 LINEDEF *pnext = NULL;
1343 previous_line->next = NULL;
1344 while (current_line)
1346 pnext = current_line->next;
1347 HeapFree(GetProcessHeap(), 0, current_line);
1348 current_line = pnext;
1349 es->line_count--;
1352 else
1354 while (current_line)
1356 current_line->index += delta;
1357 current_line = current_line->next;
1361 /* Calculate rest of modification rectangle */
1362 if (hrgn)
1364 HRGN tmphrgn;
1366 * We calculate two rectangles. One for the first line which may have
1367 * an indent with respect to the format rect. The other is a format-width
1368 * rectangle that spans the rest of the lines that changed or moved.
1370 rc.top = es->format_rect.top + nstart_line * es->line_height -
1371 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1372 rc.bottom = rc.top + es->line_height;
1373 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1374 es->text + nstart_index, istart - nstart_index,
1375 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1376 rc.right = es->format_rect.right;
1377 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1379 rc.top = rc.bottom;
1380 rc.left = es->format_rect.left;
1381 rc.right = es->format_rect.right;
1383 * If lines were added or removed we must re-paint the remainder of the
1384 * lines since the remaining lines were either shifted up or down.
1386 if (line_count < es->line_count) /* We added lines */
1387 rc.bottom = es->line_count * es->line_height;
1388 else if (line_count > es->line_count) /* We removed lines */
1389 rc.bottom = line_count * es->line_height;
1390 else
1391 rc.bottom = line_index * es->line_height;
1392 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1393 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1394 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1395 DeleteObject(tmphrgn);
1398 if (es->font)
1399 SelectObject(dc, old_font);
1401 ReleaseDC(wnd->hwndSelf, dc);
1404 /*********************************************************************
1406 * EDIT_CalcLineWidth_SL
1409 static void EDIT_CalcLineWidth_SL(WND *wnd, EDITSTATE *es)
1411 es->text_width = SLOWORD(EDIT_EM_PosFromChar(wnd, es, strlenW(es->text), FALSE));
1414 /*********************************************************************
1416 * EDIT_CallWordBreakProc
1418 * Call appropriate WordBreakProc (internal or external).
1420 * Note: The "start" argument should always be an index refering
1421 * to es->text. The actual wordbreak proc might be
1422 * 16 bit, so we can't always pass any 32 bit LPSTR.
1423 * Hence we assume that es->text is the buffer that holds
1424 * the string under examination (we can decide this for ourselves).
1427 /* ### start build ### */
1428 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1429 /* ### stop build ### */
1430 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1432 INT ret, iWndsLocks;
1434 /* To avoid any deadlocks, all the locks on the windows structures
1435 must be suspended before the control is passed to the application */
1436 iWndsLocks = WIN_SuspendWndsLock();
1438 if (es->word_break_proc16) {
1439 HGLOBAL16 hglob16;
1440 SEGPTR segptr;
1441 INT countA;
1443 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1444 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1445 segptr = K32WOWGlobalLock16(hglob16);
1446 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1447 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1448 segptr, index, countA, action);
1449 GlobalUnlock16(hglob16);
1450 GlobalFree16(hglob16);
1452 else if (es->word_break_proc)
1454 if(es->is_unicode)
1456 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1458 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1459 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1460 ret = wbpW(es->text + start, index, count, action);
1462 else
1464 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1465 INT countA;
1466 CHAR *textA;
1468 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1469 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1470 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1471 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1472 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1473 ret = wbpA(textA, index, countA, action);
1474 HeapFree(GetProcessHeap(), 0, textA);
1477 else
1478 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1480 WIN_RestoreWndsLock(iWndsLocks);
1481 return ret;
1485 /*********************************************************************
1487 * EDIT_CharFromPos
1489 * Beware: This is not the function called on EM_CHARFROMPOS
1490 * The position _can_ be outside the formatting / client
1491 * rectangle
1492 * The return value is only the character index
1495 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1497 INT index;
1498 HDC dc;
1499 HFONT old_font = 0;
1501 if (es->style & ES_MULTILINE) {
1502 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1503 INT line_index = 0;
1504 LINEDEF *line_def = es->first_line_def;
1505 INT low, high;
1506 while ((line > 0) && line_def->next) {
1507 line_index += line_def->length;
1508 line_def = line_def->next;
1509 line--;
1511 x += es->x_offset - es->format_rect.left;
1512 if (x >= line_def->width) {
1513 if (after_wrap)
1514 *after_wrap = (line_def->ending == END_WRAP);
1515 return line_index + line_def->net_length;
1517 if (x <= 0) {
1518 if (after_wrap)
1519 *after_wrap = FALSE;
1520 return line_index;
1522 dc = GetDC(wnd->hwndSelf);
1523 if (es->font)
1524 old_font = SelectObject(dc, es->font);
1525 low = line_index + 1;
1526 high = line_index + line_def->net_length + 1;
1527 while (low < high - 1)
1529 INT mid = (low + high) / 2;
1530 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1531 else low = mid;
1533 index = low;
1535 if (after_wrap)
1536 *after_wrap = ((index == line_index + line_def->net_length) &&
1537 (line_def->ending == END_WRAP));
1538 } else {
1539 LPWSTR text;
1540 SIZE size;
1541 if (after_wrap)
1542 *after_wrap = FALSE;
1543 x -= es->format_rect.left;
1544 if (!x)
1545 return es->x_offset;
1546 text = EDIT_GetPasswordPointer_SL(es);
1547 dc = GetDC(wnd->hwndSelf);
1548 if (es->font)
1549 old_font = SelectObject(dc, es->font);
1550 if (x < 0)
1552 INT low = 0;
1553 INT high = es->x_offset;
1554 while (low < high - 1)
1556 INT mid = (low + high) / 2;
1557 GetTextExtentPoint32W( dc, text + mid,
1558 es->x_offset - mid, &size );
1559 if (size.cx > -x) low = mid;
1560 else high = mid;
1562 index = low;
1564 else
1566 INT low = es->x_offset;
1567 INT high = strlenW(es->text) + 1;
1568 while (low < high - 1)
1570 INT mid = (low + high) / 2;
1571 GetTextExtentPoint32W( dc, text + es->x_offset,
1572 mid - es->x_offset, &size );
1573 if (size.cx > x) high = mid;
1574 else low = mid;
1576 index = low;
1578 if (es->style & ES_PASSWORD)
1579 HeapFree(GetProcessHeap(), 0, text);
1581 if (es->font)
1582 SelectObject(dc, old_font);
1583 ReleaseDC(wnd->hwndSelf, dc);
1584 return index;
1588 /*********************************************************************
1590 * EDIT_ConfinePoint
1592 * adjusts the point to be within the formatting rectangle
1593 * (so CharFromPos returns the nearest _visible_ character)
1596 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1598 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1599 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1603 /*********************************************************************
1605 * EDIT_GetLineRect
1607 * Calculates the bounding rectangle for a line from a starting
1608 * column to an ending column.
1611 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1613 INT line_index = EDIT_EM_LineIndex(es, line);
1615 if (es->style & ES_MULTILINE)
1616 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1617 else
1618 rc->top = es->format_rect.top;
1619 rc->bottom = rc->top + es->line_height;
1620 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1621 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1625 /*********************************************************************
1627 * EDIT_GetPasswordPointer_SL
1629 * note: caller should free the (optionally) allocated buffer
1632 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1634 if (es->style & ES_PASSWORD) {
1635 INT len = strlenW(es->text);
1636 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1637 text[len] = '\0';
1638 while(len) text[--len] = es->password_char;
1639 return text;
1640 } else
1641 return es->text;
1645 /*********************************************************************
1647 * EDIT_LockBuffer
1649 * This acts as a LOCAL_Lock(), but it locks only once. This way
1650 * you can call it whenever you like, without unlocking.
1653 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1655 if (!es) {
1656 ERR("no EDITSTATE ... please report\n");
1657 return;
1659 if (!es->text) {
1660 CHAR *textA = NULL;
1661 UINT countA = 0;
1662 BOOL _16bit = FALSE;
1664 if(es->hloc32W)
1666 if(es->hloc32A)
1668 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1669 textA = LocalLock(es->hloc32A);
1670 countA = strlen(textA) + 1;
1672 else if(es->hloc16)
1674 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1675 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
1676 countA = strlen(textA) + 1;
1677 _16bit = TRUE;
1680 else {
1681 ERR("no buffer ... please report\n");
1682 return;
1685 if(textA)
1687 HLOCAL hloc32W_new;
1688 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1689 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1690 if(countW_new > es->buffer_size + 1)
1692 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1693 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1694 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1695 if(hloc32W_new)
1697 es->hloc32W = hloc32W_new;
1698 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1699 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1701 else
1702 WARN("FAILED! Will synchronize partially\n");
1706 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1707 es->text = LocalLock(es->hloc32W);
1709 if(textA)
1711 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1712 if(_16bit)
1713 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1714 else
1715 LocalUnlock(es->hloc32A);
1718 es->lock_count++;
1722 /*********************************************************************
1724 * EDIT_SL_InvalidateText
1726 * Called from EDIT_InvalidateText().
1727 * Does the job for single-line controls only.
1730 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1732 RECT line_rect;
1733 RECT rc;
1735 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1736 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1737 EDIT_UpdateText(wnd, &rc, FALSE);
1741 /*********************************************************************
1743 * EDIT_ML_InvalidateText
1745 * Called from EDIT_InvalidateText().
1746 * Does the job for multi-line controls only.
1749 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1751 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1752 INT sl = EDIT_EM_LineFromChar(es, start);
1753 INT el = EDIT_EM_LineFromChar(es, end);
1754 INT sc;
1755 INT ec;
1756 RECT rc1;
1757 RECT rcWnd;
1758 RECT rcLine;
1759 RECT rcUpdate;
1760 INT l;
1762 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1763 return;
1765 sc = start - EDIT_EM_LineIndex(es, sl);
1766 ec = end - EDIT_EM_LineIndex(es, el);
1767 if (sl < es->y_offset) {
1768 sl = es->y_offset;
1769 sc = 0;
1771 if (el > es->y_offset + vlc) {
1772 el = es->y_offset + vlc;
1773 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1775 GetClientRect(wnd->hwndSelf, &rc1);
1776 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1777 if (sl == el) {
1778 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1779 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1780 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1781 } else {
1782 EDIT_GetLineRect(wnd, es, sl, sc,
1783 EDIT_EM_LineLength(es,
1784 EDIT_EM_LineIndex(es, sl)),
1785 &rcLine);
1786 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1787 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1788 for (l = sl + 1 ; l < el ; l++) {
1789 EDIT_GetLineRect(wnd, es, l, 0,
1790 EDIT_EM_LineLength(es,
1791 EDIT_EM_LineIndex(es, l)),
1792 &rcLine);
1793 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1794 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1796 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1797 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1798 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1803 /*********************************************************************
1805 * EDIT_InvalidateText
1807 * Invalidate the text from offset start upto, but not including,
1808 * offset end. Useful for (re)painting the selection.
1809 * Regions outside the linewidth are not invalidated.
1810 * end == -1 means end == TextLength.
1811 * start and end need not be ordered.
1814 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1816 if (end == start)
1817 return;
1819 if (end == -1)
1820 end = strlenW(es->text);
1822 ORDER_INT(start, end);
1824 if (es->style & ES_MULTILINE)
1825 EDIT_ML_InvalidateText(wnd, es, start, end);
1826 else
1827 EDIT_SL_InvalidateText(wnd, es, start, end);
1831 /*********************************************************************
1833 * EDIT_MakeFit
1835 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1838 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, UINT size)
1840 HLOCAL hNew32W;
1842 if (size <= es->buffer_size)
1843 return TRUE;
1844 if (size > es->buffer_limit) {
1845 EDIT_NOTIFY_PARENT(es, EN_MAXTEXT, "EN_MAXTEXT");
1846 return FALSE;
1848 if (size > es->buffer_limit)
1849 size = es->buffer_limit;
1851 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1853 /* Force edit to unlock it's buffer. es->text now NULL */
1854 EDIT_UnlockBuffer(wnd, es, TRUE);
1856 if (es->hloc32W) {
1857 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1858 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1859 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1860 es->hloc32W = hNew32W;
1861 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1865 EDIT_LockBuffer(wnd, es);
1867 if (es->buffer_size < size) {
1868 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1869 EDIT_NOTIFY_PARENT(es, EN_ERRSPACE, "EN_ERRSPACE");
1870 return FALSE;
1871 } else {
1872 TRACE("We now have %d+1\n", es->buffer_size);
1873 return TRUE;
1878 /*********************************************************************
1880 * EDIT_MakeUndoFit
1882 * Try to fit size + 1 bytes in the undo buffer.
1885 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1887 UINT alloc_size;
1889 if (size <= es->undo_buffer_size)
1890 return TRUE;
1892 TRACE("trying to ReAlloc to %d+1\n", size);
1894 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1895 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1896 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1897 return TRUE;
1899 else
1901 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1902 return FALSE;
1907 /*********************************************************************
1909 * EDIT_MoveBackward
1912 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1914 INT e = es->selection_end;
1916 if (e) {
1917 e--;
1918 if ((es->style & ES_MULTILINE) && e &&
1919 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1920 e--;
1921 if (e && (es->text[e - 1] == '\r'))
1922 e--;
1925 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1926 EDIT_EM_ScrollCaret(wnd, es);
1930 /*********************************************************************
1932 * EDIT_MoveDown_ML
1934 * Only for multi line controls
1935 * Move the caret one line down, on a column with the nearest
1936 * x coordinate on the screen (might be a different column).
1939 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1941 INT s = es->selection_start;
1942 INT e = es->selection_end;
1943 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1944 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1945 INT x = SLOWORD(pos);
1946 INT y = SHIWORD(pos);
1948 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1949 if (!extend)
1950 s = e;
1951 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1952 EDIT_EM_ScrollCaret(wnd, es);
1956 /*********************************************************************
1958 * EDIT_MoveEnd
1961 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1963 BOOL after_wrap = FALSE;
1964 INT e;
1966 /* Pass a high value in x to make sure of receiving the end of the line */
1967 if (es->style & ES_MULTILINE)
1968 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1969 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1970 else
1971 e = strlenW(es->text);
1972 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1973 EDIT_EM_ScrollCaret(wnd, es);
1977 /*********************************************************************
1979 * EDIT_MoveForward
1982 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1984 INT e = es->selection_end;
1986 if (es->text[e]) {
1987 e++;
1988 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1989 if (es->text[e] == '\n')
1990 e++;
1991 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1992 e += 2;
1995 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1996 EDIT_EM_ScrollCaret(wnd, es);
2000 /*********************************************************************
2002 * EDIT_MoveHome
2004 * Home key: move to beginning of line.
2007 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
2009 INT e;
2011 /* Pass the x_offset in x to make sure of receiving the first position of the line */
2012 if (es->style & ES_MULTILINE)
2013 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
2014 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
2015 else
2016 e = 0;
2017 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
2018 EDIT_EM_ScrollCaret(wnd, es);
2022 /*********************************************************************
2024 * EDIT_MovePageDown_ML
2026 * Only for multi line controls
2027 * Move the caret one page down, on a column with the nearest
2028 * x coordinate on the screen (might be a different column).
2031 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
2033 INT s = es->selection_start;
2034 INT e = es->selection_end;
2035 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2036 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
2037 INT x = SLOWORD(pos);
2038 INT y = SHIWORD(pos);
2040 e = EDIT_CharFromPos(wnd, es, x,
2041 y + (es->format_rect.bottom - es->format_rect.top),
2042 &after_wrap);
2043 if (!extend)
2044 s = e;
2045 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
2046 EDIT_EM_ScrollCaret(wnd, es);
2050 /*********************************************************************
2052 * EDIT_MovePageUp_ML
2054 * Only for multi line controls
2055 * Move the caret one page up, on a column with the nearest
2056 * x coordinate on the screen (might be a different column).
2059 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
2061 INT s = es->selection_start;
2062 INT e = es->selection_end;
2063 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2064 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
2065 INT x = SLOWORD(pos);
2066 INT y = SHIWORD(pos);
2068 e = EDIT_CharFromPos(wnd, es, x,
2069 y - (es->format_rect.bottom - es->format_rect.top),
2070 &after_wrap);
2071 if (!extend)
2072 s = e;
2073 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
2074 EDIT_EM_ScrollCaret(wnd, es);
2078 /*********************************************************************
2080 * EDIT_MoveUp_ML
2082 * Only for multi line controls
2083 * Move the caret one line up, on a column with the nearest
2084 * x coordinate on the screen (might be a different column).
2087 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
2089 INT s = es->selection_start;
2090 INT e = es->selection_end;
2091 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2092 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
2093 INT x = SLOWORD(pos);
2094 INT y = SHIWORD(pos);
2096 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
2097 if (!extend)
2098 s = e;
2099 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
2100 EDIT_EM_ScrollCaret(wnd, es);
2104 /*********************************************************************
2106 * EDIT_MoveWordBackward
2109 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
2111 INT s = es->selection_start;
2112 INT e = es->selection_end;
2113 INT l;
2114 INT ll;
2115 INT li;
2117 l = EDIT_EM_LineFromChar(es, e);
2118 ll = EDIT_EM_LineLength(es, e);
2119 li = EDIT_EM_LineIndex(es, l);
2120 if (e - li == 0) {
2121 if (l) {
2122 li = EDIT_EM_LineIndex(es, l - 1);
2123 e = li + EDIT_EM_LineLength(es, li);
2125 } else {
2126 e = li + (INT)EDIT_CallWordBreakProc(es,
2127 li, e - li, ll, WB_LEFT);
2129 if (!extend)
2130 s = e;
2131 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
2132 EDIT_EM_ScrollCaret(wnd, es);
2136 /*********************************************************************
2138 * EDIT_MoveWordForward
2141 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
2143 INT s = es->selection_start;
2144 INT e = es->selection_end;
2145 INT l;
2146 INT ll;
2147 INT li;
2149 l = EDIT_EM_LineFromChar(es, e);
2150 ll = EDIT_EM_LineLength(es, e);
2151 li = EDIT_EM_LineIndex(es, l);
2152 if (e - li == ll) {
2153 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2154 e = EDIT_EM_LineIndex(es, l + 1);
2155 } else {
2156 e = li + EDIT_CallWordBreakProc(es,
2157 li, e - li + 1, ll, WB_RIGHT);
2159 if (!extend)
2160 s = e;
2161 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
2162 EDIT_EM_ScrollCaret(wnd, es);
2166 /*********************************************************************
2168 * EDIT_PaintLine
2171 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
2173 INT s = es->selection_start;
2174 INT e = es->selection_end;
2175 INT li;
2176 INT ll;
2177 INT x;
2178 INT y;
2179 LRESULT pos;
2181 if (es->style & ES_MULTILINE) {
2182 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2183 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2184 return;
2185 } else if (line)
2186 return;
2188 TRACE("line=%d\n", line);
2190 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(es, line), FALSE);
2191 x = SLOWORD(pos);
2192 y = SHIWORD(pos);
2193 li = EDIT_EM_LineIndex(es, line);
2194 ll = EDIT_EM_LineLength(es, li);
2195 s = es->selection_start;
2196 e = es->selection_end;
2197 ORDER_INT(s, e);
2198 s = min(li + ll, max(li, s));
2199 e = min(li + ll, max(li, e));
2200 if (rev && (s != e) &&
2201 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2202 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2203 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2204 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2205 } else
2206 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2210 /*********************************************************************
2212 * EDIT_PaintText
2215 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2217 COLORREF BkColor;
2218 COLORREF TextColor;
2219 INT ret;
2220 INT li;
2221 INT BkMode;
2222 SIZE size;
2224 if (!count)
2225 return 0;
2226 BkMode = GetBkMode(dc);
2227 BkColor = GetBkColor(dc);
2228 TextColor = GetTextColor(dc);
2229 if (rev) {
2230 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2231 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2232 SetBkMode( dc, OPAQUE);
2234 li = EDIT_EM_LineIndex(es, line);
2235 if (es->style & ES_MULTILINE) {
2236 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2237 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2238 } else {
2239 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2240 TextOutW(dc, x, y, text + li + col, count);
2241 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2242 ret = size.cx;
2243 if (es->style & ES_PASSWORD)
2244 HeapFree(GetProcessHeap(), 0, text);
2246 if (rev) {
2247 SetBkColor(dc, BkColor);
2248 SetTextColor(dc, TextColor);
2249 SetBkMode( dc, BkMode);
2251 return ret;
2255 /*********************************************************************
2257 * EDIT_SetCaretPos
2260 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
2261 BOOL after_wrap)
2263 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
2264 SetCaretPos(SLOWORD(res), SHIWORD(res));
2268 /*********************************************************************
2270 * EDIT_SetRectNP
2272 * note: this is not (exactly) the handler called on EM_SETRECTNP
2273 * it is also used to set the rect of a single line control
2276 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
2278 CopyRect(&es->format_rect, rc);
2279 if (es->style & WS_BORDER) {
2280 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2281 if(TWEAK_WineLook == WIN31_LOOK)
2282 bw += 2;
2283 es->format_rect.left += bw;
2284 es->format_rect.top += bw;
2285 es->format_rect.right -= bw;
2286 es->format_rect.bottom -= bw;
2288 es->format_rect.left += es->left_margin;
2289 es->format_rect.right -= es->right_margin;
2290 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2291 if (es->style & ES_MULTILINE)
2293 INT fw, vlc, max_x_offset, max_y_offset;
2295 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2296 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2298 /* correct es->x_offset */
2299 fw = es->format_rect.right - es->format_rect.left;
2300 max_x_offset = es->text_width - fw;
2301 if(max_x_offset < 0) max_x_offset = 0;
2302 if(es->x_offset > max_x_offset)
2303 es->x_offset = max_x_offset;
2305 /* correct es->y_offset */
2306 max_y_offset = es->line_count - vlc;
2307 if(max_y_offset < 0) max_y_offset = 0;
2308 if(es->y_offset > max_y_offset)
2309 es->y_offset = max_y_offset;
2311 /* force scroll info update */
2312 EDIT_UpdateScrollInfo(wnd, es);
2314 else
2315 /* Windows doesn't care to fix text placement for SL controls */
2316 es->format_rect.bottom = es->format_rect.top + es->line_height;
2318 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2319 EDIT_BuildLineDefs_ML(wnd, es, 0, strlenW(es->text), 0, (HRGN)0);
2323 /*********************************************************************
2325 * EDIT_UnlockBuffer
2328 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
2330 /* Edit window might be already destroyed */
2331 if(!IsWindow(wnd->hwndSelf))
2333 WARN("edit wnd %04x already destroyed\n", wnd->hwndSelf);
2334 return;
2337 if (!es) {
2338 ERR("no EDITSTATE ... please report\n");
2339 return;
2341 if (!es->lock_count) {
2342 ERR("lock_count == 0 ... please report\n");
2343 return;
2345 if (!es->text) {
2346 ERR("es->text == 0 ... please report\n");
2347 return;
2350 if (force || (es->lock_count == 1)) {
2351 if (es->hloc32W) {
2352 CHAR *textA = NULL;
2353 BOOL _16bit = FALSE;
2354 UINT countA = 0;
2355 UINT countW = strlenW(es->text) + 1;
2357 if(es->hloc32A)
2359 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2360 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2361 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2362 countA = LocalSize(es->hloc32A);
2363 if(countA_new > countA)
2365 HLOCAL hloc32A_new;
2366 UINT alloc_size = ROUND_TO_GROW(countA_new);
2367 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2368 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2369 if(hloc32A_new)
2371 es->hloc32A = hloc32A_new;
2372 countA = LocalSize(hloc32A_new);
2373 TRACE("Real new size %d bytes\n", countA);
2375 else
2376 WARN("FAILED! Will synchronize partially\n");
2378 textA = LocalLock(es->hloc32A);
2380 else if(es->hloc16)
2382 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2383 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2384 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2385 countA = LOCAL_Size(wnd->hInstance, es->hloc16);
2386 if(countA_new > countA)
2388 HLOCAL16 hloc16_new;
2389 UINT alloc_size = ROUND_TO_GROW(countA_new);
2390 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2391 hloc16_new = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2392 if(hloc16_new)
2394 es->hloc16 = hloc16_new;
2395 countA = LOCAL_Size(wnd->hInstance, hloc16_new);
2396 TRACE("Real new size %d bytes\n", countA);
2398 else
2399 WARN("FAILED! Will synchronize partially\n");
2401 textA = LOCAL_Lock(wnd->hInstance, es->hloc16);
2402 _16bit = TRUE;
2405 if(textA)
2407 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2408 if(_16bit)
2409 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2410 else
2411 LocalUnlock(es->hloc32A);
2414 LocalUnlock(es->hloc32W);
2415 es->text = NULL;
2417 else {
2418 ERR("no buffer ... please report\n");
2419 return;
2422 es->lock_count--;
2426 /*********************************************************************
2428 * EDIT_UpdateScrollInfo
2431 static void EDIT_UpdateScrollInfo(WND *wnd, EDITSTATE *es)
2433 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2435 SCROLLINFO si;
2436 si.cbSize = sizeof(SCROLLINFO);
2437 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2438 si.nMin = 0;
2439 si.nMax = es->line_count - 1;
2440 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2441 si.nPos = es->y_offset;
2442 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2443 si.nMin, si.nMax, si.nPage, si.nPos);
2444 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
2447 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2449 SCROLLINFO si;
2450 si.cbSize = sizeof(SCROLLINFO);
2451 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2452 si.nMin = 0;
2453 si.nMax = es->text_width - 1;
2454 si.nPage = es->format_rect.right - es->format_rect.left;
2455 si.nPos = es->x_offset;
2456 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2457 si.nMin, si.nMax, si.nPage, si.nPos);
2458 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
2462 /*********************************************************************
2464 * EDIT_WordBreakProc
2466 * Find the beginning of words.
2467 * Note: unlike the specs for a WordBreakProc, this function only
2468 * allows to be called without linebreaks between s[0] upto
2469 * s[count - 1]. Remember it is only called
2470 * internally, so we can decide this for ourselves.
2473 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2475 INT ret = 0;
2477 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2479 if(!s) return 0;
2481 switch (action) {
2482 case WB_LEFT:
2483 if (!count)
2484 break;
2485 if (index)
2486 index--;
2487 if (s[index] == ' ') {
2488 while (index && (s[index] == ' '))
2489 index--;
2490 if (index) {
2491 while (index && (s[index] != ' '))
2492 index--;
2493 if (s[index] == ' ')
2494 index++;
2496 } else {
2497 while (index && (s[index] != ' '))
2498 index--;
2499 if (s[index] == ' ')
2500 index++;
2502 ret = index;
2503 break;
2504 case WB_RIGHT:
2505 if (!count)
2506 break;
2507 if (index)
2508 index--;
2509 if (s[index] == ' ')
2510 while ((index < count) && (s[index] == ' ')) index++;
2511 else {
2512 while (s[index] && (s[index] != ' ') && (index < count))
2513 index++;
2514 while ((s[index] == ' ') && (index < count)) index++;
2516 ret = index;
2517 break;
2518 case WB_ISDELIMITER:
2519 ret = (s[index] == ' ');
2520 break;
2521 default:
2522 ERR("unknown action code, please report !\n");
2523 break;
2525 return ret;
2529 /*********************************************************************
2531 * EM_CHARFROMPOS
2533 * returns line number (not index) in high-order word of result.
2534 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2535 * to Richedit, not to the edit control. Original documentation is valid.
2536 * FIXME: do the specs mean to return -1 if outside client area or
2537 * if outside formatting rectangle ???
2540 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
2542 POINT pt;
2543 RECT rc;
2544 INT index;
2546 pt.x = x;
2547 pt.y = y;
2548 GetClientRect(wnd->hwndSelf, &rc);
2549 if (!PtInRect(&rc, pt))
2550 return -1;
2552 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
2553 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2557 /*********************************************************************
2559 * EM_FMTLINES
2561 * Enable or disable soft breaks.
2563 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2565 es->flags &= ~EF_USE_SOFTBRK;
2566 if (add_eol) {
2567 es->flags |= EF_USE_SOFTBRK;
2568 FIXME("soft break enabled, not implemented\n");
2570 return add_eol;
2574 /*********************************************************************
2576 * EM_GETHANDLE
2578 * Hopefully this won't fire back at us.
2579 * We always start with a fixed buffer in the local heap.
2580 * Despite of the documentation says that the local heap is used
2581 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2582 * buffer on the local heap.
2585 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2587 HLOCAL hLocal;
2589 if (!(es->style & ES_MULTILINE))
2590 return 0;
2592 if(es->is_unicode)
2593 hLocal = es->hloc32W;
2594 else
2596 if(!es->hloc32A)
2598 CHAR *textA;
2599 UINT countA, alloc_size;
2600 TRACE("Allocating 32-bit ANSI alias buffer\n");
2601 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2602 alloc_size = ROUND_TO_GROW(countA);
2603 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2605 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2606 return 0;
2608 textA = LocalLock(es->hloc32A);
2609 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2610 LocalUnlock(es->hloc32A);
2612 hLocal = es->hloc32A;
2615 TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2616 return hLocal;
2620 /*********************************************************************
2622 * EM_GETHANDLE16
2624 * Hopefully this won't fire back at us.
2625 * We always start with a buffer in 32 bit linear memory.
2626 * However, with this message a 16 bit application requests
2627 * a handle of 16 bit local heap memory, where it expects to find
2628 * the text.
2629 * It's a pitty that from this moment on we have to use this
2630 * local heap, because applications may rely on the handle
2631 * in the future.
2633 * In this function we'll try to switch to local heap.
2635 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2637 CHAR *textA;
2638 UINT countA, alloc_size;
2640 if (!(es->style & ES_MULTILINE))
2641 return 0;
2643 if (es->hloc16)
2644 return es->hloc16;
2646 if (!LOCAL_HeapSize(wnd->hInstance)) {
2647 if (!LocalInit16(wnd->hInstance, 0,
2648 GlobalSize16(wnd->hInstance))) {
2649 ERR("could not initialize local heap\n");
2650 return 0;
2652 TRACE("local heap initialized\n");
2655 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2656 alloc_size = ROUND_TO_GROW(countA);
2658 TRACE("Allocating 16-bit ANSI alias buffer\n");
2659 if (!(es->hloc16 = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2660 ERR("could not allocate new 16 bit buffer\n");
2661 return 0;
2664 if (!(textA = (LPSTR)LOCAL_Lock(wnd->hInstance, es->hloc16))) {
2665 ERR("could not lock new 16 bit buffer\n");
2666 LOCAL_Free(wnd->hInstance, es->hloc16);
2667 es->hloc16 = 0;
2668 return 0;
2671 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2672 LOCAL_Unlock(wnd->hInstance, es->hloc16);
2674 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(wnd->hInstance, es->hloc16));
2675 return es->hloc16;
2679 /*********************************************************************
2681 * EM_GETLINE
2684 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2686 LPWSTR src;
2687 INT line_len, dst_len;
2688 INT i;
2690 if (es->style & ES_MULTILINE) {
2691 if (line >= es->line_count)
2692 return 0;
2693 } else
2694 line = 0;
2695 i = EDIT_EM_LineIndex(es, line);
2696 src = es->text + i;
2697 line_len = EDIT_EM_LineLength(es, i);
2698 dst_len = *(WORD *)lParam;
2699 if(unicode)
2701 LPWSTR dst = (LPWSTR)lParam;
2702 if(dst_len <= line_len)
2704 memcpy(dst, src, dst_len * sizeof(WCHAR));
2705 return dst_len;
2707 else /* Append 0 if enough space */
2709 memcpy(dst, src, line_len * sizeof(WCHAR));
2710 dst[line_len] = 0;
2711 return line_len;
2714 else
2716 LPSTR dst = (LPSTR)lParam;
2717 INT ret;
2718 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2719 if(!ret) /* Insufficient buffer size */
2720 return dst_len;
2721 if(ret < dst_len) /* Append 0 if enough space */
2722 dst[ret] = 0;
2723 return ret;
2728 /*********************************************************************
2730 * EM_GETSEL
2733 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2735 UINT s = es->selection_start;
2736 UINT e = es->selection_end;
2738 ORDER_UINT(s, e);
2739 if (start)
2740 *start = s;
2741 if (end)
2742 *end = e;
2743 return MAKELONG(s, e);
2747 /*********************************************************************
2749 * EM_GETTHUMB
2751 * FIXME: is this right ? (or should it be only VSCROLL)
2752 * (and maybe only for edit controls that really have their
2753 * own scrollbars) (and maybe only for multiline controls ?)
2754 * All in all: very poorly documented
2757 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2759 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0),
2760 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0));
2764 /*********************************************************************
2766 * EM_LINEFROMCHAR
2769 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2771 INT line;
2772 LINEDEF *line_def;
2774 if (!(es->style & ES_MULTILINE))
2775 return 0;
2776 if (index > (INT)strlenW(es->text))
2777 return es->line_count - 1;
2778 if (index == -1)
2779 index = min(es->selection_start, es->selection_end);
2781 line = 0;
2782 line_def = es->first_line_def;
2783 index -= line_def->length;
2784 while ((index >= 0) && line_def->next) {
2785 line++;
2786 line_def = line_def->next;
2787 index -= line_def->length;
2789 return line;
2793 /*********************************************************************
2795 * EM_LINEINDEX
2798 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2800 INT line_index;
2801 LINEDEF *line_def;
2803 if (!(es->style & ES_MULTILINE))
2804 return 0;
2805 if (line >= es->line_count)
2806 return -1;
2808 line_index = 0;
2809 line_def = es->first_line_def;
2810 if (line == -1) {
2811 INT index = es->selection_end - line_def->length;
2812 while ((index >= 0) && line_def->next) {
2813 line_index += line_def->length;
2814 line_def = line_def->next;
2815 index -= line_def->length;
2817 } else {
2818 while (line > 0) {
2819 line_index += line_def->length;
2820 line_def = line_def->next;
2821 line--;
2824 return line_index;
2828 /*********************************************************************
2830 * EM_LINELENGTH
2833 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2835 LINEDEF *line_def;
2837 if (!(es->style & ES_MULTILINE))
2838 return strlenW(es->text);
2840 if (index == -1) {
2841 /* get the number of remaining non-selected chars of selected lines */
2842 INT32 li;
2843 INT32 count;
2844 li = EDIT_EM_LineFromChar(es, es->selection_start);
2845 /* # chars before start of selection area */
2846 count = es->selection_start - EDIT_EM_LineIndex(es, li);
2847 li = EDIT_EM_LineFromChar(es, es->selection_end);
2848 /* # chars after end of selection */
2849 count += EDIT_EM_LineIndex(es, li) +
2850 EDIT_EM_LineLength(es, li) - es->selection_end;
2851 return count;
2853 line_def = es->first_line_def;
2854 index -= line_def->length;
2855 while ((index >= 0) && line_def->next) {
2856 line_def = line_def->next;
2857 index -= line_def->length;
2859 return line_def->net_length;
2863 /*********************************************************************
2865 * EM_LINESCROLL
2867 * NOTE: dx is in average character widths, dy - in lines;
2870 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2872 if (!(es->style & ES_MULTILINE))
2873 return FALSE;
2875 dx *= es->char_width;
2876 return EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
2879 /*********************************************************************
2881 * EDIT_EM_LineScroll_internal
2883 * Version of EDIT_EM_LineScroll for internal use.
2884 * It doesn't refuse if ES_MULTILINE is set and assumes that
2885 * dx is in pixels, dy - in lines.
2888 static BOOL EDIT_EM_LineScroll_internal(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2890 INT nyoff;
2891 INT x_offset_in_pixels;
2893 if (es->style & ES_MULTILINE)
2895 x_offset_in_pixels = es->x_offset;
2897 else
2899 dy = 0;
2900 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->x_offset, FALSE));
2903 if (-dx > x_offset_in_pixels)
2904 dx = -x_offset_in_pixels;
2905 if (dx > es->text_width - x_offset_in_pixels)
2906 dx = es->text_width - x_offset_in_pixels;
2907 nyoff = max(0, es->y_offset + dy);
2908 if (nyoff >= es->line_count)
2909 nyoff = es->line_count - 1;
2910 dy = (es->y_offset - nyoff) * es->line_height;
2911 if (dx || dy) {
2912 RECT rc1;
2913 RECT rc;
2915 es->y_offset = nyoff;
2916 if(es->style & ES_MULTILINE)
2917 es->x_offset += dx;
2918 else
2919 es->x_offset += dx / es->char_width;
2921 GetClientRect(wnd->hwndSelf, &rc1);
2922 IntersectRect(&rc, &rc1, &es->format_rect);
2923 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2924 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2925 /* force scroll info update */
2926 EDIT_UpdateScrollInfo(wnd, es);
2928 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2929 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
2930 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2931 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
2932 return TRUE;
2936 /*********************************************************************
2938 * EM_POSFROMCHAR
2941 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2943 INT len = strlenW(es->text);
2944 INT l;
2945 INT li;
2946 INT x;
2947 INT y = 0;
2948 HDC dc;
2949 HFONT old_font = 0;
2950 SIZE size;
2952 index = min(index, len);
2953 dc = GetDC(wnd->hwndSelf);
2954 if (es->font)
2955 old_font = SelectObject(dc, es->font);
2956 if (es->style & ES_MULTILINE) {
2957 l = EDIT_EM_LineFromChar(es, index);
2958 y = (l - es->y_offset) * es->line_height;
2959 li = EDIT_EM_LineIndex(es, l);
2960 if (after_wrap && (li == index) && l) {
2961 INT l2 = l - 1;
2962 LINEDEF *line_def = es->first_line_def;
2963 while (l2) {
2964 line_def = line_def->next;
2965 l2--;
2967 if (line_def->ending == END_WRAP) {
2968 l--;
2969 y -= es->line_height;
2970 li = EDIT_EM_LineIndex(es, l);
2973 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2974 es->tabs_count, es->tabs)) - es->x_offset;
2975 } else {
2976 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2977 if (index < es->x_offset) {
2978 GetTextExtentPoint32W(dc, text + index,
2979 es->x_offset - index, &size);
2980 x = -size.cx;
2981 } else {
2982 GetTextExtentPoint32W(dc, text + es->x_offset,
2983 index - es->x_offset, &size);
2984 x = size.cx;
2986 y = 0;
2987 if (es->style & ES_PASSWORD)
2988 HeapFree(GetProcessHeap(), 0, text);
2990 x += es->format_rect.left;
2991 y += es->format_rect.top;
2992 if (es->font)
2993 SelectObject(dc, old_font);
2994 ReleaseDC(wnd->hwndSelf, dc);
2995 return MAKELONG((INT16)x, (INT16)y);
2999 /*********************************************************************
3001 * EM_REPLACESEL
3003 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
3006 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
3008 UINT strl = strlenW(lpsz_replace);
3009 UINT tl = strlenW(es->text);
3010 UINT utl;
3011 UINT s;
3012 UINT e;
3013 UINT i;
3014 LPWSTR p;
3015 HRGN hrgn = 0;
3017 TRACE("%s, can_undo %d, send_update %d\n",
3018 debugstr_w(lpsz_replace), can_undo, send_update);
3020 s = es->selection_start;
3021 e = es->selection_end;
3023 if ((s == e) && !strl)
3024 return;
3026 ORDER_UINT(s, e);
3028 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
3029 return;
3031 if (e != s) {
3032 /* there is something to be deleted */
3033 if (can_undo) {
3034 utl = strlenW(es->undo_text);
3035 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3036 /* undo-buffer is extended to the right */
3037 EDIT_MakeUndoFit(es, utl + e - s);
3038 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
3039 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3040 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3041 /* undo-buffer is extended to the left */
3042 EDIT_MakeUndoFit(es, utl + e - s);
3043 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3044 p[e - s] = p[0];
3045 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3046 p[i] = (es->text + s)[i];
3047 es->undo_position = s;
3048 } else {
3049 /* new undo-buffer */
3050 EDIT_MakeUndoFit(es, e - s);
3051 strncpyW(es->undo_text, es->text + s, e - s + 1);
3052 es->undo_text[e - s] = 0; /* ensure 0 termination */
3053 es->undo_position = s;
3055 /* any deletion makes the old insertion-undo invalid */
3056 es->undo_insert_count = 0;
3057 } else
3058 EDIT_EM_EmptyUndoBuffer(es);
3060 /* now delete */
3061 strcpyW(es->text + s, es->text + e);
3063 if (strl) {
3064 /* there is an insertion */
3065 if (can_undo) {
3066 if ((s == es->undo_position) ||
3067 ((es->undo_insert_count) &&
3068 (s == es->undo_position + es->undo_insert_count)))
3070 * insertion is new and at delete position or
3071 * an extension to either left or right
3073 es->undo_insert_count += strl;
3074 else {
3075 /* new insertion undo */
3076 es->undo_position = s;
3077 es->undo_insert_count = strl;
3078 /* new insertion makes old delete-buffer invalid */
3079 *es->undo_text = '\0';
3081 } else
3082 EDIT_EM_EmptyUndoBuffer(es);
3084 /* now insert */
3085 tl = strlenW(es->text);
3086 for (p = es->text + tl ; p >= es->text + s ; p--)
3087 p[strl] = p[0];
3088 for (i = 0 , p = es->text + s ; i < strl ; i++)
3089 p[i] = lpsz_replace[i];
3090 if(es->style & ES_UPPERCASE)
3091 CharUpperBuffW(p, strl);
3092 else if(es->style & ES_LOWERCASE)
3093 CharLowerBuffW(p, strl);
3094 s += strl;
3096 if (es->style & ES_MULTILINE)
3098 INT s = min(es->selection_start, es->selection_end);
3100 hrgn = CreateRectRgn(0, 0, 0, 0);
3101 EDIT_BuildLineDefs_ML(wnd, es, s, s + strl,
3102 strl - abs(es->selection_end - es->selection_start), hrgn);
3104 else
3105 EDIT_CalcLineWidth_SL(wnd, es);
3107 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
3108 es->flags |= EF_MODIFIED;
3109 if (send_update) es->flags |= EF_UPDATE;
3110 EDIT_EM_ScrollCaret(wnd, es);
3112 /* force scroll info update */
3113 EDIT_UpdateScrollInfo(wnd, es);
3115 if (hrgn)
3117 EDIT_UpdateTextRegion(wnd, hrgn, TRUE);
3118 DeleteObject(hrgn);
3120 else
3121 EDIT_UpdateText(wnd, NULL, TRUE);
3123 if(es->flags & EF_UPDATE)
3125 es->flags &= ~EF_UPDATE;
3126 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3131 /*********************************************************************
3133 * EM_SCROLL
3136 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
3138 INT dy;
3140 if (!(es->style & ES_MULTILINE))
3141 return (LRESULT)FALSE;
3143 dy = 0;
3145 switch (action) {
3146 case SB_LINEUP:
3147 if (es->y_offset)
3148 dy = -1;
3149 break;
3150 case SB_LINEDOWN:
3151 if (es->y_offset < es->line_count - 1)
3152 dy = 1;
3153 break;
3154 case SB_PAGEUP:
3155 if (es->y_offset)
3156 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3157 break;
3158 case SB_PAGEDOWN:
3159 if (es->y_offset < es->line_count - 1)
3160 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3161 break;
3162 default:
3163 return (LRESULT)FALSE;
3165 if (dy) {
3166 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3167 /* check if we are going to move too far */
3168 if(es->y_offset + dy > es->line_count - vlc)
3169 dy = es->line_count - vlc - es->y_offset;
3171 /* Notification is done in EDIT_EM_LineScroll */
3172 if(dy)
3173 EDIT_EM_LineScroll(wnd, es, 0, dy);
3175 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3179 /*********************************************************************
3181 * EM_SCROLLCARET
3184 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
3186 if (es->style & ES_MULTILINE) {
3187 INT l;
3188 INT li;
3189 INT vlc;
3190 INT ww;
3191 INT cw = es->char_width;
3192 INT x;
3193 INT dy = 0;
3194 INT dx = 0;
3196 l = EDIT_EM_LineFromChar(es, es->selection_end);
3197 li = EDIT_EM_LineIndex(es, l);
3198 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
3199 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3200 if (l >= es->y_offset + vlc)
3201 dy = l - vlc + 1 - es->y_offset;
3202 if (l < es->y_offset)
3203 dy = l - es->y_offset;
3204 ww = es->format_rect.right - es->format_rect.left;
3205 if (x < es->format_rect.left)
3206 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3207 if (x > es->format_rect.right)
3208 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3209 if (dy || dx)
3211 /* check if we are going to move too far */
3212 if(es->x_offset + dx + ww > es->text_width)
3213 dx = es->text_width - ww - es->x_offset;
3214 if(dx || dy)
3215 EDIT_EM_LineScroll_internal(wnd, es, dx, dy);
3217 } else {
3218 INT x;
3219 INT goal;
3220 INT format_width;
3222 if (!(es->style & ES_AUTOHSCROLL))
3223 return;
3225 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
3226 format_width = es->format_rect.right - es->format_rect.left;
3227 if (x < es->format_rect.left) {
3228 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3229 do {
3230 es->x_offset--;
3231 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
3232 } while ((x < goal) && es->x_offset);
3233 /* FIXME: use ScrollWindow() somehow to improve performance */
3234 EDIT_UpdateText(wnd, NULL, TRUE);
3235 } else if (x > es->format_rect.right) {
3236 INT x_last;
3237 INT len = strlenW(es->text);
3238 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3239 do {
3240 es->x_offset++;
3241 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
3242 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
3243 } while ((x > goal) && (x_last > es->format_rect.right));
3244 /* FIXME: use ScrollWindow() somehow to improve performance */
3245 EDIT_UpdateText(wnd, NULL, TRUE);
3249 if(es->flags & EF_FOCUSED)
3250 EDIT_SetCaretPos(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3254 /*********************************************************************
3256 * EM_SETHANDLE
3258 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3261 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
3263 if (!(es->style & ES_MULTILINE))
3264 return;
3266 if (!hloc) {
3267 WARN("called with NULL handle\n");
3268 return;
3271 EDIT_UnlockBuffer(wnd, es, TRUE);
3273 if(es->hloc16)
3275 LOCAL_Free(wnd->hInstance, es->hloc16);
3276 es->hloc16 = (HLOCAL16)NULL;
3279 if(es->is_unicode)
3281 if(es->hloc32A)
3283 LocalFree(es->hloc32A);
3284 es->hloc32A = (HLOCAL)NULL;
3286 es->hloc32W = hloc;
3288 else
3290 INT countW, countA;
3291 HLOCAL hloc32W_new;
3292 WCHAR *textW;
3293 CHAR *textA;
3295 countA = LocalSize(hloc);
3296 textA = LocalLock(hloc);
3297 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3298 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3300 ERR("Could not allocate new unicode buffer\n");
3301 return;
3303 textW = LocalLock(hloc32W_new);
3304 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3305 LocalUnlock(hloc32W_new);
3306 LocalUnlock(hloc);
3308 if(es->hloc32W)
3309 LocalFree(es->hloc32W);
3311 es->hloc32W = hloc32W_new;
3312 es->hloc32A = hloc;
3315 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3317 EDIT_LockBuffer(wnd, es);
3319 es->x_offset = es->y_offset = 0;
3320 es->selection_start = es->selection_end = 0;
3321 EDIT_EM_EmptyUndoBuffer(es);
3322 es->flags &= ~EF_MODIFIED;
3323 es->flags &= ~EF_UPDATE;
3324 EDIT_BuildLineDefs_ML(wnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3325 EDIT_UpdateText(wnd, NULL, TRUE);
3326 EDIT_EM_ScrollCaret(wnd, es);
3327 /* force scroll info update */
3328 EDIT_UpdateScrollInfo(wnd, es);
3332 /*********************************************************************
3334 * EM_SETHANDLE16
3336 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3339 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
3341 INT countW, countA;
3342 HLOCAL hloc32W_new;
3343 WCHAR *textW;
3344 CHAR *textA;
3346 if (!(es->style & ES_MULTILINE))
3347 return;
3349 if (!hloc) {
3350 WARN("called with NULL handle\n");
3351 return;
3354 EDIT_UnlockBuffer(wnd, es, TRUE);
3356 if(es->hloc32A)
3358 LocalFree(es->hloc32A);
3359 es->hloc32A = (HLOCAL)NULL;
3362 countA = LOCAL_Size(wnd->hInstance, hloc);
3363 textA = LOCAL_Lock(wnd->hInstance, hloc);
3364 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3365 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3367 ERR("Could not allocate new unicode buffer\n");
3368 return;
3370 textW = LocalLock(hloc32W_new);
3371 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3372 LocalUnlock(hloc32W_new);
3373 LOCAL_Unlock(wnd->hInstance, hloc);
3375 if(es->hloc32W)
3376 LocalFree(es->hloc32W);
3378 es->hloc32W = hloc32W_new;
3379 es->hloc16 = hloc;
3381 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3383 EDIT_LockBuffer(wnd, es);
3385 es->x_offset = es->y_offset = 0;
3386 es->selection_start = es->selection_end = 0;
3387 EDIT_EM_EmptyUndoBuffer(es);
3388 es->flags &= ~EF_MODIFIED;
3389 es->flags &= ~EF_UPDATE;
3390 EDIT_BuildLineDefs_ML(wnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3391 EDIT_UpdateText(wnd, NULL, TRUE);
3392 EDIT_EM_ScrollCaret(wnd, es);
3393 /* force scroll info update */
3394 EDIT_UpdateScrollInfo(wnd, es);
3398 /*********************************************************************
3400 * EM_SETLIMITTEXT
3402 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3403 * However, the windows version is not complied to yet in all of edit.c
3406 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3408 if (es->style & ES_MULTILINE) {
3409 if (limit)
3410 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3411 else
3412 es->buffer_limit = BUFLIMIT_MULTI;
3413 } else {
3414 if (limit)
3415 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3416 else
3417 es->buffer_limit = BUFLIMIT_SINGLE;
3422 /*********************************************************************
3424 * EM_SETMARGINS
3426 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3427 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3428 * of the char's width, according to the new docs.
3431 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3432 INT left, INT right)
3434 if (action & EC_LEFTMARGIN) {
3435 if (left != EC_USEFONTINFO)
3436 es->left_margin = left;
3437 else
3438 es->left_margin = es->char_width / 3;
3441 if (action & EC_RIGHTMARGIN) {
3442 if (right != EC_USEFONTINFO)
3443 es->right_margin = right;
3444 else
3445 es->right_margin = es->char_width / 3;
3447 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3451 /*********************************************************************
3453 * EM_SETPASSWORDCHAR
3456 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, WCHAR c)
3458 if (es->style & ES_MULTILINE)
3459 return;
3461 if (es->password_char == c)
3462 return;
3464 es->password_char = c;
3465 if (c) {
3466 wnd->dwStyle |= ES_PASSWORD;
3467 es->style |= ES_PASSWORD;
3468 } else {
3469 wnd->dwStyle &= ~ES_PASSWORD;
3470 es->style &= ~ES_PASSWORD;
3472 EDIT_UpdateText(wnd, NULL, TRUE);
3476 /*********************************************************************
3478 * EDIT_EM_SetSel
3480 * note: unlike the specs say: the order of start and end
3481 * _is_ preserved in Windows. (i.e. start can be > end)
3482 * In other words: this handler is OK
3485 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3487 UINT old_start = es->selection_start;
3488 UINT old_end = es->selection_end;
3489 UINT len = strlenW(es->text);
3491 if (start == (UINT)-1) {
3492 start = es->selection_end;
3493 end = es->selection_end;
3494 } else {
3495 start = min(start, len);
3496 end = min(end, len);
3498 es->selection_start = start;
3499 es->selection_end = end;
3500 if (after_wrap)
3501 es->flags |= EF_AFTER_WRAP;
3502 else
3503 es->flags &= ~EF_AFTER_WRAP;
3504 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3505 ORDER_UINT(start, end);
3506 ORDER_UINT(end, old_end);
3507 ORDER_UINT(start, old_start);
3508 ORDER_UINT(old_start, old_end);
3509 if (end != old_start)
3512 * One can also do
3513 * ORDER_UINT32(end, old_start);
3514 * EDIT_InvalidateText(wnd, es, start, end);
3515 * EDIT_InvalidateText(wnd, es, old_start, old_end);
3516 * in place of the following if statement.
3518 if (old_start > end )
3520 EDIT_InvalidateText(wnd, es, start, end);
3521 EDIT_InvalidateText(wnd, es, old_start, old_end);
3523 else
3525 EDIT_InvalidateText(wnd, es, start, old_start);
3526 EDIT_InvalidateText(wnd, es, end, old_end);
3529 else EDIT_InvalidateText(wnd, es, start, old_end);
3533 /*********************************************************************
3535 * EM_SETTABSTOPS
3538 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3540 if (!(es->style & ES_MULTILINE))
3541 return FALSE;
3542 if (es->tabs)
3543 HeapFree(GetProcessHeap(), 0, es->tabs);
3544 es->tabs_count = count;
3545 if (!count)
3546 es->tabs = NULL;
3547 else {
3548 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3549 memcpy(es->tabs, tabs, count * sizeof(INT));
3551 return TRUE;
3555 /*********************************************************************
3557 * EM_SETTABSTOPS16
3560 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3562 if (!(es->style & ES_MULTILINE))
3563 return FALSE;
3564 if (es->tabs)
3565 HeapFree(GetProcessHeap(), 0, es->tabs);
3566 es->tabs_count = count;
3567 if (!count)
3568 es->tabs = NULL;
3569 else {
3570 INT i;
3571 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3572 for (i = 0 ; i < count ; i++)
3573 es->tabs[i] = *tabs++;
3575 return TRUE;
3579 /*********************************************************************
3581 * EM_SETWORDBREAKPROC
3584 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, LPARAM lParam)
3586 if (es->word_break_proc == (void *)lParam)
3587 return;
3589 es->word_break_proc = (void *)lParam;
3590 es->word_break_proc16 = NULL;
3592 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3593 EDIT_BuildLineDefs_ML(wnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3594 EDIT_UpdateText(wnd, NULL, TRUE);
3599 /*********************************************************************
3601 * EM_SETWORDBREAKPROC16
3604 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3606 if (es->word_break_proc16 == wbp)
3607 return;
3609 es->word_break_proc = NULL;
3610 es->word_break_proc16 = wbp;
3611 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3612 EDIT_BuildLineDefs_ML(wnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3613 EDIT_UpdateText(wnd, NULL, TRUE);
3618 /*********************************************************************
3620 * EM_UNDO / WM_UNDO
3623 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
3625 INT ulength;
3626 LPWSTR utext;
3628 /* Protect read-only edit control from modification */
3629 if(es->style & ES_READONLY)
3630 return FALSE;
3632 ulength = strlenW(es->undo_text);
3633 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3635 strcpyW(utext, es->undo_text);
3637 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3638 es->undo_insert_count, debugstr_w(utext));
3640 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3641 EDIT_EM_EmptyUndoBuffer(es);
3642 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext, FALSE);
3643 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3644 /* send the notification after the selection start and end are set */
3645 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3646 EDIT_EM_ScrollCaret(wnd, es);
3647 HeapFree(GetProcessHeap(), 0, utext);
3649 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3650 es->undo_insert_count, debugstr_w(es->undo_text));
3651 return TRUE;
3655 /*********************************************************************
3657 * WM_CHAR
3660 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, WCHAR c)
3662 BOOL control;
3664 /* Protect read-only edit control from modification */
3665 if(es->style & ES_READONLY)
3666 return;
3668 control = GetKeyState(VK_CONTROL) & 0x8000;
3670 switch (c) {
3671 case '\r':
3672 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3673 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3674 break;
3675 case '\n':
3676 if (es->style & ES_MULTILINE) {
3677 if (es->style & ES_READONLY) {
3678 EDIT_MoveHome(wnd, es, FALSE);
3679 EDIT_MoveDown_ML(wnd, es, FALSE);
3680 } else {
3681 static const WCHAR cr_lfW[] = {'\r','\n',0};
3682 EDIT_EM_ReplaceSel(wnd, es, TRUE, cr_lfW, TRUE);
3685 break;
3686 case '\t':
3687 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3689 static const WCHAR tabW[] = {'\t',0};
3690 EDIT_EM_ReplaceSel(wnd, es, TRUE, tabW, TRUE);
3692 break;
3693 case VK_BACK:
3694 if (!(es->style & ES_READONLY) && !control) {
3695 if (es->selection_start != es->selection_end)
3696 EDIT_WM_Clear(wnd, es);
3697 else {
3698 /* delete character left of caret */
3699 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
3700 EDIT_MoveBackward(wnd, es, TRUE);
3701 EDIT_WM_Clear(wnd, es);
3704 break;
3705 case 0x03: /* ^C */
3706 SendMessageW(wnd->hwndSelf, WM_COPY, 0, 0);
3707 break;
3708 case 0x16: /* ^V */
3709 SendMessageW(wnd->hwndSelf, WM_PASTE, 0, 0);
3710 break;
3711 case 0x18: /* ^X */
3712 SendMessageW(wnd->hwndSelf, WM_CUT, 0, 0);
3713 break;
3715 default:
3716 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3717 WCHAR str[2];
3718 str[0] = c;
3719 str[1] = '\0';
3720 EDIT_EM_ReplaceSel(wnd, es, TRUE, str, TRUE);
3722 break;
3727 /*********************************************************************
3729 * WM_COMMAND
3732 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3734 if (code || control)
3735 return;
3737 switch (id) {
3738 case EM_UNDO:
3739 EDIT_EM_Undo(wnd, es);
3740 break;
3741 case WM_CUT:
3742 EDIT_WM_Cut(wnd, es);
3743 break;
3744 case WM_COPY:
3745 EDIT_WM_Copy(wnd, es);
3746 break;
3747 case WM_PASTE:
3748 EDIT_WM_Paste(wnd, es);
3749 break;
3750 case WM_CLEAR:
3751 EDIT_WM_Clear(wnd, es);
3752 break;
3753 case EM_SETSEL:
3754 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
3755 EDIT_EM_ScrollCaret(wnd, es);
3756 break;
3757 default:
3758 ERR("unknown menu item, please report\n");
3759 break;
3764 /*********************************************************************
3766 * WM_CONTEXTMENU
3768 * Note: the resource files resource/sysres_??.rc cannot define a
3769 * single popup menu. Hence we use a (dummy) menubar
3770 * containing the single popup menu as its first item.
3772 * FIXME: the message identifiers have been chosen arbitrarily,
3773 * hence we use MF_BYPOSITION.
3774 * We might as well use the "real" values (anybody knows ?)
3775 * The menu definition is in resources/sysres_??.rc.
3776 * Once these are OK, we better use MF_BYCOMMAND here
3777 * (as we do in EDIT_WM_Command()).
3780 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, INT x, INT y)
3782 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3783 HMENU popup = GetSubMenu(menu, 0);
3784 UINT start = es->selection_start;
3785 UINT end = es->selection_end;
3787 ORDER_UINT(start, end);
3789 /* undo */
3790 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3791 /* cut */
3792 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3793 /* copy */
3794 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3795 /* paste */
3796 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3797 /* delete */
3798 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3799 /* select all */
3800 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3802 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3803 DestroyMenu(menu);
3807 /*********************************************************************
3809 * WM_COPY
3812 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3814 INT s = es->selection_start;
3815 INT e = es->selection_end;
3816 HGLOBAL hdst;
3817 LPWSTR dst;
3819 if (e == s)
3820 return;
3821 ORDER_INT(s, e);
3822 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3823 dst = GlobalLock(hdst);
3824 strncpyW(dst, es->text + s, e - s);
3825 dst[e - s] = 0; /* ensure 0 termination */
3826 TRACE("%s\n", debugstr_w(dst));
3827 GlobalUnlock(hdst);
3828 OpenClipboard(wnd->hwndSelf);
3829 EmptyClipboard();
3830 SetClipboardData(CF_UNICODETEXT, hdst);
3831 CloseClipboard();
3835 /*********************************************************************
3837 * WM_CREATE
3840 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCWSTR name)
3842 TRACE("%s\n", debugstr_w(name));
3844 * To initialize some final structure members, we call some helper
3845 * functions. However, since the EDITSTATE is not consistent (i.e.
3846 * not fully initialized), we should be very careful which
3847 * functions can be called, and in what order.
3849 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3850 EDIT_EM_EmptyUndoBuffer(es);
3852 if (name && *name) {
3853 EDIT_EM_ReplaceSel(wnd, es, FALSE, name, FALSE);
3854 /* if we insert text to the editline, the text scrolls out
3855 * of the window, as the caret is placed after the insert
3856 * pos normally; thus we reset es->selection... to 0 and
3857 * update caret
3859 es->selection_start = es->selection_end = 0;
3860 /* send the notification after the selection start and end are set */
3861 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
3862 EDIT_EM_ScrollCaret(wnd, es);
3864 /* force scroll info update */
3865 EDIT_UpdateScrollInfo(wnd, es);
3866 return 0;
3870 /*********************************************************************
3872 * WM_DESTROY
3875 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3877 LINEDEF *pc, *pp;
3879 if (es->hloc32W) {
3880 while (LocalUnlock(es->hloc32W)) ;
3881 LocalFree(es->hloc32W);
3883 if (es->hloc32A) {
3884 while (LocalUnlock(es->hloc32A)) ;
3885 LocalFree(es->hloc32A);
3887 if (es->hloc16) {
3888 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3889 LOCAL_Free(wnd->hInstance, es->hloc16);
3892 pc = es->first_line_def;
3893 while (pc)
3895 pp = pc->next;
3896 HeapFree(GetProcessHeap(), 0, pc);
3897 pc = pp;
3900 HeapFree(GetProcessHeap(), 0, es);
3901 *(EDITSTATE **)wnd->wExtra = NULL;
3905 /*********************************************************************
3907 * WM_ERASEBKGND
3910 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3912 HBRUSH brush;
3913 RECT rc;
3915 if ( get_app_version() >= 0x40000 &&(
3916 !es->bEnableState || (es->style & ES_READONLY)))
3917 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3918 else
3919 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3921 if (!brush)
3922 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3924 GetClientRect(wnd->hwndSelf, &rc);
3925 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3926 GetClipBox(dc, &rc);
3928 * FIXME: specs say that we should UnrealizeObject() the brush,
3929 * but the specs of UnrealizeObject() say that we shouldn't
3930 * unrealize a stock object. The default brush that
3931 * DefWndProc() returns is ... a stock object.
3933 FillRect(dc, &rc, brush);
3934 return -1;
3938 /*********************************************************************
3940 * WM_GETTEXT
3943 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3945 if(!count) return 0;
3947 if(unicode)
3949 LPWSTR textW = (LPWSTR)lParam;
3950 strncpyW(textW, es->text, count);
3951 textW[count - 1] = 0; /* ensure 0 termination */
3952 return strlenW(textW);
3954 else
3956 LPSTR textA = (LPSTR)lParam;
3957 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL);
3958 textA[count - 1] = 0; /* ensure 0 termination */
3959 return strlen(textA);
3963 /*********************************************************************
3965 * WM_HSCROLL
3968 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
3970 INT dx;
3971 INT fw;
3973 if (!(es->style & ES_MULTILINE))
3974 return 0;
3976 if (!(es->style & ES_AUTOHSCROLL))
3977 return 0;
3979 dx = 0;
3980 fw = es->format_rect.right - es->format_rect.left;
3981 switch (action) {
3982 case SB_LINELEFT:
3983 TRACE("SB_LINELEFT\n");
3984 if (es->x_offset)
3985 dx = -es->char_width;
3986 break;
3987 case SB_LINERIGHT:
3988 TRACE("SB_LINERIGHT\n");
3989 if (es->x_offset < es->text_width)
3990 dx = es->char_width;
3991 break;
3992 case SB_PAGELEFT:
3993 TRACE("SB_PAGELEFT\n");
3994 if (es->x_offset)
3995 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3996 break;
3997 case SB_PAGERIGHT:
3998 TRACE("SB_PAGERIGHT\n");
3999 if (es->x_offset < es->text_width)
4000 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
4001 break;
4002 case SB_LEFT:
4003 TRACE("SB_LEFT\n");
4004 if (es->x_offset)
4005 dx = -es->x_offset;
4006 break;
4007 case SB_RIGHT:
4008 TRACE("SB_RIGHT\n");
4009 if (es->x_offset < es->text_width)
4010 dx = es->text_width - es->x_offset;
4011 break;
4012 case SB_THUMBTRACK:
4013 TRACE("SB_THUMBTRACK %d\n", pos);
4014 es->flags |= EF_HSCROLL_TRACK;
4015 if(es->style & WS_HSCROLL)
4016 dx = pos - es->x_offset;
4017 else
4019 INT fw, new_x;
4020 /* Sanity check */
4021 if(pos < 0 || pos > 100) return 0;
4022 /* Assume default scroll range 0-100 */
4023 fw = es->format_rect.right - es->format_rect.left;
4024 new_x = pos * (es->text_width - fw) / 100;
4025 dx = es->text_width ? (new_x - es->x_offset) : 0;
4027 break;
4028 case SB_THUMBPOSITION:
4029 TRACE("SB_THUMBPOSITION %d\n", pos);
4030 es->flags &= ~EF_HSCROLL_TRACK;
4031 if(wnd->dwStyle & WS_HSCROLL)
4032 dx = pos - es->x_offset;
4033 else
4035 INT fw, new_x;
4036 /* Sanity check */
4037 if(pos < 0 || pos > 100) return 0;
4038 /* Assume default scroll range 0-100 */
4039 fw = es->format_rect.right - es->format_rect.left;
4040 new_x = pos * (es->text_width - fw) / 100;
4041 dx = es->text_width ? (new_x - es->x_offset) : 0;
4043 if (!dx) {
4044 /* force scroll info update */
4045 EDIT_UpdateScrollInfo(wnd, es);
4046 EDIT_NOTIFY_PARENT(es, EN_HSCROLL, "EN_HSCROLL");
4048 break;
4049 case SB_ENDSCROLL:
4050 TRACE("SB_ENDSCROLL\n");
4051 break;
4053 * FIXME : the next two are undocumented !
4054 * Are we doing the right thing ?
4055 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4056 * although it's also a regular control message.
4058 case EM_GETTHUMB: /* this one is used by NT notepad */
4059 case EM_GETTHUMB16:
4061 LRESULT ret;
4062 if(wnd->dwStyle & WS_HSCROLL)
4063 ret = GetScrollPos(wnd->hwndSelf, SB_HORZ);
4064 else
4066 /* Assume default scroll range 0-100 */
4067 INT fw = es->format_rect.right - es->format_rect.left;
4068 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4070 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4071 return ret;
4073 case EM_LINESCROLL16:
4074 TRACE("EM_LINESCROLL16\n");
4075 dx = pos;
4076 break;
4078 default:
4079 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4080 action, action);
4081 return 0;
4083 if (dx)
4085 INT fw = es->format_rect.right - es->format_rect.left;
4086 /* check if we are going to move too far */
4087 if(es->x_offset + dx + fw > es->text_width)
4088 dx = es->text_width - fw - es->x_offset;
4089 if(dx)
4090 EDIT_EM_LineScroll_internal(wnd, es, dx, 0);
4092 return 0;
4096 /*********************************************************************
4098 * EDIT_CheckCombo
4101 static BOOL EDIT_CheckCombo(WND *wnd, EDITSTATE *es, UINT msg, INT key)
4103 HWND hLBox = es->hwndListBox;
4104 HWND hCombo;
4105 BOOL bDropped;
4106 int nEUI;
4108 if (!hLBox)
4109 return FALSE;
4111 hCombo = wnd->parent->hwndSelf;
4112 bDropped = TRUE;
4113 nEUI = 0;
4115 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
4116 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
4118 if (key == VK_UP || key == VK_DOWN)
4120 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4121 nEUI = 1;
4123 if (msg == WM_KEYDOWN || nEUI)
4124 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4127 switch (msg)
4129 case WM_KEYDOWN:
4130 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4132 /* make sure ComboLBox pops up */
4133 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4134 key = VK_F4;
4135 nEUI = 2;
4138 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4139 break;
4141 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4142 if (nEUI)
4143 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4144 else
4145 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4146 break;
4149 if(nEUI == 2)
4150 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4152 return TRUE;
4156 /*********************************************************************
4158 * WM_KEYDOWN
4160 * Handling of special keys that don't produce a WM_CHAR
4161 * (i.e. non-printable keys) & Backspace & Delete
4164 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key)
4166 BOOL shift;
4167 BOOL control;
4169 if (GetKeyState(VK_MENU) & 0x8000)
4170 return 0;
4172 shift = GetKeyState(VK_SHIFT) & 0x8000;
4173 control = GetKeyState(VK_CONTROL) & 0x8000;
4175 switch (key) {
4176 case VK_F4:
4177 case VK_UP:
4178 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key) || key == VK_F4)
4179 break;
4181 /* fall through */
4182 case VK_LEFT:
4183 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4184 EDIT_MoveUp_ML(wnd, es, shift);
4185 else
4186 if (control)
4187 EDIT_MoveWordBackward(wnd, es, shift);
4188 else
4189 EDIT_MoveBackward(wnd, es, shift);
4190 break;
4191 case VK_DOWN:
4192 if (EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key))
4193 break;
4194 /* fall through */
4195 case VK_RIGHT:
4196 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4197 EDIT_MoveDown_ML(wnd, es, shift);
4198 else if (control)
4199 EDIT_MoveWordForward(wnd, es, shift);
4200 else
4201 EDIT_MoveForward(wnd, es, shift);
4202 break;
4203 case VK_HOME:
4204 EDIT_MoveHome(wnd, es, shift);
4205 break;
4206 case VK_END:
4207 EDIT_MoveEnd(wnd, es, shift);
4208 break;
4209 case VK_PRIOR:
4210 if (es->style & ES_MULTILINE)
4211 EDIT_MovePageUp_ML(wnd, es, shift);
4212 else
4213 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
4214 break;
4215 case VK_NEXT:
4216 if (es->style & ES_MULTILINE)
4217 EDIT_MovePageDown_ML(wnd, es, shift);
4218 else
4219 EDIT_CheckCombo(wnd, es, WM_KEYDOWN, key);
4220 break;
4221 case VK_DELETE:
4222 if (!(es->style & ES_READONLY) && !(shift && control)) {
4223 if (es->selection_start != es->selection_end) {
4224 if (shift)
4225 EDIT_WM_Cut(wnd, es);
4226 else
4227 EDIT_WM_Clear(wnd, es);
4228 } else {
4229 if (shift) {
4230 /* delete character left of caret */
4231 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
4232 EDIT_MoveBackward(wnd, es, TRUE);
4233 EDIT_WM_Clear(wnd, es);
4234 } else if (control) {
4235 /* delete to end of line */
4236 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
4237 EDIT_MoveEnd(wnd, es, TRUE);
4238 EDIT_WM_Clear(wnd, es);
4239 } else {
4240 /* delete character right of caret */
4241 EDIT_EM_SetSel(wnd, es, (UINT)-1, 0, FALSE);
4242 EDIT_MoveForward(wnd, es, TRUE);
4243 EDIT_WM_Clear(wnd, es);
4247 break;
4248 case VK_INSERT:
4249 if (shift) {
4250 if (!(es->style & ES_READONLY))
4251 EDIT_WM_Paste(wnd, es);
4252 } else if (control)
4253 EDIT_WM_Copy(wnd, es);
4254 break;
4255 case VK_RETURN:
4256 /* If the edit doesn't want the return send a message to the default object */
4257 if(!(es->style & ES_WANTRETURN))
4259 HWND hwndParent = GetParent(wnd->hwndSelf);
4260 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4261 if (HIWORD(dw) == DC_HASDEFID)
4263 SendMessageW( hwndParent, WM_COMMAND,
4264 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4265 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4268 break;
4270 return 0;
4274 /*********************************************************************
4276 * WM_KILLFOCUS
4279 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es)
4281 es->flags &= ~EF_FOCUSED;
4282 DestroyCaret();
4283 if(!(es->style & ES_NOHIDESEL))
4284 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4285 EDIT_NOTIFY_PARENT(es, EN_KILLFOCUS, "EN_KILLFOCUS");
4286 return 0;
4290 /*********************************************************************
4292 * WM_LBUTTONDBLCLK
4294 * The caret position has been set on the WM_LBUTTONDOWN message
4297 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es)
4299 INT s;
4300 INT e = es->selection_end;
4301 INT l;
4302 INT li;
4303 INT ll;
4305 if (!(es->flags & EF_FOCUSED))
4306 return 0;
4308 l = EDIT_EM_LineFromChar(es, e);
4309 li = EDIT_EM_LineIndex(es, l);
4310 ll = EDIT_EM_LineLength(es, e);
4311 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4312 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4313 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
4314 EDIT_EM_ScrollCaret(wnd, es);
4315 return 0;
4319 /*********************************************************************
4321 * WM_LBUTTONDOWN
4324 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4326 INT e;
4327 BOOL after_wrap;
4329 if (!(es->flags & EF_FOCUSED))
4330 return 0;
4332 es->bCaptureState = TRUE;
4333 SetCapture(wnd->hwndSelf);
4334 EDIT_ConfinePoint(es, &x, &y);
4335 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4336 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4337 EDIT_EM_ScrollCaret(wnd, es);
4338 es->region_posx = es->region_posy = 0;
4339 SetTimer(wnd->hwndSelf, 0, 100, NULL);
4340 return 0;
4344 /*********************************************************************
4346 * WM_LBUTTONUP
4349 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4351 if (es->bCaptureState && GetCapture() == hwndSelf) {
4352 KillTimer(hwndSelf, 0);
4353 ReleaseCapture();
4355 es->bCaptureState = FALSE;
4356 return 0;
4360 /*********************************************************************
4362 * WM_MBUTTONDOWN
4365 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
4367 SendMessageW(wnd->hwndSelf,WM_PASTE,0,0);
4368 return 0;
4372 /*********************************************************************
4374 * WM_MOUSEMOVE
4377 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, INT x, INT y)
4379 INT e;
4380 BOOL after_wrap;
4381 INT prex, prey;
4383 if (GetCapture() != wnd->hwndSelf)
4384 return 0;
4387 * FIXME: gotta do some scrolling if outside client
4388 * area. Maybe reset the timer ?
4390 prex = x; prey = y;
4391 EDIT_ConfinePoint(es, &x, &y);
4392 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4393 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4394 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
4395 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
4396 return 0;
4400 /*********************************************************************
4402 * WM_NCCREATE
4404 * See also EDIT_WM_StyleChanged
4406 static LRESULT EDIT_WM_NCCreate(WND *wnd, DWORD style, HWND hwndParent, BOOL unicode)
4408 EDITSTATE *es;
4409 UINT alloc_size;
4411 TRACE("Creating %s edit control, style = %08lx %08lx\n",
4412 unicode ? "Unicode" : "ANSI", style, wnd->dwExStyle);
4414 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4415 return FALSE;
4416 *(EDITSTATE **)wnd->wExtra = es;
4419 * Note: since the EDITSTATE has not been fully initialized yet,
4420 * we can't use any API calls that may send
4421 * WM_XXX messages before WM_NCCREATE is completed.
4424 es->is_unicode = unicode;
4425 es->style = style;
4427 es->bEnableState = !(style & WS_DISABLED);
4430 * In Win95 look and feel, the WS_BORDER style is replaced by the
4431 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4432 * control a non client area. Not always. This coordinates in some
4433 * way with the window creation code in dialog.c When making
4434 * modifications please ensure that the code still works for edit
4435 * controls created directly with style 0x50800000, exStyle 0 (
4436 * which should have a single pixel border)
4438 if (TWEAK_WineLook != WIN31_LOOK)
4440 es->style &= ~WS_BORDER;
4442 else
4444 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4445 wnd->dwStyle &= ~WS_BORDER;
4448 /* Save parent, which will be notified by EN_* messages */
4449 es->hwndParent = hwndParent;
4451 if (es->style & ES_COMBO)
4452 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4454 /* Number overrides lowercase overrides uppercase (at least it
4455 * does in Win95). However I'll bet that ES_NUMBER would be
4456 * invalid under Win 3.1.
4458 if (es->style & ES_NUMBER) {
4459 ; /* do not override the ES_NUMBER */
4460 } else if (es->style & ES_LOWERCASE) {
4461 es->style &= ~ES_UPPERCASE;
4463 if (es->style & ES_MULTILINE) {
4464 es->buffer_limit = BUFLIMIT_MULTI;
4465 if (es->style & WS_VSCROLL)
4466 es->style |= ES_AUTOVSCROLL;
4467 if (es->style & WS_HSCROLL)
4468 es->style |= ES_AUTOHSCROLL;
4469 es->style &= ~ES_PASSWORD;
4470 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4471 /* Confirmed - RIGHT overrides CENTER */
4472 if (es->style & ES_RIGHT)
4473 es->style &= ~ES_CENTER;
4474 es->style &= ~WS_HSCROLL;
4475 es->style &= ~ES_AUTOHSCROLL;
4478 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4479 es->style |= ES_AUTOVSCROLL;
4480 } else {
4481 es->buffer_limit = BUFLIMIT_SINGLE;
4482 if (WIN31_LOOK == TWEAK_WineLook ||
4483 WIN95_LOOK == TWEAK_WineLook) {
4484 es->style &= ~ES_CENTER;
4485 es->style &= ~ES_RIGHT;
4486 } else {
4487 if (es->style & ES_RIGHT)
4488 es->style &= ~ES_CENTER;
4490 es->style &= ~WS_HSCROLL;
4491 es->style &= ~WS_VSCROLL;
4492 es->style &= ~ES_AUTOVSCROLL;
4493 es->style &= ~ES_WANTRETURN;
4494 if (es->style & ES_PASSWORD)
4495 es->password_char = '*';
4497 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4498 es->style |= ES_AUTOHSCROLL;
4501 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4502 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4503 return FALSE;
4504 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4506 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4507 return FALSE;
4508 es->undo_buffer_size = es->buffer_size;
4510 if (es->style & ES_MULTILINE)
4511 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4512 return FALSE;
4513 es->line_count = 1;
4515 return TRUE;
4518 /*********************************************************************
4520 * WM_PAINT
4523 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
4525 PAINTSTRUCT ps;
4526 INT i;
4527 HDC dc;
4528 HFONT old_font = 0;
4529 RECT rc;
4530 RECT rcLine;
4531 RECT rcRgn;
4532 BOOL rev = es->bEnableState &&
4533 ((es->flags & EF_FOCUSED) ||
4534 (es->style & ES_NOHIDESEL));
4535 if (!wParam)
4536 dc = BeginPaint(wnd->hwndSelf, &ps);
4537 else
4538 dc = (HDC) wParam;
4539 if(es->style & WS_BORDER) {
4540 GetClientRect(wnd->hwndSelf, &rc);
4541 if(es->style & ES_MULTILINE) {
4542 if(es->style & WS_HSCROLL) rc.bottom++;
4543 if(es->style & WS_VSCROLL) rc.right++;
4545 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4547 IntersectClipRect(dc, es->format_rect.left,
4548 es->format_rect.top,
4549 es->format_rect.right,
4550 es->format_rect.bottom);
4551 if (es->style & ES_MULTILINE) {
4552 GetClientRect(wnd->hwndSelf, &rc);
4553 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4555 if (es->font)
4556 old_font = SelectObject(dc, es->font);
4557 if ( get_app_version() >= 0x40000 &&(
4558 !es->bEnableState || (es->style & ES_READONLY)))
4559 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
4560 else
4561 EDIT_SEND_CTLCOLOR(wnd, dc);
4563 if (!es->bEnableState)
4564 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4565 GetClipBox(dc, &rcRgn);
4566 if (es->style & ES_MULTILINE) {
4567 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4568 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4569 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
4570 if (IntersectRect(&rc, &rcRgn, &rcLine))
4571 EDIT_PaintLine(wnd, es, dc, i, rev);
4573 } else {
4574 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
4575 if (IntersectRect(&rc, &rcRgn, &rcLine))
4576 EDIT_PaintLine(wnd, es, dc, 0, rev);
4578 if (es->font)
4579 SelectObject(dc, old_font);
4581 if (!wParam)
4582 EndPaint(wnd->hwndSelf, &ps);
4586 /*********************************************************************
4588 * WM_PASTE
4591 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
4593 HGLOBAL hsrc;
4594 LPWSTR src;
4596 /* Protect read-only edit control from modification */
4597 if(es->style & ES_READONLY)
4598 return;
4600 OpenClipboard(wnd->hwndSelf);
4601 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4602 src = (LPWSTR)GlobalLock(hsrc);
4603 EDIT_EM_ReplaceSel(wnd, es, TRUE, src, TRUE);
4604 GlobalUnlock(hsrc);
4606 CloseClipboard();
4610 /*********************************************************************
4612 * WM_SETFOCUS
4615 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es)
4617 es->flags |= EF_FOCUSED;
4618 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4619 EDIT_SetCaretPos(wnd, es, es->selection_end,
4620 es->flags & EF_AFTER_WRAP);
4621 if(!(es->style & ES_NOHIDESEL))
4622 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
4623 ShowCaret(wnd->hwndSelf);
4624 EDIT_NOTIFY_PARENT(es, EN_SETFOCUS, "EN_SETFOCUS");
4628 /*********************************************************************
4630 * WM_SETFONT
4632 * With Win95 look the margins are set to default font value unless
4633 * the system font (font == 0) is being set, in which case they are left
4634 * unchanged.
4637 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
4639 TEXTMETRICW tm;
4640 HDC dc;
4641 HFONT old_font = 0;
4642 RECT r;
4644 es->font = font;
4645 dc = GetDC(wnd->hwndSelf);
4646 if (font)
4647 old_font = SelectObject(dc, font);
4648 GetTextMetricsW(dc, &tm);
4649 es->line_height = tm.tmHeight;
4650 es->char_width = tm.tmAveCharWidth;
4651 if (font)
4652 SelectObject(dc, old_font);
4653 ReleaseDC(wnd->hwndSelf, dc);
4654 if (font && (TWEAK_WineLook > WIN31_LOOK))
4655 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4656 EC_USEFONTINFO, EC_USEFONTINFO);
4658 /* Force the recalculation of the format rect for each font change */
4659 GetClientRect(wnd->hwndSelf, &r);
4660 EDIT_SetRectNP(wnd, es, &r);
4662 if (es->style & ES_MULTILINE)
4663 EDIT_BuildLineDefs_ML(wnd, es, 0, strlenW(es->text), 0, (HRGN)0);
4664 else
4665 EDIT_CalcLineWidth_SL(wnd, es);
4667 if (redraw)
4668 EDIT_UpdateText(wnd, NULL, TRUE);
4669 if (es->flags & EF_FOCUSED) {
4670 DestroyCaret();
4671 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
4672 EDIT_SetCaretPos(wnd, es, es->selection_end,
4673 es->flags & EF_AFTER_WRAP);
4674 ShowCaret(wnd->hwndSelf);
4679 /*********************************************************************
4681 * WM_SETTEXT
4683 * NOTES
4684 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4685 * The modified flag is reset. No notifications are sent.
4687 * For single-line controls, reception of WM_SETTEXT triggers:
4688 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4691 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4693 LPWSTR text = NULL;
4695 if(unicode)
4696 text = (LPWSTR)lParam;
4697 else if (lParam)
4699 LPCSTR textA = (LPCSTR)lParam;
4700 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4701 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4702 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4705 EDIT_EM_SetSel(wnd, es, 0, (UINT)-1, FALSE);
4706 if (text) {
4707 TRACE("%s\n", debugstr_w(text));
4708 EDIT_EM_ReplaceSel(wnd, es, FALSE, text, FALSE);
4709 if(!unicode)
4710 HeapFree(GetProcessHeap(), 0, text);
4711 } else {
4712 static const WCHAR empty_stringW[] = {0};
4713 TRACE("<NULL>\n");
4714 EDIT_EM_ReplaceSel(wnd, es, FALSE, empty_stringW, FALSE);
4716 es->x_offset = 0;
4717 es->flags &= ~EF_MODIFIED;
4718 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
4719 /* Send the notification after the selection start and end have been set
4720 * edit control doesn't send notification on WM_SETTEXT
4721 * if it is multiline, or it is part of combobox
4723 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4724 EDIT_NOTIFY_PARENT(es, EN_CHANGE, "EN_CHANGE");
4725 EDIT_EM_ScrollCaret(wnd, es);
4729 /*********************************************************************
4731 * WM_SIZE
4734 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4736 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4737 RECT rc;
4738 TRACE("width = %d, height = %d\n", width, height);
4739 SetRect(&rc, 0, 0, width, height);
4740 EDIT_SetRectNP(wnd, es, &rc);
4741 EDIT_UpdateText(wnd, NULL, TRUE);
4746 /*********************************************************************
4748 * WM_STYLECHANGED
4750 * This message is sent by SetWindowLong on having changed either the Style
4751 * or the extended style.
4753 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4755 * See also EDIT_WM_NCCreate
4757 * It appears that the Windows version of the edit control allows the style
4758 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4759 * style variable which will generally be different. In this function we
4760 * update the internal style based on what changed in the externally visible
4761 * style.
4763 * Much of this content as based upon the MSDN, especially:
4764 * Platform SDK Documentation -> User Interface Services ->
4765 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4766 * Edit Control Styles
4768 static LRESULT EDIT_WM_StyleChanged (WND *wnd,
4769 EDITSTATE *es,
4770 WPARAM which,
4771 const STYLESTRUCT *style)
4773 if (GWL_STYLE == which) {
4774 DWORD style_change_mask;
4775 DWORD new_style;
4776 /* Only a subset of changes can be applied after the control
4777 * has been created.
4779 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4780 ES_NUMBER;
4781 if (es->style & ES_MULTILINE)
4782 style_change_mask |= ES_WANTRETURN;
4784 new_style = style->styleNew & style_change_mask;
4786 /* Number overrides lowercase overrides uppercase (at least it
4787 * does in Win95). However I'll bet that ES_NUMBER would be
4788 * invalid under Win 3.1.
4790 if (new_style & ES_NUMBER) {
4791 ; /* do not override the ES_NUMBER */
4792 } else if (new_style & ES_LOWERCASE) {
4793 new_style &= ~ES_UPPERCASE;
4796 es->style = (es->style & ~style_change_mask) | new_style;
4797 } else if (GWL_EXSTYLE == which) {
4798 ; /* FIXME - what is needed here */
4799 } else {
4800 WARN ("Invalid style change %d\n",which);
4803 return 0;
4806 /*********************************************************************
4808 * WM_SYSKEYDOWN
4811 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4813 if ((key == VK_BACK) && (key_data & 0x2000)) {
4814 if (EDIT_EM_CanUndo(es))
4815 EDIT_EM_Undo(wnd, es);
4816 return 0;
4817 } else if (key == VK_UP || key == VK_DOWN) {
4818 if (EDIT_CheckCombo(wnd, es, WM_SYSKEYDOWN, key))
4819 return 0;
4821 return DefWindowProcW(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4825 /*********************************************************************
4827 * WM_TIMER
4830 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es)
4832 if (es->region_posx < 0) {
4833 EDIT_MoveBackward(wnd, es, TRUE);
4834 } else if (es->region_posx > 0) {
4835 EDIT_MoveForward(wnd, es, TRUE);
4838 * FIXME: gotta do some vertical scrolling here, like
4839 * EDIT_EM_LineScroll(wnd, 0, 1);
4843 /*********************************************************************
4845 * WM_VSCROLL
4848 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos)
4850 INT dy;
4852 if (!(es->style & ES_MULTILINE))
4853 return 0;
4855 if (!(es->style & ES_AUTOVSCROLL))
4856 return 0;
4858 dy = 0;
4859 switch (action) {
4860 case SB_LINEUP:
4861 case SB_LINEDOWN:
4862 case SB_PAGEUP:
4863 case SB_PAGEDOWN:
4864 TRACE("action %d\n", action);
4865 EDIT_EM_Scroll(wnd, es, action);
4866 return 0;
4867 case SB_TOP:
4868 TRACE("SB_TOP\n");
4869 dy = -es->y_offset;
4870 break;
4871 case SB_BOTTOM:
4872 TRACE("SB_BOTTOM\n");
4873 dy = es->line_count - 1 - es->y_offset;
4874 break;
4875 case SB_THUMBTRACK:
4876 TRACE("SB_THUMBTRACK %d\n", pos);
4877 es->flags |= EF_VSCROLL_TRACK;
4878 if(es->style & WS_VSCROLL)
4879 dy = pos - es->y_offset;
4880 else
4882 /* Assume default scroll range 0-100 */
4883 INT vlc, new_y;
4884 /* Sanity check */
4885 if(pos < 0 || pos > 100) return 0;
4886 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4887 new_y = pos * (es->line_count - vlc) / 100;
4888 dy = es->line_count ? (new_y - es->y_offset) : 0;
4889 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4890 es->line_count, es->y_offset, pos, dy);
4892 break;
4893 case SB_THUMBPOSITION:
4894 TRACE("SB_THUMBPOSITION %d\n", pos);
4895 es->flags &= ~EF_VSCROLL_TRACK;
4896 if(es->style & WS_VSCROLL)
4897 dy = pos - es->y_offset;
4898 else
4900 /* Assume default scroll range 0-100 */
4901 INT vlc, new_y;
4902 /* Sanity check */
4903 if(pos < 0 || pos > 100) return 0;
4904 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4905 new_y = pos * (es->line_count - vlc) / 100;
4906 dy = es->line_count ? (new_y - es->y_offset) : 0;
4907 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4908 es->line_count, es->y_offset, pos, dy);
4910 if (!dy)
4912 /* force scroll info update */
4913 EDIT_UpdateScrollInfo(wnd, es);
4914 EDIT_NOTIFY_PARENT(es, EN_VSCROLL, "EN_VSCROLL");
4916 break;
4917 case SB_ENDSCROLL:
4918 TRACE("SB_ENDSCROLL\n");
4919 break;
4921 * FIXME : the next two are undocumented !
4922 * Are we doing the right thing ?
4923 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4924 * although it's also a regular control message.
4926 case EM_GETTHUMB: /* this one is used by NT notepad */
4927 case EM_GETTHUMB16:
4929 LRESULT ret;
4930 if(wnd->dwStyle & WS_VSCROLL)
4931 ret = GetScrollPos(wnd->hwndSelf, SB_VERT);
4932 else
4934 /* Assume default scroll range 0-100 */
4935 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4936 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4938 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4939 return ret;
4941 case EM_LINESCROLL16:
4942 TRACE("EM_LINESCROLL16 %d\n", pos);
4943 dy = pos;
4944 break;
4946 default:
4947 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4948 action, action);
4949 return 0;
4951 if (dy)
4952 EDIT_EM_LineScroll(wnd, es, 0, dy);
4953 return 0;
4956 /*********************************************************************
4958 * EDIT_UpdateText
4961 static void EDIT_UpdateTextRegion(WND *wnd, HRGN hrgn, BOOL bErase)
4963 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4965 if (es->flags & EF_UPDATE)
4966 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4968 InvalidateRgn(wnd->hwndSelf, hrgn, bErase);
4972 /*********************************************************************
4974 * EDIT_UpdateText
4977 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4979 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4981 if (es->flags & EF_UPDATE)
4982 EDIT_NOTIFY_PARENT(es, EN_UPDATE, "EN_UPDATE");
4984 InvalidateRect(wnd->hwndSelf, rc, bErase);