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
80 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
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 (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
231 #define STACK_SIZE_DEFAULT 100
232 #define STACK_SIZE_MAX 1000
234 #define TEXT_LIMIT_DEFAULT 32767
236 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
238 static BOOL ME_RegisterEditorClass(HINSTANCE);
240 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
241 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
242 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
243 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
246 HANDLE me_heap = NULL;
248 static BOOL ME_ListBoxRegistered = FALSE;
249 static BOOL ME_ComboBoxRegistered = FALSE;
251 static inline int is_version_nt(void)
253 return !(GetVersion() & 0x80000000);
256 static ME_TextBuffer *ME_MakeText(void) {
258 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
260 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
261 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
267 p1->member.para.next_para = p2;
268 p2->member.para.prev_para = p1;
269 p2->member.para.nCharOfs = 0;
273 buf->pCharStyle = NULL;
279 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
281 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
284 TRACE("%08x %p\n", dwFormat, stream);
291 ME_StreamInFill(stream);
292 if (stream->editstream->dwError)
298 if (!(dwFormat & SF_UNICODE))
300 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
301 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
306 nWideChars = stream->dwSize >> 1;
307 pText = (WCHAR *)stream->buffer;
310 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
311 if (stream->dwSize == 0)
315 ME_CommitUndo(editor);
316 ME_UpdateRepaint(editor);
320 static void ME_RTFCharAttrHook(RTF_Info *info)
323 fmt.cbSize = sizeof(fmt);
327 switch(info->rtfMinor)
330 /* FIXME add more flags once they're implemented */
331 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
332 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
333 fmt.yHeight = 12*20; /* 12pt */
337 fmt.dwMask = CFM_BOLD;
338 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
341 fmt.dwMask = CFM_ITALIC;
342 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
345 fmt.dwMask = CFM_UNDERLINE;
346 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
347 fmt.bUnderlineType = CFU_CF1UNDERLINE;
350 fmt.dwMask = CFM_UNDERLINE;
354 fmt.dwMask = CFM_STRIKEOUT;
355 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
359 case rtfSubScrShrink:
360 case rtfSuperScrShrink:
362 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
363 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
364 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
365 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
368 fmt.dwMask = CFM_HIDDEN;
369 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
372 fmt.dwMask = CFM_BACKCOLOR;
374 if (info->rtfParam == 0)
375 fmt.dwEffects = CFE_AUTOBACKCOLOR;
376 else if (info->rtfParam != rtfNoParam)
378 RTFColor *c = RTFGetColor(info, info->rtfParam);
379 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
383 fmt.dwMask = CFM_COLOR;
385 if (info->rtfParam == 0)
386 fmt.dwEffects = CFE_AUTOCOLOR;
387 else if (info->rtfParam != rtfNoParam)
389 RTFColor *c = RTFGetColor(info, info->rtfParam);
391 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
397 if (info->rtfParam != rtfNoParam)
399 RTFFont *f = RTFGetFont(info, info->rtfParam);
402 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
403 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
404 fmt.bCharSet = f->rtfFCharSet;
405 fmt.dwMask = CFM_FACE | CFM_CHARSET;
410 fmt.dwMask = CFM_SIZE;
411 if (info->rtfParam != rtfNoParam)
412 fmt.yHeight = info->rtfParam*10;
417 RTFFlushOutputBuffer(info);
418 /* FIXME too slow ? how come ? */
419 style2 = ME_ApplyStyle(info->style, &fmt);
420 ME_ReleaseStyle(info->style);
421 info->style = style2;
422 info->styleChanged = TRUE;
426 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
427 the same tags mean different things in different contexts */
428 static void ME_RTFParAttrHook(RTF_Info *info)
431 fmt.cbSize = sizeof(fmt);
434 switch(info->rtfMinor)
436 case rtfParDef: /* restores default paragraph attributes */
437 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
438 fmt.wAlignment = PFA_LEFT;
440 fmt.dxOffset = fmt.dxStartIndent = 0;
441 RTFFlushOutputBuffer(info);
442 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
446 ME_DisplayItem *para;
448 RTFFlushOutputBuffer(info);
449 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
450 assert(para->member.para.pCells);
451 para->member.para.bTable = TRUE;
455 ME_GetSelectionParaFormat(info->editor, &fmt);
456 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
457 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
458 fmt.dxOffset = -info->rtfParam;
461 ME_GetSelectionParaFormat(info->editor, &fmt);
462 fmt.dwMask = PFM_STARTINDENT;
463 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
466 fmt.dwMask = PFM_RIGHTINDENT;
467 fmt.dxRightIndent = info->rtfParam;
471 fmt.dwMask = PFM_ALIGNMENT;
472 fmt.wAlignment = PFA_LEFT;
475 fmt.dwMask = PFM_ALIGNMENT;
476 fmt.wAlignment = PFA_RIGHT;
479 fmt.dwMask = PFM_ALIGNMENT;
480 fmt.wAlignment = PFA_CENTER;
483 ME_GetSelectionParaFormat(info->editor, &fmt);
484 if (!(fmt.dwMask & PFM_TABSTOPS))
486 fmt.dwMask |= PFM_TABSTOPS;
489 if (fmt.cTabCount < MAX_TAB_STOPS)
490 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
494 RTFFlushOutputBuffer(info);
495 /* FIXME too slow ? how come ?*/
496 ME_SetSelectionParaFormat(info->editor, &fmt);
500 static void ME_RTFTblAttrHook(RTF_Info *info)
502 ME_DisplayItem *para;
504 switch (info->rtfMinor)
507 RTFFlushOutputBuffer(info);
508 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
510 /* Release possibly inherited cell definitions */
511 ME_DestroyTableCellList(para);
513 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
514 para->member.para.pCells->nRightBoundary = 0;
515 para->member.para.pCells->next = NULL;
516 para->member.para.pLastCell = para->member.para.pCells;
519 RTFFlushOutputBuffer(info);
520 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
522 if (para->member.para.pLastCell->nRightBoundary)
524 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
527 para->member.para.pLastCell->next = pCell;
528 para->member.para.pLastCell = pCell;
530 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
535 static void ME_RTFReadHook(RTF_Info *info) {
536 switch(info->rtfClass)
539 switch(info->rtfMajor)
542 if (info->stackTop < maxStack) {
543 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
544 info->stack[info->stackTop].codePage = info->codePage;
545 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
548 info->styleChanged = FALSE;
553 RTFFlushOutputBuffer(info);
554 if (info->stackTop<=1) {
555 info->rtfClass = rtfEOF;
559 assert(info->stackTop >= 0);
560 if (info->styleChanged)
562 /* FIXME too slow ? how come ? */
563 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
564 ME_ReleaseStyle(info->style);
566 info->codePage = info->stack[info->stackTop].codePage;
567 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
574 switch(info->rtfMajor)
577 ME_RTFCharAttrHook(info);
580 ME_RTFParAttrHook(info);
583 ME_RTFTblAttrHook(info);
586 if (info->rtfMinor == rtfCell)
588 RTFFlushOutputBuffer(info);
589 ME_InsertTableCellFromCursor(info->editor, 0);
597 ME_StreamInFill(ME_InStream *stream)
599 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
600 (BYTE *)stream->buffer,
601 sizeof(stream->buffer),
602 (LONG *)&stream->dwSize);
606 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
610 int from, to, to2, nUndoMode;
611 int nEventMask = editor->nEventMask;
612 ME_InStream inStream;
614 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
615 editor->nEventMask = 0;
617 ME_GetSelection(editor, &from, &to);
618 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
619 style = ME_GetSelectionInsertStyle(editor);
621 ME_InternalDeleteText(editor, from, to-from);
624 style = editor->pBuffer->pDefaultStyle;
625 ME_AddRefStyle(style);
626 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
627 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
629 ME_ClearTempStyle(editor);
630 /* FIXME restore default paragraph formatting ! */
634 /* Back up undo mode to a local variable */
635 nUndoMode = editor->nUndoMode;
637 /* Only create an undo if SFF_SELECTION is set */
638 if (!(format & SFF_SELECTION))
639 editor->nUndoMode = umIgnore;
641 inStream.editstream = stream;
642 inStream.editstream->dwError = 0;
648 /* Check if it's really RTF, and if it is not, use plain text */
649 ME_StreamInFill(&inStream);
650 if (!inStream.editstream->dwError)
652 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
660 if (!inStream.editstream->dwError)
662 if (format & SF_RTF) {
663 /* setup the RTF parser */
664 memset(&parser, 0, sizeof parser);
665 RTFSetEditStream(&parser, &inStream);
666 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
667 parser.hwndEdit = editor->hWnd;
668 parser.editor = editor;
669 parser.style = style;
672 RTFSetReadHook(&parser, ME_RTFReadHook);
677 RTFFlushOutputBuffer(&parser);
680 style = parser.style;
682 else if (format & SF_TEXT)
683 ME_StreamInText(editor, format, &inStream, style);
685 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
686 ME_GetSelection(editor, &to, &to2);
687 /* put the cursor at the top */
688 if (!(format & SFF_SELECTION))
689 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
692 /* Restore saved undo mode */
693 editor->nUndoMode = nUndoMode;
695 /* even if we didn't add an undo, we need to commit anything on the stack */
696 ME_CommitUndo(editor);
698 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
699 if (!(format & SFF_SELECTION))
700 ME_EmptyUndoStack(editor);
702 ME_ReleaseStyle(style);
703 editor->nEventMask = nEventMask;
706 ME_UpdateRepaint(editor);
708 if (!(format & SFF_SELECTION)) {
709 ME_ClearTempStyle(editor);
711 ME_MoveCaret(editor);
712 ME_SendSelChange(editor);
713 ME_SendRequestResize(editor, FALSE);
719 typedef struct tagME_RTFStringStreamStruct
724 } ME_RTFStringStreamStruct;
726 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
728 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
731 count = min(cb, pStruct->length - pStruct->pos);
732 memmove(lpBuff, pStruct->string + pStruct->pos, count);
733 pStruct->pos += count;
739 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
742 ME_RTFStringStreamStruct data;
744 data.string = string;
745 data.length = strlen(string);
747 es.dwCookie = (DWORD)&data;
748 es.pfnCallback = ME_ReadFromRTFString;
749 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
754 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
756 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
758 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
759 item = ME_FindItemFwd(item, diParagraph);
764 nOffset -= item->member.para.nCharOfs;
765 if (nItemType == diParagraph) {
767 *nItemOffset = nOffset;
772 item = ME_FindItemFwd(item, diRun);
773 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
775 nOffset -= item->member.run.nCharOfs;
777 *nItemOffset = nOffset;
784 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
786 const int nLen = lstrlenW(text);
787 const int nTextLen = ME_GetTextLength(editor);
790 ME_DisplayItem *item;
791 ME_DisplayItem *para;
792 WCHAR wLastChar = ' ';
794 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
795 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
797 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
798 FIXME("Flags 0x%08x not implemented\n",
799 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
802 if (chrg->cpMax == -1)
805 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
807 /* when searching up, if cpMin < cpMax, then instead of searching
808 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
809 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
810 * case, it is always bigger than cpMin.
812 if (!(flags & FR_DOWN))
816 nMax = nMin > nTextLen ? nTextLen : nMin;
817 if (nMin < nSwap || chrg->cpMax == -1)
823 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
826 chrgText->cpMin = chrgText->cpMax = -1;
830 if (flags & FR_DOWN) /* Forward search */
832 /* If possible, find the character before where the search starts */
833 if ((flags & FR_WHOLEWORD) && nMin)
836 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
840 chrgText->cpMin = chrgText->cpMax = -1;
843 wLastChar = item->member.run.strText->szData[nStart];
847 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
851 chrgText->cpMin = chrgText->cpMax = -1;
855 para = ME_GetParagraph(item);
857 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
859 ME_DisplayItem *pCurItem = item;
860 int nCurStart = nStart;
863 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
865 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
869 if (nMatched == nLen)
871 ME_DisplayItem *pNextItem = pCurItem;
872 int nNextStart = nCurStart;
875 /* Check to see if next character is a whitespace */
876 if (flags & FR_WHOLEWORD)
878 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
880 pNextItem = ME_FindItemFwd(pCurItem, diRun);
881 nNextStart = -nMatched;
885 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
889 if (isalnumW(wNextChar))
893 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
896 chrgText->cpMin = nStart;
897 chrgText->cpMax = nStart + nLen;
899 TRACE("found at %d-%d\n", nStart, nStart + nLen);
902 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
904 pCurItem = ME_FindItemFwd(pCurItem, diRun);
905 para = ME_GetParagraph(pCurItem);
906 nCurStart = -nMatched;
910 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
915 if (nStart == ME_StrLen(item->member.run.strText))
917 item = ME_FindItemFwd(item, diRun);
918 para = ME_GetParagraph(item);
923 else /* Backward search */
925 /* If possible, find the character after where the search ends */
926 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
929 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
933 chrgText->cpMin = chrgText->cpMax = -1;
936 wLastChar = item->member.run.strText->szData[nEnd];
940 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
944 chrgText->cpMin = chrgText->cpMax = -1;
948 para = ME_GetParagraph(item);
951 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
953 ME_DisplayItem *pCurItem = item;
957 if (nCurEnd - nMatched == 0)
959 pCurItem = ME_FindItemBack(pCurItem, diRun);
960 para = ME_GetParagraph(pCurItem);
961 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
964 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
966 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
970 if (nMatched == nLen)
972 ME_DisplayItem *pPrevItem = pCurItem;
973 int nPrevEnd = nCurEnd;
976 /* Check to see if previous character is a whitespace */
977 if (flags & FR_WHOLEWORD)
979 if (nPrevEnd - nMatched == 0)
981 pPrevItem = ME_FindItemBack(pCurItem, diRun);
983 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
987 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
991 if (isalnumW(wPrevChar))
995 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
998 chrgText->cpMin = nStart;
999 chrgText->cpMax = nStart + nLen;
1001 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1004 if (nCurEnd - nMatched == 0)
1006 pCurItem = ME_FindItemBack(pCurItem, diRun);
1007 /* Don't care about pCurItem becoming NULL here; it's already taken
1008 * care of in the exterior loop condition */
1009 para = ME_GetParagraph(pCurItem);
1010 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1014 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1021 item = ME_FindItemBack(item, diRun);
1022 para = ME_GetParagraph(item);
1023 nEnd = ME_StrLen(item->member.run.strText);
1027 TRACE("not found\n");
1029 chrgText->cpMin = chrgText->cpMax = -1;
1035 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1037 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1038 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1050 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1054 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1055 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1057 if (ME_IsSelection(editor))
1058 ME_DeleteSelection(editor);
1059 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1060 ME_DeleteTextAtCursor(editor, 1, 1);
1063 ME_CommitUndo(editor);
1064 ME_UpdateRepaint(editor);
1065 ME_SendRequestResize(editor, FALSE);
1075 chf.cbSize = sizeof(chf);
1077 ME_GetSelectionCharFormat(editor, &chf);
1078 ME_DumpStyleToBuf(&chf, buf);
1079 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1083 ME_CheckCharOffsets(editor);
1090 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1095 if(!editor->lpOleCallback)
1097 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1098 if(selrange.cpMin == selrange.cpMax)
1099 seltype |= SEL_EMPTY;
1102 /* FIXME: Handle objects */
1103 seltype |= SEL_TEXT;
1104 if(selrange.cpMax-selrange.cpMin > 1)
1105 seltype |= SEL_MULTICHAR;
1107 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1109 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1115 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1116 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1120 ed->bEmulateVersion10 = FALSE;
1121 ed->pBuffer = ME_MakeText();
1123 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1124 ReleaseDC(hWnd, hDC);
1125 ed->bCaretShown = FALSE;
1127 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1128 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1129 ed->pCursors[0].nOffset = 0;
1130 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1131 ed->pCursors[1].nOffset = 0;
1132 ed->nLastTotalLength = ed->nTotalLength = 0;
1135 ed->rgbBackColor = -1;
1136 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1137 ed->bCaretAtEnd = FALSE;
1139 ed->nModifyStep = 0;
1140 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1141 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1142 ed->nUndoStackSize = 0;
1143 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1144 ed->nUndoMode = umAddToUndo;
1145 ed->nParagraphs = 1;
1146 ed->nLastSelStart = ed->nLastSelEnd = 0;
1147 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1148 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1150 ed->bHideSelection = FALSE;
1151 ed->nInvalidOfs = -1;
1152 ed->pfnWordBreak = NULL;
1153 ed->lpOleCallback = NULL;
1154 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1155 ed->AutoURLDetect_bEnable = FALSE;
1156 GetClientRect(hWnd, &ed->rcFormat);
1157 for (i=0; i<HFONT_CACHE_SIZE; i++)
1159 ed->pFontCache[i].nRefs = 0;
1160 ed->pFontCache[i].nAge = 0;
1161 ed->pFontCache[i].hFont = NULL;
1164 ME_CheckCharOffsets(ed);
1166 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1167 ed->cPasswordMask = '*';
1169 ed->cPasswordMask = 0;
1174 typedef struct tagME_GlobalDestStruct
1178 } ME_GlobalDestStruct;
1180 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1182 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1187 pDest = (WORD *)lpBuff;
1188 pSrc = (WORD *)GlobalLock(pData->hData);
1189 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1190 pDest[i] = pSrc[pData->nLength+i];
1192 pData->nLength += i;
1194 GlobalUnlock(pData->hData);
1198 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1200 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1205 pSrc = (BYTE *)GlobalLock(pData->hData);
1206 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1207 pDest[i] = pSrc[pData->nLength+i];
1209 pData->nLength += i;
1211 GlobalUnlock(pData->hData);
1216 void ME_DestroyEditor(ME_TextEditor *editor)
1218 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1219 ME_DisplayItem *p = pFirst, *pNext = NULL;
1222 ME_ClearTempStyle(editor);
1223 ME_EmptyUndoStack(editor);
1226 ME_DestroyDisplayItem(p);
1229 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1230 for (i=0; i<HFONT_CACHE_SIZE; i++)
1232 if (editor->pFontCache[i].hFont)
1233 DeleteObject(editor->pFontCache[i].hFont);
1235 DeleteObject(editor->hbrBackground);
1236 if(editor->lpOleCallback)
1237 IUnknown_Release(editor->lpOleCallback);
1239 FREE_OBJ(editor->pBuffer);
1240 FREE_OBJ(editor->pCursors);
1245 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1250 case DLL_PROCESS_ATTACH:
1251 DisableThreadLibraryCalls(hinstDLL);
1252 me_heap = HeapCreate (0, 0x10000, 0);
1253 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1257 case DLL_PROCESS_DETACH:
1258 UnregisterClassW(RichEdit20W, 0);
1259 UnregisterClassW(RichEdit50W, 0);
1260 UnregisterClassA("RichEdit20A", 0);
1261 UnregisterClassA("RichEdit50A", 0);
1262 if (ME_ListBoxRegistered)
1263 UnregisterClassW(REListBox20W, 0);
1264 if (ME_ComboBoxRegistered)
1265 UnregisterClassW(REComboBox20W, 0);
1267 HeapDestroy (me_heap);
1275 #define UNSUPPORTED_MSG(e) \
1277 FIXME(#e ": stub\n"); \
1278 return DefWindowProcW(hWnd, msg, wParam, lParam);
1280 static const char * const edit_messages[] = {
1309 "EM_SETPASSWORDCHAR",
1310 "EM_EMPTYUNDOBUFFER",
1311 "EM_GETFIRSTVISIBLELINE",
1313 "EM_SETWORDBREAKPROC",
1314 "EM_GETWORDBREAKPROC",
1315 "EM_GETPASSWORDCHAR",
1323 static const char * const richedit_messages[] = {
1328 "EM_EXLINEFROMCHAR",
1334 "EM_GETOLEINTERFACE",
1344 "EM_SETOLECALLBACK",
1346 "EM_SETTARGETDEVICE",
1354 "EM_GETWORDBREAKPROCEX",
1355 "EM_SETWORDBREAKPROCEX",
1357 "EM_UNKNOWN_USER_83",
1362 "EM_STOPGROUPTYPING",
1366 "EM_GETAUTOURLDETECT",
1369 "EM_GETTEXTLENGTHEX",
1372 "EM_UNKNOWN_USER_98",
1373 "EM_UNKNOWN_USER_99",
1374 "EM_SETPUNCTUATION",
1375 "EM_GETPUNCTUATION",
1376 "EM_SETWORDWRAPMODE",
1377 "EM_GETWORDWRAPMODE",
1383 "EM_UNKNOWN_USER_109",
1384 "EM_UNKNOWN_USER_110",
1385 "EM_UNKNOWN_USER_111",
1386 "EM_UNKNOWN_USER_112",
1387 "EM_UNKNOWN_USER_113",
1388 "EM_UNKNOWN_USER_114",
1389 "EM_UNKNOWN_USER_115",
1390 "EM_UNKNOWN_USER_116",
1391 "EM_UNKNOWN_USER_117",
1392 "EM_UNKNOWN_USER_118",
1393 "EM_UNKNOWN_USER_119",
1394 "EM_SETLANGOPTIONS",
1395 "EM_GETLANGOPTIONS",
1396 "EM_GETIMECOMPMODE",
1400 "EM_SETIMEMODEBIAS",
1405 get_msg_name(UINT msg)
1407 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1408 return edit_messages[msg - EM_GETSEL];
1409 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1410 return richedit_messages[msg - EM_CANPASTE];
1414 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1415 LPARAM lParam, BOOL unicode)
1417 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1419 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1420 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1422 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1423 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1429 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1430 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1431 UNSUPPORTED_MSG(EM_FMTLINES)
1432 UNSUPPORTED_MSG(EM_FORMATRANGE)
1433 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1434 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1435 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1436 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1437 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1438 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1439 UNSUPPORTED_MSG(EM_GETREDONAME)
1440 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1441 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1442 UNSUPPORTED_MSG(EM_GETUNDONAME)
1443 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1444 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1445 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1446 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1447 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1448 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1449 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1450 UNSUPPORTED_MSG(EM_SETPALETTE)
1451 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1452 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1453 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1454 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1455 UNSUPPORTED_MSG(WM_STYLECHANGING)
1456 UNSUPPORTED_MSG(WM_STYLECHANGED)
1457 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1459 /* Messages specific to Richedit controls */
1462 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1464 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1467 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1468 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1470 int vk = (int)((LPMSG)lParam)->wParam;
1471 /* if style says we want return key */
1472 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1474 code |= DLGC_WANTMESSAGE;
1476 /* we always handle ctrl-tab */
1477 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1479 code |= DLGC_WANTMESSAGE;
1486 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1487 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1488 editor = ME_MakeEditor(hWnd);
1489 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1492 case EM_EMPTYUNDOBUFFER:
1493 ME_EmptyUndoStack(editor);
1497 /* Note: wParam/lParam can be NULL */
1499 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1500 PUINT pto = lParam ? (PUINT)lParam : &to;
1501 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1502 if ((*pfrom|*pto) & 0xFFFF0000)
1504 return MAKELONG(*pfrom,*pto);
1508 CHARRANGE *pRange = (CHARRANGE *)lParam;
1509 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1510 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1513 case EM_SETUNDOLIMIT:
1515 if ((int)wParam < 0)
1516 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1518 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1519 /* Setting a max stack size keeps wine from getting killed
1520 for hogging memory. Windows allocates all this memory at once, so
1521 no program would realistically set a value above our maxiumum. */
1522 return editor->nUndoLimit;
1525 return editor->pUndoStack != NULL;
1527 return editor->pRedoStack != NULL;
1528 case WM_UNDO: /* FIXME: actually not the same */
1537 /* these flags are equivalent to the ES_* counterparts */
1538 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1539 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1540 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1546 /* these flags are equivalent to ES_* counterparts
1547 * ECO_READONLY is already implemented in the code, only requires
1548 * setting the bit to work
1550 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1551 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1552 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1553 DWORD settings = mask & raw;
1569 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1571 if (lParam & ECO_AUTOWORDSELECTION)
1572 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1573 if (lParam & ECO_SELECTIONBAR)
1574 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1575 if (lParam & ECO_VERTICAL)
1576 FIXME("ECO_VERTICAL not implemented yet!\n");
1577 if (lParam & ECO_AUTOHSCROLL)
1578 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1579 if (lParam & ECO_AUTOVSCROLL)
1580 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1581 if (lParam & ECO_NOHIDESEL)
1582 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1583 if (lParam & ECO_WANTRETURN)
1584 FIXME("ECO_WANTRETURN not implemented yet!\n");
1590 ME_InvalidateSelection(editor);
1591 ME_SetSelection(editor, wParam, lParam);
1592 ME_InvalidateSelection(editor);
1593 ME_SendSelChange(editor);
1596 case EM_SETSCROLLPOS:
1598 POINT *point = (POINT *)lParam;
1599 ME_ScrollAbs(editor, point->y);
1602 case EM_AUTOURLDETECT:
1604 if (wParam==1 || wParam ==0)
1606 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1609 return E_INVALIDARG;
1611 case EM_GETAUTOURLDETECT:
1613 return editor->AutoURLDetect_bEnable;
1618 CHARRANGE range = *(CHARRANGE *)lParam;
1620 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1622 ME_InvalidateSelection(editor);
1623 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1624 ME_InvalidateSelection(editor);
1625 ME_SendSelChange(editor);
1629 case EM_SHOWSCROLLBAR:
1631 ShowScrollBar(editor->hWnd, wParam, lParam);
1637 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1641 int oldModify = editor->nModifyStep;
1643 if (!pStruct) return 0;
1645 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
1646 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
1647 pStruct->flags, pStruct->codepage);
1649 /* FIXME: make use of pStruct->codepage in the to unicode translation */
1650 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
1651 len = wszText ? lstrlenW(wszText) : 0;
1653 /* FIXME: this should support RTF strings too, according to MSDN */
1654 if (pStruct->flags & ST_SELECTION) {
1655 ME_GetSelection(editor, &from, &to);
1656 style = ME_GetSelectionInsertStyle(editor);
1657 ME_InternalDeleteText(editor, from, to - from);
1658 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1659 ME_ReleaseStyle(style);
1662 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1663 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1666 ME_CommitUndo(editor);
1667 if (!(pStruct->flags & ST_KEEPUNDO))
1669 editor->nModifyStep = oldModify;
1670 ME_EmptyUndoStack(editor);
1672 ME_UpdateRepaint(editor);
1675 case EM_SETBKGNDCOLOR:
1677 LRESULT lColor = ME_GetBackColor(editor);
1678 if (editor->rgbBackColor != -1)
1679 DeleteObject(editor->hbrBackground);
1682 editor->rgbBackColor = -1;
1683 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1687 editor->rgbBackColor = lParam;
1688 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1690 if (editor->bRedraw)
1692 InvalidateRect(hWnd, NULL, TRUE);
1698 return editor->nModifyStep == 0 ? 0 : 1;
1702 editor->nModifyStep = 1;
1704 editor->nModifyStep = 0;
1708 case EM_SETREADONLY:
1710 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1712 nStyle |= ES_READONLY;
1714 nStyle &= ~ES_READONLY;
1715 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1718 case EM_SETEVENTMASK:
1720 DWORD nOldMask = editor->nEventMask;
1722 editor->nEventMask = lParam;
1725 case EM_GETEVENTMASK:
1726 return editor->nEventMask;
1727 case EM_SETCHARFORMAT:
1729 CHARFORMAT2W buf, *p;
1730 BOOL bRepaint = TRUE;
1731 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1732 if (!wParam || (editor->mode & TM_PLAINTEXT))
1733 ME_SetDefaultCharFormat(editor, p);
1734 else if (wParam == (SCF_WORD | SCF_SELECTION))
1735 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1736 else if (wParam == SCF_ALL)
1737 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1740 ME_GetSelection(editor, &from, &to);
1741 bRepaint = (from != to);
1742 ME_SetSelectionCharFormat(editor, p);
1744 editor->nModifyStep = 1;
1745 ME_CommitUndo(editor);
1747 ME_RewrapRepaint(editor);
1750 case EM_GETCHARFORMAT:
1752 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1753 if (dst->cbSize != sizeof(CHARFORMATA) &&
1754 dst->cbSize != sizeof(CHARFORMATW) &&
1755 dst->cbSize != sizeof(CHARFORMAT2A) &&
1756 dst->cbSize != sizeof(CHARFORMAT2W))
1758 tmp.cbSize = sizeof(tmp);
1760 ME_GetDefaultCharFormat(editor, &tmp);
1762 ME_GetSelectionCharFormat(editor, &tmp);
1763 ME_CopyToCFAny(dst, &tmp);
1766 case EM_SETPARAFORMAT:
1767 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1768 ME_RewrapRepaint(editor);
1769 ME_CommitUndo(editor);
1771 case EM_GETPARAFORMAT:
1772 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1774 case EM_GETFIRSTVISIBLELINE:
1776 ME_DisplayItem *p = editor->pBuffer->pFirst;
1777 int y = ME_GetYScrollPos(editor);
1782 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1783 if (p->type == diTextEnd)
1785 if (p->type == diParagraph) {
1786 ypara = p->member.para.nYPos;
1789 ystart = ypara + p->member.row.nYPos;
1790 yend = ystart + p->member.row.nHeight;
1798 case EM_HIDESELECTION:
1800 editor->bHideSelection = (wParam != 0);
1801 ME_InvalidateSelection(editor);
1806 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1807 return TRUE; /* Should return false if a single line richedit control */
1812 ME_GetSelection(editor, &from, &to);
1813 ME_InternalDeleteText(editor, from, to-from);
1814 ME_CommitUndo(editor);
1815 ME_UpdateRepaint(editor);
1822 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1823 size_t len = lstrlenW(wszText);
1824 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1826 ME_GetSelection(editor, &from, &to);
1827 style = ME_GetSelectionInsertStyle(editor);
1828 ME_InternalDeleteText(editor, from, to-from);
1829 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1830 ME_ReleaseStyle(style);
1831 /* drop temporary style if line end */
1833 * FIXME question: does abc\n mean: put abc,
1834 * clear temp style, put \n? (would require a change)
1836 if (len>0 && wszText[len-1] == '\n')
1837 ME_ClearTempStyle(editor);
1838 ME_EndToUnicode(unicode, wszText);
1839 ME_CommitUndo(editor);
1841 ME_EmptyUndoStack(editor);
1842 ME_UpdateRepaint(editor);
1845 case EM_SCROLLCARET:
1847 int top, bottom; /* row's edges relative to document top */
1849 ME_DisplayItem *para, *row;
1851 nPos = ME_GetYScrollPos(editor);
1852 row = ME_RowStart(editor->pCursors[0].pRun);
1853 para = ME_GetParagraph(row);
1854 top = para->member.para.nYPos + row->member.row.nYPos;
1855 bottom = top + row->member.row.nHeight;
1857 if (top < nPos) /* caret above window */
1858 ME_ScrollAbs(editor, top);
1859 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1860 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1868 BOOL bRepaint = LOWORD(lParam);
1871 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1872 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1874 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1875 ReleaseDC(hWnd, hDC);
1876 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1877 ME_SetDefaultCharFormat(editor, &fmt);
1879 ME_CommitUndo(editor);
1881 ME_RewrapRepaint(editor);
1886 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1889 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1890 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
1892 /* Undocumented: WM_SETTEXT supports RTF text */
1893 ME_StreamInRTFString(editor, 0, (char *)lParam);
1897 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1898 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1899 if (lstrlenW(wszText) > 0)
1901 /* uses default style! */
1902 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1904 ME_EndToUnicode(unicode, wszText);
1908 TRACE("WM_SETTEXT - NULL\n");
1909 ME_CommitUndo(editor);
1910 ME_EmptyUndoStack(editor);
1911 ME_SetSelection(editor, 0, 0);
1912 ME_UpdateRepaint(editor);
1917 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1918 if (IsClipboardFormatAvailable(nRTFFormat))
1920 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1928 ME_GlobalDestStruct gds;
1929 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1932 if (IsClipboardFormatAvailable(nRTFFormat))
1933 cf = nRTFFormat, dwFormat = SF_RTF;
1934 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1935 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1939 if (!OpenClipboard(hWnd))
1941 gds.hData = GetClipboardData(cf);
1943 es.dwCookie = (DWORD)&gds;
1944 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1945 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1953 LPDATAOBJECT dataObj = NULL;
1957 if (editor->cPasswordMask)
1958 return 0; /* Copying or Cutting masked text isn't allowed */
1960 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1961 if(editor->lpOleCallback)
1962 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1963 if(FAILED(hr) || !dataObj)
1964 hr = ME_GetDataObject(editor, &range, &dataObj);
1966 hr = OleSetClipboard(dataObj);
1967 IDataObject_Release(dataObj);
1969 if (SUCCEEDED(hr) && msg == WM_CUT)
1971 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1972 ME_CommitUndo(editor);
1973 ME_UpdateRepaint(editor);
1977 case WM_GETTEXTLENGTH:
1978 return ME_GetTextLength(editor);
1979 case EM_GETTEXTLENGTHEX:
1980 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1983 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1985 tr.chrg.cpMax = wParam ? (wParam - 1) : 0;
1986 tr.lpstrText = (WCHAR *)lParam;
1987 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
1991 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1994 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1995 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1997 if (ex->flags & GT_SELECTION)
1999 ME_GetSelection(editor, &nStart, &nCount);
2001 nCount = min(nCount, ex->cb - 1);
2006 nCount = ex->cb - 1;
2008 if (ex->codepage == 1200)
2010 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2011 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2015 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2016 we can just take a bigger buffer? :) */
2017 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2018 LPWSTR buffer = richedit_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2019 DWORD buflen = ex->cb;
2023 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2024 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2025 if (rc) rc--; /* do not count 0 terminator */
2027 richedit_free(buffer);
2034 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2035 ME_GetSelection(editor, &from, &to);
2036 tr.chrg.cpMin = from;
2038 tr.lpstrText = (WCHAR *)lParam;
2039 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2041 case EM_GETSCROLLPOS:
2043 POINT *point = (POINT *)lParam;
2044 point->x = 0; /* FIXME side scrolling not implemented */
2045 point->y = ME_GetYScrollPos(editor);
2048 case EM_GETTEXTRANGE:
2050 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2051 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2052 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2053 editor->bEmulateVersion10, ME_GetTextLength(editor));
2055 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2058 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2059 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2060 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2061 /* FIXME this is a potential security hole (buffer overrun)
2062 if you know more about wchar->mbyte conversion please explain
2064 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2071 ME_DisplayItem *run;
2072 const unsigned int nMaxChars = *(WORD *) lParam;
2073 unsigned int nEndChars, nCharsLeft = nMaxChars;
2074 char *dest = (char *) lParam;
2075 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2076 we need to know how if we have the extra \n or not */
2077 int nLF = editor->bEmulateVersion10;
2079 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2080 unicode ? "Unicode" : "Ansi");
2082 run = ME_FindRowWithNumber(editor, wParam);
2086 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2087 && !(run->member.run.nFlags & MERF_ENDPARA))
2091 if (run->type != diRun)
2093 strText = run->member.run.strText;
2094 nCopy = min(nCharsLeft, strText->nLen);
2097 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2099 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2100 nCharsLeft, NULL, NULL);
2101 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2102 nCharsLeft -= nCopy;
2105 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2106 nEndChars = min(nCharsLeft, 2 + nLF);
2107 nCharsLeft -= nEndChars;
2110 const WCHAR src[] = {'\r', '\0'};
2111 const WCHAR src10[] = {'\r', '\n', '\0'};
2112 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2115 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2117 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2119 if (nEndChars == 2 + nLF)
2120 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2122 return nMaxChars - nCharsLeft;
2124 case EM_GETLINECOUNT:
2126 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2129 while (item != editor->pBuffer->pLast)
2131 assert(item->type == diParagraph);
2132 nRows += item->member.para.nRows;
2133 item = item->member.para.next_para;
2135 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2136 return max(1, nRows);
2138 case EM_LINEFROMCHAR:
2141 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2143 return ME_RowNumberFromCharOfs(editor, wParam);
2145 case EM_EXLINEFROMCHAR:
2148 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2150 return ME_RowNumberFromCharOfs(editor, lParam);
2154 ME_DisplayItem *item, *para;
2158 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2160 item = ME_FindRowWithNumber(editor, wParam);
2163 para = ME_GetParagraph(item);
2164 item = ME_FindItemFwd(item, diRun);
2165 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2166 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2171 ME_DisplayItem *item, *item_end;
2172 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2174 if (wParam > ME_GetTextLength(editor))
2178 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2181 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2182 item = ME_RowStart(item);
2183 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2184 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2185 if (item_end->type == diStartRow)
2186 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2188 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2189 - (editor->bEmulateVersion10?2:1);
2190 nChars = nNextLineOfs - nThisLineOfs;
2191 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2194 case EM_EXLIMITTEXT:
2196 if ((int)lParam < 0)
2199 editor->nTextLimit = 65536;
2201 editor->nTextLimit = (int) lParam;
2207 editor->nTextLimit = 65536;
2209 editor->nTextLimit = (int) wParam;
2212 case EM_GETLIMITTEXT:
2214 return editor->nTextLimit;
2218 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2219 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2223 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2224 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2225 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2231 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2232 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2236 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2237 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2238 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2244 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2245 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2247 case EM_FINDTEXTEXW:
2249 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2250 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2253 if (!wParam || !lParam)
2255 *(int *)wParam = editor->nZoomNumerator;
2256 *(int *)lParam = editor->nZoomDenominator;
2259 return ME_SetZoom(editor, wParam, lParam);
2260 case EM_CHARFROMPOS:
2261 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2262 case EM_POSFROMCHAR:
2264 ME_DisplayItem *pRun;
2265 int nCharOfs, nOffset, nLength;
2269 /* detect which API version we're dealing with */
2270 if (wParam >= 0x40000)
2272 nLength = ME_GetTextLength(editor);
2274 if (nCharOfs < nLength) {
2275 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2276 assert(pRun->type == diRun);
2277 pt.y = pRun->member.run.pt.y;
2278 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2279 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2282 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2284 if (wParam >= 0x40000) {
2285 *(POINTL *)wParam = pt;
2287 return MAKELONG( pt.x, pt.y );
2290 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2291 { /* Squelch the default horizontal scrollbar it would make */
2292 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2294 ME_CommitUndo(editor);
2295 ME_WrapMarkedParagraphs(editor);
2296 ME_MoveCaret(editor);
2299 ME_DestroyEditor(editor);
2300 SetWindowLongPtrW(hWnd, 0, 0);
2302 case WM_LBUTTONDOWN:
2304 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2306 ME_LinkNotify(editor,msg,wParam,lParam);
2309 if (GetCapture() == hWnd)
2310 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2311 ME_LinkNotify(editor,msg,wParam,lParam);
2314 if (GetCapture() == hWnd)
2316 ME_LinkNotify(editor,msg,wParam,lParam);
2318 case WM_LBUTTONDBLCLK:
2319 ME_LinkNotify(editor,msg,wParam,lParam);
2320 ME_SelectWord(editor);
2322 case WM_CONTEXTMENU:
2323 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2327 if (editor->bRedraw)
2332 hDC = BeginPaint(hWnd, &ps);
2333 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2334 EndPaint(hWnd, &ps);
2338 ME_ShowCaret(editor);
2339 ME_SendOldNotify(editor, EN_SETFOCUS);
2342 ME_HideCaret(editor);
2343 ME_SendOldNotify(editor, EN_KILLFOCUS);
2347 if (editor->bRedraw)
2349 HDC hDC = (HDC)wParam;
2351 if (GetUpdateRect(hWnd,&rc,TRUE))
2353 FillRect(hDC, &rc, editor->hbrBackground);
2359 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2362 if (ME_KeyDown(editor, LOWORD(wParam)))
2370 wstr = (WCHAR)wParam;
2373 CHAR charA = wParam;
2374 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2376 if (editor->AutoURLDetect_bEnable)
2377 ME_AutoURLDetect(editor, wstr);
2381 case 1: /* Ctrl-A */
2382 ME_SetSelection(editor, 0, -1);
2384 case 3: /* Ctrl-C */
2385 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2389 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2390 MessageBeep(MB_ICONERROR);
2391 return 0; /* FIXME really 0 ? */
2396 case 22: /* Ctrl-V */
2397 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2399 case 24: /* Ctrl-X */
2400 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2402 case 25: /* Ctrl-Y */
2403 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2405 case 26: /* Ctrl-Z */
2406 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2409 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2410 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2411 ME_Style *style = ME_GetInsertStyle(editor, 0);
2412 ME_SaveTempStyle(editor);
2413 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2414 ME_ReleaseStyle(style);
2415 ME_CommitUndo(editor);
2416 ME_UpdateRepaint(editor);
2420 case EM_SCROLL: /* fall through */
2426 origNPos = ME_GetYScrollPos(editor);
2429 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2430 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2431 if (lineHeight <= 0) lineHeight = 24;
2433 switch(LOWORD(wParam))
2436 ME_ScrollUp(editor,lineHeight);
2439 ME_ScrollDown(editor,lineHeight);
2442 ME_ScrollUp(editor,editor->sizeWindow.cy);
2445 ME_ScrollDown(editor,editor->sizeWindow.cy);
2448 case SB_THUMBPOSITION:
2449 ME_ScrollAbs(editor,HIWORD(wParam));
2452 if (msg == EM_SCROLL)
2453 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2459 UINT pulScrollLines;
2461 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2462 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2464 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2466 /* FIXME follow the original */
2467 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2473 *((RECT *)lParam) = editor->rcFormat;
2481 RECT *rc = (RECT *)lParam;
2485 editor->rcFormat.left += rc->left;
2486 editor->rcFormat.top += rc->top;
2487 editor->rcFormat.right += rc->right;
2488 editor->rcFormat.bottom += rc->bottom;
2492 editor->rcFormat = *rc;
2497 GetClientRect(hWnd, &editor->rcFormat);
2499 if (msg != EM_SETRECTNP)
2500 ME_RewrapRepaint(editor);
2503 case EM_REQUESTRESIZE:
2504 ME_SendRequestResize(editor, TRUE);
2507 editor->bRedraw = wParam;
2511 GetClientRect(hWnd, &editor->rcFormat);
2512 ME_RewrapRepaint(editor);
2513 return DefWindowProcW(hWnd, msg, wParam, lParam);
2515 case EM_GETOLEINTERFACE:
2517 LPVOID *ppvObj = (LPVOID*) lParam;
2518 return CreateIRichEditOle(editor, ppvObj);
2520 case EM_GETPASSWORDCHAR:
2522 return editor->cPasswordMask;
2524 case EM_SETOLECALLBACK:
2525 if(editor->lpOleCallback)
2526 IUnknown_Release(editor->lpOleCallback);
2527 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2528 if(editor->lpOleCallback)
2529 IUnknown_AddRef(editor->lpOleCallback);
2531 case EM_GETWORDBREAKPROC:
2532 return (LRESULT)editor->pfnWordBreak;
2533 case EM_SETWORDBREAKPROC:
2535 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2537 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2538 return (LRESULT)pfnOld;
2540 case EM_SETTEXTMODE:
2545 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
2548 /*Check for valid wParam*/
2549 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2550 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2551 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2555 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2557 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2558 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2560 /*FIXME: Currently no support for undo level and code page options*/
2561 editor->mode = (editor->mode & (~mask)) | changes;
2567 case EM_SETPASSWORDCHAR:
2569 editor->cPasswordMask = wParam;
2570 ME_RewrapRepaint(editor);
2575 return DefWindowProcW(hWnd, msg, wParam, lParam);
2580 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2582 BOOL unicode = TRUE;
2584 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
2585 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
2588 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
2591 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2593 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
2596 /******************************************************************
2597 * RichEditANSIWndProc (RICHED20.10)
2599 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2601 return RichEditWndProcA(hWnd, msg, wParam, lParam);
2604 /******************************************************************
2605 * RichEdit10ANSIWndProc (RICHED20.9)
2607 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2611 /* FIXME: this is NOT the same as 2.0 version */
2612 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2613 if (msg == WM_NCCREATE)
2615 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2617 editor->bEmulateVersion10 = TRUE;
2618 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2623 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2625 HWND hWnd = editor->hWnd;
2626 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2629 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2632 ME_Cursor tmpCursor;
2633 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2638 x = (short)LOWORD(lParam);
2639 y = (short)HIWORD(lParam);
2640 nCharOfs = ME_CharFromPos(editor, x, y);
2641 if (nCharOfs < 0) return;
2643 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2644 tmpRun = &tmpCursor.pRun->member.run;
2646 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2647 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2648 { /* The clicked run has CFE_LINK set */
2649 info.nmhdr.hwndFrom = editor->hWnd;
2650 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2651 info.nmhdr.code = EN_LINK;
2653 info.wParam = wParam;
2654 info.lParam = lParam;
2655 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2656 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2657 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2661 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2663 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2666 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2667 item = item->member.para.next_para;
2670 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2671 item = item->member.para.next_para;
2678 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2680 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2682 WCHAR *pStart = buffer;
2691 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2694 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2701 item = ME_FindItemFwd(item, diRun);
2704 while(nChars && item)
2706 int nLen = ME_StrLen(item->member.run.strText);
2710 if (item->member.run.nFlags & MERF_ENDPARA)
2712 if (!ME_FindItemFwd(item, diRun))
2713 /* No '\r' is appended to the last paragraph. */
2723 /* our end paragraph consists of 2 characters now */
2724 if (editor->bEmulateVersion10)
2729 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2736 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2740 item = ME_FindItemFwd(item, diRun);
2743 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2747 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
2752 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2753 wcW.lpfnWndProc = RichEditWndProcW;
2755 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2756 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2758 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2759 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2760 wcW.lpszMenuName = NULL;
2762 if (is_version_nt())
2764 wcW.lpszClassName = RichEdit20W;
2765 if (!RegisterClassW(&wcW)) return FALSE;
2766 wcW.lpszClassName = RichEdit50W;
2767 if (!RegisterClassW(&wcW)) return FALSE;
2771 /* WNDCLASSA/W have the same layout */
2772 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
2773 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2774 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
2775 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2778 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2779 wcA.lpfnWndProc = RichEditWndProcA;
2781 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2782 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2784 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2785 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2786 wcA.lpszMenuName = NULL;
2787 wcA.lpszClassName = "RichEdit20A";
2788 if (!RegisterClassA(&wcA)) return FALSE;
2789 wcA.lpszClassName = "RichEdit50A";
2790 if (!RegisterClassA(&wcA)) return FALSE;
2795 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2796 /* FIXME: Not implemented */
2797 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
2798 hWnd, msg, get_msg_name(msg), wParam, lParam);
2799 return DefWindowProcW(hWnd, msg, wParam, lParam);
2802 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2803 /* FIXME: Not implemented */
2804 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
2805 hWnd, msg, get_msg_name(msg), wParam, lParam);
2806 return DefWindowProcW(hWnd, msg, wParam, lParam);
2809 /******************************************************************
2810 * REExtendedRegisterClass (RICHED20.8)
2812 * FIXME undocumented
2813 * Need to check for errors and implement controls and callbacks
2815 LRESULT WINAPI REExtendedRegisterClass(void)
2820 FIXME("semi stub\n");
2824 wcW.hInstance = NULL;
2827 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2828 wcW.lpszMenuName = NULL;
2830 if (!ME_ListBoxRegistered)
2832 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2833 wcW.lpfnWndProc = REListWndProc;
2834 wcW.lpszClassName = REListBox20W;
2835 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2838 if (!ME_ComboBoxRegistered)
2840 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2841 wcW.lpfnWndProc = REComboWndProc;
2842 wcW.lpszClassName = REComboBox20W;
2843 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2847 if (ME_ListBoxRegistered)
2849 if (ME_ComboBoxRegistered)
2855 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2875 int curf_ef, link_ef, def_ef;
2876 int cur_prefx, prefx_cnt;
2877 int sel_min, sel_max;
2880 int URLmin, URLmax = 0;
2883 CHARFORMAT2W cur_format;
2884 CHARFORMAT2W default_format;
2886 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2887 sel_min = ins_pt.cpMin;
2888 sel_max = ins_pt.cpMax;
2889 if (sel_min==sel_max)
2891 if (sel_min!=sel_max)
2892 car_pos = ME_GetTextLength(editor)+1;
2893 cur_format.cbSize = sizeof(cur_format);
2894 default_format.cbSize = sizeof(default_format);
2895 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2896 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2897 link.cbSize = sizeof(link);
2898 link.dwMask = CFM_LINK;
2899 link.dwEffects = CFE_LINK;
2900 curf_ef = cur_format.dwEffects & link.dwEffects;
2901 def_ef = default_format.dwEffects & link.dwEffects;
2902 link_ef = link.dwEffects & link.dwEffects;
2903 if (curf_ef == link_ef)
2905 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2907 ME_SetSelection(editor, car_pos, car_pos);
2908 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2913 if (curf_ef == def_ef)
2916 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2917 while (cur_prefx<=prefx_cnt)
2921 ft.lpstrText = prefixes[cur_prefx].text;
2922 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2923 URLmax=max(0, car_pos);
2924 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2926 URLmax = ME_GetTextLength(editor)+1;
2928 ft.chrg.cpMin = URLmin;
2929 ft.chrg.cpMax = URLmax;
2930 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2936 url.cpMax=car_pos-1;
2937 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2938 ME_RewrapRepaint(editor);