Fixed warning.
[wine/gsoc_dplay.git] / controls / edit.c
blob0b23cce4c636ff8c5675926f453ef2e9a6ad295c
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 = (es->style & ES_MULTILINE) ?
724 DLGC_WANTALLKEYS | DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS :
725 DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
726 break;
728 case WM_CHAR:
729 DPRINTF_EDIT_MSG32("WM_CHAR");
730 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
731 break;
733 case WM_CLEAR:
734 DPRINTF_EDIT_MSG32("WM_CLEAR");
735 EDIT_WM_Clear(wnd, es);
736 break;
738 case WM_COMMAND:
739 DPRINTF_EDIT_MSG32("WM_COMMAND");
740 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
741 break;
743 case WM_CONTEXTMENU:
744 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
745 EDIT_WM_ContextMenu(wnd, es, (HWND)wParam, SLOWORD(lParam), SHIWORD(lParam));
746 break;
748 case WM_COPY:
749 DPRINTF_EDIT_MSG32("WM_COPY");
750 EDIT_WM_Copy(wnd, es);
751 break;
753 case WM_CREATE:
754 DPRINTF_EDIT_MSG32("WM_CREATE");
755 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCTA)lParam);
756 break;
758 case WM_CUT:
759 DPRINTF_EDIT_MSG32("WM_CUT");
760 EDIT_WM_Cut(wnd, es);
761 break;
763 case WM_ENABLE:
764 DPRINTF_EDIT_MSG32("WM_ENABLE");
765 es->bEnableState = (BOOL) wParam;
766 EDIT_UpdateText(wnd, NULL, TRUE);
767 break;
769 case WM_ERASEBKGND:
770 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
771 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
772 break;
774 case WM_GETFONT:
775 DPRINTF_EDIT_MSG32("WM_GETFONT");
776 result = (LRESULT)es->font;
777 break;
779 case WM_GETTEXT:
780 DPRINTF_EDIT_MSG32("WM_GETTEXT");
781 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT)wParam, (LPSTR)lParam);
782 break;
784 case WM_GETTEXTLENGTH:
785 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
786 result = lstrlenA(es->text);
787 break;
789 case WM_HSCROLL:
790 DPRINTF_EDIT_MSG32("WM_HSCROLL");
791 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)lParam);
792 break;
794 case WM_KEYDOWN:
795 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
796 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
797 break;
799 case WM_KILLFOCUS:
800 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
801 result = EDIT_WM_KillFocus(wnd, es, (HWND)wParam);
802 break;
804 case WM_LBUTTONDBLCLK:
805 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
806 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
807 break;
809 case WM_LBUTTONDOWN:
810 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
811 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
812 break;
814 case WM_LBUTTONUP:
815 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
816 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
817 break;
819 case WM_MBUTTONDOWN:
820 DPRINTF_EDIT_MSG32("WM_MBUTTONDOWN");
821 result = EDIT_WM_MButtonDown(wnd);
822 break;
824 case WM_MOUSEACTIVATE:
826 * FIXME: maybe DefWindowProc() screws up, but it seems that
827 * modeless dialog boxes need this. If we don't do this, the focus
828 * will _not_ be set by DefWindowProc() for edit controls in a
829 * modeless dialog box ???
831 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
832 SetFocus(wnd->hwndSelf);
833 result = MA_ACTIVATE;
834 break;
836 case WM_MOUSEMOVE:
838 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
840 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
841 break;
843 case WM_PAINT:
844 DPRINTF_EDIT_MSG32("WM_PAINT");
845 EDIT_WM_Paint(wnd, es, wParam);
846 break;
848 case WM_PASTE:
849 DPRINTF_EDIT_MSG32("WM_PASTE");
850 EDIT_WM_Paste(wnd, es);
851 break;
853 case WM_SETFOCUS:
854 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
855 EDIT_WM_SetFocus(wnd, es, (HWND)wParam);
856 break;
858 case WM_SETFONT:
859 DPRINTF_EDIT_MSG32("WM_SETFONT");
860 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
861 break;
863 case WM_SETTEXT:
864 DPRINTF_EDIT_MSG32("WM_SETTEXT");
865 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
866 result = TRUE;
867 break;
869 case WM_SIZE:
870 DPRINTF_EDIT_MSG32("WM_SIZE");
871 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
872 break;
874 case WM_SYSKEYDOWN:
875 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
876 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
877 break;
879 case WM_TIMER:
880 DPRINTF_EDIT_MSG32("WM_TIMER");
881 EDIT_WM_Timer(wnd, es, (INT)wParam, (TIMERPROC)lParam);
882 break;
884 case WM_VSCROLL:
885 DPRINTF_EDIT_MSG32("WM_VSCROLL");
886 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)(lParam));
887 break;
889 case WM_MOUSEWHEEL:
891 short gcWheelDelta = 0;
892 UINT pulScrollLines = 3;
893 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
895 if (wParam & (MK_SHIFT | MK_CONTROL)) {
896 result = DefWindowProcA(hwnd, msg, wParam, lParam);
897 break;
899 gcWheelDelta -= (short) HIWORD(wParam);
900 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
902 int cLineScroll= (int) min((UINT) es->line_count, pulScrollLines);
903 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
904 result = EDIT_EM_LineScroll(wnd, es, 0, cLineScroll);
907 break;
908 default:
909 result = DefWindowProcA(hwnd, msg, wParam, lParam);
910 break;
912 EDIT_UnlockBuffer(wnd, es, FALSE);
913 END:
914 WIN_ReleaseWndPtr(wnd);
915 return result;
920 /*********************************************************************
922 * EDIT_BuildLineDefs_ML
924 * Build linked list of text lines.
925 * Lines can end with '\0' (last line), a character (if it is wrapped),
926 * a soft return '\r\r\n' or a hard return '\r\n'
929 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
931 HDC dc;
932 HFONT old_font = 0;
933 LPSTR start, cp;
934 INT fw;
935 LINEDEF *current_def;
936 LINEDEF **previous_next;
938 current_def = es->first_line_def;
939 do {
940 LINEDEF *next_def = current_def->next;
941 HeapFree(es->heap, 0, current_def);
942 current_def = next_def;
943 } while (current_def);
944 es->line_count = 0;
945 es->text_width = 0;
947 dc = GetDC(wnd->hwndSelf);
948 if (es->font)
949 old_font = SelectObject(dc, es->font);
951 fw = es->format_rect.right - es->format_rect.left;
952 start = es->text;
953 previous_next = &es->first_line_def;
954 do {
955 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
956 current_def->next = NULL;
957 cp = start;
958 while (*cp) {
959 if ((*cp == '\r') && (*(cp + 1) == '\n'))
960 break;
961 cp++;
963 if (!(*cp)) {
964 current_def->ending = END_0;
965 current_def->net_length = lstrlenA(start);
966 } else if ((cp > start) && (*(cp - 1) == '\r')) {
967 current_def->ending = END_SOFT;
968 current_def->net_length = cp - start - 1;
969 } else {
970 current_def->ending = END_HARD;
971 current_def->net_length = cp - start;
973 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
974 start, current_def->net_length,
975 es->tabs_count, es->tabs));
976 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
977 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
978 INT next = 0;
979 INT prev;
980 do {
981 prev = next;
982 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
983 prev + 1, current_def->net_length, WB_RIGHT);
984 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
985 start, next, es->tabs_count, es->tabs));
986 } while (current_def->width <= fw);
987 if (!prev) {
988 next = 0;
989 do {
990 prev = next;
991 next++;
992 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
993 start, next, es->tabs_count, es->tabs));
994 } while (current_def->width <= fw);
995 if (!prev)
996 prev = 1;
998 current_def->net_length = prev;
999 current_def->ending = END_WRAP;
1000 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc, start,
1001 current_def->net_length, es->tabs_count, es->tabs));
1003 switch (current_def->ending) {
1004 case END_SOFT:
1005 current_def->length = current_def->net_length + 3;
1006 break;
1007 case END_HARD:
1008 current_def->length = current_def->net_length + 2;
1009 break;
1010 case END_WRAP:
1011 case END_0:
1012 current_def->length = current_def->net_length;
1013 break;
1015 es->text_width = max(es->text_width, current_def->width);
1016 start += current_def->length;
1017 *previous_next = current_def;
1018 previous_next = &current_def->next;
1019 es->line_count++;
1020 } while (current_def->ending != END_0);
1021 if (es->font)
1022 SelectObject(dc, old_font);
1023 ReleaseDC(wnd->hwndSelf, dc);
1027 /*********************************************************************
1029 * EDIT_CallWordBreakProc
1031 * Call appropriate WordBreakProc (internal or external).
1033 * Note: The "start" argument should always be an index refering
1034 * to es->text. The actual wordbreak proc might be
1035 * 16 bit, so we can't always pass any 32 bit LPSTR.
1036 * Hence we assume that es->text is the buffer that holds
1037 * the string under examination (we can decide this for ourselves).
1040 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action)
1042 if (es->word_break_proc16) {
1043 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
1044 SEGPTR segptr = LocalLock16(hloc16);
1045 INT ret = (INT)Callbacks->CallWordBreakProc(es->word_break_proc16,
1046 segptr + start, index, count, action);
1047 LocalUnlock16(hloc16);
1048 return ret;
1050 else if (es->word_break_proc32A)
1052 TRACE_(relay)("(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
1053 es->word_break_proc32A, es->text + start, index,
1054 count, action );
1055 return (INT)es->word_break_proc32A( es->text + start, index,
1056 count, action );
1058 else
1059 return EDIT_WordBreakProc(es->text + start, index, count, action);
1063 /*********************************************************************
1065 * EDIT_CharFromPos
1067 * Beware: This is not the function called on EM_CHARFROMPOS
1068 * The position _can_ be outside the formatting / client
1069 * rectangle
1070 * The return value is only the character index
1073 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1075 INT index;
1076 HDC dc;
1077 HFONT old_font = 0;
1079 if (es->style & ES_MULTILINE) {
1080 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1081 INT line_index = 0;
1082 LINEDEF *line_def = es->first_line_def;
1083 INT low, high;
1084 while ((line > 0) && line_def->next) {
1085 line_index += line_def->length;
1086 line_def = line_def->next;
1087 line--;
1089 x += es->x_offset - es->format_rect.left;
1090 if (x >= line_def->width) {
1091 if (after_wrap)
1092 *after_wrap = (line_def->ending == END_WRAP);
1093 return line_index + line_def->net_length;
1095 if (x <= 0) {
1096 if (after_wrap)
1097 *after_wrap = FALSE;
1098 return line_index;
1100 dc = GetDC(wnd->hwndSelf);
1101 if (es->font)
1102 old_font = SelectObject(dc, es->font);
1103 low = line_index + 1;
1104 high = line_index + line_def->net_length + 1;
1105 while (low < high - 1)
1107 INT mid = (low + high) / 2;
1108 if (LOWORD(GetTabbedTextExtentA(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1109 else low = mid;
1111 index = low;
1113 if (after_wrap)
1114 *after_wrap = ((index == line_index + line_def->net_length) &&
1115 (line_def->ending == END_WRAP));
1116 } else {
1117 LPSTR text;
1118 SIZE size;
1119 if (after_wrap)
1120 *after_wrap = FALSE;
1121 x -= es->format_rect.left;
1122 if (!x)
1123 return es->x_offset;
1124 text = EDIT_GetPasswordPointer_SL(wnd, es);
1125 dc = GetDC(wnd->hwndSelf);
1126 if (es->font)
1127 old_font = SelectObject(dc, es->font);
1128 if (x < 0)
1130 INT low = 0;
1131 INT high = es->x_offset;
1132 while (low < high - 1)
1134 INT mid = (low + high) / 2;
1135 GetTextExtentPoint32A( dc, text + mid,
1136 es->x_offset - mid, &size );
1137 if (size.cx > -x) low = mid;
1138 else high = mid;
1140 index = low;
1142 else
1144 INT low = es->x_offset;
1145 INT high = lstrlenA(es->text) + 1;
1146 while (low < high - 1)
1148 INT mid = (low + high) / 2;
1149 GetTextExtentPoint32A( dc, text + es->x_offset,
1150 mid - es->x_offset, &size );
1151 if (size.cx > x) high = mid;
1152 else low = mid;
1154 index = low;
1156 if (es->style & ES_PASSWORD)
1157 HeapFree(es->heap, 0 ,text);
1159 if (es->font)
1160 SelectObject(dc, old_font);
1161 ReleaseDC(wnd->hwndSelf, dc);
1162 return index;
1166 /*********************************************************************
1168 * EDIT_ConfinePoint
1170 * adjusts the point to be within the formatting rectangle
1171 * (so CharFromPos returns the nearest _visible_ character)
1174 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y)
1176 *x = min(max(*x, es->format_rect.left), es->format_rect.right - 1);
1177 *y = min(max(*y, es->format_rect.top), es->format_rect.bottom - 1);
1181 /*********************************************************************
1183 * EDIT_GetLineRect
1185 * Calculates the bounding rectangle for a line from a starting
1186 * column to an ending column.
1189 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1191 INT line_index = EDIT_EM_LineIndex(wnd, es, line);
1193 if (es->style & ES_MULTILINE)
1194 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1195 else
1196 rc->top = es->format_rect.top;
1197 rc->bottom = rc->top + es->line_height;
1198 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1199 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1203 /*********************************************************************
1205 * EDIT_GetPasswordPointer_SL
1207 * note: caller should free the (optionally) allocated buffer
1210 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1212 if (es->style & ES_PASSWORD) {
1213 INT len = lstrlenA(es->text);
1214 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1215 RtlFillMemory(text, len, es->password_char);
1216 text[len] = '\0';
1217 return text;
1218 } else
1219 return es->text;
1223 /*********************************************************************
1225 * EDIT_LockBuffer
1227 * This acts as a LOCAL_Lock(), but it locks only once. This way
1228 * you can call it whenever you like, without unlocking.
1231 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1233 if (!es) {
1234 ERR("no EDITSTATE ... please report\n");
1235 return;
1237 if (!(es->style & ES_MULTILINE))
1238 return;
1239 if (!es->text) {
1240 if (es->hloc32)
1241 es->text = LocalLock(es->hloc32);
1242 else if (es->hloc16)
1243 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1244 else {
1245 ERR("no buffer ... please report\n");
1246 return;
1249 es->lock_count++;
1253 /*********************************************************************
1255 * EDIT_SL_InvalidateText
1257 * Called from EDIT_InvalidateText().
1258 * Does the job for single-line controls only.
1261 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1263 RECT line_rect;
1264 RECT rc;
1266 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1267 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1268 EDIT_UpdateText(wnd, &rc, FALSE);
1272 /*********************************************************************
1274 * EDIT_ML_InvalidateText
1276 * Called from EDIT_InvalidateText().
1277 * Does the job for multi-line controls only.
1280 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1282 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1283 INT sl = EDIT_EM_LineFromChar(wnd, es, start);
1284 INT el = EDIT_EM_LineFromChar(wnd, es, end);
1285 INT sc;
1286 INT ec;
1287 RECT rc1;
1288 RECT rcWnd;
1289 RECT rcLine;
1290 RECT rcUpdate;
1291 INT l;
1293 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1294 return;
1296 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1297 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1298 if (sl < es->y_offset) {
1299 sl = es->y_offset;
1300 sc = 0;
1302 if (el > es->y_offset + vlc) {
1303 el = es->y_offset + vlc;
1304 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1306 GetClientRect(wnd->hwndSelf, &rc1);
1307 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1308 if (sl == el) {
1309 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1310 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1311 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1312 } else {
1313 EDIT_GetLineRect(wnd, es, sl, sc,
1314 EDIT_EM_LineLength(wnd, es,
1315 EDIT_EM_LineIndex(wnd, es, sl)),
1316 &rcLine);
1317 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1318 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1319 for (l = sl + 1 ; l < el ; l++) {
1320 EDIT_GetLineRect(wnd, es, l, 0,
1321 EDIT_EM_LineLength(wnd, es,
1322 EDIT_EM_LineIndex(wnd, es, l)),
1323 &rcLine);
1324 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1325 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1327 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1328 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1329 EDIT_UpdateText(wnd, &rcUpdate, FALSE);
1334 /*********************************************************************
1336 * EDIT_InvalidateText
1338 * Invalidate the text from offset start upto, but not including,
1339 * offset end. Useful for (re)painting the selection.
1340 * Regions outside the linewidth are not invalidated.
1341 * end == -1 means end == TextLength.
1342 * start and end need not be ordered.
1345 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1347 if (end == start)
1348 return;
1350 if (end == -1)
1351 end = lstrlenA(es->text);
1353 ORDER_INT(start, end);
1355 if (es->style & ES_MULTILINE)
1356 EDIT_ML_InvalidateText(wnd, es, start, end);
1357 else
1358 EDIT_SL_InvalidateText(wnd, es, start, end);
1362 /*********************************************************************
1364 * EDIT_MakeFit
1366 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1369 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size)
1371 HLOCAL hNew32;
1372 HLOCAL16 hNew16;
1374 if (size <= es->buffer_size)
1375 return TRUE;
1376 if (size > es->buffer_limit) {
1377 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1378 return FALSE;
1380 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1381 if (size > es->buffer_limit)
1382 size = es->buffer_limit;
1384 TRACE("trying to ReAlloc to %d+1\n", size);
1386 EDIT_UnlockBuffer(wnd, es, TRUE);
1387 if (es->text) {
1388 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1389 es->buffer_size = min(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1390 else
1391 es->buffer_size = 0;
1392 } else if (es->hloc32) {
1393 if ((hNew32 = LocalReAlloc(es->hloc32, size + 1, 0))) {
1394 TRACE("Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1395 es->hloc32 = hNew32;
1396 es->buffer_size = min(LocalSize(es->hloc32) - 1, es->buffer_limit);
1398 } else if (es->hloc16) {
1399 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1400 TRACE("Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1401 es->hloc16 = hNew16;
1402 es->buffer_size = min(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1405 if (es->buffer_size < size) {
1406 EDIT_LockBuffer(wnd, es);
1407 WARN("FAILED ! We now have %d+1\n", es->buffer_size);
1408 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1409 return FALSE;
1410 } else {
1411 EDIT_LockBuffer(wnd, es);
1412 TRACE("We now have %d+1\n", es->buffer_size);
1413 return TRUE;
1418 /*********************************************************************
1420 * EDIT_MakeUndoFit
1422 * Try to fit size + 1 bytes in the undo buffer.
1425 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size)
1427 if (size <= es->undo_buffer_size)
1428 return TRUE;
1429 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1431 TRACE("trying to ReAlloc to %d+1\n", size);
1433 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1434 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1435 if (es->undo_buffer_size < size) {
1436 WARN("FAILED ! We now have %d+1\n", es->undo_buffer_size);
1437 return FALSE;
1439 return TRUE;
1441 return FALSE;
1445 /*********************************************************************
1447 * EDIT_MoveBackward
1450 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1452 INT e = es->selection_end;
1454 if (e) {
1455 e--;
1456 if ((es->style & ES_MULTILINE) && e &&
1457 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1458 e--;
1459 if (e && (es->text[e - 1] == '\r'))
1460 e--;
1463 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1464 EDIT_EM_ScrollCaret(wnd, es);
1468 /*********************************************************************
1470 * EDIT_MoveDown_ML
1472 * Only for multi line controls
1473 * Move the caret one line down, on a column with the nearest
1474 * x coordinate on the screen (might be a different column).
1477 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1479 INT s = es->selection_start;
1480 INT e = es->selection_end;
1481 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1482 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1483 INT x = SLOWORD(pos);
1484 INT y = SHIWORD(pos);
1486 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1487 if (!extend)
1488 s = e;
1489 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1490 EDIT_EM_ScrollCaret(wnd, es);
1494 /*********************************************************************
1496 * EDIT_MoveEnd
1499 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1501 BOOL after_wrap = FALSE;
1502 INT e;
1504 /* Pass a high value in x to make sure of receiving the en of the line */
1505 if (es->style & ES_MULTILINE)
1506 e = EDIT_CharFromPos(wnd, es, 0x3fffffff,
1507 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1508 else
1509 e = lstrlenA(es->text);
1510 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1511 EDIT_EM_ScrollCaret(wnd, es);
1515 /*********************************************************************
1517 * EDIT_MoveForward
1520 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1522 INT e = es->selection_end;
1524 if (es->text[e]) {
1525 e++;
1526 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1527 if (es->text[e] == '\n')
1528 e++;
1529 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1530 e += 2;
1533 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1534 EDIT_EM_ScrollCaret(wnd, es);
1538 /*********************************************************************
1540 * EDIT_MoveHome
1542 * Home key: move to beginning of line.
1545 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1547 INT e;
1549 /* Pass the x_offset in x to make sure of receiving the first position of the line */
1550 if (es->style & ES_MULTILINE)
1551 e = EDIT_CharFromPos(wnd, es, -es->x_offset,
1552 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1553 else
1554 e = 0;
1555 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1556 EDIT_EM_ScrollCaret(wnd, es);
1560 /*********************************************************************
1562 * EDIT_MovePageDown_ML
1564 * Only for multi line controls
1565 * Move the caret one page down, on a column with the nearest
1566 * x coordinate on the screen (might be a different column).
1569 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1571 INT s = es->selection_start;
1572 INT e = es->selection_end;
1573 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1574 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1575 INT x = SLOWORD(pos);
1576 INT y = SHIWORD(pos);
1578 e = EDIT_CharFromPos(wnd, es, x,
1579 y + (es->format_rect.bottom - es->format_rect.top),
1580 &after_wrap);
1581 if (!extend)
1582 s = e;
1583 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1584 EDIT_EM_ScrollCaret(wnd, es);
1588 /*********************************************************************
1590 * EDIT_MovePageUp_ML
1592 * Only for multi line controls
1593 * Move the caret one page up, on a column with the nearest
1594 * x coordinate on the screen (might be a different column).
1597 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1599 INT s = es->selection_start;
1600 INT e = es->selection_end;
1601 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1602 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1603 INT x = SLOWORD(pos);
1604 INT y = SHIWORD(pos);
1606 e = EDIT_CharFromPos(wnd, es, x,
1607 y - (es->format_rect.bottom - es->format_rect.top),
1608 &after_wrap);
1609 if (!extend)
1610 s = e;
1611 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1612 EDIT_EM_ScrollCaret(wnd, es);
1616 /*********************************************************************
1618 * EDIT_MoveUp_ML
1620 * Only for multi line controls
1621 * Move the caret one line up, on a column with the nearest
1622 * x coordinate on the screen (might be a different column).
1625 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1627 INT s = es->selection_start;
1628 INT e = es->selection_end;
1629 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1630 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1631 INT x = SLOWORD(pos);
1632 INT y = SHIWORD(pos);
1634 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1635 if (!extend)
1636 s = e;
1637 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1638 EDIT_EM_ScrollCaret(wnd, es);
1642 /*********************************************************************
1644 * EDIT_MoveWordBackward
1647 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1649 INT s = es->selection_start;
1650 INT e = es->selection_end;
1651 INT l;
1652 INT ll;
1653 INT li;
1655 l = EDIT_EM_LineFromChar(wnd, es, e);
1656 ll = EDIT_EM_LineLength(wnd, es, e);
1657 li = EDIT_EM_LineIndex(wnd, es, l);
1658 if (e - li == 0) {
1659 if (l) {
1660 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1661 e = li + EDIT_EM_LineLength(wnd, es, li);
1663 } else {
1664 e = li + (INT)EDIT_CallWordBreakProc(wnd, es,
1665 li, e - li, ll, WB_LEFT);
1667 if (!extend)
1668 s = e;
1669 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1670 EDIT_EM_ScrollCaret(wnd, es);
1674 /*********************************************************************
1676 * EDIT_MoveWordForward
1679 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1681 INT s = es->selection_start;
1682 INT e = es->selection_end;
1683 INT l;
1684 INT ll;
1685 INT li;
1687 l = EDIT_EM_LineFromChar(wnd, es, e);
1688 ll = EDIT_EM_LineLength(wnd, es, e);
1689 li = EDIT_EM_LineIndex(wnd, es, l);
1690 if (e - li == ll) {
1691 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1692 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1693 } else {
1694 e = li + EDIT_CallWordBreakProc(wnd, es,
1695 li, e - li + 1, ll, WB_RIGHT);
1697 if (!extend)
1698 s = e;
1699 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1700 EDIT_EM_ScrollCaret(wnd, es);
1704 /*********************************************************************
1706 * EDIT_PaintLine
1709 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1711 INT s = es->selection_start;
1712 INT e = es->selection_end;
1713 INT li;
1714 INT ll;
1715 INT x;
1716 INT y;
1717 LRESULT pos;
1719 if (es->style & ES_MULTILINE) {
1720 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1721 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1722 return;
1723 } else if (line)
1724 return;
1726 TRACE("line=%d\n", line);
1728 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1729 x = SLOWORD(pos);
1730 y = SHIWORD(pos);
1731 li = EDIT_EM_LineIndex(wnd, es, line);
1732 ll = EDIT_EM_LineLength(wnd, es, li);
1733 s = es->selection_start;
1734 e = es->selection_end;
1735 ORDER_INT(s, e);
1736 s = min(li + ll, max(li, s));
1737 e = min(li + ll, max(li, e));
1738 if (rev && (s != e) &&
1739 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1740 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1741 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1742 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1743 } else
1744 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1748 /*********************************************************************
1750 * EDIT_PaintText
1753 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1755 COLORREF BkColor;
1756 COLORREF TextColor;
1757 INT ret;
1758 INT li;
1759 SIZE size;
1761 if (!count)
1762 return 0;
1763 BkColor = GetBkColor(dc);
1764 TextColor = GetTextColor(dc);
1765 if (rev) {
1766 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
1767 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1769 li = EDIT_EM_LineIndex(wnd, es, line);
1770 if (es->style & ES_MULTILINE) {
1771 ret = (INT)LOWORD(TabbedTextOutA(dc, x, y, es->text + li + col, count,
1772 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1773 } else {
1774 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1775 TextOutA(dc, x, y, text + li + col, count);
1776 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1777 ret = size.cx;
1778 if (es->style & ES_PASSWORD)
1779 HeapFree(es->heap, 0, text);
1781 if (rev) {
1782 SetBkColor(dc, BkColor);
1783 SetTextColor(dc, TextColor);
1785 return ret;
1789 /*********************************************************************
1791 * EDIT_SetCaretPos
1794 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
1795 BOOL after_wrap)
1797 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1798 INT x = SLOWORD(res);
1799 INT y = SHIWORD(res);
1801 if(x < es->format_rect.left)
1802 x = es->format_rect.left;
1803 if(x > es->format_rect.right - 2)
1804 x = es->format_rect.right - 2;
1805 if(y > es->format_rect.bottom)
1806 y = es->format_rect.bottom;
1807 if(y < es->format_rect.top)
1808 y = es->format_rect.top;
1809 SetCaretPos(x, y);
1810 return;
1814 /*********************************************************************
1816 * EDIT_SetRectNP
1818 * note: this is not (exactly) the handler called on EM_SETRECTNP
1819 * it is also used to set the rect of a single line control
1822 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
1824 CopyRect(&es->format_rect, rc);
1825 if (es->style & WS_BORDER) {
1826 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
1827 if(TWEAK_WineLook == WIN31_LOOK)
1828 bw += 2;
1829 es->format_rect.left += bw;
1830 es->format_rect.top += bw;
1831 es->format_rect.right -= bw;
1832 es->format_rect.bottom -= bw;
1834 es->format_rect.left += es->left_margin;
1835 es->format_rect.right -= es->right_margin;
1836 es->format_rect.right = max(es->format_rect.right, es->format_rect.left + es->char_width);
1837 if (es->style & ES_MULTILINE)
1838 es->format_rect.bottom = es->format_rect.top +
1839 max(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1840 else
1841 es->format_rect.bottom = es->format_rect.top + es->line_height;
1842 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1843 EDIT_BuildLineDefs_ML(wnd, es);
1847 /*********************************************************************
1849 * EDIT_UnlockBuffer
1852 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
1854 if (!es) {
1855 ERR("no EDITSTATE ... please report\n");
1856 return;
1858 if (!(es->style & ES_MULTILINE))
1859 return;
1860 if (!es->lock_count) {
1861 ERR("lock_count == 0 ... please report\n");
1862 return;
1864 if (!es->text) {
1865 ERR("es->text == 0 ... please report\n");
1866 return;
1868 if (force || (es->lock_count == 1)) {
1869 if (es->hloc32) {
1870 LocalUnlock(es->hloc32);
1871 es->text = NULL;
1872 } else if (es->hloc16) {
1873 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1874 es->text = NULL;
1877 es->lock_count--;
1881 /*********************************************************************
1883 * EDIT_WordBreakProc
1885 * Find the beginning of words.
1886 * Note: unlike the specs for a WordBreakProc, this function only
1887 * allows to be called without linebreaks between s[0] upto
1888 * s[count - 1]. Remember it is only called
1889 * internally, so we can decide this for ourselves.
1892 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action)
1894 INT ret = 0;
1896 TRACE("s=%p, index=%u, count=%u, action=%d\n",
1897 s, index, count, action);
1899 switch (action) {
1900 case WB_LEFT:
1901 if (!count)
1902 break;
1903 if (index)
1904 index--;
1905 if (s[index] == ' ') {
1906 while (index && (s[index] == ' '))
1907 index--;
1908 if (index) {
1909 while (index && (s[index] != ' '))
1910 index--;
1911 if (s[index] == ' ')
1912 index++;
1914 } else {
1915 while (index && (s[index] != ' '))
1916 index--;
1917 if (s[index] == ' ')
1918 index++;
1920 ret = index;
1921 break;
1922 case WB_RIGHT:
1923 if (!count)
1924 break;
1925 if (index)
1926 index--;
1927 if (s[index] == ' ')
1928 while ((index < count) && (s[index] == ' ')) index++;
1929 else {
1930 while (s[index] && (s[index] != ' ') && (index < count))
1931 index++;
1932 while ((s[index] == ' ') && (index < count)) index++;
1934 ret = index;
1935 break;
1936 case WB_ISDELIMITER:
1937 ret = (s[index] == ' ');
1938 break;
1939 default:
1940 ERR("unknown action code, please report !\n");
1941 break;
1943 return ret;
1947 /*********************************************************************
1949 * EM_CHARFROMPOS
1951 * returns line number (not index) in high-order word of result.
1952 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
1953 * to Richedit, not to the edit control. Original documentation is valid.
1954 * FIXME: do the specs mean to return -1 if outside client area or
1955 * if outside formatting rectangle ???
1958 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
1960 POINT pt;
1961 RECT rc;
1962 INT index;
1964 pt.x = x;
1965 pt.y = y;
1966 GetClientRect(wnd->hwndSelf, &rc);
1967 if (!PtInRect(&rc, pt))
1968 return -1;
1970 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1971 return MAKELONG(index, EDIT_EM_LineFromChar(wnd, es, index));
1975 /*********************************************************************
1977 * EM_FMTLINES
1979 * Enable or disable soft breaks.
1981 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol)
1983 es->flags &= ~EF_USE_SOFTBRK;
1984 if (add_eol) {
1985 es->flags |= EF_USE_SOFTBRK;
1986 FIXME("soft break enabled, not implemented\n");
1988 return add_eol;
1992 /*********************************************************************
1994 * EM_GETHANDLE
1996 * Hopefully this won't fire back at us.
1997 * We always start with a fixed buffer in our own heap.
1998 * However, with this message a 32 bit application requests
1999 * a handle to 32 bit moveable local heap memory, where it expects
2000 * to find the text.
2001 * It's a pity that from this moment on we have to use this
2002 * local heap, because applications may rely on the handle
2003 * in the future.
2005 * In this function we'll try to switch to local heap.
2008 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
2010 HLOCAL newBuf;
2011 LPSTR newText;
2012 INT newSize;
2014 if (!(es->style & ES_MULTILINE))
2015 return 0;
2017 if (es->hloc32)
2018 return es->hloc32;
2019 else if (es->hloc16)
2020 return (HLOCAL)es->hloc16;
2022 if (!(newBuf = LocalAlloc(LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2023 ERR("could not allocate new 32 bit buffer\n");
2024 return 0;
2026 newSize = min(LocalSize(newBuf) - 1, es->buffer_limit);
2027 if (!(newText = LocalLock(newBuf))) {
2028 ERR("could not lock new 32 bit buffer\n");
2029 LocalFree(newBuf);
2030 return 0;
2032 lstrcpyA(newText, es->text);
2033 EDIT_UnlockBuffer(wnd, es, TRUE);
2034 if (es->text)
2035 HeapFree(es->heap, 0, es->text);
2036 es->hloc32 = newBuf;
2037 es->hloc16 = (HLOCAL16)NULL;
2038 es->buffer_size = newSize;
2039 es->text = newText;
2040 EDIT_LockBuffer(wnd, es);
2041 TRACE("switched to 32 bit local heap\n");
2043 return es->hloc32;
2047 /*********************************************************************
2049 * EM_GETHANDLE16
2051 * Hopefully this won't fire back at us.
2052 * We always start with a buffer in 32 bit linear memory.
2053 * However, with this message a 16 bit application requests
2054 * a handle of 16 bit local heap memory, where it expects to find
2055 * the text.
2056 * It's a pitty that from this moment on we have to use this
2057 * local heap, because applications may rely on the handle
2058 * in the future.
2060 * In this function we'll try to switch to local heap.
2062 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2064 HLOCAL16 newBuf;
2065 LPSTR newText;
2066 INT newSize;
2068 if (!(es->style & ES_MULTILINE))
2069 return 0;
2071 if (es->hloc16)
2072 return es->hloc16;
2074 if (!LOCAL_HeapSize(wnd->hInstance)) {
2075 if (!LocalInit16(wnd->hInstance, 0,
2076 GlobalSize16(wnd->hInstance))) {
2077 ERR("could not initialize local heap\n");
2078 return 0;
2080 TRACE("local heap initialized\n");
2082 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2083 ERR("could not allocate new 16 bit buffer\n");
2084 return 0;
2086 newSize = min(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2087 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2088 ERR("could not lock new 16 bit buffer\n");
2089 LOCAL_Free(wnd->hInstance, newBuf);
2090 return 0;
2092 lstrcpyA(newText, es->text);
2093 EDIT_UnlockBuffer(wnd, es, TRUE);
2094 if (es->text)
2095 HeapFree(es->heap, 0, es->text);
2096 else if (es->hloc32) {
2097 while (LocalFree(es->hloc32)) ;
2098 LocalFree(es->hloc32);
2100 es->hloc32 = (HLOCAL)NULL;
2101 es->hloc16 = newBuf;
2102 es->buffer_size = newSize;
2103 es->text = newText;
2104 EDIT_LockBuffer(wnd, es);
2105 TRACE("switched to 16 bit buffer\n");
2107 return es->hloc16;
2111 /*********************************************************************
2113 * EM_GETLINE
2116 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch)
2118 LPSTR src;
2119 INT len;
2120 INT i;
2122 if (es->style & ES_MULTILINE) {
2123 if (line >= es->line_count)
2124 return 0;
2125 } else
2126 line = 0;
2127 i = EDIT_EM_LineIndex(wnd, es, line);
2128 src = es->text + i;
2129 len = min(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, i));
2130 for (i = 0 ; i < len ; i++) {
2131 *lpch = *src;
2132 src++;
2133 lpch++;
2135 return (LRESULT)len;
2139 /*********************************************************************
2141 * EM_GETSEL
2144 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end)
2146 UINT s = es->selection_start;
2147 UINT e = es->selection_end;
2149 ORDER_UINT(s, e);
2150 if (start)
2151 *start = s;
2152 if (end)
2153 *end = e;
2154 return MAKELONG(s, e);
2158 /*********************************************************************
2160 * EM_GETTHUMB
2162 * FIXME: is this right ? (or should it be only VSCROLL)
2163 * (and maybe only for edit controls that really have their
2164 * own scrollbars) (and maybe only for multiline controls ?)
2165 * All in all: very poorly documented
2167 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2168 * WM_VSCROLL handlers
2171 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2173 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0, 0),
2174 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0, 0));
2178 /*********************************************************************
2180 * EM_LINEFROMCHAR
2183 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index)
2185 INT line;
2186 LINEDEF *line_def;
2188 if (!(es->style & ES_MULTILINE))
2189 return 0;
2190 if (index > lstrlenA(es->text))
2191 return es->line_count - 1;
2192 if (index == -1)
2193 index = min(es->selection_start, es->selection_end);
2195 line = 0;
2196 line_def = es->first_line_def;
2197 index -= line_def->length;
2198 while ((index >= 0) && line_def->next) {
2199 line++;
2200 line_def = line_def->next;
2201 index -= line_def->length;
2203 return line;
2207 /*********************************************************************
2209 * EM_LINEINDEX
2212 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line)
2214 INT line_index;
2215 LINEDEF *line_def;
2217 if (!(es->style & ES_MULTILINE))
2218 return 0;
2219 if (line >= es->line_count)
2220 return -1;
2222 line_index = 0;
2223 line_def = es->first_line_def;
2224 if (line == -1) {
2225 INT index = es->selection_end - line_def->length;
2226 while ((index >= 0) && line_def->next) {
2227 line_index += line_def->length;
2228 line_def = line_def->next;
2229 index -= line_def->length;
2231 } else {
2232 while (line > 0) {
2233 line_index += line_def->length;
2234 line_def = line_def->next;
2235 line--;
2238 return line_index;
2242 /*********************************************************************
2244 * EM_LINELENGTH
2247 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index)
2249 LINEDEF *line_def;
2251 if (!(es->style & ES_MULTILINE))
2252 return lstrlenA(es->text);
2254 if (index == -1) {
2255 /* get the number of remaining non-selected chars of selected lines */
2256 INT32 li;
2257 INT32 count;
2258 li = EDIT_EM_LineFromChar(wnd, es, es->selection_start);
2259 /* # chars before start of selection area */
2260 count = es->selection_start - EDIT_EM_LineIndex(wnd, es, li);
2261 li = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2262 /* # chars after end of selection */
2263 count += EDIT_EM_LineIndex(wnd, es, li) +
2264 EDIT_EM_LineLength(wnd, es, li) - es->selection_end;
2265 return count;
2267 line_def = es->first_line_def;
2268 index -= line_def->length;
2269 while ((index >= 0) && line_def->next) {
2270 line_def = line_def->next;
2271 index -= line_def->length;
2273 return line_def->net_length;
2277 /*********************************************************************
2279 * EM_LINESCROLL
2281 * FIXME: dx is in average character widths
2282 * However, we assume it is in pixels when we use this
2283 * function internally
2286 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2288 INT nyoff;
2290 if (!(es->style & ES_MULTILINE))
2291 return FALSE;
2293 if (-dx > es->x_offset)
2294 dx = -es->x_offset;
2295 if (dx > es->text_width - es->x_offset)
2296 dx = es->text_width - es->x_offset;
2297 nyoff = max(0, es->y_offset + dy);
2298 if (nyoff >= es->line_count)
2299 nyoff = es->line_count - 1;
2300 dy = (es->y_offset - nyoff) * es->line_height;
2301 if (dx || dy) {
2302 RECT rc1;
2303 RECT rc;
2304 GetClientRect(wnd->hwndSelf, &rc1);
2305 IntersectRect(&rc, &rc1, &es->format_rect);
2306 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2307 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2308 es->y_offset = nyoff;
2309 es->x_offset += dx;
2311 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2312 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2313 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2314 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2315 return TRUE;
2319 /*********************************************************************
2321 * EM_POSFROMCHAR
2324 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2326 INT len = lstrlenA(es->text);
2327 INT l;
2328 INT li;
2329 INT x;
2330 INT y = 0;
2331 HDC dc;
2332 HFONT old_font = 0;
2333 SIZE size;
2335 index = min(index, len);
2336 dc = GetDC(wnd->hwndSelf);
2337 if (es->font)
2338 old_font = SelectObject(dc, es->font);
2339 if (es->style & ES_MULTILINE) {
2340 l = EDIT_EM_LineFromChar(wnd, es, index);
2341 y = (l - es->y_offset) * es->line_height;
2342 li = EDIT_EM_LineIndex(wnd, es, l);
2343 if (after_wrap && (li == index) && l) {
2344 INT l2 = l - 1;
2345 LINEDEF *line_def = es->first_line_def;
2346 while (l2) {
2347 line_def = line_def->next;
2348 l2--;
2350 if (line_def->ending == END_WRAP) {
2351 l--;
2352 y -= es->line_height;
2353 li = EDIT_EM_LineIndex(wnd, es, l);
2356 x = LOWORD(GetTabbedTextExtentA(dc, es->text + li, index - li,
2357 es->tabs_count, es->tabs)) - es->x_offset;
2358 } else {
2359 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
2360 if (index < es->x_offset) {
2361 GetTextExtentPoint32A(dc, text + index,
2362 es->x_offset - index, &size);
2363 x = -size.cx;
2364 } else {
2365 GetTextExtentPoint32A(dc, text + es->x_offset,
2366 index - es->x_offset, &size);
2367 x = size.cx;
2369 y = 0;
2370 if (es->style & ES_PASSWORD)
2371 HeapFree(es->heap, 0 ,text);
2373 x += es->format_rect.left;
2374 y += es->format_rect.top;
2375 if (es->font)
2376 SelectObject(dc, old_font);
2377 ReleaseDC(wnd->hwndSelf, dc);
2378 return MAKELONG((INT16)x, (INT16)y);
2382 /*********************************************************************
2384 * EM_REPLACESEL
2386 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2389 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace)
2391 INT strl = lstrlenA(lpsz_replace);
2392 INT tl = lstrlenA(es->text);
2393 INT utl;
2394 UINT s;
2395 UINT e;
2396 INT i;
2397 LPSTR p;
2399 s = es->selection_start;
2400 e = es->selection_end;
2402 if ((s == e) && !strl)
2403 return;
2405 ORDER_UINT(s, e);
2407 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2408 return;
2410 if (e != s) {
2411 /* there is something to be deleted */
2412 if (can_undo) {
2413 utl = lstrlenA(es->undo_text);
2414 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2415 /* undo-buffer is extended to the right */
2416 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2417 lstrcpynA(es->undo_text + utl, es->text + s, e - s + 1);
2418 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2419 /* undo-buffer is extended to the left */
2420 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2421 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2422 p[e - s] = p[0];
2423 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2424 p[i] = (es->text + s)[i];
2425 es->undo_position = s;
2426 } else {
2427 /* new undo-buffer */
2428 EDIT_MakeUndoFit(wnd, es, e - s);
2429 lstrcpynA(es->undo_text, es->text + s, e - s + 1);
2430 es->undo_position = s;
2432 /* any deletion makes the old insertion-undo invalid */
2433 es->undo_insert_count = 0;
2434 } else
2435 EDIT_EM_EmptyUndoBuffer(wnd, es);
2437 /* now delete */
2438 lstrcpyA(es->text + s, es->text + e);
2440 if (strl) {
2441 /* there is an insertion */
2442 if (can_undo) {
2443 if ((s == es->undo_position) ||
2444 ((es->undo_insert_count) &&
2445 (s == es->undo_position + es->undo_insert_count)))
2447 * insertion is new and at delete position or
2448 * an extension to either left or right
2450 es->undo_insert_count += strl;
2451 else {
2452 /* new insertion undo */
2453 es->undo_position = s;
2454 es->undo_insert_count = strl;
2455 /* new insertion makes old delete-buffer invalid */
2456 *es->undo_text = '\0';
2458 } else
2459 EDIT_EM_EmptyUndoBuffer(wnd, es);
2461 /* now insert */
2462 tl = lstrlenA(es->text);
2463 for (p = es->text + tl ; p >= es->text + s ; p--)
2464 p[strl] = p[0];
2465 for (i = 0 , p = es->text + s ; i < strl ; i++)
2466 p[i] = lpsz_replace[i];
2467 if(es->style & ES_UPPERCASE)
2468 CharUpperBuffA(p, strl);
2469 else if(es->style & ES_LOWERCASE)
2470 CharLowerBuffA(p, strl);
2471 s += strl;
2473 /* FIXME: really inefficient */
2474 if (es->style & ES_MULTILINE)
2475 EDIT_BuildLineDefs_ML(wnd, es);
2477 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2478 es->flags |= EF_MODIFIED;
2479 es->flags |= EF_UPDATE;
2480 EDIT_EM_ScrollCaret(wnd, es);
2482 /* FIXME: really inefficient */
2483 EDIT_UpdateText(wnd, NULL, TRUE);
2487 /*********************************************************************
2489 * EM_SCROLL
2492 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2494 INT dy;
2496 if (!(es->style & ES_MULTILINE))
2497 return (LRESULT)FALSE;
2499 dy = 0;
2501 switch (action) {
2502 case SB_LINEUP:
2503 if (es->y_offset)
2504 dy = -1;
2505 break;
2506 case SB_LINEDOWN:
2507 if (es->y_offset < es->line_count - 1)
2508 dy = 1;
2509 break;
2510 case SB_PAGEUP:
2511 if (es->y_offset)
2512 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2513 break;
2514 case SB_PAGEDOWN:
2515 if (es->y_offset < es->line_count - 1)
2516 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2517 break;
2518 default:
2519 return (LRESULT)FALSE;
2521 if (dy) {
2522 EDIT_EM_LineScroll(wnd, es, 0, dy);
2523 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2525 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2529 /*********************************************************************
2531 * EM_SCROLLCARET
2534 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2536 if (es->style & ES_MULTILINE) {
2537 INT l;
2538 INT li;
2539 INT vlc;
2540 INT ww;
2541 INT cw = es->char_width;
2542 INT x;
2543 INT dy = 0;
2544 INT dx = 0;
2546 l = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2547 li = EDIT_EM_LineIndex(wnd, es, l);
2548 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2549 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2550 if (l >= es->y_offset + vlc)
2551 dy = l - vlc + 1 - es->y_offset;
2552 if (l < es->y_offset)
2553 dy = l - es->y_offset;
2554 ww = es->format_rect.right - es->format_rect.left;
2555 if (x < es->format_rect.left)
2556 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2557 if (x > es->format_rect.right)
2558 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2559 if (dy || dx)
2560 EDIT_EM_LineScroll(wnd, es, dx, dy);
2561 } else {
2562 INT x;
2563 INT goal;
2564 INT format_width;
2566 if (!(es->style & ES_AUTOHSCROLL))
2567 return;
2569 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2570 format_width = es->format_rect.right - es->format_rect.left;
2571 if (x < es->format_rect.left) {
2572 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2573 do {
2574 es->x_offset--;
2575 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2576 } while ((x < goal) && es->x_offset);
2577 /* FIXME: use ScrollWindow() somehow to improve performance */
2578 EDIT_UpdateText(wnd, NULL, TRUE);
2579 } else if (x > es->format_rect.right) {
2580 INT x_last;
2581 INT len = lstrlenA(es->text);
2582 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2583 do {
2584 es->x_offset++;
2585 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2586 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2587 } while ((x > goal) && (x_last > es->format_rect.right));
2588 /* FIXME: use ScrollWindow() somehow to improve performance */
2589 EDIT_UpdateText(wnd, NULL, TRUE);
2595 /*********************************************************************
2597 * EM_SETHANDLE
2599 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2602 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2604 if (!(es->style & ES_MULTILINE))
2605 return;
2607 if (!hloc) {
2608 WARN("called with NULL handle\n");
2609 return;
2612 EDIT_UnlockBuffer(wnd, es, TRUE);
2614 * old buffer is freed by caller, unless
2615 * it is still in our own heap. (in that case
2616 * we free it, correcting the buggy caller.)
2618 if (es->text)
2619 HeapFree(es->heap, 0, es->text);
2621 es->hloc16 = (HLOCAL16)NULL;
2622 es->hloc32 = hloc;
2623 es->text = NULL;
2624 es->buffer_size = LocalSize(es->hloc32) - 1;
2625 EDIT_LockBuffer(wnd, es);
2627 es->x_offset = es->y_offset = 0;
2628 es->selection_start = es->selection_end = 0;
2629 EDIT_EM_EmptyUndoBuffer(wnd, es);
2630 es->flags &= ~EF_MODIFIED;
2631 es->flags &= ~EF_UPDATE;
2632 EDIT_BuildLineDefs_ML(wnd, es);
2633 EDIT_UpdateText(wnd, NULL, TRUE);
2634 EDIT_EM_ScrollCaret(wnd, es);
2638 /*********************************************************************
2640 * EM_SETHANDLE16
2642 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2645 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2647 if (!(es->style & ES_MULTILINE))
2648 return;
2650 if (!hloc) {
2651 WARN("called with NULL handle\n");
2652 return;
2655 EDIT_UnlockBuffer(wnd, es, TRUE);
2657 * old buffer is freed by caller, unless
2658 * it is still in our own heap. (in that case
2659 * we free it, correcting the buggy caller.)
2661 if (es->text)
2662 HeapFree(es->heap, 0, es->text);
2664 es->hloc16 = hloc;
2665 es->hloc32 = (HLOCAL)NULL;
2666 es->text = NULL;
2667 es->buffer_size = LOCAL_Size(wnd->hInstance, es->hloc16) - 1;
2668 EDIT_LockBuffer(wnd, es);
2670 es->x_offset = es->y_offset = 0;
2671 es->selection_start = es->selection_end = 0;
2672 EDIT_EM_EmptyUndoBuffer(wnd, es);
2673 es->flags &= ~EF_MODIFIED;
2674 es->flags &= ~EF_UPDATE;
2675 EDIT_BuildLineDefs_ML(wnd, es);
2676 EDIT_UpdateText(wnd, NULL, TRUE);
2677 EDIT_EM_ScrollCaret(wnd, es);
2681 /*********************************************************************
2683 * EM_SETLIMITTEXT
2685 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
2686 * However, the windows version is not complied to yet in all of edit.c
2689 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit)
2691 if (es->style & ES_MULTILINE) {
2692 if (limit)
2693 es->buffer_limit = min(limit, BUFLIMIT_MULTI);
2694 else
2695 es->buffer_limit = BUFLIMIT_MULTI;
2696 } else {
2697 if (limit)
2698 es->buffer_limit = min(limit, BUFLIMIT_SINGLE);
2699 else
2700 es->buffer_limit = BUFLIMIT_SINGLE;
2705 /*********************************************************************
2707 * EM_SETMARGINS
2709 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2710 * action wParam despite what the docs say. EC_USEFONTINFO means one third
2711 * of the char's width, according to the new docs.
2714 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action,
2715 INT left, INT right)
2717 if (action & EC_LEFTMARGIN) {
2718 if (left != EC_USEFONTINFO)
2719 es->left_margin = left;
2720 else
2721 es->left_margin = es->char_width / 3;
2724 if (action & EC_RIGHTMARGIN) {
2725 if (right != EC_USEFONTINFO)
2726 es->right_margin = right;
2727 else
2728 es->right_margin = es->char_width / 3;
2730 TRACE("left=%d, right=%d\n", es->left_margin, es->right_margin);
2734 /*********************************************************************
2736 * EM_SETPASSWORDCHAR
2739 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c)
2741 if (es->style & ES_MULTILINE)
2742 return;
2744 if (es->password_char == c)
2745 return;
2747 es->password_char = c;
2748 if (c) {
2749 wnd->dwStyle |= ES_PASSWORD;
2750 es->style |= ES_PASSWORD;
2751 } else {
2752 wnd->dwStyle &= ~ES_PASSWORD;
2753 es->style &= ~ES_PASSWORD;
2755 EDIT_UpdateText(wnd, NULL, TRUE);
2759 /*********************************************************************
2761 * EDIT_EM_SetSel
2763 * note: unlike the specs say: the order of start and end
2764 * _is_ preserved in Windows. (i.e. start can be > end)
2765 * In other words: this handler is OK
2768 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
2770 UINT old_start = es->selection_start;
2771 UINT old_end = es->selection_end;
2772 UINT len = lstrlenA(es->text);
2774 if (start == -1) {
2775 start = es->selection_end;
2776 end = es->selection_end;
2777 } else {
2778 start = min(start, len);
2779 end = min(end, len);
2781 es->selection_start = start;
2782 es->selection_end = end;
2783 if (after_wrap)
2784 es->flags |= EF_AFTER_WRAP;
2785 else
2786 es->flags &= ~EF_AFTER_WRAP;
2787 if (es->flags & EF_FOCUSED)
2788 EDIT_SetCaretPos(wnd, es, end, after_wrap);
2789 /* This is a little bit more efficient than before, not sure if it can be improved. FIXME? */
2790 ORDER_UINT(start, end);
2791 ORDER_UINT(end, old_end);
2792 ORDER_UINT(start, old_start);
2793 ORDER_UINT(old_start, old_end);
2794 if (end != old_start)
2797 * One can also do
2798 * ORDER_UINT32(end, old_start);
2799 * EDIT_InvalidateText(wnd, es, start, end);
2800 * EDIT_InvalidateText(wnd, es, old_start, old_end);
2801 * in place of the following if statement.
2803 if (old_start > end )
2805 EDIT_InvalidateText(wnd, es, start, end);
2806 EDIT_InvalidateText(wnd, es, old_start, old_end);
2808 else
2810 EDIT_InvalidateText(wnd, es, start, old_start);
2811 EDIT_InvalidateText(wnd, es, end, old_end);
2814 else EDIT_InvalidateText(wnd, es, start, old_end);
2818 /*********************************************************************
2820 * EM_SETTABSTOPS
2823 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs)
2825 if (!(es->style & ES_MULTILINE))
2826 return FALSE;
2827 if (es->tabs)
2828 HeapFree(es->heap, 0, es->tabs);
2829 es->tabs_count = count;
2830 if (!count)
2831 es->tabs = NULL;
2832 else {
2833 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2834 memcpy(es->tabs, tabs, count * sizeof(INT));
2836 return TRUE;
2840 /*********************************************************************
2842 * EM_SETTABSTOPS16
2845 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs)
2847 if (!(es->style & ES_MULTILINE))
2848 return FALSE;
2849 if (es->tabs)
2850 HeapFree(es->heap, 0, es->tabs);
2851 es->tabs_count = count;
2852 if (!count)
2853 es->tabs = NULL;
2854 else {
2855 INT i;
2856 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2857 for (i = 0 ; i < count ; i++)
2858 es->tabs[i] = *tabs++;
2860 return TRUE;
2864 /*********************************************************************
2866 * EM_SETWORDBREAKPROC
2869 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp)
2871 if (es->word_break_proc32A == wbp)
2872 return;
2874 es->word_break_proc32A = wbp;
2875 es->word_break_proc16 = NULL;
2876 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2877 EDIT_BuildLineDefs_ML(wnd, es);
2878 EDIT_UpdateText(wnd, NULL, TRUE);
2883 /*********************************************************************
2885 * EM_SETWORDBREAKPROC16
2888 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
2890 if (es->word_break_proc16 == wbp)
2891 return;
2893 es->word_break_proc32A = NULL;
2894 es->word_break_proc16 = wbp;
2895 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2896 EDIT_BuildLineDefs_ML(wnd, es);
2897 EDIT_UpdateText(wnd, NULL, TRUE);
2902 /*********************************************************************
2904 * EM_UNDO / WM_UNDO
2907 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2909 INT ulength = lstrlenA(es->undo_text);
2910 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2912 lstrcpyA(utext, es->undo_text);
2914 TRACE("before UNDO:insertion length = %d, deletion buffer = %s\n",
2915 es->undo_insert_count, utext);
2917 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2918 EDIT_EM_EmptyUndoBuffer(wnd, es);
2919 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext);
2920 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2921 HeapFree(es->heap, 0, utext);
2923 TRACE("after UNDO:insertion length = %d, deletion buffer = %s\n",
2924 es->undo_insert_count, es->undo_text);
2926 if (es->flags & EF_UPDATE) {
2927 es->flags &= ~EF_UPDATE;
2928 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
2931 return TRUE;
2935 /*********************************************************************
2937 * WM_CHAR
2940 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data)
2942 BOOL control = GetKeyState(VK_CONTROL) & 0x8000;
2943 switch (c) {
2944 case '\r':
2945 /* If the edit doesn't want the return and it's not a multiline edit, do nothing */
2946 if(!(es->style & ES_MULTILINE) && !(es->style & ES_WANTRETURN))
2947 break;
2948 case '\n':
2949 if (es->style & ES_MULTILINE) {
2950 if (es->style & ES_READONLY) {
2951 EDIT_MoveHome(wnd, es, FALSE);
2952 EDIT_MoveDown_ML(wnd, es, FALSE);
2953 } else {
2954 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\r\n");
2955 if (es->flags & EF_UPDATE) {
2956 es->flags &= ~EF_UPDATE;
2957 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
2961 break;
2962 case '\t':
2963 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2965 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\t");
2966 if (es->flags & EF_UPDATE) {
2967 es->flags &= ~EF_UPDATE;
2968 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
2971 break;
2972 case VK_BACK:
2973 if (!(es->style & ES_READONLY) && !control) {
2974 if (es->selection_start != es->selection_end)
2975 EDIT_WM_Clear(wnd, es);
2976 else {
2977 /* delete character left of caret */
2978 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
2979 EDIT_MoveBackward(wnd, es, TRUE);
2980 EDIT_WM_Clear(wnd, es);
2983 break;
2984 case 0x03: /* ^C */
2985 SendMessageA(wnd->hwndSelf, WM_COPY, 0, 0);
2986 break;
2987 case 0x16: /* ^V */
2988 SendMessageA(wnd->hwndSelf, WM_PASTE, 0, 0);
2989 break;
2990 case 0x18: /* ^X */
2991 SendMessageA(wnd->hwndSelf, WM_CUT, 0, 0);
2992 break;
2994 default:
2995 if (!(es->style & ES_READONLY) && ((BYTE)c >= ' ') && (c != 127)) {
2996 char str[2];
2997 str[0] = c;
2998 str[1] = '\0';
2999 EDIT_EM_ReplaceSel(wnd, es, TRUE, str);
3000 if (es->flags & EF_UPDATE) {
3001 es->flags &= ~EF_UPDATE;
3002 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3005 break;
3010 /*********************************************************************
3012 * WM_COMMAND
3015 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
3017 if (code || control)
3018 return;
3020 switch (id) {
3021 case EM_UNDO:
3022 EDIT_EM_Undo(wnd, es);
3023 break;
3024 case WM_CUT:
3025 EDIT_WM_Cut(wnd, es);
3026 break;
3027 case WM_COPY:
3028 EDIT_WM_Copy(wnd, es);
3029 break;
3030 case WM_PASTE:
3031 EDIT_WM_Paste(wnd, es);
3032 break;
3033 case WM_CLEAR:
3034 EDIT_WM_Clear(wnd, es);
3035 break;
3036 case EM_SETSEL:
3037 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3038 EDIT_EM_ScrollCaret(wnd, es);
3039 break;
3040 default:
3041 ERR("unknown menu item, please report\n");
3042 break;
3047 /*********************************************************************
3049 * WM_CONTEXTMENU
3051 * Note: the resource files resource/sysres_??.rc cannot define a
3052 * single popup menu. Hence we use a (dummy) menubar
3053 * containing the single popup menu as its first item.
3055 * FIXME: the message identifiers have been chosen arbitrarily,
3056 * hence we use MF_BYPOSITION.
3057 * We might as well use the "real" values (anybody knows ?)
3058 * The menu definition is in resources/sysres_??.rc.
3059 * Once these are OK, we better use MF_BYCOMMAND here
3060 * (as we do in EDIT_WM_Command()).
3063 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y)
3065 HMENU menu = LoadMenuA(GetModuleHandleA("USER32"), "EDITMENU");
3066 HMENU popup = GetSubMenu(menu, 0);
3067 UINT start = es->selection_start;
3068 UINT end = es->selection_end;
3070 ORDER_UINT(start, end);
3072 /* undo */
3073 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
3074 /* cut */
3075 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3076 /* copy */
3077 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
3078 /* paste */
3079 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
3080 /* delete */
3081 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
3082 /* select all */
3083 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != lstrlenA(es->text)) ? MF_ENABLED : MF_GRAYED));
3085 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
3086 DestroyMenu(menu);
3090 /*********************************************************************
3092 * WM_COPY
3095 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3097 INT s = es->selection_start;
3098 INT e = es->selection_end;
3099 HGLOBAL hdst;
3100 LPSTR dst;
3102 if (e == s)
3103 return;
3104 ORDER_INT(s, e);
3105 hdst = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3106 dst = GlobalLock(hdst);
3107 lstrcpynA(dst, es->text + s, e - s + 1);
3108 GlobalUnlock(hdst);
3109 OpenClipboard(wnd->hwndSelf);
3110 EmptyClipboard();
3111 SetClipboardData(CF_TEXT, hdst);
3112 CloseClipboard();
3116 /*********************************************************************
3118 * WM_CREATE
3121 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs)
3124 * To initialize some final structure members, we call some helper
3125 * functions. However, since the EDITSTATE is not consistent (i.e.
3126 * not fully initialized), we should be very careful which
3127 * functions can be called, and in what order.
3129 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3130 EDIT_EM_EmptyUndoBuffer(wnd, es);
3132 if (cs->lpszName && *(cs->lpszName) != '\0') {
3133 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3134 /* if we insert text to the editline, the text scrolls out
3135 * of the window, as the caret is placed after the insert
3136 * pos normally; thus we reset es->selection... to 0 and
3137 * update caret
3139 es->selection_start = es->selection_end = 0;
3140 EDIT_EM_ScrollCaret(wnd, es);
3141 if (es->flags & EF_UPDATE) {
3142 es->flags &= ~EF_UPDATE;
3143 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3146 return 0;
3150 /*********************************************************************
3152 * WM_DESTROY
3155 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3157 if (es->hloc32) {
3158 while (LocalUnlock(es->hloc32)) ;
3159 LocalFree(es->hloc32);
3161 if (es->hloc16) {
3162 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3163 LOCAL_Free(wnd->hInstance, es->hloc16);
3165 HeapDestroy(es->heap);
3166 HeapFree(GetProcessHeap(), 0, es);
3167 *(EDITSTATE **)wnd->wExtra = NULL;
3171 /*********************************************************************
3173 * WM_ERASEBKGND
3176 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3178 HBRUSH brush;
3179 RECT rc;
3181 if ( VERSION_AppWinVer() >= 0x40000 &&(
3182 !es->bEnableState || (es->style & ES_READONLY)))
3183 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3184 else
3185 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3187 if (!brush)
3188 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3190 GetClientRect(wnd->hwndSelf, &rc);
3191 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3192 GetClipBox(dc, &rc);
3194 * FIXME: specs say that we should UnrealizeObject() the brush,
3195 * but the specs of UnrealizeObject() say that we shouldn't
3196 * unrealize a stock object. The default brush that
3197 * DefWndProc() returns is ... a stock object.
3199 FillRect(dc, &rc, brush);
3200 return -1;
3204 /*********************************************************************
3206 * WM_GETTEXT
3209 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text)
3211 lstrcpynA(text, es->text, count);
3212 return strlen(text);
3216 /*********************************************************************
3218 * EDIT_HScroll_Hack
3220 * 16 bit notepad needs this. Actually it is not _our_ hack,
3221 * it is notepad's. Notepad is sending us scrollbar messages with
3222 * undocumented parameters without us even having a scrollbar ... !?!?
3225 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3227 INT dx = 0;
3228 INT fw = es->format_rect.right - es->format_rect.left;
3229 LRESULT ret = 0;
3231 if (!(es->flags & EF_HSCROLL_HACK)) {
3232 ERR("hacked WM_HSCROLL handler invoked\n");
3233 ERR(" if you are _not_ running 16 bit notepad, please report\n");
3234 ERR(" (this message is only displayed once per edit control)\n");
3235 es->flags |= EF_HSCROLL_HACK;
3238 switch (action) {
3239 case SB_LINELEFT:
3240 if (es->x_offset)
3241 dx = -es->char_width;
3242 break;
3243 case SB_LINERIGHT:
3244 if (es->x_offset < es->text_width)
3245 dx = es->char_width;
3246 break;
3247 case SB_PAGELEFT:
3248 if (es->x_offset)
3249 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3250 break;
3251 case SB_PAGERIGHT:
3252 if (es->x_offset < es->text_width)
3253 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3254 break;
3255 case SB_LEFT:
3256 if (es->x_offset)
3257 dx = -es->x_offset;
3258 break;
3259 case SB_RIGHT:
3260 if (es->x_offset < es->text_width)
3261 dx = es->text_width - es->x_offset;
3262 break;
3263 case SB_THUMBTRACK:
3264 es->flags |= EF_HSCROLL_TRACK;
3265 dx = pos * es->text_width / 100 - es->x_offset;
3266 break;
3267 case SB_THUMBPOSITION:
3268 es->flags &= ~EF_HSCROLL_TRACK;
3269 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3270 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3271 break;
3272 case SB_ENDSCROLL:
3273 break;
3276 * FIXME : the next two are undocumented !
3277 * Are we doing the right thing ?
3278 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3279 * although it's also a regular control message.
3281 case EM_GETTHUMB16:
3282 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3283 break;
3284 case EM_LINESCROLL16:
3285 dx = pos;
3286 break;
3288 default:
3289 ERR("undocumented (hacked) WM_HSCROLL parameter, please report\n");
3290 return 0;
3292 if (dx)
3293 EDIT_EM_LineScroll(wnd, es, dx, 0);
3294 return ret;
3298 /*********************************************************************
3300 * WM_HSCROLL
3303 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3305 INT dx;
3306 INT fw;
3308 if (!(es->style & ES_MULTILINE))
3309 return 0;
3311 if (!(es->style & ES_AUTOHSCROLL))
3312 return 0;
3314 if (!(es->style & WS_HSCROLL))
3315 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3317 dx = 0;
3318 fw = es->format_rect.right - es->format_rect.left;
3319 switch (action) {
3320 case SB_LINELEFT:
3321 if (es->x_offset)
3322 dx = -es->char_width;
3323 break;
3324 case SB_LINERIGHT:
3325 if (es->x_offset < es->text_width)
3326 dx = es->char_width;
3327 break;
3328 case SB_PAGELEFT:
3329 if (es->x_offset)
3330 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3331 break;
3332 case SB_PAGERIGHT:
3333 if (es->x_offset < es->text_width)
3334 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3335 break;
3336 case SB_LEFT:
3337 if (es->x_offset)
3338 dx = -es->x_offset;
3339 break;
3340 case SB_RIGHT:
3341 if (es->x_offset < es->text_width)
3342 dx = es->text_width - es->x_offset;
3343 break;
3344 case SB_THUMBTRACK:
3345 es->flags |= EF_HSCROLL_TRACK;
3346 dx = pos - es->x_offset;
3347 break;
3348 case SB_THUMBPOSITION:
3349 es->flags &= ~EF_HSCROLL_TRACK;
3350 if (!(dx = pos - es->x_offset)) {
3351 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3352 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3354 break;
3355 case SB_ENDSCROLL:
3356 break;
3358 default:
3359 ERR("undocumented WM_HSCROLL parameter, please report\n");
3360 return 0;
3362 if (dx)
3363 EDIT_EM_LineScroll(wnd, es, dx, 0);
3364 return 0;
3368 /*********************************************************************
3370 * EDIT_CheckCombo
3373 static BOOL EDIT_CheckCombo(WND *wnd, UINT msg, INT key, DWORD key_data)
3375 HWND hLBox;
3377 /********************************************************************
3378 * This if statement used to check to see if the parent of the
3379 * edit control was a 'combobox' by comparing the ATOM of the parent
3380 * to a table of internal window control ATOMs. However, this check
3381 * would fail if the parent was a superclassed combobox (Although
3382 * having the same basic functionality of a combobox, it has a
3383 * different name and ATOM, thus defeating this check.)
3385 * The safe way to determine if the parent is a combobox is to send it
3386 * a message only a combo box would understand. I send it a message
3387 * CB_GETCOUNT, if I get 0 then the parent is not a combobox -
3388 * return FALSE. If I get > 0, then the parent IS a combobox
3389 * (or sub/super classed derrivative thereof)
3390 ********************************************************************/
3391 if (
3392 ((SendMessageA(wnd->parent->hwndSelf, CB_GETCOUNT, 0, 0)) > 0) &&
3393 (hLBox = COMBO_GetLBWindow(wnd->parent))
3396 HWND hCombo = wnd->parent->hwndSelf;
3397 BOOL bUIFlip = TRUE;
3399 TRACE_(combo)("[%04x]: handling msg %04x (%04x)\n",
3400 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3402 switch (msg) {
3403 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3404 if (key != VK_F4) {
3405 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3406 if (SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0))
3407 bUIFlip = FALSE;
3409 if (!bUIFlip)
3410 SendMessageA(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3411 else {
3412 /* make sure ComboLBox pops up */
3413 SendMessageA(hCombo, CB_SETEXTENDEDUI, 0, 0);
3414 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3415 SendMessageA(hCombo, CB_SETEXTENDEDUI, 1, 0);
3417 break;
3418 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3419 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3420 if (bUIFlip) {
3421 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3422 SendMessageA(hCombo, CB_SHOWDROPDOWN, (bUIFlip) ? FALSE : TRUE, 0);
3423 } else
3424 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3425 break;
3427 return TRUE;
3429 return FALSE;
3433 /*********************************************************************
3435 * WM_KEYDOWN
3437 * Handling of special keys that don't produce a WM_CHAR
3438 * (i.e. non-printable keys) & Backspace & Delete
3441 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3443 BOOL shift;
3444 BOOL control;
3446 if (GetKeyState(VK_MENU) & 0x8000)
3447 return 0;
3449 shift = GetKeyState(VK_SHIFT) & 0x8000;
3450 control = GetKeyState(VK_CONTROL) & 0x8000;
3452 switch (key) {
3453 case VK_F4:
3454 case VK_UP:
3455 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3456 break;
3457 if (key == VK_F4)
3458 break;
3459 /* fall through */
3460 case VK_LEFT:
3461 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3462 EDIT_MoveUp_ML(wnd, es, shift);
3463 else
3464 if (control)
3465 EDIT_MoveWordBackward(wnd, es, shift);
3466 else
3467 EDIT_MoveBackward(wnd, es, shift);
3468 break;
3469 case VK_DOWN:
3470 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3471 break;
3472 /* fall through */
3473 case VK_RIGHT:
3474 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3475 EDIT_MoveDown_ML(wnd, es, shift);
3476 else if (control)
3477 EDIT_MoveWordForward(wnd, es, shift);
3478 else
3479 EDIT_MoveForward(wnd, es, shift);
3480 break;
3481 case VK_HOME:
3482 EDIT_MoveHome(wnd, es, shift);
3483 break;
3484 case VK_END:
3485 EDIT_MoveEnd(wnd, es, shift);
3486 break;
3487 case VK_PRIOR:
3488 if (es->style & ES_MULTILINE)
3489 EDIT_MovePageUp_ML(wnd, es, shift);
3490 break;
3491 case VK_NEXT:
3492 if (es->style & ES_MULTILINE)
3493 EDIT_MovePageDown_ML(wnd, es, shift);
3494 break;
3495 case VK_DELETE:
3496 if (!(es->style & ES_READONLY) && !(shift && control)) {
3497 if (es->selection_start != es->selection_end) {
3498 if (shift)
3499 EDIT_WM_Cut(wnd, es);
3500 else
3501 EDIT_WM_Clear(wnd, es);
3502 } else {
3503 if (shift) {
3504 /* delete character left of caret */
3505 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3506 EDIT_MoveBackward(wnd, es, TRUE);
3507 EDIT_WM_Clear(wnd, es);
3508 } else if (control) {
3509 /* delete to end of line */
3510 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3511 EDIT_MoveEnd(wnd, es, TRUE);
3512 EDIT_WM_Clear(wnd, es);
3513 } else {
3514 /* delete character right of caret */
3515 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3516 EDIT_MoveForward(wnd, es, TRUE);
3517 EDIT_WM_Clear(wnd, es);
3521 break;
3522 case VK_INSERT:
3523 if (shift) {
3524 if (!(es->style & ES_READONLY))
3525 EDIT_WM_Paste(wnd, es);
3526 } else if (control)
3527 EDIT_WM_Copy(wnd, es);
3528 break;
3529 case VK_RETURN:
3530 /* If the edit doesn't want the return send a message to the default object */
3531 if(!(es->style & ES_WANTRETURN))
3533 HWND hwndParent = GetParent(wnd->hwndSelf);
3534 DWORD dw = SendMessage16( hwndParent, DM_GETDEFID, 0, 0 );
3535 if (HIWORD(dw) == DC_HASDEFID)
3537 SendMessageA( hwndParent, WM_COMMAND,
3538 MAKEWPARAM( LOWORD(dw), BN_CLICKED ),
3539 (LPARAM)GetDlgItem( hwndParent, LOWORD(dw) ) );
3542 break;
3544 return 0;
3548 /*********************************************************************
3550 * WM_KILLFOCUS
3553 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus)
3555 es->flags &= ~EF_FOCUSED;
3556 DestroyCaret();
3557 if(!(es->style & ES_NOHIDESEL))
3558 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3559 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3560 return 0;
3564 /*********************************************************************
3566 * WM_LBUTTONDBLCLK
3568 * The caret position has been set on the WM_LBUTTONDOWN message
3571 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3573 INT s;
3574 INT e = es->selection_end;
3575 INT l;
3576 INT li;
3577 INT ll;
3579 if (!(es->flags & EF_FOCUSED))
3580 return 0;
3582 l = EDIT_EM_LineFromChar(wnd, es, e);
3583 li = EDIT_EM_LineIndex(wnd, es, l);
3584 ll = EDIT_EM_LineLength(wnd, es, e);
3585 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3586 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3587 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3588 EDIT_EM_ScrollCaret(wnd, es);
3589 return 0;
3593 /*********************************************************************
3595 * WM_LBUTTONDOWN
3598 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3600 INT e;
3601 BOOL after_wrap;
3603 if (!(es->flags & EF_FOCUSED))
3604 return 0;
3606 es->bCaptureState = TRUE;
3607 SetCapture(wnd->hwndSelf);
3608 EDIT_ConfinePoint(wnd, es, &x, &y);
3609 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3610 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3611 EDIT_EM_ScrollCaret(wnd, es);
3612 es->region_posx = es->region_posy = 0;
3613 SetTimer(wnd->hwndSelf, 0, 100, NULL);
3614 return 0;
3618 /*********************************************************************
3620 * WM_LBUTTONUP
3623 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3625 if (es->bCaptureState && GetCapture() == wnd->hwndSelf) {
3626 KillTimer(wnd->hwndSelf, 0);
3627 ReleaseCapture();
3629 es->bCaptureState = FALSE;
3630 return 0;
3634 /*********************************************************************
3636 * WM_MBUTTONDOWN
3639 static LRESULT EDIT_WM_MButtonDown(WND *wnd)
3641 SendMessageA(wnd->hwndSelf,WM_PASTE,0,0);
3642 return 0;
3646 /*********************************************************************
3648 * WM_MOUSEMOVE
3651 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3653 INT e;
3654 BOOL after_wrap;
3655 INT prex, prey;
3657 if (GetCapture() != wnd->hwndSelf)
3658 return 0;
3661 * FIXME: gotta do some scrolling if outside client
3662 * area. Maybe reset the timer ?
3664 prex = x; prey = y;
3665 EDIT_ConfinePoint(wnd, es, &x, &y);
3666 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3667 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3668 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3669 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3670 return 0;
3674 /*********************************************************************
3676 * WM_NCCREATE
3679 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs)
3681 EDITSTATE *es;
3683 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3684 return FALSE;
3685 *(EDITSTATE **)wnd->wExtra = es;
3688 * Note: since the EDITSTATE has not been fully initialized yet,
3689 * we can't use any API calls that may send
3690 * WM_XXX messages before WM_NCCREATE is completed.
3693 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3694 return FALSE;
3695 es->style = cs->style;
3697 es->bEnableState = !(cs->style & WS_DISABLED);
3700 * In Win95 look and feel, the WS_BORDER style is replaced by the
3701 * WS_EX_CLIENTEDGE style for the edit control. This gives the edit
3702 * control a non client area.
3704 if (TWEAK_WineLook != WIN31_LOOK)
3706 if (es->style & WS_BORDER)
3708 es->style &= ~WS_BORDER;
3709 wnd->dwStyle &= ~WS_BORDER;
3710 wnd->dwExStyle |= WS_EX_CLIENTEDGE;
3713 else
3715 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3716 wnd->dwStyle &= ~WS_BORDER;
3719 if (es->style & ES_MULTILINE) {
3720 es->buffer_size = BUFSTART_MULTI;
3721 es->buffer_limit = BUFLIMIT_MULTI;
3722 if (es->style & WS_VSCROLL)
3723 es->style |= ES_AUTOVSCROLL;
3724 if (es->style & WS_HSCROLL)
3725 es->style |= ES_AUTOHSCROLL;
3726 es->style &= ~ES_PASSWORD;
3727 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3728 if (es->style & ES_RIGHT)
3729 es->style &= ~ES_CENTER;
3730 es->style &= ~WS_HSCROLL;
3731 es->style &= ~ES_AUTOHSCROLL;
3734 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3735 es->style |= ES_AUTOVSCROLL;
3736 } else {
3737 es->buffer_size = BUFSTART_SINGLE;
3738 es->buffer_limit = BUFLIMIT_SINGLE;
3739 es->style &= ~ES_CENTER;
3740 es->style &= ~ES_RIGHT;
3741 es->style &= ~WS_HSCROLL;
3742 es->style &= ~WS_VSCROLL;
3743 es->style &= ~ES_AUTOVSCROLL;
3744 es->style &= ~ES_WANTRETURN;
3745 if (es->style & ES_UPPERCASE) {
3746 es->style &= ~ES_LOWERCASE;
3747 es->style &= ~ES_NUMBER;
3748 } else if (es->style & ES_LOWERCASE)
3749 es->style &= ~ES_NUMBER;
3750 if (es->style & ES_PASSWORD)
3751 es->password_char = '*';
3753 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3754 es->style |= ES_AUTOHSCROLL;
3756 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3757 return FALSE;
3758 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3759 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3760 return FALSE;
3761 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3762 *es->text = '\0';
3763 if (es->style & ES_MULTILINE)
3764 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3765 return FALSE;
3766 es->line_count = 1;
3768 return TRUE;
3771 /*********************************************************************
3773 * WM_PAINT
3776 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es, WPARAM wParam)
3778 PAINTSTRUCT ps;
3779 INT i;
3780 HDC dc;
3781 HFONT old_font = 0;
3782 RECT rc;
3783 RECT rcLine;
3784 RECT rcRgn;
3785 BOOL rev = es->bEnableState &&
3786 ((es->flags & EF_FOCUSED) ||
3787 (es->style & ES_NOHIDESEL));
3788 if (!wParam)
3789 dc = BeginPaint(wnd->hwndSelf, &ps);
3790 else
3791 dc = (HDC) wParam;
3792 if(es->style & WS_BORDER) {
3793 GetClientRect(wnd->hwndSelf, &rc);
3794 if(es->style & ES_MULTILINE) {
3795 if(es->style & WS_HSCROLL) rc.bottom++;
3796 if(es->style & WS_VSCROLL) rc.right++;
3798 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
3800 IntersectClipRect(dc, es->format_rect.left,
3801 es->format_rect.top,
3802 es->format_rect.right,
3803 es->format_rect.bottom);
3804 if (es->style & ES_MULTILINE) {
3805 GetClientRect(wnd->hwndSelf, &rc);
3806 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3808 if (es->font)
3809 old_font = SelectObject(dc, es->font);
3810 if ( VERSION_AppWinVer() >= 0x40000 &&(
3811 !es->bEnableState || (es->style & ES_READONLY)))
3812 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3813 else
3814 EDIT_SEND_CTLCOLOR(wnd, dc);
3816 if (!es->bEnableState)
3817 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3818 GetClipBox(dc, &rcRgn);
3819 if (es->style & ES_MULTILINE) {
3820 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3821 for (i = es->y_offset ; i <= min(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3822 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3823 if (IntersectRect(&rc, &rcRgn, &rcLine))
3824 EDIT_PaintLine(wnd, es, dc, i, rev);
3826 } else {
3827 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3828 if (IntersectRect(&rc, &rcRgn, &rcLine))
3829 EDIT_PaintLine(wnd, es, dc, 0, rev);
3831 if (es->font)
3832 SelectObject(dc, old_font);
3833 if (es->flags & EF_FOCUSED)
3834 EDIT_SetCaretPos(wnd, es, es->selection_end,
3835 es->flags & EF_AFTER_WRAP);
3836 if (!wParam)
3837 EndPaint(wnd->hwndSelf, &ps);
3838 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3839 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3840 SCROLLINFO si;
3841 si.cbSize = sizeof(SCROLLINFO);
3842 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3843 si.nMin = 0;
3844 si.nMax = es->line_count + vlc - 2;
3845 si.nPage = vlc;
3846 si.nPos = es->y_offset;
3847 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
3849 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3850 SCROLLINFO si;
3851 INT fw = es->format_rect.right - es->format_rect.left;
3852 si.cbSize = sizeof(SCROLLINFO);
3853 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3854 si.nMin = 0;
3855 si.nMax = es->text_width + fw - 1;
3856 si.nPage = fw;
3857 si.nPos = es->x_offset;
3858 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3863 /*********************************************************************
3865 * WM_PASTE
3868 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3870 HGLOBAL hsrc;
3871 LPSTR src;
3873 OpenClipboard(wnd->hwndSelf);
3874 if ((hsrc = GetClipboardData(CF_TEXT))) {
3875 src = (LPSTR)GlobalLock(hsrc);
3876 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3877 GlobalUnlock(hsrc);
3879 if (es->flags & EF_UPDATE) {
3880 es->flags &= ~EF_UPDATE;
3881 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3884 CloseClipboard();
3888 /*********************************************************************
3890 * WM_SETFOCUS
3893 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus)
3895 es->flags |= EF_FOCUSED;
3896 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3897 EDIT_SetCaretPos(wnd, es, es->selection_end,
3898 es->flags & EF_AFTER_WRAP);
3899 if(!(es->style & ES_NOHIDESEL))
3900 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3901 ShowCaret(wnd->hwndSelf);
3902 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3906 /*********************************************************************
3908 * WM_SETFONT
3910 * With Win95 look the margins are set to default font value unless
3911 * the system font (font == 0) is being set, in which case they are left
3912 * unchanged.
3915 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
3917 TEXTMETRICA tm;
3918 HDC dc;
3919 HFONT old_font = 0;
3920 RECT r;
3922 es->font = font;
3923 dc = GetDC(wnd->hwndSelf);
3924 if (font)
3925 old_font = SelectObject(dc, font);
3926 GetTextMetricsA(dc, &tm);
3927 es->line_height = tm.tmHeight;
3928 es->char_width = tm.tmAveCharWidth;
3929 if (font)
3930 SelectObject(dc, old_font);
3931 ReleaseDC(wnd->hwndSelf, dc);
3932 if (font && (TWEAK_WineLook > WIN31_LOOK))
3933 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3934 EC_USEFONTINFO, EC_USEFONTINFO);
3936 /* Force the recalculation of the format rect for each font change */
3937 GetClientRect(wnd->hwndSelf, &r);
3938 EDIT_SetRectNP(wnd, es, &r);
3940 if (es->style & ES_MULTILINE)
3941 EDIT_BuildLineDefs_ML(wnd, es);
3943 if (redraw)
3944 EDIT_UpdateText(wnd, NULL, TRUE);
3945 if (es->flags & EF_FOCUSED) {
3946 DestroyCaret();
3947 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3948 EDIT_SetCaretPos(wnd, es, es->selection_end,
3949 es->flags & EF_AFTER_WRAP);
3950 ShowCaret(wnd->hwndSelf);
3955 /*********************************************************************
3957 * WM_SETTEXT
3959 * NOTES
3960 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3961 * The modified flag is reset. No notifications are sent.
3963 * For single-line controls, reception of WM_SETTEXT triggers:
3964 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3967 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3969 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3970 if (text) {
3971 TRACE("\t'%s'\n", text);
3972 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3973 } else {
3974 TRACE("\t<NULL>\n");
3975 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3977 es->x_offset = 0;
3978 if (es->style & ES_MULTILINE) {
3979 es->flags &= ~EF_UPDATE;
3980 } else {
3981 es->flags |= EF_UPDATE;
3983 es->flags &= ~EF_MODIFIED;
3984 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3985 EDIT_EM_ScrollCaret(wnd, es);
3987 if (es->flags & EF_UPDATE) {
3988 es->flags &= ~EF_UPDATE;
3989 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3994 /*********************************************************************
3996 * WM_SIZE
3999 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
4001 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
4002 RECT rc;
4003 SetRect(&rc, 0, 0, width, height);
4004 EDIT_SetRectNP(wnd, es, &rc);
4005 EDIT_UpdateText(wnd, NULL, TRUE);
4010 /*********************************************************************
4012 * WM_SYSKEYDOWN
4015 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
4017 if ((key == VK_BACK) && (key_data & 0x2000)) {
4018 if (EDIT_EM_CanUndo(wnd, es))
4019 EDIT_EM_Undo(wnd, es);
4020 return 0;
4021 } else if (key == VK_UP || key == VK_DOWN)
4022 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
4023 return 0;
4024 return DefWindowProcA(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
4028 /*********************************************************************
4030 * WM_TIMER
4033 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc)
4035 if (es->region_posx < 0) {
4036 EDIT_MoveBackward(wnd, es, TRUE);
4037 } else if (es->region_posx > 0) {
4038 EDIT_MoveForward(wnd, es, TRUE);
4041 * FIXME: gotta do some vertical scrolling here, like
4042 * EDIT_EM_LineScroll(wnd, 0, 1);
4047 /*********************************************************************
4049 * EDIT_VScroll_Hack
4051 * 16 bit notepad needs this. Actually it is not _our_ hack,
4052 * it is notepad's. Notepad is sending us scrollbar messages with
4053 * undocumented parameters without us even having a scrollbar ... !?!?
4056 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
4058 INT dy = 0;
4059 LRESULT ret = 0;
4061 if (!(es->flags & EF_VSCROLL_HACK)) {
4062 ERR("hacked WM_VSCROLL handler invoked\n");
4063 ERR(" if you are _not_ running 16 bit notepad, please report\n");
4064 ERR(" (this message is only displayed once per edit control)\n");
4065 es->flags |= EF_VSCROLL_HACK;
4068 switch (action) {
4069 case SB_LINEUP:
4070 case SB_LINEDOWN:
4071 case SB_PAGEUP:
4072 case SB_PAGEDOWN:
4073 EDIT_EM_Scroll(wnd, es, action);
4074 return 0;
4075 case SB_TOP:
4076 dy = -es->y_offset;
4077 break;
4078 case SB_BOTTOM:
4079 dy = es->line_count - 1 - es->y_offset;
4080 break;
4081 case SB_THUMBTRACK:
4082 es->flags |= EF_VSCROLL_TRACK;
4083 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
4084 break;
4085 case SB_THUMBPOSITION:
4086 es->flags &= ~EF_VSCROLL_TRACK;
4087 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
4088 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4089 break;
4090 case SB_ENDSCROLL:
4091 break;
4094 * FIXME : the next two are undocumented !
4095 * Are we doing the right thing ?
4096 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
4097 * although it's also a regular control message.
4099 case EM_GETTHUMB16:
4100 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
4101 break;
4102 case EM_LINESCROLL16:
4103 dy = pos;
4104 break;
4106 default:
4107 ERR("undocumented (hacked) WM_VSCROLL parameter, please report\n");
4108 return 0;
4110 if (dy)
4111 EDIT_EM_LineScroll(wnd, es, 0, dy);
4112 return ret;
4116 /*********************************************************************
4118 * WM_VSCROLL
4121 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
4123 INT dy;
4125 if (!(es->style & ES_MULTILINE))
4126 return 0;
4128 if (!(es->style & ES_AUTOVSCROLL))
4129 return 0;
4131 if (!(es->style & WS_VSCROLL))
4132 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
4134 dy = 0;
4135 switch (action) {
4136 case SB_LINEUP:
4137 case SB_LINEDOWN:
4138 case SB_PAGEUP:
4139 case SB_PAGEDOWN:
4140 EDIT_EM_Scroll(wnd, es, action);
4141 return 0;
4143 case SB_TOP:
4144 dy = -es->y_offset;
4145 break;
4146 case SB_BOTTOM:
4147 dy = es->line_count - 1 - es->y_offset;
4148 break;
4149 case SB_THUMBTRACK:
4150 es->flags |= EF_VSCROLL_TRACK;
4151 dy = pos - es->y_offset;
4152 break;
4153 case SB_THUMBPOSITION:
4154 es->flags &= ~EF_VSCROLL_TRACK;
4155 if (!(dy = pos - es->y_offset)) {
4156 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
4157 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
4159 break;
4160 case SB_ENDSCROLL:
4161 break;
4163 default:
4164 ERR("undocumented WM_VSCROLL action %d, please report\n",
4165 action);
4166 return 0;
4168 if (dy)
4169 EDIT_EM_LineScroll(wnd, es, 0, dy);
4170 return 0;
4174 /*********************************************************************
4176 * EDIT_UpdateText
4179 static void EDIT_UpdateText(WND *wnd, LPRECT rc, BOOL bErase)
4181 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
4183 /* EF_UPDATE will be turned off in paint */
4184 if (es->flags & EF_UPDATE)
4185 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
4187 InvalidateRect(wnd->hwndSelf, rc, bErase);