2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 API implementation status:
26 Messages (ANSI versions not done yet)
27 + EM_AUTOURLDETECT 2.0
38 + EM_FINDTEXT (only FR_DOWN flag implemented)
39 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
43 + EM_GETAUTOURLDETECT 2.0
44 - EM_GETBIDIOPTIONS 3.0
45 - EM_GETCHARFORMAT (partly done)
48 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
49 - EM_GETIMECOLOR 1.0asian
50 - EM_GETIMECOMPMODE 2.0
51 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETLANGOPTIONS 2.0
56 + EM_GETLINECOUNT returns number of rows, not of paragraphs
61 + EM_GETPASSWORDCHAR 2.0
62 - EM_GETPUNCTUATION 1.0asian
66 + EM_GETSELTEXT (ANSI&Unicode)
67 + EM_GETSCROLLPOS 3.0 (only Y value valid)
70 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 ? + EM_GETTEXTRANGE (ANSI&Unicode)
73 - EM_GETTYPOGRAPHYOPTIONS 3.0
76 - EM_GETWORDBREAKPROCEX
77 - EM_GETWORDWRAPMODE 1.0asian
80 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
89 + EM_REPLACESEL (proper style?) ANSI&Unicode
93 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETCHARFORMAT (partly done, no ANSI)
97 + EM_SETEVENTMASK (few notifications supported)
99 - EM_SETIMECOLOR 1.0asian
100 - EM_SETIMEOPTIONS 1.0asian
101 - EM_SETLANGOPTIONS 2.0
103 + EM_SETMODIFY (not sure if implementation is correct)
105 + EM_SETOPTIONS (partially implemented)
108 + EM_SETPASSWORDCHAR 2.0
109 - EM_SETPUNCTUATION 1.0asian
110 + EM_SETREADONLY no beep on modification attempt
112 + EM_SETRECTNP (EM_SETRECT without repainting)
114 + EM_SETSCROLLPOS 3.0
117 + EM_SETTEXTEX 3.0 (no rich text insertion handling, proper style?)
119 - EM_SETTYPOGRAPHYOPTIONS 3.0
120 + EM_SETUNDOLIMIT 2.0
121 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
122 - EM_SETWORDBREAKPROCEX
123 - EM_SETWORDWRAPMODE 1.0asian
125 + EM_SHOWSCROLLBAR 2.0
126 - EM_STOPGROUPTYPING 2.0
134 + WM_GETDLGCODE (the current implementation is incomplete)
135 + WM_GETTEXT (ANSI&Unicode)
136 + WM_GETTEXTLENGTH (ANSI version sucks)
139 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
141 - WM_STYLECHANGED (things like read-only flag)
146 * EN_CHANGE (sent from the wrong place)
163 * EN_UPDATE (sent from the wrong place)
171 + ES_DISABLENOSCROLL (scrollbar is always visible)
172 - ES_EX_NOCALLOLEINIT
174 - ES_MULTILINE (currently single line controls aren't supported)
176 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
182 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
189 * RICHED20 TODO (incomplete):
191 * - messages/styles/notifications listed above
193 * - add remaining CHARFORMAT/PARAFORMAT fields
194 * - right/center align should strip spaces from the beginning
195 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
196 * - COM interface (looks like a major pain in the TODO list)
197 * - calculate heights of pictures (half-done)
198 * - horizontal scrolling (not even started)
199 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
201 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
202 * - italic caret with italic fonts
204 * - most notifications aren't sent at all (the most important ones are)
205 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
206 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
207 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
208 * - full justification
211 * - ListBox & ComboBox not implemented
213 * Bugs that are probably fixed, but not so easy to verify:
214 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
215 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
216 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
217 * - caret shouldn't be displayed when selection isn't empty
218 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
219 * - undo for setting default format (done, might be buggy)
220 * - styles might be not released properly (looks like they work like charm, but who knows?
227 #define NO_SHLWAPI_STREAM
233 #define STACK_SIZE_DEFAULT 100
234 #define STACK_SIZE_MAX 1000
236 #define TEXT_LIMIT_DEFAULT 32767
238 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
240 static BOOL ME_RegisterEditorClass(HINSTANCE);
242 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
243 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
244 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
245 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
246 static HCURSOR hLeft;
247 static HCURSOR hBeam;
250 HANDLE me_heap = NULL;
252 static BOOL ME_ListBoxRegistered = FALSE;
253 static BOOL ME_ComboBoxRegistered = FALSE;
255 static inline int is_version_nt(void)
257 return !(GetVersion() & 0x80000000);
260 static ME_TextBuffer *ME_MakeText(void) {
262 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
264 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
265 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
271 p1->member.para.next_para = p2;
272 p2->member.para.prev_para = p1;
273 p2->member.para.nCharOfs = 0;
277 buf->pCharStyle = NULL;
283 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
285 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
288 TRACE("%08x %p\n", dwFormat, stream);
295 ME_StreamInFill(stream);
296 if (stream->editstream->dwError)
302 if (!(dwFormat & SF_UNICODE))
304 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
305 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
310 nWideChars = stream->dwSize >> 1;
311 pText = (WCHAR *)stream->buffer;
314 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
315 if (stream->dwSize == 0)
319 ME_CommitUndo(editor);
320 ME_UpdateRepaint(editor);
324 static void ME_RTFCharAttrHook(RTF_Info *info)
327 fmt.cbSize = sizeof(fmt);
331 switch(info->rtfMinor)
334 /* FIXME add more flags once they're implemented */
335 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
336 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
337 fmt.yHeight = 12*20; /* 12pt */
341 fmt.dwMask = CFM_BOLD;
342 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
345 fmt.dwMask = CFM_ITALIC;
346 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
349 fmt.dwMask = CFM_UNDERLINE;
350 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
351 fmt.bUnderlineType = CFU_CF1UNDERLINE;
354 fmt.dwMask = CFM_UNDERLINE;
358 fmt.dwMask = CFM_STRIKEOUT;
359 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
363 case rtfSubScrShrink:
364 case rtfSuperScrShrink:
366 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
367 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
368 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
369 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
372 fmt.dwMask = CFM_HIDDEN;
373 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
376 fmt.dwMask = CFM_BACKCOLOR;
378 if (info->rtfParam == 0)
379 fmt.dwEffects = CFE_AUTOBACKCOLOR;
380 else if (info->rtfParam != rtfNoParam)
382 RTFColor *c = RTFGetColor(info, info->rtfParam);
383 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
387 fmt.dwMask = CFM_COLOR;
389 if (info->rtfParam == 0)
390 fmt.dwEffects = CFE_AUTOCOLOR;
391 else if (info->rtfParam != rtfNoParam)
393 RTFColor *c = RTFGetColor(info, info->rtfParam);
395 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
401 if (info->rtfParam != rtfNoParam)
403 RTFFont *f = RTFGetFont(info, info->rtfParam);
406 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
407 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
408 fmt.bCharSet = f->rtfFCharSet;
409 fmt.dwMask = CFM_FACE | CFM_CHARSET;
414 fmt.dwMask = CFM_SIZE;
415 if (info->rtfParam != rtfNoParam)
416 fmt.yHeight = info->rtfParam*10;
421 RTFFlushOutputBuffer(info);
422 /* FIXME too slow ? how come ? */
423 style2 = ME_ApplyStyle(info->style, &fmt);
424 ME_ReleaseStyle(info->style);
425 info->style = style2;
426 info->styleChanged = TRUE;
430 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
431 the same tags mean different things in different contexts */
432 static void ME_RTFParAttrHook(RTF_Info *info)
435 fmt.cbSize = sizeof(fmt);
438 switch(info->rtfMinor)
440 case rtfParDef: /* restores default paragraph attributes */
441 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
442 fmt.wAlignment = PFA_LEFT;
444 fmt.dxOffset = fmt.dxStartIndent = 0;
445 RTFFlushOutputBuffer(info);
446 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
450 ME_DisplayItem *para;
452 RTFFlushOutputBuffer(info);
453 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
454 assert(para->member.para.pCells);
455 para->member.para.bTable = TRUE;
459 ME_GetSelectionParaFormat(info->editor, &fmt);
460 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
461 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
462 fmt.dxOffset = -info->rtfParam;
465 ME_GetSelectionParaFormat(info->editor, &fmt);
466 fmt.dwMask = PFM_STARTINDENT;
467 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
470 fmt.dwMask = PFM_RIGHTINDENT;
471 fmt.dxRightIndent = info->rtfParam;
475 fmt.dwMask = PFM_ALIGNMENT;
476 fmt.wAlignment = PFA_LEFT;
479 fmt.dwMask = PFM_ALIGNMENT;
480 fmt.wAlignment = PFA_RIGHT;
483 fmt.dwMask = PFM_ALIGNMENT;
484 fmt.wAlignment = PFA_CENTER;
487 ME_GetSelectionParaFormat(info->editor, &fmt);
488 if (!(fmt.dwMask & PFM_TABSTOPS))
490 fmt.dwMask |= PFM_TABSTOPS;
493 if (fmt.cTabCount < MAX_TAB_STOPS)
494 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
498 RTFFlushOutputBuffer(info);
499 /* FIXME too slow ? how come ?*/
500 ME_SetSelectionParaFormat(info->editor, &fmt);
504 static void ME_RTFTblAttrHook(RTF_Info *info)
506 ME_DisplayItem *para;
508 switch (info->rtfMinor)
511 RTFFlushOutputBuffer(info);
512 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
514 /* Release possibly inherited cell definitions */
515 ME_DestroyTableCellList(para);
517 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
518 para->member.para.pCells->nRightBoundary = 0;
519 para->member.para.pCells->next = NULL;
520 para->member.para.pLastCell = para->member.para.pCells;
523 RTFFlushOutputBuffer(info);
524 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
526 if (para->member.para.pLastCell->nRightBoundary)
528 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
531 para->member.para.pLastCell->next = pCell;
532 para->member.para.pLastCell = pCell;
534 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
539 static void ME_RTFReadHook(RTF_Info *info) {
540 switch(info->rtfClass)
543 switch(info->rtfMajor)
546 if (info->stackTop < maxStack) {
547 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
548 info->stack[info->stackTop].codePage = info->codePage;
549 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
552 info->styleChanged = FALSE;
557 RTFFlushOutputBuffer(info);
558 if (info->stackTop<=1) {
559 info->rtfClass = rtfEOF;
563 assert(info->stackTop >= 0);
564 if (info->styleChanged)
566 /* FIXME too slow ? how come ? */
567 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
568 ME_ReleaseStyle(info->style);
570 info->codePage = info->stack[info->stackTop].codePage;
571 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
578 switch(info->rtfMajor)
581 ME_RTFCharAttrHook(info);
584 ME_RTFParAttrHook(info);
587 ME_RTFTblAttrHook(info);
590 if (info->rtfMinor == rtfCell)
592 RTFFlushOutputBuffer(info);
593 ME_InsertTableCellFromCursor(info->editor, 0);
601 ME_StreamInFill(ME_InStream *stream)
603 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
604 (BYTE *)stream->buffer,
605 sizeof(stream->buffer),
606 (LONG *)&stream->dwSize);
610 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
614 int from, to, to2, nUndoMode;
615 int nEventMask = editor->nEventMask;
616 ME_InStream inStream;
618 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
619 editor->nEventMask = 0;
621 ME_GetSelection(editor, &from, &to);
622 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
623 style = ME_GetSelectionInsertStyle(editor);
625 ME_InternalDeleteText(editor, from, to-from);
628 style = editor->pBuffer->pDefaultStyle;
629 ME_AddRefStyle(style);
630 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
631 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
633 ME_ClearTempStyle(editor);
634 /* FIXME restore default paragraph formatting ! */
638 /* Back up undo mode to a local variable */
639 nUndoMode = editor->nUndoMode;
641 /* Only create an undo if SFF_SELECTION is set */
642 if (!(format & SFF_SELECTION))
643 editor->nUndoMode = umIgnore;
645 inStream.editstream = stream;
646 inStream.editstream->dwError = 0;
652 /* Check if it's really RTF, and if it is not, use plain text */
653 ME_StreamInFill(&inStream);
654 if (!inStream.editstream->dwError)
656 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
664 if (!inStream.editstream->dwError)
666 if (format & SF_RTF) {
667 /* setup the RTF parser */
668 memset(&parser, 0, sizeof parser);
669 RTFSetEditStream(&parser, &inStream);
670 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
671 parser.hwndEdit = editor->hWnd;
672 parser.editor = editor;
673 parser.style = style;
676 RTFSetReadHook(&parser, ME_RTFReadHook);
681 RTFFlushOutputBuffer(&parser);
684 style = parser.style;
686 else if (format & SF_TEXT)
687 ME_StreamInText(editor, format, &inStream, style);
689 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
690 ME_GetSelection(editor, &to, &to2);
691 /* put the cursor at the top */
692 if (!(format & SFF_SELECTION))
693 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
696 /* Restore saved undo mode */
697 editor->nUndoMode = nUndoMode;
699 /* even if we didn't add an undo, we need to commit anything on the stack */
700 ME_CommitUndo(editor);
702 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
703 if (!(format & SFF_SELECTION))
704 ME_EmptyUndoStack(editor);
706 ME_ReleaseStyle(style);
707 editor->nEventMask = nEventMask;
710 ME_UpdateRepaint(editor);
712 if (!(format & SFF_SELECTION)) {
713 ME_ClearTempStyle(editor);
715 ME_MoveCaret(editor);
716 ME_SendSelChange(editor);
717 ME_SendRequestResize(editor, FALSE);
723 typedef struct tagME_RTFStringStreamStruct
728 } ME_RTFStringStreamStruct;
730 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
732 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
735 count = min(cb, pStruct->length - pStruct->pos);
736 memmove(lpBuff, pStruct->string + pStruct->pos, count);
737 pStruct->pos += count;
743 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
746 ME_RTFStringStreamStruct data;
748 data.string = string;
749 data.length = strlen(string);
751 es.dwCookie = (DWORD)&data;
752 es.pfnCallback = ME_ReadFromRTFString;
753 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
758 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
760 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
762 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
763 item = ME_FindItemFwd(item, diParagraph);
768 nOffset -= item->member.para.nCharOfs;
769 if (nItemType == diParagraph) {
771 *nItemOffset = nOffset;
776 item = ME_FindItemFwd(item, diRun);
777 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
779 nOffset -= item->member.run.nCharOfs;
781 *nItemOffset = nOffset;
788 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
790 const int nLen = lstrlenW(text);
791 const int nTextLen = ME_GetTextLength(editor);
794 ME_DisplayItem *item;
795 ME_DisplayItem *para;
796 WCHAR wLastChar = ' ';
798 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
799 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
801 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
802 FIXME("Flags 0x%08x not implemented\n",
803 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
806 if (chrg->cpMax == -1)
809 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
811 /* when searching up, if cpMin < cpMax, then instead of searching
812 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
813 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
814 * case, it is always bigger than cpMin.
816 if (!(flags & FR_DOWN))
820 nMax = nMin > nTextLen ? nTextLen : nMin;
821 if (nMin < nSwap || chrg->cpMax == -1)
827 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
830 chrgText->cpMin = chrgText->cpMax = -1;
834 if (flags & FR_DOWN) /* Forward search */
836 /* If possible, find the character before where the search starts */
837 if ((flags & FR_WHOLEWORD) && nMin)
840 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
844 chrgText->cpMin = chrgText->cpMax = -1;
847 wLastChar = item->member.run.strText->szData[nStart];
851 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
855 chrgText->cpMin = chrgText->cpMax = -1;
859 para = ME_GetParagraph(item);
861 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
863 ME_DisplayItem *pCurItem = item;
864 int nCurStart = nStart;
867 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
869 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
873 if (nMatched == nLen)
875 ME_DisplayItem *pNextItem = pCurItem;
876 int nNextStart = nCurStart;
879 /* Check to see if next character is a whitespace */
880 if (flags & FR_WHOLEWORD)
882 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
884 pNextItem = ME_FindItemFwd(pCurItem, diRun);
885 nNextStart = -nMatched;
889 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
893 if (isalnumW(wNextChar))
897 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
900 chrgText->cpMin = nStart;
901 chrgText->cpMax = nStart + nLen;
903 TRACE("found at %d-%d\n", nStart, nStart + nLen);
906 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
908 pCurItem = ME_FindItemFwd(pCurItem, diRun);
909 para = ME_GetParagraph(pCurItem);
910 nCurStart = -nMatched;
914 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
919 if (nStart == ME_StrLen(item->member.run.strText))
921 item = ME_FindItemFwd(item, diRun);
922 para = ME_GetParagraph(item);
927 else /* Backward search */
929 /* If possible, find the character after where the search ends */
930 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
933 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
937 chrgText->cpMin = chrgText->cpMax = -1;
940 wLastChar = item->member.run.strText->szData[nEnd];
944 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
948 chrgText->cpMin = chrgText->cpMax = -1;
952 para = ME_GetParagraph(item);
955 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
957 ME_DisplayItem *pCurItem = item;
961 if (nCurEnd - nMatched == 0)
963 pCurItem = ME_FindItemBack(pCurItem, diRun);
964 para = ME_GetParagraph(pCurItem);
965 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
968 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
970 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
974 if (nMatched == nLen)
976 ME_DisplayItem *pPrevItem = pCurItem;
977 int nPrevEnd = nCurEnd;
980 /* Check to see if previous character is a whitespace */
981 if (flags & FR_WHOLEWORD)
983 if (nPrevEnd - nMatched == 0)
985 pPrevItem = ME_FindItemBack(pCurItem, diRun);
987 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
991 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
995 if (isalnumW(wPrevChar))
999 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1002 chrgText->cpMin = nStart;
1003 chrgText->cpMax = nStart + nLen;
1005 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1008 if (nCurEnd - nMatched == 0)
1010 pCurItem = ME_FindItemBack(pCurItem, diRun);
1011 /* Don't care about pCurItem becoming NULL here; it's already taken
1012 * care of in the exterior loop condition */
1013 para = ME_GetParagraph(pCurItem);
1014 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1018 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1025 item = ME_FindItemBack(item, diRun);
1026 para = ME_GetParagraph(item);
1027 nEnd = ME_StrLen(item->member.run.strText);
1031 TRACE("not found\n");
1033 chrgText->cpMin = chrgText->cpMax = -1;
1037 /* helper to send a msg filter notification */
1039 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1043 msgf.nmhdr.hwndFrom = editor->hWnd;
1044 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1045 msgf.nmhdr.code = EN_MSGFILTER;
1048 msgf.wParam = *wParam;
1049 msgf.lParam = *lParam;
1050 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1052 *wParam = msgf.wParam;
1053 *lParam = msgf.lParam;
1054 msgf.wParam = *wParam;
1060 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1062 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1063 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1075 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1079 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1080 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1082 if (ME_IsSelection(editor))
1083 ME_DeleteSelection(editor);
1084 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1085 ME_DeleteTextAtCursor(editor, 1, 1);
1088 ME_CommitUndo(editor);
1089 ME_UpdateRepaint(editor);
1090 ME_SendRequestResize(editor, FALSE);
1100 chf.cbSize = sizeof(chf);
1102 ME_GetSelectionCharFormat(editor, &chf);
1103 ME_DumpStyleToBuf(&chf, buf);
1104 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1108 ME_CheckCharOffsets(editor);
1115 static void ME_SetCursor(ME_TextEditor *editor, int x)
1117 if (x < editor->selofs || editor->linesel)
1123 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1128 if(!editor->lpOleCallback)
1130 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1131 if(selrange.cpMin == selrange.cpMax)
1132 seltype |= SEL_EMPTY;
1135 /* FIXME: Handle objects */
1136 seltype |= SEL_TEXT;
1137 if(selrange.cpMax-selrange.cpMin > 1)
1138 seltype |= SEL_MULTICHAR;
1140 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1142 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1148 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1149 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1153 ed->bEmulateVersion10 = FALSE;
1154 ed->pBuffer = ME_MakeText();
1156 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1157 ReleaseDC(hWnd, hDC);
1158 ed->bCaretShown = FALSE;
1160 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1161 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1162 ed->pCursors[0].nOffset = 0;
1163 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1164 ed->pCursors[1].nOffset = 0;
1165 ed->nLastTotalLength = ed->nTotalLength = 0;
1169 ed->rgbBackColor = -1;
1170 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1171 ed->bCaretAtEnd = FALSE;
1173 ed->nModifyStep = 0;
1174 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1175 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1176 ed->nUndoStackSize = 0;
1177 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1178 ed->nUndoMode = umAddToUndo;
1179 ed->nParagraphs = 1;
1180 ed->nLastSelStart = ed->nLastSelEnd = 0;
1181 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1182 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1184 ed->bHideSelection = FALSE;
1185 ed->nInvalidOfs = -1;
1186 ed->pfnWordBreak = NULL;
1187 ed->lpOleCallback = NULL;
1188 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1189 ed->AutoURLDetect_bEnable = FALSE;
1190 ed->bHaveFocus = FALSE;
1191 GetClientRect(hWnd, &ed->rcFormat);
1192 for (i=0; i<HFONT_CACHE_SIZE; i++)
1194 ed->pFontCache[i].nRefs = 0;
1195 ed->pFontCache[i].nAge = 0;
1196 ed->pFontCache[i].hFont = NULL;
1199 ME_CheckCharOffsets(ed);
1200 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1206 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1207 ed->cPasswordMask = '*';
1209 ed->cPasswordMask = 0;
1214 typedef struct tagME_GlobalDestStruct
1218 } ME_GlobalDestStruct;
1220 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1222 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1227 pDest = (WORD *)lpBuff;
1228 pSrc = (WORD *)GlobalLock(pData->hData);
1229 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1230 pDest[i] = pSrc[pData->nLength+i];
1232 pData->nLength += i;
1234 GlobalUnlock(pData->hData);
1238 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1240 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1245 pSrc = (BYTE *)GlobalLock(pData->hData);
1246 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1247 pDest[i] = pSrc[pData->nLength+i];
1249 pData->nLength += i;
1251 GlobalUnlock(pData->hData);
1256 void ME_DestroyEditor(ME_TextEditor *editor)
1258 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1259 ME_DisplayItem *p = pFirst, *pNext = NULL;
1262 ME_ClearTempStyle(editor);
1263 ME_EmptyUndoStack(editor);
1266 ME_DestroyDisplayItem(p);
1269 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1270 for (i=0; i<HFONT_CACHE_SIZE; i++)
1272 if (editor->pFontCache[i].hFont)
1273 DeleteObject(editor->pFontCache[i].hFont);
1275 DeleteObject(editor->hbrBackground);
1276 if(editor->lpOleCallback)
1277 IUnknown_Release(editor->lpOleCallback);
1279 FREE_OBJ(editor->pBuffer);
1280 FREE_OBJ(editor->pCursors);
1285 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1290 case DLL_PROCESS_ATTACH:
1291 DisableThreadLibraryCalls(hinstDLL);
1292 me_heap = HeapCreate (0, 0x10000, 0);
1293 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1294 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1295 hBeam = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1299 case DLL_PROCESS_DETACH:
1300 UnregisterClassW(RichEdit20W, 0);
1301 UnregisterClassW(RichEdit50W, 0);
1302 UnregisterClassA("RichEdit20A", 0);
1303 UnregisterClassA("RichEdit50A", 0);
1304 if (ME_ListBoxRegistered)
1305 UnregisterClassW(REListBox20W, 0);
1306 if (ME_ComboBoxRegistered)
1307 UnregisterClassW(REComboBox20W, 0);
1309 HeapDestroy (me_heap);
1317 #define UNSUPPORTED_MSG(e) \
1319 FIXME(#e ": stub\n"); \
1320 return DefWindowProcW(hWnd, msg, wParam, lParam);
1322 static const char * const edit_messages[] = {
1351 "EM_SETPASSWORDCHAR",
1352 "EM_EMPTYUNDOBUFFER",
1353 "EM_GETFIRSTVISIBLELINE",
1355 "EM_SETWORDBREAKPROC",
1356 "EM_GETWORDBREAKPROC",
1357 "EM_GETPASSWORDCHAR",
1365 static const char * const richedit_messages[] = {
1370 "EM_EXLINEFROMCHAR",
1376 "EM_GETOLEINTERFACE",
1386 "EM_SETOLECALLBACK",
1388 "EM_SETTARGETDEVICE",
1396 "EM_GETWORDBREAKPROCEX",
1397 "EM_SETWORDBREAKPROCEX",
1399 "EM_UNKNOWN_USER_83",
1404 "EM_STOPGROUPTYPING",
1408 "EM_GETAUTOURLDETECT",
1411 "EM_GETTEXTLENGTHEX",
1414 "EM_UNKNOWN_USER_98",
1415 "EM_UNKNOWN_USER_99",
1416 "EM_SETPUNCTUATION",
1417 "EM_GETPUNCTUATION",
1418 "EM_SETWORDWRAPMODE",
1419 "EM_GETWORDWRAPMODE",
1425 "EM_UNKNOWN_USER_109",
1426 "EM_UNKNOWN_USER_110",
1427 "EM_UNKNOWN_USER_111",
1428 "EM_UNKNOWN_USER_112",
1429 "EM_UNKNOWN_USER_113",
1430 "EM_UNKNOWN_USER_114",
1431 "EM_UNKNOWN_USER_115",
1432 "EM_UNKNOWN_USER_116",
1433 "EM_UNKNOWN_USER_117",
1434 "EM_UNKNOWN_USER_118",
1435 "EM_UNKNOWN_USER_119",
1436 "EM_SETLANGOPTIONS",
1437 "EM_GETLANGOPTIONS",
1438 "EM_GETIMECOMPMODE",
1442 "EM_SETIMEMODEBIAS",
1447 get_msg_name(UINT msg)
1449 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1450 return edit_messages[msg - EM_GETSEL];
1451 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1452 return richedit_messages[msg - EM_CANPASTE];
1456 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1457 LPARAM lParam, BOOL unicode)
1459 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1461 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1462 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1464 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1465 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1471 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1472 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1473 UNSUPPORTED_MSG(EM_FMTLINES)
1474 UNSUPPORTED_MSG(EM_FORMATRANGE)
1475 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1476 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1477 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1478 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1479 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1480 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1481 UNSUPPORTED_MSG(EM_GETREDONAME)
1482 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1483 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1484 UNSUPPORTED_MSG(EM_GETUNDONAME)
1485 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1486 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1487 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1488 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1489 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1490 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1491 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1492 UNSUPPORTED_MSG(EM_SETPALETTE)
1493 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1494 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1495 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1496 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1497 UNSUPPORTED_MSG(WM_STYLECHANGING)
1498 UNSUPPORTED_MSG(WM_STYLECHANGED)
1499 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1501 /* Messages specific to Richedit controls */
1504 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1506 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1509 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1510 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1512 int vk = (int)((LPMSG)lParam)->wParam;
1513 /* if style says we want return key */
1514 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1516 code |= DLGC_WANTMESSAGE;
1518 /* we always handle ctrl-tab */
1519 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1521 code |= DLGC_WANTMESSAGE;
1528 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1529 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1530 editor = ME_MakeEditor(hWnd);
1531 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1534 case EM_EMPTYUNDOBUFFER:
1535 ME_EmptyUndoStack(editor);
1539 /* Note: wParam/lParam can be NULL */
1541 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1542 PUINT pto = lParam ? (PUINT)lParam : &to;
1543 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1544 if ((*pfrom|*pto) & 0xFFFF0000)
1546 return MAKELONG(*pfrom,*pto);
1550 CHARRANGE *pRange = (CHARRANGE *)lParam;
1551 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1552 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1555 case EM_SETUNDOLIMIT:
1557 if ((int)wParam < 0)
1558 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1560 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1561 /* Setting a max stack size keeps wine from getting killed
1562 for hogging memory. Windows allocates all this memory at once, so
1563 no program would realistically set a value above our maximum. */
1564 return editor->nUndoLimit;
1567 return editor->pUndoStack != NULL;
1569 return editor->pRedoStack != NULL;
1570 case WM_UNDO: /* FIXME: actually not the same */
1579 /* these flags are equivalent to the ES_* counterparts */
1580 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1581 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1582 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1588 /* these flags are equivalent to ES_* counterparts
1589 * ECO_READONLY is already implemented in the code, only requires
1590 * setting the bit to work
1592 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1593 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
1594 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1595 DWORD settings = mask & raw;
1611 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1613 if (settings & ECO_AUTOWORDSELECTION)
1614 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1615 if (settings & ECO_SELECTIONBAR)
1616 editor->selofs = 16;
1619 ME_WrapMarkedParagraphs(editor);
1621 if (settings & ECO_VERTICAL)
1622 FIXME("ECO_VERTICAL not implemented yet!\n");
1623 if (settings & ECO_AUTOHSCROLL)
1624 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1625 if (settings & ECO_AUTOVSCROLL)
1626 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1627 if (settings & ECO_NOHIDESEL)
1628 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1629 if (settings & ECO_WANTRETURN)
1630 FIXME("ECO_WANTRETURN not implemented yet!\n");
1636 ME_InvalidateSelection(editor);
1637 ME_SetSelection(editor, wParam, lParam);
1638 ME_InvalidateSelection(editor);
1639 ME_SendSelChange(editor);
1642 case EM_SETSCROLLPOS:
1644 POINT *point = (POINT *)lParam;
1645 ME_ScrollAbs(editor, point->y);
1648 case EM_AUTOURLDETECT:
1650 if (wParam==1 || wParam ==0)
1652 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1655 return E_INVALIDARG;
1657 case EM_GETAUTOURLDETECT:
1659 return editor->AutoURLDetect_bEnable;
1664 CHARRANGE range = *(CHARRANGE *)lParam;
1666 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1668 ME_InvalidateSelection(editor);
1669 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1670 ME_InvalidateSelection(editor);
1671 ME_SendSelChange(editor);
1675 case EM_SHOWSCROLLBAR:
1677 ShowScrollBar(editor->hWnd, wParam, lParam);
1683 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1687 int oldModify = editor->nModifyStep;
1689 if (!pStruct) return 0;
1691 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
1692 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
1693 pStruct->flags, pStruct->codepage);
1695 /* FIXME: make use of pStruct->codepage in the to unicode translation */
1696 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
1697 len = wszText ? lstrlenW(wszText) : 0;
1699 /* FIXME: this should support RTF strings too, according to MSDN */
1700 if (pStruct->flags & ST_SELECTION) {
1701 ME_GetSelection(editor, &from, &to);
1702 style = ME_GetSelectionInsertStyle(editor);
1703 ME_InternalDeleteText(editor, from, to - from);
1704 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1705 ME_ReleaseStyle(style);
1708 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1709 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1712 ME_CommitUndo(editor);
1713 if (!(pStruct->flags & ST_KEEPUNDO))
1715 editor->nModifyStep = oldModify;
1716 ME_EmptyUndoStack(editor);
1718 ME_UpdateRepaint(editor);
1721 case EM_SETBKGNDCOLOR:
1723 LRESULT lColor = ME_GetBackColor(editor);
1724 if (editor->rgbBackColor != -1)
1725 DeleteObject(editor->hbrBackground);
1728 editor->rgbBackColor = -1;
1729 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1733 editor->rgbBackColor = lParam;
1734 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1736 if (editor->bRedraw)
1738 InvalidateRect(hWnd, NULL, TRUE);
1744 return editor->nModifyStep == 0 ? 0 : 1;
1748 editor->nModifyStep = 1;
1750 editor->nModifyStep = 0;
1754 case EM_SETREADONLY:
1756 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1758 nStyle |= ES_READONLY;
1760 nStyle &= ~ES_READONLY;
1761 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1764 case EM_SETEVENTMASK:
1766 DWORD nOldMask = editor->nEventMask;
1768 editor->nEventMask = lParam;
1771 case EM_GETEVENTMASK:
1772 return editor->nEventMask;
1773 case EM_SETCHARFORMAT:
1775 CHARFORMAT2W buf, *p;
1776 BOOL bRepaint = TRUE;
1777 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1778 if (p == NULL) return 0;
1780 ME_SetDefaultCharFormat(editor, p);
1781 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
1782 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1784 } else if (wParam == SCF_ALL) {
1785 if (editor->mode & TM_PLAINTEXT)
1786 ME_SetDefaultCharFormat(editor, p);
1788 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1789 } else if (editor->mode & TM_PLAINTEXT) {
1793 ME_GetSelection(editor, &from, &to);
1794 bRepaint = (from != to);
1795 ME_SetSelectionCharFormat(editor, p);
1797 editor->nModifyStep = 1;
1798 ME_CommitUndo(editor);
1800 ME_RewrapRepaint(editor);
1803 case EM_GETCHARFORMAT:
1805 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1806 if (dst->cbSize != sizeof(CHARFORMATA) &&
1807 dst->cbSize != sizeof(CHARFORMATW) &&
1808 dst->cbSize != sizeof(CHARFORMAT2A) &&
1809 dst->cbSize != sizeof(CHARFORMAT2W))
1811 tmp.cbSize = sizeof(tmp);
1813 ME_GetDefaultCharFormat(editor, &tmp);
1815 ME_GetSelectionCharFormat(editor, &tmp);
1816 ME_CopyToCFAny(dst, &tmp);
1819 case EM_SETPARAFORMAT:
1820 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1821 ME_RewrapRepaint(editor);
1822 ME_CommitUndo(editor);
1824 case EM_GETPARAFORMAT:
1825 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1827 case EM_GETFIRSTVISIBLELINE:
1829 ME_DisplayItem *p = editor->pBuffer->pFirst;
1830 int y = ME_GetYScrollPos(editor);
1835 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1836 if (p->type == diTextEnd)
1838 if (p->type == diParagraph) {
1839 ypara = p->member.para.nYPos;
1842 ystart = ypara + p->member.row.nYPos;
1843 yend = ystart + p->member.row.nHeight;
1851 case EM_HIDESELECTION:
1853 editor->bHideSelection = (wParam != 0);
1854 ME_InvalidateSelection(editor);
1859 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1860 return TRUE; /* Should return false if a single line richedit control */
1865 ME_GetSelection(editor, &from, &to);
1866 ME_InternalDeleteText(editor, from, to-from);
1867 ME_CommitUndo(editor);
1868 ME_UpdateRepaint(editor);
1875 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
1876 size_t len = wszText ? lstrlenW(wszText) : 0;
1877 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1879 ME_GetSelection(editor, &from, &to);
1880 style = ME_GetSelectionInsertStyle(editor);
1881 ME_InternalDeleteText(editor, from, to-from);
1882 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1883 ME_ReleaseStyle(style);
1884 /* drop temporary style if line end */
1886 * FIXME question: does abc\n mean: put abc,
1887 * clear temp style, put \n? (would require a change)
1889 if (len>0 && wszText[len-1] == '\n')
1890 ME_ClearTempStyle(editor);
1891 ME_EndToUnicode(unicode, wszText);
1892 ME_CommitUndo(editor);
1894 ME_EmptyUndoStack(editor);
1895 ME_UpdateRepaint(editor);
1898 case EM_SCROLLCARET:
1900 int top, bottom; /* row's edges relative to document top */
1902 ME_DisplayItem *para, *row;
1904 nPos = ME_GetYScrollPos(editor);
1905 row = ME_RowStart(editor->pCursors[0].pRun);
1906 para = ME_GetParagraph(row);
1907 top = para->member.para.nYPos + row->member.row.nYPos;
1908 bottom = top + row->member.row.nHeight;
1910 if (top < nPos) /* caret above window */
1911 ME_ScrollAbs(editor, top);
1912 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1913 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1921 BOOL bRepaint = LOWORD(lParam);
1924 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1925 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1927 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1928 ReleaseDC(hWnd, hDC);
1929 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1930 ME_SetDefaultCharFormat(editor, &fmt);
1932 ME_CommitUndo(editor);
1934 ME_RewrapRepaint(editor);
1939 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1942 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1943 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
1945 /* Undocumented: WM_SETTEXT supports RTF text */
1946 ME_StreamInRTFString(editor, 0, (char *)lParam);
1950 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1951 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1952 if (lstrlenW(wszText) > 0)
1956 /* uses default style! */
1957 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
1962 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
1965 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1967 ME_EndToUnicode(unicode, wszText);
1971 TRACE("WM_SETTEXT - NULL\n");
1972 ME_CommitUndo(editor);
1973 ME_EmptyUndoStack(editor);
1974 ME_SetSelection(editor, 0, 0);
1975 editor->nModifyStep = 0;
1976 ME_UpdateRepaint(editor);
1981 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1982 if (IsClipboardFormatAvailable(nRTFFormat))
1984 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1992 ME_GlobalDestStruct gds;
1993 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1996 if (IsClipboardFormatAvailable(nRTFFormat))
1997 cf = nRTFFormat, dwFormat = SF_RTF;
1998 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1999 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2003 if (!OpenClipboard(hWnd))
2005 gds.hData = GetClipboardData(cf);
2007 es.dwCookie = (DWORD)&gds;
2008 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2009 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
2017 LPDATAOBJECT dataObj = NULL;
2021 if (editor->cPasswordMask)
2022 return 0; /* Copying or Cutting masked text isn't allowed */
2024 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
2025 if(editor->lpOleCallback)
2026 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2027 if(FAILED(hr) || !dataObj)
2028 hr = ME_GetDataObject(editor, &range, &dataObj);
2030 hr = OleSetClipboard(dataObj);
2031 IDataObject_Release(dataObj);
2033 if (SUCCEEDED(hr) && msg == WM_CUT)
2035 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2036 ME_CommitUndo(editor);
2037 ME_UpdateRepaint(editor);
2041 case WM_GETTEXTLENGTH:
2043 GETTEXTLENGTHEX how;
2045 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2046 how.codepage = unicode ? 1200 : CP_ACP;
2047 return ME_GetTextLengthEx(editor, &how);
2049 case EM_GETTEXTLENGTHEX:
2050 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2055 LPSTR bufferA = NULL;
2056 LPWSTR bufferW = NULL;
2059 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2061 bufferA = heap_alloc(wParam + 2);
2063 ex.cb = wParam + (unicode ? 2*sizeof(WCHAR) : 2);
2064 ex.flags = GT_USECRLF;
2065 ex.codepage = unicode ? 1200 : CP_ACP;
2066 ex.lpDefaultChar = NULL;
2067 ex.lpUsedDefaultChar = NULL;
2068 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2072 memcpy((LPWSTR)lParam, bufferW, wParam);
2073 if (lstrlenW(bufferW) >= wParam / sizeof(WCHAR)) rc = 0;
2077 memcpy((LPSTR)lParam, bufferA, wParam);
2078 if (strlen(bufferA) >= wParam) rc = 0;
2086 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2089 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2090 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2092 if (ex->flags & GT_SELECTION)
2094 ME_GetSelection(editor, &nStart, &nCount);
2096 nCount = min(nCount, ex->cb - 1);
2101 nCount = ex->cb - 1;
2103 if (ex->codepage == 1200)
2105 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2106 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2110 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2111 we can just take a bigger buffer? :) */
2112 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2113 LPWSTR buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2114 DWORD buflen = ex->cb;
2118 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2119 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2120 if (rc) rc--; /* do not count 0 terminator */
2129 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2130 ME_GetSelection(editor, &from, &to);
2131 tr.chrg.cpMin = from;
2133 tr.lpstrText = (WCHAR *)lParam;
2134 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2136 case EM_GETSCROLLPOS:
2138 POINT *point = (POINT *)lParam;
2139 point->x = 0; /* FIXME side scrolling not implemented */
2140 point->y = ME_GetYScrollPos(editor);
2143 case EM_GETTEXTRANGE:
2145 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2146 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2147 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2148 editor->bEmulateVersion10, ME_GetTextLength(editor));
2150 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2153 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2154 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2155 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2156 /* FIXME this is a potential security hole (buffer overrun)
2157 if you know more about wchar->mbyte conversion please explain
2159 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2166 ME_DisplayItem *run;
2167 const unsigned int nMaxChars = *(WORD *) lParam;
2168 unsigned int nEndChars, nCharsLeft = nMaxChars;
2169 char *dest = (char *) lParam;
2170 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2171 we need to know how if we have the extra \n or not */
2172 int nLF = editor->bEmulateVersion10;
2174 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2175 unicode ? "Unicode" : "Ansi");
2177 run = ME_FindRowWithNumber(editor, wParam);
2181 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2182 && !(run->member.run.nFlags & MERF_ENDPARA))
2186 if (run->type != diRun)
2188 strText = run->member.run.strText;
2189 nCopy = min(nCharsLeft, strText->nLen);
2192 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2194 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2195 nCharsLeft, NULL, NULL);
2196 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2197 nCharsLeft -= nCopy;
2200 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2201 nEndChars = min(nCharsLeft, 2 + nLF);
2202 nCharsLeft -= nEndChars;
2205 const WCHAR src[] = {'\r', '\0'};
2206 const WCHAR src10[] = {'\r', '\n', '\0'};
2207 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2210 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2212 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2214 if (nEndChars == 2 + nLF)
2215 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2217 return nMaxChars - nCharsLeft;
2219 case EM_GETLINECOUNT:
2221 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2224 while (item != editor->pBuffer->pLast)
2226 assert(item->type == diParagraph);
2227 nRows += item->member.para.nRows;
2228 item = item->member.para.next_para;
2230 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2231 return max(1, nRows);
2233 case EM_LINEFROMCHAR:
2236 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2238 return ME_RowNumberFromCharOfs(editor, wParam);
2240 case EM_EXLINEFROMCHAR:
2243 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2245 return ME_RowNumberFromCharOfs(editor, lParam);
2249 ME_DisplayItem *item, *para;
2253 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2255 item = ME_FindRowWithNumber(editor, wParam);
2258 para = ME_GetParagraph(item);
2259 item = ME_FindItemFwd(item, diRun);
2260 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2261 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2266 ME_DisplayItem *item, *item_end;
2267 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2269 if (wParam > ME_GetTextLength(editor))
2273 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2276 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2277 item = ME_RowStart(item);
2278 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2279 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2280 if (item_end->type == diStartRow)
2281 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2283 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2284 - (editor->bEmulateVersion10?2:1);
2285 nChars = nNextLineOfs - nThisLineOfs;
2286 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2289 case EM_EXLIMITTEXT:
2291 if ((int)lParam < 0)
2294 editor->nTextLimit = 65536;
2296 editor->nTextLimit = (int) lParam;
2302 editor->nTextLimit = 65536;
2304 editor->nTextLimit = (int) wParam;
2307 case EM_GETLIMITTEXT:
2309 return editor->nTextLimit;
2313 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2314 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2318 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2319 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2320 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2326 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2327 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2331 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2332 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2333 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2339 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2340 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2342 case EM_FINDTEXTEXW:
2344 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2345 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2348 if (!wParam || !lParam)
2350 *(int *)wParam = editor->nZoomNumerator;
2351 *(int *)lParam = editor->nZoomDenominator;
2354 return ME_SetZoom(editor, wParam, lParam);
2355 case EM_CHARFROMPOS:
2356 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2357 case EM_POSFROMCHAR:
2359 ME_DisplayItem *pRun;
2360 int nCharOfs, nOffset, nLength;
2364 /* detect which API version we're dealing with */
2365 if (wParam >= 0x40000)
2367 nLength = ME_GetTextLength(editor);
2369 if (nCharOfs < nLength) {
2370 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2371 assert(pRun->type == diRun);
2372 pt.y = pRun->member.run.pt.y;
2373 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2374 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2377 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2379 if (wParam >= 0x40000) {
2380 *(POINTL *)wParam = pt;
2382 return MAKELONG( pt.x, pt.y );
2385 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2386 { /* Squelch the default horizontal scrollbar it would make */
2387 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2389 ME_CommitUndo(editor);
2390 ME_WrapMarkedParagraphs(editor);
2391 ME_MoveCaret(editor);
2394 ME_DestroyEditor(editor);
2395 SetWindowLongPtrW(hWnd, 0, 0);
2397 case WM_LBUTTONDOWN:
2398 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2399 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2402 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2404 ME_LinkNotify(editor,msg,wParam,lParam);
2405 ME_SetCursor(editor, LOWORD(lParam));
2408 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2409 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2411 if (GetCapture() == hWnd)
2412 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2413 ME_LinkNotify(editor,msg,wParam,lParam);
2414 ME_SetCursor(editor, LOWORD(lParam));
2417 if (GetCapture() == hWnd)
2419 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2420 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2422 editor->linesel = 0;
2423 ME_SetCursor(editor, LOWORD(lParam));
2424 ME_LinkNotify(editor,msg,wParam,lParam);
2426 case WM_LBUTTONDBLCLK:
2427 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2428 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2430 ME_LinkNotify(editor,msg,wParam,lParam);
2431 ME_SelectWord(editor);
2434 case WM_RBUTTONDOWN:
2435 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2436 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2439 case WM_CONTEXTMENU:
2440 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2444 if (editor->bRedraw)
2449 hDC = BeginPaint(hWnd, &ps);
2450 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2451 EndPaint(hWnd, &ps);
2455 editor->bHaveFocus = TRUE;
2456 ME_ShowCaret(editor);
2457 ME_SendOldNotify(editor, EN_SETFOCUS);
2460 ME_HideCaret(editor);
2461 editor->bHaveFocus = FALSE;
2462 ME_SendOldNotify(editor, EN_KILLFOCUS);
2466 if (editor->bRedraw)
2468 HDC hDC = (HDC)wParam;
2470 if (GetUpdateRect(hWnd,&rc,TRUE))
2472 FillRect(hDC, &rc, editor->hbrBackground);
2478 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2481 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2482 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2486 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2487 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2489 if (ME_KeyDown(editor, LOWORD(wParam)))
2497 wstr = (WCHAR)wParam;
2500 CHAR charA = wParam;
2501 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2503 if (editor->AutoURLDetect_bEnable)
2504 ME_AutoURLDetect(editor, wstr);
2508 case 1: /* Ctrl-A */
2509 ME_SetSelection(editor, 0, -1);
2511 case 3: /* Ctrl-C */
2512 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2516 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2517 MessageBeep(MB_ICONERROR);
2518 return 0; /* FIXME really 0 ? */
2523 case 22: /* Ctrl-V */
2524 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2526 case 24: /* Ctrl-X */
2527 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2529 case 25: /* Ctrl-Y */
2530 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2532 case 26: /* Ctrl-Z */
2533 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2536 if (((unsigned)wstr)>=' '
2537 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2539 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2540 /* WM_CHAR is restricted to nTextLimit */
2542 ME_GetSelection(editor, &from, &to);
2543 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2545 ME_Style *style = ME_GetInsertStyle(editor, 0);
2546 ME_SaveTempStyle(editor);
2547 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2548 ME_ReleaseStyle(style);
2549 ME_CommitUndo(editor);
2551 ME_UpdateRepaint(editor);
2555 case EM_SCROLL: /* fall through */
2561 origNPos = ME_GetYScrollPos(editor);
2564 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2565 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2566 if (lineHeight <= 0) lineHeight = 24;
2568 switch(LOWORD(wParam))
2571 ME_ScrollUp(editor,lineHeight);
2574 ME_ScrollDown(editor,lineHeight);
2577 ME_ScrollUp(editor,editor->sizeWindow.cy);
2580 ME_ScrollDown(editor,editor->sizeWindow.cy);
2583 case SB_THUMBPOSITION:
2584 ME_ScrollAbs(editor,HIWORD(wParam));
2587 if (msg == EM_SCROLL)
2588 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2594 UINT pulScrollLines;
2596 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2597 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2600 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2601 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2603 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2605 /* FIXME follow the original */
2606 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2612 *((RECT *)lParam) = editor->rcFormat;
2620 RECT *rc = (RECT *)lParam;
2624 editor->rcFormat.left += rc->left;
2625 editor->rcFormat.top += rc->top;
2626 editor->rcFormat.right += rc->right;
2627 editor->rcFormat.bottom += rc->bottom;
2631 editor->rcFormat = *rc;
2636 GetClientRect(hWnd, &editor->rcFormat);
2638 if (msg != EM_SETRECTNP)
2639 ME_RewrapRepaint(editor);
2642 case EM_REQUESTRESIZE:
2643 ME_SendRequestResize(editor, TRUE);
2646 editor->bRedraw = wParam;
2650 GetClientRect(hWnd, &editor->rcFormat);
2651 ME_RewrapRepaint(editor);
2652 return DefWindowProcW(hWnd, msg, wParam, lParam);
2654 /* IME messages to make richedit controls IME aware */
2655 case WM_IME_SETCONTEXT:
2656 case WM_IME_CONTROL:
2658 case WM_IME_COMPOSITIONFULL:
2660 case WM_IME_STARTCOMPOSITION:
2662 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
2663 ME_DeleteSelection(editor);
2664 ME_CommitUndo(editor);
2665 ME_UpdateRepaint(editor);
2668 case WM_IME_COMPOSITION:
2672 ME_Style *style = ME_GetInsertStyle(editor, 0);
2673 hIMC = ImmGetContext(hWnd);
2674 ME_DeleteSelection(editor);
2675 ME_CommitUndo(editor);
2676 ME_SaveTempStyle(editor);
2677 if (lParam & GCS_RESULTSTR)
2679 LPWSTR lpCompStr = NULL;
2682 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
2683 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
2684 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
2685 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
2686 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
2688 else if (lParam & GCS_COMPSTR)
2690 LPWSTR lpCompStr = NULL;
2693 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
2694 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
2695 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
2696 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
2698 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
2699 ME_SetSelection(editor,editor->imeStartIndex,
2700 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
2702 ME_ReleaseStyle(style);
2703 ME_UpdateRepaint(editor);
2706 case WM_IME_ENDCOMPOSITION:
2708 ME_DeleteSelection(editor);
2709 editor->imeStartIndex=-1;
2712 case EM_GETOLEINTERFACE:
2714 LPVOID *ppvObj = (LPVOID*) lParam;
2715 return CreateIRichEditOle(editor, ppvObj);
2717 case EM_GETPASSWORDCHAR:
2719 return editor->cPasswordMask;
2721 case EM_SETOLECALLBACK:
2722 if(editor->lpOleCallback)
2723 IUnknown_Release(editor->lpOleCallback);
2724 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2725 if(editor->lpOleCallback)
2726 IUnknown_AddRef(editor->lpOleCallback);
2728 case EM_GETWORDBREAKPROC:
2729 return (LRESULT)editor->pfnWordBreak;
2730 case EM_SETWORDBREAKPROC:
2732 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2734 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2735 return (LRESULT)pfnOld;
2737 case EM_SETTEXTMODE:
2742 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
2745 /*Check for valid wParam*/
2746 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2747 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2748 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2752 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2754 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2755 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2757 /*FIXME: Currently no support for undo level and code page options*/
2758 editor->mode = (editor->mode & (~mask)) | changes;
2764 case EM_SETPASSWORDCHAR:
2766 editor->cPasswordMask = wParam;
2767 ME_RewrapRepaint(editor);
2772 return DefWindowProcW(hWnd, msg, wParam, lParam);
2777 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2779 BOOL unicode = TRUE;
2781 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
2782 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
2785 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
2788 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2790 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
2793 /******************************************************************
2794 * RichEditANSIWndProc (RICHED20.10)
2796 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2798 return RichEditWndProcA(hWnd, msg, wParam, lParam);
2801 /******************************************************************
2802 * RichEdit10ANSIWndProc (RICHED20.9)
2804 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2808 /* FIXME: this is NOT the same as 2.0 version */
2809 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2810 if (msg == WM_NCCREATE)
2812 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2814 editor->bEmulateVersion10 = TRUE;
2815 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2820 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2822 HWND hWnd = editor->hWnd;
2823 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2826 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2829 ME_Cursor tmpCursor;
2830 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2835 x = (short)LOWORD(lParam);
2836 y = (short)HIWORD(lParam);
2837 nCharOfs = ME_CharFromPos(editor, x, y);
2838 if (nCharOfs < 0) return;
2840 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2841 tmpRun = &tmpCursor.pRun->member.run;
2843 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2844 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2845 { /* The clicked run has CFE_LINK set */
2846 info.nmhdr.hwndFrom = editor->hWnd;
2847 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2848 info.nmhdr.code = EN_LINK;
2850 info.wParam = wParam;
2851 info.lParam = lParam;
2852 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2853 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2854 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2858 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2860 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2863 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2864 item = item->member.para.next_para;
2867 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2868 item = item->member.para.next_para;
2875 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2877 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2879 WCHAR *pStart = buffer;
2888 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2891 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2898 item = ME_FindItemFwd(item, diRun);
2901 while(nChars && item)
2903 int nLen = ME_StrLen(item->member.run.strText);
2907 if (item->member.run.nFlags & MERF_ENDPARA)
2909 if (!ME_FindItemFwd(item, diRun))
2910 /* No '\r' is appended to the last paragraph. */
2912 else if (bCRLF && nChars == 1) {
2923 /* our end paragraph consists of 2 characters now */
2924 if (editor->bEmulateVersion10)
2929 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2936 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2940 item = ME_FindItemFwd(item, diRun);
2943 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2947 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
2952 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2953 wcW.lpfnWndProc = RichEditWndProcW;
2955 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2956 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2958 wcW.hCursor = hBeam;
2959 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2960 wcW.lpszMenuName = NULL;
2962 if (is_version_nt())
2964 wcW.lpszClassName = RichEdit20W;
2965 if (!RegisterClassW(&wcW)) return FALSE;
2966 wcW.lpszClassName = RichEdit50W;
2967 if (!RegisterClassW(&wcW)) return FALSE;
2971 /* WNDCLASSA/W have the same layout */
2972 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
2973 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2974 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
2975 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2978 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2979 wcA.lpfnWndProc = RichEditWndProcA;
2981 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2982 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2984 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2985 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2986 wcA.lpszMenuName = NULL;
2987 wcA.lpszClassName = "RichEdit20A";
2988 if (!RegisterClassA(&wcA)) return FALSE;
2989 wcA.lpszClassName = "RichEdit50A";
2990 if (!RegisterClassA(&wcA)) return FALSE;
2995 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2996 /* FIXME: Not implemented */
2997 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
2998 hWnd, msg, get_msg_name(msg), wParam, lParam);
2999 return DefWindowProcW(hWnd, msg, wParam, lParam);
3002 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3003 /* FIXME: Not implemented */
3004 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3005 hWnd, msg, get_msg_name(msg), wParam, lParam);
3006 return DefWindowProcW(hWnd, msg, wParam, lParam);
3009 /******************************************************************
3010 * REExtendedRegisterClass (RICHED20.8)
3012 * FIXME undocumented
3013 * Need to check for errors and implement controls and callbacks
3015 LRESULT WINAPI REExtendedRegisterClass(void)
3020 FIXME("semi stub\n");
3024 wcW.hInstance = NULL;
3027 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
3028 wcW.lpszMenuName = NULL;
3030 if (!ME_ListBoxRegistered)
3032 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
3033 wcW.lpfnWndProc = REListWndProc;
3034 wcW.lpszClassName = REListBox20W;
3035 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
3038 if (!ME_ComboBoxRegistered)
3040 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
3041 wcW.lpfnWndProc = REComboWndProc;
3042 wcW.lpszClassName = REComboBox20W;
3043 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
3047 if (ME_ListBoxRegistered)
3049 if (ME_ComboBoxRegistered)
3055 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
3075 int curf_ef, link_ef, def_ef;
3076 int cur_prefx, prefx_cnt;
3077 int sel_min, sel_max;
3080 int URLmin, URLmax = 0;
3083 CHARFORMAT2W cur_format;
3084 CHARFORMAT2W default_format;
3086 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
3087 sel_min = ins_pt.cpMin;
3088 sel_max = ins_pt.cpMax;
3089 if (sel_min==sel_max)
3091 if (sel_min!=sel_max)
3092 car_pos = ME_GetTextLength(editor)+1;
3093 cur_format.cbSize = sizeof(cur_format);
3094 default_format.cbSize = sizeof(default_format);
3095 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
3096 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
3097 link.cbSize = sizeof(link);
3098 link.dwMask = CFM_LINK;
3099 link.dwEffects = CFE_LINK;
3100 curf_ef = cur_format.dwEffects & link.dwEffects;
3101 def_ef = default_format.dwEffects & link.dwEffects;
3102 link_ef = link.dwEffects & link.dwEffects;
3103 if (curf_ef == link_ef)
3105 if( curChar == '\n' || curChar=='\r' || curChar==' ')
3107 ME_SetSelection(editor, car_pos, car_pos);
3108 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
3113 if (curf_ef == def_ef)
3116 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
3117 while (cur_prefx<=prefx_cnt)
3121 ft.lpstrText = prefixes[cur_prefx].text;
3122 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
3123 URLmax=max(0, car_pos);
3124 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
3126 URLmax = ME_GetTextLength(editor)+1;
3128 ft.chrg.cpMin = URLmin;
3129 ft.chrg.cpMax = URLmax;
3130 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
3136 url.cpMax=car_pos-1;
3137 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
3138 ME_RewrapRepaint(editor);