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
36 + EM_FINDTEXT (only FR_DOWN flag implemented)
37 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
41 - EM_GETAUTOURLDETECT 2.0
42 - EM_GETBIDIOPTIONS 3.0
43 - EM_GETCHARFORMAT (partly done)
46 - EM_GETFIRSTVISIBLELINE
47 - EM_GETIMECOLOR 1.0asian
48 - EM_GETIMECOMPMODE 2.0
49 - EM_GETIMEOPTIONS 1.0asian
51 - EM_GETLANGOPTIONS 2.0
54 + EM_GETLINECOUNT returns number of rows, not of paragraphs
59 - EM_GETPASSWORDCHAR 2.0
60 - EM_GETPUNCTUATION 1.0asian
64 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
70 ? + EM_GETTEXTRANGE (ANSI&Unicode)
71 - EM_GETTYPOGRAPHYOPTIONS 3.0
74 - EM_GETWORDBREAKPROCEX
75 - EM_GETWORDWRAPMODE 1.0asian
87 + EM_REPLACESEL (proper style?) ANSI&Unicode
91 - EM_SETBIDIOPTIONS 3.0
93 - EM_SETCHARFORMAT (partly done, no ANSI)
95 + EM_SETEVENTMASK (few notifications supported)
97 - EM_SETIMECOLOR 1.0asian
98 - EM_SETIMEOPTIONS 1.0asian
99 - EM_SETLANGOPTIONS 2.0
101 + EM_SETMODIFY (not sure if implementation is correct)
106 - EM_SETPASSWORDCHAR 2.0
107 - EM_SETPUNCTUATION 1.0asian
108 + EM_SETREADONLY no beep on modification attempt
110 + EM_SETRECTNP (EM_SETRECT without repainting)
112 - EM_SETSCROLLPOS 3.0
115 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
117 - EM_SETTYPOGRAPHYOPTIONS 3.0
118 - EM_SETUNDOLIMIT 2.0
119 - EM_SETWORDBREAKPROC
120 - EM_SETWORDBREAKPROCEX
121 - EM_SETWORDWRAPMODE 1.0asian
123 - EM_SHOWSCROLLBAR 2.0
124 - EM_STOPGROUPTYPING 2.0
132 + WM_GETDLGCODE (the current implementation is incomplete)
133 + WM_GETTEXT (ANSI&Unicode)
134 + WM_GETTEXTLENGTH (ANSI version sucks)
137 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
139 - WM_STYLECHANGED (things like read-only flag)
144 * EN_CHANGE (sent from the wrong place)
161 * EN_UPDATE (sent from the wrong place)
169 - ES_DISABLENOSCROLL (scrollbar is always visible)
170 - ES_EX_NOCALLOLEINIT
172 - ES_MULTILINE (currently single line controls aren't supported)
174 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
180 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
187 * RICHED20 TODO (incomplete):
189 * - messages/styles/notifications listed above
191 * - add remaining CHARFORMAT/PARAFORMAT fields
192 * - right/center align should strip spaces from the beginning
193 * - more advanced navigation (Ctrl-arrows)
195 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
196 * - COM interface (looks like a major pain in the TODO list)
197 * - calculate heights of pictures (half-done)
198 * - horizontal scrolling (not even started)
199 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
201 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
202 * - italic caret with italic fonts
204 * - most notifications aren't sent at all (the most important ones are)
205 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
206 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
207 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
208 * - full justification
212 * Bugs that are probably fixed, but not so easy to verify:
213 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
214 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
215 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
216 * - caret shouldn't be displayed when selection isn't empty
217 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
218 * - undo for setting default format (done, might be buggy)
219 * - styles might be not released properly (looks like they work like charm, but who knows?
228 #define NO_SHLWAPI_STREAM
233 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
236 HANDLE me_heap = NULL;
238 static ME_TextBuffer *ME_MakeText(void) {
240 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
242 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
243 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
249 p1->member.para.next_para = p2;
250 p2->member.para.prev_para = p1;
251 p2->member.para.nCharOfs = 0;
255 buf->pCharStyle = NULL;
261 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
263 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
266 TRACE("%08lx %p\n", dwFormat, stream);
273 ME_StreamInFill(stream);
274 if (stream->editstream->dwError)
280 if (!(dwFormat & SF_UNICODE))
282 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
283 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
288 nWideChars = stream->dwSize >> 1;
289 pText = (WCHAR *)stream->buffer;
292 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
293 if (stream->dwSize == 0)
297 ME_CommitUndo(editor);
302 static void ME_RTFCharAttrHook(RTF_Info *info)
305 fmt.cbSize = sizeof(fmt);
308 switch(info->rtfMinor)
311 /* FIXME add more flags once they're implemented */
312 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
313 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
314 fmt.yHeight = 12*20; /* 12pt */
318 fmt.dwMask = CFM_BOLD;
319 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
322 fmt.dwMask = CFM_ITALIC;
323 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
326 fmt.dwMask = CFM_UNDERLINE;
327 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
328 fmt.bUnderlineType = CFU_CF1UNDERLINE;
331 fmt.dwMask = CFM_UNDERLINE;
335 fmt.dwMask = CFM_STRIKEOUT;
336 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
340 case rtfSubScrShrink:
341 case rtfSuperScrShrink:
343 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
344 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
345 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
346 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
349 fmt.dwMask = CFM_BACKCOLOR;
351 if (info->rtfParam == 0)
352 fmt.dwEffects = CFE_AUTOBACKCOLOR;
353 else if (info->rtfParam != rtfNoParam)
355 RTFColor *c = RTFGetColor(info, info->rtfParam);
356 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
360 fmt.dwMask = CFM_COLOR;
362 if (info->rtfParam == 0)
363 fmt.dwEffects = CFE_AUTOCOLOR;
364 else if (info->rtfParam != rtfNoParam)
366 RTFColor *c = RTFGetColor(info, info->rtfParam);
367 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
371 if (info->rtfParam != rtfNoParam)
373 RTFFont *f = RTFGetFont(info, info->rtfParam);
376 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
377 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
378 fmt.bCharSet = f->rtfFCharSet;
379 fmt.dwMask = CFM_FACE | CFM_CHARSET;
384 fmt.dwMask = CFM_SIZE;
385 if (info->rtfParam != rtfNoParam)
386 fmt.yHeight = info->rtfParam*10;
391 RTFFlushOutputBuffer(info);
392 /* FIXME too slow ? how come ? */
393 style2 = ME_ApplyStyle(info->style, &fmt);
394 ME_ReleaseStyle(info->style);
395 info->style = style2;
396 info->styleChanged = TRUE;
400 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
401 the same tags mean different things in different contexts */
402 static void ME_RTFParAttrHook(RTF_Info *info)
405 fmt.cbSize = sizeof(fmt);
408 switch(info->rtfMinor)
410 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
411 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
412 fmt.wAlignment = PFA_LEFT;
414 fmt.dxOffset = fmt.dxStartIndent = 0;
417 ME_GetSelectionParaFormat(info->editor, &fmt);
418 fmt.dwMask = PFM_STARTINDENT;
419 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
424 ME_GetSelectionParaFormat(info->editor, &fmt);
425 first = fmt.dxStartIndent;
426 left = info->rtfParam;
427 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
428 fmt.dxStartIndent = first + left;
429 fmt.dxOffset = -first;
433 fmt.dwMask = PFM_RIGHTINDENT;
434 fmt.dxRightIndent = info->rtfParam;
438 fmt.dwMask = PFM_ALIGNMENT;
439 fmt.wAlignment = PFA_LEFT;
442 fmt.dwMask = PFM_ALIGNMENT;
443 fmt.wAlignment = PFA_RIGHT;
446 fmt.dwMask = PFM_ALIGNMENT;
447 fmt.wAlignment = PFA_CENTER;
450 ME_GetSelectionParaFormat(info->editor, &fmt);
451 if (!(fmt.dwMask & PFM_TABSTOPS))
453 fmt.dwMask |= PFM_TABSTOPS;
456 if (fmt.cTabCount < MAX_TAB_STOPS)
457 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
461 RTFFlushOutputBuffer(info);
462 /* FIXME too slow ? how come ?*/
463 ME_SetSelectionParaFormat(info->editor, &fmt);
467 static void ME_RTFReadHook(RTF_Info *info) {
468 switch(info->rtfClass)
471 switch(info->rtfMajor)
474 if (info->stackTop < maxStack) {
475 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
476 info->stack[info->stackTop].codePage = info->codePage;
477 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
480 info->styleChanged = FALSE;
485 RTFFlushOutputBuffer(info);
487 if (info->styleChanged)
489 /* FIXME too slow ? how come ? */
490 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
491 ME_ReleaseStyle(info->style);
493 info->codePage = info->stack[info->stackTop].codePage;
494 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
501 switch(info->rtfMajor)
504 ME_RTFCharAttrHook(info);
507 ME_RTFParAttrHook(info);
515 ME_StreamInFill(ME_InStream *stream)
517 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
518 (BYTE *)stream->buffer,
519 sizeof(stream->buffer),
520 (LONG *)&stream->dwSize);
524 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
528 int from, to, to2, nUndoMode;
530 int nEventMask = editor->nEventMask;
531 ME_InStream inStream;
533 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
534 editor->nEventMask = 0;
536 ME_GetSelection(editor, &from, &to);
537 if (format & SFF_SELECTION) {
538 style = ME_GetSelectionInsertStyle(editor);
540 ME_InternalDeleteText(editor, from, to-from);
543 style = editor->pBuffer->pDefaultStyle;
544 ME_AddRefStyle(style);
545 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
546 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
548 ME_ClearTempStyle(editor);
549 /* FIXME restore default paragraph formatting ! */
552 nUndoMode = editor->nUndoMode;
553 editor->nUndoMode = umIgnore;
555 inStream.editstream = stream;
556 inStream.editstream->dwError = 0;
562 /* Check if it's really RTF, and if it is not, use plain text */
563 ME_StreamInFill(&inStream);
564 if (!inStream.editstream->dwError)
566 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
574 if (!inStream.editstream->dwError)
576 if (format & SF_RTF) {
577 /* setup the RTF parser */
578 memset(&parser, 0, sizeof parser);
579 RTFSetEditStream(&parser, &inStream);
580 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
581 parser.hwndEdit = editor->hWnd;
582 parser.editor = editor;
583 parser.style = style;
586 RTFSetReadHook(&parser, ME_RTFReadHook);
591 RTFFlushOutputBuffer(&parser);
594 style = parser.style;
596 else if (format & SF_TEXT)
597 ME_StreamInText(editor, format, &inStream, style);
599 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
600 ME_GetSelection(editor, &to, &to2);
601 /* put the cursor at the top */
602 if (!(format & SFF_SELECTION))
603 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
606 /* FIXME where to put cursor now ? */
610 editor->nUndoMode = nUndoMode;
611 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
612 TRACE("from %d to %d\n", from, to);
613 if (pUI && from < to)
618 ME_CommitUndo(editor);
619 ME_ReleaseStyle(style);
620 editor->nEventMask = nEventMask;
623 InvalidateRect(editor->hWnd, NULL, TRUE);
624 ME_UpdateRepaint(editor);
626 if (!(format & SFF_SELECTION)) {
627 ME_ClearTempStyle(editor);
629 ME_MoveCaret(editor);
630 ME_SendSelChange(editor);
637 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
639 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
641 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
642 item = ME_FindItemFwd(item, diParagraph);
647 nOffset -= item->member.para.nCharOfs;
648 if (nItemType == diParagraph) {
650 *nItemOffset = nOffset;
655 item = ME_FindItemFwd(item, diRun);
656 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
658 nOffset -= item->member.run.nCharOfs;
660 *nItemOffset = nOffset;
667 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
670 int nLen = lstrlenW(text);
672 ME_DisplayItem *item;
673 ME_DisplayItem *para;
675 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
676 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
678 if (!(flags & FR_MATCHCASE))
679 FIXME("Case-insensitive search not implemented\n");
680 if (flags & ~(FR_DOWN | FR_MATCHCASE))
681 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
683 if (chrg->cpMax == -1)
686 nMax = ME_GetTextLength(editor);
690 nMin = min(chrg->cpMin, chrg->cpMax);
691 nMax = max(chrg->cpMin, chrg->cpMax);
697 chrgText->cpMin = chrgText->cpMax = ((flags & FR_DOWN) ? nMin : nMax);
698 return chrgText->cpMin;
701 if (flags & FR_DOWN) /* Forward search */
704 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
708 para = ME_GetParagraph(item);
710 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < nMax)
712 ME_DisplayItem *pCurItem = item;
713 int nCurStart = nStart;
716 while (pCurItem && pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
719 if (nMatched == nLen)
721 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
724 chrgText->cpMin = nStart;
725 chrgText->cpMax = nStart + nLen;
727 TRACE("found at %d-%d\n", nStart, nStart + nLen);
730 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
732 pCurItem = ME_FindItemFwd(pCurItem, diRun);
733 para = ME_GetParagraph(pCurItem);
734 nCurStart = -nMatched;
738 if (nStart == ME_StrLen(item->member.run.strText))
740 item = ME_FindItemFwd(item, diRun);
741 para = ME_GetParagraph(item);
746 else /* Backward search */
749 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
753 para = ME_GetParagraph(item);
756 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
758 ME_DisplayItem *pCurItem = item;
762 while (pCurItem && pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1] == text[nLen - nMatched - 1])
765 if (nMatched == nLen)
767 nStart = para->member.para.nCharOfs + item->member.run.nCharOfs + nCurEnd - nMatched;
770 chrgText->cpMin = nStart;
771 chrgText->cpMax = nStart + nLen;
773 TRACE("found at %d-%d\n", nStart, nStart + nLen);
776 if (nCurEnd - nMatched == 0)
778 pCurItem = ME_FindItemBack(pCurItem, diRun);
779 para = ME_GetParagraph(pCurItem);
780 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
786 item = ME_FindItemBack(item, diRun);
787 para = ME_GetParagraph(item);
788 nEnd = ME_StrLen(item->member.run.strText);
792 TRACE("not found\n");
797 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
798 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
802 ed->bEmulateVersion10 = FALSE;
803 ed->pBuffer = ME_MakeText();
805 ME_MakeFirstParagraph(hDC, ed->pBuffer);
806 ReleaseDC(hWnd, hDC);
807 ed->bCaretShown = FALSE;
809 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
810 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
811 ed->pCursors[0].nOffset = 0;
812 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
813 ed->pCursors[1].nOffset = 0;
814 ed->nLastTotalLength = ed->nTotalLength = 0;
817 ed->rgbBackColor = -1;
818 ed->bCaretAtEnd = FALSE;
821 ed->pUndoStack = ed->pRedoStack = NULL;
822 ed->nUndoMode = umAddToUndo;
824 ed->nLastSelStart = ed->nLastSelEnd = 0;
826 ed->nZoomNumerator = ed->nZoomDenominator = 0;
828 GetClientRect(hWnd, &ed->rcFormat);
829 for (i=0; i<HFONT_CACHE_SIZE; i++)
831 ed->pFontCache[i].nRefs = 0;
832 ed->pFontCache[i].nAge = 0;
833 ed->pFontCache[i].hFont = NULL;
835 ME_CheckCharOffsets(ed);
839 typedef struct tagME_GlobalDestStruct
843 } ME_GlobalDestStruct;
845 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
847 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
851 nMaxSize = GlobalSize(pData->hData);
852 if (pData->nLength+cb+1 >= cb)
854 /* round up to 2^17 */
855 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
856 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
858 pDest = (BYTE *)GlobalLock(pData->hData);
859 memcpy(pDest + pData->nLength, lpBuff, cb);
860 pData->nLength += cb;
861 pDest[pData->nLength] = '\0';
862 GlobalUnlock(pData->hData);
868 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
870 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
875 pDest = (WORD *)lpBuff;
876 pSrc = (WORD *)GlobalLock(pData->hData);
877 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
878 pDest[i] = pSrc[pData->nLength+i];
882 GlobalUnlock(pData->hData);
886 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
888 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
893 pSrc = (BYTE *)GlobalLock(pData->hData);
894 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
895 pDest[i] = pSrc[pData->nLength+i];
899 GlobalUnlock(pData->hData);
904 void ME_DestroyEditor(ME_TextEditor *editor)
906 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
907 ME_DisplayItem *p = pFirst, *pNext = NULL;
910 ME_ClearTempStyle(editor);
911 ME_EmptyUndoStack(editor);
914 ME_DestroyDisplayItem(p);
917 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
918 for (i=0; i<HFONT_CACHE_SIZE; i++)
920 if (editor->pFontCache[i].hFont)
921 DeleteObject(editor->pFontCache[i].hFont);
927 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
928 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
930 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
935 case DLL_PROCESS_ATTACH:
936 DisableThreadLibraryCalls(hinstDLL);
937 me_heap = HeapCreate (0, 0x10000, 0);
938 ME_RegisterEditorClass(hinstDLL);
941 case DLL_PROCESS_DETACH:
942 UnregisterClassW(wszClassName, 0);
943 UnregisterClassW(wszClassName50, 0);
944 UnregisterClassA("RichEdit20A", 0);
945 UnregisterClassA("RichEdit50A", 0);
946 HeapDestroy (me_heap);
954 #define UNSUPPORTED_MSG(e) \
956 FIXME(#e ": stub\n"); \
957 return DefWindowProcW(hWnd, msg, wParam, lParam);
959 static const char * const edit_messages[] = {
988 "EM_SETPASSWORDCHAR",
989 "EM_EMPTYUNDOBUFFER",
990 "EM_GETFIRSTVISIBLELINE",
992 "EM_SETWORDBREAKPROC",
993 "EM_GETWORDBREAKPROC",
994 "EM_GETPASSWORDCHAR",
1002 static const char * const richedit_messages[] = {
1007 "EM_EXLINEFROMCHAR",
1013 "EM_GETOLEINTERFACE",
1023 "EM_SETOLECALLBACK",
1025 "EM_SETTARGETDEVICE",
1033 "EM_GETWORDBREAKPROCEX",
1034 "EM_SETWORDBREAKPROCEX",
1036 "EM_UNKNOWN_USER_83",
1041 "EM_STOPGROUPTYPING",
1045 "EM_GETAUTOURLDETECT",
1048 "EM_GETTEXTLENGTHEX",
1051 "EM_UNKNOWN_USER_98",
1052 "EM_UNKNOWN_USER_99",
1053 "EM_SETPUNCTUATION",
1054 "EM_GETPUNCTUATION",
1055 "EM_SETWORDWRAPMODE",
1056 "EM_GETWORDWRAPMODE",
1062 "EM_UNKNOWN_USER_109",
1063 "EM_UNKNOWN_USER_110",
1064 "EM_UNKNOWN_USER_111",
1065 "EM_UNKNOWN_USER_112",
1066 "EM_UNKNOWN_USER_113",
1067 "EM_UNKNOWN_USER_114",
1068 "EM_UNKNOWN_USER_115",
1069 "EM_UNKNOWN_USER_116",
1070 "EM_UNKNOWN_USER_117",
1071 "EM_UNKNOWN_USER_118",
1072 "EM_UNKNOWN_USER_119",
1073 "EM_SETLANGOPTIONS",
1074 "EM_GETLANGOPTIONS",
1075 "EM_GETIMECOMPMODE",
1079 "EM_SETIMEMODEBIAS",
1084 get_msg_name(UINT msg)
1086 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1087 return edit_messages[msg - EM_GETSEL];
1088 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1089 return richedit_messages[msg - EM_CANPASTE];
1093 /******************************************************************
1094 * RichEditANSIWndProc (RICHED20.10)
1096 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1098 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1100 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1101 hWnd, msg, get_msg_name(msg), wParam, lParam);
1105 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1106 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1107 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1108 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1109 UNSUPPORTED_MSG(EM_FMTLINES)
1110 UNSUPPORTED_MSG(EM_FORMATRANGE)
1111 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1112 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1113 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1114 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
1115 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1116 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1117 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1118 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1119 UNSUPPORTED_MSG(EM_GETLINE)
1120 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1121 UNSUPPORTED_MSG(EM_GETOPTIONS)
1122 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1123 UNSUPPORTED_MSG(EM_GETREDONAME)
1124 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1125 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1126 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1127 UNSUPPORTED_MSG(EM_GETUNDONAME)
1128 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
1129 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1130 UNSUPPORTED_MSG(EM_HIDESELECTION)
1131 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1132 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1133 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
1134 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
1135 UNSUPPORTED_MSG(EM_SCROLL)
1136 UNSUPPORTED_MSG(EM_SCROLLCARET)
1137 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1138 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1139 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1140 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1141 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1142 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1143 UNSUPPORTED_MSG(EM_SETOPTIONS)
1144 UNSUPPORTED_MSG(EM_SETPALETTE)
1145 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1146 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1147 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1148 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1149 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1150 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1151 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1152 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
1153 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1154 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1155 UNSUPPORTED_MSG(WM_SETFONT)
1156 UNSUPPORTED_MSG(WM_STYLECHANGING)
1157 UNSUPPORTED_MSG(WM_STYLECHANGED)
1158 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1160 /* Messages specific to Richedit controls */
1163 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1165 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1168 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1169 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1170 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1175 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1176 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1177 editor = ME_MakeEditor(hWnd);
1178 SetWindowLongW(hWnd, 0, (long)editor);
1179 pcs = 0; /* ignore */
1182 case EM_EMPTYUNDOBUFFER:
1183 ME_EmptyUndoStack(editor);
1187 /* Note: wParam/lParam can be NULL */
1189 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1190 PUINT pto = lParam ? (PUINT)lParam : &to;
1191 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1192 if ((*pfrom|*pto) & 0xFFFF0000)
1194 return MAKELONG(*pfrom,*pto);
1198 CHARRANGE *pRange = (CHARRANGE *)lParam;
1199 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1200 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1204 return editor->pUndoStack != NULL;
1206 return editor->pRedoStack != NULL;
1207 case WM_UNDO: /* FIXME: actually not the same */
1216 ME_SetSelection(editor, wParam, lParam);
1218 ME_SendSelChange(editor);
1223 CHARRANGE *pRange = (CHARRANGE *)lParam;
1224 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1225 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1226 /* FIXME optimize */
1228 ME_SendSelChange(editor);
1233 LPWSTR wszText = (LPWSTR)lParam;
1234 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1235 size_t len = lstrlenW(wszText);
1238 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1239 if (pStruct->codepage != 1200) {
1240 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1243 /* FIXME: this should support RTF strings too, according to MSDN */
1244 if (pStruct->flags & ST_SELECTION) {
1245 ME_GetSelection(editor, &from, &to);
1246 style = ME_GetSelectionInsertStyle(editor);
1247 ME_InternalDeleteText(editor, from, to - from);
1248 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1249 ME_ReleaseStyle(style);
1252 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1253 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1256 ME_CommitUndo(editor);
1257 if (!(pStruct->flags & ST_KEEPUNDO))
1258 ME_EmptyUndoStack(editor);
1259 ME_UpdateRepaint(editor);
1262 case EM_SETBKGNDCOLOR:
1264 LRESULT lColor = ME_GetBackColor(editor);
1266 editor->rgbBackColor = -1;
1268 editor->rgbBackColor = lParam;
1269 if (editor->bRedraw)
1271 InvalidateRect(hWnd, NULL, TRUE);
1277 return editor->nModifyStep == 0 ? 0 : 1;
1281 editor->nModifyStep = 0x80000000;
1283 editor->nModifyStep = 0;
1287 case EM_SETREADONLY:
1289 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1291 nStyle |= ES_READONLY;
1293 nStyle &= ~ES_READONLY;
1294 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1298 case EM_SETEVENTMASK:
1300 DWORD nOldMask = editor->nEventMask;
1302 editor->nEventMask = lParam;
1305 case EM_GETEVENTMASK:
1306 return editor->nEventMask;
1307 case EM_SETCHARFORMAT:
1309 CHARFORMAT2W buf, *p;
1310 BOOL bRepaint = TRUE;
1311 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1313 ME_SetDefaultCharFormat(editor, p);
1314 else if (wParam == (SCF_WORD | SCF_SELECTION))
1315 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1316 else if (wParam == SCF_ALL)
1317 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1320 ME_GetSelection(editor, &from, &to);
1321 bRepaint = (from != to);
1322 ME_SetSelectionCharFormat(editor, p);
1324 ME_CommitUndo(editor);
1326 ME_UpdateRepaint(editor);
1329 case EM_GETCHARFORMAT:
1331 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1332 if (dst->cbSize != sizeof(CHARFORMATA) &&
1333 dst->cbSize != sizeof(CHARFORMATW) &&
1334 dst->cbSize != sizeof(CHARFORMAT2A) &&
1335 dst->cbSize != sizeof(CHARFORMAT2W))
1337 tmp.cbSize = sizeof(tmp);
1339 ME_GetDefaultCharFormat(editor, &tmp);
1341 ME_GetSelectionCharFormat(editor, &tmp);
1342 ME_CopyToCFAny(dst, &tmp);
1345 case EM_SETPARAFORMAT:
1346 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1347 ME_UpdateRepaint(editor);
1348 ME_CommitUndo(editor);
1350 case EM_GETPARAFORMAT:
1351 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1355 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1356 nPos += 8 * lParam; /* FIXME follow the original */
1361 if (nPos != editor->nScrollPosY) {
1362 int dy = editor->nScrollPosY - nPos;
1363 editor->nScrollPosY = nPos;
1364 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1365 if (editor->bRedraw)
1367 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1371 return TRUE; /* Should return false if a single line richedit control */
1376 ME_GetSelection(editor, &from, &to);
1377 ME_InternalDeleteText(editor, from, to-from);
1378 ME_CommitUndo(editor);
1379 ME_UpdateRepaint(editor);
1386 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1387 size_t len = lstrlenW(wszText);
1388 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1390 ME_GetSelection(editor, &from, &to);
1391 style = ME_GetSelectionInsertStyle(editor);
1392 ME_InternalDeleteText(editor, from, to-from);
1393 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1394 ME_ReleaseStyle(style);
1395 ME_EndToUnicode(hWnd, wszText);
1396 /* drop temporary style if line end */
1397 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1398 if (len>0 && wszText[len-1] == '\n')
1399 ME_ClearTempStyle(editor);
1401 ME_CommitUndo(editor);
1403 ME_EmptyUndoStack(editor);
1404 ME_UpdateRepaint(editor);
1409 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1412 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1413 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1414 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1415 if (lstrlenW(wszText) > 0)
1417 /* uses default style! */
1418 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1420 ME_EndToUnicode(hWnd, wszText);
1423 TRACE("WM_SETTEXT - NULL\n");
1424 ME_CommitUndo(editor);
1425 ME_EmptyUndoStack(editor);
1426 ME_UpdateRepaint(editor);
1431 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1432 if (IsClipboardFormatAvailable(nRTFFormat))
1434 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1442 ME_GlobalDestStruct gds;
1443 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1446 if (IsClipboardFormatAvailable(nRTFFormat))
1447 cf = nRTFFormat, dwFormat = SF_RTF;
1448 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1449 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1453 if (!OpenClipboard(hWnd))
1455 gds.hData = GetClipboardData(cf);
1457 es.dwCookie = (DWORD)&gds;
1458 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1459 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1471 ME_GlobalDestStruct gds;
1473 if (!OpenClipboard(hWnd))
1477 ME_GetSelection(editor, &from, &to);
1478 pars = ME_CountParagraphsBetween(editor, from, to);
1479 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1480 data = (WCHAR *)GlobalLock(hData);
1481 ME_GetTextW(editor, data, from, to-from, TRUE);
1482 GlobalUnlock(hData);
1484 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1486 es.dwCookie = (DWORD)&gds;
1487 es.pfnCallback = ME_AppendToHGLOBAL;
1488 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1489 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1491 SetClipboardData(CF_UNICODETEXT, hData);
1492 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1497 ME_InternalDeleteText(editor, from, to-from);
1498 ME_CommitUndo(editor);
1499 ME_UpdateRepaint(editor);
1503 case WM_GETTEXTLENGTH:
1504 return ME_GetTextLength(editor);
1505 case EM_GETTEXTLENGTHEX:
1506 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1509 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1511 tr.chrg.cpMax = wParam-1;
1512 tr.lpstrText = (WCHAR *)lParam;
1513 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1517 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1520 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1521 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1523 if (ex->flags & GT_SELECTION)
1525 ME_GetSelection(editor, &nStart, &nCount);
1527 nCount = min(nCount, ex->cb - 1);
1532 nCount = ex->cb - 1;
1534 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1536 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1537 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1541 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (nCount + 1) * sizeof(WCHAR));
1542 DWORD buflen = ex->cb;
1546 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1547 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1549 HeapFree(GetProcessHeap(),0,buffer);
1556 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1557 ME_GetSelection(editor, &from, &to);
1558 tr.chrg.cpMin = from;
1560 tr.lpstrText = (WCHAR *)lParam;
1561 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1563 case EM_GETTEXTRANGE:
1565 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1566 if (IsWindowUnicode(hWnd))
1567 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1570 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1571 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1572 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1573 /* FIXME this is a potential security hole (buffer overrun)
1574 if you know more about wchar->mbyte conversion please explain
1576 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1581 case EM_GETLINECOUNT:
1583 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1586 while (item != editor->pBuffer->pLast)
1588 assert(item->type == diParagraph);
1589 nRows += item->member.para.nRows;
1590 item = item->member.para.next_para;
1592 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1593 return max(1, nRows);
1595 case EM_LINEFROMCHAR:
1598 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1600 return ME_RowNumberFromCharOfs(editor, wParam);
1602 case EM_EXLINEFROMCHAR:
1604 return ME_RowNumberFromCharOfs(editor, lParam);
1608 ME_DisplayItem *item, *para;
1612 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1614 item = ME_FindRowWithNumber(editor, wParam);
1617 para = ME_GetParagraph(item);
1618 item = ME_FindItemFwd(item, diRun);
1619 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1620 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1625 ME_DisplayItem *item, *item_end;
1626 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1628 if (wParam > ME_GetTextLength(editor))
1632 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1635 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1636 item = ME_RowStart(item);
1637 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1638 item_end = ME_FindItemFwd(item, diStartRow);
1640 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1642 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs-1;
1643 nChars = nNextLineOfs - nThisLineOfs;
1644 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1649 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1650 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1653 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1654 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1655 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1659 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1660 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1663 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1664 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1665 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1669 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1670 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1672 case EM_FINDTEXTEXW:
1674 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1675 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1678 if (!wParam || !lParam)
1680 *(int *)wParam = editor->nZoomNumerator;
1681 *(int *)lParam = editor->nZoomDenominator;
1684 return ME_SetZoom(editor, wParam, lParam);
1685 case EM_CHARFROMPOS:
1686 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1688 ME_CommitUndo(editor);
1689 ME_WrapMarkedParagraphs(editor);
1690 ME_MoveCaret(editor);
1693 ME_DestroyEditor(editor);
1694 SetWindowLongW(hWnd, 0, 0);
1696 case WM_LBUTTONDOWN:
1698 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1702 if (GetCapture() == hWnd)
1703 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1706 if (GetCapture() == hWnd)
1710 if (editor->bRedraw)
1715 hDC = BeginPaint(hWnd, &ps);
1716 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1717 EndPaint(hWnd, &ps);
1721 ME_ShowCaret(editor);
1722 ME_SendOldNotify(editor, EN_SETFOCUS);
1725 ME_HideCaret(editor);
1726 ME_SendOldNotify(editor, EN_KILLFOCUS);
1730 if (editor->bRedraw)
1732 HDC hDC = (HDC)wParam;
1734 COLORREF rgbBG = ME_GetBackColor(editor);
1735 if (GetUpdateRect(hWnd,&rc,TRUE))
1737 HBRUSH hbr = CreateSolidBrush(rgbBG);
1738 FillRect(hDC, &rc, hbr);
1745 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1748 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1749 ME_CommitUndo(editor);
1750 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1752 ME_MoveCaret(editor);
1756 if (GetKeyState(VK_CONTROL)<0)
1758 if (LOWORD(wParam)=='W')
1762 ME_GetSelectionCharFormat(editor, &chf);
1763 ME_DumpStyleToBuf(&chf, buf);
1764 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1766 if (LOWORD(wParam)=='Q')
1768 ME_CheckCharOffsets(editor);
1774 WCHAR wstr = LOWORD(wParam);
1778 case 3: /* Ctrl-C */
1779 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1783 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1784 MessageBeep(MB_ICONERROR);
1785 return 0; /* FIXME really 0 ? */
1790 case 1: /* Ctrl-A */
1791 ME_SetSelection(editor, 0, -1);
1793 case 22: /* Ctrl-V */
1794 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1796 case 24: /* Ctrl-X */
1797 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1799 case 25: /* Ctrl-Y */
1800 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1802 case 26: /* Ctrl-Z */
1803 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1806 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1807 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1808 ME_Style *style = ME_GetInsertStyle(editor, 0);
1809 ME_SaveTempStyle(editor);
1810 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1811 ME_ReleaseStyle(style);
1812 ME_CommitUndo(editor);
1813 ME_UpdateRepaint(editor);
1819 int nPos = editor->nScrollPosY;
1820 si.cbSize = sizeof(SCROLLINFO);
1821 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1822 GetScrollInfo(hWnd, SB_VERT, &si);
1823 switch(LOWORD(wParam)) {
1825 nPos -= 24; /* FIXME follow the original */
1826 if (nPos<0) nPos = 0;
1830 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1831 nPos += 24; /* FIXME follow the original */
1832 if (nPos>=nEnd) nPos = nEnd;
1836 nPos -= editor->sizeWindow.cy;
1837 if (nPos<0) nPos = 0;
1840 nPos += editor->sizeWindow.cy;
1841 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1844 case SB_THUMBPOSITION:
1845 nPos = si.nTrackPos;
1848 if (nPos != editor->nScrollPosY) {
1849 int dy = editor->nScrollPosY - nPos;
1850 editor->nScrollPosY = nPos;
1851 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1852 if (editor->bRedraw)
1854 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1862 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1863 UINT pulScrollLines;
1864 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1865 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1866 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1867 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1872 if (nPos != editor->nScrollPosY) {
1873 int dy = editor->nScrollPosY - nPos;
1874 editor->nScrollPosY = nPos;
1875 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1876 if (editor->bRedraw)
1878 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1886 *((RECT *)lParam) = editor->rcFormat;
1894 RECT *rc = (RECT *)lParam;
1898 editor->rcFormat.left += rc->left;
1899 editor->rcFormat.top += rc->top;
1900 editor->rcFormat.right += rc->right;
1901 editor->rcFormat.bottom += rc->bottom;
1905 editor->rcFormat = *rc;
1910 GetClientRect(hWnd, &editor->rcFormat);
1912 if (msg != EM_SETRECTNP)
1913 ME_RewrapRepaint(editor);
1917 editor->bRedraw = wParam;
1921 GetClientRect(hWnd, &editor->rcFormat);
1922 ME_RewrapRepaint(editor);
1923 return DefWindowProcW(hWnd, msg, wParam, lParam);
1925 case EM_GETOLEINTERFACE:
1927 LPVOID *ppvObj = (LPVOID*) lParam;
1928 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1929 return CreateIRichEditOle(ppvObj);
1933 return DefWindowProcW(hWnd, msg, wParam, lParam);
1939 /******************************************************************
1940 * RichEdit10ANSIWndProc (RICHED20.9)
1942 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1946 /* FIXME: this is NOT the same as 2.0 version */
1947 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1948 if (msg == WM_NCCREATE)
1950 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1952 editor->bEmulateVersion10 = TRUE;
1953 editor->pBuffer->pLast->member.para.nCharOfs = 2;
1958 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1960 HWND hWnd = editor->hWnd;
1961 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1964 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1966 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1969 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1970 item = item->member.para.next_para;
1973 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1974 item = item->member.para.next_para;
1981 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1983 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1994 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1997 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2004 item = ME_FindItemFwd(item, diRun);
2007 while(nChars && item)
2009 int nLen = ME_StrLen(item->member.run.strText);
2013 if (item->member.run.nFlags & MERF_ENDPARA)
2024 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2034 item = ME_FindItemFwd(item, diRun);
2040 void ME_RegisterEditorClass(HINSTANCE hInstance)
2046 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2047 wcW.lpfnWndProc = RichEditANSIWndProc;
2050 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2052 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2053 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2054 wcW.lpszMenuName = NULL;
2055 wcW.lpszClassName = wszClassName;
2056 bResult = RegisterClassW(&wcW);
2058 wcW.lpszClassName = wszClassName50;
2059 bResult = RegisterClassW(&wcW);
2062 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2063 wcA.lpfnWndProc = RichEditANSIWndProc;
2066 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2068 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2069 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2070 wcA.lpszMenuName = NULL;
2071 wcA.lpszClassName = "RichEdit20A";
2072 bResult = RegisterClassA(&wcA);
2074 wcA.lpszClassName = "RichEdit50A";
2075 bResult = RegisterClassA(&wcA);
2078 /******************************************************************
2079 * CreateTextServices (RICHED20.4)
2081 * FIXME should be ITextHost instead of void*
2083 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
2087 /* FIXME should support aggregation */
2089 return CLASS_E_NOAGGREGATION;
2091 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
2094 /******************************************************************
2095 * REExtendedRegisterClass (RICHED20.8)
2097 * FIXME undocumented
2099 void WINAPI REExtendedRegisterClass(void)