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