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
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);
278 void ME_RTFCharAttrHook(RTF_Info *info)
281 fmt.cbSize = sizeof(fmt);
284 switch(info->rtfMinor)
287 /* FIXME add more flags once they're implemented */
288 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
289 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
290 fmt.yHeight = 12*20; /* 12pt */
294 fmt.dwMask = CFM_BOLD;
295 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
298 fmt.dwMask = CFM_ITALIC;
299 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
302 fmt.dwMask = CFM_UNDERLINE;
303 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
306 fmt.dwMask = CFM_STRIKEOUT;
307 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
310 fmt.dwMask = CFM_BACKCOLOR;
312 if (info->rtfParam == 0)
313 fmt.dwEffects = CFE_AUTOBACKCOLOR;
314 else if (info->rtfParam != rtfNoParam)
316 RTFColor *c = RTFGetColor(info, info->rtfParam);
317 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
321 fmt.dwMask = CFM_COLOR;
323 if (info->rtfParam == 0)
324 fmt.dwEffects = CFE_AUTOCOLOR;
325 else if (info->rtfParam != rtfNoParam)
327 RTFColor *c = RTFGetColor(info, info->rtfParam);
328 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
332 if (info->rtfParam != rtfNoParam)
334 RTFFont *f = RTFGetFont(info, info->rtfParam);
337 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
338 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
339 fmt.dwMask = CFM_FACE;
344 fmt.dwMask = CFM_SIZE;
345 if (info->rtfParam != rtfNoParam)
346 fmt.yHeight = info->rtfParam*10;
351 RTFFlushOutputBuffer(info);
352 /* FIXME too slow ? how come ? */
353 style2 = ME_ApplyStyle(info->style, &fmt);
354 ME_ReleaseStyle(info->style);
355 info->style = style2;
359 void ME_RTFParAttrHook(RTF_Info *info)
362 fmt.cbSize = sizeof(fmt);
365 switch(info->rtfMinor)
367 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
368 fmt.dwMask = PFM_ALIGNMENT;
369 fmt.wAlignment = PFA_LEFT;
373 fmt.dwMask = PFM_ALIGNMENT;
374 fmt.wAlignment = PFA_LEFT;
377 fmt.dwMask = PFM_ALIGNMENT;
378 fmt.wAlignment = PFA_RIGHT;
381 fmt.dwMask = PFM_ALIGNMENT;
382 fmt.wAlignment = PFA_CENTER;
386 RTFFlushOutputBuffer(info);
387 /* FIXME too slow ? how come ?*/
388 ME_SetSelectionParaFormat(info->editor, &fmt);
392 void ME_RTFReadHook(RTF_Info *info) {
393 switch(info->rtfClass)
396 switch(info->rtfMajor)
399 if (info->formatStackTop < maxCharFormatStack) {
400 info->formatStack[info->formatStackTop].cbSize = sizeof(info->formatStack[0]);
401 memcpy(&info->formatStack[info->formatStackTop], &info->style->fmt, sizeof(CHARFORMAT2W));
402 info->codePageStack[info->formatStackTop] = info->codePage;
404 info->formatStackTop++;
409 RTFFlushOutputBuffer(info);
410 info->formatStackTop--;
411 /* FIXME too slow ? how come ? */
412 s = ME_ApplyStyle(info->style, &info->formatStack[info->formatStackTop]);
413 ME_ReleaseStyle(info->style);
415 info->codePage = info->codePageStack[info->formatStackTop];
421 switch(info->rtfMajor)
424 ME_RTFCharAttrHook(info);
427 ME_RTFParAttrHook(info);
434 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
438 int from, to, to2, nUndoMode;
440 int nEventMask = editor->nEventMask;
442 TRACE("%p %p\n", stream, editor->hWnd);
443 editor->nEventMask = 0;
445 ME_GetSelection(editor, &from, &to);
446 if (format & SFF_SELECTION) {
447 style = ME_GetSelectionInsertStyle(editor);
449 ME_InternalDeleteText(editor, from, to-from);
452 style = editor->pBuffer->pDefaultStyle;
453 ME_AddRefStyle(style);
454 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
455 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
457 ME_ClearTempStyle(editor);
458 /* FIXME restore default paragraph formatting ! */
461 nUndoMode = editor->nUndoMode;
462 editor->nUndoMode = umIgnore;
463 if (format & SF_RTF) {
464 /* setup the RTF parser */
465 memset(&parser, 0, sizeof parser);
466 RTFSetEditStream(&parser, stream);
467 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
468 parser.hwndEdit = editor->hWnd;
469 parser.editor = editor;
470 parser.style = style;
473 RTFSetReadHook(&parser, ME_RTFReadHook);
478 RTFFlushOutputBuffer(&parser);
480 style = parser.style;
482 else if (format & SF_TEXT)
483 ME_StreamInText(editor, format, stream, style);
485 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
486 ME_GetSelection(editor, &to, &to2);
487 /* put the cursor at the top */
488 if (!(format & SFF_SELECTION))
489 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
492 /* FIXME where to put cursor now ? */
495 editor->nUndoMode = nUndoMode;
496 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
497 TRACE("from %d to %d\n", from, to);
498 if (pUI && from < to)
503 ME_CommitUndo(editor);
504 ME_ReleaseStyle(style);
505 editor->nEventMask = nEventMask;
506 ME_UpdateRepaint(editor);
507 if (!(format & SFF_SELECTION)) {
508 ME_ClearTempStyle(editor);
509 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
511 ME_MoveCaret(editor);
512 ME_SendSelChange(editor);
519 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
521 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
523 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
524 item = ME_FindItemFwd(item, diParagraph);
529 nOffset -= item->member.para.nCharOfs;
530 if (nItemType == diParagraph) {
532 *nItemOffset = nOffset;
537 item = ME_FindItemFwd(item, diRun);
538 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
540 nOffset -= item->member.run.nCharOfs;
542 *nItemOffset = nOffset;
548 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
549 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
553 ed->pBuffer = ME_MakeText();
555 ME_MakeFirstParagraph(hDC, ed->pBuffer);
556 ReleaseDC(hWnd, hDC);
557 ed->bCaretShown = FALSE;
559 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
560 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
561 ed->pCursors[0].nOffset = 0;
562 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
563 ed->pCursors[1].nOffset = 0;
564 ed->nLastTotalLength = ed->nTotalLength = 0;
568 ed->rgbBackColor = -1;
569 ed->bCaretAtEnd = FALSE;
572 ed->pUndoStack = ed->pRedoStack = NULL;
573 ed->nUndoMode = umAddToUndo;
575 ed->nLastSelStart = ed->nLastSelEnd = 0;
576 for (i=0; i<HFONT_CACHE_SIZE; i++)
578 ed->pFontCache[i].nRefs = 0;
579 ed->pFontCache[i].nAge = 0;
580 ed->pFontCache[i].hFont = NULL;
582 ME_CheckCharOffsets(ed);
586 typedef struct tagME_GlobalDestStruct
590 } ME_GlobalDestStruct;
592 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
594 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
598 nMaxSize = GlobalSize(pData->hData);
599 if (pData->nLength+cb+1 >= cb)
601 /* round up to 2^17 */
602 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
603 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
605 pDest = (BYTE *)GlobalLock(pData->hData);
606 memcpy(pDest + pData->nLength, lpBuff, cb);
607 pData->nLength += cb;
608 pDest[pData->nLength] = '\0';
609 GlobalUnlock(pData->hData);
615 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
617 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
622 pDest = (WORD *)lpBuff;
623 pSrc = (WORD *)GlobalLock(pData->hData);
624 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
625 pDest[i] = pSrc[pData->nLength+i];
629 GlobalUnlock(pData->hData);
633 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
635 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
640 pSrc = (BYTE *)GlobalLock(pData->hData);
641 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
642 pDest[i] = pSrc[pData->nLength+i];
646 GlobalUnlock(pData->hData);
650 void ME_DestroyEditor(ME_TextEditor *editor)
652 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
653 ME_DisplayItem *p = pFirst, *pNext = NULL;
656 ME_ClearTempStyle(editor);
657 ME_EmptyUndoStack(editor);
660 ME_DestroyDisplayItem(p);
663 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
664 for (i=0; i<HFONT_CACHE_SIZE; i++)
666 if (editor->pFontCache[i].hFont)
667 DeleteObject(editor->pFontCache[i].hFont);
673 #define UNSUPPORTED_MSG(e) \
675 FIXME(#e ": stub\n"); \
676 return DefWindowProcW(hWnd, msg, wParam, lParam);
678 /******************************************************************
679 * RichEditANSIWndProc (RICHED20.10)
681 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
685 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
686 TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
689 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
690 UNSUPPORTED_MSG(EM_CANPASTE)
691 UNSUPPORTED_MSG(EM_CHARFROMPOS)
692 UNSUPPORTED_MSG(EM_DISPLAYBAND)
693 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
694 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
695 UNSUPPORTED_MSG(EM_FINDTEXT)
696 UNSUPPORTED_MSG(EM_FINDTEXTEX)
697 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
698 UNSUPPORTED_MSG(EM_FMTLINES)
699 UNSUPPORTED_MSG(EM_FORMATRANGE)
700 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
701 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
702 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
703 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
704 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
705 UNSUPPORTED_MSG(EM_GETLINE)
706 UNSUPPORTED_MSG(EM_GETLINECOUNT)
707 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
708 UNSUPPORTED_MSG(EM_GETOPTIONS)
709 UNSUPPORTED_MSG(EM_GETRECT)
710 UNSUPPORTED_MSG(EM_GETREDONAME)
711 UNSUPPORTED_MSG(EM_GETTEXTMODE)
712 UNSUPPORTED_MSG(EM_GETUNDONAME)
713 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
714 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
715 UNSUPPORTED_MSG(EM_HIDESELECTION)
716 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
717 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
718 UNSUPPORTED_MSG(EM_LINEINDEX)
719 UNSUPPORTED_MSG(EM_LINELENGTH)
720 UNSUPPORTED_MSG(EM_LINESCROLL)
721 UNSUPPORTED_MSG(EM_PASTESPECIAL)
722 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
723 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
724 UNSUPPORTED_MSG(EM_SCROLL)
725 UNSUPPORTED_MSG(EM_SCROLLCARET)
726 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
727 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
728 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
729 UNSUPPORTED_MSG(EM_SETOPTIONS)
730 UNSUPPORTED_MSG(EM_SETRECT)
731 UNSUPPORTED_MSG(EM_SETRECTNP)
732 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
733 UNSUPPORTED_MSG(EM_SETTEXTMODE)
734 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
735 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
736 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
737 UNSUPPORTED_MSG(WM_SETFONT)
738 UNSUPPORTED_MSG(WM_STYLECHANGING)
739 UNSUPPORTED_MSG(WM_STYLECHANGED)
740 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
742 /* Messages specific to Richedit controls */
745 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
747 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
750 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
751 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
752 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
757 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
758 editor = ME_MakeEditor(hWnd);
759 SetWindowLongW(hWnd, 0, (long)editor);
760 pcs = 0; /* ignore */
763 case EM_EMPTYUNDOBUFFER:
764 ME_EmptyUndoStack(editor);
768 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
769 if (!((wParam|lParam) & 0xFFFF0000))
770 return (lParam<<16)|wParam;
775 CHARRANGE *pRange = (CHARRANGE *)lParam;
776 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
780 return editor->pUndoStack != NULL;
782 return editor->pRedoStack != NULL;
791 ME_SetSelection(editor, wParam, lParam);
793 ME_SendSelChange(editor);
798 CHARRANGE *pRange = (CHARRANGE *)lParam;
799 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
802 ME_SendSelChange(editor);
805 case EM_SETBKGNDCOLOR:
807 LRESULT lColor = ME_GetBackColor(editor);
809 editor->rgbBackColor = -1;
811 editor->rgbBackColor = lParam;
812 InvalidateRect(hWnd, NULL, TRUE);
817 return editor->nModifyStep == 0 ? 0 : 1;
821 editor->nModifyStep = 0x80000000;
823 editor->nModifyStep = 0;
829 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
831 nStyle |= ES_READONLY;
833 nStyle &= ~ES_READONLY;
834 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
838 case EM_SETEVENTMASK:
839 editor->nEventMask = lParam;
841 case EM_GETEVENTMASK:
842 return editor->nEventMask;
843 case EM_SETCHARFORMAT:
845 CHARFORMAT2W buf, *p;
846 BOOL bRepaint = TRUE;
847 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
849 ME_SetDefaultCharFormat(editor, p);
850 else if (wParam == (SCF_WORD | SCF_SELECTION))
851 FIXME("word selection not supported\n");
852 else if (wParam == SCF_ALL)
853 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
856 ME_GetSelection(editor, &from, &to);
857 bRepaint = (from != to);
858 ME_SetSelectionCharFormat(editor, p);
860 ME_CommitUndo(editor);
862 ME_UpdateRepaint(editor);
865 case EM_GETCHARFORMAT:
868 tmp.cbSize = sizeof(tmp);
870 ME_GetDefaultCharFormat(editor, &tmp);
872 ME_GetSelectionCharFormat(editor, &tmp);
873 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
876 case EM_SETPARAFORMAT:
877 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
878 ME_UpdateRepaint(editor);
879 ME_CommitUndo(editor);
881 case EM_GETPARAFORMAT:
882 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
887 ME_GetSelection(editor, &from, &to);
888 ME_InternalDeleteText(editor, from, to-from);
889 ME_CommitUndo(editor);
890 ME_UpdateRepaint(editor);
897 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
898 size_t len = lstrlenW(wszText);
899 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
901 ME_GetSelection(editor, &from, &to);
902 style = ME_GetSelectionInsertStyle(editor);
903 ME_InternalDeleteText(editor, from, to-from);
904 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
905 ME_ReleaseStyle(style);
906 ME_EndToUnicode(hWnd, wszText);
907 /* drop temporary style if line end */
908 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
909 if (len>0 && wszText[len-1] == '\n')
910 ME_ClearTempStyle(editor);
912 ME_CommitUndo(editor);
914 ME_EmptyUndoStack(editor);
915 ME_UpdateRepaint(editor);
920 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
921 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
922 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
923 /* uses default style! */
924 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
925 ME_EndToUnicode(hWnd, wszText);
926 ME_CommitUndo(editor);
927 ME_EmptyUndoStack(editor);
928 ME_UpdateRepaint(editor);
935 ME_GlobalDestStruct gds;
936 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
939 if (!OpenClipboard(hWnd))
941 if (IsClipboardFormatAvailable(nRTFFormat))
942 cf = nRTFFormat, dwFormat = SF_RTF;
943 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
944 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
948 gds.hData = GetClipboardData(cf);
950 es.dwCookie = (DWORD)&gds;
951 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
952 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
964 ME_GlobalDestStruct gds;
966 if (!OpenClipboard(hWnd))
970 ME_GetSelection(editor, &from, &to);
971 pars = ME_CountParagraphsBetween(editor, from, to);
972 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
973 data = (WCHAR *)GlobalLock(hData);
974 ME_GetTextW(editor, data, from, to-from, TRUE);
977 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
979 es.dwCookie = (DWORD)&gds;
980 es.pfnCallback = ME_AppendToHGLOBAL;
981 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
982 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
984 SetClipboardData(CF_UNICODETEXT, hData);
985 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
990 ME_InternalDeleteText(editor, from, to-from);
991 ME_CommitUndo(editor);
992 ME_UpdateRepaint(editor);
996 case WM_GETTEXTLENGTH:
997 return ME_GetTextLength(editor);
1000 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1002 tr.chrg.cpMax = wParam-1;
1003 tr.lpstrText = (WCHAR *)lParam;
1004 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1009 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1010 ME_GetSelection(editor, &from, &to);
1011 tr.chrg.cpMin = from;
1013 tr.lpstrText = (WCHAR *)lParam;
1014 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1016 case EM_GETTEXTRANGE:
1018 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1019 if (IsWindowUnicode(hWnd))
1020 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1023 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1024 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1025 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
1026 /* FIXME this is a potential security hole (buffer overrun)
1027 if you know more about wchar->mbyte conversion please explain
1029 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1033 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1036 ME_CommitUndo(editor);
1037 ME_WrapMarkedParagraphs(editor);
1038 ME_MoveCaret(editor);
1041 ME_DestroyEditor(editor);
1042 SetWindowLongW(hWnd, 0, 0);
1044 case WM_LBUTTONDOWN:
1046 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1050 if (GetCapture() == hWnd)
1051 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1054 if (GetCapture() == hWnd)
1058 hDC = BeginPaint(hWnd, &ps);
1059 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1060 EndPaint(hWnd, &ps);
1063 ME_ShowCaret(editor);
1064 ME_SendOldNotify(editor, EN_SETFOCUS);
1067 ME_HideCaret(editor);
1068 ME_SendOldNotify(editor, EN_KILLFOCUS);
1072 HDC hDC = (HDC)wParam;
1074 COLORREF rgbBG = ME_GetBackColor(editor);
1075 if (GetUpdateRect(hWnd,&rc,TRUE))
1077 HBRUSH hbr = CreateSolidBrush(rgbBG);
1078 FillRect(hDC, &rc, hbr);
1084 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1087 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1088 ME_CommitUndo(editor);
1089 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1091 ME_MoveCaret(editor);
1095 if (GetKeyState(VK_CONTROL)<0)
1097 if (LOWORD(wParam)=='W')
1101 ME_GetSelectionCharFormat(editor, &chf);
1102 ME_DumpStyleToBuf(&chf, buf);
1103 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1105 if (LOWORD(wParam)=='Q')
1107 ME_CheckCharOffsets(editor);
1114 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1115 MessageBeep(MB_ICONERROR);
1116 return 0; /* FIXME really 0 ? */
1118 wstr = LOWORD(wParam);
1119 if (((unsigned)wstr)>=' ' || wstr=='\r') {
1120 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1121 ME_Style *style = ME_GetInsertStyle(editor, 0);
1122 ME_SaveTempStyle(editor);
1123 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1124 ME_ReleaseStyle(style);
1125 ME_CommitUndo(editor);
1126 ME_UpdateRepaint(editor);
1132 si.cbSize = sizeof(SCROLLINFO);
1133 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1134 GetScrollInfo(hWnd, SB_VERT, &si);
1135 switch(LOWORD(wParam)) {
1137 SetScrollPos(hWnd, SB_VERT, si.nTrackPos, FALSE);
1138 ScrollWindow(hWnd, 0, si.nPos-si.nTrackPos, NULL, NULL);
1139 /* InvalidateRect(hWnd, NULL, TRUE); */
1147 ME_MarkAllForWrapping(editor);
1149 return DefWindowProcW(hWnd, msg, wParam, lParam);
1151 case EM_GETOLEINTERFACE:
1153 LPVOID *ppvObj = (LPVOID*) lParam;
1154 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1155 return CreateIRichEditOle(ppvObj);
1159 return DefWindowProcW(hWnd, msg, wParam, lParam);
1164 /******************************************************************
1165 * RichEdit10ANSIWndProc (RICHED20.9)
1167 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1169 /* FIXME: this is NOT the same as 2.0 version */
1170 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1173 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1175 HWND hWnd = editor->hWnd;
1176 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1179 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1181 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1184 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1185 item = item->member.para.next_para;
1188 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1189 item = item->member.para.next_para;
1196 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1198 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1209 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1212 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1219 item = ME_FindItemFwd(item, diRun);
1222 while(nChars && item)
1224 int nLen = ME_StrLen(item->member.run.strText);
1228 if (item->member.run.nFlags & MERF_ENDPARA)
1238 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1248 item = ME_FindItemFwd(item, diRun);
1254 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1255 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1257 void ME_RegisterEditorClass(HINSTANCE hInstance)
1263 wcW.style = CS_HREDRAW | CS_VREDRAW;
1264 wcW.lpfnWndProc = RichEditANSIWndProc;
1267 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1269 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1270 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1271 wcW.lpszMenuName = NULL;
1272 wcW.lpszClassName = wszClassName;
1273 bResult = RegisterClassW(&wcW);
1275 wcW.lpszClassName = wszClassName50;
1276 bResult = RegisterClassW(&wcW);
1279 wcA.style = CS_HREDRAW | CS_VREDRAW;
1280 wcA.lpfnWndProc = RichEditANSIWndProc;
1283 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1285 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1286 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1287 wcA.lpszMenuName = NULL;
1288 wcA.lpszClassName = "RichEdit20A";
1289 bResult = RegisterClassA(&wcA);
1291 wcA.lpszClassName = "RichEdit50A";
1292 bResult = RegisterClassA(&wcA);
1296 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1301 case DLL_PROCESS_ATTACH:
1302 DisableThreadLibraryCalls(hinstDLL);
1303 me_heap = HeapCreate (0, 0x10000, 0);
1304 ME_RegisterEditorClass(hinstDLL);
1307 case DLL_PROCESS_DETACH:
1308 UnregisterClassW(wszClassName, 0);
1309 UnregisterClassW(wszClassName50, 0);
1310 UnregisterClassA("RichEdit20A", 0);
1311 UnregisterClassA("RichEdit50A", 0);
1312 HeapDestroy (me_heap);
1319 /******************************************************************
1320 * CreateTextServices (RICHED20.4)
1322 * FIXME should be ITextHost instead of void*
1324 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1328 /* FIXME should support aggregation */
1330 return CLASS_E_NOAGGREGATION;
1332 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1335 /******************************************************************
1336 * REExtendedRegisterClass (RICHED20.8)
1338 * FIXME undocumented
1340 void WINAPI REExtendedRegisterClass(void)