Adapted to cursor/icon handling changes.
[wine/multimedia.git] / controls / edit.c
blob125ed0fdb3f8e1c1566745b13dfef3b96c2a03d3
1 /*
2 * Edit control
4 * Copyright David W. Metcalfe, 1994
5 * Copyright William Magro, 1995, 1996
6 * Copyright Frans van Dorsselaer, 1996, 1997
8 */
11 * please read EDIT.TODO (and update it when you change things)
14 #include <string.h>
15 #include "winnt.h"
16 #include "win.h"
17 #include "wine/winbase16.h"
18 #include "combo.h"
19 #include "local.h"
20 #include "resource.h"
21 #include "debug.h"
22 #include "callback.h"
23 #include "tweak.h"
25 #define BUFLIMIT_MULTI 65534 /* maximum buffer size (not including '\0')
26 FIXME: BTW, new specs say 65535 (do you dare ???) */
27 #define BUFLIMIT_SINGLE 32766 /* maximum buffer size (not including '\0') */
28 #define BUFSTART_MULTI 1024 /* starting size */
29 #define BUFSTART_SINGLE 256 /* starting size */
30 #define GROWLENGTH 64 /* buffers grow by this much */
31 #define HSCROLL_FRACTION 3 /* scroll window by 1/3 width */
34 * extra flags for EDITSTATE.flags field
36 #define EF_MODIFIED 0x0001 /* text has been modified */
37 #define EF_FOCUSED 0x0002 /* we have input focus */
38 #define EF_UPDATE 0x0004 /* notify parent of changed state on next WM_PAINT */
39 #define EF_VSCROLL_TRACK 0x0008 /* don't SetScrollPos() since we are tracking the thumb */
40 #define EF_HSCROLL_TRACK 0x0010 /* don't SetScrollPos() since we are tracking the thumb */
41 #define EF_VSCROLL_HACK 0x0020 /* we already have informed the user of the hacked handler */
42 #define EF_HSCROLL_HACK 0x0040 /* we already have informed the user of the hacked handler */
43 #define EF_AFTER_WRAP 0x0080 /* the caret is displayed after the last character of a
44 wrapped line, instead of in front of the next character */
45 #define EF_USE_SOFTBRK 0x0100 /* Enable soft breaks in text. */
47 typedef enum
49 END_0 = 0, /* line ends with terminating '\0' character */
50 END_WRAP, /* line is wrapped */
51 END_HARD, /* line ends with a hard return '\r\n' */
52 END_SOFT /* line ends with a soft return '\r\r\n' */
53 } LINE_END;
55 typedef struct tagLINEDEF {
56 INT length; /* bruto length of a line in bytes */
57 INT net_length; /* netto length of a line in visible characters */
58 LINE_END ending;
59 INT width; /* width of the line in pixels */
60 struct tagLINEDEF *next;
61 } LINEDEF;
63 typedef struct
65 HANDLE heap; /* our own heap */
66 LPSTR text; /* the actual contents of the control */
67 INT buffer_size; /* the size of the buffer */
68 INT buffer_limit; /* the maximum size to which the buffer may grow */
69 HFONT font; /* NULL means standard system font */
70 INT x_offset; /* scroll offset for multi lines this is in pixels
71 for single lines it's in characters */
72 INT line_height; /* height of a screen line in pixels */
73 INT char_width; /* average character width in pixels */
74 DWORD style; /* sane version of wnd->dwStyle */
75 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
76 INT undo_insert_count; /* number of characters inserted in sequence */
77 INT undo_position; /* character index of the insertion and deletion */
78 LPSTR undo_text; /* deleted text */
79 INT undo_buffer_size; /* size of the deleted text buffer */
80 INT selection_start; /* == selection_end if no selection */
81 INT selection_end; /* == current caret position */
82 CHAR password_char; /* == 0 if no password char, and for multi line controls */
83 INT left_margin; /* in pixels */
84 INT right_margin; /* in pixels */
85 RECT format_rect;
86 INT region_posx; /* Position of cursor relative to region: */
87 INT region_posy; /* -1: to left, 0: within, 1: to right */
88 EDITWORDBREAKPROC16 word_break_proc16;
89 EDITWORDBREAKPROCA word_break_proc32A;
90 INT line_count; /* number of lines */
91 INT y_offset; /* scroll offset in number of lines */
93 * only for multi line controls
95 INT lock_count; /* amount of re-entries in the EditWndProc */
96 INT tabs_count;
97 LPINT tabs;
98 INT text_width; /* width of the widest line in pixels */
99 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
100 HLOCAL16 hloc16; /* for controls receiving EM_GETHANDLE16 */
101 HLOCAL hloc32; /* for controls receiving EM_GETHANDLE */
102 } EDITSTATE;
105 #define SWAP_INT32(x,y) do { INT temp = (INT)(x); (x) = (INT)(y); (y) = temp; } while(0)
106 #define ORDER_INT(x,y) do { if ((INT)(y) < (INT)(x)) SWAP_INT32((x),(y)); } while(0)
108 #define SWAP_UINT32(x,y) do { UINT temp = (UINT)(x); (x) = (UINT)(y); (y) = temp; } while(0)
109 #define ORDER_UINT(x,y) do { if ((UINT)(y) < (UINT)(x)) SWAP_UINT32((x),(y)); } while(0)
111 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
112 ({TRACE(edit, "notification " str " sent to hwnd=%08x\n", \
113 (UINT)(hwnd));})
115 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
116 (SendMessageA((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
117 (WPARAM)(hdc), (LPARAM)(wnd)->hwndSelf))
118 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
119 (DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str), \
120 SendMessageA((wnd)->parent->hwndSelf, WM_COMMAND, \
121 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
122 (LPARAM)(wnd)->hwndSelf))
123 #define DPRINTF_EDIT_MSG16(str) \
124 TRACE(edit, \
125 "16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
126 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
127 #define DPRINTF_EDIT_MSG32(str) \
128 TRACE(edit, \
129 "32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
130 (UINT)hwnd, (UINT)wParam, (UINT)lParam)
132 /*********************************************************************
134 * Declarations
139 * These functions have trivial implementations
140 * We still like to call them internally
141 * "static __inline__" makes them more like macro's
143 static __inline__ BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
144 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
145 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
146 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
148 * This is the only exported function
150 LRESULT WINAPI EditWndProc( HWND hwnd, UINT msg,
151 WPARAM wParam, LPARAM lParam );
153 * Helper functions only valid for one type of control
155 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
156 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
157 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
158 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend);
159 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
160 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend);
162 * Helper functions valid for both single line _and_ multi line controls
164 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action);
165 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap);
166 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y);
167 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc);
168 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end);
169 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
170 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size);
171 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size);
172 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend);
173 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend);
174 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend);
175 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend);
176 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend);
177 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend);
178 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC hdc, INT line, BOOL rev);
179 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC hdc, INT x, INT y, INT line, INT col, INT count, BOOL rev);
180 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos, BOOL after_wrap);
181 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT lprc);
182 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force);
183 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action);
185 * EM_XXX message handlers
187 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y);
188 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol);
189 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
190 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
191 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch);
192 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT start, LPUINT end);
193 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
194 static INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index);
195 static INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line);
196 static INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index);
197 static BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy);
198 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT index, BOOL after_wrap);
199 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL can_undo, LPCSTR lpsz_replace);
200 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action);
201 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
202 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL hloc);
203 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
204 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT limit);
205 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT action, INT left, INT right);
206 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
207 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT start, UINT end, BOOL after_wrap);
208 static BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT tabs);
209 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT count, LPINT16 tabs);
210 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROCA wbp);
211 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
212 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
214 * WM_XXX message handlers
216 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
217 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT code, INT id, HWND conrtol);
218 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND hwnd, INT x, INT y);
219 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
220 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs);
221 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
222 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc);
223 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text);
224 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
225 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
226 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus);
227 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
228 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
229 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
230 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y);
231 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs);
232 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es);
233 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
234 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus);
235 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw);
236 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
237 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height);
238 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data);
239 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc);
240 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar);
243 /*********************************************************************
245 * EM_CANUNDO
248 static __inline__ BOOL EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
250 return (es->undo_insert_count || lstrlenA(es->undo_text));
254 /*********************************************************************
256 * EM_EMPTYUNDOBUFFER
259 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
261 es->undo_insert_count = 0;
262 *es->undo_text = '\0';
266 /*********************************************************************
268 * WM_CLEAR
271 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
273 EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
277 /*********************************************************************
279 * WM_CUT
282 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
284 EDIT_WM_Copy(wnd, es);
285 EDIT_WM_Clear(wnd, es);
289 /*********************************************************************
291 * EditWndProc()
293 * The messages are in the order of the actual integer values
294 * (which can be found in include/windows.h)
295 * Whereever possible the 16 bit versions are converted to
296 * the 32 bit ones, so that we can 'fall through' to the
297 * helper functions. These are mostly 32 bit (with a few
298 * exceptions, clearly indicated by a '16' extension to their
299 * names).
302 LRESULT WINAPI EditWndProc( HWND hwnd, UINT msg,
303 WPARAM wParam, LPARAM lParam )
305 WND *wnd = WIN_FindWndPtr(hwnd);
306 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
307 LRESULT result = 0;
309 switch (msg) {
310 case WM_DESTROY:
311 DPRINTF_EDIT_MSG32("WM_DESTROY");
312 EDIT_WM_Destroy(wnd, es);
313 return 0;
315 case WM_NCCREATE:
316 DPRINTF_EDIT_MSG32("WM_NCCREATE");
317 return EDIT_WM_NCCreate(wnd, (LPCREATESTRUCTA)lParam);
320 if (!es)
321 return DefWindowProcA(hwnd, msg, wParam, lParam);
323 EDIT_LockBuffer(wnd, es);
324 switch (msg) {
325 case EM_GETSEL16:
326 DPRINTF_EDIT_MSG16("EM_GETSEL");
327 wParam = 0;
328 lParam = 0;
329 /* fall through */
330 case EM_GETSEL:
331 DPRINTF_EDIT_MSG32("EM_GETSEL");
332 result = EDIT_EM_GetSel(wnd, es, (LPUINT)wParam, (LPUINT)lParam);
333 break;
335 case EM_SETSEL16:
336 DPRINTF_EDIT_MSG16("EM_SETSEL");
337 if (SLOWORD(lParam) == -1)
338 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
339 else
340 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
341 if (!wParam)
342 EDIT_EM_ScrollCaret(wnd, es);
343 result = 1;
344 break;
345 case EM_SETSEL:
346 DPRINTF_EDIT_MSG32("EM_SETSEL");
347 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
348 result = 1;
349 break;
351 case EM_GETRECT16:
352 DPRINTF_EDIT_MSG16("EM_GETRECT");
353 if (lParam)
354 CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
355 break;
356 case EM_GETRECT:
357 DPRINTF_EDIT_MSG32("EM_GETRECT");
358 if (lParam)
359 CopyRect((LPRECT)lParam, &es->format_rect);
360 break;
362 case EM_SETRECT16:
363 DPRINTF_EDIT_MSG16("EM_SETRECT");
364 if ((es->style & ES_MULTILINE) && lParam) {
365 RECT rc;
366 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
367 EDIT_SetRectNP(wnd, es, &rc);
368 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
370 break;
371 case EM_SETRECT:
372 DPRINTF_EDIT_MSG32("EM_SETRECT");
373 if ((es->style & ES_MULTILINE) && lParam) {
374 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
375 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
377 break;
379 case EM_SETRECTNP16:
380 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
381 if ((es->style & ES_MULTILINE) && lParam) {
382 RECT rc;
383 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
384 EDIT_SetRectNP(wnd, es, &rc);
386 break;
387 case EM_SETRECTNP:
388 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
389 if ((es->style & ES_MULTILINE) && lParam)
390 EDIT_SetRectNP(wnd, es, (LPRECT)lParam);
391 break;
393 case EM_SCROLL16:
394 DPRINTF_EDIT_MSG16("EM_SCROLL");
395 /* fall through */
396 case EM_SCROLL:
397 DPRINTF_EDIT_MSG32("EM_SCROLL");
398 result = EDIT_EM_Scroll(wnd, es, (INT)wParam);
399 break;
401 case EM_LINESCROLL16:
402 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
403 wParam = (WPARAM)(INT)SHIWORD(lParam);
404 lParam = (LPARAM)(INT)SLOWORD(lParam);
405 /* fall through */
406 case EM_LINESCROLL:
407 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
408 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT)wParam, (INT)lParam);
409 break;
411 case EM_SCROLLCARET16:
412 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
413 /* fall through */
414 case EM_SCROLLCARET:
415 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
416 EDIT_EM_ScrollCaret(wnd, es);
417 result = 1;
418 break;
420 case EM_GETMODIFY16:
421 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
422 /* fall through */
423 case EM_GETMODIFY:
424 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
425 return ((es->flags & EF_MODIFIED) != 0);
426 break;
428 case EM_SETMODIFY16:
429 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
430 /* fall through */
431 case EM_SETMODIFY:
432 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
433 if (wParam)
434 es->flags |= EF_MODIFIED;
435 else
436 es->flags &= ~EF_MODIFIED;
437 break;
439 case EM_GETLINECOUNT16:
440 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
441 /* fall through */
442 case EM_GETLINECOUNT:
443 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
444 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
445 break;
447 case EM_LINEINDEX16:
448 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
449 if ((INT16)wParam == -1)
450 wParam = (WPARAM)-1;
451 /* fall through */
452 case EM_LINEINDEX:
453 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
454 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT)wParam);
455 break;
457 case EM_SETHANDLE16:
458 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
459 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
460 break;
461 case EM_SETHANDLE:
462 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
463 EDIT_EM_SetHandle(wnd, es, (HLOCAL)wParam);
464 break;
466 case EM_GETHANDLE16:
467 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
468 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
469 break;
470 case EM_GETHANDLE:
471 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
472 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
473 break;
475 case EM_GETTHUMB16:
476 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
477 /* fall through */
478 case EM_GETTHUMB:
479 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
480 result = EDIT_EM_GetThumb(wnd, es);
481 break;
483 /* messages 0x00bf and 0x00c0 missing from specs */
485 case WM_USER+15:
486 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
487 /* fall through */
488 case 0x00bf:
489 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
490 result = DefWindowProcA(hwnd, msg, wParam, lParam);
491 break;
493 case WM_USER+16:
494 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
495 /* fall through */
496 case 0x00c0:
497 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
498 result = DefWindowProcA(hwnd, msg, wParam, lParam);
499 break;
501 case EM_LINELENGTH16:
502 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
503 /* fall through */
504 case EM_LINELENGTH:
505 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
506 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT)wParam);
507 break;
509 case EM_REPLACESEL16:
510 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
511 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
512 /* fall through */
513 case EM_REPLACESEL:
514 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
515 EDIT_EM_ReplaceSel(wnd, es, (BOOL)wParam, (LPCSTR)lParam);
516 result = 1;
517 break;
519 /* message 0x00c3 missing from specs */
521 case WM_USER+19:
522 DPRINTF_EDIT_MSG16("undocumented WM_USER+19, please report");
523 /* fall through */
524 case 0x00c3:
525 DPRINTF_EDIT_MSG32("undocumented 0x00c3, please report");
526 result = DefWindowProcA(hwnd, msg, wParam, lParam);
527 break;
529 case EM_GETLINE16:
530 DPRINTF_EDIT_MSG16("EM_GETLINE");
531 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
532 /* fall through */
533 case EM_GETLINE:
534 DPRINTF_EDIT_MSG32("EM_GETLINE");
535 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT)wParam, (LPSTR)lParam);
536 break;
538 case EM_LIMITTEXT16:
539 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
540 /* fall through */
541 case EM_SETLIMITTEXT:
542 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
543 EDIT_EM_SetLimitText(wnd, es, (INT)wParam);
544 break;
546 case EM_CANUNDO16:
547 DPRINTF_EDIT_MSG16("EM_CANUNDO");
548 /* fall through */
549 case EM_CANUNDO:
550 DPRINTF_EDIT_MSG32("EM_CANUNDO");
551 result = (LRESULT)EDIT_EM_CanUndo(wnd, es);
552 break;
554 case EM_UNDO16:
555 DPRINTF_EDIT_MSG16("EM_UNDO");
556 /* fall through */
557 case EM_UNDO:
558 /* fall through */
559 case WM_UNDO:
560 DPRINTF_EDIT_MSG32("EM_UNDO / WM_UNDO");
561 result = (LRESULT)EDIT_EM_Undo(wnd, es);
562 break;
564 case EM_FMTLINES16:
565 DPRINTF_EDIT_MSG16("EM_FMTLINES");
566 /* fall through */
567 case EM_FMTLINES:
568 DPRINTF_EDIT_MSG32("EM_FMTLINES");
569 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL)wParam);
570 break;
572 case EM_LINEFROMCHAR16:
573 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
574 /* fall through */
575 case EM_LINEFROMCHAR:
576 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
577 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT)wParam);
578 break;
580 /* message 0x00ca missing from specs */
582 case WM_USER+26:
583 DPRINTF_EDIT_MSG16("undocumented WM_USER+26, please report");
584 /* fall through */
585 case 0x00ca:
586 DPRINTF_EDIT_MSG32("undocumented 0x00ca, please report");
587 result = DefWindowProcA(hwnd, msg, wParam, lParam);
588 break;
590 case EM_SETTABSTOPS16:
591 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
592 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
593 break;
594 case EM_SETTABSTOPS:
595 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
596 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT)wParam, (LPINT)lParam);
597 break;
599 case EM_SETPASSWORDCHAR16:
600 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
601 /* fall through */
602 case EM_SETPASSWORDCHAR:
603 DPRINTF_EDIT_MSG32("EM_SETPASSWORDCHAR");
604 EDIT_EM_SetPasswordChar(wnd, es, (CHAR)wParam);
605 break;
607 case EM_EMPTYUNDOBUFFER16:
608 DPRINTF_EDIT_MSG16("EM_EMPTYUNDOBUFFER");
609 /* fall through */
610 case EM_EMPTYUNDOBUFFER:
611 DPRINTF_EDIT_MSG32("EM_EMPTYUNDOBUFFER");
612 EDIT_EM_EmptyUndoBuffer(wnd, es);
613 break;
615 case EM_GETFIRSTVISIBLELINE16:
616 DPRINTF_EDIT_MSG16("EM_GETFIRSTVISIBLELINE");
617 result = es->y_offset;
618 break;
619 case EM_GETFIRSTVISIBLELINE:
620 DPRINTF_EDIT_MSG32("EM_GETFIRSTVISIBLELINE");
621 result = (es->style & ES_MULTILINE) ? es->y_offset : es->x_offset;
622 break;
624 case EM_SETREADONLY16:
625 DPRINTF_EDIT_MSG16("EM_SETREADONLY");
626 /* fall through */
627 case EM_SETREADONLY:
628 DPRINTF_EDIT_MSG32("EM_SETREADONLY");
629 if (wParam) {
630 wnd->dwStyle |= ES_READONLY;
631 es->style |= ES_READONLY;
632 } else {
633 wnd->dwStyle &= ~ES_READONLY;
634 es->style &= ~ES_READONLY;
636 return 1;
637 break;
639 case EM_SETWORDBREAKPROC16:
640 DPRINTF_EDIT_MSG16("EM_SETWORDBREAKPROC");
641 EDIT_EM_SetWordBreakProc16(wnd, es, (EDITWORDBREAKPROC16)lParam);
642 break;
643 case EM_SETWORDBREAKPROC:
644 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
645 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROCA)lParam);
646 break;
648 case EM_GETWORDBREAKPROC16:
649 DPRINTF_EDIT_MSG16("EM_GETWORDBREAKPROC");
650 result = (LRESULT)es->word_break_proc16;
651 break;
652 case EM_GETWORDBREAKPROC:
653 DPRINTF_EDIT_MSG32("EM_GETWORDBREAKPROC");
654 result = (LRESULT)es->word_break_proc32A;
655 break;
657 case EM_GETPASSWORDCHAR16:
658 DPRINTF_EDIT_MSG16("EM_GETPASSWORDCHAR");
659 /* fall through */
660 case EM_GETPASSWORDCHAR:
661 DPRINTF_EDIT_MSG32("EM_GETPASSWORDCHAR");
662 result = es->password_char;
663 break;
665 /* The following EM_xxx are new to win95 and don't exist for 16 bit */
667 case EM_SETMARGINS:
668 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
669 EDIT_EM_SetMargins(wnd, es, (INT)wParam, SLOWORD(lParam), SHIWORD(lParam));
670 break;
672 case EM_GETMARGINS:
673 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
674 result = MAKELONG(es->left_margin, es->right_margin);
675 break;
677 case EM_GETLIMITTEXT:
678 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
679 result = es->buffer_limit;
680 break;
682 case EM_POSFROMCHAR:
683 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
684 result = EDIT_EM_PosFromChar(wnd, es, (INT)wParam, FALSE);
685 break;
687 case EM_CHARFROMPOS:
688 DPRINTF_EDIT_MSG32("EM_CHARFROMPOS");
689 result = EDIT_EM_CharFromPos(wnd, es, SLOWORD(lParam), SHIWORD(lParam));
690 break;
692 case WM_GETDLGCODE:
693 DPRINTF_EDIT_MSG32("WM_GETDLGCODE");
694 result = (es->style & ES_MULTILINE) ?
695 DLGC_WANTALLKEYS | DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS :
696 DLGC_HASSETSEL | DLGC_WANTCHARS | DLGC_WANTARROWS;
697 break;
699 case WM_CHAR:
700 DPRINTF_EDIT_MSG32("WM_CHAR");
701 EDIT_WM_Char(wnd, es, (CHAR)wParam, (DWORD)lParam);
702 break;
704 case WM_CLEAR:
705 DPRINTF_EDIT_MSG32("WM_CLEAR");
706 EDIT_WM_Clear(wnd, es);
707 break;
709 case WM_COMMAND:
710 DPRINTF_EDIT_MSG32("WM_COMMAND");
711 EDIT_WM_Command(wnd, es, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
712 break;
714 case WM_CONTEXTMENU:
715 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
716 EDIT_WM_ContextMenu(wnd, es, (HWND)wParam, SLOWORD(lParam), SHIWORD(lParam));
717 break;
719 case WM_COPY:
720 DPRINTF_EDIT_MSG32("WM_COPY");
721 EDIT_WM_Copy(wnd, es);
722 break;
724 case WM_CREATE:
725 DPRINTF_EDIT_MSG32("WM_CREATE");
726 result = EDIT_WM_Create(wnd, es, (LPCREATESTRUCTA)lParam);
727 break;
729 case WM_CUT:
730 DPRINTF_EDIT_MSG32("WM_CUT");
731 EDIT_WM_Cut(wnd, es);
732 break;
734 case WM_ENABLE:
735 DPRINTF_EDIT_MSG32("WM_ENABLE");
736 InvalidateRect(hwnd, NULL, TRUE);
737 break;
739 case WM_ERASEBKGND:
740 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
741 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC)wParam);
742 break;
744 case WM_GETFONT:
745 DPRINTF_EDIT_MSG32("WM_GETFONT");
746 result = (LRESULT)es->font;
747 break;
749 case WM_GETTEXT:
750 DPRINTF_EDIT_MSG32("WM_GETTEXT");
751 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT)wParam, (LPSTR)lParam);
752 break;
754 case WM_GETTEXTLENGTH:
755 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
756 result = lstrlenA(es->text);
757 break;
759 case WM_HSCROLL:
760 DPRINTF_EDIT_MSG32("WM_HSCROLL");
761 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)lParam);
762 break;
764 case WM_KEYDOWN:
765 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
766 result = EDIT_WM_KeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
767 break;
769 case WM_KILLFOCUS:
770 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
771 result = EDIT_WM_KillFocus(wnd, es, (HWND)wParam);
772 break;
774 case WM_LBUTTONDBLCLK:
775 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
776 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
777 break;
779 case WM_LBUTTONDOWN:
780 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
781 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
782 break;
784 case WM_LBUTTONUP:
785 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
786 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
787 break;
789 case WM_MOUSEACTIVATE:
791 * FIXME: maybe DefWindowProc() screws up, but it seems that
792 * modalless dialog boxes need this. If we don't do this, the focus
793 * will _not_ be set by DefWindowProc() for edit controls in a
794 * modalless dialog box ???
796 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
797 SetFocus(wnd->hwndSelf);
798 result = MA_ACTIVATE;
799 break;
801 case WM_MOUSEMOVE:
803 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
805 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
806 break;
808 case WM_PAINT:
809 DPRINTF_EDIT_MSG32("WM_PAINT");
810 EDIT_WM_Paint(wnd, es);
811 break;
813 case WM_PASTE:
814 DPRINTF_EDIT_MSG32("WM_PASTE");
815 EDIT_WM_Paste(wnd, es);
816 break;
818 case WM_SETFOCUS:
819 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
820 EDIT_WM_SetFocus(wnd, es, (HWND)wParam);
821 break;
823 case WM_SETFONT:
824 DPRINTF_EDIT_MSG32("WM_SETFONT");
825 EDIT_WM_SetFont(wnd, es, (HFONT)wParam, LOWORD(lParam) != 0);
826 break;
828 case WM_SETTEXT:
829 DPRINTF_EDIT_MSG32("WM_SETTEXT");
830 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
831 result = TRUE;
832 break;
834 case WM_SIZE:
835 DPRINTF_EDIT_MSG32("WM_SIZE");
836 EDIT_WM_Size(wnd, es, (UINT)wParam, LOWORD(lParam), HIWORD(lParam));
837 break;
839 case WM_SYSKEYDOWN:
840 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
841 result = EDIT_WM_SysKeyDown(wnd, es, (INT)wParam, (DWORD)lParam);
842 break;
844 case WM_TIMER:
845 DPRINTF_EDIT_MSG32("WM_TIMER");
846 EDIT_WM_Timer(wnd, es, (INT)wParam, (TIMERPROC)lParam);
847 break;
849 case WM_VSCROLL:
850 DPRINTF_EDIT_MSG32("WM_VSCROLL");
851 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND)(lParam));
852 break;
854 default:
855 result = DefWindowProcA(hwnd, msg, wParam, lParam);
856 break;
858 EDIT_UnlockBuffer(wnd, es, FALSE);
859 return result;
863 /*********************************************************************
865 * EDIT_BuildLineDefs_ML
867 * Build linked list of text lines.
868 * Lines can end with '\0' (last line), a character (if it is wrapped),
869 * a soft return '\r\r\n' or a hard return '\r\n'
872 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
874 HDC dc;
875 HFONT old_font = 0;
876 LPSTR start, cp;
877 INT fw;
878 LINEDEF *current_def;
879 LINEDEF **previous_next;
881 current_def = es->first_line_def;
882 do {
883 LINEDEF *next_def = current_def->next;
884 HeapFree(es->heap, 0, current_def);
885 current_def = next_def;
886 } while (current_def);
887 es->line_count = 0;
888 es->text_width = 0;
890 dc = GetDC(wnd->hwndSelf);
891 if (es->font)
892 old_font = SelectObject(dc, es->font);
894 fw = es->format_rect.right - es->format_rect.left;
895 start = es->text;
896 previous_next = &es->first_line_def;
897 do {
898 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
899 current_def->next = NULL;
900 cp = start;
901 while (*cp) {
902 if ((*cp == '\r') && (*(cp + 1) == '\n'))
903 break;
904 cp++;
906 if (!(*cp)) {
907 current_def->ending = END_0;
908 current_def->net_length = lstrlenA(start);
909 } else if ((cp > start) && (*(cp - 1) == '\r')) {
910 current_def->ending = END_SOFT;
911 current_def->net_length = cp - start - 1;
912 } else {
913 current_def->ending = END_HARD;
914 current_def->net_length = cp - start;
916 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
917 start, current_def->net_length,
918 es->tabs_count, es->tabs));
919 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
920 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
921 INT next = 0;
922 INT prev;
923 do {
924 prev = next;
925 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
926 prev + 1, current_def->net_length, WB_RIGHT);
927 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
928 start, next, es->tabs_count, es->tabs));
929 } while (current_def->width <= fw);
930 if (!prev) {
931 next = 0;
932 do {
933 prev = next;
934 next++;
935 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc,
936 start, next, es->tabs_count, es->tabs));
937 } while (current_def->width <= fw);
938 if (!prev)
939 prev = 1;
941 current_def->net_length = prev;
942 current_def->ending = END_WRAP;
943 current_def->width = (INT)LOWORD(GetTabbedTextExtentA(dc, start,
944 current_def->net_length, es->tabs_count, es->tabs));
946 switch (current_def->ending) {
947 case END_SOFT:
948 current_def->length = current_def->net_length + 3;
949 break;
950 case END_HARD:
951 current_def->length = current_def->net_length + 2;
952 break;
953 case END_WRAP:
954 case END_0:
955 current_def->length = current_def->net_length;
956 break;
958 es->text_width = MAX(es->text_width, current_def->width);
959 start += current_def->length;
960 *previous_next = current_def;
961 previous_next = &current_def->next;
962 es->line_count++;
963 } while (current_def->ending != END_0);
964 if (es->font)
965 SelectObject(dc, old_font);
966 ReleaseDC(wnd->hwndSelf, dc);
970 /*********************************************************************
972 * EDIT_CallWordBreakProc
974 * Call appropriate WordBreakProc (internal or external).
976 * Note: The "start" argument should always be an index refering
977 * to es->text. The actual wordbreak proc might be
978 * 16 bit, so we can't always pass any 32 bit LPSTR.
979 * Hence we assume that es->text is the buffer that holds
980 * the string under examination (we can decide this for ourselves).
983 static INT EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT start, INT index, INT count, INT action)
985 if (es->word_break_proc16) {
986 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
987 SEGPTR segptr = LocalLock16(hloc16);
988 INT ret = (INT)Callbacks->CallWordBreakProc(es->word_break_proc16,
989 segptr + start, index, count, action);
990 LocalUnlock16(hloc16);
991 return ret;
993 else if (es->word_break_proc32A)
995 TRACE(relay, "(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
996 es->word_break_proc32A, es->text + start, index,
997 count, action );
998 return (INT)es->word_break_proc32A( es->text + start, index,
999 count, action );
1001 else
1002 return EDIT_WordBreakProc(es->text + start, index, count, action);
1006 /*********************************************************************
1008 * EDIT_CharFromPos
1010 * Beware: This is not the function called on EM_CHARFROMPOS
1011 * The position _can_ be outside the formatting / client
1012 * rectangle
1013 * The return value is only the character index
1016 static INT EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
1018 INT index;
1019 HDC dc;
1020 HFONT old_font = 0;
1022 if (es->style & ES_MULTILINE) {
1023 INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1024 INT line_index = 0;
1025 LINEDEF *line_def = es->first_line_def;
1026 INT low, high;
1027 while ((line > 0) && line_def->next) {
1028 line_index += line_def->length;
1029 line_def = line_def->next;
1030 line--;
1032 x += es->x_offset - es->format_rect.left;
1033 if (x >= line_def->width) {
1034 if (after_wrap)
1035 *after_wrap = (line_def->ending == END_WRAP);
1036 return line_index + line_def->net_length;
1038 if (x <= 0) {
1039 if (after_wrap)
1040 *after_wrap = FALSE;
1041 return line_index;
1043 dc = GetDC(wnd->hwndSelf);
1044 if (es->font)
1045 old_font = SelectObject(dc, es->font);
1046 low = line_index + 1;
1047 high = line_index + line_def->net_length + 1;
1048 while (low < high - 1)
1050 INT mid = (low + high) / 2;
1051 if (LOWORD(GetTabbedTextExtentA(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1052 else low = mid;
1054 index = low;
1056 if (after_wrap)
1057 *after_wrap = ((index == line_index + line_def->net_length) &&
1058 (line_def->ending == END_WRAP));
1059 } else {
1060 LPSTR text;
1061 SIZE size;
1062 if (after_wrap)
1063 *after_wrap = FALSE;
1064 x -= es->format_rect.left;
1065 if (!x)
1066 return es->x_offset;
1067 text = EDIT_GetPasswordPointer_SL(wnd, es);
1068 dc = GetDC(wnd->hwndSelf);
1069 if (es->font)
1070 old_font = SelectObject(dc, es->font);
1071 if (x < 0)
1073 INT low = 0;
1074 INT high = es->x_offset;
1075 while (low < high - 1)
1077 INT mid = (low + high) / 2;
1078 GetTextExtentPoint32A( dc, text + mid,
1079 es->x_offset - mid, &size );
1080 if (size.cx > -x) low = mid;
1081 else high = mid;
1083 index = low;
1085 else
1087 INT low = es->x_offset;
1088 INT high = lstrlenA(es->text) + 1;
1089 while (low < high - 1)
1091 INT mid = (low + high) / 2;
1092 GetTextExtentPoint32A( dc, text + es->x_offset,
1093 mid - es->x_offset, &size );
1094 if (size.cx > x) high = mid;
1095 else low = mid;
1097 index = low;
1099 if (es->style & ES_PASSWORD)
1100 HeapFree(es->heap, 0 ,text);
1102 if (es->font)
1103 SelectObject(dc, old_font);
1104 ReleaseDC(wnd->hwndSelf, dc);
1105 return index;
1109 /*********************************************************************
1111 * EDIT_ConfinePoint
1113 * adjusts the point to be within the formatting rectangle
1114 * (so CharFromPos returns the nearest _visible_ character)
1117 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT x, LPINT y)
1119 *x = MIN(MAX(*x, es->format_rect.left), es->format_rect.right - 1);
1120 *y = MIN(MAX(*y, es->format_rect.top), es->format_rect.bottom - 1);
1124 /*********************************************************************
1126 * EDIT_GetLineRect
1128 * Calculates the bounding rectangle for a line from a starting
1129 * column to an ending column.
1132 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT line, INT scol, INT ecol, LPRECT rc)
1134 INT line_index = EDIT_EM_LineIndex(wnd, es, line);
1136 if (es->style & ES_MULTILINE)
1137 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1138 else
1139 rc->top = es->format_rect.top;
1140 rc->bottom = rc->top + es->line_height;
1141 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1142 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1146 /*********************************************************************
1148 * EDIT_GetPasswordPointer_SL
1150 * note: caller should free the (optionally) allocated buffer
1153 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1155 if (es->style & ES_PASSWORD) {
1156 INT len = lstrlenA(es->text);
1157 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1158 RtlFillMemory(text, len, es->password_char);
1159 text[len] = '\0';
1160 return text;
1161 } else
1162 return es->text;
1166 /*********************************************************************
1168 * EDIT_LockBuffer
1170 * This acts as a LOCAL_Lock(), but it locks only once. This way
1171 * you can call it whenever you like, without unlocking.
1174 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1176 if (!es) {
1177 ERR(edit, "no EDITSTATE ... please report\n");
1178 return;
1180 if (!(es->style & ES_MULTILINE))
1181 return;
1182 if (!es->text) {
1183 if (es->hloc32)
1184 es->text = LocalLock(es->hloc32);
1185 else if (es->hloc16)
1186 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1187 else {
1188 ERR(edit, "no buffer ... please report\n");
1189 return;
1192 es->lock_count++;
1196 /*********************************************************************
1198 * EDIT_SL_InvalidateText
1200 * Called from EDIT_InvalidateText().
1201 * Does the job for single-line controls only.
1204 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1206 RECT line_rect;
1207 RECT rc;
1209 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1210 if (IntersectRect(&rc, &line_rect, &es->format_rect))
1211 InvalidateRect(wnd->hwndSelf, &rc, FALSE);
1215 /*********************************************************************
1217 * EDIT_ML_InvalidateText
1219 * Called from EDIT_InvalidateText().
1220 * Does the job for multi-line controls only.
1223 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1225 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1226 INT sl = EDIT_EM_LineFromChar(wnd, es, start);
1227 INT el = EDIT_EM_LineFromChar(wnd, es, end);
1228 INT sc;
1229 INT ec;
1230 RECT rc1;
1231 RECT rcWnd;
1232 RECT rcLine;
1233 RECT rcUpdate;
1234 INT l;
1236 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1237 return;
1239 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1240 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1241 if (sl < es->y_offset) {
1242 sl = es->y_offset;
1243 sc = 0;
1245 if (el > es->y_offset + vlc) {
1246 el = es->y_offset + vlc;
1247 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1249 GetClientRect(wnd->hwndSelf, &rc1);
1250 IntersectRect(&rcWnd, &rc1, &es->format_rect);
1251 if (sl == el) {
1252 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1253 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1254 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1255 } else {
1256 EDIT_GetLineRect(wnd, es, sl, sc,
1257 EDIT_EM_LineLength(wnd, es,
1258 EDIT_EM_LineIndex(wnd, es, sl)),
1259 &rcLine);
1260 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1261 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1262 for (l = sl + 1 ; l < el ; l++) {
1263 EDIT_GetLineRect(wnd, es, l, 0,
1264 EDIT_EM_LineLength(wnd, es,
1265 EDIT_EM_LineIndex(wnd, es, l)),
1266 &rcLine);
1267 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1268 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1270 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1271 if (IntersectRect(&rcUpdate, &rcWnd, &rcLine))
1272 InvalidateRect(wnd->hwndSelf, &rcUpdate, FALSE);
1277 /*********************************************************************
1279 * EDIT_InvalidateText
1281 * Invalidate the text from offset start upto, but not including,
1282 * offset end. Useful for (re)painting the selection.
1283 * Regions outside the linewidth are not invalidated.
1284 * end == -1 means end == TextLength.
1285 * start and end need not be ordered.
1288 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT start, INT end)
1290 if (end == start)
1291 return;
1293 if (end == -1)
1294 end = lstrlenA(es->text);
1296 ORDER_INT(start, end);
1298 if (es->style & ES_MULTILINE)
1299 EDIT_ML_InvalidateText(wnd, es, start, end);
1300 else
1301 EDIT_SL_InvalidateText(wnd, es, start, end);
1305 /*********************************************************************
1307 * EDIT_MakeFit
1309 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1312 static BOOL EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT size)
1314 HLOCAL hNew32;
1315 HLOCAL16 hNew16;
1317 if (size <= es->buffer_size)
1318 return TRUE;
1319 if (size > es->buffer_limit) {
1320 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1321 return FALSE;
1323 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1324 if (size > es->buffer_limit)
1325 size = es->buffer_limit;
1327 TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1329 EDIT_UnlockBuffer(wnd, es, TRUE);
1330 if (es->text) {
1331 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1332 es->buffer_size = MIN(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1333 else
1334 es->buffer_size = 0;
1335 } else if (es->hloc32) {
1336 if ((hNew32 = LocalReAlloc(es->hloc32, size + 1, 0))) {
1337 TRACE(edit, "Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1338 es->hloc32 = hNew32;
1339 es->buffer_size = MIN(LocalSize(es->hloc32) - 1, es->buffer_limit);
1341 } else if (es->hloc16) {
1342 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1343 TRACE(edit, "Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1344 es->hloc16 = hNew16;
1345 es->buffer_size = MIN(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1348 if (es->buffer_size < size) {
1349 EDIT_LockBuffer(wnd, es);
1350 WARN(edit, "FAILED ! We now have %d+1\n", es->buffer_size);
1351 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1352 return FALSE;
1353 } else {
1354 EDIT_LockBuffer(wnd, es);
1355 TRACE(edit, "We now have %d+1\n", es->buffer_size);
1356 return TRUE;
1361 /*********************************************************************
1363 * EDIT_MakeUndoFit
1365 * Try to fit size + 1 bytes in the undo buffer.
1368 static BOOL EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT size)
1370 if (size <= es->undo_buffer_size)
1371 return TRUE;
1372 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1374 TRACE(edit, "trying to ReAlloc to %d+1\n", size);
1376 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1377 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1378 if (es->undo_buffer_size < size) {
1379 WARN(edit, "FAILED ! We now have %d+1\n", es->undo_buffer_size);
1380 return FALSE;
1382 return TRUE;
1384 return FALSE;
1388 /*********************************************************************
1390 * EDIT_MoveBackward
1393 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1395 INT e = es->selection_end;
1397 if (e) {
1398 e--;
1399 if ((es->style & ES_MULTILINE) && e &&
1400 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1401 e--;
1402 if (e && (es->text[e - 1] == '\r'))
1403 e--;
1406 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1407 EDIT_EM_ScrollCaret(wnd, es);
1411 /*********************************************************************
1413 * EDIT_MoveDown_ML
1415 * Only for multi line controls
1416 * Move the caret one line down, on a column with the nearest
1417 * x coordinate on the screen (might be a different column).
1420 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1422 INT s = es->selection_start;
1423 INT e = es->selection_end;
1424 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1425 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1426 INT x = SLOWORD(pos);
1427 INT y = SHIWORD(pos);
1429 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1430 if (!extend)
1431 s = e;
1432 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1433 EDIT_EM_ScrollCaret(wnd, es);
1437 /*********************************************************************
1439 * EDIT_MoveEnd
1442 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL extend)
1444 BOOL after_wrap = FALSE;
1445 INT e;
1447 if (es->style & ES_MULTILINE)
1448 e = EDIT_CharFromPos(wnd, es, 0x7fffffff,
1449 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1450 else
1451 e = lstrlenA(es->text);
1452 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1453 EDIT_EM_ScrollCaret(wnd, es);
1457 /*********************************************************************
1459 * EDIT_MoveForward
1462 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL extend)
1464 INT e = es->selection_end;
1466 if (es->text[e]) {
1467 e++;
1468 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1469 if (es->text[e] == '\n')
1470 e++;
1471 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1472 e += 2;
1475 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1476 EDIT_EM_ScrollCaret(wnd, es);
1480 /*********************************************************************
1482 * EDIT_MoveHome
1484 * Home key: move to beginning of line.
1487 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL extend)
1489 INT e;
1491 if (es->style & ES_MULTILINE)
1492 e = EDIT_CharFromPos(wnd, es, 0x80000000,
1493 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1494 else
1495 e = 0;
1496 EDIT_EM_SetSel(wnd, es, e, extend ? es->selection_start : e, FALSE);
1497 EDIT_EM_ScrollCaret(wnd, es);
1501 /*********************************************************************
1503 * EDIT_MovePageDown_ML
1505 * Only for multi line controls
1506 * Move the caret one page down, on a column with the nearest
1507 * x coordinate on the screen (might be a different column).
1510 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1512 INT s = es->selection_start;
1513 INT e = es->selection_end;
1514 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1515 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1516 INT x = SLOWORD(pos);
1517 INT y = SHIWORD(pos);
1519 e = EDIT_CharFromPos(wnd, es, x,
1520 y + (es->format_rect.bottom - es->format_rect.top),
1521 &after_wrap);
1522 if (!extend)
1523 s = e;
1524 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1525 EDIT_EM_ScrollCaret(wnd, es);
1529 /*********************************************************************
1531 * EDIT_MovePageUp_ML
1533 * Only for multi line controls
1534 * Move the caret one page up, on a column with the nearest
1535 * x coordinate on the screen (might be a different column).
1538 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1540 INT s = es->selection_start;
1541 INT e = es->selection_end;
1542 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1543 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1544 INT x = SLOWORD(pos);
1545 INT y = SHIWORD(pos);
1547 e = EDIT_CharFromPos(wnd, es, x,
1548 y - (es->format_rect.bottom - es->format_rect.top),
1549 &after_wrap);
1550 if (!extend)
1551 s = e;
1552 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1553 EDIT_EM_ScrollCaret(wnd, es);
1557 /*********************************************************************
1559 * EDIT_MoveUp_ML
1561 * Only for multi line controls
1562 * Move the caret one line up, on a column with the nearest
1563 * x coordinate on the screen (might be a different column).
1566 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL extend)
1568 INT s = es->selection_start;
1569 INT e = es->selection_end;
1570 BOOL after_wrap = (es->flags & EF_AFTER_WRAP);
1571 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1572 INT x = SLOWORD(pos);
1573 INT y = SHIWORD(pos);
1575 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1576 if (!extend)
1577 s = e;
1578 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1579 EDIT_EM_ScrollCaret(wnd, es);
1583 /*********************************************************************
1585 * EDIT_MoveWordBackward
1588 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL extend)
1590 INT s = es->selection_start;
1591 INT e = es->selection_end;
1592 INT l;
1593 INT ll;
1594 INT li;
1596 l = EDIT_EM_LineFromChar(wnd, es, e);
1597 ll = EDIT_EM_LineLength(wnd, es, e);
1598 li = EDIT_EM_LineIndex(wnd, es, l);
1599 if (e - li == 0) {
1600 if (l) {
1601 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1602 e = li + EDIT_EM_LineLength(wnd, es, li);
1604 } else {
1605 e = li + (INT)EDIT_CallWordBreakProc(wnd, es,
1606 li, e - li, ll, WB_LEFT);
1608 if (!extend)
1609 s = e;
1610 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1611 EDIT_EM_ScrollCaret(wnd, es);
1615 /*********************************************************************
1617 * EDIT_MoveWordForward
1620 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL extend)
1622 INT s = es->selection_start;
1623 INT e = es->selection_end;
1624 INT l;
1625 INT ll;
1626 INT li;
1628 l = EDIT_EM_LineFromChar(wnd, es, e);
1629 ll = EDIT_EM_LineLength(wnd, es, e);
1630 li = EDIT_EM_LineIndex(wnd, es, l);
1631 if (e - li == ll) {
1632 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1633 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1634 } else {
1635 e = li + EDIT_CallWordBreakProc(wnd, es,
1636 li, e - li + 1, ll, WB_RIGHT);
1638 if (!extend)
1639 s = e;
1640 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1641 EDIT_EM_ScrollCaret(wnd, es);
1645 /*********************************************************************
1647 * EDIT_PaintLine
1650 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC dc, INT line, BOOL rev)
1652 INT s = es->selection_start;
1653 INT e = es->selection_end;
1654 INT li;
1655 INT ll;
1656 INT x;
1657 INT y;
1658 LRESULT pos;
1660 if (es->style & ES_MULTILINE) {
1661 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1662 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1663 return;
1664 } else if (line)
1665 return;
1667 TRACE(edit, "line=%d\n", line);
1669 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1670 x = SLOWORD(pos);
1671 y = SHIWORD(pos);
1672 li = EDIT_EM_LineIndex(wnd, es, line);
1673 ll = EDIT_EM_LineLength(wnd, es, li);
1674 s = es->selection_start;
1675 e = es->selection_end;
1676 ORDER_INT(s, e);
1677 s = MIN(li + ll, MAX(li, s));
1678 e = MIN(li + ll, MAX(li, e));
1679 if (rev && (s != e) &&
1680 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1681 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1682 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1683 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1684 } else
1685 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1689 /*********************************************************************
1691 * EDIT_PaintText
1694 static INT EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC dc, INT x, INT y, INT line, INT col, INT count, BOOL rev)
1696 COLORREF BkColor;
1697 COLORREF TextColor;
1698 INT ret;
1699 INT li;
1700 SIZE size;
1702 if (!count)
1703 return 0;
1704 BkColor = GetBkColor(dc);
1705 TextColor = GetTextColor(dc);
1706 if (rev) {
1707 SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
1708 SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1710 li = EDIT_EM_LineIndex(wnd, es, line);
1711 if (es->style & ES_MULTILINE) {
1712 ret = (INT)LOWORD(TabbedTextOutA(dc, x, y, es->text + li + col, count,
1713 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1714 } else {
1715 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1716 TextOutA(dc, x, y, text + li + col, count);
1717 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1718 ret = size.cx;
1719 if (es->style & ES_PASSWORD)
1720 HeapFree(es->heap, 0, text);
1722 if (rev) {
1723 SetBkColor(dc, BkColor);
1724 SetTextColor(dc, TextColor);
1726 return ret;
1730 /*********************************************************************
1732 * EDIT_SetCaretPos
1735 static void EDIT_SetCaretPos(WND *wnd, EDITSTATE *es, INT pos,
1736 BOOL after_wrap)
1738 LRESULT res = EDIT_EM_PosFromChar(wnd, es, pos, after_wrap);
1739 INT x = SLOWORD(res);
1740 INT y = SHIWORD(res);
1742 if(x < es->format_rect.left)
1743 x = es->format_rect.left;
1744 if(x > es->format_rect.right - 2)
1745 x = es->format_rect.right - 2;
1746 if(y > es->format_rect.bottom)
1747 y = es->format_rect.bottom;
1748 if(y < es->format_rect.top)
1749 y = es->format_rect.top;
1750 SetCaretPos(x, y);
1751 return;
1755 /*********************************************************************
1757 * EDIT_SetRectNP
1759 * note: this is not (exactly) the handler called on EM_SETRECTNP
1760 * it is also used to set the rect of a single line control
1763 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT rc)
1765 CopyRect(&es->format_rect, rc);
1766 if (es->style & WS_BORDER) {
1767 INT bw = GetSystemMetrics(SM_CXBORDER) + 1;
1768 if(TWEAK_WineLook == WIN31_LOOK)
1769 bw += 2;
1770 es->format_rect.left += bw;
1771 es->format_rect.top += bw;
1772 es->format_rect.right -= bw;
1773 es->format_rect.bottom -= bw;
1775 es->format_rect.left += es->left_margin;
1776 es->format_rect.right -= es->right_margin;
1777 es->format_rect.right = MAX(es->format_rect.right, es->format_rect.left + es->char_width);
1778 if (es->style & ES_MULTILINE)
1779 es->format_rect.bottom = es->format_rect.top +
1780 MAX(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1781 else
1782 es->format_rect.bottom = es->format_rect.top + es->line_height;
1783 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1784 EDIT_BuildLineDefs_ML(wnd, es);
1788 /*********************************************************************
1790 * EDIT_UnlockBuffer
1793 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL force)
1795 if (!es) {
1796 ERR(edit, "no EDITSTATE ... please report\n");
1797 return;
1799 if (!(es->style & ES_MULTILINE))
1800 return;
1801 if (!es->lock_count) {
1802 ERR(edit, "lock_count == 0 ... please report\n");
1803 return;
1805 if (!es->text) {
1806 ERR(edit, "es->text == 0 ... please report\n");
1807 return;
1809 if (force || (es->lock_count == 1)) {
1810 if (es->hloc32) {
1811 LocalUnlock(es->hloc32);
1812 es->text = NULL;
1813 } else if (es->hloc16) {
1814 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1815 es->text = NULL;
1818 es->lock_count--;
1822 /*********************************************************************
1824 * EDIT_WordBreakProc
1826 * Find the beginning of words.
1827 * Note: unlike the specs for a WordBreakProc, this function only
1828 * allows to be called without linebreaks between s[0] upto
1829 * s[count - 1]. Remember it is only called
1830 * internally, so we can decide this for ourselves.
1833 static INT EDIT_WordBreakProc(LPSTR s, INT index, INT count, INT action)
1835 INT ret = 0;
1837 TRACE(edit, "s=%p, index=%u, count=%u, action=%d\n",
1838 s, index, count, action);
1840 switch (action) {
1841 case WB_LEFT:
1842 if (!count)
1843 break;
1844 if (index)
1845 index--;
1846 if (s[index] == ' ') {
1847 while (index && (s[index] == ' '))
1848 index--;
1849 if (index) {
1850 while (index && (s[index] != ' '))
1851 index--;
1852 if (s[index] == ' ')
1853 index++;
1855 } else {
1856 while (index && (s[index] != ' '))
1857 index--;
1858 if (s[index] == ' ')
1859 index++;
1861 ret = index;
1862 break;
1863 case WB_RIGHT:
1864 if (!count)
1865 break;
1866 if (index)
1867 index--;
1868 if (s[index] == ' ')
1869 while ((index < count) && (s[index] == ' ')) index++;
1870 else {
1871 while (s[index] && (s[index] != ' ') && (index < count))
1872 index++;
1873 while ((s[index] == ' ') && (index < count)) index++;
1875 ret = index;
1876 break;
1877 case WB_ISDELIMITER:
1878 ret = (s[index] == ' ');
1879 break;
1880 default:
1881 ERR(edit, "unknown action code, please report !\n");
1882 break;
1884 return ret;
1888 /*********************************************************************
1890 * EM_CHARFROMPOS
1892 * returns line number (not index) in high-order word of result.
1893 * NB : Q137805 is unclear about this. POINT * pointer in lParam apply
1894 * to Richedit, not to the edit control. Original documentation is valid.
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, INT x, INT y)
1901 POINT pt;
1902 RECT rc;
1903 INT index;
1905 pt.x = x;
1906 pt.y = y;
1907 GetClientRect(wnd->hwndSelf, &rc);
1908 if (!PtInRect(&rc, pt))
1909 return -1;
1911 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1912 return MAKELONG(index, EDIT_EM_LineFromChar(wnd, es, index));
1916 /*********************************************************************
1918 * EM_FMTLINES
1920 * Enable or disable soft breaks.
1922 static BOOL EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL add_eol)
1924 es->flags &= ~EF_USE_SOFTBRK;
1925 if (add_eol) {
1926 es->flags |= EF_USE_SOFTBRK;
1927 FIXME(edit, "soft break enabled, not implemented\n");
1929 return add_eol;
1933 /*********************************************************************
1935 * EM_GETHANDLE
1937 * Hopefully this won't fire back at us.
1938 * We always start with a fixed buffer in our own heap.
1939 * However, with this message a 32 bit application requests
1940 * a handle to 32 bit moveable local heap memory, where it expects
1941 * to find the text.
1942 * It's a pity that from this moment on we have to use this
1943 * local heap, because applications may rely on the handle
1944 * in the future.
1946 * In this function we'll try to switch to local heap.
1949 static HLOCAL EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
1951 HLOCAL newBuf;
1952 LPSTR newText;
1953 INT newSize;
1955 if (!(es->style & ES_MULTILINE))
1956 return 0;
1958 if (es->hloc32)
1959 return es->hloc32;
1960 else if (es->hloc16)
1961 return (HLOCAL)es->hloc16;
1963 if (!(newBuf = LocalAlloc(LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
1964 ERR(edit, "could not allocate new 32 bit buffer\n");
1965 return 0;
1967 newSize = MIN(LocalSize(newBuf) - 1, es->buffer_limit);
1968 if (!(newText = LocalLock(newBuf))) {
1969 ERR(edit, "could not lock new 32 bit buffer\n");
1970 LocalFree(newBuf);
1971 return 0;
1973 lstrcpyA(newText, es->text);
1974 EDIT_UnlockBuffer(wnd, es, TRUE);
1975 if (es->text)
1976 HeapFree(es->heap, 0, es->text);
1977 es->hloc32 = newBuf;
1978 es->hloc16 = (HLOCAL16)NULL;
1979 es->buffer_size = newSize;
1980 es->text = newText;
1981 EDIT_LockBuffer(wnd, es);
1982 TRACE(edit, "switched to 32 bit local heap\n");
1984 return es->hloc32;
1988 /*********************************************************************
1990 * EM_GETHANDLE16
1992 * Hopefully this won't fire back at us.
1993 * We always start with a buffer in 32 bit linear memory.
1994 * However, with this message a 16 bit application requests
1995 * a handle of 16 bit local heap memory, where it expects to find
1996 * the text.
1997 * It's a pitty that from this moment on we have to use this
1998 * local heap, because applications may rely on the handle
1999 * in the future.
2001 * In this function we'll try to switch to local heap.
2003 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2005 HLOCAL16 newBuf;
2006 LPSTR newText;
2007 INT newSize;
2009 if (!(es->style & ES_MULTILINE))
2010 return 0;
2012 if (es->hloc16)
2013 return es->hloc16;
2015 if (!LOCAL_HeapSize(wnd->hInstance)) {
2016 if (!LocalInit16(wnd->hInstance, 0,
2017 GlobalSize16(wnd->hInstance))) {
2018 ERR(edit, "could not initialize local heap\n");
2019 return 0;
2021 TRACE(edit, "local heap initialized\n");
2023 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlenA(es->text) + 1))) {
2024 ERR(edit, "could not allocate new 16 bit buffer\n");
2025 return 0;
2027 newSize = MIN(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2028 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2029 ERR(edit, "could not lock new 16 bit buffer\n");
2030 LOCAL_Free(wnd->hInstance, newBuf);
2031 return 0;
2033 lstrcpyA(newText, es->text);
2034 EDIT_UnlockBuffer(wnd, es, TRUE);
2035 if (es->text)
2036 HeapFree(es->heap, 0, es->text);
2037 else if (es->hloc32) {
2038 while (LocalFree(es->hloc32)) ;
2039 LocalFree(es->hloc32);
2041 es->hloc32 = (HLOCAL)NULL;
2042 es->hloc16 = newBuf;
2043 es->buffer_size = newSize;
2044 es->text = newText;
2045 EDIT_LockBuffer(wnd, es);
2046 TRACE(edit, "switched to 16 bit buffer\n");
2048 return es->hloc16;
2052 /*********************************************************************
2054 * EM_GETLINE
2057 static INT EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT line, LPSTR lpch)
2059 LPSTR src;
2060 INT len;
2061 INT i;
2063 if (es->style & ES_MULTILINE) {
2064 if (line >= es->line_count)
2065 return 0;
2066 } else
2067 line = 0;
2068 i = EDIT_EM_LineIndex(wnd, es, line);
2069 src = es->text + i;
2070 len = MIN(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, i));
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, LPUINT start, LPUINT end)
2087 UINT s = es->selection_start;
2088 UINT e = es->selection_end;
2090 ORDER_UINT(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 INT EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT index)
2126 INT line;
2127 LINEDEF *line_def;
2129 if (!(es->style & ES_MULTILINE))
2130 return 0;
2131 if (index > lstrlenA(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 INT EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT line)
2155 INT 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 INT 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 INT EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT index)
2190 LINEDEF *line_def;
2192 if (!(es->style & ES_MULTILINE))
2193 return lstrlenA(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 BOOL EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT dx, INT dy)
2226 INT 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 RECT rc1;
2241 RECT rc;
2242 GetClientRect(wnd->hwndSelf, &rc1);
2243 IntersectRect(&rc, &rc1, &es->format_rect);
2244 ScrollWindowEx(wnd->hwndSelf, -dx, dy,
2245 NULL, &rc, (HRGN)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, INT index, BOOL after_wrap)
2264 INT len = lstrlenA(es->text);
2265 INT l;
2266 INT li;
2267 INT x;
2268 INT y = 0;
2269 HDC dc;
2270 HFONT old_font = 0;
2271 SIZE size;
2273 index = MIN(index, len);
2274 dc = GetDC(wnd->hwndSelf);
2275 if (es->font)
2276 old_font = SelectObject(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 INT 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(GetTabbedTextExtentA(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 SelectObject(dc, old_font);
2315 ReleaseDC(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, BOOL can_undo, LPCSTR lpsz_replace)
2329 INT strl = lstrlenA(lpsz_replace);
2330 INT tl = lstrlenA(es->text);
2331 INT utl;
2332 UINT s;
2333 UINT e;
2334 INT i;
2335 LPSTR p;
2337 s = es->selection_start;
2338 e = es->selection_end;
2340 if ((s == e) && !strl)
2341 return;
2343 ORDER_UINT(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 = lstrlenA(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 lstrcpynA(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 lstrcpynA(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 lstrcpyA(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 = lstrlenA(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 CharUpperBuffA(p, strl);
2407 else if(es->style & ES_LOWERCASE)
2408 CharLowerBuffA(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 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2425 /*********************************************************************
2427 * EM_SCROLL
2430 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT action)
2432 INT 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 INT l;
2476 INT li;
2477 INT vlc;
2478 INT ww;
2479 INT cw = es->char_width;
2480 INT x;
2481 INT dy = 0;
2482 INT 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 INT x;
2501 INT goal;
2502 INT 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 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2517 } else if (x > es->format_rect.right) {
2518 INT x_last;
2519 INT len = lstrlenA(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 InvalidateRect(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, HLOCAL 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 = LocalSize(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 InvalidateRect(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 = (HLOCAL)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 InvalidateRect(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, INT 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, INT action,
2653 INT left, INT 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 InvalidateRect(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, UINT start, UINT end, BOOL after_wrap)
2714 UINT old_start = es->selection_start;
2715 UINT old_end = es->selection_end;
2716 UINT len = lstrlenA(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_UINT(start, end);
2735 ORDER_UINT(end, old_end);
2736 ORDER_UINT(start, old_start);
2737 ORDER_UINT(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 BOOL EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT count, LPINT 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(INT));
2778 memcpy(es->tabs, tabs, count * sizeof(INT));
2780 return TRUE;
2784 /*********************************************************************
2786 * EM_SETTABSTOPS16
2789 static BOOL EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT 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 INT i;
2800 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT));
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, EDITWORDBREAKPROCA 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 InvalidateRect(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 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
2846 /*********************************************************************
2848 * EM_UNDO / WM_UNDO
2851 static BOOL EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2853 INT ulength = lstrlenA(es->undo_text);
2854 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2856 lstrcpyA(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, INT code, INT id, HWND control)
2915 if (code || control)
2916 return;
2918 switch (id) {
2919 case EM_UNDO:
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_SETSEL:
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, HWND hwnd, INT x, INT y)
2963 HMENU menu = LoadMenuIndirectA(SYSRES_GetResPtr(SYSRES_MENU_EDITMENU));
2964 HMENU popup = GetSubMenu(menu, 0);
2965 UINT start = es->selection_start;
2966 UINT end = es->selection_end;
2968 ORDER_UINT(start, end);
2970 /* undo */
2971 EnableMenuItem(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
2972 /* cut */
2973 EnableMenuItem(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2974 /* copy */
2975 EnableMenuItem(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2976 /* paste */
2977 EnableMenuItem(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
2978 /* delete */
2979 EnableMenuItem(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
2980 /* select all */
2981 EnableMenuItem(popup, 7, MF_BYPOSITION | (start || (end != lstrlenA(es->text)) ? MF_ENABLED : MF_GRAYED));
2983 TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
2984 DestroyMenu(menu);
2988 /*********************************************************************
2990 * WM_COPY
2993 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
2995 INT s = es->selection_start;
2996 INT e = es->selection_end;
2997 HGLOBAL hdst;
2998 LPSTR dst;
3000 if (e == s)
3001 return;
3002 ORDER_INT(s, e);
3003 hdst = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(e - s + 1));
3004 dst = GlobalLock(hdst);
3005 lstrcpynA(dst, es->text + s, e - s + 1);
3006 GlobalUnlock(hdst);
3007 OpenClipboard(wnd->hwndSelf);
3008 EmptyClipboard();
3009 SetClipboardData(CF_TEXT, hdst);
3010 CloseClipboard();
3014 /*********************************************************************
3016 * WM_CREATE
3019 static LRESULT EDIT_WM_Create(WND *wnd, EDITSTATE *es, LPCREATESTRUCTA cs)
3022 * To initialize some final structure members, we call some helper
3023 * functions. However, since the EDITSTATE is not consistent (i.e.
3024 * not fully initialized), we should be very careful which
3025 * functions can be called, and in what order.
3027 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3028 EDIT_EM_EmptyUndoBuffer(wnd, es);
3030 if (cs->lpszName && *(cs->lpszName) != '\0') {
3031 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3032 /* 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 */
3033 es->selection_start = es->selection_end = 0;
3034 EDIT_EM_ScrollCaret(wnd, es);
3036 return 0;
3040 /*********************************************************************
3042 * WM_DESTROY
3045 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3047 if (es->hloc32) {
3048 while (LocalUnlock(es->hloc32)) ;
3049 LocalFree(es->hloc32);
3051 if (es->hloc16) {
3052 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3053 LOCAL_Free(wnd->hInstance, es->hloc16);
3055 HeapDestroy(es->heap);
3056 HeapFree(GetProcessHeap(), 0, es);
3057 *(EDITSTATE **)wnd->wExtra = NULL;
3061 /*********************************************************************
3063 * WM_ERASEBKGND
3066 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC dc)
3068 HBRUSH brush;
3069 RECT rc;
3071 if (!(brush = (HBRUSH)EDIT_SEND_CTLCOLOR(wnd, dc)))
3072 brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
3074 GetClientRect(wnd->hwndSelf, &rc);
3075 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3076 GetClipBox(dc, &rc);
3078 * FIXME: specs say that we should UnrealizeObject() the brush,
3079 * but the specs of UnrealizeObject() say that we shouldn't
3080 * unrealize a stock object. The default brush that
3081 * DefWndProc() returns is ... a stock object.
3083 FillRect(dc, &rc, brush);
3084 return -1;
3088 /*********************************************************************
3090 * WM_GETTEXT
3093 static INT EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT count, LPSTR text)
3095 INT len = lstrlenA(es->text);
3097 if (count > len) {
3098 lstrcpyA(text, es->text);
3099 return len;
3100 } else
3101 return -1;
3105 /*********************************************************************
3107 * EDIT_HScroll_Hack
3109 * 16 bit notepad needs this. Actually it is not _our_ hack,
3110 * it is notepad's. Notepad is sending us scrollbar messages with
3111 * undocumented parameters without us even having a scrollbar ... !?!?
3114 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3116 INT dx = 0;
3117 INT fw = es->format_rect.right - es->format_rect.left;
3118 LRESULT ret = 0;
3120 if (!(es->flags & EF_HSCROLL_HACK)) {
3121 ERR(edit, "hacked WM_HSCROLL handler invoked\n");
3122 ERR(edit, " if you are _not_ running 16 bit notepad, please report\n");
3123 ERR(edit, " (this message is only displayed once per edit control)\n");
3124 es->flags |= EF_HSCROLL_HACK;
3127 switch (action) {
3128 case SB_LINELEFT:
3129 if (es->x_offset)
3130 dx = -es->char_width;
3131 break;
3132 case SB_LINERIGHT:
3133 if (es->x_offset < es->text_width)
3134 dx = es->char_width;
3135 break;
3136 case SB_PAGELEFT:
3137 if (es->x_offset)
3138 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3139 break;
3140 case SB_PAGERIGHT:
3141 if (es->x_offset < es->text_width)
3142 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3143 break;
3144 case SB_LEFT:
3145 if (es->x_offset)
3146 dx = -es->x_offset;
3147 break;
3148 case SB_RIGHT:
3149 if (es->x_offset < es->text_width)
3150 dx = es->text_width - es->x_offset;
3151 break;
3152 case SB_THUMBTRACK:
3153 es->flags |= EF_HSCROLL_TRACK;
3154 dx = pos * es->text_width / 100 - es->x_offset;
3155 break;
3156 case SB_THUMBPOSITION:
3157 es->flags &= ~EF_HSCROLL_TRACK;
3158 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3159 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3160 break;
3161 case SB_ENDSCROLL:
3162 break;
3165 * FIXME : the next two are undocumented !
3166 * Are we doing the right thing ?
3167 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3168 * although it's also a regular control message.
3170 case EM_GETTHUMB16:
3171 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3172 break;
3173 case EM_LINESCROLL16:
3174 dx = pos;
3175 break;
3177 default:
3178 ERR(edit, "undocumented (hacked) WM_HSCROLL parameter, please report\n");
3179 return 0;
3181 if (dx)
3182 EDIT_EM_LineScroll(wnd, es, dx, 0);
3183 return ret;
3187 /*********************************************************************
3189 * WM_HSCROLL
3192 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3194 INT dx;
3195 INT fw;
3197 if (!(es->style & ES_MULTILINE))
3198 return 0;
3200 if (!(es->style & ES_AUTOHSCROLL))
3201 return 0;
3203 if (!(es->style & WS_HSCROLL))
3204 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3206 dx = 0;
3207 fw = es->format_rect.right - es->format_rect.left;
3208 switch (action) {
3209 case SB_LINELEFT:
3210 if (es->x_offset)
3211 dx = -es->char_width;
3212 break;
3213 case SB_LINERIGHT:
3214 if (es->x_offset < es->text_width)
3215 dx = es->char_width;
3216 break;
3217 case SB_PAGELEFT:
3218 if (es->x_offset)
3219 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3220 break;
3221 case SB_PAGERIGHT:
3222 if (es->x_offset < es->text_width)
3223 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3224 break;
3225 case SB_LEFT:
3226 if (es->x_offset)
3227 dx = -es->x_offset;
3228 break;
3229 case SB_RIGHT:
3230 if (es->x_offset < es->text_width)
3231 dx = es->text_width - es->x_offset;
3232 break;
3233 case SB_THUMBTRACK:
3234 es->flags |= EF_HSCROLL_TRACK;
3235 dx = pos - es->x_offset;
3236 break;
3237 case SB_THUMBPOSITION:
3238 es->flags &= ~EF_HSCROLL_TRACK;
3239 if (!(dx = pos - es->x_offset)) {
3240 SetScrollPos(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3241 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3243 break;
3244 case SB_ENDSCROLL:
3245 break;
3247 default:
3248 ERR(edit, "undocumented WM_HSCROLL parameter, please report\n");
3249 return 0;
3251 if (dx)
3252 EDIT_EM_LineScroll(wnd, es, dx, 0);
3253 return 0;
3257 /*********************************************************************
3259 * EDIT_CheckCombo
3262 static BOOL EDIT_CheckCombo(WND *wnd, UINT msg, INT key, DWORD key_data)
3264 HWND hLBox;
3266 if (WIDGETS_IsControl(wnd->parent, BIC32_COMBO) &&
3267 (hLBox = COMBO_GetLBWindow(wnd->parent))) {
3268 HWND hCombo = wnd->parent->hwndSelf;
3269 BOOL bUIFlip = TRUE;
3271 TRACE(combo, "[%04x]: handling msg %04x (%04x)\n",
3272 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3274 switch (msg) {
3275 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3276 if (key != VK_F4) {
3277 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3278 if (SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0))
3279 bUIFlip = FALSE;
3281 if (!bUIFlip)
3282 SendMessageA(hLBox, WM_KEYDOWN, (WPARAM)key, 0);
3283 else {
3284 /* make sure ComboLBox pops up */
3285 SendMessageA(hCombo, CB_SETEXTENDEDUI, 0, 0);
3286 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3287 SendMessageA(hCombo, CB_SETEXTENDEDUI, 1, 0);
3289 break;
3290 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3291 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETEXTENDEDUI, 0, 0);
3292 if (bUIFlip) {
3293 bUIFlip = (BOOL)SendMessageA(hCombo, CB_GETDROPPEDSTATE, 0, 0);
3294 SendMessageA(hCombo, CB_SHOWDROPDOWN, (bUIFlip) ? FALSE : TRUE, 0);
3295 } else
3296 SendMessageA(hLBox, WM_KEYDOWN, VK_F4, 0);
3297 break;
3299 return TRUE;
3301 return FALSE;
3305 /*********************************************************************
3307 * WM_KEYDOWN
3309 * Handling of special keys that don't produce a WM_CHAR
3310 * (i.e. non-printable keys) & Backspace & Delete
3313 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3315 BOOL shift;
3316 BOOL control;
3318 if (GetKeyState(VK_MENU) & 0x8000)
3319 return 0;
3321 shift = GetKeyState(VK_SHIFT) & 0x8000;
3322 control = GetKeyState(VK_CONTROL) & 0x8000;
3324 switch (key) {
3325 case VK_F4:
3326 case VK_UP:
3327 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3328 break;
3329 if (key == VK_F4)
3330 break;
3331 /* fall through */
3332 case VK_LEFT:
3333 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3334 EDIT_MoveUp_ML(wnd, es, shift);
3335 else
3336 if (control)
3337 EDIT_MoveWordBackward(wnd, es, shift);
3338 else
3339 EDIT_MoveBackward(wnd, es, shift);
3340 break;
3341 case VK_DOWN:
3342 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3343 break;
3344 /* fall through */
3345 case VK_RIGHT:
3346 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3347 EDIT_MoveDown_ML(wnd, es, shift);
3348 else if (control)
3349 EDIT_MoveWordForward(wnd, es, shift);
3350 else
3351 EDIT_MoveForward(wnd, es, shift);
3352 break;
3353 case VK_HOME:
3354 EDIT_MoveHome(wnd, es, shift);
3355 break;
3356 case VK_END:
3357 EDIT_MoveEnd(wnd, es, shift);
3358 break;
3359 case VK_PRIOR:
3360 if (es->style & ES_MULTILINE)
3361 EDIT_MovePageUp_ML(wnd, es, shift);
3362 break;
3363 case VK_NEXT:
3364 if (es->style & ES_MULTILINE)
3365 EDIT_MovePageDown_ML(wnd, es, shift);
3366 break;
3367 case VK_BACK:
3368 if (!(es->style & ES_READONLY) && !control) {
3369 if (es->selection_start != es->selection_end)
3370 EDIT_WM_Clear(wnd, es);
3371 else {
3372 /* delete character left of caret */
3373 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3374 EDIT_MoveBackward(wnd, es, TRUE);
3375 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);
3405 break;
3406 case VK_INSERT:
3407 if (shift) {
3408 if (!(es->style & ES_READONLY))
3409 EDIT_WM_Paste(wnd, es);
3410 } else if (control)
3411 EDIT_WM_Copy(wnd, es);
3412 break;
3414 return 0;
3418 /*********************************************************************
3420 * WM_KILLFOCUS
3423 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND window_getting_focus)
3425 es->flags &= ~EF_FOCUSED;
3426 DestroyCaret();
3427 if(!(es->style & ES_NOHIDESEL))
3428 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3429 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3430 return 0;
3434 /*********************************************************************
3436 * WM_LBUTTONDBLCLK
3438 * The caret position has been set on the WM_LBUTTONDOWN message
3441 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3443 INT s;
3444 INT e = es->selection_end;
3445 INT l;
3446 INT li;
3447 INT ll;
3449 if (!(es->flags & EF_FOCUSED))
3450 return 0;
3452 l = EDIT_EM_LineFromChar(wnd, es, e);
3453 li = EDIT_EM_LineIndex(wnd, es, l);
3454 ll = EDIT_EM_LineLength(wnd, es, e);
3455 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3456 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3457 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3458 EDIT_EM_ScrollCaret(wnd, es);
3459 return 0;
3463 /*********************************************************************
3465 * WM_LBUTTONDOWN
3468 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3470 INT e;
3471 BOOL after_wrap;
3473 if (!(es->flags & EF_FOCUSED))
3474 return 0;
3476 SetCapture(wnd->hwndSelf);
3477 EDIT_ConfinePoint(wnd, es, &x, &y);
3478 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3479 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3480 EDIT_EM_ScrollCaret(wnd, es);
3481 es->region_posx = es->region_posy = 0;
3482 SetTimer(wnd->hwndSelf, 0, 100, NULL);
3483 return 0;
3487 /*********************************************************************
3489 * WM_LBUTTONUP
3492 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3494 if (GetCapture() == wnd->hwndSelf) {
3495 KillTimer(wnd->hwndSelf, 0);
3496 ReleaseCapture();
3498 return 0;
3502 /*********************************************************************
3504 * WM_MOUSEMOVE
3507 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT x, INT y)
3509 INT e;
3510 BOOL after_wrap;
3511 INT prex, prey;
3513 if (GetCapture() != wnd->hwndSelf)
3514 return 0;
3517 * FIXME: gotta do some scrolling if outside client
3518 * area. Maybe reset the timer ?
3520 prex = x; prey = y;
3521 EDIT_ConfinePoint(wnd, es, &x, &y);
3522 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3523 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3524 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3525 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3526 return 0;
3530 /*********************************************************************
3532 * WM_NCCREATE
3535 static LRESULT EDIT_WM_NCCreate(WND *wnd, LPCREATESTRUCTA cs)
3537 EDITSTATE *es;
3539 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
3540 return FALSE;
3541 *(EDITSTATE **)wnd->wExtra = es;
3544 * Note: since the EDITSTATE has not been fully initialized yet,
3545 * we can't use any API calls that may send
3546 * WM_XXX messages before WM_NCCREATE is completed.
3549 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
3550 return FALSE;
3551 es->style = cs->style;
3553 if ((es->style & WS_BORDER) && !(es->style & WS_DLGFRAME))
3554 wnd->dwStyle &= ~WS_BORDER;
3556 if (es->style & ES_MULTILINE) {
3557 es->buffer_size = BUFSTART_MULTI;
3558 es->buffer_limit = BUFLIMIT_MULTI;
3559 if (es->style & WS_VSCROLL)
3560 es->style |= ES_AUTOVSCROLL;
3561 if (es->style & WS_HSCROLL)
3562 es->style |= ES_AUTOHSCROLL;
3563 es->style &= ~ES_PASSWORD;
3564 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3565 if (es->style & ES_RIGHT)
3566 es->style &= ~ES_CENTER;
3567 es->style &= ~WS_HSCROLL;
3568 es->style &= ~ES_AUTOHSCROLL;
3571 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3572 es->style |= ES_AUTOVSCROLL;
3573 } else {
3574 es->buffer_size = BUFSTART_SINGLE;
3575 es->buffer_limit = BUFLIMIT_SINGLE;
3576 es->style &= ~ES_CENTER;
3577 es->style &= ~ES_RIGHT;
3578 es->style &= ~WS_HSCROLL;
3579 es->style &= ~WS_VSCROLL;
3580 es->style &= ~ES_AUTOVSCROLL;
3581 es->style &= ~ES_WANTRETURN;
3582 if (es->style & ES_UPPERCASE) {
3583 es->style &= ~ES_LOWERCASE;
3584 es->style &= ~ES_NUMBER;
3585 } else if (es->style & ES_LOWERCASE)
3586 es->style &= ~ES_NUMBER;
3587 if (es->style & ES_PASSWORD)
3588 es->password_char = '*';
3590 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3591 es->style |= ES_AUTOHSCROLL;
3593 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3594 return FALSE;
3595 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3596 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3597 return FALSE;
3598 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3599 *es->text = '\0';
3600 if (es->style & ES_MULTILINE)
3601 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3602 return FALSE;
3603 es->line_count = 1;
3605 return TRUE;
3608 /*********************************************************************
3610 * WM_PAINT
3613 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es)
3615 PAINTSTRUCT ps;
3616 INT i;
3617 HDC dc;
3618 HFONT old_font = 0;
3619 RECT rc;
3620 RECT rcLine;
3621 RECT rcRgn;
3622 BOOL rev = IsWindowEnabled(wnd->hwndSelf) &&
3623 ((es->flags & EF_FOCUSED) ||
3624 (es->style & ES_NOHIDESEL));
3626 if (es->flags & EF_UPDATE)
3627 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
3629 dc = BeginPaint(wnd->hwndSelf, &ps);
3630 if(es->style & WS_BORDER) {
3631 GetClientRect(wnd->hwndSelf, &rc);
3632 if(es->style & ES_MULTILINE) {
3633 if(es->style & WS_HSCROLL) rc.bottom++;
3634 if(es->style & WS_VSCROLL) rc.right++;
3636 Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
3638 IntersectClipRect(dc, es->format_rect.left,
3639 es->format_rect.top,
3640 es->format_rect.right,
3641 es->format_rect.bottom);
3642 if (es->style & ES_MULTILINE) {
3643 GetClientRect(wnd->hwndSelf, &rc);
3644 IntersectClipRect(dc, rc.left, rc.top, rc.right, rc.bottom);
3646 if (es->font)
3647 old_font = SelectObject(dc, es->font);
3648 EDIT_SEND_CTLCOLOR(wnd, dc);
3649 if (!IsWindowEnabled(wnd->hwndSelf))
3650 SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
3651 GetClipBox(dc, &rcRgn);
3652 if (es->style & ES_MULTILINE) {
3653 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3654 for (i = es->y_offset ; i <= MIN(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3655 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3656 if (IntersectRect(&rc, &rcRgn, &rcLine))
3657 EDIT_PaintLine(wnd, es, dc, i, rev);
3659 } else {
3660 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3661 if (IntersectRect(&rc, &rcRgn, &rcLine))
3662 EDIT_PaintLine(wnd, es, dc, 0, rev);
3664 if (es->font)
3665 SelectObject(dc, old_font);
3666 if (es->flags & EF_FOCUSED)
3667 EDIT_SetCaretPos(wnd, es, es->selection_end,
3668 es->flags & EF_AFTER_WRAP);
3669 EndPaint(wnd->hwndSelf, &ps);
3670 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3671 INT vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3672 SCROLLINFO si;
3673 si.cbSize = sizeof(SCROLLINFO);
3674 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3675 si.nMin = 0;
3676 si.nMax = es->line_count + vlc - 2;
3677 si.nPage = vlc;
3678 si.nPos = es->y_offset;
3679 SetScrollInfo(wnd->hwndSelf, SB_VERT, &si, TRUE);
3681 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3682 SCROLLINFO si;
3683 INT fw = es->format_rect.right - es->format_rect.left;
3684 si.cbSize = sizeof(SCROLLINFO);
3685 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3686 si.nMin = 0;
3687 si.nMax = es->text_width + fw - 1;
3688 si.nPage = fw;
3689 si.nPos = es->x_offset;
3690 SetScrollInfo(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3693 if (es->flags & EF_UPDATE) {
3694 es->flags &= ~EF_UPDATE;
3695 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3700 /*********************************************************************
3702 * WM_PASTE
3705 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3707 HGLOBAL hsrc;
3708 LPSTR src;
3710 OpenClipboard(wnd->hwndSelf);
3711 if ((hsrc = GetClipboardData(CF_TEXT))) {
3712 src = (LPSTR)GlobalLock(hsrc);
3713 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3714 GlobalUnlock(hsrc);
3716 CloseClipboard();
3720 /*********************************************************************
3722 * WM_SETFOCUS
3725 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND window_losing_focus)
3727 es->flags |= EF_FOCUSED;
3728 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3729 EDIT_SetCaretPos(wnd, es, es->selection_end,
3730 es->flags & EF_AFTER_WRAP);
3731 if(!(es->style & ES_NOHIDESEL))
3732 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3733 ShowCaret(wnd->hwndSelf);
3734 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3738 /*********************************************************************
3740 * WM_SETFONT
3742 * With Win95 look the margins are set to default font value unless
3743 * the system font (font == 0) is being set, in which case they are left
3744 * unchanged.
3747 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT font, BOOL redraw)
3749 TEXTMETRICA tm;
3750 HDC dc;
3751 HFONT old_font = 0;
3753 es->font = font;
3754 dc = GetDC(wnd->hwndSelf);
3755 if (font)
3756 old_font = SelectObject(dc, font);
3757 GetTextMetricsA(dc, &tm);
3758 es->line_height = tm.tmHeight;
3759 es->char_width = tm.tmAveCharWidth;
3760 if (font)
3761 SelectObject(dc, old_font);
3762 ReleaseDC(wnd->hwndSelf, dc);
3763 if (font && (TWEAK_WineLook > WIN31_LOOK))
3764 EDIT_EM_SetMargins(wnd, es, EC_LEFTMARGIN | EC_RIGHTMARGIN,
3765 EC_USEFONTINFO, EC_USEFONTINFO);
3766 if (es->style & ES_MULTILINE)
3767 EDIT_BuildLineDefs_ML(wnd, es);
3768 else {
3769 RECT r;
3770 GetClientRect(wnd->hwndSelf, &r);
3771 EDIT_SetRectNP(wnd, es, &r);
3773 if (redraw)
3774 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
3775 if (es->flags & EF_FOCUSED) {
3776 DestroyCaret();
3777 CreateCaret(wnd->hwndSelf, 0, 2, es->line_height);
3778 EDIT_SetCaretPos(wnd, es, es->selection_end,
3779 es->flags & EF_AFTER_WRAP);
3780 ShowCaret(wnd->hwndSelf);
3785 /*********************************************************************
3787 * WM_SETTEXT
3789 * NOTES
3790 * For multiline controls (ES_MULTILINE), reception of WM_SETTEXT triggers:
3791 * The modified flag is reset. No notifications are sent.
3793 * For single-line controls, reception of WM_SETTEXT triggers:
3794 * The modified flag is reset. EN_UPDATE and EN_CHANGE notifications are sent.
3797 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3799 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3800 if (text) {
3801 TRACE(edit, "\t'%s'\n", text);
3802 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3803 } else {
3804 TRACE(edit, "\t<NULL>\n");
3805 EDIT_EM_ReplaceSel(wnd, es, FALSE, "");
3807 es->x_offset = 0;
3808 if (es->style & ES_MULTILINE) {
3809 es->flags &= ~EF_UPDATE;
3810 } else {
3811 es->flags |= EF_UPDATE;
3813 es->flags &= ~EF_MODIFIED;
3814 EDIT_EM_SetSel(wnd, es, 0, 0, FALSE);
3815 EDIT_EM_ScrollCaret(wnd, es);
3819 /*********************************************************************
3821 * WM_SIZE
3824 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT action, INT width, INT height)
3826 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3827 RECT rc;
3828 SetRect(&rc, 0, 0, width, height);
3829 EDIT_SetRectNP(wnd, es, &rc);
3830 InvalidateRect(wnd->hwndSelf, NULL, TRUE);
3835 /*********************************************************************
3837 * WM_SYSKEYDOWN
3840 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT key, DWORD key_data)
3842 if ((key == VK_BACK) && (key_data & 0x2000)) {
3843 if (EDIT_EM_CanUndo(wnd, es))
3844 EDIT_EM_Undo(wnd, es);
3845 return 0;
3846 } else if (key == VK_UP || key == VK_DOWN)
3847 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
3848 return 0;
3849 return DefWindowProcA(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM)key, (LPARAM)key_data);
3853 /*********************************************************************
3855 * WM_TIMER
3858 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT id, TIMERPROC timer_proc)
3860 if (es->region_posx < 0) {
3861 EDIT_MoveBackward(wnd, es, TRUE);
3862 } else if (es->region_posx > 0) {
3863 EDIT_MoveForward(wnd, es, TRUE);
3866 * FIXME: gotta do some vertical scrolling here, like
3867 * EDIT_EM_LineScroll(wnd, 0, 1);
3872 /*********************************************************************
3874 * EDIT_VScroll_Hack
3876 * 16 bit notepad needs this. Actually it is not _our_ hack,
3877 * it is notepad's. Notepad is sending us scrollbar messages with
3878 * undocumented parameters without us even having a scrollbar ... !?!?
3881 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3883 INT dy = 0;
3884 LRESULT ret = 0;
3886 if (!(es->flags & EF_VSCROLL_HACK)) {
3887 ERR(edit, "hacked WM_VSCROLL handler invoked\n");
3888 ERR(edit, " if you are _not_ running 16 bit notepad, please report\n");
3889 ERR(edit, " (this message is only displayed once per edit control)\n");
3890 es->flags |= EF_VSCROLL_HACK;
3893 switch (action) {
3894 case SB_LINEUP:
3895 case SB_LINEDOWN:
3896 case SB_PAGEUP:
3897 case SB_PAGEDOWN:
3898 EDIT_EM_Scroll(wnd, es, action);
3899 return 0;
3900 case SB_TOP:
3901 dy = -es->y_offset;
3902 break;
3903 case SB_BOTTOM:
3904 dy = es->line_count - 1 - es->y_offset;
3905 break;
3906 case SB_THUMBTRACK:
3907 es->flags |= EF_VSCROLL_TRACK;
3908 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
3909 break;
3910 case SB_THUMBPOSITION:
3911 es->flags &= ~EF_VSCROLL_TRACK;
3912 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
3913 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3914 break;
3915 case SB_ENDSCROLL:
3916 break;
3919 * FIXME : the next two are undocumented !
3920 * Are we doing the right thing ?
3921 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3922 * although it's also a regular control message.
3924 case EM_GETTHUMB16:
3925 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
3926 break;
3927 case EM_LINESCROLL16:
3928 dy = pos;
3929 break;
3931 default:
3932 ERR(edit, "undocumented (hacked) WM_VSCROLL parameter, please report\n");
3933 return 0;
3935 if (dy)
3936 EDIT_EM_LineScroll(wnd, es, 0, dy);
3937 return ret;
3941 /*********************************************************************
3943 * WM_VSCROLL
3946 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT action, INT pos, HWND scroll_bar)
3948 INT dy;
3950 if (!(es->style & ES_MULTILINE))
3951 return 0;
3953 if (!(es->style & ES_AUTOVSCROLL))
3954 return 0;
3956 if (!(es->style & WS_VSCROLL))
3957 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
3959 dy = 0;
3960 switch (action) {
3961 case SB_LINEUP:
3962 case SB_LINEDOWN:
3963 case SB_PAGEUP:
3964 case SB_PAGEDOWN:
3965 EDIT_EM_Scroll(wnd, es, action);
3966 return 0;
3968 case SB_TOP:
3969 dy = -es->y_offset;
3970 break;
3971 case SB_BOTTOM:
3972 dy = es->line_count - 1 - es->y_offset;
3973 break;
3974 case SB_THUMBTRACK:
3975 es->flags |= EF_VSCROLL_TRACK;
3976 dy = pos - es->y_offset;
3977 break;
3978 case SB_THUMBPOSITION:
3979 es->flags &= ~EF_VSCROLL_TRACK;
3980 if (!(dy = pos - es->y_offset)) {
3981 SetScrollPos(wnd->hwndSelf, SB_VERT, pos, TRUE);
3982 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3984 break;
3985 case SB_ENDSCROLL:
3986 break;
3988 default:
3989 ERR(edit, "undocumented WM_VSCROLL action %d, please report\n",
3990 action);
3991 return 0;
3993 if (dy)
3994 EDIT_EM_LineScroll(wnd, es, 0, dy);
3995 return 0;