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("%08x %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);
381 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
387 if (info->rtfParam != rtfNoParam)
389 RTFFont *f = RTFGetFont(info, info->rtfParam);
392 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
393 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
394 fmt.bCharSet = f->rtfFCharSet;
395 fmt.dwMask = CFM_FACE | CFM_CHARSET;
400 fmt.dwMask = CFM_SIZE;
401 if (info->rtfParam != rtfNoParam)
402 fmt.yHeight = info->rtfParam*10;
407 RTFFlushOutputBuffer(info);
408 /* FIXME too slow ? how come ? */
409 style2 = ME_ApplyStyle(info->style, &fmt);
410 ME_ReleaseStyle(info->style);
411 info->style = style2;
412 info->styleChanged = TRUE;
416 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
417 the same tags mean different things in different contexts */
418 static void ME_RTFParAttrHook(RTF_Info *info)
421 fmt.cbSize = sizeof(fmt);
424 switch(info->rtfMinor)
426 case rtfParDef: /* restores default paragraph attributes */
427 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
428 fmt.wAlignment = PFA_LEFT;
430 fmt.dxOffset = fmt.dxStartIndent = 0;
431 RTFFlushOutputBuffer(info);
432 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
436 ME_DisplayItem *para;
438 RTFFlushOutputBuffer(info);
439 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
440 assert(para->member.para.pCells);
441 para->member.para.bTable = TRUE;
445 ME_GetSelectionParaFormat(info->editor, &fmt);
446 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
447 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
448 fmt.dxOffset = -info->rtfParam;
451 ME_GetSelectionParaFormat(info->editor, &fmt);
452 fmt.dwMask = PFM_STARTINDENT;
453 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
456 fmt.dwMask = PFM_RIGHTINDENT;
457 fmt.dxRightIndent = info->rtfParam;
461 fmt.dwMask = PFM_ALIGNMENT;
462 fmt.wAlignment = PFA_LEFT;
465 fmt.dwMask = PFM_ALIGNMENT;
466 fmt.wAlignment = PFA_RIGHT;
469 fmt.dwMask = PFM_ALIGNMENT;
470 fmt.wAlignment = PFA_CENTER;
473 ME_GetSelectionParaFormat(info->editor, &fmt);
474 if (!(fmt.dwMask & PFM_TABSTOPS))
476 fmt.dwMask |= PFM_TABSTOPS;
479 if (fmt.cTabCount < MAX_TAB_STOPS)
480 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
484 RTFFlushOutputBuffer(info);
485 /* FIXME too slow ? how come ?*/
486 ME_SetSelectionParaFormat(info->editor, &fmt);
490 static void ME_RTFTblAttrHook(RTF_Info *info)
492 ME_DisplayItem *para;
494 switch (info->rtfMinor)
497 RTFFlushOutputBuffer(info);
498 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
500 /* Release possibly inherited cell definitions */
501 ME_DestroyTableCellList(para);
503 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
504 para->member.para.pCells->nRightBoundary = 0;
505 para->member.para.pCells->next = NULL;
506 para->member.para.pLastCell = para->member.para.pCells;
509 RTFFlushOutputBuffer(info);
510 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
512 if (para->member.para.pLastCell->nRightBoundary)
514 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
517 para->member.para.pLastCell->next = pCell;
518 para->member.para.pLastCell = pCell;
520 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
525 static void ME_RTFReadHook(RTF_Info *info) {
526 switch(info->rtfClass)
529 switch(info->rtfMajor)
532 if (info->stackTop < maxStack) {
533 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
534 info->stack[info->stackTop].codePage = info->codePage;
535 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
538 info->styleChanged = FALSE;
543 RTFFlushOutputBuffer(info);
544 if (info->stackTop<=1) {
545 info->rtfClass = rtfEOF;
549 assert(info->stackTop >= 0);
550 if (info->styleChanged)
552 /* FIXME too slow ? how come ? */
553 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
554 ME_ReleaseStyle(info->style);
556 info->codePage = info->stack[info->stackTop].codePage;
557 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
564 switch(info->rtfMajor)
567 ME_RTFCharAttrHook(info);
570 ME_RTFParAttrHook(info);
573 ME_RTFTblAttrHook(info);
576 if (info->rtfMinor == rtfCell)
578 RTFFlushOutputBuffer(info);
579 ME_InsertTableCellFromCursor(info->editor, 0);
587 ME_StreamInFill(ME_InStream *stream)
589 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
590 (BYTE *)stream->buffer,
591 sizeof(stream->buffer),
592 (LONG *)&stream->dwSize);
596 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
601 int nEventMask = editor->nEventMask;
602 ME_InStream inStream;
604 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
605 editor->nEventMask = 0;
607 ME_GetSelection(editor, &from, &to);
608 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
609 style = ME_GetSelectionInsertStyle(editor);
611 ME_InternalDeleteText(editor, from, to-from);
614 style = editor->pBuffer->pDefaultStyle;
615 ME_AddRefStyle(style);
616 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
617 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
619 ME_ClearTempStyle(editor);
620 /* FIXME restore default paragraph formatting ! */
623 inStream.editstream = stream;
624 inStream.editstream->dwError = 0;
630 /* Check if it's really RTF, and if it is not, use plain text */
631 ME_StreamInFill(&inStream);
632 if (!inStream.editstream->dwError)
634 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
642 if (!inStream.editstream->dwError)
644 if (format & SF_RTF) {
645 /* setup the RTF parser */
646 memset(&parser, 0, sizeof parser);
647 RTFSetEditStream(&parser, &inStream);
648 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
649 parser.hwndEdit = editor->hWnd;
650 parser.editor = editor;
651 parser.style = style;
654 RTFSetReadHook(&parser, ME_RTFReadHook);
659 RTFFlushOutputBuffer(&parser);
662 style = parser.style;
664 else if (format & SF_TEXT)
665 ME_StreamInText(editor, format, &inStream, style);
667 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
668 ME_GetSelection(editor, &to, &to2);
669 /* put the cursor at the top */
670 if (!(format & SFF_SELECTION))
671 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
674 if (format & SFF_SELECTION)
676 /* even if we didn't add an undo, we need to commit the ones added earlier */
677 ME_CommitUndo(editor);
681 ME_EmptyUndoStack(editor);
684 ME_ReleaseStyle(style);
685 editor->nEventMask = nEventMask;
688 ME_UpdateRepaint(editor);
690 if (!(format & SFF_SELECTION)) {
691 ME_ClearTempStyle(editor);
693 ME_MoveCaret(editor);
694 ME_SendSelChange(editor);
695 ME_SendRequestResize(editor, FALSE);
701 typedef struct tagME_RTFStringStreamStruct
706 } ME_RTFStringStreamStruct;
708 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
710 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
713 count = min(cb, pStruct->length - pStruct->pos);
714 memmove(lpBuff, pStruct->string + pStruct->pos, count);
715 pStruct->pos += count;
721 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
724 ME_RTFStringStreamStruct data;
726 data.string = string;
727 data.length = strlen(string);
729 es.dwCookie = (DWORD)&data;
730 es.pfnCallback = ME_ReadFromRTFString;
731 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
736 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
738 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
740 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
741 item = ME_FindItemFwd(item, diParagraph);
746 nOffset -= item->member.para.nCharOfs;
747 if (nItemType == diParagraph) {
749 *nItemOffset = nOffset;
754 item = ME_FindItemFwd(item, diRun);
755 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
757 nOffset -= item->member.run.nCharOfs;
759 *nItemOffset = nOffset;
766 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
768 const int nLen = lstrlenW(text);
769 const int nTextLen = ME_GetTextLength(editor);
772 ME_DisplayItem *item;
773 ME_DisplayItem *para;
774 WCHAR wLastChar = ' ';
776 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
777 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
779 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
780 FIXME("Flags 0x%08x not implemented\n",
781 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
784 if (chrg->cpMax == -1)
787 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
789 /* when searching up, if cpMin < cpMax, then instead of searching
790 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
791 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
792 * case, it is always bigger than cpMin.
794 if (!(flags & FR_DOWN))
798 nMax = nMin > nTextLen ? nTextLen : nMin;
799 if (nMin < nSwap || chrg->cpMax == -1)
805 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
808 chrgText->cpMin = chrgText->cpMax = -1;
812 if (flags & FR_DOWN) /* Forward search */
814 /* If possible, find the character before where the search starts */
815 if ((flags & FR_WHOLEWORD) && nMin)
818 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
822 chrgText->cpMin = chrgText->cpMax = -1;
825 wLastChar = item->member.run.strText->szData[nStart];
829 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
833 chrgText->cpMin = chrgText->cpMax = -1;
837 para = ME_GetParagraph(item);
839 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
841 ME_DisplayItem *pCurItem = item;
842 int nCurStart = nStart;
845 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
847 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
851 if (nMatched == nLen)
853 ME_DisplayItem *pNextItem = pCurItem;
854 int nNextStart = nCurStart;
857 /* Check to see if next character is a whitespace */
858 if (flags & FR_WHOLEWORD)
860 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
862 pNextItem = ME_FindItemFwd(pCurItem, diRun);
863 nNextStart = -nMatched;
867 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
871 if (isalnumW(wNextChar))
875 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
878 chrgText->cpMin = nStart;
879 chrgText->cpMax = nStart + nLen;
881 TRACE("found at %d-%d\n", nStart, nStart + nLen);
884 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
886 pCurItem = ME_FindItemFwd(pCurItem, diRun);
887 para = ME_GetParagraph(pCurItem);
888 nCurStart = -nMatched;
892 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
897 if (nStart == ME_StrLen(item->member.run.strText))
899 item = ME_FindItemFwd(item, diRun);
900 para = ME_GetParagraph(item);
905 else /* Backward search */
907 /* If possible, find the character after where the search ends */
908 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
911 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
915 chrgText->cpMin = chrgText->cpMax = -1;
918 wLastChar = item->member.run.strText->szData[nEnd];
922 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
926 chrgText->cpMin = chrgText->cpMax = -1;
930 para = ME_GetParagraph(item);
933 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
935 ME_DisplayItem *pCurItem = item;
939 if (nCurEnd - nMatched == 0)
941 pCurItem = ME_FindItemBack(pCurItem, diRun);
942 para = ME_GetParagraph(pCurItem);
943 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
946 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
948 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
952 if (nMatched == nLen)
954 ME_DisplayItem *pPrevItem = pCurItem;
955 int nPrevEnd = nCurEnd;
958 /* Check to see if previous character is a whitespace */
959 if (flags & FR_WHOLEWORD)
961 if (nPrevEnd - nMatched == 0)
963 pPrevItem = ME_FindItemBack(pCurItem, diRun);
965 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
969 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
973 if (isalnumW(wPrevChar))
977 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
980 chrgText->cpMin = nStart;
981 chrgText->cpMax = nStart + nLen;
983 TRACE("found at %d-%d\n", nStart, nStart + nLen);
986 if (nCurEnd - nMatched == 0)
988 pCurItem = ME_FindItemBack(pCurItem, diRun);
989 /* Don't care about pCurItem becoming NULL here; it's already taken
990 * care of in the exterior loop condition */
991 para = ME_GetParagraph(pCurItem);
992 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
996 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1003 item = ME_FindItemBack(item, diRun);
1004 para = ME_GetParagraph(item);
1005 nEnd = ME_StrLen(item->member.run.strText);
1009 TRACE("not found\n");
1011 chrgText->cpMin = chrgText->cpMax = -1;
1017 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1019 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1020 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1032 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1036 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1037 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1039 if (ME_IsSelection(editor))
1040 ME_DeleteSelection(editor);
1041 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1042 ME_DeleteTextAtCursor(editor, 1, 1);
1045 ME_CommitUndo(editor);
1046 ME_UpdateRepaint(editor);
1047 ME_SendRequestResize(editor, FALSE);
1057 chf.cbSize = sizeof(chf);
1059 ME_GetSelectionCharFormat(editor, &chf);
1060 ME_DumpStyleToBuf(&chf, buf);
1061 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1065 ME_CheckCharOffsets(editor);
1072 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1077 if(!editor->lpOleCallback)
1079 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1080 if(selrange.cpMin == selrange.cpMax)
1081 seltype |= SEL_EMPTY;
1084 /* FIXME: Handle objects */
1085 seltype |= SEL_TEXT;
1086 if(selrange.cpMax-selrange.cpMin > 1)
1087 seltype |= SEL_MULTICHAR;
1089 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1091 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1097 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1098 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1102 ed->bEmulateVersion10 = FALSE;
1103 ed->pBuffer = ME_MakeText();
1105 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1106 ReleaseDC(hWnd, hDC);
1107 ed->bCaretShown = FALSE;
1109 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1110 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1111 ed->pCursors[0].nOffset = 0;
1112 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1113 ed->pCursors[1].nOffset = 0;
1114 ed->nLastTotalLength = ed->nTotalLength = 0;
1117 ed->rgbBackColor = -1;
1118 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1119 ed->bCaretAtEnd = FALSE;
1121 ed->nModifyStep = 0;
1122 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1123 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1124 ed->nUndoStackSize = 0;
1125 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1126 ed->nUndoMode = umAddToUndo;
1127 ed->nParagraphs = 1;
1128 ed->nLastSelStart = ed->nLastSelEnd = 0;
1129 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1130 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1132 ed->bHideSelection = FALSE;
1133 ed->nInvalidOfs = -1;
1134 ed->pfnWordBreak = NULL;
1135 ed->lpOleCallback = NULL;
1136 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1137 ed->AutoURLDetect_bEnable = FALSE;
1138 GetClientRect(hWnd, &ed->rcFormat);
1139 for (i=0; i<HFONT_CACHE_SIZE; i++)
1141 ed->pFontCache[i].nRefs = 0;
1142 ed->pFontCache[i].nAge = 0;
1143 ed->pFontCache[i].hFont = NULL;
1146 ME_CheckCharOffsets(ed);
1148 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1149 ed->cPasswordMask = '*';
1151 ed->cPasswordMask = 0;
1156 typedef struct tagME_GlobalDestStruct
1160 } ME_GlobalDestStruct;
1162 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1164 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1169 pDest = (WORD *)lpBuff;
1170 pSrc = (WORD *)GlobalLock(pData->hData);
1171 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1172 pDest[i] = pSrc[pData->nLength+i];
1174 pData->nLength += i;
1176 GlobalUnlock(pData->hData);
1180 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1182 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1187 pSrc = (BYTE *)GlobalLock(pData->hData);
1188 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1189 pDest[i] = pSrc[pData->nLength+i];
1191 pData->nLength += i;
1193 GlobalUnlock(pData->hData);
1198 void ME_DestroyEditor(ME_TextEditor *editor)
1200 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1201 ME_DisplayItem *p = pFirst, *pNext = NULL;
1204 ME_ClearTempStyle(editor);
1205 ME_EmptyUndoStack(editor);
1208 ME_DestroyDisplayItem(p);
1211 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1212 for (i=0; i<HFONT_CACHE_SIZE; i++)
1214 if (editor->pFontCache[i].hFont)
1215 DeleteObject(editor->pFontCache[i].hFont);
1217 DeleteObject(editor->hbrBackground);
1218 if(editor->lpOleCallback)
1219 IUnknown_Release(editor->lpOleCallback);
1221 FREE_OBJ(editor->pBuffer);
1222 FREE_OBJ(editor->pCursors);
1227 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1228 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1229 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1230 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1232 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1237 case DLL_PROCESS_ATTACH:
1238 DisableThreadLibraryCalls(hinstDLL);
1239 me_heap = HeapCreate (0, 0x10000, 0);
1240 ME_RegisterEditorClass(hinstDLL);
1244 case DLL_PROCESS_DETACH:
1245 UnregisterClassW(wszClassName, 0);
1246 UnregisterClassW(wszClassName50, 0);
1247 UnregisterClassA("RichEdit20A", 0);
1248 UnregisterClassA("RichEdit50A", 0);
1249 if (ME_ListBoxRegistered)
1250 UnregisterClassW(wszClassNameListBox, 0);
1251 if (ME_ComboBoxRegistered)
1252 UnregisterClassW(wszClassNameComboBox, 0);
1254 HeapDestroy (me_heap);
1262 #define UNSUPPORTED_MSG(e) \
1264 FIXME(#e ": stub\n"); \
1265 return DefWindowProcW(hWnd, msg, wParam, lParam);
1267 static const char * const edit_messages[] = {
1296 "EM_SETPASSWORDCHAR",
1297 "EM_EMPTYUNDOBUFFER",
1298 "EM_GETFIRSTVISIBLELINE",
1300 "EM_SETWORDBREAKPROC",
1301 "EM_GETWORDBREAKPROC",
1302 "EM_GETPASSWORDCHAR",
1310 static const char * const richedit_messages[] = {
1315 "EM_EXLINEFROMCHAR",
1321 "EM_GETOLEINTERFACE",
1331 "EM_SETOLECALLBACK",
1333 "EM_SETTARGETDEVICE",
1341 "EM_GETWORDBREAKPROCEX",
1342 "EM_SETWORDBREAKPROCEX",
1344 "EM_UNKNOWN_USER_83",
1349 "EM_STOPGROUPTYPING",
1353 "EM_GETAUTOURLDETECT",
1356 "EM_GETTEXTLENGTHEX",
1359 "EM_UNKNOWN_USER_98",
1360 "EM_UNKNOWN_USER_99",
1361 "EM_SETPUNCTUATION",
1362 "EM_GETPUNCTUATION",
1363 "EM_SETWORDWRAPMODE",
1364 "EM_GETWORDWRAPMODE",
1370 "EM_UNKNOWN_USER_109",
1371 "EM_UNKNOWN_USER_110",
1372 "EM_UNKNOWN_USER_111",
1373 "EM_UNKNOWN_USER_112",
1374 "EM_UNKNOWN_USER_113",
1375 "EM_UNKNOWN_USER_114",
1376 "EM_UNKNOWN_USER_115",
1377 "EM_UNKNOWN_USER_116",
1378 "EM_UNKNOWN_USER_117",
1379 "EM_UNKNOWN_USER_118",
1380 "EM_UNKNOWN_USER_119",
1381 "EM_SETLANGOPTIONS",
1382 "EM_GETLANGOPTIONS",
1383 "EM_GETIMECOMPMODE",
1387 "EM_SETIMEMODEBIAS",
1392 get_msg_name(UINT msg)
1394 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1395 return edit_messages[msg - EM_GETSEL];
1396 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1397 return richedit_messages[msg - EM_CANPASTE];
1401 /******************************************************************
1402 * RichEditANSIWndProc (RICHED20.10)
1404 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1405 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1407 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1408 hWnd, msg, get_msg_name(msg), wParam, lParam);
1410 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1411 ERR("RichEditANSIWndProc called with invalid hWnd %p - application bug?\n", hWnd);
1417 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1418 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1419 UNSUPPORTED_MSG(EM_FMTLINES)
1420 UNSUPPORTED_MSG(EM_FORMATRANGE)
1421 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1422 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1423 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1424 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1425 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1426 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1427 UNSUPPORTED_MSG(EM_GETREDONAME)
1428 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1429 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1430 UNSUPPORTED_MSG(EM_GETUNDONAME)
1431 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1432 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1433 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1434 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1435 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1436 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1437 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1438 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1439 UNSUPPORTED_MSG(EM_SETPALETTE)
1440 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1441 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1442 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1443 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1444 UNSUPPORTED_MSG(WM_STYLECHANGING)
1445 UNSUPPORTED_MSG(WM_STYLECHANGED)
1446 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1448 /* Messages specific to Richedit controls */
1451 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1453 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1456 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1457 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1459 int vk = (int)((LPMSG)lParam)->wParam;
1460 /* if style says we want return key */
1461 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1463 code |= DLGC_WANTMESSAGE;
1465 /* we always handle ctrl-tab */
1466 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1468 code |= DLGC_WANTMESSAGE;
1475 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1476 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1477 editor = ME_MakeEditor(hWnd);
1478 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1479 pcs = 0; /* ignore */
1482 case EM_EMPTYUNDOBUFFER:
1483 ME_EmptyUndoStack(editor);
1487 /* Note: wParam/lParam can be NULL */
1489 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1490 PUINT pto = lParam ? (PUINT)lParam : &to;
1491 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1492 if ((*pfrom|*pto) & 0xFFFF0000)
1494 return MAKELONG(*pfrom,*pto);
1498 CHARRANGE *pRange = (CHARRANGE *)lParam;
1499 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1500 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1503 case EM_SETUNDOLIMIT:
1505 if ((int)wParam < 0)
1506 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1508 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1509 /* Setting a max stack size keeps wine from getting killed
1510 for hogging memory. Windows allocates all this memory at once, so
1511 no program would realistically set a value above our maxiumum. */
1512 return editor->nUndoLimit;
1515 return editor->pUndoStack != NULL;
1517 return editor->pRedoStack != NULL;
1518 case WM_UNDO: /* FIXME: actually not the same */
1527 /* these flags are equivalent to the ES_* counterparts */
1528 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1529 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1530 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1536 /* these flags are equivalent to ES_* counterparts
1537 * ECO_READONLY is already implemented in the code, only requires
1538 * setting the bit to work
1540 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1541 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1542 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1543 DWORD settings = mask & raw;
1559 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1561 if (lParam & ECO_AUTOWORDSELECTION)
1562 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1563 if (lParam & ECO_SELECTIONBAR)
1564 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1565 if (lParam & ECO_VERTICAL)
1566 FIXME("ECO_VERTICAL not implemented yet!\n");
1567 if (lParam & ECO_AUTOHSCROLL)
1568 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1569 if (lParam & ECO_AUTOVSCROLL)
1570 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1571 if (lParam & ECO_NOHIDESEL)
1572 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1573 if (lParam & ECO_WANTRETURN)
1574 FIXME("ECO_WANTRETURN not implemented yet!\n");
1580 ME_InvalidateSelection(editor);
1581 ME_SetSelection(editor, wParam, lParam);
1582 ME_InvalidateSelection(editor);
1583 ME_SendSelChange(editor);
1586 case EM_SETSCROLLPOS:
1588 POINT *point = (POINT *)lParam;
1589 ME_ScrollAbs(editor, point->y);
1592 case EM_AUTOURLDETECT:
1594 if (wParam==1 || wParam ==0)
1596 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1599 return E_INVALIDARG;
1601 case EM_GETAUTOURLDETECT:
1603 return editor->AutoURLDetect_bEnable;
1608 CHARRANGE range = *(CHARRANGE *)lParam;
1610 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1612 ME_InvalidateSelection(editor);
1613 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1614 ME_InvalidateSelection(editor);
1615 ME_SendSelChange(editor);
1619 case EM_SHOWSCROLLBAR:
1621 ShowScrollBar(editor->hWnd, wParam, lParam);
1626 LPWSTR wszText = (LPWSTR)lParam;
1627 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1628 size_t len = wszText ? lstrlenW(wszText) : 0;
1631 int oldModify = editor->nModifyStep;
1632 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1633 if (pStruct->codepage != 1200) {
1634 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1637 /* FIXME: this should support RTF strings too, according to MSDN */
1638 if (pStruct->flags & ST_SELECTION) {
1639 ME_GetSelection(editor, &from, &to);
1640 style = ME_GetSelectionInsertStyle(editor);
1641 ME_InternalDeleteText(editor, from, to - from);
1642 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1643 ME_ReleaseStyle(style);
1646 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1647 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1650 ME_CommitUndo(editor);
1651 if (!(pStruct->flags & ST_KEEPUNDO))
1653 editor->nModifyStep = oldModify;
1654 ME_EmptyUndoStack(editor);
1656 ME_UpdateRepaint(editor);
1659 case EM_SETBKGNDCOLOR:
1661 LRESULT lColor = ME_GetBackColor(editor);
1662 if (editor->rgbBackColor != -1)
1663 DeleteObject(editor->hbrBackground);
1666 editor->rgbBackColor = -1;
1667 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1671 editor->rgbBackColor = lParam;
1672 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1674 if (editor->bRedraw)
1676 InvalidateRect(hWnd, NULL, TRUE);
1682 return editor->nModifyStep == 0 ? 0 : 1;
1686 editor->nModifyStep = 1;
1688 editor->nModifyStep = 0;
1692 case EM_SETREADONLY:
1694 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1696 nStyle |= ES_READONLY;
1698 nStyle &= ~ES_READONLY;
1699 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1702 case EM_SETEVENTMASK:
1704 DWORD nOldMask = editor->nEventMask;
1706 editor->nEventMask = lParam;
1709 case EM_GETEVENTMASK:
1710 return editor->nEventMask;
1711 case EM_SETCHARFORMAT:
1713 CHARFORMAT2W buf, *p;
1714 BOOL bRepaint = TRUE;
1715 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1716 if (!wParam || (editor->mode & TM_PLAINTEXT))
1717 ME_SetDefaultCharFormat(editor, p);
1718 else if (wParam == (SCF_WORD | SCF_SELECTION))
1719 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1720 else if (wParam == SCF_ALL)
1721 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1724 ME_GetSelection(editor, &from, &to);
1725 bRepaint = (from != to);
1726 ME_SetSelectionCharFormat(editor, p);
1728 editor->nModifyStep = 1;
1729 ME_CommitUndo(editor);
1731 ME_RewrapRepaint(editor);
1734 case EM_GETCHARFORMAT:
1736 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1737 if (dst->cbSize != sizeof(CHARFORMATA) &&
1738 dst->cbSize != sizeof(CHARFORMATW) &&
1739 dst->cbSize != sizeof(CHARFORMAT2A) &&
1740 dst->cbSize != sizeof(CHARFORMAT2W))
1742 tmp.cbSize = sizeof(tmp);
1744 ME_GetDefaultCharFormat(editor, &tmp);
1746 ME_GetSelectionCharFormat(editor, &tmp);
1747 ME_CopyToCFAny(dst, &tmp);
1750 case EM_SETPARAFORMAT:
1751 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1752 ME_RewrapRepaint(editor);
1753 ME_CommitUndo(editor);
1755 case EM_GETPARAFORMAT:
1756 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1758 case EM_GETFIRSTVISIBLELINE:
1760 ME_DisplayItem *p = editor->pBuffer->pFirst;
1761 int y = ME_GetYScrollPos(editor);
1766 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1767 if (p->type == diTextEnd)
1769 if (p->type == diParagraph) {
1770 ypara = p->member.para.nYPos;
1773 ystart = ypara + p->member.row.nYPos;
1774 yend = ystart + p->member.row.nHeight;
1782 case EM_HIDESELECTION:
1784 editor->bHideSelection = (wParam != 0);
1785 ME_InvalidateSelection(editor);
1790 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1791 return TRUE; /* Should return false if a single line richedit control */
1796 ME_GetSelection(editor, &from, &to);
1797 ME_InternalDeleteText(editor, from, to-from);
1798 ME_CommitUndo(editor);
1799 ME_UpdateRepaint(editor);
1806 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1807 size_t len = lstrlenW(wszText);
1808 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1810 ME_GetSelection(editor, &from, &to);
1811 style = ME_GetSelectionInsertStyle(editor);
1812 ME_InternalDeleteText(editor, from, to-from);
1813 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1814 ME_ReleaseStyle(style);
1815 /* drop temporary style if line end */
1817 * FIXME question: does abc\n mean: put abc,
1818 * clear temp style, put \n? (would require a change)
1820 if (len>0 && wszText[len-1] == '\n')
1821 ME_ClearTempStyle(editor);
1822 ME_EndToUnicode(hWnd, wszText);
1823 ME_CommitUndo(editor);
1825 ME_EmptyUndoStack(editor);
1826 ME_UpdateRepaint(editor);
1829 case EM_SCROLLCARET:
1831 int top, bottom; /* row's edges relative to document top */
1833 ME_DisplayItem *para, *row;
1835 nPos = ME_GetYScrollPos(editor);
1836 row = ME_RowStart(editor->pCursors[0].pRun);
1837 para = ME_GetParagraph(row);
1838 top = para->member.para.nYPos + row->member.row.nYPos;
1839 bottom = top + row->member.row.nHeight;
1841 if (top < nPos) /* caret above window */
1842 ME_ScrollAbs(editor, top);
1843 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1844 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1852 BOOL bRepaint = LOWORD(lParam);
1855 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1856 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1858 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1859 ReleaseDC(hWnd, hDC);
1860 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1861 ME_SetDefaultCharFormat(editor, &fmt);
1863 ME_CommitUndo(editor);
1865 ME_RewrapRepaint(editor);
1870 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1873 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1874 if (!IsWindowUnicode(hWnd) && !strncmp((char *)lParam, "{\\rtf", 5))
1876 /* Undocumented: WM_SETTEXT supports RTF text */
1877 ME_StreamInRTFString(editor, 0, (char *)lParam);
1881 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1882 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1883 if (lstrlenW(wszText) > 0)
1885 /* uses default style! */
1886 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1888 ME_EndToUnicode(hWnd, wszText);
1892 TRACE("WM_SETTEXT - NULL\n");
1893 ME_CommitUndo(editor);
1894 ME_EmptyUndoStack(editor);
1895 ME_SetSelection(editor, 0, 0);
1896 ME_UpdateRepaint(editor);
1901 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1902 if (IsClipboardFormatAvailable(nRTFFormat))
1904 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1912 ME_GlobalDestStruct gds;
1913 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1916 if (IsClipboardFormatAvailable(nRTFFormat))
1917 cf = nRTFFormat, dwFormat = SF_RTF;
1918 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1919 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1923 if (!OpenClipboard(hWnd))
1925 gds.hData = GetClipboardData(cf);
1927 es.dwCookie = (DWORD)&gds;
1928 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1929 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1937 LPDATAOBJECT dataObj = NULL;
1941 if (editor->cPasswordMask)
1942 return 0; /* Copying or Cutting masked text isn't allowed */
1944 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1945 if(editor->lpOleCallback)
1946 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1947 if(FAILED(hr) || !dataObj)
1948 hr = ME_GetDataObject(editor, &range, &dataObj);
1950 hr = OleSetClipboard(dataObj);
1951 IDataObject_Release(dataObj);
1953 if (SUCCEEDED(hr) && msg == WM_CUT)
1955 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1956 ME_CommitUndo(editor);
1957 ME_UpdateRepaint(editor);
1961 case WM_GETTEXTLENGTH:
1962 return ME_GetTextLength(editor);
1963 case EM_GETTEXTLENGTHEX:
1964 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1967 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1969 tr.chrg.cpMax = wParam-1;
1970 tr.lpstrText = (WCHAR *)lParam;
1971 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1975 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1978 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1979 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1981 if (ex->flags & GT_SELECTION)
1983 ME_GetSelection(editor, &nStart, &nCount);
1985 nCount = min(nCount, ex->cb - 1);
1990 nCount = ex->cb - 1;
1992 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1994 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1995 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1999 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2000 we can just take a bigger buffer? :) */
2001 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2002 LPWSTR buffer = richedit_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2003 DWORD buflen = ex->cb;
2007 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2008 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2010 richedit_free(buffer);
2017 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2018 ME_GetSelection(editor, &from, &to);
2019 tr.chrg.cpMin = from;
2021 tr.lpstrText = (WCHAR *)lParam;
2022 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2024 case EM_GETSCROLLPOS:
2026 POINT *point = (POINT *)lParam;
2027 point->x = 0; /* FIXME side scrolling not implemented */
2028 point->y = ME_GetYScrollPos(editor);
2031 case EM_GETTEXTRANGE:
2033 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2034 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2035 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
2036 editor->bEmulateVersion10, ME_GetTextLength(editor));
2037 if (IsWindowUnicode(hWnd))
2038 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2041 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2042 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2043 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2044 /* FIXME this is a potential security hole (buffer overrun)
2045 if you know more about wchar->mbyte conversion please explain
2047 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2054 ME_DisplayItem *run;
2055 const BOOL bUnicode = IsWindowUnicode(hWnd);
2056 const unsigned int nMaxChars = *(WORD *) lParam;
2057 unsigned int nEndChars, nCharsLeft = nMaxChars;
2058 char *dest = (char *) lParam;
2059 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2060 we need to know how if we have the extra \n or not */
2061 int nLF = editor->bEmulateVersion10;
2063 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2064 bUnicode ? "Unicode" : "Ansi");
2066 run = ME_FindRowWithNumber(editor, wParam);
2070 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2071 && !(run->member.run.nFlags & MERF_ENDPARA))
2075 if (run->type != diRun)
2077 strText = run->member.run.strText;
2078 nCopy = min(nCharsLeft, strText->nLen);
2081 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2083 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2084 nCharsLeft, NULL, NULL);
2085 dest += nCopy * (bUnicode ? sizeof(WCHAR) : 1);
2086 nCharsLeft -= nCopy;
2089 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2090 nEndChars = min(nCharsLeft, 2 + nLF);
2091 nCharsLeft -= nEndChars;
2094 const WCHAR src[] = {'\r', '\0'};
2095 const WCHAR src10[] = {'\r', '\n', '\0'};
2096 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2099 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2101 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2103 if (nEndChars == 2 + nLF)
2104 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2106 return nMaxChars - nCharsLeft;
2108 case EM_GETLINECOUNT:
2110 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2113 while (item != editor->pBuffer->pLast)
2115 assert(item->type == diParagraph);
2116 nRows += item->member.para.nRows;
2117 item = item->member.para.next_para;
2119 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2120 return max(1, nRows);
2122 case EM_LINEFROMCHAR:
2125 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2127 return ME_RowNumberFromCharOfs(editor, wParam);
2129 case EM_EXLINEFROMCHAR:
2132 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2134 return ME_RowNumberFromCharOfs(editor, lParam);
2138 ME_DisplayItem *item, *para;
2142 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2144 item = ME_FindRowWithNumber(editor, wParam);
2147 para = ME_GetParagraph(item);
2148 item = ME_FindItemFwd(item, diRun);
2149 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2150 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2155 ME_DisplayItem *item, *item_end;
2156 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2158 if (wParam > ME_GetTextLength(editor))
2162 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2165 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2166 item = ME_RowStart(item);
2167 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2168 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2169 if (item_end->type == diStartRow)
2170 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2172 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2173 - (editor->bEmulateVersion10?2:1);
2174 nChars = nNextLineOfs - nThisLineOfs;
2175 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2178 case EM_EXLIMITTEXT:
2180 if ((int)lParam < 0)
2183 editor->nTextLimit = 65536;
2185 editor->nTextLimit = (int) lParam;
2188 case EM_GETLIMITTEXT:
2190 return editor->nTextLimit;
2194 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2195 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2199 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2200 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2201 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2207 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2208 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2212 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2213 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2214 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2220 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2221 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2223 case EM_FINDTEXTEXW:
2225 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2226 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2229 if (!wParam || !lParam)
2231 *(int *)wParam = editor->nZoomNumerator;
2232 *(int *)lParam = editor->nZoomDenominator;
2235 return ME_SetZoom(editor, wParam, lParam);
2236 case EM_CHARFROMPOS:
2237 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2238 case EM_POSFROMCHAR:
2240 ME_DisplayItem *pRun;
2241 int nCharOfs, nOffset, nLength;
2245 /* detect which API version we're dealing with */
2246 if (wParam >= 0x40000)
2248 nLength = ME_GetTextLength(editor);
2250 if (nCharOfs < nLength) {
2251 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2252 assert(pRun->type == diRun);
2253 pt.y = pRun->member.run.pt.y;
2254 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2255 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2258 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2260 if (wParam >= 0x40000) {
2261 *(POINTL *)wParam = pt;
2263 return MAKELONG( pt.x, pt.y );
2266 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2267 { /* Squelch the default horizontal scrollbar it would make */
2268 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2270 ME_CommitUndo(editor);
2271 ME_WrapMarkedParagraphs(editor);
2272 ME_MoveCaret(editor);
2275 ME_DestroyEditor(editor);
2276 SetWindowLongPtrW(hWnd, 0, 0);
2278 case WM_LBUTTONDOWN:
2280 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2282 ME_LinkNotify(editor,msg,wParam,lParam);
2285 if (GetCapture() == hWnd)
2286 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2287 ME_LinkNotify(editor,msg,wParam,lParam);
2290 if (GetCapture() == hWnd)
2292 ME_LinkNotify(editor,msg,wParam,lParam);
2294 case WM_LBUTTONDBLCLK:
2295 ME_LinkNotify(editor,msg,wParam,lParam);
2296 ME_SelectWord(editor);
2298 case WM_CONTEXTMENU:
2299 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2303 if (editor->bRedraw)
2308 hDC = BeginPaint(hWnd, &ps);
2309 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2310 EndPaint(hWnd, &ps);
2314 ME_ShowCaret(editor);
2315 ME_SendOldNotify(editor, EN_SETFOCUS);
2318 ME_HideCaret(editor);
2319 ME_SendOldNotify(editor, EN_KILLFOCUS);
2323 if (editor->bRedraw)
2325 HDC hDC = (HDC)wParam;
2327 if (GetUpdateRect(hWnd,&rc,TRUE))
2329 FillRect(hDC, &rc, editor->hbrBackground);
2335 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2338 if (ME_KeyDown(editor, LOWORD(wParam)))
2343 WCHAR wstr = LOWORD(wParam);
2344 if (editor->AutoURLDetect_bEnable)
2345 ME_AutoURLDetect(editor, wstr);
2349 case 1: /* Ctrl-A */
2350 ME_SetSelection(editor, 0, -1);
2352 case 3: /* Ctrl-C */
2353 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2357 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2358 MessageBeep(MB_ICONERROR);
2359 return 0; /* FIXME really 0 ? */
2364 case 22: /* Ctrl-V */
2365 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2367 case 24: /* Ctrl-X */
2368 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2370 case 25: /* Ctrl-Y */
2371 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2373 case 26: /* Ctrl-Z */
2374 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2377 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2378 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2379 ME_Style *style = ME_GetInsertStyle(editor, 0);
2380 ME_SaveTempStyle(editor);
2381 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2382 ME_ReleaseStyle(style);
2383 ME_CommitUndo(editor);
2384 ME_UpdateRepaint(editor);
2388 case EM_SCROLL: /* fall through */
2394 origNPos = ME_GetYScrollPos(editor);
2397 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2398 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2399 if (lineHeight <= 0) lineHeight = 24;
2401 switch(LOWORD(wParam))
2404 ME_ScrollUp(editor,lineHeight);
2407 ME_ScrollDown(editor,lineHeight);
2410 ME_ScrollUp(editor,editor->sizeWindow.cy);
2413 ME_ScrollDown(editor,editor->sizeWindow.cy);
2416 case SB_THUMBPOSITION:
2417 ME_ScrollAbs(editor,HIWORD(wParam));
2420 if (msg == EM_SCROLL)
2421 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2427 UINT pulScrollLines;
2429 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2430 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2432 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2434 /* FIXME follow the original */
2435 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2441 *((RECT *)lParam) = editor->rcFormat;
2449 RECT *rc = (RECT *)lParam;
2453 editor->rcFormat.left += rc->left;
2454 editor->rcFormat.top += rc->top;
2455 editor->rcFormat.right += rc->right;
2456 editor->rcFormat.bottom += rc->bottom;
2460 editor->rcFormat = *rc;
2465 GetClientRect(hWnd, &editor->rcFormat);
2467 if (msg != EM_SETRECTNP)
2468 ME_RewrapRepaint(editor);
2471 case EM_REQUESTRESIZE:
2472 ME_SendRequestResize(editor, TRUE);
2475 editor->bRedraw = wParam;
2479 GetClientRect(hWnd, &editor->rcFormat);
2480 ME_RewrapRepaint(editor);
2481 return DefWindowProcW(hWnd, msg, wParam, lParam);
2483 case EM_GETOLEINTERFACE:
2485 LPVOID *ppvObj = (LPVOID*) lParam;
2486 return CreateIRichEditOle(editor, ppvObj);
2488 case EM_GETPASSWORDCHAR:
2490 return editor->cPasswordMask;
2492 case EM_SETOLECALLBACK:
2493 if(editor->lpOleCallback)
2494 IUnknown_Release(editor->lpOleCallback);
2495 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2496 if(editor->lpOleCallback)
2497 IUnknown_AddRef(editor->lpOleCallback);
2499 case EM_GETWORDBREAKPROC:
2500 return (LRESULT)editor->pfnWordBreak;
2501 case EM_SETWORDBREAKPROC:
2503 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2505 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2506 return (LRESULT)pfnOld;
2508 case EM_SETTEXTMODE:
2513 if ((ret = RichEditANSIWndProc(hWnd, WM_GETTEXTLENGTH, 0, 0)) == 0)
2515 /*Check for valid wParam*/
2516 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2517 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2518 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2522 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2524 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2525 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2527 /*FIXME: Currently no support for undo level and code page options*/
2528 editor->mode = (editor->mode & (~mask)) | changes;
2534 case EM_SETPASSWORDCHAR:
2536 editor->cPasswordMask = wParam;
2537 ME_RewrapRepaint(editor);
2542 return DefWindowProcW(hWnd, msg, wParam, lParam);
2548 /******************************************************************
2549 * RichEdit10ANSIWndProc (RICHED20.9)
2551 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2555 /* FIXME: this is NOT the same as 2.0 version */
2556 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2557 if (msg == WM_NCCREATE)
2559 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2561 editor->bEmulateVersion10 = TRUE;
2562 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2567 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2569 HWND hWnd = editor->hWnd;
2570 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2573 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2576 ME_Cursor tmpCursor;
2580 x = (short)LOWORD(lParam);
2581 y = (short)HIWORD(lParam);
2582 ME_FindPixelPos(editor, x, y, &tmpCursor, &bNothing);
2583 tmpRun = &tmpCursor.pRun->member.run;
2585 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2586 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2587 { /* The clicked run has CFE_LINK set */
2588 info.nmhdr.hwndFrom = editor->hWnd;
2589 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2590 info.nmhdr.code = EN_LINK;
2592 info.wParam = wParam;
2593 info.lParam = lParam;
2594 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2595 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2596 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2600 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2602 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2605 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2606 item = item->member.para.next_para;
2609 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2610 item = item->member.para.next_para;
2617 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2619 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2621 WCHAR *pStart = buffer;
2630 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2633 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2640 item = ME_FindItemFwd(item, diRun);
2643 while(nChars && item)
2645 int nLen = ME_StrLen(item->member.run.strText);
2649 if (item->member.run.nFlags & MERF_ENDPARA)
2651 if (!ME_FindItemFwd(item, diRun))
2652 /* No '\r' is appended to the last paragraph. */
2662 /* our end paragraph consists of 2 characters now */
2663 if (editor->bEmulateVersion10)
2668 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2675 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2679 item = ME_FindItemFwd(item, diRun);
2682 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2686 void ME_RegisterEditorClass(HINSTANCE hInstance)
2692 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2693 wcW.lpfnWndProc = RichEditANSIWndProc;
2695 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2696 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2698 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2699 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2700 wcW.lpszMenuName = NULL;
2701 wcW.lpszClassName = wszClassName;
2702 bResult = RegisterClassW(&wcW);
2704 wcW.lpszClassName = wszClassName50;
2705 bResult = RegisterClassW(&wcW);
2708 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2709 wcA.lpfnWndProc = RichEditANSIWndProc;
2711 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2712 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2714 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2715 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2716 wcA.lpszMenuName = NULL;
2717 wcA.lpszClassName = "RichEdit20A";
2718 bResult = RegisterClassA(&wcA);
2720 wcA.lpszClassName = "RichEdit50A";
2721 bResult = RegisterClassA(&wcA);
2725 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2726 /* FIXME: Not implemented */
2727 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2728 hWnd, msg, get_msg_name(msg), wParam, lParam);
2729 return DefWindowProcW(hWnd, msg, wParam, lParam);
2732 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2733 /* FIXME: Not implemented */
2734 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2735 hWnd, msg, get_msg_name(msg), wParam, lParam);
2736 return DefWindowProcW(hWnd, msg, wParam, lParam);
2739 /******************************************************************
2740 * REExtendedRegisterClass (RICHED20.8)
2742 * FIXME undocumented
2743 * Need to check for errors and implement controls and callbacks
2745 LRESULT WINAPI REExtendedRegisterClass(void)
2750 FIXME("semi stub\n");
2754 wcW.hInstance = NULL;
2757 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2758 wcW.lpszMenuName = NULL;
2760 if (!ME_ListBoxRegistered)
2762 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2763 wcW.lpfnWndProc = REListWndProc;
2764 wcW.lpszClassName = wszClassNameListBox;
2765 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2768 if (!ME_ComboBoxRegistered)
2770 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2771 wcW.lpfnWndProc = REComboWndProc;
2772 wcW.lpszClassName = wszClassNameComboBox;
2773 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2777 if (ME_ListBoxRegistered)
2779 if (ME_ComboBoxRegistered)
2785 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2805 int curf_ef, link_ef, def_ef;
2806 int cur_prefx, prefx_cnt;
2807 int sel_min, sel_max;
2810 int URLmin, URLmax = 0;
2813 CHARFORMAT2W cur_format;
2814 CHARFORMAT2W default_format;
2816 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2817 sel_min = ins_pt.cpMin;
2818 sel_max = ins_pt.cpMax;
2819 if (sel_min==sel_max)
2821 if (sel_min!=sel_max)
2822 car_pos = ME_GetTextLength(editor)+1;
2823 cur_format.cbSize = sizeof(cur_format);
2824 default_format.cbSize = sizeof(default_format);
2825 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2826 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2827 link.cbSize = sizeof(link);
2828 link.dwMask = CFM_LINK;
2829 link.dwEffects = CFE_LINK;
2830 curf_ef = cur_format.dwEffects & link.dwEffects;
2831 def_ef = default_format.dwEffects & link.dwEffects;
2832 link_ef = link.dwEffects & link.dwEffects;
2833 if (curf_ef == link_ef)
2835 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2837 ME_SetSelection(editor, car_pos, car_pos);
2838 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2843 if (curf_ef == def_ef)
2846 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2847 while (cur_prefx<=prefx_cnt)
2851 ft.lpstrText = prefixes[cur_prefx].text;
2852 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2853 URLmax=max(0, car_pos);
2854 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2856 URLmax = ME_GetTextLength(editor)+1;
2858 ft.chrg.cpMin = URLmin;
2859 ft.chrg.cpMax = URLmax;
2860 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2866 url.cpMax=car_pos-1;
2867 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2868 ME_RewrapRepaint(editor);