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
22 API implementation status:
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
41 - EM_GETCHARFORMAT (partly done)
43 - EM_GETFIRSTVISIBLELINE
44 - EM_GETIMECOLOR 1.0asian
45 - EM_GETIMECOMPMODE 2.0
46 - EM_GETIMEOPTIONS 1.0asian
48 - EM_GETLANGOPTIONS 2.0
51 - EM_GETLINECOUNT returns number of rows, not of paragraphs
56 - EM_GETPUNCTUATION 1.0asian
60 + EM_GETSELTEXT (ANSI&Unicode)
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
66 - EM_GETWORDBREAKPROCEX
67 - EM_GETWORDWRAPMODE 1.0asian
78 + EM_REPLACESEL (proper style?) ANSI&Unicode
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
89 + EM_SETMODIFY (not sure if implementation is correct)
93 - EM_SETPUNCTUATION 1.0asian
94 + EM_SETREADONLY no beep on modification attempt
96 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
100 - EM_SETUNDOLIMIT 2.0
101 - EM_SETWORDBREAKPROC
102 - EM_SETWORDBREAKPROCEX
103 - EM_SETWORDWRAPMODE 1.0asian
104 - EM_STOPGROUPTYPING 2.0
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)
117 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
119 - WM_STYLECHANGED (things like read-only flag)
124 * EN_CHANGE (sent from the wrong place)
141 * EN_UPDATE (sent from the wrong place)
149 - ES_DISABLENOSCROLL (scrollbar is always visible)
150 - ES_EX_NOCALLOLEINIT
152 - ES_MULTILINE (currently single line controls aren't supported)
154 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
160 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
167 * RICHED20 TODO (incomplete):
170 * - add remaining CHARFORMAT/PARAFORMAT fields
171 * - right/center align should strip spaces from the beginning
172 * - more advanced navigation (Ctrl-arrows, PageUp/PageDn)
174 * - pictures (not just smiling faces that lack API support ;-) )
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
183 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
184 * - italic cursor with italic fonts
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?
209 #define NO_SHLWAPI_STREAM
214 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
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);
230 p1->member.para.next_para = p2;
231 p2->member.para.prev_para = p1;
232 p2->member.para.nCharOfs = 0;
236 buf->pCharStyle = NULL;
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);
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);
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);
268 nWideChars = nDataSize>>1;
269 ME_InsertTextFromCursor(editor, 0, wszText, nWideChars, style);
270 if (nDataSize<STREAMIN_BUFFER_SIZE)
273 ME_CommitUndo(editor);
277 void ME_RTFCharAttrHook(RTF_Info *info)
280 fmt.cbSize = sizeof(fmt);
283 switch(info->rtfMinor)
286 fmt.dwMask = CFM_BOLD;
287 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
290 fmt.dwMask = CFM_ITALIC;
291 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
294 fmt.dwMask = CFM_UNDERLINE;
295 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
298 fmt.dwMask = CFM_STRIKEOUT;
299 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
302 fmt.dwMask = CFM_BACKCOLOR;
304 if (info->rtfParam == 0)
305 fmt.dwEffects = CFE_AUTOBACKCOLOR;
306 else if (info->rtfParam != rtfNoParam)
308 RTFColor *c = RTFGetColor(info, info->rtfParam);
309 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
313 fmt.dwMask = CFM_COLOR;
315 if (info->rtfParam == 0)
316 fmt.dwEffects = CFE_AUTOCOLOR;
317 else if (info->rtfParam != rtfNoParam)
319 RTFColor *c = RTFGetColor(info, info->rtfParam);
320 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
324 if (info->rtfParam != rtfNoParam)
326 RTFFont *f = RTFGetFont(info, info->rtfParam);
329 strncpy(fmt.szFaceName, f->rtfFName, sizeof(fmt.szFaceName)-1);
330 fmt.szFaceName[sizeof(fmt.szFaceName)-1] = '\0';
331 fmt.dwMask = CFM_FACE;
336 fmt.dwMask = CFM_SIZE;
337 if (info->rtfParam != rtfNoParam)
338 fmt.yHeight = info->rtfParam*10;
342 RTFFlushOutputBuffer(info);
343 SendMessageW(info->hwndEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
347 void ME_RTFParAttrHook(RTF_Info *info)
350 fmt.cbSize = sizeof(fmt);
353 switch(info->rtfMinor)
355 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
356 fmt.dwMask = PFM_ALIGNMENT;
357 fmt.wAlignment = PFA_LEFT;
361 fmt.dwMask = PFM_ALIGNMENT;
362 fmt.wAlignment = PFA_LEFT;
365 fmt.dwMask = PFM_ALIGNMENT;
366 fmt.wAlignment = PFA_RIGHT;
369 fmt.dwMask = PFM_ALIGNMENT;
370 fmt.wAlignment = PFA_CENTER;
374 RTFFlushOutputBuffer(info);
375 SendMessageW(info->hwndEdit, EM_SETPARAFORMAT, 0, (LPARAM)&fmt);
379 void ME_RTFReadHook(RTF_Info *info) {
380 switch(info->rtfClass)
383 switch(info->rtfMajor)
386 ME_RTFCharAttrHook(info);
389 ME_RTFParAttrHook(info);
396 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
400 int from, to, to2, nUndoMode;
403 TRACE("%p %p\n", stream, editor->hWnd);
405 ME_GetSelection(editor, &from, &to);
406 if (format & SFF_SELECTION) {
407 style = ME_GetSelectionInsertStyle(editor);
409 ME_InternalDeleteText(editor, from, to-from);
412 style = editor->pBuffer->pDefaultStyle;
413 ME_AddRefStyle(style);
414 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
415 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
417 ME_ClearTempStyle(editor);
420 nUndoMode = editor->nUndoMode;
421 editor->nUndoMode = umIgnore;
422 if (format & SF_RTF) {
423 /* setup the RTF parser */
424 memset(&parser, 0, sizeof parser);
425 RTFSetEditStream(&parser, stream);
426 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
427 parser.hwndEdit = editor->hWnd;
430 RTFSetReadHook(&parser, ME_RTFReadHook);
435 RTFFlushOutputBuffer(&parser);
437 else if (format & SF_TEXT)
438 ME_StreamInText(editor, format, stream, style);
440 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
441 ME_GetSelection(editor, &to, &to2);
442 /* put the cursor at the top */
443 if (!(format & SFF_SELECTION))
444 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
447 /* FIXME where to put cursor now ? */
450 editor->nUndoMode = nUndoMode;
451 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
452 FIXME("from %d to %d\n", from, to);
453 if (pUI && from < to)
458 ME_CommitUndo(editor);
459 ME_ReleaseStyle(style);
464 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
465 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
469 ed->pBuffer = ME_MakeText();
471 ME_MakeFirstParagraph(hDC, ed->pBuffer);
472 ReleaseDC(hWnd, hDC);
473 ed->bCaretShown = FALSE;
475 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
476 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
477 ed->pCursors[0].nOffset = 0;
478 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
479 ed->pCursors[1].nOffset = 0;
480 ed->nLastTotalLength = ed->nTotalLength = 0;
484 ed->rgbBackColor = -1;
485 ed->bCaretAtEnd = FALSE;
488 ed->pUndoStack = ed->pRedoStack = NULL;
489 ed->nUndoMode = umAddToUndo;
491 ed->nLastSelStart = ed->nLastSelEnd = 0;
492 for (i=0; i<HFONT_CACHE_SIZE; i++)
494 ed->pFontCache[i].nRefs = 0;
495 ed->pFontCache[i].nAge = 0;
496 ed->pFontCache[i].hFont = NULL;
498 ME_CheckCharOffsets(ed);
502 void ME_DestroyEditor(ME_TextEditor *editor)
504 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
505 ME_DisplayItem *p = pFirst, *pNext = NULL;
508 ME_ClearTempStyle(editor);
509 ME_EmptyUndoStack(editor);
512 ME_DestroyDisplayItem(p);
515 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
516 for (i=0; i<HFONT_CACHE_SIZE; i++)
518 if (editor->pFontCache[i].hFont)
519 DeleteObject(editor->pFontCache[i].hFont);
525 #define UNSUPPORTED_MSG(e) \
527 FIXME(#e ": stub\n"); \
528 return DefWindowProcW(hWnd, msg, wParam, lParam);
530 /******************************************************************
531 * RichEditANSIWndProc (RICHED20.10)
533 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
537 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
538 TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
541 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
542 UNSUPPORTED_MSG(EM_CANPASTE)
543 UNSUPPORTED_MSG(EM_CHARFROMPOS)
544 UNSUPPORTED_MSG(EM_DISPLAYBAND)
545 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
546 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
547 UNSUPPORTED_MSG(EM_FINDTEXT)
548 UNSUPPORTED_MSG(EM_FINDTEXTEX)
549 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
550 UNSUPPORTED_MSG(EM_FMTLINES)
551 UNSUPPORTED_MSG(EM_FORMATRANGE)
552 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
553 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
554 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
555 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
556 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
557 UNSUPPORTED_MSG(EM_GETLINE)
558 UNSUPPORTED_MSG(EM_GETLINECOUNT)
559 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
560 UNSUPPORTED_MSG(EM_GETOPTIONS)
561 UNSUPPORTED_MSG(EM_GETRECT)
562 UNSUPPORTED_MSG(EM_GETREDONAME)
563 UNSUPPORTED_MSG(EM_GETTEXTMODE)
564 UNSUPPORTED_MSG(EM_GETUNDONAME)
565 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
566 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
567 UNSUPPORTED_MSG(EM_HIDESELECTION)
568 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
569 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
570 UNSUPPORTED_MSG(EM_LINEINDEX)
571 UNSUPPORTED_MSG(EM_LINELENGTH)
572 UNSUPPORTED_MSG(EM_LINESCROLL)
573 UNSUPPORTED_MSG(EM_PASTESPECIAL)
574 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
575 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
576 UNSUPPORTED_MSG(EM_SCROLL)
577 UNSUPPORTED_MSG(EM_SCROLLCARET)
578 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
579 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
580 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
581 UNSUPPORTED_MSG(EM_SETOPTIONS)
582 UNSUPPORTED_MSG(EM_SETRECT)
583 UNSUPPORTED_MSG(EM_SETRECTNP)
584 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
585 UNSUPPORTED_MSG(EM_SETTEXTMODE)
586 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
587 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
588 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
589 UNSUPPORTED_MSG(EM_STREAMOUT)
590 UNSUPPORTED_MSG(WM_SETFONT)
591 UNSUPPORTED_MSG(WM_PASTE)
592 UNSUPPORTED_MSG(WM_STYLECHANGING)
593 UNSUPPORTED_MSG(WM_STYLECHANGED)
594 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
596 /* Messages specific to Richedit controls */
599 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
602 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
603 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
604 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
609 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
610 editor = ME_MakeEditor(hWnd);
611 SetWindowLongW(hWnd, 0, (long)editor);
612 pcs = 0; /* ignore */
615 case EM_EMPTYUNDOBUFFER:
616 ME_EmptyUndoStack(editor);
620 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
621 if (!((wParam|lParam) & 0xFFFF0000))
622 return (lParam<<16)|wParam;
627 CHARRANGE *pRange = (CHARRANGE *)lParam;
628 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
632 return editor->pUndoStack != NULL;
634 return editor->pRedoStack != NULL;
643 ME_SetSelection(editor, wParam, lParam);
645 ME_SendSelChange(editor);
650 CHARRANGE *pRange = (CHARRANGE *)lParam;
651 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
654 ME_SendSelChange(editor);
657 case EM_SETBKGNDCOLOR:
659 LRESULT lColor = ME_GetBackColor(editor);
661 editor->rgbBackColor = -1;
663 editor->rgbBackColor = lParam;
664 InvalidateRect(hWnd, NULL, TRUE);
669 return editor->nModifyStep == 0 ? 0 : 1;
673 editor->nModifyStep = 0x80000000;
675 editor->nModifyStep = 0;
681 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
683 nStyle |= ES_READONLY;
685 nStyle &= ~ES_READONLY;
686 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
690 case EM_SETEVENTMASK:
691 editor->nEventMask = lParam;
693 case EM_GETEVENTMASK:
694 return editor->nEventMask;
695 case EM_SETCHARFORMAT:
697 CHARFORMAT2W buf, *p;
698 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
700 ME_SetDefaultCharFormat(editor, p);
701 else if (wParam == (SCF_WORD | SCF_SELECTION))
702 FIXME("word selection not supported\n");
703 else if (wParam == SCF_ALL)
704 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
706 ME_SetSelectionCharFormat(editor, p);
707 ME_CommitUndo(editor);
708 ME_UpdateRepaint(editor);
711 case EM_GETCHARFORMAT:
714 tmp.cbSize = sizeof(tmp);
716 ME_GetDefaultCharFormat(editor, &tmp);
718 ME_GetSelectionCharFormat(editor, &tmp);
719 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
722 case EM_SETPARAFORMAT:
723 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
724 ME_CommitUndo(editor);
726 case EM_GETPARAFORMAT:
727 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
732 ME_GetSelection(editor, &from, &to);
733 ME_InternalDeleteText(editor, from, to-from);
734 ME_CommitUndo(editor);
735 ME_UpdateRepaint(editor);
742 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
743 size_t len = lstrlenW(wszText);
744 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
746 ME_GetSelection(editor, &from, &to);
747 style = ME_GetSelectionInsertStyle(editor);
748 ME_InternalDeleteText(editor, from, to-from);
749 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
750 ME_ReleaseStyle(style);
751 ME_EndToUnicode(hWnd, wszText);
752 /* drop temporary style if line end */
753 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
754 if (len>0 && wszText[len-1] == '\n')
755 ME_ClearTempStyle(editor);
757 ME_CommitUndo(editor);
759 ME_EmptyUndoStack(editor);
760 ME_UpdateRepaint(editor);
765 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
766 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
767 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
768 /* uses default style! */
769 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
770 ME_EndToUnicode(hWnd, wszText);
771 ME_CommitUndo(editor);
772 ME_EmptyUndoStack(editor);
773 ME_UpdateRepaint(editor);
783 if (!OpenClipboard(hWnd))
787 ME_GetSelection(editor, &from, &to);
788 pars = ME_CountParagraphsBetween(editor, from, to);
789 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
790 data = (WCHAR *)GlobalLock(hData);
791 ME_GetTextW(editor, data, from, to-from, TRUE);
793 SetClipboardData(CF_UNICODETEXT, hData);
797 ME_InternalDeleteText(editor, from, to-from);
798 ME_CommitUndo(editor);
799 ME_UpdateRepaint(editor);
803 case WM_GETTEXTLENGTH:
804 return ME_GetTextLength(editor);
807 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
809 tr.chrg.cpMax = wParam-1;
810 tr.lpstrText = (WCHAR *)lParam;
811 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
816 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
817 ME_GetSelection(editor, &from, &to);
818 tr.chrg.cpMin = from;
820 tr.lpstrText = (WCHAR *)lParam;
821 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
823 case EM_GETTEXTRANGE:
825 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
826 if (IsWindowUnicode(hWnd))
827 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
830 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
831 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
832 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
833 /* FIXME this is a potential security hole (buffer overrun)
834 if you know more about wchar->mbyte conversion please explain
836 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
840 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
843 ME_CommitUndo(editor);
844 ME_WrapMarkedParagraphs(editor);
845 ME_MoveCaret(editor);
848 ME_DestroyEditor(editor);
849 SetWindowLongW(hWnd, 0, 0);
853 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
857 if (GetCapture() == hWnd)
858 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
861 if (GetCapture() == hWnd)
865 hDC = BeginPaint(hWnd, &ps);
866 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
870 ME_ShowCaret(editor);
871 ME_SendOldNotify(editor, EN_SETFOCUS);
874 ME_HideCaret(editor);
875 ME_SendOldNotify(editor, EN_KILLFOCUS);
879 HDC hDC = (HDC)wParam;
881 COLORREF rgbBG = ME_GetBackColor(editor);
882 if (GetUpdateRect(hWnd,&rc,TRUE))
884 HBRUSH hbr = CreateSolidBrush(rgbBG);
885 FillRect(hDC, &rc, hbr);
891 TRACE("editor wnd command = %d\n", LOWORD(wParam));
894 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
895 ME_CommitUndo(editor);
896 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
898 ME_MoveCaret(editor);
902 if (GetKeyState(VK_CONTROL)<0)
904 if (LOWORD(wParam)=='W')
908 ME_GetSelectionCharFormat(editor, &chf);
909 ME_DumpStyleToBuf(&chf, buf);
910 MessageBoxA(NULL, buf, "Style dump", MB_OK);
912 if (LOWORD(wParam)=='Q')
914 ME_CheckCharOffsets(editor);
921 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
922 MessageBeep(MB_ICONERROR);
923 return 0; /* FIXME really 0 ? */
925 wstr = LOWORD(wParam);
926 if (((unsigned)wstr)>=' ' || wstr=='\r') {
927 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
928 ME_Style *style = ME_GetInsertStyle(editor, 0);
929 ME_SaveTempStyle(editor);
930 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
931 ME_ReleaseStyle(style);
932 ME_CommitUndo(editor);
933 ME_UpdateRepaint(editor);
939 si.cbSize = sizeof(SCROLLINFO);
940 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
941 GetScrollInfo(hWnd, SB_VERT, &si);
942 switch(LOWORD(wParam)) {
944 SetScrollPos(hWnd, SB_VERT, si.nTrackPos, FALSE);
945 ScrollWindow(hWnd, 0, si.nPos-si.nTrackPos, NULL, NULL);
946 /* InvalidateRect(hWnd, NULL, TRUE); */
954 ME_MarkAllForWrapping(editor);
956 return DefWindowProcW(hWnd, msg, wParam, lParam);
958 case EM_GETOLEINTERFACE:
960 LPVOID *ppvObj = (LPVOID*) lParam;
961 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
962 return CreateIRichEditOle(ppvObj);
966 return DefWindowProcW(hWnd, msg, wParam, lParam);
971 /******************************************************************
972 * RichEdit10ANSIWndProc (RICHED20.9)
974 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
976 /* FIXME: this is NOT the same as 2.0 version */
977 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
980 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
982 HWND hWnd = editor->hWnd;
983 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
986 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
988 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
991 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
992 item = item->member.para.next_para;
995 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
996 item = item->member.para.next_para;
1002 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1004 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1007 while(item && item->member.para.next_para->member.para.nCharOfs <= nStart)
1008 item = ME_FindItemFwd(item, diParagraph);
1013 nStart -= item->member.para.nCharOfs;
1016 item = ME_FindItemFwd(item, diRun);
1017 } while(item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nStart));
1020 nStart -= item->member.run.nCharOfs;
1024 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1027 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1034 item = ME_FindItemFwd(item, diRun);
1037 while(nChars && item)
1039 int nLen = ME_StrLen(item->member.run.strText);
1043 if (item->member.run.nFlags & MERF_ENDPARA)
1053 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1063 item = ME_FindItemFwd(item, diRun);
1069 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1070 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1072 void ME_RegisterEditorClass(HINSTANCE hInstance)
1078 wcW.style = CS_HREDRAW | CS_VREDRAW;
1079 wcW.lpfnWndProc = RichEditANSIWndProc;
1082 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1084 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1085 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1086 wcW.lpszMenuName = NULL;
1087 wcW.lpszClassName = wszClassName;
1088 bResult = RegisterClassW(&wcW);
1090 wcW.lpszClassName = wszClassName50;
1091 bResult = RegisterClassW(&wcW);
1094 wcA.style = CS_HREDRAW | CS_VREDRAW;
1095 wcA.lpfnWndProc = RichEditANSIWndProc;
1098 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1100 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1101 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1102 wcA.lpszMenuName = NULL;
1103 wcA.lpszClassName = "RichEdit20A";
1104 bResult = RegisterClassA(&wcA);
1106 wcA.lpszClassName = "RichEdit50A";
1107 bResult = RegisterClassA(&wcA);
1111 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1116 case DLL_PROCESS_ATTACH:
1117 DisableThreadLibraryCalls(hinstDLL);
1118 me_heap = HeapCreate (0, 0x10000, 0);
1119 ME_RegisterEditorClass(hinstDLL);
1122 case DLL_PROCESS_DETACH:
1123 UnregisterClassW(wszClassName, 0);
1124 UnregisterClassW(wszClassName50, 0);
1125 UnregisterClassA("RichEdit20A", 0);
1126 UnregisterClassA("RichEdit50A", 0);
1127 HeapDestroy (me_heap);
1134 /******************************************************************
1135 * CreateTextServices (RICHED20.4)
1137 * FIXME should be ITextHost instead of void*
1139 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1143 /* FIXME should support aggregation */
1145 return CLASS_E_NOAGGREGATION;
1147 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1150 /******************************************************************
1151 * REExtendedRegisterClass (RICHED20.8)
1153 * FIXME undocumented
1155 void WINAPI REExtendedRegisterClass(void)