2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 API implementation status:
26 Messages (ANSI versions not done yet)
27 + EM_AUTOURLDETECT 2.0
38 + EM_FINDTEXT (only FR_DOWN flag implemented)
39 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
43 + EM_GETAUTOURLDETECT 2.0
44 - EM_GETBIDIOPTIONS 3.0
45 - EM_GETCHARFORMAT (partly done)
48 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
49 - EM_GETIMECOLOR 1.0asian
50 - EM_GETIMECOMPMODE 2.0
51 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETLANGOPTIONS 2.0
56 + EM_GETLINECOUNT returns number of rows, not of paragraphs
61 + EM_GETPASSWORDCHAR 2.0
62 - EM_GETPUNCTUATION 1.0asian
66 + EM_GETSELTEXT (ANSI&Unicode)
67 + EM_GETSCROLLPOS 3.0 (only Y value valid)
70 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 ? + EM_GETTEXTRANGE (ANSI&Unicode)
73 - EM_GETTYPOGRAPHYOPTIONS 3.0
76 - EM_GETWORDBREAKPROCEX
77 - EM_GETWORDWRAPMODE 1.0asian
89 + EM_REPLACESEL (proper style?) ANSI&Unicode
93 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETCHARFORMAT (partly done, no ANSI)
97 + EM_SETEVENTMASK (few notifications supported)
99 - EM_SETIMECOLOR 1.0asian
100 - EM_SETIMEOPTIONS 1.0asian
101 - EM_SETLANGOPTIONS 2.0
103 + EM_SETMODIFY (not sure if implementation is correct)
105 + EM_SETOPTIONS (partially implemented)
108 + EM_SETPASSWORDCHAR 2.0
109 - EM_SETPUNCTUATION 1.0asian
110 + EM_SETREADONLY no beep on modification attempt
112 + EM_SETRECTNP (EM_SETRECT without repainting)
114 + EM_SETSCROLLPOS 3.0
117 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
119 - EM_SETTYPOGRAPHYOPTIONS 3.0
120 + EM_SETUNDOLIMIT 2.0
121 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
122 - EM_SETWORDBREAKPROCEX
123 - EM_SETWORDWRAPMODE 1.0asian
125 + EM_SHOWSCROLLBAR 2.0
126 - EM_STOPGROUPTYPING 2.0
134 + WM_GETDLGCODE (the current implementation is incomplete)
135 + WM_GETTEXT (ANSI&Unicode)
136 + WM_GETTEXTLENGTH (ANSI version sucks)
139 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
141 - WM_STYLECHANGED (things like read-only flag)
146 * EN_CHANGE (sent from the wrong place)
163 * EN_UPDATE (sent from the wrong place)
171 + ES_DISABLENOSCROLL (scrollbar is always visible)
172 - ES_EX_NOCALLOLEINIT
174 - ES_MULTILINE (currently single line controls aren't supported)
176 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
182 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
189 * RICHED20 TODO (incomplete):
191 * - messages/styles/notifications listed above
193 * - add remaining CHARFORMAT/PARAFORMAT fields
194 * - right/center align should strip spaces from the beginning
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
211 * - ListBox & ComboBox not implemented
213 * Bugs that are probably fixed, but not so easy to verify:
214 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
215 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
216 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
217 * - caret shouldn't be displayed when selection isn't empty
218 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
219 * - undo for setting default format (done, might be buggy)
220 * - styles might be not released properly (looks like they work like charm, but who knows?
227 #define NO_SHLWAPI_STREAM
230 #include "textserv.h"
233 #define STACK_SIZE_DEFAULT 100
234 #define STACK_SIZE_MAX 1000
236 #define TEXT_LIMIT_DEFAULT 32767
238 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
241 HANDLE me_heap = NULL;
243 static BOOL ME_ListBoxRegistered = FALSE;
244 static BOOL ME_ComboBoxRegistered = FALSE;
246 static ME_TextBuffer *ME_MakeText(void) {
248 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
250 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
251 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
257 p1->member.para.next_para = p2;
258 p2->member.para.prev_para = p1;
259 p2->member.para.nCharOfs = 0;
263 buf->pCharStyle = NULL;
269 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
271 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
274 TRACE("%08lx %p\n", dwFormat, stream);
281 ME_StreamInFill(stream);
282 if (stream->editstream->dwError)
288 if (!(dwFormat & SF_UNICODE))
290 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
291 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
296 nWideChars = stream->dwSize >> 1;
297 pText = (WCHAR *)stream->buffer;
300 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
301 if (stream->dwSize == 0)
305 ME_CommitUndo(editor);
306 ME_UpdateRepaint(editor);
310 static void ME_RTFCharAttrHook(RTF_Info *info)
313 fmt.cbSize = sizeof(fmt);
317 switch(info->rtfMinor)
320 /* FIXME add more flags once they're implemented */
321 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
322 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
323 fmt.yHeight = 12*20; /* 12pt */
327 fmt.dwMask = CFM_BOLD;
328 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
331 fmt.dwMask = CFM_ITALIC;
332 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
335 fmt.dwMask = CFM_UNDERLINE;
336 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
337 fmt.bUnderlineType = CFU_CF1UNDERLINE;
340 fmt.dwMask = CFM_UNDERLINE;
344 fmt.dwMask = CFM_STRIKEOUT;
345 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
349 case rtfSubScrShrink:
350 case rtfSuperScrShrink:
352 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
353 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
354 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
355 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
358 fmt.dwMask = CFM_HIDDEN;
359 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
362 fmt.dwMask = CFM_BACKCOLOR;
364 if (info->rtfParam == 0)
365 fmt.dwEffects = CFE_AUTOBACKCOLOR;
366 else if (info->rtfParam != rtfNoParam)
368 RTFColor *c = RTFGetColor(info, info->rtfParam);
369 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
373 fmt.dwMask = CFM_COLOR;
375 if (info->rtfParam == 0)
376 fmt.dwEffects = CFE_AUTOCOLOR;
377 else if (info->rtfParam != rtfNoParam)
379 RTFColor *c = RTFGetColor(info, info->rtfParam);
380 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
384 if (info->rtfParam != rtfNoParam)
386 RTFFont *f = RTFGetFont(info, info->rtfParam);
389 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
390 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
391 fmt.bCharSet = f->rtfFCharSet;
392 fmt.dwMask = CFM_FACE | CFM_CHARSET;
397 fmt.dwMask = CFM_SIZE;
398 if (info->rtfParam != rtfNoParam)
399 fmt.yHeight = info->rtfParam*10;
404 RTFFlushOutputBuffer(info);
405 /* FIXME too slow ? how come ? */
406 style2 = ME_ApplyStyle(info->style, &fmt);
407 ME_ReleaseStyle(info->style);
408 info->style = style2;
409 info->styleChanged = TRUE;
413 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
414 the same tags mean different things in different contexts */
415 static void ME_RTFParAttrHook(RTF_Info *info)
418 fmt.cbSize = sizeof(fmt);
421 switch(info->rtfMinor)
423 case rtfParDef: /* restores default paragraph attributes */
424 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
425 fmt.wAlignment = PFA_LEFT;
427 fmt.dxOffset = fmt.dxStartIndent = 0;
428 RTFFlushOutputBuffer(info);
429 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
433 ME_DisplayItem *para;
435 RTFFlushOutputBuffer(info);
436 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
437 assert(para->member.para.pCells);
438 para->member.para.bTable = TRUE;
442 ME_GetSelectionParaFormat(info->editor, &fmt);
443 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
444 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
445 fmt.dxOffset = -info->rtfParam;
448 ME_GetSelectionParaFormat(info->editor, &fmt);
449 fmt.dwMask = PFM_STARTINDENT;
450 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
453 fmt.dwMask = PFM_RIGHTINDENT;
454 fmt.dxRightIndent = info->rtfParam;
458 fmt.dwMask = PFM_ALIGNMENT;
459 fmt.wAlignment = PFA_LEFT;
462 fmt.dwMask = PFM_ALIGNMENT;
463 fmt.wAlignment = PFA_RIGHT;
466 fmt.dwMask = PFM_ALIGNMENT;
467 fmt.wAlignment = PFA_CENTER;
470 ME_GetSelectionParaFormat(info->editor, &fmt);
471 if (!(fmt.dwMask & PFM_TABSTOPS))
473 fmt.dwMask |= PFM_TABSTOPS;
476 if (fmt.cTabCount < MAX_TAB_STOPS)
477 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
481 RTFFlushOutputBuffer(info);
482 /* FIXME too slow ? how come ?*/
483 ME_SetSelectionParaFormat(info->editor, &fmt);
487 static void ME_RTFTblAttrHook(RTF_Info *info)
489 ME_DisplayItem *para;
491 switch (info->rtfMinor)
494 RTFFlushOutputBuffer(info);
495 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
497 /* Release possibly inherited cell definitions */
498 ME_DestroyTableCellList(para);
500 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
501 para->member.para.pCells->nRightBoundary = 0;
502 para->member.para.pCells->next = NULL;
503 para->member.para.pLastCell = para->member.para.pCells;
506 RTFFlushOutputBuffer(info);
507 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
509 if (para->member.para.pLastCell->nRightBoundary)
511 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
514 para->member.para.pLastCell->next = pCell;
515 para->member.para.pLastCell = pCell;
517 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
522 static void ME_RTFReadHook(RTF_Info *info) {
523 switch(info->rtfClass)
526 switch(info->rtfMajor)
529 if (info->stackTop < maxStack) {
530 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
531 info->stack[info->stackTop].codePage = info->codePage;
532 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
535 info->styleChanged = FALSE;
540 RTFFlushOutputBuffer(info);
541 if (info->stackTop<=1) {
542 info->rtfClass = rtfEOF;
546 assert(info->stackTop >= 0);
547 if (info->styleChanged)
549 /* FIXME too slow ? how come ? */
550 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
551 ME_ReleaseStyle(info->style);
553 info->codePage = info->stack[info->stackTop].codePage;
554 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
561 switch(info->rtfMajor)
564 ME_RTFCharAttrHook(info);
567 ME_RTFParAttrHook(info);
570 ME_RTFTblAttrHook(info);
573 if (info->rtfMinor == rtfCell)
575 RTFFlushOutputBuffer(info);
576 ME_InsertTableCellFromCursor(info->editor, 0);
584 ME_StreamInFill(ME_InStream *stream)
586 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
587 (BYTE *)stream->buffer,
588 sizeof(stream->buffer),
589 (LONG *)&stream->dwSize);
593 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
597 int from, to, to2, nUndoMode;
599 int nEventMask = editor->nEventMask;
600 ME_InStream inStream;
602 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
603 editor->nEventMask = 0;
605 ME_GetSelection(editor, &from, &to);
606 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
607 style = ME_GetSelectionInsertStyle(editor);
609 ME_InternalDeleteText(editor, from, to-from);
612 style = editor->pBuffer->pDefaultStyle;
613 ME_AddRefStyle(style);
614 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
615 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
617 ME_ClearTempStyle(editor);
618 /* FIXME restore default paragraph formatting ! */
621 nUndoMode = editor->nUndoMode;
622 editor->nUndoMode = umIgnore;
624 inStream.editstream = stream;
625 inStream.editstream->dwError = 0;
631 /* Check if it's really RTF, and if it is not, use plain text */
632 ME_StreamInFill(&inStream);
633 if (!inStream.editstream->dwError)
635 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
643 if (!inStream.editstream->dwError)
645 if (format & SF_RTF) {
646 /* setup the RTF parser */
647 memset(&parser, 0, sizeof parser);
648 RTFSetEditStream(&parser, &inStream);
649 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
650 parser.hwndEdit = editor->hWnd;
651 parser.editor = editor;
652 parser.style = style;
655 RTFSetReadHook(&parser, ME_RTFReadHook);
660 RTFFlushOutputBuffer(&parser);
663 style = parser.style;
665 else if (format & SF_TEXT)
666 ME_StreamInText(editor, format, &inStream, style);
668 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
669 ME_GetSelection(editor, &to, &to2);
670 /* put the cursor at the top */
671 if (!(format & SFF_SELECTION))
672 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
675 editor->nUndoMode = nUndoMode;
676 if (format & SFF_SELECTION)
678 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
679 TRACE("from %d to %d\n", from, to);
680 if (pUI && from < to)
685 ME_CommitUndo(editor);
689 ME_EmptyUndoStack(editor);
692 ME_ReleaseStyle(style);
693 editor->nEventMask = nEventMask;
696 ME_UpdateRepaint(editor);
698 if (!(format & SFF_SELECTION)) {
699 ME_ClearTempStyle(editor);
701 ME_MoveCaret(editor);
702 ME_SendSelChange(editor);
703 ME_SendRequestResize(editor, FALSE);
709 typedef struct tagME_RTFStringStreamStruct
714 } ME_RTFStringStreamStruct;
716 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
718 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
721 count = min(cb, pStruct->length - pStruct->pos);
722 memmove(lpBuff, pStruct->string + pStruct->pos, count);
723 pStruct->pos += count;
729 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
732 ME_RTFStringStreamStruct data;
734 data.string = string;
735 data.length = strlen(string);
737 es.dwCookie = (DWORD)&data;
738 es.pfnCallback = ME_ReadFromRTFString;
739 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
744 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
746 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
748 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
749 item = ME_FindItemFwd(item, diParagraph);
754 nOffset -= item->member.para.nCharOfs;
755 if (nItemType == diParagraph) {
757 *nItemOffset = nOffset;
762 item = ME_FindItemFwd(item, diRun);
763 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
765 nOffset -= item->member.run.nCharOfs;
767 *nItemOffset = nOffset;
774 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
776 const int nLen = lstrlenW(text);
777 const int nTextLen = ME_GetTextLength(editor);
780 ME_DisplayItem *item;
781 ME_DisplayItem *para;
782 WCHAR wLastChar = ' ';
784 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
785 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
787 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
788 FIXME("Flags 0x%08lx not implemented\n",
789 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
792 if (chrg->cpMax == -1)
795 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
797 /* when searching up, if cpMin < cpMax, then instead of searching
798 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
799 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
800 * case, it is always bigger than cpMin.
802 if (!(flags & FR_DOWN))
806 nMax = nMin > nTextLen ? nTextLen : nMin;
807 if (nMin < nSwap || chrg->cpMax == -1)
813 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
816 chrgText->cpMin = chrgText->cpMax = -1;
820 if (flags & FR_DOWN) /* Forward search */
822 /* If possible, find the character before where the search starts */
823 if ((flags & FR_WHOLEWORD) && nMin)
826 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
830 chrgText->cpMin = chrgText->cpMax = -1;
833 wLastChar = item->member.run.strText->szData[nStart];
837 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
841 chrgText->cpMin = chrgText->cpMax = -1;
845 para = ME_GetParagraph(item);
847 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
849 ME_DisplayItem *pCurItem = item;
850 int nCurStart = nStart;
853 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
855 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
859 if (nMatched == nLen)
861 ME_DisplayItem *pNextItem = pCurItem;
862 int nNextStart = nCurStart;
865 /* Check to see if next character is a whitespace */
866 if (flags & FR_WHOLEWORD)
868 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
870 pNextItem = ME_FindItemFwd(pCurItem, diRun);
871 nNextStart = -nMatched;
875 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
879 if (isalnumW(wNextChar))
883 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
886 chrgText->cpMin = nStart;
887 chrgText->cpMax = nStart + nLen;
889 TRACE("found at %d-%d\n", nStart, nStart + nLen);
892 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
894 pCurItem = ME_FindItemFwd(pCurItem, diRun);
895 para = ME_GetParagraph(pCurItem);
896 nCurStart = -nMatched;
900 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
905 if (nStart == ME_StrLen(item->member.run.strText))
907 item = ME_FindItemFwd(item, diRun);
908 para = ME_GetParagraph(item);
913 else /* Backward search */
915 /* If possible, find the character after where the search ends */
916 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
919 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
923 chrgText->cpMin = chrgText->cpMax = -1;
926 wLastChar = item->member.run.strText->szData[nEnd];
930 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
934 chrgText->cpMin = chrgText->cpMax = -1;
938 para = ME_GetParagraph(item);
941 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
943 ME_DisplayItem *pCurItem = item;
947 if (nCurEnd - nMatched == 0)
949 pCurItem = ME_FindItemBack(pCurItem, diRun);
950 para = ME_GetParagraph(pCurItem);
951 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
954 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
956 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
960 if (nMatched == nLen)
962 ME_DisplayItem *pPrevItem = pCurItem;
963 int nPrevEnd = nCurEnd;
966 /* Check to see if previous character is a whitespace */
967 if (flags & FR_WHOLEWORD)
969 if (nPrevEnd - nMatched == 0)
971 pPrevItem = ME_FindItemBack(pCurItem, diRun);
973 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
977 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
981 if (isalnumW(wPrevChar))
985 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
988 chrgText->cpMin = nStart;
989 chrgText->cpMax = nStart + nLen;
991 TRACE("found at %d-%d\n", nStart, nStart + nLen);
994 if (nCurEnd - nMatched == 0)
996 pCurItem = ME_FindItemBack(pCurItem, diRun);
997 /* Don't care about pCurItem becoming NULL here; it's already taken
998 * care of in the exterior loop condition */
999 para = ME_GetParagraph(pCurItem);
1000 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1004 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1011 item = ME_FindItemBack(item, diRun);
1012 para = ME_GetParagraph(item);
1013 nEnd = ME_StrLen(item->member.run.strText);
1017 TRACE("not found\n");
1019 chrgText->cpMin = chrgText->cpMax = -1;
1025 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1027 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1028 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1040 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1044 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1045 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1047 if (ME_IsSelection(editor))
1048 ME_DeleteSelection(editor);
1049 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1050 ME_DeleteTextAtCursor(editor, 1, 1);
1053 ME_CommitUndo(editor);
1054 ME_UpdateRepaint(editor);
1055 ME_SendRequestResize(editor, FALSE);
1065 chf.cbSize = sizeof(chf);
1067 ME_GetSelectionCharFormat(editor, &chf);
1068 ME_DumpStyleToBuf(&chf, buf);
1069 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1073 ME_CheckCharOffsets(editor);
1080 static void ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1085 if(!editor->lpOleCallback)
1087 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1088 if(selrange.cpMin == selrange.cpMax)
1089 seltype |= SEL_EMPTY;
1092 /* FIXME: Handle objects */
1093 seltype |= SEL_TEXT;
1094 if(selrange.cpMax-selrange.cpMin > 1)
1095 seltype |= SEL_MULTICHAR;
1097 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1099 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1104 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1105 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1109 ed->bEmulateVersion10 = FALSE;
1110 ed->pBuffer = ME_MakeText();
1112 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1113 ReleaseDC(hWnd, hDC);
1114 ed->bCaretShown = FALSE;
1116 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1117 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1118 ed->pCursors[0].nOffset = 0;
1119 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1120 ed->pCursors[1].nOffset = 0;
1121 ed->nLastTotalLength = ed->nTotalLength = 0;
1124 ed->rgbBackColor = -1;
1125 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1126 ed->bCaretAtEnd = FALSE;
1128 ed->nModifyStep = 0;
1129 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1130 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1131 ed->nUndoStackSize = 0;
1132 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1133 ed->nUndoMode = umAddToUndo;
1134 ed->nParagraphs = 1;
1135 ed->nLastSelStart = ed->nLastSelEnd = 0;
1136 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1137 ed->nScrollPosY = 0;
1138 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1140 ed->bHideSelection = FALSE;
1141 ed->nInvalidOfs = -1;
1142 ed->pfnWordBreak = NULL;
1143 ed->lpOleCallback = NULL;
1144 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1145 GetClientRect(hWnd, &ed->rcFormat);
1146 for (i=0; i<HFONT_CACHE_SIZE; i++)
1148 ed->pFontCache[i].nRefs = 0;
1149 ed->pFontCache[i].nAge = 0;
1150 ed->pFontCache[i].hFont = NULL;
1153 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
1154 FIXME("WS_HSCROLL requested, but horizontal scrolling isn't implemented yet.\n");
1156 ed->bScrollY = GetWindowLongW(hWnd, GWL_STYLE) & WS_VSCROLL;
1158 ME_CheckCharOffsets(ed);
1160 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1161 ed->cPasswordMask = '*';
1163 ed->cPasswordMask = 0;
1168 typedef struct tagME_GlobalDestStruct
1172 } ME_GlobalDestStruct;
1174 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1176 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1181 pDest = (WORD *)lpBuff;
1182 pSrc = (WORD *)GlobalLock(pData->hData);
1183 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1184 pDest[i] = pSrc[pData->nLength+i];
1186 pData->nLength += i;
1188 GlobalUnlock(pData->hData);
1192 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1194 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1199 pSrc = (BYTE *)GlobalLock(pData->hData);
1200 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1201 pDest[i] = pSrc[pData->nLength+i];
1203 pData->nLength += i;
1205 GlobalUnlock(pData->hData);
1210 void ME_DestroyEditor(ME_TextEditor *editor)
1212 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1213 ME_DisplayItem *p = pFirst, *pNext = NULL;
1216 ME_ClearTempStyle(editor);
1217 ME_EmptyUndoStack(editor);
1220 ME_DestroyDisplayItem(p);
1223 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1224 for (i=0; i<HFONT_CACHE_SIZE; i++)
1226 if (editor->pFontCache[i].hFont)
1227 DeleteObject(editor->pFontCache[i].hFont);
1229 DeleteObject(editor->hbrBackground);
1230 if(editor->lpOleCallback)
1231 IUnknown_Release(editor->lpOleCallback);
1236 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1237 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1238 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1239 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1241 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1246 case DLL_PROCESS_ATTACH:
1247 DisableThreadLibraryCalls(hinstDLL);
1248 me_heap = HeapCreate (0, 0x10000, 0);
1249 ME_RegisterEditorClass(hinstDLL);
1252 case DLL_PROCESS_DETACH:
1253 UnregisterClassW(wszClassName, 0);
1254 UnregisterClassW(wszClassName50, 0);
1255 UnregisterClassA("RichEdit20A", 0);
1256 UnregisterClassA("RichEdit50A", 0);
1257 if (ME_ListBoxRegistered)
1258 UnregisterClassW(wszClassNameListBox, 0);
1259 if (ME_ComboBoxRegistered)
1260 UnregisterClassW(wszClassNameComboBox, 0);
1261 HeapDestroy (me_heap);
1269 #define UNSUPPORTED_MSG(e) \
1271 FIXME(#e ": stub\n"); \
1272 return DefWindowProcW(hWnd, msg, wParam, lParam);
1274 static const char * const edit_messages[] = {
1303 "EM_SETPASSWORDCHAR",
1304 "EM_EMPTYUNDOBUFFER",
1305 "EM_GETFIRSTVISIBLELINE",
1307 "EM_SETWORDBREAKPROC",
1308 "EM_GETWORDBREAKPROC",
1309 "EM_GETPASSWORDCHAR",
1317 static const char * const richedit_messages[] = {
1322 "EM_EXLINEFROMCHAR",
1328 "EM_GETOLEINTERFACE",
1338 "EM_SETOLECALLBACK",
1340 "EM_SETTARGETDEVICE",
1348 "EM_GETWORDBREAKPROCEX",
1349 "EM_SETWORDBREAKPROCEX",
1351 "EM_UNKNOWN_USER_83",
1356 "EM_STOPGROUPTYPING",
1360 "EM_GETAUTOURLDETECT",
1363 "EM_GETTEXTLENGTHEX",
1366 "EM_UNKNOWN_USER_98",
1367 "EM_UNKNOWN_USER_99",
1368 "EM_SETPUNCTUATION",
1369 "EM_GETPUNCTUATION",
1370 "EM_SETWORDWRAPMODE",
1371 "EM_GETWORDWRAPMODE",
1377 "EM_UNKNOWN_USER_109",
1378 "EM_UNKNOWN_USER_110",
1379 "EM_UNKNOWN_USER_111",
1380 "EM_UNKNOWN_USER_112",
1381 "EM_UNKNOWN_USER_113",
1382 "EM_UNKNOWN_USER_114",
1383 "EM_UNKNOWN_USER_115",
1384 "EM_UNKNOWN_USER_116",
1385 "EM_UNKNOWN_USER_117",
1386 "EM_UNKNOWN_USER_118",
1387 "EM_UNKNOWN_USER_119",
1388 "EM_SETLANGOPTIONS",
1389 "EM_GETLANGOPTIONS",
1390 "EM_GETIMECOMPMODE",
1394 "EM_SETIMEMODEBIAS",
1399 get_msg_name(UINT msg)
1401 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1402 return edit_messages[msg - EM_GETSEL];
1403 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1404 return richedit_messages[msg - EM_CANPASTE];
1408 /******************************************************************
1409 * RichEditANSIWndProc (RICHED20.10)
1411 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1413 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1415 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1416 hWnd, msg, get_msg_name(msg), wParam, lParam);
1418 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1419 ERR("RichEditANSIWndProc called with invalid hWnd %p - application bug?\n", hWnd);
1425 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1426 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1427 UNSUPPORTED_MSG(EM_FMTLINES)
1428 UNSUPPORTED_MSG(EM_FORMATRANGE)
1429 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1430 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1431 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1432 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1433 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1434 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1435 UNSUPPORTED_MSG(EM_GETREDONAME)
1436 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1437 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1438 UNSUPPORTED_MSG(EM_GETUNDONAME)
1439 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1440 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1441 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1442 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1443 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1444 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1445 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1446 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1447 UNSUPPORTED_MSG(EM_SETPALETTE)
1448 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1449 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1450 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1451 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1452 UNSUPPORTED_MSG(WM_STYLECHANGING)
1453 UNSUPPORTED_MSG(WM_STYLECHANGED)
1454 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1456 /* Messages specific to Richedit controls */
1459 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1461 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1464 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1465 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1466 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1471 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1472 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1473 editor = ME_MakeEditor(hWnd);
1474 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1475 pcs = 0; /* ignore */
1478 case EM_EMPTYUNDOBUFFER:
1479 ME_EmptyUndoStack(editor);
1483 /* Note: wParam/lParam can be NULL */
1485 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1486 PUINT pto = lParam ? (PUINT)lParam : &to;
1487 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1488 if ((*pfrom|*pto) & 0xFFFF0000)
1490 return MAKELONG(*pfrom,*pto);
1494 CHARRANGE *pRange = (CHARRANGE *)lParam;
1495 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1496 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1499 case EM_SETUNDOLIMIT:
1501 if ((int)wParam < 0)
1502 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1504 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1505 /* Setting a max stack size keeps wine from getting killed
1506 for hogging memory. Windows allocates all this memory at once, so
1507 no program would realistically set a value above our maxiumum. */
1508 return editor->nUndoLimit;
1511 return editor->pUndoStack != NULL;
1513 return editor->pRedoStack != NULL;
1514 case WM_UNDO: /* FIXME: actually not the same */
1523 /* these flags are equivalent to the ES_* counterparts */
1524 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1525 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1526 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1532 /* these flags are equivalent to ES_* counterparts
1533 * ECO_READONLY is already implemented in the code, only requires
1534 * setting the bit to work
1536 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1537 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1538 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1539 DWORD settings = mask & raw;
1555 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1557 if (lParam & ECO_AUTOWORDSELECTION)
1558 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1559 if (lParam & ECO_SELECTIONBAR)
1560 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1561 if (lParam & ECO_VERTICAL)
1562 FIXME("ECO_VERTICAL not implemented yet!\n");
1563 if (lParam & ECO_AUTOHSCROLL)
1564 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1565 if (lParam & ECO_AUTOVSCROLL)
1566 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1567 if (lParam & ECO_NOHIDESEL)
1568 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1569 if (lParam & ECO_WANTRETURN)
1570 FIXME("ECO_WANTRETURN not implemented yet!\n");
1576 ME_InvalidateSelection(editor);
1577 ME_SetSelection(editor, wParam, lParam);
1578 ME_InvalidateSelection(editor);
1579 ME_SendSelChange(editor);
1582 case EM_SETSCROLLPOS:
1584 POINT *point = (POINT *)lParam;
1585 /* Native behavior when point->y is too large is very odd / dosn't follow MSDN.
1586 This seems to be a pretty close approximation of what it does. */
1587 ME_Scroll(editor, 0, -(min(point->y, (editor->nTotalLength - 1)) - editor->nScrollPosY));
1590 case EM_AUTOURLDETECT:
1592 if (wParam==1 || wParam ==0)
1594 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1597 return E_INVALIDARG;
1599 case EM_GETAUTOURLDETECT:
1601 return editor->AutoURLDetect_bEnable;
1605 CHARRANGE *pRange = (CHARRANGE *)lParam;
1606 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1607 ME_InvalidateSelection(editor);
1608 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1609 ME_InvalidateSelection(editor);
1610 ME_SendSelChange(editor);
1613 case EM_SHOWSCROLLBAR:
1615 if (wParam == SB_VERT)
1616 editor->bScrollY = lParam;
1617 else if (wParam == SB_HORZ)
1618 editor->bScrollX = lParam;
1619 ME_UpdateScrollBar(editor);
1624 LPWSTR wszText = (LPWSTR)lParam;
1625 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1626 size_t len = wszText ? lstrlenW(wszText) : 0;
1629 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1630 if (pStruct->codepage != 1200) {
1631 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1634 /* FIXME: this should support RTF strings too, according to MSDN */
1635 if (pStruct->flags & ST_SELECTION) {
1636 ME_GetSelection(editor, &from, &to);
1637 style = ME_GetSelectionInsertStyle(editor);
1638 ME_InternalDeleteText(editor, from, to - from);
1639 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1640 ME_ReleaseStyle(style);
1643 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1644 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1647 ME_CommitUndo(editor);
1648 if (!(pStruct->flags & ST_KEEPUNDO))
1649 ME_EmptyUndoStack(editor);
1650 ME_UpdateRepaint(editor);
1653 case EM_SETBKGNDCOLOR:
1655 LRESULT lColor = ME_GetBackColor(editor);
1656 if (editor->rgbBackColor != -1)
1657 DeleteObject(editor->hbrBackground);
1660 editor->rgbBackColor = -1;
1661 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1665 editor->rgbBackColor = lParam;
1666 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1668 if (editor->bRedraw)
1670 InvalidateRect(hWnd, NULL, TRUE);
1676 return editor->nModifyStep == 0 ? 0 : 1;
1680 editor->nModifyStep = 0x80000000;
1682 editor->nModifyStep = 0;
1686 case EM_SETREADONLY:
1688 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1690 nStyle |= ES_READONLY;
1692 nStyle &= ~ES_READONLY;
1693 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1696 case EM_SETEVENTMASK:
1698 DWORD nOldMask = editor->nEventMask;
1700 editor->nEventMask = lParam;
1703 case EM_GETEVENTMASK:
1704 return editor->nEventMask;
1705 case EM_SETCHARFORMAT:
1707 CHARFORMAT2W buf, *p;
1708 BOOL bRepaint = TRUE;
1709 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1710 if (!wParam || (editor->mode & TM_PLAINTEXT))
1711 ME_SetDefaultCharFormat(editor, p);
1712 else if (wParam == (SCF_WORD | SCF_SELECTION))
1713 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1714 else if (wParam == SCF_ALL)
1715 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1718 ME_GetSelection(editor, &from, &to);
1719 bRepaint = (from != to);
1720 ME_SetSelectionCharFormat(editor, p);
1722 ME_CommitUndo(editor);
1724 ME_UpdateRepaint(editor);
1727 case EM_GETCHARFORMAT:
1729 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1730 if (dst->cbSize != sizeof(CHARFORMATA) &&
1731 dst->cbSize != sizeof(CHARFORMATW) &&
1732 dst->cbSize != sizeof(CHARFORMAT2A) &&
1733 dst->cbSize != sizeof(CHARFORMAT2W))
1735 tmp.cbSize = sizeof(tmp);
1737 ME_GetDefaultCharFormat(editor, &tmp);
1739 ME_GetSelectionCharFormat(editor, &tmp);
1740 ME_CopyToCFAny(dst, &tmp);
1743 case EM_SETPARAFORMAT:
1744 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1745 ME_UpdateRepaint(editor);
1746 ME_CommitUndo(editor);
1748 case EM_GETPARAFORMAT:
1749 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1751 case EM_GETFIRSTVISIBLELINE:
1753 ME_DisplayItem *p = editor->pBuffer->pFirst;
1754 int y = editor->nScrollPosY;
1759 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1760 if (p->type == diTextEnd)
1762 if (p->type == diParagraph) {
1763 ypara = p->member.para.nYPos;
1766 ystart = ypara + p->member.row.nYPos;
1767 yend = ystart + p->member.row.nHeight;
1775 case EM_HIDESELECTION:
1777 editor->bHideSelection = (wParam != 0);
1778 ME_InvalidateSelection(editor);
1783 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1784 nPos += 8 * lParam; /* FIXME follow the original */
1789 if (nPos != editor->nScrollPosY) {
1790 int dy = editor->nScrollPosY - nPos;
1791 editor->nScrollPosY = nPos;
1792 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1793 if (editor->bRedraw)
1795 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1799 return TRUE; /* Should return false if a single line richedit control */
1804 ME_GetSelection(editor, &from, &to);
1805 ME_InternalDeleteText(editor, from, to-from);
1806 ME_CommitUndo(editor);
1807 ME_UpdateRepaint(editor);
1814 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1815 size_t len = lstrlenW(wszText);
1816 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1818 ME_GetSelection(editor, &from, &to);
1819 style = ME_GetSelectionInsertStyle(editor);
1820 ME_InternalDeleteText(editor, from, to-from);
1821 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1822 ME_ReleaseStyle(style);
1823 ME_EndToUnicode(hWnd, wszText);
1824 /* drop temporary style if line end */
1825 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1826 if (len>0 && wszText[len-1] == '\n')
1827 ME_ClearTempStyle(editor);
1829 ME_CommitUndo(editor);
1831 ME_EmptyUndoStack(editor);
1832 ME_UpdateRepaint(editor);
1835 case EM_SCROLLCARET:
1837 int top, bottom; /* row's edges relative to document top */
1838 ME_DisplayItem *para, *row;
1840 row = ME_RowStart(editor->pCursors[0].pRun);
1841 para = ME_GetParagraph(row);
1842 top = para->member.para.nYPos + row->member.row.nYPos;
1843 bottom = top + row->member.row.nHeight;
1845 if ((top < editor->nScrollPosY)
1846 || (editor->nScrollPosY + editor->sizeWindow.cy < bottom))
1849 int prevScrollPosY = editor->nScrollPosY;
1851 if (top < editor->nScrollPosY) /* caret above window */
1852 editor->nScrollPosY = top;
1853 else /* caret below window */
1854 editor->nScrollPosY = bottom - editor->sizeWindow.cy;
1856 if (editor->nScrollPosY < 0)
1857 editor->nScrollPosY = 0;
1859 dy = prevScrollPosY - editor->nScrollPosY;
1860 SetScrollPos(hWnd, SB_VERT, editor->nScrollPosY, TRUE);
1861 if (editor->bRedraw)
1863 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1875 BOOL bRepaint = LOWORD(lParam);
1878 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1879 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1881 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1882 ReleaseDC(hWnd, hDC);
1883 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1884 ME_SetDefaultCharFormat(editor, &fmt);
1886 ME_CommitUndo(editor);
1888 ME_UpdateRepaint(editor);
1893 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1896 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1897 if (!IsWindowUnicode(hWnd) && !strncmp((char *)lParam, "{\\rtf", 5))
1899 /* Undocumented: WM_SETTEXT supports RTF text */
1900 ME_StreamInRTFString(editor, 0, (char *)lParam);
1904 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1905 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1906 if (lstrlenW(wszText) > 0)
1908 /* uses default style! */
1909 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1911 ME_EndToUnicode(hWnd, wszText);
1915 TRACE("WM_SETTEXT - NULL\n");
1916 ME_CommitUndo(editor);
1917 ME_EmptyUndoStack(editor);
1918 ME_SetSelection(editor, 0, 0);
1919 ME_UpdateRepaint(editor);
1924 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1925 if (IsClipboardFormatAvailable(nRTFFormat))
1927 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1935 ME_GlobalDestStruct gds;
1936 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1939 if (IsClipboardFormatAvailable(nRTFFormat))
1940 cf = nRTFFormat, dwFormat = SF_RTF;
1941 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1942 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1946 if (!OpenClipboard(hWnd))
1948 gds.hData = GetClipboardData(cf);
1950 es.dwCookie = (DWORD)&gds;
1951 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1952 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1960 LPDATAOBJECT dataObj = NULL;
1964 if (editor->cPasswordMask)
1965 return 0; /* Copying or Cutting masked text isn't allowed */
1967 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1968 if(editor->lpOleCallback)
1969 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1970 if(FAILED(hr) || !dataObj)
1971 hr = ME_GetDataObject(editor, &range, &dataObj);
1973 hr = OleSetClipboard(dataObj);
1974 IDataObject_Release(dataObj);
1976 if (SUCCEEDED(hr) && msg == WM_CUT)
1978 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1979 ME_CommitUndo(editor);
1980 ME_UpdateRepaint(editor);
1984 case WM_GETTEXTLENGTH:
1985 return ME_GetTextLength(editor);
1986 case EM_GETTEXTLENGTHEX:
1987 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1990 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1992 tr.chrg.cpMax = wParam-1;
1993 tr.lpstrText = (WCHAR *)lParam;
1994 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1998 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2001 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2002 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2004 if (ex->flags & GT_SELECTION)
2006 ME_GetSelection(editor, &nStart, &nCount);
2008 nCount = min(nCount, ex->cb - 1);
2013 nCount = ex->cb - 1;
2015 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
2017 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2018 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2022 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2023 we can just take a bigger buffer? :) */
2024 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2025 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
2026 DWORD buflen = ex->cb;
2030 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2031 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2033 HeapFree(GetProcessHeap(),0,buffer);
2040 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2041 ME_GetSelection(editor, &from, &to);
2042 tr.chrg.cpMin = from;
2044 tr.lpstrText = (WCHAR *)lParam;
2045 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2047 case EM_GETSCROLLPOS:
2049 POINT *point = (POINT *)lParam;
2050 point->x = 0; /* FIXME side scrolling not implemented */
2051 point->y = editor->nScrollPosY;
2054 case EM_GETTEXTRANGE:
2056 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2057 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
2058 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
2059 editor->bEmulateVersion10, ME_GetTextLength(editor));
2060 if (IsWindowUnicode(hWnd))
2061 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2064 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2065 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2066 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2067 /* FIXME this is a potential security hole (buffer overrun)
2068 if you know more about wchar->mbyte conversion please explain
2070 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2077 ME_DisplayItem *run;
2078 const BOOL bUnicode = IsWindowUnicode(hWnd);
2079 const unsigned int nMaxChars = *(WORD *) lParam;
2080 unsigned int nEndChars, nCharsLeft = nMaxChars;
2081 char *dest = (char *) lParam;
2082 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2083 we need to know how if we have the extra \n or not */
2084 int nLF = editor->bEmulateVersion10;
2086 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2087 bUnicode ? "Unicode" : "Ansi");
2089 run = ME_FindRowWithNumber(editor, wParam);
2093 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2094 && !(run->member.run.nFlags & MERF_ENDPARA))
2098 if (run->type != diRun)
2100 strText = run->member.run.strText;
2101 nCopy = min(nCharsLeft, strText->nLen);
2104 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2106 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2107 nCharsLeft, NULL, NULL);
2108 dest += nCopy * (bUnicode ? sizeof(WCHAR) : 1);
2109 nCharsLeft -= nCopy;
2112 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2113 nEndChars = min(nCharsLeft, 2 + nLF);
2114 nCharsLeft -= nEndChars;
2117 const WCHAR src[] = {'\r', '\0'};
2118 const WCHAR src10[] = {'\r', '\n', '\0'};
2119 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2122 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2124 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2126 if (nEndChars == 2 + nLF)
2127 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2129 return nMaxChars - nCharsLeft;
2131 case EM_GETLINECOUNT:
2133 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2136 while (item != editor->pBuffer->pLast)
2138 assert(item->type == diParagraph);
2139 nRows += item->member.para.nRows;
2140 item = item->member.para.next_para;
2142 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2143 return max(1, nRows);
2145 case EM_LINEFROMCHAR:
2148 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2150 return ME_RowNumberFromCharOfs(editor, wParam);
2152 case EM_EXLINEFROMCHAR:
2155 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2157 return ME_RowNumberFromCharOfs(editor, lParam);
2161 ME_DisplayItem *item, *para;
2165 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2167 item = ME_FindRowWithNumber(editor, wParam);
2170 para = ME_GetParagraph(item);
2171 item = ME_FindItemFwd(item, diRun);
2172 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2173 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2178 ME_DisplayItem *item, *item_end;
2179 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2181 if (wParam > ME_GetTextLength(editor))
2185 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2188 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2189 item = ME_RowStart(item);
2190 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2191 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2192 if (item_end->type == diStartRow)
2193 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2195 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2196 - (editor->bEmulateVersion10?2:1);
2197 nChars = nNextLineOfs - nThisLineOfs;
2198 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2201 case EM_EXLIMITTEXT:
2203 if (wParam != 0 || lParam < 0)
2206 editor->nTextLimit = 65536;
2208 editor->nTextLimit = (int) lParam;
2211 case EM_GETLIMITTEXT:
2213 return editor->nTextLimit;
2217 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2218 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2221 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2222 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2223 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2227 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2228 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2231 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2232 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2233 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2237 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2238 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2240 case EM_FINDTEXTEXW:
2242 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2243 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2246 if (!wParam || !lParam)
2248 *(int *)wParam = editor->nZoomNumerator;
2249 *(int *)lParam = editor->nZoomDenominator;
2252 return ME_SetZoom(editor, wParam, lParam);
2253 case EM_CHARFROMPOS:
2254 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2255 case EM_POSFROMCHAR:
2257 ME_DisplayItem *pRun;
2258 int nCharOfs, nOffset, nLength;
2262 /* detect which API version we're dealing with */
2263 if (wParam >= 0x40000)
2265 nLength = ME_GetTextLength(editor);
2267 if (nCharOfs < nLength) {
2268 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2269 assert(pRun->type == diRun);
2270 pt.y = pRun->member.run.pt.y;
2271 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2272 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2275 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2277 if (wParam >= 0x40000) {
2278 *(POINTL *)wParam = pt;
2280 return MAKELONG( pt.x, pt.y );
2283 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2284 { /* Squelch the default horizontal scrollbar it would make */
2285 si.cbSize = sizeof(SCROLLINFO);
2286 si.fMask = SIF_POS | SIF_RANGE;
2290 SetScrollInfo(hWnd, SB_HORZ, &si, FALSE);
2292 ME_CommitUndo(editor);
2293 ME_WrapMarkedParagraphs(editor);
2294 ME_MoveCaret(editor);
2297 ME_DestroyEditor(editor);
2298 SetWindowLongPtrW(hWnd, 0, 0);
2300 case WM_LBUTTONDOWN:
2302 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2304 ME_LinkNotify(editor,msg,wParam,lParam);
2307 if (GetCapture() == hWnd)
2308 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2309 ME_LinkNotify(editor,msg,wParam,lParam);
2312 if (GetCapture() == hWnd)
2314 ME_LinkNotify(editor,msg,wParam,lParam);
2316 case WM_LBUTTONDBLCLK:
2317 ME_LinkNotify(editor,msg,wParam,lParam);
2318 ME_SelectWord(editor);
2320 case WM_CONTEXTMENU:
2321 ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2324 if (editor->bRedraw)
2329 hDC = BeginPaint(hWnd, &ps);
2330 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2331 EndPaint(hWnd, &ps);
2335 ME_ShowCaret(editor);
2336 ME_SendOldNotify(editor, EN_SETFOCUS);
2339 ME_HideCaret(editor);
2340 ME_SendOldNotify(editor, EN_KILLFOCUS);
2344 if (editor->bRedraw)
2346 HDC hDC = (HDC)wParam;
2348 if (GetUpdateRect(hWnd,&rc,TRUE))
2350 FillRect(hDC, &rc, editor->hbrBackground);
2356 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2359 if (ME_KeyDown(editor, LOWORD(wParam)))
2364 WCHAR wstr = LOWORD(wParam);
2365 if (editor->AutoURLDetect_bEnable)
2366 ME_AutoURLDetect(editor, wstr);
2370 case 1: /* Ctrl-A */
2371 ME_SetSelection(editor, 0, -1);
2373 case 3: /* Ctrl-C */
2374 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2378 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2379 MessageBeep(MB_ICONERROR);
2380 return 0; /* FIXME really 0 ? */
2385 case 22: /* Ctrl-V */
2386 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2388 case 24: /* Ctrl-X */
2389 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2391 case 25: /* Ctrl-Y */
2392 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2394 case 26: /* Ctrl-Z */
2395 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2398 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2399 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2400 ME_Style *style = ME_GetInsertStyle(editor, 0);
2401 ME_SaveTempStyle(editor);
2402 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2403 ME_ReleaseStyle(style);
2404 ME_CommitUndo(editor);
2405 ME_UpdateRepaint(editor);
2409 case EM_SCROLL: /* fall through */
2413 int nPos = editor->nScrollPosY;
2414 int origNPos = nPos;
2415 int lineHeight = 24;
2416 si.cbSize = sizeof(SCROLLINFO);
2417 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
2418 GetScrollInfo(hWnd, SB_VERT, &si);
2419 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2420 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2421 if (lineHeight <= 0) lineHeight = 24;
2422 switch(LOWORD(wParam)) {
2425 if (nPos<0) nPos = 0;
2429 nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2430 if (nEnd < 0) nEnd = 0;
2432 if (nPos>=nEnd) nPos = nEnd;
2436 nPos -= editor->sizeWindow.cy;
2437 if (nPos<0) nPos = 0;
2440 nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2441 if (nEnd < 0) nEnd = 0;
2442 nPos += editor->sizeWindow.cy;
2443 if (nPos>=nEnd) nPos = nEnd;
2446 case SB_THUMBPOSITION:
2447 nPos = si.nTrackPos;
2450 if (nPos != editor->nScrollPosY) {
2451 int dy = editor->nScrollPosY - nPos;
2452 editor->nScrollPosY = nPos;
2453 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
2454 if (editor->bRedraw)
2456 ScrollWindow(hWnd, 0, dy, NULL, NULL);
2460 if (msg == EM_SCROLL)
2461 return 0x00010000 | (((nPos - origNPos)/lineHeight) & 0xffff);
2466 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2467 UINT pulScrollLines;
2468 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2469 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
2470 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2471 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
2476 if (nPos != editor->nScrollPosY) {
2477 int dy = editor->nScrollPosY - nPos;
2478 editor->nScrollPosY = nPos;
2479 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
2480 if (editor->bRedraw)
2482 ScrollWindow(hWnd, 0, dy, NULL, NULL);
2490 *((RECT *)lParam) = editor->rcFormat;
2498 RECT *rc = (RECT *)lParam;
2502 editor->rcFormat.left += rc->left;
2503 editor->rcFormat.top += rc->top;
2504 editor->rcFormat.right += rc->right;
2505 editor->rcFormat.bottom += rc->bottom;
2509 editor->rcFormat = *rc;
2514 GetClientRect(hWnd, &editor->rcFormat);
2516 if (msg != EM_SETRECTNP)
2517 ME_RewrapRepaint(editor);
2520 case EM_REQUESTRESIZE:
2521 ME_SendRequestResize(editor, TRUE);
2524 editor->bRedraw = wParam;
2528 GetClientRect(hWnd, &editor->rcFormat);
2529 ME_RewrapRepaint(editor);
2530 return DefWindowProcW(hWnd, msg, wParam, lParam);
2532 case EM_GETOLEINTERFACE:
2534 LPVOID *ppvObj = (LPVOID*) lParam;
2535 return CreateIRichEditOle(editor, ppvObj);
2537 case EM_GETPASSWORDCHAR:
2539 return editor->cPasswordMask;
2541 case EM_SETOLECALLBACK:
2542 if(editor->lpOleCallback)
2543 IUnknown_Release(editor->lpOleCallback);
2544 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2545 if(editor->lpOleCallback)
2546 IUnknown_AddRef(editor->lpOleCallback);
2548 case EM_GETWORDBREAKPROC:
2549 return (LRESULT)editor->pfnWordBreak;
2550 case EM_SETWORDBREAKPROC:
2552 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2554 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2555 return (LRESULT)pfnOld;
2557 case EM_SETTEXTMODE:
2562 if ((ret = RichEditANSIWndProc(hWnd, WM_GETTEXTLENGTH, 0, 0)) == 0)
2564 /*Check for valid wParam*/
2565 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2566 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2567 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2571 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2573 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2574 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2576 /*FIXME: Currently no support for undo level and code page options*/
2577 editor->mode = (editor->mode & (~mask)) | changes;
2583 case EM_SETPASSWORDCHAR:
2585 editor->cPasswordMask = wParam;
2586 ME_RewrapRepaint(editor);
2591 return DefWindowProcW(hWnd, msg, wParam, lParam);
2597 /******************************************************************
2598 * RichEdit10ANSIWndProc (RICHED20.9)
2600 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2604 /* FIXME: this is NOT the same as 2.0 version */
2605 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2606 if (msg == WM_NCCREATE)
2608 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2610 editor->bEmulateVersion10 = TRUE;
2611 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2616 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2618 HWND hWnd = editor->hWnd;
2619 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2622 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2625 ME_Cursor tmpCursor;
2629 x = (short)LOWORD(lParam);
2630 y = (short)HIWORD(lParam);
2631 ME_FindPixelPos(editor, x, y, &tmpCursor, &bNothing);
2632 tmpRun = &tmpCursor.pRun->member.run;
2633 if (tmpRun->style->fmt.dwMask & CFM_UNDERLINE)
2634 FIXME("CFM_UNDERLINE! GASP!\n");
2635 if (tmpRun->style->fmt.dwEffects & CFE_UNDERLINE)
2636 FIXME("CFE_UNDERLINE! GASP!\n");
2638 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2639 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2640 { /* The clicked run has CFE_LINK set */
2641 info.nmhdr.hwndFrom = editor->hWnd;
2642 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2643 info.nmhdr.code = EN_LINK;
2645 info.wParam = wParam;
2646 info.lParam = lParam;
2647 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2648 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2649 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2653 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2655 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2658 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2659 item = item->member.para.next_para;
2662 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2663 item = item->member.para.next_para;
2670 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2672 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2674 WCHAR *pStart = buffer;
2683 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2686 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2693 item = ME_FindItemFwd(item, diRun);
2696 while(nChars && item)
2698 int nLen = ME_StrLen(item->member.run.strText);
2702 if (item->member.run.nFlags & MERF_ENDPARA)
2704 if (!ME_FindItemFwd(item, diRun))
2705 /* No '\r' is appended to the last paragraph. */
2715 /* our end paragraph consists of 2 characters now */
2716 if (editor->bEmulateVersion10)
2721 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2728 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2732 item = ME_FindItemFwd(item, diRun);
2735 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2739 void ME_RegisterEditorClass(HINSTANCE hInstance)
2745 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2746 wcW.lpfnWndProc = RichEditANSIWndProc;
2748 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2749 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2751 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2752 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2753 wcW.lpszMenuName = NULL;
2754 wcW.lpszClassName = wszClassName;
2755 bResult = RegisterClassW(&wcW);
2757 wcW.lpszClassName = wszClassName50;
2758 bResult = RegisterClassW(&wcW);
2761 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2762 wcA.lpfnWndProc = RichEditANSIWndProc;
2764 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2765 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2767 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2768 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2769 wcA.lpszMenuName = NULL;
2770 wcA.lpszClassName = "RichEdit20A";
2771 bResult = RegisterClassA(&wcA);
2773 wcA.lpszClassName = "RichEdit50A";
2774 bResult = RegisterClassA(&wcA);
2778 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2779 /* FIXME: Not implemented */
2780 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2781 hWnd, msg, get_msg_name(msg), wParam, lParam);
2782 return DefWindowProcW(hWnd, msg, wParam, lParam);
2785 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2786 /* FIXME: Not implemented */
2787 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2788 hWnd, msg, get_msg_name(msg), wParam, lParam);
2789 return DefWindowProcW(hWnd, msg, wParam, lParam);
2792 /******************************************************************
2793 * REExtendedRegisterClass (RICHED20.8)
2795 * FIXME undocumented
2796 * Need to check for errors and implement controls and callbacks
2798 LRESULT WINAPI REExtendedRegisterClass(void)
2803 FIXME("semi stub\n");
2807 wcW.hInstance = NULL;
2810 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2811 wcW.lpszMenuName = NULL;
2813 if (!ME_ListBoxRegistered)
2815 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2816 wcW.lpfnWndProc = REListWndProc;
2817 wcW.lpszClassName = wszClassNameListBox;
2818 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2821 if (!ME_ComboBoxRegistered)
2823 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2824 wcW.lpfnWndProc = REComboWndProc;
2825 wcW.lpszClassName = wszClassNameComboBox;
2826 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2830 if (ME_ListBoxRegistered)
2832 if (ME_ComboBoxRegistered)
2838 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2858 int curf_ef, link_ef, def_ef;
2859 int cur_prefx, prefx_cnt;
2860 int sel_min, sel_max;
2863 int URLmin, URLmax = 0;
2866 CHARFORMAT2W cur_format;
2867 CHARFORMAT2W default_format;
2869 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2870 sel_min = ins_pt.cpMin;
2871 sel_max = ins_pt.cpMax;
2872 if (sel_min==sel_max)
2874 if (sel_min!=sel_max)
2875 car_pos = ME_GetTextLength(editor)+1;
2876 cur_format.cbSize = sizeof(cur_format);
2877 default_format.cbSize = sizeof(default_format);
2878 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2879 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2880 link.cbSize = sizeof(link);
2881 link.dwMask = CFM_LINK;
2882 link.dwEffects = CFE_LINK;
2883 curf_ef = cur_format.dwEffects & link.dwEffects;
2884 def_ef = default_format.dwEffects & link.dwEffects;
2885 link_ef = link.dwEffects & link.dwEffects;
2886 if (curf_ef == link_ef)
2888 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2890 ME_SetSelection(editor, car_pos, car_pos);
2891 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2896 if (curf_ef == def_ef)
2899 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2900 while (cur_prefx<=prefx_cnt)
2904 ft.lpstrText = prefixes[cur_prefx].text;
2905 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2906 URLmax=max(0, car_pos);
2907 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2909 URLmax = ME_GetTextLength(editor)+1;
2911 ft.chrg.cpMin = URLmin;
2912 ft.chrg.cpMax = URLmax;
2913 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2919 url.cpMax=car_pos-1;
2920 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);