2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 API implementation status:
25 Messages (ANSI versions not done yet)
26 - EM_AUTOURLDETECT 2.0
37 + EM_FINDTEXT (only FR_DOWN flag implemented)
38 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
42 - EM_GETAUTOURLDETECT 2.0
43 - EM_GETBIDIOPTIONS 3.0
44 - EM_GETCHARFORMAT (partly done)
47 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
48 - EM_GETIMECOLOR 1.0asian
49 - EM_GETIMECOMPMODE 2.0
50 - EM_GETIMEOPTIONS 1.0asian
52 - EM_GETLANGOPTIONS 2.0
55 + EM_GETLINECOUNT returns number of rows, not of paragraphs
60 - EM_GETPASSWORDCHAR 2.0
61 - EM_GETPUNCTUATION 1.0asian
65 + EM_GETSELTEXT (ANSI&Unicode)
69 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
71 ? + EM_GETTEXTRANGE (ANSI&Unicode)
72 - EM_GETTYPOGRAPHYOPTIONS 3.0
75 - EM_GETWORDBREAKPROCEX
76 - EM_GETWORDWRAPMODE 1.0asian
88 + EM_REPLACESEL (proper style?) ANSI&Unicode
92 - EM_SETBIDIOPTIONS 3.0
94 - EM_SETCHARFORMAT (partly done, no ANSI)
96 + EM_SETEVENTMASK (few notifications supported)
98 - EM_SETIMECOLOR 1.0asian
99 - EM_SETIMEOPTIONS 1.0asian
100 - EM_SETLANGOPTIONS 2.0
102 + EM_SETMODIFY (not sure if implementation is correct)
107 - EM_SETPASSWORDCHAR 2.0
108 - EM_SETPUNCTUATION 1.0asian
109 + EM_SETREADONLY no beep on modification attempt
111 + EM_SETRECTNP (EM_SETRECT without repainting)
113 - EM_SETSCROLLPOS 3.0
116 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
118 - EM_SETTYPOGRAPHYOPTIONS 3.0
119 - EM_SETUNDOLIMIT 2.0
120 - EM_SETWORDBREAKPROC
121 - EM_SETWORDBREAKPROCEX
122 - EM_SETWORDWRAPMODE 1.0asian
124 - EM_SHOWSCROLLBAR 2.0
125 - EM_STOPGROUPTYPING 2.0
133 + WM_GETDLGCODE (the current implementation is incomplete)
134 + WM_GETTEXT (ANSI&Unicode)
135 + WM_GETTEXTLENGTH (ANSI version sucks)
138 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
140 - WM_STYLECHANGED (things like read-only flag)
145 * EN_CHANGE (sent from the wrong place)
162 * EN_UPDATE (sent from the wrong place)
170 - ES_DISABLENOSCROLL (scrollbar is always visible)
171 - ES_EX_NOCALLOLEINIT
173 - ES_MULTILINE (currently single line controls aren't supported)
175 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
181 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
188 * RICHED20 TODO (incomplete):
190 * - messages/styles/notifications listed above
192 * - add remaining CHARFORMAT/PARAFORMAT fields
193 * - right/center align should strip spaces from the beginning
194 * - more advanced navigation (Ctrl-arrows)
196 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
197 * - COM interface (looks like a major pain in the TODO list)
198 * - calculate heights of pictures (half-done)
199 * - horizontal scrolling (not even started)
200 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
202 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
203 * - italic caret with italic fonts
205 * - most notifications aren't sent at all (the most important ones are)
206 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
207 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
208 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
209 * - full justification
212 * - ListBox & ComboBox not implemented
214 * Bugs that are probably fixed, but not so easy to verify:
215 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
216 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
217 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
218 * - caret shouldn't be displayed when selection isn't empty
219 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
220 * - undo for setting default format (done, might be buggy)
221 * - styles might be not released properly (looks like they work like charm, but who knows?
230 #define NO_SHLWAPI_STREAM
235 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
238 HANDLE me_heap = NULL;
240 static BOOL ME_ListBoxRegistered = FALSE;
241 static BOOL ME_ComboBoxRegistered = FALSE;
243 static ME_TextBuffer *ME_MakeText(void) {
245 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
247 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
248 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
254 p1->member.para.next_para = p2;
255 p2->member.para.prev_para = p1;
256 p2->member.para.nCharOfs = 0;
260 buf->pCharStyle = NULL;
266 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
268 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
271 TRACE("%08lx %p\n", dwFormat, stream);
278 ME_StreamInFill(stream);
279 if (stream->editstream->dwError)
285 if (!(dwFormat & SF_UNICODE))
287 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
288 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
293 nWideChars = stream->dwSize >> 1;
294 pText = (WCHAR *)stream->buffer;
297 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
298 if (stream->dwSize == 0)
302 ME_CommitUndo(editor);
307 static void ME_RTFCharAttrHook(RTF_Info *info)
310 fmt.cbSize = sizeof(fmt);
313 switch(info->rtfMinor)
316 /* FIXME add more flags once they're implemented */
317 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
318 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
319 fmt.yHeight = 12*20; /* 12pt */
323 fmt.dwMask = CFM_BOLD;
324 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
327 fmt.dwMask = CFM_ITALIC;
328 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
331 fmt.dwMask = CFM_UNDERLINE;
332 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
333 fmt.bUnderlineType = CFU_CF1UNDERLINE;
336 fmt.dwMask = CFM_UNDERLINE;
340 fmt.dwMask = CFM_STRIKEOUT;
341 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
345 case rtfSubScrShrink:
346 case rtfSuperScrShrink:
348 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
349 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
350 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
351 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
354 fmt.dwMask = CFM_BACKCOLOR;
356 if (info->rtfParam == 0)
357 fmt.dwEffects = CFE_AUTOBACKCOLOR;
358 else if (info->rtfParam != rtfNoParam)
360 RTFColor *c = RTFGetColor(info, info->rtfParam);
361 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
365 fmt.dwMask = CFM_COLOR;
367 if (info->rtfParam == 0)
368 fmt.dwEffects = CFE_AUTOCOLOR;
369 else if (info->rtfParam != rtfNoParam)
371 RTFColor *c = RTFGetColor(info, info->rtfParam);
372 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
376 if (info->rtfParam != rtfNoParam)
378 RTFFont *f = RTFGetFont(info, info->rtfParam);
381 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
382 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
383 fmt.bCharSet = f->rtfFCharSet;
384 fmt.dwMask = CFM_FACE | CFM_CHARSET;
389 fmt.dwMask = CFM_SIZE;
390 if (info->rtfParam != rtfNoParam)
391 fmt.yHeight = info->rtfParam*10;
396 RTFFlushOutputBuffer(info);
397 /* FIXME too slow ? how come ? */
398 style2 = ME_ApplyStyle(info->style, &fmt);
399 ME_ReleaseStyle(info->style);
400 info->style = style2;
401 info->styleChanged = TRUE;
405 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
406 the same tags mean different things in different contexts */
407 static void ME_RTFParAttrHook(RTF_Info *info)
410 fmt.cbSize = sizeof(fmt);
413 switch(info->rtfMinor)
415 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
416 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
417 fmt.wAlignment = PFA_LEFT;
419 fmt.dxOffset = fmt.dxStartIndent = 0;
422 ME_GetSelectionParaFormat(info->editor, &fmt);
423 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
424 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
425 fmt.dxOffset = -info->rtfParam;
428 ME_GetSelectionParaFormat(info->editor, &fmt);
429 fmt.dwMask = PFM_STARTINDENT;
430 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
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);
486 if (info->stackTop<=1) {
487 info->rtfClass = rtfEOF;
491 assert(info->stackTop >= 0);
492 if (info->styleChanged)
494 /* FIXME too slow ? how come ? */
495 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
496 ME_ReleaseStyle(info->style);
498 info->codePage = info->stack[info->stackTop].codePage;
499 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
506 switch(info->rtfMajor)
509 ME_RTFCharAttrHook(info);
512 ME_RTFParAttrHook(info);
520 ME_StreamInFill(ME_InStream *stream)
522 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
523 (BYTE *)stream->buffer,
524 sizeof(stream->buffer),
525 (LONG *)&stream->dwSize);
529 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
533 int from, to, to2, nUndoMode;
535 int nEventMask = editor->nEventMask;
536 ME_InStream inStream;
538 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
539 editor->nEventMask = 0;
541 ME_GetSelection(editor, &from, &to);
542 if (format & SFF_SELECTION) {
543 style = ME_GetSelectionInsertStyle(editor);
545 ME_InternalDeleteText(editor, from, to-from);
548 style = editor->pBuffer->pDefaultStyle;
549 ME_AddRefStyle(style);
550 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
551 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
553 ME_ClearTempStyle(editor);
554 /* FIXME restore default paragraph formatting ! */
557 nUndoMode = editor->nUndoMode;
558 editor->nUndoMode = umIgnore;
560 inStream.editstream = stream;
561 inStream.editstream->dwError = 0;
567 /* Check if it's really RTF, and if it is not, use plain text */
568 ME_StreamInFill(&inStream);
569 if (!inStream.editstream->dwError)
571 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
579 if (!inStream.editstream->dwError)
581 if (format & SF_RTF) {
582 /* setup the RTF parser */
583 memset(&parser, 0, sizeof parser);
584 RTFSetEditStream(&parser, &inStream);
585 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
586 parser.hwndEdit = editor->hWnd;
587 parser.editor = editor;
588 parser.style = style;
591 RTFSetReadHook(&parser, ME_RTFReadHook);
596 RTFFlushOutputBuffer(&parser);
599 style = parser.style;
601 else if (format & SF_TEXT)
602 ME_StreamInText(editor, format, &inStream, style);
604 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
605 ME_GetSelection(editor, &to, &to2);
606 /* put the cursor at the top */
607 if (!(format & SFF_SELECTION))
608 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
611 /* FIXME where to put cursor now ? */
615 editor->nUndoMode = nUndoMode;
616 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
617 TRACE("from %d to %d\n", from, to);
618 if (pUI && from < to)
623 ME_CommitUndo(editor);
624 ME_ReleaseStyle(style);
625 editor->nEventMask = nEventMask;
628 InvalidateRect(editor->hWnd, NULL, TRUE);
629 ME_UpdateRepaint(editor);
631 if (!(format & SFF_SELECTION)) {
632 ME_ClearTempStyle(editor);
634 ME_MoveCaret(editor);
635 ME_SendSelChange(editor);
636 ME_SendRequestResize(editor, FALSE);
643 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
645 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
647 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
648 item = ME_FindItemFwd(item, diParagraph);
653 nOffset -= item->member.para.nCharOfs;
654 if (nItemType == diParagraph) {
656 *nItemOffset = nOffset;
661 item = ME_FindItemFwd(item, diRun);
662 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
664 nOffset -= item->member.run.nCharOfs;
666 *nItemOffset = nOffset;
673 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
676 int nLen = lstrlenW(text);
678 ME_DisplayItem *item;
679 ME_DisplayItem *para;
681 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
682 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
684 if (!(flags & FR_MATCHCASE))
685 FIXME("Case-insensitive search not implemented\n");
686 if (flags & ~(FR_DOWN | FR_MATCHCASE))
687 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
689 if (chrg->cpMax == -1)
692 nMax = ME_GetTextLength(editor);
696 nMin = min(chrg->cpMin, chrg->cpMax);
697 nMax = max(chrg->cpMin, chrg->cpMax);
703 chrgText->cpMin = chrgText->cpMax = ((flags & FR_DOWN) ? nMin : nMax);
704 return chrgText->cpMin;
707 if (flags & FR_DOWN) /* Forward search */
710 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
714 para = ME_GetParagraph(item);
716 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < nMax)
718 ME_DisplayItem *pCurItem = item;
719 int nCurStart = nStart;
722 while (pCurItem && pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
725 if (nMatched == nLen)
727 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
730 chrgText->cpMin = nStart;
731 chrgText->cpMax = nStart + nLen;
733 TRACE("found at %d-%d\n", nStart, nStart + nLen);
736 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
738 pCurItem = ME_FindItemFwd(pCurItem, diRun);
739 para = ME_GetParagraph(pCurItem);
740 nCurStart = -nMatched;
744 if (nStart == ME_StrLen(item->member.run.strText))
746 item = ME_FindItemFwd(item, diRun);
747 para = ME_GetParagraph(item);
752 else /* Backward search */
755 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
759 para = ME_GetParagraph(item);
762 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
764 ME_DisplayItem *pCurItem = item;
768 while (pCurItem && pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1] == text[nLen - nMatched - 1])
771 if (nMatched == nLen)
773 nStart = para->member.para.nCharOfs + item->member.run.nCharOfs + nCurEnd - nMatched;
776 chrgText->cpMin = nStart;
777 chrgText->cpMax = nStart + nLen;
779 TRACE("found at %d-%d\n", nStart, nStart + nLen);
782 if (nCurEnd - nMatched == 0)
784 pCurItem = ME_FindItemBack(pCurItem, diRun);
785 para = ME_GetParagraph(pCurItem);
786 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
792 item = ME_FindItemBack(item, diRun);
793 para = ME_GetParagraph(item);
794 nEnd = ME_StrLen(item->member.run.strText);
798 TRACE("not found\n");
803 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
804 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
808 ed->bEmulateVersion10 = FALSE;
809 ed->pBuffer = ME_MakeText();
811 ME_MakeFirstParagraph(hDC, ed->pBuffer);
812 ReleaseDC(hWnd, hDC);
813 ed->bCaretShown = FALSE;
815 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
816 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
817 ed->pCursors[0].nOffset = 0;
818 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
819 ed->pCursors[1].nOffset = 0;
820 ed->nLastTotalLength = ed->nTotalLength = 0;
823 ed->rgbBackColor = -1;
824 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
825 ed->bCaretAtEnd = FALSE;
828 ed->pUndoStack = ed->pRedoStack = NULL;
829 ed->nUndoMode = umAddToUndo;
831 ed->nLastSelStart = ed->nLastSelEnd = 0;
833 ed->nZoomNumerator = ed->nZoomDenominator = 0;
835 GetClientRect(hWnd, &ed->rcFormat);
836 for (i=0; i<HFONT_CACHE_SIZE; i++)
838 ed->pFontCache[i].nRefs = 0;
839 ed->pFontCache[i].nAge = 0;
840 ed->pFontCache[i].hFont = NULL;
842 ME_CheckCharOffsets(ed);
846 typedef struct tagME_GlobalDestStruct
850 } ME_GlobalDestStruct;
852 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
854 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
858 nMaxSize = GlobalSize(pData->hData);
859 if (pData->nLength+cb+1 >= cb)
861 /* round up to 2^17 */
862 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
863 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
865 pDest = (BYTE *)GlobalLock(pData->hData);
866 memcpy(pDest + pData->nLength, lpBuff, cb);
867 pData->nLength += cb;
868 pDest[pData->nLength] = '\0';
869 GlobalUnlock(pData->hData);
875 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
877 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
882 pDest = (WORD *)lpBuff;
883 pSrc = (WORD *)GlobalLock(pData->hData);
884 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
885 pDest[i] = pSrc[pData->nLength+i];
889 GlobalUnlock(pData->hData);
893 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
895 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
900 pSrc = (BYTE *)GlobalLock(pData->hData);
901 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
902 pDest[i] = pSrc[pData->nLength+i];
906 GlobalUnlock(pData->hData);
911 void ME_DestroyEditor(ME_TextEditor *editor)
913 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
914 ME_DisplayItem *p = pFirst, *pNext = NULL;
917 ME_ClearTempStyle(editor);
918 ME_EmptyUndoStack(editor);
921 ME_DestroyDisplayItem(p);
924 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
925 for (i=0; i<HFONT_CACHE_SIZE; i++)
927 if (editor->pFontCache[i].hFont)
928 DeleteObject(editor->pFontCache[i].hFont);
930 DeleteObject(editor->hbrBackground);
935 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
936 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
937 static WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
938 static WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
940 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
945 case DLL_PROCESS_ATTACH:
946 DisableThreadLibraryCalls(hinstDLL);
947 me_heap = HeapCreate (0, 0x10000, 0);
948 ME_RegisterEditorClass(hinstDLL);
951 case DLL_PROCESS_DETACH:
952 UnregisterClassW(wszClassName, 0);
953 UnregisterClassW(wszClassName50, 0);
954 UnregisterClassA("RichEdit20A", 0);
955 UnregisterClassA("RichEdit50A", 0);
956 if (ME_ListBoxRegistered)
957 UnregisterClassW(wszClassNameListBox, 0);
958 if (ME_ComboBoxRegistered)
959 UnregisterClassW(wszClassNameComboBox, 0);
960 HeapDestroy (me_heap);
968 #define UNSUPPORTED_MSG(e) \
970 FIXME(#e ": stub\n"); \
971 return DefWindowProcW(hWnd, msg, wParam, lParam);
973 static const char * const edit_messages[] = {
1002 "EM_SETPASSWORDCHAR",
1003 "EM_EMPTYUNDOBUFFER",
1004 "EM_GETFIRSTVISIBLELINE",
1006 "EM_SETWORDBREAKPROC",
1007 "EM_GETWORDBREAKPROC",
1008 "EM_GETPASSWORDCHAR",
1016 static const char * const richedit_messages[] = {
1021 "EM_EXLINEFROMCHAR",
1027 "EM_GETOLEINTERFACE",
1037 "EM_SETOLECALLBACK",
1039 "EM_SETTARGETDEVICE",
1047 "EM_GETWORDBREAKPROCEX",
1048 "EM_SETWORDBREAKPROCEX",
1050 "EM_UNKNOWN_USER_83",
1055 "EM_STOPGROUPTYPING",
1059 "EM_GETAUTOURLDETECT",
1062 "EM_GETTEXTLENGTHEX",
1065 "EM_UNKNOWN_USER_98",
1066 "EM_UNKNOWN_USER_99",
1067 "EM_SETPUNCTUATION",
1068 "EM_GETPUNCTUATION",
1069 "EM_SETWORDWRAPMODE",
1070 "EM_GETWORDWRAPMODE",
1076 "EM_UNKNOWN_USER_109",
1077 "EM_UNKNOWN_USER_110",
1078 "EM_UNKNOWN_USER_111",
1079 "EM_UNKNOWN_USER_112",
1080 "EM_UNKNOWN_USER_113",
1081 "EM_UNKNOWN_USER_114",
1082 "EM_UNKNOWN_USER_115",
1083 "EM_UNKNOWN_USER_116",
1084 "EM_UNKNOWN_USER_117",
1085 "EM_UNKNOWN_USER_118",
1086 "EM_UNKNOWN_USER_119",
1087 "EM_SETLANGOPTIONS",
1088 "EM_GETLANGOPTIONS",
1089 "EM_GETIMECOMPMODE",
1093 "EM_SETIMEMODEBIAS",
1098 get_msg_name(UINT msg)
1100 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1101 return edit_messages[msg - EM_GETSEL];
1102 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1103 return richedit_messages[msg - EM_CANPASTE];
1107 /******************************************************************
1108 * RichEditANSIWndProc (RICHED20.10)
1110 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1112 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1114 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1115 hWnd, msg, get_msg_name(msg), wParam, lParam);
1119 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1120 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1121 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1122 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1123 UNSUPPORTED_MSG(EM_FMTLINES)
1124 UNSUPPORTED_MSG(EM_FORMATRANGE)
1125 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1126 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1127 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1128 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1129 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1130 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1131 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1132 UNSUPPORTED_MSG(EM_GETLINE)
1133 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1134 UNSUPPORTED_MSG(EM_GETOPTIONS)
1135 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1136 UNSUPPORTED_MSG(EM_GETREDONAME)
1137 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1138 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1139 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1140 UNSUPPORTED_MSG(EM_GETUNDONAME)
1141 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
1142 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1143 UNSUPPORTED_MSG(EM_HIDESELECTION)
1144 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1145 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1146 UNSUPPORTED_MSG(EM_SCROLL)
1147 UNSUPPORTED_MSG(EM_SCROLLCARET)
1148 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1149 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1150 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1151 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1152 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1153 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1154 UNSUPPORTED_MSG(EM_SETOPTIONS)
1155 UNSUPPORTED_MSG(EM_SETPALETTE)
1156 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1157 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1158 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1159 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1160 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1161 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1162 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1163 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
1164 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1165 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1166 UNSUPPORTED_MSG(WM_SETFONT)
1167 UNSUPPORTED_MSG(WM_STYLECHANGING)
1168 UNSUPPORTED_MSG(WM_STYLECHANGED)
1169 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1171 /* Messages specific to Richedit controls */
1174 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1176 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1179 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1180 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1181 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1186 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1187 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1188 editor = ME_MakeEditor(hWnd);
1189 SetWindowLongW(hWnd, 0, (long)editor);
1190 pcs = 0; /* ignore */
1193 case EM_EMPTYUNDOBUFFER:
1194 ME_EmptyUndoStack(editor);
1198 /* Note: wParam/lParam can be NULL */
1200 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1201 PUINT pto = lParam ? (PUINT)lParam : &to;
1202 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1203 if ((*pfrom|*pto) & 0xFFFF0000)
1205 return MAKELONG(*pfrom,*pto);
1209 CHARRANGE *pRange = (CHARRANGE *)lParam;
1210 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1211 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1215 return editor->pUndoStack != NULL;
1217 return editor->pRedoStack != NULL;
1218 case WM_UNDO: /* FIXME: actually not the same */
1227 ME_SetSelection(editor, wParam, lParam);
1229 ME_SendSelChange(editor);
1234 CHARRANGE *pRange = (CHARRANGE *)lParam;
1235 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1236 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1237 /* FIXME optimize */
1239 ME_SendSelChange(editor);
1244 LPWSTR wszText = (LPWSTR)lParam;
1245 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1246 size_t len = lstrlenW(wszText);
1249 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1250 if (pStruct->codepage != 1200) {
1251 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1254 /* FIXME: this should support RTF strings too, according to MSDN */
1255 if (pStruct->flags & ST_SELECTION) {
1256 ME_GetSelection(editor, &from, &to);
1257 style = ME_GetSelectionInsertStyle(editor);
1258 ME_InternalDeleteText(editor, from, to - from);
1259 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1260 ME_ReleaseStyle(style);
1263 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1264 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1267 ME_CommitUndo(editor);
1268 if (!(pStruct->flags & ST_KEEPUNDO))
1269 ME_EmptyUndoStack(editor);
1270 ME_UpdateRepaint(editor);
1273 case EM_SETBKGNDCOLOR:
1275 LRESULT lColor = ME_GetBackColor(editor);
1276 if (editor->rgbBackColor != -1)
1277 DeleteObject(editor->hbrBackground);
1280 editor->rgbBackColor = -1;
1281 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1285 editor->rgbBackColor = lParam;
1286 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1288 if (editor->bRedraw)
1290 InvalidateRect(hWnd, NULL, TRUE);
1296 return editor->nModifyStep == 0 ? 0 : 1;
1300 editor->nModifyStep = 0x80000000;
1302 editor->nModifyStep = 0;
1306 case EM_SETREADONLY:
1308 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1310 nStyle |= ES_READONLY;
1312 nStyle &= ~ES_READONLY;
1313 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1317 case EM_SETEVENTMASK:
1319 DWORD nOldMask = editor->nEventMask;
1321 editor->nEventMask = lParam;
1324 case EM_GETEVENTMASK:
1325 return editor->nEventMask;
1326 case EM_SETCHARFORMAT:
1328 CHARFORMAT2W buf, *p;
1329 BOOL bRepaint = TRUE;
1330 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1332 ME_SetDefaultCharFormat(editor, p);
1333 else if (wParam == (SCF_WORD | SCF_SELECTION))
1334 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1335 else if (wParam == SCF_ALL)
1336 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1339 ME_GetSelection(editor, &from, &to);
1340 bRepaint = (from != to);
1341 ME_SetSelectionCharFormat(editor, p);
1343 ME_CommitUndo(editor);
1345 ME_UpdateRepaint(editor);
1348 case EM_GETCHARFORMAT:
1350 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1351 if (dst->cbSize != sizeof(CHARFORMATA) &&
1352 dst->cbSize != sizeof(CHARFORMATW) &&
1353 dst->cbSize != sizeof(CHARFORMAT2A) &&
1354 dst->cbSize != sizeof(CHARFORMAT2W))
1356 tmp.cbSize = sizeof(tmp);
1358 ME_GetDefaultCharFormat(editor, &tmp);
1360 ME_GetSelectionCharFormat(editor, &tmp);
1361 ME_CopyToCFAny(dst, &tmp);
1364 case EM_SETPARAFORMAT:
1365 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1366 ME_UpdateRepaint(editor);
1367 ME_CommitUndo(editor);
1369 case EM_GETPARAFORMAT:
1370 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1372 case EM_GETFIRSTVISIBLELINE:
1374 ME_DisplayItem *p = editor->pBuffer->pFirst;
1375 int y = editor->nScrollPosY;
1380 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1381 if (p->type == diTextEnd)
1383 if (p->type == diParagraph) {
1384 ypara = p->member.para.nYPos;
1387 ystart = ypara + p->member.row.nYPos;
1388 yend = ystart + p->member.row.nHeight;
1398 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1399 nPos += 8 * lParam; /* FIXME follow the original */
1404 if (nPos != editor->nScrollPosY) {
1405 int dy = editor->nScrollPosY - nPos;
1406 editor->nScrollPosY = nPos;
1407 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1408 if (editor->bRedraw)
1410 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1414 return TRUE; /* Should return false if a single line richedit control */
1419 ME_GetSelection(editor, &from, &to);
1420 ME_InternalDeleteText(editor, from, to-from);
1421 ME_CommitUndo(editor);
1422 ME_UpdateRepaint(editor);
1429 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1430 size_t len = lstrlenW(wszText);
1431 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1433 ME_GetSelection(editor, &from, &to);
1434 style = ME_GetSelectionInsertStyle(editor);
1435 ME_InternalDeleteText(editor, from, to-from);
1436 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1437 ME_ReleaseStyle(style);
1438 ME_EndToUnicode(hWnd, wszText);
1439 /* drop temporary style if line end */
1440 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1441 if (len>0 && wszText[len-1] == '\n')
1442 ME_ClearTempStyle(editor);
1444 ME_CommitUndo(editor);
1446 ME_EmptyUndoStack(editor);
1447 ME_UpdateRepaint(editor);
1452 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1455 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1456 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1457 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1458 if (lstrlenW(wszText) > 0)
1460 /* uses default style! */
1461 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1463 ME_EndToUnicode(hWnd, wszText);
1466 TRACE("WM_SETTEXT - NULL\n");
1467 ME_CommitUndo(editor);
1468 ME_EmptyUndoStack(editor);
1469 ME_SetSelection(editor, 0, 0);
1470 ME_UpdateRepaint(editor);
1475 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1476 if (IsClipboardFormatAvailable(nRTFFormat))
1478 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1486 ME_GlobalDestStruct gds;
1487 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1490 if (IsClipboardFormatAvailable(nRTFFormat))
1491 cf = nRTFFormat, dwFormat = SF_RTF;
1492 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1493 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1497 if (!OpenClipboard(hWnd))
1499 gds.hData = GetClipboardData(cf);
1501 es.dwCookie = (DWORD)&gds;
1502 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1503 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1515 ME_GlobalDestStruct gds;
1517 if (!OpenClipboard(hWnd))
1521 ME_GetSelection(editor, &from, &to);
1522 pars = ME_CountParagraphsBetween(editor, from, to);
1523 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1524 data = (WCHAR *)GlobalLock(hData);
1525 ME_GetTextW(editor, data, from, to-from, TRUE);
1526 GlobalUnlock(hData);
1528 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1530 es.dwCookie = (DWORD)&gds;
1531 es.pfnCallback = ME_AppendToHGLOBAL;
1532 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1533 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1535 SetClipboardData(CF_UNICODETEXT, hData);
1536 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1541 ME_InternalDeleteText(editor, from, to-from);
1542 ME_CommitUndo(editor);
1543 ME_UpdateRepaint(editor);
1547 case WM_GETTEXTLENGTH:
1548 return ME_GetTextLength(editor);
1549 case EM_GETTEXTLENGTHEX:
1550 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1553 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1555 tr.chrg.cpMax = wParam-1;
1556 tr.lpstrText = (WCHAR *)lParam;
1557 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1561 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1564 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1565 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1567 if (ex->flags & GT_SELECTION)
1569 ME_GetSelection(editor, &nStart, &nCount);
1571 nCount = min(nCount, ex->cb - 1);
1576 nCount = ex->cb - 1;
1578 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1580 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1581 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1585 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1586 we can just take a bigger buffer? :) */
1587 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1588 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
1589 DWORD buflen = ex->cb;
1593 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1594 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1596 HeapFree(GetProcessHeap(),0,buffer);
1603 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1604 ME_GetSelection(editor, &from, &to);
1605 tr.chrg.cpMin = from;
1607 tr.lpstrText = (WCHAR *)lParam;
1608 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1610 case EM_GETTEXTRANGE:
1612 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1613 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
1614 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
1615 editor->bEmulateVersion10, ME_GetTextLength(editor));
1616 if (IsWindowUnicode(hWnd))
1617 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1620 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1621 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1622 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1623 /* FIXME this is a potential security hole (buffer overrun)
1624 if you know more about wchar->mbyte conversion please explain
1626 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1631 case EM_GETLINECOUNT:
1633 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1636 while (item != editor->pBuffer->pLast)
1638 assert(item->type == diParagraph);
1639 nRows += item->member.para.nRows;
1640 item = item->member.para.next_para;
1642 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1643 return max(1, nRows);
1645 case EM_LINEFROMCHAR:
1648 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1650 return ME_RowNumberFromCharOfs(editor, wParam);
1652 case EM_EXLINEFROMCHAR:
1654 return ME_RowNumberFromCharOfs(editor, lParam);
1658 ME_DisplayItem *item, *para;
1662 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1664 item = ME_FindRowWithNumber(editor, wParam);
1667 para = ME_GetParagraph(item);
1668 item = ME_FindItemFwd(item, diRun);
1669 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1670 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1675 ME_DisplayItem *item, *item_end;
1676 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1678 if (wParam > ME_GetTextLength(editor))
1682 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1685 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1686 item = ME_RowStart(item);
1687 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1688 item_end = ME_FindItemFwd(item, diStartRow);
1690 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1692 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1693 - (editor->bEmulateVersion10?2:1);
1694 nChars = nNextLineOfs - nThisLineOfs;
1695 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1700 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1701 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1704 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1705 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1706 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1710 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1711 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1714 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1715 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1716 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1720 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1721 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1723 case EM_FINDTEXTEXW:
1725 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1726 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1729 if (!wParam || !lParam)
1731 *(int *)wParam = editor->nZoomNumerator;
1732 *(int *)lParam = editor->nZoomDenominator;
1735 return ME_SetZoom(editor, wParam, lParam);
1736 case EM_CHARFROMPOS:
1737 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1738 case EM_POSFROMCHAR:
1740 ME_DisplayItem *pRun;
1741 int nCharOfs, nOffset, nLength;
1745 /* detect which API version we're dealing with */
1746 if (wParam >= 0x40000)
1748 nLength = ME_GetTextLength(editor);
1750 if (nCharOfs < nLength) {
1751 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1752 assert(pRun->type == diRun);
1753 pt.y = pRun->member.run.pt.y;
1754 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1755 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1758 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1760 if (wParam >= 0x40000) {
1761 *(POINTL *)wParam = pt;
1763 return MAKELONG( pt.x, pt.y );
1766 ME_CommitUndo(editor);
1767 ME_WrapMarkedParagraphs(editor);
1768 ME_MoveCaret(editor);
1771 ME_DestroyEditor(editor);
1772 SetWindowLongW(hWnd, 0, 0);
1774 case WM_LBUTTONDOWN:
1776 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1780 if (GetCapture() == hWnd)
1781 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1784 if (GetCapture() == hWnd)
1788 if (editor->bRedraw)
1793 hDC = BeginPaint(hWnd, &ps);
1794 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1795 EndPaint(hWnd, &ps);
1799 ME_ShowCaret(editor);
1800 ME_SendOldNotify(editor, EN_SETFOCUS);
1803 ME_HideCaret(editor);
1804 ME_SendOldNotify(editor, EN_KILLFOCUS);
1808 if (editor->bRedraw)
1810 HDC hDC = (HDC)wParam;
1812 if (GetUpdateRect(hWnd,&rc,TRUE))
1814 FillRect(hDC, &rc, editor->hbrBackground);
1820 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1823 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1824 ME_CommitUndo(editor);
1825 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1827 ME_MoveCaret(editor);
1831 if (GetKeyState(VK_CONTROL)<0)
1833 if (LOWORD(wParam)=='W')
1837 ME_GetSelectionCharFormat(editor, &chf);
1838 ME_DumpStyleToBuf(&chf, buf);
1839 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1841 if (LOWORD(wParam)=='Q')
1843 ME_CheckCharOffsets(editor);
1849 WCHAR wstr = LOWORD(wParam);
1853 case 3: /* Ctrl-C */
1854 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1858 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1859 MessageBeep(MB_ICONERROR);
1860 return 0; /* FIXME really 0 ? */
1865 case 1: /* Ctrl-A */
1866 ME_SetSelection(editor, 0, -1);
1868 case 22: /* Ctrl-V */
1869 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1871 case 24: /* Ctrl-X */
1872 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1874 case 25: /* Ctrl-Y */
1875 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1877 case 26: /* Ctrl-Z */
1878 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1881 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1882 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1883 ME_Style *style = ME_GetInsertStyle(editor, 0);
1884 ME_SaveTempStyle(editor);
1885 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1886 ME_ReleaseStyle(style);
1887 ME_CommitUndo(editor);
1888 ME_UpdateRepaint(editor);
1894 int nPos = editor->nScrollPosY;
1895 si.cbSize = sizeof(SCROLLINFO);
1896 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1897 GetScrollInfo(hWnd, SB_VERT, &si);
1898 switch(LOWORD(wParam)) {
1900 nPos -= 24; /* FIXME follow the original */
1901 if (nPos<0) nPos = 0;
1905 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1906 nPos += 24; /* FIXME follow the original */
1907 if (nPos>=nEnd) nPos = nEnd;
1911 nPos -= editor->sizeWindow.cy;
1912 if (nPos<0) nPos = 0;
1915 nPos += editor->sizeWindow.cy;
1916 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1919 case SB_THUMBPOSITION:
1920 nPos = si.nTrackPos;
1923 if (nPos != editor->nScrollPosY) {
1924 int dy = editor->nScrollPosY - nPos;
1925 editor->nScrollPosY = nPos;
1926 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1927 if (editor->bRedraw)
1929 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1937 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1938 UINT pulScrollLines;
1939 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1940 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1941 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1942 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1947 if (nPos != editor->nScrollPosY) {
1948 int dy = editor->nScrollPosY - nPos;
1949 editor->nScrollPosY = nPos;
1950 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1951 if (editor->bRedraw)
1953 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1961 *((RECT *)lParam) = editor->rcFormat;
1969 RECT *rc = (RECT *)lParam;
1973 editor->rcFormat.left += rc->left;
1974 editor->rcFormat.top += rc->top;
1975 editor->rcFormat.right += rc->right;
1976 editor->rcFormat.bottom += rc->bottom;
1980 editor->rcFormat = *rc;
1985 GetClientRect(hWnd, &editor->rcFormat);
1987 if (msg != EM_SETRECTNP)
1988 ME_RewrapRepaint(editor);
1991 case EM_REQUESTRESIZE:
1992 ME_SendRequestResize(editor, TRUE);
1995 editor->bRedraw = wParam;
1999 GetClientRect(hWnd, &editor->rcFormat);
2000 ME_RewrapRepaint(editor);
2001 return DefWindowProcW(hWnd, msg, wParam, lParam);
2003 case EM_GETOLEINTERFACE:
2005 LPVOID *ppvObj = (LPVOID*) lParam;
2006 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
2007 return CreateIRichEditOle(ppvObj);
2011 return DefWindowProcW(hWnd, msg, wParam, lParam);
2017 /******************************************************************
2018 * RichEdit10ANSIWndProc (RICHED20.9)
2020 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2024 /* FIXME: this is NOT the same as 2.0 version */
2025 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2026 if (msg == WM_NCCREATE)
2028 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2030 editor->bEmulateVersion10 = TRUE;
2031 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2036 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2038 HWND hWnd = editor->hWnd;
2039 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2042 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2044 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2047 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2048 item = item->member.para.next_para;
2051 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2052 item = item->member.para.next_para;
2059 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2061 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2063 WCHAR *pStart = buffer;
2072 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2075 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2082 item = ME_FindItemFwd(item, diRun);
2085 while(nChars && item)
2087 int nLen = ME_StrLen(item->member.run.strText);
2091 if (item->member.run.nFlags & MERF_ENDPARA)
2100 /* our end paragraph consists of 2 characters now */
2101 if (editor->bEmulateVersion10)
2105 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2112 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2116 item = ME_FindItemFwd(item, diRun);
2119 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2123 void ME_RegisterEditorClass(HINSTANCE hInstance)
2129 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2130 wcW.lpfnWndProc = RichEditANSIWndProc;
2133 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2135 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2136 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2137 wcW.lpszMenuName = NULL;
2138 wcW.lpszClassName = wszClassName;
2139 bResult = RegisterClassW(&wcW);
2141 wcW.lpszClassName = wszClassName50;
2142 bResult = RegisterClassW(&wcW);
2145 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2146 wcA.lpfnWndProc = RichEditANSIWndProc;
2149 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2151 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2152 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2153 wcA.lpszMenuName = NULL;
2154 wcA.lpszClassName = "RichEdit20A";
2155 bResult = RegisterClassA(&wcA);
2157 wcA.lpszClassName = "RichEdit50A";
2158 bResult = RegisterClassA(&wcA);
2161 /******************************************************************
2162 * CreateTextServices (RICHED20.4)
2164 * FIXME should be ITextHost instead of void*
2166 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
2170 /* FIXME should support aggregation */
2172 return CLASS_E_NOAGGREGATION;
2174 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
2177 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2178 /* FIXME: Not implemented */
2179 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2180 hWnd, msg, get_msg_name(msg), wParam, lParam);
2181 return DefWindowProcW(hWnd, msg, wParam, lParam);
2184 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2185 /* FIXME: Not implemented */
2186 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2187 hWnd, msg, get_msg_name(msg), wParam, lParam);
2188 return DefWindowProcW(hWnd, msg, wParam, lParam);
2191 /******************************************************************
2192 * REExtendedRegisterClass (RICHED20.8)
2194 * FIXME undocumented
2195 * Need to check for errors and implement controls and callbacks
2197 LRESULT WINAPI REExtendedRegisterClass(void)
2202 FIXME("semi stub\n");
2206 wcW.hInstance = NULL;
2209 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2210 wcW.lpszMenuName = NULL;
2212 if (!ME_ListBoxRegistered)
2214 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2215 wcW.lpfnWndProc = REListWndProc;
2216 wcW.lpszClassName = wszClassNameListBox;
2217 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2220 if (!ME_ComboBoxRegistered)
2222 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2223 wcW.lpfnWndProc = REComboWndProc;
2224 wcW.lpszClassName = wszClassNameComboBox;
2225 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2229 if (ME_ListBoxRegistered)
2231 if (ME_ComboBoxRegistered)