Fixed a couple of bugs in _except_handler3.
[wine/multimedia.git] / controls / edit.c
blob46fe497a0fbabfb35f53079838357d0cd8685318
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(hwnd,hdc) \
134 (SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC, \
135 (WPARAM)(hdc), (LPARAM)(hwnd)))
136 #define EDIT_SEND_CTLCOLOR(hwnd,hdc) \
137 (SendMessageW(GetParent(hwnd), WM_CTLCOLOREDIT, \
138 (WPARAM)(hdc), (LPARAM)(hwnd)))
139 #define EDIT_NOTIFY_PARENT(hwnd, 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(GetWindowLongA((hwnd),GWL_ID), wNotifyCode), \
145 (LPARAM)(hwnd)); \
146 } while(0)
147 #define DPRINTF_EDIT_MSG16(str) \
148 TRACE(\
149 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
150 hwnd, (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 hwnd, (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(HWND hwnd, EDITSTATE *es);
171 static inline void EDIT_WM_Cut(HWND hwnd, EDITSTATE *es);
174 * Helper functions only valid for one type of control
176 static void EDIT_BuildLineDefs_ML(HWND hwnd, EDITSTATE *es, INT iStart, INT iEnd, INT delta, HRGN hrgn);
177 static void EDIT_CalcLineWidth_SL(HWND hwnd, EDITSTATE *es);
178 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es);
179 static void EDIT_MoveDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
180 static void EDIT_MovePageDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
181 static void EDIT_MovePageUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend);
182 static void EDIT_MoveUp_ML(HWND hwnd, 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(HWND hwnd, 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(HWND hwnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
190 static void EDIT_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end);
191 static void EDIT_LockBuffer(HWND hwnd, EDITSTATE *es);
192 static BOOL EDIT_MakeFit(HWND hwnd, EDITSTATE *es, UINT size);
193 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size);
194 static void EDIT_MoveBackward(HWND hwnd, EDITSTATE *es, BOOL extend);
195 static void EDIT_MoveEnd(HWND hwnd, EDITSTATE *es, BOOL extend);
196 static void EDIT_MoveForward(HWND hwnd, EDITSTATE *es, BOOL extend);
197 static void EDIT_MoveHome(HWND hwnd, EDITSTATE *es, BOOL extend);
198 static void EDIT_MoveWordBackward(HWND hwnd, EDITSTATE *es, BOOL extend);
199 static void EDIT_MoveWordForward(HWND hwnd, EDITSTATE *es, BOOL extend);
200 static void EDIT_PaintLine(HWND hwnd, 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(HWND hwnd, EDITSTATE *es, INT pos, BOOL after_wrap);
203 static void EDIT_SetRectNP(HWND hwnd, EDITSTATE *es, LPRECT lprc);
204 static void EDIT_UnlockBuffer(HWND hwnd, EDITSTATE *es, BOOL force);
205 static void EDIT_UpdateScrollInfo(HWND hwnd, 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(HWND hwnd, 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(HWND hwnd, 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(HWND hwnd, 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(HWND hwnd, EDITSTATE *es, INT dx, INT dy);
221 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy);
222 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap);
223 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update);
224 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action);
225 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es);
226 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc);
227 static void EDIT_EM_SetHandle16(HWND hwnd, 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(HWND hwnd, EDITSTATE *es, WCHAR c);
231 static void EDIT_EM_SetSel(HWND hwnd, 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(HWND hwnd, EDITSTATE *es, LPARAM lParam);
235 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
236 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es);
238 * WM_XXX message handlers
240 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c);
241 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
242 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y);
243 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es);
244 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name);
245 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es);
246 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc);
247 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode);
248 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos);
249 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key);
250 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es);
251 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es);
252 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y);
253 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es);
254 static LRESULT EDIT_WM_MButtonDown(HWND hwnd);
255 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y);
256 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode);
257 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam);
258 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es);
259 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es);
260 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw);
261 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode);
262 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height);
263 static LRESULT EDIT_WM_StyleChanged (HWND hwnd, EDITSTATE *es, WPARAM which, const STYLESTRUCT *style);
264 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data);
265 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es);
266 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos);
267 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase);
268 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, 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(HWND hwnd, 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(hwnd, es, TRUE, empty_stringW, TRUE);
328 /*********************************************************************
330 * WM_CUT
333 static inline void EDIT_WM_Cut(HWND hwnd, EDITSTATE *es)
335 EDIT_WM_Copy(hwnd, es);
336 EDIT_WM_Clear(hwnd, 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_common
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_common( HWND hwnd, UINT msg,
390 WPARAM wParam, LPARAM lParam, BOOL unicode )
392 EDITSTATE *es = (EDITSTATE *)GetWindowLongA( hwnd, 0 );
393 LRESULT result = 0;
395 switch (msg) {
396 case WM_DESTROY:
397 DPRINTF_EDIT_MSG32("WM_DESTROY");
398 if (es) EDIT_WM_Destroy(hwnd, 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(hwnd, cs->style, cs->hwndParent, TRUE);
409 else
411 LPCREATESTRUCTA cs = (LPCREATESTRUCTA)lParam;
412 result = EDIT_WM_NCCreate(hwnd, cs->style, cs->hwndParent, FALSE);
414 goto END;
417 if (!es)
419 if(unicode)
420 result = DefWindowProcW(hwnd, msg, wParam, lParam);
421 else
422 result = DefWindowProcA(hwnd, msg, wParam, lParam);
423 goto END;
427 EDIT_LockBuffer(hwnd, 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(hwnd, es, (UINT)-1, 0, FALSE);
443 else
444 EDIT_EM_SetSel(hwnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
445 if (!wParam)
446 EDIT_EM_ScrollCaret(hwnd, es);
447 result = 1;
448 break;
449 case EM_SETSEL:
450 DPRINTF_EDIT_MSG32("EM_SETSEL");
451 EDIT_EM_SetSel(hwnd, es, wParam, lParam, FALSE);
452 EDIT_EM_ScrollCaret(hwnd, 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(hwnd, es, &rc);
473 EDIT_UpdateText(hwnd, es, NULL, TRUE);
475 break;
476 case EM_SETRECT:
477 DPRINTF_EDIT_MSG32("EM_SETRECT");
478 if ((es->style & ES_MULTILINE) && lParam) {
479 EDIT_SetRectNP(hwnd, es, (LPRECT)lParam);
480 EDIT_UpdateText(hwnd, es, 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(hwnd, es, &rc);
491 break;
492 case EM_SETRECTNP:
493 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
494 if ((es->style & ES_MULTILINE) && lParam)
495 EDIT_SetRectNP(hwnd, 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(hwnd, 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(hwnd, 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(hwnd, 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(hwnd, es, (HLOCAL16)wParam);
565 break;
566 case EM_SETHANDLE:
567 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
568 EDIT_EM_SetHandle(hwnd, es, (HLOCAL)wParam);
569 break;
571 case EM_GETHANDLE16:
572 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
573 result = (LRESULT)EDIT_EM_GetHandle16(hwnd, 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(hwnd, 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 result = DefWindowProcW(hwnd, msg, wParam, lParam);
596 break;
598 case WM_USER+16:
599 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
600 /* fall through */
601 case 0x00c0:
602 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
603 result = DefWindowProcW(hwnd, msg, wParam, lParam);
604 break;
606 case EM_LINELENGTH16:
607 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
608 /* fall through */
609 case EM_LINELENGTH:
610 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
611 result = (LRESULT)EDIT_EM_LineLength(es, (INT)wParam);
612 break;
614 case EM_REPLACESEL16:
615 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
616 lParam = (LPARAM)MapSL(lParam);
617 unicode = FALSE; /* 16-bit message is always ascii */
618 /* fall through */
619 case EM_REPLACESEL:
621 LPWSTR textW;
622 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
624 if(unicode)
625 textW = (LPWSTR)lParam;
626 else
628 LPSTR textA = (LPSTR)lParam;
629 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
630 if((textW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
631 MultiByteToWideChar(CP_ACP, 0, textA, -1, textW, countW);
634 EDIT_EM_ReplaceSel(hwnd, es, (BOOL)wParam, textW, TRUE);
635 result = 1;
637 if(!unicode)
638 HeapFree(GetProcessHeap(), 0, textW);
639 break;
641 /* message 0x00c3 missing from specs */
643 case WM_USER+19:
644 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
645 /* fall through */
646 case 0x00c3:
647 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
648 result = DefWindowProcW(hwnd, msg, wParam, lParam);
649 break;
651 case EM_GETLINE16:
652 DPRINTF_EDIT_MSG16("EM_GETLINE");
653 lParam = (LPARAM)MapSL(lParam);
654 unicode = FALSE; /* 16-bit message is always ascii */
655 /* fall through */
656 case EM_GETLINE:
657 DPRINTF_EDIT_MSG32("EM_GETLINE");
658 result = (LRESULT)EDIT_EM_GetLine(es, (INT)wParam, lParam, unicode);
659 break;
661 case EM_LIMITTEXT16:
662 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
663 /* fall through */
664 case EM_SETLIMITTEXT:
665 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
666 EDIT_EM_SetLimitText(es, (INT)wParam);
667 break;
669 case EM_CANUNDO16:
670 DPRINTF_EDIT_MSG16("EM_CANUNDO");
671 /* fall through */
672 case EM_CANUNDO:
673 DPRINTF_EDIT_MSG32("EM_CANUNDO");
674 result = (LRESULT)EDIT_EM_CanUndo(es);
675 break;
677 case EM_UNDO16:
678 DPRINTF_EDIT_MSG16("EM_UNDO");
679 /* fall through */
680 case EM_UNDO:
681 /* fall through */
682 case WM_UNDO:
683 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
684 result = (LRESULT)EDIT_EM_Undo(hwnd, es);
685 break;
687 case EM_FMTLINES16:
688 DPRINTF_EDIT_MSG16("EM_FMTLINES");
689 /* fall through */
690 case EM_FMTLINES:
691 DPRINTF_EDIT_MSG32("EM_FMTLINES");
692 result = (LRESULT)EDIT_EM_FmtLines(es, (BOOL)wParam);
693 break;
695 case EM_LINEFROMCHAR16:
696 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
697 /* fall through */
698 case EM_LINEFROMCHAR:
699 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
700 result = (LRESULT)EDIT_EM_LineFromChar(es, (INT)wParam);
701 break;
703 /* message 0x00ca missing from specs */
705 case WM_USER+26:
706 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
707 /* fall through */
708 case 0x00ca:
709 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
710 result = DefWindowProcW(hwnd, msg, wParam, lParam);
711 break;
713 case EM_SETTABSTOPS16:
714 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
715 result = (LRESULT)EDIT_EM_SetTabStops16(es, (INT)wParam, MapSL(lParam));
716 break;
717 case EM_SETTABSTOPS:
718 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
719 result = (LRESULT)EDIT_EM_SetTabStops(es, (INT)wParam, (LPINT)lParam);
720 break;
722 case EM_SETPASSWORDCHAR16:
723 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
724 unicode = FALSE; /* 16-bit message is always ascii */
725 /* fall through */
726 case EM_SETPASSWORDCHAR:
728 WCHAR charW = 0;
729 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
731 if(unicode)
732 charW = (WCHAR)wParam;
733 else
735 CHAR charA = wParam;
736 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
739 EDIT_EM_SetPasswordChar(hwnd, es, charW);
740 break;
743 case EM_EMPTYUNDOBUFFER16:
744 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
745 /* fall through */
746 case EM_EMPTYUNDOBUFFER:
747 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
748 EDIT_EM_EmptyUndoBuffer(es);
749 break;
751 case EM_GETFIRSTVISIBLELINE16:
752 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
753 result = es->y_offset;
754 break;
755 case EM_GETFIRSTVISIBLELINE:
756 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
757 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
758 break;
760 case EM_SETREADONLY16:
761 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
762 /* fall through */
763 case EM_SETREADONLY:
764 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
765 if (wParam) {
766 SetWindowLongA( hwnd, GWL_STYLE,
767 GetWindowLongA( hwnd, GWL_STYLE ) | ES_READONLY );
768 es->style |= ES_READONLY;
769 } else {
770 SetWindowLongA( hwnd, GWL_STYLE,
771 GetWindowLongA( hwnd, GWL_STYLE ) & ~ES_READONLY );
772 es->style &= ~ES_READONLY;
774 result = 1;
775 break;
777 case EM_SETWORDBREAKPROC16:
778 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
779 EDIT_EM_SetWordBreakProc16(hwnd, es, (EDITWORDBREAKPROC16)lParam);
780 break;
781 case EM_SETWORDBREAKPROC:
782 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
783 EDIT_EM_SetWordBreakProc(hwnd, es, lParam);
784 break;
786 case EM_GETWORDBREAKPROC16:
787 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
788 result = (LRESULT)es->word_break_proc16;
789 break;
790 case EM_GETWORDBREAKPROC:
791 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
792 result = (LRESULT)es->word_break_proc;
793 break;
795 case EM_GETPASSWORDCHAR16:
796 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
797 unicode = FALSE; /* 16-bit message is always ascii */
798 /* fall through */
799 case EM_GETPASSWORDCHAR:
801 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
803 if(unicode)
804 result = es->password_char;
805 else
807 WCHAR charW = es->password_char;
808 CHAR charA = 0;
809 WideCharToMultiByte(CP_ACP, 0, &charW, 1, &charA, 1, NULL, NULL);
810 result = charA;
812 break;
815 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
817 case EM_SETMARGINS:
818 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
819 EDIT_EM_SetMargins(es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
820 break;
822 case EM_GETMARGINS:
823 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
824 result = MAKELONG(es->left_margin, es->right_margin);
825 break;
827 case EM_GETLIMITTEXT:
828 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
829 result = es->buffer_limit;
830 break;
832 case EM_POSFROMCHAR:
833 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
834 result = EDIT_EM_PosFromChar(hwnd, es, (INT)wParam, FALSE);
835 break;
837 case EM_CHARFROMPOS:
838 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
839 result = EDIT_EM_CharFromPos(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
840 break;
842 /* End of the EM_ messages which were in numerical order; what order
843 * are these in? vaguely alphabetical?
846 case WM_GETDLGCODE:
847 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
848 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
850 if (lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
852 int vk = (int)((LPMSG)lParam)->wParam;
854 if (vk == VK_RETURN && (GetWindowLongA( hwnd, GWL_STYLE ) & ES_WANTRETURN))
856 result |= DLGC_WANTMESSAGE;
858 else if (es->hwndListBox && (vk == VK_RETURN || vk == VK_ESCAPE))
860 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
861 result |= DLGC_WANTMESSAGE;
864 break;
866 case WM_CHAR:
868 WCHAR charW;
869 DPRINTF_EDIT_MSG32("WM_CHAR");
871 if(unicode)
872 charW = wParam;
873 else
875 CHAR charA = wParam;
876 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &charW, 1);
879 if ((charW == VK_RETURN || charW == VK_ESCAPE) && es->hwndListBox)
881 if (SendMessageW(GetParent(hwnd), CB_GETDROPPEDSTATE, 0, 0))
882 SendMessageW(GetParent(hwnd), WM_KEYDOWN, charW, 0);
883 break;
885 EDIT_WM_Char(hwnd, es, charW);
886 break;
889 case WM_CLEAR:
890 DPRINTF_EDIT_MSG32("WM_CLEAR");
891 EDIT_WM_Clear(hwnd, es);
892 break;
894 case WM_COMMAND:
895 DPRINTF_EDIT_MSG32("WM_COMMAND");
896 EDIT_WM_Command(hwnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
897 break;
899 case WM_CONTEXTMENU:
900 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
901 EDIT_WM_ContextMenu(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
902 break;
904 case WM_COPY:
905 DPRINTF_EDIT_MSG32("WM_COPY");
906 EDIT_WM_Copy(hwnd, es);
907 break;
909 case WM_CREATE:
910 DPRINTF_EDIT_MSG32("WM_CREATE");
911 if(unicode)
912 result = EDIT_WM_Create(hwnd, es, ((LPCREATESTRUCTW)lParam)->lpszName);
913 else
915 LPCSTR nameA = ((LPCREATESTRUCTA)lParam)->lpszName;
916 LPWSTR nameW = NULL;
917 if(nameA)
919 INT countW = MultiByteToWideChar(CP_ACP, 0, nameA, -1, NULL, 0);
920 if((nameW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
921 MultiByteToWideChar(CP_ACP, 0, nameA, -1, nameW, countW);
923 result = EDIT_WM_Create(hwnd, es, nameW);
924 if(nameW)
925 HeapFree(GetProcessHeap(), 0, nameW);
927 break;
929 case WM_CUT:
930 DPRINTF_EDIT_MSG32("WM_CUT");
931 EDIT_WM_Cut(hwnd, es);
932 break;
934 case WM_ENABLE:
935 DPRINTF_EDIT_MSG32("WM_ENABLE");
936 es->bEnableState = (BOOL) wParam;
937 EDIT_UpdateText(hwnd, es, NULL, TRUE);
938 break;
940 case WM_ERASEBKGND:
941 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
942 result = EDIT_WM_EraseBkGnd(hwnd, es, (HDC)wParam);
943 break;
945 case WM_GETFONT:
946 DPRINTF_EDIT_MSG32("WM_GETFONT");
947 result = (LRESULT)es->font;
948 break;
950 case WM_GETTEXT:
951 DPRINTF_EDIT_MSG32("WM_GETTEXT");
952 result = (LRESULT)EDIT_WM_GetText(es, (INT)wParam, lParam, unicode);
953 break;
955 case WM_GETTEXTLENGTH:
956 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
957 result = strlenW(es->text);
958 break;
960 case WM_HSCROLL:
961 DPRINTF_EDIT_MSG32("WM_HSCROLL");
962 result = EDIT_WM_HScroll(hwnd, es, LOWORD(wParam), SHIWORD(wParam));
963 break;
965 case WM_KEYDOWN:
966 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
967 result = EDIT_WM_KeyDown(hwnd, es, (INT)wParam);
968 break;
970 case WM_KILLFOCUS:
971 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
972 result = EDIT_WM_KillFocus(hwnd, es);
973 break;
975 case WM_LBUTTONDBLCLK:
976 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
977 result = EDIT_WM_LButtonDblClk(hwnd, es);
978 break;
980 case WM_LBUTTONDOWN:
981 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
982 result = EDIT_WM_LButtonDown(hwnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
983 break;
985 case WM_LBUTTONUP:
986 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
987 result = EDIT_WM_LButtonUp(hwnd, es);
988 break;
990 case WM_MBUTTONDOWN:
991 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
992 result = EDIT_WM_MButtonDown(hwnd);
993 break;
995 case WM_MOUSEACTIVATE:
997 * FIXME: maybe DefWindowProc() screws up, but it seems that
998 * modeless dialog boxes need this. If we don't do this, the focus
999 * will _not_ be set by DefWindowProc() for edit controls in a
1000 * modeless dialog box ???
1002 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
1003 SetFocus(hwnd);
1004 result = MA_ACTIVATE;
1005 break;
1007 case WM_MOUSEMOVE:
1009 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
1011 result = EDIT_WM_MouseMove(hwnd, es, SLOWORD(lParam), SHIWORD(lParam));
1012 break;
1014 case WM_PAINT:
1015 DPRINTF_EDIT_MSG32("WM_PAINT");
1016 EDIT_WM_Paint(hwnd, es, wParam);
1017 break;
1019 case WM_PASTE:
1020 DPRINTF_EDIT_MSG32("WM_PASTE");
1021 EDIT_WM_Paste(hwnd, es);
1022 break;
1024 case WM_SETFOCUS:
1025 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
1026 EDIT_WM_SetFocus(hwnd, es);
1027 break;
1029 case WM_SETFONT:
1030 DPRINTF_EDIT_MSG32("WM_SETFONT");
1031 EDIT_WM_SetFont(hwnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
1032 break;
1034 case WM_SETREDRAW:
1035 /* FIXME: actually set an internal flag and behave accordingly */
1036 break;
1038 case WM_SETTEXT:
1039 DPRINTF_EDIT_MSG32("WM_SETTEXT");
1040 EDIT_WM_SetText(hwnd, es, lParam, unicode);
1041 result = TRUE;
1042 break;
1044 case WM_SIZE:
1045 DPRINTF_EDIT_MSG32("WM_SIZE");
1046 EDIT_WM_Size(hwnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
1047 break;
1049 case WM_STYLECHANGED:
1050 DPRINTF_EDIT_MSG32("WM_STYLECHANGED");
1051 result = EDIT_WM_StyleChanged (hwnd, es, wParam, (const STYLESTRUCT *)lParam);
1052 break;
1054 case WM_STYLECHANGING:
1055 DPRINTF_EDIT_MSG32("WM_STYLECHANGING");
1056 result = 0; /* See EDIT_WM_StyleChanged */
1057 break;
1059 case WM_SYSKEYDOWN:
1060 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
1061 result = EDIT_WM_SysKeyDown(hwnd, es, (INT)wParam, (DWORD)lParam);
1062 break;
1064 case WM_TIMER:
1065 DPRINTF_EDIT_MSG32("WM_TIMER");
1066 EDIT_WM_Timer(hwnd, es);
1067 break;
1069 case WM_VSCROLL:
1070 DPRINTF_EDIT_MSG32("WM_VSCROLL");
1071 result = EDIT_WM_VScroll(hwnd, es, LOWORD(wParam), SHIWORD(wParam));
1072 break;
1074 case WM_MOUSEWHEEL:
1076 int gcWheelDelta = 0;
1077 UINT pulScrollLines = 3;
1078 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1080 if (wParam & (MK_SHIFT | MK_CONTROL)) {
1081 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1082 break;
1084 gcWheelDelta -= SHIWORD(wParam);
1085 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1087 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
1088 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
1089 result = EDIT_EM_LineScroll(hwnd, es, 0, cLineScroll);
1092 break;
1093 default:
1094 if(unicode)
1095 result = DefWindowProcW(hwnd, msg, wParam, lParam);
1096 else
1097 result = DefWindowProcA(hwnd, msg, wParam, lParam);
1098 break;
1100 EDIT_UnlockBuffer(hwnd, es, FALSE);
1101 END:
1102 return result;
1105 /*********************************************************************
1107 * EditWndProcW (USER32.@)
1109 LRESULT WINAPI EditWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1111 if (!IsWindow( hWnd )) return 0;
1112 return EditWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1115 /*********************************************************************
1117 * EditWndProc (USER32.@)
1119 LRESULT WINAPI EditWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1121 if (!IsWindow( hWnd )) return 0;
1122 return EditWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1125 /*********************************************************************
1127 * EDIT_BuildLineDefs_ML
1129 * Build linked list of text lines.
1130 * Lines can end with '\0' (last line), a character (if it is wrapped),
1131 * a soft return '\r\r\n' or a hard return '\r\n'
1134 static void EDIT_BuildLineDefs_ML(HWND hwnd, EDITSTATE *es, INT istart, INT iend, INT delta, HRGN hrgn)
1136 HDC dc;
1137 HFONT old_font = 0;
1138 LPWSTR current_position, cp;
1139 INT fw;
1140 LINEDEF *current_line;
1141 LINEDEF *previous_line;
1142 LINEDEF *start_line;
1143 INT line_index = 0, nstart_line = 0, nstart_index = 0;
1144 INT line_count = es->line_count;
1145 INT orig_net_length;
1146 RECT rc;
1148 if (istart == iend && delta == 0)
1149 return;
1151 dc = GetDC(hwnd);
1152 if (es->font)
1153 old_font = SelectObject(dc, es->font);
1155 previous_line = NULL;
1156 current_line = es->first_line_def;
1158 /* Find starting line. istart must lie inside an existing line or
1159 * at the end of buffer */
1160 do {
1161 if (istart < current_line->index + current_line->length ||
1162 current_line->ending == END_0)
1163 break;
1165 previous_line = current_line;
1166 current_line = current_line->next;
1167 line_index++;
1168 } while (current_line);
1170 if (!current_line) /* Error occurred start is not inside previous buffer */
1172 FIXME(" modification occurred outside buffer\n");
1173 return;
1176 /* Remember start of modifications in order to calculate update region */
1177 nstart_line = line_index;
1178 nstart_index = current_line->index;
1180 /* We must start to reformat from the previous line since the modifications
1181 * may have caused the line to wrap upwards. */
1182 if (!(es->style & ES_AUTOHSCROLL) && line_index > 0)
1184 line_index--;
1185 current_line = previous_line;
1187 start_line = current_line;
1189 fw = es->format_rect.right - es->format_rect.left;
1190 current_position = es->text + current_line->index;
1191 do {
1192 if (current_line != start_line)
1194 if (!current_line || current_line->index + delta > current_position - es->text)
1196 /* The buffer has been expanded, create a new line and
1197 insert it into the link list */
1198 LINEDEF *new_line = HeapAlloc(GetProcessHeap(), 0, sizeof(LINEDEF));
1199 new_line->next = previous_line->next;
1200 previous_line->next = new_line;
1201 current_line = new_line;
1202 es->line_count++;
1204 else if (current_line->index + delta < current_position - es->text)
1206 /* The previous line merged with this line so we delete this extra entry */
1207 previous_line->next = current_line->next;
1208 HeapFree(GetProcessHeap(), 0, current_line);
1209 current_line = previous_line->next;
1210 es->line_count--;
1211 continue;
1213 else /* current_line->index + delta == current_position */
1215 if (current_position - es->text > iend)
1216 break; /* We reached end of line modifications */
1217 /* else recalulate this line */
1221 current_line->index = current_position - es->text;
1222 orig_net_length = current_line->net_length;
1224 /* Find end of line */
1225 cp = current_position;
1226 while (*cp) {
1227 if ((*cp == '\r') && (*(cp + 1) == '\n'))
1228 break;
1229 cp++;
1232 /* Mark type of line termination */
1233 if (!(*cp)) {
1234 current_line->ending = END_0;
1235 current_line->net_length = strlenW(current_position);
1236 } else if ((cp > current_position) && (*(cp - 1) == '\r')) {
1237 current_line->ending = END_SOFT;
1238 current_line->net_length = cp - current_position - 1;
1239 } else {
1240 current_line->ending = END_HARD;
1241 current_line->net_length = cp - current_position;
1244 /* Calculate line width */
1245 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1246 current_position, current_line->net_length,
1247 es->tabs_count, es->tabs));
1249 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
1250 if ((!(es->style & ES_AUTOHSCROLL)) && (current_line->width > fw)) {
1251 INT next = 0;
1252 INT prev;
1253 do {
1254 prev = next;
1255 next = EDIT_CallWordBreakProc(es, current_position - es->text,
1256 prev + 1, current_line->net_length, WB_RIGHT);
1257 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1258 current_position, next, es->tabs_count, es->tabs));
1259 } while (current_line->width <= fw);
1260 if (!prev) { /* Didn't find a line break so force a break */
1261 next = 0;
1262 do {
1263 prev = next;
1264 next++;
1265 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc,
1266 current_position, next, es->tabs_count, es->tabs));
1267 } while (current_line->width <= fw);
1268 if (!prev)
1269 prev = 1;
1272 /* If the first line we are calculating, wrapped before istart, we must
1273 * adjust istart in order for this to be reflected in the update region. */
1274 if (current_line->index == nstart_index && istart > current_line->index + prev)
1275 istart = current_line->index + prev;
1276 /* else if we are updating the previous line before the first line we
1277 * are re-caulculating and it expanded */
1278 else if (current_line == start_line &&
1279 current_line->index != nstart_index && orig_net_length < prev)
1281 /* Line expanded due to an upwards line wrap so we must partially include
1282 * previous line in update region */
1283 nstart_line = line_index;
1284 nstart_index = current_line->index;
1285 istart = current_line->index + orig_net_length;
1288 current_line->net_length = prev;
1289 current_line->ending = END_WRAP;
1290 current_line->width = (INT)LOWORD(GetTabbedTextExtentW(dc, current_position,
1291 current_line->net_length, es->tabs_count, es->tabs));
1295 /* Adjust length to include line termination */
1296 switch (current_line->ending) {
1297 case END_SOFT:
1298 current_line->length = current_line->net_length + 3;
1299 break;
1300 case END_HARD:
1301 current_line->length = current_line->net_length + 2;
1302 break;
1303 case END_WRAP:
1304 case END_0:
1305 current_line->length = current_line->net_length;
1306 break;
1308 es->text_width = max(es->text_width, current_line->width);
1309 current_position += current_line->length;
1310 previous_line = current_line;
1311 current_line = current_line->next;
1312 line_index++;
1313 } while (previous_line->ending != END_0);
1315 /* Finish adjusting line index's by delta or remove hanging lines */
1316 if (previous_line->ending == END_0)
1318 LINEDEF *pnext = NULL;
1320 previous_line->next = NULL;
1321 while (current_line)
1323 pnext = current_line->next;
1324 HeapFree(GetProcessHeap(), 0, current_line);
1325 current_line = pnext;
1326 es->line_count--;
1329 else
1331 while (current_line)
1333 current_line->index += delta;
1334 current_line = current_line->next;
1338 /* Calculate rest of modification rectangle */
1339 if (hrgn)
1341 HRGN tmphrgn;
1343 * We calculate two rectangles. One for the first line which may have
1344 * an indent with respect to the format rect. The other is a format-width
1345 * rectangle that spans the rest of the lines that changed or moved.
1347 rc.top = es->format_rect.top + nstart_line * es->line_height -
1348 (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1349 rc.bottom = rc.top + es->line_height;
1350 rc.left = es->format_rect.left + (INT)LOWORD(GetTabbedTextExtentW(dc,
1351 es->text + nstart_index, istart - nstart_index,
1352 es->tabs_count, es->tabs)) - es->x_offset; /* Adjust for horz scroll */
1353 rc.right = es->format_rect.right;
1354 SetRectRgn(hrgn, rc.left, rc.top, rc.right, rc.bottom);
1356 rc.top = rc.bottom;
1357 rc.left = es->format_rect.left;
1358 rc.right = es->format_rect.right;
1360 * If lines were added or removed we must re-paint the remainder of the
1361 * lines since the remaining lines were either shifted up or down.
1363 if (line_count < es->line_count) /* We added lines */
1364 rc.bottom = es->line_count * es->line_height;
1365 else if (line_count > es->line_count) /* We removed lines */
1366 rc.bottom = line_count * es->line_height;
1367 else
1368 rc.bottom = line_index * es->line_height;
1369 rc.bottom -= (es->y_offset * es->line_height); /* Adjust for vertical scrollbar */
1370 tmphrgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
1371 CombineRgn(hrgn, hrgn, tmphrgn, RGN_OR);
1372 DeleteObject(tmphrgn);
1375 if (es->font)
1376 SelectObject(dc, old_font);
1378 ReleaseDC(hwnd, dc);
1381 /*********************************************************************
1383 * EDIT_CalcLineWidth_SL
1386 static void EDIT_CalcLineWidth_SL(HWND hwnd, EDITSTATE *es)
1388 es->text_width = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, strlenW(es->text), FALSE));
1391 /*********************************************************************
1393 * EDIT_CallWordBreakProc
1395 * Call appropriate WordBreakProc (internal or external).
1397 * Note: The "start" argument should always be an index refering
1398 * to es->text. The actual wordbreak proc might be
1399 * 16 bit, so we can't always pass any 32 bit LPSTR.
1400 * Hence we assume that es->text is the buffer that holds
1401 * the string under examination (we can decide this for ourselves).
1404 /* ### start build ### */
1405 extern WORD CALLBACK EDIT_CallTo16_word_lwww(EDITWORDBREAKPROC16,SEGPTR,WORD,WORD,WORD);
1406 /* ### stop build ### */
1407 static INT EDIT_CallWordBreakProc(EDITSTATE *es, INT start, INT index, INT count, INT action)
1409 INT ret, iWndsLocks;
1411 /* To avoid any deadlocks, all the locks on the windows structures
1412 must be suspended before the control is passed to the application */
1413 iWndsLocks = WIN_SuspendWndsLock();
1415 if (es->word_break_proc16) {
1416 HGLOBAL16 hglob16;
1417 SEGPTR segptr;
1418 INT countA;
1420 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1421 hglob16 = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT, countA);
1422 segptr = K32WOWGlobalLock16(hglob16);
1423 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, MapSL(segptr), countA, NULL, NULL);
1424 ret = (INT)EDIT_CallTo16_word_lwww(es->word_break_proc16,
1425 segptr, index, countA, action);
1426 GlobalUnlock16(hglob16);
1427 GlobalFree16(hglob16);
1429 else if (es->word_break_proc)
1431 if(es->is_unicode)
1433 EDITWORDBREAKPROCW wbpW = (EDITWORDBREAKPROCW)es->word_break_proc;
1435 TRACE_(relay)("(UNICODE wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1436 es->word_break_proc, debugstr_wn(es->text + start, count), index, count, action);
1437 ret = wbpW(es->text + start, index, count, action);
1439 else
1441 EDITWORDBREAKPROCA wbpA = (EDITWORDBREAKPROCA)es->word_break_proc;
1442 INT countA;
1443 CHAR *textA;
1445 countA = WideCharToMultiByte(CP_ACP, 0, es->text + start, count, NULL, 0, NULL, NULL);
1446 textA = HeapAlloc(GetProcessHeap(), 0, countA);
1447 WideCharToMultiByte(CP_ACP, 0, es->text + start, count, textA, countA, NULL, NULL);
1448 TRACE_(relay)("(ANSI wordbrk=%p,str=%s,idx=%d,cnt=%d,act=%d)\n",
1449 es->word_break_proc, debugstr_an(textA, countA), index, countA, action);
1450 ret = wbpA(textA, index, countA, action);
1451 HeapFree(GetProcessHeap(), 0, textA);
1454 else
1455 ret = EDIT_WordBreakProc(es->text + start, index, count, action);
1457 WIN_RestoreWndsLock(iWndsLocks);
1458 return ret;
1462 /*********************************************************************
1464 * EDIT_CharFromPos
1466 * Beware: This is not the function called on EM_CHARFROMPOS
1467 * The position _can_ be outside the formatting / client
1468 * rectangle
1469 * The return value is only the character index
1472 static INT EDIT_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1474 INT index;
1475 HDC dc;
1476 HFONT old_font = 0;
1478 if (es->style & ES_MULTILINE) {
1479 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1480 INT line_index = 0;
1481 LINEDEF *line_def = es->first_line_def;
1482 INT low, high;
1483 while ((line > 0) && line_def->next) {
1484 line_index += line_def->length;
1485 line_def = line_def->next;
1486 line--;
1488 x += es->x_offset - es->format_rect.left;
1489 if (x >= line_def->width) {
1490 if (after_wrap)
1491 *after_wrap = (line_def->ending == END_WRAP);
1492 return line_index + line_def->net_length;
1494 if (x <= 0) {
1495 if (after_wrap)
1496 *after_wrap = FALSE;
1497 return line_index;
1499 dc = GetDC(hwnd);
1500 if (es->font)
1501 old_font = SelectObject(dc, es->font);
1502 low = line_index + 1;
1503 high = line_index + line_def->net_length + 1;
1504 while (low < high - 1)
1506 INT mid = (low + high) / 2;
1507 if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1508 else low = mid;
1510 index = low;
1512 if (after_wrap)
1513 *after_wrap = ((index == line_index + line_def->net_length) &&
1514 (line_def->ending == END_WRAP));
1515 } else {
1516 LPWSTR text;
1517 SIZE size;
1518 if (after_wrap)
1519 *after_wrap = FALSE;
1520 x -= es->format_rect.left;
1521 if (!x)
1522 return es->x_offset;
1523 text = EDIT_GetPasswordPointer_SL(es);
1524 dc = GetDC(hwnd);
1525 if (es->font)
1526 old_font = SelectObject(dc, es->font);
1527 if (x < 0)
1529 INT low = 0;
1530 INT high = es->x_offset;
1531 while (low < high - 1)
1533 INT mid = (low + high) / 2;
1534 GetTextExtentPoint32W( dc, text + mid,
1535 es->x_offset - mid, &size );
1536 if (size.cx > -x) low = mid;
1537 else high = mid;
1539 index = low;
1541 else
1543 INT low = es->x_offset;
1544 INT high = strlenW(es->text) + 1;
1545 while (low < high - 1)
1547 INT mid = (low + high) / 2;
1548 GetTextExtentPoint32W( dc, text + es->x_offset,
1549 mid - es->x_offset, &size );
1550 if (size.cx > x) high = mid;
1551 else low = mid;
1553 index = low;
1555 if (es->style & ES_PASSWORD)
1556 HeapFree(GetProcessHeap(), 0, text);
1558 if (es->font)
1559 SelectObject(dc, old_font);
1560 ReleaseDC(hwnd, dc);
1561 return index;
1565 /*********************************************************************
1567 * EDIT_ConfinePoint
1569 * adjusts the point to be within the formatting rectangle
1570 * (so CharFromPos returns the nearest _visible_ character)
1573 static void EDIT_ConfinePoint(EDITSTATE *es, LPINT x, LPINT y)
1575 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1576 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1580 /*********************************************************************
1582 * EDIT_GetLineRect
1584 * Calculates the bounding rectangle for a line from a starting
1585 * column to an ending column.
1588 static void EDIT_GetLineRect(HWND hwnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1590 INT line_index = EDIT_EM_LineIndex(es, line);
1592 if (es->style & ES_MULTILINE)
1593 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1594 else
1595 rc->top = es->format_rect.top;
1596 rc->bottom = rc->top + es->line_height;
1597 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(hwnd, es, line_index + scol, TRUE));
1598 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(hwnd, es, line_index + ecol, TRUE));
1602 /*********************************************************************
1604 * EDIT_GetPasswordPointer_SL
1606 * note: caller should free the (optionally) allocated buffer
1609 static LPWSTR EDIT_GetPasswordPointer_SL(EDITSTATE *es)
1611 if (es->style & ES_PASSWORD) {
1612 INT len = strlenW(es->text);
1613 LPWSTR text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1614 text[len] = '\0';
1615 while(len) text[--len] = es->password_char;
1616 return text;
1617 } else
1618 return es->text;
1622 /*********************************************************************
1624 * EDIT_LockBuffer
1626 * This acts as a LOCAL_Lock(), but it locks only once. This way
1627 * you can call it whenever you like, without unlocking.
1630 static void EDIT_LockBuffer(HWND hwnd, EDITSTATE *es)
1632 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
1633 if (!es) {
1634 ERR("no EDITSTATE ... please report\n");
1635 return;
1637 if (!es->text) {
1638 CHAR *textA = NULL;
1639 UINT countA = 0;
1640 BOOL _16bit = FALSE;
1642 if(es->hloc32W)
1644 if(es->hloc32A)
1646 TRACE("Synchronizing with 32-bit ANSI buffer\n");
1647 textA = LocalLock(es->hloc32A);
1648 countA = strlen(textA) + 1;
1650 else if(es->hloc16)
1652 TRACE("Synchronizing with 16-bit ANSI buffer\n");
1653 textA = LOCAL_Lock(hInstance, es->hloc16);
1654 countA = strlen(textA) + 1;
1655 _16bit = TRUE;
1658 else {
1659 ERR("no buffer ... please report\n");
1660 return;
1663 if(textA)
1665 HLOCAL hloc32W_new;
1666 UINT countW_new = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
1667 TRACE("%d bytes translated to %d WCHARs\n", countA, countW_new);
1668 if(countW_new > es->buffer_size + 1)
1670 UINT alloc_size = ROUND_TO_GROW(countW_new * sizeof(WCHAR));
1671 TRACE("Resizing 32-bit UNICODE buffer from %d+1 to %d WCHARs\n", es->buffer_size, countW_new);
1672 hloc32W_new = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
1673 if(hloc32W_new)
1675 es->hloc32W = hloc32W_new;
1676 es->buffer_size = LocalSize(hloc32W_new)/sizeof(WCHAR) - 1;
1677 TRACE("Real new size %d+1 WCHARs\n", es->buffer_size);
1679 else
1680 WARN("FAILED! Will synchronize partially\n");
1684 /*TRACE("Locking 32-bit UNICODE buffer\n");*/
1685 es->text = LocalLock(es->hloc32W);
1687 if(textA)
1689 MultiByteToWideChar(CP_ACP, 0, textA, countA, es->text, es->buffer_size + 1);
1690 if(_16bit)
1691 LOCAL_Unlock(hInstance, es->hloc16);
1692 else
1693 LocalUnlock(es->hloc32A);
1696 es->lock_count++;
1700 /*********************************************************************
1702 * EDIT_SL_InvalidateText
1704 * Called from EDIT_InvalidateText().
1705 * Does the job for single-line controls only.
1708 static void EDIT_SL_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1710 RECT line_rect;
1711 RECT rc;
1713 EDIT_GetLineRect(hwnd, es, 0, start, end, &line_rect);
1714 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1715 EDIT_UpdateText(hwnd, es, &rc, FALSE);
1719 /*********************************************************************
1721 * EDIT_ML_InvalidateText
1723 * Called from EDIT_InvalidateText().
1724 * Does the job for multi-line controls only.
1727 static void EDIT_ML_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1729 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1730 INT sl = EDIT_EM_LineFromChar(es, start);
1731 INT el = EDIT_EM_LineFromChar(es, end);
1732 INT sc;
1733 INT ec;
1734 RECT rc1;
1735 RECT rcWnd;
1736 RECT rcLine;
1737 RECT rcUpdate;
1738 INT l;
1740 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1741 return;
1743 sc = start - EDIT_EM_LineIndex(es, sl);
1744 ec = end - EDIT_EM_LineIndex(es, el);
1745 if (sl < es->y_offset) {
1746 sl = es->y_offset;
1747 sc = 0;
1749 if (el > es->y_offset + vlc) {
1750 el = es->y_offset + vlc;
1751 ec = EDIT_EM_LineLength(es, EDIT_EM_LineIndex(es, el));
1753 GetClientRect(hwnd, &rc1);
1754 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1755 if (sl == el) {
1756 EDIT_GetLineRect(hwnd, es, sl, sc, ec, &rcLine);
1757 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1758 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1759 } else {
1760 EDIT_GetLineRect(hwnd, es, sl, sc,
1761 EDIT_EM_LineLength(es,
1762 EDIT_EM_LineIndex(es, sl)),
1763 &rcLine);
1764 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1765 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1766 for (l = sl + 1 ; l < el ; l++) {
1767 EDIT_GetLineRect(hwnd, es, l, 0,
1768 EDIT_EM_LineLength(es,
1769 EDIT_EM_LineIndex(es, l)),
1770 &rcLine);
1771 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1772 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1774 EDIT_GetLineRect(hwnd, es, el, 0, ec, &rcLine);
1775 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1776 EDIT_UpdateText(hwnd, es, &rcUpdate, FALSE);
1781 /*********************************************************************
1783 * EDIT_InvalidateText
1785 * Invalidate the text from offset start upto, but not including,
1786 * offset end. Useful for (re)painting the selection.
1787 * Regions outside the linewidth are not invalidated.
1788 * end == -1 means end == TextLength.
1789 * start and end need not be ordered.
1792 static void EDIT_InvalidateText(HWND hwnd, EDITSTATE *es, INT start, INT end)
1794 if (end == start)
1795 return;
1797 if (end == -1)
1798 end = strlenW(es->text);
1800 ORDER_INT(start, end);
1802 if (es->style & ES_MULTILINE)
1803 EDIT_ML_InvalidateText(hwnd, es, start, end);
1804 else
1805 EDIT_SL_InvalidateText(hwnd, es, start, end);
1809 /*********************************************************************
1811 * EDIT_MakeFit
1813 * Try to fit size + 1 characters in the buffer. Constrain to limits.
1816 static BOOL EDIT_MakeFit(HWND hwnd, EDITSTATE *es, UINT size)
1818 HLOCAL hNew32W;
1820 if (size <= es->buffer_size)
1821 return TRUE;
1822 if (size > es->buffer_limit) {
1823 EDIT_NOTIFY_PARENT(hwnd, es, EN_MAXTEXT, "EN_MAXTEXT");
1824 return FALSE;
1826 if (size > es->buffer_limit)
1827 size = es->buffer_limit;
1829 TRACE("trying to ReAlloc to %d+1 characters\n", size);
1831 /* Force edit to unlock it's buffer. es->text now NULL */
1832 EDIT_UnlockBuffer(hwnd, es, TRUE);
1834 if (es->hloc32W) {
1835 UINT alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1836 if ((hNew32W = LocalReAlloc(es->hloc32W, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT))) {
1837 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32W, hNew32W);
1838 es->hloc32W = hNew32W;
1839 es->buffer_size = LocalSize(hNew32W)/sizeof(WCHAR) - 1;
1843 EDIT_LockBuffer(hwnd, es);
1845 if (es->buffer_size < size) {
1846 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1847 EDIT_NOTIFY_PARENT(hwnd, es, EN_ERRSPACE, "EN_ERRSPACE");
1848 return FALSE;
1849 } else {
1850 TRACE("We now have %d+1\n", es->buffer_size);
1851 return TRUE;
1856 /*********************************************************************
1858 * EDIT_MakeUndoFit
1860 * Try to fit size + 1 bytes in the undo buffer.
1863 static BOOL EDIT_MakeUndoFit(EDITSTATE *es, UINT size)
1865 UINT alloc_size;
1867 if (size <= es->undo_buffer_size)
1868 return TRUE;
1870 TRACE("trying to ReAlloc to %d+1\n", size);
1872 alloc_size = ROUND_TO_GROW((size + 1) * sizeof(WCHAR));
1873 if ((es->undo_text = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, es->undo_text, alloc_size))) {
1874 es->undo_buffer_size = alloc_size/sizeof(WCHAR);
1875 return TRUE;
1877 else
1879 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1880 return FALSE;
1885 /*********************************************************************
1887 * EDIT_MoveBackward
1890 static void EDIT_MoveBackward(HWND hwnd, EDITSTATE *es, BOOL extend)
1892 INT e = es->selection_end;
1894 if (e) {
1895 e--;
1896 if ((es->style & ES_MULTILINE) && e &&
1897 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1898 e--;
1899 if (e && (es->text[e - 1] == '\r'))
1900 e--;
1903 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1904 EDIT_EM_ScrollCaret(hwnd, es);
1908 /*********************************************************************
1910 * EDIT_MoveDown_ML
1912 * Only for multi line controls
1913 * Move the caret one line down, on a column with the nearest
1914 * x coordinate on the screen (might be a different column).
1917 static void EDIT_MoveDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
1919 INT s = es->selection_start;
1920 INT e = es->selection_end;
1921 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1922 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
1923 INT x = SLOWORD(pos);
1924 INT y = SHIWORD(pos);
1926 e = EDIT_CharFromPos(hwnd, es, x, y + es->line_height, &after_wrap);
1927 if (!extend)
1928 s = e;
1929 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
1930 EDIT_EM_ScrollCaret(hwnd, es);
1934 /*********************************************************************
1936 * EDIT_MoveEnd
1939 static void EDIT_MoveEnd(HWND hwnd, EDITSTATE *es, BOOL extend)
1941 BOOL after_wrap = FALSE;
1942 INT e;
1944 /* Pass a high value in x to make sure of receiving the end of the line */
1945 if (es->style & ES_MULTILINE)
1946 e = EDIT_CharFromPos(hwnd, es, 0x3fffffff,
1947 HIWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1948 else
1949 e = strlenW(es->text);
1950 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, after_wrap);
1951 EDIT_EM_ScrollCaret(hwnd, es);
1955 /*********************************************************************
1957 * EDIT_MoveForward
1960 static void EDIT_MoveForward(HWND hwnd, EDITSTATE *es, BOOL extend)
1962 INT e = es->selection_end;
1964 if (es->text[e]) {
1965 e++;
1966 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1967 if (es->text[e] == '\n')
1968 e++;
1969 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1970 e += 2;
1973 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1974 EDIT_EM_ScrollCaret(hwnd, es);
1978 /*********************************************************************
1980 * EDIT_MoveHome
1982 * Home key: move to beginning of line.
1985 static void EDIT_MoveHome(HWND hwnd, EDITSTATE *es, BOOL extend)
1987 INT e;
1989 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1990 if (es->style & ES_MULTILINE)
1991 e = EDIT_CharFromPos(hwnd, es, -es->x_offset,
1992 HIWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1993 else
1994 e = 0;
1995 EDIT_EM_SetSel(hwnd, es, extend ? es->selection_start : e, e, FALSE);
1996 EDIT_EM_ScrollCaret(hwnd, es);
2000 /*********************************************************************
2002 * EDIT_MovePageDown_ML
2004 * Only for multi line controls
2005 * Move the caret one page down, on a column with the nearest
2006 * x coordinate on the screen (might be a different column).
2009 static void EDIT_MovePageDown_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2011 INT s = es->selection_start;
2012 INT e = es->selection_end;
2013 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2014 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2015 INT x = SLOWORD(pos);
2016 INT y = SHIWORD(pos);
2018 e = EDIT_CharFromPos(hwnd, es, x,
2019 y + (es->format_rect.bottom - es->format_rect.top),
2020 &after_wrap);
2021 if (!extend)
2022 s = e;
2023 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2024 EDIT_EM_ScrollCaret(hwnd, es);
2028 /*********************************************************************
2030 * EDIT_MovePageUp_ML
2032 * Only for multi line controls
2033 * Move the caret one page up, on a column with the nearest
2034 * x coordinate on the screen (might be a different column).
2037 static void EDIT_MovePageUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2039 INT s = es->selection_start;
2040 INT e = es->selection_end;
2041 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2042 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2043 INT x = SLOWORD(pos);
2044 INT y = SHIWORD(pos);
2046 e = EDIT_CharFromPos(hwnd, es, x,
2047 y - (es->format_rect.bottom - es->format_rect.top),
2048 &after_wrap);
2049 if (!extend)
2050 s = e;
2051 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2052 EDIT_EM_ScrollCaret(hwnd, es);
2056 /*********************************************************************
2058 * EDIT_MoveUp_ML
2060 * Only for multi line controls
2061 * Move the caret one line up, on a column with the nearest
2062 * x coordinate on the screen (might be a different column).
2065 static void EDIT_MoveUp_ML(HWND hwnd, EDITSTATE *es, BOOL extend)
2067 INT s = es->selection_start;
2068 INT e = es->selection_end;
2069 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
2070 LRESULT pos = EDIT_EM_PosFromChar(hwnd, es, e, after_wrap);
2071 INT x = SLOWORD(pos);
2072 INT y = SHIWORD(pos);
2074 e = EDIT_CharFromPos(hwnd, es, x, y - es->line_height, &after_wrap);
2075 if (!extend)
2076 s = e;
2077 EDIT_EM_SetSel(hwnd, es, s, e, after_wrap);
2078 EDIT_EM_ScrollCaret(hwnd, es);
2082 /*********************************************************************
2084 * EDIT_MoveWordBackward
2087 static void EDIT_MoveWordBackward(HWND hwnd, EDITSTATE *es, BOOL extend)
2089 INT s = es->selection_start;
2090 INT e = es->selection_end;
2091 INT l;
2092 INT ll;
2093 INT li;
2095 l = EDIT_EM_LineFromChar(es, e);
2096 ll = EDIT_EM_LineLength(es, e);
2097 li = EDIT_EM_LineIndex(es, l);
2098 if (e - li == 0) {
2099 if (l) {
2100 li = EDIT_EM_LineIndex(es, l - 1);
2101 e = li + EDIT_EM_LineLength(es, li);
2103 } else {
2104 e = li + (INT)EDIT_CallWordBreakProc(es,
2105 li, e - li, ll, WB_LEFT);
2107 if (!extend)
2108 s = e;
2109 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
2110 EDIT_EM_ScrollCaret(hwnd, es);
2114 /*********************************************************************
2116 * EDIT_MoveWordForward
2119 static void EDIT_MoveWordForward(HWND hwnd, EDITSTATE *es, BOOL extend)
2121 INT s = es->selection_start;
2122 INT e = es->selection_end;
2123 INT l;
2124 INT ll;
2125 INT li;
2127 l = EDIT_EM_LineFromChar(es, e);
2128 ll = EDIT_EM_LineLength(es, e);
2129 li = EDIT_EM_LineIndex(es, l);
2130 if (e - li == ll) {
2131 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
2132 e = EDIT_EM_LineIndex(es, l + 1);
2133 } else {
2134 e = li + EDIT_CallWordBreakProc(es,
2135 li, e - li + 1, ll, WB_RIGHT);
2137 if (!extend)
2138 s = e;
2139 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
2140 EDIT_EM_ScrollCaret(hwnd, es);
2144 /*********************************************************************
2146 * EDIT_PaintLine
2149 static void EDIT_PaintLine(HWND hwnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
2151 INT s = es->selection_start;
2152 INT e = es->selection_end;
2153 INT li;
2154 INT ll;
2155 INT x;
2156 INT y;
2157 LRESULT pos;
2159 if (es->style & ES_MULTILINE) {
2160 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2161 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
2162 return;
2163 } else if (line)
2164 return;
2166 TRACE("line=%d\n", line);
2168 pos = EDIT_EM_PosFromChar(hwnd, es, EDIT_EM_LineIndex(es, line), FALSE);
2169 x = SLOWORD(pos);
2170 y = SHIWORD(pos);
2171 li = EDIT_EM_LineIndex(es, line);
2172 ll = EDIT_EM_LineLength(es, li);
2173 s = es->selection_start;
2174 e = es->selection_end;
2175 ORDER_INT(s, e);
2176 s = min(li + ll, max(li, s));
2177 e = min(li + ll, max(li, e));
2178 if (rev && (s != e) &&
2179 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
2180 x += EDIT_PaintText(es, dc, x, y, line, 0, s - li, FALSE);
2181 x += EDIT_PaintText(es, dc, x, y, line, s - li, e - s, TRUE);
2182 x += EDIT_PaintText(es, dc, x, y, line, e - li, li + ll - e, FALSE);
2183 } else
2184 x += EDIT_PaintText(es, dc, x, y, line, 0, ll, FALSE);
2188 /*********************************************************************
2190 * EDIT_PaintText
2193 static INT EDIT_PaintText(EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
2195 COLORREF BkColor;
2196 COLORREF TextColor;
2197 INT ret;
2198 INT li;
2199 INT BkMode;
2200 SIZE size;
2202 if (!count)
2203 return 0;
2204 BkMode = GetBkMode(dc);
2205 BkColor = GetBkColor(dc);
2206 TextColor = GetTextColor(dc);
2207 if (rev) {
2208 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
2209 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
2210 SetBkMode( dc, OPAQUE);
2212 li = EDIT_EM_LineIndex(es, line);
2213 if (es->style & ES_MULTILINE) {
2214 ret = (INT)LOWORD(TabbedTextOutW(dc, x, y, es->text + li + col, count,
2215 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
2216 } else {
2217 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2218 TextOutW(dc, x, y, text + li + col, count);
2219 GetTextExtentPoint32W(dc, text + li + col, count, &size);
2220 ret = size.cx;
2221 if (es->style & ES_PASSWORD)
2222 HeapFree(GetProcessHeap(), 0, text);
2224 if (rev) {
2225 SetBkColor(dc, BkColor);
2226 SetTextColor(dc, TextColor);
2227 SetBkMode( dc, BkMode);
2229 return ret;
2233 /*********************************************************************
2235 * EDIT_SetCaretPos
2238 static void EDIT_SetCaretPos(HWND hwnd, EDITSTATE *es, INT pos,
2239 BOOL after_wrap)
2241 LRESULT res = EDIT_EM_PosFromChar(hwnd, es, pos, after_wrap);
2242 SetCaretPos(SLOWORD(res), SHIWORD(res));
2246 /*********************************************************************
2248 * EDIT_SetRectNP
2250 * note: this is not (exactly) the handler called on EM_SETRECTNP
2251 * it is also used to set the rect of a single line control
2254 static void EDIT_SetRectNP(HWND hwnd, EDITSTATE *es, LPRECT rc)
2256 CopyRect(&es->format_rect, rc);
2257 if (es->style & WS_BORDER) {
2258 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
2259 if(TWEAK_WineLook == WIN31_LOOK)
2260 bw += 2;
2261 es->format_rect.left += bw;
2262 es->format_rect.top += bw;
2263 es->format_rect.right -= bw;
2264 es->format_rect.bottom -= bw;
2266 es->format_rect.left += es->left_margin;
2267 es->format_rect.right -= es->right_margin;
2268 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
2269 if (es->style & ES_MULTILINE)
2271 INT fw, vlc, max_x_offset, max_y_offset;
2273 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2274 es->format_rect.bottom = es->format_rect.top + max(1, vlc) * es->line_height;
2276 /* correct es->x_offset */
2277 fw = es->format_rect.right - es->format_rect.left;
2278 max_x_offset = es->text_width - fw;
2279 if(max_x_offset < 0) max_x_offset = 0;
2280 if(es->x_offset > max_x_offset)
2281 es->x_offset = max_x_offset;
2283 /* correct es->y_offset */
2284 max_y_offset = es->line_count - vlc;
2285 if(max_y_offset < 0) max_y_offset = 0;
2286 if(es->y_offset > max_y_offset)
2287 es->y_offset = max_y_offset;
2289 /* force scroll info update */
2290 EDIT_UpdateScrollInfo(hwnd, es);
2292 else
2293 /* Windows doesn't care to fix text placement for SL controls */
2294 es->format_rect.bottom = es->format_rect.top + es->line_height;
2296 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
2297 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
2301 /*********************************************************************
2303 * EDIT_UnlockBuffer
2306 static void EDIT_UnlockBuffer(HWND hwnd, EDITSTATE *es, BOOL force)
2308 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
2310 /* Edit window might be already destroyed */
2311 if(!IsWindow(hwnd))
2313 WARN("edit hwnd %04x already destroyed\n", hwnd);
2314 return;
2317 if (!es) {
2318 ERR("no EDITSTATE ... please report\n");
2319 return;
2321 if (!es->lock_count) {
2322 ERR("lock_count == 0 ... please report\n");
2323 return;
2325 if (!es->text) {
2326 ERR("es->text == 0 ... please report\n");
2327 return;
2330 if (force || (es->lock_count == 1)) {
2331 if (es->hloc32W) {
2332 CHAR *textA = NULL;
2333 BOOL _16bit = FALSE;
2334 UINT countA = 0;
2335 UINT countW = strlenW(es->text) + 1;
2337 if(es->hloc32A)
2339 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2340 TRACE("Synchronizing with 32-bit ANSI buffer\n");
2341 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2342 countA = LocalSize(es->hloc32A);
2343 if(countA_new > countA)
2345 HLOCAL hloc32A_new;
2346 UINT alloc_size = ROUND_TO_GROW(countA_new);
2347 TRACE("Resizing 32-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2348 hloc32A_new = LocalReAlloc(es->hloc32A, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2349 if(hloc32A_new)
2351 es->hloc32A = hloc32A_new;
2352 countA = LocalSize(hloc32A_new);
2353 TRACE("Real new size %d bytes\n", countA);
2355 else
2356 WARN("FAILED! Will synchronize partially\n");
2358 textA = LocalLock(es->hloc32A);
2360 else if(es->hloc16)
2362 UINT countA_new = WideCharToMultiByte(CP_ACP, 0, es->text, countW, NULL, 0, NULL, NULL);
2363 TRACE("Synchronizing with 16-bit ANSI buffer\n");
2364 TRACE("%d WCHARs translated to %d bytes\n", countW, countA_new);
2365 countA = LOCAL_Size(hInstance, es->hloc16);
2366 if(countA_new > countA)
2368 HLOCAL16 hloc16_new;
2369 UINT alloc_size = ROUND_TO_GROW(countA_new);
2370 TRACE("Resizing 16-bit ANSI buffer from %d to %d bytes\n", countA, alloc_size);
2371 hloc16_new = LOCAL_ReAlloc(hInstance, es->hloc16, alloc_size, LMEM_MOVEABLE | LMEM_ZEROINIT);
2372 if(hloc16_new)
2374 es->hloc16 = hloc16_new;
2375 countA = LOCAL_Size(hInstance, hloc16_new);
2376 TRACE("Real new size %d bytes\n", countA);
2378 else
2379 WARN("FAILED! Will synchronize partially\n");
2381 textA = LOCAL_Lock(hInstance, es->hloc16);
2382 _16bit = TRUE;
2385 if(textA)
2387 WideCharToMultiByte(CP_ACP, 0, es->text, countW, textA, countA, NULL, NULL);
2388 if(_16bit)
2389 LOCAL_Unlock(hInstance, es->hloc16);
2390 else
2391 LocalUnlock(es->hloc32A);
2394 LocalUnlock(es->hloc32W);
2395 es->text = NULL;
2397 else {
2398 ERR("no buffer ... please report\n");
2399 return;
2402 es->lock_count--;
2406 /*********************************************************************
2408 * EDIT_UpdateScrollInfo
2411 static void EDIT_UpdateScrollInfo(HWND hwnd, EDITSTATE *es)
2413 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK))
2415 SCROLLINFO si;
2416 si.cbSize = sizeof(SCROLLINFO);
2417 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2418 si.nMin = 0;
2419 si.nMax = es->line_count - 1;
2420 si.nPage = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2421 si.nPos = es->y_offset;
2422 TRACE("SB_VERT, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2423 si.nMin, si.nMax, si.nPage, si.nPos);
2424 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
2427 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK))
2429 SCROLLINFO si;
2430 si.cbSize = sizeof(SCROLLINFO);
2431 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
2432 si.nMin = 0;
2433 si.nMax = es->text_width - 1;
2434 si.nPage = es->format_rect.right - es->format_rect.left;
2435 si.nPos = es->x_offset;
2436 TRACE("SB_HORZ, nMin=%d, nMax=%d, nPage=%d, nPos=%d\n",
2437 si.nMin, si.nMax, si.nPage, si.nPos);
2438 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
2442 /*********************************************************************
2444 * EDIT_WordBreakProc
2446 * Find the beginning of words.
2447 * Note: unlike the specs for a WordBreakProc, this function only
2448 * allows to be called without linebreaks between s[0] upto
2449 * s[count - 1]. Remember it is only called
2450 * internally, so we can decide this for ourselves.
2453 static INT CALLBACK EDIT_WordBreakProc(LPWSTR s, INT index, INT count, INT action)
2455 INT ret = 0;
2457 TRACE("s=%p, index=%d, count=%d, action=%d\n", s, index, count, action);
2459 if(!s) return 0;
2461 switch (action) {
2462 case WB_LEFT:
2463 if (!count)
2464 break;
2465 if (index)
2466 index--;
2467 if (s[index] == ' ') {
2468 while (index && (s[index] == ' '))
2469 index--;
2470 if (index) {
2471 while (index && (s[index] != ' '))
2472 index--;
2473 if (s[index] == ' ')
2474 index++;
2476 } else {
2477 while (index && (s[index] != ' '))
2478 index--;
2479 if (s[index] == ' ')
2480 index++;
2482 ret = index;
2483 break;
2484 case WB_RIGHT:
2485 if (!count)
2486 break;
2487 if (index)
2488 index--;
2489 if (s[index] == ' ')
2490 while ((index < count) && (s[index] == ' ')) index++;
2491 else {
2492 while (s[index] && (s[index] != ' ') && (index < count))
2493 index++;
2494 while ((s[index] == ' ') && (index < count)) index++;
2496 ret = index;
2497 break;
2498 case WB_ISDELIMITER:
2499 ret = (s[index] == ' ');
2500 break;
2501 default:
2502 ERR("unknown action code, please report !\n");
2503 break;
2505 return ret;
2509 /*********************************************************************
2511 * EM_CHARFROMPOS
2513 * returns line number (not index) in high-order word of result.
2514 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
2515 * to Richedit, not to the edit control. Original documentation is valid.
2516 * FIXME: do the specs mean to return -1 if outside client area or
2517 * if outside formatting rectangle ???
2520 static LRESULT EDIT_EM_CharFromPos(HWND hwnd, EDITSTATE *es, INT x, INT y)
2522 POINT pt;
2523 RECT rc;
2524 INT index;
2526 pt.x = x;
2527 pt.y = y;
2528 GetClientRect(hwnd, &rc);
2529 if (!PtInRect(&rc, pt))
2530 return -1;
2532 index = EDIT_CharFromPos(hwnd, es, x, y, NULL);
2533 return MAKELONG(index, EDIT_EM_LineFromChar(es, index));
2537 /*********************************************************************
2539 * EM_FMTLINES
2541 * Enable or disable soft breaks.
2543 static BOOL EDIT_EM_FmtLines(EDITSTATE *es, BOOL add_eol)
2545 es->flags &= ~EF_USE_SOFTBRK;
2546 if (add_eol) {
2547 es->flags |= EF_USE_SOFTBRK;
2548 FIXME("soft break enabled, not implemented\n");
2550 return add_eol;
2554 /*********************************************************************
2556 * EM_GETHANDLE
2558 * Hopefully this won't fire back at us.
2559 * We always start with a fixed buffer in the local heap.
2560 * Despite of the documentation says that the local heap is used
2561 * only if DS_LOCALEDIT flag is set, NT and 2000 always allocate
2562 * buffer on the local heap.
2565 static HLOCAL EDIT_EM_GetHandle(EDITSTATE *es)
2567 HLOCAL hLocal;
2569 if (!(es->style & ES_MULTILINE))
2570 return 0;
2572 if(es->is_unicode)
2573 hLocal = es->hloc32W;
2574 else
2576 if(!es->hloc32A)
2578 CHAR *textA;
2579 UINT countA, alloc_size;
2580 TRACE("Allocating 32-bit ANSI alias buffer\n");
2581 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2582 alloc_size = ROUND_TO_GROW(countA);
2583 if(!(es->hloc32A = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
2585 ERR("Could not allocate %d bytes for 32-bit ANSI alias buffer\n", alloc_size);
2586 return 0;
2588 textA = LocalLock(es->hloc32A);
2589 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2590 LocalUnlock(es->hloc32A);
2592 hLocal = es->hloc32A;
2595 TRACE("Returning %04X, LocalSize() = %d\n", hLocal, LocalSize(hLocal));
2596 return hLocal;
2600 /*********************************************************************
2602 * EM_GETHANDLE16
2604 * Hopefully this won't fire back at us.
2605 * We always start with a buffer in 32 bit linear memory.
2606 * However, with this message a 16 bit application requests
2607 * a handle of 16 bit local heap memory, where it expects to find
2608 * the text.
2609 * It's a pitty that from this moment on we have to use this
2610 * local heap, because applications may rely on the handle
2611 * in the future.
2613 * In this function we'll try to switch to local heap.
2615 static HLOCAL16 EDIT_EM_GetHandle16(HWND hwnd, EDITSTATE *es)
2617 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
2618 CHAR *textA;
2619 UINT countA, alloc_size;
2621 if (!(es->style & ES_MULTILINE))
2622 return 0;
2624 if (es->hloc16)
2625 return es->hloc16;
2627 if (!LOCAL_HeapSize(hInstance)) {
2628 if (!LocalInit16(hInstance, 0,
2629 GlobalSize16(hInstance))) {
2630 ERR("could not initialize local heap\n");
2631 return 0;
2633 TRACE("local heap initialized\n");
2636 countA = WideCharToMultiByte(CP_ACP, 0, es->text, -1, NULL, 0, NULL, NULL);
2637 alloc_size = ROUND_TO_GROW(countA);
2639 TRACE("Allocating 16-bit ANSI alias buffer\n");
2640 if (!(es->hloc16 = LOCAL_Alloc(hInstance, LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size))) {
2641 ERR("could not allocate new 16 bit buffer\n");
2642 return 0;
2645 if (!(textA = (LPSTR)LOCAL_Lock(hInstance, es->hloc16))) {
2646 ERR("could not lock new 16 bit buffer\n");
2647 LOCAL_Free(hInstance, es->hloc16);
2648 es->hloc16 = 0;
2649 return 0;
2652 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, countA, NULL, NULL);
2653 LOCAL_Unlock(hInstance, es->hloc16);
2655 TRACE("Returning %04X, LocalSize() = %d\n", es->hloc16, LOCAL_Size(hInstance, es->hloc16));
2656 return es->hloc16;
2660 /*********************************************************************
2662 * EM_GETLINE
2665 static INT EDIT_EM_GetLine(EDITSTATE *es, INT line, LPARAM lParam, BOOL unicode)
2667 LPWSTR src;
2668 INT line_len, dst_len;
2669 INT i;
2671 if (es->style & ES_MULTILINE) {
2672 if (line >= es->line_count)
2673 return 0;
2674 } else
2675 line = 0;
2676 i = EDIT_EM_LineIndex(es, line);
2677 src = es->text + i;
2678 line_len = EDIT_EM_LineLength(es, i);
2679 dst_len = *(WORD *)lParam;
2680 if(unicode)
2682 LPWSTR dst = (LPWSTR)lParam;
2683 if(dst_len <= line_len)
2685 memcpy(dst, src, dst_len * sizeof(WCHAR));
2686 return dst_len;
2688 else /* Append 0 if enough space */
2690 memcpy(dst, src, line_len * sizeof(WCHAR));
2691 dst[line_len] = 0;
2692 return line_len;
2695 else
2697 LPSTR dst = (LPSTR)lParam;
2698 INT ret;
2699 ret = WideCharToMultiByte(CP_ACP, 0, src, line_len, dst, dst_len, NULL, NULL);
2700 if(!ret) /* Insufficient buffer size */
2701 return dst_len;
2702 if(ret < dst_len) /* Append 0 if enough space */
2703 dst[ret] = 0;
2704 return ret;
2709 /*********************************************************************
2711 * EM_GETSEL
2714 static LRESULT EDIT_EM_GetSel(EDITSTATE *es, LPUINT start, LPUINT end)
2716 UINT s = es->selection_start;
2717 UINT e = es->selection_end;
2719 ORDER_UINT(s, e);
2720 if (start)
2721 *start = s;
2722 if (end)
2723 *end = e;
2724 return MAKELONG(s, e);
2728 /*********************************************************************
2730 * EM_GETTHUMB
2732 * FIXME: is this right ? (or should it be only VSCROLL)
2733 * (and maybe only for edit controls that really have their
2734 * own scrollbars) (and maybe only for multiline controls ?)
2735 * All in all: very poorly documented
2738 static LRESULT EDIT_EM_GetThumb(HWND hwnd, EDITSTATE *es)
2740 return MAKELONG(EDIT_WM_VScroll(hwnd, es, EM_GETTHUMB16, 0),
2741 EDIT_WM_HScroll(hwnd, es, EM_GETTHUMB16, 0));
2745 /*********************************************************************
2747 * EM_LINEFROMCHAR
2750 static INT EDIT_EM_LineFromChar(EDITSTATE *es, INT index)
2752 INT line;
2753 LINEDEF *line_def;
2755 if (!(es->style & ES_MULTILINE))
2756 return 0;
2757 if (index > (INT)strlenW(es->text))
2758 return es->line_count - 1;
2759 if (index == -1)
2760 index = min(es->selection_start, es->selection_end);
2762 line = 0;
2763 line_def = es->first_line_def;
2764 index -= line_def->length;
2765 while ((index >= 0) && line_def->next) {
2766 line++;
2767 line_def = line_def->next;
2768 index -= line_def->length;
2770 return line;
2774 /*********************************************************************
2776 * EM_LINEINDEX
2779 static INT EDIT_EM_LineIndex(EDITSTATE *es, INT line)
2781 INT line_index;
2782 LINEDEF *line_def;
2784 if (!(es->style & ES_MULTILINE))
2785 return 0;
2786 if (line >= es->line_count)
2787 return -1;
2789 line_index = 0;
2790 line_def = es->first_line_def;
2791 if (line == -1) {
2792 INT index = es->selection_end - line_def->length;
2793 while ((index >= 0) && line_def->next) {
2794 line_index += line_def->length;
2795 line_def = line_def->next;
2796 index -= line_def->length;
2798 } else {
2799 while (line > 0) {
2800 line_index += line_def->length;
2801 line_def = line_def->next;
2802 line--;
2805 return line_index;
2809 /*********************************************************************
2811 * EM_LINELENGTH
2814 static INT EDIT_EM_LineLength(EDITSTATE *es, INT index)
2816 LINEDEF *line_def;
2818 if (!(es->style & ES_MULTILINE))
2819 return strlenW(es->text);
2821 if (index == -1) {
2822 /* get the number of remaining non-selected chars of selected lines */
2823 INT32 li;
2824 INT32 count;
2825 li = EDIT_EM_LineFromChar(es, es->selection_start);
2826 /* # chars before start of selection area */
2827 count = es->selection_start - EDIT_EM_LineIndex(es, li);
2828 li = EDIT_EM_LineFromChar(es, es->selection_end);
2829 /* # chars after end of selection */
2830 count += EDIT_EM_LineIndex(es, li) +
2831 EDIT_EM_LineLength(es, li) - es->selection_end;
2832 return count;
2834 line_def = es->first_line_def;
2835 index -= line_def->length;
2836 while ((index >= 0) && line_def->next) {
2837 line_def = line_def->next;
2838 index -= line_def->length;
2840 return line_def->net_length;
2844 /*********************************************************************
2846 * EM_LINESCROLL
2848 * NOTE: dx is in average character widths, dy - in lines;
2851 static BOOL EDIT_EM_LineScroll(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2853 if (!(es->style & ES_MULTILINE))
2854 return FALSE;
2856 dx *= es->char_width;
2857 return EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
2860 /*********************************************************************
2862 * EDIT_EM_LineScroll_internal
2864 * Version of EDIT_EM_LineScroll for internal use.
2865 * It doesn't refuse if ES_MULTILINE is set and assumes that
2866 * dx is in pixels, dy - in lines.
2869 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2871 INT nyoff;
2872 INT x_offset_in_pixels;
2874 if (es->style & ES_MULTILINE)
2876 x_offset_in_pixels = es->x_offset;
2878 else
2880 dy = 0;
2881 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->x_offset, FALSE));
2884 if (-dx > x_offset_in_pixels)
2885 dx = -x_offset_in_pixels;
2886 if (dx > es->text_width - x_offset_in_pixels)
2887 dx = es->text_width - x_offset_in_pixels;
2888 nyoff = max(0, es->y_offset + dy);
2889 if (nyoff >= es->line_count)
2890 nyoff = es->line_count - 1;
2891 dy = (es->y_offset - nyoff) * es->line_height;
2892 if (dx || dy) {
2893 RECT rc1;
2894 RECT rc;
2896 es->y_offset = nyoff;
2897 if(es->style & ES_MULTILINE)
2898 es->x_offset += dx;
2899 else
2900 es->x_offset += dx / es->char_width;
2902 GetClientRect(hwnd, &rc1);
2903 IntersectRect(&rc, &rc1, &es->format_rect);
2904 ScrollWindowEx(hwnd, -dx, dy,
2905 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2906 /* force scroll info update */
2907 EDIT_UpdateScrollInfo(hwnd, es);
2909 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2910 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
2911 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2912 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
2913 return TRUE;
2917 /*********************************************************************
2919 * EM_POSFROMCHAR
2922 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap)
2924 INT len = strlenW(es->text);
2925 INT l;
2926 INT li;
2927 INT x;
2928 INT y = 0;
2929 HDC dc;
2930 HFONT old_font = 0;
2931 SIZE size;
2933 index = min(index, len);
2934 dc = GetDC(hwnd);
2935 if (es->font)
2936 old_font = SelectObject(dc, es->font);
2937 if (es->style & ES_MULTILINE) {
2938 l = EDIT_EM_LineFromChar(es, index);
2939 y = (l - es->y_offset) * es->line_height;
2940 li = EDIT_EM_LineIndex(es, l);
2941 if (after_wrap && (li == index) && l) {
2942 INT l2 = l - 1;
2943 LINEDEF *line_def = es->first_line_def;
2944 while (l2) {
2945 line_def = line_def->next;
2946 l2--;
2948 if (line_def->ending == END_WRAP) {
2949 l--;
2950 y -= es->line_height;
2951 li = EDIT_EM_LineIndex(es, l);
2954 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2955 es->tabs_count, es->tabs)) - es->x_offset;
2956 } else {
2957 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2958 if (index < es->x_offset) {
2959 GetTextExtentPoint32W(dc, text + index,
2960 es->x_offset - index, &size);
2961 x = -size.cx;
2962 } else {
2963 GetTextExtentPoint32W(dc, text + es->x_offset,
2964 index - es->x_offset, &size);
2965 x = size.cx;
2967 y = 0;
2968 if (es->style & ES_PASSWORD)
2969 HeapFree(GetProcessHeap(), 0, text);
2971 x += es->format_rect.left;
2972 y += es->format_rect.top;
2973 if (es->font)
2974 SelectObject(dc, old_font);
2975 ReleaseDC(hwnd, dc);
2976 return MAKELONG((INT16)x, (INT16)y);
2980 /*********************************************************************
2982 * EM_REPLACESEL
2984 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2987 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2989 UINT strl = strlenW(lpsz_replace);
2990 UINT tl = strlenW(es->text);
2991 UINT utl;
2992 UINT s;
2993 UINT e;
2994 UINT i;
2995 LPWSTR p;
2996 HRGN hrgn = 0;
2998 TRACE("%s, can_undo %d, send_update %d\n",
2999 debugstr_w(lpsz_replace), can_undo, send_update);
3001 s = es->selection_start;
3002 e = es->selection_end;
3004 if ((s == e) && !strl)
3005 return;
3007 ORDER_UINT(s, e);
3009 if (!EDIT_MakeFit(hwnd, es, tl - (e - s) + strl))
3010 return;
3012 if (e != s) {
3013 /* there is something to be deleted */
3014 if (can_undo) {
3015 utl = strlenW(es->undo_text);
3016 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3017 /* undo-buffer is extended to the right */
3018 EDIT_MakeUndoFit(es, utl + e - s);
3019 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
3020 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3021 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3022 /* undo-buffer is extended to the left */
3023 EDIT_MakeUndoFit(es, utl + e - s);
3024 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3025 p[e - s] = p[0];
3026 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3027 p[i] = (es->text + s)[i];
3028 es->undo_position = s;
3029 } else {
3030 /* new undo-buffer */
3031 EDIT_MakeUndoFit(es, e - s);
3032 strncpyW(es->undo_text, es->text + s, e - s + 1);
3033 es->undo_text[e - s] = 0; /* ensure 0 termination */
3034 es->undo_position = s;
3036 /* any deletion makes the old insertion-undo invalid */
3037 es->undo_insert_count = 0;
3038 } else
3039 EDIT_EM_EmptyUndoBuffer(es);
3041 /* now delete */
3042 strcpyW(es->text + s, es->text + e);
3044 if (strl) {
3045 /* there is an insertion */
3046 if (can_undo) {
3047 if ((s == es->undo_position) ||
3048 ((es->undo_insert_count) &&
3049 (s == es->undo_position + es->undo_insert_count)))
3051 * insertion is new and at delete position or
3052 * an extension to either left or right
3054 es->undo_insert_count += strl;
3055 else {
3056 /* new insertion undo */
3057 es->undo_position = s;
3058 es->undo_insert_count = strl;
3059 /* new insertion makes old delete-buffer invalid */
3060 *es->undo_text = '\0';
3062 } else
3063 EDIT_EM_EmptyUndoBuffer(es);
3065 /* now insert */
3066 tl = strlenW(es->text);
3067 for (p = es->text + tl ; p >= es->text + s ; p--)
3068 p[strl] = p[0];
3069 for (i = 0 , p = es->text + s ; i < strl ; i++)
3070 p[i] = lpsz_replace[i];
3071 if(es->style & ES_UPPERCASE)
3072 CharUpperBuffW(p, strl);
3073 else if(es->style & ES_LOWERCASE)
3074 CharLowerBuffW(p, strl);
3075 s += strl;
3077 if (es->style & ES_MULTILINE)
3079 INT s = min(es->selection_start, es->selection_end);
3081 hrgn = CreateRectRgn(0, 0, 0, 0);
3082 EDIT_BuildLineDefs_ML(hwnd, es, s, s + strl,
3083 strl - abs(es->selection_end - es->selection_start), hrgn);
3085 else
3086 EDIT_CalcLineWidth_SL(hwnd, es);
3088 EDIT_EM_SetSel(hwnd, es, s, s, FALSE);
3089 es->flags |= EF_MODIFIED;
3090 if (send_update) es->flags |= EF_UPDATE;
3091 EDIT_EM_ScrollCaret(hwnd, es);
3093 /* force scroll info update */
3094 EDIT_UpdateScrollInfo(hwnd, es);
3096 if (hrgn)
3098 EDIT_UpdateTextRegion(hwnd, es, hrgn, TRUE);
3099 DeleteObject(hrgn);
3101 else
3102 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3104 if(es->flags & EF_UPDATE)
3106 es->flags &= ~EF_UPDATE;
3107 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3112 /*********************************************************************
3114 * EM_SCROLL
3117 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action)
3119 INT dy;
3121 if (!(es->style & ES_MULTILINE))
3122 return (LRESULT)FALSE;
3124 dy = 0;
3126 switch (action) {
3127 case SB_LINEUP:
3128 if (es->y_offset)
3129 dy = -1;
3130 break;
3131 case SB_LINEDOWN:
3132 if (es->y_offset < es->line_count - 1)
3133 dy = 1;
3134 break;
3135 case SB_PAGEUP:
3136 if (es->y_offset)
3137 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3138 break;
3139 case SB_PAGEDOWN:
3140 if (es->y_offset < es->line_count - 1)
3141 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3142 break;
3143 default:
3144 return (LRESULT)FALSE;
3146 if (dy) {
3147 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3148 /* check if we are going to move too far */
3149 if(es->y_offset + dy > es->line_count - vlc)
3150 dy = es->line_count - vlc - es->y_offset;
3152 /* Notification is done in EDIT_EM_LineScroll */
3153 if(dy)
3154 EDIT_EM_LineScroll(hwnd, es, 0, dy);
3156 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3160 /*********************************************************************
3162 * EM_SCROLLCARET
3165 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es)
3167 if (es->style & ES_MULTILINE) {
3168 INT l;
3169 INT li;
3170 INT vlc;
3171 INT ww;
3172 INT cw = es->char_width;
3173 INT x;
3174 INT dy = 0;
3175 INT dx = 0;
3177 l = EDIT_EM_LineFromChar(es, es->selection_end);
3178 li = EDIT_EM_LineIndex(es, l);
3179 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
3180 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3181 if (l >= es->y_offset + vlc)
3182 dy = l - vlc + 1 - es->y_offset;
3183 if (l < es->y_offset)
3184 dy = l - es->y_offset;
3185 ww = es->format_rect.right - es->format_rect.left;
3186 if (x < es->format_rect.left)
3187 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3188 if (x > es->format_rect.right)
3189 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3190 if (dy || dx)
3192 /* check if we are going to move too far */
3193 if(es->x_offset + dx + ww > es->text_width)
3194 dx = es->text_width - ww - es->x_offset;
3195 if(dx || dy)
3196 EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
3198 } else {
3199 INT x;
3200 INT goal;
3201 INT format_width;
3203 if (!(es->style & ES_AUTOHSCROLL))
3204 return;
3206 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3207 format_width = es->format_rect.right - es->format_rect.left;
3208 if (x < es->format_rect.left) {
3209 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3210 do {
3211 es->x_offset--;
3212 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3213 } while ((x < goal) && es->x_offset);
3214 /* FIXME: use ScrollWindow() somehow to improve performance */
3215 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3216 } else if (x > es->format_rect.right) {
3217 INT x_last;
3218 INT len = strlenW(es->text);
3219 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3220 do {
3221 es->x_offset++;
3222 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3223 x_last = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, len, FALSE));
3224 } while ((x > goal) && (x_last > es->format_rect.right));
3225 /* FIXME: use ScrollWindow() somehow to improve performance */
3226 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3230 if(es->flags & EF_FOCUSED)
3231 EDIT_SetCaretPos(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3235 /*********************************************************************
3237 * EM_SETHANDLE
3239 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3242 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc)
3244 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3246 if (!(es->style & ES_MULTILINE))
3247 return;
3249 if (!hloc) {
3250 WARN("called with NULL handle\n");
3251 return;
3254 EDIT_UnlockBuffer(hwnd, es, TRUE);
3256 if(es->hloc16)
3258 LOCAL_Free(hInstance, es->hloc16);
3259 es->hloc16 = (HLOCAL16)NULL;
3262 if(es->is_unicode)
3264 if(es->hloc32A)
3266 LocalFree(es->hloc32A);
3267 es->hloc32A = (HLOCAL)NULL;
3269 es->hloc32W = hloc;
3271 else
3273 INT countW, countA;
3274 HLOCAL hloc32W_new;
3275 WCHAR *textW;
3276 CHAR *textA;
3278 countA = LocalSize(hloc);
3279 textA = LocalLock(hloc);
3280 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3281 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3283 ERR("Could not allocate new unicode buffer\n");
3284 return;
3286 textW = LocalLock(hloc32W_new);
3287 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3288 LocalUnlock(hloc32W_new);
3289 LocalUnlock(hloc);
3291 if(es->hloc32W)
3292 LocalFree(es->hloc32W);
3294 es->hloc32W = hloc32W_new;
3295 es->hloc32A = hloc;
3298 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3300 EDIT_LockBuffer(hwnd, es);
3302 es->x_offset = es->y_offset = 0;
3303 es->selection_start = es->selection_end = 0;
3304 EDIT_EM_EmptyUndoBuffer(es);
3305 es->flags &= ~EF_MODIFIED;
3306 es->flags &= ~EF_UPDATE;
3307 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3308 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3309 EDIT_EM_ScrollCaret(hwnd, es);
3310 /* force scroll info update */
3311 EDIT_UpdateScrollInfo(hwnd, es);
3315 /*********************************************************************
3317 * EM_SETHANDLE16
3319 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3322 static void EDIT_EM_SetHandle16(HWND hwnd, EDITSTATE *es, HLOCAL16 hloc)
3324 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3325 INT countW, countA;
3326 HLOCAL hloc32W_new;
3327 WCHAR *textW;
3328 CHAR *textA;
3330 if (!(es->style & ES_MULTILINE))
3331 return;
3333 if (!hloc) {
3334 WARN("called with NULL handle\n");
3335 return;
3338 EDIT_UnlockBuffer(hwnd, es, TRUE);
3340 if(es->hloc32A)
3342 LocalFree(es->hloc32A);
3343 es->hloc32A = (HLOCAL)NULL;
3346 countA = LOCAL_Size(hInstance, hloc);
3347 textA = LOCAL_Lock(hInstance, hloc);
3348 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3349 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3351 ERR("Could not allocate new unicode buffer\n");
3352 return;
3354 textW = LocalLock(hloc32W_new);
3355 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3356 LocalUnlock(hloc32W_new);
3357 LOCAL_Unlock(hInstance, hloc);
3359 if(es->hloc32W)
3360 LocalFree(es->hloc32W);
3362 es->hloc32W = hloc32W_new;
3363 es->hloc16 = hloc;
3365 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3367 EDIT_LockBuffer(hwnd, es);
3369 es->x_offset = es->y_offset = 0;
3370 es->selection_start = es->selection_end = 0;
3371 EDIT_EM_EmptyUndoBuffer(es);
3372 es->flags &= ~EF_MODIFIED;
3373 es->flags &= ~EF_UPDATE;
3374 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3375 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3376 EDIT_EM_ScrollCaret(hwnd, es);
3377 /* force scroll info update */
3378 EDIT_UpdateScrollInfo(hwnd, es);
3382 /*********************************************************************
3384 * EM_SETLIMITTEXT
3386 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3387 * However, the windows version is not complied to yet in all of edit.c
3390 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3392 if (es->style & ES_MULTILINE) {
3393 if (limit)
3394 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3395 else
3396 es->buffer_limit = BUFLIMIT_MULTI;
3397 } else {
3398 if (limit)
3399 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3400 else
3401 es->buffer_limit = BUFLIMIT_SINGLE;
3406 /*********************************************************************
3408 * EM_SETMARGINS
3410 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3411 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3412 * of the char's width, according to the new docs.
3415 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3416 INT left, INT right)
3418 if (action & EC_LEFTMARGIN) {
3419 if (left != EC_USEFONTINFO)
3420 es->left_margin = left;
3421 else
3422 es->left_margin = es->char_width / 3;
3425 if (action & EC_RIGHTMARGIN) {
3426 if (right != EC_USEFONTINFO)
3427 es->right_margin = right;
3428 else
3429 es->right_margin = es->char_width / 3;
3431 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3435 /*********************************************************************
3437 * EM_SETPASSWORDCHAR
3440 static void EDIT_EM_SetPasswordChar(HWND hwnd, EDITSTATE *es, WCHAR c)
3442 LONG style;
3444 if (es->style & ES_MULTILINE)
3445 return;
3447 if (es->password_char == c)
3448 return;
3450 style = GetWindowLongA( hwnd, GWL_STYLE );
3451 es->password_char = c;
3452 if (c) {
3453 SetWindowLongA( hwnd, GWL_STYLE, style | ES_PASSWORD );
3454 es->style |= ES_PASSWORD;
3455 } else {
3456 SetWindowLongA( hwnd, GWL_STYLE, style & ~ES_PASSWORD );
3457 es->style &= ~ES_PASSWORD;
3459 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3463 /*********************************************************************
3465 * EDIT_EM_SetSel
3467 * note: unlike the specs say: the order of start and end
3468 * _is_ preserved in Windows. (i.e. start can be > end)
3469 * In other words: this handler is OK
3472 static void EDIT_EM_SetSel(HWND hwnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3474 UINT old_start = es->selection_start;
3475 UINT old_end = es->selection_end;
3476 UINT len = strlenW(es->text);
3478 if (start == (UINT)-1) {
3479 start = es->selection_end;
3480 end = es->selection_end;
3481 } else {
3482 start = min(start, len);
3483 end = min(end, len);
3485 es->selection_start = start;
3486 es->selection_end = end;
3487 if (after_wrap)
3488 es->flags |= EF_AFTER_WRAP;
3489 else
3490 es->flags &= ~EF_AFTER_WRAP;
3491 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3492 ORDER_UINT(start, end);
3493 ORDER_UINT(end, old_end);
3494 ORDER_UINT(start, old_start);
3495 ORDER_UINT(old_start, old_end);
3496 if (end != old_start)
3499 * One can also do
3500 * ORDER_UINT32(end, old_start);
3501 * EDIT_InvalidateText(hwnd, es, start, end);
3502 * EDIT_InvalidateText(hwnd, es, old_start, old_end);
3503 * in place of the following if statement.
3505 if (old_start > end )
3507 EDIT_InvalidateText(hwnd, es, start, end);
3508 EDIT_InvalidateText(hwnd, es, old_start, old_end);
3510 else
3512 EDIT_InvalidateText(hwnd, es, start, old_start);
3513 EDIT_InvalidateText(hwnd, es, end, old_end);
3516 else EDIT_InvalidateText(hwnd, es, start, old_end);
3520 /*********************************************************************
3522 * EM_SETTABSTOPS
3525 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3527 if (!(es->style & ES_MULTILINE))
3528 return FALSE;
3529 if (es->tabs)
3530 HeapFree(GetProcessHeap(), 0, es->tabs);
3531 es->tabs_count = count;
3532 if (!count)
3533 es->tabs = NULL;
3534 else {
3535 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3536 memcpy(es->tabs, tabs, count * sizeof(INT));
3538 return TRUE;
3542 /*********************************************************************
3544 * EM_SETTABSTOPS16
3547 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3549 if (!(es->style & ES_MULTILINE))
3550 return FALSE;
3551 if (es->tabs)
3552 HeapFree(GetProcessHeap(), 0, es->tabs);
3553 es->tabs_count = count;
3554 if (!count)
3555 es->tabs = NULL;
3556 else {
3557 INT i;
3558 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3559 for (i = 0 ; i < count ; i++)
3560 es->tabs[i] = *tabs++;
3562 return TRUE;
3566 /*********************************************************************
3568 * EM_SETWORDBREAKPROC
3571 static void EDIT_EM_SetWordBreakProc(HWND hwnd, EDITSTATE *es, LPARAM lParam)
3573 if (es->word_break_proc == (void *)lParam)
3574 return;
3576 es->word_break_proc = (void *)lParam;
3577 es->word_break_proc16 = NULL;
3579 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3580 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3581 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3586 /*********************************************************************
3588 * EM_SETWORDBREAKPROC16
3591 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3593 if (es->word_break_proc16 == wbp)
3594 return;
3596 es->word_break_proc = NULL;
3597 es->word_break_proc16 = wbp;
3598 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3599 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3600 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3605 /*********************************************************************
3607 * EM_UNDO / WM_UNDO
3610 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es)
3612 INT ulength;
3613 LPWSTR utext;
3615 /* Protect read-only edit control from modification */
3616 if(es->style & ES_READONLY)
3617 return FALSE;
3619 ulength = strlenW(es->undo_text);
3620 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3622 strcpyW(utext, es->undo_text);
3624 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3625 es->undo_insert_count, debugstr_w(utext));
3627 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3628 EDIT_EM_EmptyUndoBuffer(es);
3629 EDIT_EM_ReplaceSel(hwnd, es, TRUE, utext, FALSE);
3630 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3631 /* send the notification after the selection start and end are set */
3632 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3633 EDIT_EM_ScrollCaret(hwnd, es);
3634 HeapFree(GetProcessHeap(), 0, utext);
3636 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3637 es->undo_insert_count, debugstr_w(es->undo_text));
3638 return TRUE;
3642 /*********************************************************************
3644 * WM_CHAR
3647 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c)
3649 BOOL control;
3651 /* Protect read-only edit control from modification */
3652 if(es->style & ES_READONLY)
3653 return;
3655 control = GetKeyState(VK_CONTROL) & 0x8000;
3657 switch (c) {
3658 case '\r':
3659 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3660 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3661 break;
3662 case '\n':
3663 if (es->style & ES_MULTILINE) {
3664 if (es->style & ES_READONLY) {
3665 EDIT_MoveHome(hwnd, es, FALSE);
3666 EDIT_MoveDown_ML(hwnd, es, FALSE);
3667 } else {
3668 static const WCHAR cr_lfW[] = {'\r','\n',0};
3669 EDIT_EM_ReplaceSel(hwnd, es, TRUE, cr_lfW, TRUE);
3672 break;
3673 case '\t':
3674 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3676 static const WCHAR tabW[] = {'\t',0};
3677 EDIT_EM_ReplaceSel(hwnd, es, TRUE, tabW, TRUE);
3679 break;
3680 case VK_BACK:
3681 if (!(es->style & ES_READONLY) && !control) {
3682 if (es->selection_start != es->selection_end)
3683 EDIT_WM_Clear(hwnd, es);
3684 else {
3685 /* delete character left of caret */
3686 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
3687 EDIT_MoveBackward(hwnd, es, TRUE);
3688 EDIT_WM_Clear(hwnd, es);
3691 break;
3692 case 0x03: /* ^C */
3693 SendMessageW(hwnd, WM_COPY, 0, 0);
3694 break;
3695 case 0x16: /* ^V */
3696 SendMessageW(hwnd, WM_PASTE, 0, 0);
3697 break;
3698 case 0x18: /* ^X */
3699 SendMessageW(hwnd, WM_CUT, 0, 0);
3700 break;
3702 default:
3703 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3704 WCHAR str[2];
3705 str[0] = c;
3706 str[1] = '\0';
3707 EDIT_EM_ReplaceSel(hwnd, es, TRUE, str, TRUE);
3709 break;
3714 /*********************************************************************
3716 * WM_COMMAND
3719 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND control)
3721 if (code || control)
3722 return;
3724 switch (id) {
3725 case EM_UNDO:
3726 EDIT_EM_Undo(hwnd, es);
3727 break;
3728 case WM_CUT:
3729 EDIT_WM_Cut(hwnd, es);
3730 break;
3731 case WM_COPY:
3732 EDIT_WM_Copy(hwnd, es);
3733 break;
3734 case WM_PASTE:
3735 EDIT_WM_Paste(hwnd, es);
3736 break;
3737 case WM_CLEAR:
3738 EDIT_WM_Clear(hwnd, es);
3739 break;
3740 case EM_SETSEL:
3741 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
3742 EDIT_EM_ScrollCaret(hwnd, es);
3743 break;
3744 default:
3745 ERR("unknown menu item, please report\n");
3746 break;
3751 /*********************************************************************
3753 * WM_CONTEXTMENU
3755 * Note: the resource files resource/sysres_??.rc cannot define a
3756 * single popup menu. Hence we use a (dummy) menubar
3757 * containing the single popup menu as its first item.
3759 * FIXME: the message identifiers have been chosen arbitrarily,
3760 * hence we use MF_BYPOSITION.
3761 * We might as well use the "real" values (anybody knows ?)
3762 * The menu definition is in resources/sysres_??.rc.
3763 * Once these are OK, we better use MF_BYCOMMAND here
3764 * (as we do in EDIT_WM_Command()).
3767 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y)
3769 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3770 HMENU popup = GetSubMenu(menu, 0);
3771 UINT start = es->selection_start;
3772 UINT end = es->selection_end;
3774 ORDER_UINT(start, end);
3776 /* undo */
3777 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3778 /* cut */
3779 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3780 /* copy */
3781 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3782 /* paste */
3783 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3784 /* delete */
3785 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3786 /* select all */
3787 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3789 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, hwnd, NULL);
3790 DestroyMenu(menu);
3794 /*********************************************************************
3796 * WM_COPY
3799 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es)
3801 INT s = es->selection_start;
3802 INT e = es->selection_end;
3803 HGLOBAL hdst;
3804 LPWSTR dst;
3806 if (e == s)
3807 return;
3808 ORDER_INT(s, e);
3809 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3810 dst = GlobalLock(hdst);
3811 strncpyW(dst, es->text + s, e - s);
3812 dst[e - s] = 0; /* ensure 0 termination */
3813 TRACE("%s\n", debugstr_w(dst));
3814 GlobalUnlock(hdst);
3815 OpenClipboard(hwnd);
3816 EmptyClipboard();
3817 SetClipboardData(CF_UNICODETEXT, hdst);
3818 CloseClipboard();
3822 /*********************************************************************
3824 * WM_CREATE
3827 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name)
3829 TRACE("%s\n", debugstr_w(name));
3831 * To initialize some final structure members, we call some helper
3832 * functions. However, since the EDITSTATE is not consistent (i.e.
3833 * not fully initialized), we should be very careful which
3834 * functions can be called, and in what order.
3836 EDIT_WM_SetFont(hwnd, es, 0, FALSE);
3837 EDIT_EM_EmptyUndoBuffer(es);
3839 if (name && *name) {
3840 EDIT_EM_ReplaceSel(hwnd, es, FALSE, name, FALSE);
3841 /* if we insert text to the editline, the text scrolls out
3842 * of the window, as the caret is placed after the insert
3843 * pos normally; thus we reset es->selection... to 0 and
3844 * update caret
3846 es->selection_start = es->selection_end = 0;
3847 /* send the notification after the selection start and end are set */
3848 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3849 EDIT_EM_ScrollCaret(hwnd, es);
3851 /* force scroll info update */
3852 EDIT_UpdateScrollInfo(hwnd, es);
3853 return 0;
3857 /*********************************************************************
3859 * WM_DESTROY
3862 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es)
3864 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3865 LINEDEF *pc, *pp;
3867 if (es->hloc32W) {
3868 while (LocalUnlock(es->hloc32W)) ;
3869 LocalFree(es->hloc32W);
3871 if (es->hloc32A) {
3872 while (LocalUnlock(es->hloc32A)) ;
3873 LocalFree(es->hloc32A);
3875 if (es->hloc16) {
3876 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3877 LOCAL_Free(hInstance, es->hloc16);
3880 pc = es->first_line_def;
3881 while (pc)
3883 pp = pc->next;
3884 HeapFree(GetProcessHeap(), 0, pc);
3885 pc = pp;
3888 SetWindowLongA( hwnd, 0, 0 );
3889 HeapFree(GetProcessHeap(), 0, es);
3893 /*********************************************************************
3895 * WM_ERASEBKGND
3898 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc)
3900 HBRUSH brush;
3901 RECT rc;
3903 if ( get_app_version() >= 0x40000 &&(
3904 !es->bEnableState || (es->style & ES_READONLY)))
3905 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
3906 else
3907 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(hwnd, dc);
3909 if (!brush)
3910 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3912 GetClientRect(hwnd, &rc);
3913 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3914 GetClipBox(dc, &rc);
3916 * FIXME: specs say that we should UnrealizeObject() the brush,
3917 * but the specs of UnrealizeObject() say that we shouldn't
3918 * unrealize a stock object. The default brush that
3919 * DefWndProc() returns is ... a stock object.
3921 FillRect(dc, &rc, brush);
3922 return -1;
3926 /*********************************************************************
3928 * WM_GETTEXT
3931 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3933 if(!count) return 0;
3935 if(unicode)
3937 LPWSTR textW = (LPWSTR)lParam;
3938 strncpyW(textW, es->text, count);
3939 textW[count - 1] = 0; /* ensure 0 termination */
3940 return strlenW(textW);
3942 else
3944 LPSTR textA = (LPSTR)lParam;
3945 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL);
3946 textA[count - 1] = 0; /* ensure 0 termination */
3947 return strlen(textA);
3951 /*********************************************************************
3953 * WM_HSCROLL
3956 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
3958 INT dx;
3959 INT fw;
3961 if (!(es->style & ES_MULTILINE))
3962 return 0;
3964 if (!(es->style & ES_AUTOHSCROLL))
3965 return 0;
3967 dx = 0;
3968 fw = es->format_rect.right - es->format_rect.left;
3969 switch (action) {
3970 case SB_LINELEFT:
3971 TRACE("SB_LINELEFT\n");
3972 if (es->x_offset)
3973 dx = -es->char_width;
3974 break;
3975 case SB_LINERIGHT:
3976 TRACE("SB_LINERIGHT\n");
3977 if (es->x_offset < es->text_width)
3978 dx = es->char_width;
3979 break;
3980 case SB_PAGELEFT:
3981 TRACE("SB_PAGELEFT\n");
3982 if (es->x_offset)
3983 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3984 break;
3985 case SB_PAGERIGHT:
3986 TRACE("SB_PAGERIGHT\n");
3987 if (es->x_offset < es->text_width)
3988 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3989 break;
3990 case SB_LEFT:
3991 TRACE("SB_LEFT\n");
3992 if (es->x_offset)
3993 dx = -es->x_offset;
3994 break;
3995 case SB_RIGHT:
3996 TRACE("SB_RIGHT\n");
3997 if (es->x_offset < es->text_width)
3998 dx = es->text_width - es->x_offset;
3999 break;
4000 case SB_THUMBTRACK:
4001 TRACE("SB_THUMBTRACK %d\n", pos);
4002 es->flags |= EF_HSCROLL_TRACK;
4003 if(es->style & WS_HSCROLL)
4004 dx = pos - es->x_offset;
4005 else
4007 INT fw, new_x;
4008 /* Sanity check */
4009 if(pos < 0 || pos > 100) return 0;
4010 /* Assume default scroll range 0-100 */
4011 fw = es->format_rect.right - es->format_rect.left;
4012 new_x = pos * (es->text_width - fw) / 100;
4013 dx = es->text_width ? (new_x - es->x_offset) : 0;
4015 break;
4016 case SB_THUMBPOSITION:
4017 TRACE("SB_THUMBPOSITION %d\n", pos);
4018 es->flags &= ~EF_HSCROLL_TRACK;
4019 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4020 dx = pos - es->x_offset;
4021 else
4023 INT fw, new_x;
4024 /* Sanity check */
4025 if(pos < 0 || pos > 100) return 0;
4026 /* Assume default scroll range 0-100 */
4027 fw = es->format_rect.right - es->format_rect.left;
4028 new_x = pos * (es->text_width - fw) / 100;
4029 dx = es->text_width ? (new_x - es->x_offset) : 0;
4031 if (!dx) {
4032 /* force scroll info update */
4033 EDIT_UpdateScrollInfo(hwnd, es);
4034 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
4036 break;
4037 case SB_ENDSCROLL:
4038 TRACE("SB_ENDSCROLL\n");
4039 break;
4041 * FIXME : the next two are undocumented !
4042 * Are we doing the right thing ?
4043 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4044 * although it's also a regular control message.
4046 case EM_GETTHUMB: /* this one is used by NT notepad */
4047 case EM_GETTHUMB16:
4049 LRESULT ret;
4050 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4051 ret = GetScrollPos(hwnd, SB_HORZ);
4052 else
4054 /* Assume default scroll range 0-100 */
4055 INT fw = es->format_rect.right - es->format_rect.left;
4056 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4058 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4059 return ret;
4061 case EM_LINESCROLL16:
4062 TRACE("EM_LINESCROLL16\n");
4063 dx = pos;
4064 break;
4066 default:
4067 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4068 action, action);
4069 return 0;
4071 if (dx)
4073 INT fw = es->format_rect.right - es->format_rect.left;
4074 /* check if we are going to move too far */
4075 if(es->x_offset + dx + fw > es->text_width)
4076 dx = es->text_width - fw - es->x_offset;
4077 if(dx)
4078 EDIT_EM_LineScroll_internal(hwnd, es, dx, 0);
4080 return 0;
4084 /*********************************************************************
4086 * EDIT_CheckCombo
4089 static BOOL EDIT_CheckCombo(HWND hwnd, EDITSTATE *es, UINT msg, INT key)
4091 HWND hLBox = es->hwndListBox;
4092 HWND hCombo;
4093 BOOL bDropped;
4094 int nEUI;
4096 if (!hLBox)
4097 return FALSE;
4099 hCombo = GetParent(hwnd);
4100 bDropped = TRUE;
4101 nEUI = 0;
4103 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
4104 hwnd, (UINT16)msg, (UINT16)key);
4106 if (key == VK_UP || key == VK_DOWN)
4108 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4109 nEUI = 1;
4111 if (msg == WM_KEYDOWN || nEUI)
4112 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4115 switch (msg)
4117 case WM_KEYDOWN:
4118 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4120 /* make sure ComboLBox pops up */
4121 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4122 key = VK_F4;
4123 nEUI = 2;
4126 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4127 break;
4129 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4130 if (nEUI)
4131 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4132 else
4133 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4134 break;
4137 if(nEUI == 2)
4138 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4140 return TRUE;
4144 /*********************************************************************
4146 * WM_KEYDOWN
4148 * Handling of special keys that don't produce a WM_CHAR
4149 * (i.e. non-printable keys) & Backspace & Delete
4152 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key)
4154 BOOL shift;
4155 BOOL control;
4157 if (GetKeyState(VK_MENU) & 0x8000)
4158 return 0;
4160 shift = GetKeyState(VK_SHIFT) & 0x8000;
4161 control = GetKeyState(VK_CONTROL) & 0x8000;
4163 switch (key) {
4164 case VK_F4:
4165 case VK_UP:
4166 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key) || key == VK_F4)
4167 break;
4169 /* fall through */
4170 case VK_LEFT:
4171 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4172 EDIT_MoveUp_ML(hwnd, es, shift);
4173 else
4174 if (control)
4175 EDIT_MoveWordBackward(hwnd, es, shift);
4176 else
4177 EDIT_MoveBackward(hwnd, es, shift);
4178 break;
4179 case VK_DOWN:
4180 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key))
4181 break;
4182 /* fall through */
4183 case VK_RIGHT:
4184 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4185 EDIT_MoveDown_ML(hwnd, es, shift);
4186 else if (control)
4187 EDIT_MoveWordForward(hwnd, es, shift);
4188 else
4189 EDIT_MoveForward(hwnd, es, shift);
4190 break;
4191 case VK_HOME:
4192 EDIT_MoveHome(hwnd, es, shift);
4193 break;
4194 case VK_END:
4195 EDIT_MoveEnd(hwnd, es, shift);
4196 break;
4197 case VK_PRIOR:
4198 if (es->style & ES_MULTILINE)
4199 EDIT_MovePageUp_ML(hwnd, es, shift);
4200 else
4201 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4202 break;
4203 case VK_NEXT:
4204 if (es->style & ES_MULTILINE)
4205 EDIT_MovePageDown_ML(hwnd, es, shift);
4206 else
4207 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4208 break;
4209 case VK_DELETE:
4210 if (!(es->style & ES_READONLY) && !(shift && control)) {
4211 if (es->selection_start != es->selection_end) {
4212 if (shift)
4213 EDIT_WM_Cut(hwnd, es);
4214 else
4215 EDIT_WM_Clear(hwnd, es);
4216 } else {
4217 if (shift) {
4218 /* delete character left of caret */
4219 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4220 EDIT_MoveBackward(hwnd, es, TRUE);
4221 EDIT_WM_Clear(hwnd, es);
4222 } else if (control) {
4223 /* delete to end of line */
4224 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4225 EDIT_MoveEnd(hwnd, es, TRUE);
4226 EDIT_WM_Clear(hwnd, es);
4227 } else {
4228 /* delete character right of caret */
4229 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4230 EDIT_MoveForward(hwnd, es, TRUE);
4231 EDIT_WM_Clear(hwnd, es);
4235 break;
4236 case VK_INSERT:
4237 if (shift) {
4238 if (!(es->style & ES_READONLY))
4239 EDIT_WM_Paste(hwnd, es);
4240 } else if (control)
4241 EDIT_WM_Copy(hwnd, es);
4242 break;
4243 case VK_RETURN:
4244 /* If the edit doesn't want the return send a message to the default object */
4245 if(!(es->style & ES_WANTRETURN))
4247 HWND hwndParent = GetParent(hwnd);
4248 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4249 if (HIWORD(dw) == DC_HASDEFID)
4251 SendMessageW( hwndParent, WM_COMMAND,
4252 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4253 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4256 break;
4258 return 0;
4262 /*********************************************************************
4264 * WM_KILLFOCUS
4267 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es)
4269 es->flags &= ~EF_FOCUSED;
4270 DestroyCaret();
4271 if(!(es->style & ES_NOHIDESEL))
4272 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4273 EDIT_NOTIFY_PARENT(hwnd, es, EN_KILLFOCUS, "EN_KILLFOCUS");
4274 return 0;
4278 /*********************************************************************
4280 * WM_LBUTTONDBLCLK
4282 * The caret position has been set on the WM_LBUTTONDOWN message
4285 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es)
4287 INT s;
4288 INT e = es->selection_end;
4289 INT l;
4290 INT li;
4291 INT ll;
4293 if (!(es->flags & EF_FOCUSED))
4294 return 0;
4296 l = EDIT_EM_LineFromChar(es, e);
4297 li = EDIT_EM_LineIndex(es, l);
4298 ll = EDIT_EM_LineLength(es, e);
4299 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4300 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4301 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
4302 EDIT_EM_ScrollCaret(hwnd, es);
4303 return 0;
4307 /*********************************************************************
4309 * WM_LBUTTONDOWN
4312 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4314 INT e;
4315 BOOL after_wrap;
4317 if (!(es->flags & EF_FOCUSED))
4318 return 0;
4320 es->bCaptureState = TRUE;
4321 SetCapture(hwnd);
4322 EDIT_ConfinePoint(es, &x, &y);
4323 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4324 EDIT_EM_SetSel(hwnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4325 EDIT_EM_ScrollCaret(hwnd, es);
4326 es->region_posx = es->region_posy = 0;
4327 SetTimer(hwnd, 0, 100, NULL);
4328 return 0;
4332 /*********************************************************************
4334 * WM_LBUTTONUP
4337 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4339 if (es->bCaptureState && GetCapture() == hwndSelf) {
4340 KillTimer(hwndSelf, 0);
4341 ReleaseCapture();
4343 es->bCaptureState = FALSE;
4344 return 0;
4348 /*********************************************************************
4350 * WM_MBUTTONDOWN
4353 static LRESULT EDIT_WM_MButtonDown(HWND hwnd)
4355 SendMessageW(hwnd,WM_PASTE,0,0);
4356 return 0;
4360 /*********************************************************************
4362 * WM_MOUSEMOVE
4365 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y)
4367 INT e;
4368 BOOL after_wrap;
4369 INT prex, prey;
4371 if (GetCapture() != hwnd)
4372 return 0;
4375 * FIXME: gotta do some scrolling if outside client
4376 * area. Maybe reset the timer ?
4378 prex = x; prey = y;
4379 EDIT_ConfinePoint(es, &x, &y);
4380 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4381 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4382 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4383 EDIT_EM_SetSel(hwnd, es, es->selection_start, e, after_wrap);
4384 return 0;
4388 /*********************************************************************
4390 * WM_NCCREATE
4392 * See also EDIT_WM_StyleChanged
4394 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode)
4396 EDITSTATE *es;
4397 UINT alloc_size;
4399 TRACE("Creating %s edit control, style = %08lx\n",
4400 unicode ? "Unicode" : "ANSI", style);
4402 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4403 return FALSE;
4404 SetWindowLongA( hwnd, 0, (LONG)es );
4407 * Note: since the EDITSTATE has not been fully initialized yet,
4408 * we can't use any API calls that may send
4409 * WM_XXX messages before WM_NCCREATE is completed.
4412 es->is_unicode = unicode;
4413 es->style = style;
4415 es->bEnableState = !(style & WS_DISABLED);
4418 * In Win95 look and feel, the WS_BORDER style is replaced by the
4419 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4420 * control a non client area. Not always. This coordinates in some
4421 * way with the window creation code in dialog.c When making
4422 * modifications please ensure that the code still works for edit
4423 * controls created directly with style 0x50800000, exStyle 0 (
4424 * which should have a single pixel border)
4426 if (TWEAK_WineLook != WIN31_LOOK)
4428 es->style &= ~WS_BORDER;
4430 else
4432 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4433 SetWindowLongA( hwnd, GWL_STYLE,
4434 GetWindowLongA( hwnd, GWL_STYLE ) & ~WS_BORDER );
4437 /* Save parent, which will be notified by EN_* messages */
4438 es->hwndParent = hwndParent;
4440 if (es->style & ES_COMBO)
4441 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4443 /* Number overrides lowercase overrides uppercase (at least it
4444 * does in Win95). However I'll bet that ES_NUMBER would be
4445 * invalid under Win 3.1.
4447 if (es->style & ES_NUMBER) {
4448 ; /* do not override the ES_NUMBER */
4449 } else if (es->style & ES_LOWERCASE) {
4450 es->style &= ~ES_UPPERCASE;
4452 if (es->style & ES_MULTILINE) {
4453 es->buffer_limit = BUFLIMIT_MULTI;
4454 if (es->style & WS_VSCROLL)
4455 es->style |= ES_AUTOVSCROLL;
4456 if (es->style & WS_HSCROLL)
4457 es->style |= ES_AUTOHSCROLL;
4458 es->style &= ~ES_PASSWORD;
4459 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4460 /* Confirmed - RIGHT overrides CENTER */
4461 if (es->style & ES_RIGHT)
4462 es->style &= ~ES_CENTER;
4463 es->style &= ~WS_HSCROLL;
4464 es->style &= ~ES_AUTOHSCROLL;
4467 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4468 es->style |= ES_AUTOVSCROLL;
4469 } else {
4470 es->buffer_limit = BUFLIMIT_SINGLE;
4471 if (WIN31_LOOK == TWEAK_WineLook ||
4472 WIN95_LOOK == TWEAK_WineLook) {
4473 es->style &= ~ES_CENTER;
4474 es->style &= ~ES_RIGHT;
4475 } else {
4476 if (es->style & ES_RIGHT)
4477 es->style &= ~ES_CENTER;
4479 es->style &= ~WS_HSCROLL;
4480 es->style &= ~WS_VSCROLL;
4481 es->style &= ~ES_AUTOVSCROLL;
4482 es->style &= ~ES_WANTRETURN;
4483 if (es->style & ES_PASSWORD)
4484 es->password_char = '*';
4486 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4487 es->style |= ES_AUTOHSCROLL;
4490 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4491 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4492 return FALSE;
4493 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4495 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4496 return FALSE;
4497 es->undo_buffer_size = es->buffer_size;
4499 if (es->style & ES_MULTILINE)
4500 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4501 return FALSE;
4502 es->line_count = 1;
4504 return TRUE;
4507 /*********************************************************************
4509 * WM_PAINT
4512 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam)
4514 PAINTSTRUCT ps;
4515 INT i;
4516 HDC dc;
4517 HFONT old_font = 0;
4518 RECT rc;
4519 RECT rcLine;
4520 RECT rcRgn;
4521 BOOL rev = es->bEnableState &&
4522 ((es->flags & EF_FOCUSED) ||
4523 (es->style & ES_NOHIDESEL));
4524 if (!wParam)
4525 dc = BeginPaint(hwnd, &ps);
4526 else
4527 dc = (HDC) wParam;
4528 if(es->style & WS_BORDER) {
4529 GetClientRect(hwnd, &rc);
4530 if(es->style & ES_MULTILINE) {
4531 if(es->style & WS_HSCROLL) rc.bottom++;
4532 if(es->style & WS_VSCROLL) rc.right++;
4534 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4536 IntersectClipRect(dc, es->format_rect.left,
4537 es->format_rect.top,
4538 es->format_rect.right,
4539 es->format_rect.bottom);
4540 if (es->style & ES_MULTILINE) {
4541 GetClientRect(hwnd, &rc);
4542 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4544 if (es->font)
4545 old_font = SelectObject(dc, es->font);
4546 if ( get_app_version() >= 0x40000 &&(
4547 !es->bEnableState || (es->style & ES_READONLY)))
4548 EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
4549 else
4550 EDIT_SEND_CTLCOLOR(hwnd, dc);
4552 if (!es->bEnableState)
4553 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4554 GetClipBox(dc, &rcRgn);
4555 if (es->style & ES_MULTILINE) {
4556 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4557 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4558 EDIT_GetLineRect(hwnd, es, i, 0, -1, &rcLine);
4559 if (IntersectRect(&rc, &rcRgn, &rcLine))
4560 EDIT_PaintLine(hwnd, es, dc, i, rev);
4562 } else {
4563 EDIT_GetLineRect(hwnd, es, 0, 0, -1, &rcLine);
4564 if (IntersectRect(&rc, &rcRgn, &rcLine))
4565 EDIT_PaintLine(hwnd, es, dc, 0, rev);
4567 if (es->font)
4568 SelectObject(dc, old_font);
4570 if (!wParam)
4571 EndPaint(hwnd, &ps);
4575 /*********************************************************************
4577 * WM_PASTE
4580 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es)
4582 HGLOBAL hsrc;
4583 LPWSTR src;
4585 /* Protect read-only edit control from modification */
4586 if(es->style & ES_READONLY)
4587 return;
4589 OpenClipboard(hwnd);
4590 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4591 src = (LPWSTR)GlobalLock(hsrc);
4592 EDIT_EM_ReplaceSel(hwnd, es, TRUE, src, TRUE);
4593 GlobalUnlock(hsrc);
4595 CloseClipboard();
4599 /*********************************************************************
4601 * WM_SETFOCUS
4604 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es)
4606 es->flags |= EF_FOCUSED;
4607 CreateCaret(hwnd, 0, 2, es->line_height);
4608 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4609 es->flags & EF_AFTER_WRAP);
4610 if(!(es->style & ES_NOHIDESEL))
4611 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4612 ShowCaret(hwnd);
4613 EDIT_NOTIFY_PARENT(hwnd, es, EN_SETFOCUS, "EN_SETFOCUS");
4617 /*********************************************************************
4619 * WM_SETFONT
4621 * With Win95 look the margins are set to default font value unless
4622 * the system font (font == 0) is being set, in which case they are left
4623 * unchanged.
4626 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw)
4628 TEXTMETRICW tm;
4629 HDC dc;
4630 HFONT old_font = 0;
4631 RECT r;
4633 es->font = font;
4634 dc = GetDC(hwnd);
4635 if (font)
4636 old_font = SelectObject(dc, font);
4637 GetTextMetricsW(dc, &tm);
4638 es->line_height = tm.tmHeight;
4639 es->char_width = tm.tmAveCharWidth;
4640 if (font)
4641 SelectObject(dc, old_font);
4642 ReleaseDC(hwnd, dc);
4643 if (font && (TWEAK_WineLook > WIN31_LOOK))
4644 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4645 EC_USEFONTINFO, EC_USEFONTINFO);
4647 /* Force the recalculation of the format rect for each font change */
4648 GetClientRect(hwnd, &r);
4649 EDIT_SetRectNP(hwnd, es, &r);
4651 if (es->style & ES_MULTILINE)
4652 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
4653 else
4654 EDIT_CalcLineWidth_SL(hwnd, es);
4656 if (redraw)
4657 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4658 if (es->flags & EF_FOCUSED) {
4659 DestroyCaret();
4660 CreateCaret(hwnd, 0, 2, es->line_height);
4661 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4662 es->flags & EF_AFTER_WRAP);
4663 ShowCaret(hwnd);
4668 /*********************************************************************
4670 * WM_SETTEXT
4672 * NOTES
4673 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4674 * The modified flag is reset. No notifications are sent.
4676 * For single-line controls, reception of WM_SETTEXT triggers:
4677 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4680 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4682 LPWSTR text = NULL;
4684 if(unicode)
4685 text = (LPWSTR)lParam;
4686 else if (lParam)
4688 LPCSTR textA = (LPCSTR)lParam;
4689 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4690 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4691 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4694 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
4695 if (text) {
4696 TRACE("%s\n", debugstr_w(text));
4697 EDIT_EM_ReplaceSel(hwnd, es, FALSE, text, FALSE);
4698 if(!unicode)
4699 HeapFree(GetProcessHeap(), 0, text);
4700 } else {
4701 static const WCHAR empty_stringW[] = {0};
4702 TRACE("<NULL>\n");
4703 EDIT_EM_ReplaceSel(hwnd, es, FALSE, empty_stringW, FALSE);
4705 es->x_offset = 0;
4706 es->flags &= ~EF_MODIFIED;
4707 EDIT_EM_SetSel(hwnd, es, 0, 0, FALSE);
4708 /* Send the notification after the selection start and end have been set
4709 * edit control doesn't send notification on WM_SETTEXT
4710 * if it is multiline, or it is part of combobox
4712 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4713 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
4714 EDIT_EM_ScrollCaret(hwnd, es);
4718 /*********************************************************************
4720 * WM_SIZE
4723 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height)
4725 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4726 RECT rc;
4727 TRACE("width = %d, height = %d\n", width, height);
4728 SetRect(&rc, 0, 0, width, height);
4729 EDIT_SetRectNP(hwnd, es, &rc);
4730 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4735 /*********************************************************************
4737 * WM_STYLECHANGED
4739 * This message is sent by SetWindowLong on having changed either the Style
4740 * or the extended style.
4742 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4744 * See also EDIT_WM_NCCreate
4746 * It appears that the Windows version of the edit control allows the style
4747 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4748 * style variable which will generally be different. In this function we
4749 * update the internal style based on what changed in the externally visible
4750 * style.
4752 * Much of this content as based upon the MSDN, especially:
4753 * Platform SDK Documentation -> User Interface Services ->
4754 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4755 * Edit Control Styles
4757 static LRESULT EDIT_WM_StyleChanged (HWND hwnd,
4758 EDITSTATE *es,
4759 WPARAM which,
4760 const STYLESTRUCT *style)
4762 if (GWL_STYLE == which) {
4763 DWORD style_change_mask;
4764 DWORD new_style;
4765 /* Only a subset of changes can be applied after the control
4766 * has been created.
4768 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4769 ES_NUMBER;
4770 if (es->style & ES_MULTILINE)
4771 style_change_mask |= ES_WANTRETURN;
4773 new_style = style->styleNew & style_change_mask;
4775 /* Number overrides lowercase overrides uppercase (at least it
4776 * does in Win95). However I'll bet that ES_NUMBER would be
4777 * invalid under Win 3.1.
4779 if (new_style & ES_NUMBER) {
4780 ; /* do not override the ES_NUMBER */
4781 } else if (new_style & ES_LOWERCASE) {
4782 new_style &= ~ES_UPPERCASE;
4785 es->style = (es->style & ~style_change_mask) | new_style;
4786 } else if (GWL_EXSTYLE == which) {
4787 ; /* FIXME - what is needed here */
4788 } else {
4789 WARN ("Invalid style change %d\n",which);
4792 return 0;
4795 /*********************************************************************
4797 * WM_SYSKEYDOWN
4800 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data)
4802 if ((key == VK_BACK) && (key_data & 0x2000)) {
4803 if (EDIT_EM_CanUndo(es))
4804 EDIT_EM_Undo(hwnd, es);
4805 return 0;
4806 } else if (key == VK_UP || key == VK_DOWN) {
4807 if (EDIT_CheckCombo(hwnd, es, WM_SYSKEYDOWN, key))
4808 return 0;
4810 return DefWindowProcW(hwnd, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4814 /*********************************************************************
4816 * WM_TIMER
4819 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es)
4821 if (es->region_posx < 0) {
4822 EDIT_MoveBackward(hwnd, es, TRUE);
4823 } else if (es->region_posx > 0) {
4824 EDIT_MoveForward(hwnd, es, TRUE);
4827 * FIXME: gotta do some vertical scrolling here, like
4828 * EDIT_EM_LineScroll(hwnd, 0, 1);
4832 /*********************************************************************
4834 * WM_VSCROLL
4837 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
4839 INT dy;
4841 if (!(es->style & ES_MULTILINE))
4842 return 0;
4844 if (!(es->style & ES_AUTOVSCROLL))
4845 return 0;
4847 dy = 0;
4848 switch (action) {
4849 case SB_LINEUP:
4850 case SB_LINEDOWN:
4851 case SB_PAGEUP:
4852 case SB_PAGEDOWN:
4853 TRACE("action %d\n", action);
4854 EDIT_EM_Scroll(hwnd, es, action);
4855 return 0;
4856 case SB_TOP:
4857 TRACE("SB_TOP\n");
4858 dy = -es->y_offset;
4859 break;
4860 case SB_BOTTOM:
4861 TRACE("SB_BOTTOM\n");
4862 dy = es->line_count - 1 - es->y_offset;
4863 break;
4864 case SB_THUMBTRACK:
4865 TRACE("SB_THUMBTRACK %d\n", pos);
4866 es->flags |= EF_VSCROLL_TRACK;
4867 if(es->style & WS_VSCROLL)
4868 dy = pos - es->y_offset;
4869 else
4871 /* Assume default scroll range 0-100 */
4872 INT vlc, new_y;
4873 /* Sanity check */
4874 if(pos < 0 || pos > 100) return 0;
4875 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4876 new_y = pos * (es->line_count - vlc) / 100;
4877 dy = es->line_count ? (new_y - es->y_offset) : 0;
4878 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4879 es->line_count, es->y_offset, pos, dy);
4881 break;
4882 case SB_THUMBPOSITION:
4883 TRACE("SB_THUMBPOSITION %d\n", pos);
4884 es->flags &= ~EF_VSCROLL_TRACK;
4885 if(es->style & WS_VSCROLL)
4886 dy = pos - es->y_offset;
4887 else
4889 /* Assume default scroll range 0-100 */
4890 INT vlc, new_y;
4891 /* Sanity check */
4892 if(pos < 0 || pos > 100) return 0;
4893 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4894 new_y = pos * (es->line_count - vlc) / 100;
4895 dy = es->line_count ? (new_y - es->y_offset) : 0;
4896 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4897 es->line_count, es->y_offset, pos, dy);
4899 if (!dy)
4901 /* force scroll info update */
4902 EDIT_UpdateScrollInfo(hwnd, es);
4903 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
4905 break;
4906 case SB_ENDSCROLL:
4907 TRACE("SB_ENDSCROLL\n");
4908 break;
4910 * FIXME : the next two are undocumented !
4911 * Are we doing the right thing ?
4912 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4913 * although it's also a regular control message.
4915 case EM_GETTHUMB: /* this one is used by NT notepad */
4916 case EM_GETTHUMB16:
4918 LRESULT ret;
4919 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_VSCROLL)
4920 ret = GetScrollPos(hwnd, SB_VERT);
4921 else
4923 /* Assume default scroll range 0-100 */
4924 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4925 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4927 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4928 return ret;
4930 case EM_LINESCROLL16:
4931 TRACE("EM_LINESCROLL16 %d\n", pos);
4932 dy = pos;
4933 break;
4935 default:
4936 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4937 action, action);
4938 return 0;
4940 if (dy)
4941 EDIT_EM_LineScroll(hwnd, es, 0, dy);
4942 return 0;
4945 /*********************************************************************
4947 * EDIT_UpdateText
4950 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, HRGN hrgn, BOOL bErase)
4952 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4953 InvalidateRgn(hwnd, hrgn, bErase);
4957 /*********************************************************************
4959 * EDIT_UpdateText
4962 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase)
4964 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4965 InvalidateRect(hwnd, rc, bErase);