Send WM_CTLCOLOREDIT not WM_CTLCOLORSTATIC messages to parent of a
[wine.git] / controls / edit.c
blob3f759dd73ebb020dc48657d7bcb87d5048d01523
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
8 */
11 * please read EDIT.TODO (and update it when you change things)
14 #include "config.h"
16 #include <string.h>
17 #include "winnt.h"
18 #include "win.h"
19 #include "wine/winbase16.h"
20 #include "combo.h"
21 #include "local.h"
22 #include "resource.h"
23 #include "debugtools.h"
24 #include "callback.h"
25 #include "tweak.h"
26 #include "winversion.h"
28 DECLARE_DEBUG_CHANNEL(combo)
29 DECLARE_DEBUG_CHANNEL(edit)
30 DECLARE_DEBUG_CHANNEL(relay)
32 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
33 FIXME: BTW, new specs say 65535 (do you dare ???) */
34 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
35 #define BUFSTART_MULTI 1024 /* starting size */
36 #define BUFSTART_SINGLE 256 /* starting size */
37 #define GROWLENGTH 64 /* buffers grow by this much */
38 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
41 * extra flags for EDITSTATE.flags field
43 #define EF_MODIFIED 0x0001 /* text has been modified */
44 #define EF_FOCUSED 0x0002 /* we have input focus */
45 #define EF_UPDATE 0x0004 /* notify parent of changed state on next WM_PAINT */
46 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
47 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
48 #define EF_VSCROLL_HACK 0x0020 /* we already have informed the user of the hacked handler */
49 #define EF_HSCROLL_HACK 0x0040 /* we already have informed the user of the hacked handler */
50 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
51 wrapped line, instead of in front of the next character */
52 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
54 typedef enum
56 END_0 = 0, /* line ends with terminating '\0' character */
57 END_WRAP, /* line is wrapped */
58 END_HARD, /* line ends with a hard return '\r\n' */
59 END_SOFT /* line ends with a soft return '\r\r\n' */
60 } LINE_END;
62 typedef struct tagLINEDEF {
63 INT length; /* bruto length of a line in bytes */
64 INT net_length; /* netto length of a line in visible characters */
65 LINE_END ending;
66 INT width; /* width of the line in pixels */
67 struct tagLINEDEF *next;
68 } LINEDEF;
70 typedef struct
72 HANDLE heap; /* our own heap */
73 LPSTR text; /* the actual contents of the control */
74 INT buffer_size; /* the size of the buffer */
75 INT buffer_limit; /* the maximum size to which the buffer may grow */
76 HFONT font; /* NULL means standard system font */
77 INT x_offset; /* scroll offset for multi lines this is in pixels
78 for single lines it's in characters */
79 INT line_height; /* height of a screen line in pixels */
80 INT char_width; /* average character width in pixels */
81 DWORD style; /* sane version of wnd->dwStyle */
82 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
83 INT undo_insert_count; /* number of characters inserted in sequence */
84 INT undo_position; /* character index of the insertion and deletion */
85 LPSTR undo_text; /* deleted text */
86 INT undo_buffer_size; /* size of the deleted text buffer */
87 INT selection_start; /* == selection_end if no selection */
88 INT selection_end; /* == current caret position */
89 CHAR password_char; /* == 0 if no password char, and for multi line controls */
90 INT left_margin; /* in pixels */
91 INT right_margin; /* in pixels */
92 RECT format_rect;
93 INT region_posx; /* Position of cursor relative to region: */
94 INT region_posy; /* -1: to left, 0: within, 1: to right */
95 EDITWORDBREAKPROC16 word_break_proc16;
96 EDITWORDBREAKPROCA word_break_proc32A;
97 INT line_count; /* number of lines */
98 INT y_offset; /* scroll offset in number of lines */
99 BOOL bCaptureState; /* flag indicating whether mouse was captured */
100 BOOL bEnableState; /* flag keeping the enable state */
102 * only for multi line controls
104 INT lock_count; /* amount of re-entries in the EditWndProc */
105 INT tabs_count;
106 LPINT tabs;
107 INT text_width; /* width of the widest line in pixels */
108 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
109 HLOCAL16 hloc16; /* for controls receiving EM_GETHANDLE16 */
110 HLOCAL hloc32; /* for controls receiving EM_GETHANDLE */
111 } EDITSTATE;
114 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
115 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
117 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
118 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
120 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
121 do {TRACE_(edit)("notification " str " sent to hwnd=%08x\n", \
122 (UINT)(hwnd));} while(0)
124 /* used for disabled or read-only edit control */
125 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
126 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
127 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
128 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
129 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
130 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
131 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
132 do {DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str); \
133 SendMessageA((wnd)->parent->hwndSelf, WM_COMMAND, \
134 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
135 (LPARAM)(wnd)->hwndSelf);} while(0)
136 #define DPRINTF_EDIT_MSG16(str) \
137 TRACE_(edit)(\
138 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
139 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
140 #define DPRINTF_EDIT_MSG32(str) \
141 TRACE_(edit)(\
142 "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
143 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
145 /*********************************************************************
147 * Declarations
152 * These functions have trivial implementations
153 * We still like to call them internally
154 * "static inline" makes them more like macro's
156 static inline BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
157 static inline void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
158 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
159 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
162 * Helper functions only valid for one type of control
164 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
165 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
166 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
167 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
168 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
169 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
171 * Helper functions valid for both single line _and_ multi line controls
173 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action);
174 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
175 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y);
176 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
177 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
178 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
179 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size);
180 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size);
181 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
182 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
183 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
184 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
185 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
186 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
187 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
188 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
189 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
190 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
191 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
192 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action);
194 * EM_XXX message handlers
196 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
197 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol);
198 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
199 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
200 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch);
201 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end);
202 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
203 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index);
204 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line);
205 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index);
206 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
207 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
208 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace);
209 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
210 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
211 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
212 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
213 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit);
214 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action, INT left, INT right);
215 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
216 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
217 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs);
218 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs);
219 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp);
220 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
221 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
223 * WM_XXX message handlers
225 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
226 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
227 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y);
228 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
229 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs);
230 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
231 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
232 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text);
233 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
234 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
235 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus);
236 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
237 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
238 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
239 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
240 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs);
241 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam);
242 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
243 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus);
244 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
245 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
246 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
247 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
248 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc);
249 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
252 /*********************************************************************
254 * EM_CANUNDO
257 static inline BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
259 return (es->undo_insert_count || lstrlenA(es->undo_text));
263 /*********************************************************************
265 * EM_EMPTYUNDOBUFFER
268 static inline void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
270 es->undo_insert_count = 0;
271 *es->undo_text = '\0';
275 /*********************************************************************
277 * WM_CLEAR
280 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
282 EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
286 /*********************************************************************
288 * WM_CUT
291 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
293 EDIT_WM_Copy(wnd, es);
294 EDIT_WM_Clear(wnd, es);
298 /*********************************************************************
300 * EditWndProc()
302 * The messages are in the order of the actual integer values
303 * (which can be found in include/windows.h)
304 * Whereever possible the 16 bit versions are converted to
305 * the 32 bit ones, so that we can 'fall through' to the
306 * helper functions. These are mostly 32 bit (with a few
307 * exceptions, clearly indicated by a '16' extension to their
308 * names).
311 LRESULT WINAPI EditWndProc( HWND hwnd, UINT msg,
312 WPARAM wParam, LPARAM lParam )
314 WND *wnd = WIN_FindWndPtr(hwnd);
315 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
316 LRESULT result = 0;
318 switch (msg) {
319 case WM_DESTROY:
320 DPRINTF_EDIT_MSG32("WM_DESTROY");
321 EDIT_WM_Destroy(wnd, es);
322 result = 0;
323 goto END;
325 case WM_NCCREATE:
326 DPRINTF_EDIT_MSG32("WM_NCCREATE");
327 result = EDIT_WM_NCCreate(wnd, (LPCREATESTRUCTA)lParam);
328 goto END;
331 if (!es)
333 result = DefWindowProcA(hwnd, msg, wParam, lParam);
334 goto END;
338 EDIT_LockBuffer(wnd, es);
339 switch (msg) {
340 case EM_GETSEL16:
341 DPRINTF_EDIT_MSG16("EM_GETSEL");
342 wParam = 0;
343 lParam = 0;
344 /* fall through */
345 case EM_GETSEL:
346 DPRINTF_EDIT_MSG32("EM_GETSEL");
347 result = EDIT_EM_GetSel(wnd, es, (LPUINT)wParam, (LPUINT)lParam);
348 break;
350 case EM_SETSEL16:
351 DPRINTF_EDIT_MSG16("EM_SETSEL");
352 if (SLOWORD(lParam) == -1)
353 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
354 else
355 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
356 if (!wParam)
357 EDIT_EM_ScrollCaret(wnd, es);
358 result = 1;
359 break;
360 case EM_SETSEL:
361 DPRINTF_EDIT_MSG32("EM_SETSEL");
362 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
363 EDIT_EM_ScrollCaret(wnd, es);
364 result = 1;
365 break;
367 case EM_GETRECT16:
368 DPRINTF_EDIT_MSG16("EM_GETRECT");
369 if (lParam)
370 CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
371 break;
372 case EM_GETRECT:
373 DPRINTF_EDIT_MSG32("EM_GETRECT");
374 if (lParam)
375 CopyRect((LPRECT)lParam, &es->format_rect);
376 break;
378 case EM_SETRECT16:
379 DPRINTF_EDIT_MSG16("EM_SETRECT");
380 if ((es->style & ES_MULTILINE) && lParam) {
381 RECT rc;
382 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
383 EDIT_SetRectNP(wnd, es, &rc);
384 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
386 break;
387 case EM_SETRECT:
388 DPRINTF_EDIT_MSG32("EM_SETRECT");
389 if ((es->style & ES_MULTILINE) && lParam) {
390 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
391 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
393 break;
395 case EM_SETRECTNP16:
396 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
397 if ((es->style & ES_MULTILINE) && lParam) {
398 RECT rc;
399 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
400 EDIT_SetRectNP(wnd, es, &rc);
402 break;
403 case EM_SETRECTNP:
404 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
405 if ((es->style & ES_MULTILINE) && lParam)
406 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
407 break;
409 case EM_SCROLL16:
410 DPRINTF_EDIT_MSG16("EM_SCROLL");
411 /* fall through */
412 case EM_SCROLL:
413 DPRINTF_EDIT_MSG32("EM_SCROLL");
414 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
415 break;
417 case EM_LINESCROLL16:
418 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
419 wParam = (WPARAM)(INT)SHIWORD(lParam);
420 lParam = (LPARAM)(INT)SLOWORD(lParam);
421 /* fall through */
422 case EM_LINESCROLL:
423 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
424 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
425 break;
427 case EM_SCROLLCARET16:
428 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
429 /* fall through */
430 case EM_SCROLLCARET:
431 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
432 EDIT_EM_ScrollCaret(wnd, es);
433 result = 1;
434 break;
436 case EM_GETMODIFY16:
437 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
438 /* fall through */
439 case EM_GETMODIFY:
440 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
441 result = ((es->flags & EF_MODIFIED) != 0);
442 break;
444 case EM_SETMODIFY16:
445 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
446 /* fall through */
447 case EM_SETMODIFY:
448 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
449 if (wParam)
450 es->flags |= EF_MODIFIED;
451 else
452 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
453 break;
455 case EM_GETLINECOUNT16:
456 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
457 /* fall through */
458 case EM_GETLINECOUNT:
459 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
460 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
461 break;
463 case EM_LINEINDEX16:
464 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
465 if ((INT16)wParam == -1)
466 wParam = (WPARAM)-1;
467 /* fall through */
468 case EM_LINEINDEX:
469 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
470 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT)wParam);
471 break;
473 case EM_SETHANDLE16:
474 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
475 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
476 break;
477 case EM_SETHANDLE:
478 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
479 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
480 break;
482 case EM_GETHANDLE16:
483 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
484 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
485 break;
486 case EM_GETHANDLE:
487 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
488 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
489 break;
491 case EM_GETTHUMB16:
492 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
493 /* fall through */
494 case EM_GETTHUMB:
495 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
496 result = EDIT_EM_GetThumb(wnd, es);
497 break;
499 /* messages 0x00bf and 0x00c0 missing from specs */
501 case WM_USER+15:
502 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
503 /* fall through */
504 case 0x00bf:
505 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
506 result = DefWindowProcA(hwnd, msg, wParam, lParam);
507 break;
509 case WM_USER+16:
510 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
511 /* fall through */
512 case 0x00c0:
513 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
514 result = DefWindowProcA(hwnd, msg, wParam, lParam);
515 break;
517 case EM_LINELENGTH16:
518 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
519 /* fall through */
520 case EM_LINELENGTH:
521 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
522 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT)wParam);
523 break;
525 case EM_REPLACESEL16:
526 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
527 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
528 /* fall through */
529 case EM_REPLACESEL:
530 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
531 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, (LPCSTR)lParam);
532 result = 1;
533 break;
535 /* message 0x00c3 missing from specs */
537 case WM_USER+19:
538 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
539 /* fall through */
540 case 0x00c3:
541 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
542 result = DefWindowProcA(hwnd, msg, wParam, lParam);
543 break;
545 case EM_GETLINE16:
546 DPRINTF_EDIT_MSG16("EM_GETLINE");
547 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
548 /* fall through */
549 case EM_GETLINE:
550 DPRINTF_EDIT_MSG32("EM_GETLINE");
551 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT)wParam, (LPSTR)lParam);
552 break;
554 case EM_LIMITTEXT16:
555 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
556 /* fall through */
557 case EM_SETLIMITTEXT:
558 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
559 EDIT_EM_SetLimitText(wnd, es, (INT)wParam);
560 break;
562 case EM_CANUNDO16:
563 DPRINTF_EDIT_MSG16("EM_CANUNDO");
564 /* fall through */
565 case EM_CANUNDO:
566 DPRINTF_EDIT_MSG32("EM_CANUNDO");
567 result = (LRESULT)EDIT_EM_CanUndo(wnd, es);
568 break;
570 case EM_UNDO16:
571 DPRINTF_EDIT_MSG16("EM_UNDO");
572 /* fall through */
573 case EM_UNDO:
574 /* fall through */
575 case WM_UNDO:
576 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
577 result = (LRESULT)EDIT_EM_Undo(wnd, es);
578 break;
580 case EM_FMTLINES16:
581 DPRINTF_EDIT_MSG16("EM_FMTLINES");
582 /* fall through */
583 case EM_FMTLINES:
584 DPRINTF_EDIT_MSG32("EM_FMTLINES");
585 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL)wParam);
586 break;
588 case EM_LINEFROMCHAR16:
589 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
590 /* fall through */
591 case EM_LINEFROMCHAR:
592 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
593 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT)wParam);
594 break;
596 /* message 0x00ca missing from specs */
598 case WM_USER+26:
599 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
600 /* fall through */
601 case 0x00ca:
602 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
603 result = DefWindowProcA(hwnd, msg, wParam, lParam);
604 break;
606 case EM_SETTABSTOPS16:
607 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
608 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
609 break;
610 case EM_SETTABSTOPS:
611 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
612 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT)wParam, (LPINT)lParam);
613 break;
615 case EM_SETPASSWORDCHAR16:
616 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
617 /* fall through */
618 case EM_SETPASSWORDCHAR:
619 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
620 EDIT_EM_SetPasswordChar(wnd, es, (CHAR)wParam);
621 break;
623 case EM_EMPTYUNDOBUFFER16:
624 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
625 /* fall through */
626 case EM_EMPTYUNDOBUFFER:
627 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
628 EDIT_EM_EmptyUndoBuffer(wnd, es);
629 break;
631 case EM_GETFIRSTVISIBLELINE16:
632 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
633 result = es->y_offset;
634 break;
635 case EM_GETFIRSTVISIBLELINE:
636 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
637 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
638 break;
640 case EM_SETREADONLY16:
641 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
642 /* fall through */
643 case EM_SETREADONLY:
644 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
645 if (wParam) {
646 wnd->dwStyle |= ES_READONLY;
647 es->style |= ES_READONLY;
648 } else {
649 wnd->dwStyle &= ~ES_READONLY;
650 es->style &= ~ES_READONLY;
652 result = 1;
653 break;
655 case EM_SETWORDBREAKPROC16:
656 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
657 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
658 break;
659 case EM_SETWORDBREAKPROC:
660 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
661 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROCA)lParam);
662 break;
664 case EM_GETWORDBREAKPROC16:
665 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
666 result = (LRESULT)es->word_break_proc16;
667 break;
668 case EM_GETWORDBREAKPROC:
669 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
670 result = (LRESULT)es->word_break_proc32A;
671 break;
673 case EM_GETPASSWORDCHAR16:
674 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
675 /* fall through */
676 case EM_GETPASSWORDCHAR:
677 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
678 result = es->password_char;
679 break;
681 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
683 case EM_SETMARGINS:
684 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
685 EDIT_EM_SetMargins(wnd, es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
686 break;
688 case EM_GETMARGINS:
689 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
690 result = MAKELONG(es->left_margin, es->right_margin);
691 break;
693 case EM_GETLIMITTEXT:
694 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
695 result = es->buffer_limit;
696 break;
698 case EM_POSFROMCHAR:
699 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
700 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
701 break;
703 case EM_CHARFROMPOS:
704 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
705 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
706 break;
708 case WM_GETDLGCODE:
709 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
710 result = (es->style & ES_MULTILINE) ?
711 DLGC_WANTALLKEYS | DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS :
712 DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
713 break;
715 case WM_CHAR:
716 DPRINTF_EDIT_MSG32("WM_CHAR");
717 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
718 break;
720 case WM_CLEAR:
721 DPRINTF_EDIT_MSG32("WM_CLEAR");
722 EDIT_WM_Clear(wnd, es);
723 break;
725 case WM_COMMAND:
726 DPRINTF_EDIT_MSG32("WM_COMMAND");
727 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
728 break;
730 case WM_CONTEXTMENU:
731 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
732 EDIT_WM_ContextMenu(wnd, es, (HWND)wParam, SLOWORD(lParam), SHIWORD(lParam));
733 break;
735 case WM_COPY:
736 DPRINTF_EDIT_MSG32("WM_COPY");
737 EDIT_WM_Copy(wnd, es);
738 break;
740 case WM_CREATE:
741 DPRINTF_EDIT_MSG32("WM_CREATE");
742 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCTA)lParam);
743 break;
745 case WM_CUT:
746 DPRINTF_EDIT_MSG32("WM_CUT");
747 EDIT_WM_Cut(wnd, es);
748 break;
750 case WM_ENABLE:
751 DPRINTF_EDIT_MSG32("WM_ENABLE");
752 es->bEnableState = (BOOL) wParam;
753 InvalidateRect(hwnd, NULL, TRUE);
754 break;
756 case WM_ERASEBKGND:
757 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
758 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
759 break;
761 case WM_GETFONT:
762 DPRINTF_EDIT_MSG32("WM_GETFONT");
763 result = (LRESULT)es->font;
764 break;
766 case WM_GETTEXT:
767 DPRINTF_EDIT_MSG32("WM_GETTEXT");
768 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT)wParam, (LPSTR)lParam);
769 break;
771 case WM_GETTEXTLENGTH:
772 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
773 result = lstrlenA(es->text);
774 break;
776 case WM_HSCROLL:
777 DPRINTF_EDIT_MSG32("WM_HSCROLL");
778 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)lParam);
779 break;
781 case WM_KEYDOWN:
782 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
783 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
784 break;
786 case WM_KILLFOCUS:
787 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
788 result = EDIT_WM_KillFocus(wnd, es, (HWND)wParam);
789 break;
791 case WM_LBUTTONDBLCLK:
792 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
793 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
794 break;
796 case WM_LBUTTONDOWN:
797 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
798 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
799 break;
801 case WM_LBUTTONUP:
802 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
803 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
804 break;
806 case WM_MOUSEACTIVATE:
808 * FIXME: maybe DefWindowProc() screws up, but it seems that
809 * modalless dialog boxes need this. If we don't do this, the focus
810 * will _not_ be set by DefWindowProc() for edit controls in a
811 * modalless dialog box ???
813 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
814 SetFocus(wnd->hwndSelf);
815 result = MA_ACTIVATE;
816 break;
818 case WM_MOUSEMOVE:
820 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
822 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
823 break;
825 case WM_PAINT:
826 DPRINTF_EDIT_MSG32("WM_PAINT");
827 EDIT_WM_Paint(wnd, es, wParam);
828 break;
830 case WM_PASTE:
831 DPRINTF_EDIT_MSG32("WM_PASTE");
832 EDIT_WM_Paste(wnd, es);
833 break;
835 case WM_SETFOCUS:
836 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
837 EDIT_WM_SetFocus(wnd, es, (HWND)wParam);
838 break;
840 case WM_SETFONT:
841 DPRINTF_EDIT_MSG32("WM_SETFONT");
842 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
843 break;
845 case WM_SETTEXT:
846 DPRINTF_EDIT_MSG32("WM_SETTEXT");
847 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
848 result = TRUE;
849 break;
851 case WM_SIZE:
852 DPRINTF_EDIT_MSG32("WM_SIZE");
853 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
854 break;
856 case WM_SYSKEYDOWN:
857 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
858 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
859 break;
861 case WM_TIMER:
862 DPRINTF_EDIT_MSG32("WM_TIMER");
863 EDIT_WM_Timer(wnd, es, (INT)wParam, (TIMERPROC)lParam);
864 break;
866 case WM_VSCROLL:
867 DPRINTF_EDIT_MSG32("WM_VSCROLL");
868 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)(lParam));
869 break;
871 default:
872 result = DefWindowProcA(hwnd, msg, wParam, lParam);
873 break;
875 EDIT_UnlockBuffer(wnd, es, FALSE);
876 END:
877 WIN_ReleaseWndPtr(wnd);
878 return result;
883 /*********************************************************************
885 * EDIT_BuildLineDefs_ML
887 * Build linked list of text lines.
888 * Lines can end with '\0' (last line), a character (if it is wrapped),
889 * a soft return '\r\r\n' or a hard return '\r\n'
892 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
894 HDC dc;
895 HFONT old_font = 0;
896 LPSTR start, cp;
897 INT fw;
898 LINEDEF *current_def;
899 LINEDEF **previous_next;
901 current_def = es->first_line_def;
902 do {
903 LINEDEF *next_def = current_def->next;
904 HeapFree(es->heap, 0, current_def);
905 current_def = next_def;
906 } while (current_def);
907 es->line_count = 0;
908 es->text_width = 0;
910 dc = GetDC(wnd->hwndSelf);
911 if (es->font)
912 old_font = SelectObject(dc, es->font);
914 fw = es->format_rect.right - es->format_rect.left;
915 start = es->text;
916 previous_next = &es->first_line_def;
917 do {
918 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
919 current_def->next = NULL;
920 cp = start;
921 while (*cp) {
922 if ((*cp == '\r') && (*(cp + 1) == '\n'))
923 break;
924 cp++;
926 if (!(*cp)) {
927 current_def->ending = END_0;
928 current_def->net_length = lstrlenA(start);
929 } else if ((cp > start) && (*(cp - 1) == '\r')) {
930 current_def->ending = END_SOFT;
931 current_def->net_length = cp - start - 1;
932 } else {
933 current_def->ending = END_HARD;
934 current_def->net_length = cp - start;
936 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
937 start, current_def->net_length,
938 es->tabs_count, es->tabs));
939 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
940 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
941 INT next = 0;
942 INT prev;
943 do {
944 prev = next;
945 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
946 prev + 1, current_def->net_length, WB_RIGHT);
947 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
948 start, next, es->tabs_count, es->tabs));
949 } while (current_def->width <= fw);
950 if (!prev) {
951 next = 0;
952 do {
953 prev = next;
954 next++;
955 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
956 start, next, es->tabs_count, es->tabs));
957 } while (current_def->width <= fw);
958 if (!prev)
959 prev = 1;
961 current_def->net_length = prev;
962 current_def->ending = END_WRAP;
963 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc, start,
964 current_def->net_length, es->tabs_count, es->tabs));
966 switch (current_def->ending) {
967 case END_SOFT:
968 current_def->length = current_def->net_length + 3;
969 break;
970 case END_HARD:
971 current_def->length = current_def->net_length + 2;
972 break;
973 case END_WRAP:
974 case END_0:
975 current_def->length = current_def->net_length;
976 break;
978 es->text_width = MAX(es->text_width, current_def->width);
979 start += current_def->length;
980 *previous_next = current_def;
981 previous_next = &current_def->next;
982 es->line_count++;
983 } while (current_def->ending != END_0);
984 if (es->font)
985 SelectObject(dc, old_font);
986 ReleaseDC(wnd->hwndSelf, dc);
990 /*********************************************************************
992 * EDIT_CallWordBreakProc
994 * Call appropriate WordBreakProc (internal or external).
996 * Note: The "start" argument should always be an index refering
997 * to es->text. The actual wordbreak proc might be
998 * 16 bit, so we can't always pass any 32 bit LPSTR.
999 * Hence we assume that es->text is the buffer that holds
1000 * the string under examination (we can decide this for ourselves).
1003 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action)
1005 if (es->word_break_proc16) {
1006 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
1007 SEGPTR segptr = LocalLock16(hloc16);
1008 INT ret = (INT)Callbacks->CallWordBreakProc(es->word_break_proc16,
1009 segptr + start, index, count, action);
1010 LocalUnlock16(hloc16);
1011 return ret;
1013 else if (es->word_break_proc32A)
1015 TRACE_(relay)("(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
1016 es->word_break_proc32A, es->text + start, index,
1017 count, action );
1018 return (INT)es->word_break_proc32A( es->text + start, index,
1019 count, action );
1021 else
1022 return EDIT_WordBreakProc(es->text + start, index, count, action);
1026 /*********************************************************************
1028 * EDIT_CharFromPos
1030 * Beware: This is not the function called on EM_CHARFROMPOS
1031 * The position _can_ be outside the formatting / client
1032 * rectangle
1033 * The return value is only the character index
1036 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1038 INT index;
1039 HDC dc;
1040 HFONT old_font = 0;
1042 if (es->style & ES_MULTILINE) {
1043 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1044 INT line_index = 0;
1045 LINEDEF *line_def = es->first_line_def;
1046 INT low, high;
1047 while ((line > 0) && line_def->next) {
1048 line_index += line_def->length;
1049 line_def = line_def->next;
1050 line--;
1052 x += es->x_offset - es->format_rect.left;
1053 if (x >= line_def->width) {
1054 if (after_wrap)
1055 *after_wrap = (line_def->ending == END_WRAP);
1056 return line_index + line_def->net_length;
1058 if (x <= 0) {
1059 if (after_wrap)
1060 *after_wrap = FALSE;
1061 return line_index;
1063 dc = GetDC(wnd->hwndSelf);
1064 if (es->font)
1065 old_font = SelectObject(dc, es->font);
1066 low = line_index + 1;
1067 high = line_index + line_def->net_length + 1;
1068 while (low < high - 1)
1070 INT mid = (low + high) / 2;
1071 if (LOWORD(GetTabbedTextExtentA(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1072 else low = mid;
1074 index = low;
1076 if (after_wrap)
1077 *after_wrap = ((index == line_index + line_def->net_length) &&
1078 (line_def->ending == END_WRAP));
1079 } else {
1080 LPSTR text;
1081 SIZE size;
1082 if (after_wrap)
1083 *after_wrap = FALSE;
1084 x -= es->format_rect.left;
1085 if (!x)
1086 return es->x_offset;
1087 text = EDIT_GetPasswordPointer_SL(wnd, es);
1088 dc = GetDC(wnd->hwndSelf);
1089 if (es->font)
1090 old_font = SelectObject(dc, es->font);
1091 if (x < 0)
1093 INT low = 0;
1094 INT high = es->x_offset;
1095 while (low < high - 1)
1097 INT mid = (low + high) / 2;
1098 GetTextExtentPoint32A( dc, text + mid,
1099 es->x_offset - mid, &size );
1100 if (size.cx > -x) low = mid;
1101 else high = mid;
1103 index = low;
1105 else
1107 INT low = es->x_offset;
1108 INT high = lstrlenA(es->text) + 1;
1109 while (low < high - 1)
1111 INT mid = (low + high) / 2;
1112 GetTextExtentPoint32A( dc, text + es->x_offset,
1113 mid - es->x_offset, &size );
1114 if (size.cx > x) high = mid;
1115 else low = mid;
1117 index = low;
1119 if (es->style & ES_PASSWORD)
1120 HeapFree(es->heap, 0 ,text);
1122 if (es->font)
1123 SelectObject(dc, old_font);
1124 ReleaseDC(wnd->hwndSelf, dc);
1125 return index;
1129 /*********************************************************************
1131 * EDIT_ConfinePoint
1133 * adjusts the point to be within the formatting rectangle
1134 * (so CharFromPos returns the nearest _visible_ character)
1137 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y)
1139 *x = MIN(MAX(*x, es->format_rect.left), es->format_rect.right - 1);
1140 *y = MIN(MAX(*y, es->format_rect.top), es->format_rect.bottom - 1);
1144 /*********************************************************************
1146 * EDIT_GetLineRect
1148 * Calculates the bounding rectangle for a line from a starting
1149 * column to an ending column.
1152 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1154 INT line_index = EDIT_EM_LineIndex(wnd, es, line);
1156 if (es->style & ES_MULTILINE)
1157 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1158 else
1159 rc->top = es->format_rect.top;
1160 rc->bottom = rc->top + es->line_height;
1161 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1162 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1166 /*********************************************************************
1168 * EDIT_GetPasswordPointer_SL
1170 * note: caller should free the (optionally) allocated buffer
1173 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1175 if (es->style & ES_PASSWORD) {
1176 INT len = lstrlenA(es->text);
1177 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1178 RtlFillMemory(text, len, es->password_char);
1179 text[len] = '\0';
1180 return text;
1181 } else
1182 return es->text;
1186 /*********************************************************************
1188 * EDIT_LockBuffer
1190 * This acts as a LOCAL_Lock(), but it locks only once. This way
1191 * you can call it whenever you like, without unlocking.
1194 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1196 if (!es) {
1197 ERR_(edit)("no EDITSTATE ... please report\n");
1198 return;
1200 if (!(es->style & ES_MULTILINE))
1201 return;
1202 if (!es->text) {
1203 if (es->hloc32)
1204 es->text = LocalLock(es->hloc32);
1205 else if (es->hloc16)
1206 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1207 else {
1208 ERR_(edit)("no buffer ... please report\n");
1209 return;
1212 es->lock_count++;
1216 /*********************************************************************
1218 * EDIT_SL_InvalidateText
1220 * Called from EDIT_InvalidateText().
1221 * Does the job for single-line controls only.
1224 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1226 RECT line_rect;
1227 RECT rc;
1229 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1230 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1231 InvalidateRect(wnd->hwndSelf, &rc, FALSE);
1235 /*********************************************************************
1237 * EDIT_ML_InvalidateText
1239 * Called from EDIT_InvalidateText().
1240 * Does the job for multi-line controls only.
1243 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1245 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1246 INT sl = EDIT_EM_LineFromChar(wnd, es, start);
1247 INT el = EDIT_EM_LineFromChar(wnd, es, end);
1248 INT sc;
1249 INT ec;
1250 RECT rc1;
1251 RECT rcWnd;
1252 RECT rcLine;
1253 RECT rcUpdate;
1254 INT l;
1256 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1257 return;
1259 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1260 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1261 if (sl < es->y_offset) {
1262 sl = es->y_offset;
1263 sc = 0;
1265 if (el > es->y_offset + vlc) {
1266 el = es->y_offset + vlc;
1267 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1269 GetClientRect(wnd->hwndSelf, &rc1);
1270 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1271 if (sl == el) {
1272 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1273 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1274 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1275 } else {
1276 EDIT_GetLineRect(wnd, es, sl, sc,
1277 EDIT_EM_LineLength(wnd, es,
1278 EDIT_EM_LineIndex(wnd, es, sl)),
1279 &rcLine);
1280 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1281 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1282 for (l = sl + 1 ; l < el ; l++) {
1283 EDIT_GetLineRect(wnd, es, l, 0,
1284 EDIT_EM_LineLength(wnd, es,
1285 EDIT_EM_LineIndex(wnd, es, l)),
1286 &rcLine);
1287 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1288 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1290 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1291 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1292 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1297 /*********************************************************************
1299 * EDIT_InvalidateText
1301 * Invalidate the text from offset start upto, but not including,
1302 * offset end. Useful for (re)painting the selection.
1303 * Regions outside the linewidth are not invalidated.
1304 * end == -1 means end == TextLength.
1305 * start and end need not be ordered.
1308 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1310 if (end == start)
1311 return;
1313 if (end == -1)
1314 end = lstrlenA(es->text);
1316 ORDER_INT(start, end);
1318 if (es->style & ES_MULTILINE)
1319 EDIT_ML_InvalidateText(wnd, es, start, end);
1320 else
1321 EDIT_SL_InvalidateText(wnd, es, start, end);
1325 /*********************************************************************
1327 * EDIT_MakeFit
1329 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1332 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size)
1334 HLOCAL hNew32;
1335 HLOCAL16 hNew16;
1337 if (size <= es->buffer_size)
1338 return TRUE;
1339 if (size > es->buffer_limit) {
1340 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1341 return FALSE;
1343 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1344 if (size > es->buffer_limit)
1345 size = es->buffer_limit;
1347 TRACE_(edit)("trying to ReAlloc to %d+1\n", size);
1349 EDIT_UnlockBuffer(wnd, es, TRUE);
1350 if (es->text) {
1351 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1352 es->buffer_size = MIN(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1353 else
1354 es->buffer_size = 0;
1355 } else if (es->hloc32) {
1356 if ((hNew32 = LocalReAlloc(es->hloc32, size + 1, 0))) {
1357 TRACE_(edit)("Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1358 es->hloc32 = hNew32;
1359 es->buffer_size = MIN(LocalSize(es->hloc32) - 1, es->buffer_limit);
1361 } else if (es->hloc16) {
1362 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1363 TRACE_(edit)("Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1364 es->hloc16 = hNew16;
1365 es->buffer_size = MIN(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1368 if (es->buffer_size < size) {
1369 EDIT_LockBuffer(wnd, es);
1370 WARN_(edit)("FAILED ! We now have %d+1\n", es->buffer_size);
1371 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1372 return FALSE;
1373 } else {
1374 EDIT_LockBuffer(wnd, es);
1375 TRACE_(edit)("We now have %d+1\n", es->buffer_size);
1376 return TRUE;
1381 /*********************************************************************
1383 * EDIT_MakeUndoFit
1385 * Try to fit size + 1 bytes in the undo buffer.
1388 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size)
1390 if (size <= es->undo_buffer_size)
1391 return TRUE;
1392 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1394 TRACE_(edit)("trying to ReAlloc to %d+1\n", size);
1396 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1397 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1398 if (es->undo_buffer_size < size) {
1399 WARN_(edit)("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1400 return FALSE;
1402 return TRUE;
1404 return FALSE;
1408 /*********************************************************************
1410 * EDIT_MoveBackward
1413 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1415 INT e = es->selection_end;
1417 if (e) {
1418 e--;
1419 if ((es->style & ES_MULTILINE) && e &&
1420 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1421 e--;
1422 if (e && (es->text[e - 1] == '\r'))
1423 e--;
1426 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1427 EDIT_EM_ScrollCaret(wnd, es);
1431 /*********************************************************************
1433 * EDIT_MoveDown_ML
1435 * Only for multi line controls
1436 * Move the caret one line down, on a column with the nearest
1437 * x coordinate on the screen (might be a different column).
1440 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1442 INT s = es->selection_start;
1443 INT e = es->selection_end;
1444 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1445 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1446 INT x = SLOWORD(pos);
1447 INT y = SHIWORD(pos);
1449 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1450 if (!extend)
1451 s = e;
1452 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1453 EDIT_EM_ScrollCaret(wnd, es);
1457 /*********************************************************************
1459 * EDIT_MoveEnd
1462 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1464 BOOL after_wrap = FALSE;
1465 INT e;
1467 /* Pass a high value in x to make sure of receiving the en of the line */
1468 if (es->style & ES_MULTILINE)
1469 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1470 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1471 else
1472 e = lstrlenA(es->text);
1473 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1474 EDIT_EM_ScrollCaret(wnd, es);
1478 /*********************************************************************
1480 * EDIT_MoveForward
1483 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1485 INT e = es->selection_end;
1487 if (es->text[e]) {
1488 e++;
1489 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1490 if (es->text[e] == '\n')
1491 e++;
1492 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1493 e += 2;
1496 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1497 EDIT_EM_ScrollCaret(wnd, es);
1501 /*********************************************************************
1503 * EDIT_MoveHome
1505 * Home key: move to beginning of line.
1508 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1510 INT e;
1512 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1513 if (es->style & ES_MULTILINE)
1514 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1515 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1516 else
1517 e = 0;
1518 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1519 EDIT_EM_ScrollCaret(wnd, es);
1523 /*********************************************************************
1525 * EDIT_MovePageDown_ML
1527 * Only for multi line controls
1528 * Move the caret one page down, on a column with the nearest
1529 * x coordinate on the screen (might be a different column).
1532 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1534 INT s = es->selection_start;
1535 INT e = es->selection_end;
1536 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1537 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1538 INT x = SLOWORD(pos);
1539 INT y = SHIWORD(pos);
1541 e = EDIT_CharFromPos(wnd, es, x,
1542 y + (es->format_rect.bottom - es->format_rect.top),
1543 &after_wrap);
1544 if (!extend)
1545 s = e;
1546 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1547 EDIT_EM_ScrollCaret(wnd, es);
1551 /*********************************************************************
1553 * EDIT_MovePageUp_ML
1555 * Only for multi line controls
1556 * Move the caret one page up, on a column with the nearest
1557 * x coordinate on the screen (might be a different column).
1560 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1562 INT s = es->selection_start;
1563 INT e = es->selection_end;
1564 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1565 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1566 INT x = SLOWORD(pos);
1567 INT y = SHIWORD(pos);
1569 e = EDIT_CharFromPos(wnd, es, x,
1570 y - (es->format_rect.bottom - es->format_rect.top),
1571 &after_wrap);
1572 if (!extend)
1573 s = e;
1574 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1575 EDIT_EM_ScrollCaret(wnd, es);
1579 /*********************************************************************
1581 * EDIT_MoveUp_ML
1583 * Only for multi line controls
1584 * Move the caret one line up, on a column with the nearest
1585 * x coordinate on the screen (might be a different column).
1588 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1590 INT s = es->selection_start;
1591 INT e = es->selection_end;
1592 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1593 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1594 INT x = SLOWORD(pos);
1595 INT y = SHIWORD(pos);
1597 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1598 if (!extend)
1599 s = e;
1600 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1601 EDIT_EM_ScrollCaret(wnd, es);
1605 /*********************************************************************
1607 * EDIT_MoveWordBackward
1610 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1612 INT s = es->selection_start;
1613 INT e = es->selection_end;
1614 INT l;
1615 INT ll;
1616 INT li;
1618 l = EDIT_EM_LineFromChar(wnd, es, e);
1619 ll = EDIT_EM_LineLength(wnd, es, e);
1620 li = EDIT_EM_LineIndex(wnd, es, l);
1621 if (e - li == 0) {
1622 if (l) {
1623 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1624 e = li + EDIT_EM_LineLength(wnd, es, li);
1626 } else {
1627 e = li + (INT)EDIT_CallWordBreakProc(wnd, es,
1628 li, e - li, ll, WB_LEFT);
1630 if (!extend)
1631 s = e;
1632 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1633 EDIT_EM_ScrollCaret(wnd, es);
1637 /*********************************************************************
1639 * EDIT_MoveWordForward
1642 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1644 INT s = es->selection_start;
1645 INT e = es->selection_end;
1646 INT l;
1647 INT ll;
1648 INT li;
1650 l = EDIT_EM_LineFromChar(wnd, es, e);
1651 ll = EDIT_EM_LineLength(wnd, es, e);
1652 li = EDIT_EM_LineIndex(wnd, es, l);
1653 if (e - li == ll) {
1654 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1655 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1656 } else {
1657 e = li + EDIT_CallWordBreakProc(wnd, es,
1658 li, e - li + 1, ll, WB_RIGHT);
1660 if (!extend)
1661 s = e;
1662 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1663 EDIT_EM_ScrollCaret(wnd, es);
1667 /*********************************************************************
1669 * EDIT_PaintLine
1672 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1674 INT s = es->selection_start;
1675 INT e = es->selection_end;
1676 INT li;
1677 INT ll;
1678 INT x;
1679 INT y;
1680 LRESULT pos;
1682 if (es->style & ES_MULTILINE) {
1683 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1684 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1685 return;
1686 } else if (line)
1687 return;
1689 TRACE_(edit)("line=%d\n", line);
1691 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1692 x = SLOWORD(pos);
1693 y = SHIWORD(pos);
1694 li = EDIT_EM_LineIndex(wnd, es, line);
1695 ll = EDIT_EM_LineLength(wnd, es, li);
1696 s = es->selection_start;
1697 e = es->selection_end;
1698 ORDER_INT(s, e);
1699 s = MIN(li + ll, MAX(li, s));
1700 e = MIN(li + ll, MAX(li, e));
1701 if (rev && (s != e) &&
1702 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1703 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1704 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1705 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1706 } else
1707 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1711 /*********************************************************************
1713 * EDIT_PaintText
1716 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1718 COLORREF BkColor;
1719 COLORREF TextColor;
1720 INT ret;
1721 INT li;
1722 SIZE size;
1724 if (!count)
1725 return 0;
1726 BkColor = GetBkColor(dc);
1727 TextColor = GetTextColor(dc);
1728 if (rev) {
1729 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
1730 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1732 li = EDIT_EM_LineIndex(wnd, es, line);
1733 if (es->style & ES_MULTILINE) {
1734 ret = (INT)LOWORD(TabbedTextOutA(dc, x, y, es->text + li + col, count,
1735 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1736 } else {
1737 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1738 TextOutA(dc, x, y, text + li + col, count);
1739 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1740 ret = size.cx;
1741 if (es->style & ES_PASSWORD)
1742 HeapFree(es->heap, 0, text);
1744 if (rev) {
1745 SetBkColor(dc, BkColor);
1746 SetTextColor(dc, TextColor);
1748 return ret;
1752 /*********************************************************************
1754 * EDIT_SetCaretPos
1757 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
1758 BOOL after_wrap)
1760 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1761 INT x = SLOWORD(res);
1762 INT y = SHIWORD(res);
1764 if(x < es->format_rect.left)
1765 x = es->format_rect.left;
1766 if(x > es->format_rect.right - 2)
1767 x = es->format_rect.right - 2;
1768 if(y > es->format_rect.bottom)
1769 y = es->format_rect.bottom;
1770 if(y < es->format_rect.top)
1771 y = es->format_rect.top;
1772 SetCaretPos(x, y);
1773 return;
1777 /*********************************************************************
1779 * EDIT_SetRectNP
1781 * note: this is not (exactly) the handler called on EM_SETRECTNP
1782 * it is also used to set the rect of a single line control
1785 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
1787 CopyRect(&es->format_rect, rc);
1788 if (es->style & WS_BORDER) {
1789 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
1790 if(TWEAK_WineLook == WIN31_LOOK)
1791 bw += 2;
1792 es->format_rect.left += bw;
1793 es->format_rect.top += bw;
1794 es->format_rect.right -= bw;
1795 es->format_rect.bottom -= bw;
1797 es->format_rect.left += es->left_margin;
1798 es->format_rect.right -= es->right_margin;
1799 es->format_rect.right = MAX(es->format_rect.right, es->format_rect.left + es->char_width);
1800 if (es->style & ES_MULTILINE)
1801 es->format_rect.bottom = es->format_rect.top +
1802 MAX(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1803 else
1804 es->format_rect.bottom = es->format_rect.top + es->line_height;
1805 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1806 EDIT_BuildLineDefs_ML(wnd, es);
1810 /*********************************************************************
1812 * EDIT_UnlockBuffer
1815 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
1817 if (!es) {
1818 ERR_(edit)("no EDITSTATE ... please report\n");
1819 return;
1821 if (!(es->style & ES_MULTILINE))
1822 return;
1823 if (!es->lock_count) {
1824 ERR_(edit)("lock_count == 0 ... please report\n");
1825 return;
1827 if (!es->text) {
1828 ERR_(edit)("es->text == 0 ... please report\n");
1829 return;
1831 if (force || (es->lock_count == 1)) {
1832 if (es->hloc32) {
1833 LocalUnlock(es->hloc32);
1834 es->text = NULL;
1835 } else if (es->hloc16) {
1836 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1837 es->text = NULL;
1840 es->lock_count--;
1844 /*********************************************************************
1846 * EDIT_WordBreakProc
1848 * Find the beginning of words.
1849 * Note: unlike the specs for a WordBreakProc, this function only
1850 * allows to be called without linebreaks between s[0] upto
1851 * s[count - 1]. Remember it is only called
1852 * internally, so we can decide this for ourselves.
1855 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action)
1857 INT ret = 0;
1859 TRACE_(edit)("s=%p, index=%u, count=%u, action=%d\n",
1860 s, index, count, action);
1862 switch (action) {
1863 case WB_LEFT:
1864 if (!count)
1865 break;
1866 if (index)
1867 index--;
1868 if (s[index] == ' ') {
1869 while (index && (s[index] == ' '))
1870 index--;
1871 if (index) {
1872 while (index && (s[index] != ' '))
1873 index--;
1874 if (s[index] == ' ')
1875 index++;
1877 } else {
1878 while (index && (s[index] != ' '))
1879 index--;
1880 if (s[index] == ' ')
1881 index++;
1883 ret = index;
1884 break;
1885 case WB_RIGHT:
1886 if (!count)
1887 break;
1888 if (index)
1889 index--;
1890 if (s[index] == ' ')
1891 while ((index < count) && (s[index] == ' ')) index++;
1892 else {
1893 while (s[index] && (s[index] != ' ') && (index < count))
1894 index++;
1895 while ((s[index] == ' ') && (index < count)) index++;
1897 ret = index;
1898 break;
1899 case WB_ISDELIMITER:
1900 ret = (s[index] == ' ');
1901 break;
1902 default:
1903 ERR_(edit)("unknown action code, please report !\n");
1904 break;
1906 return ret;
1910 /*********************************************************************
1912 * EM_CHARFROMPOS
1914 * returns line number (not index) in high-order word of result.
1915 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
1916 * to Richedit, not to the edit control. Original documentation is valid.
1917 * FIXME: do the specs mean to return -1 if outside client area or
1918 * if outside formatting rectangle ???
1921 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
1923 POINT pt;
1924 RECT rc;
1925 INT index;
1927 pt.x = x;
1928 pt.y = y;
1929 GetClientRect(wnd->hwndSelf, &rc);
1930 if (!PtInRect(&rc, pt))
1931 return -1;
1933 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1934 return MAKELONG(index, EDIT_EM_LineFromChar(wnd, es, index));
1938 /*********************************************************************
1940 * EM_FMTLINES
1942 * Enable or disable soft breaks.
1944 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol)
1946 es->flags &= ~EF_USE_SOFTBRK;
1947 if (add_eol) {
1948 es->flags |= EF_USE_SOFTBRK;
1949 FIXME_(edit)("soft break enabled, not implemented\n");
1951 return add_eol;
1955 /*********************************************************************
1957 * EM_GETHANDLE
1959 * Hopefully this won't fire back at us.
1960 * We always start with a fixed buffer in our own heap.
1961 * However, with this message a 32 bit application requests
1962 * a handle to 32 bit moveable local heap memory, where it expects
1963 * to find the text.
1964 * It's a pity that from this moment on we have to use this
1965 * local heap, because applications may rely on the handle
1966 * in the future.
1968 * In this function we'll try to switch to local heap.
1971 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
1973 HLOCAL newBuf;
1974 LPSTR newText;
1975 INT newSize;
1977 if (!(es->style & ES_MULTILINE))
1978 return 0;
1980 if (es->hloc32)
1981 return es->hloc32;
1982 else if (es->hloc16)
1983 return (HLOCAL)es->hloc16;
1985 if (!(newBuf = LocalAlloc(LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
1986 ERR_(edit)("could not allocate new 32 bit buffer\n");
1987 return 0;
1989 newSize = MIN(LocalSize(newBuf) - 1, es->buffer_limit);
1990 if (!(newText = LocalLock(newBuf))) {
1991 ERR_(edit)("could not lock new 32 bit buffer\n");
1992 LocalFree(newBuf);
1993 return 0;
1995 lstrcpyA(newText, es->text);
1996 EDIT_UnlockBuffer(wnd, es, TRUE);
1997 if (es->text)
1998 HeapFree(es->heap, 0, es->text);
1999 es->hloc32 = newBuf;
2000 es->hloc16 = (HLOCAL16)NULL;
2001 es->buffer_size = newSize;
2002 es->text = newText;
2003 EDIT_LockBuffer(wnd, es);
2004 TRACE_(edit)("switched to 32 bit local heap\n");
2006 return es->hloc32;
2010 /*********************************************************************
2012 * EM_GETHANDLE16
2014 * Hopefully this won't fire back at us.
2015 * We always start with a buffer in 32 bit linear memory.
2016 * However, with this message a 16 bit application requests
2017 * a handle of 16 bit local heap memory, where it expects to find
2018 * the text.
2019 * It's a pitty that from this moment on we have to use this
2020 * local heap, because applications may rely on the handle
2021 * in the future.
2023 * In this function we'll try to switch to local heap.
2025 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2027 HLOCAL16 newBuf;
2028 LPSTR newText;
2029 INT newSize;
2031 if (!(es->style & ES_MULTILINE))
2032 return 0;
2034 if (es->hloc16)
2035 return es->hloc16;
2037 if (!LOCAL_HeapSize(wnd->hInstance)) {
2038 if (!LocalInit16(wnd->hInstance, 0,
2039 GlobalSize16(wnd->hInstance))) {
2040 ERR_(edit)("could not initialize local heap\n");
2041 return 0;
2043 TRACE_(edit)("local heap initialized\n");
2045 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2046 ERR_(edit)("could not allocate new 16 bit buffer\n");
2047 return 0;
2049 newSize = MIN(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2050 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2051 ERR_(edit)("could not lock new 16 bit buffer\n");
2052 LOCAL_Free(wnd->hInstance, newBuf);
2053 return 0;
2055 lstrcpyA(newText, es->text);
2056 EDIT_UnlockBuffer(wnd, es, TRUE);
2057 if (es->text)
2058 HeapFree(es->heap, 0, es->text);
2059 else if (es->hloc32) {
2060 while (LocalFree(es->hloc32)) ;
2061 LocalFree(es->hloc32);
2063 es->hloc32 = (HLOCAL)NULL;
2064 es->hloc16 = newBuf;
2065 es->buffer_size = newSize;
2066 es->text = newText;
2067 EDIT_LockBuffer(wnd, es);
2068 TRACE_(edit)("switched to 16 bit buffer\n");
2070 return es->hloc16;
2074 /*********************************************************************
2076 * EM_GETLINE
2079 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch)
2081 LPSTR src;
2082 INT len;
2083 INT i;
2085 if (es->style & ES_MULTILINE) {
2086 if (line >= es->line_count)
2087 return 0;
2088 } else
2089 line = 0;
2090 i = EDIT_EM_LineIndex(wnd, es, line);
2091 src = es->text + i;
2092 len = MIN(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, i));
2093 for (i = 0 ; i < len ; i++) {
2094 *lpch = *src;
2095 src++;
2096 lpch++;
2098 return (LRESULT)len;
2102 /*********************************************************************
2104 * EM_GETSEL
2107 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end)
2109 UINT s = es->selection_start;
2110 UINT e = es->selection_end;
2112 ORDER_UINT(s, e);
2113 if (start)
2114 *start = s;
2115 if (end)
2116 *end = e;
2117 return MAKELONG(s, e);
2121 /*********************************************************************
2123 * EM_GETTHUMB
2125 * FIXME: is this right ? (or should it be only VSCROLL)
2126 * (and maybe only for edit controls that really have their
2127 * own scrollbars) (and maybe only for multiline controls ?)
2128 * All in all: very poorly documented
2130 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2131 * WM_VSCROLL handlers
2134 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2136 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0, 0),
2137 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0, 0));
2141 /*********************************************************************
2143 * EM_LINEFROMCHAR
2146 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index)
2148 INT line;
2149 LINEDEF *line_def;
2151 if (!(es->style & ES_MULTILINE))
2152 return 0;
2153 if (index > lstrlenA(es->text))
2154 return es->line_count - 1;
2155 if (index == -1)
2156 index = MIN(es->selection_start, es->selection_end);
2158 line = 0;
2159 line_def = es->first_line_def;
2160 index -= line_def->length;
2161 while ((index >= 0) && line_def->next) {
2162 line++;
2163 line_def = line_def->next;
2164 index -= line_def->length;
2166 return line;
2170 /*********************************************************************
2172 * EM_LINEINDEX
2175 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line)
2177 INT line_index;
2178 LINEDEF *line_def;
2180 if (!(es->style & ES_MULTILINE))
2181 return 0;
2182 if (line >= es->line_count)
2183 return -1;
2185 line_index = 0;
2186 line_def = es->first_line_def;
2187 if (line == -1) {
2188 INT index = es->selection_end - line_def->length;
2189 while ((index >= 0) && line_def->next) {
2190 line_index += line_def->length;
2191 line_def = line_def->next;
2192 index -= line_def->length;
2194 } else {
2195 while (line > 0) {
2196 line_index += line_def->length;
2197 line_def = line_def->next;
2198 line--;
2201 return line_index;
2205 /*********************************************************************
2207 * EM_LINELENGTH
2210 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index)
2212 LINEDEF *line_def;
2214 if (!(es->style & ES_MULTILINE))
2215 return lstrlenA(es->text);
2217 if (index == -1) {
2218 /* FIXME: broken
2219 INT32 sl = EDIT_EM_LineFromChar(wnd, es, es->selection_start);
2220 INT32 el = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2221 return es->selection_start - es->line_defs[sl].offset +
2222 es->line_defs[el].offset +
2223 es->line_defs[el].length - es->selection_end;
2225 return 0;
2227 line_def = es->first_line_def;
2228 index -= line_def->length;
2229 while ((index >= 0) && line_def->next) {
2230 line_def = line_def->next;
2231 index -= line_def->length;
2233 return line_def->net_length;
2237 /*********************************************************************
2239 * EM_LINESCROLL
2241 * FIXME: dx is in average character widths
2242 * However, we assume it is in pixels when we use this
2243 * function internally
2246 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2248 INT nyoff;
2250 if (!(es->style & ES_MULTILINE))
2251 return FALSE;
2253 if (-dx > es->x_offset)
2254 dx = -es->x_offset;
2255 if (dx > es->text_width - es->x_offset)
2256 dx = es->text_width - es->x_offset;
2257 nyoff = MAX(0, es->y_offset + dy);
2258 if (nyoff >= es->line_count)
2259 nyoff = es->line_count - 1;
2260 dy = (es->y_offset - nyoff) * es->line_height;
2261 if (dx || dy) {
2262 RECT rc1;
2263 RECT rc;
2264 GetClientRect(wnd->hwndSelf, &rc1);
2265 IntersectRect(&rc, &rc1, &es->format_rect);
2266 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2267 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2268 es->y_offset = nyoff;
2269 es->x_offset += dx;
2271 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2272 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2273 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2274 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2275 return TRUE;
2279 /*********************************************************************
2281 * EM_POSFROMCHAR
2284 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2286 INT len = lstrlenA(es->text);
2287 INT l;
2288 INT li;
2289 INT x;
2290 INT y = 0;
2291 HDC dc;
2292 HFONT old_font = 0;
2293 SIZE size;
2295 index = MIN(index, len);
2296 dc = GetDC(wnd->hwndSelf);
2297 if (es->font)
2298 old_font = SelectObject(dc, es->font);
2299 if (es->style & ES_MULTILINE) {
2300 l = EDIT_EM_LineFromChar(wnd, es, index);
2301 y = (l - es->y_offset) * es->line_height;
2302 li = EDIT_EM_LineIndex(wnd, es, l);
2303 if (after_wrap && (li == index) && l) {
2304 INT l2 = l - 1;
2305 LINEDEF *line_def = es->first_line_def;
2306 while (l2) {
2307 line_def = line_def->next;
2308 l2--;
2310 if (line_def->ending == END_WRAP) {
2311 l--;
2312 y -= es->line_height;
2313 li = EDIT_EM_LineIndex(wnd, es, l);
2316 x = LOWORD(GetTabbedTextExtentA(dc, es->text + li, index - li,
2317 es->tabs_count, es->tabs)) - es->x_offset;
2318 } else {
2319 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
2320 if (index < es->x_offset) {
2321 GetTextExtentPoint32A(dc, text + index,
2322 es->x_offset - index, &size);
2323 x = -size.cx;
2324 } else {
2325 GetTextExtentPoint32A(dc, text + es->x_offset,
2326 index - es->x_offset, &size);
2327 x = size.cx;
2329 y = 0;
2330 if (es->style & ES_PASSWORD)
2331 HeapFree(es->heap, 0 ,text);
2333 x += es->format_rect.left;
2334 y += es->format_rect.top;
2335 if (es->font)
2336 SelectObject(dc, old_font);
2337 ReleaseDC(wnd->hwndSelf, dc);
2338 return MAKELONG((INT16)x, (INT16)y);
2342 /*********************************************************************
2344 * EM_REPLACESEL
2346 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2349 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace)
2351 INT strl = lstrlenA(lpsz_replace);
2352 INT tl = lstrlenA(es->text);
2353 INT utl;
2354 UINT s;
2355 UINT e;
2356 INT i;
2357 LPSTR p;
2359 s = es->selection_start;
2360 e = es->selection_end;
2362 if ((s == e) && !strl)
2363 return;
2365 ORDER_UINT(s, e);
2367 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2368 return;
2370 if (e != s) {
2371 /* there is something to be deleted */
2372 if (can_undo) {
2373 utl = lstrlenA(es->undo_text);
2374 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2375 /* undo-buffer is extended to the right */
2376 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2377 lstrcpynA(es->undo_text + utl, es->text + s, e - s + 1);
2378 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2379 /* undo-buffer is extended to the left */
2380 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2381 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2382 p[e - s] = p[0];
2383 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2384 p[i] = (es->text + s)[i];
2385 es->undo_position = s;
2386 } else {
2387 /* new undo-buffer */
2388 EDIT_MakeUndoFit(wnd, es, e - s);
2389 lstrcpynA(es->undo_text, es->text + s, e - s + 1);
2390 es->undo_position = s;
2392 /* any deletion makes the old insertion-undo invalid */
2393 es->undo_insert_count = 0;
2394 } else
2395 EDIT_EM_EmptyUndoBuffer(wnd, es);
2397 /* now delete */
2398 lstrcpyA(es->text + s, es->text + e);
2400 if (strl) {
2401 /* there is an insertion */
2402 if (can_undo) {
2403 if ((s == es->undo_position) ||
2404 ((es->undo_insert_count) &&
2405 (s == es->undo_position + es->undo_insert_count)))
2407 * insertion is new and at delete position or
2408 * an extension to either left or right
2410 es->undo_insert_count += strl;
2411 else {
2412 /* new insertion undo */
2413 es->undo_position = s;
2414 es->undo_insert_count = strl;
2415 /* new insertion makes old delete-buffer invalid */
2416 *es->undo_text = '\0';
2418 } else
2419 EDIT_EM_EmptyUndoBuffer(wnd, es);
2421 /* now insert */
2422 tl = lstrlenA(es->text);
2423 for (p = es->text + tl ; p >= es->text + s ; p--)
2424 p[strl] = p[0];
2425 for (i = 0 , p = es->text + s ; i < strl ; i++)
2426 p[i] = lpsz_replace[i];
2427 if(es->style & ES_UPPERCASE)
2428 CharUpperBuffA(p, strl);
2429 else if(es->style & ES_LOWERCASE)
2430 CharLowerBuffA(p, strl);
2431 s += strl;
2433 /* FIXME: really inefficient */
2434 if (es->style & ES_MULTILINE)
2435 EDIT_BuildLineDefs_ML(wnd, es);
2437 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2438 es->flags |= EF_MODIFIED;
2439 es->flags |= EF_UPDATE;
2440 EDIT_EM_ScrollCaret(wnd, es);
2442 /* FIXME: really inefficient */
2443 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2447 /*********************************************************************
2449 * EM_SCROLL
2452 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2454 INT dy;
2456 if (!(es->style & ES_MULTILINE))
2457 return (LRESULT)FALSE;
2459 dy = 0;
2461 switch (action) {
2462 case SB_LINEUP:
2463 if (es->y_offset)
2464 dy = -1;
2465 break;
2466 case SB_LINEDOWN:
2467 if (es->y_offset < es->line_count - 1)
2468 dy = 1;
2469 break;
2470 case SB_PAGEUP:
2471 if (es->y_offset)
2472 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2473 break;
2474 case SB_PAGEDOWN:
2475 if (es->y_offset < es->line_count - 1)
2476 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2477 break;
2478 default:
2479 return (LRESULT)FALSE;
2481 if (dy) {
2482 EDIT_EM_LineScroll(wnd, es, 0, dy);
2483 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2485 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2489 /*********************************************************************
2491 * EM_SCROLLCARET
2494 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2496 if (es->style & ES_MULTILINE) {
2497 INT l;
2498 INT li;
2499 INT vlc;
2500 INT ww;
2501 INT cw = es->char_width;
2502 INT x;
2503 INT dy = 0;
2504 INT dx = 0;
2506 l = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2507 li = EDIT_EM_LineIndex(wnd, es, l);
2508 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2509 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2510 if (l >= es->y_offset + vlc)
2511 dy = l - vlc + 1 - es->y_offset;
2512 if (l < es->y_offset)
2513 dy = l - es->y_offset;
2514 ww = es->format_rect.right - es->format_rect.left;
2515 if (x < es->format_rect.left)
2516 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2517 if (x > es->format_rect.right)
2518 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2519 if (dy || dx)
2520 EDIT_EM_LineScroll(wnd, es, dx, dy);
2521 } else {
2522 INT x;
2523 INT goal;
2524 INT format_width;
2526 if (!(es->style & ES_AUTOHSCROLL))
2527 return;
2529 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2530 format_width = es->format_rect.right - es->format_rect.left;
2531 if (x < es->format_rect.left) {
2532 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2533 do {
2534 es->x_offset--;
2535 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2536 } while ((x < goal) && es->x_offset);
2537 /* FIXME: use ScrollWindow() somehow to improve performance */
2538 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2539 } else if (x > es->format_rect.right) {
2540 INT x_last;
2541 INT len = lstrlenA(es->text);
2542 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2543 do {
2544 es->x_offset++;
2545 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2546 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2547 } while ((x > goal) && (x_last > es->format_rect.right));
2548 /* FIXME: use ScrollWindow() somehow to improve performance */
2549 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2555 /*********************************************************************
2557 * EM_SETHANDLE
2559 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2562 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2564 if (!(es->style & ES_MULTILINE))
2565 return;
2567 if (!hloc) {
2568 WARN_(edit)("called with NULL handle\n");
2569 return;
2572 EDIT_UnlockBuffer(wnd, es, TRUE);
2574 * old buffer is freed by caller, unless
2575 * it is still in our own heap. (in that case
2576 * we free it, correcting the buggy caller.)
2578 if (es->text)
2579 HeapFree(es->heap, 0, es->text);
2581 es->hloc16 = (HLOCAL16)NULL;
2582 es->hloc32 = hloc;
2583 es->text = NULL;
2584 es->buffer_size = LocalSize(es->hloc32) - 1;
2585 EDIT_LockBuffer(wnd, es);
2587 es->x_offset = es->y_offset = 0;
2588 es->selection_start = es->selection_end = 0;
2589 EDIT_EM_EmptyUndoBuffer(wnd, es);
2590 es->flags &= ~EF_MODIFIED;
2591 es->flags &= ~EF_UPDATE;
2592 EDIT_BuildLineDefs_ML(wnd, es);
2593 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2594 EDIT_EM_ScrollCaret(wnd, es);
2598 /*********************************************************************
2600 * EM_SETHANDLE16
2602 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2605 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2607 if (!(es->style & ES_MULTILINE))
2608 return;
2610 if (!hloc) {
2611 WARN_(edit)("called with NULL handle\n");
2612 return;
2615 EDIT_UnlockBuffer(wnd, es, TRUE);
2617 * old buffer is freed by caller, unless
2618 * it is still in our own heap. (in that case
2619 * we free it, correcting the buggy caller.)
2621 if (es->text)
2622 HeapFree(es->heap, 0, es->text);
2624 es->hloc16 = hloc;
2625 es->hloc32 = (HLOCAL)NULL;
2626 es->text = NULL;
2627 es->buffer_size = LOCAL_Size(wnd->hInstance, es->hloc16) - 1;
2628 EDIT_LockBuffer(wnd, es);
2630 es->x_offset = es->y_offset = 0;
2631 es->selection_start = es->selection_end = 0;
2632 EDIT_EM_EmptyUndoBuffer(wnd, es);
2633 es->flags &= ~EF_MODIFIED;
2634 es->flags &= ~EF_UPDATE;
2635 EDIT_BuildLineDefs_ML(wnd, es);
2636 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2637 EDIT_EM_ScrollCaret(wnd, es);
2641 /*********************************************************************
2643 * EM_SETLIMITTEXT
2645 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
2646 * However, the windows version is not complied to yet in all of edit.c
2649 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit)
2651 if (es->style & ES_MULTILINE) {
2652 if (limit)
2653 es->buffer_limit = MIN(limit, BUFLIMIT_MULTI);
2654 else
2655 es->buffer_limit = BUFLIMIT_MULTI;
2656 } else {
2657 if (limit)
2658 es->buffer_limit = MIN(limit, BUFLIMIT_SINGLE);
2659 else
2660 es->buffer_limit = BUFLIMIT_SINGLE;
2665 /*********************************************************************
2667 * EM_SETMARGINS
2669 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2670 * action wParam despite what the docs say. EC_USEFONTINFO means one third
2671 * of the char's width, according to the new docs.
2674 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action,
2675 INT left, INT right)
2677 if (action & EC_LEFTMARGIN) {
2678 if (left != EC_USEFONTINFO)
2679 es->left_margin = left;
2680 else
2681 es->left_margin = es->char_width / 3;
2684 if (action & EC_RIGHTMARGIN) {
2685 if (right != EC_USEFONTINFO)
2686 es->right_margin = right;
2687 else
2688 es->right_margin = es->char_width / 3;
2690 TRACE_(edit)("left=%d, right=%d\n", es->left_margin, es->right_margin);
2694 /*********************************************************************
2696 * EM_SETPASSWORDCHAR
2699 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c)
2701 if (es->style & ES_MULTILINE)
2702 return;
2704 if (es->password_char == c)
2705 return;
2707 es->password_char = c;
2708 if (c) {
2709 wnd->dwStyle |= ES_PASSWORD;
2710 es->style |= ES_PASSWORD;
2711 } else {
2712 wnd->dwStyle &= ~ES_PASSWORD;
2713 es->style &= ~ES_PASSWORD;
2715 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2719 /*********************************************************************
2721 * EDIT_EM_SetSel
2723 * note: unlike the specs say: the order of start and end
2724 * _is_ preserved in Windows. (i.e. start can be > end)
2725 * In other words: this handler is OK
2728 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
2730 UINT old_start = es->selection_start;
2731 UINT old_end = es->selection_end;
2732 UINT len = lstrlenA(es->text);
2734 if (start == -1) {
2735 start = es->selection_end;
2736 end = es->selection_end;
2737 } else {
2738 start = MIN(start, len);
2739 end = MIN(end, len);
2741 es->selection_start = start;
2742 es->selection_end = end;
2743 if (after_wrap)
2744 es->flags |= EF_AFTER_WRAP;
2745 else
2746 es->flags &= ~EF_AFTER_WRAP;
2747 if (es->flags & EF_FOCUSED)
2748 EDIT_SetCaretPos(wnd, es, end, after_wrap);
2749 /* This is little bit more efficient than before, not sure if it can be improved. FIXME? */
2750 ORDER_UINT(start, end);
2751 ORDER_UINT(end, old_end);
2752 ORDER_UINT(start, old_start);
2753 ORDER_UINT(old_start, old_end);
2754 if (end != old_start)
2757 * One can also do
2758 * ORDER_UINT32(end, old_start);
2759 * EDIT_InvalidateText(wnd, es, start, end);
2760 * EDIT_InvalidateText(wnd, es, old_start, old_end);
2761 * in place of the following if statement.
2763 if (old_start > end )
2765 EDIT_InvalidateText(wnd, es, start, end);
2766 EDIT_InvalidateText(wnd, es, old_start, old_end);
2768 else
2770 EDIT_InvalidateText(wnd, es, start, old_start);
2771 EDIT_InvalidateText(wnd, es, end, old_end);
2774 else EDIT_InvalidateText(wnd, es, start, old_end);
2778 /*********************************************************************
2780 * EM_SETTABSTOPS
2783 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs)
2785 if (!(es->style & ES_MULTILINE))
2786 return FALSE;
2787 if (es->tabs)
2788 HeapFree(es->heap, 0, es->tabs);
2789 es->tabs_count = count;
2790 if (!count)
2791 es->tabs = NULL;
2792 else {
2793 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2794 memcpy(es->tabs, tabs, count * sizeof(INT));
2796 return TRUE;
2800 /*********************************************************************
2802 * EM_SETTABSTOPS16
2805 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs)
2807 if (!(es->style & ES_MULTILINE))
2808 return FALSE;
2809 if (es->tabs)
2810 HeapFree(es->heap, 0, es->tabs);
2811 es->tabs_count = count;
2812 if (!count)
2813 es->tabs = NULL;
2814 else {
2815 INT i;
2816 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2817 for (i = 0 ; i < count ; i++)
2818 es->tabs[i] = *tabs++;
2820 return TRUE;
2824 /*********************************************************************
2826 * EM_SETWORDBREAKPROC
2829 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp)
2831 if (es->word_break_proc32A == wbp)
2832 return;
2834 es->word_break_proc32A = wbp;
2835 es->word_break_proc16 = NULL;
2836 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2837 EDIT_BuildLineDefs_ML(wnd, es);
2838 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2843 /*********************************************************************
2845 * EM_SETWORDBREAKPROC16
2848 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
2850 if (es->word_break_proc16 == wbp)
2851 return;
2853 es->word_break_proc32A = NULL;
2854 es->word_break_proc16 = wbp;
2855 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2856 EDIT_BuildLineDefs_ML(wnd, es);
2857 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2862 /*********************************************************************
2864 * EM_UNDO / WM_UNDO
2867 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2869 INT ulength = lstrlenA(es->undo_text);
2870 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2872 lstrcpyA(utext, es->undo_text);
2874 TRACE_(edit)("before UNDO:insertion length = %d, deletion buffer = %s\n",
2875 es->undo_insert_count, utext);
2877 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2878 EDIT_EM_EmptyUndoBuffer(wnd, es);
2879 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext);
2880 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2881 HeapFree(es->heap, 0, utext);
2883 TRACE_(edit)("after UNDO:insertion length = %d, deletion buffer = %s\n",
2884 es->undo_insert_count, es->undo_text);
2886 return TRUE;
2890 /*********************************************************************
2892 * WM_CHAR
2895 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data)
2897 BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
2898 switch (c) {
2899 case '\r':
2900 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
2901 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2902 break;
2903 case '\n':
2904 if (es->style & ES_MULTILINE) {
2905 if (es->style & ES_READONLY) {
2906 EDIT_MoveHome(wnd, es, FALSE);
2907 EDIT_MoveDown_ML(wnd, es, FALSE);
2908 } else
2909 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\r\n");
2911 break;
2912 case '\t':
2913 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2914 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\t");
2915 break;
2916 case VK_BACK:
2917 if (!(es->style & ES_READONLY) && !control) {
2918 if (es->selection_start != es->selection_end)
2919 EDIT_WM_Clear(wnd, es);
2920 else {
2921 /* delete character left of caret */
2922 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
2923 EDIT_MoveBackward(wnd, es, TRUE);
2924 EDIT_WM_Clear(wnd, es);
2927 break;
2928 default:
2929 if (!(es->style & ES_READONLY) && ((BYTE)c >= ' ') && (c != 127)) {
2930 char str[2];
2931 str[0] = c;
2932 str[1] = '\0';
2933 EDIT_EM_ReplaceSel(wnd, es, TRUE, str);
2935 break;
2940 /*********************************************************************
2942 * WM_COMMAND
2945 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
2947 if (code || control)
2948 return;
2950 switch (id) {
2951 case EM_UNDO:
2952 EDIT_EM_Undo(wnd, es);
2953 break;
2954 case WM_CUT:
2955 EDIT_WM_Cut(wnd, es);
2956 break;
2957 case WM_COPY:
2958 EDIT_WM_Copy(wnd, es);
2959 break;
2960 case WM_PASTE:
2961 EDIT_WM_Paste(wnd, es);
2962 break;
2963 case WM_CLEAR:
2964 EDIT_WM_Clear(wnd, es);
2965 break;
2966 case EM_SETSEL:
2967 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
2968 EDIT_EM_ScrollCaret(wnd, es);
2969 break;
2970 default:
2971 ERR_(edit)("unknown menu item, please report\n");
2972 break;
2977 /*********************************************************************
2979 * WM_CONTEXTMENU
2981 * Note: the resource files resource/sysres_??.rc cannot define a
2982 * single popup menu. Hence we use a (dummy) menubar
2983 * containing the single popup menu as its first item.
2985 * FIXME: the message identifiers have been chosen arbitrarily,
2986 * hence we use MF_BYPOSITION.
2987 * We might as well use the "real" values (anybody knows ?)
2988 * The menu definition is in resources/sysres_??.rc.
2989 * Once these are OK, we better use MF_BYCOMMAND here
2990 * (as we do in EDIT_WM_Command()).
2993 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y)
2995 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
2996 HMENU popup = GetSubMenu(menu, 0);
2997 UINT start = es->selection_start;
2998 UINT end = es->selection_end;
3000 ORDER_UINT(start, end);
3002 /* undo */
3003 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
3004 /* cut */
3005 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3006 /* copy */
3007 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3008 /* paste */
3009 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
3010 /* delete */
3011 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3012 /* select all */
3013 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != lstrlenA(es->text)) ? MF_ENABLED : MF_GRAYED));
3015 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3016 DestroyMenu(menu);
3020 /*********************************************************************
3022 * WM_COPY
3025 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3027 INT s = es->selection_start;
3028 INT e = es->selection_end;
3029 HGLOBAL hdst;
3030 LPSTR dst;
3032 if (e == s)
3033 return;
3034 ORDER_INT(s, e);
3035 hdst = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3036 dst = GlobalLock(hdst);
3037 lstrcpynA(dst, es->text + s, e - s + 1);
3038 GlobalUnlock(hdst);
3039 OpenClipboard(wnd->hwndSelf);
3040 EmptyClipboard();
3041 SetClipboardData(CF_TEXT, hdst);
3042 CloseClipboard();
3046 /*********************************************************************
3048 * WM_CREATE
3051 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs)
3054 * To initialize some final structure members, we call some helper
3055 * functions. However, since the EDITSTATE is not consistent (i.e.
3056 * not fully initialized), we should be very careful which
3057 * functions can be called, and in what order.
3059 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3060 EDIT_EM_EmptyUndoBuffer(wnd, es);
3062 if (cs->lpszName && *(cs->lpszName) != '\0') {
3063 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3064 /* if we insert text to the editline, the text scrolls out
3065 * of the window, as the caret is placed after the insert
3066 * pos normally; thus we reset es->selection... to 0 and
3067 * update caret
3069 es->selection_start = es->selection_end = 0;
3070 EDIT_EM_ScrollCaret(wnd, es);
3072 return 0;
3076 /*********************************************************************
3078 * WM_DESTROY
3081 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3083 if (es->hloc32) {
3084 while (LocalUnlock(es->hloc32)) ;
3085 LocalFree(es->hloc32);
3087 if (es->hloc16) {
3088 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3089 LOCAL_Free(wnd->hInstance, es->hloc16);
3091 HeapDestroy(es->heap);
3092 HeapFree(GetProcessHeap(), 0, es);
3093 *(EDITSTATE **)wnd->wExtra = NULL;
3097 /*********************************************************************
3099 * WM_ERASEBKGND
3102 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3104 HBRUSH brush;
3105 RECT rc;
3107 if ( VERSION_AppWinVer() >= 0x40000 &&(
3108 !es->bEnableState || (es->style & ES_READONLY)))
3109 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3110 else
3111 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3113 if (!brush)
3114 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3116 GetClientRect(wnd->hwndSelf, &rc);
3117 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3118 GetClipBox(dc, &rc);
3120 * FIXME: specs say that we should UnrealizeObject() the brush,
3121 * but the specs of UnrealizeObject() say that we shouldn't
3122 * unrealize a stock object. The default brush that
3123 * DefWndProc() returns is ... a stock object.
3125 FillRect(dc, &rc, brush);
3126 return -1;
3130 /*********************************************************************
3132 * WM_GETTEXT
3135 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text)
3137 INT len = lstrlenA(es->text);
3139 if (count > len) {
3140 lstrcpyA(text, es->text);
3141 return len;
3142 } else
3143 return -1;
3147 /*********************************************************************
3149 * EDIT_HScroll_Hack
3151 * 16 bit notepad needs this. Actually it is not _our_ hack,
3152 * it is notepad's. Notepad is sending us scrollbar messages with
3153 * undocumented parameters without us even having a scrollbar ... !?!?
3156 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3158 INT dx = 0;
3159 INT fw = es->format_rect.right - es->format_rect.left;
3160 LRESULT ret = 0;
3162 if (!(es->flags & EF_HSCROLL_HACK)) {
3163 ERR_(edit)("hacked WM_HSCROLL handler invoked\n");
3164 ERR_(edit)(" if you are _not_ running 16 bit notepad, please report\n");
3165 ERR_(edit)(" (this message is only displayed once per edit control)\n");
3166 es->flags |= EF_HSCROLL_HACK;
3169 switch (action) {
3170 case SB_LINELEFT:
3171 if (es->x_offset)
3172 dx = -es->char_width;
3173 break;
3174 case SB_LINERIGHT:
3175 if (es->x_offset < es->text_width)
3176 dx = es->char_width;
3177 break;
3178 case SB_PAGELEFT:
3179 if (es->x_offset)
3180 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3181 break;
3182 case SB_PAGERIGHT:
3183 if (es->x_offset < es->text_width)
3184 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3185 break;
3186 case SB_LEFT:
3187 if (es->x_offset)
3188 dx = -es->x_offset;
3189 break;
3190 case SB_RIGHT:
3191 if (es->x_offset < es->text_width)
3192 dx = es->text_width - es->x_offset;
3193 break;
3194 case SB_THUMBTRACK:
3195 es->flags |= EF_HSCROLL_TRACK;
3196 dx = pos * es->text_width / 100 - es->x_offset;
3197 break;
3198 case SB_THUMBPOSITION:
3199 es->flags &= ~EF_HSCROLL_TRACK;
3200 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3201 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3202 break;
3203 case SB_ENDSCROLL:
3204 break;
3207 * FIXME : the next two are undocumented !
3208 * Are we doing the right thing ?
3209 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3210 * although it's also a regular control message.
3212 case EM_GETTHUMB16:
3213 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3214 break;
3215 case EM_LINESCROLL16:
3216 dx = pos;
3217 break;
3219 default:
3220 ERR_(edit)("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3221 return 0;
3223 if (dx)
3224 EDIT_EM_LineScroll(wnd, es, dx, 0);
3225 return ret;
3229 /*********************************************************************
3231 * WM_HSCROLL
3234 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3236 INT dx;
3237 INT fw;
3239 if (!(es->style & ES_MULTILINE))
3240 return 0;
3242 if (!(es->style & ES_AUTOHSCROLL))
3243 return 0;
3245 if (!(es->style & WS_HSCROLL))
3246 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3248 dx = 0;
3249 fw = es->format_rect.right - es->format_rect.left;
3250 switch (action) {
3251 case SB_LINELEFT:
3252 if (es->x_offset)
3253 dx = -es->char_width;
3254 break;
3255 case SB_LINERIGHT:
3256 if (es->x_offset < es->text_width)
3257 dx = es->char_width;
3258 break;
3259 case SB_PAGELEFT:
3260 if (es->x_offset)
3261 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3262 break;
3263 case SB_PAGERIGHT:
3264 if (es->x_offset < es->text_width)
3265 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3266 break;
3267 case SB_LEFT:
3268 if (es->x_offset)
3269 dx = -es->x_offset;
3270 break;
3271 case SB_RIGHT:
3272 if (es->x_offset < es->text_width)
3273 dx = es->text_width - es->x_offset;
3274 break;
3275 case SB_THUMBTRACK:
3276 es->flags |= EF_HSCROLL_TRACK;
3277 dx = pos - es->x_offset;
3278 break;
3279 case SB_THUMBPOSITION:
3280 es->flags &= ~EF_HSCROLL_TRACK;
3281 if (!(dx = pos - es->x_offset)) {
3282 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3283 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3285 break;
3286 case SB_ENDSCROLL:
3287 break;
3289 default:
3290 ERR_(edit)("undocumented WM_HSCROLL parameter, please report\n");
3291 return 0;
3293 if (dx)
3294 EDIT_EM_LineScroll(wnd, es, dx, 0);
3295 return 0;
3299 /*********************************************************************
3301 * EDIT_CheckCombo
3304 static BOOL EDIT_CheckCombo(WND *wnd, UINT msg, INT key, DWORD key_data)
3306 HWND hLBox;
3308 if (WIDGETS_IsControl(wnd->parent, BIC32_COMBO) &&
3309 (hLBox = COMBO_GetLBWindow(wnd->parent))) {
3310 HWND hCombo = wnd->parent->hwndSelf;
3311 BOOL bUIFlip = TRUE;
3313 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3314 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3316 switch (msg) {
3317 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3318 if (key != VK_F4) {
3319 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3320 if (SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0))
3321 bUIFlip = FALSE;
3323 if (!bUIFlip)
3324 SendMessageA(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3325 else {
3326 /* make sure ComboLBox pops up */
3327 SendMessageA(hCombo, CB_SETEXTENDEDUI, 0, 0);
3328 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3329 SendMessageA(hCombo, CB_SETEXTENDEDUI, 1, 0);
3331 break;
3332 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3333 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3334 if (bUIFlip) {
3335 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3336 SendMessageA(hCombo, CB_SHOWDROPDOWN, (bUIFlip) ? FALSE : TRUE, 0);
3337 } else
3338 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3339 break;
3341 return TRUE;
3343 return FALSE;
3347 /*********************************************************************
3349 * WM_KEYDOWN
3351 * Handling of special keys that don't produce a WM_CHAR
3352 * (i.e. non-printable keys) & Backspace & Delete
3355 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3357 BOOL shift;
3358 BOOL control;
3360 if (GetKeyState(VK_MENU) & 0x8000)
3361 return 0;
3363 shift = GetKeyState(VK_SHIFT) & 0x8000;
3364 control = GetKeyState(VK_CONTROL) & 0x8000;
3366 switch (key) {
3367 case VK_F4:
3368 case VK_UP:
3369 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3370 break;
3371 if (key == VK_F4)
3372 break;
3373 /* fall through */
3374 case VK_LEFT:
3375 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3376 EDIT_MoveUp_ML(wnd, es, shift);
3377 else
3378 if (control)
3379 EDIT_MoveWordBackward(wnd, es, shift);
3380 else
3381 EDIT_MoveBackward(wnd, es, shift);
3382 break;
3383 case VK_DOWN:
3384 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3385 break;
3386 /* fall through */
3387 case VK_RIGHT:
3388 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3389 EDIT_MoveDown_ML(wnd, es, shift);
3390 else if (control)
3391 EDIT_MoveWordForward(wnd, es, shift);
3392 else
3393 EDIT_MoveForward(wnd, es, shift);
3394 break;
3395 case VK_HOME:
3396 EDIT_MoveHome(wnd, es, shift);
3397 break;
3398 case VK_END:
3399 EDIT_MoveEnd(wnd, es, shift);
3400 break;
3401 case VK_PRIOR:
3402 if (es->style & ES_MULTILINE)
3403 EDIT_MovePageUp_ML(wnd, es, shift);
3404 break;
3405 case VK_NEXT:
3406 if (es->style & ES_MULTILINE)
3407 EDIT_MovePageDown_ML(wnd, es, shift);
3408 break;
3409 case VK_DELETE:
3410 if (!(es->style & ES_READONLY) && !(shift && control)) {
3411 if (es->selection_start != es->selection_end) {
3412 if (shift)
3413 EDIT_WM_Cut(wnd, es);
3414 else
3415 EDIT_WM_Clear(wnd, es);
3416 } else {
3417 if (shift) {
3418 /* delete character left of caret */
3419 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3420 EDIT_MoveBackward(wnd, es, TRUE);
3421 EDIT_WM_Clear(wnd, es);
3422 } else if (control) {
3423 /* delete to end of line */
3424 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3425 EDIT_MoveEnd(wnd, es, TRUE);
3426 EDIT_WM_Clear(wnd, es);
3427 } else {
3428 /* delete character right of caret */
3429 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3430 EDIT_MoveForward(wnd, es, TRUE);
3431 EDIT_WM_Clear(wnd, es);
3435 break;
3436 case VK_INSERT:
3437 if (shift) {
3438 if (!(es->style & ES_READONLY))
3439 EDIT_WM_Paste(wnd, es);
3440 } else if (control)
3441 EDIT_WM_Copy(wnd, es);
3442 break;
3443 case VK_RETURN:
3444 /* If the edit doesn't want the return send a message to the default object */
3445 if(!(es->style & ES_WANTRETURN))
3447 HWND hwndParent = GetParent(wnd->hwndSelf);
3448 DWORD dw = SendMessage16( hwndParent, DM_GETDEFID, 0, 0 );
3449 if (HIWORD(dw) == DC_HASDEFID)
3451 SendMessageA( hwndParent, WM_COMMAND,
3452 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3453 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3456 break;
3458 return 0;
3462 /*********************************************************************
3464 * WM_KILLFOCUS
3467 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus)
3469 es->flags &= ~EF_FOCUSED;
3470 DestroyCaret();
3471 if(!(es->style & ES_NOHIDESEL))
3472 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3473 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3474 return 0;
3478 /*********************************************************************
3480 * WM_LBUTTONDBLCLK
3482 * The caret position has been set on the WM_LBUTTONDOWN message
3485 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3487 INT s;
3488 INT e = es->selection_end;
3489 INT l;
3490 INT li;
3491 INT ll;
3493 if (!(es->flags & EF_FOCUSED))
3494 return 0;
3496 l = EDIT_EM_LineFromChar(wnd, es, e);
3497 li = EDIT_EM_LineIndex(wnd, es, l);
3498 ll = EDIT_EM_LineLength(wnd, es, e);
3499 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3500 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3501 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3502 EDIT_EM_ScrollCaret(wnd, es);
3503 return 0;
3507 /*********************************************************************
3509 * WM_LBUTTONDOWN
3512 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3514 INT e;
3515 BOOL after_wrap;
3517 if (!(es->flags & EF_FOCUSED))
3518 return 0;
3520 es->bCaptureState = TRUE;
3521 SetCapture(wnd->hwndSelf);
3522 EDIT_ConfinePoint(wnd, es, &x, &y);
3523 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3524 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3525 EDIT_EM_ScrollCaret(wnd, es);
3526 es->region_posx = es->region_posy = 0;
3527 SetTimer(wnd->hwndSelf, 0, 100, NULL);
3528 return 0;
3532 /*********************************************************************
3534 * WM_LBUTTONUP
3537 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3539 if (es->bCaptureState && GetCapture() == wnd->hwndSelf) {
3540 KillTimer(wnd->hwndSelf, 0);
3541 ReleaseCapture();
3543 es->bCaptureState = FALSE;
3544 return 0;
3548 /*********************************************************************
3550 * WM_MOUSEMOVE
3553 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3555 INT e;
3556 BOOL after_wrap;
3557 INT prex, prey;
3559 if (GetCapture() != wnd->hwndSelf)
3560 return 0;
3563 * FIXME: gotta do some scrolling if outside client
3564 * area. Maybe reset the timer ?
3566 prex = x; prey = y;
3567 EDIT_ConfinePoint(wnd, es, &x, &y);
3568 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3569 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3570 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3571 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3572 return 0;
3576 /*********************************************************************
3578 * WM_NCCREATE
3581 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs)
3583 EDITSTATE *es;
3585 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3586 return FALSE;
3587 *(EDITSTATE **)wnd->wExtra = es;
3590 * Note: since the EDITSTATE has not been fully initialized yet,
3591 * we can't use any API calls that may send
3592 * WM_XXX messages before WM_NCCREATE is completed.
3595 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3596 return FALSE;
3597 es->style = cs->style;
3599 es->bEnableState = !(cs->style & WS_DISABLED);
3602 * In Win95 look and feel, the WS_BORDER style is replaced by the
3603 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
3604 * control a non client area.
3606 if (TWEAK_WineLook != WIN31_LOOK)
3608 if (es->style & WS_BORDER)
3610 es->style &= ~WS_BORDER;
3611 wnd->dwStyle &= ~WS_BORDER;
3612 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
3615 else
3617 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3618 wnd->dwStyle &= ~WS_BORDER;
3621 if (es->style & ES_MULTILINE) {
3622 es->buffer_size = BUFSTART_MULTI;
3623 es->buffer_limit = BUFLIMIT_MULTI;
3624 if (es->style & WS_VSCROLL)
3625 es->style |= ES_AUTOVSCROLL;
3626 if (es->style & WS_HSCROLL)
3627 es->style |= ES_AUTOHSCROLL;
3628 es->style &= ~ES_PASSWORD;
3629 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3630 if (es->style & ES_RIGHT)
3631 es->style &= ~ES_CENTER;
3632 es->style &= ~WS_HSCROLL;
3633 es->style &= ~ES_AUTOHSCROLL;
3636 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3637 es->style |= ES_AUTOVSCROLL;
3638 } else {
3639 es->buffer_size = BUFSTART_SINGLE;
3640 es->buffer_limit = BUFLIMIT_SINGLE;
3641 es->style &= ~ES_CENTER;
3642 es->style &= ~ES_RIGHT;
3643 es->style &= ~WS_HSCROLL;
3644 es->style &= ~WS_VSCROLL;
3645 es->style &= ~ES_AUTOVSCROLL;
3646 es->style &= ~ES_WANTRETURN;
3647 if (es->style & ES_UPPERCASE) {
3648 es->style &= ~ES_LOWERCASE;
3649 es->style &= ~ES_NUMBER;
3650 } else if (es->style & ES_LOWERCASE)
3651 es->style &= ~ES_NUMBER;
3652 if (es->style & ES_PASSWORD)
3653 es->password_char = '*';
3655 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3656 es->style |= ES_AUTOHSCROLL;
3658 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3659 return FALSE;
3660 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3661 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3662 return FALSE;
3663 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3664 *es->text = '\0';
3665 if (es->style & ES_MULTILINE)
3666 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3667 return FALSE;
3668 es->line_count = 1;
3670 return TRUE;
3673 /*********************************************************************
3675 * WM_PAINT
3678 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
3680 PAINTSTRUCT ps;
3681 INT i;
3682 HDC dc;
3683 HFONT old_font = 0;
3684 RECT rc;
3685 RECT rcLine;
3686 RECT rcRgn;
3687 BOOL rev = es->bEnableState &&
3688 ((es->flags & EF_FOCUSED) ||
3689 (es->style & ES_NOHIDESEL));
3690 if (es->flags & EF_UPDATE)
3691 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
3693 if (!wParam)
3694 dc = BeginPaint(wnd->hwndSelf, &ps);
3695 else
3696 dc = (HDC) wParam;
3697 if(es->style & WS_BORDER) {
3698 GetClientRect(wnd->hwndSelf, &rc);
3699 if(es->style & ES_MULTILINE) {
3700 if(es->style & WS_HSCROLL) rc.bottom++;
3701 if(es->style & WS_VSCROLL) rc.right++;
3703 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
3705 IntersectClipRect(dc, es->format_rect.left,
3706 es->format_rect.top,
3707 es->format_rect.right,
3708 es->format_rect.bottom);
3709 if (es->style & ES_MULTILINE) {
3710 GetClientRect(wnd->hwndSelf, &rc);
3711 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3713 if (es->font)
3714 old_font = SelectObject(dc, es->font);
3715 if ( VERSION_AppWinVer() >= 0x40000 &&(
3716 !es->bEnableState || (es->style & ES_READONLY)))
3717 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3718 else
3719 EDIT_SEND_CTLCOLOR(wnd, dc);
3721 if (!es->bEnableState)
3722 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3723 GetClipBox(dc, &rcRgn);
3724 if (es->style & ES_MULTILINE) {
3725 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3726 for (i = es->y_offset ; i <= MIN(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3727 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3728 if (IntersectRect(&rc, &rcRgn, &rcLine))
3729 EDIT_PaintLine(wnd, es, dc, i, rev);
3731 } else {
3732 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3733 if (IntersectRect(&rc, &rcRgn, &rcLine))
3734 EDIT_PaintLine(wnd, es, dc, 0, rev);
3736 if (es->font)
3737 SelectObject(dc, old_font);
3738 if (es->flags & EF_FOCUSED)
3739 EDIT_SetCaretPos(wnd, es, es->selection_end,
3740 es->flags & EF_AFTER_WRAP);
3741 if (!wParam)
3742 EndPaint(wnd->hwndSelf, &ps);
3743 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3744 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3745 SCROLLINFO si;
3746 si.cbSize = sizeof(SCROLLINFO);
3747 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3748 si.nMin = 0;
3749 si.nMax = es->line_count + vlc - 2;
3750 si.nPage = vlc;
3751 si.nPos = es->y_offset;
3752 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
3754 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3755 SCROLLINFO si;
3756 INT fw = es->format_rect.right - es->format_rect.left;
3757 si.cbSize = sizeof(SCROLLINFO);
3758 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3759 si.nMin = 0;
3760 si.nMax = es->text_width + fw - 1;
3761 si.nPage = fw;
3762 si.nPos = es->x_offset;
3763 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3766 if (es->flags & EF_UPDATE) {
3767 es->flags &= ~EF_UPDATE;
3768 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3773 /*********************************************************************
3775 * WM_PASTE
3778 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3780 HGLOBAL hsrc;
3781 LPSTR src;
3783 OpenClipboard(wnd->hwndSelf);
3784 if ((hsrc = GetClipboardData(CF_TEXT))) {
3785 src = (LPSTR)GlobalLock(hsrc);
3786 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3787 GlobalUnlock(hsrc);
3789 CloseClipboard();
3793 /*********************************************************************
3795 * WM_SETFOCUS
3798 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus)
3800 es->flags |= EF_FOCUSED;
3801 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3802 EDIT_SetCaretPos(wnd, es, es->selection_end,
3803 es->flags & EF_AFTER_WRAP);
3804 if(!(es->style & ES_NOHIDESEL))
3805 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3806 ShowCaret(wnd->hwndSelf);
3807 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3811 /*********************************************************************
3813 * WM_SETFONT
3815 * With Win95 look the margins are set to default font value unless
3816 * the system font (font == 0) is being set, in which case they are left
3817 * unchanged.
3820 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
3822 TEXTMETRICA tm;
3823 HDC dc;
3824 HFONT old_font = 0;
3825 RECT r;
3827 es->font = font;
3828 dc = GetDC(wnd->hwndSelf);
3829 if (font)
3830 old_font = SelectObject(dc, font);
3831 GetTextMetricsA(dc, &tm);
3832 es->line_height = tm.tmHeight;
3833 es->char_width = tm.tmAveCharWidth;
3834 if (font)
3835 SelectObject(dc, old_font);
3836 ReleaseDC(wnd->hwndSelf, dc);
3837 if (font && (TWEAK_WineLook > WIN31_LOOK))
3838 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3839 EC_USEFONTINFO, EC_USEFONTINFO);
3841 /* Force the recalculation of the format rect for each font change */
3842 GetClientRect(wnd->hwndSelf, &r);
3843 EDIT_SetRectNP(wnd, es, &r);
3845 if (es->style & ES_MULTILINE)
3846 EDIT_BuildLineDefs_ML(wnd, es);
3848 if (redraw)
3849 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
3850 if (es->flags & EF_FOCUSED) {
3851 DestroyCaret();
3852 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3853 EDIT_SetCaretPos(wnd, es, es->selection_end,
3854 es->flags & EF_AFTER_WRAP);
3855 ShowCaret(wnd->hwndSelf);
3860 /*********************************************************************
3862 * WM_SETTEXT
3864 * NOTES
3865 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3866 * The modified flag is reset. No notifications are sent.
3868 * For single-line controls, reception of WM_SETTEXT triggers:
3869 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3872 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3874 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3875 if (text) {
3876 TRACE_(edit)("\t'%s'\n", text);
3877 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3878 } else {
3879 TRACE_(edit)("\t<NULL>\n");
3880 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3882 es->x_offset = 0;
3883 if (es->style & ES_MULTILINE) {
3884 es->flags &= ~EF_UPDATE;
3885 } else {
3886 es->flags |= EF_UPDATE;
3888 es->flags &= ~EF_MODIFIED;
3889 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3890 EDIT_EM_ScrollCaret(wnd, es);
3894 /*********************************************************************
3896 * WM_SIZE
3899 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
3901 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3902 RECT rc;
3903 SetRect(&rc, 0, 0, width, height);
3904 EDIT_SetRectNP(wnd, es, &rc);
3905 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
3910 /*********************************************************************
3912 * WM_SYSKEYDOWN
3915 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3917 if ((key == VK_BACK) && (key_data & 0x2000)) {
3918 if (EDIT_EM_CanUndo(wnd, es))
3919 EDIT_EM_Undo(wnd, es);
3920 return 0;
3921 } else if (key == VK_UP || key == VK_DOWN)
3922 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
3923 return 0;
3924 return DefWindowProcA(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
3928 /*********************************************************************
3930 * WM_TIMER
3933 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc)
3935 if (es->region_posx < 0) {
3936 EDIT_MoveBackward(wnd, es, TRUE);
3937 } else if (es->region_posx > 0) {
3938 EDIT_MoveForward(wnd, es, TRUE);
3941 * FIXME: gotta do some vertical scrolling here, like
3942 * EDIT_EM_LineScroll(wnd, 0, 1);
3947 /*********************************************************************
3949 * EDIT_VScroll_Hack
3951 * 16 bit notepad needs this. Actually it is not _our_ hack,
3952 * it is notepad's. Notepad is sending us scrollbar messages with
3953 * undocumented parameters without us even having a scrollbar ... !?!?
3956 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3958 INT dy = 0;
3959 LRESULT ret = 0;
3961 if (!(es->flags & EF_VSCROLL_HACK)) {
3962 ERR_(edit)("hacked WM_VSCROLL handler invoked\n");
3963 ERR_(edit)(" if you are _not_ running 16 bit notepad, please report\n");
3964 ERR_(edit)(" (this message is only displayed once per edit control)\n");
3965 es->flags |= EF_VSCROLL_HACK;
3968 switch (action) {
3969 case SB_LINEUP:
3970 case SB_LINEDOWN:
3971 case SB_PAGEUP:
3972 case SB_PAGEDOWN:
3973 EDIT_EM_Scroll(wnd, es, action);
3974 return 0;
3975 case SB_TOP:
3976 dy = -es->y_offset;
3977 break;
3978 case SB_BOTTOM:
3979 dy = es->line_count - 1 - es->y_offset;
3980 break;
3981 case SB_THUMBTRACK:
3982 es->flags |= EF_VSCROLL_TRACK;
3983 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
3984 break;
3985 case SB_THUMBPOSITION:
3986 es->flags &= ~EF_VSCROLL_TRACK;
3987 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
3988 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3989 break;
3990 case SB_ENDSCROLL:
3991 break;
3994 * FIXME : the next two are undocumented !
3995 * Are we doing the right thing ?
3996 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3997 * although it's also a regular control message.
3999 case EM_GETTHUMB16:
4000 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4001 break;
4002 case EM_LINESCROLL16:
4003 dy = pos;
4004 break;
4006 default:
4007 ERR_(edit)("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4008 return 0;
4010 if (dy)
4011 EDIT_EM_LineScroll(wnd, es, 0, dy);
4012 return ret;
4016 /*********************************************************************
4018 * WM_VSCROLL
4021 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
4023 INT dy;
4025 if (!(es->style & ES_MULTILINE))
4026 return 0;
4028 if (!(es->style & ES_AUTOVSCROLL))
4029 return 0;
4031 if (!(es->style & WS_VSCROLL))
4032 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
4034 dy = 0;
4035 switch (action) {
4036 case SB_LINEUP:
4037 case SB_LINEDOWN:
4038 case SB_PAGEUP:
4039 case SB_PAGEDOWN:
4040 EDIT_EM_Scroll(wnd, es, action);
4041 return 0;
4043 case SB_TOP:
4044 dy = -es->y_offset;
4045 break;
4046 case SB_BOTTOM:
4047 dy = es->line_count - 1 - es->y_offset;
4048 break;
4049 case SB_THUMBTRACK:
4050 es->flags |= EF_VSCROLL_TRACK;
4051 dy = pos - es->y_offset;
4052 break;
4053 case SB_THUMBPOSITION:
4054 es->flags &= ~EF_VSCROLL_TRACK;
4055 if (!(dy = pos - es->y_offset)) {
4056 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4057 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4059 break;
4060 case SB_ENDSCROLL:
4061 break;
4063 default:
4064 ERR_(edit)("undocumented WM_VSCROLL action %d, please report\n",
4065 action);
4066 return 0;
4068 if (dy)
4069 EDIT_EM_LineScroll(wnd, es, 0, dy);
4070 return 0;