Release 971221
[wine/multimedia.git] / controls / edit.c
blob303d2f1f70e388ec8b1d61c4f841fcc9f9a0a88c
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 <stdio.h>
15 #include "windows.h"
16 #include "winnt.h"
17 #include "win.h"
18 #include "combo.h"
19 #include "local.h"
20 #include "resource.h"
21 #include "stddebug.h"
22 #include "debug.h"
23 #include "callback.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 */
46 typedef BOOL32 *LPBOOL32;
48 typedef enum
50 END_0 = 0, /* line ends with terminating '\0' character */
51 END_WRAP, /* line is wrapped */
52 END_HARD, /* line ends with a hard return '\r\n' */
53 END_SOFT, /* line ends with a soft return '\r\r\n' */
54 } LINE_END;
56 typedef struct tagLINEDEF {
57 INT32 length; /* bruto length of a line in bytes */
58 INT32 net_length; /* netto length of a line in visible characters */
59 LINE_END ending;
60 INT32 width; /* width of the line in pixels */
61 struct tagLINEDEF *next;
62 } LINEDEF;
64 typedef struct
66 HANDLE32 heap; /* our own heap */
67 LPSTR text; /* the actual contents of the control */
68 INT32 buffer_size; /* the size of the buffer */
69 INT32 buffer_limit; /* the maximum size to which the buffer may grow */
70 HFONT32 font; /* NULL means standard system font */
71 INT32 x_offset; /* scroll offset for multi lines this is in pixels
72 for single lines it's in characters */
73 INT32 line_height; /* height of a screen line in pixels */
74 INT32 char_width; /* average character width in pixels */
75 DWORD style; /* sane version of wnd->dwStyle */
76 WORD flags; /* flags that are not in es->style or wnd->flags (EF_XXX) */
77 INT32 undo_insert_count; /* number of characters inserted in sequence */
78 INT32 undo_position; /* character index of the insertion and deletion */
79 LPSTR undo_text; /* deleted text */
80 INT32 undo_buffer_size; /* size of the deleted text buffer */
81 INT32 selection_start; /* == selection_end if no selection */
82 INT32 selection_end; /* == current caret position */
83 CHAR password_char; /* == 0 if no password char, and for multi line controls */
84 INT32 left_margin; /* in pixels */
85 INT32 right_margin; /* in pixels */
86 RECT32 format_rect;
87 INT32 region_posx; /* Position of cursor relative to region: */
88 INT32 region_posy; /* -1: to left, 0: within, 1: to right */
89 EDITWORDBREAKPROC16 word_break_proc16;
90 EDITWORDBREAKPROC32A word_break_proc32A;
91 INT32 line_count; /* number of lines */
92 INT32 y_offset; /* scroll offset in number of lines */
94 * only for multi line controls
96 INT32 lock_count; /* amount of re-entries in the EditWndProc */
97 INT32 tabs_count;
98 LPINT32 tabs;
99 INT32 text_width; /* width of the widest line in pixels */
100 LINEDEF *first_line_def; /* linked list of (soft) linebreaks */
101 HLOCAL16 hloc16; /* for controls receiving EM_GETHANDLE16 */
102 HLOCAL32 hloc32; /* for controls receiving EM_GETHANDLE */
103 } EDITSTATE;
106 #define SWAP_INT32(x,y) do { INT32 temp = (INT32)(x); (x) = (INT32)(y); (y) = temp; } while(0)
107 #define ORDER_INT32(x,y) do { if ((INT32)(y) < (INT32)(x)) SWAP_INT32((x),(y)); } while(0)
109 #define SWAP_UINT32(x,y) do { UINT32 temp = (UINT32)(x); (x) = (UINT32)(y); (y) = temp; } while(0)
110 #define ORDER_UINT32(x,y) do { if ((UINT32)(y) < (UINT32)(x)) SWAP_UINT32((x),(y)); } while(0)
112 #define DPRINTF_EDIT_NOTIFY(hwnd, str) \
113 ({dprintf_edit(stddeb, \
114 "edit: notification " str " sent to hwnd=%08x\n", \
115 (UINT32)(hwnd));})
117 #define EDIT_SEND_CTLCOLOR(wnd,hdc) \
118 (SendMessage32A((wnd)->parent->hwndSelf, WM_CTLCOLOREDIT, \
119 (WPARAM32)(hdc), (LPARAM)(wnd)->hwndSelf))
120 #define EDIT_NOTIFY_PARENT(wnd, wNotifyCode, str) \
121 (DPRINTF_EDIT_NOTIFY((wnd)->parent->hwndSelf, str), \
122 SendMessage32A((wnd)->parent->hwndSelf, WM_COMMAND, \
123 MAKEWPARAM((wnd)->wIDmenu, wNotifyCode), \
124 (LPARAM)(wnd)->hwndSelf))
125 #define DPRINTF_EDIT_MSG16(str) \
126 dprintf_edit(stddeb, \
127 "edit: 16 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
128 (UINT32)hwnd, (UINT32)wParam, (UINT32)lParam)
129 #define DPRINTF_EDIT_MSG32(str) \
130 dprintf_edit(stddeb, \
131 "edit: 32 bit : " str ": hwnd=%08x, wParam=%08x, lParam=%08x\n", \
132 (UINT32)hwnd, (UINT32)wParam, (UINT32)lParam)
135 /*********************************************************************
137 * Declarations
142 * These functions have trivial implementations
143 * We still like to call them internally
144 * "static __inline__" makes them more like macro's
146 static __inline__ BOOL32 EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es);
147 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es);
148 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es);
149 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es);
151 * This is the only exported function
153 LRESULT WINAPI EditWndProc( HWND32 hwnd, UINT32 msg,
154 WPARAM32 wParam, LPARAM lParam );
156 * Helper functions only valid for one type of control
158 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es);
159 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es);
160 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
161 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
162 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
163 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend);
165 * Helper functions valid for both single line _and_ multi line controls
167 static INT32 EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT32 start, INT32 index, INT32 count, INT32 action);
168 static INT32 EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y, LPBOOL32 after_wrap);
169 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT32 x, LPINT32 y);
170 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT32 line, INT32 scol, INT32 ecol, LPRECT32 rc);
171 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end);
172 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es);
173 static BOOL32 EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT32 size);
174 static BOOL32 EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT32 size);
175 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL32 extend);
176 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL32 extend);
177 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL32 extend);
178 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL32 extend);
179 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL32 extend);
180 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL32 extend);
181 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC32 hdc, INT32 line, BOOL32 rev);
182 static INT32 EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC32 hdc, INT32 x, INT32 y, INT32 line, INT32 col, INT32 count, BOOL32 rev);
183 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT32 lprc);
184 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL32 force);
185 static INT32 EDIT_WordBreakProc(LPSTR s, INT32 index, INT32 count, INT32 action);
187 * EM_XXX message handlers
189 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y);
190 static BOOL32 EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL32 add_eol);
191 static HLOCAL32 EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es);
192 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es);
193 static INT32 EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT32 line, LPSTR lpch);
194 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT32 start, LPUINT32 end);
195 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es);
196 static INT32 EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT32 index);
197 static INT32 EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT32 line);
198 static INT32 EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT32 index);
199 static BOOL32 EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT32 dx, INT32 dy);
200 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT32 index, BOOL32 after_wrap);
201 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL32 can_undo, LPCSTR lpsz_replace);
202 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT32 action);
203 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es);
204 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL32 hloc);
205 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc);
206 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT32 limit);
207 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT32 action, INT32 left, INT32 right);
208 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c);
209 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT32 start, UINT32 end, BOOL32 after_wrap);
210 static BOOL32 EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT32 count, LPINT32 tabs);
211 static BOOL32 EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT32 count, LPINT16 tabs);
212 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC32A wbp);
213 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp);
214 static BOOL32 EDIT_EM_Undo(WND *wnd, EDITSTATE *es);
216 * WM_XXX message handlers
218 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data);
219 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT32 code, INT32 id, HWND32 conrtol);
220 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND32 hwnd, INT32 x, INT32 y);
221 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es);
222 static LRESULT EDIT_WM_Create(WND *wnd, LPCREATESTRUCT32A cs);
223 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es);
224 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC32 dc);
225 static INT32 EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT32 count, LPSTR text);
226 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar);
227 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data);
228 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND32 window_getting_focus);
229 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
230 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
231 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
232 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y);
233 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es);
234 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es);
235 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND32 window_losing_focus);
236 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT32 font, BOOL32 redraw);
237 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text);
238 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT32 action, INT32 width, INT32 height);
239 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data);
240 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT32 id, TIMERPROC32 timer_proc);
241 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar);
244 /*********************************************************************
246 * EM_CANUNDO
249 static __inline__ BOOL32 EDIT_EM_CanUndo(WND *wnd, EDITSTATE *es)
251 return (es->undo_insert_count || lstrlen32A(es->undo_text));
255 /*********************************************************************
257 * EM_EMPTYUNDOBUFFER
260 static __inline__ void EDIT_EM_EmptyUndoBuffer(WND *wnd, EDITSTATE *es)
262 es->undo_insert_count = 0;
263 *es->undo_text = '\0';
267 /*********************************************************************
269 * WM_CLEAR
272 static __inline__ void EDIT_WM_Clear(WND *wnd, EDITSTATE *es)
274 EDIT_EM_ReplaceSel(wnd, es, TRUE, "");
278 /*********************************************************************
280 * WM_CUT
283 static __inline__ void EDIT_WM_Cut(WND *wnd, EDITSTATE *es)
285 EDIT_WM_Copy(wnd, es);
286 EDIT_WM_Clear(wnd, es);
290 /*********************************************************************
292 * EditWndProc()
294 * The messages are in the order of the actual integer values
295 * (which can be found in include/windows.h)
296 * Whereever possible the 16 bit versions are converted to
297 * the 32 bit ones, so that we can 'fall through' to the
298 * helper functions. These are mostly 32 bit (with a few
299 * exceptions, clearly indicated by a '16' extension to their
300 * names).
303 LRESULT WINAPI EditWndProc( HWND32 hwnd, UINT32 msg,
304 WPARAM32 wParam, LPARAM lParam )
306 WND *wnd = WIN_FindWndPtr(hwnd);
307 EDITSTATE *es = *(EDITSTATE **)((wnd)->wExtra);
308 LRESULT result = 0;
310 switch (msg) {
311 case WM_CREATE:
312 DPRINTF_EDIT_MSG32("WM_CREATE");
313 return EDIT_WM_Create(wnd, (LPCREATESTRUCT32A)lParam);
315 case WM_DESTROY:
316 DPRINTF_EDIT_MSG32("WM_DESTROY");
317 EDIT_WM_Destroy(wnd, es);
318 return 0;
321 if (!es)
322 return DefWindowProc32A(hwnd, msg, wParam, lParam);
324 EDIT_LockBuffer(wnd, es);
325 switch (msg) {
326 case EM_GETSEL16:
327 DPRINTF_EDIT_MSG16("EM_GETSEL");
328 wParam = 0;
329 lParam = 0;
330 /* fall through */
331 case EM_GETSEL32:
332 DPRINTF_EDIT_MSG32("EM_GETSEL");
333 result = EDIT_EM_GetSel(wnd, es, (LPUINT32)wParam, (LPUINT32)lParam);
334 break;
336 case EM_SETSEL16:
337 DPRINTF_EDIT_MSG16("EM_SETSEL");
338 if (SLOWORD(lParam) == -1)
339 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
340 else
341 EDIT_EM_SetSel(wnd, es, LOWORD(lParam), HIWORD(lParam), FALSE);
342 if (!wParam)
343 EDIT_EM_ScrollCaret(wnd, es);
344 result = 1;
345 break;
346 case EM_SETSEL32:
347 DPRINTF_EDIT_MSG32("EM_SETSEL");
348 EDIT_EM_SetSel(wnd, es, wParam, lParam, FALSE);
349 result = 1;
350 break;
352 case EM_GETRECT16:
353 DPRINTF_EDIT_MSG16("EM_GETRECT");
354 if (lParam)
355 CONV_RECT32TO16(&es->format_rect, (LPRECT16)PTR_SEG_TO_LIN(lParam));
356 break;
357 case EM_GETRECT32:
358 DPRINTF_EDIT_MSG32("EM_GETRECT");
359 if (lParam)
360 CopyRect32((LPRECT32)lParam, &es->format_rect);
361 break;
363 case EM_SETRECT16:
364 DPRINTF_EDIT_MSG16("EM_SETRECT");
365 if ((es->style & ES_MULTILINE) && lParam) {
366 RECT32 rc;
367 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
368 EDIT_SetRectNP(wnd, es, &rc);
369 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
371 break;
372 case EM_SETRECT32:
373 DPRINTF_EDIT_MSG32("EM_SETRECT");
374 if ((es->style & ES_MULTILINE) && lParam) {
375 EDIT_SetRectNP(wnd, es, (LPRECT32)lParam);
376 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
378 break;
380 case EM_SETRECTNP16:
381 DPRINTF_EDIT_MSG16("EM_SETRECTNP");
382 if ((es->style & ES_MULTILINE) && lParam) {
383 RECT32 rc;
384 CONV_RECT16TO32((LPRECT16)PTR_SEG_TO_LIN(lParam), &rc);
385 EDIT_SetRectNP(wnd, es, &rc);
387 break;
388 case EM_SETRECTNP32:
389 DPRINTF_EDIT_MSG32("EM_SETRECTNP");
390 if ((es->style & ES_MULTILINE) && lParam)
391 EDIT_SetRectNP(wnd, es, (LPRECT32)lParam);
392 break;
394 case EM_SCROLL16:
395 DPRINTF_EDIT_MSG16("EM_SCROLL");
396 /* fall through */
397 case EM_SCROLL32:
398 DPRINTF_EDIT_MSG32("EM_SCROLL");
399 result = EDIT_EM_Scroll(wnd, es, (INT32)wParam);
400 break;
402 case EM_LINESCROLL16:
403 DPRINTF_EDIT_MSG16("EM_LINESCROLL");
404 wParam = (WPARAM32)(INT32)SHIWORD(lParam);
405 lParam = (LPARAM)(INT32)SLOWORD(lParam);
406 /* fall through */
407 case EM_LINESCROLL32:
408 DPRINTF_EDIT_MSG32("EM_LINESCROLL");
409 result = (LRESULT)EDIT_EM_LineScroll(wnd, es, (INT32)wParam, (INT32)lParam);
410 break;
412 case EM_SCROLLCARET16:
413 DPRINTF_EDIT_MSG16("EM_SCROLLCARET");
414 /* fall through */
415 case EM_SCROLLCARET32:
416 DPRINTF_EDIT_MSG32("EM_SCROLLCARET");
417 EDIT_EM_ScrollCaret(wnd, es);
418 result = 1;
419 break;
421 case EM_GETMODIFY16:
422 DPRINTF_EDIT_MSG16("EM_GETMODIFY");
423 /* fall through */
424 case EM_GETMODIFY32:
425 DPRINTF_EDIT_MSG32("EM_GETMODIFY");
426 return ((es->flags & EF_MODIFIED) != 0);
427 break;
429 case EM_SETMODIFY16:
430 DPRINTF_EDIT_MSG16("EM_SETMODIFY");
431 /* fall through */
432 case EM_SETMODIFY32:
433 DPRINTF_EDIT_MSG32("EM_SETMODIFY");
434 if (wParam)
435 es->flags |= EF_MODIFIED;
436 else
437 es->flags &= ~EF_MODIFIED;
438 break;
440 case EM_GETLINECOUNT16:
441 DPRINTF_EDIT_MSG16("EM_GETLINECOUNT");
442 /* fall through */
443 case EM_GETLINECOUNT32:
444 DPRINTF_EDIT_MSG32("EM_GETLINECOUNT");
445 result = (es->style & ES_MULTILINE) ? es->line_count : 1;
446 break;
448 case EM_LINEINDEX16:
449 DPRINTF_EDIT_MSG16("EM_LINEINDEX");
450 if ((INT16)wParam == -1)
451 wParam = (WPARAM32)-1;
452 /* fall through */
453 case EM_LINEINDEX32:
454 DPRINTF_EDIT_MSG32("EM_LINEINDEX");
455 result = (LRESULT)EDIT_EM_LineIndex(wnd, es, (INT32)wParam);
456 break;
458 case EM_SETHANDLE16:
459 DPRINTF_EDIT_MSG16("EM_SETHANDLE");
460 EDIT_EM_SetHandle16(wnd, es, (HLOCAL16)wParam);
461 break;
462 case EM_SETHANDLE32:
463 DPRINTF_EDIT_MSG32("EM_SETHANDLE");
464 EDIT_EM_SetHandle(wnd, es, (HLOCAL32)wParam);
465 break;
467 case EM_GETHANDLE16:
468 DPRINTF_EDIT_MSG16("EM_GETHANDLE");
469 result = (LRESULT)EDIT_EM_GetHandle16(wnd, es);
470 break;
471 case EM_GETHANDLE32:
472 DPRINTF_EDIT_MSG32("EM_GETHANDLE");
473 result = (LRESULT)EDIT_EM_GetHandle(wnd, es);
474 break;
476 case EM_GETTHUMB16:
477 DPRINTF_EDIT_MSG16("EM_GETTHUMB");
478 /* fall through */
479 case EM_GETTHUMB32:
480 DPRINTF_EDIT_MSG32("EM_GETTHUMB");
481 result = EDIT_EM_GetThumb(wnd, es);
482 break;
484 /* messages 0x00bf and 0x00c0 missing from specs */
486 case WM_USER+15:
487 DPRINTF_EDIT_MSG16("undocumented WM_USER+15, please report");
488 /* fall through */
489 case 0x00bf:
490 DPRINTF_EDIT_MSG32("undocumented 0x00bf, please report");
491 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
492 break;
494 case WM_USER+16:
495 DPRINTF_EDIT_MSG16("undocumented WM_USER+16, please report");
496 /* fall through */
497 case 0x00c0:
498 DPRINTF_EDIT_MSG32("undocumented 0x00c0, please report");
499 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
500 break;
502 case EM_LINELENGTH16:
503 DPRINTF_EDIT_MSG16("EM_LINELENGTH");
504 /* fall through */
505 case EM_LINELENGTH32:
506 DPRINTF_EDIT_MSG32("EM_LINELENGTH");
507 result = (LRESULT)EDIT_EM_LineLength(wnd, es, (INT32)wParam);
508 break;
510 case EM_REPLACESEL16:
511 DPRINTF_EDIT_MSG16("EM_REPLACESEL");
512 lParam = (LPARAM)PTR_SEG_TO_LIN((SEGPTR)lParam);
513 /* fall through */
514 case EM_REPLACESEL32:
515 DPRINTF_EDIT_MSG32("EM_REPLACESEL");
516 EDIT_EM_ReplaceSel(wnd, es, (BOOL32)wParam, (LPCSTR)lParam);
517 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 = DefWindowProc32A(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_GETLINE32:
534 DPRINTF_EDIT_MSG32("EM_GETLINE");
535 result = (LRESULT)EDIT_EM_GetLine(wnd, es, (INT32)wParam, (LPSTR)lParam);
536 break;
538 case EM_LIMITTEXT16:
539 DPRINTF_EDIT_MSG16("EM_LIMITTEXT");
540 /* fall through */
541 case EM_SETLIMITTEXT32:
542 DPRINTF_EDIT_MSG32("EM_SETLIMITTEXT");
543 EDIT_EM_SetLimitText(wnd, es, (INT32)wParam);
544 break;
546 case EM_CANUNDO16:
547 DPRINTF_EDIT_MSG16("EM_CANUNDO");
548 /* fall through */
549 case EM_CANUNDO32:
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_UNDO32:
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_FMTLINES32:
568 DPRINTF_EDIT_MSG32("EM_FMTLINES");
569 result = (LRESULT)EDIT_EM_FmtLines(wnd, es, (BOOL32)wParam);
570 break;
572 case EM_LINEFROMCHAR16:
573 DPRINTF_EDIT_MSG16("EM_LINEFROMCHAR");
574 /* fall through */
575 case EM_LINEFROMCHAR32:
576 DPRINTF_EDIT_MSG32("EM_LINEFROMCHAR");
577 result = (LRESULT)EDIT_EM_LineFromChar(wnd, es, (INT32)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 = DefWindowProc32A(hwnd, msg, wParam, lParam);
588 break;
590 case EM_SETTABSTOPS16:
591 DPRINTF_EDIT_MSG16("EM_SETTABSTOPS");
592 result = (LRESULT)EDIT_EM_SetTabStops16(wnd, es, (INT32)wParam, (LPINT16)PTR_SEG_TO_LIN((SEGPTR)lParam));
593 break;
594 case EM_SETTABSTOPS32:
595 DPRINTF_EDIT_MSG32("EM_SETTABSTOPS");
596 result = (LRESULT)EDIT_EM_SetTabStops(wnd, es, (INT32)wParam, (LPINT32)lParam);
597 break;
599 case EM_SETPASSWORDCHAR16:
600 DPRINTF_EDIT_MSG16("EM_SETPASSWORDCHAR");
601 /* fall through */
602 case EM_SETPASSWORDCHAR32:
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_EMPTYUNDOBUFFER32:
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_GETFIRSTVISIBLELINE32:
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_SETREADONLY32:
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_SETWORDBREAKPROC32:
644 DPRINTF_EDIT_MSG32("EM_SETWORDBREAKPROC");
645 EDIT_EM_SetWordBreakProc(wnd, es, (EDITWORDBREAKPROC32A)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_GETWORDBREAKPROC32:
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_GETPASSWORDCHAR32:
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_SETMARGINS32:
668 DPRINTF_EDIT_MSG32("EM_SETMARGINS");
669 EDIT_EM_SetMargins(wnd, es, (INT32)wParam, SLOWORD(lParam), SHIWORD(lParam));
670 break;
672 case EM_GETMARGINS32:
673 DPRINTF_EDIT_MSG32("EM_GETMARGINS");
674 result = MAKELONG(es->left_margin, es->right_margin);
675 break;
677 case EM_GETLIMITTEXT32:
678 DPRINTF_EDIT_MSG32("EM_GETLIMITTEXT");
679 result = es->buffer_limit;
680 break;
682 case EM_POSFROMCHAR32:
683 DPRINTF_EDIT_MSG32("EM_POSFROMCHAR");
684 result = EDIT_EM_PosFromChar(wnd, es, (INT32)wParam, FALSE);
685 break;
687 case EM_CHARFROMPOS32:
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), (HWND32)lParam);
712 break;
714 case WM_CONTEXTMENU:
715 DPRINTF_EDIT_MSG32("WM_CONTEXTMENU");
716 EDIT_WM_ContextMenu(wnd, es, (HWND32)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_CUT:
725 DPRINTF_EDIT_MSG32("WM_CUT");
726 EDIT_WM_Cut(wnd, es);
727 break;
729 case WM_ENABLE:
730 DPRINTF_EDIT_MSG32("WM_ENABLE");
731 InvalidateRect32(hwnd, NULL, TRUE);
732 break;
734 case WM_ERASEBKGND:
735 DPRINTF_EDIT_MSG32("WM_ERASEBKGND");
736 result = EDIT_WM_EraseBkGnd(wnd, es, (HDC32)wParam);
737 break;
739 case WM_GETFONT:
740 DPRINTF_EDIT_MSG32("WM_GETFONT");
741 result = (LRESULT)es->font;
742 break;
744 case WM_GETTEXT:
745 DPRINTF_EDIT_MSG32("WM_GETTEXT");
746 result = (LRESULT)EDIT_WM_GetText(wnd, es, (INT32)wParam, (LPSTR)lParam);
747 break;
749 case WM_GETTEXTLENGTH:
750 DPRINTF_EDIT_MSG32("WM_GETTEXTLENGTH");
751 result = lstrlen32A(es->text);
752 break;
754 case WM_HSCROLL:
755 DPRINTF_EDIT_MSG32("WM_HSCROLL");
756 result = EDIT_WM_HScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND32)lParam);
757 break;
759 case WM_KEYDOWN:
760 DPRINTF_EDIT_MSG32("WM_KEYDOWN");
761 result = EDIT_WM_KeyDown(wnd, es, (INT32)wParam, (DWORD)lParam);
762 break;
764 case WM_KILLFOCUS:
765 DPRINTF_EDIT_MSG32("WM_KILLFOCUS");
766 result = EDIT_WM_KillFocus(wnd, es, (HWND32)wParam);
767 break;
769 case WM_LBUTTONDBLCLK:
770 DPRINTF_EDIT_MSG32("WM_LBUTTONDBLCLK");
771 result = EDIT_WM_LButtonDblClk(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
772 break;
774 case WM_LBUTTONDOWN:
775 DPRINTF_EDIT_MSG32("WM_LBUTTONDOWN");
776 result = EDIT_WM_LButtonDown(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
777 break;
779 case WM_LBUTTONUP:
780 DPRINTF_EDIT_MSG32("WM_LBUTTONUP");
781 result = EDIT_WM_LButtonUp(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
782 break;
784 case WM_MOUSEACTIVATE:
786 * FIXME: maybe DefWindowProc() screws up, but it seems that
787 * modalless dialog boxes need this. If we don't do this, the focus
788 * will _not_ be set by DefWindowProc() for edit controls in a
789 * modalless dialog box ???
791 DPRINTF_EDIT_MSG32("WM_MOUSEACTIVATE");
792 SetFocus32(wnd->hwndSelf);
793 result = MA_ACTIVATE;
794 break;
796 case WM_MOUSEMOVE:
798 * DPRINTF_EDIT_MSG32("WM_MOUSEMOVE");
800 result = EDIT_WM_MouseMove(wnd, es, (DWORD)wParam, SLOWORD(lParam), SHIWORD(lParam));
801 break;
803 case WM_PAINT:
804 DPRINTF_EDIT_MSG32("WM_PAINT");
805 EDIT_WM_Paint(wnd, es);
806 break;
808 case WM_PASTE:
809 DPRINTF_EDIT_MSG32("WM_PASTE");
810 EDIT_WM_Paste(wnd, es);
811 break;
813 case WM_SETFOCUS:
814 DPRINTF_EDIT_MSG32("WM_SETFOCUS");
815 EDIT_WM_SetFocus(wnd, es, (HWND32)wParam);
816 break;
818 case WM_SETFONT:
819 DPRINTF_EDIT_MSG32("WM_SETFONT");
820 EDIT_WM_SetFont(wnd, es, (HFONT32)wParam, LOWORD(lParam) != 0);
821 break;
823 case WM_SETTEXT:
824 DPRINTF_EDIT_MSG32("WM_SETTEXT");
825 EDIT_WM_SetText(wnd, es, (LPCSTR)lParam);
826 result = TRUE;
827 break;
829 case WM_SIZE:
830 DPRINTF_EDIT_MSG32("WM_SIZE");
831 EDIT_WM_Size(wnd, es, (UINT32)wParam, LOWORD(lParam), HIWORD(lParam));
832 break;
834 case WM_SYSKEYDOWN:
835 DPRINTF_EDIT_MSG32("WM_SYSKEYDOWN");
836 result = EDIT_WM_SysKeyDown(wnd, es, (INT32)wParam, (DWORD)lParam);
837 break;
839 case WM_TIMER:
840 DPRINTF_EDIT_MSG32("WM_TIMER");
841 EDIT_WM_Timer(wnd, es, (INT32)wParam, (TIMERPROC32)lParam);
842 break;
844 case WM_VSCROLL:
845 DPRINTF_EDIT_MSG32("WM_VSCROLL");
846 result = EDIT_WM_VScroll(wnd, es, LOWORD(wParam), SHIWORD(wParam), (HWND32)(lParam));
847 break;
849 default:
850 result = DefWindowProc32A(hwnd, msg, wParam, lParam);
851 break;
853 EDIT_UnlockBuffer(wnd, es, FALSE);
854 return result;
858 /*********************************************************************
860 * EDIT_BuildLineDefs_ML
862 * Build linked list of text lines.
863 * Lines can end with '\0' (last line), a character (if it is wrapped),
864 * a soft return '\r\r\n' or a hard return '\r\n'
867 static void EDIT_BuildLineDefs_ML(WND *wnd, EDITSTATE *es)
869 HDC32 dc;
870 HFONT32 old_font = 0;
871 LPSTR start, cp;
872 INT32 fw;
873 LINEDEF *current_def;
874 LINEDEF **previous_next;
876 current_def = es->first_line_def;
877 do {
878 LINEDEF *next_def = current_def->next;
879 HeapFree(es->heap, 0, current_def);
880 current_def = next_def;
881 } while (current_def);
882 es->line_count = 0;
883 es->text_width = 0;
885 dc = GetDC32(wnd->hwndSelf);
886 if (es->font)
887 old_font = SelectObject32(dc, es->font);
889 fw = es->format_rect.right - es->format_rect.left;
890 start = es->text;
891 previous_next = &es->first_line_def;
892 do {
893 current_def = HeapAlloc(es->heap, 0, sizeof(LINEDEF));
894 current_def->next = NULL;
895 cp = start;
896 while (*cp) {
897 if ((*cp == '\r') && (*(cp + 1) == '\n'))
898 break;
899 cp++;
901 if (!(*cp)) {
902 current_def->ending = END_0;
903 current_def->net_length = lstrlen32A(start);
904 } else if ((cp > start) && (*(cp - 1) == '\r')) {
905 current_def->ending = END_SOFT;
906 current_def->net_length = cp - start - 1;
907 } else {
908 current_def->ending = END_HARD;
909 current_def->net_length = cp - start;
911 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
912 start, current_def->net_length,
913 es->tabs_count, es->tabs));
914 /* FIXME: check here for lines that are too wide even in AUTOHSCROLL (> 32767 ???) */
915 if ((!(es->style & ES_AUTOHSCROLL)) && (current_def->width > fw)) {
916 INT32 next = 0;
917 INT32 prev;
918 do {
919 prev = next;
920 next = EDIT_CallWordBreakProc(wnd, es, start - es->text,
921 prev + 1, current_def->net_length, WB_RIGHT);
922 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
923 start, next, es->tabs_count, es->tabs));
924 } while (current_def->width <= fw);
925 if (!prev) {
926 next = 0;
927 do {
928 prev = next;
929 next++;
930 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc,
931 start, next, es->tabs_count, es->tabs));
932 } while (current_def->width <= fw);
933 if (!prev)
934 prev = 1;
936 current_def->net_length = prev;
937 current_def->ending = END_WRAP;
938 current_def->width = (INT32)LOWORD(GetTabbedTextExtent32A(dc, start,
939 current_def->net_length, es->tabs_count, es->tabs));
941 switch (current_def->ending) {
942 case END_SOFT:
943 current_def->length = current_def->net_length + 3;
944 break;
945 case END_HARD:
946 current_def->length = current_def->net_length + 2;
947 break;
948 case END_WRAP:
949 case END_0:
950 current_def->length = current_def->net_length;
951 break;
953 es->text_width = MAX(es->text_width, current_def->width);
954 start += current_def->length;
955 *previous_next = current_def;
956 previous_next = &current_def->next;
957 es->line_count++;
958 } while (current_def->ending != END_0);
959 if (es->font)
960 SelectObject32(dc, old_font);
961 ReleaseDC32(wnd->hwndSelf, dc);
965 /*********************************************************************
967 * EDIT_CallWordBreakProc
969 * Call appropriate WordBreakProc (internal or external).
971 * Note: The "start" argument should always be an index refering
972 * to es->text. The actual wordbreak proc might be
973 * 16 bit, so we can't always pass any 32 bit LPSTR.
974 * Hence we assume that es->text is the buffer that holds
975 * the string under examination (we can decide this for ourselves).
978 static INT32 EDIT_CallWordBreakProc(WND *wnd, EDITSTATE *es, INT32 start, INT32 index, INT32 count, INT32 action)
980 if (es->word_break_proc16) {
981 HLOCAL16 hloc16 = EDIT_EM_GetHandle16(wnd, es);
982 SEGPTR segptr = LocalLock16(hloc16);
983 INT32 ret = (INT32)Callbacks->CallWordBreakProc(es->word_break_proc16,
984 segptr + start, index, count, action);
985 LocalUnlock16(hloc16);
986 return ret;
988 else if (es->word_break_proc32A)
990 dprintf_relay( stddeb, "CallTo32(wordbrk=%p,str='%s',idx=%d,cnt=%d,act=%d)\n",
991 es->word_break_proc32A, es->text + start, index,
992 count, action );
993 return (INT32)es->word_break_proc32A( es->text + start, index,
994 count, action );
996 else
997 return EDIT_WordBreakProc(es->text + start, index, count, action);
1001 /*********************************************************************
1003 * EDIT_CharFromPos
1005 * Beware: This is not the function called on EM_CHARFROMPOS
1006 * The position _can_ be outside the formatting / client
1007 * rectangle
1008 * The return value is only the character index
1011 static INT32 EDIT_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y, LPBOOL32 after_wrap)
1013 INT32 index;
1014 HDC32 dc;
1015 HFONT32 old_font = 0;
1017 if (es->style & ES_MULTILINE) {
1018 INT32 line = (y - es->format_rect.top) / es->line_height + es->y_offset;
1019 INT32 line_index = 0;
1020 LINEDEF *line_def = es->first_line_def;
1021 INT32 low, high;
1022 while ((line > 0) && line_def->next) {
1023 line_index += line_def->length;
1024 line_def = line_def->next;
1025 line--;
1027 x += es->x_offset - es->format_rect.left;
1028 if (x >= line_def->width) {
1029 if (after_wrap)
1030 *after_wrap = (line_def->ending == END_WRAP);
1031 return line_index + line_def->net_length;
1033 if (x <= 0) {
1034 if (after_wrap)
1035 *after_wrap = FALSE;
1036 return line_index;
1038 dc = GetDC32(wnd->hwndSelf);
1039 if (es->font)
1040 old_font = SelectObject32(dc, es->font);
1041 low = line_index + 1;
1042 high = line_index + line_def->net_length + 1;
1043 while (low < high - 1)
1045 INT32 mid = (low + high) / 2;
1046 if (LOWORD(GetTabbedTextExtent32A(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
1047 else low = mid;
1049 index = low;
1051 if (after_wrap)
1052 *after_wrap = ((index == line_index + line_def->net_length) &&
1053 (line_def->ending == END_WRAP));
1054 } else {
1055 LPSTR text;
1056 SIZE32 size;
1057 if (after_wrap)
1058 *after_wrap = FALSE;
1059 x -= es->format_rect.left;
1060 if (!x)
1061 return es->x_offset;
1062 text = EDIT_GetPasswordPointer_SL(wnd, es);
1063 dc = GetDC32(wnd->hwndSelf);
1064 if (es->font)
1065 old_font = SelectObject32(dc, es->font);
1066 if (x < 0)
1068 INT32 low = 0;
1069 INT32 high = es->x_offset;
1070 while (low < high - 1)
1072 INT32 mid = (low + high) / 2;
1073 GetTextExtentPoint32A( dc, text + mid,
1074 es->x_offset - mid, &size );
1075 if (size.cx > -x) low = mid;
1076 else high = mid;
1078 index = low;
1080 else
1082 INT32 low = es->x_offset;
1083 INT32 high = lstrlen32A(es->text) + 1;
1084 while (low < high - 1)
1086 INT32 mid = (low + high) / 2;
1087 GetTextExtentPoint32A( dc, text + es->x_offset,
1088 mid - es->x_offset, &size );
1089 if (size.cx > x) high = mid;
1090 else low = mid;
1092 index = low;
1094 if (es->style & ES_PASSWORD)
1095 HeapFree(es->heap, 0 ,text);
1097 if (es->font)
1098 SelectObject32(dc, old_font);
1099 ReleaseDC32(wnd->hwndSelf, dc);
1100 return index;
1104 /*********************************************************************
1106 * EDIT_ConfinePoint
1108 * adjusts the point to be within the formatting rectangle
1109 * (so CharFromPos returns the nearest _visible_ character)
1112 static void EDIT_ConfinePoint(WND *wnd, EDITSTATE *es, LPINT32 x, LPINT32 y)
1114 *x = MIN(MAX(*x, es->format_rect.left), es->format_rect.right - 1);
1115 *y = MIN(MAX(*y, es->format_rect.top), es->format_rect.bottom - 1);
1119 /*********************************************************************
1121 * EDIT_GetLineRect
1123 * Calculates the bounding rectangle for a line from a starting
1124 * column to an ending column.
1127 static void EDIT_GetLineRect(WND *wnd, EDITSTATE *es, INT32 line, INT32 scol, INT32 ecol, LPRECT32 rc)
1129 INT32 line_index = EDIT_EM_LineIndex(wnd, es, line);
1131 if (es->style & ES_MULTILINE)
1132 rc->top = es->format_rect.top + (line - es->y_offset) * es->line_height;
1133 else
1134 rc->top = es->format_rect.top;
1135 rc->bottom = rc->top + es->line_height;
1136 rc->left = (scol == 0) ? es->format_rect.left : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + scol, TRUE));
1137 rc->right = (ecol == -1) ? es->format_rect.right : SLOWORD(EDIT_EM_PosFromChar(wnd, es, line_index + ecol, TRUE));
1141 /*********************************************************************
1143 * EDIT_GetPasswordPointer_SL
1145 * note: caller should free the (optionally) allocated buffer
1148 static LPSTR EDIT_GetPasswordPointer_SL(WND *wnd, EDITSTATE *es)
1150 if (es->style & ES_PASSWORD) {
1151 INT32 len = lstrlen32A(es->text);
1152 LPSTR text = HeapAlloc(es->heap, 0, len + 1);
1153 RtlFillMemory(text, len, es->password_char);
1154 text[len] = '\0';
1155 return text;
1156 } else
1157 return es->text;
1161 /*********************************************************************
1163 * EDIT_LockBuffer
1165 * This acts as a LOCAL_Lock(), but it locks only once. This way
1166 * you can call it whenever you like, without unlocking.
1169 static void EDIT_LockBuffer(WND *wnd, EDITSTATE *es)
1171 if (!es) {
1172 fprintf(stderr, "edit: LockBuffer() without an EDITSTATE ... please report\n");
1173 return;
1175 if (!(es->style & ES_MULTILINE))
1176 return;
1177 if (!es->text) {
1178 if (es->hloc32)
1179 es->text = LocalLock32(es->hloc32);
1180 else if (es->hloc16)
1181 es->text = LOCAL_Lock(wnd->hInstance, es->hloc16);
1182 else {
1183 fprintf(stderr, "edit: LockBuffer() without a buffer ... please report\n");
1184 return;
1187 es->lock_count++;
1191 /*********************************************************************
1193 * EDIT_SL_InvalidateText
1195 * Called from EDIT_InvalidateText().
1196 * Does the job for single-line controls only.
1199 static void EDIT_SL_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1201 RECT32 line_rect;
1202 RECT32 rc;
1204 EDIT_GetLineRect(wnd, es, 0, start, end, &line_rect);
1205 if (IntersectRect32(&rc, &line_rect, &es->format_rect))
1206 InvalidateRect32(wnd->hwndSelf, &rc, FALSE);
1210 /*********************************************************************
1212 * EDIT_ML_InvalidateText
1214 * Called from EDIT_InvalidateText().
1215 * Does the job for multi-line controls only.
1218 static void EDIT_ML_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1220 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1221 INT32 sl = EDIT_EM_LineFromChar(wnd, es, start);
1222 INT32 el = EDIT_EM_LineFromChar(wnd, es, end);
1223 INT32 sc;
1224 INT32 ec;
1225 RECT32 rc1;
1226 RECT32 rcWnd;
1227 RECT32 rcLine;
1228 RECT32 rcUpdate;
1229 INT32 l;
1231 if ((el < es->y_offset) || (sl > es->y_offset + vlc))
1232 return;
1234 sc = start - EDIT_EM_LineIndex(wnd, es, sl);
1235 ec = end - EDIT_EM_LineIndex(wnd, es, el);
1236 if (sl < es->y_offset) {
1237 sl = es->y_offset;
1238 sc = 0;
1240 if (el > es->y_offset + vlc) {
1241 el = es->y_offset + vlc;
1242 ec = EDIT_EM_LineLength(wnd, es, EDIT_EM_LineIndex(wnd, es, el));
1244 GetClientRect32(wnd->hwndSelf, &rc1);
1245 IntersectRect32(&rcWnd, &rc1, &es->format_rect);
1246 if (sl == el) {
1247 EDIT_GetLineRect(wnd, es, sl, sc, ec, &rcLine);
1248 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1249 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1250 } else {
1251 EDIT_GetLineRect(wnd, es, sl, sc,
1252 EDIT_EM_LineLength(wnd, es,
1253 EDIT_EM_LineIndex(wnd, es, sl)),
1254 &rcLine);
1255 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1256 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1257 for (l = sl + 1 ; l < el ; l++) {
1258 EDIT_GetLineRect(wnd, es, l, 0,
1259 EDIT_EM_LineLength(wnd, es,
1260 EDIT_EM_LineIndex(wnd, es, l)),
1261 &rcLine);
1262 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1263 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1265 EDIT_GetLineRect(wnd, es, el, 0, ec, &rcLine);
1266 if (IntersectRect32(&rcUpdate, &rcWnd, &rcLine))
1267 InvalidateRect32(wnd->hwndSelf, &rcUpdate, FALSE);
1272 /*********************************************************************
1274 * EDIT_InvalidateText
1276 * Invalidate the text from offset start upto, but not including,
1277 * offset end. Useful for (re)painting the selection.
1278 * Regions outside the linewidth are not invalidated.
1279 * end == -1 means end == TextLength.
1280 * start and end need not be ordered.
1283 static void EDIT_InvalidateText(WND *wnd, EDITSTATE *es, INT32 start, INT32 end)
1285 if (end == start)
1286 return;
1288 if (end == -1)
1289 end = lstrlen32A(es->text);
1291 ORDER_INT32(start, end);
1293 if (es->style & ES_MULTILINE)
1294 EDIT_ML_InvalidateText(wnd, es, start, end);
1295 else
1296 EDIT_SL_InvalidateText(wnd, es, start, end);
1300 /*********************************************************************
1302 * EDIT_MakeFit
1304 * Try to fit size + 1 bytes in the buffer. Constrain to limits.
1307 static BOOL32 EDIT_MakeFit(WND *wnd, EDITSTATE *es, INT32 size)
1309 HLOCAL32 hNew32;
1310 HLOCAL16 hNew16;
1312 if (size <= es->buffer_size)
1313 return TRUE;
1314 if (size > es->buffer_limit) {
1315 EDIT_NOTIFY_PARENT(wnd, EN_MAXTEXT, "EN_MAXTEXT");
1316 return FALSE;
1318 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1319 if (size > es->buffer_limit)
1320 size = es->buffer_limit;
1322 dprintf_edit(stddeb, "edit: EDIT_MakeFit: trying to ReAlloc to %d+1\n", size);
1324 EDIT_UnlockBuffer(wnd, es, TRUE);
1325 if (es->text) {
1326 if ((es->text = HeapReAlloc(es->heap, 0, es->text, size + 1)))
1327 es->buffer_size = MIN(HeapSize(es->heap, 0, es->text) - 1, es->buffer_limit);
1328 else
1329 es->buffer_size = 0;
1330 } else if (es->hloc32) {
1331 if ((hNew32 = LocalReAlloc32(es->hloc32, size + 1, 0))) {
1332 dprintf_edit(stddeb, "edit: EDIT_MakeFit: Old 32 bit handle %08x, new handle %08x\n", es->hloc32, hNew32);
1333 es->hloc32 = hNew32;
1334 es->buffer_size = MIN(LocalSize32(es->hloc32) - 1, es->buffer_limit);
1336 } else if (es->hloc16) {
1337 if ((hNew16 = LOCAL_ReAlloc(wnd->hInstance, es->hloc16, size + 1, LMEM_MOVEABLE))) {
1338 dprintf_edit(stddeb, "edit: EDIT_MakeFit: Old 16 bit handle %08x, new handle %08x\n", es->hloc16, hNew16);
1339 es->hloc16 = hNew16;
1340 es->buffer_size = MIN(LOCAL_Size(wnd->hInstance, es->hloc16) - 1, es->buffer_limit);
1343 if (es->buffer_size < size) {
1344 EDIT_LockBuffer(wnd, es);
1345 dprintf_edit(stddeb, "edit: EDIT_MakeFit: FAILED ! We now have %d+1\n", es->buffer_size);
1346 EDIT_NOTIFY_PARENT(wnd, EN_ERRSPACE, "EN_ERRSPACE");
1347 return FALSE;
1348 } else {
1349 EDIT_LockBuffer(wnd, es);
1350 dprintf_edit(stddeb, "edit: EDIT_MakeFit: We now have %d+1\n", es->buffer_size);
1351 return TRUE;
1356 /*********************************************************************
1358 * EDIT_MakeUndoFit
1360 * Try to fit size + 1 bytes in the undo buffer.
1363 static BOOL32 EDIT_MakeUndoFit(WND *wnd, EDITSTATE *es, INT32 size)
1365 if (size <= es->undo_buffer_size)
1366 return TRUE;
1367 size = ((size / GROWLENGTH) + 1) * GROWLENGTH;
1369 dprintf_edit(stddeb, "edit: EDIT_MakeUndoFit: trying to ReAlloc to %d+1\n", size);
1371 if ((es->undo_text = HeapReAlloc(es->heap, 0, es->undo_text, size + 1))) {
1372 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
1373 if (es->undo_buffer_size < size) {
1374 dprintf_edit(stddeb, "edit: EDIT_MakeUndoFit: FAILED ! We now have %d+1\n", es->undo_buffer_size);
1375 return FALSE;
1377 return TRUE;
1379 return FALSE;
1383 /*********************************************************************
1385 * EDIT_MoveBackward
1388 static void EDIT_MoveBackward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1390 INT32 e = es->selection_end;
1392 if (e) {
1393 e--;
1394 if ((es->style & ES_MULTILINE) && e &&
1395 (es->text[e - 1] == '\r') && (es->text[e] == '\n')) {
1396 e--;
1397 if (e && (es->text[e - 1] == '\r'))
1398 e--;
1401 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1402 EDIT_EM_ScrollCaret(wnd, es);
1406 /*********************************************************************
1408 * EDIT_MoveDown_ML
1410 * Only for multi line controls
1411 * Move the caret one line down, on a column with the nearest
1412 * x coordinate on the screen (might be a different column).
1415 static void EDIT_MoveDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1417 INT32 s = es->selection_start;
1418 INT32 e = es->selection_end;
1419 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1420 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1421 INT32 x = SLOWORD(pos);
1422 INT32 y = SHIWORD(pos);
1424 e = EDIT_CharFromPos(wnd, es, x, y + es->line_height, &after_wrap);
1425 if (!extend)
1426 s = e;
1427 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1428 EDIT_EM_ScrollCaret(wnd, es);
1432 /*********************************************************************
1434 * EDIT_MoveEnd
1437 static void EDIT_MoveEnd(WND *wnd, EDITSTATE *es, BOOL32 extend)
1439 BOOL32 after_wrap = FALSE;
1440 INT32 e;
1442 if (es->style & ES_MULTILINE)
1443 e = EDIT_CharFromPos(wnd, es, 0x7fffffff,
1444 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), &after_wrap);
1445 else
1446 e = lstrlen32A(es->text);
1447 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, after_wrap);
1448 EDIT_EM_ScrollCaret(wnd, es);
1452 /*********************************************************************
1454 * EDIT_MoveForward
1457 static void EDIT_MoveForward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1459 INT32 e = es->selection_end;
1461 if (es->text[e]) {
1462 e++;
1463 if ((es->style & ES_MULTILINE) && (es->text[e - 1] == '\r')) {
1464 if (es->text[e] == '\n')
1465 e++;
1466 else if ((es->text[e] == '\r') && (es->text[e + 1] == '\n'))
1467 e += 2;
1470 EDIT_EM_SetSel(wnd, es, extend ? es->selection_start : e, e, FALSE);
1471 EDIT_EM_ScrollCaret(wnd, es);
1475 /*********************************************************************
1477 * EDIT_MoveHome
1479 * Home key: move to beginning of line.
1482 static void EDIT_MoveHome(WND *wnd, EDITSTATE *es, BOOL32 extend)
1484 INT32 e;
1486 if (es->style & ES_MULTILINE)
1487 e = EDIT_CharFromPos(wnd, es, 0x80000000,
1488 HIWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP)), NULL);
1489 else
1490 e = 0;
1491 EDIT_EM_SetSel(wnd, es, e, extend ? es->selection_start : e, FALSE);
1492 EDIT_EM_ScrollCaret(wnd, es);
1496 /*********************************************************************
1498 * EDIT_MovePageDown_ML
1500 * Only for multi line controls
1501 * Move the caret one page down, on a column with the nearest
1502 * x coordinate on the screen (might be a different column).
1505 static void EDIT_MovePageDown_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1507 INT32 s = es->selection_start;
1508 INT32 e = es->selection_end;
1509 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1510 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1511 INT32 x = SLOWORD(pos);
1512 INT32 y = SHIWORD(pos);
1514 e = EDIT_CharFromPos(wnd, es, x,
1515 y + (es->format_rect.bottom - es->format_rect.top),
1516 &after_wrap);
1517 if (!extend)
1518 s = e;
1519 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1520 EDIT_EM_ScrollCaret(wnd, es);
1524 /*********************************************************************
1526 * EDIT_MovePageUp_ML
1528 * Only for multi line controls
1529 * Move the caret one page up, on a column with the nearest
1530 * x coordinate on the screen (might be a different column).
1533 static void EDIT_MovePageUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1535 INT32 s = es->selection_start;
1536 INT32 e = es->selection_end;
1537 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1538 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1539 INT32 x = SLOWORD(pos);
1540 INT32 y = SHIWORD(pos);
1542 e = EDIT_CharFromPos(wnd, es, x,
1543 y - (es->format_rect.bottom - es->format_rect.top),
1544 &after_wrap);
1545 if (!extend)
1546 s = e;
1547 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1548 EDIT_EM_ScrollCaret(wnd, es);
1552 /*********************************************************************
1554 * EDIT_MoveUp_ML
1556 * Only for multi line controls
1557 * Move the caret one line up, on a column with the nearest
1558 * x coordinate on the screen (might be a different column).
1561 static void EDIT_MoveUp_ML(WND *wnd, EDITSTATE *es, BOOL32 extend)
1563 INT32 s = es->selection_start;
1564 INT32 e = es->selection_end;
1565 BOOL32 after_wrap = (es->flags & EF_AFTER_WRAP);
1566 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, e, after_wrap);
1567 INT32 x = SLOWORD(pos);
1568 INT32 y = SHIWORD(pos);
1570 e = EDIT_CharFromPos(wnd, es, x, y - es->line_height, &after_wrap);
1571 if (!extend)
1572 s = e;
1573 EDIT_EM_SetSel(wnd, es, s, e, after_wrap);
1574 EDIT_EM_ScrollCaret(wnd, es);
1578 /*********************************************************************
1580 * EDIT_MoveWordBackward
1583 static void EDIT_MoveWordBackward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1585 INT32 s = es->selection_start;
1586 INT32 e = es->selection_end;
1587 INT32 l;
1588 INT32 ll;
1589 INT32 li;
1591 l = EDIT_EM_LineFromChar(wnd, es, e);
1592 ll = EDIT_EM_LineLength(wnd, es, e);
1593 li = EDIT_EM_LineIndex(wnd, es, l);
1594 if (e - li == 0) {
1595 if (l) {
1596 li = EDIT_EM_LineIndex(wnd, es, l - 1);
1597 e = li + EDIT_EM_LineLength(wnd, es, li);
1599 } else {
1600 e = li + (INT32)EDIT_CallWordBreakProc(wnd, es,
1601 li, e - li, ll, WB_LEFT);
1603 if (!extend)
1604 s = e;
1605 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1606 EDIT_EM_ScrollCaret(wnd, es);
1610 /*********************************************************************
1612 * EDIT_MoveWordForward
1615 static void EDIT_MoveWordForward(WND *wnd, EDITSTATE *es, BOOL32 extend)
1617 INT32 s = es->selection_start;
1618 INT32 e = es->selection_end;
1619 INT32 l;
1620 INT32 ll;
1621 INT32 li;
1623 l = EDIT_EM_LineFromChar(wnd, es, e);
1624 ll = EDIT_EM_LineLength(wnd, es, e);
1625 li = EDIT_EM_LineIndex(wnd, es, l);
1626 if (e - li == ll) {
1627 if ((es->style & ES_MULTILINE) && (l != es->line_count - 1))
1628 e = EDIT_EM_LineIndex(wnd, es, l + 1);
1629 } else {
1630 e = li + EDIT_CallWordBreakProc(wnd, es,
1631 li, e - li + 1, ll, WB_RIGHT);
1633 if (!extend)
1634 s = e;
1635 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
1636 EDIT_EM_ScrollCaret(wnd, es);
1640 /*********************************************************************
1642 * EDIT_PaintLine
1645 static void EDIT_PaintLine(WND *wnd, EDITSTATE *es, HDC32 dc, INT32 line, BOOL32 rev)
1647 INT32 s = es->selection_start;
1648 INT32 e = es->selection_end;
1649 INT32 li;
1650 INT32 ll;
1651 INT32 x;
1652 INT32 y;
1653 LRESULT pos;
1655 if (es->style & ES_MULTILINE) {
1656 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1657 if ((line < es->y_offset) || (line > es->y_offset + vlc) || (line >= es->line_count))
1658 return;
1659 } else if (line)
1660 return;
1662 dprintf_edit(stddeb, "edit: EDIT_PaintLine: line=%d\n", line);
1664 pos = EDIT_EM_PosFromChar(wnd, es, EDIT_EM_LineIndex(wnd, es, line), FALSE);
1665 x = SLOWORD(pos);
1666 y = SHIWORD(pos);
1667 li = EDIT_EM_LineIndex(wnd, es, line);
1668 ll = EDIT_EM_LineLength(wnd, es, li);
1669 s = es->selection_start;
1670 e = es->selection_end;
1671 ORDER_INT32(s, e);
1672 s = MIN(li + ll, MAX(li, s));
1673 e = MIN(li + ll, MAX(li, e));
1674 if (rev && (s != e) &&
1675 ((es->flags & EF_FOCUSED) || (es->style & ES_NOHIDESEL))) {
1676 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, s - li, FALSE);
1677 x += EDIT_PaintText(wnd, es, dc, x, y, line, s - li, e - s, TRUE);
1678 x += EDIT_PaintText(wnd, es, dc, x, y, line, e - li, li + ll - e, FALSE);
1679 } else
1680 x += EDIT_PaintText(wnd, es, dc, x, y, line, 0, ll, FALSE);
1684 /*********************************************************************
1686 * EDIT_PaintText
1689 static INT32 EDIT_PaintText(WND *wnd, EDITSTATE *es, HDC32 dc, INT32 x, INT32 y, INT32 line, INT32 col, INT32 count, BOOL32 rev)
1691 COLORREF BkColor;
1692 COLORREF TextColor;
1693 INT32 ret;
1694 INT32 li;
1695 SIZE32 size;
1697 if (!count)
1698 return 0;
1699 BkColor = GetBkColor32(dc);
1700 TextColor = GetTextColor32(dc);
1701 if (rev) {
1702 SetBkColor32(dc, GetSysColor32(COLOR_HIGHLIGHT));
1703 SetTextColor32(dc, GetSysColor32(COLOR_HIGHLIGHTTEXT));
1705 li = EDIT_EM_LineIndex(wnd, es, line);
1706 if (es->style & ES_MULTILINE) {
1707 ret = (INT32)LOWORD(TabbedTextOut32A(dc, x, y, es->text + li + col, count,
1708 es->tabs_count, es->tabs, es->format_rect.left - es->x_offset));
1709 } else {
1710 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
1711 TextOut32A(dc, x, y, text + li + col, count);
1712 GetTextExtentPoint32A(dc, text + li + col, count, &size);
1713 ret = size.cx;
1714 if (es->style & ES_PASSWORD)
1715 HeapFree(es->heap, 0, text);
1717 if (rev) {
1718 SetBkColor32(dc, BkColor);
1719 SetTextColor32(dc, TextColor);
1721 return ret;
1725 /*********************************************************************
1727 * EM_SCROLLCARET
1730 static void EDIT_EM_ScrollCaret(WND *wnd, EDITSTATE *es)
1732 if (es->style & ES_MULTILINE) {
1733 INT32 l;
1734 INT32 li;
1735 INT32 vlc;
1736 INT32 ww;
1737 INT32 cw = es->char_width;
1738 INT32 x;
1739 INT32 dy = 0;
1740 INT32 dx = 0;
1742 l = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
1743 li = EDIT_EM_LineIndex(wnd, es, l);
1744 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP));
1745 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
1746 if (l >= es->y_offset + vlc)
1747 dy = l - vlc + 1 - es->y_offset;
1748 if (l < es->y_offset)
1749 dy = l - es->y_offset;
1750 ww = es->format_rect.right - es->format_rect.left;
1751 if (x < es->format_rect.left)
1752 dx = x - es->format_rect.left - ww / HSCROLL_FRACTION / cw * cw;
1753 if (x > es->format_rect.right)
1754 dx = x - es->format_rect.left - (HSCROLL_FRACTION - 1) * ww / HSCROLL_FRACTION / cw * cw;
1755 if (dy || dx)
1756 EDIT_EM_LineScroll(wnd, es, dx, dy);
1757 } else {
1758 INT32 x;
1759 INT32 goal;
1760 INT32 format_width;
1762 if (!(es->style & ES_AUTOHSCROLL))
1763 return;
1765 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
1766 format_width = es->format_rect.right - es->format_rect.left;
1767 if (x < es->format_rect.left) {
1768 goal = es->format_rect.left + format_width / HSCROLL_FRACTION;
1769 do {
1770 es->x_offset--;
1771 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
1772 } while ((x < goal) && es->x_offset);
1773 /* FIXME: use ScrollWindow() somehow to improve performance */
1774 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
1775 } else if (x > es->format_rect.right) {
1776 INT32 x_last;
1777 INT32 len = lstrlen32A(es->text);
1778 goal = es->format_rect.right - format_width / HSCROLL_FRACTION;
1779 do {
1780 es->x_offset++;
1781 x = SLOWORD(EDIT_EM_PosFromChar(wnd, es, es->selection_end, FALSE));
1782 x_last = SLOWORD(EDIT_EM_PosFromChar(wnd, es, len, FALSE));
1783 } while ((x > goal) && (x_last > es->format_rect.right));
1784 /* FIXME: use ScrollWindow() somehow to improve performance */
1785 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
1791 /*********************************************************************
1793 * EDIT_SetRectNP
1795 * note: this is not (exactly) the handler called on EM_SETRECTNP
1796 * it is also used to set the rect of a single line control
1799 static void EDIT_SetRectNP(WND *wnd, EDITSTATE *es, LPRECT32 rc)
1801 CopyRect32(&es->format_rect, rc);
1802 if (es->style & WS_BORDER) {
1803 INT32 bw = GetSystemMetrics32(SM_CXBORDER) + 1;
1804 es->format_rect.left += bw;
1805 es->format_rect.top += bw;
1806 es->format_rect.right -= bw;
1807 es->format_rect.bottom -= bw;
1809 es->format_rect.left += es->left_margin;
1810 es->format_rect.right -= es->right_margin;
1811 es->format_rect.right = MAX(es->format_rect.right, es->format_rect.left + es->char_width);
1812 if (es->style & ES_MULTILINE)
1813 es->format_rect.bottom = es->format_rect.top +
1814 MAX(1, (es->format_rect.bottom - es->format_rect.top) / es->line_height) * es->line_height;
1815 else
1816 es->format_rect.bottom = es->format_rect.top + es->line_height;
1817 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL))
1818 EDIT_BuildLineDefs_ML(wnd, es);
1822 /*********************************************************************
1824 * EDIT_UnlockBuffer
1827 static void EDIT_UnlockBuffer(WND *wnd, EDITSTATE *es, BOOL32 force)
1829 if (!es) {
1830 fprintf(stderr, "edit: UnlockBuffer() without an EDITSTATE ... please report\n");
1831 return;
1833 if (!(es->style & ES_MULTILINE))
1834 return;
1835 if (!es->lock_count) {
1836 fprintf(stderr, "edit: UnlockBuffer() with lock_count == 0 ... please report\n");
1837 return;
1839 if (!es->text) {
1840 fprintf(stderr, "edit: UnlockBuffer() with es->text == 0 ... please report\n");
1841 return;
1843 if (force || (es->lock_count == 1)) {
1844 if (es->hloc32) {
1845 LocalUnlock32(es->hloc32);
1846 es->text = NULL;
1847 } else if (es->hloc16) {
1848 LOCAL_Unlock(wnd->hInstance, es->hloc16);
1849 es->text = NULL;
1852 es->lock_count--;
1856 /*********************************************************************
1858 * EDIT_WordBreakProc
1860 * Find the beginning of words.
1861 * Note: unlike the specs for a WordBreakProc, this function only
1862 * allows to be called without linebreaks between s[0] upto
1863 * s[count - 1]. Remember it is only called
1864 * internally, so we can decide this for ourselves.
1867 static INT32 EDIT_WordBreakProc(LPSTR s, INT32 index, INT32 count, INT32 action)
1869 INT32 ret = 0;
1871 dprintf_edit(stddeb, "edit: EDIT_WordBreakProc: s=%p, index=%u"
1872 ", count=%u, action=%d\n", s, index, count, action);
1874 switch (action) {
1875 case WB_LEFT:
1876 if (!count)
1877 break;
1878 if (index)
1879 index--;
1880 if (s[index] == ' ') {
1881 while (index && (s[index] == ' '))
1882 index--;
1883 if (index) {
1884 while (index && (s[index] != ' '))
1885 index--;
1886 if (s[index] == ' ')
1887 index++;
1889 } else {
1890 while (index && (s[index] != ' '))
1891 index--;
1892 if (s[index] == ' ')
1893 index++;
1895 ret = index;
1896 break;
1897 case WB_RIGHT:
1898 if (!count)
1899 break;
1900 if (index)
1901 index--;
1902 if (s[index] == ' ')
1903 while ((index < count) && (s[index] == ' ')) index++;
1904 else {
1905 while (s[index] && (s[index] != ' ') && (index < count))
1906 index++;
1907 while ((s[index] == ' ') && (index < count)) index++;
1909 ret = index;
1910 break;
1911 case WB_ISDELIMITER:
1912 ret = (s[index] == ' ');
1913 break;
1914 default:
1915 fprintf(stderr, "edit: EDIT_WordBreakProc: unknown action code, please report !\n");
1916 break;
1918 return ret;
1922 /*********************************************************************
1924 * EM_CHARFROMPOS
1926 * FIXME: do the specs mean to return LineIndex or LineNumber ???
1927 * Let's assume LineIndex is meant
1928 * FIXME: do the specs mean to return -1 if outside client area or
1929 * if outside formatting rectangle ???
1932 static LRESULT EDIT_EM_CharFromPos(WND *wnd, EDITSTATE *es, INT32 x, INT32 y)
1934 POINT32 pt;
1935 RECT32 rc;
1936 INT32 index;
1938 pt.x = x;
1939 pt.y = y;
1940 GetClientRect32(wnd->hwndSelf, &rc);
1941 if (!PtInRect32(&rc, pt))
1942 return -1;
1944 index = EDIT_CharFromPos(wnd, es, x, y, NULL);
1945 return MAKELONG(index, EDIT_EM_LineIndex(wnd, es,
1946 EDIT_EM_LineFromChar(wnd, es, index)));
1950 /*********************************************************************
1952 * EM_FMTLINES
1955 static BOOL32 EDIT_EM_FmtLines(WND *wnd, EDITSTATE *es, BOOL32 add_eol)
1957 fprintf(stdnimp, "edit: EM_FMTLINES: message not implemented\n");
1958 return add_eol;
1962 /*********************************************************************
1964 * EM_GETHANDLE
1966 * Hopefully this won't fire back at us.
1967 * We always start with a fixed buffer in our own heap.
1968 * However, with this message a 32 bit application requests
1969 * a handle to 32 bit moveable local heap memory, where it expects
1970 * to find the text.
1971 * It's a pitty that from this moment on we have to use this
1972 * local heap, because applications may rely on the handle
1973 * in the future.
1975 * In this function we'll try to switch to local heap.
1978 static HLOCAL32 EDIT_EM_GetHandle(WND *wnd, EDITSTATE *es)
1980 HLOCAL32 newBuf;
1981 LPSTR newText;
1982 INT32 newSize;
1984 if (!(es->style & ES_MULTILINE))
1985 return 0;
1987 if (es->hloc32)
1988 return es->hloc32;
1989 else if (es->hloc16)
1990 return (HLOCAL32)es->hloc16;
1992 if (!(newBuf = LocalAlloc32(LMEM_MOVEABLE, lstrlen32A(es->text) + 1))) {
1993 fprintf(stderr, "edit: EM_GETHANDLE: could not allocate new 32 bit buffer\n");
1994 return 0;
1996 newSize = MIN(LocalSize32(newBuf) - 1, es->buffer_limit);
1997 if (!(newText = LocalLock32(newBuf))) {
1998 fprintf(stderr, "edit: EM_GETHANDLE: could not lock new 32 bit buffer\n");
1999 LocalFree32(newBuf);
2000 return 0;
2002 lstrcpy32A(newText, es->text);
2003 EDIT_UnlockBuffer(wnd, es, TRUE);
2004 if (es->text)
2005 HeapFree(es->heap, 0, es->text);
2006 es->hloc32 = newBuf;
2007 es->hloc16 = (HLOCAL16)NULL;
2008 es->buffer_size = newSize;
2009 es->text = newText;
2010 EDIT_LockBuffer(wnd, es);
2011 dprintf_edit(stddeb, "edit: EM_GETHANDLE: switched to 32 bit local heap\n");
2013 return es->hloc32;
2017 /*********************************************************************
2019 * EM_GETHANDLE16
2021 * Hopefully this won't fire back at us.
2022 * We always start with a buffer in 32 bit linear memory.
2023 * However, with this message a 16 bit application requests
2024 * a handle of 16 bit local heap memory, where it expects to find
2025 * the text.
2026 * It's a pitty that from this moment on we have to use this
2027 * local heap, because applications may rely on the handle
2028 * in the future.
2030 * In this function we'll try to switch to local heap.
2032 static HLOCAL16 EDIT_EM_GetHandle16(WND *wnd, EDITSTATE *es)
2034 HLOCAL16 newBuf;
2035 LPSTR newText;
2036 INT32 newSize;
2038 if (!(es->style & ES_MULTILINE))
2039 return 0;
2041 if (es->hloc16)
2042 return es->hloc16;
2044 if (!LOCAL_HeapSize(wnd->hInstance)) {
2045 if (!LocalInit(wnd->hInstance, 0,
2046 GlobalSize16(wnd->hInstance))) {
2047 fprintf(stderr, "edit: EM_GETHANDLE: could not initialize local heap\n");
2048 return 0;
2050 dprintf_edit(stddeb, "edit: EM_GETHANDLE: local heap initialized\n");
2052 if (!(newBuf = LOCAL_Alloc(wnd->hInstance, LMEM_MOVEABLE, lstrlen32A(es->text) + 1))) {
2053 fprintf(stderr, "edit: EM_GETHANDLE: could not allocate new 16 bit buffer\n");
2054 return 0;
2056 newSize = MIN(LOCAL_Size(wnd->hInstance, newBuf) - 1, es->buffer_limit);
2057 if (!(newText = LOCAL_Lock(wnd->hInstance, newBuf))) {
2058 fprintf(stderr, "edit: EM_GETHANDLE: could not lock new 16 bit buffer\n");
2059 LOCAL_Free(wnd->hInstance, newBuf);
2060 return 0;
2062 lstrcpy32A(newText, es->text);
2063 EDIT_UnlockBuffer(wnd, es, TRUE);
2064 if (es->text)
2065 HeapFree(es->heap, 0, es->text);
2066 else if (es->hloc32) {
2067 while (LocalFree32(es->hloc32)) ;
2068 LocalFree32(es->hloc32);
2070 es->hloc32 = (HLOCAL32)NULL;
2071 es->hloc16 = newBuf;
2072 es->buffer_size = newSize;
2073 es->text = newText;
2074 EDIT_LockBuffer(wnd, es);
2075 dprintf_edit(stddeb, "edit: EM_GETHANDLE: switched to 16 bit buffer\n");
2077 return es->hloc16;
2081 /*********************************************************************
2083 * EM_GETLINE
2086 static INT32 EDIT_EM_GetLine(WND *wnd, EDITSTATE *es, INT32 line, LPSTR lpch)
2088 LPSTR src;
2089 INT32 len;
2090 INT32 i;
2092 if (es->style & ES_MULTILINE) {
2093 if (line >= es->line_count)
2094 return 0;
2095 } else
2096 line = 0;
2097 src = es->text + EDIT_EM_LineIndex(wnd, es, line);
2098 len = MIN(*(WORD *)lpch, EDIT_EM_LineLength(wnd, es, line));
2099 for (i = 0 ; i < len ; i++) {
2100 *lpch = *src;
2101 src++;
2102 lpch++;
2104 return (LRESULT)len;
2108 /*********************************************************************
2110 * EM_GETSEL
2113 static LRESULT EDIT_EM_GetSel(WND *wnd, EDITSTATE *es, LPUINT32 start, LPUINT32 end)
2115 UINT32 s = es->selection_start;
2116 UINT32 e = es->selection_end;
2118 ORDER_UINT32(s, e);
2119 if (start)
2120 *start = s;
2121 if (end)
2122 *end = e;
2123 return MAKELONG(s, e);
2127 /*********************************************************************
2129 * EM_GETTHUMB
2131 * FIXME: is this right ? (or should it be only VSCROLL)
2132 * (and maybe only for edit controls that really have their
2133 * own scrollbars) (and maybe only for multiline controls ?)
2134 * All in all: very poorly documented
2136 * FIXME: now it's also broken, because of the new WM_HSCROLL /
2137 * WM_VSCROLL handlers
2140 static LRESULT EDIT_EM_GetThumb(WND *wnd, EDITSTATE *es)
2142 return MAKELONG(EDIT_WM_VScroll(wnd, es, EM_GETTHUMB16, 0, 0),
2143 EDIT_WM_HScroll(wnd, es, EM_GETTHUMB16, 0, 0));
2147 /*********************************************************************
2149 * EM_LINEFROMCHAR
2152 static INT32 EDIT_EM_LineFromChar(WND *wnd, EDITSTATE *es, INT32 index)
2154 INT32 line;
2155 LINEDEF *line_def;
2157 if (!(es->style & ES_MULTILINE))
2158 return 0;
2159 if (index > lstrlen32A(es->text))
2160 return es->line_count - 1;
2161 if (index == -1)
2162 index = MIN(es->selection_start, es->selection_end);
2164 line = 0;
2165 line_def = es->first_line_def;
2166 index -= line_def->length;
2167 while ((index >= 0) && line_def->next) {
2168 line++;
2169 line_def = line_def->next;
2170 index -= line_def->length;
2172 return line;
2176 /*********************************************************************
2178 * EM_LINEINDEX
2181 static INT32 EDIT_EM_LineIndex(WND *wnd, EDITSTATE *es, INT32 line)
2183 INT32 line_index;
2184 LINEDEF *line_def;
2186 if (!(es->style & ES_MULTILINE))
2187 return 0;
2188 if (line >= es->line_count)
2189 return -1;
2191 line_index = 0;
2192 line_def = es->first_line_def;
2193 if (line == -1) {
2194 INT32 index = es->selection_end - line_def->length;
2195 while ((index >= 0) && line_def->next) {
2196 line_index += line_def->length;
2197 line_def = line_def->next;
2198 index -= line_def->length;
2200 } else {
2201 while (line > 0) {
2202 line_index += line_def->length;
2203 line_def = line_def->next;
2204 line--;
2207 return line_index;
2211 /*********************************************************************
2213 * EM_LINELENGTH
2216 static INT32 EDIT_EM_LineLength(WND *wnd, EDITSTATE *es, INT32 index)
2218 LINEDEF *line_def;
2220 if (!(es->style & ES_MULTILINE))
2221 return lstrlen32A(es->text);
2223 if (index == -1) {
2224 /* FIXME: broken
2225 INT32 sl = EDIT_EM_LineFromChar(wnd, es, es->selection_start);
2226 INT32 el = EDIT_EM_LineFromChar(wnd, es, es->selection_end);
2227 return es->selection_start - es->line_defs[sl].offset +
2228 es->line_defs[el].offset +
2229 es->line_defs[el].length - es->selection_end;
2231 return 0;
2233 line_def = es->first_line_def;
2234 index -= line_def->length;
2235 while ((index >= 0) && line_def->next) {
2236 line_def = line_def->next;
2237 index -= line_def->length;
2239 return line_def->net_length;
2243 /*********************************************************************
2245 * EM_LINESCROLL
2247 * FIXME: dx is in average character widths
2248 * However, we assume it is in pixels when we use this
2249 * function internally
2252 static BOOL32 EDIT_EM_LineScroll(WND *wnd, EDITSTATE *es, INT32 dx, INT32 dy)
2254 INT32 nyoff;
2256 if (!(es->style & ES_MULTILINE))
2257 return FALSE;
2259 if (-dx > es->x_offset)
2260 dx = -es->x_offset;
2261 if (dx > es->text_width - es->x_offset)
2262 dx = es->text_width - es->x_offset;
2263 nyoff = MAX(0, es->y_offset + dy);
2264 if (nyoff >= es->line_count)
2265 nyoff = es->line_count - 1;
2266 dy = (es->y_offset - nyoff) * es->line_height;
2267 if (dx || dy) {
2268 RECT32 rc1;
2269 RECT32 rc;
2270 GetClientRect32(wnd->hwndSelf, &rc1);
2271 IntersectRect32(&rc, &rc1, &es->format_rect);
2272 ScrollWindowEx32(wnd->hwndSelf, -dx, dy,
2273 NULL, &rc, (HRGN32)NULL, NULL, SW_INVALIDATE);
2274 es->y_offset = nyoff;
2275 es->x_offset += dx;
2277 if (dx && !(es->flags & EF_HSCROLL_TRACK))
2278 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
2279 if (dy && !(es->flags & EF_VSCROLL_TRACK))
2280 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2281 return TRUE;
2285 /*********************************************************************
2287 * EM_POSFROMCHAR
2290 static LRESULT EDIT_EM_PosFromChar(WND *wnd, EDITSTATE *es, INT32 index, BOOL32 after_wrap)
2292 INT32 len = lstrlen32A(es->text);
2293 INT32 l;
2294 INT32 li;
2295 INT32 x;
2296 INT32 y = 0;
2297 HDC32 dc;
2298 HFONT32 old_font = 0;
2299 SIZE32 size;
2301 index = MIN(index, len);
2302 dc = GetDC32(wnd->hwndSelf);
2303 if (es->font)
2304 old_font = SelectObject32(dc, es->font);
2305 if (es->style & ES_MULTILINE) {
2306 l = EDIT_EM_LineFromChar(wnd, es, index);
2307 y = (l - es->y_offset) * es->line_height;
2308 li = EDIT_EM_LineIndex(wnd, es, l);
2309 if (after_wrap && (li == index) && l) {
2310 INT32 l2 = l - 1;
2311 LINEDEF *line_def = es->first_line_def;
2312 while (l2) {
2313 line_def = line_def->next;
2314 l2--;
2316 if (line_def->ending == END_WRAP) {
2317 l--;
2318 y -= es->line_height;
2319 li = EDIT_EM_LineIndex(wnd, es, l);
2322 x = LOWORD(GetTabbedTextExtent32A(dc, es->text + li, index - li,
2323 es->tabs_count, es->tabs)) - es->x_offset;
2324 } else {
2325 LPSTR text = EDIT_GetPasswordPointer_SL(wnd, es);
2326 if (index < es->x_offset) {
2327 GetTextExtentPoint32A(dc, text + index,
2328 es->x_offset - index, &size);
2329 x = -size.cx;
2330 } else {
2331 GetTextExtentPoint32A(dc, text + es->x_offset,
2332 index - es->x_offset, &size);
2333 x = size.cx;
2335 y = 0;
2336 if (es->style & ES_PASSWORD)
2337 HeapFree(es->heap, 0 ,text);
2339 x += es->format_rect.left;
2340 y += es->format_rect.top;
2341 if (es->font)
2342 SelectObject32(dc, old_font);
2343 ReleaseDC32(wnd->hwndSelf, dc);
2344 return MAKELONG((INT16)x, (INT16)y);
2348 /*********************************************************************
2350 * EM_REPLACESEL
2352 * FIXME: handle ES_NUMBER and ES_OEMCONVERT here
2355 static void EDIT_EM_ReplaceSel(WND *wnd, EDITSTATE *es, BOOL32 can_undo, LPCSTR lpsz_replace)
2357 INT32 strl = lstrlen32A(lpsz_replace);
2358 INT32 tl = lstrlen32A(es->text);
2359 INT32 utl;
2360 UINT32 s;
2361 UINT32 e;
2362 INT32 i;
2363 LPSTR p;
2365 s = es->selection_start;
2366 e = es->selection_end;
2368 if ((s == e) && !strl)
2369 return;
2371 ORDER_UINT32(s, e);
2373 if (!EDIT_MakeFit(wnd, es, tl - (e - s) + strl))
2374 return;
2376 if (e != s) {
2377 /* there is something to be deleted */
2378 if (can_undo) {
2379 utl = lstrlen32A(es->undo_text);
2380 if (!es->undo_insert_count && (*es->undo_text && (s == es->undo_position))) {
2381 /* undo-buffer is extended to the right */
2382 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2383 lstrcpyn32A(es->undo_text + utl, es->text + s, e - s + 1);
2384 } else if (!es->undo_insert_count && (*es->undo_text && (e == es->undo_position))) {
2385 /* undo-buffer is extended to the left */
2386 EDIT_MakeUndoFit(wnd, es, utl + e - s);
2387 for (p = es->undo_text + utl ; p >= es->undo_text ; p--)
2388 p[e - s] = p[0];
2389 for (i = 0 , p = es->undo_text ; i < e - s ; i++)
2390 p[i] = (es->text + s)[i];
2391 es->undo_position = s;
2392 } else {
2393 /* new undo-buffer */
2394 EDIT_MakeUndoFit(wnd, es, e - s);
2395 lstrcpyn32A(es->undo_text, es->text + s, e - s + 1);
2396 es->undo_position = s;
2398 /* any deletion makes the old insertion-undo invalid */
2399 es->undo_insert_count = 0;
2400 } else
2401 EDIT_EM_EmptyUndoBuffer(wnd, es);
2403 /* now delete */
2404 lstrcpy32A(es->text + s, es->text + e);
2406 if (strl) {
2407 /* there is an insertion */
2408 if (can_undo) {
2409 if ((s == es->undo_position) ||
2410 ((es->undo_insert_count) &&
2411 (s == es->undo_position + es->undo_insert_count)))
2413 * insertion is new and at delete position or
2414 * an extension to either left or right
2416 es->undo_insert_count += strl;
2417 else {
2418 /* new insertion undo */
2419 es->undo_position = s;
2420 es->undo_insert_count = strl;
2421 /* new insertion makes old delete-buffer invalid */
2422 *es->undo_text = '\0';
2424 } else
2425 EDIT_EM_EmptyUndoBuffer(wnd, es);
2427 /* now insert */
2428 tl = lstrlen32A(es->text);
2429 for (p = es->text + tl ; p >= es->text + s ; p--)
2430 p[strl] = p[0];
2431 for (i = 0 , p = es->text + s ; i < strl ; i++)
2432 p[i] = lpsz_replace[i];
2433 if(es->style & ES_UPPERCASE)
2434 CharUpperBuff32A(p, strl);
2435 else if(es->style & ES_LOWERCASE)
2436 CharLowerBuff32A(p, strl);
2437 s += strl;
2439 /* FIXME: really inefficient */
2440 if (es->style & ES_MULTILINE)
2441 EDIT_BuildLineDefs_ML(wnd, es);
2443 EDIT_EM_SetSel(wnd, es, s, s, FALSE);
2444 es->flags |= EF_MODIFIED;
2445 es->flags |= EF_UPDATE;
2446 EDIT_EM_ScrollCaret(wnd, es);
2448 /* FIXME: really inefficient */
2449 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2453 /*********************************************************************
2455 * EM_SCROLL
2458 static LRESULT EDIT_EM_Scroll(WND *wnd, EDITSTATE *es, INT32 action)
2460 INT32 dy;
2462 if (!(es->style & ES_MULTILINE))
2463 return (LRESULT)FALSE;
2465 dy = 0;
2467 switch (action) {
2468 case SB_LINEUP:
2469 if (es->y_offset)
2470 dy = -1;
2471 break;
2472 case SB_LINEDOWN:
2473 if (es->y_offset < es->line_count - 1)
2474 dy = 1;
2475 break;
2476 case SB_PAGEUP:
2477 if (es->y_offset)
2478 dy = -(es->format_rect.bottom - es->format_rect.top) / es->line_height;
2479 break;
2480 case SB_PAGEDOWN:
2481 if (es->y_offset < es->line_count - 1)
2482 dy = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
2483 break;
2484 default:
2485 return (LRESULT)FALSE;
2487 if (dy) {
2488 EDIT_EM_LineScroll(wnd, es, 0, dy);
2489 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
2491 return MAKELONG((INT16)dy, (BOOL16)TRUE);
2495 /*********************************************************************
2497 * EM_SETHANDLE
2499 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2502 static void EDIT_EM_SetHandle(WND *wnd, EDITSTATE *es, HLOCAL32 hloc)
2504 if (!(es->style & ES_MULTILINE))
2505 return;
2507 if (!hloc) {
2508 fprintf(stderr, "edit: EM_SETHANDLE called with NULL handle\n");
2509 return;
2512 EDIT_UnlockBuffer(wnd, es, TRUE);
2514 * old buffer is freed by caller, unless
2515 * it is still in our own heap. (in that case
2516 * we free it, correcting the buggy caller.)
2518 if (es->text)
2519 HeapFree(es->heap, 0, es->text);
2521 es->hloc16 = (HLOCAL16)NULL;
2522 es->hloc32 = hloc;
2523 es->text = NULL;
2524 es->buffer_size = LocalSize32(es->hloc32) - 1;
2525 EDIT_LockBuffer(wnd, es);
2527 es->x_offset = es->y_offset = 0;
2528 es->selection_start = es->selection_end = 0;
2529 EDIT_EM_EmptyUndoBuffer(wnd, es);
2530 es->flags &= ~EF_MODIFIED;
2531 es->flags &= ~EF_UPDATE;
2532 EDIT_BuildLineDefs_ML(wnd, es);
2533 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2534 EDIT_EM_ScrollCaret(wnd, es);
2538 /*********************************************************************
2540 * EM_SETHANDLE16
2542 * FIXME: ES_LOWERCASE, ES_UPPERCASE, ES_OEMCONVERT, ES_NUMBER ???
2545 static void EDIT_EM_SetHandle16(WND *wnd, EDITSTATE *es, HLOCAL16 hloc)
2547 if (!(es->style & ES_MULTILINE))
2548 return;
2550 if (!hloc) {
2551 fprintf(stderr, "edit: EM_SETHANDLE called with NULL handle\n");
2552 return;
2555 EDIT_UnlockBuffer(wnd, es, TRUE);
2557 * old buffer is freed by caller, unless
2558 * it is still in our own heap. (in that case
2559 * we free it, correcting the buggy caller.)
2561 if (es->text)
2562 HeapFree(es->heap, 0, es->text);
2564 es->hloc16 = hloc;
2565 es->hloc32 = (HLOCAL32)NULL;
2566 es->text = NULL;
2567 es->buffer_size = LOCAL_Size(wnd->hInstance, es->hloc16) - 1;
2568 EDIT_LockBuffer(wnd, es);
2570 es->x_offset = es->y_offset = 0;
2571 es->selection_start = es->selection_end = 0;
2572 EDIT_EM_EmptyUndoBuffer(wnd, es);
2573 es->flags &= ~EF_MODIFIED;
2574 es->flags &= ~EF_UPDATE;
2575 EDIT_BuildLineDefs_ML(wnd, es);
2576 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2577 EDIT_EM_ScrollCaret(wnd, es);
2581 /*********************************************************************
2583 * EM_SETLIMITTEXT
2585 * FIXME: in WinNT maxsize is 0x7FFFFFFF / 0xFFFFFFFF
2586 * However, the windows version is not complied to yet in all of edit.c
2589 static void EDIT_EM_SetLimitText(WND *wnd, EDITSTATE *es, INT32 limit)
2591 if (es->style & ES_MULTILINE) {
2592 if (limit)
2593 es->buffer_limit = MIN(limit, BUFLIMIT_MULTI);
2594 else
2595 es->buffer_limit = BUFLIMIT_MULTI;
2596 } else {
2597 if (limit)
2598 es->buffer_limit = MIN(limit, BUFLIMIT_SINGLE);
2599 else
2600 es->buffer_limit = BUFLIMIT_SINGLE;
2605 /*********************************************************************
2607 * EM_SETMARGINS
2610 static void EDIT_EM_SetMargins(WND *wnd, EDITSTATE *es, INT32 action, INT32 left, INT32 right)
2612 if (action & EC_USEFONTINFO) {
2613 if (es->style & ES_MULTILINE) {
2615 * FIXME: do some GetABCCharWidth, or so
2616 * This is just preliminary
2618 es->left_margin = es->right_margin = es->char_width/4;
2619 } else
2620 es->left_margin = es->right_margin = es->char_width/4;
2621 } else {
2622 if (action & EC_LEFTMARGIN)
2623 es->left_margin = left;
2624 if (action & EC_RIGHTMARGIN)
2625 es->right_margin = right;
2627 dprintf_edit(stddeb, "EDIT_EM_SetMargins: left=%d, right=%d\n", es->left_margin, es->right_margin);
2631 /*********************************************************************
2633 * EM_SETPASSWORDCHAR
2636 static void EDIT_EM_SetPasswordChar(WND *wnd, EDITSTATE *es, CHAR c)
2638 if (es->style & ES_MULTILINE)
2639 return;
2641 if (es->password_char == c)
2642 return;
2644 es->password_char = c;
2645 if (c) {
2646 wnd->dwStyle |= ES_PASSWORD;
2647 es->style |= ES_PASSWORD;
2648 } else {
2649 wnd->dwStyle &= ~ES_PASSWORD;
2650 es->style &= ~ES_PASSWORD;
2652 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2656 /*********************************************************************
2658 * EDIT_EM_SetSel
2660 * note: unlike the specs say: the order of start and end
2661 * _is_ preserved in Windows. (i.e. start can be > end)
2662 * In other words: this handler is OK
2665 static void EDIT_EM_SetSel(WND *wnd, EDITSTATE *es, UINT32 start, UINT32 end, BOOL32 after_wrap)
2667 UINT32 old_start = es->selection_start;
2668 UINT32 old_end = es->selection_end;
2669 UINT32 len = lstrlen32A(es->text);
2671 if (start == -1) {
2672 start = es->selection_end;
2673 end = es->selection_end;
2674 } else {
2675 start = MIN(start, len);
2676 end = MIN(end, len);
2678 es->selection_start = start;
2679 es->selection_end = end;
2680 if (after_wrap)
2681 es->flags |= EF_AFTER_WRAP;
2682 else
2683 es->flags &= ~EF_AFTER_WRAP;
2684 if (es->flags & EF_FOCUSED) {
2685 LRESULT pos = EDIT_EM_PosFromChar(wnd, es, end, after_wrap);
2686 SetCaretPos32(SLOWORD(pos), SHIWORD(pos));
2688 /* This is little bit more efficient than before, not sure if it can be improved. FIXME? */
2689 ORDER_UINT32(start, end);
2690 ORDER_UINT32(end, old_end);
2691 ORDER_UINT32(start, old_start);
2692 ORDER_UINT32(old_start, old_end);
2693 if (end != old_start)
2696 * One can also do
2697 * ORDER_UINT32(end, old_start);
2698 * EDIT_InvalidateText(wnd, es, start, end);
2699 * EDIT_InvalidateText(wnd, es, old_start, old_end);
2700 * in place of the following if statement.
2702 if (old_start > end )
2704 EDIT_InvalidateText(wnd, es, start, end);
2705 EDIT_InvalidateText(wnd, es, old_start, old_end);
2707 else
2709 EDIT_InvalidateText(wnd, es, start, old_start);
2710 EDIT_InvalidateText(wnd, es, end, old_end);
2713 else EDIT_InvalidateText(wnd, es, start, old_end);
2717 /*********************************************************************
2719 * EM_SETTABSTOPS
2722 static BOOL32 EDIT_EM_SetTabStops(WND *wnd, EDITSTATE *es, INT32 count, LPINT32 tabs)
2724 if (!(es->style & ES_MULTILINE))
2725 return FALSE;
2726 if (es->tabs)
2727 HeapFree(es->heap, 0, es->tabs);
2728 es->tabs_count = count;
2729 if (!count)
2730 es->tabs = NULL;
2731 else {
2732 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT32));
2733 memcpy(es->tabs, tabs, count * sizeof(INT32));
2735 return TRUE;
2739 /*********************************************************************
2741 * EM_SETTABSTOPS16
2744 static BOOL32 EDIT_EM_SetTabStops16(WND *wnd, EDITSTATE *es, INT32 count, LPINT16 tabs)
2746 if (!(es->style & ES_MULTILINE))
2747 return FALSE;
2748 if (es->tabs)
2749 HeapFree(es->heap, 0, es->tabs);
2750 es->tabs_count = count;
2751 if (!count)
2752 es->tabs = NULL;
2753 else {
2754 INT32 i;
2755 es->tabs = HeapAlloc(es->heap, 0, count * sizeof(INT32));
2756 for (i = 0 ; i < count ; i++)
2757 es->tabs[i] = *tabs++;
2759 return TRUE;
2763 /*********************************************************************
2765 * EM_SETWORDBREAKPROC
2768 static void EDIT_EM_SetWordBreakProc(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC32A wbp)
2770 if (es->word_break_proc32A == wbp)
2771 return;
2773 es->word_break_proc32A = wbp;
2774 es->word_break_proc16 = NULL;
2775 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2776 EDIT_BuildLineDefs_ML(wnd, es);
2777 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2782 /*********************************************************************
2784 * EM_SETWORDBREAKPROC16
2787 static void EDIT_EM_SetWordBreakProc16(WND *wnd, EDITSTATE *es, EDITWORDBREAKPROC16 wbp)
2789 if (es->word_break_proc16 == wbp)
2790 return;
2792 es->word_break_proc32A = NULL;
2793 es->word_break_proc16 = wbp;
2794 if ((es->style & ES_MULTILINE) && !(es->style & ES_AUTOHSCROLL)) {
2795 EDIT_BuildLineDefs_ML(wnd, es);
2796 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
2801 /*********************************************************************
2803 * EM_UNDO / WM_UNDO
2806 static BOOL32 EDIT_EM_Undo(WND *wnd, EDITSTATE *es)
2808 INT32 ulength = lstrlen32A(es->undo_text);
2809 LPSTR utext = HeapAlloc(es->heap, 0, ulength + 1);
2811 lstrcpy32A(utext, es->undo_text);
2813 dprintf_edit(stddeb, "edit: before UNDO:insertion length = %d, deletion buffer = %s\n",
2814 es->undo_insert_count, utext);
2816 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2817 EDIT_EM_EmptyUndoBuffer(wnd, es);
2818 EDIT_EM_ReplaceSel(wnd, es, TRUE, utext);
2819 EDIT_EM_SetSel(wnd, es, es->undo_position, es->undo_position + es->undo_insert_count, FALSE);
2820 HeapFree(es->heap, 0, utext);
2822 dprintf_edit(stddeb, "edit: after UNDO: insertion length = %d, deletion buffer = %s\n",
2823 es->undo_insert_count, es->undo_text);
2825 return TRUE;
2829 /*********************************************************************
2831 * WM_CHAR
2834 static void EDIT_WM_Char(WND *wnd, EDITSTATE *es, CHAR c, DWORD key_data)
2836 switch (c) {
2837 case '\r':
2838 case '\n':
2839 if (es->style & ES_MULTILINE) {
2840 if (es->style & ES_READONLY) {
2841 EDIT_MoveHome(wnd, es, FALSE);
2842 EDIT_MoveDown_ML(wnd, es, FALSE);
2843 } else
2844 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\r\n");
2846 break;
2847 case '\t':
2848 if ((es->style & ES_MULTILINE) && !(es->style & ES_READONLY))
2849 EDIT_EM_ReplaceSel(wnd, es, TRUE, "\t");
2850 break;
2851 default:
2852 if (!(es->style & ES_READONLY) && ((BYTE)c >= ' ') && (c != 127)) {
2853 char str[2];
2854 str[0] = c;
2855 str[1] = '\0';
2856 EDIT_EM_ReplaceSel(wnd, es, TRUE, str);
2858 break;
2863 /*********************************************************************
2865 * WM_COMMAND
2868 static void EDIT_WM_Command(WND *wnd, EDITSTATE *es, INT32 code, INT32 id, HWND32 control)
2870 if (code || control)
2871 return;
2873 switch (id) {
2874 case EM_UNDO32:
2875 EDIT_EM_Undo(wnd, es);
2876 break;
2877 case WM_CUT:
2878 EDIT_WM_Cut(wnd, es);
2879 break;
2880 case WM_COPY:
2881 EDIT_WM_Copy(wnd, es);
2882 break;
2883 case WM_PASTE:
2884 EDIT_WM_Paste(wnd, es);
2885 break;
2886 case WM_CLEAR:
2887 EDIT_WM_Clear(wnd, es);
2888 break;
2889 case EM_SETSEL32:
2890 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
2891 EDIT_EM_ScrollCaret(wnd, es);
2892 break;
2893 default:
2894 dprintf_edit(stddeb, "edit: unknown menu item, please report\n");
2895 break;
2900 /*********************************************************************
2902 * WM_CONTEXTMENU
2904 * Note: the resource files resource/sysres_??.rc cannot define a
2905 * single popup menu. Hence we use a (dummy) menubar
2906 * containing the single popup menu as its first item.
2908 * FIXME: the message identifiers have been chosen arbitrarily,
2909 * hence we use MF_BYPOSITION.
2910 * We might as well use the "real" values (anybody knows ?)
2911 * The menu definition is in resources/sysres_??.rc.
2912 * Once these are OK, we better use MF_BYCOMMAND here
2913 * (as we do in EDIT_WM_Command()).
2916 static void EDIT_WM_ContextMenu(WND *wnd, EDITSTATE *es, HWND32 hwnd, INT32 x, INT32 y)
2918 HMENU32 menu = LoadMenuIndirect32A(SYSRES_GetResPtr(SYSRES_MENU_EDITMENU));
2919 HMENU32 popup = GetSubMenu32(menu, 0);
2920 UINT32 start = es->selection_start;
2921 UINT32 end = es->selection_end;
2923 ORDER_UINT32(start, end);
2925 /* undo */
2926 EnableMenuItem32(popup, 0, MF_BYPOSITION | (EDIT_EM_CanUndo(wnd, es) ? MF_ENABLED : MF_GRAYED));
2927 /* cut */
2928 EnableMenuItem32(popup, 2, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2929 /* copy */
2930 EnableMenuItem32(popup, 3, MF_BYPOSITION | ((end - start) && !(es->style & ES_PASSWORD) ? MF_ENABLED : MF_GRAYED));
2931 /* paste */
2932 EnableMenuItem32(popup, 4, MF_BYPOSITION | (IsClipboardFormatAvailable32(CF_TEXT) ? MF_ENABLED : MF_GRAYED));
2933 /* delete */
2934 EnableMenuItem32(popup, 5, MF_BYPOSITION | ((end - start) ? MF_ENABLED : MF_GRAYED));
2935 /* select all */
2936 EnableMenuItem32(popup, 7, MF_BYPOSITION | (start || (end != lstrlen32A(es->text)) ? MF_ENABLED : MF_GRAYED));
2938 TrackPopupMenu32(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, wnd->hwndSelf, NULL);
2939 DestroyMenu32(menu);
2943 /*********************************************************************
2945 * WM_COPY
2947 * FIXME: replace with 32 bit calls as soon as they are implemented
2948 * in the clipboard code
2951 static void EDIT_WM_Copy(WND *wnd, EDITSTATE *es)
2953 INT32 s = es->selection_start;
2954 INT32 e = es->selection_end;
2955 HGLOBAL16 hdst;
2956 LPSTR dst;
2958 if (e == s)
2959 return;
2960 ORDER_INT32(s, e);
2961 hdst = GlobalAlloc16(GMEM_MOVEABLE, (DWORD)(e - s + 1));
2962 dst = GlobalLock16(hdst);
2963 lstrcpyn32A(dst, es->text + s, e - s + 1);
2964 GlobalUnlock16(hdst);
2965 OpenClipboard32(wnd->hwndSelf);
2966 EmptyClipboard32();
2967 SetClipboardData16(CF_TEXT, hdst);
2968 CloseClipboard32();
2972 /*********************************************************************
2974 * WM_CREATE
2977 static LRESULT EDIT_WM_Create(WND *wnd, LPCREATESTRUCT32A cs)
2979 EDITSTATE *es;
2981 if (!(es = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*es))))
2982 return -1;
2983 *(EDITSTATE **)wnd->wExtra = es;
2984 if (!(es->heap = HeapCreate(0, 0x10000, 0)))
2985 return -1;
2986 es->style = cs->style;
2988 /* remove the WS_CAPTION style if it has been set - this is really a */
2989 /* pseudo option made from a combination of WS_BORDER and WS_DLGFRAME */
2990 if ((es->style & WS_BORDER) && (es->style & WS_DLGFRAME))
2991 es->style ^= WS_DLGFRAME;
2993 if (es->style & ES_MULTILINE) {
2994 es->buffer_size = BUFSTART_MULTI;
2995 es->buffer_limit = BUFLIMIT_MULTI;
2996 if (es->style & WS_VSCROLL)
2997 es->style |= ES_AUTOVSCROLL;
2998 if (es->style & WS_HSCROLL)
2999 es->style |= ES_AUTOHSCROLL;
3000 es->style &= ~ES_PASSWORD;
3001 if ((es->style & ES_CENTER) || (es->style & ES_RIGHT)) {
3002 if (es->style & ES_RIGHT)
3003 es->style &= ~ES_CENTER;
3004 es->style &= ~WS_HSCROLL;
3005 es->style &= ~ES_AUTOHSCROLL;
3008 /* FIXME: for now, all multi line controls are AUTOVSCROLL */
3009 es->style |= ES_AUTOVSCROLL;
3010 } else {
3011 es->buffer_size = BUFSTART_SINGLE;
3012 es->buffer_limit = BUFLIMIT_SINGLE;
3013 es->style &= ~ES_CENTER;
3014 es->style &= ~ES_RIGHT;
3015 es->style &= ~WS_HSCROLL;
3016 es->style &= ~WS_VSCROLL;
3017 es->style &= ~ES_AUTOVSCROLL;
3018 es->style &= ~ES_WANTRETURN;
3019 if (es->style & ES_UPPERCASE) {
3020 es->style &= ~ES_LOWERCASE;
3021 es->style &= ~ES_NUMBER;
3022 } else if (es->style & ES_LOWERCASE)
3023 es->style &= ~ES_NUMBER;
3024 if (es->style & ES_PASSWORD)
3025 es->password_char = '*';
3027 /* FIXME: for now, all single line controls are AUTOHSCROLL */
3028 es->style |= ES_AUTOHSCROLL;
3030 if (!(es->text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3031 return -1;
3032 es->buffer_size = HeapSize(es->heap, 0, es->text) - 1;
3033 if (!(es->undo_text = HeapAlloc(es->heap, 0, es->buffer_size + 1)))
3034 return -1;
3035 es->undo_buffer_size = HeapSize(es->heap, 0, es->undo_text) - 1;
3036 *es->text = '\0';
3037 if (es->style & ES_MULTILINE)
3038 if (!(es->first_line_def = HeapAlloc(es->heap, HEAP_ZERO_MEMORY, sizeof(LINEDEF))))
3039 return -1;
3040 es->line_count = 1;
3042 * To initialize some final structure members, we call some helper
3043 * functions. However, since the EDITSTATE is not consistent (i.e.
3044 * not fully initialized), we should be very careful which
3045 * functions can be called, and in what order.
3047 EDIT_WM_SetFont(wnd, es, 0, FALSE);
3048 if (cs->lpszName && *(cs->lpszName) != '\0') {
3049 EDIT_EM_ReplaceSel(wnd, es, FALSE, cs->lpszName);
3050 /* 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 */
3051 es->selection_start = es->selection_end = 0;
3052 EDIT_EM_ScrollCaret(wnd, es);
3054 return 0;
3058 /*********************************************************************
3060 * WM_DESTROY
3063 static void EDIT_WM_Destroy(WND *wnd, EDITSTATE *es)
3065 if (es->hloc32) {
3066 while (LocalUnlock32(es->hloc32)) ;
3067 LocalFree32(es->hloc32);
3069 if (es->hloc16) {
3070 while (LOCAL_Unlock(wnd->hInstance, es->hloc16)) ;
3071 LOCAL_Free(wnd->hInstance, es->hloc16);
3073 HeapDestroy(es->heap);
3074 HeapFree(GetProcessHeap(), 0, es);
3075 *(EDITSTATE **)wnd->wExtra = NULL;
3079 /*********************************************************************
3081 * WM_ERASEBKGND
3084 static LRESULT EDIT_WM_EraseBkGnd(WND *wnd, EDITSTATE *es, HDC32 dc)
3086 HBRUSH32 brush;
3087 RECT32 rc;
3089 if (!(brush = (HBRUSH32)EDIT_SEND_CTLCOLOR(wnd, dc)))
3090 brush = (HBRUSH32)GetStockObject32(WHITE_BRUSH);
3092 GetClientRect32(wnd->hwndSelf, &rc);
3093 IntersectClipRect32(dc, rc.left, rc.top, rc.right, rc.bottom);
3094 GetClipBox32(dc, &rc);
3096 * FIXME: specs say that we should UnrealizeObject() the brush,
3097 * but the specs of UnrealizeObject() say that we shouldn't
3098 * unrealize a stock object. The default brush that
3099 * DefWndProc() returns is ... a stock object.
3101 FillRect32(dc, &rc, brush);
3102 return -1;
3106 /*********************************************************************
3108 * WM_GETTEXT
3111 static INT32 EDIT_WM_GetText(WND *wnd, EDITSTATE *es, INT32 count, LPSTR text)
3113 INT32 len = lstrlen32A(es->text);
3115 if (count > len) {
3116 lstrcpy32A(text, es->text);
3117 return len;
3118 } else
3119 return -1;
3123 /*********************************************************************
3125 * EDIT_HScroll_Hack
3127 * 16 bit notepad needs this. Actually it is not _our_ hack,
3128 * it is notepad's. Notepad is sending us scrollbar messages with
3129 * undocumented parameters without us even having a scrollbar ... !?!?
3132 static LRESULT EDIT_HScroll_Hack(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3134 INT32 dx = 0;
3135 INT32 fw = es->format_rect.right - es->format_rect.left;
3136 LRESULT ret = 0;
3138 if (!(es->flags & EF_HSCROLL_HACK)) {
3139 fprintf(stderr, "edit: hacked WM_HSCROLL handler invoked\n");
3140 fprintf(stderr, " if you are _not_ running 16 bit notepad, please report\n");
3141 fprintf(stderr, " (this message is only displayed once per edit control)\n");
3142 es->flags |= EF_HSCROLL_HACK;
3145 switch (action) {
3146 case SB_LINELEFT:
3147 if (es->x_offset)
3148 dx = -es->char_width;
3149 break;
3150 case SB_LINERIGHT:
3151 if (es->x_offset < es->text_width)
3152 dx = es->char_width;
3153 break;
3154 case SB_PAGELEFT:
3155 if (es->x_offset)
3156 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3157 break;
3158 case SB_PAGERIGHT:
3159 if (es->x_offset < es->text_width)
3160 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3161 break;
3162 case SB_LEFT:
3163 if (es->x_offset)
3164 dx = -es->x_offset;
3165 break;
3166 case SB_RIGHT:
3167 if (es->x_offset < es->text_width)
3168 dx = es->text_width - es->x_offset;
3169 break;
3170 case SB_THUMBTRACK:
3171 es->flags |= EF_HSCROLL_TRACK;
3172 dx = pos * es->text_width / 100 - es->x_offset;
3173 break;
3174 case SB_THUMBPOSITION:
3175 es->flags &= ~EF_HSCROLL_TRACK;
3176 if (!(dx = pos * es->text_width / 100 - es->x_offset))
3177 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3178 break;
3179 case SB_ENDSCROLL:
3180 break;
3183 * FIXME : the next two are undocumented !
3184 * Are we doing the right thing ?
3185 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3186 * although it's also a regular control message.
3188 case EM_GETTHUMB16:
3189 ret = es->text_width ? es->x_offset * 100 / es->text_width : 0;
3190 break;
3191 case EM_LINESCROLL16:
3192 dx = pos;
3193 break;
3195 default:
3196 dprintf_edit(stddeb, "edit: undocumented (hacked) WM_HSCROLL parameter, please report\n");
3197 return 0;
3199 if (dx)
3200 EDIT_EM_LineScroll(wnd, es, dx, 0);
3201 return ret;
3205 /*********************************************************************
3207 * WM_HSCROLL
3210 static LRESULT EDIT_WM_HScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3212 INT32 dx;
3213 INT32 fw;
3215 if (!(es->style & ES_MULTILINE))
3216 return 0;
3218 if (!(es->style & ES_AUTOHSCROLL))
3219 return 0;
3221 if (!(es->style & WS_HSCROLL))
3222 return EDIT_HScroll_Hack(wnd, es, action, pos, scroll_bar);
3224 dx = 0;
3225 fw = es->format_rect.right - es->format_rect.left;
3226 switch (action) {
3227 case SB_LINELEFT:
3228 if (es->x_offset)
3229 dx = -es->char_width;
3230 break;
3231 case SB_LINERIGHT:
3232 if (es->x_offset < es->text_width)
3233 dx = es->char_width;
3234 break;
3235 case SB_PAGELEFT:
3236 if (es->x_offset)
3237 dx = -fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3238 break;
3239 case SB_PAGERIGHT:
3240 if (es->x_offset < es->text_width)
3241 dx = fw / HSCROLL_FRACTION / es->char_width * es->char_width;
3242 break;
3243 case SB_LEFT:
3244 if (es->x_offset)
3245 dx = -es->x_offset;
3246 break;
3247 case SB_RIGHT:
3248 if (es->x_offset < es->text_width)
3249 dx = es->text_width - es->x_offset;
3250 break;
3251 case SB_THUMBTRACK:
3252 es->flags |= EF_HSCROLL_TRACK;
3253 dx = pos - es->x_offset;
3254 break;
3255 case SB_THUMBPOSITION:
3256 es->flags &= ~EF_HSCROLL_TRACK;
3257 if (!(dx = pos - es->x_offset)) {
3258 SetScrollPos32(wnd->hwndSelf, SB_HORZ, pos, TRUE);
3259 EDIT_NOTIFY_PARENT(wnd, EN_HSCROLL, "EN_HSCROLL");
3261 break;
3262 case SB_ENDSCROLL:
3263 break;
3265 default:
3266 fprintf(stderr, "edit: undocumented WM_HSCROLL parameter, please report\n");
3267 return 0;
3269 if (dx)
3270 EDIT_EM_LineScroll(wnd, es, dx, 0);
3271 return 0;
3275 /*********************************************************************
3277 * EDIT_CheckCombo
3280 static BOOL32 EDIT_CheckCombo(WND *wnd, UINT32 msg, INT32 key, DWORD key_data)
3282 HWND32 hLBox;
3284 if (WIDGETS_IsControl32(wnd->parent, BIC32_COMBO) &&
3285 (hLBox = COMBO_GetLBWindow(wnd->parent))) {
3286 HWND32 hCombo = wnd->parent->hwndSelf;
3287 BOOL32 bUIFlip = TRUE;
3289 dprintf_combo(stddeb, "EDIT_CheckCombo [%04x]: handling msg %04x (%04x)\n",
3290 wnd->hwndSelf, (UINT16)msg, (UINT16)key);
3292 switch (msg) {
3293 case WM_KEYDOWN: /* Handle F4 and arrow keys */
3294 if (key != VK_F4) {
3295 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETEXTENDEDUI32, 0, 0);
3296 if (SendMessage32A(hCombo, CB_GETDROPPEDSTATE32, 0, 0))
3297 bUIFlip = FALSE;
3299 if (!bUIFlip)
3300 SendMessage32A(hLBox, WM_KEYDOWN, (WPARAM32)key, 0);
3301 else {
3302 /* make sure ComboLBox pops up */
3303 SendMessage32A(hCombo, CB_SETEXTENDEDUI32, 0, 0);
3304 SendMessage32A(hLBox, WM_KEYDOWN, VK_F4, 0);
3305 SendMessage32A(hCombo, CB_SETEXTENDEDUI32, 1, 0);
3307 break;
3308 case WM_SYSKEYDOWN: /* Handle Alt+up/down arrows */
3309 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETEXTENDEDUI32, 0, 0);
3310 if (bUIFlip) {
3311 bUIFlip = (BOOL32)SendMessage32A(hCombo, CB_GETDROPPEDSTATE32, 0, 0);
3312 SendMessage32A(hCombo, CB_SHOWDROPDOWN32, (bUIFlip) ? FALSE : TRUE, 0);
3313 } else
3314 SendMessage32A(hLBox, WM_KEYDOWN, VK_F4, 0);
3315 break;
3317 return TRUE;
3319 return FALSE;
3323 /*********************************************************************
3325 * WM_KEYDOWN
3327 * Handling of special keys that don't produce a WM_CHAR
3328 * (i.e. non-printable keys) & Backspace & Delete
3331 static LRESULT EDIT_WM_KeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data)
3333 BOOL32 shift;
3334 BOOL32 control;
3336 if (GetKeyState32(VK_MENU) & 0x8000)
3337 return 0;
3339 shift = GetKeyState32(VK_SHIFT) & 0x8000;
3340 control = GetKeyState32(VK_CONTROL) & 0x8000;
3342 switch (key) {
3343 case VK_F4:
3344 case VK_UP:
3345 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3346 break;
3347 if (key == VK_F4)
3348 break;
3349 /* fall through */
3350 case VK_LEFT:
3351 if ((es->style & ES_MULTILINE) && (key == VK_UP))
3352 EDIT_MoveUp_ML(wnd, es, shift);
3353 else
3354 if (control)
3355 EDIT_MoveWordBackward(wnd, es, shift);
3356 else
3357 EDIT_MoveBackward(wnd, es, shift);
3358 break;
3359 case VK_DOWN:
3360 if (EDIT_CheckCombo(wnd, WM_KEYDOWN, key, key_data))
3361 break;
3362 /* fall through */
3363 case VK_RIGHT:
3364 if ((es->style & ES_MULTILINE) && (key == VK_DOWN))
3365 EDIT_MoveDown_ML(wnd, es, shift);
3366 else if (control)
3367 EDIT_MoveWordForward(wnd, es, shift);
3368 else
3369 EDIT_MoveForward(wnd, es, shift);
3370 break;
3371 case VK_HOME:
3372 EDIT_MoveHome(wnd, es, shift);
3373 break;
3374 case VK_END:
3375 EDIT_MoveEnd(wnd, es, shift);
3376 break;
3377 case VK_PRIOR:
3378 if (es->style & ES_MULTILINE)
3379 EDIT_MovePageUp_ML(wnd, es, shift);
3380 break;
3381 case VK_NEXT:
3382 if (es->style & ES_MULTILINE)
3383 EDIT_MovePageDown_ML(wnd, es, shift);
3384 break;
3385 case VK_BACK:
3386 if (!(es->style & ES_READONLY) && !control)
3387 if (es->selection_start != es->selection_end)
3388 EDIT_WM_Clear(wnd, es);
3389 else {
3390 /* delete character left of caret */
3391 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3392 EDIT_MoveBackward(wnd, es, TRUE);
3393 EDIT_WM_Clear(wnd, es);
3395 break;
3396 case VK_DELETE:
3397 if (!(es->style & ES_READONLY) && !(shift && control))
3398 if (es->selection_start != es->selection_end) {
3399 if (shift)
3400 EDIT_WM_Cut(wnd, es);
3401 else
3402 EDIT_WM_Clear(wnd, es);
3403 } else {
3404 if (shift) {
3405 /* delete character left of caret */
3406 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3407 EDIT_MoveBackward(wnd, es, TRUE);
3408 EDIT_WM_Clear(wnd, es);
3409 } else if (control) {
3410 /* delete to end of line */
3411 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3412 EDIT_MoveEnd(wnd, es, TRUE);
3413 EDIT_WM_Clear(wnd, es);
3414 } else {
3415 /* delete character right of caret */
3416 EDIT_EM_SetSel(wnd, es, -1, 0, FALSE);
3417 EDIT_MoveForward(wnd, es, TRUE);
3418 EDIT_WM_Clear(wnd, es);
3421 break;
3422 case VK_INSERT:
3423 if (shift) {
3424 if (!(es->style & ES_READONLY))
3425 EDIT_WM_Paste(wnd, es);
3426 } else if (control)
3427 EDIT_WM_Copy(wnd, es);
3428 break;
3430 return 0;
3434 /*********************************************************************
3436 * WM_KILLFOCUS
3439 static LRESULT EDIT_WM_KillFocus(WND *wnd, EDITSTATE *es, HWND32 window_getting_focus)
3441 es->flags &= ~EF_FOCUSED;
3442 DestroyCaret32();
3443 if(!(es->style & ES_NOHIDESEL))
3444 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3445 EDIT_NOTIFY_PARENT(wnd, EN_KILLFOCUS, "EN_KILLFOCUS");
3446 return 0;
3450 /*********************************************************************
3452 * WM_LBUTTONDBLCLK
3454 * The caret position has been set on the WM_LBUTTONDOWN message
3457 static LRESULT EDIT_WM_LButtonDblClk(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3459 INT32 s;
3460 INT32 e = es->selection_end;
3461 INT32 l;
3462 INT32 li;
3463 INT32 ll;
3465 if (!(es->flags & EF_FOCUSED))
3466 return 0;
3468 l = EDIT_EM_LineFromChar(wnd, es, e);
3469 li = EDIT_EM_LineIndex(wnd, es, l);
3470 ll = EDIT_EM_LineLength(wnd, es, e);
3471 s = li + EDIT_CallWordBreakProc (wnd, es, li, e - li, ll, WB_LEFT);
3472 e = li + EDIT_CallWordBreakProc(wnd, es, li, e - li, ll, WB_RIGHT);
3473 EDIT_EM_SetSel(wnd, es, s, e, FALSE);
3474 EDIT_EM_ScrollCaret(wnd, es);
3475 return 0;
3479 /*********************************************************************
3481 * WM_LBUTTONDOWN
3484 static LRESULT EDIT_WM_LButtonDown(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3486 INT32 e;
3487 BOOL32 after_wrap;
3489 if (!(es->flags & EF_FOCUSED))
3490 return 0;
3492 SetCapture32(wnd->hwndSelf);
3493 EDIT_ConfinePoint(wnd, es, &x, &y);
3494 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3495 EDIT_EM_SetSel(wnd, es, (keys & MK_SHIFT) ? es->selection_start : e, e, after_wrap);
3496 EDIT_EM_ScrollCaret(wnd, es);
3497 es->region_posx = es->region_posy = 0;
3498 SetTimer32(wnd->hwndSelf, 0, 100, NULL);
3499 return 0;
3503 /*********************************************************************
3505 * WM_LBUTTONUP
3508 static LRESULT EDIT_WM_LButtonUp(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3510 if (GetCapture32() == wnd->hwndSelf) {
3511 KillTimer32(wnd->hwndSelf, 0);
3512 ReleaseCapture();
3514 return 0;
3518 /*********************************************************************
3520 * WM_MOUSEMOVE
3523 static LRESULT EDIT_WM_MouseMove(WND *wnd, EDITSTATE *es, DWORD keys, INT32 x, INT32 y)
3525 INT32 e;
3526 BOOL32 after_wrap;
3527 INT32 prex, prey;
3529 if (GetCapture32() != wnd->hwndSelf)
3530 return 0;
3533 * FIXME: gotta do some scrolling if outside client
3534 * area. Maybe reset the timer ?
3536 prex = x; prey = y;
3537 EDIT_ConfinePoint(wnd, es, &x, &y);
3538 es->region_posx = (prex < x) ? -1 : ((prex > x) ? 1 : 0);
3539 es->region_posy = (prey < y) ? -1 : ((prey > y) ? 1 : 0);
3540 e = EDIT_CharFromPos(wnd, es, x, y, &after_wrap);
3541 EDIT_EM_SetSel(wnd, es, es->selection_start, e, after_wrap);
3542 return 0;
3546 /*********************************************************************
3548 * WM_PAINT
3551 static void EDIT_WM_Paint(WND *wnd, EDITSTATE *es)
3553 PAINTSTRUCT32 ps;
3554 INT32 i;
3555 HDC32 dc;
3556 HFONT32 old_font = 0;
3557 RECT32 rc;
3558 RECT32 rcLine;
3559 RECT32 rcRgn;
3560 LRESULT pos;
3561 BOOL32 rev = IsWindowEnabled32(wnd->hwndSelf) &&
3562 ((es->flags & EF_FOCUSED) ||
3563 (es->style & ES_NOHIDESEL));
3565 if (es->flags & EF_UPDATE)
3566 EDIT_NOTIFY_PARENT(wnd, EN_UPDATE, "EN_UPDATE");
3568 dc = BeginPaint32(wnd->hwndSelf, &ps);
3569 IntersectClipRect32(dc, es->format_rect.left,
3570 es->format_rect.top,
3571 es->format_rect.right,
3572 es->format_rect.bottom);
3573 if (es->style & ES_MULTILINE) {
3574 GetClientRect32(wnd->hwndSelf, &rc);
3575 IntersectClipRect32(dc, rc.left, rc.top, rc.right, rc.bottom);
3577 if (es->font)
3578 old_font = SelectObject32(dc, es->font);
3579 EDIT_SEND_CTLCOLOR(wnd, dc);
3580 if (!IsWindowEnabled32(wnd->hwndSelf))
3581 SetTextColor32(dc, GetSysColor32(COLOR_GRAYTEXT));
3582 GetClipBox32(dc, &rcRgn);
3583 if (es->style & ES_MULTILINE) {
3584 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3585 for (i = es->y_offset ; i <= MIN(es->y_offset + vlc, es->y_offset + es->line_count - 1) ; i++) {
3586 EDIT_GetLineRect(wnd, es, i, 0, -1, &rcLine);
3587 if (IntersectRect32(&rc, &rcRgn, &rcLine))
3588 EDIT_PaintLine(wnd, es, dc, i, rev);
3590 } else {
3591 EDIT_GetLineRect(wnd, es, 0, 0, -1, &rcLine);
3592 if (IntersectRect32(&rc, &rcRgn, &rcLine))
3593 EDIT_PaintLine(wnd, es, dc, 0, rev);
3595 if (es->font)
3596 SelectObject32(dc, old_font);
3597 if (es->flags & EF_FOCUSED) {
3598 pos = EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3599 SetCaretPos32(SLOWORD(pos), SHIWORD(pos));
3601 EndPaint32(wnd->hwndSelf, &ps);
3602 if ((es->style & WS_VSCROLL) && !(es->flags & EF_VSCROLL_TRACK)) {
3603 INT32 vlc = (es->format_rect.bottom - es->format_rect.top) / es->line_height;
3604 SCROLLINFO si;
3605 si.cbSize = sizeof(SCROLLINFO);
3606 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3607 si.nMin = 0;
3608 si.nMax = es->line_count + vlc - 2;
3609 si.nPage = vlc;
3610 si.nPos = es->y_offset;
3611 SetScrollInfo32(wnd->hwndSelf, SB_VERT, &si, TRUE);
3613 if ((es->style & WS_HSCROLL) && !(es->flags & EF_HSCROLL_TRACK)) {
3614 SCROLLINFO si;
3615 INT32 fw = es->format_rect.right - es->format_rect.left;
3616 si.cbSize = sizeof(SCROLLINFO);
3617 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
3618 si.nMin = 0;
3619 si.nMax = es->text_width + fw - 1;
3620 si.nPage = fw;
3621 si.nPos = es->x_offset;
3622 SetScrollInfo32(wnd->hwndSelf, SB_HORZ, &si, TRUE);
3625 if (es->flags & EF_UPDATE) {
3626 es->flags &= ~EF_UPDATE;
3627 EDIT_NOTIFY_PARENT(wnd, EN_CHANGE, "EN_CHANGE");
3632 /*********************************************************************
3634 * WM_PASTE
3636 * FIXME: replace with 32 bit handler once GetClipboardData32() is
3637 * implemented in misc/clipboard.c
3640 static void EDIT_WM_Paste(WND *wnd, EDITSTATE *es)
3642 HGLOBAL16 hsrc;
3643 LPSTR src;
3645 OpenClipboard32(wnd->hwndSelf);
3646 if ((hsrc = GetClipboardData16(CF_TEXT))) {
3647 src = (LPSTR)GlobalLock16(hsrc);
3648 EDIT_EM_ReplaceSel(wnd, es, TRUE, src);
3649 GlobalUnlock16(hsrc);
3651 CloseClipboard32();
3655 /*********************************************************************
3657 * WM_SETFOCUS
3660 static void EDIT_WM_SetFocus(WND *wnd, EDITSTATE *es, HWND32 window_losing_focus)
3662 LRESULT pos;
3664 es->flags |= EF_FOCUSED;
3665 CreateCaret32(wnd->hwndSelf, 0, 2, es->line_height);
3666 pos = EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3667 SetCaretPos32(SLOWORD(pos), SHIWORD(pos));
3668 if(!(es->style & ES_NOHIDESEL))
3669 EDIT_InvalidateText(wnd, es, es->selection_start, es->selection_end);
3670 ShowCaret32(wnd->hwndSelf);
3671 EDIT_NOTIFY_PARENT(wnd, EN_SETFOCUS, "EN_SETFOCUS");
3675 /*********************************************************************
3677 * WM_SETFONT
3680 static void EDIT_WM_SetFont(WND *wnd, EDITSTATE *es, HFONT32 font, BOOL32 redraw)
3682 TEXTMETRIC32A tm;
3683 HDC32 dc;
3684 HFONT32 old_font = 0;
3686 es->font = font;
3687 dc = GetDC32(wnd->hwndSelf);
3688 if (font)
3689 old_font = SelectObject32(dc, font);
3690 GetTextMetrics32A(dc, &tm);
3691 es->line_height = tm.tmHeight;
3692 es->char_width = tm.tmAveCharWidth;
3693 if (font)
3694 SelectObject32(dc, old_font);
3695 ReleaseDC32(wnd->hwndSelf, dc);
3696 if (wnd->flags & WIN_ISWIN32)
3697 EDIT_EM_SetMargins(wnd, es, EC_USEFONTINFO, 0, 0);
3698 if (es->style & ES_MULTILINE)
3699 EDIT_BuildLineDefs_ML(wnd, es);
3700 if (redraw)
3701 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
3702 if (es->flags & EF_FOCUSED) {
3703 LRESULT pos;
3704 DestroyCaret32();
3705 CreateCaret32(wnd->hwndSelf, 0, 2, es->line_height);
3706 pos = EDIT_EM_PosFromChar(wnd, es, es->selection_end, es->flags & EF_AFTER_WRAP);
3707 SetCaretPos32(SLOWORD(pos), SHIWORD(pos));
3708 ShowCaret32(wnd->hwndSelf);
3713 /*********************************************************************
3715 * WM_SETTEXT
3718 static void EDIT_WM_SetText(WND *wnd, EDITSTATE *es, LPCSTR text)
3720 EDIT_EM_SetSel(wnd, es, 0, -1, FALSE);
3721 if (text) {
3722 dprintf_edit(stddeb, "\t'%s'\n", text);
3723 EDIT_EM_ReplaceSel(wnd, es, FALSE, text);
3724 es->x_offset = 0;
3726 es->flags |= EF_MODIFIED;
3727 es->flags |= EF_UPDATE;
3728 EDIT_EM_ScrollCaret(wnd, es);
3732 /*********************************************************************
3734 * WM_SIZE
3737 static void EDIT_WM_Size(WND *wnd, EDITSTATE *es, UINT32 action, INT32 width, INT32 height)
3739 if ((action == SIZE_MAXIMIZED) || (action == SIZE_RESTORED)) {
3740 RECT32 rc;
3741 SetRect32(&rc, 0, 0, width, height);
3742 EDIT_SetRectNP(wnd, es, &rc);
3743 InvalidateRect32(wnd->hwndSelf, NULL, TRUE);
3748 /*********************************************************************
3750 * WM_SYSKEYDOWN
3753 static LRESULT EDIT_WM_SysKeyDown(WND *wnd, EDITSTATE *es, INT32 key, DWORD key_data)
3755 if ((key == VK_BACK) && (key_data & 0x2000)) {
3756 if (EDIT_EM_CanUndo(wnd, es))
3757 EDIT_EM_Undo(wnd, es);
3758 return 0;
3759 } else if (key == VK_UP || key == VK_DOWN)
3760 if (EDIT_CheckCombo(wnd, WM_SYSKEYDOWN, key, key_data))
3761 return 0;
3762 return DefWindowProc32A(wnd->hwndSelf, WM_SYSKEYDOWN, (WPARAM32)key, (LPARAM)key_data);
3766 /*********************************************************************
3768 * WM_TIMER
3771 static void EDIT_WM_Timer(WND *wnd, EDITSTATE *es, INT32 id, TIMERPROC32 timer_proc)
3773 if (es->region_posx < 0) {
3774 EDIT_MoveBackward(wnd, es, TRUE);
3775 } else if (es->region_posx > 0) {
3776 EDIT_MoveForward(wnd, es, TRUE);
3779 * FIXME: gotta do some vertical scrolling here, like
3780 * EDIT_EM_LineScroll(wnd, 0, 1);
3785 /*********************************************************************
3787 * EDIT_VScroll_Hack
3789 * 16 bit notepad needs this. Actually it is not _our_ hack,
3790 * it is notepad's. Notepad is sending us scrollbar messages with
3791 * undocumented parameters without us even having a scrollbar ... !?!?
3794 static LRESULT EDIT_VScroll_Hack(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3796 INT32 dy = 0;
3797 LRESULT ret = 0;
3799 if (!(es->flags & EF_VSCROLL_HACK)) {
3800 fprintf(stderr, "edit: hacked WM_VSCROLL handler invoked\n");
3801 fprintf(stderr, " if you are _not_ running 16 bit notepad, please report\n");
3802 fprintf(stderr, " (this message is only displayed once per edit control)\n");
3803 es->flags |= EF_VSCROLL_HACK;
3806 switch (action) {
3807 case SB_LINEUP:
3808 case SB_LINEDOWN:
3809 case SB_PAGEUP:
3810 case SB_PAGEDOWN:
3811 EDIT_EM_Scroll(wnd, es, action);
3812 return 0;
3813 case SB_TOP:
3814 dy = -es->y_offset;
3815 break;
3816 case SB_BOTTOM:
3817 dy = es->line_count - 1 - es->y_offset;
3818 break;
3819 case SB_THUMBTRACK:
3820 es->flags |= EF_VSCROLL_TRACK;
3821 dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset;
3822 break;
3823 case SB_THUMBPOSITION:
3824 es->flags &= ~EF_VSCROLL_TRACK;
3825 if (!(dy = (pos * (es->line_count - 1) + 50) / 100 - es->y_offset))
3826 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3827 break;
3828 case SB_ENDSCROLL:
3829 break;
3832 * FIXME : the next two are undocumented !
3833 * Are we doing the right thing ?
3834 * At least Win 3.1 Notepad makes use of EM_GETTHUMB this way,
3835 * although it's also a regular control message.
3837 case EM_GETTHUMB16:
3838 ret = (es->line_count > 1) ? es->y_offset * 100 / (es->line_count - 1) : 0;
3839 break;
3840 case EM_LINESCROLL16:
3841 dy = pos;
3842 break;
3844 default:
3845 fprintf(stderr, "edit: undocumented (hacked) WM_VSCROLL parameter, please report\n");
3846 return 0;
3848 if (dy)
3849 EDIT_EM_LineScroll(wnd, es, 0, dy);
3850 return ret;
3854 /*********************************************************************
3856 * WM_VSCROLL
3859 static LRESULT EDIT_WM_VScroll(WND *wnd, EDITSTATE *es, INT32 action, INT32 pos, HWND32 scroll_bar)
3861 INT32 dy;
3863 if (!(es->style & ES_MULTILINE))
3864 return 0;
3866 if (!(es->style & ES_AUTOVSCROLL))
3867 return 0;
3869 if (!(es->style & WS_VSCROLL))
3870 return EDIT_VScroll_Hack(wnd, es, action, pos, scroll_bar);
3872 dy = 0;
3873 switch (action) {
3874 case SB_LINEUP:
3875 case SB_LINEDOWN:
3876 case SB_PAGEUP:
3877 case SB_PAGEDOWN:
3878 EDIT_EM_Scroll(wnd, es, action);
3879 return 0;
3881 case SB_TOP:
3882 dy = -es->y_offset;
3883 break;
3884 case SB_BOTTOM:
3885 dy = es->line_count - 1 - es->y_offset;
3886 break;
3887 case SB_THUMBTRACK:
3888 es->flags |= EF_VSCROLL_TRACK;
3889 dy = pos - es->y_offset;
3890 break;
3891 case SB_THUMBPOSITION:
3892 es->flags &= ~EF_VSCROLL_TRACK;
3893 if (!(dy = pos - es->y_offset)) {
3894 SetScrollPos32(wnd->hwndSelf, SB_VERT, pos, TRUE);
3895 EDIT_NOTIFY_PARENT(wnd, EN_VSCROLL, "EN_VSCROLL");
3897 break;
3898 case SB_ENDSCROLL:
3899 break;
3901 default:
3902 fprintf(stderr, "edit: undocumented WM_VSCROLL parameter, please report\n");
3903 return 0;
3905 if (dy)
3906 EDIT_EM_LineScroll(wnd, es, 0, dy);
3907 return 0;