source: trunk/src/user32/edit.c@ 9758

Last change on this file since 9758 was 9758, checked in by sandervl, 23 years ago

DT: Edit control: fixed heap corruption in undo buffer

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