Release 980913
[wine/multimedia.git] / controls / edit.c
blob8a90e01c1c707844f8923e1b3f2f587c0c732204
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 "windows.h"
15 #include "winnt.h"
16 #include "win.h"
17 #include "combo.h"
18 #include "local.h"
19 #include "resource.h"
20 #include "debug.h"
21 #include "callback.h"
22 #include "tweak.h"
24 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
25 FIXME: BTW, new specs say 65535 (do you dare ???) */
26 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
27 #define BUFSTART_MULTI 1024 /* starting size */
28 #define BUFSTART_SINGLE 256 /* starting size */
29 #define GROWLENGTH 64 /* buffers grow by this much */
30 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
33 * extra flags for EDITSTATE.flags field
35 #define EF_MODIFIED 0x0001 /* text has been modified */
36 #define EF_FOCUSED 0x0002 /* we have input focus */
37 #define EF_UPDATE 0x0004 /* notify parent of changed state on next WM_PAINT */
38 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
39 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
40 #define EF_VSCROLL_HACK 0x0020 /* we already have informed the user of the hacked handler */
41 #define EF_HSCROLL_HACK 0x0040 /* we already have informed the user of the hacked handler */
42 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
43 wrapped line, instead of in front of the next character */
44 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
46 typedef BOOL32 *LPBOOL32;
48 typedef enum
50 END_0 = 0, /* line ends with terminating '\0' character */
51 END_WRAP, /* line is wrapped */
52 END_HARD, /* line ends with a hard return '\r\n' */
53 END_SOFT /* line ends with a soft return '\r\r\n' */
54 } LINE_END;
56 typedef struct tagLINEDEF {
57 INT32 length; /* bruto length of a line in bytes */
58 INT32 net_length; /* netto length of a line in visible characters */
59 LINE_END ending;
60 INT32 width; /* width of the line in pixels */
61 struct tagLINEDEF *next;
62 } LINEDEF;
64 typedef struct
66 HANDLE32 heap; /* our own heap */
67 LPSTR text; /* the actual contents of the control */
68 INT32 buffer_size; /* the size of the buffer */
69 INT32 buffer_limit; /* the maximum size to which the buffer may grow */
70 HFONT32 font; /* NULL means standard system font */
71 INT32 x_offset; /* scroll offset for multi lines this is in pixels
72 for single lines it's in characters */
73 INT32 line_height; /* height of a screen line in pixels */
74 INT32 char_width; /* average character width in pixels */
75 DWORD style; /* sane version of wnd->dwStyle */
76 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
77 INT32 undo_insert_count; /* number of characters inserted in sequence */
78 INT32 undo_position; /* character index of the insertion and deletion */
79 LPSTR undo_text; /* deleted text */
80 INT32 undo_buffer_size; /* size of the deleted text buffer */
81 INT32 selection_start; /* == selection_end if no selection */
82 INT32 selection_end; /* == current caret position */
83 CHAR password_char; /* == 0 if no password char, and for multi line controls */
84 INT32 left_margin; /* in pixels */
85 INT32 right_margin; /* in pixels */
86 RECT32 format_rect;
87 INT32 region_posx; /* Position of cursor relative to region: */
88 INT32 region_posy; /* -1: to left, 0: within, 1: to right */
89 EDITWORDBREAKPROC16 word_break_proc16;
90 EDITWORDBREAKPROC32A word_break_proc32A;
91 INT32 line_count; /* number of lines */
92 INT32 y_offset; /* scroll offset in number of lines */
94 * only for multi line controls
96 INT32 lock_count; /* amount of re-entries in the EditWndProc */
97 INT32 tabs_count;
98 LPINT32 tabs;
99 INT32 text_width; /* width of the widest line in pixels */
100 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
101 HLOCAL16 hloc16; /* for controls receiving EM_GETHANDLE16 */
102 HLOCAL32 hloc32; /* for controls receiving EM_GETHANDLE */
103 } EDITSTATE;
106 #define SWAP_INT32(x,y) do { INT32 temp = (INT32)(x); (x) = (INT32)(y); (y) = temp; } while(0)
107 #define ORDER_INT32(x,y) do { if ((INT32)(y) < (INT32)(x)) SWAP_INT32((x),(y)); } while(0)
109 #define SWAP_UINT32(x,y) do { UINT32 temp = (UINT32)(x); (x) = (UINT32)(y); (y) = temp; } while(0)
110 #define ORDER_UINT32(x,y) do { if ((UINT32)(y) < (UINT32)(x)) SWAP_UINT32((x),(y)); } while(0)
112 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
113 ({TRACE(edit, "notification " str " sent to hwnd=%08x\n", \
114 (UINT32)(hwnd));})
116 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
117 (SendMessage32A((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
118 (WPARAM32)(hdc), (LPARAM)(wnd)->hwndSelf))
119 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
120 (DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str), \
121 SendMessage32A((wnd)->parent->hwndSelf, WM_COMMAND, \
122 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
123 (LPARAM)(wnd)->hwndSelf))
124 #define DPRINTF_EDIT_MSG16(str) \
125 TRACE(edit, \
126 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
127 (UINT32)hwnd, (UINT32)wParam, (UINT32)lParam)
128 #define DPRINTF_EDIT_MSG32(str) \
129 TRACE(edit, \
130 "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
131 (UINT32)hwnd, (UINT32)wParam, (UINT32)lParam)
133 /*********************************************************************
135 * Declarations
140 * These functions have trivial implementations
141 * We still like to call them internally
142 * "static __inline__" makes them more like macro's
144 static __inline__ BOOL32 EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
145 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
146 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
147 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
149 * This is the only exported function
151 LRESULT WINAPI EditWndProc( HWND32 hwnd, UINT32 msg,
152 WPARAM32 wParam, LPARAM lParam );
154 * Helper functions only valid for one type of control
156 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
157 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
158 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
159 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
160 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
161 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
163 * Helper functions valid for both single line _and_ multi line controls
165 static INT32 EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT32 start, INT32 index, INT32 count, INT32 action);
166 static INT32 EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y, LPBOOL32 after_wrap);
167 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT32 x, LPINT32 y);
168 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT32 line, INT32 scol, INT32 ecol, LPRECT32 rc);
169 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end);
170 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
171 static BOOL32 EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT32 size);
172 static BOOL32 EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT32 size);
173 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL32 extend);
174 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL32 extend);
175 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL32 extend);
176 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL32 extend);
177 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL32 extend);
178 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL32 extend);
179 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC32 hdc, INT32 line, BOOL32 rev);
180 static INT32 EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC32 hdc, INT32 x, INT32 y, INT32 line, INT32 col, INT32 count, BOOL32 rev);
181 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT32 pos, BOOL32 after_wrap);
182 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT32 lprc);
183 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL32 force);
184 static INT32 EDIT_WordBreakProc(LPSTR s, INT32 index, INT32 count, INT32 action);
186 * EM_XXX message handlers
188 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y);
189 static BOOL32 EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL32 add_eol);
190 static HLOCAL32 EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
191 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
192 static INT32 EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT32 line, LPSTR lpch);
193 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT32 start, LPUINT32 end);
194 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
195 static INT32 EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT32 index);
196 static INT32 EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT32 line);
197 static INT32 EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT32 index);
198 static BOOL32 EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT32 dx, INT32 dy);
199 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT32 index, BOOL32 after_wrap);
200 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL32 can_undo, LPCSTR lpsz_replace);
201 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT32 action);
202 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
203 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL32 hloc);
204 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
205 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT32 limit);
206 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT32 action, INT32 left, INT32 right);
207 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
208 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT32 start, UINT32 end, BOOL32 after_wrap);
209 static BOOL32 EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT32 count, LPINT32 tabs);
210 static BOOL32 EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT32 count, LPINT16 tabs);
211 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC32A wbp);
212 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
213 static BOOL32 EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
215 * WM_XXX message handlers
217 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
218 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT32 code, INT32 id, HWND32 conrtol);
219 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND32 hwnd, INT32 x, INT32 y);
220 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
221 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCT32A cs);
222 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
223 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC32 dc);
224 static INT32 EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT32 count, LPSTR text);
225 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar);
226 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data);
227 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND32 window_getting_focus);
228 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
229 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
230 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
231 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
232 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCT32A cs);
233 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es);
234 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
235 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND32 window_losing_focus);
236 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT32 font, BOOL32 redraw);
237 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
238 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT32 action, INT32 width, INT32 height);
239 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data);
240 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT32 id, TIMERPROC32 timer_proc);
241 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar);
244 /*********************************************************************
246 * EM_CANUNDO
249 static __inline__ BOOL32 EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
251 return (es->undo_insert_count || lstrlen32A(es->undo_text));
255 /*********************************************************************
257 * EM_EMPTYUNDOBUFFER
260 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
262 es->undo_insert_count = 0;
263 *es->undo_text = '\0';
267 /*********************************************************************
269 * WM_CLEAR
272 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
274 EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
278 /*********************************************************************
280 * WM_CUT
283 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
285 EDIT_WM_Copy(wnd, es);
286 EDIT_WM_Clear(wnd, es);
290 /*********************************************************************
292 * EditWndProc()
294 * The messages are in the order of the actual integer values
295 * (which can be found in include/windows.h)
296 * Whereever possible the 16 bit versions are converted to
297 * the 32 bit ones, so that we can 'fall through' to the
298 * helper functions. These are mostly 32 bit (with a few
299 * exceptions, clearly indicated by a '16' extension to their
300 * names).
303 LRESULT WINAPI EditWndProc( HWND32 hwnd, UINT32 msg,
304 WPARAM32 wParam, LPARAM lParam )
306 WND *wnd = WIN_FindWndPtr(hwnd);
307 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
308 LRESULT result = 0;
310 switch (msg) {
311 case WM_DESTROY:
312 DPRINTF_EDIT_MSG32("WM_DESTROY");
313 EDIT_WM_Destroy(wnd, es);
314 return 0;
316 case WM_NCCREATE:
317 DPRINTF_EDIT_MSG32("WM_NCCREATE");
318 return EDIT_WM_NCCreate(wnd, (LPCREATESTRUCT32A)lParam);
321 if (!es)
322 return DefWindowProc32A(hwnd, msg, wParam, lParam);
324 EDIT_LockBuffer(wnd, es);
325 switch (msg) {
326 case EM_GETSEL16:
327 DPRINTF_EDIT_MSG16("EM_GETSEL");
328 wParam = 0;
329 lParam = 0;
330 /* fall through */
331 case EM_GETSEL32:
332 DPRINTF_EDIT_MSG32("EM_GETSEL");
333 result = EDIT_EM_GetSel(wnd, es, (LPUINT32)wParam, (LPUINT32)lParam);
334 break;
336 case EM_SETSEL16:
337 DPRINTF_EDIT_MSG16("EM_SETSEL");
338 if (SLOWORD(lParam) == -1)
339 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
340 else
341 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
342 if (!wParam)
343 EDIT_EM_ScrollCaret(wnd, es);
344 result = 1;
345 break;
346 case EM_SETSEL32:
347 DPRINTF_EDIT_MSG32("EM_SETSEL");
348 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
349 result = 1;
350 break;
352 case EM_GETRECT16:
353 DPRINTF_EDIT_MSG16("EM_GETRECT");
354 if (lParam)
355 CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
356 break;
357 case EM_GETRECT32:
358 DPRINTF_EDIT_MSG32("EM_GETRECT");
359 if (lParam)
360 CopyRect32((LPRECT32)lParam, &es->format_rect);
361 break;
363 case EM_SETRECT16:
364 DPRINTF_EDIT_MSG16("EM_SETRECT");
365 if ((es->style & ES_MULTILINE) && lParam) {
366 RECT32 rc;
367 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
368 EDIT_SetRectNP(wnd, es, &rc);
369 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
371 break;
372 case EM_SETRECT32:
373 DPRINTF_EDIT_MSG32("EM_SETRECT");
374 if ((es->style & ES_MULTILINE) && lParam) {
375 EDIT_SetRectNP(wnd, es, (LPRECT32)lParam);
376 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
378 break;
380 case EM_SETRECTNP16:
381 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
382 if ((es->style & ES_MULTILINE) && lParam) {
383 RECT32 rc;
384 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
385 EDIT_SetRectNP(wnd, es, &rc);
387 break;
388 case EM_SETRECTNP32:
389 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
390 if ((es->style & ES_MULTILINE) && lParam)
391 EDIT_SetRectNP(wnd, es, (LPRECT32)lParam);
392 break;
394 case EM_SCROLL16:
395 DPRINTF_EDIT_MSG16("EM_SCROLL");
396 /* fall through */
397 case EM_SCROLL32:
398 DPRINTF_EDIT_MSG32("EM_SCROLL");
399 result = EDIT_EM_Scroll(wnd, es, (INT32)wParam);
400 break;
402 case EM_LINESCROLL16:
403 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
404 wParam = (WPARAM32)(INT32)SHIWORD(lParam);
405 lParam = (LPARAM)(INT32)SLOWORD(lParam);
406 /* fall through */
407 case EM_LINESCROLL32:
408 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
409 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT32)wParam, (INT32)lParam);
410 break;
412 case EM_SCROLLCARET16:
413 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
414 /* fall through */
415 case EM_SCROLLCARET32:
416 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
417 EDIT_EM_ScrollCaret(wnd, es);
418 result = 1;
419 break;
421 case EM_GETMODIFY16:
422 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
423 /* fall through */
424 case EM_GETMODIFY32:
425 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
426 return ((es->flags & EF_MODIFIED) != 0);
427 break;
429 case EM_SETMODIFY16:
430 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
431 /* fall through */
432 case EM_SETMODIFY32:
433 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
434 if (wParam)
435 es->flags |= EF_MODIFIED;
436 else
437 es->flags &= ~EF_MODIFIED;
438 break;
440 case EM_GETLINECOUNT16:
441 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
442 /* fall through */
443 case EM_GETLINECOUNT32:
444 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
445 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
446 break;
448 case EM_LINEINDEX16:
449 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
450 if ((INT16)wParam == -1)
451 wParam = (WPARAM32)-1;
452 /* fall through */
453 case EM_LINEINDEX32:
454 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
455 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT32)wParam);
456 break;
458 case EM_SETHANDLE16:
459 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
460 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
461 break;
462 case EM_SETHANDLE32:
463 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
464 EDIT_EM_SetHandle(wnd, es, (HLOCAL32)wParam);
465 break;
467 case EM_GETHANDLE16:
468 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
469 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
470 break;
471 case EM_GETHANDLE32:
472 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
473 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
474 break;
476 case EM_GETTHUMB16:
477 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
478 /* fall through */
479 case EM_GETTHUMB32:
480 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
481 result = EDIT_EM_GetThumb(wnd, es);
482 break;
484 /* messages 0x00bf and 0x00c0 missing from specs */
486 case WM_USER+15:
487 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
488 /* fall through */
489 case 0x00bf:
490 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
491 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
492 break;
494 case WM_USER+16:
495 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
496 /* fall through */
497 case 0x00c0:
498 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
499 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
500 break;
502 case EM_LINELENGTH16:
503 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
504 /* fall through */
505 case EM_LINELENGTH32:
506 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
507 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT32)wParam);
508 break;
510 case EM_REPLACESEL16:
511 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
512 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
513 /* fall through */
514 case EM_REPLACESEL32:
515 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
516 EDIT_EM_ReplaceSel(wnd, es, (BOOL32)wParam, (LPCSTR)lParam);
517 result = 1;
518 break;
520 /* message 0x00c3 missing from specs */
522 case WM_USER+19:
523 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
524 /* fall through */
525 case 0x00c3:
526 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
527 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
528 break;
530 case EM_GETLINE16:
531 DPRINTF_EDIT_MSG16("EM_GETLINE");
532 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
533 /* fall through */
534 case EM_GETLINE32:
535 DPRINTF_EDIT_MSG32("EM_GETLINE");
536 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT32)wParam, (LPSTR)lParam);
537 break;
539 case EM_LIMITTEXT16:
540 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
541 /* fall through */
542 case EM_SETLIMITTEXT32:
543 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
544 EDIT_EM_SetLimitText(wnd, es, (INT32)wParam);
545 break;
547 case EM_CANUNDO16:
548 DPRINTF_EDIT_MSG16("EM_CANUNDO");
549 /* fall through */
550 case EM_CANUNDO32:
551 DPRINTF_EDIT_MSG32("EM_CANUNDO");
552 result = (LRESULT)EDIT_EM_CanUndo(wnd, es);
553 break;
555 case EM_UNDO16:
556 DPRINTF_EDIT_MSG16("EM_UNDO");
557 /* fall through */
558 case EM_UNDO32:
559 /* fall through */
560 case WM_UNDO:
561 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
562 result = (LRESULT)EDIT_EM_Undo(wnd, es);
563 break;
565 case EM_FMTLINES16:
566 DPRINTF_EDIT_MSG16("EM_FMTLINES");
567 /* fall through */
568 case EM_FMTLINES32:
569 DPRINTF_EDIT_MSG32("EM_FMTLINES");
570 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL32)wParam);
571 break;
573 case EM_LINEFROMCHAR16:
574 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
575 /* fall through */
576 case EM_LINEFROMCHAR32:
577 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
578 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT32)wParam);
579 break;
581 /* message 0x00ca missing from specs */
583 case WM_USER+26:
584 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
585 /* fall through */
586 case 0x00ca:
587 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
588 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
589 break;
591 case EM_SETTABSTOPS16:
592 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
593 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT32)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
594 break;
595 case EM_SETTABSTOPS32:
596 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
597 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT32)wParam, (LPINT32)lParam);
598 break;
600 case EM_SETPASSWORDCHAR16:
601 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
602 /* fall through */
603 case EM_SETPASSWORDCHAR32:
604 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
605 EDIT_EM_SetPasswordChar(wnd, es, (CHAR)wParam);
606 break;
608 case EM_EMPTYUNDOBUFFER16:
609 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
610 /* fall through */
611 case EM_EMPTYUNDOBUFFER32:
612 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
613 EDIT_EM_EmptyUndoBuffer(wnd, es);
614 break;
616 case EM_GETFIRSTVISIBLELINE16:
617 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
618 result = es->y_offset;
619 break;
620 case EM_GETFIRSTVISIBLELINE32:
621 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
622 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
623 break;
625 case EM_SETREADONLY16:
626 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
627 /* fall through */
628 case EM_SETREADONLY32:
629 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
630 if (wParam) {
631 wnd->dwStyle |= ES_READONLY;
632 es->style |= ES_READONLY;
633 } else {
634 wnd->dwStyle &= ~ES_READONLY;
635 es->style &= ~ES_READONLY;
637 return 1;
638 break;
640 case EM_SETWORDBREAKPROC16:
641 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
642 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
643 break;
644 case EM_SETWORDBREAKPROC32:
645 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
646 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROC32A)lParam);
647 break;
649 case EM_GETWORDBREAKPROC16:
650 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
651 result = (LRESULT)es->word_break_proc16;
652 break;
653 case EM_GETWORDBREAKPROC32:
654 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
655 result = (LRESULT)es->word_break_proc32A;
656 break;
658 case EM_GETPASSWORDCHAR16:
659 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
660 /* fall through */
661 case EM_GETPASSWORDCHAR32:
662 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
663 result = es->password_char;
664 break;
666 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
668 case EM_SETMARGINS32:
669 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
670 EDIT_EM_SetMargins(wnd, es, (INT32)wParam, SLOWORD(lParam), SHIWORD(lParam));
671 break;
673 case EM_GETMARGINS32:
674 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
675 result = MAKELONG(es->left_margin, es->right_margin);
676 break;
678 case EM_GETLIMITTEXT32:
679 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
680 result = es->buffer_limit;
681 break;
683 case EM_POSFROMCHAR32:
684 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
685 result = EDIT_EM_PosFromChar(wnd, es, (INT32)wParam, FALSE);
686 break;
688 case EM_CHARFROMPOS32:
689 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
690 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
691 break;
693 case WM_GETDLGCODE:
694 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
695 result = (es->style & ES_MULTILINE) ?
696 DLGC_WANTALLKEYS | DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS :
697 DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
698 break;
700 case WM_CHAR:
701 DPRINTF_EDIT_MSG32("WM_CHAR");
702 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
703 break;
705 case WM_CLEAR:
706 DPRINTF_EDIT_MSG32("WM_CLEAR");
707 EDIT_WM_Clear(wnd, es);
708 break;
710 case WM_COMMAND:
711 DPRINTF_EDIT_MSG32("WM_COMMAND");
712 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND32)lParam);
713 break;
715 case WM_CONTEXTMENU:
716 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
717 EDIT_WM_ContextMenu(wnd, es, (HWND32)wParam, SLOWORD(lParam), SHIWORD(lParam));
718 break;
720 case WM_COPY:
721 DPRINTF_EDIT_MSG32("WM_COPY");
722 EDIT_WM_Copy(wnd, es);
723 break;
725 case WM_CREATE:
726 DPRINTF_EDIT_MSG32("WM_CREATE");
727 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCT32A)lParam);
728 break;
730 case WM_CUT:
731 DPRINTF_EDIT_MSG32("WM_CUT");
732 EDIT_WM_Cut(wnd, es);
733 break;
735 case WM_ENABLE:
736 DPRINTF_EDIT_MSG32("WM_ENABLE");
737 InvalidateRect32(hwnd, NULL, TRUE);
738 break;
740 case WM_ERASEBKGND:
741 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
742 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC32)wParam);
743 break;
745 case WM_GETFONT:
746 DPRINTF_EDIT_MSG32("WM_GETFONT");
747 result = (LRESULT)es->font;
748 break;
750 case WM_GETTEXT:
751 DPRINTF_EDIT_MSG32("WM_GETTEXT");
752 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT32)wParam, (LPSTR)lParam);
753 break;
755 case WM_GETTEXTLENGTH:
756 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
757 result = lstrlen32A(es->text);
758 break;
760 case WM_HSCROLL:
761 DPRINTF_EDIT_MSG32("WM_HSCROLL");
762 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND32)lParam);
763 break;
765 case WM_KEYDOWN:
766 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
767 result = EDIT_WM_KeyDown(wnd, es, (INT32)wParam, (DWORD)lParam);
768 break;
770 case WM_KILLFOCUS:
771 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
772 result = EDIT_WM_KillFocus(wnd, es, (HWND32)wParam);
773 break;
775 case WM_LBUTTONDBLCLK:
776 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
777 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
778 break;
780 case WM_LBUTTONDOWN:
781 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
782 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
783 break;
785 case WM_LBUTTONUP:
786 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
787 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
788 break;
790 case WM_MOUSEACTIVATE:
792 * FIXME: maybe DefWindowProc() screws up, but it seems that
793 * modalless dialog boxes need this. If we don't do this, the focus
794 * will _not_ be set by DefWindowProc() for edit controls in a
795 * modalless dialog box ???
797 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
798 SetFocus32(wnd->hwndSelf);
799 result = MA_ACTIVATE;
800 break;
802 case WM_MOUSEMOVE:
804 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
806 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
807 break;
809 case WM_PAINT:
810 DPRINTF_EDIT_MSG32("WM_PAINT");
811 EDIT_WM_Paint(wnd, es);
812 break;
814 case WM_PASTE:
815 DPRINTF_EDIT_MSG32("WM_PASTE");
816 EDIT_WM_Paste(wnd, es);
817 break;
819 case WM_SETFOCUS:
820 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
821 EDIT_WM_SetFocus(wnd, es, (HWND32)wParam);
822 break;
824 case WM_SETFONT:
825 DPRINTF_EDIT_MSG32("WM_SETFONT");
826 EDIT_WM_SetFont(wnd, es, (HFONT32)wParam, LOWORD(lParam) != 0);
827 break;
829 case WM_SETTEXT:
830 DPRINTF_EDIT_MSG32("WM_SETTEXT");
831 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
832 result = TRUE;
833 break;
835 case WM_SIZE:
836 DPRINTF_EDIT_MSG32("WM_SIZE");
837 EDIT_WM_Size(wnd, es, (UINT32)wParam, LOWORD(lParam), HIWORD(lParam));
838 break;
840 case WM_SYSKEYDOWN:
841 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
842 result = EDIT_WM_SysKeyDown(wnd, es, (INT32)wParam, (DWORD)lParam);
843 break;
845 case WM_TIMER:
846 DPRINTF_EDIT_MSG32("WM_TIMER");
847 EDIT_WM_Timer(wnd, es, (INT32)wParam, (TIMERPROC32)lParam);
848 break;
850 case WM_VSCROLL:
851 DPRINTF_EDIT_MSG32("WM_VSCROLL");
852 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND32)(lParam));
853 break;
855 default:
856 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
857 break;
859 EDIT_UnlockBuffer(wnd, es, FALSE);
860 return result;
864 /*********************************************************************
866 * EDIT_BuildLineDefs_ML
868 * Build linked list of text lines.
869 * Lines can end with '\0' (last line), a character (if it is wrapped),
870 * a soft return '\r\r\n' or a hard return '\r\n'
873 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
875 HDC32 dc;
876 HFONT32 old_font = 0;
877 LPSTR start, cp;
878 INT32 fw;
879 LINEDEF *current_def;
880 LINEDEF **previous_next;
882 current_def = es->first_line_def;
883 do {
884 LINEDEF *next_def = current_def->next;
885 HeapFree(es->heap, 0, current_def);
886 current_def = next_def;
887 } while (current_def);
888 es->line_count = 0;
889 es->text_width = 0;
891 dc = GetDC32(wnd->hwndSelf);
892 if (es->font)
893 old_font = SelectObject32(dc, es->font);
895 fw = es->format_rect.right - es->format_rect.left;
896 start = es->text;
897 previous_next = &es->first_line_def;
898 do {
899 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
900 current_def->next = NULL;
901 cp = start;
902 while (*cp) {
903 if ((*cp == '\r') && (*(cp + 1) == '\n'))
904 break;
905 cp++;
907 if (!(*cp)) {
908 current_def->ending = END_0;
909 current_def->net_length = lstrlen32A(start);
910 } else if ((cp > start) && (*(cp - 1) == '\r')) {
911 current_def->ending = END_SOFT;
912 current_def->net_length = cp - start - 1;
913 } else {
914 current_def->ending = END_HARD;
915 current_def->net_length = cp - start;
917 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
918 start, current_def->net_length,
919 es->tabs_count, es->tabs));
920 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
921 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
922 INT32 next = 0;
923 INT32 prev;
924 do {
925 prev = next;
926 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
927 prev + 1, current_def->net_length, WB_RIGHT);
928 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
929 start, next, es->tabs_count, es->tabs));
930 } while (current_def->width <= fw);
931 if (!prev) {
932 next = 0;
933 do {
934 prev = next;
935 next++;
936 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
937 start, next, es->tabs_count, es->tabs));
938 } while (current_def->width <= fw);
939 if (!prev)
940 prev = 1;
942 current_def->net_length = prev;
943 current_def->ending = END_WRAP;
944 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc, start,
945 current_def->net_length, es->tabs_count, es->tabs));
947 switch (current_def->ending) {
948 case END_SOFT:
949 current_def->length = current_def->net_length + 3;
950 break;
951 case END_HARD:
952 current_def->length = current_def->net_length + 2;
953 break;
954 case END_WRAP:
955 case END_0:
956 current_def->length = current_def->net_length;
957 break;
959 es->text_width = MAX(es->text_width, current_def->width);
960 start += current_def->length;
961 *previous_next = current_def;
962 previous_next = &current_def->next;
963 es->line_count++;
964 } while (current_def->ending != END_0);
965 if (es->font)
966 SelectObject32(dc, old_font);
967 ReleaseDC32(wnd->hwndSelf, dc);
971 /*********************************************************************
973 * EDIT_CallWordBreakProc
975 * Call appropriate WordBreakProc (internal or external).
977 * Note: The "start" argument should always be an index refering
978 * to es->text. The actual wordbreak proc might be
979 * 16 bit, so we can't always pass any 32 bit LPSTR.
980 * Hence we assume that es->text is the buffer that holds
981 * the string under examination (we can decide this for ourselves).
984 static INT32 EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT32 start, INT32 index, INT32 count, INT32 action)
986 if (es->word_break_proc16) {
987 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
988 SEGPTR segptr = LocalLock16(hloc16);
989 INT32 ret = (INT32)Callbacks->CallWordBreakProc(es->word_break_proc16,
990 segptr + start, index, count, action);
991 LocalUnlock16(hloc16);
992 return ret;
994 else if (es->word_break_proc32A)
996 TRACE(relay, "(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
997 es->word_break_proc32A, es->text + start, index,
998 count, action );
999 return (INT32)es->word_break_proc32A( es->text + start, index,
1000 count, action );
1002 else
1003 return EDIT_WordBreakProc(es->text + start, index, count, action);
1007 /*********************************************************************
1009 * EDIT_CharFromPos
1011 * Beware: This is not the function called on EM_CHARFROMPOS
1012 * The position _can_ be outside the formatting / client
1013 * rectangle
1014 * The return value is only the character index
1017 static INT32 EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y, LPBOOL32 after_wrap)
1019 INT32 index;
1020 HDC32 dc;
1021 HFONT32 old_font = 0;
1023 if (es->style & ES_MULTILINE) {
1024 INT32 line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1025 INT32 line_index = 0;
1026 LINEDEF *line_def = es->first_line_def;
1027 INT32 low, high;
1028 while ((line > 0) && line_def->next) {
1029 line_index += line_def->length;
1030 line_def = line_def->next;
1031 line--;
1033 x += es->x_offset - es->format_rect.left;
1034 if (x >= line_def->width) {
1035 if (after_wrap)
1036 *after_wrap = (line_def->ending == END_WRAP);
1037 return line_index + line_def->net_length;
1039 if (x <= 0) {
1040 if (after_wrap)
1041 *after_wrap = FALSE;
1042 return line_index;
1044 dc = GetDC32(wnd->hwndSelf);
1045 if (es->font)
1046 old_font = SelectObject32(dc, es->font);
1047 low = line_index + 1;
1048 high = line_index + line_def->net_length + 1;
1049 while (low < high - 1)
1051 INT32 mid = (low + high) / 2;
1052 if (LOWORD(GetTabbedTextExtent32A(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1053 else low = mid;
1055 index = low;
1057 if (after_wrap)
1058 *after_wrap = ((index == line_index + line_def->net_length) &&
1059 (line_def->ending == END_WRAP));
1060 } else {
1061 LPSTR text;
1062 SIZE32 size;
1063 if (after_wrap)
1064 *after_wrap = FALSE;
1065 x -= es->format_rect.left;
1066 if (!x)
1067 return es->x_offset;
1068 text = EDIT_GetPasswordPointer_SL(wnd, es);
1069 dc = GetDC32(wnd->hwndSelf);
1070 if (es->font)
1071 old_font = SelectObject32(dc, es->font);
1072 if (x < 0)
1074 INT32 low = 0;
1075 INT32 high = es->x_offset;
1076 while (low < high - 1)
1078 INT32 mid = (low + high) / 2;
1079 GetTextExtentPoint32A( dc, text + mid,
1080 es->x_offset - mid, &size );
1081 if (size.cx > -x) low = mid;
1082 else high = mid;
1084 index = low;
1086 else
1088 INT32 low = es->x_offset;
1089 INT32 high = lstrlen32A(es->text) + 1;
1090 while (low < high - 1)
1092 INT32 mid = (low + high) / 2;
1093 GetTextExtentPoint32A( dc, text + es->x_offset,
1094 mid - es->x_offset, &size );
1095 if (size.cx > x) high = mid;
1096 else low = mid;
1098 index = low;
1100 if (es->style & ES_PASSWORD)
1101 HeapFree(es->heap, 0 ,text);
1103 if (es->font)
1104 SelectObject32(dc, old_font);
1105 ReleaseDC32(wnd->hwndSelf, dc);
1106 return index;
1110 /*********************************************************************
1112 * EDIT_ConfinePoint
1114 * adjusts the point to be within the formatting rectangle
1115 * (so CharFromPos returns the nearest _visible_ character)
1118 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT32 x, LPINT32 y)
1120 *x = MIN(MAX(*x, es->format_rect.left), es->format_rect.right - 1);
1121 *y = MIN(MAX(*y, es->format_rect.top), es->format_rect.bottom - 1);
1125 /*********************************************************************
1127 * EDIT_GetLineRect
1129 * Calculates the bounding rectangle for a line from a starting
1130 * column to an ending column.
1133 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT32 line, INT32 scol, INT32 ecol, LPRECT32 rc)
1135 INT32 line_index = EDIT_EM_LineIndex(wnd, es, line);
1137 if (es->style & ES_MULTILINE)
1138 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1139 else
1140 rc->top = es->format_rect.top;
1141 rc->bottom = rc->top + es->line_height;
1142 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1143 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1147 /*********************************************************************
1149 * EDIT_GetPasswordPointer_SL
1151 * note: caller should free the (optionally) allocated buffer
1154 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1156 if (es->style & ES_PASSWORD) {
1157 INT32 len = lstrlen32A(es->text);
1158 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1159 RtlFillMemory(text, len, es->password_char);
1160 text[len] = '\0';
1161 return text;
1162 } else
1163 return es->text;
1167 /*********************************************************************
1169 * EDIT_LockBuffer
1171 * This acts as a LOCAL_Lock(), but it locks only once. This way
1172 * you can call it whenever you like, without unlocking.
1175 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1177 if (!es) {
1178 ERR(edit, "no EDITSTATE ... please report\n");
1179 return;
1181 if (!(es->style & ES_MULTILINE))
1182 return;
1183 if (!es->text) {
1184 if (es->hloc32)
1185 es->text = LocalLock32(es->hloc32);
1186 else if (es->hloc16)
1187 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1188 else {
1189 ERR(edit, "no buffer ... please report\n");
1190 return;
1193 es->lock_count++;
1197 /*********************************************************************
1199 * EDIT_SL_InvalidateText
1201 * Called from EDIT_InvalidateText().
1202 * Does the job for single-line controls only.
1205 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1207 RECT32 line_rect;
1208 RECT32 rc;
1210 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1211 if (IntersectRect32(&rc, &line_rect, &es->format_rect))
1212 InvalidateRect32(wnd->hwndSelf, &rc, FALSE);
1216 /*********************************************************************
1218 * EDIT_ML_InvalidateText
1220 * Called from EDIT_InvalidateText().
1221 * Does the job for multi-line controls only.
1224 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1226 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1227 INT32 sl = EDIT_EM_LineFromChar(wnd, es, start);
1228 INT32 el = EDIT_EM_LineFromChar(wnd, es, end);
1229 INT32 sc;
1230 INT32 ec;
1231 RECT32 rc1;
1232 RECT32 rcWnd;
1233 RECT32 rcLine;
1234 RECT32 rcUpdate;
1235 INT32 l;
1237 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1238 return;
1240 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1241 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1242 if (sl < es->y_offset) {
1243 sl = es->y_offset;
1244 sc = 0;
1246 if (el > es->y_offset + vlc) {
1247 el = es->y_offset + vlc;
1248 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1250 GetClientRect32(wnd->hwndSelf, &rc1);
1251 IntersectRect32(&rcWnd, &rc1, &es->format_rect);
1252 if (sl == el) {
1253 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1254 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1255 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1256 } else {
1257 EDIT_GetLineRect(wnd, es, sl, sc,
1258 EDIT_EM_LineLength(wnd, es,
1259 EDIT_EM_LineIndex(wnd, es, sl)),
1260 &rcLine);
1261 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1262 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1263 for (l = sl + 1 ; l < el ; l++) {
1264 EDIT_GetLineRect(wnd, es, l, 0,
1265 EDIT_EM_LineLength(wnd, es,
1266 EDIT_EM_LineIndex(wnd, es, l)),
1267 &rcLine);
1268 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1269 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1271 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1272 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1273 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1278 /*********************************************************************
1280 * EDIT_InvalidateText
1282 * Invalidate the text from offset start upto, but not including,
1283 * offset end. Useful for (re)painting the selection.
1284 * Regions outside the linewidth are not invalidated.
1285 * end == -1 means end == TextLength.
1286 * start and end need not be ordered.
1289 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1291 if (end == start)
1292 return;
1294 if (end == -1)
1295 end = lstrlen32A(es->text);
1297 ORDER_INT32(start, end);
1299 if (es->style & ES_MULTILINE)
1300 EDIT_ML_InvalidateText(wnd, es, start, end);
1301 else
1302 EDIT_SL_InvalidateText(wnd, es, start, end);
1306 /*********************************************************************
1308 * EDIT_MakeFit
1310 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1313 static BOOL32 EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT32 size)
1315 HLOCAL32 hNew32;
1316 HLOCAL16 hNew16;
1318 if (size <= es->buffer_size)
1319 return TRUE;
1320 if (size > es->buffer_limit) {
1321 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1322 return FALSE;
1324 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1325 if (size > es->buffer_limit)
1326 size = es->buffer_limit;
1328 TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1330 EDIT_UnlockBuffer(wnd, es, TRUE);
1331 if (es->text) {
1332 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1333 es->buffer_size = MIN(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1334 else
1335 es->buffer_size = 0;
1336 } else if (es->hloc32) {
1337 if ((hNew32 = LocalReAlloc32(es->hloc32, size + 1, 0))) {
1338 TRACE(edit, "Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1339 es->hloc32 = hNew32;
1340 es->buffer_size = MIN(LocalSize32(es->hloc32) - 1, es->buffer_limit);
1342 } else if (es->hloc16) {
1343 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1344 TRACE(edit, "Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1345 es->hloc16 = hNew16;
1346 es->buffer_size = MIN(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1349 if (es->buffer_size < size) {
1350 EDIT_LockBuffer(wnd, es);
1351 WARN(edit, "FAILED ! We now have %d+1\n", es->buffer_size);
1352 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1353 return FALSE;
1354 } else {
1355 EDIT_LockBuffer(wnd, es);
1356 TRACE(edit, "We now have %d+1\n", es->buffer_size);
1357 return TRUE;
1362 /*********************************************************************
1364 * EDIT_MakeUndoFit
1366 * Try to fit size + 1 bytes in the undo buffer.
1369 static BOOL32 EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT32 size)
1371 if (size <= es->undo_buffer_size)
1372 return TRUE;
1373 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1375 TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1377 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1378 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1379 if (es->undo_buffer_size < size) {
1380 WARN(edit, "FAILED ! We now have %d+1\n", es->undo_buffer_size);
1381 return FALSE;
1383 return TRUE;
1385 return FALSE;
1389 /*********************************************************************
1391 * EDIT_MoveBackward
1394 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1396 INT32 e = es->selection_end;
1398 if (e) {
1399 e--;
1400 if ((es->style & ES_MULTILINE) && e &&
1401 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1402 e--;
1403 if (e && (es->text[e - 1] == '\r'))
1404 e--;
1407 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1408 EDIT_EM_ScrollCaret(wnd, es);
1412 /*********************************************************************
1414 * EDIT_MoveDown_ML
1416 * Only for multi line controls
1417 * Move the caret one line down, on a column with the nearest
1418 * x coordinate on the screen (might be a different column).
1421 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1423 INT32 s = es->selection_start;
1424 INT32 e = es->selection_end;
1425 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1426 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1427 INT32 x = SLOWORD(pos);
1428 INT32 y = SHIWORD(pos);
1430 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1431 if (!extend)
1432 s = e;
1433 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1434 EDIT_EM_ScrollCaret(wnd, es);
1438 /*********************************************************************
1440 * EDIT_MoveEnd
1443 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL32 extend)
1445 BOOL32 after_wrap = FALSE;
1446 INT32 e;
1448 if (es->style & ES_MULTILINE)
1449 e = EDIT_CharFromPos(wnd, es, 0x7fffffff,
1450 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1451 else
1452 e = lstrlen32A(es->text);
1453 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1454 EDIT_EM_ScrollCaret(wnd, es);
1458 /*********************************************************************
1460 * EDIT_MoveForward
1463 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1465 INT32 e = es->selection_end;
1467 if (es->text[e]) {
1468 e++;
1469 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1470 if (es->text[e] == '\n')
1471 e++;
1472 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1473 e += 2;
1476 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1477 EDIT_EM_ScrollCaret(wnd, es);
1481 /*********************************************************************
1483 * EDIT_MoveHome
1485 * Home key: move to beginning of line.
1488 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL32 extend)
1490 INT32 e;
1492 if (es->style & ES_MULTILINE)
1493 e = EDIT_CharFromPos(wnd, es, 0x80000000,
1494 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1495 else
1496 e = 0;
1497 EDIT_EM_SetSel(wnd, es, e, extend ? es->selection_start : e, FALSE);
1498 EDIT_EM_ScrollCaret(wnd, es);
1502 /*********************************************************************
1504 * EDIT_MovePageDown_ML
1506 * Only for multi line controls
1507 * Move the caret one page down, on a column with the nearest
1508 * x coordinate on the screen (might be a different column).
1511 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1513 INT32 s = es->selection_start;
1514 INT32 e = es->selection_end;
1515 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1516 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1517 INT32 x = SLOWORD(pos);
1518 INT32 y = SHIWORD(pos);
1520 e = EDIT_CharFromPos(wnd, es, x,
1521 y + (es->format_rect.bottom - es->format_rect.top),
1522 &after_wrap);
1523 if (!extend)
1524 s = e;
1525 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1526 EDIT_EM_ScrollCaret(wnd, es);
1530 /*********************************************************************
1532 * EDIT_MovePageUp_ML
1534 * Only for multi line controls
1535 * Move the caret one page up, on a column with the nearest
1536 * x coordinate on the screen (might be a different column).
1539 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1541 INT32 s = es->selection_start;
1542 INT32 e = es->selection_end;
1543 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1544 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1545 INT32 x = SLOWORD(pos);
1546 INT32 y = SHIWORD(pos);
1548 e = EDIT_CharFromPos(wnd, es, x,
1549 y - (es->format_rect.bottom - es->format_rect.top),
1550 &after_wrap);
1551 if (!extend)
1552 s = e;
1553 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1554 EDIT_EM_ScrollCaret(wnd, es);
1558 /*********************************************************************
1560 * EDIT_MoveUp_ML
1562 * Only for multi line controls
1563 * Move the caret one line up, on a column with the nearest
1564 * x coordinate on the screen (might be a different column).
1567 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1569 INT32 s = es->selection_start;
1570 INT32 e = es->selection_end;
1571 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1572 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1573 INT32 x = SLOWORD(pos);
1574 INT32 y = SHIWORD(pos);
1576 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1577 if (!extend)
1578 s = e;
1579 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1580 EDIT_EM_ScrollCaret(wnd, es);
1584 /*********************************************************************
1586 * EDIT_MoveWordBackward
1589 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1591 INT32 s = es->selection_start;
1592 INT32 e = es->selection_end;
1593 INT32 l;
1594 INT32 ll;
1595 INT32 li;
1597 l = EDIT_EM_LineFromChar(wnd, es, e);
1598 ll = EDIT_EM_LineLength(wnd, es, e);
1599 li = EDIT_EM_LineIndex(wnd, es, l);
1600 if (e - li == 0) {
1601 if (l) {
1602 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1603 e = li + EDIT_EM_LineLength(wnd, es, li);
1605 } else {
1606 e = li + (INT32)EDIT_CallWordBreakProc(wnd, es,
1607 li, e - li, ll, WB_LEFT);
1609 if (!extend)
1610 s = e;
1611 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1612 EDIT_EM_ScrollCaret(wnd, es);
1616 /*********************************************************************
1618 * EDIT_MoveWordForward
1621 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1623 INT32 s = es->selection_start;
1624 INT32 e = es->selection_end;
1625 INT32 l;
1626 INT32 ll;
1627 INT32 li;
1629 l = EDIT_EM_LineFromChar(wnd, es, e);
1630 ll = EDIT_EM_LineLength(wnd, es, e);
1631 li = EDIT_EM_LineIndex(wnd, es, l);
1632 if (e - li == ll) {
1633 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1634 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1635 } else {
1636 e = li + EDIT_CallWordBreakProc(wnd, es,
1637 li, e - li + 1, ll, WB_RIGHT);
1639 if (!extend)
1640 s = e;
1641 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1642 EDIT_EM_ScrollCaret(wnd, es);
1646 /*********************************************************************
1648 * EDIT_PaintLine
1651 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC32 dc, INT32 line, BOOL32 rev)
1653 INT32 s = es->selection_start;
1654 INT32 e = es->selection_end;
1655 INT32 li;
1656 INT32 ll;
1657 INT32 x;
1658 INT32 y;
1659 LRESULT pos;
1661 if (es->style & ES_MULTILINE) {
1662 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1663 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1664 return;
1665 } else if (line)
1666 return;
1668 TRACE(edit, "line=%d\n", line);
1670 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1671 x = SLOWORD(pos);
1672 y = SHIWORD(pos);
1673 li = EDIT_EM_LineIndex(wnd, es, line);
1674 ll = EDIT_EM_LineLength(wnd, es, li);
1675 s = es->selection_start;
1676 e = es->selection_end;
1677 ORDER_INT32(s, e);
1678 s = MIN(li + ll, MAX(li, s));
1679 e = MIN(li + ll, MAX(li, e));
1680 if (rev && (s != e) &&
1681 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1682 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1683 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1684 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1685 } else
1686 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1690 /*********************************************************************
1692 * EDIT_PaintText
1695 static INT32 EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC32 dc, INT32 x, INT32 y, INT32 line, INT32 col, INT32 count, BOOL32 rev)
1697 COLORREF BkColor;
1698 COLORREF TextColor;
1699 INT32 ret;
1700 INT32 li;
1701 SIZE32 size;
1703 if (!count)
1704 return 0;
1705 BkColor = GetBkColor32(dc);
1706 TextColor = GetTextColor32(dc);
1707 if (rev) {
1708 SetBkColor32(dc, GetSysColor32(COLOR_HIGHLIGHT));
1709 SetTextColor32(dc, GetSysColor32(COLOR_HIGHLIGHTTEXT));
1711 li = EDIT_EM_LineIndex(wnd, es, line);
1712 if (es->style & ES_MULTILINE) {
1713 ret = (INT32)LOWORD(TabbedTextOut32A(dc, x, y, es->text + li + col, count,
1714 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1715 } else {
1716 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1717 TextOut32A(dc, x, y, text + li + col, count);
1718 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1719 ret = size.cx;
1720 if (es->style & ES_PASSWORD)
1721 HeapFree(es->heap, 0, text);
1723 if (rev) {
1724 SetBkColor32(dc, BkColor);
1725 SetTextColor32(dc, TextColor);
1727 return ret;
1731 /*********************************************************************
1733 * EDIT_SetCaretPos
1736 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT32 pos,
1737 BOOL32 after_wrap)
1739 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1740 INT32 x = SLOWORD(res);
1741 INT32 y = SHIWORD(res);
1743 if(x < es->format_rect.left)
1744 x = es->format_rect.left;
1745 if(x > es->format_rect.right - 2)
1746 x = es->format_rect.right - 2;
1747 if(y > es->format_rect.bottom)
1748 y = es->format_rect.bottom;
1749 if(y < es->format_rect.top)
1750 y = es->format_rect.top;
1751 SetCaretPos32(x, y);
1752 return;
1756 /*********************************************************************
1758 * EDIT_SetRectNP
1760 * note: this is not (exactly) the handler called on EM_SETRECTNP
1761 * it is also used to set the rect of a single line control
1764 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT32 rc)
1766 CopyRect32(&es->format_rect, rc);
1767 if (es->style & WS_BORDER) {
1768 INT32 bw = GetSystemMetrics32(SM_CXBORDER) + 1;
1769 if(!TWEAK_Win95Look)
1770 bw += 2;
1771 es->format_rect.left += bw;
1772 es->format_rect.top += bw;
1773 es->format_rect.right -= bw;
1774 es->format_rect.bottom -= bw;
1776 es->format_rect.left += es->left_margin;
1777 es->format_rect.right -= es->right_margin;
1778 es->format_rect.right = MAX(es->format_rect.right, es->format_rect.left + es->char_width);
1779 if (es->style & ES_MULTILINE)
1780 es->format_rect.bottom = es->format_rect.top +
1781 MAX(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1782 else
1783 es->format_rect.bottom = es->format_rect.top + es->line_height;
1784 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1785 EDIT_BuildLineDefs_ML(wnd, es);
1789 /*********************************************************************
1791 * EDIT_UnlockBuffer
1794 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL32 force)
1796 if (!es) {
1797 ERR(edit, "no EDITSTATE ... please report\n");
1798 return;
1800 if (!(es->style & ES_MULTILINE))
1801 return;
1802 if (!es->lock_count) {
1803 ERR(edit, "lock_count == 0 ... please report\n");
1804 return;
1806 if (!es->text) {
1807 ERR(edit, "es->text == 0 ... please report\n");
1808 return;
1810 if (force || (es->lock_count == 1)) {
1811 if (es->hloc32) {
1812 LocalUnlock32(es->hloc32);
1813 es->text = NULL;
1814 } else if (es->hloc16) {
1815 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1816 es->text = NULL;
1819 es->lock_count--;
1823 /*********************************************************************
1825 * EDIT_WordBreakProc
1827 * Find the beginning of words.
1828 * Note: unlike the specs for a WordBreakProc, this function only
1829 * allows to be called without linebreaks between s[0] upto
1830 * s[count - 1]. Remember it is only called
1831 * internally, so we can decide this for ourselves.
1834 static INT32 EDIT_WordBreakProc(LPSTR s, INT32 index, INT32 count, INT32 action)
1836 INT32 ret = 0;
1838 TRACE(edit, "s=%p, index=%u, count=%u, action=%d\n",
1839 s, index, count, action);
1841 switch (action) {
1842 case WB_LEFT:
1843 if (!count)
1844 break;
1845 if (index)
1846 index--;
1847 if (s[index] == ' ') {
1848 while (index && (s[index] == ' '))
1849 index--;
1850 if (index) {
1851 while (index && (s[index] != ' '))
1852 index--;
1853 if (s[index] == ' ')
1854 index++;
1856 } else {
1857 while (index && (s[index] != ' '))
1858 index--;
1859 if (s[index] == ' ')
1860 index++;
1862 ret = index;
1863 break;
1864 case WB_RIGHT:
1865 if (!count)
1866 break;
1867 if (index)
1868 index--;
1869 if (s[index] == ' ')
1870 while ((index < count) && (s[index] == ' ')) index++;
1871 else {
1872 while (s[index] && (s[index] != ' ') && (index < count))
1873 index++;
1874 while ((s[index] == ' ') && (index < count)) index++;
1876 ret = index;
1877 break;
1878 case WB_ISDELIMITER:
1879 ret = (s[index] == ' ');
1880 break;
1881 default:
1882 ERR(edit, "unknown action code, please report !\n");
1883 break;
1885 return ret;
1889 /*********************************************************************
1891 * EM_CHARFROMPOS
1893 * FIXME: do the specs mean to return LineIndex or LineNumber ???
1894 * Let's assume LineIndex is meant
1895 * FIXME: do the specs mean to return -1 if outside client area or
1896 * if outside formatting rectangle ???
1899 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y)
1901 POINT32 pt;
1902 RECT32 rc;
1903 INT32 index;
1905 pt.x = x;
1906 pt.y = y;
1907 GetClientRect32(wnd->hwndSelf, &rc);
1908 if (!PtInRect32(&rc, pt))
1909 return -1;
1911 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1912 return MAKELONG(index, EDIT_EM_LineIndex(wnd, es,
1913 EDIT_EM_LineFromChar(wnd, es, index)));
1917 /*********************************************************************
1919 * EM_FMTLINES
1921 * Enable or disable soft breaks.
1923 static BOOL32 EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL32 add_eol)
1925 es->flags &= ~EF_USE_SOFTBRK;
1926 if (add_eol) {
1927 es->flags |= EF_USE_SOFTBRK;
1928 FIXME(edit, "soft break enabled, not implemented\n");
1930 return add_eol;
1934 /*********************************************************************
1936 * EM_GETHANDLE
1938 * Hopefully this won't fire back at us.
1939 * We always start with a fixed buffer in our own heap.
1940 * However, with this message a 32 bit application requests
1941 * a handle to 32 bit moveable local heap memory, where it expects
1942 * to find the text.
1943 * It's a pity that from this moment on we have to use this
1944 * local heap, because applications may rely on the handle
1945 * in the future.
1947 * In this function we'll try to switch to local heap.
1950 static HLOCAL32 EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
1952 HLOCAL32 newBuf;
1953 LPSTR newText;
1954 INT32 newSize;
1956 if (!(es->style & ES_MULTILINE))
1957 return 0;
1959 if (es->hloc32)
1960 return es->hloc32;
1961 else if (es->hloc16)
1962 return (HLOCAL32)es->hloc16;
1964 if (!(newBuf = LocalAlloc32(LMEM_MOVEABLE, lstrlen32A(es->text) + 1))) {
1965 ERR(edit, "could not allocate new 32 bit buffer\n");
1966 return 0;
1968 newSize = MIN(LocalSize32(newBuf) - 1, es->buffer_limit);
1969 if (!(newText = LocalLock32(newBuf))) {
1970 ERR(edit, "could not lock new 32 bit buffer\n");
1971 LocalFree32(newBuf);
1972 return 0;
1974 lstrcpy32A(newText, es->text);
1975 EDIT_UnlockBuffer(wnd, es, TRUE);
1976 if (es->text)
1977 HeapFree(es->heap, 0, es->text);
1978 es->hloc32 = newBuf;
1979 es->hloc16 = (HLOCAL16)NULL;
1980 es->buffer_size = newSize;
1981 es->text = newText;
1982 EDIT_LockBuffer(wnd, es);
1983 TRACE(edit, "switched to 32 bit local heap\n");
1985 return es->hloc32;
1989 /*********************************************************************
1991 * EM_GETHANDLE16
1993 * Hopefully this won't fire back at us.
1994 * We always start with a buffer in 32 bit linear memory.
1995 * However, with this message a 16 bit application requests
1996 * a handle of 16 bit local heap memory, where it expects to find
1997 * the text.
1998 * It's a pitty that from this moment on we have to use this
1999 * local heap, because applications may rely on the handle
2000 * in the future.
2002 * In this function we'll try to switch to local heap.
2004 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2006 HLOCAL16 newBuf;
2007 LPSTR newText;
2008 INT32 newSize;
2010 if (!(es->style & ES_MULTILINE))
2011 return 0;
2013 if (es->hloc16)
2014 return es->hloc16;
2016 if (!LOCAL_HeapSize(wnd->hInstance)) {
2017 if (!LocalInit(wnd->hInstance, 0,
2018 GlobalSize16(wnd->hInstance))) {
2019 ERR(edit, "could not initialize local heap\n");
2020 return 0;
2022 TRACE(edit, "local heap initialized\n");
2024 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlen32A(es->text) + 1))) {
2025 ERR(edit, "could not allocate new 16 bit buffer\n");
2026 return 0;
2028 newSize = MIN(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2029 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2030 ERR(edit, "could not lock new 16 bit buffer\n");
2031 LOCAL_Free(wnd->hInstance, newBuf);
2032 return 0;
2034 lstrcpy32A(newText, es->text);
2035 EDIT_UnlockBuffer(wnd, es, TRUE);
2036 if (es->text)
2037 HeapFree(es->heap, 0, es->text);
2038 else if (es->hloc32) {
2039 while (LocalFree32(es->hloc32)) ;
2040 LocalFree32(es->hloc32);
2042 es->hloc32 = (HLOCAL32)NULL;
2043 es->hloc16 = newBuf;
2044 es->buffer_size = newSize;
2045 es->text = newText;
2046 EDIT_LockBuffer(wnd, es);
2047 TRACE(edit, "switched to 16 bit buffer\n");
2049 return es->hloc16;
2053 /*********************************************************************
2055 * EM_GETLINE
2058 static INT32 EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT32 line, LPSTR lpch)
2060 LPSTR src;
2061 INT32 len;
2062 INT32 i;
2064 if (es->style & ES_MULTILINE) {
2065 if (line >= es->line_count)
2066 return 0;
2067 } else
2068 line = 0;
2069 src = es->text + EDIT_EM_LineIndex(wnd, es, line);
2070 len = MIN(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, line));
2071 for (i = 0 ; i < len ; i++) {
2072 *lpch = *src;
2073 src++;
2074 lpch++;
2076 return (LRESULT)len;
2080 /*********************************************************************
2082 * EM_GETSEL
2085 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT32 start, LPUINT32 end)
2087 UINT32 s = es->selection_start;
2088 UINT32 e = es->selection_end;
2090 ORDER_UINT32(s, e);
2091 if (start)
2092 *start = s;
2093 if (end)
2094 *end = e;
2095 return MAKELONG(s, e);
2099 /*********************************************************************
2101 * EM_GETTHUMB
2103 * FIXME: is this right ? (or should it be only VSCROLL)
2104 * (and maybe only for edit controls that really have their
2105 * own scrollbars) (and maybe only for multiline controls ?)
2106 * All in all: very poorly documented
2108 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2109 * WM_VSCROLL handlers
2112 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2114 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0, 0),
2115 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0, 0));
2119 /*********************************************************************
2121 * EM_LINEFROMCHAR
2124 static INT32 EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT32 index)
2126 INT32 line;
2127 LINEDEF *line_def;
2129 if (!(es->style & ES_MULTILINE))
2130 return 0;
2131 if (index > lstrlen32A(es->text))
2132 return es->line_count - 1;
2133 if (index == -1)
2134 index = MIN(es->selection_start, es->selection_end);
2136 line = 0;
2137 line_def = es->first_line_def;
2138 index -= line_def->length;
2139 while ((index >= 0) && line_def->next) {
2140 line++;
2141 line_def = line_def->next;
2142 index -= line_def->length;
2144 return line;
2148 /*********************************************************************
2150 * EM_LINEINDEX
2153 static INT32 EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT32 line)
2155 INT32 line_index;
2156 LINEDEF *line_def;
2158 if (!(es->style & ES_MULTILINE))
2159 return 0;
2160 if (line >= es->line_count)
2161 return -1;
2163 line_index = 0;
2164 line_def = es->first_line_def;
2165 if (line == -1) {
2166 INT32 index = es->selection_end - line_def->length;
2167 while ((index >= 0) && line_def->next) {
2168 line_index += line_def->length;
2169 line_def = line_def->next;
2170 index -= line_def->length;
2172 } else {
2173 while (line > 0) {
2174 line_index += line_def->length;
2175 line_def = line_def->next;
2176 line--;
2179 return line_index;
2183 /*********************************************************************
2185 * EM_LINELENGTH
2188 static INT32 EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT32 index)
2190 LINEDEF *line_def;
2192 if (!(es->style & ES_MULTILINE))
2193 return lstrlen32A(es->text);
2195 if (index == -1) {
2196 /* FIXME: broken
2197 INT32 sl = EDIT_EM_LineFromChar(wnd, es, es->selection_start);
2198 INT32 el = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2199 return es->selection_start - es->line_defs[sl].offset +
2200 es->line_defs[el].offset +
2201 es->line_defs[el].length - es->selection_end;
2203 return 0;
2205 line_def = es->first_line_def;
2206 index -= line_def->length;
2207 while ((index >= 0) && line_def->next) {
2208 line_def = line_def->next;
2209 index -= line_def->length;
2211 return line_def->net_length;
2215 /*********************************************************************
2217 * EM_LINESCROLL
2219 * FIXME: dx is in average character widths
2220 * However, we assume it is in pixels when we use this
2221 * function internally
2224 static BOOL32 EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT32 dx, INT32 dy)
2226 INT32 nyoff;
2228 if (!(es->style & ES_MULTILINE))
2229 return FALSE;
2231 if (-dx > es->x_offset)
2232 dx = -es->x_offset;
2233 if (dx > es->text_width - es->x_offset)
2234 dx = es->text_width - es->x_offset;
2235 nyoff = MAX(0, es->y_offset + dy);
2236 if (nyoff >= es->line_count)
2237 nyoff = es->line_count - 1;
2238 dy = (es->y_offset - nyoff) * es->line_height;
2239 if (dx || dy) {
2240 RECT32 rc1;
2241 RECT32 rc;
2242 GetClientRect32(wnd->hwndSelf, &rc1);
2243 IntersectRect32(&rc, &rc1, &es->format_rect);
2244 ScrollWindowEx32(wnd->hwndSelf, -dx, dy,
2245 NULL, &rc, (HRGN32)NULL, NULL, SW_INVALIDATE);
2246 es->y_offset = nyoff;
2247 es->x_offset += dx;
2249 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2250 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2251 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2252 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2253 return TRUE;
2257 /*********************************************************************
2259 * EM_POSFROMCHAR
2262 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT32 index, BOOL32 after_wrap)
2264 INT32 len = lstrlen32A(es->text);
2265 INT32 l;
2266 INT32 li;
2267 INT32 x;
2268 INT32 y = 0;
2269 HDC32 dc;
2270 HFONT32 old_font = 0;
2271 SIZE32 size;
2273 index = MIN(index, len);
2274 dc = GetDC32(wnd->hwndSelf);
2275 if (es->font)
2276 old_font = SelectObject32(dc, es->font);
2277 if (es->style & ES_MULTILINE) {
2278 l = EDIT_EM_LineFromChar(wnd, es, index);
2279 y = (l - es->y_offset) * es->line_height;
2280 li = EDIT_EM_LineIndex(wnd, es, l);
2281 if (after_wrap && (li == index) && l) {
2282 INT32 l2 = l - 1;
2283 LINEDEF *line_def = es->first_line_def;
2284 while (l2) {
2285 line_def = line_def->next;
2286 l2--;
2288 if (line_def->ending == END_WRAP) {
2289 l--;
2290 y -= es->line_height;
2291 li = EDIT_EM_LineIndex(wnd, es, l);
2294 x = LOWORD(GetTabbedTextExtent32A(dc, es->text + li, index - li,
2295 es->tabs_count, es->tabs)) - es->x_offset;
2296 } else {
2297 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
2298 if (index < es->x_offset) {
2299 GetTextExtentPoint32A(dc, text + index,
2300 es->x_offset - index, &size);
2301 x = -size.cx;
2302 } else {
2303 GetTextExtentPoint32A(dc, text + es->x_offset,
2304 index - es->x_offset, &size);
2305 x = size.cx;
2307 y = 0;
2308 if (es->style & ES_PASSWORD)
2309 HeapFree(es->heap, 0 ,text);
2311 x += es->format_rect.left;
2312 y += es->format_rect.top;
2313 if (es->font)
2314 SelectObject32(dc, old_font);
2315 ReleaseDC32(wnd->hwndSelf, dc);
2316 return MAKELONG((INT16)x, (INT16)y);
2320 /*********************************************************************
2322 * EM_REPLACESEL
2324 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2327 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL32 can_undo, LPCSTR lpsz_replace)
2329 INT32 strl = lstrlen32A(lpsz_replace);
2330 INT32 tl = lstrlen32A(es->text);
2331 INT32 utl;
2332 UINT32 s;
2333 UINT32 e;
2334 INT32 i;
2335 LPSTR p;
2337 s = es->selection_start;
2338 e = es->selection_end;
2340 if ((s == e) && !strl)
2341 return;
2343 ORDER_UINT32(s, e);
2345 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2346 return;
2348 if (e != s) {
2349 /* there is something to be deleted */
2350 if (can_undo) {
2351 utl = lstrlen32A(es->undo_text);
2352 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2353 /* undo-buffer is extended to the right */
2354 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2355 lstrcpyn32A(es->undo_text + utl, es->text + s, e - s + 1);
2356 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2357 /* undo-buffer is extended to the left */
2358 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2359 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2360 p[e - s] = p[0];
2361 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2362 p[i] = (es->text + s)[i];
2363 es->undo_position = s;
2364 } else {
2365 /* new undo-buffer */
2366 EDIT_MakeUndoFit(wnd, es, e - s);
2367 lstrcpyn32A(es->undo_text, es->text + s, e - s + 1);
2368 es->undo_position = s;
2370 /* any deletion makes the old insertion-undo invalid */
2371 es->undo_insert_count = 0;
2372 } else
2373 EDIT_EM_EmptyUndoBuffer(wnd, es);
2375 /* now delete */
2376 lstrcpy32A(es->text + s, es->text + e);
2378 if (strl) {
2379 /* there is an insertion */
2380 if (can_undo) {
2381 if ((s == es->undo_position) ||
2382 ((es->undo_insert_count) &&
2383 (s == es->undo_position + es->undo_insert_count)))
2385 * insertion is new and at delete position or
2386 * an extension to either left or right
2388 es->undo_insert_count += strl;
2389 else {
2390 /* new insertion undo */
2391 es->undo_position = s;
2392 es->undo_insert_count = strl;
2393 /* new insertion makes old delete-buffer invalid */
2394 *es->undo_text = '\0';
2396 } else
2397 EDIT_EM_EmptyUndoBuffer(wnd, es);
2399 /* now insert */
2400 tl = lstrlen32A(es->text);
2401 for (p = es->text + tl ; p >= es->text + s ; p--)
2402 p[strl] = p[0];
2403 for (i = 0 , p = es->text + s ; i < strl ; i++)
2404 p[i] = lpsz_replace[i];
2405 if(es->style & ES_UPPERCASE)
2406 CharUpperBuff32A(p, strl);
2407 else if(es->style & ES_LOWERCASE)
2408 CharLowerBuff32A(p, strl);
2409 s += strl;
2411 /* FIXME: really inefficient */
2412 if (es->style & ES_MULTILINE)
2413 EDIT_BuildLineDefs_ML(wnd, es);
2415 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2416 es->flags |= EF_MODIFIED;
2417 es->flags |= EF_UPDATE;
2418 EDIT_EM_ScrollCaret(wnd, es);
2420 /* FIXME: really inefficient */
2421 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2425 /*********************************************************************
2427 * EM_SCROLL
2430 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT32 action)
2432 INT32 dy;
2434 if (!(es->style & ES_MULTILINE))
2435 return (LRESULT)FALSE;
2437 dy = 0;
2439 switch (action) {
2440 case SB_LINEUP:
2441 if (es->y_offset)
2442 dy = -1;
2443 break;
2444 case SB_LINEDOWN:
2445 if (es->y_offset < es->line_count - 1)
2446 dy = 1;
2447 break;
2448 case SB_PAGEUP:
2449 if (es->y_offset)
2450 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2451 break;
2452 case SB_PAGEDOWN:
2453 if (es->y_offset < es->line_count - 1)
2454 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2455 break;
2456 default:
2457 return (LRESULT)FALSE;
2459 if (dy) {
2460 EDIT_EM_LineScroll(wnd, es, 0, dy);
2461 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2463 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2467 /*********************************************************************
2469 * EM_SCROLLCARET
2472 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
2474 if (es->style & ES_MULTILINE) {
2475 INT32 l;
2476 INT32 li;
2477 INT32 vlc;
2478 INT32 ww;
2479 INT32 cw = es->char_width;
2480 INT32 x;
2481 INT32 dy = 0;
2482 INT32 dx = 0;
2484 l = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2485 li = EDIT_EM_LineIndex(wnd, es, l);
2486 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
2487 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2488 if (l >= es->y_offset + vlc)
2489 dy = l - vlc + 1 - es->y_offset;
2490 if (l < es->y_offset)
2491 dy = l - es->y_offset;
2492 ww = es->format_rect.right - es->format_rect.left;
2493 if (x < es->format_rect.left)
2494 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
2495 if (x > es->format_rect.right)
2496 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
2497 if (dy || dx)
2498 EDIT_EM_LineScroll(wnd, es, dx, dy);
2499 } else {
2500 INT32 x;
2501 INT32 goal;
2502 INT32 format_width;
2504 if (!(es->style & ES_AUTOHSCROLL))
2505 return;
2507 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2508 format_width = es->format_rect.right - es->format_rect.left;
2509 if (x < es->format_rect.left) {
2510 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
2511 do {
2512 es->x_offset--;
2513 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2514 } while ((x < goal) && es->x_offset);
2515 /* FIXME: use ScrollWindow() somehow to improve performance */
2516 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2517 } else if (x > es->format_rect.right) {
2518 INT32 x_last;
2519 INT32 len = lstrlen32A(es->text);
2520 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
2521 do {
2522 es->x_offset++;
2523 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
2524 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
2525 } while ((x > goal) && (x_last > es->format_rect.right));
2526 /* FIXME: use ScrollWindow() somehow to improve performance */
2527 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2533 /*********************************************************************
2535 * EM_SETHANDLE
2537 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2540 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL32 hloc)
2542 if (!(es->style & ES_MULTILINE))
2543 return;
2545 if (!hloc) {
2546 WARN(edit, "called with NULL handle\n");
2547 return;
2550 EDIT_UnlockBuffer(wnd, es, TRUE);
2552 * old buffer is freed by caller, unless
2553 * it is still in our own heap. (in that case
2554 * we free it, correcting the buggy caller.)
2556 if (es->text)
2557 HeapFree(es->heap, 0, es->text);
2559 es->hloc16 = (HLOCAL16)NULL;
2560 es->hloc32 = hloc;
2561 es->text = NULL;
2562 es->buffer_size = LocalSize32(es->hloc32) - 1;
2563 EDIT_LockBuffer(wnd, es);
2565 es->x_offset = es->y_offset = 0;
2566 es->selection_start = es->selection_end = 0;
2567 EDIT_EM_EmptyUndoBuffer(wnd, es);
2568 es->flags &= ~EF_MODIFIED;
2569 es->flags &= ~EF_UPDATE;
2570 EDIT_BuildLineDefs_ML(wnd, es);
2571 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2572 EDIT_EM_ScrollCaret(wnd, es);
2576 /*********************************************************************
2578 * EM_SETHANDLE16
2580 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2583 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2585 if (!(es->style & ES_MULTILINE))
2586 return;
2588 if (!hloc) {
2589 WARN(edit, "called with NULL handle\n");
2590 return;
2593 EDIT_UnlockBuffer(wnd, es, TRUE);
2595 * old buffer is freed by caller, unless
2596 * it is still in our own heap. (in that case
2597 * we free it, correcting the buggy caller.)
2599 if (es->text)
2600 HeapFree(es->heap, 0, es->text);
2602 es->hloc16 = hloc;
2603 es->hloc32 = (HLOCAL32)NULL;
2604 es->text = NULL;
2605 es->buffer_size = LOCAL_Size(wnd->hInstance, es->hloc16) - 1;
2606 EDIT_LockBuffer(wnd, es);
2608 es->x_offset = es->y_offset = 0;
2609 es->selection_start = es->selection_end = 0;
2610 EDIT_EM_EmptyUndoBuffer(wnd, es);
2611 es->flags &= ~EF_MODIFIED;
2612 es->flags &= ~EF_UPDATE;
2613 EDIT_BuildLineDefs_ML(wnd, es);
2614 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2615 EDIT_EM_ScrollCaret(wnd, es);
2619 /*********************************************************************
2621 * EM_SETLIMITTEXT
2623 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
2624 * However, the windows version is not complied to yet in all of edit.c
2627 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT32 limit)
2629 if (es->style & ES_MULTILINE) {
2630 if (limit)
2631 es->buffer_limit = MIN(limit, BUFLIMIT_MULTI);
2632 else
2633 es->buffer_limit = BUFLIMIT_MULTI;
2634 } else {
2635 if (limit)
2636 es->buffer_limit = MIN(limit, BUFLIMIT_SINGLE);
2637 else
2638 es->buffer_limit = BUFLIMIT_SINGLE;
2643 /*********************************************************************
2645 * EM_SETMARGINS
2647 * EC_USEFONTINFO is used as a left or right value i.e. lParam and not as an
2648 * action wParam despite what the docs say. It also appears not to affect
2649 * multiline controls??
2652 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT32 action,
2653 INT32 left, INT32 right)
2655 if (action & EC_LEFTMARGIN) {
2656 if (left != EC_USEFONTINFO)
2657 es->left_margin = left;
2658 else
2659 if (es->style & ES_MULTILINE)
2660 es->left_margin = 0; /* ?? */
2661 else
2662 es->left_margin = es->char_width;
2665 if (action & EC_RIGHTMARGIN) {
2666 if (right != EC_USEFONTINFO)
2667 es->right_margin = right;
2668 else
2669 if (es->style & ES_MULTILINE)
2670 es->right_margin = 0; /* ?? */
2671 else
2672 es->right_margin = es->char_width;
2674 TRACE(edit, "left=%d, right=%d\n", es->left_margin, es->right_margin);
2678 /*********************************************************************
2680 * EM_SETPASSWORDCHAR
2683 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c)
2685 if (es->style & ES_MULTILINE)
2686 return;
2688 if (es->password_char == c)
2689 return;
2691 es->password_char = c;
2692 if (c) {
2693 wnd->dwStyle |= ES_PASSWORD;
2694 es->style |= ES_PASSWORD;
2695 } else {
2696 wnd->dwStyle &= ~ES_PASSWORD;
2697 es->style &= ~ES_PASSWORD;
2699 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2703 /*********************************************************************
2705 * EDIT_EM_SetSel
2707 * note: unlike the specs say: the order of start and end
2708 * _is_ preserved in Windows. (i.e. start can be > end)
2709 * In other words: this handler is OK
2712 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT32 start, UINT32 end, BOOL32 after_wrap)
2714 UINT32 old_start = es->selection_start;
2715 UINT32 old_end = es->selection_end;
2716 UINT32 len = lstrlen32A(es->text);
2718 if (start == -1) {
2719 start = es->selection_end;
2720 end = es->selection_end;
2721 } else {
2722 start = MIN(start, len);
2723 end = MIN(end, len);
2725 es->selection_start = start;
2726 es->selection_end = end;
2727 if (after_wrap)
2728 es->flags |= EF_AFTER_WRAP;
2729 else
2730 es->flags &= ~EF_AFTER_WRAP;
2731 if (es->flags & EF_FOCUSED)
2732 EDIT_SetCaretPos(wnd, es, end, after_wrap);
2733 /* This is little bit more efficient than before, not sure if it can be improved. FIXME? */
2734 ORDER_UINT32(start, end);
2735 ORDER_UINT32(end, old_end);
2736 ORDER_UINT32(start, old_start);
2737 ORDER_UINT32(old_start, old_end);
2738 if (end != old_start)
2741 * One can also do
2742 * ORDER_UINT32(end, old_start);
2743 * EDIT_InvalidateText(wnd, es, start, end);
2744 * EDIT_InvalidateText(wnd, es, old_start, old_end);
2745 * in place of the following if statement.
2747 if (old_start > end )
2749 EDIT_InvalidateText(wnd, es, start, end);
2750 EDIT_InvalidateText(wnd, es, old_start, old_end);
2752 else
2754 EDIT_InvalidateText(wnd, es, start, old_start);
2755 EDIT_InvalidateText(wnd, es, end, old_end);
2758 else EDIT_InvalidateText(wnd, es, start, old_end);
2762 /*********************************************************************
2764 * EM_SETTABSTOPS
2767 static BOOL32 EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT32 count, LPINT32 tabs)
2769 if (!(es->style & ES_MULTILINE))
2770 return FALSE;
2771 if (es->tabs)
2772 HeapFree(es->heap, 0, es->tabs);
2773 es->tabs_count = count;
2774 if (!count)
2775 es->tabs = NULL;
2776 else {
2777 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT32));
2778 memcpy(es->tabs, tabs, count * sizeof(INT32));
2780 return TRUE;
2784 /*********************************************************************
2786 * EM_SETTABSTOPS16
2789 static BOOL32 EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT32 count, LPINT16 tabs)
2791 if (!(es->style & ES_MULTILINE))
2792 return FALSE;
2793 if (es->tabs)
2794 HeapFree(es->heap, 0, es->tabs);
2795 es->tabs_count = count;
2796 if (!count)
2797 es->tabs = NULL;
2798 else {
2799 INT32 i;
2800 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT32));
2801 for (i = 0 ; i < count ; i++)
2802 es->tabs[i] = *tabs++;
2804 return TRUE;
2808 /*********************************************************************
2810 * EM_SETWORDBREAKPROC
2813 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC32A wbp)
2815 if (es->word_break_proc32A == wbp)
2816 return;
2818 es->word_break_proc32A = wbp;
2819 es->word_break_proc16 = NULL;
2820 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2821 EDIT_BuildLineDefs_ML(wnd, es);
2822 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2827 /*********************************************************************
2829 * EM_SETWORDBREAKPROC16
2832 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
2834 if (es->word_break_proc16 == wbp)
2835 return;
2837 es->word_break_proc32A = NULL;
2838 es->word_break_proc16 = wbp;
2839 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2840 EDIT_BuildLineDefs_ML(wnd, es);
2841 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2846 /*********************************************************************
2848 * EM_UNDO / WM_UNDO
2851 static BOOL32 EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2853 INT32 ulength = lstrlen32A(es->undo_text);
2854 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2856 lstrcpy32A(utext, es->undo_text);
2858 TRACE(edit, "before UNDO:insertion length = %d, deletion buffer = %s\n",
2859 es->undo_insert_count, utext);
2861 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2862 EDIT_EM_EmptyUndoBuffer(wnd, es);
2863 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext);
2864 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2865 HeapFree(es->heap, 0, utext);
2867 TRACE(edit, "after UNDO:insertion length = %d, deletion buffer = %s\n",
2868 es->undo_insert_count, es->undo_text);
2870 return TRUE;
2874 /*********************************************************************
2876 * WM_CHAR
2879 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data)
2881 switch (c) {
2882 case '\r':
2883 case '\n':
2884 if (es->style & ES_MULTILINE) {
2885 if (es->style & ES_READONLY) {
2886 EDIT_MoveHome(wnd, es, FALSE);
2887 EDIT_MoveDown_ML(wnd, es, FALSE);
2888 } else
2889 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\r\n");
2891 break;
2892 case '\t':
2893 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2894 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\t");
2895 break;
2896 default:
2897 if (!(es->style & ES_READONLY) && ((BYTE)c >= ' ') && (c != 127)) {
2898 char str[2];
2899 str[0] = c;
2900 str[1] = '\0';
2901 EDIT_EM_ReplaceSel(wnd, es, TRUE, str);
2903 break;
2908 /*********************************************************************
2910 * WM_COMMAND
2913 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT32 code, INT32 id, HWND32 control)
2915 if (code || control)
2916 return;
2918 switch (id) {
2919 case EM_UNDO32:
2920 EDIT_EM_Undo(wnd, es);
2921 break;
2922 case WM_CUT:
2923 EDIT_WM_Cut(wnd, es);
2924 break;
2925 case WM_COPY:
2926 EDIT_WM_Copy(wnd, es);
2927 break;
2928 case WM_PASTE:
2929 EDIT_WM_Paste(wnd, es);
2930 break;
2931 case WM_CLEAR:
2932 EDIT_WM_Clear(wnd, es);
2933 break;
2934 case EM_SETSEL32:
2935 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
2936 EDIT_EM_ScrollCaret(wnd, es);
2937 break;
2938 default:
2939 ERR(edit, "unknown menu item, please report\n");
2940 break;
2945 /*********************************************************************
2947 * WM_CONTEXTMENU
2949 * Note: the resource files resource/sysres_??.rc cannot define a
2950 * single popup menu. Hence we use a (dummy) menubar
2951 * containing the single popup menu as its first item.
2953 * FIXME: the message identifiers have been chosen arbitrarily,
2954 * hence we use MF_BYPOSITION.
2955 * We might as well use the "real" values (anybody knows ?)
2956 * The menu definition is in resources/sysres_??.rc.
2957 * Once these are OK, we better use MF_BYCOMMAND here
2958 * (as we do in EDIT_WM_Command()).
2961 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND32 hwnd, INT32 x, INT32 y)
2963 HMENU32 menu = LoadMenuIndirect32A(SYSRES_GetResPtr(SYSRES_MENU_EDITMENU));
2964 HMENU32 popup = GetSubMenu32(menu, 0);
2965 UINT32 start = es->selection_start;
2966 UINT32 end = es->selection_end;
2968 ORDER_UINT32(start, end);
2970 /* undo */
2971 EnableMenuItem32(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
2972 /* cut */
2973 EnableMenuItem32(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2974 /* copy */
2975 EnableMenuItem32(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2976 /* paste */
2977 EnableMenuItem32(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable32(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
2978 /* delete */
2979 EnableMenuItem32(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
2980 /* select all */
2981 EnableMenuItem32(popup, 7, MF_BYPOSITION | (start || (end != lstrlen32A(es->text)) ? MF_ENABLED : MF_GRAYED));
2983 TrackPopupMenu32(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
2984 DestroyMenu32(menu);
2988 /*********************************************************************
2990 * WM_COPY
2992 * FIXME: replace with 32 bit calls as soon as they are implemented
2993 * in the clipboard code
2996 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
2998 INT32 s = es->selection_start;
2999 INT32 e = es->selection_end;
3000 HGLOBAL16 hdst;
3001 LPSTR dst;
3003 if (e == s)
3004 return;
3005 ORDER_INT32(s, e);
3006 hdst = GlobalAlloc16(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3007 dst = GlobalLock16(hdst);
3008 lstrcpyn32A(dst, es->text + s, e - s + 1);
3009 GlobalUnlock16(hdst);
3010 OpenClipboard32(wnd->hwndSelf);
3011 EmptyClipboard32();
3012 SetClipboardData16(CF_TEXT, hdst);
3013 CloseClipboard32();
3017 /*********************************************************************
3019 * WM_CREATE
3022 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCT32A cs)
3025 * To initialize some final structure members, we call some helper
3026 * functions. However, since the EDITSTATE is not consistent (i.e.
3027 * not fully initialized), we should be very careful which
3028 * functions can be called, and in what order.
3030 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3031 if (cs->lpszName && *(cs->lpszName) != '\0') {
3032 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3033 /* 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 */
3034 es->selection_start = es->selection_end = 0;
3035 EDIT_EM_ScrollCaret(wnd, es);
3037 return 0;
3041 /*********************************************************************
3043 * WM_DESTROY
3046 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3048 if (es->hloc32) {
3049 while (LocalUnlock32(es->hloc32)) ;
3050 LocalFree32(es->hloc32);
3052 if (es->hloc16) {
3053 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3054 LOCAL_Free(wnd->hInstance, es->hloc16);
3056 HeapDestroy(es->heap);
3057 HeapFree(GetProcessHeap(), 0, es);
3058 *(EDITSTATE **)wnd->wExtra = NULL;
3062 /*********************************************************************
3064 * WM_ERASEBKGND
3067 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC32 dc)
3069 HBRUSH32 brush;
3070 RECT32 rc;
3072 if (!(brush = (HBRUSH32)EDIT_SEND_CTLCOLOR(wnd, dc)))
3073 brush = (HBRUSH32)GetStockObject32(WHITE_BRUSH);
3075 GetClientRect32(wnd->hwndSelf, &rc);
3076 IntersectClipRect32(dc, rc.left, rc.top, rc.right, rc.bottom);
3077 GetClipBox32(dc, &rc);
3079 * FIXME: specs say that we should UnrealizeObject() the brush,
3080 * but the specs of UnrealizeObject() say that we shouldn't
3081 * unrealize a stock object. The default brush that
3082 * DefWndProc() returns is ... a stock object.
3084 FillRect32(dc, &rc, brush);
3085 return -1;
3089 /*********************************************************************
3091 * WM_GETTEXT
3094 static INT32 EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT32 count, LPSTR text)
3096 INT32 len = lstrlen32A(es->text);
3098 if (count > len) {
3099 lstrcpy32A(text, es->text);
3100 return len;
3101 } else
3102 return -1;
3106 /*********************************************************************
3108 * EDIT_HScroll_Hack
3110 * 16 bit notepad needs this. Actually it is not _our_ hack,
3111 * it is notepad's. Notepad is sending us scrollbar messages with
3112 * undocumented parameters without us even having a scrollbar ... !?!?
3115 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3117 INT32 dx = 0;
3118 INT32 fw = es->format_rect.right - es->format_rect.left;
3119 LRESULT ret = 0;
3121 if (!(es->flags & EF_HSCROLL_HACK)) {
3122 ERR(edit, "hacked WM_HSCROLL handler invoked\n");
3123 ERR(edit, " if you are _not_ running 16 bit notepad, please report\n");
3124 ERR(edit, " (this message is only displayed once per edit control)\n");
3125 es->flags |= EF_HSCROLL_HACK;
3128 switch (action) {
3129 case SB_LINELEFT:
3130 if (es->x_offset)
3131 dx = -es->char_width;
3132 break;
3133 case SB_LINERIGHT:
3134 if (es->x_offset < es->text_width)
3135 dx = es->char_width;
3136 break;
3137 case SB_PAGELEFT:
3138 if (es->x_offset)
3139 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3140 break;
3141 case SB_PAGERIGHT:
3142 if (es->x_offset < es->text_width)
3143 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3144 break;
3145 case SB_LEFT:
3146 if (es->x_offset)
3147 dx = -es->x_offset;
3148 break;
3149 case SB_RIGHT:
3150 if (es->x_offset < es->text_width)
3151 dx = es->text_width - es->x_offset;
3152 break;
3153 case SB_THUMBTRACK:
3154 es->flags |= EF_HSCROLL_TRACK;
3155 dx = pos * es->text_width / 100 - es->x_offset;
3156 break;
3157 case SB_THUMBPOSITION:
3158 es->flags &= ~EF_HSCROLL_TRACK;
3159 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3160 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3161 break;
3162 case SB_ENDSCROLL:
3163 break;
3166 * FIXME : the next two are undocumented !
3167 * Are we doing the right thing ?
3168 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3169 * although it's also a regular control message.
3171 case EM_GETTHUMB16:
3172 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3173 break;
3174 case EM_LINESCROLL16:
3175 dx = pos;
3176 break;
3178 default:
3179 ERR(edit, "undocumented (hacked) WM_HSCROLL parameter, please report\n");
3180 return 0;
3182 if (dx)
3183 EDIT_EM_LineScroll(wnd, es, dx, 0);
3184 return ret;
3188 /*********************************************************************
3190 * WM_HSCROLL
3193 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3195 INT32 dx;
3196 INT32 fw;
3198 if (!(es->style & ES_MULTILINE))
3199 return 0;
3201 if (!(es->style & ES_AUTOHSCROLL))
3202 return 0;
3204 if (!(es->style & WS_HSCROLL))
3205 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3207 dx = 0;
3208 fw = es->format_rect.right - es->format_rect.left;
3209 switch (action) {
3210 case SB_LINELEFT:
3211 if (es->x_offset)
3212 dx = -es->char_width;
3213 break;
3214 case SB_LINERIGHT:
3215 if (es->x_offset < es->text_width)
3216 dx = es->char_width;
3217 break;
3218 case SB_PAGELEFT:
3219 if (es->x_offset)
3220 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3221 break;
3222 case SB_PAGERIGHT:
3223 if (es->x_offset < es->text_width)
3224 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3225 break;
3226 case SB_LEFT:
3227 if (es->x_offset)
3228 dx = -es->x_offset;
3229 break;
3230 case SB_RIGHT:
3231 if (es->x_offset < es->text_width)
3232 dx = es->text_width - es->x_offset;
3233 break;
3234 case SB_THUMBTRACK:
3235 es->flags |= EF_HSCROLL_TRACK;
3236 dx = pos - es->x_offset;
3237 break;
3238 case SB_THUMBPOSITION:
3239 es->flags &= ~EF_HSCROLL_TRACK;
3240 if (!(dx = pos - es->x_offset)) {
3241 SetScrollPos32(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3242 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3244 break;
3245 case SB_ENDSCROLL:
3246 break;
3248 default:
3249 ERR(edit, "undocumented WM_HSCROLL parameter, please report\n");
3250 return 0;
3252 if (dx)
3253 EDIT_EM_LineScroll(wnd, es, dx, 0);
3254 return 0;
3258 /*********************************************************************
3260 * EDIT_CheckCombo
3263 static BOOL32 EDIT_CheckCombo(WND *wnd, UINT32 msg, INT32 key, DWORD key_data)
3265 HWND32 hLBox;
3267 if (WIDGETS_IsControl32(wnd->parent, BIC32_COMBO) &&
3268 (hLBox = COMBO_GetLBWindow(wnd->parent))) {
3269 HWND32 hCombo = wnd->parent->hwndSelf;
3270 BOOL32 bUIFlip = TRUE;
3272 TRACE(combo, "[%04x]: handling msg %04x (%04x)\n",
3273 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3275 switch (msg) {
3276 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3277 if (key != VK_F4) {
3278 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETEXTENDEDUI32, 0, 0);
3279 if (SendMessage32A(hCombo, CB_GETDROPPEDSTATE32, 0, 0))
3280 bUIFlip = FALSE;
3282 if (!bUIFlip)
3283 SendMessage32A(hLBox, WM_KEYDOWN, (WPARAM32)key, 0);
3284 else {
3285 /* make sure ComboLBox pops up */
3286 SendMessage32A(hCombo, CB_SETEXTENDEDUI32, 0, 0);
3287 SendMessage32A(hLBox, WM_KEYDOWN, VK_F4, 0);
3288 SendMessage32A(hCombo, CB_SETEXTENDEDUI32, 1, 0);
3290 break;
3291 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3292 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETEXTENDEDUI32, 0, 0);
3293 if (bUIFlip) {
3294 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETDROPPEDSTATE32, 0, 0);
3295 SendMessage32A(hCombo, CB_SHOWDROPDOWN32, (bUIFlip) ? FALSE : TRUE, 0);
3296 } else
3297 SendMessage32A(hLBox, WM_KEYDOWN, VK_F4, 0);
3298 break;
3300 return TRUE;
3302 return FALSE;
3306 /*********************************************************************
3308 * WM_KEYDOWN
3310 * Handling of special keys that don't produce a WM_CHAR
3311 * (i.e. non-printable keys) & Backspace & Delete
3314 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data)
3316 BOOL32 shift;
3317 BOOL32 control;
3319 if (GetKeyState32(VK_MENU) & 0x8000)
3320 return 0;
3322 shift = GetKeyState32(VK_SHIFT) & 0x8000;
3323 control = GetKeyState32(VK_CONTROL) & 0x8000;
3325 switch (key) {
3326 case VK_F4:
3327 case VK_UP:
3328 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3329 break;
3330 if (key == VK_F4)
3331 break;
3332 /* fall through */
3333 case VK_LEFT:
3334 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3335 EDIT_MoveUp_ML(wnd, es, shift);
3336 else
3337 if (control)
3338 EDIT_MoveWordBackward(wnd, es, shift);
3339 else
3340 EDIT_MoveBackward(wnd, es, shift);
3341 break;
3342 case VK_DOWN:
3343 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3344 break;
3345 /* fall through */
3346 case VK_RIGHT:
3347 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3348 EDIT_MoveDown_ML(wnd, es, shift);
3349 else if (control)
3350 EDIT_MoveWordForward(wnd, es, shift);
3351 else
3352 EDIT_MoveForward(wnd, es, shift);
3353 break;
3354 case VK_HOME:
3355 EDIT_MoveHome(wnd, es, shift);
3356 break;
3357 case VK_END:
3358 EDIT_MoveEnd(wnd, es, shift);
3359 break;
3360 case VK_PRIOR:
3361 if (es->style & ES_MULTILINE)
3362 EDIT_MovePageUp_ML(wnd, es, shift);
3363 break;
3364 case VK_NEXT:
3365 if (es->style & ES_MULTILINE)
3366 EDIT_MovePageDown_ML(wnd, es, shift);
3367 break;
3368 case VK_BACK:
3369 if (!(es->style & ES_READONLY) && !control)
3370 if (es->selection_start != es->selection_end)
3371 EDIT_WM_Clear(wnd, es);
3372 else {
3373 /* delete character left of caret */
3374 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3375 EDIT_MoveBackward(wnd, es, TRUE);
3376 EDIT_WM_Clear(wnd, es);
3378 break;
3379 case VK_DELETE:
3380 if (!(es->style & ES_READONLY) && !(shift && control))
3381 if (es->selection_start != es->selection_end) {
3382 if (shift)
3383 EDIT_WM_Cut(wnd, es);
3384 else
3385 EDIT_WM_Clear(wnd, es);
3386 } else {
3387 if (shift) {
3388 /* delete character left of caret */
3389 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3390 EDIT_MoveBackward(wnd, es, TRUE);
3391 EDIT_WM_Clear(wnd, es);
3392 } else if (control) {
3393 /* delete to end of line */
3394 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3395 EDIT_MoveEnd(wnd, es, TRUE);
3396 EDIT_WM_Clear(wnd, es);
3397 } else {
3398 /* delete character right of caret */
3399 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3400 EDIT_MoveForward(wnd, es, TRUE);
3401 EDIT_WM_Clear(wnd, es);
3404 break;
3405 case VK_INSERT:
3406 if (shift) {
3407 if (!(es->style & ES_READONLY))
3408 EDIT_WM_Paste(wnd, es);
3409 } else if (control)
3410 EDIT_WM_Copy(wnd, es);
3411 break;
3413 return 0;
3417 /*********************************************************************
3419 * WM_KILLFOCUS
3422 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND32 window_getting_focus)
3424 es->flags &= ~EF_FOCUSED;
3425 DestroyCaret32();
3426 if(!(es->style & ES_NOHIDESEL))
3427 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3428 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3429 return 0;
3433 /*********************************************************************
3435 * WM_LBUTTONDBLCLK
3437 * The caret position has been set on the WM_LBUTTONDOWN message
3440 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3442 INT32 s;
3443 INT32 e = es->selection_end;
3444 INT32 l;
3445 INT32 li;
3446 INT32 ll;
3448 if (!(es->flags & EF_FOCUSED))
3449 return 0;
3451 l = EDIT_EM_LineFromChar(wnd, es, e);
3452 li = EDIT_EM_LineIndex(wnd, es, l);
3453 ll = EDIT_EM_LineLength(wnd, es, e);
3454 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3455 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3456 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3457 EDIT_EM_ScrollCaret(wnd, es);
3458 return 0;
3462 /*********************************************************************
3464 * WM_LBUTTONDOWN
3467 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3469 INT32 e;
3470 BOOL32 after_wrap;
3472 if (!(es->flags & EF_FOCUSED))
3473 return 0;
3475 SetCapture32(wnd->hwndSelf);
3476 EDIT_ConfinePoint(wnd, es, &x, &y);
3477 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3478 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3479 EDIT_EM_ScrollCaret(wnd, es);
3480 es->region_posx = es->region_posy = 0;
3481 SetTimer32(wnd->hwndSelf, 0, 100, NULL);
3482 return 0;
3486 /*********************************************************************
3488 * WM_LBUTTONUP
3491 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3493 if (GetCapture32() == wnd->hwndSelf) {
3494 KillTimer32(wnd->hwndSelf, 0);
3495 ReleaseCapture();
3497 return 0;
3501 /*********************************************************************
3503 * WM_MOUSEMOVE
3506 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3508 INT32 e;
3509 BOOL32 after_wrap;
3510 INT32 prex, prey;
3512 if (GetCapture32() != wnd->hwndSelf)
3513 return 0;
3516 * FIXME: gotta do some scrolling if outside client
3517 * area. Maybe reset the timer ?
3519 prex = x; prey = y;
3520 EDIT_ConfinePoint(wnd, es, &x, &y);
3521 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3522 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3523 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3524 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3525 return 0;
3529 /*********************************************************************
3531 * WM_NCCREATE
3534 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCT32A cs)
3536 EDITSTATE *es;
3538 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3539 return FALSE;
3540 *(EDITSTATE **)wnd->wExtra = es;
3541 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3542 return FALSE;
3543 es->style = cs->style;
3545 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3546 SetWindowLong32A(wnd->hwndSelf, GWL_STYLE,
3547 es->style & ~WS_BORDER);
3549 if (es->style & ES_MULTILINE) {
3550 es->buffer_size = BUFSTART_MULTI;
3551 es->buffer_limit = BUFLIMIT_MULTI;
3552 if (es->style & WS_VSCROLL)
3553 es->style |= ES_AUTOVSCROLL;
3554 if (es->style & WS_HSCROLL)
3555 es->style |= ES_AUTOHSCROLL;
3556 es->style &= ~ES_PASSWORD;
3557 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3558 if (es->style & ES_RIGHT)
3559 es->style &= ~ES_CENTER;
3560 es->style &= ~WS_HSCROLL;
3561 es->style &= ~ES_AUTOHSCROLL;
3564 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3565 es->style |= ES_AUTOVSCROLL;
3566 } else {
3567 es->buffer_size = BUFSTART_SINGLE;
3568 es->buffer_limit = BUFLIMIT_SINGLE;
3569 es->style &= ~ES_CENTER;
3570 es->style &= ~ES_RIGHT;
3571 es->style &= ~WS_HSCROLL;
3572 es->style &= ~WS_VSCROLL;
3573 es->style &= ~ES_AUTOVSCROLL;
3574 es->style &= ~ES_WANTRETURN;
3575 if (es->style & ES_UPPERCASE) {
3576 es->style &= ~ES_LOWERCASE;
3577 es->style &= ~ES_NUMBER;
3578 } else if (es->style & ES_LOWERCASE)
3579 es->style &= ~ES_NUMBER;
3580 if (es->style & ES_PASSWORD)
3581 es->password_char = '*';
3583 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3584 es->style |= ES_AUTOHSCROLL;
3586 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3587 return FALSE;
3588 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3589 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3590 return FALSE;
3591 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3592 *es->text = '\0';
3593 if (es->style & ES_MULTILINE)
3594 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3595 return FALSE;
3596 es->line_count = 1;
3598 return TRUE;
3601 /*********************************************************************
3603 * WM_PAINT
3606 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es)
3608 PAINTSTRUCT32 ps;
3609 INT32 i;
3610 HDC32 dc;
3611 HFONT32 old_font = 0;
3612 RECT32 rc;
3613 RECT32 rcLine;
3614 RECT32 rcRgn;
3615 BOOL32 rev = IsWindowEnabled32(wnd->hwndSelf) &&
3616 ((es->flags & EF_FOCUSED) ||
3617 (es->style & ES_NOHIDESEL));
3619 if (es->flags & EF_UPDATE)
3620 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
3622 dc = BeginPaint32(wnd->hwndSelf, &ps);
3623 if(es->style & WS_BORDER) {
3624 GetClientRect32(wnd->hwndSelf, &rc);
3625 if(es->style & ES_MULTILINE) {
3626 if(es->style & WS_HSCROLL) rc.bottom++;
3627 if(es->style & WS_VSCROLL) rc.right++;
3629 Rectangle32(dc, rc.left, rc.top, rc.right, rc.bottom);
3631 IntersectClipRect32(dc, es->format_rect.left,
3632 es->format_rect.top,
3633 es->format_rect.right,
3634 es->format_rect.bottom);
3635 if (es->style & ES_MULTILINE) {
3636 GetClientRect32(wnd->hwndSelf, &rc);
3637 IntersectClipRect32(dc, rc.left, rc.top, rc.right, rc.bottom);
3639 if (es->font)
3640 old_font = SelectObject32(dc, es->font);
3641 EDIT_SEND_CTLCOLOR(wnd, dc);
3642 if (!IsWindowEnabled32(wnd->hwndSelf))
3643 SetTextColor32(dc, GetSysColor32(COLOR_GRAYTEXT));
3644 GetClipBox32(dc, &rcRgn);
3645 if (es->style & ES_MULTILINE) {
3646 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3647 for (i = es->y_offset ; i <= MIN(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3648 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3649 if (IntersectRect32(&rc, &rcRgn, &rcLine))
3650 EDIT_PaintLine(wnd, es, dc, i, rev);
3652 } else {
3653 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3654 if (IntersectRect32(&rc, &rcRgn, &rcLine))
3655 EDIT_PaintLine(wnd, es, dc, 0, rev);
3657 if (es->font)
3658 SelectObject32(dc, old_font);
3659 if (es->flags & EF_FOCUSED)
3660 EDIT_SetCaretPos(wnd, es, es->selection_end,
3661 es->flags & EF_AFTER_WRAP);
3662 EndPaint32(wnd->hwndSelf, &ps);
3663 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3664 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3665 SCROLLINFO si;
3666 si.cbSize = sizeof(SCROLLINFO);
3667 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3668 si.nMin = 0;
3669 si.nMax = es->line_count + vlc - 2;
3670 si.nPage = vlc;
3671 si.nPos = es->y_offset;
3672 SetScrollInfo32(wnd->hwndSelf, SB_VERT, &si, TRUE);
3674 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3675 SCROLLINFO si;
3676 INT32 fw = es->format_rect.right - es->format_rect.left;
3677 si.cbSize = sizeof(SCROLLINFO);
3678 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3679 si.nMin = 0;
3680 si.nMax = es->text_width + fw - 1;
3681 si.nPage = fw;
3682 si.nPos = es->x_offset;
3683 SetScrollInfo32(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3686 if (es->flags & EF_UPDATE) {
3687 es->flags &= ~EF_UPDATE;
3688 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3693 /*********************************************************************
3695 * WM_PASTE
3697 * FIXME: replace with 32 bit handler once GetClipboardData32() is
3698 * implemented in misc/clipboard.c
3701 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3703 HGLOBAL16 hsrc;
3704 LPSTR src;
3706 OpenClipboard32(wnd->hwndSelf);
3707 if ((hsrc = GetClipboardData16(CF_TEXT))) {
3708 src = (LPSTR)GlobalLock16(hsrc);
3709 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3710 GlobalUnlock16(hsrc);
3712 CloseClipboard32();
3716 /*********************************************************************
3718 * WM_SETFOCUS
3721 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND32 window_losing_focus)
3723 es->flags |= EF_FOCUSED;
3724 CreateCaret32(wnd->hwndSelf, 0, 2, es->line_height);
3725 EDIT_SetCaretPos(wnd, es, es->selection_end,
3726 es->flags & EF_AFTER_WRAP);
3727 if(!(es->style & ES_NOHIDESEL))
3728 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3729 ShowCaret32(wnd->hwndSelf);
3730 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3734 /*********************************************************************
3736 * WM_SETFONT
3738 * With Win95 look the margins are set to default font value unless
3739 * the system font (font == 0) is being set, in which case they are left
3740 * unchanged.
3743 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT32 font, BOOL32 redraw)
3745 TEXTMETRIC32A tm;
3746 HDC32 dc;
3747 HFONT32 old_font = 0;
3749 es->font = font;
3750 dc = GetDC32(wnd->hwndSelf);
3751 if (font)
3752 old_font = SelectObject32(dc, font);
3753 GetTextMetrics32A(dc, &tm);
3754 es->line_height = tm.tmHeight;
3755 es->char_width = tm.tmAveCharWidth;
3756 if (font)
3757 SelectObject32(dc, old_font);
3758 ReleaseDC32(wnd->hwndSelf, dc);
3759 if (font & TWEAK_Win95Look)
3760 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3761 EC_USEFONTINFO, EC_USEFONTINFO);
3762 if (es->style & ES_MULTILINE)
3763 EDIT_BuildLineDefs_ML(wnd, es);
3764 else {
3765 RECT32 r;
3766 GetClientRect32(wnd->hwndSelf, &r);
3767 EDIT_SetRectNP(wnd, es, &r);
3769 if (redraw)
3770 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
3771 if (es->flags & EF_FOCUSED) {
3772 DestroyCaret32();
3773 CreateCaret32(wnd->hwndSelf, 0, 2, es->line_height);
3774 EDIT_SetCaretPos(wnd, es, es->selection_end,
3775 es->flags & EF_AFTER_WRAP);
3776 ShowCaret32(wnd->hwndSelf);
3781 /*********************************************************************
3783 * WM_SETTEXT
3786 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3788 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3789 if (text) {
3790 TRACE(edit, "\t'%s'\n", text);
3791 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3792 } else {
3793 TRACE(edit, "\t<NULL>\n");
3794 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3796 es->x_offset = 0;
3797 es->flags &= ~EF_MODIFIED;
3798 es->flags &= ~EF_UPDATE;
3799 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3800 EDIT_EM_ScrollCaret(wnd, es);
3804 /*********************************************************************
3806 * WM_SIZE
3809 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT32 action, INT32 width, INT32 height)
3811 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3812 RECT32 rc;
3813 SetRect32(&rc, 0, 0, width, height);
3814 EDIT_SetRectNP(wnd, es, &rc);
3815 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
3820 /*********************************************************************
3822 * WM_SYSKEYDOWN
3825 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data)
3827 if ((key == VK_BACK) && (key_data & 0x2000)) {
3828 if (EDIT_EM_CanUndo(wnd, es))
3829 EDIT_EM_Undo(wnd, es);
3830 return 0;
3831 } else if (key == VK_UP || key == VK_DOWN)
3832 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
3833 return 0;
3834 return DefWindowProc32A(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM32)key, (LPARAM)key_data);
3838 /*********************************************************************
3840 * WM_TIMER
3843 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT32 id, TIMERPROC32 timer_proc)
3845 if (es->region_posx < 0) {
3846 EDIT_MoveBackward(wnd, es, TRUE);
3847 } else if (es->region_posx > 0) {
3848 EDIT_MoveForward(wnd, es, TRUE);
3851 * FIXME: gotta do some vertical scrolling here, like
3852 * EDIT_EM_LineScroll(wnd, 0, 1);
3857 /*********************************************************************
3859 * EDIT_VScroll_Hack
3861 * 16 bit notepad needs this. Actually it is not _our_ hack,
3862 * it is notepad's. Notepad is sending us scrollbar messages with
3863 * undocumented parameters without us even having a scrollbar ... !?!?
3866 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3868 INT32 dy = 0;
3869 LRESULT ret = 0;
3871 if (!(es->flags & EF_VSCROLL_HACK)) {
3872 ERR(edit, "hacked WM_VSCROLL handler invoked\n");
3873 ERR(edit, " if you are _not_ running 16 bit notepad, please report\n");
3874 ERR(edit, " (this message is only displayed once per edit control)\n");
3875 es->flags |= EF_VSCROLL_HACK;
3878 switch (action) {
3879 case SB_LINEUP:
3880 case SB_LINEDOWN:
3881 case SB_PAGEUP:
3882 case SB_PAGEDOWN:
3883 EDIT_EM_Scroll(wnd, es, action);
3884 return 0;
3885 case SB_TOP:
3886 dy = -es->y_offset;
3887 break;
3888 case SB_BOTTOM:
3889 dy = es->line_count - 1 - es->y_offset;
3890 break;
3891 case SB_THUMBTRACK:
3892 es->flags |= EF_VSCROLL_TRACK;
3893 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
3894 break;
3895 case SB_THUMBPOSITION:
3896 es->flags &= ~EF_VSCROLL_TRACK;
3897 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
3898 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3899 break;
3900 case SB_ENDSCROLL:
3901 break;
3904 * FIXME : the next two are undocumented !
3905 * Are we doing the right thing ?
3906 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3907 * although it's also a regular control message.
3909 case EM_GETTHUMB16:
3910 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
3911 break;
3912 case EM_LINESCROLL16:
3913 dy = pos;
3914 break;
3916 default:
3917 ERR(edit, "undocumented (hacked) WM_VSCROLL parameter, please report\n");
3918 return 0;
3920 if (dy)
3921 EDIT_EM_LineScroll(wnd, es, 0, dy);
3922 return ret;
3926 /*********************************************************************
3928 * WM_VSCROLL
3931 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3933 INT32 dy;
3935 if (!(es->style & ES_MULTILINE))
3936 return 0;
3938 if (!(es->style & ES_AUTOVSCROLL))
3939 return 0;
3941 if (!(es->style & WS_VSCROLL))
3942 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
3944 dy = 0;
3945 switch (action) {
3946 case SB_LINEUP:
3947 case SB_LINEDOWN:
3948 case SB_PAGEUP:
3949 case SB_PAGEDOWN:
3950 EDIT_EM_Scroll(wnd, es, action);
3951 return 0;
3953 case SB_TOP:
3954 dy = -es->y_offset;
3955 break;
3956 case SB_BOTTOM:
3957 dy = es->line_count - 1 - es->y_offset;
3958 break;
3959 case SB_THUMBTRACK:
3960 es->flags |= EF_VSCROLL_TRACK;
3961 dy = pos - es->y_offset;
3962 break;
3963 case SB_THUMBPOSITION:
3964 es->flags &= ~EF_VSCROLL_TRACK;
3965 if (!(dy = pos - es->y_offset)) {
3966 SetScrollPos32(wnd->hwndSelf, SB_VERT, pos, TRUE);
3967 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3969 break;
3970 case SB_ENDSCROLL:
3971 break;
3973 default:
3974 ERR(edit, "undocumented WM_VSCROLL action %d, please report\n",
3975 action);
3976 return 0;
3978 if (dy)
3979 EDIT_EM_LineScroll(wnd, es, 0, dy);
3980 return 0;