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)
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 inStream.editstream = stream;
625 inStream.editstream->dwError = 0;
631 /* Check if it's really RTF, and if it is not, use plain text */
632 ME_StreamInFill(&inStream);
633 if (!inStream.editstream->dwError)
635 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
643 if (!inStream.editstream->dwError)
645 if (format & SF_RTF) {
646 /* setup the RTF parser */
647 memset(&parser, 0, sizeof parser);
648 RTFSetEditStream(&parser, &inStream);
649 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
650 parser.hwndEdit = editor->hWnd;
651 parser.editor = editor;
652 parser.style = style;
655 RTFSetReadHook(&parser, ME_RTFReadHook);
660 RTFFlushOutputBuffer(&parser);
663 style = parser.style;
665 else if (format & SF_TEXT)
666 ME_StreamInText(editor, format, &inStream, style);
668 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
669 ME_GetSelection(editor, &to, &to2);
670 /* put the cursor at the top */
671 if (!(format & SFF_SELECTION))
672 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
675 if (format & SFF_SELECTION)
677 if(from < to) /* selection overwritten is non-empty */
679 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
680 TRACE("from %d to %d\n", from, to);
687 /* even if we didn't add an undo, we need to commit the ones added earlier */
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 ed->AutoURLDetect_bEnable = FALSE;
1149 GetClientRect(hWnd, &ed->rcFormat);
1150 for (i=0; i<HFONT_CACHE_SIZE; i++)
1152 ed->pFontCache[i].nRefs = 0;
1153 ed->pFontCache[i].nAge = 0;
1154 ed->pFontCache[i].hFont = NULL;
1157 ME_CheckCharOffsets(ed);
1159 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1160 ed->cPasswordMask = '*';
1162 ed->cPasswordMask = 0;
1167 typedef struct tagME_GlobalDestStruct
1171 } ME_GlobalDestStruct;
1173 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1175 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1180 pDest = (WORD *)lpBuff;
1181 pSrc = (WORD *)GlobalLock(pData->hData);
1182 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1183 pDest[i] = pSrc[pData->nLength+i];
1185 pData->nLength += i;
1187 GlobalUnlock(pData->hData);
1191 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1193 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1198 pSrc = (BYTE *)GlobalLock(pData->hData);
1199 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1200 pDest[i] = pSrc[pData->nLength+i];
1202 pData->nLength += i;
1204 GlobalUnlock(pData->hData);
1209 void ME_DestroyEditor(ME_TextEditor *editor)
1211 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1212 ME_DisplayItem *p = pFirst, *pNext = NULL;
1215 ME_ClearTempStyle(editor);
1216 ME_EmptyUndoStack(editor);
1219 ME_DestroyDisplayItem(p);
1222 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1223 for (i=0; i<HFONT_CACHE_SIZE; i++)
1225 if (editor->pFontCache[i].hFont)
1226 DeleteObject(editor->pFontCache[i].hFont);
1228 DeleteObject(editor->hbrBackground);
1229 if(editor->lpOleCallback)
1230 IUnknown_Release(editor->lpOleCallback);
1232 FREE_OBJ(editor->pBuffer);
1233 FREE_OBJ(editor->pCursors);
1238 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1239 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1240 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1241 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1243 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1248 case DLL_PROCESS_ATTACH:
1249 DisableThreadLibraryCalls(hinstDLL);
1250 me_heap = HeapCreate (0, 0x10000, 0);
1251 ME_RegisterEditorClass(hinstDLL);
1255 case DLL_PROCESS_DETACH:
1256 UnregisterClassW(wszClassName, 0);
1257 UnregisterClassW(wszClassName50, 0);
1258 UnregisterClassA("RichEdit20A", 0);
1259 UnregisterClassA("RichEdit50A", 0);
1260 if (ME_ListBoxRegistered)
1261 UnregisterClassW(wszClassNameListBox, 0);
1262 if (ME_ComboBoxRegistered)
1263 UnregisterClassW(wszClassNameComboBox, 0);
1265 HeapDestroy (me_heap);
1273 #define UNSUPPORTED_MSG(e) \
1275 FIXME(#e ": stub\n"); \
1276 return DefWindowProcW(hWnd, msg, wParam, lParam);
1278 static const char * const edit_messages[] = {
1307 "EM_SETPASSWORDCHAR",
1308 "EM_EMPTYUNDOBUFFER",
1309 "EM_GETFIRSTVISIBLELINE",
1311 "EM_SETWORDBREAKPROC",
1312 "EM_GETWORDBREAKPROC",
1313 "EM_GETPASSWORDCHAR",
1321 static const char * const richedit_messages[] = {
1326 "EM_EXLINEFROMCHAR",
1332 "EM_GETOLEINTERFACE",
1342 "EM_SETOLECALLBACK",
1344 "EM_SETTARGETDEVICE",
1352 "EM_GETWORDBREAKPROCEX",
1353 "EM_SETWORDBREAKPROCEX",
1355 "EM_UNKNOWN_USER_83",
1360 "EM_STOPGROUPTYPING",
1364 "EM_GETAUTOURLDETECT",
1367 "EM_GETTEXTLENGTHEX",
1370 "EM_UNKNOWN_USER_98",
1371 "EM_UNKNOWN_USER_99",
1372 "EM_SETPUNCTUATION",
1373 "EM_GETPUNCTUATION",
1374 "EM_SETWORDWRAPMODE",
1375 "EM_GETWORDWRAPMODE",
1381 "EM_UNKNOWN_USER_109",
1382 "EM_UNKNOWN_USER_110",
1383 "EM_UNKNOWN_USER_111",
1384 "EM_UNKNOWN_USER_112",
1385 "EM_UNKNOWN_USER_113",
1386 "EM_UNKNOWN_USER_114",
1387 "EM_UNKNOWN_USER_115",
1388 "EM_UNKNOWN_USER_116",
1389 "EM_UNKNOWN_USER_117",
1390 "EM_UNKNOWN_USER_118",
1391 "EM_UNKNOWN_USER_119",
1392 "EM_SETLANGOPTIONS",
1393 "EM_GETLANGOPTIONS",
1394 "EM_GETIMECOMPMODE",
1398 "EM_SETIMEMODEBIAS",
1403 get_msg_name(UINT msg)
1405 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1406 return edit_messages[msg - EM_GETSEL];
1407 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1408 return richedit_messages[msg - EM_CANPASTE];
1412 /******************************************************************
1413 * RichEditANSIWndProc (RICHED20.10)
1415 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1416 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1418 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1419 hWnd, msg, get_msg_name(msg), wParam, lParam);
1421 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1422 ERR("RichEditANSIWndProc called with invalid hWnd %p - application bug?\n", hWnd);
1428 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1429 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1430 UNSUPPORTED_MSG(EM_FMTLINES)
1431 UNSUPPORTED_MSG(EM_FORMATRANGE)
1432 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1433 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1434 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1435 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1436 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1437 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1438 UNSUPPORTED_MSG(EM_GETREDONAME)
1439 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1440 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1441 UNSUPPORTED_MSG(EM_GETUNDONAME)
1442 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1443 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1444 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1445 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1446 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1447 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1448 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1449 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1450 UNSUPPORTED_MSG(EM_SETPALETTE)
1451 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1452 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1453 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1454 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1455 UNSUPPORTED_MSG(WM_STYLECHANGING)
1456 UNSUPPORTED_MSG(WM_STYLECHANGED)
1457 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1459 /* Messages specific to Richedit controls */
1462 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1464 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1467 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1468 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1470 int vk = (int)((LPMSG)lParam)->wParam;
1471 /* if style says we want return key */
1472 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1474 code |= DLGC_WANTMESSAGE;
1476 /* we always handle ctrl-tab */
1477 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1479 code |= DLGC_WANTMESSAGE;
1486 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1487 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1488 editor = ME_MakeEditor(hWnd);
1489 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1490 pcs = 0; /* ignore */
1493 case EM_EMPTYUNDOBUFFER:
1494 ME_EmptyUndoStack(editor);
1498 /* Note: wParam/lParam can be NULL */
1500 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1501 PUINT pto = lParam ? (PUINT)lParam : &to;
1502 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1503 if ((*pfrom|*pto) & 0xFFFF0000)
1505 return MAKELONG(*pfrom,*pto);
1509 CHARRANGE *pRange = (CHARRANGE *)lParam;
1510 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1511 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1514 case EM_SETUNDOLIMIT:
1516 if ((int)wParam < 0)
1517 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1519 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1520 /* Setting a max stack size keeps wine from getting killed
1521 for hogging memory. Windows allocates all this memory at once, so
1522 no program would realistically set a value above our maxiumum. */
1523 return editor->nUndoLimit;
1526 return editor->pUndoStack != NULL;
1528 return editor->pRedoStack != NULL;
1529 case WM_UNDO: /* FIXME: actually not the same */
1538 /* these flags are equivalent to the ES_* counterparts */
1539 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1540 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1541 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1547 /* these flags are equivalent to ES_* counterparts
1548 * ECO_READONLY is already implemented in the code, only requires
1549 * setting the bit to work
1551 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1552 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1553 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1554 DWORD settings = mask & raw;
1570 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1572 if (lParam & ECO_AUTOWORDSELECTION)
1573 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1574 if (lParam & ECO_SELECTIONBAR)
1575 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1576 if (lParam & ECO_VERTICAL)
1577 FIXME("ECO_VERTICAL not implemented yet!\n");
1578 if (lParam & ECO_AUTOHSCROLL)
1579 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1580 if (lParam & ECO_AUTOVSCROLL)
1581 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1582 if (lParam & ECO_NOHIDESEL)
1583 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1584 if (lParam & ECO_WANTRETURN)
1585 FIXME("ECO_WANTRETURN not implemented yet!\n");
1591 ME_InvalidateSelection(editor);
1592 ME_SetSelection(editor, wParam, lParam);
1593 ME_InvalidateSelection(editor);
1594 ME_SendSelChange(editor);
1597 case EM_SETSCROLLPOS:
1599 POINT *point = (POINT *)lParam;
1600 ME_ScrollAbs(editor, point->y);
1603 case EM_AUTOURLDETECT:
1605 if (wParam==1 || wParam ==0)
1607 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1610 return E_INVALIDARG;
1612 case EM_GETAUTOURLDETECT:
1614 return editor->AutoURLDetect_bEnable;
1619 CHARRANGE range = *(CHARRANGE *)lParam;
1621 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1623 ME_InvalidateSelection(editor);
1624 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1625 ME_InvalidateSelection(editor);
1626 ME_SendSelChange(editor);
1630 case EM_SHOWSCROLLBAR:
1632 ShowScrollBar(editor->hWnd, wParam, lParam);
1637 LPWSTR wszText = (LPWSTR)lParam;
1638 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1639 size_t len = wszText ? lstrlenW(wszText) : 0;
1642 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1643 if (pStruct->codepage != 1200) {
1644 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1647 /* FIXME: this should support RTF strings too, according to MSDN */
1648 if (pStruct->flags & ST_SELECTION) {
1649 ME_GetSelection(editor, &from, &to);
1650 style = ME_GetSelectionInsertStyle(editor);
1651 ME_InternalDeleteText(editor, from, to - from);
1652 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1653 ME_ReleaseStyle(style);
1656 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1657 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1660 ME_CommitUndo(editor);
1661 if (!(pStruct->flags & ST_KEEPUNDO))
1662 ME_EmptyUndoStack(editor);
1663 ME_UpdateRepaint(editor);
1666 case EM_SETBKGNDCOLOR:
1668 LRESULT lColor = ME_GetBackColor(editor);
1669 if (editor->rgbBackColor != -1)
1670 DeleteObject(editor->hbrBackground);
1673 editor->rgbBackColor = -1;
1674 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1678 editor->rgbBackColor = lParam;
1679 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1681 if (editor->bRedraw)
1683 InvalidateRect(hWnd, NULL, TRUE);
1689 return editor->nModifyStep == 0 ? 0 : 1;
1693 editor->nModifyStep = 0x80000000;
1695 editor->nModifyStep = 0;
1699 case EM_SETREADONLY:
1701 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1703 nStyle |= ES_READONLY;
1705 nStyle &= ~ES_READONLY;
1706 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1709 case EM_SETEVENTMASK:
1711 DWORD nOldMask = editor->nEventMask;
1713 editor->nEventMask = lParam;
1716 case EM_GETEVENTMASK:
1717 return editor->nEventMask;
1718 case EM_SETCHARFORMAT:
1720 CHARFORMAT2W buf, *p;
1721 BOOL bRepaint = TRUE;
1722 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1723 if (!wParam || (editor->mode & TM_PLAINTEXT))
1724 ME_SetDefaultCharFormat(editor, p);
1725 else if (wParam == (SCF_WORD | SCF_SELECTION))
1726 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1727 else if (wParam == SCF_ALL)
1728 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1731 ME_GetSelection(editor, &from, &to);
1732 bRepaint = (from != to);
1733 ME_SetSelectionCharFormat(editor, p);
1735 ME_CommitUndo(editor);
1737 ME_RewrapRepaint(editor);
1740 case EM_GETCHARFORMAT:
1742 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1743 if (dst->cbSize != sizeof(CHARFORMATA) &&
1744 dst->cbSize != sizeof(CHARFORMATW) &&
1745 dst->cbSize != sizeof(CHARFORMAT2A) &&
1746 dst->cbSize != sizeof(CHARFORMAT2W))
1748 tmp.cbSize = sizeof(tmp);
1750 ME_GetDefaultCharFormat(editor, &tmp);
1752 ME_GetSelectionCharFormat(editor, &tmp);
1753 ME_CopyToCFAny(dst, &tmp);
1756 case EM_SETPARAFORMAT:
1757 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1758 ME_RewrapRepaint(editor);
1759 ME_CommitUndo(editor);
1761 case EM_GETPARAFORMAT:
1762 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1764 case EM_GETFIRSTVISIBLELINE:
1766 ME_DisplayItem *p = editor->pBuffer->pFirst;
1767 int y = ME_GetYScrollPos(editor);
1772 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1773 if (p->type == diTextEnd)
1775 if (p->type == diParagraph) {
1776 ypara = p->member.para.nYPos;
1779 ystart = ypara + p->member.row.nYPos;
1780 yend = ystart + p->member.row.nHeight;
1788 case EM_HIDESELECTION:
1790 editor->bHideSelection = (wParam != 0);
1791 ME_InvalidateSelection(editor);
1796 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1797 return TRUE; /* Should return false if a single line richedit control */
1802 ME_GetSelection(editor, &from, &to);
1803 ME_InternalDeleteText(editor, from, to-from);
1804 ME_CommitUndo(editor);
1805 ME_UpdateRepaint(editor);
1812 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1813 size_t len = lstrlenW(wszText);
1814 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1816 ME_GetSelection(editor, &from, &to);
1817 style = ME_GetSelectionInsertStyle(editor);
1818 ME_InternalDeleteText(editor, from, to-from);
1819 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1820 ME_ReleaseStyle(style);
1821 /* drop temporary style if line end */
1823 * FIXME question: does abc\n mean: put abc,
1824 * clear temp style, put \n? (would require a change)
1826 if (len>0 && wszText[len-1] == '\n')
1827 ME_ClearTempStyle(editor);
1828 ME_EndToUnicode(hWnd, wszText);
1829 ME_CommitUndo(editor);
1831 ME_EmptyUndoStack(editor);
1832 ME_UpdateRepaint(editor);
1835 case EM_SCROLLCARET:
1837 int top, bottom; /* row's edges relative to document top */
1839 ME_DisplayItem *para, *row;
1841 nPos = ME_GetYScrollPos(editor);
1842 row = ME_RowStart(editor->pCursors[0].pRun);
1843 para = ME_GetParagraph(row);
1844 top = para->member.para.nYPos + row->member.row.nYPos;
1845 bottom = top + row->member.row.nHeight;
1847 if (top < nPos) /* caret above window */
1848 ME_ScrollAbs(editor, top);
1849 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1850 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1858 BOOL bRepaint = LOWORD(lParam);
1861 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1862 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1864 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1865 ReleaseDC(hWnd, hDC);
1866 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1867 ME_SetDefaultCharFormat(editor, &fmt);
1869 ME_CommitUndo(editor);
1871 ME_RewrapRepaint(editor);
1876 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1879 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1880 if (!IsWindowUnicode(hWnd) && !strncmp((char *)lParam, "{\\rtf", 5))
1882 /* Undocumented: WM_SETTEXT supports RTF text */
1883 ME_StreamInRTFString(editor, 0, (char *)lParam);
1887 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1888 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1889 if (lstrlenW(wszText) > 0)
1891 /* uses default style! */
1892 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1894 ME_EndToUnicode(hWnd, wszText);
1898 TRACE("WM_SETTEXT - NULL\n");
1899 ME_CommitUndo(editor);
1900 ME_EmptyUndoStack(editor);
1901 ME_SetSelection(editor, 0, 0);
1902 ME_UpdateRepaint(editor);
1907 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1908 if (IsClipboardFormatAvailable(nRTFFormat))
1910 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1918 ME_GlobalDestStruct gds;
1919 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1922 if (IsClipboardFormatAvailable(nRTFFormat))
1923 cf = nRTFFormat, dwFormat = SF_RTF;
1924 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1925 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1929 if (!OpenClipboard(hWnd))
1931 gds.hData = GetClipboardData(cf);
1933 es.dwCookie = (DWORD)&gds;
1934 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1935 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1943 LPDATAOBJECT dataObj = NULL;
1947 if (editor->cPasswordMask)
1948 return 0; /* Copying or Cutting masked text isn't allowed */
1950 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1951 if(editor->lpOleCallback)
1952 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1953 if(FAILED(hr) || !dataObj)
1954 hr = ME_GetDataObject(editor, &range, &dataObj);
1956 hr = OleSetClipboard(dataObj);
1957 IDataObject_Release(dataObj);
1959 if (SUCCEEDED(hr) && msg == WM_CUT)
1961 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1962 ME_CommitUndo(editor);
1963 ME_UpdateRepaint(editor);
1967 case WM_GETTEXTLENGTH:
1968 return ME_GetTextLength(editor);
1969 case EM_GETTEXTLENGTHEX:
1970 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1973 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1975 tr.chrg.cpMax = wParam-1;
1976 tr.lpstrText = (WCHAR *)lParam;
1977 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1981 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1984 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1985 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1987 if (ex->flags & GT_SELECTION)
1989 ME_GetSelection(editor, &nStart, &nCount);
1991 nCount = min(nCount, ex->cb - 1);
1996 nCount = ex->cb - 1;
1998 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
2000 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2001 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2005 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2006 we can just take a bigger buffer? :) */
2007 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2008 LPWSTR buffer = richedit_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2009 DWORD buflen = ex->cb;
2013 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2014 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2016 richedit_free(buffer);
2023 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2024 ME_GetSelection(editor, &from, &to);
2025 tr.chrg.cpMin = from;
2027 tr.lpstrText = (WCHAR *)lParam;
2028 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2030 case EM_GETSCROLLPOS:
2032 POINT *point = (POINT *)lParam;
2033 point->x = 0; /* FIXME side scrolling not implemented */
2034 point->y = ME_GetYScrollPos(editor);
2037 case EM_GETTEXTRANGE:
2039 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2040 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2041 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
2042 editor->bEmulateVersion10, ME_GetTextLength(editor));
2043 if (IsWindowUnicode(hWnd))
2044 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2047 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2048 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2049 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2050 /* FIXME this is a potential security hole (buffer overrun)
2051 if you know more about wchar->mbyte conversion please explain
2053 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2060 ME_DisplayItem *run;
2061 const BOOL bUnicode = IsWindowUnicode(hWnd);
2062 const unsigned int nMaxChars = *(WORD *) lParam;
2063 unsigned int nEndChars, nCharsLeft = nMaxChars;
2064 char *dest = (char *) lParam;
2065 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2066 we need to know how if we have the extra \n or not */
2067 int nLF = editor->bEmulateVersion10;
2069 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2070 bUnicode ? "Unicode" : "Ansi");
2072 run = ME_FindRowWithNumber(editor, wParam);
2076 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2077 && !(run->member.run.nFlags & MERF_ENDPARA))
2081 if (run->type != diRun)
2083 strText = run->member.run.strText;
2084 nCopy = min(nCharsLeft, strText->nLen);
2087 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2089 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2090 nCharsLeft, NULL, NULL);
2091 dest += nCopy * (bUnicode ? sizeof(WCHAR) : 1);
2092 nCharsLeft -= nCopy;
2095 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2096 nEndChars = min(nCharsLeft, 2 + nLF);
2097 nCharsLeft -= nEndChars;
2100 const WCHAR src[] = {'\r', '\0'};
2101 const WCHAR src10[] = {'\r', '\n', '\0'};
2102 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2105 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2107 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2109 if (nEndChars == 2 + nLF)
2110 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2112 return nMaxChars - nCharsLeft;
2114 case EM_GETLINECOUNT:
2116 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2119 while (item != editor->pBuffer->pLast)
2121 assert(item->type == diParagraph);
2122 nRows += item->member.para.nRows;
2123 item = item->member.para.next_para;
2125 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2126 return max(1, nRows);
2128 case EM_LINEFROMCHAR:
2131 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2133 return ME_RowNumberFromCharOfs(editor, wParam);
2135 case EM_EXLINEFROMCHAR:
2138 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2140 return ME_RowNumberFromCharOfs(editor, lParam);
2144 ME_DisplayItem *item, *para;
2148 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2150 item = ME_FindRowWithNumber(editor, wParam);
2153 para = ME_GetParagraph(item);
2154 item = ME_FindItemFwd(item, diRun);
2155 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2156 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2161 ME_DisplayItem *item, *item_end;
2162 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2164 if (wParam > ME_GetTextLength(editor))
2168 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2171 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2172 item = ME_RowStart(item);
2173 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2174 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2175 if (item_end->type == diStartRow)
2176 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2178 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2179 - (editor->bEmulateVersion10?2:1);
2180 nChars = nNextLineOfs - nThisLineOfs;
2181 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2184 case EM_EXLIMITTEXT:
2186 if ((int)lParam < 0)
2189 editor->nTextLimit = 65536;
2191 editor->nTextLimit = (int) lParam;
2194 case EM_GETLIMITTEXT:
2196 return editor->nTextLimit;
2200 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2201 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2205 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2206 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2207 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2213 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2214 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2218 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2219 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2220 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2226 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2227 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2229 case EM_FINDTEXTEXW:
2231 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2232 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2235 if (!wParam || !lParam)
2237 *(int *)wParam = editor->nZoomNumerator;
2238 *(int *)lParam = editor->nZoomDenominator;
2241 return ME_SetZoom(editor, wParam, lParam);
2242 case EM_CHARFROMPOS:
2243 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2244 case EM_POSFROMCHAR:
2246 ME_DisplayItem *pRun;
2247 int nCharOfs, nOffset, nLength;
2251 /* detect which API version we're dealing with */
2252 if (wParam >= 0x40000)
2254 nLength = ME_GetTextLength(editor);
2256 if (nCharOfs < nLength) {
2257 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2258 assert(pRun->type == diRun);
2259 pt.y = pRun->member.run.pt.y;
2260 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2261 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2264 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2266 if (wParam >= 0x40000) {
2267 *(POINTL *)wParam = pt;
2269 return MAKELONG( pt.x, pt.y );
2272 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2273 { /* Squelch the default horizontal scrollbar it would make */
2274 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2276 ME_CommitUndo(editor);
2277 ME_WrapMarkedParagraphs(editor);
2278 ME_MoveCaret(editor);
2281 ME_DestroyEditor(editor);
2282 SetWindowLongPtrW(hWnd, 0, 0);
2284 case WM_LBUTTONDOWN:
2286 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2288 ME_LinkNotify(editor,msg,wParam,lParam);
2291 if (GetCapture() == hWnd)
2292 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2293 ME_LinkNotify(editor,msg,wParam,lParam);
2296 if (GetCapture() == hWnd)
2298 ME_LinkNotify(editor,msg,wParam,lParam);
2300 case WM_LBUTTONDBLCLK:
2301 ME_LinkNotify(editor,msg,wParam,lParam);
2302 ME_SelectWord(editor);
2304 case WM_CONTEXTMENU:
2305 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2309 if (editor->bRedraw)
2314 hDC = BeginPaint(hWnd, &ps);
2315 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2316 EndPaint(hWnd, &ps);
2320 ME_ShowCaret(editor);
2321 ME_SendOldNotify(editor, EN_SETFOCUS);
2324 ME_HideCaret(editor);
2325 ME_SendOldNotify(editor, EN_KILLFOCUS);
2329 if (editor->bRedraw)
2331 HDC hDC = (HDC)wParam;
2333 if (GetUpdateRect(hWnd,&rc,TRUE))
2335 FillRect(hDC, &rc, editor->hbrBackground);
2341 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2344 if (ME_KeyDown(editor, LOWORD(wParam)))
2349 WCHAR wstr = LOWORD(wParam);
2350 if (editor->AutoURLDetect_bEnable)
2351 ME_AutoURLDetect(editor, wstr);
2355 case 1: /* Ctrl-A */
2356 ME_SetSelection(editor, 0, -1);
2358 case 3: /* Ctrl-C */
2359 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2363 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2364 MessageBeep(MB_ICONERROR);
2365 return 0; /* FIXME really 0 ? */
2370 case 22: /* Ctrl-V */
2371 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2373 case 24: /* Ctrl-X */
2374 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2376 case 25: /* Ctrl-Y */
2377 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2379 case 26: /* Ctrl-Z */
2380 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2383 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2384 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2385 ME_Style *style = ME_GetInsertStyle(editor, 0);
2386 ME_SaveTempStyle(editor);
2387 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2388 ME_ReleaseStyle(style);
2389 ME_CommitUndo(editor);
2390 ME_UpdateRepaint(editor);
2394 case EM_SCROLL: /* fall through */
2400 origNPos = ME_GetYScrollPos(editor);
2403 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2404 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2405 if (lineHeight <= 0) lineHeight = 24;
2407 switch(LOWORD(wParam))
2410 ME_ScrollUp(editor,lineHeight);
2413 ME_ScrollDown(editor,lineHeight);
2416 ME_ScrollUp(editor,editor->sizeWindow.cy);
2419 ME_ScrollDown(editor,editor->sizeWindow.cy);
2422 case SB_THUMBPOSITION:
2423 ME_ScrollAbs(editor,HIWORD(wParam));
2426 if (msg == EM_SCROLL)
2427 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2433 UINT pulScrollLines;
2435 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2436 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2438 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2440 /* FIXME follow the original */
2441 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2447 *((RECT *)lParam) = editor->rcFormat;
2455 RECT *rc = (RECT *)lParam;
2459 editor->rcFormat.left += rc->left;
2460 editor->rcFormat.top += rc->top;
2461 editor->rcFormat.right += rc->right;
2462 editor->rcFormat.bottom += rc->bottom;
2466 editor->rcFormat = *rc;
2471 GetClientRect(hWnd, &editor->rcFormat);
2473 if (msg != EM_SETRECTNP)
2474 ME_RewrapRepaint(editor);
2477 case EM_REQUESTRESIZE:
2478 ME_SendRequestResize(editor, TRUE);
2481 editor->bRedraw = wParam;
2485 GetClientRect(hWnd, &editor->rcFormat);
2486 ME_RewrapRepaint(editor);
2487 return DefWindowProcW(hWnd, msg, wParam, lParam);
2489 case EM_GETOLEINTERFACE:
2491 LPVOID *ppvObj = (LPVOID*) lParam;
2492 return CreateIRichEditOle(editor, ppvObj);
2494 case EM_GETPASSWORDCHAR:
2496 return editor->cPasswordMask;
2498 case EM_SETOLECALLBACK:
2499 if(editor->lpOleCallback)
2500 IUnknown_Release(editor->lpOleCallback);
2501 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2502 if(editor->lpOleCallback)
2503 IUnknown_AddRef(editor->lpOleCallback);
2505 case EM_GETWORDBREAKPROC:
2506 return (LRESULT)editor->pfnWordBreak;
2507 case EM_SETWORDBREAKPROC:
2509 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2511 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2512 return (LRESULT)pfnOld;
2514 case EM_SETTEXTMODE:
2519 if ((ret = RichEditANSIWndProc(hWnd, WM_GETTEXTLENGTH, 0, 0)) == 0)
2521 /*Check for valid wParam*/
2522 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2523 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2524 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2528 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2530 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2531 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2533 /*FIXME: Currently no support for undo level and code page options*/
2534 editor->mode = (editor->mode & (~mask)) | changes;
2540 case EM_SETPASSWORDCHAR:
2542 editor->cPasswordMask = wParam;
2543 ME_RewrapRepaint(editor);
2548 return DefWindowProcW(hWnd, msg, wParam, lParam);
2554 /******************************************************************
2555 * RichEdit10ANSIWndProc (RICHED20.9)
2557 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2561 /* FIXME: this is NOT the same as 2.0 version */
2562 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2563 if (msg == WM_NCCREATE)
2565 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2567 editor->bEmulateVersion10 = TRUE;
2568 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2573 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2575 HWND hWnd = editor->hWnd;
2576 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2579 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2582 ME_Cursor tmpCursor;
2586 x = (short)LOWORD(lParam);
2587 y = (short)HIWORD(lParam);
2588 ME_FindPixelPos(editor, x, y, &tmpCursor, &bNothing);
2589 tmpRun = &tmpCursor.pRun->member.run;
2591 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2592 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2593 { /* The clicked run has CFE_LINK set */
2594 info.nmhdr.hwndFrom = editor->hWnd;
2595 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2596 info.nmhdr.code = EN_LINK;
2598 info.wParam = wParam;
2599 info.lParam = lParam;
2600 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2601 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2602 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2606 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2608 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2611 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2612 item = item->member.para.next_para;
2615 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2616 item = item->member.para.next_para;
2623 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2625 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2627 WCHAR *pStart = buffer;
2636 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2639 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2646 item = ME_FindItemFwd(item, diRun);
2649 while(nChars && item)
2651 int nLen = ME_StrLen(item->member.run.strText);
2655 if (item->member.run.nFlags & MERF_ENDPARA)
2657 if (!ME_FindItemFwd(item, diRun))
2658 /* No '\r' is appended to the last paragraph. */
2668 /* our end paragraph consists of 2 characters now */
2669 if (editor->bEmulateVersion10)
2674 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2681 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2685 item = ME_FindItemFwd(item, diRun);
2688 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2692 void ME_RegisterEditorClass(HINSTANCE hInstance)
2698 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2699 wcW.lpfnWndProc = RichEditANSIWndProc;
2701 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2702 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2704 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2705 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2706 wcW.lpszMenuName = NULL;
2707 wcW.lpszClassName = wszClassName;
2708 bResult = RegisterClassW(&wcW);
2710 wcW.lpszClassName = wszClassName50;
2711 bResult = RegisterClassW(&wcW);
2714 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2715 wcA.lpfnWndProc = RichEditANSIWndProc;
2717 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2718 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2720 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2721 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2722 wcA.lpszMenuName = NULL;
2723 wcA.lpszClassName = "RichEdit20A";
2724 bResult = RegisterClassA(&wcA);
2726 wcA.lpszClassName = "RichEdit50A";
2727 bResult = RegisterClassA(&wcA);
2731 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2732 /* FIXME: Not implemented */
2733 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2734 hWnd, msg, get_msg_name(msg), wParam, lParam);
2735 return DefWindowProcW(hWnd, msg, wParam, lParam);
2738 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2739 /* FIXME: Not implemented */
2740 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2741 hWnd, msg, get_msg_name(msg), wParam, lParam);
2742 return DefWindowProcW(hWnd, msg, wParam, lParam);
2745 /******************************************************************
2746 * REExtendedRegisterClass (RICHED20.8)
2748 * FIXME undocumented
2749 * Need to check for errors and implement controls and callbacks
2751 LRESULT WINAPI REExtendedRegisterClass(void)
2756 FIXME("semi stub\n");
2760 wcW.hInstance = NULL;
2763 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2764 wcW.lpszMenuName = NULL;
2766 if (!ME_ListBoxRegistered)
2768 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2769 wcW.lpfnWndProc = REListWndProc;
2770 wcW.lpszClassName = wszClassNameListBox;
2771 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2774 if (!ME_ComboBoxRegistered)
2776 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2777 wcW.lpfnWndProc = REComboWndProc;
2778 wcW.lpszClassName = wszClassNameComboBox;
2779 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2783 if (ME_ListBoxRegistered)
2785 if (ME_ComboBoxRegistered)
2791 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2811 int curf_ef, link_ef, def_ef;
2812 int cur_prefx, prefx_cnt;
2813 int sel_min, sel_max;
2816 int URLmin, URLmax = 0;
2819 CHARFORMAT2W cur_format;
2820 CHARFORMAT2W default_format;
2822 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2823 sel_min = ins_pt.cpMin;
2824 sel_max = ins_pt.cpMax;
2825 if (sel_min==sel_max)
2827 if (sel_min!=sel_max)
2828 car_pos = ME_GetTextLength(editor)+1;
2829 cur_format.cbSize = sizeof(cur_format);
2830 default_format.cbSize = sizeof(default_format);
2831 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2832 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2833 link.cbSize = sizeof(link);
2834 link.dwMask = CFM_LINK;
2835 link.dwEffects = CFE_LINK;
2836 curf_ef = cur_format.dwEffects & link.dwEffects;
2837 def_ef = default_format.dwEffects & link.dwEffects;
2838 link_ef = link.dwEffects & link.dwEffects;
2839 if (curf_ef == link_ef)
2841 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2843 ME_SetSelection(editor, car_pos, car_pos);
2844 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2849 if (curf_ef == def_ef)
2852 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2853 while (cur_prefx<=prefx_cnt)
2857 ft.lpstrText = prefixes[cur_prefx].text;
2858 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2859 URLmax=max(0, car_pos);
2860 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2862 URLmax = ME_GetTextLength(editor)+1;
2864 ft.chrg.cpMin = URLmin;
2865 ft.chrg.cpMax = URLmax;
2866 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2872 url.cpMax=car_pos-1;
2873 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2874 ME_RewrapRepaint(editor);