Changed the Wine internal cdrom interface to the NT model.
[wine/gsoc_dplay.git] / controls / edit.c
blob469b9d939a53e4182214914fa80823348abddca8
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-calculating 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 indexes 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 referring
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 window 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 l; /* line number */
2824 INT32 li; /* index of first char in line */
2825 INT32 count;
2826 l = EDIT_EM_LineFromChar(es, es->selection_start);
2827 /* # chars before start of selection area */
2828 count = es->selection_start - EDIT_EM_LineIndex(es, l);
2829 l = EDIT_EM_LineFromChar(es, es->selection_end);
2830 /* # chars after end of selection */
2831 li = EDIT_EM_LineIndex(es, l);
2832 count += li + EDIT_EM_LineLength(es, li) - es->selection_end;
2833 return count;
2835 line_def = es->first_line_def;
2836 index -= line_def->length;
2837 while ((index >= 0) && line_def->next) {
2838 line_def = line_def->next;
2839 index -= line_def->length;
2841 return line_def->net_length;
2845 /*********************************************************************
2847 * EM_LINESCROLL
2849 * NOTE: dx is in average character widths, dy - in lines;
2852 static BOOL EDIT_EM_LineScroll(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2854 if (!(es->style & ES_MULTILINE))
2855 return FALSE;
2857 dx *= es->char_width;
2858 return EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
2861 /*********************************************************************
2863 * EDIT_EM_LineScroll_internal
2865 * Version of EDIT_EM_LineScroll for internal use.
2866 * It doesn't refuse if ES_MULTILINE is set and assumes that
2867 * dx is in pixels, dy - in lines.
2870 static BOOL EDIT_EM_LineScroll_internal(HWND hwnd, EDITSTATE *es, INT dx, INT dy)
2872 INT nyoff;
2873 INT x_offset_in_pixels;
2875 if (es->style & ES_MULTILINE)
2877 x_offset_in_pixels = es->x_offset;
2879 else
2881 dy = 0;
2882 x_offset_in_pixels = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->x_offset, FALSE));
2885 if (-dx > x_offset_in_pixels)
2886 dx = -x_offset_in_pixels;
2887 if (dx > es->text_width - x_offset_in_pixels)
2888 dx = es->text_width - x_offset_in_pixels;
2889 nyoff = max(0, es->y_offset + dy);
2890 if (nyoff >= es->line_count)
2891 nyoff = es->line_count - 1;
2892 dy = (es->y_offset - nyoff) * es->line_height;
2893 if (dx || dy) {
2894 RECT rc1;
2895 RECT rc;
2897 es->y_offset = nyoff;
2898 if(es->style & ES_MULTILINE)
2899 es->x_offset += dx;
2900 else
2901 es->x_offset += dx / es->char_width;
2903 GetClientRect(hwnd, &rc1);
2904 IntersectRect(&rc, &rc1, &es->format_rect);
2905 ScrollWindowEx(hwnd, -dx, dy,
2906 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2907 /* force scroll info update */
2908 EDIT_UpdateScrollInfo(hwnd, es);
2910 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2911 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
2912 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2913 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
2914 return TRUE;
2918 /*********************************************************************
2920 * EM_POSFROMCHAR
2923 static LRESULT EDIT_EM_PosFromChar(HWND hwnd, EDITSTATE *es, INT index, BOOL after_wrap)
2925 INT len = strlenW(es->text);
2926 INT l;
2927 INT li;
2928 INT x;
2929 INT y = 0;
2930 HDC dc;
2931 HFONT old_font = 0;
2932 SIZE size;
2934 index = min(index, len);
2935 dc = GetDC(hwnd);
2936 if (es->font)
2937 old_font = SelectObject(dc, es->font);
2938 if (es->style & ES_MULTILINE) {
2939 l = EDIT_EM_LineFromChar(es, index);
2940 y = (l - es->y_offset) * es->line_height;
2941 li = EDIT_EM_LineIndex(es, l);
2942 if (after_wrap && (li == index) && l) {
2943 INT l2 = l - 1;
2944 LINEDEF *line_def = es->first_line_def;
2945 while (l2) {
2946 line_def = line_def->next;
2947 l2--;
2949 if (line_def->ending == END_WRAP) {
2950 l--;
2951 y -= es->line_height;
2952 li = EDIT_EM_LineIndex(es, l);
2955 x = LOWORD(GetTabbedTextExtentW(dc, es->text + li, index - li,
2956 es->tabs_count, es->tabs)) - es->x_offset;
2957 } else {
2958 LPWSTR text = EDIT_GetPasswordPointer_SL(es);
2959 if (index < es->x_offset) {
2960 GetTextExtentPoint32W(dc, text + index,
2961 es->x_offset - index, &size);
2962 x = -size.cx;
2963 } else {
2964 GetTextExtentPoint32W(dc, text + es->x_offset,
2965 index - es->x_offset, &size);
2966 x = size.cx;
2968 y = 0;
2969 if (es->style & ES_PASSWORD)
2970 HeapFree(GetProcessHeap(), 0, text);
2972 x += es->format_rect.left;
2973 y += es->format_rect.top;
2974 if (es->font)
2975 SelectObject(dc, old_font);
2976 ReleaseDC(hwnd, dc);
2977 return MAKELONG((INT16)x, (INT16)y);
2981 /*********************************************************************
2983 * EM_REPLACESEL
2985 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2988 static void EDIT_EM_ReplaceSel(HWND hwnd, EDITSTATE *es, BOOL can_undo, LPCWSTR lpsz_replace, BOOL send_update)
2990 UINT strl = strlenW(lpsz_replace);
2991 UINT tl = strlenW(es->text);
2992 UINT utl;
2993 UINT s;
2994 UINT e;
2995 UINT i;
2996 LPWSTR p;
2997 HRGN hrgn = 0;
2999 TRACE("%s, can_undo %d, send_update %d\n",
3000 debugstr_w(lpsz_replace), can_undo, send_update);
3002 s = es->selection_start;
3003 e = es->selection_end;
3005 if ((s == e) && !strl)
3006 return;
3008 ORDER_UINT(s, e);
3010 if (!EDIT_MakeFit(hwnd, es, tl - (e - s) + strl))
3011 return;
3013 if (e != s) {
3014 /* there is something to be deleted */
3015 TRACE("deleting stuff.\n");
3016 if (can_undo) {
3017 utl = strlenW(es->undo_text);
3018 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
3019 /* undo-buffer is extended to the right */
3020 EDIT_MakeUndoFit(es, utl + e - s);
3021 strncpyW(es->undo_text + utl, es->text + s, e - s + 1);
3022 (es->undo_text + utl)[e - s] = 0; /* ensure 0 termination */
3023 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
3024 /* undo-buffer is extended to the left */
3025 EDIT_MakeUndoFit(es, utl + e - s);
3026 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
3027 p[e - s] = p[0];
3028 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
3029 p[i] = (es->text + s)[i];
3030 es->undo_position = s;
3031 } else {
3032 /* new undo-buffer */
3033 EDIT_MakeUndoFit(es, e - s);
3034 strncpyW(es->undo_text, es->text + s, e - s + 1);
3035 es->undo_text[e - s] = 0; /* ensure 0 termination */
3036 es->undo_position = s;
3038 /* any deletion makes the old insertion-undo invalid */
3039 es->undo_insert_count = 0;
3040 } else
3041 EDIT_EM_EmptyUndoBuffer(es);
3043 /* now delete */
3044 strcpyW(es->text + s, es->text + e);
3046 if (strl) {
3047 /* there is an insertion */
3048 if (can_undo) {
3049 if ((s == es->undo_position) ||
3050 ((es->undo_insert_count) &&
3051 (s == es->undo_position + es->undo_insert_count)))
3053 * insertion is new and at delete position or
3054 * an extension to either left or right
3056 es->undo_insert_count += strl;
3057 else {
3058 /* new insertion undo */
3059 es->undo_position = s;
3060 es->undo_insert_count = strl;
3061 /* new insertion makes old delete-buffer invalid */
3062 *es->undo_text = '\0';
3064 } else
3065 EDIT_EM_EmptyUndoBuffer(es);
3067 /* now insert */
3068 tl = strlenW(es->text);
3069 TRACE("inserting stuff (tl %d, strl %d, selstart %d ('%s'), text '%s')\n", tl, strl, s, debugstr_w(es->text + s), debugstr_w(es->text));
3070 for (p = es->text + tl ; p >= es->text + s ; p--)
3071 p[strl] = p[0];
3072 for (i = 0 , p = es->text + s ; i < strl ; i++)
3073 p[i] = lpsz_replace[i];
3074 if(es->style & ES_UPPERCASE)
3075 CharUpperBuffW(p, strl);
3076 else if(es->style & ES_LOWERCASE)
3077 CharLowerBuffW(p, strl);
3078 s += strl;
3080 if (es->style & ES_MULTILINE)
3082 INT s = min(es->selection_start, es->selection_end);
3084 hrgn = CreateRectRgn(0, 0, 0, 0);
3085 EDIT_BuildLineDefs_ML(hwnd, es, s, s + strl,
3086 strl - abs(es->selection_end - es->selection_start), hrgn);
3088 else
3089 EDIT_CalcLineWidth_SL(hwnd, es);
3091 EDIT_EM_SetSel(hwnd, es, s, s, FALSE);
3092 es->flags |= EF_MODIFIED;
3093 if (send_update) es->flags |= EF_UPDATE;
3094 EDIT_EM_ScrollCaret(hwnd, es);
3096 /* force scroll info update */
3097 EDIT_UpdateScrollInfo(hwnd, es);
3099 if (hrgn)
3101 EDIT_UpdateTextRegion(hwnd, es, hrgn, TRUE);
3102 DeleteObject(hrgn);
3104 else
3105 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3107 if(es->flags & EF_UPDATE)
3109 es->flags &= ~EF_UPDATE;
3110 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3115 /*********************************************************************
3117 * EM_SCROLL
3120 static LRESULT EDIT_EM_Scroll(HWND hwnd, EDITSTATE *es, INT action)
3122 INT dy;
3124 if (!(es->style & ES_MULTILINE))
3125 return (LRESULT)FALSE;
3127 dy = 0;
3129 switch (action) {
3130 case SB_LINEUP:
3131 if (es->y_offset)
3132 dy = -1;
3133 break;
3134 case SB_LINEDOWN:
3135 if (es->y_offset < es->line_count - 1)
3136 dy = 1;
3137 break;
3138 case SB_PAGEUP:
3139 if (es->y_offset)
3140 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
3141 break;
3142 case SB_PAGEDOWN:
3143 if (es->y_offset < es->line_count - 1)
3144 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3145 break;
3146 default:
3147 return (LRESULT)FALSE;
3149 if (dy) {
3150 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3151 /* check if we are going to move too far */
3152 if(es->y_offset + dy > es->line_count - vlc)
3153 dy = es->line_count - vlc - es->y_offset;
3155 /* Notification is done in EDIT_EM_LineScroll */
3156 if(dy)
3157 EDIT_EM_LineScroll(hwnd, es, 0, dy);
3159 return MAKELONG((INT16)dy, (BOOL16)TRUE);
3163 /*********************************************************************
3165 * EM_SCROLLCARET
3168 static void EDIT_EM_ScrollCaret(HWND hwnd, EDITSTATE *es)
3170 if (es->style & ES_MULTILINE) {
3171 INT l;
3172 INT li;
3173 INT vlc;
3174 INT ww;
3175 INT cw = es->char_width;
3176 INT x;
3177 INT dy = 0;
3178 INT dx = 0;
3180 l = EDIT_EM_LineFromChar(es, es->selection_end);
3181 li = EDIT_EM_LineIndex(es, l);
3182 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
3183 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3184 if (l >= es->y_offset + vlc)
3185 dy = l - vlc + 1 - es->y_offset;
3186 if (l < es->y_offset)
3187 dy = l - es->y_offset;
3188 ww = es->format_rect.right - es->format_rect.left;
3189 if (x < es->format_rect.left)
3190 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
3191 if (x > es->format_rect.right)
3192 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
3193 if (dy || dx)
3195 /* check if we are going to move too far */
3196 if(es->x_offset + dx + ww > es->text_width)
3197 dx = es->text_width - ww - es->x_offset;
3198 if(dx || dy)
3199 EDIT_EM_LineScroll_internal(hwnd, es, dx, dy);
3201 } else {
3202 INT x;
3203 INT goal;
3204 INT format_width;
3206 if (!(es->style & ES_AUTOHSCROLL))
3207 return;
3209 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3210 format_width = es->format_rect.right - es->format_rect.left;
3211 if (x < es->format_rect.left) {
3212 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
3213 do {
3214 es->x_offset--;
3215 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3216 } while ((x < goal) && es->x_offset);
3217 /* FIXME: use ScrollWindow() somehow to improve performance */
3218 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3219 } else if (x > es->format_rect.right) {
3220 INT x_last;
3221 INT len = strlenW(es->text);
3222 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
3223 do {
3224 es->x_offset++;
3225 x = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, es->selection_end, FALSE));
3226 x_last = SLOWORD(EDIT_EM_PosFromChar(hwnd, es, len, FALSE));
3227 } while ((x > goal) && (x_last > es->format_rect.right));
3228 /* FIXME: use ScrollWindow() somehow to improve performance */
3229 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3233 if(es->flags & EF_FOCUSED)
3234 EDIT_SetCaretPos(hwnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3238 /*********************************************************************
3240 * EM_SETHANDLE
3242 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3245 static void EDIT_EM_SetHandle(HWND hwnd, EDITSTATE *es, HLOCAL hloc)
3247 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3249 if (!(es->style & ES_MULTILINE))
3250 return;
3252 if (!hloc) {
3253 WARN("called with NULL handle\n");
3254 return;
3257 EDIT_UnlockBuffer(hwnd, es, TRUE);
3259 if(es->hloc16)
3261 LOCAL_Free(hInstance, es->hloc16);
3262 es->hloc16 = (HLOCAL16)NULL;
3265 if(es->is_unicode)
3267 if(es->hloc32A)
3269 LocalFree(es->hloc32A);
3270 es->hloc32A = (HLOCAL)NULL;
3272 es->hloc32W = hloc;
3274 else
3276 INT countW, countA;
3277 HLOCAL hloc32W_new;
3278 WCHAR *textW;
3279 CHAR *textA;
3281 countA = LocalSize(hloc);
3282 textA = LocalLock(hloc);
3283 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3284 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3286 ERR("Could not allocate new unicode buffer\n");
3287 return;
3289 textW = LocalLock(hloc32W_new);
3290 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3291 LocalUnlock(hloc32W_new);
3292 LocalUnlock(hloc);
3294 if(es->hloc32W)
3295 LocalFree(es->hloc32W);
3297 es->hloc32W = hloc32W_new;
3298 es->hloc32A = hloc;
3301 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3303 EDIT_LockBuffer(hwnd, es);
3305 es->x_offset = es->y_offset = 0;
3306 es->selection_start = es->selection_end = 0;
3307 EDIT_EM_EmptyUndoBuffer(es);
3308 es->flags &= ~EF_MODIFIED;
3309 es->flags &= ~EF_UPDATE;
3310 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3311 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3312 EDIT_EM_ScrollCaret(hwnd, es);
3313 /* force scroll info update */
3314 EDIT_UpdateScrollInfo(hwnd, es);
3318 /*********************************************************************
3320 * EM_SETHANDLE16
3322 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
3325 static void EDIT_EM_SetHandle16(HWND hwnd, EDITSTATE *es, HLOCAL16 hloc)
3327 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3328 INT countW, countA;
3329 HLOCAL hloc32W_new;
3330 WCHAR *textW;
3331 CHAR *textA;
3333 if (!(es->style & ES_MULTILINE))
3334 return;
3336 if (!hloc) {
3337 WARN("called with NULL handle\n");
3338 return;
3341 EDIT_UnlockBuffer(hwnd, es, TRUE);
3343 if(es->hloc32A)
3345 LocalFree(es->hloc32A);
3346 es->hloc32A = (HLOCAL)NULL;
3349 countA = LOCAL_Size(hInstance, hloc);
3350 textA = LOCAL_Lock(hInstance, hloc);
3351 countW = MultiByteToWideChar(CP_ACP, 0, textA, countA, NULL, 0);
3352 if(!(hloc32W_new = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, countW * sizeof(WCHAR))))
3354 ERR("Could not allocate new unicode buffer\n");
3355 return;
3357 textW = LocalLock(hloc32W_new);
3358 MultiByteToWideChar(CP_ACP, 0, textA, countA, textW, countW);
3359 LocalUnlock(hloc32W_new);
3360 LOCAL_Unlock(hInstance, hloc);
3362 if(es->hloc32W)
3363 LocalFree(es->hloc32W);
3365 es->hloc32W = hloc32W_new;
3366 es->hloc16 = hloc;
3368 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
3370 EDIT_LockBuffer(hwnd, es);
3372 es->x_offset = es->y_offset = 0;
3373 es->selection_start = es->selection_end = 0;
3374 EDIT_EM_EmptyUndoBuffer(es);
3375 es->flags &= ~EF_MODIFIED;
3376 es->flags &= ~EF_UPDATE;
3377 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3378 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3379 EDIT_EM_ScrollCaret(hwnd, es);
3380 /* force scroll info update */
3381 EDIT_UpdateScrollInfo(hwnd, es);
3385 /*********************************************************************
3387 * EM_SETLIMITTEXT
3389 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
3390 * However, the windows version is not complied to yet in all of edit.c
3393 static void EDIT_EM_SetLimitText(EDITSTATE *es, INT limit)
3395 if (es->style & ES_MULTILINE) {
3396 if (limit)
3397 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
3398 else
3399 es->buffer_limit = BUFLIMIT_MULTI;
3400 } else {
3401 if (limit)
3402 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
3403 else
3404 es->buffer_limit = BUFLIMIT_SINGLE;
3409 /*********************************************************************
3411 * EM_SETMARGINS
3413 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
3414 * action wParam despite what the docs say. EC_USEFONTINFO means one third
3415 * of the char's width, according to the new docs.
3418 static void EDIT_EM_SetMargins(EDITSTATE *es, INT action,
3419 INT left, INT right)
3421 if (action & EC_LEFTMARGIN) {
3422 if (left != EC_USEFONTINFO)
3423 es->left_margin = left;
3424 else
3425 es->left_margin = es->char_width / 3;
3428 if (action & EC_RIGHTMARGIN) {
3429 if (right != EC_USEFONTINFO)
3430 es->right_margin = right;
3431 else
3432 es->right_margin = es->char_width / 3;
3434 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
3438 /*********************************************************************
3440 * EM_SETPASSWORDCHAR
3443 static void EDIT_EM_SetPasswordChar(HWND hwnd, EDITSTATE *es, WCHAR c)
3445 LONG style;
3447 if (es->style & ES_MULTILINE)
3448 return;
3450 if (es->password_char == c)
3451 return;
3453 style = GetWindowLongA( hwnd, GWL_STYLE );
3454 es->password_char = c;
3455 if (c) {
3456 SetWindowLongA( hwnd, GWL_STYLE, style | ES_PASSWORD );
3457 es->style |= ES_PASSWORD;
3458 } else {
3459 SetWindowLongA( hwnd, GWL_STYLE, style & ~ES_PASSWORD );
3460 es->style &= ~ES_PASSWORD;
3462 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3466 /*********************************************************************
3468 * EDIT_EM_SetSel
3470 * note: unlike the specs say: the order of start and end
3471 * _is_ preserved in Windows. (i.e. start can be > end)
3472 * In other words: this handler is OK
3475 static void EDIT_EM_SetSel(HWND hwnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
3477 UINT old_start = es->selection_start;
3478 UINT old_end = es->selection_end;
3479 UINT len = strlenW(es->text);
3481 if (start == (UINT)-1) {
3482 start = es->selection_end;
3483 end = es->selection_end;
3484 } else {
3485 start = min(start, len);
3486 end = min(end, len);
3488 es->selection_start = start;
3489 es->selection_end = end;
3490 if (after_wrap)
3491 es->flags |= EF_AFTER_WRAP;
3492 else
3493 es->flags &= ~EF_AFTER_WRAP;
3494 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
3495 ORDER_UINT(start, end);
3496 ORDER_UINT(end, old_end);
3497 ORDER_UINT(start, old_start);
3498 ORDER_UINT(old_start, old_end);
3499 if (end != old_start)
3502 * One can also do
3503 * ORDER_UINT32(end, old_start);
3504 * EDIT_InvalidateText(hwnd, es, start, end);
3505 * EDIT_InvalidateText(hwnd, es, old_start, old_end);
3506 * in place of the following if statement.
3508 if (old_start > end )
3510 EDIT_InvalidateText(hwnd, es, start, end);
3511 EDIT_InvalidateText(hwnd, es, old_start, old_end);
3513 else
3515 EDIT_InvalidateText(hwnd, es, start, old_start);
3516 EDIT_InvalidateText(hwnd, es, end, old_end);
3519 else EDIT_InvalidateText(hwnd, es, start, old_end);
3523 /*********************************************************************
3525 * EM_SETTABSTOPS
3528 static BOOL EDIT_EM_SetTabStops(EDITSTATE *es, INT count, LPINT tabs)
3530 if (!(es->style & ES_MULTILINE))
3531 return FALSE;
3532 if (es->tabs)
3533 HeapFree(GetProcessHeap(), 0, es->tabs);
3534 es->tabs_count = count;
3535 if (!count)
3536 es->tabs = NULL;
3537 else {
3538 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3539 memcpy(es->tabs, tabs, count * sizeof(INT));
3541 return TRUE;
3545 /*********************************************************************
3547 * EM_SETTABSTOPS16
3550 static BOOL EDIT_EM_SetTabStops16(EDITSTATE *es, INT count, LPINT16 tabs)
3552 if (!(es->style & ES_MULTILINE))
3553 return FALSE;
3554 if (es->tabs)
3555 HeapFree(GetProcessHeap(), 0, es->tabs);
3556 es->tabs_count = count;
3557 if (!count)
3558 es->tabs = NULL;
3559 else {
3560 INT i;
3561 es->tabs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
3562 for (i = 0 ; i < count ; i++)
3563 es->tabs[i] = *tabs++;
3565 return TRUE;
3569 /*********************************************************************
3571 * EM_SETWORDBREAKPROC
3574 static void EDIT_EM_SetWordBreakProc(HWND hwnd, EDITSTATE *es, LPARAM lParam)
3576 if (es->word_break_proc == (void *)lParam)
3577 return;
3579 es->word_break_proc = (void *)lParam;
3580 es->word_break_proc16 = NULL;
3582 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3583 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3584 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3589 /*********************************************************************
3591 * EM_SETWORDBREAKPROC16
3594 static void EDIT_EM_SetWordBreakProc16(HWND hwnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
3596 if (es->word_break_proc16 == wbp)
3597 return;
3599 es->word_break_proc = NULL;
3600 es->word_break_proc16 = wbp;
3601 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
3602 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
3603 EDIT_UpdateText(hwnd, es, NULL, TRUE);
3608 /*********************************************************************
3610 * EM_UNDO / WM_UNDO
3613 static BOOL EDIT_EM_Undo(HWND hwnd, EDITSTATE *es)
3615 INT ulength;
3616 LPWSTR utext;
3618 /* Protect read-only edit control from modification */
3619 if(es->style & ES_READONLY)
3620 return FALSE;
3622 ulength = strlenW(es->undo_text);
3623 utext = HeapAlloc(GetProcessHeap(), 0, (ulength + 1) * sizeof(WCHAR));
3625 strcpyW(utext, es->undo_text);
3627 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
3628 es->undo_insert_count, debugstr_w(utext));
3630 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3631 EDIT_EM_EmptyUndoBuffer(es);
3632 EDIT_EM_ReplaceSel(hwnd, es, TRUE, utext, FALSE);
3633 EDIT_EM_SetSel(hwnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
3634 /* send the notification after the selection start and end are set */
3635 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3636 EDIT_EM_ScrollCaret(hwnd, es);
3637 HeapFree(GetProcessHeap(), 0, utext);
3639 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
3640 es->undo_insert_count, debugstr_w(es->undo_text));
3641 return TRUE;
3645 /*********************************************************************
3647 * WM_CHAR
3650 static void EDIT_WM_Char(HWND hwnd, EDITSTATE *es, WCHAR c)
3652 BOOL control;
3654 /* Protect read-only edit control from modification */
3655 if(es->style & ES_READONLY)
3656 return;
3658 control = GetKeyState(VK_CONTROL) & 0x8000;
3660 switch (c) {
3661 case '\r':
3662 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
3663 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
3664 break;
3665 case '\n':
3666 if (es->style & ES_MULTILINE) {
3667 if (es->style & ES_READONLY) {
3668 EDIT_MoveHome(hwnd, es, FALSE);
3669 EDIT_MoveDown_ML(hwnd, es, FALSE);
3670 } else {
3671 static const WCHAR cr_lfW[] = {'\r','\n',0};
3672 EDIT_EM_ReplaceSel(hwnd, es, TRUE, cr_lfW, TRUE);
3675 break;
3676 case '\t':
3677 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
3679 static const WCHAR tabW[] = {'\t',0};
3680 EDIT_EM_ReplaceSel(hwnd, es, TRUE, tabW, TRUE);
3682 break;
3683 case VK_BACK:
3684 if (!(es->style & ES_READONLY) && !control) {
3685 if (es->selection_start != es->selection_end)
3686 EDIT_WM_Clear(hwnd, es);
3687 else {
3688 /* delete character left of caret */
3689 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
3690 EDIT_MoveBackward(hwnd, es, TRUE);
3691 EDIT_WM_Clear(hwnd, es);
3694 break;
3695 case 0x03: /* ^C */
3696 SendMessageW(hwnd, WM_COPY, 0, 0);
3697 break;
3698 case 0x16: /* ^V */
3699 SendMessageW(hwnd, WM_PASTE, 0, 0);
3700 break;
3701 case 0x18: /* ^X */
3702 SendMessageW(hwnd, WM_CUT, 0, 0);
3703 break;
3705 default:
3706 if (!(es->style & ES_READONLY) && (c >= ' ') && (c != 127)) {
3707 WCHAR str[2];
3708 str[0] = c;
3709 str[1] = '\0';
3710 EDIT_EM_ReplaceSel(hwnd, es, TRUE, str, TRUE);
3712 break;
3717 /*********************************************************************
3719 * WM_COMMAND
3722 static void EDIT_WM_Command(HWND hwnd, EDITSTATE *es, INT code, INT id, HWND control)
3724 if (code || control)
3725 return;
3727 switch (id) {
3728 case EM_UNDO:
3729 EDIT_EM_Undo(hwnd, es);
3730 break;
3731 case WM_CUT:
3732 EDIT_WM_Cut(hwnd, es);
3733 break;
3734 case WM_COPY:
3735 EDIT_WM_Copy(hwnd, es);
3736 break;
3737 case WM_PASTE:
3738 EDIT_WM_Paste(hwnd, es);
3739 break;
3740 case WM_CLEAR:
3741 EDIT_WM_Clear(hwnd, es);
3742 break;
3743 case EM_SETSEL:
3744 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
3745 EDIT_EM_ScrollCaret(hwnd, es);
3746 break;
3747 default:
3748 ERR("unknown menu item, please report\n");
3749 break;
3754 /*********************************************************************
3756 * WM_CONTEXTMENU
3758 * Note: the resource files resource/sysres_??.rc cannot define a
3759 * single popup menu. Hence we use a (dummy) menubar
3760 * containing the single popup menu as its first item.
3762 * FIXME: the message identifiers have been chosen arbitrarily,
3763 * hence we use MF_BYPOSITION.
3764 * We might as well use the "real" values (anybody knows ?)
3765 * The menu definition is in resources/sysres_??.rc.
3766 * Once these are OK, we better use MF_BYCOMMAND here
3767 * (as we do in EDIT_WM_Command()).
3770 static void EDIT_WM_ContextMenu(HWND hwnd, EDITSTATE *es, INT x, INT y)
3772 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3773 HMENU popup = GetSubMenu(menu, 0);
3774 UINT start = es->selection_start;
3775 UINT end = es->selection_end;
3777 ORDER_UINT(start, end);
3779 /* undo */
3780 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(es) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3781 /* cut */
3782 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3783 /* copy */
3784 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3785 /* paste */
3786 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_UNICODETEXT) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3787 /* delete */
3788 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) && !(es->style & ES_READONLY) ? MF_ENABLED : MF_GRAYED));
3789 /* select all */
3790 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != strlenW(es->text)) ? MF_ENABLED : MF_GRAYED));
3792 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, hwnd, NULL);
3793 DestroyMenu(menu);
3797 /*********************************************************************
3799 * WM_COPY
3802 static void EDIT_WM_Copy(HWND hwnd, EDITSTATE *es)
3804 INT s = es->selection_start;
3805 INT e = es->selection_end;
3806 HGLOBAL hdst;
3807 LPWSTR dst;
3809 if (e == s)
3810 return;
3811 ORDER_INT(s, e);
3812 hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (DWORD)(e - s + 1) * sizeof(WCHAR));
3813 dst = GlobalLock(hdst);
3814 strncpyW(dst, es->text + s, e - s);
3815 dst[e - s] = 0; /* ensure 0 termination */
3816 TRACE("%s\n", debugstr_w(dst));
3817 GlobalUnlock(hdst);
3818 OpenClipboard(hwnd);
3819 EmptyClipboard();
3820 SetClipboardData(CF_UNICODETEXT, hdst);
3821 CloseClipboard();
3825 /*********************************************************************
3827 * WM_CREATE
3830 static LRESULT EDIT_WM_Create(HWND hwnd, EDITSTATE *es, LPCWSTR name)
3832 TRACE("%s\n", debugstr_w(name));
3834 * To initialize some final structure members, we call some helper
3835 * functions. However, since the EDITSTATE is not consistent (i.e.
3836 * not fully initialized), we should be very careful which
3837 * functions can be called, and in what order.
3839 EDIT_WM_SetFont(hwnd, es, 0, FALSE);
3840 EDIT_EM_EmptyUndoBuffer(es);
3842 if (name && *name) {
3843 EDIT_EM_ReplaceSel(hwnd, es, FALSE, name, FALSE);
3844 /* if we insert text to the editline, the text scrolls out
3845 * of the window, as the caret is placed after the insert
3846 * pos normally; thus we reset es->selection... to 0 and
3847 * update caret
3849 es->selection_start = es->selection_end = 0;
3850 /* send the notification after the selection start and end are set */
3851 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
3852 EDIT_EM_ScrollCaret(hwnd, es);
3854 /* force scroll info update */
3855 EDIT_UpdateScrollInfo(hwnd, es);
3856 return 0;
3860 /*********************************************************************
3862 * WM_DESTROY
3865 static void EDIT_WM_Destroy(HWND hwnd, EDITSTATE *es)
3867 HINSTANCE hInstance = GetWindowLongA( hwnd, GWL_HINSTANCE );
3868 LINEDEF *pc, *pp;
3870 if (es->hloc32W) {
3871 while (LocalUnlock(es->hloc32W)) ;
3872 LocalFree(es->hloc32W);
3874 if (es->hloc32A) {
3875 while (LocalUnlock(es->hloc32A)) ;
3876 LocalFree(es->hloc32A);
3878 if (es->hloc16) {
3879 while (LOCAL_Unlock(hInstance, es->hloc16)) ;
3880 LOCAL_Free(hInstance, es->hloc16);
3883 pc = es->first_line_def;
3884 while (pc)
3886 pp = pc->next;
3887 HeapFree(GetProcessHeap(), 0, pc);
3888 pc = pp;
3891 SetWindowLongA( hwnd, 0, 0 );
3892 HeapFree(GetProcessHeap(), 0, es);
3896 /*********************************************************************
3898 * WM_ERASEBKGND
3901 static LRESULT EDIT_WM_EraseBkGnd(HWND hwnd, EDITSTATE *es, HDC dc)
3903 HBRUSH brush;
3904 RECT rc;
3906 if ( get_app_version() >= 0x40000 &&(
3907 !es->bEnableState || (es->style & ES_READONLY)))
3908 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
3909 else
3910 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(hwnd, dc);
3912 if (!brush)
3913 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3915 GetClientRect(hwnd, &rc);
3916 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3917 GetClipBox(dc, &rc);
3919 * FIXME: specs say that we should UnrealizeObject() the brush,
3920 * but the specs of UnrealizeObject() say that we shouldn't
3921 * unrealize a stock object. The default brush that
3922 * DefWndProc() returns is ... a stock object.
3924 FillRect(dc, &rc, brush);
3925 return -1;
3929 /*********************************************************************
3931 * WM_GETTEXT
3934 static INT EDIT_WM_GetText(EDITSTATE *es, INT count, LPARAM lParam, BOOL unicode)
3936 if(!count) return 0;
3938 if(unicode)
3940 LPWSTR textW = (LPWSTR)lParam;
3941 strncpyW(textW, es->text, count);
3942 textW[count - 1] = 0; /* ensure 0 termination */
3943 return strlenW(textW);
3945 else
3947 LPSTR textA = (LPSTR)lParam;
3948 WideCharToMultiByte(CP_ACP, 0, es->text, -1, textA, count, NULL, NULL);
3949 textA[count - 1] = 0; /* ensure 0 termination */
3950 return strlen(textA);
3954 /*********************************************************************
3956 * WM_HSCROLL
3959 static LRESULT EDIT_WM_HScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
3961 INT dx;
3962 INT fw;
3964 if (!(es->style & ES_MULTILINE))
3965 return 0;
3967 if (!(es->style & ES_AUTOHSCROLL))
3968 return 0;
3970 dx = 0;
3971 fw = es->format_rect.right - es->format_rect.left;
3972 switch (action) {
3973 case SB_LINELEFT:
3974 TRACE("SB_LINELEFT\n");
3975 if (es->x_offset)
3976 dx = -es->char_width;
3977 break;
3978 case SB_LINERIGHT:
3979 TRACE("SB_LINERIGHT\n");
3980 if (es->x_offset < es->text_width)
3981 dx = es->char_width;
3982 break;
3983 case SB_PAGELEFT:
3984 TRACE("SB_PAGELEFT\n");
3985 if (es->x_offset)
3986 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3987 break;
3988 case SB_PAGERIGHT:
3989 TRACE("SB_PAGERIGHT\n");
3990 if (es->x_offset < es->text_width)
3991 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3992 break;
3993 case SB_LEFT:
3994 TRACE("SB_LEFT\n");
3995 if (es->x_offset)
3996 dx = -es->x_offset;
3997 break;
3998 case SB_RIGHT:
3999 TRACE("SB_RIGHT\n");
4000 if (es->x_offset < es->text_width)
4001 dx = es->text_width - es->x_offset;
4002 break;
4003 case SB_THUMBTRACK:
4004 TRACE("SB_THUMBTRACK %d\n", pos);
4005 es->flags |= EF_HSCROLL_TRACK;
4006 if(es->style & WS_HSCROLL)
4007 dx = pos - es->x_offset;
4008 else
4010 INT fw, new_x;
4011 /* Sanity check */
4012 if(pos < 0 || pos > 100) return 0;
4013 /* Assume default scroll range 0-100 */
4014 fw = es->format_rect.right - es->format_rect.left;
4015 new_x = pos * (es->text_width - fw) / 100;
4016 dx = es->text_width ? (new_x - es->x_offset) : 0;
4018 break;
4019 case SB_THUMBPOSITION:
4020 TRACE("SB_THUMBPOSITION %d\n", pos);
4021 es->flags &= ~EF_HSCROLL_TRACK;
4022 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4023 dx = pos - es->x_offset;
4024 else
4026 INT fw, new_x;
4027 /* Sanity check */
4028 if(pos < 0 || pos > 100) return 0;
4029 /* Assume default scroll range 0-100 */
4030 fw = es->format_rect.right - es->format_rect.left;
4031 new_x = pos * (es->text_width - fw) / 100;
4032 dx = es->text_width ? (new_x - es->x_offset) : 0;
4034 if (!dx) {
4035 /* force scroll info update */
4036 EDIT_UpdateScrollInfo(hwnd, es);
4037 EDIT_NOTIFY_PARENT(hwnd, es, EN_HSCROLL, "EN_HSCROLL");
4039 break;
4040 case SB_ENDSCROLL:
4041 TRACE("SB_ENDSCROLL\n");
4042 break;
4044 * FIXME : the next two are undocumented !
4045 * Are we doing the right thing ?
4046 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4047 * although it's also a regular control message.
4049 case EM_GETTHUMB: /* this one is used by NT notepad */
4050 case EM_GETTHUMB16:
4052 LRESULT ret;
4053 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_HSCROLL)
4054 ret = GetScrollPos(hwnd, SB_HORZ);
4055 else
4057 /* Assume default scroll range 0-100 */
4058 INT fw = es->format_rect.right - es->format_rect.left;
4059 ret = es->text_width ? es->x_offset * 100 / (es->text_width - fw) : 0;
4061 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4062 return ret;
4064 case EM_LINESCROLL16:
4065 TRACE("EM_LINESCROLL16\n");
4066 dx = pos;
4067 break;
4069 default:
4070 ERR("undocumented WM_HSCROLL action %d (0x%04x), please report\n",
4071 action, action);
4072 return 0;
4074 if (dx)
4076 INT fw = es->format_rect.right - es->format_rect.left;
4077 /* check if we are going to move too far */
4078 if(es->x_offset + dx + fw > es->text_width)
4079 dx = es->text_width - fw - es->x_offset;
4080 if(dx)
4081 EDIT_EM_LineScroll_internal(hwnd, es, dx, 0);
4083 return 0;
4087 /*********************************************************************
4089 * EDIT_CheckCombo
4092 static BOOL EDIT_CheckCombo(HWND hwnd, EDITSTATE *es, UINT msg, INT key)
4094 HWND hLBox = es->hwndListBox;
4095 HWND hCombo;
4096 BOOL bDropped;
4097 int nEUI;
4099 if (!hLBox)
4100 return FALSE;
4102 hCombo = GetParent(hwnd);
4103 bDropped = TRUE;
4104 nEUI = 0;
4106 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
4107 hwnd, (UINT16)msg, (UINT16)key);
4109 if (key == VK_UP || key == VK_DOWN)
4111 if (SendMessageW(hCombo, CB_GETEXTENDEDUI, 0, 0))
4112 nEUI = 1;
4114 if (msg == WM_KEYDOWN || nEUI)
4115 bDropped = (BOOL)SendMessageW(hCombo, CB_GETDROPPEDSTATE, 0, 0);
4118 switch (msg)
4120 case WM_KEYDOWN:
4121 if (!bDropped && nEUI && (key == VK_UP || key == VK_DOWN))
4123 /* make sure ComboLBox pops up */
4124 SendMessageW(hCombo, CB_SETEXTENDEDUI, FALSE, 0);
4125 key = VK_F4;
4126 nEUI = 2;
4129 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
4130 break;
4132 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
4133 if (nEUI)
4134 SendMessageW(hCombo, CB_SHOWDROPDOWN, bDropped ? FALSE : TRUE, 0);
4135 else
4136 SendMessageW(hLBox, WM_KEYDOWN, (WPARAM)VK_F4, 0);
4137 break;
4140 if(nEUI == 2)
4141 SendMessageW(hCombo, CB_SETEXTENDEDUI, TRUE, 0);
4143 return TRUE;
4147 /*********************************************************************
4149 * WM_KEYDOWN
4151 * Handling of special keys that don't produce a WM_CHAR
4152 * (i.e. non-printable keys) & Backspace & Delete
4155 static LRESULT EDIT_WM_KeyDown(HWND hwnd, EDITSTATE *es, INT key)
4157 BOOL shift;
4158 BOOL control;
4160 if (GetKeyState(VK_MENU) & 0x8000)
4161 return 0;
4163 shift = GetKeyState(VK_SHIFT) & 0x8000;
4164 control = GetKeyState(VK_CONTROL) & 0x8000;
4166 switch (key) {
4167 case VK_F4:
4168 case VK_UP:
4169 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key) || key == VK_F4)
4170 break;
4172 /* fall through */
4173 case VK_LEFT:
4174 if ((es->style & ES_MULTILINE) && (key == VK_UP))
4175 EDIT_MoveUp_ML(hwnd, es, shift);
4176 else
4177 if (control)
4178 EDIT_MoveWordBackward(hwnd, es, shift);
4179 else
4180 EDIT_MoveBackward(hwnd, es, shift);
4181 break;
4182 case VK_DOWN:
4183 if (EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key))
4184 break;
4185 /* fall through */
4186 case VK_RIGHT:
4187 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
4188 EDIT_MoveDown_ML(hwnd, es, shift);
4189 else if (control)
4190 EDIT_MoveWordForward(hwnd, es, shift);
4191 else
4192 EDIT_MoveForward(hwnd, es, shift);
4193 break;
4194 case VK_HOME:
4195 EDIT_MoveHome(hwnd, es, shift);
4196 break;
4197 case VK_END:
4198 EDIT_MoveEnd(hwnd, es, shift);
4199 break;
4200 case VK_PRIOR:
4201 if (es->style & ES_MULTILINE)
4202 EDIT_MovePageUp_ML(hwnd, es, shift);
4203 else
4204 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4205 break;
4206 case VK_NEXT:
4207 if (es->style & ES_MULTILINE)
4208 EDIT_MovePageDown_ML(hwnd, es, shift);
4209 else
4210 EDIT_CheckCombo(hwnd, es, WM_KEYDOWN, key);
4211 break;
4212 case VK_DELETE:
4213 if (!(es->style & ES_READONLY) && !(shift && control)) {
4214 if (es->selection_start != es->selection_end) {
4215 if (shift)
4216 EDIT_WM_Cut(hwnd, es);
4217 else
4218 EDIT_WM_Clear(hwnd, es);
4219 } else {
4220 if (shift) {
4221 /* delete character left of caret */
4222 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4223 EDIT_MoveBackward(hwnd, es, TRUE);
4224 EDIT_WM_Clear(hwnd, es);
4225 } else if (control) {
4226 /* delete to end of line */
4227 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4228 EDIT_MoveEnd(hwnd, es, TRUE);
4229 EDIT_WM_Clear(hwnd, es);
4230 } else {
4231 /* delete character right of caret */
4232 EDIT_EM_SetSel(hwnd, es, (UINT)-1, 0, FALSE);
4233 EDIT_MoveForward(hwnd, es, TRUE);
4234 EDIT_WM_Clear(hwnd, es);
4238 break;
4239 case VK_INSERT:
4240 if (shift) {
4241 if (!(es->style & ES_READONLY))
4242 EDIT_WM_Paste(hwnd, es);
4243 } else if (control)
4244 EDIT_WM_Copy(hwnd, es);
4245 break;
4246 case VK_RETURN:
4247 /* If the edit doesn't want the return send a message to the default object */
4248 if(!(es->style & ES_WANTRETURN))
4250 HWND hwndParent = GetParent(hwnd);
4251 DWORD dw = SendMessageW( hwndParent, DM_GETDEFID, 0, 0 );
4252 if (HIWORD(dw) == DC_HASDEFID)
4254 SendMessageW( hwndParent, WM_COMMAND,
4255 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
4256 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
4259 break;
4261 return 0;
4265 /*********************************************************************
4267 * WM_KILLFOCUS
4270 static LRESULT EDIT_WM_KillFocus(HWND hwnd, EDITSTATE *es)
4272 es->flags &= ~EF_FOCUSED;
4273 DestroyCaret();
4274 if(!(es->style & ES_NOHIDESEL))
4275 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4276 EDIT_NOTIFY_PARENT(hwnd, es, EN_KILLFOCUS, "EN_KILLFOCUS");
4277 return 0;
4281 /*********************************************************************
4283 * WM_LBUTTONDBLCLK
4285 * The caret position has been set on the WM_LBUTTONDOWN message
4288 static LRESULT EDIT_WM_LButtonDblClk(HWND hwnd, EDITSTATE *es)
4290 INT s;
4291 INT e = es->selection_end;
4292 INT l;
4293 INT li;
4294 INT ll;
4296 if (!(es->flags & EF_FOCUSED))
4297 return 0;
4299 l = EDIT_EM_LineFromChar(es, e);
4300 li = EDIT_EM_LineIndex(es, l);
4301 ll = EDIT_EM_LineLength(es, e);
4302 s = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_LEFT);
4303 e = li + EDIT_CallWordBreakProc(es, li, e - li, ll, WB_RIGHT);
4304 EDIT_EM_SetSel(hwnd, es, s, e, FALSE);
4305 EDIT_EM_ScrollCaret(hwnd, es);
4306 return 0;
4310 /*********************************************************************
4312 * WM_LBUTTONDOWN
4315 static LRESULT EDIT_WM_LButtonDown(HWND hwnd, EDITSTATE *es, DWORD keys, INT x, INT y)
4317 INT e;
4318 BOOL after_wrap;
4320 if (!(es->flags & EF_FOCUSED))
4321 return 0;
4323 es->bCaptureState = TRUE;
4324 SetCapture(hwnd);
4325 EDIT_ConfinePoint(es, &x, &y);
4326 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4327 EDIT_EM_SetSel(hwnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
4328 EDIT_EM_ScrollCaret(hwnd, es);
4329 es->region_posx = es->region_posy = 0;
4330 SetTimer(hwnd, 0, 100, NULL);
4331 return 0;
4335 /*********************************************************************
4337 * WM_LBUTTONUP
4340 static LRESULT EDIT_WM_LButtonUp(HWND hwndSelf, EDITSTATE *es)
4342 if (es->bCaptureState && GetCapture() == hwndSelf) {
4343 KillTimer(hwndSelf, 0);
4344 ReleaseCapture();
4346 es->bCaptureState = FALSE;
4347 return 0;
4351 /*********************************************************************
4353 * WM_MBUTTONDOWN
4356 static LRESULT EDIT_WM_MButtonDown(HWND hwnd)
4358 SendMessageW(hwnd,WM_PASTE,0,0);
4359 return 0;
4363 /*********************************************************************
4365 * WM_MOUSEMOVE
4368 static LRESULT EDIT_WM_MouseMove(HWND hwnd, EDITSTATE *es, INT x, INT y)
4370 INT e;
4371 BOOL after_wrap;
4372 INT prex, prey;
4374 if (GetCapture() != hwnd)
4375 return 0;
4378 * FIXME: gotta do some scrolling if outside client
4379 * area. Maybe reset the timer ?
4381 prex = x; prey = y;
4382 EDIT_ConfinePoint(es, &x, &y);
4383 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
4384 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
4385 e = EDIT_CharFromPos(hwnd, es, x, y, &after_wrap);
4386 EDIT_EM_SetSel(hwnd, es, es->selection_start, e, after_wrap);
4387 return 0;
4391 /*********************************************************************
4393 * WM_NCCREATE
4395 * See also EDIT_WM_StyleChanged
4397 static LRESULT EDIT_WM_NCCreate(HWND hwnd, DWORD style, HWND hwndParent, BOOL unicode)
4399 EDITSTATE *es;
4400 UINT alloc_size;
4402 TRACE("Creating %s edit control, style = %08lx\n",
4403 unicode ? "Unicode" : "ANSI", style);
4405 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
4406 return FALSE;
4407 SetWindowLongA( hwnd, 0, (LONG)es );
4410 * Note: since the EDITSTATE has not been fully initialized yet,
4411 * we can't use any API calls that may send
4412 * WM_XXX messages before WM_NCCREATE is completed.
4415 es->is_unicode = unicode;
4416 es->style = style;
4418 es->bEnableState = !(style & WS_DISABLED);
4420 /* Save parent, which will be notified by EN_* messages */
4421 es->hwndParent = hwndParent;
4423 if (es->style & ES_COMBO)
4424 es->hwndListBox = GetDlgItem(hwndParent, ID_CB_LISTBOX);
4426 /* Number overrides lowercase overrides uppercase (at least it
4427 * does in Win95). However I'll bet that ES_NUMBER would be
4428 * invalid under Win 3.1.
4430 if (es->style & ES_NUMBER) {
4431 ; /* do not override the ES_NUMBER */
4432 } else if (es->style & ES_LOWERCASE) {
4433 es->style &= ~ES_UPPERCASE;
4435 if (es->style & ES_MULTILINE) {
4436 es->buffer_limit = BUFLIMIT_MULTI;
4437 if (es->style & WS_VSCROLL)
4438 es->style |= ES_AUTOVSCROLL;
4439 if (es->style & WS_HSCROLL)
4440 es->style |= ES_AUTOHSCROLL;
4441 es->style &= ~ES_PASSWORD;
4442 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
4443 /* Confirmed - RIGHT overrides CENTER */
4444 if (es->style & ES_RIGHT)
4445 es->style &= ~ES_CENTER;
4446 es->style &= ~WS_HSCROLL;
4447 es->style &= ~ES_AUTOHSCROLL;
4450 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
4451 es->style |= ES_AUTOVSCROLL;
4452 } else {
4453 es->buffer_limit = BUFLIMIT_SINGLE;
4454 if (WIN31_LOOK == TWEAK_WineLook ||
4455 WIN95_LOOK == TWEAK_WineLook) {
4456 es->style &= ~ES_CENTER;
4457 es->style &= ~ES_RIGHT;
4458 } else {
4459 if (es->style & ES_RIGHT)
4460 es->style &= ~ES_CENTER;
4462 es->style &= ~WS_HSCROLL;
4463 es->style &= ~WS_VSCROLL;
4464 es->style &= ~ES_AUTOVSCROLL;
4465 es->style &= ~ES_WANTRETURN;
4466 if (es->style & ES_PASSWORD)
4467 es->password_char = '*';
4469 /* FIXME: for now, all single line controls are AUTOHSCROLL */
4470 es->style |= ES_AUTOHSCROLL;
4473 alloc_size = ROUND_TO_GROW((es->buffer_size + 1) * sizeof(WCHAR));
4474 if(!(es->hloc32W = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, alloc_size)))
4475 return FALSE;
4476 es->buffer_size = LocalSize(es->hloc32W)/sizeof(WCHAR) - 1;
4478 if (!(es->undo_text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (es->buffer_size + 1) * sizeof(WCHAR))))
4479 return FALSE;
4480 es->undo_buffer_size = es->buffer_size;
4482 if (es->style & ES_MULTILINE)
4483 if (!(es->first_line_def = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
4484 return FALSE;
4485 es->line_count = 1;
4488 * In Win95 look and feel, the WS_BORDER style is replaced by the
4489 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
4490 * control a non client area. Not always. This coordinates in some
4491 * way with the window creation code in dialog.c When making
4492 * modifications please ensure that the code still works for edit
4493 * controls created directly with style 0x50800000, exStyle 0 (
4494 * which should have a single pixel border)
4496 if (TWEAK_WineLook != WIN31_LOOK)
4498 es->style &= ~WS_BORDER;
4500 else
4502 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
4503 SetWindowLongA( hwnd, GWL_STYLE,
4504 GetWindowLongA( hwnd, GWL_STYLE ) & ~WS_BORDER );
4507 return TRUE;
4510 /*********************************************************************
4512 * WM_PAINT
4515 static void EDIT_WM_Paint(HWND hwnd, EDITSTATE *es, WPARAM wParam)
4517 PAINTSTRUCT ps;
4518 INT i;
4519 HDC dc;
4520 HFONT old_font = 0;
4521 RECT rc;
4522 RECT rcLine;
4523 RECT rcRgn;
4524 BOOL rev = es->bEnableState &&
4525 ((es->flags & EF_FOCUSED) ||
4526 (es->style & ES_NOHIDESEL));
4527 if (!wParam)
4528 dc = BeginPaint(hwnd, &ps);
4529 else
4530 dc = (HDC) wParam;
4531 if(es->style & WS_BORDER) {
4532 GetClientRect(hwnd, &rc);
4533 if(es->style & ES_MULTILINE) {
4534 if(es->style & WS_HSCROLL) rc.bottom++;
4535 if(es->style & WS_VSCROLL) rc.right++;
4537 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
4539 IntersectClipRect(dc, es->format_rect.left,
4540 es->format_rect.top,
4541 es->format_rect.right,
4542 es->format_rect.bottom);
4543 if (es->style & ES_MULTILINE) {
4544 GetClientRect(hwnd, &rc);
4545 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
4547 if (es->font)
4548 old_font = SelectObject(dc, es->font);
4549 if ( get_app_version() >= 0x40000 &&(
4550 !es->bEnableState || (es->style & ES_READONLY)))
4551 EDIT_SEND_CTLCOLORSTATIC(hwnd, dc);
4552 else
4553 EDIT_SEND_CTLCOLOR(hwnd, dc);
4555 if (!es->bEnableState)
4556 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
4557 GetClipBox(dc, &rcRgn);
4558 if (es->style & ES_MULTILINE) {
4559 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4560 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
4561 EDIT_GetLineRect(hwnd, es, i, 0, -1, &rcLine);
4562 if (IntersectRect(&rc, &rcRgn, &rcLine))
4563 EDIT_PaintLine(hwnd, es, dc, i, rev);
4565 } else {
4566 EDIT_GetLineRect(hwnd, es, 0, 0, -1, &rcLine);
4567 if (IntersectRect(&rc, &rcRgn, &rcLine))
4568 EDIT_PaintLine(hwnd, es, dc, 0, rev);
4570 if (es->font)
4571 SelectObject(dc, old_font);
4573 if (!wParam)
4574 EndPaint(hwnd, &ps);
4578 /*********************************************************************
4580 * WM_PASTE
4583 static void EDIT_WM_Paste(HWND hwnd, EDITSTATE *es)
4585 HGLOBAL hsrc;
4586 LPWSTR src;
4588 /* Protect read-only edit control from modification */
4589 if(es->style & ES_READONLY)
4590 return;
4592 OpenClipboard(hwnd);
4593 if ((hsrc = GetClipboardData(CF_UNICODETEXT))) {
4594 src = (LPWSTR)GlobalLock(hsrc);
4595 EDIT_EM_ReplaceSel(hwnd, es, TRUE, src, TRUE);
4596 GlobalUnlock(hsrc);
4598 CloseClipboard();
4602 /*********************************************************************
4604 * WM_SETFOCUS
4607 static void EDIT_WM_SetFocus(HWND hwnd, EDITSTATE *es)
4609 es->flags |= EF_FOCUSED;
4610 CreateCaret(hwnd, 0, 2, es->line_height);
4611 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4612 es->flags & EF_AFTER_WRAP);
4613 if(!(es->style & ES_NOHIDESEL))
4614 EDIT_InvalidateText(hwnd, es, es->selection_start, es->selection_end);
4615 ShowCaret(hwnd);
4616 EDIT_NOTIFY_PARENT(hwnd, es, EN_SETFOCUS, "EN_SETFOCUS");
4620 /*********************************************************************
4622 * WM_SETFONT
4624 * With Win95 look the margins are set to default font value unless
4625 * the system font (font == 0) is being set, in which case they are left
4626 * unchanged.
4629 static void EDIT_WM_SetFont(HWND hwnd, EDITSTATE *es, HFONT font, BOOL redraw)
4631 TEXTMETRICW tm;
4632 HDC dc;
4633 HFONT old_font = 0;
4634 RECT r;
4636 es->font = font;
4637 dc = GetDC(hwnd);
4638 if (font)
4639 old_font = SelectObject(dc, font);
4640 GetTextMetricsW(dc, &tm);
4641 es->line_height = tm.tmHeight;
4642 es->char_width = tm.tmAveCharWidth;
4643 if (font)
4644 SelectObject(dc, old_font);
4645 ReleaseDC(hwnd, dc);
4646 if (font && (TWEAK_WineLook > WIN31_LOOK))
4647 EDIT_EM_SetMargins(es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
4648 EC_USEFONTINFO, EC_USEFONTINFO);
4650 /* Force the recalculation of the format rect for each font change */
4651 GetClientRect(hwnd, &r);
4652 EDIT_SetRectNP(hwnd, es, &r);
4654 if (es->style & ES_MULTILINE)
4655 EDIT_BuildLineDefs_ML(hwnd, es, 0, strlenW(es->text), 0, (HRGN)0);
4656 else
4657 EDIT_CalcLineWidth_SL(hwnd, es);
4659 if (redraw)
4660 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4661 if (es->flags & EF_FOCUSED) {
4662 DestroyCaret();
4663 CreateCaret(hwnd, 0, 2, es->line_height);
4664 EDIT_SetCaretPos(hwnd, es, es->selection_end,
4665 es->flags & EF_AFTER_WRAP);
4666 ShowCaret(hwnd);
4671 /*********************************************************************
4673 * WM_SETTEXT
4675 * NOTES
4676 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
4677 * The modified flag is reset. No notifications are sent.
4679 * For single-line controls, reception of WM_SETTEXT triggers:
4680 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
4683 static void EDIT_WM_SetText(HWND hwnd, EDITSTATE *es, LPARAM lParam, BOOL unicode)
4685 LPWSTR text = NULL;
4687 if(unicode)
4688 text = (LPWSTR)lParam;
4689 else if (lParam)
4691 LPCSTR textA = (LPCSTR)lParam;
4692 INT countW = MultiByteToWideChar(CP_ACP, 0, textA, -1, NULL, 0);
4693 if((text = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4694 MultiByteToWideChar(CP_ACP, 0, textA, -1, text, countW);
4697 EDIT_EM_SetSel(hwnd, es, 0, (UINT)-1, FALSE);
4698 if (text) {
4699 TRACE("%s\n", debugstr_w(text));
4700 EDIT_EM_ReplaceSel(hwnd, es, FALSE, text, FALSE);
4701 if(!unicode)
4702 HeapFree(GetProcessHeap(), 0, text);
4703 } else {
4704 static const WCHAR empty_stringW[] = {0};
4705 TRACE("<NULL>\n");
4706 EDIT_EM_ReplaceSel(hwnd, es, FALSE, empty_stringW, FALSE);
4708 es->x_offset = 0;
4709 es->flags &= ~EF_MODIFIED;
4710 EDIT_EM_SetSel(hwnd, es, 0, 0, FALSE);
4711 /* Send the notification after the selection start and end have been set
4712 * edit control doesn't send notification on WM_SETTEXT
4713 * if it is multiline, or it is part of combobox
4715 if( !((es->style & ES_MULTILINE) || es->hwndListBox))
4716 EDIT_NOTIFY_PARENT(hwnd, es, EN_CHANGE, "EN_CHANGE");
4717 EDIT_EM_ScrollCaret(hwnd, es);
4721 /*********************************************************************
4723 * WM_SIZE
4726 static void EDIT_WM_Size(HWND hwnd, EDITSTATE *es, UINT action, INT width, INT height)
4728 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4729 RECT rc;
4730 TRACE("width = %d, height = %d\n", width, height);
4731 SetRect(&rc, 0, 0, width, height);
4732 EDIT_SetRectNP(hwnd, es, &rc);
4733 EDIT_UpdateText(hwnd, es, NULL, TRUE);
4738 /*********************************************************************
4740 * WM_STYLECHANGED
4742 * This message is sent by SetWindowLong on having changed either the Style
4743 * or the extended style.
4745 * We ensure that the window's version of the styles and the EDITSTATE's agree.
4747 * See also EDIT_WM_NCCreate
4749 * It appears that the Windows version of the edit control allows the style
4750 * (as retrieved by GetWindowLong) to be any value and maintains an internal
4751 * style variable which will generally be different. In this function we
4752 * update the internal style based on what changed in the externally visible
4753 * style.
4755 * Much of this content as based upon the MSDN, especially:
4756 * Platform SDK Documentation -> User Interface Services ->
4757 * Windows User Interface -> Edit Controls -> Edit Control Reference ->
4758 * Edit Control Styles
4760 static LRESULT EDIT_WM_StyleChanged (HWND hwnd,
4761 EDITSTATE *es,
4762 WPARAM which,
4763 const STYLESTRUCT *style)
4765 if (GWL_STYLE == which) {
4766 DWORD style_change_mask;
4767 DWORD new_style;
4768 /* Only a subset of changes can be applied after the control
4769 * has been created.
4771 style_change_mask = ES_UPPERCASE | ES_LOWERCASE |
4772 ES_NUMBER;
4773 if (es->style & ES_MULTILINE)
4774 style_change_mask |= ES_WANTRETURN;
4776 new_style = style->styleNew & style_change_mask;
4778 /* Number overrides lowercase overrides uppercase (at least it
4779 * does in Win95). However I'll bet that ES_NUMBER would be
4780 * invalid under Win 3.1.
4782 if (new_style & ES_NUMBER) {
4783 ; /* do not override the ES_NUMBER */
4784 } else if (new_style & ES_LOWERCASE) {
4785 new_style &= ~ES_UPPERCASE;
4788 es->style = (es->style & ~style_change_mask) | new_style;
4789 } else if (GWL_EXSTYLE == which) {
4790 ; /* FIXME - what is needed here */
4791 } else {
4792 WARN ("Invalid style change %d\n",which);
4795 return 0;
4798 /*********************************************************************
4800 * WM_SYSKEYDOWN
4803 static LRESULT EDIT_WM_SysKeyDown(HWND hwnd, EDITSTATE *es, INT key, DWORD key_data)
4805 if ((key == VK_BACK) && (key_data & 0x2000)) {
4806 if (EDIT_EM_CanUndo(es))
4807 EDIT_EM_Undo(hwnd, es);
4808 return 0;
4809 } else if (key == VK_UP || key == VK_DOWN) {
4810 if (EDIT_CheckCombo(hwnd, es, WM_SYSKEYDOWN, key))
4811 return 0;
4813 return DefWindowProcW(hwnd, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4817 /*********************************************************************
4819 * WM_TIMER
4822 static void EDIT_WM_Timer(HWND hwnd, EDITSTATE *es)
4824 if (es->region_posx < 0) {
4825 EDIT_MoveBackward(hwnd, es, TRUE);
4826 } else if (es->region_posx > 0) {
4827 EDIT_MoveForward(hwnd, es, TRUE);
4830 * FIXME: gotta do some vertical scrolling here, like
4831 * EDIT_EM_LineScroll(hwnd, 0, 1);
4835 /*********************************************************************
4837 * WM_VSCROLL
4840 static LRESULT EDIT_WM_VScroll(HWND hwnd, EDITSTATE *es, INT action, INT pos)
4842 INT dy;
4844 if (!(es->style & ES_MULTILINE))
4845 return 0;
4847 if (!(es->style & ES_AUTOVSCROLL))
4848 return 0;
4850 dy = 0;
4851 switch (action) {
4852 case SB_LINEUP:
4853 case SB_LINEDOWN:
4854 case SB_PAGEUP:
4855 case SB_PAGEDOWN:
4856 TRACE("action %d\n", action);
4857 EDIT_EM_Scroll(hwnd, es, action);
4858 return 0;
4859 case SB_TOP:
4860 TRACE("SB_TOP\n");
4861 dy = -es->y_offset;
4862 break;
4863 case SB_BOTTOM:
4864 TRACE("SB_BOTTOM\n");
4865 dy = es->line_count - 1 - es->y_offset;
4866 break;
4867 case SB_THUMBTRACK:
4868 TRACE("SB_THUMBTRACK %d\n", pos);
4869 es->flags |= EF_VSCROLL_TRACK;
4870 if(es->style & WS_VSCROLL)
4871 dy = pos - es->y_offset;
4872 else
4874 /* Assume default scroll range 0-100 */
4875 INT vlc, new_y;
4876 /* Sanity check */
4877 if(pos < 0 || pos > 100) return 0;
4878 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4879 new_y = pos * (es->line_count - vlc) / 100;
4880 dy = es->line_count ? (new_y - es->y_offset) : 0;
4881 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4882 es->line_count, es->y_offset, pos, dy);
4884 break;
4885 case SB_THUMBPOSITION:
4886 TRACE("SB_THUMBPOSITION %d\n", pos);
4887 es->flags &= ~EF_VSCROLL_TRACK;
4888 if(es->style & WS_VSCROLL)
4889 dy = pos - es->y_offset;
4890 else
4892 /* Assume default scroll range 0-100 */
4893 INT vlc, new_y;
4894 /* Sanity check */
4895 if(pos < 0 || pos > 100) return 0;
4896 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4897 new_y = pos * (es->line_count - vlc) / 100;
4898 dy = es->line_count ? (new_y - es->y_offset) : 0;
4899 TRACE("line_count=%d, y_offset=%d, pos=%d, dy = %d\n",
4900 es->line_count, es->y_offset, pos, dy);
4902 if (!dy)
4904 /* force scroll info update */
4905 EDIT_UpdateScrollInfo(hwnd, es);
4906 EDIT_NOTIFY_PARENT(hwnd, es, EN_VSCROLL, "EN_VSCROLL");
4908 break;
4909 case SB_ENDSCROLL:
4910 TRACE("SB_ENDSCROLL\n");
4911 break;
4913 * FIXME : the next two are undocumented !
4914 * Are we doing the right thing ?
4915 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4916 * although it's also a regular control message.
4918 case EM_GETTHUMB: /* this one is used by NT notepad */
4919 case EM_GETTHUMB16:
4921 LRESULT ret;
4922 if(GetWindowLongA( hwnd, GWL_STYLE ) & WS_VSCROLL)
4923 ret = GetScrollPos(hwnd, SB_VERT);
4924 else
4926 /* Assume default scroll range 0-100 */
4927 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
4928 ret = es->line_count ? es->y_offset * 100 / (es->line_count - vlc) : 0;
4930 TRACE("EM_GETTHUMB: returning %ld\n", ret);
4931 return ret;
4933 case EM_LINESCROLL16:
4934 TRACE("EM_LINESCROLL16 %d\n", pos);
4935 dy = pos;
4936 break;
4938 default:
4939 ERR("undocumented WM_VSCROLL action %d (0x%04x), please report\n",
4940 action, action);
4941 return 0;
4943 if (dy)
4944 EDIT_EM_LineScroll(hwnd, es, 0, dy);
4945 return 0;
4948 /*********************************************************************
4950 * EDIT_UpdateText
4953 static void EDIT_UpdateTextRegion(HWND hwnd, EDITSTATE *es, HRGN hrgn, BOOL bErase)
4955 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4956 InvalidateRgn(hwnd, hrgn, bErase);
4960 /*********************************************************************
4962 * EDIT_UpdateText
4965 static void EDIT_UpdateText(HWND hwnd, EDITSTATE *es, LPRECT rc, BOOL bErase)
4967 if (es->flags & EF_UPDATE) EDIT_NOTIFY_PARENT(hwnd, es, EN_UPDATE, "EN_UPDATE");
4968 InvalidateRect(hwnd, rc, bErase);