Changed buffer size for EM_STREAMIN message to 4096, as it's what M$
[wine/testsucceed.git] / dlls / riched20 / editor.c
blobc4f8d59de1f407f73b6667ef694b1cface47e158
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /*
22 API implementation status:
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
26 - EM_CANPASTE
27 + EM_CANREDO 2.0
28 + EM_CANUNDO
29 - EM_CHARFROMPOS
30 - EM_DISPLAYBAND
31 + EM_EMPTYUNDOBUFFER
32 + EM_EXGETSEL
33 - EM_EXLIMITTEXT
34 - EM_EXLINEFROMCHAR
35 + EM_EXSETSEL
36 - EM_FINDTEXT
37 - EM_FINDTEXTEX
38 - EM_FINDWORDBREAK
39 - EM_FMTLINES
40 - EM_FORMATRANGE
41 - EM_GETCHARFORMAT (partly done)
42 + EM_GETEVENTMASK
43 - EM_GETFIRSTVISIBLELINE
44 - EM_GETIMECOLOR 1.0asian
45 - EM_GETIMECOMPMODE 2.0
46 - EM_GETIMEOPTIONS 1.0asian
47 - EM_GETIMESTATUS
48 - EM_GETLANGOPTIONS 2.0
49 - EM_GETLIMITTEXT
50 - EM_GETLINE
51 - EM_GETLINECOUNT returns number of rows, not of paragraphs
52 + EM_GETMODIFY
53 - EM_GETOLEINTERFACE
54 - EM_GETOPTIONS
55 + EM_GETPARAFORMAT
56 - EM_GETPUNCTUATION 1.0asian
57 - EM_GETRECT
58 - EM_GETREDONAME 2.0
59 + EM_GETSEL
60 + EM_GETSELTEXT (ANSI&Unicode)
61 ! - EM_GETTHUMB
62 - EM_GETTEXTMODE 2.0
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
64 - EM_GETUNDONAME
65 - EM_GETWORDBREAKPROC
66 - EM_GETWORDBREAKPROCEX
67 - EM_GETWORDWRAPMODE 1.0asian
68 - EM_HIDESELECTION
69 - EM_LIMITTEXT
70 - EM_LINEFROMCHAR
71 - EM_LINEINDEX
72 - EM_LINELENGTH
73 - EM_LINESCROLL
74 - EM_PASTESPECIAL
75 - EM_POSFROMCHARS
76 - EM_REDO 2.0
77 - EM_REQUESTRESIZE
78 + EM_REPLACESEL (proper style?) ANSI&Unicode
79 - EM_SCROLL
80 - EM_SCROLLCARET
81 - EM_SELECTIONTYPE
82 + EM_SETBKGNDCOLOR
83 - EM_SETCHARFORMAT (partly done, no ANSI)
84 + EM_SETEVENTMASK (few notifications supported)
85 - EM_SETIMECOLOR 1.0asian
86 - EM_SETIMEOPTIONS 1.0asian
87 - EM_SETLANGOPTIONS 2.0
88 - EM_SETLIMITTEXT
89 + EM_SETMODIFY (not sure if implementation is correct)
90 - EM_SETOLECALLBACK
91 - EM_SETOPTIONS
92 + EM_SETPARAFORMAT
93 - EM_SETPUNCTUATION 1.0asian
94 + EM_SETREADONLY no beep on modification attempt
95 - EM_SETRECT
96 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
97 + EM_SETSEL
98 - EM_SETTARGETDEVICE
99 - EM_SETTEXTMODE 2.0
100 - EM_SETUNDOLIMIT 2.0
101 - EM_SETWORDBREAKPROC
102 - EM_SETWORDBREAKPROCEX
103 - EM_SETWORDWRAPMODE 1.0asian
104 - EM_STOPGROUPTYPING 2.0
105 - EM_STREAMIN
106 - EM_STREAMOUT
107 - EM_UNDO
108 + WM_CHAR
109 + WM_CLEAR
110 - WM_COPY (lame implementation, no RTF support)
111 - WM_CUT (lame implementation, no RTF support)
112 + WM_GETDLGCODE (the current implementation is incomplete)
113 + WM_GETTEXT (ANSI&Unicode)
114 + WM_GETTEXTLENGTH (ANSI version sucks)
115 - WM_PASTE
116 - WM_SETFONT
117 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
118 - WM_STYLECHANGING
119 - WM_STYLECHANGED (things like read-only flag)
120 - WM_UNICHAR
122 Notifications
124 * EN_CHANGE (sent from the wrong place)
125 - EN_CORRECTTEXT
126 - EN_DROPFILES
127 - EN_ERRSPACE
128 - EN_HSCROLL
129 - EN_IMECHANGE
130 + EN_KILLFOCUS
131 - EN_LINK
132 - EN_MAXTEXT
133 - EN_MSGFILTER
134 - EN_OLEOPFAILED
135 - EN_PROTECTED
136 - EN_REQUESTRESIZE
137 - EN_SAVECLIPBOARD
138 + EN_SELCHANGE
139 + EN_SETFOCUS
140 - EN_STOPNOUNDO
141 * EN_UPDATE (sent from the wrong place)
142 - EN_VSCROLL
144 Styles
146 - ES_AUTOHSCROLL
147 - ES_AUTOVSCROLL
148 - ES_CENTER
149 - ES_DISABLENOSCROLL (scrollbar is always visible)
150 - ES_EX_NOCALLOLEINIT
151 - ES_LEFT
152 - ES_MULTILINE (currently single line controls aren't supported)
153 - ES_NOIME
154 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
155 - ES_RIGHT
156 - ES_SAVESEL
157 - ES_SELFIME
158 - ES_SUNKEN
159 - ES_VERTICAL
160 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
161 - WS_SETFONT
162 - WS_HSCROLL
163 - WS_VSCROLL
167 * RICHED20 TODO (incomplete):
169 * - font caching
170 * - add remaining CHARFORMAT/PARAFORMAT fields
171 * - right/center align should strip spaces from the beginning
172 * - more advanced navigation (Ctrl-arrows, PageUp/PageDn)
173 * - tabs
174 * - pictures (not just smiling faces that lack API support ;-) )
175 * - OLE objects
176 * - calculate heights of pictures (half-done)
177 * - EM_STREAMIN/EM_STREAMOUT
178 * - horizontal scrolling (not even started)
179 * - fix scrollbars and refresh (it sucks bigtime)
180 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
181 * - should remember maximum row width for wrap hysteresis
182 * - find/replace
183 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
184 * - italic cursor with italic fonts
185 * - IME
186 * - most notifications aren't sent at all (the most important ones are)
187 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
188 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
189 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
190 * - bugs in end-of-text handling (the gray bar) could get me in jail ;-)
191 * - determination of row size
192 * - end-of-paragraph marks should be of reasonable size
194 * Bugs that are probably fixed, but not so easy to verify:
195 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
196 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
197 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
198 * - caret shouldn't be displayed when selection isn't empty
199 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
200 * - undo for setting default format (done, might be buggy)
201 * - styles might be not released properly (looks like they work like charm, but who knows?
205 #include "editor.h"
206 #include <ole2.h>
207 #include <richole.h>
208 #include <winreg.h>
209 #define NO_SHLWAPI_STREAM
210 #include <shlwapi.h>
212 #include "rtf.h"
214 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
216 int me_debug = 0;
217 HANDLE me_heap = NULL;
219 ME_TextBuffer *ME_MakeText() {
221 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
223 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
224 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
226 p1->prev = NULL;
227 p1->next = p2;
228 p2->prev = p1;
229 p2->next = NULL;
230 p1->member.para.next_para = p2;
231 p2->member.para.prev_para = p1;
232 p2->member.para.nCharOfs = 0;
234 buf->pFirst = p1;
235 buf->pLast = p2;
236 buf->pCharStyle = NULL;
238 return buf;
241 #define STREAMIN_BUFFER_SIZE 4096 /* M$ compatibility */
243 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream, ME_Style *style)
245 BYTE buffer[STREAMIN_BUFFER_SIZE+1];
246 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
248 TRACE("%08lx %p\n", dwFormat, stream);
249 stream->dwError = 0;
251 do {
252 long nDataSize = 0, nWideChars = 0;
253 stream->dwError = stream->pfnCallback(stream->dwCookie,
254 (dwFormat & SF_UNICODE ? (BYTE *)wszText : buffer),
255 STREAMIN_BUFFER_SIZE, &nDataSize);
257 if (stream->dwError)
258 break;
259 if (!nDataSize)
260 break;
262 if (!(dwFormat & SF_UNICODE))
264 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
265 nWideChars = MultiByteToWideChar(CP_ACP, 0, buffer, nDataSize, wszText, STREAMIN_BUFFER_SIZE);
267 else
268 nWideChars = nDataSize>>1;
269 ME_InsertTextFromCursor(editor, 0, wszText, nWideChars, style);
270 if (nDataSize<STREAMIN_BUFFER_SIZE)
271 break;
272 } while(1);
273 ME_CommitUndo(editor);
274 return 0;
277 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
279 RTF_Info parser;
280 ME_Style *style;
282 FIXME("%08lx %p\n", format, stream);
283 TRACE("%p %p\n", stream, editor->hWnd);
285 if (format & SFF_SELECTION) {
286 style = ME_GetSelectionInsertStyle(editor);
287 SendMessageW(editor->hWnd, WM_CLEAR, 0, 0);
289 else {
290 style = editor->pBuffer->pDefaultStyle;
291 ME_AddRefStyle(style);
292 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
293 SetWindowTextA(editor->hWnd, "");
294 ME_ClearTempStyle(editor);
297 if (format & SF_RTF) {
298 /* setup the RTF parser */
299 memset(&parser, 0, sizeof parser);
300 RTFSetEditStream(&parser, stream);
301 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
302 parser.hwndEdit = editor->hWnd;
303 WriterInit(&parser);
304 RTFInit(&parser);
305 BeginFile(&parser);
307 /* do the parsing */
308 RTFRead(&parser);
309 RTFFlushOutputBuffer(&parser);
311 else if (format & SF_TEXT)
312 ME_StreamInText(editor, format, stream, style);
313 else
314 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
315 /* put the cursor at the top */
316 if (!(format & SFF_SELECTION))
317 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
318 else
320 /* FIXME where to put cursor now ? */
322 ME_ReleaseStyle(style);
324 return 0;
327 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
328 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
329 HDC hDC;
330 int i;
331 ed->hWnd = hWnd;
332 ed->pBuffer = ME_MakeText();
333 hDC = GetDC(hWnd);
334 ME_MakeFirstParagraph(hDC, ed->pBuffer);
335 ReleaseDC(hWnd, hDC);
336 ed->bCaretShown = FALSE;
337 ed->nCursors = 3;
338 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
339 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
340 ed->pCursors[0].nOffset = 0;
341 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
342 ed->pCursors[1].nOffset = 0;
343 ed->nTotalLength = 0;
344 ed->nScrollPos = 0;
345 ed->nUDArrowX = -1;
346 ed->nSequence = 0;
347 ed->rgbBackColor = -1;
348 ed->bCaretAtEnd = FALSE;
349 ed->nEventMask = 0;
350 ed->nModifyStep = 0;
351 ed->pUndoStack = ed->pRedoStack = NULL;
352 ed->nUndoMode = umAddToUndo;
353 ed->nParagraphs = 1;
354 ed->nLastSelStart = ed->nLastSelEnd = 0;
355 for (i=0; i<HFONT_CACHE_SIZE; i++)
357 ed->pFontCache[i].nRefs = 0;
358 ed->pFontCache[i].nAge = 0;
359 ed->pFontCache[i].hFont = NULL;
361 ME_CheckCharOffsets(ed);
362 return ed;
365 void ME_DestroyEditor(ME_TextEditor *editor)
367 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
368 ME_DisplayItem *p = pFirst, *pNext = NULL;
369 int i;
371 ME_ClearTempStyle(editor);
372 ME_EmptyUndoStack(editor);
373 while(p) {
374 pNext = p->next;
375 ME_DestroyDisplayItem(p);
376 p = pNext;
378 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
379 for (i=0; i<HFONT_CACHE_SIZE; i++)
381 if (editor->pFontCache[i].hFont)
382 DeleteObject(editor->pFontCache[i].hFont);
385 FREE_OBJ(editor);
388 #define UNSUPPORTED_MSG(e) \
389 case e: \
390 FIXME(#e ": stub\n"); \
391 return DefWindowProcW(hWnd, msg, wParam, lParam);
393 /******************************************************************
394 * RichEditANSIWndProc (RICHED20.10)
396 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
397 HDC hDC;
398 PAINTSTRUCT ps;
399 SCROLLINFO si;
400 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
401 switch(msg) {
403 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
404 UNSUPPORTED_MSG(EM_CANPASTE)
405 UNSUPPORTED_MSG(EM_CHARFROMPOS)
406 UNSUPPORTED_MSG(EM_DISPLAYBAND)
407 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
408 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
409 UNSUPPORTED_MSG(EM_FINDTEXT)
410 UNSUPPORTED_MSG(EM_FINDTEXTEX)
411 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
412 UNSUPPORTED_MSG(EM_FMTLINES)
413 UNSUPPORTED_MSG(EM_FORMATRANGE)
414 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
415 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
416 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
417 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
418 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
419 UNSUPPORTED_MSG(EM_GETLINE)
420 UNSUPPORTED_MSG(EM_GETLINECOUNT)
421 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
422 UNSUPPORTED_MSG(EM_GETOPTIONS)
423 UNSUPPORTED_MSG(EM_GETRECT)
424 UNSUPPORTED_MSG(EM_GETREDONAME)
425 UNSUPPORTED_MSG(EM_GETTEXTMODE)
426 UNSUPPORTED_MSG(EM_GETUNDONAME)
427 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
428 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
429 UNSUPPORTED_MSG(EM_HIDESELECTION)
430 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
431 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
432 UNSUPPORTED_MSG(EM_LINEINDEX)
433 UNSUPPORTED_MSG(EM_LINELENGTH)
434 UNSUPPORTED_MSG(EM_LINESCROLL)
435 UNSUPPORTED_MSG(EM_PASTESPECIAL)
436 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
437 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
438 UNSUPPORTED_MSG(EM_SCROLL)
439 UNSUPPORTED_MSG(EM_SCROLLCARET)
440 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
441 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
442 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
443 UNSUPPORTED_MSG(EM_SETOPTIONS)
444 UNSUPPORTED_MSG(EM_SETRECT)
445 UNSUPPORTED_MSG(EM_SETRECTNP)
446 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
447 UNSUPPORTED_MSG(EM_SETTEXTMODE)
448 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
449 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
450 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
451 UNSUPPORTED_MSG(EM_STREAMOUT)
452 UNSUPPORTED_MSG(WM_SETFONT)
453 UNSUPPORTED_MSG(WM_PASTE)
454 UNSUPPORTED_MSG(WM_STYLECHANGING)
455 UNSUPPORTED_MSG(WM_STYLECHANGED)
456 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
458 /* Messages specific to Richedit controls */
460 case EM_STREAMIN:
461 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
462 case WM_GETDLGCODE:
464 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
465 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
466 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
467 return code;
469 case WM_NCCREATE:
471 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
472 editor = ME_MakeEditor(hWnd);
473 SetWindowLongW(hWnd, 0, (long)editor);
474 pcs = 0; /* ignore */
475 return TRUE;
477 case EM_EMPTYUNDOBUFFER:
478 ME_EmptyUndoStack(editor);
479 return 0;
480 case EM_GETSEL:
482 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
483 if (!((wParam|lParam) & 0xFFFF0000))
484 return (lParam<<16)|wParam;
485 return -1;
487 case EM_EXGETSEL:
489 CHARRANGE *pRange = (CHARRANGE *)lParam;
490 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
491 return 0;
493 case EM_CANUNDO:
494 return editor->pUndoStack != NULL;
495 case EM_CANREDO:
496 return editor->pRedoStack != NULL;
497 case EM_UNDO:
498 ME_Undo(editor);
499 return 0;
500 case EM_REDO:
501 ME_Redo(editor);
502 return 0;
503 case EM_SETSEL:
505 ME_SetSelection(editor, wParam, lParam);
506 ME_Repaint(editor);
507 ME_SendSelChange(editor);
508 return 0;
510 case EM_EXSETSEL:
512 CHARRANGE *pRange = (CHARRANGE *)lParam;
513 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
514 /* FIXME optimize */
515 ME_Repaint(editor);
516 ME_SendSelChange(editor);
517 return 0;
519 case EM_SETBKGNDCOLOR:
521 LRESULT lColor = ME_GetBackColor(editor);
522 if (wParam)
523 editor->rgbBackColor = -1;
524 else
525 editor->rgbBackColor = lParam;
526 InvalidateRect(hWnd, NULL, TRUE);
527 UpdateWindow(hWnd);
528 return lColor;
530 case EM_GETMODIFY:
531 return editor->nModifyStep == 0 ? 0 : 1;
532 case EM_SETMODIFY:
534 if (wParam)
535 editor->nModifyStep = 0x80000000;
536 else
537 editor->nModifyStep = 0;
539 return 0;
541 case EM_SETREADONLY:
543 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
544 if (wParam)
545 nStyle |= ES_READONLY;
546 else
547 nStyle &= ~ES_READONLY;
548 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
549 ME_Repaint(editor);
550 return 0;
552 case EM_SETEVENTMASK:
553 editor->nEventMask = lParam;
554 return 0;
555 case EM_GETEVENTMASK:
556 return editor->nEventMask;
557 case EM_SETCHARFORMAT:
559 CHARFORMAT2W buf, *p;
560 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
561 if (!wParam)
562 ME_SetDefaultCharFormat(editor, p);
563 else if (wParam == (SCF_WORD | SCF_SELECTION))
564 FIXME("word selection not supported\n");
565 else if (wParam == SCF_ALL)
566 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
567 else
568 ME_SetSelectionCharFormat(editor, p);
569 ME_CommitUndo(editor);
570 ME_UpdateRepaint(editor);
571 return 0;
573 case EM_GETCHARFORMAT:
575 CHARFORMAT2W tmp;
576 tmp.cbSize = sizeof(tmp);
577 if (!wParam)
578 ME_GetDefaultCharFormat(editor, &tmp);
579 else
580 ME_GetSelectionCharFormat(editor, &tmp);
581 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
582 return 0;
584 case EM_SETPARAFORMAT:
585 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
586 ME_CommitUndo(editor);
587 return 0;
588 case EM_GETPARAFORMAT:
589 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
590 return 0;
591 case WM_CLEAR:
593 int from, to;
594 ME_GetSelection(editor, &from, &to);
595 ME_InternalDeleteText(editor, from, to-from);
596 ME_CommitUndo(editor);
597 ME_UpdateRepaint(editor);
598 return 0;
600 case EM_REPLACESEL:
602 int from, to;
603 ME_Style *style;
604 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
605 size_t len = lstrlenW(wszText);
606 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
608 ME_GetSelection(editor, &from, &to);
609 style = ME_GetSelectionInsertStyle(editor);
610 ME_InternalDeleteText(editor, from, to-from);
611 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
612 ME_ReleaseStyle(style);
613 ME_EndToUnicode(hWnd, wszText);
614 /* drop temporary style if line end */
615 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
616 if (len>0 && wszText[len-1] == '\n')
617 ME_ClearTempStyle(editor);
619 ME_CommitUndo(editor);
620 if (!wParam)
621 ME_EmptyUndoStack(editor);
622 ME_UpdateRepaint(editor);
623 return 0;
625 case WM_SETTEXT:
627 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
628 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
629 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
630 /* uses default style! */
631 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
632 ME_EndToUnicode(hWnd, wszText);
633 ME_CommitUndo(editor);
634 ME_EmptyUndoStack(editor);
635 ME_UpdateRepaint(editor);
636 return 0;
638 case WM_CUT:
639 case WM_COPY:
641 int from, to, pars;
642 WCHAR *data;
643 HANDLE hData;
645 if (!OpenClipboard(hWnd))
646 return 0;
648 EmptyClipboard();
649 ME_GetSelection(editor, &from, &to);
650 pars = ME_CountParagraphsBetween(editor, from, to);
651 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
652 data = (WCHAR *)GlobalLock(hData);
653 ME_GetTextW(editor, data, from, to-from, TRUE);
654 GlobalUnlock(hData);
655 SetClipboardData(CF_UNICODETEXT, hData);
656 CloseClipboard();
657 if (msg == WM_CUT)
659 ME_InternalDeleteText(editor, from, to-from);
660 ME_CommitUndo(editor);
661 ME_UpdateRepaint(editor);
663 return 0;
665 case WM_GETTEXTLENGTH:
666 return ME_GetTextLength(editor);
667 case WM_GETTEXT:
669 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
670 tr.chrg.cpMin = 0;
671 tr.chrg.cpMax = wParam-1;
672 tr.lpstrText = (WCHAR *)lParam;
673 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
675 case EM_GETSELTEXT:
677 int from, to;
678 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
679 ME_GetSelection(editor, &from, &to);
680 tr.chrg.cpMin = from;
681 tr.chrg.cpMax = to;
682 tr.lpstrText = (WCHAR *)lParam;
683 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
685 case EM_GETTEXTRANGE:
687 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
688 if (IsWindowUnicode(hWnd))
689 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
690 else
692 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
693 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
694 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
695 /* FIXME this is a potential security hole (buffer overrun)
696 if you know more about wchar->mbyte conversion please explain
698 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
699 FREE_OBJ(p);
700 return nChars;
702 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
704 case WM_CREATE:
705 ME_CommitUndo(editor);
706 /* ME_InsertTextFromCursor(editor, 0, (WCHAR *)L"x", 1, editor->pBuffer->pDefaultStyle); */
707 ME_WrapMarkedParagraphs(editor);
708 ME_MoveCaret(editor);
709 return 0;
710 case WM_DESTROY:
711 ME_DestroyEditor(editor);
712 SetWindowLongW(hWnd, 0, 0);
713 PostQuitMessage(0);
714 break;
715 case WM_LBUTTONDOWN:
716 SetFocus(hWnd);
717 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
718 SetCapture(hWnd);
719 break;
720 case WM_MOUSEMOVE:
721 if (GetCapture() == hWnd)
722 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
723 break;
724 case WM_LBUTTONUP:
725 if (GetCapture() == hWnd)
726 ReleaseCapture();
727 break;
728 case WM_PAINT:
729 hDC = BeginPaint(hWnd, &ps);
730 ME_PaintContent(editor, hDC, FALSE);
731 EndPaint(hWnd, &ps);
732 break;
733 case WM_SETFOCUS:
734 ME_ShowCaret(editor);
735 ME_SendOldNotify(editor, EN_SETFOCUS);
736 return 0;
737 case WM_KILLFOCUS:
738 ME_HideCaret(editor);
739 ME_SendOldNotify(editor, EN_KILLFOCUS);
740 return 0;
741 case WM_ERASEBKGND:
743 HDC hDC = (HDC)wParam;
744 RECT rc;
745 COLORREF rgbBG = ME_GetBackColor(editor);
746 if (GetUpdateRect(hWnd,&rc,TRUE))
748 HBRUSH hbr = CreateSolidBrush(rgbBG);
749 FillRect(hDC, &rc, hbr);
750 DeleteObject(hbr);
752 return 1;
754 case WM_COMMAND:
755 TRACE("editor wnd command = %d\n", LOWORD(wParam));
756 return 0;
757 case WM_KEYDOWN:
758 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
759 ME_CommitUndo(editor);
760 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
761 HideCaret(hWnd);
762 ME_MoveCaret(editor);
763 ShowCaret(hWnd);
764 return 0;
766 if (GetKeyState(VK_CONTROL)<0)
768 if (LOWORD(wParam)=='W')
770 CHARFORMAT2W chf;
771 char buf[2048];
772 ME_GetSelectionCharFormat(editor, &chf);
773 ME_DumpStyleToBuf(&chf, buf);
774 MessageBoxA(NULL, buf, "Style dump", MB_OK);
776 if (LOWORD(wParam)=='Q')
778 ME_CheckCharOffsets(editor);
781 goto do_default;
782 case WM_CHAR:
784 WCHAR wstr;
785 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
786 MessageBeep(MB_ICONERROR);
787 return 0; /* FIXME really 0 ? */
789 wstr = LOWORD(wParam);
790 if (((unsigned)wstr)>=' ' || wstr=='\r') {
791 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
792 ME_Style *style = ME_GetInsertStyle(editor, 0);
793 ME_SaveTempStyle(editor);
794 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
795 ME_ReleaseStyle(style);
796 ME_CommitUndo(editor);
797 ME_UpdateRepaint(editor);
799 return 0;
801 case WM_VSCROLL:
803 si.cbSize = sizeof(SCROLLINFO);
804 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
805 GetScrollInfo(hWnd, SB_VERT, &si);
806 switch(LOWORD(wParam)) {
807 case SB_THUMBTRACK:
808 SetScrollPos(hWnd, SB_VERT, si.nTrackPos, FALSE);
809 ScrollWindow(hWnd, 0, si.nPos-si.nTrackPos, NULL, NULL);
810 /* InvalidateRect(hWnd, NULL, TRUE); */
811 UpdateWindow(hWnd);
812 break;
814 break;
816 case WM_SIZE:
818 ME_MarkAllForWrapping(editor);
819 ME_Repaint(editor);
820 return DefWindowProcW(hWnd, msg, wParam, lParam);
822 case EM_GETOLEINTERFACE:
824 LPVOID *ppvObj = (LPVOID*) lParam;
825 TRACE("EM_GETOLEINTERFACE %p\n", ppvObj);
826 return CreateIRichEditOle(ppvObj);
828 default:
829 do_default:
830 return DefWindowProcW(hWnd, msg, wParam, lParam);
832 return 0L;
835 /******************************************************************
836 * RichEdit10ANSIWndProc (RICHED20.9)
838 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
840 /* FIXME: this is NOT the same as 2.0 version */
841 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
844 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
846 HWND hWnd = editor->hWnd;
847 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
850 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
852 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
853 int i = 0;
855 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
856 item = item->member.para.next_para;
857 if (!item)
858 return 0;
859 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
860 item = item->member.para.next_para;
861 i++;
863 return i;
866 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
868 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
869 int nWritten = 0;
871 while(item && item->member.para.next_para->member.para.nCharOfs <= nStart)
872 item = ME_FindItemFwd(item, diParagraph);
873 if (!item) {
874 *buffer = L'\0';
875 return 0;
877 nStart -= item->member.para.nCharOfs;
879 do {
880 item = ME_FindItemFwd(item, diRun);
881 } while(item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nStart));
882 assert(item);
884 nStart -= item->member.run.nCharOfs;
886 if (nStart)
888 int nLen = ME_StrLen(item->member.run.strText) - nStart;
889 if (nLen > nChars)
890 nLen = nChars;
891 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
892 nChars -= nLen;
893 nWritten += nLen;
894 if (!nChars)
895 return nWritten;
896 buffer += nLen;
897 nStart = 0;
898 item = ME_FindItemFwd(item, diRun);
901 while(nChars && item)
903 int nLen = ME_StrLen(item->member.run.strText);
904 if (nLen > nChars)
905 nLen = nChars;
907 if (item->member.run.nFlags & MERF_ENDPARA)
909 if (bCRLF) {
910 *buffer++ = '\r';
911 nWritten++;
913 *buffer = '\n';
914 assert(nLen == 1);
916 else
917 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
918 nChars -= nLen;
919 nWritten += nLen;
920 buffer += nLen;
922 if (!nChars)
924 *buffer = L'\0';
925 return nWritten;
927 item = ME_FindItemFwd(item, diRun);
929 *buffer = L'\0';
930 return nWritten;
933 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
934 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
936 void ME_RegisterEditorClass(HINSTANCE hInstance)
938 BOOL bResult;
939 WNDCLASSW wcW;
940 WNDCLASSA wcA;
942 wcW.style = CS_HREDRAW | CS_VREDRAW;
943 wcW.lpfnWndProc = RichEditANSIWndProc;
944 wcW.cbClsExtra = 0;
945 wcW.cbWndExtra = 4;
946 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
947 wcW.hIcon = NULL;
948 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
949 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
950 wcW.lpszMenuName = NULL;
951 wcW.lpszClassName = wszClassName;
952 bResult = RegisterClassW(&wcW);
953 assert(bResult);
954 wcW.lpszClassName = wszClassName50;
955 bResult = RegisterClassW(&wcW);
956 assert(bResult);
958 wcA.style = CS_HREDRAW | CS_VREDRAW;
959 wcA.lpfnWndProc = RichEditANSIWndProc;
960 wcA.cbClsExtra = 0;
961 wcA.cbWndExtra = 4;
962 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
963 wcA.hIcon = NULL;
964 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
965 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
966 wcA.lpszMenuName = NULL;
967 wcA.lpszClassName = "RichEdit20A";
968 bResult = RegisterClassA(&wcA);
969 assert(bResult);
970 wcA.lpszClassName = "RichEdit50A";
971 bResult = RegisterClassA(&wcA);
972 assert(bResult);
975 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
977 TRACE("\n");
978 switch (fdwReason)
980 case DLL_PROCESS_ATTACH:
981 DisableThreadLibraryCalls(hinstDLL);
982 me_heap = HeapCreate (0, 0x10000, 0);
983 ME_RegisterEditorClass(hinstDLL);
984 break;
986 case DLL_PROCESS_DETACH:
987 UnregisterClassW(wszClassName, 0);
988 UnregisterClassW(wszClassName50, 0);
989 UnregisterClassA("RichEdit20A", 0);
990 UnregisterClassA("RichEdit50A", 0);
991 HeapDestroy (me_heap);
992 me_heap = NULL;
993 break;
995 return TRUE;
998 /******************************************************************
999 * CreateTextServices (RICHED20.4)
1001 * FIXME should be ITextHost instead of void*
1003 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1004 IUnknown **ppUnk)
1006 FIXME("stub\n");
1007 /* FIXME should support aggregation */
1008 if (punkOuter)
1009 return CLASS_E_NOAGGREGATION;
1011 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1014 /******************************************************************
1015 * REExtendedRegisterClass (RICHED20.8)
1017 * FIXME undocumented
1019 void WINAPI REExtendedRegisterClass(void)
1021 FIXME("stub\n");