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)
600 int from, to, to2, nUndoMode;
602 int nEventMask = editor->nEventMask;
603 ME_InStream inStream;
605 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
606 editor->nEventMask = 0;
608 ME_GetSelection(editor, &from, &to);
609 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
610 style = ME_GetSelectionInsertStyle(editor);
612 ME_InternalDeleteText(editor, from, to-from);
615 style = editor->pBuffer->pDefaultStyle;
616 ME_AddRefStyle(style);
617 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
618 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
620 ME_ClearTempStyle(editor);
621 /* FIXME restore default paragraph formatting ! */
624 nUndoMode = editor->nUndoMode;
625 editor->nUndoMode = umIgnore;
627 inStream.editstream = stream;
628 inStream.editstream->dwError = 0;
634 /* Check if it's really RTF, and if it is not, use plain text */
635 ME_StreamInFill(&inStream);
636 if (!inStream.editstream->dwError)
638 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
646 if (!inStream.editstream->dwError)
648 if (format & SF_RTF) {
649 /* setup the RTF parser */
650 memset(&parser, 0, sizeof parser);
651 RTFSetEditStream(&parser, &inStream);
652 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
653 parser.hwndEdit = editor->hWnd;
654 parser.editor = editor;
655 parser.style = style;
658 RTFSetReadHook(&parser, ME_RTFReadHook);
663 RTFFlushOutputBuffer(&parser);
666 style = parser.style;
668 else if (format & SF_TEXT)
669 ME_StreamInText(editor, format, &inStream, style);
671 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
672 ME_GetSelection(editor, &to, &to2);
673 /* put the cursor at the top */
674 if (!(format & SFF_SELECTION))
675 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
678 editor->nUndoMode = nUndoMode;
679 if (format & SFF_SELECTION)
681 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
682 TRACE("from %d to %d\n", from, to);
683 if (pUI && from < to)
688 ME_CommitUndo(editor);
692 ME_EmptyUndoStack(editor);
695 ME_ReleaseStyle(style);
696 editor->nEventMask = nEventMask;
699 ME_UpdateRepaint(editor);
701 if (!(format & SFF_SELECTION)) {
702 ME_ClearTempStyle(editor);
704 ME_MoveCaret(editor);
705 ME_SendSelChange(editor);
706 ME_SendRequestResize(editor, FALSE);
712 typedef struct tagME_RTFStringStreamStruct
717 } ME_RTFStringStreamStruct;
719 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
721 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
724 count = min(cb, pStruct->length - pStruct->pos);
725 memmove(lpBuff, pStruct->string + pStruct->pos, count);
726 pStruct->pos += count;
732 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
735 ME_RTFStringStreamStruct data;
737 data.string = string;
738 data.length = strlen(string);
740 es.dwCookie = (DWORD)&data;
741 es.pfnCallback = ME_ReadFromRTFString;
742 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
747 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
749 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
751 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
752 item = ME_FindItemFwd(item, diParagraph);
757 nOffset -= item->member.para.nCharOfs;
758 if (nItemType == diParagraph) {
760 *nItemOffset = nOffset;
765 item = ME_FindItemFwd(item, diRun);
766 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
768 nOffset -= item->member.run.nCharOfs;
770 *nItemOffset = nOffset;
777 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
779 const int nLen = lstrlenW(text);
780 const int nTextLen = ME_GetTextLength(editor);
783 ME_DisplayItem *item;
784 ME_DisplayItem *para;
785 WCHAR wLastChar = ' ';
787 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
788 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
790 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
791 FIXME("Flags 0x%08x not implemented\n",
792 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
795 if (chrg->cpMax == -1)
798 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
800 /* when searching up, if cpMin < cpMax, then instead of searching
801 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
802 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
803 * case, it is always bigger than cpMin.
805 if (!(flags & FR_DOWN))
809 nMax = nMin > nTextLen ? nTextLen : nMin;
810 if (nMin < nSwap || chrg->cpMax == -1)
816 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
819 chrgText->cpMin = chrgText->cpMax = -1;
823 if (flags & FR_DOWN) /* Forward search */
825 /* If possible, find the character before where the search starts */
826 if ((flags & FR_WHOLEWORD) && nMin)
829 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
833 chrgText->cpMin = chrgText->cpMax = -1;
836 wLastChar = item->member.run.strText->szData[nStart];
840 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
844 chrgText->cpMin = chrgText->cpMax = -1;
848 para = ME_GetParagraph(item);
850 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
852 ME_DisplayItem *pCurItem = item;
853 int nCurStart = nStart;
856 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
858 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
862 if (nMatched == nLen)
864 ME_DisplayItem *pNextItem = pCurItem;
865 int nNextStart = nCurStart;
868 /* Check to see if next character is a whitespace */
869 if (flags & FR_WHOLEWORD)
871 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
873 pNextItem = ME_FindItemFwd(pCurItem, diRun);
874 nNextStart = -nMatched;
878 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
882 if (isalnumW(wNextChar))
886 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
889 chrgText->cpMin = nStart;
890 chrgText->cpMax = nStart + nLen;
892 TRACE("found at %d-%d\n", nStart, nStart + nLen);
895 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
897 pCurItem = ME_FindItemFwd(pCurItem, diRun);
898 para = ME_GetParagraph(pCurItem);
899 nCurStart = -nMatched;
903 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
908 if (nStart == ME_StrLen(item->member.run.strText))
910 item = ME_FindItemFwd(item, diRun);
911 para = ME_GetParagraph(item);
916 else /* Backward search */
918 /* If possible, find the character after where the search ends */
919 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
922 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
926 chrgText->cpMin = chrgText->cpMax = -1;
929 wLastChar = item->member.run.strText->szData[nEnd];
933 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
937 chrgText->cpMin = chrgText->cpMax = -1;
941 para = ME_GetParagraph(item);
944 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
946 ME_DisplayItem *pCurItem = item;
950 if (nCurEnd - nMatched == 0)
952 pCurItem = ME_FindItemBack(pCurItem, diRun);
953 para = ME_GetParagraph(pCurItem);
954 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
957 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
959 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
963 if (nMatched == nLen)
965 ME_DisplayItem *pPrevItem = pCurItem;
966 int nPrevEnd = nCurEnd;
969 /* Check to see if previous character is a whitespace */
970 if (flags & FR_WHOLEWORD)
972 if (nPrevEnd - nMatched == 0)
974 pPrevItem = ME_FindItemBack(pCurItem, diRun);
976 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
980 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
984 if (isalnumW(wPrevChar))
988 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
991 chrgText->cpMin = nStart;
992 chrgText->cpMax = nStart + nLen;
994 TRACE("found at %d-%d\n", nStart, nStart + nLen);
997 if (nCurEnd - nMatched == 0)
999 pCurItem = ME_FindItemBack(pCurItem, diRun);
1000 /* Don't care about pCurItem becoming NULL here; it's already taken
1001 * care of in the exterior loop condition */
1002 para = ME_GetParagraph(pCurItem);
1003 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1007 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1014 item = ME_FindItemBack(item, diRun);
1015 para = ME_GetParagraph(item);
1016 nEnd = ME_StrLen(item->member.run.strText);
1020 TRACE("not found\n");
1022 chrgText->cpMin = chrgText->cpMax = -1;
1028 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1030 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1031 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1043 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1047 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1048 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1050 if (ME_IsSelection(editor))
1051 ME_DeleteSelection(editor);
1052 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1053 ME_DeleteTextAtCursor(editor, 1, 1);
1056 ME_CommitUndo(editor);
1057 ME_UpdateRepaint(editor);
1058 ME_SendRequestResize(editor, FALSE);
1068 chf.cbSize = sizeof(chf);
1070 ME_GetSelectionCharFormat(editor, &chf);
1071 ME_DumpStyleToBuf(&chf, buf);
1072 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1076 ME_CheckCharOffsets(editor);
1083 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1088 if(!editor->lpOleCallback)
1090 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1091 if(selrange.cpMin == selrange.cpMax)
1092 seltype |= SEL_EMPTY;
1095 /* FIXME: Handle objects */
1096 seltype |= SEL_TEXT;
1097 if(selrange.cpMax-selrange.cpMin > 1)
1098 seltype |= SEL_MULTICHAR;
1100 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1102 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1108 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1109 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1113 ed->bEmulateVersion10 = FALSE;
1114 ed->pBuffer = ME_MakeText();
1116 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1117 ReleaseDC(hWnd, hDC);
1118 ed->bCaretShown = FALSE;
1120 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1121 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1122 ed->pCursors[0].nOffset = 0;
1123 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1124 ed->pCursors[1].nOffset = 0;
1125 ed->nLastTotalLength = ed->nTotalLength = 0;
1128 ed->rgbBackColor = -1;
1129 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1130 ed->bCaretAtEnd = FALSE;
1132 ed->nModifyStep = 0;
1133 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1134 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1135 ed->nUndoStackSize = 0;
1136 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1137 ed->nUndoMode = umAddToUndo;
1138 ed->nParagraphs = 1;
1139 ed->nLastSelStart = ed->nLastSelEnd = 0;
1140 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1141 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1143 ed->bHideSelection = FALSE;
1144 ed->nInvalidOfs = -1;
1145 ed->pfnWordBreak = NULL;
1146 ed->lpOleCallback = NULL;
1147 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1148 GetClientRect(hWnd, &ed->rcFormat);
1149 for (i=0; i<HFONT_CACHE_SIZE; i++)
1151 ed->pFontCache[i].nRefs = 0;
1152 ed->pFontCache[i].nAge = 0;
1153 ed->pFontCache[i].hFont = NULL;
1156 ME_CheckCharOffsets(ed);
1158 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1159 ed->cPasswordMask = '*';
1161 ed->cPasswordMask = 0;
1166 typedef struct tagME_GlobalDestStruct
1170 } ME_GlobalDestStruct;
1172 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1174 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1179 pDest = (WORD *)lpBuff;
1180 pSrc = (WORD *)GlobalLock(pData->hData);
1181 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1182 pDest[i] = pSrc[pData->nLength+i];
1184 pData->nLength += i;
1186 GlobalUnlock(pData->hData);
1190 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1192 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1197 pSrc = (BYTE *)GlobalLock(pData->hData);
1198 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1199 pDest[i] = pSrc[pData->nLength+i];
1201 pData->nLength += i;
1203 GlobalUnlock(pData->hData);
1208 void ME_DestroyEditor(ME_TextEditor *editor)
1210 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1211 ME_DisplayItem *p = pFirst, *pNext = NULL;
1214 ME_ClearTempStyle(editor);
1215 ME_EmptyUndoStack(editor);
1218 ME_DestroyDisplayItem(p);
1221 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1222 for (i=0; i<HFONT_CACHE_SIZE; i++)
1224 if (editor->pFontCache[i].hFont)
1225 DeleteObject(editor->pFontCache[i].hFont);
1227 DeleteObject(editor->hbrBackground);
1228 if(editor->lpOleCallback)
1229 IUnknown_Release(editor->lpOleCallback);
1234 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1235 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1236 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1237 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1239 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1244 case DLL_PROCESS_ATTACH:
1245 DisableThreadLibraryCalls(hinstDLL);
1246 me_heap = HeapCreate (0, 0x10000, 0);
1247 ME_RegisterEditorClass(hinstDLL);
1250 case DLL_PROCESS_DETACH:
1251 UnregisterClassW(wszClassName, 0);
1252 UnregisterClassW(wszClassName50, 0);
1253 UnregisterClassA("RichEdit20A", 0);
1254 UnregisterClassA("RichEdit50A", 0);
1255 if (ME_ListBoxRegistered)
1256 UnregisterClassW(wszClassNameListBox, 0);
1257 if (ME_ComboBoxRegistered)
1258 UnregisterClassW(wszClassNameComboBox, 0);
1259 HeapDestroy (me_heap);
1267 #define UNSUPPORTED_MSG(e) \
1269 FIXME(#e ": stub\n"); \
1270 return DefWindowProcW(hWnd, msg, wParam, lParam);
1272 static const char * const edit_messages[] = {
1301 "EM_SETPASSWORDCHAR",
1302 "EM_EMPTYUNDOBUFFER",
1303 "EM_GETFIRSTVISIBLELINE",
1305 "EM_SETWORDBREAKPROC",
1306 "EM_GETWORDBREAKPROC",
1307 "EM_GETPASSWORDCHAR",
1315 static const char * const richedit_messages[] = {
1320 "EM_EXLINEFROMCHAR",
1326 "EM_GETOLEINTERFACE",
1336 "EM_SETOLECALLBACK",
1338 "EM_SETTARGETDEVICE",
1346 "EM_GETWORDBREAKPROCEX",
1347 "EM_SETWORDBREAKPROCEX",
1349 "EM_UNKNOWN_USER_83",
1354 "EM_STOPGROUPTYPING",
1358 "EM_GETAUTOURLDETECT",
1361 "EM_GETTEXTLENGTHEX",
1364 "EM_UNKNOWN_USER_98",
1365 "EM_UNKNOWN_USER_99",
1366 "EM_SETPUNCTUATION",
1367 "EM_GETPUNCTUATION",
1368 "EM_SETWORDWRAPMODE",
1369 "EM_GETWORDWRAPMODE",
1375 "EM_UNKNOWN_USER_109",
1376 "EM_UNKNOWN_USER_110",
1377 "EM_UNKNOWN_USER_111",
1378 "EM_UNKNOWN_USER_112",
1379 "EM_UNKNOWN_USER_113",
1380 "EM_UNKNOWN_USER_114",
1381 "EM_UNKNOWN_USER_115",
1382 "EM_UNKNOWN_USER_116",
1383 "EM_UNKNOWN_USER_117",
1384 "EM_UNKNOWN_USER_118",
1385 "EM_UNKNOWN_USER_119",
1386 "EM_SETLANGOPTIONS",
1387 "EM_GETLANGOPTIONS",
1388 "EM_GETIMECOMPMODE",
1392 "EM_SETIMEMODEBIAS",
1397 get_msg_name(UINT msg)
1399 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1400 return edit_messages[msg - EM_GETSEL];
1401 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1402 return richedit_messages[msg - EM_CANPASTE];
1406 /******************************************************************
1407 * RichEditANSIWndProc (RICHED20.10)
1409 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1410 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1412 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1413 hWnd, msg, get_msg_name(msg), wParam, lParam);
1415 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1416 ERR("RichEditANSIWndProc called with invalid hWnd %p - application bug?\n", hWnd);
1422 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1423 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1424 UNSUPPORTED_MSG(EM_FMTLINES)
1425 UNSUPPORTED_MSG(EM_FORMATRANGE)
1426 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1427 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1428 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1429 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1430 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1431 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1432 UNSUPPORTED_MSG(EM_GETREDONAME)
1433 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1434 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1435 UNSUPPORTED_MSG(EM_GETUNDONAME)
1436 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1437 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1438 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1439 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1440 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1441 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1442 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1443 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1444 UNSUPPORTED_MSG(EM_SETPALETTE)
1445 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1446 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1447 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1448 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1449 UNSUPPORTED_MSG(WM_STYLECHANGING)
1450 UNSUPPORTED_MSG(WM_STYLECHANGED)
1451 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1453 /* Messages specific to Richedit controls */
1456 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1458 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1461 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1462 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1463 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1468 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1469 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1470 editor = ME_MakeEditor(hWnd);
1471 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1472 pcs = 0; /* ignore */
1475 case EM_EMPTYUNDOBUFFER:
1476 ME_EmptyUndoStack(editor);
1480 /* Note: wParam/lParam can be NULL */
1482 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1483 PUINT pto = lParam ? (PUINT)lParam : &to;
1484 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1485 if ((*pfrom|*pto) & 0xFFFF0000)
1487 return MAKELONG(*pfrom,*pto);
1491 CHARRANGE *pRange = (CHARRANGE *)lParam;
1492 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1493 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1496 case EM_SETUNDOLIMIT:
1498 if ((int)wParam < 0)
1499 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1501 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1502 /* Setting a max stack size keeps wine from getting killed
1503 for hogging memory. Windows allocates all this memory at once, so
1504 no program would realistically set a value above our maxiumum. */
1505 return editor->nUndoLimit;
1508 return editor->pUndoStack != NULL;
1510 return editor->pRedoStack != NULL;
1511 case WM_UNDO: /* FIXME: actually not the same */
1520 /* these flags are equivalent to the ES_* counterparts */
1521 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1522 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1523 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1529 /* these flags are equivalent to ES_* counterparts
1530 * ECO_READONLY is already implemented in the code, only requires
1531 * setting the bit to work
1533 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1534 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1535 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1536 DWORD settings = mask & raw;
1552 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1554 if (lParam & ECO_AUTOWORDSELECTION)
1555 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1556 if (lParam & ECO_SELECTIONBAR)
1557 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1558 if (lParam & ECO_VERTICAL)
1559 FIXME("ECO_VERTICAL not implemented yet!\n");
1560 if (lParam & ECO_AUTOHSCROLL)
1561 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1562 if (lParam & ECO_AUTOVSCROLL)
1563 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1564 if (lParam & ECO_NOHIDESEL)
1565 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1566 if (lParam & ECO_WANTRETURN)
1567 FIXME("ECO_WANTRETURN not implemented yet!\n");
1573 ME_InvalidateSelection(editor);
1574 ME_SetSelection(editor, wParam, lParam);
1575 ME_InvalidateSelection(editor);
1576 ME_SendSelChange(editor);
1579 case EM_SETSCROLLPOS:
1581 POINT *point = (POINT *)lParam;
1582 ME_ScrollAbs(editor, point->y);
1585 case EM_AUTOURLDETECT:
1587 if (wParam==1 || wParam ==0)
1589 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1592 return E_INVALIDARG;
1594 case EM_GETAUTOURLDETECT:
1596 return editor->AutoURLDetect_bEnable;
1601 CHARRANGE range = *(CHARRANGE *)lParam;
1603 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1605 /* if cpMin < 0, then selection is deselected and caret moved to end of
1606 * the current selection */
1607 if (range.cpMin < 0)
1609 ME_GetSelection(editor, &start, &end);
1613 else if (range.cpMax > ME_GetTextLength(editor) +1)
1615 range.cpMax = ME_GetTextLength(editor) + 1;
1618 ME_InvalidateSelection(editor);
1619 ME_SetSelection(editor, range.cpMin, range.cpMax);
1620 ME_InvalidateSelection(editor);
1621 ME_SendSelChange(editor);
1625 case EM_SHOWSCROLLBAR:
1627 ShowScrollBar(editor->hWnd, wParam, lParam);
1632 LPWSTR wszText = (LPWSTR)lParam;
1633 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1634 size_t len = wszText ? lstrlenW(wszText) : 0;
1637 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1638 if (pStruct->codepage != 1200) {
1639 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1642 /* FIXME: this should support RTF strings too, according to MSDN */
1643 if (pStruct->flags & ST_SELECTION) {
1644 ME_GetSelection(editor, &from, &to);
1645 style = ME_GetSelectionInsertStyle(editor);
1646 ME_InternalDeleteText(editor, from, to - from);
1647 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1648 ME_ReleaseStyle(style);
1651 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1652 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1655 ME_CommitUndo(editor);
1656 if (!(pStruct->flags & ST_KEEPUNDO))
1657 ME_EmptyUndoStack(editor);
1658 ME_UpdateRepaint(editor);
1661 case EM_SETBKGNDCOLOR:
1663 LRESULT lColor = ME_GetBackColor(editor);
1664 if (editor->rgbBackColor != -1)
1665 DeleteObject(editor->hbrBackground);
1668 editor->rgbBackColor = -1;
1669 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1673 editor->rgbBackColor = lParam;
1674 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1676 if (editor->bRedraw)
1678 InvalidateRect(hWnd, NULL, TRUE);
1684 return editor->nModifyStep == 0 ? 0 : 1;
1688 editor->nModifyStep = 0x80000000;
1690 editor->nModifyStep = 0;
1694 case EM_SETREADONLY:
1696 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1698 nStyle |= ES_READONLY;
1700 nStyle &= ~ES_READONLY;
1701 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1704 case EM_SETEVENTMASK:
1706 DWORD nOldMask = editor->nEventMask;
1708 editor->nEventMask = lParam;
1711 case EM_GETEVENTMASK:
1712 return editor->nEventMask;
1713 case EM_SETCHARFORMAT:
1715 CHARFORMAT2W buf, *p;
1716 BOOL bRepaint = TRUE;
1717 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1718 if (!wParam || (editor->mode & TM_PLAINTEXT))
1719 ME_SetDefaultCharFormat(editor, p);
1720 else if (wParam == (SCF_WORD | SCF_SELECTION))
1721 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1722 else if (wParam == SCF_ALL)
1723 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1726 ME_GetSelection(editor, &from, &to);
1727 bRepaint = (from != to);
1728 ME_SetSelectionCharFormat(editor, p);
1730 ME_CommitUndo(editor);
1732 ME_UpdateRepaint(editor);
1735 case EM_GETCHARFORMAT:
1737 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1738 if (dst->cbSize != sizeof(CHARFORMATA) &&
1739 dst->cbSize != sizeof(CHARFORMATW) &&
1740 dst->cbSize != sizeof(CHARFORMAT2A) &&
1741 dst->cbSize != sizeof(CHARFORMAT2W))
1743 tmp.cbSize = sizeof(tmp);
1745 ME_GetDefaultCharFormat(editor, &tmp);
1747 ME_GetSelectionCharFormat(editor, &tmp);
1748 ME_CopyToCFAny(dst, &tmp);
1751 case EM_SETPARAFORMAT:
1752 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1753 ME_UpdateRepaint(editor);
1754 ME_CommitUndo(editor);
1756 case EM_GETPARAFORMAT:
1757 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1759 case EM_GETFIRSTVISIBLELINE:
1761 ME_DisplayItem *p = editor->pBuffer->pFirst;
1762 int y = ME_GetYScrollPos(editor);
1767 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1768 if (p->type == diTextEnd)
1770 if (p->type == diParagraph) {
1771 ypara = p->member.para.nYPos;
1774 ystart = ypara + p->member.row.nYPos;
1775 yend = ystart + p->member.row.nHeight;
1783 case EM_HIDESELECTION:
1785 editor->bHideSelection = (wParam != 0);
1786 ME_InvalidateSelection(editor);
1791 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1792 return TRUE; /* Should return false if a single line richedit control */
1797 ME_GetSelection(editor, &from, &to);
1798 ME_InternalDeleteText(editor, from, to-from);
1799 ME_CommitUndo(editor);
1800 ME_UpdateRepaint(editor);
1807 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1808 size_t len = lstrlenW(wszText);
1809 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1811 ME_GetSelection(editor, &from, &to);
1812 style = ME_GetSelectionInsertStyle(editor);
1813 ME_InternalDeleteText(editor, from, to-from);
1814 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1815 ME_ReleaseStyle(style);
1816 ME_EndToUnicode(hWnd, wszText);
1817 /* drop temporary style if line end */
1818 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1819 if (len>0 && wszText[len-1] == '\n')
1820 ME_ClearTempStyle(editor);
1822 ME_CommitUndo(editor);
1824 ME_EmptyUndoStack(editor);
1825 ME_UpdateRepaint(editor);
1828 case EM_SCROLLCARET:
1830 int top, bottom; /* row's edges relative to document top */
1832 ME_DisplayItem *para, *row;
1834 nPos = ME_GetYScrollPos(editor);
1835 row = ME_RowStart(editor->pCursors[0].pRun);
1836 para = ME_GetParagraph(row);
1837 top = para->member.para.nYPos + row->member.row.nYPos;
1838 bottom = top + row->member.row.nHeight;
1840 if (top < nPos) /* caret above window */
1841 ME_ScrollAbs(editor, top);
1842 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1843 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1851 BOOL bRepaint = LOWORD(lParam);
1854 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1855 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1857 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1858 ReleaseDC(hWnd, hDC);
1859 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1860 ME_SetDefaultCharFormat(editor, &fmt);
1862 ME_CommitUndo(editor);
1864 ME_UpdateRepaint(editor);
1869 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1872 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1873 if (!IsWindowUnicode(hWnd) && !strncmp((char *)lParam, "{\\rtf", 5))
1875 /* Undocumented: WM_SETTEXT supports RTF text */
1876 ME_StreamInRTFString(editor, 0, (char *)lParam);
1880 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1881 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1882 if (lstrlenW(wszText) > 0)
1884 /* uses default style! */
1885 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1887 ME_EndToUnicode(hWnd, wszText);
1891 TRACE("WM_SETTEXT - NULL\n");
1892 ME_CommitUndo(editor);
1893 ME_EmptyUndoStack(editor);
1894 ME_SetSelection(editor, 0, 0);
1895 ME_UpdateRepaint(editor);
1900 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1901 if (IsClipboardFormatAvailable(nRTFFormat))
1903 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1911 ME_GlobalDestStruct gds;
1912 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1915 if (IsClipboardFormatAvailable(nRTFFormat))
1916 cf = nRTFFormat, dwFormat = SF_RTF;
1917 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1918 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1922 if (!OpenClipboard(hWnd))
1924 gds.hData = GetClipboardData(cf);
1926 es.dwCookie = (DWORD)&gds;
1927 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1928 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1936 LPDATAOBJECT dataObj = NULL;
1940 if (editor->cPasswordMask)
1941 return 0; /* Copying or Cutting masked text isn't allowed */
1943 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1944 if(editor->lpOleCallback)
1945 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1946 if(FAILED(hr) || !dataObj)
1947 hr = ME_GetDataObject(editor, &range, &dataObj);
1949 hr = OleSetClipboard(dataObj);
1950 IDataObject_Release(dataObj);
1952 if (SUCCEEDED(hr) && msg == WM_CUT)
1954 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1955 ME_CommitUndo(editor);
1956 ME_UpdateRepaint(editor);
1960 case WM_GETTEXTLENGTH:
1961 return ME_GetTextLength(editor);
1962 case EM_GETTEXTLENGTHEX:
1963 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1966 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1968 tr.chrg.cpMax = wParam-1;
1969 tr.lpstrText = (WCHAR *)lParam;
1970 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1974 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1977 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1978 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1980 if (ex->flags & GT_SELECTION)
1982 ME_GetSelection(editor, &nStart, &nCount);
1984 nCount = min(nCount, ex->cb - 1);
1989 nCount = ex->cb - 1;
1991 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1993 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1994 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1998 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1999 we can just take a bigger buffer? :) */
2000 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2001 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
2002 DWORD buflen = ex->cb;
2006 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2007 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2009 HeapFree(GetProcessHeap(),0,buffer);
2016 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2017 ME_GetSelection(editor, &from, &to);
2018 tr.chrg.cpMin = from;
2020 tr.lpstrText = (WCHAR *)lParam;
2021 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2023 case EM_GETSCROLLPOS:
2025 POINT *point = (POINT *)lParam;
2026 point->x = 0; /* FIXME side scrolling not implemented */
2027 point->y = ME_GetYScrollPos(editor);
2030 case EM_GETTEXTRANGE:
2032 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2033 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2034 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
2035 editor->bEmulateVersion10, ME_GetTextLength(editor));
2036 if (IsWindowUnicode(hWnd))
2037 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2040 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2041 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2042 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2043 /* FIXME this is a potential security hole (buffer overrun)
2044 if you know more about wchar->mbyte conversion please explain
2046 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2053 ME_DisplayItem *run;
2054 const BOOL bUnicode = IsWindowUnicode(hWnd);
2055 const unsigned int nMaxChars = *(WORD *) lParam;
2056 unsigned int nEndChars, nCharsLeft = nMaxChars;
2057 char *dest = (char *) lParam;
2058 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2059 we need to know how if we have the extra \n or not */
2060 int nLF = editor->bEmulateVersion10;
2062 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2063 bUnicode ? "Unicode" : "Ansi");
2065 run = ME_FindRowWithNumber(editor, wParam);
2069 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2070 && !(run->member.run.nFlags & MERF_ENDPARA))
2074 if (run->type != diRun)
2076 strText = run->member.run.strText;
2077 nCopy = min(nCharsLeft, strText->nLen);
2080 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2082 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2083 nCharsLeft, NULL, NULL);
2084 dest += nCopy * (bUnicode ? sizeof(WCHAR) : 1);
2085 nCharsLeft -= nCopy;
2088 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2089 nEndChars = min(nCharsLeft, 2 + nLF);
2090 nCharsLeft -= nEndChars;
2093 const WCHAR src[] = {'\r', '\0'};
2094 const WCHAR src10[] = {'\r', '\n', '\0'};
2095 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2098 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2100 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2102 if (nEndChars == 2 + nLF)
2103 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2105 return nMaxChars - nCharsLeft;
2107 case EM_GETLINECOUNT:
2109 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2112 while (item != editor->pBuffer->pLast)
2114 assert(item->type == diParagraph);
2115 nRows += item->member.para.nRows;
2116 item = item->member.para.next_para;
2118 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2119 return max(1, nRows);
2121 case EM_LINEFROMCHAR:
2124 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2126 return ME_RowNumberFromCharOfs(editor, wParam);
2128 case EM_EXLINEFROMCHAR:
2131 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2133 return ME_RowNumberFromCharOfs(editor, lParam);
2137 ME_DisplayItem *item, *para;
2141 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2143 item = ME_FindRowWithNumber(editor, wParam);
2146 para = ME_GetParagraph(item);
2147 item = ME_FindItemFwd(item, diRun);
2148 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2149 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2154 ME_DisplayItem *item, *item_end;
2155 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2157 if (wParam > ME_GetTextLength(editor))
2161 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2164 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2165 item = ME_RowStart(item);
2166 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2167 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2168 if (item_end->type == diStartRow)
2169 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2171 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2172 - (editor->bEmulateVersion10?2:1);
2173 nChars = nNextLineOfs - nThisLineOfs;
2174 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2177 case EM_EXLIMITTEXT:
2179 if (wParam != 0 || lParam < 0)
2182 editor->nTextLimit = 65536;
2184 editor->nTextLimit = (int) lParam;
2187 case EM_GETLIMITTEXT:
2189 return editor->nTextLimit;
2193 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2194 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2197 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2198 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2199 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2203 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2204 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2207 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2208 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2209 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2213 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2214 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2216 case EM_FINDTEXTEXW:
2218 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2219 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2222 if (!wParam || !lParam)
2224 *(int *)wParam = editor->nZoomNumerator;
2225 *(int *)lParam = editor->nZoomDenominator;
2228 return ME_SetZoom(editor, wParam, lParam);
2229 case EM_CHARFROMPOS:
2230 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2231 case EM_POSFROMCHAR:
2233 ME_DisplayItem *pRun;
2234 int nCharOfs, nOffset, nLength;
2238 /* detect which API version we're dealing with */
2239 if (wParam >= 0x40000)
2241 nLength = ME_GetTextLength(editor);
2243 if (nCharOfs < nLength) {
2244 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2245 assert(pRun->type == diRun);
2246 pt.y = pRun->member.run.pt.y;
2247 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2248 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2251 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2253 if (wParam >= 0x40000) {
2254 *(POINTL *)wParam = pt;
2256 return MAKELONG( pt.x, pt.y );
2259 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2260 { /* Squelch the default horizontal scrollbar it would make */
2261 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2263 ME_CommitUndo(editor);
2264 ME_WrapMarkedParagraphs(editor);
2265 ME_MoveCaret(editor);
2268 ME_DestroyEditor(editor);
2269 SetWindowLongPtrW(hWnd, 0, 0);
2271 case WM_LBUTTONDOWN:
2273 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2275 ME_LinkNotify(editor,msg,wParam,lParam);
2278 if (GetCapture() == hWnd)
2279 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2280 ME_LinkNotify(editor,msg,wParam,lParam);
2283 if (GetCapture() == hWnd)
2285 ME_LinkNotify(editor,msg,wParam,lParam);
2287 case WM_LBUTTONDBLCLK:
2288 ME_LinkNotify(editor,msg,wParam,lParam);
2289 ME_SelectWord(editor);
2291 case WM_CONTEXTMENU:
2292 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2296 if (editor->bRedraw)
2301 hDC = BeginPaint(hWnd, &ps);
2302 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2303 EndPaint(hWnd, &ps);
2307 ME_ShowCaret(editor);
2308 ME_SendOldNotify(editor, EN_SETFOCUS);
2311 ME_HideCaret(editor);
2312 ME_SendOldNotify(editor, EN_KILLFOCUS);
2316 if (editor->bRedraw)
2318 HDC hDC = (HDC)wParam;
2320 if (GetUpdateRect(hWnd,&rc,TRUE))
2322 FillRect(hDC, &rc, editor->hbrBackground);
2328 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2331 if (ME_KeyDown(editor, LOWORD(wParam)))
2336 WCHAR wstr = LOWORD(wParam);
2337 if (editor->AutoURLDetect_bEnable)
2338 ME_AutoURLDetect(editor, wstr);
2342 case 1: /* Ctrl-A */
2343 ME_SetSelection(editor, 0, -1);
2345 case 3: /* Ctrl-C */
2346 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2350 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2351 MessageBeep(MB_ICONERROR);
2352 return 0; /* FIXME really 0 ? */
2357 case 22: /* Ctrl-V */
2358 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2360 case 24: /* Ctrl-X */
2361 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2363 case 25: /* Ctrl-Y */
2364 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2366 case 26: /* Ctrl-Z */
2367 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2370 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2371 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2372 ME_Style *style = ME_GetInsertStyle(editor, 0);
2373 ME_SaveTempStyle(editor);
2374 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2375 ME_ReleaseStyle(style);
2376 ME_CommitUndo(editor);
2377 ME_UpdateRepaint(editor);
2381 case EM_SCROLL: /* fall through */
2387 origNPos = ME_GetYScrollPos(editor);
2390 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2391 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2392 if (lineHeight <= 0) lineHeight = 24;
2394 switch(LOWORD(wParam))
2397 ME_ScrollUp(editor,lineHeight);
2400 ME_ScrollDown(editor,lineHeight);
2403 ME_ScrollUp(editor,editor->sizeWindow.cy);
2406 ME_ScrollDown(editor,editor->sizeWindow.cy);
2409 case SB_THUMBPOSITION:
2410 ME_ScrollAbs(editor,HIWORD(wParam));
2413 if (msg == EM_SCROLL)
2414 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2420 UINT pulScrollLines;
2422 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2423 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2425 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2427 /* FIXME follow the original */
2428 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2434 *((RECT *)lParam) = editor->rcFormat;
2442 RECT *rc = (RECT *)lParam;
2446 editor->rcFormat.left += rc->left;
2447 editor->rcFormat.top += rc->top;
2448 editor->rcFormat.right += rc->right;
2449 editor->rcFormat.bottom += rc->bottom;
2453 editor->rcFormat = *rc;
2458 GetClientRect(hWnd, &editor->rcFormat);
2460 if (msg != EM_SETRECTNP)
2461 ME_RewrapRepaint(editor);
2464 case EM_REQUESTRESIZE:
2465 ME_SendRequestResize(editor, TRUE);
2468 editor->bRedraw = wParam;
2472 GetClientRect(hWnd, &editor->rcFormat);
2473 ME_RewrapRepaint(editor);
2474 return DefWindowProcW(hWnd, msg, wParam, lParam);
2476 case EM_GETOLEINTERFACE:
2478 LPVOID *ppvObj = (LPVOID*) lParam;
2479 return CreateIRichEditOle(editor, ppvObj);
2481 case EM_GETPASSWORDCHAR:
2483 return editor->cPasswordMask;
2485 case EM_SETOLECALLBACK:
2486 if(editor->lpOleCallback)
2487 IUnknown_Release(editor->lpOleCallback);
2488 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2489 if(editor->lpOleCallback)
2490 IUnknown_AddRef(editor->lpOleCallback);
2492 case EM_GETWORDBREAKPROC:
2493 return (LRESULT)editor->pfnWordBreak;
2494 case EM_SETWORDBREAKPROC:
2496 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2498 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2499 return (LRESULT)pfnOld;
2501 case EM_SETTEXTMODE:
2506 if ((ret = RichEditANSIWndProc(hWnd, WM_GETTEXTLENGTH, 0, 0)) == 0)
2508 /*Check for valid wParam*/
2509 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2510 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2511 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2515 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2517 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2518 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2520 /*FIXME: Currently no support for undo level and code page options*/
2521 editor->mode = (editor->mode & (~mask)) | changes;
2527 case EM_SETPASSWORDCHAR:
2529 editor->cPasswordMask = wParam;
2530 ME_RewrapRepaint(editor);
2535 return DefWindowProcW(hWnd, msg, wParam, lParam);
2541 /******************************************************************
2542 * RichEdit10ANSIWndProc (RICHED20.9)
2544 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2548 /* FIXME: this is NOT the same as 2.0 version */
2549 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2550 if (msg == WM_NCCREATE)
2552 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2554 editor->bEmulateVersion10 = TRUE;
2555 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2560 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2562 HWND hWnd = editor->hWnd;
2563 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2566 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2569 ME_Cursor tmpCursor;
2573 x = (short)LOWORD(lParam);
2574 y = (short)HIWORD(lParam);
2575 ME_FindPixelPos(editor, x, y, &tmpCursor, &bNothing);
2576 tmpRun = &tmpCursor.pRun->member.run;
2578 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2579 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2580 { /* The clicked run has CFE_LINK set */
2581 info.nmhdr.hwndFrom = editor->hWnd;
2582 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2583 info.nmhdr.code = EN_LINK;
2585 info.wParam = wParam;
2586 info.lParam = lParam;
2587 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2588 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2589 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2593 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2595 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2598 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2599 item = item->member.para.next_para;
2602 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2603 item = item->member.para.next_para;
2610 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2612 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2614 WCHAR *pStart = buffer;
2623 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2626 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2633 item = ME_FindItemFwd(item, diRun);
2636 while(nChars && item)
2638 int nLen = ME_StrLen(item->member.run.strText);
2642 if (item->member.run.nFlags & MERF_ENDPARA)
2644 if (!ME_FindItemFwd(item, diRun))
2645 /* No '\r' is appended to the last paragraph. */
2655 /* our end paragraph consists of 2 characters now */
2656 if (editor->bEmulateVersion10)
2661 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2668 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2672 item = ME_FindItemFwd(item, diRun);
2675 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2679 void ME_RegisterEditorClass(HINSTANCE hInstance)
2685 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2686 wcW.lpfnWndProc = RichEditANSIWndProc;
2688 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2689 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2691 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2692 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2693 wcW.lpszMenuName = NULL;
2694 wcW.lpszClassName = wszClassName;
2695 bResult = RegisterClassW(&wcW);
2697 wcW.lpszClassName = wszClassName50;
2698 bResult = RegisterClassW(&wcW);
2701 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2702 wcA.lpfnWndProc = RichEditANSIWndProc;
2704 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2705 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2707 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2708 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2709 wcA.lpszMenuName = NULL;
2710 wcA.lpszClassName = "RichEdit20A";
2711 bResult = RegisterClassA(&wcA);
2713 wcA.lpszClassName = "RichEdit50A";
2714 bResult = RegisterClassA(&wcA);
2718 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2719 /* FIXME: Not implemented */
2720 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2721 hWnd, msg, get_msg_name(msg), wParam, lParam);
2722 return DefWindowProcW(hWnd, msg, wParam, lParam);
2725 LRESULT WINAPI REListWndProc(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 /******************************************************************
2733 * REExtendedRegisterClass (RICHED20.8)
2735 * FIXME undocumented
2736 * Need to check for errors and implement controls and callbacks
2738 LRESULT WINAPI REExtendedRegisterClass(void)
2743 FIXME("semi stub\n");
2747 wcW.hInstance = NULL;
2750 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2751 wcW.lpszMenuName = NULL;
2753 if (!ME_ListBoxRegistered)
2755 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2756 wcW.lpfnWndProc = REListWndProc;
2757 wcW.lpszClassName = wszClassNameListBox;
2758 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2761 if (!ME_ComboBoxRegistered)
2763 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2764 wcW.lpfnWndProc = REComboWndProc;
2765 wcW.lpszClassName = wszClassNameComboBox;
2766 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2770 if (ME_ListBoxRegistered)
2772 if (ME_ComboBoxRegistered)
2778 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2798 int curf_ef, link_ef, def_ef;
2799 int cur_prefx, prefx_cnt;
2800 int sel_min, sel_max;
2803 int URLmin, URLmax = 0;
2806 CHARFORMAT2W cur_format;
2807 CHARFORMAT2W default_format;
2809 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2810 sel_min = ins_pt.cpMin;
2811 sel_max = ins_pt.cpMax;
2812 if (sel_min==sel_max)
2814 if (sel_min!=sel_max)
2815 car_pos = ME_GetTextLength(editor)+1;
2816 cur_format.cbSize = sizeof(cur_format);
2817 default_format.cbSize = sizeof(default_format);
2818 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2819 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2820 link.cbSize = sizeof(link);
2821 link.dwMask = CFM_LINK;
2822 link.dwEffects = CFE_LINK;
2823 curf_ef = cur_format.dwEffects & link.dwEffects;
2824 def_ef = default_format.dwEffects & link.dwEffects;
2825 link_ef = link.dwEffects & link.dwEffects;
2826 if (curf_ef == link_ef)
2828 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2830 ME_SetSelection(editor, car_pos, car_pos);
2831 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2836 if (curf_ef == def_ef)
2839 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2840 while (cur_prefx<=prefx_cnt)
2844 ft.lpstrText = prefixes[cur_prefx].text;
2845 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2846 URLmax=max(0, car_pos);
2847 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2849 URLmax = ME_GetTextLength(editor)+1;
2851 ft.chrg.cpMin = URLmin;
2852 ft.chrg.cpMax = URLmax;
2853 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2859 url.cpMax=car_pos-1;
2860 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2861 ME_RewrapRepaint(editor);