Casts to (SEGPTR) removed. They did nothing anyway.
[wine/hacks.git] / controls / edit.c
blob9590fe33ccf2e38bd88eea389e6c5a5db6ec05b1
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 <string.h>
15 #include "winnt.h"
16 #include "win.h"
17 #include "wine/winbase16.h"
18 #include "combo.h"
19 #include "local.h"
20 #include "resource.h"
21 #include "debug.h"
22 #include "callback.h"
23 #include "tweak.h"
25 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
26 FIXME: BTW, new specs say 65535 (do you dare ???) */
27 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
28 #define BUFSTART_MULTI 1024 /* starting size */
29 #define BUFSTART_SINGLE 256 /* starting size */
30 #define GROWLENGTH 64 /* buffers grow by this much */
31 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
34 * extra flags for EDITSTATE.flags field
36 #define EF_MODIFIED 0x0001 /* text has been modified */
37 #define EF_FOCUSED 0x0002 /* we have input focus */
38 #define EF_UPDATE 0x0004 /* notify parent of changed state on next WM_PAINT */
39 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
40 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
41 #define EF_VSCROLL_HACK 0x0020 /* we already have informed the user of the hacked handler */
42 #define EF_HSCROLL_HACK 0x0040 /* we already have informed the user of the hacked handler */
43 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
44 wrapped line, instead of in front of the next character */
45 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
47 typedef enum
49 END_0 = 0, /* line ends with terminating '\0' character */
50 END_WRAP, /* line is wrapped */
51 END_HARD, /* line ends with a hard return '\r\n' */
52 END_SOFT /* line ends with a soft return '\r\r\n' */
53 } LINE_END;
55 typedef struct tagLINEDEF {
56 INT length; /* bruto length of a line in bytes */
57 INT net_length; /* netto length of a line in visible characters */
58 LINE_END ending;
59 INT width; /* width of the line in pixels */
60 struct tagLINEDEF *next;
61 } LINEDEF;
63 typedef struct
65 HANDLE heap; /* our own heap */
66 LPSTR text; /* the actual contents of the control */
67 INT buffer_size; /* the size of the buffer */
68 INT buffer_limit; /* the maximum size to which the buffer may grow */
69 HFONT font; /* NULL means standard system font */
70 INT x_offset; /* scroll offset for multi lines this is in pixels
71 for single lines it's in characters */
72 INT line_height; /* height of a screen line in pixels */
73 INT char_width; /* average character width in pixels */
74 DWORD style; /* sane version of wnd->dwStyle */
75 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
76 INT undo_insert_count; /* number of characters inserted in sequence */
77 INT undo_position; /* character index of the insertion and deletion */
78 LPSTR undo_text; /* deleted text */
79 INT undo_buffer_size; /* size of the deleted text buffer */
80 INT selection_start; /* == selection_end if no selection */
81 INT selection_end; /* == current caret position */
82 CHAR password_char; /* == 0 if no password char, and for multi line controls */
83 INT left_margin; /* in pixels */
84 INT right_margin; /* in pixels */
85 RECT format_rect;
86 INT region_posx; /* Position of cursor relative to region: */
87 INT region_posy; /* -1: to left, 0: within, 1: to right */
88 EDITWORDBREAKPROC16 word_break_proc16;
89 EDITWORDBREAKPROCA word_break_proc32A;
90 INT line_count; /* number of lines */
91 INT y_offset; /* scroll offset in number of lines */
93 * only for multi line controls
95 INT lock_count; /* amount of re-entries in the EditWndProc */
96 INT tabs_count;
97 LPINT tabs;
98 INT text_width; /* width of the widest line in pixels */
99 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
100 HLOCAL16 hloc16; /* for controls receiving EM_GETHANDLE16 */
101 HLOCAL hloc32; /* for controls receiving EM_GETHANDLE */
102 } EDITSTATE;
105 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
106 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
108 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
109 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
111 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
112 ({TRACE(edit, "notification " str " sent to hwnd=%08x\n", \
113 (UINT)(hwnd));})
114 /* used for disabled or read-only edit control */
115 #define EDIT_SEND_CTLCOLORSTATIC(wnd,hdc) \
116 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLORSTATIC, \
117 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
118 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
119 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
120 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
121 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
122 (DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str), \
123 SendMessageA((wnd)->parent->hwndSelf, WM_COMMAND, \
124 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
125 (LPARAM)(wnd)->hwndSelf))
126 #define DPRINTF_EDIT_MSG16(str) \
127 TRACE(edit, \
128 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
129 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
130 #define DPRINTF_EDIT_MSG32(str) \
131 TRACE(edit, \
132 "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
133 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
135 /*********************************************************************
137 * Declarations
142 * These functions have trivial implementations
143 * We still like to call them internally
144 * "static __inline__" makes them more like macro's
146 static __inline__ BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
147 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
148 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
149 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
151 * This is the only exported function
153 LRESULT WINAPI EditWndProc( HWND hwnd, UINT msg,
154 WPARAM wParam, LPARAM lParam );
156 * Helper functions only valid for one type of control
158 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
159 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
160 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
161 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
162 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
163 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
165 * Helper functions valid for both single line _and_ multi line controls
167 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action);
168 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
169 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y);
170 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
171 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
172 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
173 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size);
174 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size);
175 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
176 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
177 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
178 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
179 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
180 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
181 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
182 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
183 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
184 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
185 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
186 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action);
188 * EM_XXX message handlers
190 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
191 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol);
192 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
193 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
194 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch);
195 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end);
196 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
197 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index);
198 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line);
199 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index);
200 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
201 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
202 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace);
203 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
204 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
205 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
206 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
207 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit);
208 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action, INT left, INT right);
209 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
210 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
211 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs);
212 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs);
213 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp);
214 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
215 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
217 * WM_XXX message handlers
219 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
220 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
221 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y);
222 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
223 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs);
224 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
225 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
226 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text);
227 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
228 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
229 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus);
230 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
231 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
232 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
233 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
234 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs);
235 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es);
236 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
237 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus);
238 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
239 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
240 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
241 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
242 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc);
243 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
246 /*********************************************************************
248 * EM_CANUNDO
251 static __inline__ BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
253 return (es->undo_insert_count || lstrlenA(es->undo_text));
257 /*********************************************************************
259 * EM_EMPTYUNDOBUFFER
262 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
264 es->undo_insert_count = 0;
265 *es->undo_text = '\0';
269 /*********************************************************************
271 * WM_CLEAR
274 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
276 EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
280 /*********************************************************************
282 * WM_CUT
285 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
287 EDIT_WM_Copy(wnd, es);
288 EDIT_WM_Clear(wnd, es);
292 /*********************************************************************
294 * EditWndProc()
296 * The messages are in the order of the actual integer values
297 * (which can be found in include/windows.h)
298 * Whereever possible the 16 bit versions are converted to
299 * the 32 bit ones, so that we can 'fall through' to the
300 * helper functions. These are mostly 32 bit (with a few
301 * exceptions, clearly indicated by a '16' extension to their
302 * names).
305 LRESULT WINAPI EditWndProc( HWND hwnd, UINT msg,
306 WPARAM wParam, LPARAM lParam )
308 WND *wnd = WIN_FindWndPtr(hwnd);
309 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
310 LRESULT result = 0;
312 switch (msg) {
313 case WM_DESTROY:
314 DPRINTF_EDIT_MSG32("WM_DESTROY");
315 EDIT_WM_Destroy(wnd, es);
316 result = 0;
317 goto END;
319 case WM_NCCREATE:
320 DPRINTF_EDIT_MSG32("WM_NCCREATE");
321 result = EDIT_WM_NCCreate(wnd, (LPCREATESTRUCTA)lParam);
322 goto END;
325 if (!es)
327 result = DefWindowProcA(hwnd, msg, wParam, lParam);
328 goto END;
332 EDIT_LockBuffer(wnd, es);
333 switch (msg) {
334 case EM_GETSEL16:
335 DPRINTF_EDIT_MSG16("EM_GETSEL");
336 wParam = 0;
337 lParam = 0;
338 /* fall through */
339 case EM_GETSEL:
340 DPRINTF_EDIT_MSG32("EM_GETSEL");
341 result = EDIT_EM_GetSel(wnd, es, (LPUINT)wParam, (LPUINT)lParam);
342 break;
344 case EM_SETSEL16:
345 DPRINTF_EDIT_MSG16("EM_SETSEL");
346 if (SLOWORD(lParam) == -1)
347 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
348 else
349 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
350 if (!wParam)
351 EDIT_EM_ScrollCaret(wnd, es);
352 result = 1;
353 break;
354 case EM_SETSEL:
355 DPRINTF_EDIT_MSG32("EM_SETSEL");
356 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
357 result = 1;
358 break;
360 case EM_GETRECT16:
361 DPRINTF_EDIT_MSG16("EM_GETRECT");
362 if (lParam)
363 CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
364 break;
365 case EM_GETRECT:
366 DPRINTF_EDIT_MSG32("EM_GETRECT");
367 if (lParam)
368 CopyRect((LPRECT)lParam, &es->format_rect);
369 break;
371 case EM_SETRECT16:
372 DPRINTF_EDIT_MSG16("EM_SETRECT");
373 if ((es->style & ES_MULTILINE) && lParam) {
374 RECT rc;
375 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
376 EDIT_SetRectNP(wnd, es, &rc);
377 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
379 break;
380 case EM_SETRECT:
381 DPRINTF_EDIT_MSG32("EM_SETRECT");
382 if ((es->style & ES_MULTILINE) && lParam) {
383 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
384 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
386 break;
388 case EM_SETRECTNP16:
389 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
390 if ((es->style & ES_MULTILINE) && lParam) {
391 RECT rc;
392 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
393 EDIT_SetRectNP(wnd, es, &rc);
395 break;
396 case EM_SETRECTNP:
397 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
398 if ((es->style & ES_MULTILINE) && lParam)
399 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
400 break;
402 case EM_SCROLL16:
403 DPRINTF_EDIT_MSG16("EM_SCROLL");
404 /* fall through */
405 case EM_SCROLL:
406 DPRINTF_EDIT_MSG32("EM_SCROLL");
407 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
408 break;
410 case EM_LINESCROLL16:
411 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
412 wParam = (WPARAM)(INT)SHIWORD(lParam);
413 lParam = (LPARAM)(INT)SLOWORD(lParam);
414 /* fall through */
415 case EM_LINESCROLL:
416 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
417 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
418 break;
420 case EM_SCROLLCARET16:
421 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
422 /* fall through */
423 case EM_SCROLLCARET:
424 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
425 EDIT_EM_ScrollCaret(wnd, es);
426 result = 1;
427 break;
429 case EM_GETMODIFY16:
430 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
431 /* fall through */
432 case EM_GETMODIFY:
433 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
434 return ((es->flags & EF_MODIFIED) != 0);
435 break;
437 case EM_SETMODIFY16:
438 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
439 /* fall through */
440 case EM_SETMODIFY:
441 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
442 if (wParam)
443 es->flags |= EF_MODIFIED;
444 else
445 es->flags &= ~EF_MODIFIED;
446 break;
448 case EM_GETLINECOUNT16:
449 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
450 /* fall through */
451 case EM_GETLINECOUNT:
452 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
453 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
454 break;
456 case EM_LINEINDEX16:
457 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
458 if ((INT16)wParam == -1)
459 wParam = (WPARAM)-1;
460 /* fall through */
461 case EM_LINEINDEX:
462 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
463 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT)wParam);
464 break;
466 case EM_SETHANDLE16:
467 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
468 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
469 break;
470 case EM_SETHANDLE:
471 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
472 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
473 break;
475 case EM_GETHANDLE16:
476 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
477 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
478 break;
479 case EM_GETHANDLE:
480 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
481 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
482 break;
484 case EM_GETTHUMB16:
485 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
486 /* fall through */
487 case EM_GETTHUMB:
488 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
489 result = EDIT_EM_GetThumb(wnd, es);
490 break;
492 /* messages 0x00bf and 0x00c0 missing from specs */
494 case WM_USER+15:
495 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
496 /* fall through */
497 case 0x00bf:
498 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
499 result = DefWindowProcA(hwnd, msg, wParam, lParam);
500 break;
502 case WM_USER+16:
503 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
504 /* fall through */
505 case 0x00c0:
506 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
507 result = DefWindowProcA(hwnd, msg, wParam, lParam);
508 break;
510 case EM_LINELENGTH16:
511 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
512 /* fall through */
513 case EM_LINELENGTH:
514 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
515 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT)wParam);
516 break;
518 case EM_REPLACESEL16:
519 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
520 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
521 /* fall through */
522 case EM_REPLACESEL:
523 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
524 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, (LPCSTR)lParam);
525 result = 1;
526 break;
528 /* message 0x00c3 missing from specs */
530 case WM_USER+19:
531 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
532 /* fall through */
533 case 0x00c3:
534 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
535 result = DefWindowProcA(hwnd, msg, wParam, lParam);
536 break;
538 case EM_GETLINE16:
539 DPRINTF_EDIT_MSG16("EM_GETLINE");
540 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
541 /* fall through */
542 case EM_GETLINE:
543 DPRINTF_EDIT_MSG32("EM_GETLINE");
544 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT)wParam, (LPSTR)lParam);
545 break;
547 case EM_LIMITTEXT16:
548 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
549 /* fall through */
550 case EM_SETLIMITTEXT:
551 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
552 EDIT_EM_SetLimitText(wnd, es, (INT)wParam);
553 break;
555 case EM_CANUNDO16:
556 DPRINTF_EDIT_MSG16("EM_CANUNDO");
557 /* fall through */
558 case EM_CANUNDO:
559 DPRINTF_EDIT_MSG32("EM_CANUNDO");
560 result = (LRESULT)EDIT_EM_CanUndo(wnd, es);
561 break;
563 case EM_UNDO16:
564 DPRINTF_EDIT_MSG16("EM_UNDO");
565 /* fall through */
566 case EM_UNDO:
567 /* fall through */
568 case WM_UNDO:
569 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
570 result = (LRESULT)EDIT_EM_Undo(wnd, es);
571 break;
573 case EM_FMTLINES16:
574 DPRINTF_EDIT_MSG16("EM_FMTLINES");
575 /* fall through */
576 case EM_FMTLINES:
577 DPRINTF_EDIT_MSG32("EM_FMTLINES");
578 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL)wParam);
579 break;
581 case EM_LINEFROMCHAR16:
582 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
583 /* fall through */
584 case EM_LINEFROMCHAR:
585 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
586 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT)wParam);
587 break;
589 /* message 0x00ca missing from specs */
591 case WM_USER+26:
592 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
593 /* fall through */
594 case 0x00ca:
595 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
596 result = DefWindowProcA(hwnd, msg, wParam, lParam);
597 break;
599 case EM_SETTABSTOPS16:
600 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
601 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
602 break;
603 case EM_SETTABSTOPS:
604 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
605 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT)wParam, (LPINT)lParam);
606 break;
608 case EM_SETPASSWORDCHAR16:
609 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
610 /* fall through */
611 case EM_SETPASSWORDCHAR:
612 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
613 EDIT_EM_SetPasswordChar(wnd, es, (CHAR)wParam);
614 break;
616 case EM_EMPTYUNDOBUFFER16:
617 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
618 /* fall through */
619 case EM_EMPTYUNDOBUFFER:
620 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
621 EDIT_EM_EmptyUndoBuffer(wnd, es);
622 break;
624 case EM_GETFIRSTVISIBLELINE16:
625 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
626 result = es->y_offset;
627 break;
628 case EM_GETFIRSTVISIBLELINE:
629 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
630 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
631 break;
633 case EM_SETREADONLY16:
634 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
635 /* fall through */
636 case EM_SETREADONLY:
637 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
638 if (wParam) {
639 wnd->dwStyle |= ES_READONLY;
640 es->style |= ES_READONLY;
641 } else {
642 wnd->dwStyle &= ~ES_READONLY;
643 es->style &= ~ES_READONLY;
645 result = 1;
646 break;
648 case EM_SETWORDBREAKPROC16:
649 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
650 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
651 break;
652 case EM_SETWORDBREAKPROC:
653 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
654 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROCA)lParam);
655 break;
657 case EM_GETWORDBREAKPROC16:
658 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
659 result = (LRESULT)es->word_break_proc16;
660 break;
661 case EM_GETWORDBREAKPROC:
662 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
663 result = (LRESULT)es->word_break_proc32A;
664 break;
666 case EM_GETPASSWORDCHAR16:
667 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
668 /* fall through */
669 case EM_GETPASSWORDCHAR:
670 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
671 result = es->password_char;
672 break;
674 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
676 case EM_SETMARGINS:
677 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
678 EDIT_EM_SetMargins(wnd, es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
679 break;
681 case EM_GETMARGINS:
682 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
683 result = MAKELONG(es->left_margin, es->right_margin);
684 break;
686 case EM_GETLIMITTEXT:
687 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
688 result = es->buffer_limit;
689 break;
691 case EM_POSFROMCHAR:
692 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
693 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
694 break;
696 case EM_CHARFROMPOS:
697 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
698 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
699 break;
701 case WM_GETDLGCODE:
702 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
703 result = (es->style & ES_MULTILINE) ?
704 DLGC_WANTALLKEYS | DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS :
705 DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
706 break;
708 case WM_CHAR:
709 DPRINTF_EDIT_MSG32("WM_CHAR");
710 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
711 break;
713 case WM_CLEAR:
714 DPRINTF_EDIT_MSG32("WM_CLEAR");
715 EDIT_WM_Clear(wnd, es);
716 break;
718 case WM_COMMAND:
719 DPRINTF_EDIT_MSG32("WM_COMMAND");
720 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
721 break;
723 case WM_CONTEXTMENU:
724 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
725 EDIT_WM_ContextMenu(wnd, es, (HWND)wParam, SLOWORD(lParam), SHIWORD(lParam));
726 break;
728 case WM_COPY:
729 DPRINTF_EDIT_MSG32("WM_COPY");
730 EDIT_WM_Copy(wnd, es);
731 break;
733 case WM_CREATE:
734 DPRINTF_EDIT_MSG32("WM_CREATE");
735 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCTA)lParam);
736 break;
738 case WM_CUT:
739 DPRINTF_EDIT_MSG32("WM_CUT");
740 EDIT_WM_Cut(wnd, es);
741 break;
743 case WM_ENABLE:
744 DPRINTF_EDIT_MSG32("WM_ENABLE");
745 InvalidateRect(hwnd, NULL, TRUE);
746 break;
748 case WM_ERASEBKGND:
749 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
750 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
751 break;
753 case WM_GETFONT:
754 DPRINTF_EDIT_MSG32("WM_GETFONT");
755 result = (LRESULT)es->font;
756 break;
758 case WM_GETTEXT:
759 DPRINTF_EDIT_MSG32("WM_GETTEXT");
760 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT)wParam, (LPSTR)lParam);
761 break;
763 case WM_GETTEXTLENGTH:
764 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
765 result = lstrlenA(es->text);
766 break;
768 case WM_HSCROLL:
769 DPRINTF_EDIT_MSG32("WM_HSCROLL");
770 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)lParam);
771 break;
773 case WM_KEYDOWN:
774 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
775 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
776 break;
778 case WM_KILLFOCUS:
779 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
780 result = EDIT_WM_KillFocus(wnd, es, (HWND)wParam);
781 break;
783 case WM_LBUTTONDBLCLK:
784 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
785 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
786 break;
788 case WM_LBUTTONDOWN:
789 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
790 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
791 break;
793 case WM_LBUTTONUP:
794 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
795 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
796 break;
798 case WM_MOUSEACTIVATE:
800 * FIXME: maybe DefWindowProc() screws up, but it seems that
801 * modalless dialog boxes need this. If we don't do this, the focus
802 * will _not_ be set by DefWindowProc() for edit controls in a
803 * modalless dialog box ???
805 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
806 SetFocus(wnd->hwndSelf);
807 result = MA_ACTIVATE;
808 break;
810 case WM_MOUSEMOVE:
812 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
814 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
815 break;
817 case WM_PAINT:
818 DPRINTF_EDIT_MSG32("WM_PAINT");
819 EDIT_WM_Paint(wnd, es);
820 break;
822 case WM_PASTE:
823 DPRINTF_EDIT_MSG32("WM_PASTE");
824 EDIT_WM_Paste(wnd, es);
825 break;
827 case WM_SETFOCUS:
828 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
829 EDIT_WM_SetFocus(wnd, es, (HWND)wParam);
830 break;
832 case WM_SETFONT:
833 DPRINTF_EDIT_MSG32("WM_SETFONT");
834 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
835 break;
837 case WM_SETTEXT:
838 DPRINTF_EDIT_MSG32("WM_SETTEXT");
839 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
840 result = TRUE;
841 break;
843 case WM_SIZE:
844 DPRINTF_EDIT_MSG32("WM_SIZE");
845 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
846 break;
848 case WM_SYSKEYDOWN:
849 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
850 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
851 break;
853 case WM_TIMER:
854 DPRINTF_EDIT_MSG32("WM_TIMER");
855 EDIT_WM_Timer(wnd, es, (INT)wParam, (TIMERPROC)lParam);
856 break;
858 case WM_VSCROLL:
859 DPRINTF_EDIT_MSG32("WM_VSCROLL");
860 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)(lParam));
861 break;
863 default:
864 result = DefWindowProcA(hwnd, msg, wParam, lParam);
865 break;
867 EDIT_UnlockBuffer(wnd, es, FALSE);
868 END:
869 WIN_ReleaseWndPtr(wnd);
870 return result;
875 /*********************************************************************
877 * EDIT_BuildLineDefs_ML
879 * Build linked list of text lines.
880 * Lines can end with '\0' (last line), a character (if it is wrapped),
881 * a soft return '\r\r\n' or a hard return '\r\n'
884 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
886 HDC dc;
887 HFONT old_font = 0;
888 LPSTR start, cp;
889 INT fw;
890 LINEDEF *current_def;
891 LINEDEF **previous_next;
893 current_def = es->first_line_def;
894 do {
895 LINEDEF *next_def = current_def->next;
896 HeapFree(es->heap, 0, current_def);
897 current_def = next_def;
898 } while (current_def);
899 es->line_count = 0;
900 es->text_width = 0;
902 dc = GetDC(wnd->hwndSelf);
903 if (es->font)
904 old_font = SelectObject(dc, es->font);
906 fw = es->format_rect.right - es->format_rect.left;
907 start = es->text;
908 previous_next = &es->first_line_def;
909 do {
910 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
911 current_def->next = NULL;
912 cp = start;
913 while (*cp) {
914 if ((*cp == '\r') && (*(cp + 1) == '\n'))
915 break;
916 cp++;
918 if (!(*cp)) {
919 current_def->ending = END_0;
920 current_def->net_length = lstrlenA(start);
921 } else if ((cp > start) && (*(cp - 1) == '\r')) {
922 current_def->ending = END_SOFT;
923 current_def->net_length = cp - start - 1;
924 } else {
925 current_def->ending = END_HARD;
926 current_def->net_length = cp - start;
928 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
929 start, current_def->net_length,
930 es->tabs_count, es->tabs));
931 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
932 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
933 INT next = 0;
934 INT prev;
935 do {
936 prev = next;
937 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
938 prev + 1, current_def->net_length, WB_RIGHT);
939 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
940 start, next, es->tabs_count, es->tabs));
941 } while (current_def->width <= fw);
942 if (!prev) {
943 next = 0;
944 do {
945 prev = next;
946 next++;
947 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
948 start, next, es->tabs_count, es->tabs));
949 } while (current_def->width <= fw);
950 if (!prev)
951 prev = 1;
953 current_def->net_length = prev;
954 current_def->ending = END_WRAP;
955 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc, start,
956 current_def->net_length, es->tabs_count, es->tabs));
958 switch (current_def->ending) {
959 case END_SOFT:
960 current_def->length = current_def->net_length + 3;
961 break;
962 case END_HARD:
963 current_def->length = current_def->net_length + 2;
964 break;
965 case END_WRAP:
966 case END_0:
967 current_def->length = current_def->net_length;
968 break;
970 es->text_width = MAX(es->text_width, current_def->width);
971 start += current_def->length;
972 *previous_next = current_def;
973 previous_next = &current_def->next;
974 es->line_count++;
975 } while (current_def->ending != END_0);
976 if (es->font)
977 SelectObject(dc, old_font);
978 ReleaseDC(wnd->hwndSelf, dc);
982 /*********************************************************************
984 * EDIT_CallWordBreakProc
986 * Call appropriate WordBreakProc (internal or external).
988 * Note: The "start" argument should always be an index refering
989 * to es->text. The actual wordbreak proc might be
990 * 16 bit, so we can't always pass any 32 bit LPSTR.
991 * Hence we assume that es->text is the buffer that holds
992 * the string under examination (we can decide this for ourselves).
995 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action)
997 if (es->word_break_proc16) {
998 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
999 SEGPTR segptr = LocalLock16(hloc16);
1000 INT ret = (INT)Callbacks->CallWordBreakProc(es->word_break_proc16,
1001 segptr + start, index, count, action);
1002 LocalUnlock16(hloc16);
1003 return ret;
1005 else if (es->word_break_proc32A)
1007 TRACE(relay, "(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
1008 es->word_break_proc32A, es->text + start, index,
1009 count, action );
1010 return (INT)es->word_break_proc32A( es->text + start, index,
1011 count, action );
1013 else
1014 return EDIT_WordBreakProc(es->text + start, index, count, action);
1018 /*********************************************************************
1020 * EDIT_CharFromPos
1022 * Beware: This is not the function called on EM_CHARFROMPOS
1023 * The position _can_ be outside the formatting / client
1024 * rectangle
1025 * The return value is only the character index
1028 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1030 INT index;
1031 HDC dc;
1032 HFONT old_font = 0;
1034 if (es->style & ES_MULTILINE) {
1035 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1036 INT line_index = 0;
1037 LINEDEF *line_def = es->first_line_def;
1038 INT low, high;
1039 while ((line > 0) && line_def->next) {
1040 line_index += line_def->length;
1041 line_def = line_def->next;
1042 line--;
1044 x += es->x_offset - es->format_rect.left;
1045 if (x >= line_def->width) {
1046 if (after_wrap)
1047 *after_wrap = (line_def->ending == END_WRAP);
1048 return line_index + line_def->net_length;
1050 if (x <= 0) {
1051 if (after_wrap)
1052 *after_wrap = FALSE;
1053 return line_index;
1055 dc = GetDC(wnd->hwndSelf);
1056 if (es->font)
1057 old_font = SelectObject(dc, es->font);
1058 low = line_index + 1;
1059 high = line_index + line_def->net_length + 1;
1060 while (low < high - 1)
1062 INT mid = (low + high) / 2;
1063 if (LOWORD(GetTabbedTextExtentA(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1064 else low = mid;
1066 index = low;
1068 if (after_wrap)
1069 *after_wrap = ((index == line_index + line_def->net_length) &&
1070 (line_def->ending == END_WRAP));
1071 } else {
1072 LPSTR text;
1073 SIZE size;
1074 if (after_wrap)
1075 *after_wrap = FALSE;
1076 x -= es->format_rect.left;
1077 if (!x)
1078 return es->x_offset;
1079 text = EDIT_GetPasswordPointer_SL(wnd, es);
1080 dc = GetDC(wnd->hwndSelf);
1081 if (es->font)
1082 old_font = SelectObject(dc, es->font);
1083 if (x < 0)
1085 INT low = 0;
1086 INT high = es->x_offset;
1087 while (low < high - 1)
1089 INT mid = (low + high) / 2;
1090 GetTextExtentPoint32A( dc, text + mid,
1091 es->x_offset - mid, &size );
1092 if (size.cx > -x) low = mid;
1093 else high = mid;
1095 index = low;
1097 else
1099 INT low = es->x_offset;
1100 INT high = lstrlenA(es->text) + 1;
1101 while (low < high - 1)
1103 INT mid = (low + high) / 2;
1104 GetTextExtentPoint32A( dc, text + es->x_offset,
1105 mid - es->x_offset, &size );
1106 if (size.cx > x) high = mid;
1107 else low = mid;
1109 index = low;
1111 if (es->style & ES_PASSWORD)
1112 HeapFree(es->heap, 0 ,text);
1114 if (es->font)
1115 SelectObject(dc, old_font);
1116 ReleaseDC(wnd->hwndSelf, dc);
1117 return index;
1121 /*********************************************************************
1123 * EDIT_ConfinePoint
1125 * adjusts the point to be within the formatting rectangle
1126 * (so CharFromPos returns the nearest _visible_ character)
1129 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y)
1131 *x = MIN(MAX(*x, es->format_rect.left), es->format_rect.right - 1);
1132 *y = MIN(MAX(*y, es->format_rect.top), es->format_rect.bottom - 1);
1136 /*********************************************************************
1138 * EDIT_GetLineRect
1140 * Calculates the bounding rectangle for a line from a starting
1141 * column to an ending column.
1144 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1146 INT line_index = EDIT_EM_LineIndex(wnd, es, line);
1148 if (es->style & ES_MULTILINE)
1149 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1150 else
1151 rc->top = es->format_rect.top;
1152 rc->bottom = rc->top + es->line_height;
1153 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1154 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1158 /*********************************************************************
1160 * EDIT_GetPasswordPointer_SL
1162 * note: caller should free the (optionally) allocated buffer
1165 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1167 if (es->style & ES_PASSWORD) {
1168 INT len = lstrlenA(es->text);
1169 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1170 RtlFillMemory(text, len, es->password_char);
1171 text[len] = '\0';
1172 return text;
1173 } else
1174 return es->text;
1178 /*********************************************************************
1180 * EDIT_LockBuffer
1182 * This acts as a LOCAL_Lock(), but it locks only once. This way
1183 * you can call it whenever you like, without unlocking.
1186 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1188 if (!es) {
1189 ERR(edit, "no EDITSTATE ... please report\n");
1190 return;
1192 if (!(es->style & ES_MULTILINE))
1193 return;
1194 if (!es->text) {
1195 if (es->hloc32)
1196 es->text = LocalLock(es->hloc32);
1197 else if (es->hloc16)
1198 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1199 else {
1200 ERR(edit, "no buffer ... please report\n");
1201 return;
1204 es->lock_count++;
1208 /*********************************************************************
1210 * EDIT_SL_InvalidateText
1212 * Called from EDIT_InvalidateText().
1213 * Does the job for single-line controls only.
1216 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1218 RECT line_rect;
1219 RECT rc;
1221 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1222 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1223 InvalidateRect(wnd->hwndSelf, &rc, FALSE);
1227 /*********************************************************************
1229 * EDIT_ML_InvalidateText
1231 * Called from EDIT_InvalidateText().
1232 * Does the job for multi-line controls only.
1235 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1237 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1238 INT sl = EDIT_EM_LineFromChar(wnd, es, start);
1239 INT el = EDIT_EM_LineFromChar(wnd, es, end);
1240 INT sc;
1241 INT ec;
1242 RECT rc1;
1243 RECT rcWnd;
1244 RECT rcLine;
1245 RECT rcUpdate;
1246 INT l;
1248 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1249 return;
1251 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1252 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1253 if (sl < es->y_offset) {
1254 sl = es->y_offset;
1255 sc = 0;
1257 if (el > es->y_offset + vlc) {
1258 el = es->y_offset + vlc;
1259 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1261 GetClientRect(wnd->hwndSelf, &rc1);
1262 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1263 if (sl == el) {
1264 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1265 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1266 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1267 } else {
1268 EDIT_GetLineRect(wnd, es, sl, sc,
1269 EDIT_EM_LineLength(wnd, es,
1270 EDIT_EM_LineIndex(wnd, es, sl)),
1271 &rcLine);
1272 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1273 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1274 for (l = sl + 1 ; l < el ; l++) {
1275 EDIT_GetLineRect(wnd, es, l, 0,
1276 EDIT_EM_LineLength(wnd, es,
1277 EDIT_EM_LineIndex(wnd, es, l)),
1278 &rcLine);
1279 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1280 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1282 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1283 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1284 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1289 /*********************************************************************
1291 * EDIT_InvalidateText
1293 * Invalidate the text from offset start upto, but not including,
1294 * offset end. Useful for (re)painting the selection.
1295 * Regions outside the linewidth are not invalidated.
1296 * end == -1 means end == TextLength.
1297 * start and end need not be ordered.
1300 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1302 if (end == start)
1303 return;
1305 if (end == -1)
1306 end = lstrlenA(es->text);
1308 ORDER_INT(start, end);
1310 if (es->style & ES_MULTILINE)
1311 EDIT_ML_InvalidateText(wnd, es, start, end);
1312 else
1313 EDIT_SL_InvalidateText(wnd, es, start, end);
1317 /*********************************************************************
1319 * EDIT_MakeFit
1321 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1324 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size)
1326 HLOCAL hNew32;
1327 HLOCAL16 hNew16;
1329 if (size <= es->buffer_size)
1330 return TRUE;
1331 if (size > es->buffer_limit) {
1332 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1333 return FALSE;
1335 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1336 if (size > es->buffer_limit)
1337 size = es->buffer_limit;
1339 TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1341 EDIT_UnlockBuffer(wnd, es, TRUE);
1342 if (es->text) {
1343 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1344 es->buffer_size = MIN(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1345 else
1346 es->buffer_size = 0;
1347 } else if (es->hloc32) {
1348 if ((hNew32 = LocalReAlloc(es->hloc32, size + 1, 0))) {
1349 TRACE(edit, "Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1350 es->hloc32 = hNew32;
1351 es->buffer_size = MIN(LocalSize(es->hloc32) - 1, es->buffer_limit);
1353 } else if (es->hloc16) {
1354 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1355 TRACE(edit, "Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1356 es->hloc16 = hNew16;
1357 es->buffer_size = MIN(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1360 if (es->buffer_size < size) {
1361 EDIT_LockBuffer(wnd, es);
1362 WARN(edit, "FAILED ! We now have %d+1\n", es->buffer_size);
1363 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1364 return FALSE;
1365 } else {
1366 EDIT_LockBuffer(wnd, es);
1367 TRACE(edit, "We now have %d+1\n", es->buffer_size);
1368 return TRUE;
1373 /*********************************************************************
1375 * EDIT_MakeUndoFit
1377 * Try to fit size + 1 bytes in the undo buffer.
1380 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size)
1382 if (size <= es->undo_buffer_size)
1383 return TRUE;
1384 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1386 TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1388 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1389 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1390 if (es->undo_buffer_size < size) {
1391 WARN(edit, "FAILED ! We now have %d+1\n", es->undo_buffer_size);
1392 return FALSE;
1394 return TRUE;
1396 return FALSE;
1400 /*********************************************************************
1402 * EDIT_MoveBackward
1405 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1407 INT e = es->selection_end;
1409 if (e) {
1410 e--;
1411 if ((es->style & ES_MULTILINE) && e &&
1412 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1413 e--;
1414 if (e && (es->text[e - 1] == '\r'))
1415 e--;
1418 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1419 EDIT_EM_ScrollCaret(wnd, es);
1423 /*********************************************************************
1425 * EDIT_MoveDown_ML
1427 * Only for multi line controls
1428 * Move the caret one line down, on a column with the nearest
1429 * x coordinate on the screen (might be a different column).
1432 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1434 INT s = es->selection_start;
1435 INT e = es->selection_end;
1436 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1437 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1438 INT x = SLOWORD(pos);
1439 INT y = SHIWORD(pos);
1441 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1442 if (!extend)
1443 s = e;
1444 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1445 EDIT_EM_ScrollCaret(wnd, es);
1449 /*********************************************************************
1451 * EDIT_MoveEnd
1454 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1456 BOOL after_wrap = FALSE;
1457 INT e;
1459 if (es->style & ES_MULTILINE)
1460 e = EDIT_CharFromPos(wnd, es, 0x7fffffff,
1461 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1462 else
1463 e = lstrlenA(es->text);
1464 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1465 EDIT_EM_ScrollCaret(wnd, es);
1469 /*********************************************************************
1471 * EDIT_MoveForward
1474 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1476 INT e = es->selection_end;
1478 if (es->text[e]) {
1479 e++;
1480 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1481 if (es->text[e] == '\n')
1482 e++;
1483 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1484 e += 2;
1487 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1488 EDIT_EM_ScrollCaret(wnd, es);
1492 /*********************************************************************
1494 * EDIT_MoveHome
1496 * Home key: move to beginning of line.
1499 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1501 INT e;
1503 if (es->style & ES_MULTILINE)
1504 e = EDIT_CharFromPos(wnd, es, 0x80000000,
1505 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1506 else
1507 e = 0;
1508 EDIT_EM_SetSel(wnd, es, e, extend ? es->selection_start : e, FALSE);
1509 EDIT_EM_ScrollCaret(wnd, es);
1513 /*********************************************************************
1515 * EDIT_MovePageDown_ML
1517 * Only for multi line controls
1518 * Move the caret one page down, on a column with the nearest
1519 * x coordinate on the screen (might be a different column).
1522 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1524 INT s = es->selection_start;
1525 INT e = es->selection_end;
1526 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1527 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1528 INT x = SLOWORD(pos);
1529 INT y = SHIWORD(pos);
1531 e = EDIT_CharFromPos(wnd, es, x,
1532 y + (es->format_rect.bottom - es->format_rect.top),
1533 &after_wrap);
1534 if (!extend)
1535 s = e;
1536 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1537 EDIT_EM_ScrollCaret(wnd, es);
1541 /*********************************************************************
1543 * EDIT_MovePageUp_ML
1545 * Only for multi line controls
1546 * Move the caret one page up, on a column with the nearest
1547 * x coordinate on the screen (might be a different column).
1550 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1552 INT s = es->selection_start;
1553 INT e = es->selection_end;
1554 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1555 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1556 INT x = SLOWORD(pos);
1557 INT y = SHIWORD(pos);
1559 e = EDIT_CharFromPos(wnd, es, x,
1560 y - (es->format_rect.bottom - es->format_rect.top),
1561 &after_wrap);
1562 if (!extend)
1563 s = e;
1564 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1565 EDIT_EM_ScrollCaret(wnd, es);
1569 /*********************************************************************
1571 * EDIT_MoveUp_ML
1573 * Only for multi line controls
1574 * Move the caret one line up, on a column with the nearest
1575 * x coordinate on the screen (might be a different column).
1578 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1580 INT s = es->selection_start;
1581 INT e = es->selection_end;
1582 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1583 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1584 INT x = SLOWORD(pos);
1585 INT y = SHIWORD(pos);
1587 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1588 if (!extend)
1589 s = e;
1590 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1591 EDIT_EM_ScrollCaret(wnd, es);
1595 /*********************************************************************
1597 * EDIT_MoveWordBackward
1600 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1602 INT s = es->selection_start;
1603 INT e = es->selection_end;
1604 INT l;
1605 INT ll;
1606 INT li;
1608 l = EDIT_EM_LineFromChar(wnd, es, e);
1609 ll = EDIT_EM_LineLength(wnd, es, e);
1610 li = EDIT_EM_LineIndex(wnd, es, l);
1611 if (e - li == 0) {
1612 if (l) {
1613 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1614 e = li + EDIT_EM_LineLength(wnd, es, li);
1616 } else {
1617 e = li + (INT)EDIT_CallWordBreakProc(wnd, es,
1618 li, e - li, ll, WB_LEFT);
1620 if (!extend)
1621 s = e;
1622 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1623 EDIT_EM_ScrollCaret(wnd, es);
1627 /*********************************************************************
1629 * EDIT_MoveWordForward
1632 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1634 INT s = es->selection_start;
1635 INT e = es->selection_end;
1636 INT l;
1637 INT ll;
1638 INT li;
1640 l = EDIT_EM_LineFromChar(wnd, es, e);
1641 ll = EDIT_EM_LineLength(wnd, es, e);
1642 li = EDIT_EM_LineIndex(wnd, es, l);
1643 if (e - li == ll) {
1644 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1645 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1646 } else {
1647 e = li + EDIT_CallWordBreakProc(wnd, es,
1648 li, e - li + 1, ll, WB_RIGHT);
1650 if (!extend)
1651 s = e;
1652 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1653 EDIT_EM_ScrollCaret(wnd, es);
1657 /*********************************************************************
1659 * EDIT_PaintLine
1662 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1664 INT s = es->selection_start;
1665 INT e = es->selection_end;
1666 INT li;
1667 INT ll;
1668 INT x;
1669 INT y;
1670 LRESULT pos;
1672 if (es->style & ES_MULTILINE) {
1673 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1674 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1675 return;
1676 } else if (line)
1677 return;
1679 TRACE(edit, "line=%d\n", line);
1681 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1682 x = SLOWORD(pos);
1683 y = SHIWORD(pos);
1684 li = EDIT_EM_LineIndex(wnd, es, line);
1685 ll = EDIT_EM_LineLength(wnd, es, li);
1686 s = es->selection_start;
1687 e = es->selection_end;
1688 ORDER_INT(s, e);
1689 s = MIN(li + ll, MAX(li, s));
1690 e = MIN(li + ll, MAX(li, e));
1691 if (rev && (s != e) &&
1692 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1693 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1694 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1695 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1696 } else
1697 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1701 /*********************************************************************
1703 * EDIT_PaintText
1706 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1708 COLORREF BkColor;
1709 COLORREF TextColor;
1710 INT ret;
1711 INT li;
1712 SIZE size;
1714 if (!count)
1715 return 0;
1716 BkColor = GetBkColor(dc);
1717 TextColor = GetTextColor(dc);
1718 if (rev) {
1719 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
1720 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1722 li = EDIT_EM_LineIndex(wnd, es, line);
1723 if (es->style & ES_MULTILINE) {
1724 ret = (INT)LOWORD(TabbedTextOutA(dc, x, y, es->text + li + col, count,
1725 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1726 } else {
1727 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1728 TextOutA(dc, x, y, text + li + col, count);
1729 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1730 ret = size.cx;
1731 if (es->style & ES_PASSWORD)
1732 HeapFree(es->heap, 0, text);
1734 if (rev) {
1735 SetBkColor(dc, BkColor);
1736 SetTextColor(dc, TextColor);
1738 return ret;
1742 /*********************************************************************
1744 * EDIT_SetCaretPos
1747 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
1748 BOOL after_wrap)
1750 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1751 INT x = SLOWORD(res);
1752 INT y = SHIWORD(res);
1754 if(x < es->format_rect.left)
1755 x = es->format_rect.left;
1756 if(x > es->format_rect.right - 2)
1757 x = es->format_rect.right - 2;
1758 if(y > es->format_rect.bottom)
1759 y = es->format_rect.bottom;
1760 if(y < es->format_rect.top)
1761 y = es->format_rect.top;
1762 SetCaretPos(x, y);
1763 return;
1767 /*********************************************************************
1769 * EDIT_SetRectNP
1771 * note: this is not (exactly) the handler called on EM_SETRECTNP
1772 * it is also used to set the rect of a single line control
1775 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
1777 CopyRect(&es->format_rect, rc);
1778 if (es->style & WS_BORDER) {
1779 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
1780 if(TWEAK_WineLook == WIN31_LOOK)
1781 bw += 2;
1782 es->format_rect.left += bw;
1783 es->format_rect.top += bw;
1784 es->format_rect.right -= bw;
1785 es->format_rect.bottom -= bw;
1787 es->format_rect.left += es->left_margin;
1788 es->format_rect.right -= es->right_margin;
1789 es->format_rect.right = MAX(es->format_rect.right, es->format_rect.left + es->char_width);
1790 if (es->style & ES_MULTILINE)
1791 es->format_rect.bottom = es->format_rect.top +
1792 MAX(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1793 else
1794 es->format_rect.bottom = es->format_rect.top + es->line_height;
1795 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1796 EDIT_BuildLineDefs_ML(wnd, es);
1800 /*********************************************************************
1802 * EDIT_UnlockBuffer
1805 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
1807 if (!es) {
1808 ERR(edit, "no EDITSTATE ... please report\n");
1809 return;
1811 if (!(es->style & ES_MULTILINE))
1812 return;
1813 if (!es->lock_count) {
1814 ERR(edit, "lock_count == 0 ... please report\n");
1815 return;
1817 if (!es->text) {
1818 ERR(edit, "es->text == 0 ... please report\n");
1819 return;
1821 if (force || (es->lock_count == 1)) {
1822 if (es->hloc32) {
1823 LocalUnlock(es->hloc32);
1824 es->text = NULL;
1825 } else if (es->hloc16) {
1826 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1827 es->text = NULL;
1830 es->lock_count--;
1834 /*********************************************************************
1836 * EDIT_WordBreakProc
1838 * Find the beginning of words.
1839 * Note: unlike the specs for a WordBreakProc, this function only
1840 * allows to be called without linebreaks between s[0] upto
1841 * s[count - 1]. Remember it is only called
1842 * internally, so we can decide this for ourselves.
1845 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action)
1847 INT ret = 0;
1849 TRACE(edit, "s=%p, index=%u, count=%u, action=%d\n",
1850 s, index, count, action);
1852 switch (action) {
1853 case WB_LEFT:
1854 if (!count)
1855 break;
1856 if (index)
1857 index--;
1858 if (s[index] == ' ') {
1859 while (index && (s[index] == ' '))
1860 index--;
1861 if (index) {
1862 while (index && (s[index] != ' '))
1863 index--;
1864 if (s[index] == ' ')
1865 index++;
1867 } else {
1868 while (index && (s[index] != ' '))
1869 index--;
1870 if (s[index] == ' ')
1871 index++;
1873 ret = index;
1874 break;
1875 case WB_RIGHT:
1876 if (!count)
1877 break;
1878 if (index)
1879 index--;
1880 if (s[index] == ' ')
1881 while ((index < count) && (s[index] == ' ')) index++;
1882 else {
1883 while (s[index] && (s[index] != ' ') && (index < count))
1884 index++;
1885 while ((s[index] == ' ') && (index < count)) index++;
1887 ret = index;
1888 break;
1889 case WB_ISDELIMITER:
1890 ret = (s[index] == ' ');
1891 break;
1892 default:
1893 ERR(edit, "unknown action code, please report !\n");
1894 break;
1896 return ret;
1900 /*********************************************************************
1902 * EM_CHARFROMPOS
1904 * returns line number (not index) in high-order word of result.
1905 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
1906 * to Richedit, not to the edit control. Original documentation is valid.
1907 * FIXME: do the specs mean to return -1 if outside client area or
1908 * if outside formatting rectangle ???
1911 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y)
1913 POINT pt;
1914 RECT rc;
1915 INT index;
1917 pt.x = x;
1918 pt.y = y;
1919 GetClientRect(wnd->hwndSelf, &rc);
1920 if (!PtInRect(&rc, pt))
1921 return -1;
1923 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1924 return MAKELONG(index, EDIT_EM_LineFromChar(wnd, es, index));
1928 /*********************************************************************
1930 * EM_FMTLINES
1932 * Enable or disable soft breaks.
1934 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol)
1936 es->flags &= ~EF_USE_SOFTBRK;
1937 if (add_eol) {
1938 es->flags |= EF_USE_SOFTBRK;
1939 FIXME(edit, "soft break enabled, not implemented\n");
1941 return add_eol;
1945 /*********************************************************************
1947 * EM_GETHANDLE
1949 * Hopefully this won't fire back at us.
1950 * We always start with a fixed buffer in our own heap.
1951 * However, with this message a 32 bit application requests
1952 * a handle to 32 bit moveable local heap memory, where it expects
1953 * to find the text.
1954 * It's a pity that from this moment on we have to use this
1955 * local heap, because applications may rely on the handle
1956 * in the future.
1958 * In this function we'll try to switch to local heap.
1961 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
1963 HLOCAL newBuf;
1964 LPSTR newText;
1965 INT newSize;
1967 if (!(es->style & ES_MULTILINE))
1968 return 0;
1970 if (es->hloc32)
1971 return es->hloc32;
1972 else if (es->hloc16)
1973 return (HLOCAL)es->hloc16;
1975 if (!(newBuf = LocalAlloc(LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
1976 ERR(edit, "could not allocate new 32 bit buffer\n");
1977 return 0;
1979 newSize = MIN(LocalSize(newBuf) - 1, es->buffer_limit);
1980 if (!(newText = LocalLock(newBuf))) {
1981 ERR(edit, "could not lock new 32 bit buffer\n");
1982 LocalFree(newBuf);
1983 return 0;
1985 lstrcpyA(newText, es->text);
1986 EDIT_UnlockBuffer(wnd, es, TRUE);
1987 if (es->text)
1988 HeapFree(es->heap, 0, es->text);
1989 es->hloc32 = newBuf;
1990 es->hloc16 = (HLOCAL16)NULL;
1991 es->buffer_size = newSize;
1992 es->text = newText;
1993 EDIT_LockBuffer(wnd, es);
1994 TRACE(edit, "switched to 32 bit local heap\n");
1996 return es->hloc32;
2000 /*********************************************************************
2002 * EM_GETHANDLE16
2004 * Hopefully this won't fire back at us.
2005 * We always start with a buffer in 32 bit linear memory.
2006 * However, with this message a 16 bit application requests
2007 * a handle of 16 bit local heap memory, where it expects to find
2008 * the text.
2009 * It's a pitty that from this moment on we have to use this
2010 * local heap, because applications may rely on the handle
2011 * in the future.
2013 * In this function we'll try to switch to local heap.
2015 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2017 HLOCAL16 newBuf;
2018 LPSTR newText;
2019 INT newSize;
2021 if (!(es->style & ES_MULTILINE))
2022 return 0;
2024 if (es->hloc16)
2025 return es->hloc16;
2027 if (!LOCAL_HeapSize(wnd->hInstance)) {
2028 if (!LocalInit16(wnd->hInstance, 0,
2029 GlobalSize16(wnd->hInstance))) {
2030 ERR(edit, "could not initialize local heap\n");
2031 return 0;
2033 TRACE(edit, "local heap initialized\n");
2035 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2036 ERR(edit, "could not allocate new 16 bit buffer\n");
2037 return 0;
2039 newSize = MIN(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2040 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2041 ERR(edit, "could not lock new 16 bit buffer\n");
2042 LOCAL_Free(wnd->hInstance, newBuf);
2043 return 0;
2045 lstrcpyA(newText, es->text);
2046 EDIT_UnlockBuffer(wnd, es, TRUE);
2047 if (es->text)
2048 HeapFree(es->heap, 0, es->text);
2049 else if (es->hloc32) {
2050 while (LocalFree(es->hloc32)) ;
2051 LocalFree(es->hloc32);
2053 es->hloc32 = (HLOCAL)NULL;
2054 es->hloc16 = newBuf;
2055 es->buffer_size = newSize;
2056 es->text = newText;
2057 EDIT_LockBuffer(wnd, es);
2058 TRACE(edit, "switched to 16 bit buffer\n");
2060 return es->hloc16;
2064 /*********************************************************************
2066 * EM_GETLINE
2069 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch)
2071 LPSTR src;
2072 INT len;
2073 INT i;
2075 if (es->style & ES_MULTILINE) {
2076 if (line >= es->line_count)
2077 return 0;
2078 } else
2079 line = 0;
2080 i = EDIT_EM_LineIndex(wnd, es, line);
2081 src = es->text + i;
2082 len = MIN(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, i));
2083 for (i = 0 ; i < len ; i++) {
2084 *lpch = *src;
2085 src++;
2086 lpch++;
2088 return (LRESULT)len;
2092 /*********************************************************************
2094 * EM_GETSEL
2097 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end)
2099 UINT s = es->selection_start;
2100 UINT e = es->selection_end;
2102 ORDER_UINT(s, e);
2103 if (start)
2104 *start = s;
2105 if (end)
2106 *end = e;
2107 return MAKELONG(s, e);
2111 /*********************************************************************
2113 * EM_GETTHUMB
2115 * FIXME: is this right ? (or should it be only VSCROLL)
2116 * (and maybe only for edit controls that really have their
2117 * own scrollbars) (and maybe only for multiline controls ?)
2118 * All in all: very poorly documented
2120 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2121 * WM_VSCROLL handlers
2124 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2126 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0, 0),
2127 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0, 0));
2131 /*********************************************************************
2133 * EM_LINEFROMCHAR
2136 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index)
2138 INT line;
2139 LINEDEF *line_def;
2141 if (!(es->style & ES_MULTILINE))
2142 return 0;
2143 if (index > lstrlenA(es->text))
2144 return es->line_count - 1;
2145 if (index == -1)
2146 index = MIN(es->selection_start, es->selection_end);
2148 line = 0;
2149 line_def = es->first_line_def;
2150 index -= line_def->length;
2151 while ((index >= 0) && line_def->next) {
2152 line++;
2153 line_def = line_def->next;
2154 index -= line_def->length;
2156 return line;
2160 /*********************************************************************
2162 * EM_LINEINDEX
2165 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line)
2167 INT line_index;
2168 LINEDEF *line_def;
2170 if (!(es->style & ES_MULTILINE))
2171 return 0;
2172 if (line >= es->line_count)
2173 return -1;
2175 line_index = 0;
2176 line_def = es->first_line_def;
2177 if (line == -1) {
2178 INT index = es->selection_end - line_def->length;
2179 while ((index >= 0) && line_def->next) {
2180 line_index += line_def->length;
2181 line_def = line_def->next;
2182 index -= line_def->length;
2184 } else {
2185 while (line > 0) {
2186 line_index += line_def->length;
2187 line_def = line_def->next;
2188 line--;
2191 return line_index;
2195 /*********************************************************************
2197 * EM_LINELENGTH
2200 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index)
2202 LINEDEF *line_def;
2204 if (!(es->style & ES_MULTILINE))
2205 return lstrlenA(es->text);
2207 if (index == -1) {
2208 /* FIXME: broken
2209 INT32 sl = EDIT_EM_LineFromChar(wnd, es, es->selection_start);
2210 INT32 el = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2211 return es->selection_start - es->line_defs[sl].offset +
2212 es->line_defs[el].offset +
2213 es->line_defs[el].length - es->selection_end;
2215 return 0;
2217 line_def = es->first_line_def;
2218 index -= line_def->length;
2219 while ((index >= 0) && line_def->next) {
2220 line_def = line_def->next;
2221 index -= line_def->length;
2223 return line_def->net_length;
2227 /*********************************************************************
2229 * EM_LINESCROLL
2231 * FIXME: dx is in average character widths
2232 * However, we assume it is in pixels when we use this
2233 * function internally
2236 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2238 INT nyoff;
2240 if (!(es->style & ES_MULTILINE))
2241 return FALSE;
2243 if (-dx > es->x_offset)
2244 dx = -es->x_offset;
2245 if (dx > es->text_width - es->x_offset)
2246 dx = es->text_width - es->x_offset;
2247 nyoff = MAX(0, es->y_offset + dy);
2248 if (nyoff >= es->line_count)
2249 nyoff = es->line_count - 1;
2250 dy = (es->y_offset - nyoff) * es->line_height;
2251 if (dx || dy) {
2252 RECT rc1;
2253 RECT rc;
2254 GetClientRect(wnd->hwndSelf, &rc1);
2255 IntersectRect(&rc, &rc1, &es->format_rect);
2256 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2257 NULL, &rc, (HRGN)NULL, NULL, SW_INVALIDATE);
2258 es->y_offset = nyoff;
2259 es->x_offset += dx;
2261 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2262 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2263 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2264 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2265 return TRUE;
2269 /*********************************************************************
2271 * EM_POSFROMCHAR
2274 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap)
2276 INT len = lstrlenA(es->text);
2277 INT l;
2278 INT li;
2279 INT x;
2280 INT y = 0;
2281 HDC dc;
2282 HFONT old_font = 0;
2283 SIZE size;
2285 index = MIN(index, len);
2286 dc = GetDC(wnd->hwndSelf);
2287 if (es->font)
2288 old_font = SelectObject(dc, es->font);
2289 if (es->style & ES_MULTILINE) {
2290 l = EDIT_EM_LineFromChar(wnd, es, index);
2291 y = (l - es->y_offset) * es->line_height;
2292 li = EDIT_EM_LineIndex(wnd, es, l);
2293 if (after_wrap && (li == index) && l) {
2294 INT l2 = l - 1;
2295 LINEDEF *line_def = es->first_line_def;
2296 while (l2) {
2297 line_def = line_def->next;
2298 l2--;
2300 if (line_def->ending == END_WRAP) {
2301 l--;
2302 y -= es->line_height;
2303 li = EDIT_EM_LineIndex(wnd, es, l);
2306 x = LOWORD(GetTabbedTextExtentA(dc, es->text + li, index - li,
2307 es->tabs_count, es->tabs)) - es->x_offset;
2308 } else {
2309 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
2310 if (index < es->x_offset) {
2311 GetTextExtentPoint32A(dc, text + index,
2312 es->x_offset - index, &size);
2313 x = -size.cx;
2314 } else {
2315 GetTextExtentPoint32A(dc, text + es->x_offset,
2316 index - es->x_offset, &size);
2317 x = size.cx;
2319 y = 0;
2320 if (es->style & ES_PASSWORD)
2321 HeapFree(es->heap, 0 ,text);
2323 x += es->format_rect.left;
2324 y += es->format_rect.top;
2325 if (es->font)
2326 SelectObject(dc, old_font);
2327 ReleaseDC(wnd->hwndSelf, dc);
2328 return MAKELONG((INT16)x, (INT16)y);
2332 /*********************************************************************
2334 * EM_REPLACESEL
2336 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2339 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace)
2341 INT strl = lstrlenA(lpsz_replace);
2342 INT tl = lstrlenA(es->text);
2343 INT utl;
2344 UINT s;
2345 UINT e;
2346 INT i;
2347 LPSTR p;
2349 s = es->selection_start;
2350 e = es->selection_end;
2352 if ((s == e) && !strl)
2353 return;
2355 ORDER_UINT(s, e);
2357 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2358 return;
2360 if (e != s) {
2361 /* there is something to be deleted */
2362 if (can_undo) {
2363 utl = lstrlenA(es->undo_text);
2364 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2365 /* undo-buffer is extended to the right */
2366 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2367 lstrcpynA(es->undo_text + utl, es->text + s, e - s + 1);
2368 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2369 /* undo-buffer is extended to the left */
2370 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2371 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2372 p[e - s] = p[0];
2373 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2374 p[i] = (es->text + s)[i];
2375 es->undo_position = s;
2376 } else {
2377 /* new undo-buffer */
2378 EDIT_MakeUndoFit(wnd, es, e - s);
2379 lstrcpynA(es->undo_text, es->text + s, e - s + 1);
2380 es->undo_position = s;
2382 /* any deletion makes the old insertion-undo invalid */
2383 es->undo_insert_count = 0;
2384 } else
2385 EDIT_EM_EmptyUndoBuffer(wnd, es);
2387 /* now delete */
2388 lstrcpyA(es->text + s, es->text + e);
2390 if (strl) {
2391 /* there is an insertion */
2392 if (can_undo) {
2393 if ((s == es->undo_position) ||
2394 ((es->undo_insert_count) &&
2395 (s == es->undo_position + es->undo_insert_count)))
2397 * insertion is new and at delete position or
2398 * an extension to either left or right
2400 es->undo_insert_count += strl;
2401 else {
2402 /* new insertion undo */
2403 es->undo_position = s;
2404 es->undo_insert_count = strl;
2405 /* new insertion makes old delete-buffer invalid */
2406 *es->undo_text = '\0';
2408 } else
2409 EDIT_EM_EmptyUndoBuffer(wnd, es);
2411 /* now insert */
2412 tl = lstrlenA(es->text);
2413 for (p = es->text + tl ; p >= es->text + s ; p--)
2414 p[strl] = p[0];
2415 for (i = 0 , p = es->text + s ; i < strl ; i++)
2416 p[i] = lpsz_replace[i];
2417 if(es->style & ES_UPPERCASE)
2418 CharUpperBuffA(p, strl);
2419 else if(es->style & ES_LOWERCASE)
2420 CharLowerBuffA(p, strl);
2421 s += strl;
2423 /* FIXME: really inefficient */
2424 if (es->style & ES_MULTILINE)
2425 EDIT_BuildLineDefs_ML(wnd, es);
2427 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2428 es->flags |= EF_MODIFIED;
2429 es->flags |= EF_UPDATE;
2430 EDIT_EM_ScrollCaret(wnd, es);
2432 /* FIXME: really inefficient */
2433 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2437 /*********************************************************************
2439 * EM_SCROLL
2442 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2444 INT dy;
2446 if (!(es->style & ES_MULTILINE))
2447 return (LRESULT)FALSE;
2449 dy = 0;
2451 switch (action) {
2452 case SB_LINEUP:
2453 if (es->y_offset)
2454 dy = -1;
2455 break;
2456 case SB_LINEDOWN:
2457 if (es->y_offset < es->line_count - 1)
2458 dy = 1;
2459 break;
2460 case SB_PAGEUP:
2461 if (es->y_offset)
2462 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2463 break;
2464 case SB_PAGEDOWN:
2465 if (es->y_offset < es->line_count - 1)
2466 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2467 break;
2468 default:
2469 return (LRESULT)FALSE;
2471 if (dy) {
2472 EDIT_EM_LineScroll(wnd, es, 0, dy);
2473 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2475 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2479 /*********************************************************************
2481 * EM_SCROLLCARET
2484 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2486 if (es->style & ES_MULTILINE) {
2487 INT l;
2488 INT li;
2489 INT vlc;
2490 INT ww;
2491 INT cw = es->char_width;
2492 INT x;
2493 INT dy = 0;
2494 INT dx = 0;
2496 l = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2497 li = EDIT_EM_LineIndex(wnd, es, l);
2498 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2499 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2500 if (l >= es->y_offset + vlc)
2501 dy = l - vlc + 1 - es->y_offset;
2502 if (l < es->y_offset)
2503 dy = l - es->y_offset;
2504 ww = es->format_rect.right - es->format_rect.left;
2505 if (x < es->format_rect.left)
2506 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2507 if (x > es->format_rect.right)
2508 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2509 if (dy || dx)
2510 EDIT_EM_LineScroll(wnd, es, dx, dy);
2511 } else {
2512 INT x;
2513 INT goal;
2514 INT format_width;
2516 if (!(es->style & ES_AUTOHSCROLL))
2517 return;
2519 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2520 format_width = es->format_rect.right - es->format_rect.left;
2521 if (x < es->format_rect.left) {
2522 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2523 do {
2524 es->x_offset--;
2525 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2526 } while ((x < goal) && es->x_offset);
2527 /* FIXME: use ScrollWindow() somehow to improve performance */
2528 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2529 } else if (x > es->format_rect.right) {
2530 INT x_last;
2531 INT len = lstrlenA(es->text);
2532 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2533 do {
2534 es->x_offset++;
2535 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2536 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2537 } while ((x > goal) && (x_last > es->format_rect.right));
2538 /* FIXME: use ScrollWindow() somehow to improve performance */
2539 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2545 /*********************************************************************
2547 * EM_SETHANDLE
2549 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2552 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc)
2554 if (!(es->style & ES_MULTILINE))
2555 return;
2557 if (!hloc) {
2558 WARN(edit, "called with NULL handle\n");
2559 return;
2562 EDIT_UnlockBuffer(wnd, es, TRUE);
2564 * old buffer is freed by caller, unless
2565 * it is still in our own heap. (in that case
2566 * we free it, correcting the buggy caller.)
2568 if (es->text)
2569 HeapFree(es->heap, 0, es->text);
2571 es->hloc16 = (HLOCAL16)NULL;
2572 es->hloc32 = hloc;
2573 es->text = NULL;
2574 es->buffer_size = LocalSize(es->hloc32) - 1;
2575 EDIT_LockBuffer(wnd, es);
2577 es->x_offset = es->y_offset = 0;
2578 es->selection_start = es->selection_end = 0;
2579 EDIT_EM_EmptyUndoBuffer(wnd, es);
2580 es->flags &= ~EF_MODIFIED;
2581 es->flags &= ~EF_UPDATE;
2582 EDIT_BuildLineDefs_ML(wnd, es);
2583 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2584 EDIT_EM_ScrollCaret(wnd, es);
2588 /*********************************************************************
2590 * EM_SETHANDLE16
2592 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2595 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2597 if (!(es->style & ES_MULTILINE))
2598 return;
2600 if (!hloc) {
2601 WARN(edit, "called with NULL handle\n");
2602 return;
2605 EDIT_UnlockBuffer(wnd, es, TRUE);
2607 * old buffer is freed by caller, unless
2608 * it is still in our own heap. (in that case
2609 * we free it, correcting the buggy caller.)
2611 if (es->text)
2612 HeapFree(es->heap, 0, es->text);
2614 es->hloc16 = hloc;
2615 es->hloc32 = (HLOCAL)NULL;
2616 es->text = NULL;
2617 es->buffer_size = LOCAL_Size(wnd->hInstance, es->hloc16) - 1;
2618 EDIT_LockBuffer(wnd, es);
2620 es->x_offset = es->y_offset = 0;
2621 es->selection_start = es->selection_end = 0;
2622 EDIT_EM_EmptyUndoBuffer(wnd, es);
2623 es->flags &= ~EF_MODIFIED;
2624 es->flags &= ~EF_UPDATE;
2625 EDIT_BuildLineDefs_ML(wnd, es);
2626 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2627 EDIT_EM_ScrollCaret(wnd, es);
2631 /*********************************************************************
2633 * EM_SETLIMITTEXT
2635 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
2636 * However, the windows version is not complied to yet in all of edit.c
2639 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit)
2641 if (es->style & ES_MULTILINE) {
2642 if (limit)
2643 es->buffer_limit = MIN(limit, BUFLIMIT_MULTI);
2644 else
2645 es->buffer_limit = BUFLIMIT_MULTI;
2646 } else {
2647 if (limit)
2648 es->buffer_limit = MIN(limit, BUFLIMIT_SINGLE);
2649 else
2650 es->buffer_limit = BUFLIMIT_SINGLE;
2655 /*********************************************************************
2657 * EM_SETMARGINS
2659 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2660 * action wParam despite what the docs say. EC_USEFONTINFO means one third
2661 * of the char's width, according to the new docs.
2664 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action,
2665 INT left, INT right)
2667 if (action & EC_LEFTMARGIN) {
2668 if (left != EC_USEFONTINFO)
2669 es->left_margin = left;
2670 else
2671 es->left_margin = es->char_width / 3;
2674 if (action & EC_RIGHTMARGIN) {
2675 if (right != EC_USEFONTINFO)
2676 es->right_margin = right;
2677 else
2678 es->right_margin = es->char_width / 3;
2680 TRACE(edit, "left=%d, right=%d\n", es->left_margin, es->right_margin);
2684 /*********************************************************************
2686 * EM_SETPASSWORDCHAR
2689 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c)
2691 if (es->style & ES_MULTILINE)
2692 return;
2694 if (es->password_char == c)
2695 return;
2697 es->password_char = c;
2698 if (c) {
2699 wnd->dwStyle |= ES_PASSWORD;
2700 es->style |= ES_PASSWORD;
2701 } else {
2702 wnd->dwStyle &= ~ES_PASSWORD;
2703 es->style &= ~ES_PASSWORD;
2705 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2709 /*********************************************************************
2711 * EDIT_EM_SetSel
2713 * note: unlike the specs say: the order of start and end
2714 * _is_ preserved in Windows. (i.e. start can be > end)
2715 * In other words: this handler is OK
2718 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap)
2720 UINT old_start = es->selection_start;
2721 UINT old_end = es->selection_end;
2722 UINT len = lstrlenA(es->text);
2724 if (start == -1) {
2725 start = es->selection_end;
2726 end = es->selection_end;
2727 } else {
2728 start = MIN(start, len);
2729 end = MIN(end, len);
2731 es->selection_start = start;
2732 es->selection_end = end;
2733 if (after_wrap)
2734 es->flags |= EF_AFTER_WRAP;
2735 else
2736 es->flags &= ~EF_AFTER_WRAP;
2737 if (es->flags & EF_FOCUSED)
2738 EDIT_SetCaretPos(wnd, es, end, after_wrap);
2739 /* This is little bit more efficient than before, not sure if it can be improved. FIXME? */
2740 ORDER_UINT(start, end);
2741 ORDER_UINT(end, old_end);
2742 ORDER_UINT(start, old_start);
2743 ORDER_UINT(old_start, old_end);
2744 if (end != old_start)
2747 * One can also do
2748 * ORDER_UINT32(end, old_start);
2749 * EDIT_InvalidateText(wnd, es, start, end);
2750 * EDIT_InvalidateText(wnd, es, old_start, old_end);
2751 * in place of the following if statement.
2753 if (old_start > end )
2755 EDIT_InvalidateText(wnd, es, start, end);
2756 EDIT_InvalidateText(wnd, es, old_start, old_end);
2758 else
2760 EDIT_InvalidateText(wnd, es, start, old_start);
2761 EDIT_InvalidateText(wnd, es, end, old_end);
2764 else EDIT_InvalidateText(wnd, es, start, old_end);
2768 /*********************************************************************
2770 * EM_SETTABSTOPS
2773 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs)
2775 if (!(es->style & ES_MULTILINE))
2776 return FALSE;
2777 if (es->tabs)
2778 HeapFree(es->heap, 0, es->tabs);
2779 es->tabs_count = count;
2780 if (!count)
2781 es->tabs = NULL;
2782 else {
2783 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2784 memcpy(es->tabs, tabs, count * sizeof(INT));
2786 return TRUE;
2790 /*********************************************************************
2792 * EM_SETTABSTOPS16
2795 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs)
2797 if (!(es->style & ES_MULTILINE))
2798 return FALSE;
2799 if (es->tabs)
2800 HeapFree(es->heap, 0, es->tabs);
2801 es->tabs_count = count;
2802 if (!count)
2803 es->tabs = NULL;
2804 else {
2805 INT i;
2806 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
2807 for (i = 0 ; i < count ; i++)
2808 es->tabs[i] = *tabs++;
2810 return TRUE;
2814 /*********************************************************************
2816 * EM_SETWORDBREAKPROC
2819 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp)
2821 if (es->word_break_proc32A == wbp)
2822 return;
2824 es->word_break_proc32A = wbp;
2825 es->word_break_proc16 = NULL;
2826 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2827 EDIT_BuildLineDefs_ML(wnd, es);
2828 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2833 /*********************************************************************
2835 * EM_SETWORDBREAKPROC16
2838 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
2840 if (es->word_break_proc16 == wbp)
2841 return;
2843 es->word_break_proc32A = NULL;
2844 es->word_break_proc16 = wbp;
2845 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2846 EDIT_BuildLineDefs_ML(wnd, es);
2847 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2852 /*********************************************************************
2854 * EM_UNDO / WM_UNDO
2857 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2859 INT ulength = lstrlenA(es->undo_text);
2860 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2862 lstrcpyA(utext, es->undo_text);
2864 TRACE(edit, "before UNDO:insertion length = %d, deletion buffer = %s\n",
2865 es->undo_insert_count, utext);
2867 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2868 EDIT_EM_EmptyUndoBuffer(wnd, es);
2869 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext);
2870 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2871 HeapFree(es->heap, 0, utext);
2873 TRACE(edit, "after UNDO:insertion length = %d, deletion buffer = %s\n",
2874 es->undo_insert_count, es->undo_text);
2876 return TRUE;
2880 /*********************************************************************
2882 * WM_CHAR
2885 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data)
2887 switch (c) {
2888 case '\r':
2889 case '\n':
2890 if (es->style & ES_MULTILINE) {
2891 if (es->style & ES_READONLY) {
2892 EDIT_MoveHome(wnd, es, FALSE);
2893 EDIT_MoveDown_ML(wnd, es, FALSE);
2894 } else
2895 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\r\n");
2897 break;
2898 case '\t':
2899 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2900 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\t");
2901 break;
2902 default:
2903 if (!(es->style & ES_READONLY) && ((BYTE)c >= ' ') && (c != 127)) {
2904 char str[2];
2905 str[0] = c;
2906 str[1] = '\0';
2907 EDIT_EM_ReplaceSel(wnd, es, TRUE, str);
2909 break;
2914 /*********************************************************************
2916 * WM_COMMAND
2919 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND control)
2921 if (code || control)
2922 return;
2924 switch (id) {
2925 case EM_UNDO:
2926 EDIT_EM_Undo(wnd, es);
2927 break;
2928 case WM_CUT:
2929 EDIT_WM_Cut(wnd, es);
2930 break;
2931 case WM_COPY:
2932 EDIT_WM_Copy(wnd, es);
2933 break;
2934 case WM_PASTE:
2935 EDIT_WM_Paste(wnd, es);
2936 break;
2937 case WM_CLEAR:
2938 EDIT_WM_Clear(wnd, es);
2939 break;
2940 case EM_SETSEL:
2941 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
2942 EDIT_EM_ScrollCaret(wnd, es);
2943 break;
2944 default:
2945 ERR(edit, "unknown menu item, please report\n");
2946 break;
2951 /*********************************************************************
2953 * WM_CONTEXTMENU
2955 * Note: the resource files resource/sysres_??.rc cannot define a
2956 * single popup menu. Hence we use a (dummy) menubar
2957 * containing the single popup menu as its first item.
2959 * FIXME: the message identifiers have been chosen arbitrarily,
2960 * hence we use MF_BYPOSITION.
2961 * We might as well use the "real" values (anybody knows ?)
2962 * The menu definition is in resources/sysres_??.rc.
2963 * Once these are OK, we better use MF_BYCOMMAND here
2964 * (as we do in EDIT_WM_Command()).
2967 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y)
2969 HMENU menu = LoadMenuIndirectA(SYSRES_GetResPtr(SYSRES_MENU_EDITMENU));
2970 HMENU popup = GetSubMenu(menu, 0);
2971 UINT start = es->selection_start;
2972 UINT end = es->selection_end;
2974 ORDER_UINT(start, end);
2976 /* undo */
2977 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
2978 /* cut */
2979 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2980 /* copy */
2981 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2982 /* paste */
2983 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
2984 /* delete */
2985 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
2986 /* select all */
2987 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != lstrlenA(es->text)) ? MF_ENABLED : MF_GRAYED));
2989 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
2990 DestroyMenu(menu);
2994 /*********************************************************************
2996 * WM_COPY
2999 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
3001 INT s = es->selection_start;
3002 INT e = es->selection_end;
3003 HGLOBAL hdst;
3004 LPSTR dst;
3006 if (e == s)
3007 return;
3008 ORDER_INT(s, e);
3009 hdst = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3010 dst = GlobalLock(hdst);
3011 lstrcpynA(dst, es->text + s, e - s + 1);
3012 GlobalUnlock(hdst);
3013 OpenClipboard(wnd->hwndSelf);
3014 EmptyClipboard();
3015 SetClipboardData(CF_TEXT, hdst);
3016 CloseClipboard();
3020 /*********************************************************************
3022 * WM_CREATE
3025 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs)
3028 * To initialize some final structure members, we call some helper
3029 * functions. However, since the EDITSTATE is not consistent (i.e.
3030 * not fully initialized), we should be very careful which
3031 * functions can be called, and in what order.
3033 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3034 EDIT_EM_EmptyUndoBuffer(wnd, es);
3036 if (cs->lpszName && *(cs->lpszName) != '\0') {
3037 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3038 /* if we insert text to the editline, the text scrolls out of the window, as the caret is placed after the insert pos normally; thus we reset es->selection... to 0 and update caret */
3039 es->selection_start = es->selection_end = 0;
3040 EDIT_EM_ScrollCaret(wnd, es);
3042 return 0;
3046 /*********************************************************************
3048 * WM_DESTROY
3051 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3053 if (es->hloc32) {
3054 while (LocalUnlock(es->hloc32)) ;
3055 LocalFree(es->hloc32);
3057 if (es->hloc16) {
3058 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3059 LOCAL_Free(wnd->hInstance, es->hloc16);
3061 HeapDestroy(es->heap);
3062 HeapFree(GetProcessHeap(), 0, es);
3063 *(EDITSTATE **)wnd->wExtra = NULL;
3067 /*********************************************************************
3069 * WM_ERASEBKGND
3072 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3074 HBRUSH brush;
3075 RECT rc;
3077 if (IsWindowEnabled(wnd->hwndSelf) || (es->style & ES_READONLY))
3078 brush = (HBRUSH)EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3079 else
3080 brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc);
3082 if (!brush)
3083 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3085 GetClientRect(wnd->hwndSelf, &rc);
3086 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3087 GetClipBox(dc, &rc);
3089 * FIXME: specs say that we should UnrealizeObject() the brush,
3090 * but the specs of UnrealizeObject() say that we shouldn't
3091 * unrealize a stock object. The default brush that
3092 * DefWndProc() returns is ... a stock object.
3094 FillRect(dc, &rc, brush);
3095 return -1;
3099 /*********************************************************************
3101 * WM_GETTEXT
3104 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text)
3106 INT len = lstrlenA(es->text);
3108 if (count > len) {
3109 lstrcpyA(text, es->text);
3110 return len;
3111 } else
3112 return -1;
3116 /*********************************************************************
3118 * EDIT_HScroll_Hack
3120 * 16 bit notepad needs this. Actually it is not _our_ hack,
3121 * it is notepad's. Notepad is sending us scrollbar messages with
3122 * undocumented parameters without us even having a scrollbar ... !?!?
3125 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3127 INT dx = 0;
3128 INT fw = es->format_rect.right - es->format_rect.left;
3129 LRESULT ret = 0;
3131 if (!(es->flags & EF_HSCROLL_HACK)) {
3132 ERR(edit, "hacked WM_HSCROLL handler invoked\n");
3133 ERR(edit, " if you are _not_ running 16 bit notepad, please report\n");
3134 ERR(edit, " (this message is only displayed once per edit control)\n");
3135 es->flags |= EF_HSCROLL_HACK;
3138 switch (action) {
3139 case SB_LINELEFT:
3140 if (es->x_offset)
3141 dx = -es->char_width;
3142 break;
3143 case SB_LINERIGHT:
3144 if (es->x_offset < es->text_width)
3145 dx = es->char_width;
3146 break;
3147 case SB_PAGELEFT:
3148 if (es->x_offset)
3149 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3150 break;
3151 case SB_PAGERIGHT:
3152 if (es->x_offset < es->text_width)
3153 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3154 break;
3155 case SB_LEFT:
3156 if (es->x_offset)
3157 dx = -es->x_offset;
3158 break;
3159 case SB_RIGHT:
3160 if (es->x_offset < es->text_width)
3161 dx = es->text_width - es->x_offset;
3162 break;
3163 case SB_THUMBTRACK:
3164 es->flags |= EF_HSCROLL_TRACK;
3165 dx = pos * es->text_width / 100 - es->x_offset;
3166 break;
3167 case SB_THUMBPOSITION:
3168 es->flags &= ~EF_HSCROLL_TRACK;
3169 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3170 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3171 break;
3172 case SB_ENDSCROLL:
3173 break;
3176 * FIXME : the next two are undocumented !
3177 * Are we doing the right thing ?
3178 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3179 * although it's also a regular control message.
3181 case EM_GETTHUMB16:
3182 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3183 break;
3184 case EM_LINESCROLL16:
3185 dx = pos;
3186 break;
3188 default:
3189 ERR(edit, "undocumented (hacked) WM_HSCROLL parameter, please report\n");
3190 return 0;
3192 if (dx)
3193 EDIT_EM_LineScroll(wnd, es, dx, 0);
3194 return ret;
3198 /*********************************************************************
3200 * WM_HSCROLL
3203 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3205 INT dx;
3206 INT fw;
3208 if (!(es->style & ES_MULTILINE))
3209 return 0;
3211 if (!(es->style & ES_AUTOHSCROLL))
3212 return 0;
3214 if (!(es->style & WS_HSCROLL))
3215 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3217 dx = 0;
3218 fw = es->format_rect.right - es->format_rect.left;
3219 switch (action) {
3220 case SB_LINELEFT:
3221 if (es->x_offset)
3222 dx = -es->char_width;
3223 break;
3224 case SB_LINERIGHT:
3225 if (es->x_offset < es->text_width)
3226 dx = es->char_width;
3227 break;
3228 case SB_PAGELEFT:
3229 if (es->x_offset)
3230 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3231 break;
3232 case SB_PAGERIGHT:
3233 if (es->x_offset < es->text_width)
3234 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3235 break;
3236 case SB_LEFT:
3237 if (es->x_offset)
3238 dx = -es->x_offset;
3239 break;
3240 case SB_RIGHT:
3241 if (es->x_offset < es->text_width)
3242 dx = es->text_width - es->x_offset;
3243 break;
3244 case SB_THUMBTRACK:
3245 es->flags |= EF_HSCROLL_TRACK;
3246 dx = pos - es->x_offset;
3247 break;
3248 case SB_THUMBPOSITION:
3249 es->flags &= ~EF_HSCROLL_TRACK;
3250 if (!(dx = pos - es->x_offset)) {
3251 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3252 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3254 break;
3255 case SB_ENDSCROLL:
3256 break;
3258 default:
3259 ERR(edit, "undocumented WM_HSCROLL parameter, please report\n");
3260 return 0;
3262 if (dx)
3263 EDIT_EM_LineScroll(wnd, es, dx, 0);
3264 return 0;
3268 /*********************************************************************
3270 * EDIT_CheckCombo
3273 static BOOL EDIT_CheckCombo(WND *wnd, UINT msg, INT key, DWORD key_data)
3275 HWND hLBox;
3277 if (WIDGETS_IsControl(wnd->parent, BIC32_COMBO) &&
3278 (hLBox = COMBO_GetLBWindow(wnd->parent))) {
3279 HWND hCombo = wnd->parent->hwndSelf;
3280 BOOL bUIFlip = TRUE;
3282 TRACE(combo, "[%04x]: handling msg %04x (%04x)\n",
3283 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3285 switch (msg) {
3286 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3287 if (key != VK_F4) {
3288 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3289 if (SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0))
3290 bUIFlip = FALSE;
3292 if (!bUIFlip)
3293 SendMessageA(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3294 else {
3295 /* make sure ComboLBox pops up */
3296 SendMessageA(hCombo, CB_SETEXTENDEDUI, 0, 0);
3297 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3298 SendMessageA(hCombo, CB_SETEXTENDEDUI, 1, 0);
3300 break;
3301 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3302 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3303 if (bUIFlip) {
3304 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3305 SendMessageA(hCombo, CB_SHOWDROPDOWN, (bUIFlip) ? FALSE : TRUE, 0);
3306 } else
3307 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3308 break;
3310 return TRUE;
3312 return FALSE;
3316 /*********************************************************************
3318 * WM_KEYDOWN
3320 * Handling of special keys that don't produce a WM_CHAR
3321 * (i.e. non-printable keys) & Backspace & Delete
3324 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3326 BOOL shift;
3327 BOOL control;
3329 if (GetKeyState(VK_MENU) & 0x8000)
3330 return 0;
3332 shift = GetKeyState(VK_SHIFT) & 0x8000;
3333 control = GetKeyState(VK_CONTROL) & 0x8000;
3335 switch (key) {
3336 case VK_F4:
3337 case VK_UP:
3338 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3339 break;
3340 if (key == VK_F4)
3341 break;
3342 /* fall through */
3343 case VK_LEFT:
3344 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3345 EDIT_MoveUp_ML(wnd, es, shift);
3346 else
3347 if (control)
3348 EDIT_MoveWordBackward(wnd, es, shift);
3349 else
3350 EDIT_MoveBackward(wnd, es, shift);
3351 break;
3352 case VK_DOWN:
3353 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3354 break;
3355 /* fall through */
3356 case VK_RIGHT:
3357 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3358 EDIT_MoveDown_ML(wnd, es, shift);
3359 else if (control)
3360 EDIT_MoveWordForward(wnd, es, shift);
3361 else
3362 EDIT_MoveForward(wnd, es, shift);
3363 break;
3364 case VK_HOME:
3365 EDIT_MoveHome(wnd, es, shift);
3366 break;
3367 case VK_END:
3368 EDIT_MoveEnd(wnd, es, shift);
3369 break;
3370 case VK_PRIOR:
3371 if (es->style & ES_MULTILINE)
3372 EDIT_MovePageUp_ML(wnd, es, shift);
3373 break;
3374 case VK_NEXT:
3375 if (es->style & ES_MULTILINE)
3376 EDIT_MovePageDown_ML(wnd, es, shift);
3377 break;
3378 case VK_BACK:
3379 if (!(es->style & ES_READONLY) && !control) {
3380 if (es->selection_start != es->selection_end)
3381 EDIT_WM_Clear(wnd, es);
3382 else {
3383 /* delete character left of caret */
3384 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3385 EDIT_MoveBackward(wnd, es, TRUE);
3386 EDIT_WM_Clear(wnd, es);
3389 break;
3390 case VK_DELETE:
3391 if (!(es->style & ES_READONLY) && !(shift && control)) {
3392 if (es->selection_start != es->selection_end) {
3393 if (shift)
3394 EDIT_WM_Cut(wnd, es);
3395 else
3396 EDIT_WM_Clear(wnd, es);
3397 } else {
3398 if (shift) {
3399 /* delete character left of caret */
3400 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3401 EDIT_MoveBackward(wnd, es, TRUE);
3402 EDIT_WM_Clear(wnd, es);
3403 } else if (control) {
3404 /* delete to end of line */
3405 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3406 EDIT_MoveEnd(wnd, es, TRUE);
3407 EDIT_WM_Clear(wnd, es);
3408 } else {
3409 /* delete character right of caret */
3410 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3411 EDIT_MoveForward(wnd, es, TRUE);
3412 EDIT_WM_Clear(wnd, es);
3416 break;
3417 case VK_INSERT:
3418 if (shift) {
3419 if (!(es->style & ES_READONLY))
3420 EDIT_WM_Paste(wnd, es);
3421 } else if (control)
3422 EDIT_WM_Copy(wnd, es);
3423 break;
3425 return 0;
3429 /*********************************************************************
3431 * WM_KILLFOCUS
3434 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus)
3436 es->flags &= ~EF_FOCUSED;
3437 DestroyCaret();
3438 if(!(es->style & ES_NOHIDESEL))
3439 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3440 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3441 return 0;
3445 /*********************************************************************
3447 * WM_LBUTTONDBLCLK
3449 * The caret position has been set on the WM_LBUTTONDOWN message
3452 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3454 INT s;
3455 INT e = es->selection_end;
3456 INT l;
3457 INT li;
3458 INT ll;
3460 if (!(es->flags & EF_FOCUSED))
3461 return 0;
3463 l = EDIT_EM_LineFromChar(wnd, es, e);
3464 li = EDIT_EM_LineIndex(wnd, es, l);
3465 ll = EDIT_EM_LineLength(wnd, es, e);
3466 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3467 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3468 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3469 EDIT_EM_ScrollCaret(wnd, es);
3470 return 0;
3474 /*********************************************************************
3476 * WM_LBUTTONDOWN
3479 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3481 INT e;
3482 BOOL after_wrap;
3484 if (!(es->flags & EF_FOCUSED))
3485 return 0;
3487 SetCapture(wnd->hwndSelf);
3488 EDIT_ConfinePoint(wnd, es, &x, &y);
3489 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3490 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3491 EDIT_EM_ScrollCaret(wnd, es);
3492 es->region_posx = es->region_posy = 0;
3493 SetTimer(wnd->hwndSelf, 0, 100, NULL);
3494 return 0;
3498 /*********************************************************************
3500 * WM_LBUTTONUP
3503 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3505 if (GetCapture() == wnd->hwndSelf) {
3506 KillTimer(wnd->hwndSelf, 0);
3507 ReleaseCapture();
3509 return 0;
3513 /*********************************************************************
3515 * WM_MOUSEMOVE
3518 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3520 INT e;
3521 BOOL after_wrap;
3522 INT prex, prey;
3524 if (GetCapture() != wnd->hwndSelf)
3525 return 0;
3528 * FIXME: gotta do some scrolling if outside client
3529 * area. Maybe reset the timer ?
3531 prex = x; prey = y;
3532 EDIT_ConfinePoint(wnd, es, &x, &y);
3533 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3534 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3535 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3536 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3537 return 0;
3541 /*********************************************************************
3543 * WM_NCCREATE
3546 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs)
3548 EDITSTATE *es;
3550 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3551 return FALSE;
3552 *(EDITSTATE **)wnd->wExtra = es;
3555 * Note: since the EDITSTATE has not been fully initialized yet,
3556 * we can't use any API calls that may send
3557 * WM_XXX messages before WM_NCCREATE is completed.
3560 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3561 return FALSE;
3562 es->style = cs->style;
3564 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3565 wnd->dwStyle &= ~WS_BORDER;
3567 if (es->style & ES_MULTILINE) {
3568 es->buffer_size = BUFSTART_MULTI;
3569 es->buffer_limit = BUFLIMIT_MULTI;
3570 if (es->style & WS_VSCROLL)
3571 es->style |= ES_AUTOVSCROLL;
3572 if (es->style & WS_HSCROLL)
3573 es->style |= ES_AUTOHSCROLL;
3574 es->style &= ~ES_PASSWORD;
3575 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3576 if (es->style & ES_RIGHT)
3577 es->style &= ~ES_CENTER;
3578 es->style &= ~WS_HSCROLL;
3579 es->style &= ~ES_AUTOHSCROLL;
3582 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3583 es->style |= ES_AUTOVSCROLL;
3584 } else {
3585 es->buffer_size = BUFSTART_SINGLE;
3586 es->buffer_limit = BUFLIMIT_SINGLE;
3587 es->style &= ~ES_CENTER;
3588 es->style &= ~ES_RIGHT;
3589 es->style &= ~WS_HSCROLL;
3590 es->style &= ~WS_VSCROLL;
3591 es->style &= ~ES_AUTOVSCROLL;
3592 es->style &= ~ES_WANTRETURN;
3593 if (es->style & ES_UPPERCASE) {
3594 es->style &= ~ES_LOWERCASE;
3595 es->style &= ~ES_NUMBER;
3596 } else if (es->style & ES_LOWERCASE)
3597 es->style &= ~ES_NUMBER;
3598 if (es->style & ES_PASSWORD)
3599 es->password_char = '*';
3601 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3602 es->style |= ES_AUTOHSCROLL;
3604 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3605 return FALSE;
3606 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3607 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3608 return FALSE;
3609 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3610 *es->text = '\0';
3611 if (es->style & ES_MULTILINE)
3612 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3613 return FALSE;
3614 es->line_count = 1;
3616 return TRUE;
3619 /*********************************************************************
3621 * WM_PAINT
3624 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es)
3626 PAINTSTRUCT ps;
3627 INT i;
3628 HDC dc;
3629 HFONT old_font = 0;
3630 RECT rc;
3631 RECT rcLine;
3632 RECT rcRgn;
3633 BOOL rev = IsWindowEnabled(wnd->hwndSelf) &&
3634 ((es->flags & EF_FOCUSED) ||
3635 (es->style & ES_NOHIDESEL));
3637 if (es->flags & EF_UPDATE)
3638 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
3640 dc = BeginPaint(wnd->hwndSelf, &ps);
3641 if(es->style & WS_BORDER) {
3642 GetClientRect(wnd->hwndSelf, &rc);
3643 if(es->style & ES_MULTILINE) {
3644 if(es->style & WS_HSCROLL) rc.bottom++;
3645 if(es->style & WS_VSCROLL) rc.right++;
3647 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
3649 IntersectClipRect(dc, es->format_rect.left,
3650 es->format_rect.top,
3651 es->format_rect.right,
3652 es->format_rect.bottom);
3653 if (es->style & ES_MULTILINE) {
3654 GetClientRect(wnd->hwndSelf, &rc);
3655 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3657 if (es->font)
3658 old_font = SelectObject(dc, es->font);
3659 if (IsWindowEnabled(wnd->hwndSelf) || (es->style & ES_READONLY))
3660 EDIT_SEND_CTLCOLORSTATIC(wnd, dc);
3661 else
3662 EDIT_SEND_CTLCOLOR(wnd, dc);
3663 if (!IsWindowEnabled(wnd->hwndSelf))
3664 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3665 GetClipBox(dc, &rcRgn);
3666 if (es->style & ES_MULTILINE) {
3667 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3668 for (i = es->y_offset ; i <= MIN(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3669 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3670 if (IntersectRect(&rc, &rcRgn, &rcLine))
3671 EDIT_PaintLine(wnd, es, dc, i, rev);
3673 } else {
3674 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3675 if (IntersectRect(&rc, &rcRgn, &rcLine))
3676 EDIT_PaintLine(wnd, es, dc, 0, rev);
3678 if (es->font)
3679 SelectObject(dc, old_font);
3680 if (es->flags & EF_FOCUSED)
3681 EDIT_SetCaretPos(wnd, es, es->selection_end,
3682 es->flags & EF_AFTER_WRAP);
3683 EndPaint(wnd->hwndSelf, &ps);
3684 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3685 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3686 SCROLLINFO si;
3687 si.cbSize = sizeof(SCROLLINFO);
3688 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3689 si.nMin = 0;
3690 si.nMax = es->line_count + vlc - 2;
3691 si.nPage = vlc;
3692 si.nPos = es->y_offset;
3693 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
3695 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3696 SCROLLINFO si;
3697 INT fw = es->format_rect.right - es->format_rect.left;
3698 si.cbSize = sizeof(SCROLLINFO);
3699 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3700 si.nMin = 0;
3701 si.nMax = es->text_width + fw - 1;
3702 si.nPage = fw;
3703 si.nPos = es->x_offset;
3704 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3707 if (es->flags & EF_UPDATE) {
3708 es->flags &= ~EF_UPDATE;
3709 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3714 /*********************************************************************
3716 * WM_PASTE
3719 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3721 HGLOBAL hsrc;
3722 LPSTR src;
3724 OpenClipboard(wnd->hwndSelf);
3725 if ((hsrc = GetClipboardData(CF_TEXT))) {
3726 src = (LPSTR)GlobalLock(hsrc);
3727 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3728 GlobalUnlock(hsrc);
3730 CloseClipboard();
3734 /*********************************************************************
3736 * WM_SETFOCUS
3739 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus)
3741 es->flags |= EF_FOCUSED;
3742 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3743 EDIT_SetCaretPos(wnd, es, es->selection_end,
3744 es->flags & EF_AFTER_WRAP);
3745 if(!(es->style & ES_NOHIDESEL))
3746 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3747 ShowCaret(wnd->hwndSelf);
3748 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3752 /*********************************************************************
3754 * WM_SETFONT
3756 * With Win95 look the margins are set to default font value unless
3757 * the system font (font == 0) is being set, in which case they are left
3758 * unchanged.
3761 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
3763 TEXTMETRICA tm;
3764 HDC dc;
3765 HFONT old_font = 0;
3767 es->font = font;
3768 dc = GetDC(wnd->hwndSelf);
3769 if (font)
3770 old_font = SelectObject(dc, font);
3771 GetTextMetricsA(dc, &tm);
3772 es->line_height = tm.tmHeight;
3773 es->char_width = tm.tmAveCharWidth;
3774 if (font)
3775 SelectObject(dc, old_font);
3776 ReleaseDC(wnd->hwndSelf, dc);
3777 if (font && (TWEAK_WineLook > WIN31_LOOK))
3778 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3779 EC_USEFONTINFO, EC_USEFONTINFO);
3780 if (es->style & ES_MULTILINE)
3781 EDIT_BuildLineDefs_ML(wnd, es);
3782 else {
3783 RECT r;
3784 GetClientRect(wnd->hwndSelf, &r);
3785 EDIT_SetRectNP(wnd, es, &r);
3787 if (redraw)
3788 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
3789 if (es->flags & EF_FOCUSED) {
3790 DestroyCaret();
3791 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3792 EDIT_SetCaretPos(wnd, es, es->selection_end,
3793 es->flags & EF_AFTER_WRAP);
3794 ShowCaret(wnd->hwndSelf);
3799 /*********************************************************************
3801 * WM_SETTEXT
3803 * NOTES
3804 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3805 * The modified flag is reset. No notifications are sent.
3807 * For single-line controls, reception of WM_SETTEXT triggers:
3808 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3811 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3813 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3814 if (text) {
3815 TRACE(edit, "\t'%s'\n", text);
3816 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3817 } else {
3818 TRACE(edit, "\t<NULL>\n");
3819 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3821 es->x_offset = 0;
3822 if (es->style & ES_MULTILINE) {
3823 es->flags &= ~EF_UPDATE;
3824 } else {
3825 es->flags |= EF_UPDATE;
3827 es->flags &= ~EF_MODIFIED;
3828 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3829 EDIT_EM_ScrollCaret(wnd, es);
3833 /*********************************************************************
3835 * WM_SIZE
3838 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
3840 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3841 RECT rc;
3842 SetRect(&rc, 0, 0, width, height);
3843 EDIT_SetRectNP(wnd, es, &rc);
3844 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
3849 /*********************************************************************
3851 * WM_SYSKEYDOWN
3854 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3856 if ((key == VK_BACK) && (key_data & 0x2000)) {
3857 if (EDIT_EM_CanUndo(wnd, es))
3858 EDIT_EM_Undo(wnd, es);
3859 return 0;
3860 } else if (key == VK_UP || key == VK_DOWN)
3861 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
3862 return 0;
3863 return DefWindowProcA(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
3867 /*********************************************************************
3869 * WM_TIMER
3872 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc)
3874 if (es->region_posx < 0) {
3875 EDIT_MoveBackward(wnd, es, TRUE);
3876 } else if (es->region_posx > 0) {
3877 EDIT_MoveForward(wnd, es, TRUE);
3880 * FIXME: gotta do some vertical scrolling here, like
3881 * EDIT_EM_LineScroll(wnd, 0, 1);
3886 /*********************************************************************
3888 * EDIT_VScroll_Hack
3890 * 16 bit notepad needs this. Actually it is not _our_ hack,
3891 * it is notepad's. Notepad is sending us scrollbar messages with
3892 * undocumented parameters without us even having a scrollbar ... !?!?
3895 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3897 INT dy = 0;
3898 LRESULT ret = 0;
3900 if (!(es->flags & EF_VSCROLL_HACK)) {
3901 ERR(edit, "hacked WM_VSCROLL handler invoked\n");
3902 ERR(edit, " if you are _not_ running 16 bit notepad, please report\n");
3903 ERR(edit, " (this message is only displayed once per edit control)\n");
3904 es->flags |= EF_VSCROLL_HACK;
3907 switch (action) {
3908 case SB_LINEUP:
3909 case SB_LINEDOWN:
3910 case SB_PAGEUP:
3911 case SB_PAGEDOWN:
3912 EDIT_EM_Scroll(wnd, es, action);
3913 return 0;
3914 case SB_TOP:
3915 dy = -es->y_offset;
3916 break;
3917 case SB_BOTTOM:
3918 dy = es->line_count - 1 - es->y_offset;
3919 break;
3920 case SB_THUMBTRACK:
3921 es->flags |= EF_VSCROLL_TRACK;
3922 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
3923 break;
3924 case SB_THUMBPOSITION:
3925 es->flags &= ~EF_VSCROLL_TRACK;
3926 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
3927 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3928 break;
3929 case SB_ENDSCROLL:
3930 break;
3933 * FIXME : the next two are undocumented !
3934 * Are we doing the right thing ?
3935 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3936 * although it's also a regular control message.
3938 case EM_GETTHUMB16:
3939 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
3940 break;
3941 case EM_LINESCROLL16:
3942 dy = pos;
3943 break;
3945 default:
3946 ERR(edit, "undocumented (hacked) WM_VSCROLL parameter, please report\n");
3947 return 0;
3949 if (dy)
3950 EDIT_EM_LineScroll(wnd, es, 0, dy);
3951 return ret;
3955 /*********************************************************************
3957 * WM_VSCROLL
3960 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3962 INT dy;
3964 if (!(es->style & ES_MULTILINE))
3965 return 0;
3967 if (!(es->style & ES_AUTOVSCROLL))
3968 return 0;
3970 if (!(es->style & WS_VSCROLL))
3971 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
3973 dy = 0;
3974 switch (action) {
3975 case SB_LINEUP:
3976 case SB_LINEDOWN:
3977 case SB_PAGEUP:
3978 case SB_PAGEDOWN:
3979 EDIT_EM_Scroll(wnd, es, action);
3980 return 0;
3982 case SB_TOP:
3983 dy = -es->y_offset;
3984 break;
3985 case SB_BOTTOM:
3986 dy = es->line_count - 1 - es->y_offset;
3987 break;
3988 case SB_THUMBTRACK:
3989 es->flags |= EF_VSCROLL_TRACK;
3990 dy = pos - es->y_offset;
3991 break;
3992 case SB_THUMBPOSITION:
3993 es->flags &= ~EF_VSCROLL_TRACK;
3994 if (!(dy = pos - es->y_offset)) {
3995 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
3996 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3998 break;
3999 case SB_ENDSCROLL:
4000 break;
4002 default:
4003 ERR(edit, "undocumented WM_VSCROLL action %d, please report\n",
4004 action);
4005 return 0;
4007 if (dy)
4008 EDIT_EM_LineScroll(wnd, es, 0, dy);
4009 return 0;