Merged display.dll into USER.
[wine.git] / controls / edit.c
blobde3b35bea0a2fa18a6f8a2572039726226dedce0
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>
18 #include "winbase.h"
19 #include "winnt.h"
20 #include "win.h"
21 #include "wine/winbase16.h"
22 #include "combo.h"
23 #include "local.h"
24 #include "selectors.h"
25 #include "debugtools.h"
26 #include "callback.h"
27 #include "tweak.h"
28 #include "winversion.h"
30 DEFAULT_DEBUG_CHANNEL(edit)
31 DECLARE_DEBUG_CHANNEL(combo)
32 DECLARE_DEBUG_CHANNEL(relay)
34 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
35 FIXME: BTW, new specs say 65535 (do you dare ???) */
36 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
37 #define BUFSTART_MULTI 1024 /* starting size */
38 #define BUFSTART_SINGLE 256 /* starting size */
39 #define GROWLENGTH 64 /* buffers grow by this much */
40 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
43 * extra flags for EDITSTATE.flags field
45 #define EF_MODIFIED 0x0001 /* text has been modified */
46 #define EF_FOCUSED 0x0002 /* we have input focus */
47 #define EF_UPDATE 0x0004 /* notify parent of changed state on next WM_PAINT */
48 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
49 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
50 #define EF_VSCROLL_HACK 0x0020 /* we already have informed the user of the hacked handler */
51 #define EF_HSCROLL_HACK 0x0040 /* we already have informed the user of the hacked handler */
52 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
53 wrapped line, instead of in front of the next character */
54 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
56 typedef enum
58 END_0 = 0, /* line ends with terminating '\0' character */
59 END_WRAP, /* line is wrapped */
60 END_HARD, /* line ends with a hard return '\r\n' */
61 END_SOFT /* line ends with a soft return '\r\r\n' */
62 } LINE_END;
64 typedef struct tagLINEDEF {
65 INT length; /* bruto length of a line in bytes */
66 INT net_length; /* netto length of a line in visible characters */
67 LINE_END ending;
68 INT width; /* width of the line in pixels */
69 struct tagLINEDEF *next;
70 } LINEDEF;
72 typedef struct
74 HANDLE heap; /* our own heap */
75 LPSTR text; /* the actual contents of the control */
76 INT buffer_size; /* the size of the buffer */
77 INT buffer_limit; /* the maximum size to which the buffer may grow */
78 HFONT font; /* NULL means standard system font */
79 INT x_offset; /* scroll offset for multi lines this is in pixels
80 for single lines it's in characters */
81 INT line_height; /* height of a screen line in pixels */
82 INT char_width; /* average character width in pixels */
83 DWORD style; /* sane version of wnd->dwStyle */
84 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
85 INT undo_insert_count; /* number of characters inserted in sequence */
86 INT undo_position; /* character index of the insertion and deletion */
87 LPSTR undo_text; /* deleted text */
88 INT undo_buffer_size; /* size of the deleted text buffer */
89 INT selection_start; /* == selection_end if no selection */
90 INT selection_end; /* == current caret position */
91 CHAR password_char; /* == 0 if no password char, and for multi line controls */
92 INT left_margin; /* in pixels */
93 INT right_margin; /* in pixels */
94 RECT format_rect;
95 INT region_posx; /* Position of cursor relative to region: */
96 INT region_posy; /* -1: to left, 0: within, 1: to right */
97 EDITWORDBREAKPROC16 word_break_proc16;
98 EDITWORDBREAKPROCA word_break_proc32A;
99 INT line_count; /* number of lines */
100 INT y_offset; /* scroll offset in number of lines */
101 BOOL bCaptureState; /* flag indicating whether mouse was captured */
102 BOOL bEnableState; /* flag keeping the enable state */
104 * only for multi line controls
106 INT lock_count; /* amount of re-entries in the EditWndProc */
107 INT tabs_count;
108 LPINT tabs;
109 INT text_width; /* width of the widest line in pixels */
110 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
111 HLOCAL16 hloc16; /* for controls receiving EM_GETHANDLE16 */
112 HLOCAL hloc32; /* for controls receiving EM_GETHANDLE */
113 } EDITSTATE;
116 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
117 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
119 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
120 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
122 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
123 do {TRACE("notification " str " sent to hwnd=%08x\n", \
124 (UINT)(hwnd));} while(0)
126 /* used for disabled or read-only edit control */
127 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
128 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
129 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
130 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
131 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
132 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
133 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
134 do {DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str); \
135 SendMessageA((wnd)->parent->hwndSelf, WM_COMMAND, \
136 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
137 (LPARAM)(wnd)->hwndSelf);} while(0)
138 #define DPRINTF_EDIT_MSG16(str) \
139 TRACE(\
140 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
141 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
142 #define DPRINTF_EDIT_MSG32(str) \
143 TRACE(\
144 "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
145 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
147 /*********************************************************************
149 * Declarations
154 * These functions have trivial implementations
155 * We still like to call them internally
156 * "static inline" makes them more like macro's
158 static inline BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
159 static inline void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
160 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
161 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
164 * Helper functions only valid for one type of control
166 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
167 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
168 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
169 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
170 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
171 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
173 * Helper functions valid for both single line _and_ multi line controls
175 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action);
176 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
177 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y);
178 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
179 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
180 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
181 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size);
182 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size);
183 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
184 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
185 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
186 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
187 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
188 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
189 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
190 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
191 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
192 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
193 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
194 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action);
196 * EM_XXX message handlers
198 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
199 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol);
200 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
201 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
202 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch);
203 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end);
204 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
205 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index);
206 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line);
207 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index);
208 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
209 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
210 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace);
211 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
212 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
213 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
214 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
215 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit);
216 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action, INT left, INT right);
217 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
218 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
219 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs);
220 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs);
221 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp);
222 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
223 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
225 * WM_XXX message handlers
227 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
228 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
229 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y);
230 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
231 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs);
232 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
233 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
234 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text);
235 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
236 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
237 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus);
238 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
239 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
240 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
241 static LRESULT EDIT_WM_MButtonDown(WND *wnd);
242 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
243 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs);
244 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam);
245 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
246 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus);
247 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
248 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
249 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
250 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
251 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc);
252 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
253 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase);
256 /*********************************************************************
258 * EM_CANUNDO
261 static inline BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
263 return (es->undo_insert_count || lstrlenA(es->undo_text));
267 /*********************************************************************
269 * EM_EMPTYUNDOBUFFER
272 static inline void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
274 es->undo_insert_count = 0;
275 *es->undo_text = '\0';
279 /*********************************************************************
281 * WM_CLEAR
284 static inline void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
286 EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
288 if (es->flags & EF_UPDATE) {
289 es->flags &= ~EF_UPDATE;
290 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
295 /*********************************************************************
297 * WM_CUT
300 static inline void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
302 EDIT_WM_Copy(wnd, es);
303 EDIT_WM_Clear(wnd, es);
307 /*********************************************************************
309 * EditWndProc()
311 * The messages are in the order of the actual integer values
312 * (which can be found in include/windows.h)
313 * Whereever possible the 16 bit versions are converted to
314 * the 32 bit ones, so that we can 'fall through' to the
315 * helper functions. These are mostly 32 bit (with a few
316 * exceptions, clearly indicated by a '16' extension to their
317 * names).
320 LRESULT WINAPI EditWndProc( HWND hwnd, UINT msg,
321 WPARAM wParam, LPARAM lParam )
323 WND *wnd = WIN_FindWndPtr(hwnd);
324 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
325 LRESULT result = 0;
327 switch (msg) {
328 case WM_DESTROY:
329 DPRINTF_EDIT_MSG32("WM_DESTROY");
330 EDIT_WM_Destroy(wnd, es);
331 result = 0;
332 goto END;
334 case WM_NCCREATE:
335 DPRINTF_EDIT_MSG32("WM_NCCREATE");
336 result = EDIT_WM_NCCreate(wnd, (LPCREATESTRUCTA)lParam);
337 goto END;
340 if (!es)
342 result = DefWindowProcA(hwnd, msg, wParam, lParam);
343 goto END;
347 EDIT_LockBuffer(wnd, es);
348 switch (msg) {
349 case EM_GETSEL16:
350 DPRINTF_EDIT_MSG16("EM_GETSEL");
351 wParam = 0;
352 lParam = 0;
353 /* fall through */
354 case EM_GETSEL:
355 DPRINTF_EDIT_MSG32("EM_GETSEL");
356 result = EDIT_EM_GetSel(wnd, es, (LPUINT)wParam, (LPUINT)lParam);
357 break;
359 case EM_SETSEL16:
360 DPRINTF_EDIT_MSG16("EM_SETSEL");
361 if (SLOWORD(lParam) == -1)
362 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
363 else
364 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
365 if (!wParam)
366 EDIT_EM_ScrollCaret(wnd, es);
367 result = 1;
368 break;
369 case EM_SETSEL:
370 DPRINTF_EDIT_MSG32("EM_SETSEL");
371 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
372 EDIT_EM_ScrollCaret(wnd, es);
373 result = 1;
374 break;
376 case EM_GETRECT16:
377 DPRINTF_EDIT_MSG16("EM_GETRECT");
378 if (lParam)
379 CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
380 break;
381 case EM_GETRECT:
382 DPRINTF_EDIT_MSG32("EM_GETRECT");
383 if (lParam)
384 CopyRect((LPRECT)lParam, &es->format_rect);
385 break;
387 case EM_SETRECT16:
388 DPRINTF_EDIT_MSG16("EM_SETRECT");
389 if ((es->style & ES_MULTILINE) && lParam) {
390 RECT rc;
391 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
392 EDIT_SetRectNP(wnd, es, &rc);
393 EDIT_UpdateText(wnd, NULL, TRUE);
395 break;
396 case EM_SETRECT:
397 DPRINTF_EDIT_MSG32("EM_SETRECT");
398 if ((es->style & ES_MULTILINE) && lParam) {
399 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
400 EDIT_UpdateText(wnd, NULL, TRUE);
402 break;
404 case EM_SETRECTNP16:
405 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
406 if ((es->style & ES_MULTILINE) && lParam) {
407 RECT rc;
408 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
409 EDIT_SetRectNP(wnd, es, &rc);
411 break;
412 case EM_SETRECTNP:
413 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
414 if ((es->style & ES_MULTILINE) && lParam)
415 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
416 break;
418 case EM_SCROLL16:
419 DPRINTF_EDIT_MSG16("EM_SCROLL");
420 /* fall through */
421 case EM_SCROLL:
422 DPRINTF_EDIT_MSG32("EM_SCROLL");
423 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
424 break;
426 case EM_LINESCROLL16:
427 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
428 wParam = (WPARAM)(INT)SHIWORD(lParam);
429 lParam = (LPARAM)(INT)SLOWORD(lParam);
430 /* fall through */
431 case EM_LINESCROLL:
432 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
433 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
434 break;
436 case EM_SCROLLCARET16:
437 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
438 /* fall through */
439 case EM_SCROLLCARET:
440 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
441 EDIT_EM_ScrollCaret(wnd, es);
442 result = 1;
443 break;
445 case EM_GETMODIFY16:
446 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
447 /* fall through */
448 case EM_GETMODIFY:
449 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
450 result = ((es->flags & EF_MODIFIED) != 0);
451 break;
453 case EM_SETMODIFY16:
454 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
455 /* fall through */
456 case EM_SETMODIFY:
457 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
458 if (wParam)
459 es->flags |= EF_MODIFIED;
460 else
461 es->flags &= ~(EF_MODIFIED | EF_UPDATE); /* reset pending updates */
462 break;
464 case EM_GETLINECOUNT16:
465 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
466 /* fall through */
467 case EM_GETLINECOUNT:
468 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
469 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
470 break;
472 case EM_LINEINDEX16:
473 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
474 if ((INT16)wParam == -1)
475 wParam = (WPARAM)-1;
476 /* fall through */
477 case EM_LINEINDEX:
478 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
479 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT)wParam);
480 break;
482 case EM_SETHANDLE16:
483 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
484 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
485 break;
486 case EM_SETHANDLE:
487 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
488 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
489 break;
491 case EM_GETHANDLE16:
492 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
493 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
494 break;
495 case EM_GETHANDLE:
496 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
497 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
498 break;
500 case EM_GETTHUMB16:
501 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
502 /* fall through */
503 case EM_GETTHUMB:
504 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
505 result = EDIT_EM_GetThumb(wnd, es);
506 break;
508 /* messages 0x00bf and 0x00c0 missing from specs */
510 case WM_USER+15:
511 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
512 /* fall through */
513 case 0x00bf:
514 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
515 result = DefWindowProcA(hwnd, msg, wParam, lParam);
516 break;
518 case WM_USER+16:
519 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
520 /* fall through */
521 case 0x00c0:
522 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
523 result = DefWindowProcA(hwnd, msg, wParam, lParam);
524 break;
526 case EM_LINELENGTH16:
527 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
528 /* fall through */
529 case EM_LINELENGTH:
530 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
531 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT)wParam);
532 break;
534 case EM_REPLACESEL16:
535 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
536 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
537 /* fall through */
538 case EM_REPLACESEL:
539 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
540 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, (LPCSTR)lParam);
541 if (es->flags & EF_UPDATE) {
542 es->flags &= ~EF_UPDATE;
543 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
545 result = 1;
546 break;
548 /* message 0x00c3 missing from specs */
550 case WM_USER+19:
551 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
552 /* fall through */
553 case 0x00c3:
554 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
555 result = DefWindowProcA(hwnd, msg, wParam, lParam);
556 break;
558 case EM_GETLINE16:
559 DPRINTF_EDIT_MSG16("EM_GETLINE");
560 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
561 /* fall through */
562 case EM_GETLINE:
563 DPRINTF_EDIT_MSG32("EM_GETLINE");
564 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT)wParam, (LPSTR)lParam);
565 break;
567 case EM_LIMITTEXT16:
568 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
569 /* fall through */
570 case EM_SETLIMITTEXT:
571 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
572 EDIT_EM_SetLimitText(wnd, es, (INT)wParam);
573 break;
575 case EM_CANUNDO16:
576 DPRINTF_EDIT_MSG16("EM_CANUNDO");
577 /* fall through */
578 case EM_CANUNDO:
579 DPRINTF_EDIT_MSG32("EM_CANUNDO");
580 result = (LRESULT)EDIT_EM_CanUndo(wnd, es);
581 break;
583 case EM_UNDO16:
584 DPRINTF_EDIT_MSG16("EM_UNDO");
585 /* fall through */
586 case EM_UNDO:
587 /* fall through */
588 case WM_UNDO:
589 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
590 result = (LRESULT)EDIT_EM_Undo(wnd, es);
591 break;
593 case EM_FMTLINES16:
594 DPRINTF_EDIT_MSG16("EM_FMTLINES");
595 /* fall through */
596 case EM_FMTLINES:
597 DPRINTF_EDIT_MSG32("EM_FMTLINES");
598 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL)wParam);
599 break;
601 case EM_LINEFROMCHAR16:
602 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
603 /* fall through */
604 case EM_LINEFROMCHAR:
605 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
606 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT)wParam);
607 break;
609 /* message 0x00ca missing from specs */
611 case WM_USER+26:
612 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
613 /* fall through */
614 case 0x00ca:
615 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
616 result = DefWindowProcA(hwnd, msg, wParam, lParam);
617 break;
619 case EM_SETTABSTOPS16:
620 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
621 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
622 break;
623 case EM_SETTABSTOPS:
624 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
625 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT)wParam, (LPINT)lParam);
626 break;
628 case EM_SETPASSWORDCHAR16:
629 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
630 /* fall through */
631 case EM_SETPASSWORDCHAR:
632 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
633 EDIT_EM_SetPasswordChar(wnd, es, (CHAR)wParam);
634 break;
636 case EM_EMPTYUNDOBUFFER16:
637 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
638 /* fall through */
639 case EM_EMPTYUNDOBUFFER:
640 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
641 EDIT_EM_EmptyUndoBuffer(wnd, es);
642 break;
644 case EM_GETFIRSTVISIBLELINE16:
645 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
646 result = es->y_offset;
647 break;
648 case EM_GETFIRSTVISIBLELINE:
649 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
650 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
651 break;
653 case EM_SETREADONLY16:
654 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
655 /* fall through */
656 case EM_SETREADONLY:
657 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
658 if (wParam) {
659 wnd->dwStyle |= ES_READONLY;
660 es->style |= ES_READONLY;
661 } else {
662 wnd->dwStyle &= ~ES_READONLY;
663 es->style &= ~ES_READONLY;
665 result = 1;
666 break;
668 case EM_SETWORDBREAKPROC16:
669 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
670 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
671 break;
672 case EM_SETWORDBREAKPROC:
673 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
674 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROCA)lParam);
675 break;
677 case EM_GETWORDBREAKPROC16:
678 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
679 result = (LRESULT)es->word_break_proc16;
680 break;
681 case EM_GETWORDBREAKPROC:
682 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
683 result = (LRESULT)es->word_break_proc32A;
684 break;
686 case EM_GETPASSWORDCHAR16:
687 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
688 /* fall through */
689 case EM_GETPASSWORDCHAR:
690 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
691 result = es->password_char;
692 break;
694 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
696 case EM_SETMARGINS:
697 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
698 EDIT_EM_SetMargins(wnd, es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
699 break;
701 case EM_GETMARGINS:
702 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
703 result = MAKELONG(es->left_margin, es->right_margin);
704 break;
706 case EM_GETLIMITTEXT:
707 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
708 result = es->buffer_limit;
709 break;
711 case EM_POSFROMCHAR:
712 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
713 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
714 break;
716 case EM_CHARFROMPOS:
717 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
718 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
719 break;
721 case WM_GETDLGCODE:
722 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
723 result = DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
724 if (wnd->dwStyle & ES_WANTRETURN)
726 LPMSG msg = (LPMSG)lParam;
727 if (msg && (msg->message == WM_KEYDOWN) && (msg->wParam == VK_RETURN))
728 result |= DLGC_WANTMESSAGE;
730 break;
732 case WM_CHAR:
733 DPRINTF_EDIT_MSG32("WM_CHAR");
734 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
735 break;
737 case WM_CLEAR:
738 DPRINTF_EDIT_MSG32("WM_CLEAR");
739 EDIT_WM_Clear(wnd, es);
740 break;
742 case WM_COMMAND:
743 DPRINTF_EDIT_MSG32("WM_COMMAND");
744 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
745 break;
747 case WM_CONTEXTMENU:
748 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
749 EDIT_WM_ContextMenu(wnd, es, (HWND)wParam, SLOWORD(lParam), SHIWORD(lParam));
750 break;
752 case WM_COPY:
753 DPRINTF_EDIT_MSG32("WM_COPY");
754 EDIT_WM_Copy(wnd, es);
755 break;
757 case WM_CREATE:
758 DPRINTF_EDIT_MSG32("WM_CREATE");
759 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCTA)lParam);
760 break;
762 case WM_CUT:
763 DPRINTF_EDIT_MSG32("WM_CUT");
764 EDIT_WM_Cut(wnd, es);
765 break;
767 case WM_ENABLE:
768 DPRINTF_EDIT_MSG32("WM_ENABLE");
769 es->bEnableState = (BOOL) wParam;
770 EDIT_UpdateText(wnd, NULL, TRUE);
771 break;
773 case WM_ERASEBKGND:
774 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
775 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
776 break;
778 case WM_GETFONT:
779 DPRINTF_EDIT_MSG32("WM_GETFONT");
780 result = (LRESULT)es->font;
781 break;
783 case WM_GETTEXT:
784 DPRINTF_EDIT_MSG32("WM_GETTEXT");
785 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT)wParam, (LPSTR)lParam);
786 break;
788 case WM_GETTEXTLENGTH:
789 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
790 result = lstrlenA(es->text);
791 break;
793 case WM_HSCROLL:
794 DPRINTF_EDIT_MSG32("WM_HSCROLL");
795 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)lParam);
796 break;
798 case WM_KEYDOWN:
799 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
800 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
801 break;
803 case WM_KILLFOCUS:
804 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
805 result = EDIT_WM_KillFocus(wnd, es, (HWND)wParam);
806 break;
808 case WM_LBUTTONDBLCLK:
809 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
810 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
811 break;
813 case WM_LBUTTONDOWN:
814 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
815 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
816 break;
818 case WM_LBUTTONUP:
819 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
820 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
821 break;
823 case WM_MBUTTONDOWN:
824 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
825 result = EDIT_WM_MButtonDown(wnd);
826 break;
828 case WM_MOUSEACTIVATE:
830 * FIXME: maybe DefWindowProc() screws up, but it seems that
831 * modeless dialog boxes need this. If we don't do this, the focus
832 * will _not_ be set by DefWindowProc() for edit controls in a
833 * modeless dialog box ???
835 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
836 SetFocus(wnd->hwndSelf);
837 result = MA_ACTIVATE;
838 break;
840 case WM_MOUSEMOVE:
842 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
844 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
845 break;
847 case WM_PAINT:
848 DPRINTF_EDIT_MSG32("WM_PAINT");
849 EDIT_WM_Paint(wnd, es, wParam);
850 break;
852 case WM_PASTE:
853 DPRINTF_EDIT_MSG32("WM_PASTE");
854 EDIT_WM_Paste(wnd, es);
855 break;
857 case WM_SETFOCUS:
858 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
859 EDIT_WM_SetFocus(wnd, es, (HWND)wParam);
860 break;
862 case WM_SETFONT:
863 DPRINTF_EDIT_MSG32("WM_SETFONT");
864 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
865 break;
867 case WM_SETTEXT:
868 DPRINTF_EDIT_MSG32("WM_SETTEXT");
869 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
870 result = TRUE;
871 break;
873 case WM_SIZE:
874 DPRINTF_EDIT_MSG32("WM_SIZE");
875 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
876 break;
878 case WM_SYSKEYDOWN:
879 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
880 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
881 break;
883 case WM_TIMER:
884 DPRINTF_EDIT_MSG32("WM_TIMER");
885 EDIT_WM_Timer(wnd, es, (INT)wParam, (TIMERPROC)lParam);
886 break;
888 case WM_VSCROLL:
889 DPRINTF_EDIT_MSG32("WM_VSCROLL");
890 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)(lParam));
891 break;
893 case WM_MOUSEWHEEL:
895 short gcWheelDelta = 0;
896 UINT pulScrollLines = 3;
897 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
899 if (wParam & (MK_SHIFT | MK_CONTROL)) {
900 result = DefWindowProcA(hwnd, msg, wParam, lParam);
901 break;
903 gcWheelDelta -= (short) HIWORD(wParam);
904 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
906 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
907 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
908 result = EDIT_EM_LineScroll(wnd, es, 0, cLineScroll);
911 break;
912 default:
913 result = DefWindowProcA(hwnd, msg, wParam, lParam);
914 break;
916 EDIT_UnlockBuffer(wnd, es, FALSE);
917 END:
918 WIN_ReleaseWndPtr(wnd);
919 return result;
924 /*********************************************************************
926 * EDIT_BuildLineDefs_ML
928 * Build linked list of text lines.
929 * Lines can end with '\0' (last line), a character (if it is wrapped),
930 * a soft return '\r\r\n' or a hard return '\r\n'
933 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
935 HDC dc;
936 HFONT old_font = 0;
937 LPSTR start, cp;
938 INT fw;
939 LINEDEF *current_def;
940 LINEDEF **previous_next;
942 current_def = es->first_line_def;
943 do {
944 LINEDEF *next_def = current_def->next;
945 HeapFree(es->heap, 0, current_def);
946 current_def = next_def;
947 } while (current_def);
948 es->line_count = 0;
949 es->text_width = 0;
951 dc = GetDC(wnd->hwndSelf);
952 if (es->font)
953 old_font = SelectObject(dc, es->font);
955 fw = es->format_rect.right - es->format_rect.left;
956 start = es->text;
957 previous_next = &es->first_line_def;
958 do {
959 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
960 current_def->next = NULL;
961 cp = start;
962 while (*cp) {
963 if ((*cp == '\r') && (*(cp + 1) == '\n'))
964 break;
965 cp++;
967 if (!(*cp)) {
968 current_def->ending = END_0;
969 current_def->net_length = lstrlenA(start);
970 } else if ((cp > start) && (*(cp - 1) == '\r')) {
971 current_def->ending = END_SOFT;
972 current_def->net_length = cp - start - 1;
973 } else {
974 current_def->ending = END_HARD;
975 current_def->net_length = cp - start;
977 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
978 start, current_def->net_length,
979 es->tabs_count, es->tabs));
980 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
981 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
982 INT next = 0;
983 INT prev;
984 do {
985 prev = next;
986 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
987 prev + 1, current_def->net_length, WB_RIGHT);
988 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
989 start, next, es->tabs_count, es->tabs));
990 } while (current_def->width <= fw);
991 if (!prev) {
992 next = 0;
993 do {
994 prev = next;
995 next++;
996 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
997 start, next, es->tabs_count, es->tabs));
998 } while (current_def->width <= fw);
999 if (!prev)
1000 prev = 1;
1002 current_def->net_length = prev;
1003 current_def->ending = END_WRAP;
1004 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc, start,
1005 current_def->net_length, es->tabs_count, es->tabs));
1007 switch (current_def->ending) {
1008 case END_SOFT:
1009 current_def->length = current_def->net_length + 3;
1010 break;
1011 case END_HARD:
1012 current_def->length = current_def->net_length + 2;
1013 break;
1014 case END_WRAP:
1015 case END_0:
1016 current_def->length = current_def->net_length;
1017 break;
1019 es->text_width = max(es->text_width, current_def->width);
1020 start += current_def->length;
1021 *previous_next = current_def;
1022 previous_next = &current_def->next;
1023 es->line_count++;
1024 } while (current_def->ending != END_0);
1025 if (es->font)
1026 SelectObject(dc, old_font);
1027 ReleaseDC(wnd->hwndSelf, dc);
1031 /*********************************************************************
1033 * EDIT_CallWordBreakProc
1035 * Call appropriate WordBreakProc (internal or external).
1037 * Note: The "start" argument should always be an index refering
1038 * to es->text. The actual wordbreak proc might be
1039 * 16 bit, so we can't always pass any 32 bit LPSTR.
1040 * Hence we assume that es->text is the buffer that holds
1041 * the string under examination (we can decide this for ourselves).
1044 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action)
1046 if (es->word_break_proc16) {
1047 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
1048 SEGPTR segptr = LocalLock16(hloc16);
1049 INT ret = (INT)Callbacks->CallWordBreakProc(es->word_break_proc16,
1050 segptr + start, index, count, action);
1051 LocalUnlock16(hloc16);
1052 return ret;
1054 else if (es->word_break_proc32A)
1056 TRACE_(relay)("(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
1057 es->word_break_proc32A, es->text + start, index,
1058 count, action );
1059 return (INT)es->word_break_proc32A( es->text + start, index,
1060 count, action );
1062 else
1063 return EDIT_WordBreakProc(es->text + start, index, count, action);
1067 /*********************************************************************
1069 * EDIT_CharFromPos
1071 * Beware: This is not the function called on EM_CHARFROMPOS
1072 * The position _can_ be outside the formatting / client
1073 * rectangle
1074 * The return value is only the character index
1077 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1079 INT index;
1080 HDC dc;
1081 HFONT old_font = 0;
1083 if (es->style & ES_MULTILINE) {
1084 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1085 INT line_index = 0;
1086 LINEDEF *line_def = es->first_line_def;
1087 INT low, high;
1088 while ((line > 0) && line_def->next) {
1089 line_index += line_def->length;
1090 line_def = line_def->next;
1091 line--;
1093 x += es->x_offset - es->format_rect.left;
1094 if (x >= line_def->width) {
1095 if (after_wrap)
1096 *after_wrap = (line_def->ending == END_WRAP);
1097 return line_index + line_def->net_length;
1099 if (x <= 0) {
1100 if (after_wrap)
1101 *after_wrap = FALSE;
1102 return line_index;
1104 dc = GetDC(wnd->hwndSelf);
1105 if (es->font)
1106 old_font = SelectObject(dc, es->font);
1107 low = line_index + 1;
1108 high = line_index + line_def->net_length + 1;
1109 while (low < high - 1)
1111 INT mid = (low + high) / 2;
1112 if (LOWORD(GetTabbedTextExtentA(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1113 else low = mid;
1115 index = low;
1117 if (after_wrap)
1118 *after_wrap = ((index == line_index + line_def->net_length) &&
1119 (line_def->ending == END_WRAP));
1120 } else {
1121 LPSTR text;
1122 SIZE size;
1123 if (after_wrap)
1124 *after_wrap = FALSE;
1125 x -= es->format_rect.left;
1126 if (!x)
1127 return es->x_offset;
1128 text = EDIT_GetPasswordPointer_SL(wnd, es);
1129 dc = GetDC(wnd->hwndSelf);
1130 if (es->font)
1131 old_font = SelectObject(dc, es->font);
1132 if (x < 0)
1134 INT low = 0;
1135 INT high = es->x_offset;
1136 while (low < high - 1)
1138 INT mid = (low + high) / 2;
1139 GetTextExtentPoint32A( dc, text + mid,
1140 es->x_offset - mid, &size );
1141 if (size.cx > -x) low = mid;
1142 else high = mid;
1144 index = low;
1146 else
1148 INT low = es->x_offset;
1149 INT high = lstrlenA(es->text) + 1;
1150 while (low < high - 1)
1152 INT mid = (low + high) / 2;
1153 GetTextExtentPoint32A( dc, text + es->x_offset,
1154 mid - es->x_offset, &size );
1155 if (size.cx > x) high = mid;
1156 else low = mid;
1158 index = low;
1160 if (es->style & ES_PASSWORD)
1161 HeapFree(es->heap, 0 ,text);
1163 if (es->font)
1164 SelectObject(dc, old_font);
1165 ReleaseDC(wnd->hwndSelf, dc);
1166 return index;
1170 /*********************************************************************
1172 * EDIT_ConfinePoint
1174 * adjusts the point to be within the formatting rectangle
1175 * (so CharFromPos returns the nearest _visible_ character)
1178 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y)
1180 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1181 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1185 /*********************************************************************
1187 * EDIT_GetLineRect
1189 * Calculates the bounding rectangle for a line from a starting
1190 * column to an ending column.
1193 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1195 INT line_index = EDIT_EM_LineIndex(wnd, es, line);
1197 if (es->style & ES_MULTILINE)
1198 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1199 else
1200 rc->top = es->format_rect.top;
1201 rc->bottom = rc->top + es->line_height;
1202 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1203 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1207 /*********************************************************************
1209 * EDIT_GetPasswordPointer_SL
1211 * note: caller should free the (optionally) allocated buffer
1214 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1216 if (es->style & ES_PASSWORD) {
1217 INT len = lstrlenA(es->text);
1218 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1219 RtlFillMemory(text, len, es->password_char);
1220 text[len] = '\0';
1221 return text;
1222 } else
1223 return es->text;
1227 /*********************************************************************
1229 * EDIT_LockBuffer
1231 * This acts as a LOCAL_Lock(), but it locks only once. This way
1232 * you can call it whenever you like, without unlocking.
1235 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1237 if (!es) {
1238 ERR("no EDITSTATE ... please report\n");
1239 return;
1241 if (!(es->style & ES_MULTILINE))
1242 return;
1243 if (!es->text) {
1244 if (es->hloc32)
1245 es->text = LocalLock(es->hloc32);
1246 else if (es->hloc16)
1247 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1248 else {
1249 ERR("no buffer ... please report\n");
1250 return;
1253 es->lock_count++;
1257 /*********************************************************************
1259 * EDIT_SL_InvalidateText
1261 * Called from EDIT_InvalidateText().
1262 * Does the job for single-line controls only.
1265 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1267 RECT line_rect;
1268 RECT rc;
1270 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1271 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1272 EDIT_UpdateText(wnd, &rc, FALSE);
1276 /*********************************************************************
1278 * EDIT_ML_InvalidateText
1280 * Called from EDIT_InvalidateText().
1281 * Does the job for multi-line controls only.
1284 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1286 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1287 INT sl = EDIT_EM_LineFromChar(wnd, es, start);
1288 INT el = EDIT_EM_LineFromChar(wnd, es, end);
1289 INT sc;
1290 INT ec;
1291 RECT rc1;
1292 RECT rcWnd;
1293 RECT rcLine;
1294 RECT rcUpdate;
1295 INT l;
1297 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1298 return;
1300 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1301 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1302 if (sl < es->y_offset) {
1303 sl = es->y_offset;
1304 sc = 0;
1306 if (el > es->y_offset + vlc) {
1307 el = es->y_offset + vlc;
1308 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1310 GetClientRect(wnd->hwndSelf, &rc1);
1311 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1312 if (sl == el) {
1313 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1314 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1315 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1316 } else {
1317 EDIT_GetLineRect(wnd, es, sl, sc,
1318 EDIT_EM_LineLength(wnd, es,
1319 EDIT_EM_LineIndex(wnd, es, sl)),
1320 &rcLine);
1321 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1322 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1323 for (l = sl + 1 ; l < el ; l++) {
1324 EDIT_GetLineRect(wnd, es, l, 0,
1325 EDIT_EM_LineLength(wnd, es,
1326 EDIT_EM_LineIndex(wnd, es, l)),
1327 &rcLine);
1328 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1329 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1331 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1332 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1333 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1338 /*********************************************************************
1340 * EDIT_InvalidateText
1342 * Invalidate the text from offset start upto, but not including,
1343 * offset end. Useful for (re)painting the selection.
1344 * Regions outside the linewidth are not invalidated.
1345 * end == -1 means end == TextLength.
1346 * start and end need not be ordered.
1349 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1351 if (end == start)
1352 return;
1354 if (end == -1)
1355 end = lstrlenA(es->text);
1357 ORDER_INT(start, end);
1359 if (es->style & ES_MULTILINE)
1360 EDIT_ML_InvalidateText(wnd, es, start, end);
1361 else
1362 EDIT_SL_InvalidateText(wnd, es, start, end);
1366 /*********************************************************************
1368 * EDIT_MakeFit
1370 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1373 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size)
1375 HLOCAL hNew32;
1376 HLOCAL16 hNew16;
1378 if (size <= es->buffer_size)
1379 return TRUE;
1380 if (size > es->buffer_limit) {
1381 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1382 return FALSE;
1384 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1385 if (size > es->buffer_limit)
1386 size = es->buffer_limit;
1388 TRACE("trying to ReAlloc to %d+1\n", size);
1390 EDIT_UnlockBuffer(wnd, es, TRUE);
1391 if (es->text) {
1392 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1393 es->buffer_size = min(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1394 else
1395 es->buffer_size = 0;
1396 } else if (es->hloc32) {
1397 if ((hNew32 = LocalReAlloc(es->hloc32, size + 1, 0))) {
1398 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1399 es->hloc32 = hNew32;
1400 es->buffer_size = min(LocalSize(es->hloc32) - 1, es->buffer_limit);
1402 } else if (es->hloc16) {
1403 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1404 TRACE("Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1405 es->hloc16 = hNew16;
1406 es->buffer_size = min(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1409 if (es->buffer_size < size) {
1410 EDIT_LockBuffer(wnd, es);
1411 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1412 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1413 return FALSE;
1414 } else {
1415 EDIT_LockBuffer(wnd, es);
1416 TRACE("We now have %d+1\n", es->buffer_size);
1417 return TRUE;
1422 /*********************************************************************
1424 * EDIT_MakeUndoFit
1426 * Try to fit size + 1 bytes in the undo buffer.
1429 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size)
1431 if (size <= es->undo_buffer_size)
1432 return TRUE;
1433 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1435 TRACE("trying to ReAlloc to %d+1\n", size);
1437 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1438 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1439 if (es->undo_buffer_size < size) {
1440 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1441 return FALSE;
1443 return TRUE;
1445 return FALSE;
1449 /*********************************************************************
1451 * EDIT_MoveBackward
1454 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1456 INT e = es->selection_end;
1458 if (e) {
1459 e--;
1460 if ((es->style & ES_MULTILINE) && e &&
1461 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1462 e--;
1463 if (e && (es->text[e - 1] == '\r'))
1464 e--;
1467 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1468 EDIT_EM_ScrollCaret(wnd, es);
1472 /*********************************************************************
1474 * EDIT_MoveDown_ML
1476 * Only for multi line controls
1477 * Move the caret one line down, on a column with the nearest
1478 * x coordinate on the screen (might be a different column).
1481 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1483 INT s = es->selection_start;
1484 INT e = es->selection_end;
1485 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1486 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1487 INT x = SLOWORD(pos);
1488 INT y = SHIWORD(pos);
1490 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1491 if (!extend)
1492 s = e;
1493 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1494 EDIT_EM_ScrollCaret(wnd, es);
1498 /*********************************************************************
1500 * EDIT_MoveEnd
1503 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1505 BOOL after_wrap = FALSE;
1506 INT e;
1508 /* Pass a high value in x to make sure of receiving the en of the line */
1509 if (es->style & ES_MULTILINE)
1510 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1511 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1512 else
1513 e = lstrlenA(es->text);
1514 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1515 EDIT_EM_ScrollCaret(wnd, es);
1519 /*********************************************************************
1521 * EDIT_MoveForward
1524 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1526 INT e = es->selection_end;
1528 if (es->text[e]) {
1529 e++;
1530 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1531 if (es->text[e] == '\n')
1532 e++;
1533 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1534 e += 2;
1537 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1538 EDIT_EM_ScrollCaret(wnd, es);
1542 /*********************************************************************
1544 * EDIT_MoveHome
1546 * Home key: move to beginning of line.
1549 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1551 INT e;
1553 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1554 if (es->style & ES_MULTILINE)
1555 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1556 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1557 else
1558 e = 0;
1559 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1560 EDIT_EM_ScrollCaret(wnd, es);
1564 /*********************************************************************
1566 * EDIT_MovePageDown_ML
1568 * Only for multi line controls
1569 * Move the caret one page down, on a column with the nearest
1570 * x coordinate on the screen (might be a different column).
1573 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1575 INT s = es->selection_start;
1576 INT e = es->selection_end;
1577 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1578 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1579 INT x = SLOWORD(pos);
1580 INT y = SHIWORD(pos);
1582 e = EDIT_CharFromPos(wnd, es, x,
1583 y + (es->format_rect.bottom - es->format_rect.top),
1584 &after_wrap);
1585 if (!extend)
1586 s = e;
1587 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1588 EDIT_EM_ScrollCaret(wnd, es);
1592 /*********************************************************************
1594 * EDIT_MovePageUp_ML
1596 * Only for multi line controls
1597 * Move the caret one page up, on a column with the nearest
1598 * x coordinate on the screen (might be a different column).
1601 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1603 INT s = es->selection_start;
1604 INT e = es->selection_end;
1605 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1606 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1607 INT x = SLOWORD(pos);
1608 INT y = SHIWORD(pos);
1610 e = EDIT_CharFromPos(wnd, es, x,
1611 y - (es->format_rect.bottom - es->format_rect.top),
1612 &after_wrap);
1613 if (!extend)
1614 s = e;
1615 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1616 EDIT_EM_ScrollCaret(wnd, es);
1620 /*********************************************************************
1622 * EDIT_MoveUp_ML
1624 * Only for multi line controls
1625 * Move the caret one line up, on a column with the nearest
1626 * x coordinate on the screen (might be a different column).
1629 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1631 INT s = es->selection_start;
1632 INT e = es->selection_end;
1633 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1634 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1635 INT x = SLOWORD(pos);
1636 INT y = SHIWORD(pos);
1638 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1639 if (!extend)
1640 s = e;
1641 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1642 EDIT_EM_ScrollCaret(wnd, es);
1646 /*********************************************************************
1648 * EDIT_MoveWordBackward
1651 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1653 INT s = es->selection_start;
1654 INT e = es->selection_end;
1655 INT l;
1656 INT ll;
1657 INT li;
1659 l = EDIT_EM_LineFromChar(wnd, es, e);
1660 ll = EDIT_EM_LineLength(wnd, es, e);
1661 li = EDIT_EM_LineIndex(wnd, es, l);
1662 if (e - li == 0) {
1663 if (l) {
1664 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1665 e = li + EDIT_EM_LineLength(wnd, es, li);
1667 } else {
1668 e = li + (INT)EDIT_CallWordBreakProc(wnd, es,
1669 li, e - li, ll, WB_LEFT);
1671 if (!extend)
1672 s = e;
1673 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1674 EDIT_EM_ScrollCaret(wnd, es);
1678 /*********************************************************************
1680 * EDIT_MoveWordForward
1683 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1685 INT s = es->selection_start;
1686 INT e = es->selection_end;
1687 INT l;
1688 INT ll;
1689 INT li;
1691 l = EDIT_EM_LineFromChar(wnd, es, e);
1692 ll = EDIT_EM_LineLength(wnd, es, e);
1693 li = EDIT_EM_LineIndex(wnd, es, l);
1694 if (e - li == ll) {
1695 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1696 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1697 } else {
1698 e = li + EDIT_CallWordBreakProc(wnd, es,
1699 li, e - li + 1, ll, WB_RIGHT);
1701 if (!extend)
1702 s = e;
1703 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1704 EDIT_EM_ScrollCaret(wnd, es);
1708 /*********************************************************************
1710 * EDIT_PaintLine
1713 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1715 INT s = es->selection_start;
1716 INT e = es->selection_end;
1717 INT li;
1718 INT ll;
1719 INT x;
1720 INT y;
1721 LRESULT pos;
1723 if (es->style & ES_MULTILINE) {
1724 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1725 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1726 return;
1727 } else if (line)
1728 return;
1730 TRACE("line=%d\n", line);
1732 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1733 x = SLOWORD(pos);
1734 y = SHIWORD(pos);
1735 li = EDIT_EM_LineIndex(wnd, es, line);
1736 ll = EDIT_EM_LineLength(wnd, es, li);
1737 s = es->selection_start;
1738 e = es->selection_end;
1739 ORDER_INT(s, e);
1740 s = min(li + ll, max(li, s));
1741 e = min(li + ll, max(li, e));
1742 if (rev && (s != e) &&
1743 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1744 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1745 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1746 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1747 } else
1748 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1752 /*********************************************************************
1754 * EDIT_PaintText
1757 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1759 COLORREF BkColor;
1760 COLORREF TextColor;
1761 INT ret;
1762 INT li;
1763 SIZE size;
1765 if (!count)
1766 return 0;
1767 BkColor = GetBkColor(dc);
1768 TextColor = GetTextColor(dc);
1769 if (rev) {
1770 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
1771 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1773 li = EDIT_EM_LineIndex(wnd, es, line);
1774 if (es->style & ES_MULTILINE) {
1775 ret = (INT)LOWORD(TabbedTextOutA(dc, x, y, es->text + li + col, count,
1776 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1777 } else {
1778 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1779 TextOutA(dc, x, y, text + li + col, count);
1780 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1781 ret = size.cx;
1782 if (es->style & ES_PASSWORD)
1783 HeapFree(es->heap, 0, text);
1785 if (rev) {
1786 SetBkColor(dc, BkColor);
1787 SetTextColor(dc, TextColor);
1789 return ret;
1793 /*********************************************************************
1795 * EDIT_SetCaretPos
1798 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
1799 BOOL after_wrap)
1801 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1802 INT x = SLOWORD(res);
1803 INT y = SHIWORD(res);
1805 if(x < es->format_rect.left)
1806 x = es->format_rect.left;
1807 if(x > es->format_rect.right - 2)
1808 x = es->format_rect.right - 2;
1809 if(y > es->format_rect.bottom)
1810 y = es->format_rect.bottom;
1811 if(y < es->format_rect.top)
1812 y = es->format_rect.top;
1813 SetCaretPos(x, y);
1814 return;
1818 /*********************************************************************
1820 * EDIT_SetRectNP
1822 * note: this is not (exactly) the handler called on EM_SETRECTNP
1823 * it is also used to set the rect of a single line control
1826 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
1828 CopyRect(&es->format_rect, rc);
1829 if (es->style & WS_BORDER) {
1830 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
1831 if(TWEAK_WineLook == WIN31_LOOK)
1832 bw += 2;
1833 es->format_rect.left += bw;
1834 es->format_rect.top += bw;
1835 es->format_rect.right -= bw;
1836 es->format_rect.bottom -= bw;
1838 es->format_rect.left += es->left_margin;
1839 es->format_rect.right -= es->right_margin;
1840 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
1841 if (es->style & ES_MULTILINE)
1842 es->format_rect.bottom = es->format_rect.top +
1843 max(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1844 else
1845 es->format_rect.bottom = es->format_rect.top + es->line_height;
1846 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1847 EDIT_BuildLineDefs_ML(wnd, es);
1851 /*********************************************************************
1853 * EDIT_UnlockBuffer
1856 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
1858 if (!es) {
1859 ERR("no EDITSTATE ... please report\n");
1860 return;
1862 if (!(es->style & ES_MULTILINE))
1863 return;
1864 if (!es->lock_count) {
1865 ERR("lock_count == 0 ... please report\n");
1866 return;
1868 if (!es->text) {
1869 ERR("es->text == 0 ... please report\n");
1870 return;
1872 if (force || (es->lock_count == 1)) {
1873 if (es->hloc32) {
1874 LocalUnlock(es->hloc32);
1875 es->text = NULL;
1876 } else if (es->hloc16) {
1877 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1878 es->text = NULL;
1881 es->lock_count--;
1885 /*********************************************************************
1887 * EDIT_WordBreakProc
1889 * Find the beginning of words.
1890 * Note: unlike the specs for a WordBreakProc, this function only
1891 * allows to be called without linebreaks between s[0] upto
1892 * s[count - 1]. Remember it is only called
1893 * internally, so we can decide this for ourselves.
1896 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action)
1898 INT ret = 0;
1900 TRACE("s=%p, index=%u, count=%u, action=%d\n",
1901 s, index, count, action);
1903 switch (action) {
1904 case WB_LEFT:
1905 if (!count)
1906 break;
1907 if (index)
1908 index--;
1909 if (s[index] == ' ') {
1910 while (index && (s[index] == ' '))
1911 index--;
1912 if (index) {
1913 while (index && (s[index] != ' '))
1914 index--;
1915 if (s[index] == ' ')
1916 index++;
1918 } else {
1919 while (index && (s[index] != ' '))
1920 index--;
1921 if (s[index] == ' ')
1922 index++;
1924 ret = index;
1925 break;
1926 case WB_RIGHT:
1927 if (!count)
1928 break;
1929 if (index)
1930 index--;
1931 if (s[index] == ' ')
1932 while ((index < count) && (s[index] == ' ')) index++;
1933 else {
1934 while (s[index] && (s[index] != ' ') && (index < count))
1935 index++;
1936 while ((s[index] == ' ') && (index < count)) index++;
1938 ret = index;
1939 break;
1940 case WB_ISDELIMITER:
1941 ret = (s[index] == ' ');
1942 break;
1943 default:
1944 ERR("unknown action code, please report !\n");
1945 break;
1947 return ret;
1951 /*********************************************************************
1953 * EM_CHARFROMPOS
1955 * returns line number (not index) in high-order word of result.
1956 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
1957 * to Richedit, not to the edit control. Original documentation is valid.
1958 * FIXME: do the specs mean to return -1 if outside client area or
1959 * if outside formatting rectangle ???
1962 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
1964 POINT pt;
1965 RECT rc;
1966 INT index;
1968 pt.x = x;
1969 pt.y = y;
1970 GetClientRect(wnd->hwndSelf, &rc);
1971 if (!PtInRect(&rc, pt))
1972 return -1;
1974 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1975 return MAKELONG(index, EDIT_EM_LineFromChar(wnd, es, index));
1979 /*********************************************************************
1981 * EM_FMTLINES
1983 * Enable or disable soft breaks.
1985 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol)
1987 es->flags &= ~EF_USE_SOFTBRK;
1988 if (add_eol) {
1989 es->flags |= EF_USE_SOFTBRK;
1990 FIXME("soft break enabled, not implemented\n");
1992 return add_eol;
1996 /*********************************************************************
1998 * EM_GETHANDLE
2000 * Hopefully this won't fire back at us.
2001 * We always start with a fixed buffer in our own heap.
2002 * However, with this message a 32 bit application requests
2003 * a handle to 32 bit moveable local heap memory, where it expects
2004 * to find the text.
2005 * It's a pity that from this moment on we have to use this
2006 * local heap, because applications may rely on the handle
2007 * in the future.
2009 * In this function we'll try to switch to local heap.
2012 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
2014 HLOCAL newBuf;
2015 LPSTR newText;
2016 INT newSize;
2018 if (!(es->style & ES_MULTILINE))
2019 return 0;
2021 if (es->hloc32)
2022 return es->hloc32;
2023 else if (es->hloc16)
2024 return (HLOCAL)es->hloc16;
2026 if (!(newBuf = LocalAlloc(LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2027 ERR("could not allocate new 32 bit buffer\n");
2028 return 0;
2030 newSize = min(LocalSize(newBuf) - 1, es->buffer_limit);
2031 if (!(newText = LocalLock(newBuf))) {
2032 ERR("could not lock new 32 bit buffer\n");
2033 LocalFree(newBuf);
2034 return 0;
2036 lstrcpyA(newText, es->text);
2037 EDIT_UnlockBuffer(wnd, es, TRUE);
2038 if (es->text)
2039 HeapFree(es->heap, 0, es->text);
2040 es->hloc32 = newBuf;
2041 es->hloc16 = (HLOCAL16)NULL;
2042 es->buffer_size = newSize;
2043 es->text = newText;
2044 EDIT_LockBuffer(wnd, es);
2045 TRACE("switched to 32 bit local heap\n");
2047 return es->hloc32;
2051 /*********************************************************************
2053 * EM_GETHANDLE16
2055 * Hopefully this won't fire back at us.
2056 * We always start with a buffer in 32 bit linear memory.
2057 * However, with this message a 16 bit application requests
2058 * a handle of 16 bit local heap memory, where it expects to find
2059 * the text.
2060 * It's a pitty that from this moment on we have to use this
2061 * local heap, because applications may rely on the handle
2062 * in the future.
2064 * In this function we'll try to switch to local heap.
2066 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2068 HLOCAL16 newBuf;
2069 LPSTR newText;
2070 INT newSize;
2072 if (!(es->style & ES_MULTILINE))
2073 return 0;
2075 if (es->hloc16)
2076 return es->hloc16;
2078 if (!LOCAL_HeapSize(wnd->hInstance)) {
2079 if (!LocalInit16(wnd->hInstance, 0,
2080 GlobalSize16(wnd->hInstance))) {
2081 ERR("could not initialize local heap\n");
2082 return 0;
2084 TRACE("local heap initialized\n");
2086 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2087 ERR("could not allocate new 16 bit buffer\n");
2088 return 0;
2090 newSize = min(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2091 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2092 ERR("could not lock new 16 bit buffer\n");
2093 LOCAL_Free(wnd->hInstance, newBuf);
2094 return 0;
2096 lstrcpyA(newText, es->text);
2097 EDIT_UnlockBuffer(wnd, es, TRUE);
2098 if (es->text)
2099 HeapFree(es->heap, 0, es->text);
2100 else if (es->hloc32) {
2101 while (LocalFree(es->hloc32)) ;
2102 LocalFree(es->hloc32);
2104 es->hloc32 = (HLOCAL)NULL;
2105 es->hloc16 = newBuf;
2106 es->buffer_size = newSize;
2107 es->text = newText;
2108 EDIT_LockBuffer(wnd, es);
2109 TRACE("switched to 16 bit buffer\n");
2111 return es->hloc16;
2115 /*********************************************************************
2117 * EM_GETLINE
2120 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch)
2122 LPSTR src;
2123 INT len;
2124 INT i;
2126 if (es->style & ES_MULTILINE) {
2127 if (line >= es->line_count)
2128 return 0;
2129 } else
2130 line = 0;
2131 i = EDIT_EM_LineIndex(wnd, es, line);
2132 src = es->text + i;
2133 len = min(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, i));
2134 for (i = 0 ; i < len ; i++) {
2135 *lpch = *src;
2136 src++;
2137 lpch++;
2139 return (LRESULT)len;
2143 /*********************************************************************
2145 * EM_GETSEL
2148 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end)
2150 UINT s = es->selection_start;
2151 UINT e = es->selection_end;
2153 ORDER_UINT(s, e);
2154 if (start)
2155 *start = s;
2156 if (end)
2157 *end = e;
2158 return MAKELONG(s, e);
2162 /*********************************************************************
2164 * EM_GETTHUMB
2166 * FIXME: is this right ? (or should it be only VSCROLL)
2167 * (and maybe only for edit controls that really have their
2168 * own scrollbars) (and maybe only for multiline controls ?)
2169 * All in all: very poorly documented
2171 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2172 * WM_VSCROLL handlers
2175 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2177 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0, 0),
2178 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0, 0));
2182 /*********************************************************************
2184 * EM_LINEFROMCHAR
2187 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index)
2189 INT line;
2190 LINEDEF *line_def;
2192 if (!(es->style & ES_MULTILINE))
2193 return 0;
2194 if (index > lstrlenA(es->text))
2195 return es->line_count - 1;
2196 if (index == -1)
2197 index = min(es->selection_start, es->selection_end);
2199 line = 0;
2200 line_def = es->first_line_def;
2201 index -= line_def->length;
2202 while ((index >= 0) && line_def->next) {
2203 line++;
2204 line_def = line_def->next;
2205 index -= line_def->length;
2207 return line;
2211 /*********************************************************************
2213 * EM_LINEINDEX
2216 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line)
2218 INT line_index;
2219 LINEDEF *line_def;
2221 if (!(es->style & ES_MULTILINE))
2222 return 0;
2223 if (line >= es->line_count)
2224 return -1;
2226 line_index = 0;
2227 line_def = es->first_line_def;
2228 if (line == -1) {
2229 INT index = es->selection_end - line_def->length;
2230 while ((index >= 0) && line_def->next) {
2231 line_index += line_def->length;
2232 line_def = line_def->next;
2233 index -= line_def->length;
2235 } else {
2236 while (line > 0) {
2237 line_index += line_def->length;
2238 line_def = line_def->next;
2239 line--;
2242 return line_index;
2246 /*********************************************************************
2248 * EM_LINELENGTH
2251 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index)
2253 LINEDEF *line_def;
2255 if (!(es->style & ES_MULTILINE))
2256 return lstrlenA(es->text);
2258 if (index == -1) {
2259 /* get the number of remaining non-selected chars of selected lines */
2260 INT32 li;
2261 INT32 count;
2262 li = EDIT_EM_LineFromChar(wnd, es, es->selection_start);
2263 /* # chars before start of selection area */
2264 count = es->selection_start - EDIT_EM_LineIndex(wnd, es, li);
2265 li = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2266 /* # chars after end of selection */
2267 count += EDIT_EM_LineIndex(wnd, es, li) +
2268 EDIT_EM_LineLength(wnd, es, li) - es->selection_end;
2269 return count;
2271 line_def = es->first_line_def;
2272 index -= line_def->length;
2273 while ((index >= 0) && line_def->next) {
2274 line_def = line_def->next;
2275 index -= line_def->length;
2277 return line_def->net_length;
2281 /*********************************************************************
2283 * EM_LINESCROLL
2285 * FIXME: dx is in average character widths
2286 * However, we assume it is in pixels when we use this
2287 * function internally
2290 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2292 INT nyoff;
2294 if (!(es->style & ES_MULTILINE))
2295 return FALSE;
2297 if (-dx > es->x_offset)
2298 dx = -es->x_offset;
2299 if (dx > es->text_width - es->x_offset)
2300 dx = es->text_width - es->x_offset;
2301 nyoff = max(0, es->y_offset + dy);
2302 if (nyoff >= es->line_count)
2303 nyoff = es->line_count - 1;
2304 dy = (es->y_offset - nyoff) * es->line_height;
2305 if (dx || dy) {
2306 RECT rc1;
2307 RECT rc;
2308 GetClientRect(wnd->hwndSelf, &rc1);
2309 IntersectRect(&rc, &rc1, &es->format_rect);
2310 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2311 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2312 es->y_offset = nyoff;
2313 es->x_offset += dx;
2315 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2316 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2317 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2318 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2319 return TRUE;
2323 /*********************************************************************
2325 * EM_POSFROMCHAR
2328 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2330 INT len = lstrlenA(es->text);
2331 INT l;
2332 INT li;
2333 INT x;
2334 INT y = 0;
2335 HDC dc;
2336 HFONT old_font = 0;
2337 SIZE size;
2339 index = min(index, len);
2340 dc = GetDC(wnd->hwndSelf);
2341 if (es->font)
2342 old_font = SelectObject(dc, es->font);
2343 if (es->style & ES_MULTILINE) {
2344 l = EDIT_EM_LineFromChar(wnd, es, index);
2345 y = (l - es->y_offset) * es->line_height;
2346 li = EDIT_EM_LineIndex(wnd, es, l);
2347 if (after_wrap && (li == index) && l) {
2348 INT l2 = l - 1;
2349 LINEDEF *line_def = es->first_line_def;
2350 while (l2) {
2351 line_def = line_def->next;
2352 l2--;
2354 if (line_def->ending == END_WRAP) {
2355 l--;
2356 y -= es->line_height;
2357 li = EDIT_EM_LineIndex(wnd, es, l);
2360 x = LOWORD(GetTabbedTextExtentA(dc, es->text + li, index - li,
2361 es->tabs_count, es->tabs)) - es->x_offset;
2362 } else {
2363 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
2364 if (index < es->x_offset) {
2365 GetTextExtentPoint32A(dc, text + index,
2366 es->x_offset - index, &size);
2367 x = -size.cx;
2368 } else {
2369 GetTextExtentPoint32A(dc, text + es->x_offset,
2370 index - es->x_offset, &size);
2371 x = size.cx;
2373 y = 0;
2374 if (es->style & ES_PASSWORD)
2375 HeapFree(es->heap, 0 ,text);
2377 x += es->format_rect.left;
2378 y += es->format_rect.top;
2379 if (es->font)
2380 SelectObject(dc, old_font);
2381 ReleaseDC(wnd->hwndSelf, dc);
2382 return MAKELONG((INT16)x, (INT16)y);
2386 /*********************************************************************
2388 * EM_REPLACESEL
2390 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2393 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace)
2395 INT strl = lstrlenA(lpsz_replace);
2396 INT tl = lstrlenA(es->text);
2397 INT utl;
2398 UINT s;
2399 UINT e;
2400 INT i;
2401 LPSTR p;
2403 s = es->selection_start;
2404 e = es->selection_end;
2406 if ((s == e) && !strl)
2407 return;
2409 ORDER_UINT(s, e);
2411 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2412 return;
2414 if (e != s) {
2415 /* there is something to be deleted */
2416 if (can_undo) {
2417 utl = lstrlenA(es->undo_text);
2418 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2419 /* undo-buffer is extended to the right */
2420 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2421 lstrcpynA(es->undo_text + utl, es->text + s, e - s + 1);
2422 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2423 /* undo-buffer is extended to the left */
2424 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2425 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2426 p[e - s] = p[0];
2427 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2428 p[i] = (es->text + s)[i];
2429 es->undo_position = s;
2430 } else {
2431 /* new undo-buffer */
2432 EDIT_MakeUndoFit(wnd, es, e - s);
2433 lstrcpynA(es->undo_text, es->text + s, e - s + 1);
2434 es->undo_position = s;
2436 /* any deletion makes the old insertion-undo invalid */
2437 es->undo_insert_count = 0;
2438 } else
2439 EDIT_EM_EmptyUndoBuffer(wnd, es);
2441 /* now delete */
2442 lstrcpyA(es->text + s, es->text + e);
2444 if (strl) {
2445 /* there is an insertion */
2446 if (can_undo) {
2447 if ((s == es->undo_position) ||
2448 ((es->undo_insert_count) &&
2449 (s == es->undo_position + es->undo_insert_count)))
2451 * insertion is new and at delete position or
2452 * an extension to either left or right
2454 es->undo_insert_count += strl;
2455 else {
2456 /* new insertion undo */
2457 es->undo_position = s;
2458 es->undo_insert_count = strl;
2459 /* new insertion makes old delete-buffer invalid */
2460 *es->undo_text = '\0';
2462 } else
2463 EDIT_EM_EmptyUndoBuffer(wnd, es);
2465 /* now insert */
2466 tl = lstrlenA(es->text);
2467 for (p = es->text + tl ; p >= es->text + s ; p--)
2468 p[strl] = p[0];
2469 for (i = 0 , p = es->text + s ; i < strl ; i++)
2470 p[i] = lpsz_replace[i];
2471 if(es->style & ES_UPPERCASE)
2472 CharUpperBuffA(p, strl);
2473 else if(es->style & ES_LOWERCASE)
2474 CharLowerBuffA(p, strl);
2475 s += strl;
2477 /* FIXME: really inefficient */
2478 if (es->style & ES_MULTILINE)
2479 EDIT_BuildLineDefs_ML(wnd, es);
2481 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2482 es->flags |= EF_MODIFIED;
2483 es->flags |= EF_UPDATE;
2484 EDIT_EM_ScrollCaret(wnd, es);
2486 /* FIXME: really inefficient */
2487 EDIT_UpdateText(wnd, NULL, TRUE);
2491 /*********************************************************************
2493 * EM_SCROLL
2496 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2498 INT dy;
2500 if (!(es->style & ES_MULTILINE))
2501 return (LRESULT)FALSE;
2503 dy = 0;
2505 switch (action) {
2506 case SB_LINEUP:
2507 if (es->y_offset)
2508 dy = -1;
2509 break;
2510 case SB_LINEDOWN:
2511 if (es->y_offset < es->line_count - 1)
2512 dy = 1;
2513 break;
2514 case SB_PAGEUP:
2515 if (es->y_offset)
2516 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2517 break;
2518 case SB_PAGEDOWN:
2519 if (es->y_offset < es->line_count - 1)
2520 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2521 break;
2522 default:
2523 return (LRESULT)FALSE;
2525 if (dy) {
2526 EDIT_EM_LineScroll(wnd, es, 0, dy);
2527 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2529 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2533 /*********************************************************************
2535 * EM_SCROLLCARET
2538 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2540 if (es->style & ES_MULTILINE) {
2541 INT l;
2542 INT li;
2543 INT vlc;
2544 INT ww;
2545 INT cw = es->char_width;
2546 INT x;
2547 INT dy = 0;
2548 INT dx = 0;
2550 l = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2551 li = EDIT_EM_LineIndex(wnd, es, l);
2552 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2553 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2554 if (l >= es->y_offset + vlc)
2555 dy = l - vlc + 1 - es->y_offset;
2556 if (l < es->y_offset)
2557 dy = l - es->y_offset;
2558 ww = es->format_rect.right - es->format_rect.left;
2559 if (x < es->format_rect.left)
2560 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2561 if (x > es->format_rect.right)
2562 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2563 if (dy || dx)
2564 EDIT_EM_LineScroll(wnd, es, dx, dy);
2565 } else {
2566 INT x;
2567 INT goal;
2568 INT format_width;
2570 if (!(es->style & ES_AUTOHSCROLL))
2571 return;
2573 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2574 format_width = es->format_rect.right - es->format_rect.left;
2575 if (x < es->format_rect.left) {
2576 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2577 do {
2578 es->x_offset--;
2579 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2580 } while ((x < goal) && es->x_offset);
2581 /* FIXME: use ScrollWindow() somehow to improve performance */
2582 EDIT_UpdateText(wnd, NULL, TRUE);
2583 } else if (x > es->format_rect.right) {
2584 INT x_last;
2585 INT len = lstrlenA(es->text);
2586 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2587 do {
2588 es->x_offset++;
2589 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2590 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2591 } while ((x > goal) && (x_last > es->format_rect.right));
2592 /* FIXME: use ScrollWindow() somehow to improve performance */
2593 EDIT_UpdateText(wnd, NULL, TRUE);
2599 /*********************************************************************
2601 * EM_SETHANDLE
2603 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2606 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2608 if (!(es->style & ES_MULTILINE))
2609 return;
2611 if (!hloc) {
2612 WARN("called with NULL handle\n");
2613 return;
2616 EDIT_UnlockBuffer(wnd, es, TRUE);
2618 * old buffer is freed by caller, unless
2619 * it is still in our own heap. (in that case
2620 * we free it, correcting the buggy caller.)
2622 if (es->text)
2623 HeapFree(es->heap, 0, es->text);
2625 es->hloc16 = (HLOCAL16)NULL;
2626 es->hloc32 = hloc;
2627 es->text = NULL;
2628 es->buffer_size = LocalSize(es->hloc32) - 1;
2629 EDIT_LockBuffer(wnd, es);
2631 es->x_offset = es->y_offset = 0;
2632 es->selection_start = es->selection_end = 0;
2633 EDIT_EM_EmptyUndoBuffer(wnd, es);
2634 es->flags &= ~EF_MODIFIED;
2635 es->flags &= ~EF_UPDATE;
2636 EDIT_BuildLineDefs_ML(wnd, es);
2637 EDIT_UpdateText(wnd, NULL, TRUE);
2638 EDIT_EM_ScrollCaret(wnd, es);
2642 /*********************************************************************
2644 * EM_SETHANDLE16
2646 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2649 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2651 if (!(es->style & ES_MULTILINE))
2652 return;
2654 if (!hloc) {
2655 WARN("called with NULL handle\n");
2656 return;
2659 EDIT_UnlockBuffer(wnd, es, TRUE);
2661 * old buffer is freed by caller, unless
2662 * it is still in our own heap. (in that case
2663 * we free it, correcting the buggy caller.)
2665 if (es->text)
2666 HeapFree(es->heap, 0, es->text);
2668 es->hloc16 = hloc;
2669 es->hloc32 = (HLOCAL)NULL;
2670 es->text = NULL;
2671 es->buffer_size = LOCAL_Size(wnd->hInstance, es->hloc16) - 1;
2672 EDIT_LockBuffer(wnd, es);
2674 es->x_offset = es->y_offset = 0;
2675 es->selection_start = es->selection_end = 0;
2676 EDIT_EM_EmptyUndoBuffer(wnd, es);
2677 es->flags &= ~EF_MODIFIED;
2678 es->flags &= ~EF_UPDATE;
2679 EDIT_BuildLineDefs_ML(wnd, es);
2680 EDIT_UpdateText(wnd, NULL, TRUE);
2681 EDIT_EM_ScrollCaret(wnd, es);
2685 /*********************************************************************
2687 * EM_SETLIMITTEXT
2689 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
2690 * However, the windows version is not complied to yet in all of edit.c
2693 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit)
2695 if (es->style & ES_MULTILINE) {
2696 if (limit)
2697 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
2698 else
2699 es->buffer_limit = BUFLIMIT_MULTI;
2700 } else {
2701 if (limit)
2702 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
2703 else
2704 es->buffer_limit = BUFLIMIT_SINGLE;
2709 /*********************************************************************
2711 * EM_SETMARGINS
2713 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2714 * action wParam despite what the docs say. EC_USEFONTINFO means one third
2715 * of the char's width, according to the new docs.
2718 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action,
2719 INT left, INT right)
2721 if (action & EC_LEFTMARGIN) {
2722 if (left != EC_USEFONTINFO)
2723 es->left_margin = left;
2724 else
2725 es->left_margin = es->char_width / 3;
2728 if (action & EC_RIGHTMARGIN) {
2729 if (right != EC_USEFONTINFO)
2730 es->right_margin = right;
2731 else
2732 es->right_margin = es->char_width / 3;
2734 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2738 /*********************************************************************
2740 * EM_SETPASSWORDCHAR
2743 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c)
2745 if (es->style & ES_MULTILINE)
2746 return;
2748 if (es->password_char == c)
2749 return;
2751 es->password_char = c;
2752 if (c) {
2753 wnd->dwStyle |= ES_PASSWORD;
2754 es->style |= ES_PASSWORD;
2755 } else {
2756 wnd->dwStyle &= ~ES_PASSWORD;
2757 es->style &= ~ES_PASSWORD;
2759 EDIT_UpdateText(wnd, NULL, TRUE);
2763 /*********************************************************************
2765 * EDIT_EM_SetSel
2767 * note: unlike the specs say: the order of start and end
2768 * _is_ preserved in Windows. (i.e. start can be > end)
2769 * In other words: this handler is OK
2772 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
2774 UINT old_start = es->selection_start;
2775 UINT old_end = es->selection_end;
2776 UINT len = lstrlenA(es->text);
2778 if (start == -1) {
2779 start = es->selection_end;
2780 end = es->selection_end;
2781 } else {
2782 start = min(start, len);
2783 end = min(end, len);
2785 es->selection_start = start;
2786 es->selection_end = end;
2787 if (after_wrap)
2788 es->flags |= EF_AFTER_WRAP;
2789 else
2790 es->flags &= ~EF_AFTER_WRAP;
2791 if (es->flags & EF_FOCUSED)
2792 EDIT_SetCaretPos(wnd, es, end, after_wrap);
2793 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
2794 ORDER_UINT(start, end);
2795 ORDER_UINT(end, old_end);
2796 ORDER_UINT(start, old_start);
2797 ORDER_UINT(old_start, old_end);
2798 if (end != old_start)
2801 * One can also do
2802 * ORDER_UINT32(end, old_start);
2803 * EDIT_InvalidateText(wnd, es, start, end);
2804 * EDIT_InvalidateText(wnd, es, old_start, old_end);
2805 * in place of the following if statement.
2807 if (old_start > end )
2809 EDIT_InvalidateText(wnd, es, start, end);
2810 EDIT_InvalidateText(wnd, es, old_start, old_end);
2812 else
2814 EDIT_InvalidateText(wnd, es, start, old_start);
2815 EDIT_InvalidateText(wnd, es, end, old_end);
2818 else EDIT_InvalidateText(wnd, es, start, old_end);
2822 /*********************************************************************
2824 * EM_SETTABSTOPS
2827 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs)
2829 if (!(es->style & ES_MULTILINE))
2830 return FALSE;
2831 if (es->tabs)
2832 HeapFree(es->heap, 0, es->tabs);
2833 es->tabs_count = count;
2834 if (!count)
2835 es->tabs = NULL;
2836 else {
2837 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2838 memcpy(es->tabs, tabs, count * sizeof(INT));
2840 return TRUE;
2844 /*********************************************************************
2846 * EM_SETTABSTOPS16
2849 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs)
2851 if (!(es->style & ES_MULTILINE))
2852 return FALSE;
2853 if (es->tabs)
2854 HeapFree(es->heap, 0, es->tabs);
2855 es->tabs_count = count;
2856 if (!count)
2857 es->tabs = NULL;
2858 else {
2859 INT i;
2860 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2861 for (i = 0 ; i < count ; i++)
2862 es->tabs[i] = *tabs++;
2864 return TRUE;
2868 /*********************************************************************
2870 * EM_SETWORDBREAKPROC
2873 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp)
2875 if (es->word_break_proc32A == wbp)
2876 return;
2878 es->word_break_proc32A = wbp;
2879 es->word_break_proc16 = NULL;
2880 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2881 EDIT_BuildLineDefs_ML(wnd, es);
2882 EDIT_UpdateText(wnd, NULL, TRUE);
2887 /*********************************************************************
2889 * EM_SETWORDBREAKPROC16
2892 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
2894 if (es->word_break_proc16 == wbp)
2895 return;
2897 es->word_break_proc32A = NULL;
2898 es->word_break_proc16 = wbp;
2899 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2900 EDIT_BuildLineDefs_ML(wnd, es);
2901 EDIT_UpdateText(wnd, NULL, TRUE);
2906 /*********************************************************************
2908 * EM_UNDO / WM_UNDO
2911 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2913 INT ulength = lstrlenA(es->undo_text);
2914 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2916 lstrcpyA(utext, es->undo_text);
2918 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2919 es->undo_insert_count, utext);
2921 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2922 EDIT_EM_EmptyUndoBuffer(wnd, es);
2923 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext);
2924 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2925 HeapFree(es->heap, 0, utext);
2927 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2928 es->undo_insert_count, es->undo_text);
2930 if (es->flags & EF_UPDATE) {
2931 es->flags &= ~EF_UPDATE;
2932 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
2935 return TRUE;
2939 /*********************************************************************
2941 * WM_CHAR
2944 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data)
2946 BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
2947 switch (c) {
2948 case '\r':
2949 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
2950 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2951 break;
2952 case '\n':
2953 if (es->style & ES_MULTILINE) {
2954 if (es->style & ES_READONLY) {
2955 EDIT_MoveHome(wnd, es, FALSE);
2956 EDIT_MoveDown_ML(wnd, es, FALSE);
2957 } else {
2958 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\r\n");
2959 if (es->flags & EF_UPDATE) {
2960 es->flags &= ~EF_UPDATE;
2961 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
2965 break;
2966 case '\t':
2967 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2969 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\t");
2970 if (es->flags & EF_UPDATE) {
2971 es->flags &= ~EF_UPDATE;
2972 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
2975 break;
2976 case VK_BACK:
2977 if (!(es->style & ES_READONLY) && !control) {
2978 if (es->selection_start != es->selection_end)
2979 EDIT_WM_Clear(wnd, es);
2980 else {
2981 /* delete character left of caret */
2982 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
2983 EDIT_MoveBackward(wnd, es, TRUE);
2984 EDIT_WM_Clear(wnd, es);
2987 break;
2988 case 0x03: /* ^C */
2989 SendMessageA(wnd->hwndSelf, WM_COPY, 0, 0);
2990 break;
2991 case 0x16: /* ^V */
2992 SendMessageA(wnd->hwndSelf, WM_PASTE, 0, 0);
2993 break;
2994 case 0x18: /* ^X */
2995 SendMessageA(wnd->hwndSelf, WM_CUT, 0, 0);
2996 break;
2998 default:
2999 if (!(es->style & ES_READONLY) && ((BYTE)c >= ' ') && (c != 127)) {
3000 char str[2];
3001 str[0] = c;
3002 str[1] = '\0';
3003 EDIT_EM_ReplaceSel(wnd, es, TRUE, str);
3004 if (es->flags & EF_UPDATE) {
3005 es->flags &= ~EF_UPDATE;
3006 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3009 break;
3014 /*********************************************************************
3016 * WM_COMMAND
3019 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3021 if (code || control)
3022 return;
3024 switch (id) {
3025 case EM_UNDO:
3026 EDIT_EM_Undo(wnd, es);
3027 break;
3028 case WM_CUT:
3029 EDIT_WM_Cut(wnd, es);
3030 break;
3031 case WM_COPY:
3032 EDIT_WM_Copy(wnd, es);
3033 break;
3034 case WM_PASTE:
3035 EDIT_WM_Paste(wnd, es);
3036 break;
3037 case WM_CLEAR:
3038 EDIT_WM_Clear(wnd, es);
3039 break;
3040 case EM_SETSEL:
3041 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3042 EDIT_EM_ScrollCaret(wnd, es);
3043 break;
3044 default:
3045 ERR("unknown menu item, please report\n");
3046 break;
3051 /*********************************************************************
3053 * WM_CONTEXTMENU
3055 * Note: the resource files resource/sysres_??.rc cannot define a
3056 * single popup menu. Hence we use a (dummy) menubar
3057 * containing the single popup menu as its first item.
3059 * FIXME: the message identifiers have been chosen arbitrarily,
3060 * hence we use MF_BYPOSITION.
3061 * We might as well use the "real" values (anybody knows ?)
3062 * The menu definition is in resources/sysres_??.rc.
3063 * Once these are OK, we better use MF_BYCOMMAND here
3064 * (as we do in EDIT_WM_Command()).
3067 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y)
3069 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3070 HMENU popup = GetSubMenu(menu, 0);
3071 UINT start = es->selection_start;
3072 UINT end = es->selection_end;
3074 ORDER_UINT(start, end);
3076 /* undo */
3077 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
3078 /* cut */
3079 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3080 /* copy */
3081 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3082 /* paste */
3083 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
3084 /* delete */
3085 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3086 /* select all */
3087 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != lstrlenA(es->text)) ? MF_ENABLED : MF_GRAYED));
3089 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3090 DestroyMenu(menu);
3094 /*********************************************************************
3096 * WM_COPY
3099 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3101 INT s = es->selection_start;
3102 INT e = es->selection_end;
3103 HGLOBAL hdst;
3104 LPSTR dst;
3106 if (e == s)
3107 return;
3108 ORDER_INT(s, e);
3109 hdst = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3110 dst = GlobalLock(hdst);
3111 lstrcpynA(dst, es->text + s, e - s + 1);
3112 GlobalUnlock(hdst);
3113 OpenClipboard(wnd->hwndSelf);
3114 EmptyClipboard();
3115 SetClipboardData(CF_TEXT, hdst);
3116 CloseClipboard();
3120 /*********************************************************************
3122 * WM_CREATE
3125 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs)
3128 * To initialize some final structure members, we call some helper
3129 * functions. However, since the EDITSTATE is not consistent (i.e.
3130 * not fully initialized), we should be very careful which
3131 * functions can be called, and in what order.
3133 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3134 EDIT_EM_EmptyUndoBuffer(wnd, es);
3136 if (cs->lpszName && *(cs->lpszName) != '\0') {
3137 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3138 /* if we insert text to the editline, the text scrolls out
3139 * of the window, as the caret is placed after the insert
3140 * pos normally; thus we reset es->selection... to 0 and
3141 * update caret
3143 es->selection_start = es->selection_end = 0;
3144 EDIT_EM_ScrollCaret(wnd, es);
3145 if (es->flags & EF_UPDATE) {
3146 es->flags &= ~EF_UPDATE;
3147 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3150 return 0;
3154 /*********************************************************************
3156 * WM_DESTROY
3159 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3161 if (es->hloc32) {
3162 while (LocalUnlock(es->hloc32)) ;
3163 LocalFree(es->hloc32);
3165 if (es->hloc16) {
3166 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3167 LOCAL_Free(wnd->hInstance, es->hloc16);
3169 HeapDestroy(es->heap);
3170 HeapFree(GetProcessHeap(), 0, es);
3171 *(EDITSTATE **)wnd->wExtra = NULL;
3175 /*********************************************************************
3177 * WM_ERASEBKGND
3180 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3182 HBRUSH brush;
3183 RECT rc;
3185 if ( VERSION_AppWinVer() >= 0x40000 &&(
3186 !es->bEnableState || (es->style & ES_READONLY)))
3187 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3188 else
3189 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3191 if (!brush)
3192 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3194 GetClientRect(wnd->hwndSelf, &rc);
3195 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3196 GetClipBox(dc, &rc);
3198 * FIXME: specs say that we should UnrealizeObject() the brush,
3199 * but the specs of UnrealizeObject() say that we shouldn't
3200 * unrealize a stock object. The default brush that
3201 * DefWndProc() returns is ... a stock object.
3203 FillRect(dc, &rc, brush);
3204 return -1;
3208 /*********************************************************************
3210 * WM_GETTEXT
3213 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text)
3215 lstrcpynA(text, es->text, count);
3216 return strlen(text);
3220 /*********************************************************************
3222 * EDIT_HScroll_Hack
3224 * 16 bit notepad needs this. Actually it is not _our_ hack,
3225 * it is notepad's. Notepad is sending us scrollbar messages with
3226 * undocumented parameters without us even having a scrollbar ... !?!?
3229 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3231 INT dx = 0;
3232 INT fw = es->format_rect.right - es->format_rect.left;
3233 LRESULT ret = 0;
3235 if (!(es->flags & EF_HSCROLL_HACK)) {
3236 ERR("hacked WM_HSCROLL handler invoked\n");
3237 ERR(" if you are _not_ running 16 bit notepad, please report\n");
3238 ERR(" (this message is only displayed once per edit control)\n");
3239 es->flags |= EF_HSCROLL_HACK;
3242 switch (action) {
3243 case SB_LINELEFT:
3244 if (es->x_offset)
3245 dx = -es->char_width;
3246 break;
3247 case SB_LINERIGHT:
3248 if (es->x_offset < es->text_width)
3249 dx = es->char_width;
3250 break;
3251 case SB_PAGELEFT:
3252 if (es->x_offset)
3253 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3254 break;
3255 case SB_PAGERIGHT:
3256 if (es->x_offset < es->text_width)
3257 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3258 break;
3259 case SB_LEFT:
3260 if (es->x_offset)
3261 dx = -es->x_offset;
3262 break;
3263 case SB_RIGHT:
3264 if (es->x_offset < es->text_width)
3265 dx = es->text_width - es->x_offset;
3266 break;
3267 case SB_THUMBTRACK:
3268 es->flags |= EF_HSCROLL_TRACK;
3269 dx = pos * es->text_width / 100 - es->x_offset;
3270 break;
3271 case SB_THUMBPOSITION:
3272 es->flags &= ~EF_HSCROLL_TRACK;
3273 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3274 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3275 break;
3276 case SB_ENDSCROLL:
3277 break;
3280 * FIXME : the next two are undocumented !
3281 * Are we doing the right thing ?
3282 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3283 * although it's also a regular control message.
3285 case EM_GETTHUMB16:
3286 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3287 break;
3288 case EM_LINESCROLL16:
3289 dx = pos;
3290 break;
3292 default:
3293 ERR("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3294 return 0;
3296 if (dx)
3297 EDIT_EM_LineScroll(wnd, es, dx, 0);
3298 return ret;
3302 /*********************************************************************
3304 * WM_HSCROLL
3307 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3309 INT dx;
3310 INT fw;
3312 if (!(es->style & ES_MULTILINE))
3313 return 0;
3315 if (!(es->style & ES_AUTOHSCROLL))
3316 return 0;
3318 if (!(es->style & WS_HSCROLL))
3319 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3321 dx = 0;
3322 fw = es->format_rect.right - es->format_rect.left;
3323 switch (action) {
3324 case SB_LINELEFT:
3325 if (es->x_offset)
3326 dx = -es->char_width;
3327 break;
3328 case SB_LINERIGHT:
3329 if (es->x_offset < es->text_width)
3330 dx = es->char_width;
3331 break;
3332 case SB_PAGELEFT:
3333 if (es->x_offset)
3334 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3335 break;
3336 case SB_PAGERIGHT:
3337 if (es->x_offset < es->text_width)
3338 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3339 break;
3340 case SB_LEFT:
3341 if (es->x_offset)
3342 dx = -es->x_offset;
3343 break;
3344 case SB_RIGHT:
3345 if (es->x_offset < es->text_width)
3346 dx = es->text_width - es->x_offset;
3347 break;
3348 case SB_THUMBTRACK:
3349 es->flags |= EF_HSCROLL_TRACK;
3350 dx = pos - es->x_offset;
3351 break;
3352 case SB_THUMBPOSITION:
3353 es->flags &= ~EF_HSCROLL_TRACK;
3354 if (!(dx = pos - es->x_offset)) {
3355 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3356 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3358 break;
3359 case SB_ENDSCROLL:
3360 break;
3362 default:
3363 ERR("undocumented WM_HSCROLL parameter, please report\n");
3364 return 0;
3366 if (dx)
3367 EDIT_EM_LineScroll(wnd, es, dx, 0);
3368 return 0;
3372 /*********************************************************************
3374 * EDIT_CheckCombo
3377 static BOOL EDIT_CheckCombo(WND *wnd, UINT msg, INT key, DWORD key_data)
3379 HWND hLBox;
3381 /********************************************************************
3382 * This if statement used to check to see if the parent of the
3383 * edit control was a 'combobox' by comparing the ATOM of the parent
3384 * to a table of internal window control ATOMs. However, this check
3385 * would fail if the parent was a superclassed combobox (Although
3386 * having the same basic functionality of a combobox, it has a
3387 * different name and ATOM, thus defeating this check.)
3389 * The safe way to determine if the parent is a combobox is to send it
3390 * a message only a combo box would understand. I send it a message
3391 * CB_GETCOUNT, if I get 0 then the parent is not a combobox -
3392 * return FALSE. If I get > 0, then the parent IS a combobox
3393 * (or sub/super classed derrivative thereof)
3394 ********************************************************************/
3395 if (
3396 ((SendMessageA(wnd->parent->hwndSelf, CB_GETCOUNT, 0, 0)) > 0) &&
3397 (hLBox = COMBO_GetLBWindow(wnd->parent))
3400 HWND hCombo = wnd->parent->hwndSelf;
3401 BOOL bUIFlip = TRUE;
3403 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3404 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3406 switch (msg) {
3407 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3408 if (key != VK_F4) {
3409 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3410 if (SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0))
3411 bUIFlip = FALSE;
3413 if (!bUIFlip)
3414 SendMessageA(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3415 else {
3416 /* make sure ComboLBox pops up */
3417 SendMessageA(hCombo, CB_SETEXTENDEDUI, 0, 0);
3418 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3419 SendMessageA(hCombo, CB_SETEXTENDEDUI, 1, 0);
3421 break;
3422 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3423 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3424 if (bUIFlip) {
3425 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3426 SendMessageA(hCombo, CB_SHOWDROPDOWN, (bUIFlip) ? FALSE : TRUE, 0);
3427 } else
3428 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3429 break;
3431 return TRUE;
3433 return FALSE;
3437 /*********************************************************************
3439 * WM_KEYDOWN
3441 * Handling of special keys that don't produce a WM_CHAR
3442 * (i.e. non-printable keys) & Backspace & Delete
3445 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3447 BOOL shift;
3448 BOOL control;
3450 if (GetKeyState(VK_MENU) & 0x8000)
3451 return 0;
3453 shift = GetKeyState(VK_SHIFT) & 0x8000;
3454 control = GetKeyState(VK_CONTROL) & 0x8000;
3456 switch (key) {
3457 case VK_F4:
3458 case VK_UP:
3459 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3460 break;
3461 if (key == VK_F4)
3462 break;
3463 /* fall through */
3464 case VK_LEFT:
3465 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3466 EDIT_MoveUp_ML(wnd, es, shift);
3467 else
3468 if (control)
3469 EDIT_MoveWordBackward(wnd, es, shift);
3470 else
3471 EDIT_MoveBackward(wnd, es, shift);
3472 break;
3473 case VK_DOWN:
3474 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3475 break;
3476 /* fall through */
3477 case VK_RIGHT:
3478 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3479 EDIT_MoveDown_ML(wnd, es, shift);
3480 else if (control)
3481 EDIT_MoveWordForward(wnd, es, shift);
3482 else
3483 EDIT_MoveForward(wnd, es, shift);
3484 break;
3485 case VK_HOME:
3486 EDIT_MoveHome(wnd, es, shift);
3487 break;
3488 case VK_END:
3489 EDIT_MoveEnd(wnd, es, shift);
3490 break;
3491 case VK_PRIOR:
3492 if (es->style & ES_MULTILINE)
3493 EDIT_MovePageUp_ML(wnd, es, shift);
3494 break;
3495 case VK_NEXT:
3496 if (es->style & ES_MULTILINE)
3497 EDIT_MovePageDown_ML(wnd, es, shift);
3498 break;
3499 case VK_DELETE:
3500 if (!(es->style & ES_READONLY) && !(shift && control)) {
3501 if (es->selection_start != es->selection_end) {
3502 if (shift)
3503 EDIT_WM_Cut(wnd, es);
3504 else
3505 EDIT_WM_Clear(wnd, es);
3506 } else {
3507 if (shift) {
3508 /* delete character left of caret */
3509 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3510 EDIT_MoveBackward(wnd, es, TRUE);
3511 EDIT_WM_Clear(wnd, es);
3512 } else if (control) {
3513 /* delete to end of line */
3514 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3515 EDIT_MoveEnd(wnd, es, TRUE);
3516 EDIT_WM_Clear(wnd, es);
3517 } else {
3518 /* delete character right of caret */
3519 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3520 EDIT_MoveForward(wnd, es, TRUE);
3521 EDIT_WM_Clear(wnd, es);
3525 break;
3526 case VK_INSERT:
3527 if (shift) {
3528 if (!(es->style & ES_READONLY))
3529 EDIT_WM_Paste(wnd, es);
3530 } else if (control)
3531 EDIT_WM_Copy(wnd, es);
3532 break;
3533 case VK_RETURN:
3534 /* If the edit doesn't want the return send a message to the default object */
3535 if(!(es->style & ES_WANTRETURN))
3537 HWND hwndParent = GetParent(wnd->hwndSelf);
3538 DWORD dw = SendMessage16( hwndParent, DM_GETDEFID, 0, 0 );
3539 if (HIWORD(dw) == DC_HASDEFID)
3541 SendMessageA( hwndParent, WM_COMMAND,
3542 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3543 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3546 break;
3548 return 0;
3552 /*********************************************************************
3554 * WM_KILLFOCUS
3557 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus)
3559 es->flags &= ~EF_FOCUSED;
3560 DestroyCaret();
3561 if(!(es->style & ES_NOHIDESEL))
3562 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3563 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3564 return 0;
3568 /*********************************************************************
3570 * WM_LBUTTONDBLCLK
3572 * The caret position has been set on the WM_LBUTTONDOWN message
3575 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3577 INT s;
3578 INT e = es->selection_end;
3579 INT l;
3580 INT li;
3581 INT ll;
3583 if (!(es->flags & EF_FOCUSED))
3584 return 0;
3586 l = EDIT_EM_LineFromChar(wnd, es, e);
3587 li = EDIT_EM_LineIndex(wnd, es, l);
3588 ll = EDIT_EM_LineLength(wnd, es, e);
3589 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3590 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3591 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3592 EDIT_EM_ScrollCaret(wnd, es);
3593 return 0;
3597 /*********************************************************************
3599 * WM_LBUTTONDOWN
3602 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3604 INT e;
3605 BOOL after_wrap;
3607 if (!(es->flags & EF_FOCUSED))
3608 return 0;
3610 es->bCaptureState = TRUE;
3611 SetCapture(wnd->hwndSelf);
3612 EDIT_ConfinePoint(wnd, es, &x, &y);
3613 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3614 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3615 EDIT_EM_ScrollCaret(wnd, es);
3616 es->region_posx = es->region_posy = 0;
3617 SetTimer(wnd->hwndSelf, 0, 100, NULL);
3618 return 0;
3622 /*********************************************************************
3624 * WM_LBUTTONUP
3627 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3629 if (es->bCaptureState && GetCapture() == wnd->hwndSelf) {
3630 KillTimer(wnd->hwndSelf, 0);
3631 ReleaseCapture();
3633 es->bCaptureState = FALSE;
3634 return 0;
3638 /*********************************************************************
3640 * WM_MBUTTONDOWN
3643 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
3645 SendMessageA(wnd->hwndSelf,WM_PASTE,0,0);
3646 return 0;
3650 /*********************************************************************
3652 * WM_MOUSEMOVE
3655 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3657 INT e;
3658 BOOL after_wrap;
3659 INT prex, prey;
3661 if (GetCapture() != wnd->hwndSelf)
3662 return 0;
3665 * FIXME: gotta do some scrolling if outside client
3666 * area. Maybe reset the timer ?
3668 prex = x; prey = y;
3669 EDIT_ConfinePoint(wnd, es, &x, &y);
3670 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3671 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3672 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3673 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3674 return 0;
3678 /*********************************************************************
3680 * WM_NCCREATE
3683 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs)
3685 EDITSTATE *es;
3687 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3688 return FALSE;
3689 *(EDITSTATE **)wnd->wExtra = es;
3692 * Note: since the EDITSTATE has not been fully initialized yet,
3693 * we can't use any API calls that may send
3694 * WM_XXX messages before WM_NCCREATE is completed.
3697 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3698 return FALSE;
3699 es->style = cs->style;
3701 es->bEnableState = !(cs->style & WS_DISABLED);
3704 * In Win95 look and feel, the WS_BORDER style is replaced by the
3705 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
3706 * control a non client area.
3708 if (TWEAK_WineLook != WIN31_LOOK)
3710 if (es->style & WS_BORDER)
3712 es->style &= ~WS_BORDER;
3713 wnd->dwStyle &= ~WS_BORDER;
3714 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
3717 else
3719 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3720 wnd->dwStyle &= ~WS_BORDER;
3723 if (es->style & ES_MULTILINE) {
3724 es->buffer_size = BUFSTART_MULTI;
3725 es->buffer_limit = BUFLIMIT_MULTI;
3726 if (es->style & WS_VSCROLL)
3727 es->style |= ES_AUTOVSCROLL;
3728 if (es->style & WS_HSCROLL)
3729 es->style |= ES_AUTOHSCROLL;
3730 es->style &= ~ES_PASSWORD;
3731 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3732 if (es->style & ES_RIGHT)
3733 es->style &= ~ES_CENTER;
3734 es->style &= ~WS_HSCROLL;
3735 es->style &= ~ES_AUTOHSCROLL;
3738 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3739 es->style |= ES_AUTOVSCROLL;
3740 } else {
3741 es->buffer_size = BUFSTART_SINGLE;
3742 es->buffer_limit = BUFLIMIT_SINGLE;
3743 es->style &= ~ES_CENTER;
3744 es->style &= ~ES_RIGHT;
3745 es->style &= ~WS_HSCROLL;
3746 es->style &= ~WS_VSCROLL;
3747 es->style &= ~ES_AUTOVSCROLL;
3748 es->style &= ~ES_WANTRETURN;
3749 if (es->style & ES_UPPERCASE) {
3750 es->style &= ~ES_LOWERCASE;
3751 es->style &= ~ES_NUMBER;
3752 } else if (es->style & ES_LOWERCASE)
3753 es->style &= ~ES_NUMBER;
3754 if (es->style & ES_PASSWORD)
3755 es->password_char = '*';
3757 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3758 es->style |= ES_AUTOHSCROLL;
3760 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3761 return FALSE;
3762 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3763 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3764 return FALSE;
3765 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3766 *es->text = '\0';
3767 if (es->style & ES_MULTILINE)
3768 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3769 return FALSE;
3770 es->line_count = 1;
3772 return TRUE;
3775 /*********************************************************************
3777 * WM_PAINT
3780 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
3782 PAINTSTRUCT ps;
3783 INT i;
3784 HDC dc;
3785 HFONT old_font = 0;
3786 RECT rc;
3787 RECT rcLine;
3788 RECT rcRgn;
3789 BOOL rev = es->bEnableState &&
3790 ((es->flags & EF_FOCUSED) ||
3791 (es->style & ES_NOHIDESEL));
3792 if (!wParam)
3793 dc = BeginPaint(wnd->hwndSelf, &ps);
3794 else
3795 dc = (HDC) wParam;
3796 if(es->style & WS_BORDER) {
3797 GetClientRect(wnd->hwndSelf, &rc);
3798 if(es->style & ES_MULTILINE) {
3799 if(es->style & WS_HSCROLL) rc.bottom++;
3800 if(es->style & WS_VSCROLL) rc.right++;
3802 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
3804 IntersectClipRect(dc, es->format_rect.left,
3805 es->format_rect.top,
3806 es->format_rect.right,
3807 es->format_rect.bottom);
3808 if (es->style & ES_MULTILINE) {
3809 GetClientRect(wnd->hwndSelf, &rc);
3810 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3812 if (es->font)
3813 old_font = SelectObject(dc, es->font);
3814 if ( VERSION_AppWinVer() >= 0x40000 &&(
3815 !es->bEnableState || (es->style & ES_READONLY)))
3816 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3817 else
3818 EDIT_SEND_CTLCOLOR(wnd, dc);
3820 if (!es->bEnableState)
3821 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3822 GetClipBox(dc, &rcRgn);
3823 if (es->style & ES_MULTILINE) {
3824 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3825 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3826 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3827 if (IntersectRect(&rc, &rcRgn, &rcLine))
3828 EDIT_PaintLine(wnd, es, dc, i, rev);
3830 } else {
3831 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3832 if (IntersectRect(&rc, &rcRgn, &rcLine))
3833 EDIT_PaintLine(wnd, es, dc, 0, rev);
3835 if (es->font)
3836 SelectObject(dc, old_font);
3837 if (es->flags & EF_FOCUSED)
3838 EDIT_SetCaretPos(wnd, es, es->selection_end,
3839 es->flags & EF_AFTER_WRAP);
3840 if (!wParam)
3841 EndPaint(wnd->hwndSelf, &ps);
3842 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3843 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3844 SCROLLINFO si;
3845 si.cbSize = sizeof(SCROLLINFO);
3846 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3847 si.nMin = 0;
3848 si.nMax = es->line_count + vlc - 2;
3849 si.nPage = vlc;
3850 si.nPos = es->y_offset;
3851 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
3853 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3854 SCROLLINFO si;
3855 INT fw = es->format_rect.right - es->format_rect.left;
3856 si.cbSize = sizeof(SCROLLINFO);
3857 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3858 si.nMin = 0;
3859 si.nMax = es->text_width + fw - 1;
3860 si.nPage = fw;
3861 si.nPos = es->x_offset;
3862 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3867 /*********************************************************************
3869 * WM_PASTE
3872 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3874 HGLOBAL hsrc;
3875 LPSTR src;
3877 OpenClipboard(wnd->hwndSelf);
3878 if ((hsrc = GetClipboardData(CF_TEXT))) {
3879 src = (LPSTR)GlobalLock(hsrc);
3880 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3881 GlobalUnlock(hsrc);
3883 if (es->flags & EF_UPDATE) {
3884 es->flags &= ~EF_UPDATE;
3885 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3888 CloseClipboard();
3892 /*********************************************************************
3894 * WM_SETFOCUS
3897 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus)
3899 es->flags |= EF_FOCUSED;
3900 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3901 EDIT_SetCaretPos(wnd, es, es->selection_end,
3902 es->flags & EF_AFTER_WRAP);
3903 if(!(es->style & ES_NOHIDESEL))
3904 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3905 ShowCaret(wnd->hwndSelf);
3906 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3910 /*********************************************************************
3912 * WM_SETFONT
3914 * With Win95 look the margins are set to default font value unless
3915 * the system font (font == 0) is being set, in which case they are left
3916 * unchanged.
3919 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
3921 TEXTMETRICA tm;
3922 HDC dc;
3923 HFONT old_font = 0;
3924 RECT r;
3926 es->font = font;
3927 dc = GetDC(wnd->hwndSelf);
3928 if (font)
3929 old_font = SelectObject(dc, font);
3930 GetTextMetricsA(dc, &tm);
3931 es->line_height = tm.tmHeight;
3932 es->char_width = tm.tmAveCharWidth;
3933 if (font)
3934 SelectObject(dc, old_font);
3935 ReleaseDC(wnd->hwndSelf, dc);
3936 if (font && (TWEAK_WineLook > WIN31_LOOK))
3937 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3938 EC_USEFONTINFO, EC_USEFONTINFO);
3940 /* Force the recalculation of the format rect for each font change */
3941 GetClientRect(wnd->hwndSelf, &r);
3942 EDIT_SetRectNP(wnd, es, &r);
3944 if (es->style & ES_MULTILINE)
3945 EDIT_BuildLineDefs_ML(wnd, es);
3947 if (redraw)
3948 EDIT_UpdateText(wnd, NULL, TRUE);
3949 if (es->flags & EF_FOCUSED) {
3950 DestroyCaret();
3951 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3952 EDIT_SetCaretPos(wnd, es, es->selection_end,
3953 es->flags & EF_AFTER_WRAP);
3954 ShowCaret(wnd->hwndSelf);
3959 /*********************************************************************
3961 * WM_SETTEXT
3963 * NOTES
3964 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3965 * The modified flag is reset. No notifications are sent.
3967 * For single-line controls, reception of WM_SETTEXT triggers:
3968 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3971 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3973 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3974 if (text) {
3975 TRACE("\t'%s'\n", text);
3976 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3977 } else {
3978 TRACE("\t<NULL>\n");
3979 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3981 es->x_offset = 0;
3982 if (es->style & ES_MULTILINE) {
3983 es->flags &= ~EF_UPDATE;
3984 } else {
3985 es->flags |= EF_UPDATE;
3987 es->flags &= ~EF_MODIFIED;
3988 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3989 EDIT_EM_ScrollCaret(wnd, es);
3991 if (es->flags & EF_UPDATE) {
3992 es->flags &= ~EF_UPDATE;
3993 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3998 /*********************************************************************
4000 * WM_SIZE
4003 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4005 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4006 RECT rc;
4007 SetRect(&rc, 0, 0, width, height);
4008 EDIT_SetRectNP(wnd, es, &rc);
4009 EDIT_UpdateText(wnd, NULL, TRUE);
4014 /*********************************************************************
4016 * WM_SYSKEYDOWN
4019 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4021 if ((key == VK_BACK) && (key_data & 0x2000)) {
4022 if (EDIT_EM_CanUndo(wnd, es))
4023 EDIT_EM_Undo(wnd, es);
4024 return 0;
4025 } else if (key == VK_UP || key == VK_DOWN)
4026 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
4027 return 0;
4028 return DefWindowProcA(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4032 /*********************************************************************
4034 * WM_TIMER
4037 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc)
4039 if (es->region_posx < 0) {
4040 EDIT_MoveBackward(wnd, es, TRUE);
4041 } else if (es->region_posx > 0) {
4042 EDIT_MoveForward(wnd, es, TRUE);
4045 * FIXME: gotta do some vertical scrolling here, like
4046 * EDIT_EM_LineScroll(wnd, 0, 1);
4051 /*********************************************************************
4053 * EDIT_VScroll_Hack
4055 * 16 bit notepad needs this. Actually it is not _our_ hack,
4056 * it is notepad's. Notepad is sending us scrollbar messages with
4057 * undocumented parameters without us even having a scrollbar ... !?!?
4060 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
4062 INT dy = 0;
4063 LRESULT ret = 0;
4065 if (!(es->flags & EF_VSCROLL_HACK)) {
4066 ERR("hacked WM_VSCROLL handler invoked\n");
4067 ERR(" if you are _not_ running 16 bit notepad, please report\n");
4068 ERR(" (this message is only displayed once per edit control)\n");
4069 es->flags |= EF_VSCROLL_HACK;
4072 switch (action) {
4073 case SB_LINEUP:
4074 case SB_LINEDOWN:
4075 case SB_PAGEUP:
4076 case SB_PAGEDOWN:
4077 EDIT_EM_Scroll(wnd, es, action);
4078 return 0;
4079 case SB_TOP:
4080 dy = -es->y_offset;
4081 break;
4082 case SB_BOTTOM:
4083 dy = es->line_count - 1 - es->y_offset;
4084 break;
4085 case SB_THUMBTRACK:
4086 es->flags |= EF_VSCROLL_TRACK;
4087 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
4088 break;
4089 case SB_THUMBPOSITION:
4090 es->flags &= ~EF_VSCROLL_TRACK;
4091 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
4092 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4093 break;
4094 case SB_ENDSCROLL:
4095 break;
4098 * FIXME : the next two are undocumented !
4099 * Are we doing the right thing ?
4100 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4101 * although it's also a regular control message.
4103 case EM_GETTHUMB16:
4104 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4105 break;
4106 case EM_LINESCROLL16:
4107 dy = pos;
4108 break;
4110 default:
4111 ERR("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4112 return 0;
4114 if (dy)
4115 EDIT_EM_LineScroll(wnd, es, 0, dy);
4116 return ret;
4120 /*********************************************************************
4122 * WM_VSCROLL
4125 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
4127 INT dy;
4129 if (!(es->style & ES_MULTILINE))
4130 return 0;
4132 if (!(es->style & ES_AUTOVSCROLL))
4133 return 0;
4135 if (!(es->style & WS_VSCROLL))
4136 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
4138 dy = 0;
4139 switch (action) {
4140 case SB_LINEUP:
4141 case SB_LINEDOWN:
4142 case SB_PAGEUP:
4143 case SB_PAGEDOWN:
4144 EDIT_EM_Scroll(wnd, es, action);
4145 return 0;
4147 case SB_TOP:
4148 dy = -es->y_offset;
4149 break;
4150 case SB_BOTTOM:
4151 dy = es->line_count - 1 - es->y_offset;
4152 break;
4153 case SB_THUMBTRACK:
4154 es->flags |= EF_VSCROLL_TRACK;
4155 dy = pos - es->y_offset;
4156 break;
4157 case SB_THUMBPOSITION:
4158 es->flags &= ~EF_VSCROLL_TRACK;
4159 if (!(dy = pos - es->y_offset)) {
4160 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4161 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4163 break;
4164 case SB_ENDSCROLL:
4165 break;
4167 default:
4168 ERR("undocumented WM_VSCROLL action %d, please report\n",
4169 action);
4170 return 0;
4172 if (dy)
4173 EDIT_EM_LineScroll(wnd, es, 0, dy);
4174 return 0;
4178 /*********************************************************************
4180 * EDIT_UpdateText
4183 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4185 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4187 /* EF_UPDATE will be turned off in paint */
4188 if (es->flags & EF_UPDATE)
4189 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
4191 InvalidateRect(wnd->hwndSelf, rc, bErase);