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 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
239 HANDLE me_heap = NULL;
241 static BOOL ME_ListBoxRegistered = FALSE;
242 static BOOL ME_ComboBoxRegistered = FALSE;
244 static ME_TextBuffer *ME_MakeText(void) {
246 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
248 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
249 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
255 p1->member.para.next_para = p2;
256 p2->member.para.prev_para = p1;
257 p2->member.para.nCharOfs = 0;
261 buf->pCharStyle = NULL;
267 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
269 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
272 TRACE("%08lx %p\n", dwFormat, stream);
279 ME_StreamInFill(stream);
280 if (stream->editstream->dwError)
286 if (!(dwFormat & SF_UNICODE))
288 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
289 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
294 nWideChars = stream->dwSize >> 1;
295 pText = (WCHAR *)stream->buffer;
298 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
299 if (stream->dwSize == 0)
303 ME_CommitUndo(editor);
304 ME_UpdateRepaint(editor);
308 static void ME_RTFCharAttrHook(RTF_Info *info)
311 fmt.cbSize = sizeof(fmt);
315 switch(info->rtfMinor)
318 /* FIXME add more flags once they're implemented */
319 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
320 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
321 fmt.yHeight = 12*20; /* 12pt */
325 fmt.dwMask = CFM_BOLD;
326 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
329 fmt.dwMask = CFM_ITALIC;
330 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
333 fmt.dwMask = CFM_UNDERLINE;
334 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
335 fmt.bUnderlineType = CFU_CF1UNDERLINE;
338 fmt.dwMask = CFM_UNDERLINE;
342 fmt.dwMask = CFM_STRIKEOUT;
343 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
347 case rtfSubScrShrink:
348 case rtfSuperScrShrink:
350 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
351 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
352 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
353 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
356 fmt.dwMask = CFM_HIDDEN;
357 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
360 fmt.dwMask = CFM_BACKCOLOR;
362 if (info->rtfParam == 0)
363 fmt.dwEffects = CFE_AUTOBACKCOLOR;
364 else if (info->rtfParam != rtfNoParam)
366 RTFColor *c = RTFGetColor(info, info->rtfParam);
367 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
371 fmt.dwMask = CFM_COLOR;
373 if (info->rtfParam == 0)
374 fmt.dwEffects = CFE_AUTOCOLOR;
375 else if (info->rtfParam != rtfNoParam)
377 RTFColor *c = RTFGetColor(info, info->rtfParam);
378 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
382 if (info->rtfParam != rtfNoParam)
384 RTFFont *f = RTFGetFont(info, info->rtfParam);
387 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
388 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
389 fmt.bCharSet = f->rtfFCharSet;
390 fmt.dwMask = CFM_FACE | CFM_CHARSET;
395 fmt.dwMask = CFM_SIZE;
396 if (info->rtfParam != rtfNoParam)
397 fmt.yHeight = info->rtfParam*10;
402 RTFFlushOutputBuffer(info);
403 /* FIXME too slow ? how come ? */
404 style2 = ME_ApplyStyle(info->style, &fmt);
405 ME_ReleaseStyle(info->style);
406 info->style = style2;
407 info->styleChanged = TRUE;
411 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
412 the same tags mean different things in different contexts */
413 static void ME_RTFParAttrHook(RTF_Info *info)
416 fmt.cbSize = sizeof(fmt);
419 switch(info->rtfMinor)
421 case rtfParDef: /* restores default paragraph attributes */
422 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
423 fmt.wAlignment = PFA_LEFT;
425 fmt.dxOffset = fmt.dxStartIndent = 0;
426 RTFFlushOutputBuffer(info);
427 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
431 ME_DisplayItem *para;
433 RTFFlushOutputBuffer(info);
434 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
435 assert(para->member.para.pCells);
436 para->member.para.bTable = TRUE;
440 ME_GetSelectionParaFormat(info->editor, &fmt);
441 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
442 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
443 fmt.dxOffset = -info->rtfParam;
446 ME_GetSelectionParaFormat(info->editor, &fmt);
447 fmt.dwMask = PFM_STARTINDENT;
448 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
451 fmt.dwMask = PFM_RIGHTINDENT;
452 fmt.dxRightIndent = info->rtfParam;
456 fmt.dwMask = PFM_ALIGNMENT;
457 fmt.wAlignment = PFA_LEFT;
460 fmt.dwMask = PFM_ALIGNMENT;
461 fmt.wAlignment = PFA_RIGHT;
464 fmt.dwMask = PFM_ALIGNMENT;
465 fmt.wAlignment = PFA_CENTER;
468 ME_GetSelectionParaFormat(info->editor, &fmt);
469 if (!(fmt.dwMask & PFM_TABSTOPS))
471 fmt.dwMask |= PFM_TABSTOPS;
474 if (fmt.cTabCount < MAX_TAB_STOPS)
475 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
479 RTFFlushOutputBuffer(info);
480 /* FIXME too slow ? how come ?*/
481 ME_SetSelectionParaFormat(info->editor, &fmt);
485 static void ME_RTFTblAttrHook(RTF_Info *info)
487 ME_DisplayItem *para;
489 switch (info->rtfMinor)
492 RTFFlushOutputBuffer(info);
493 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
495 /* Release possibly inherited cell definitions */
496 ME_DestroyTableCellList(para);
498 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
499 para->member.para.pCells->nRightBoundary = 0;
500 para->member.para.pCells->next = NULL;
501 para->member.para.pLastCell = para->member.para.pCells;
504 RTFFlushOutputBuffer(info);
505 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
507 if (para->member.para.pLastCell->nRightBoundary)
509 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
512 para->member.para.pLastCell->next = pCell;
513 para->member.para.pLastCell = pCell;
515 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
520 static void ME_RTFReadHook(RTF_Info *info) {
521 switch(info->rtfClass)
524 switch(info->rtfMajor)
527 if (info->stackTop < maxStack) {
528 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
529 info->stack[info->stackTop].codePage = info->codePage;
530 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
533 info->styleChanged = FALSE;
538 RTFFlushOutputBuffer(info);
539 if (info->stackTop<=1) {
540 info->rtfClass = rtfEOF;
544 assert(info->stackTop >= 0);
545 if (info->styleChanged)
547 /* FIXME too slow ? how come ? */
548 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
549 ME_ReleaseStyle(info->style);
551 info->codePage = info->stack[info->stackTop].codePage;
552 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
559 switch(info->rtfMajor)
562 ME_RTFCharAttrHook(info);
565 ME_RTFParAttrHook(info);
568 ME_RTFTblAttrHook(info);
571 if (info->rtfMinor == rtfCell)
573 RTFFlushOutputBuffer(info);
574 ME_InsertTableCellFromCursor(info->editor, 0);
582 ME_StreamInFill(ME_InStream *stream)
584 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
585 (BYTE *)stream->buffer,
586 sizeof(stream->buffer),
587 (LONG *)&stream->dwSize);
591 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
595 int from, to, to2, nUndoMode;
597 int nEventMask = editor->nEventMask;
598 ME_InStream inStream;
600 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
601 editor->nEventMask = 0;
603 ME_GetSelection(editor, &from, &to);
604 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
605 style = ME_GetSelectionInsertStyle(editor);
607 ME_InternalDeleteText(editor, from, to-from);
610 style = editor->pBuffer->pDefaultStyle;
611 ME_AddRefStyle(style);
612 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
613 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
615 ME_ClearTempStyle(editor);
616 /* FIXME restore default paragraph formatting ! */
619 nUndoMode = editor->nUndoMode;
620 editor->nUndoMode = umIgnore;
622 inStream.editstream = stream;
623 inStream.editstream->dwError = 0;
629 /* Check if it's really RTF, and if it is not, use plain text */
630 ME_StreamInFill(&inStream);
631 if (!inStream.editstream->dwError)
633 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
641 if (!inStream.editstream->dwError)
643 if (format & SF_RTF) {
644 /* setup the RTF parser */
645 memset(&parser, 0, sizeof parser);
646 RTFSetEditStream(&parser, &inStream);
647 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
648 parser.hwndEdit = editor->hWnd;
649 parser.editor = editor;
650 parser.style = style;
653 RTFSetReadHook(&parser, ME_RTFReadHook);
658 RTFFlushOutputBuffer(&parser);
661 style = parser.style;
663 else if (format & SF_TEXT)
664 ME_StreamInText(editor, format, &inStream, style);
666 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
667 ME_GetSelection(editor, &to, &to2);
668 /* put the cursor at the top */
669 if (!(format & SFF_SELECTION))
670 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
673 editor->nUndoMode = nUndoMode;
674 if (format & SFF_SELECTION)
676 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
677 TRACE("from %d to %d\n", from, to);
678 if (pUI && from < to)
683 ME_CommitUndo(editor);
687 ME_EmptyUndoStack(editor);
690 ME_ReleaseStyle(style);
691 editor->nEventMask = nEventMask;
694 ME_UpdateRepaint(editor);
696 if (!(format & SFF_SELECTION)) {
697 ME_ClearTempStyle(editor);
699 ME_MoveCaret(editor);
700 ME_SendSelChange(editor);
701 ME_SendRequestResize(editor, FALSE);
707 typedef struct tagME_RTFStringStreamStruct
712 } ME_RTFStringStreamStruct;
714 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
716 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
719 count = min(cb, pStruct->length - pStruct->pos);
720 memmove(lpBuff, pStruct->string + pStruct->pos, count);
721 pStruct->pos += count;
727 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
730 ME_RTFStringStreamStruct data;
732 data.string = string;
733 data.length = strlen(string);
735 es.dwCookie = (DWORD)&data;
736 es.pfnCallback = ME_ReadFromRTFString;
737 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
742 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
744 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
746 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
747 item = ME_FindItemFwd(item, diParagraph);
752 nOffset -= item->member.para.nCharOfs;
753 if (nItemType == diParagraph) {
755 *nItemOffset = nOffset;
760 item = ME_FindItemFwd(item, diRun);
761 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
763 nOffset -= item->member.run.nCharOfs;
765 *nItemOffset = nOffset;
772 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
774 const int nLen = lstrlenW(text);
775 const int nTextLen = ME_GetTextLength(editor);
778 ME_DisplayItem *item;
779 ME_DisplayItem *para;
780 WCHAR wLastChar = ' ';
782 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
783 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
785 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
786 FIXME("Flags 0x%08lx not implemented\n",
787 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
790 if (chrg->cpMax == -1)
793 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
795 /* when searching up, if cpMin < cpMax, then instead of searching
796 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
797 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
798 * case, it is always bigger than cpMin.
800 if (!(flags & FR_DOWN))
804 nMax = nMin > nTextLen ? nTextLen : nMin;
805 if (nMin < nSwap || chrg->cpMax == -1)
811 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
814 chrgText->cpMin = chrgText->cpMax = -1;
818 if (flags & FR_DOWN) /* Forward search */
820 /* If possible, find the character before where the search starts */
821 if ((flags & FR_WHOLEWORD) && nMin)
824 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
828 chrgText->cpMin = chrgText->cpMax = -1;
831 wLastChar = item->member.run.strText->szData[nStart];
835 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
839 chrgText->cpMin = chrgText->cpMax = -1;
843 para = ME_GetParagraph(item);
845 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
847 ME_DisplayItem *pCurItem = item;
848 int nCurStart = nStart;
851 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
853 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
857 if (nMatched == nLen)
859 ME_DisplayItem *pNextItem = pCurItem;
860 int nNextStart = nCurStart;
863 /* Check to see if next character is a whitespace */
864 if (flags & FR_WHOLEWORD)
866 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
868 pNextItem = ME_FindItemFwd(pCurItem, diRun);
869 nNextStart = -nMatched;
873 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
877 if (isalnumW(wNextChar))
881 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
884 chrgText->cpMin = nStart;
885 chrgText->cpMax = nStart + nLen;
887 TRACE("found at %d-%d\n", nStart, nStart + nLen);
890 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
892 pCurItem = ME_FindItemFwd(pCurItem, diRun);
893 para = ME_GetParagraph(pCurItem);
894 nCurStart = -nMatched;
898 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
903 if (nStart == ME_StrLen(item->member.run.strText))
905 item = ME_FindItemFwd(item, diRun);
906 para = ME_GetParagraph(item);
911 else /* Backward search */
913 /* If possible, find the character after where the search ends */
914 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
917 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
921 chrgText->cpMin = chrgText->cpMax = -1;
924 wLastChar = item->member.run.strText->szData[nEnd];
928 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
932 chrgText->cpMin = chrgText->cpMax = -1;
936 para = ME_GetParagraph(item);
939 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
941 ME_DisplayItem *pCurItem = item;
945 if (nCurEnd - nMatched == 0)
947 pCurItem = ME_FindItemBack(pCurItem, diRun);
948 para = ME_GetParagraph(pCurItem);
949 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
952 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
954 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
958 if (nMatched == nLen)
960 ME_DisplayItem *pPrevItem = pCurItem;
961 int nPrevEnd = nCurEnd;
964 /* Check to see if previous character is a whitespace */
965 if (flags & FR_WHOLEWORD)
967 if (nPrevEnd - nMatched == 0)
969 pPrevItem = ME_FindItemBack(pCurItem, diRun);
971 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
975 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
979 if (isalnumW(wPrevChar))
983 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
986 chrgText->cpMin = nStart;
987 chrgText->cpMax = nStart + nLen;
989 TRACE("found at %d-%d\n", nStart, nStart + nLen);
992 if (nCurEnd - nMatched == 0)
994 pCurItem = ME_FindItemBack(pCurItem, diRun);
995 /* Don't care about pCurItem becoming NULL here; it's already taken
996 * care of in the exterior loop condition */
997 para = ME_GetParagraph(pCurItem);
998 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1002 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1009 item = ME_FindItemBack(item, diRun);
1010 para = ME_GetParagraph(item);
1011 nEnd = ME_StrLen(item->member.run.strText);
1015 TRACE("not found\n");
1017 chrgText->cpMin = chrgText->cpMax = -1;
1023 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1025 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1026 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1038 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1042 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1043 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1045 if (ME_IsSelection(editor))
1046 ME_DeleteSelection(editor);
1047 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1048 ME_DeleteTextAtCursor(editor, 1, 1);
1051 ME_QueueInvalidateFromCursor(editor, 1);
1052 ME_UpdateRepaint(editor);
1053 ME_SendRequestResize(editor, FALSE);
1063 chf.cbSize = sizeof(chf);
1065 ME_GetSelectionCharFormat(editor, &chf);
1066 ME_DumpStyleToBuf(&chf, buf);
1067 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1071 ME_CheckCharOffsets(editor);
1078 static void ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1083 if(!editor->lpOleCallback)
1085 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1086 if(selrange.cpMin == selrange.cpMax)
1087 seltype |= SEL_EMPTY;
1090 /* FIXME: Handle objects */
1091 seltype |= SEL_TEXT;
1092 if(selrange.cpMax-selrange.cpMin > 1)
1093 seltype |= SEL_MULTICHAR;
1095 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1097 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1102 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1103 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1107 ed->bEmulateVersion10 = FALSE;
1108 ed->pBuffer = ME_MakeText();
1110 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1111 ReleaseDC(hWnd, hDC);
1112 ed->bCaretShown = FALSE;
1114 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1115 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1116 ed->pCursors[0].nOffset = 0;
1117 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1118 ed->pCursors[1].nOffset = 0;
1119 ed->nLastTotalLength = ed->nTotalLength = 0;
1122 ed->rgbBackColor = -1;
1123 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1124 ed->bCaretAtEnd = FALSE;
1126 ed->nModifyStep = 0;
1127 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1128 ed->nUndoStackSize = 0;
1129 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1130 ed->nUndoMode = umAddToUndo;
1131 ed->nParagraphs = 1;
1132 ed->nLastSelStart = ed->nLastSelEnd = 0;
1133 ed->nScrollPosY = 0;
1134 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1136 ed->bHideSelection = FALSE;
1137 ed->nInvalidOfs = -1;
1138 ed->pfnWordBreak = NULL;
1139 ed->lpOleCallback = NULL;
1140 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1141 GetClientRect(hWnd, &ed->rcFormat);
1142 for (i=0; i<HFONT_CACHE_SIZE; i++)
1144 ed->pFontCache[i].nRefs = 0;
1145 ed->pFontCache[i].nAge = 0;
1146 ed->pFontCache[i].hFont = NULL;
1148 ME_CheckCharOffsets(ed);
1150 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1151 ed->cPasswordMask = '*';
1153 ed->cPasswordMask = 0;
1158 typedef struct tagME_GlobalDestStruct
1162 } ME_GlobalDestStruct;
1164 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1166 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1171 pDest = (WORD *)lpBuff;
1172 pSrc = (WORD *)GlobalLock(pData->hData);
1173 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1174 pDest[i] = pSrc[pData->nLength+i];
1176 pData->nLength += i;
1178 GlobalUnlock(pData->hData);
1182 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1184 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1189 pSrc = (BYTE *)GlobalLock(pData->hData);
1190 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1191 pDest[i] = pSrc[pData->nLength+i];
1193 pData->nLength += i;
1195 GlobalUnlock(pData->hData);
1200 void ME_DestroyEditor(ME_TextEditor *editor)
1202 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1203 ME_DisplayItem *p = pFirst, *pNext = NULL;
1206 ME_ClearTempStyle(editor);
1207 ME_EmptyUndoStack(editor);
1210 ME_DestroyDisplayItem(p);
1213 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1214 for (i=0; i<HFONT_CACHE_SIZE; i++)
1216 if (editor->pFontCache[i].hFont)
1217 DeleteObject(editor->pFontCache[i].hFont);
1219 DeleteObject(editor->hbrBackground);
1220 if(editor->lpOleCallback)
1221 IUnknown_Release(editor->lpOleCallback);
1226 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1227 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1228 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1229 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1231 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1236 case DLL_PROCESS_ATTACH:
1237 DisableThreadLibraryCalls(hinstDLL);
1238 me_heap = HeapCreate (0, 0x10000, 0);
1239 ME_RegisterEditorClass(hinstDLL);
1242 case DLL_PROCESS_DETACH:
1243 UnregisterClassW(wszClassName, 0);
1244 UnregisterClassW(wszClassName50, 0);
1245 UnregisterClassA("RichEdit20A", 0);
1246 UnregisterClassA("RichEdit50A", 0);
1247 if (ME_ListBoxRegistered)
1248 UnregisterClassW(wszClassNameListBox, 0);
1249 if (ME_ComboBoxRegistered)
1250 UnregisterClassW(wszClassNameComboBox, 0);
1251 HeapDestroy (me_heap);
1259 #define UNSUPPORTED_MSG(e) \
1261 FIXME(#e ": stub\n"); \
1262 return DefWindowProcW(hWnd, msg, wParam, lParam);
1264 static const char * const edit_messages[] = {
1293 "EM_SETPASSWORDCHAR",
1294 "EM_EMPTYUNDOBUFFER",
1295 "EM_GETFIRSTVISIBLELINE",
1297 "EM_SETWORDBREAKPROC",
1298 "EM_GETWORDBREAKPROC",
1299 "EM_GETPASSWORDCHAR",
1307 static const char * const richedit_messages[] = {
1312 "EM_EXLINEFROMCHAR",
1318 "EM_GETOLEINTERFACE",
1328 "EM_SETOLECALLBACK",
1330 "EM_SETTARGETDEVICE",
1338 "EM_GETWORDBREAKPROCEX",
1339 "EM_SETWORDBREAKPROCEX",
1341 "EM_UNKNOWN_USER_83",
1346 "EM_STOPGROUPTYPING",
1350 "EM_GETAUTOURLDETECT",
1353 "EM_GETTEXTLENGTHEX",
1356 "EM_UNKNOWN_USER_98",
1357 "EM_UNKNOWN_USER_99",
1358 "EM_SETPUNCTUATION",
1359 "EM_GETPUNCTUATION",
1360 "EM_SETWORDWRAPMODE",
1361 "EM_GETWORDWRAPMODE",
1367 "EM_UNKNOWN_USER_109",
1368 "EM_UNKNOWN_USER_110",
1369 "EM_UNKNOWN_USER_111",
1370 "EM_UNKNOWN_USER_112",
1371 "EM_UNKNOWN_USER_113",
1372 "EM_UNKNOWN_USER_114",
1373 "EM_UNKNOWN_USER_115",
1374 "EM_UNKNOWN_USER_116",
1375 "EM_UNKNOWN_USER_117",
1376 "EM_UNKNOWN_USER_118",
1377 "EM_UNKNOWN_USER_119",
1378 "EM_SETLANGOPTIONS",
1379 "EM_GETLANGOPTIONS",
1380 "EM_GETIMECOMPMODE",
1384 "EM_SETIMEMODEBIAS",
1389 get_msg_name(UINT msg)
1391 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1392 return edit_messages[msg - EM_GETSEL];
1393 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1394 return richedit_messages[msg - EM_CANPASTE];
1398 /******************************************************************
1399 * RichEditANSIWndProc (RICHED20.10)
1401 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1403 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1405 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1406 hWnd, msg, get_msg_name(msg), wParam, lParam);
1408 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1409 ERR("RichEditANSIWndProc called with invalid hWnd %p - application bug?\n", hWnd);
1415 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1416 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1417 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1418 UNSUPPORTED_MSG(EM_FMTLINES)
1419 UNSUPPORTED_MSG(EM_FORMATRANGE)
1420 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1421 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1422 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1423 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1424 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1425 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1426 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1427 UNSUPPORTED_MSG(EM_GETREDONAME)
1428 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1429 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1430 UNSUPPORTED_MSG(EM_GETUNDONAME)
1431 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1432 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1433 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1434 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1435 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1436 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1437 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1438 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1439 UNSUPPORTED_MSG(EM_SETPALETTE)
1440 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1441 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1442 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1443 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1444 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1445 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1446 UNSUPPORTED_MSG(WM_STYLECHANGING)
1447 UNSUPPORTED_MSG(WM_STYLECHANGED)
1448 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1450 /* Messages specific to Richedit controls */
1453 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1455 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1458 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1459 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1460 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1465 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1466 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1467 editor = ME_MakeEditor(hWnd);
1468 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1469 pcs = 0; /* ignore */
1472 case EM_EMPTYUNDOBUFFER:
1473 ME_EmptyUndoStack(editor);
1477 /* Note: wParam/lParam can be NULL */
1479 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1480 PUINT pto = lParam ? (PUINT)lParam : &to;
1481 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1482 if ((*pfrom|*pto) & 0xFFFF0000)
1484 return MAKELONG(*pfrom,*pto);
1488 CHARRANGE *pRange = (CHARRANGE *)lParam;
1489 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1490 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1493 case EM_SETUNDOLIMIT:
1495 if ((int)wParam < 0)
1496 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1498 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1499 /* Setting a max stack size keeps wine from getting killed
1500 for hogging memory. Windows allocates all this memory at once, so
1501 no program would realistically set a value above our maxiumum. */
1502 return editor->nUndoLimit;
1505 return editor->pUndoStack != NULL;
1507 return editor->pRedoStack != NULL;
1508 case WM_UNDO: /* FIXME: actually not the same */
1517 /* these flags are equivalent to the ES_* counterparts */
1518 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1519 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1520 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1526 /* these flags are equivalent to ES_* counterparts
1527 * ECO_READONLY is already implemented in the code, only requires
1528 * setting the bit to work
1530 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1531 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1532 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1533 DWORD settings = mask & raw;
1549 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1551 if (lParam & ECO_AUTOWORDSELECTION)
1552 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1553 if (lParam & ECO_SELECTIONBAR)
1554 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1555 if (lParam & ECO_VERTICAL)
1556 FIXME("ECO_VERTICAL not implemented yet!\n");
1557 if (lParam & ECO_AUTOHSCROLL)
1558 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1559 if (lParam & ECO_AUTOVSCROLL)
1560 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1561 if (lParam & ECO_NOHIDESEL)
1562 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1563 if (lParam & ECO_WANTRETURN)
1564 FIXME("ECO_WANTRETURN not implemented yet!\n");
1570 ME_InvalidateSelection(editor);
1571 ME_SetSelection(editor, wParam, lParam);
1572 ME_InvalidateSelection(editor);
1573 ME_SendSelChange(editor);
1576 case EM_AUTOURLDETECT:
1578 if (wParam==1 || wParam ==0)
1580 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1583 return E_INVALIDARG;
1585 case EM_GETAUTOURLDETECT:
1587 return editor->AutoURLDetect_bEnable;
1591 CHARRANGE *pRange = (CHARRANGE *)lParam;
1592 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1593 ME_InvalidateSelection(editor);
1594 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1595 ME_InvalidateSelection(editor);
1596 ME_SendSelChange(editor);
1601 LPWSTR wszText = (LPWSTR)lParam;
1602 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1603 size_t len = lstrlenW(wszText);
1606 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1607 if (pStruct->codepage != 1200) {
1608 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1611 /* FIXME: this should support RTF strings too, according to MSDN */
1612 if (pStruct->flags & ST_SELECTION) {
1613 ME_GetSelection(editor, &from, &to);
1614 style = ME_GetSelectionInsertStyle(editor);
1615 ME_InternalDeleteText(editor, from, to - from);
1616 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1617 ME_ReleaseStyle(style);
1620 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1621 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1624 ME_CommitUndo(editor);
1625 if (!(pStruct->flags & ST_KEEPUNDO))
1626 ME_EmptyUndoStack(editor);
1627 ME_UpdateRepaint(editor);
1630 case EM_SETBKGNDCOLOR:
1632 LRESULT lColor = ME_GetBackColor(editor);
1633 if (editor->rgbBackColor != -1)
1634 DeleteObject(editor->hbrBackground);
1637 editor->rgbBackColor = -1;
1638 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1642 editor->rgbBackColor = lParam;
1643 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1645 if (editor->bRedraw)
1647 InvalidateRect(hWnd, NULL, TRUE);
1653 return editor->nModifyStep == 0 ? 0 : 1;
1657 editor->nModifyStep = 0x80000000;
1659 editor->nModifyStep = 0;
1663 case EM_SETREADONLY:
1665 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1667 nStyle |= ES_READONLY;
1669 nStyle &= ~ES_READONLY;
1670 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1673 case EM_SETEVENTMASK:
1675 DWORD nOldMask = editor->nEventMask;
1677 editor->nEventMask = lParam;
1680 case EM_GETEVENTMASK:
1681 return editor->nEventMask;
1682 case EM_SETCHARFORMAT:
1684 CHARFORMAT2W buf, *p;
1685 BOOL bRepaint = TRUE;
1686 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1687 if (!wParam || (editor->mode & TM_PLAINTEXT))
1688 ME_SetDefaultCharFormat(editor, p);
1689 else if (wParam == (SCF_WORD | SCF_SELECTION))
1690 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1691 else if (wParam == SCF_ALL)
1692 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1695 ME_GetSelection(editor, &from, &to);
1696 bRepaint = (from != to);
1697 ME_SetSelectionCharFormat(editor, p);
1699 ME_CommitUndo(editor);
1701 ME_UpdateRepaint(editor);
1704 case EM_GETCHARFORMAT:
1706 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1707 if (dst->cbSize != sizeof(CHARFORMATA) &&
1708 dst->cbSize != sizeof(CHARFORMATW) &&
1709 dst->cbSize != sizeof(CHARFORMAT2A) &&
1710 dst->cbSize != sizeof(CHARFORMAT2W))
1712 tmp.cbSize = sizeof(tmp);
1714 ME_GetDefaultCharFormat(editor, &tmp);
1716 ME_GetSelectionCharFormat(editor, &tmp);
1717 ME_CopyToCFAny(dst, &tmp);
1720 case EM_SETPARAFORMAT:
1721 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1722 ME_UpdateRepaint(editor);
1723 ME_CommitUndo(editor);
1725 case EM_GETPARAFORMAT:
1726 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1728 case EM_GETFIRSTVISIBLELINE:
1730 ME_DisplayItem *p = editor->pBuffer->pFirst;
1731 int y = editor->nScrollPosY;
1736 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1737 if (p->type == diTextEnd)
1739 if (p->type == diParagraph) {
1740 ypara = p->member.para.nYPos;
1743 ystart = ypara + p->member.row.nYPos;
1744 yend = ystart + p->member.row.nHeight;
1752 case EM_HIDESELECTION:
1754 editor->bHideSelection = (wParam != 0);
1755 ME_InvalidateSelection(editor);
1760 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1761 nPos += 8 * lParam; /* FIXME follow the original */
1766 if (nPos != editor->nScrollPosY) {
1767 int dy = editor->nScrollPosY - nPos;
1768 editor->nScrollPosY = nPos;
1769 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1770 if (editor->bRedraw)
1772 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1776 return TRUE; /* Should return false if a single line richedit control */
1781 ME_GetSelection(editor, &from, &to);
1782 ME_InternalDeleteText(editor, from, to-from);
1783 ME_CommitUndo(editor);
1784 ME_UpdateRepaint(editor);
1791 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1792 size_t len = lstrlenW(wszText);
1793 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1795 ME_GetSelection(editor, &from, &to);
1796 style = ME_GetSelectionInsertStyle(editor);
1797 ME_InternalDeleteText(editor, from, to-from);
1798 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1799 ME_ReleaseStyle(style);
1800 ME_EndToUnicode(hWnd, wszText);
1801 /* drop temporary style if line end */
1802 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1803 if (len>0 && wszText[len-1] == '\n')
1804 ME_ClearTempStyle(editor);
1806 ME_CommitUndo(editor);
1808 ME_EmptyUndoStack(editor);
1809 ME_UpdateRepaint(editor);
1812 case EM_SCROLLCARET:
1814 int top, bottom; /* row's edges relative to document top */
1815 ME_DisplayItem *para, *row;
1817 row = ME_RowStart(editor->pCursors[0].pRun);
1818 para = ME_GetParagraph(row);
1819 top = para->member.para.nYPos + row->member.row.nYPos;
1820 bottom = top + row->member.row.nHeight;
1822 if ((top < editor->nScrollPosY)
1823 || (editor->nScrollPosY + editor->sizeWindow.cy < bottom))
1826 int prevScrollPosY = editor->nScrollPosY;
1828 if (top < editor->nScrollPosY) /* caret above window */
1829 editor->nScrollPosY = top;
1830 else /* caret below window */
1831 editor->nScrollPosY = bottom - editor->sizeWindow.cy;
1833 if (editor->nScrollPosY < 0)
1834 editor->nScrollPosY = 0;
1836 dy = prevScrollPosY - editor->nScrollPosY;
1837 SetScrollPos(hWnd, SB_VERT, editor->nScrollPosY, TRUE);
1838 if (editor->bRedraw)
1840 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1852 BOOL bRepaint = LOWORD(lParam);
1855 wParam = (WPARAM)GetStockObject(DEFAULT_GUI_FONT);
1856 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1858 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1859 ReleaseDC(hWnd, hDC);
1860 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1861 ME_SetDefaultCharFormat(editor, &fmt);
1863 ME_CommitUndo(editor);
1865 ME_UpdateRepaint(editor);
1870 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1873 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1874 if (!IsWindowUnicode(hWnd) && !strncmp((char *)lParam, "{\\rtf1", 6))
1876 /* Undocumented: WM_SETTEXT supports RTF text */
1877 ME_StreamInRTFString(editor, 0, (char *)lParam);
1881 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1882 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1883 if (lstrlenW(wszText) > 0)
1885 /* uses default style! */
1886 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1888 ME_EndToUnicode(hWnd, wszText);
1892 TRACE("WM_SETTEXT - NULL\n");
1893 ME_CommitUndo(editor);
1894 ME_EmptyUndoStack(editor);
1895 ME_SetSelection(editor, 0, 0);
1896 ME_UpdateRepaint(editor);
1901 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1902 if (IsClipboardFormatAvailable(nRTFFormat))
1904 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1912 ME_GlobalDestStruct gds;
1913 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1916 if (IsClipboardFormatAvailable(nRTFFormat))
1917 cf = nRTFFormat, dwFormat = SF_RTF;
1918 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1919 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1923 if (!OpenClipboard(hWnd))
1925 gds.hData = GetClipboardData(cf);
1927 es.dwCookie = (DWORD)&gds;
1928 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1929 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1937 LPDATAOBJECT dataObj = NULL;
1941 if (editor->cPasswordMask)
1942 return 0; /* Copying or Cutting masked text isn't allowed */
1944 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1945 if(editor->lpOleCallback)
1946 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1947 if(FAILED(hr) || !dataObj)
1948 hr = ME_GetDataObject(editor, &range, &dataObj);
1950 hr = OleSetClipboard(dataObj);
1951 IDataObject_Release(dataObj);
1953 if (SUCCEEDED(hr) && msg == WM_CUT)
1955 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1956 ME_CommitUndo(editor);
1957 ME_UpdateRepaint(editor);
1961 case WM_GETTEXTLENGTH:
1962 return ME_GetTextLength(editor);
1963 case EM_GETTEXTLENGTHEX:
1964 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1967 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1969 tr.chrg.cpMax = wParam-1;
1970 tr.lpstrText = (WCHAR *)lParam;
1971 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1975 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1978 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1979 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1981 if (ex->flags & GT_SELECTION)
1983 ME_GetSelection(editor, &nStart, &nCount);
1985 nCount = min(nCount, ex->cb - 1);
1990 nCount = ex->cb - 1;
1992 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1994 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1995 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1999 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2000 we can just take a bigger buffer? :) */
2001 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2002 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
2003 DWORD buflen = ex->cb;
2007 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2008 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2010 HeapFree(GetProcessHeap(),0,buffer);
2017 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2018 ME_GetSelection(editor, &from, &to);
2019 tr.chrg.cpMin = from;
2021 tr.lpstrText = (WCHAR *)lParam;
2022 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2024 case EM_GETSCROLLPOS:
2026 POINT *point = (POINT *)lParam;
2027 point->x = 0; /* FIXME side scrolling not implemented */
2028 point->y = editor->nScrollPosY;
2031 case EM_GETTEXTRANGE:
2033 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2034 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
2035 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
2036 editor->bEmulateVersion10, ME_GetTextLength(editor));
2037 if (IsWindowUnicode(hWnd))
2038 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2041 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2042 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2043 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2044 /* FIXME this is a potential security hole (buffer overrun)
2045 if you know more about wchar->mbyte conversion please explain
2047 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2054 ME_DisplayItem *run;
2055 const BOOL bUnicode = IsWindowUnicode(hWnd);
2056 const unsigned int nMaxChars = *(WORD *) lParam;
2057 unsigned int nEndChars, nCharsLeft = nMaxChars;
2058 char *dest = (char *) lParam;
2059 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2060 we need to know how if we have the extra \n or not */
2061 int nLF = editor->bEmulateVersion10;
2063 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2064 bUnicode ? "Unicode" : "Ansi");
2066 run = ME_FindRowWithNumber(editor, wParam);
2070 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2071 && !(run->member.run.nFlags & MERF_ENDPARA))
2075 if (run->type != diRun)
2077 strText = run->member.run.strText;
2078 nCopy = min(nCharsLeft, strText->nLen);
2081 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2083 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2084 nCharsLeft, NULL, NULL);
2085 dest += nCopy * (bUnicode ? sizeof(WCHAR) : 1);
2086 nCharsLeft -= nCopy;
2089 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2090 nEndChars = min(nCharsLeft, 2 + nLF);
2091 nCharsLeft -= nEndChars;
2094 const WCHAR src[] = {'\r', '\0'};
2095 const WCHAR src10[] = {'\r', '\n', '\0'};
2096 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2099 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2101 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2103 if (nEndChars == 2 + nLF)
2104 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2106 return nMaxChars - nCharsLeft;
2108 case EM_GETLINECOUNT:
2110 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2113 while (item != editor->pBuffer->pLast)
2115 assert(item->type == diParagraph);
2116 nRows += item->member.para.nRows;
2117 item = item->member.para.next_para;
2119 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2120 return max(1, nRows);
2122 case EM_LINEFROMCHAR:
2125 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2127 return ME_RowNumberFromCharOfs(editor, wParam);
2129 case EM_EXLINEFROMCHAR:
2131 return ME_RowNumberFromCharOfs(editor, lParam);
2135 ME_DisplayItem *item, *para;
2139 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2141 item = ME_FindRowWithNumber(editor, wParam);
2144 para = ME_GetParagraph(item);
2145 item = ME_FindItemFwd(item, diRun);
2146 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2147 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2152 ME_DisplayItem *item, *item_end;
2153 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2155 if (wParam > ME_GetTextLength(editor))
2159 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2162 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2163 item = ME_RowStart(item);
2164 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2165 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2166 if (item_end->type == diStartRow)
2167 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2169 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2170 - (editor->bEmulateVersion10?2:1);
2171 nChars = nNextLineOfs - nThisLineOfs;
2172 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2177 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2178 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2181 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2182 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2183 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2187 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2188 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2191 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2192 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2193 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2197 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2198 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2200 case EM_FINDTEXTEXW:
2202 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2203 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2206 if (!wParam || !lParam)
2208 *(int *)wParam = editor->nZoomNumerator;
2209 *(int *)lParam = editor->nZoomDenominator;
2212 return ME_SetZoom(editor, wParam, lParam);
2213 case EM_CHARFROMPOS:
2214 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2215 case EM_POSFROMCHAR:
2217 ME_DisplayItem *pRun;
2218 int nCharOfs, nOffset, nLength;
2222 /* detect which API version we're dealing with */
2223 if (wParam >= 0x40000)
2225 nLength = ME_GetTextLength(editor);
2227 if (nCharOfs < nLength) {
2228 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2229 assert(pRun->type == diRun);
2230 pt.y = pRun->member.run.pt.y;
2231 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2232 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2235 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2237 if (wParam >= 0x40000) {
2238 *(POINTL *)wParam = pt;
2240 return MAKELONG( pt.x, pt.y );
2243 ME_CommitUndo(editor);
2244 ME_WrapMarkedParagraphs(editor);
2245 ME_MoveCaret(editor);
2248 ME_DestroyEditor(editor);
2249 SetWindowLongPtrW(hWnd, 0, 0);
2251 case WM_LBUTTONDOWN:
2253 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2257 if (GetCapture() == hWnd)
2258 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2261 if (GetCapture() == hWnd)
2264 case WM_LBUTTONDBLCLK:
2265 ME_SelectWord(editor);
2267 case WM_CONTEXTMENU:
2268 ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2271 if (editor->bRedraw)
2276 hDC = BeginPaint(hWnd, &ps);
2277 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2278 EndPaint(hWnd, &ps);
2282 ME_ShowCaret(editor);
2283 ME_SendOldNotify(editor, EN_SETFOCUS);
2286 ME_HideCaret(editor);
2287 ME_SendOldNotify(editor, EN_KILLFOCUS);
2291 if (editor->bRedraw)
2293 HDC hDC = (HDC)wParam;
2295 if (GetUpdateRect(hWnd,&rc,TRUE))
2297 FillRect(hDC, &rc, editor->hbrBackground);
2303 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2306 if (ME_KeyDown(editor, LOWORD(wParam)))
2311 WCHAR wstr = LOWORD(wParam);
2312 if (editor->AutoURLDetect_bEnable)
2313 ME_AutoURLDetect(editor, wstr);
2317 case 1: /* Ctrl-A */
2318 ME_SetSelection(editor, 0, -1);
2320 case 3: /* Ctrl-C */
2321 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2325 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2326 MessageBeep(MB_ICONERROR);
2327 return 0; /* FIXME really 0 ? */
2332 case 22: /* Ctrl-V */
2333 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2335 case 24: /* Ctrl-X */
2336 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2338 case 25: /* Ctrl-Y */
2339 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2341 case 26: /* Ctrl-Z */
2342 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2345 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2346 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2347 ME_Style *style = ME_GetInsertStyle(editor, 0);
2348 ME_SaveTempStyle(editor);
2349 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2350 ME_ReleaseStyle(style);
2351 ME_CommitUndo(editor);
2352 ME_UpdateRepaint(editor);
2356 case EM_SCROLL: /* fall through */
2360 int nPos = editor->nScrollPosY;
2361 int origNPos = nPos;
2362 int lineHeight = 24;
2363 si.cbSize = sizeof(SCROLLINFO);
2364 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
2365 GetScrollInfo(hWnd, SB_VERT, &si);
2366 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2367 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2368 if (lineHeight <= 0) lineHeight = 24;
2369 switch(LOWORD(wParam)) {
2372 if (nPos<0) nPos = 0;
2376 nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2377 if (nEnd < 0) nEnd = 0;
2379 if (nPos>=nEnd) nPos = nEnd;
2383 nPos -= editor->sizeWindow.cy;
2384 if (nPos<0) nPos = 0;
2387 nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2388 if (nEnd < 0) nEnd = 0;
2389 nPos += editor->sizeWindow.cy;
2390 if (nPos>=nEnd) nPos = nEnd;
2393 case SB_THUMBPOSITION:
2394 nPos = si.nTrackPos;
2397 if (nPos != editor->nScrollPosY) {
2398 int dy = editor->nScrollPosY - nPos;
2399 editor->nScrollPosY = nPos;
2400 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
2401 if (editor->bRedraw)
2403 ScrollWindow(hWnd, 0, dy, NULL, NULL);
2407 if (msg == EM_SCROLL)
2408 return 0x00010000 | (((nPos - origNPos)/lineHeight) & 0xffff);
2413 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2414 UINT pulScrollLines;
2415 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2416 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
2417 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2418 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
2423 if (nPos != editor->nScrollPosY) {
2424 int dy = editor->nScrollPosY - nPos;
2425 editor->nScrollPosY = nPos;
2426 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
2427 if (editor->bRedraw)
2429 ScrollWindow(hWnd, 0, dy, NULL, NULL);
2437 *((RECT *)lParam) = editor->rcFormat;
2445 RECT *rc = (RECT *)lParam;
2449 editor->rcFormat.left += rc->left;
2450 editor->rcFormat.top += rc->top;
2451 editor->rcFormat.right += rc->right;
2452 editor->rcFormat.bottom += rc->bottom;
2456 editor->rcFormat = *rc;
2461 GetClientRect(hWnd, &editor->rcFormat);
2463 if (msg != EM_SETRECTNP)
2464 ME_RewrapRepaint(editor);
2467 case EM_REQUESTRESIZE:
2468 ME_SendRequestResize(editor, TRUE);
2471 editor->bRedraw = wParam;
2475 GetClientRect(hWnd, &editor->rcFormat);
2476 ME_RewrapRepaint(editor);
2477 return DefWindowProcW(hWnd, msg, wParam, lParam);
2479 case EM_GETOLEINTERFACE:
2481 LPVOID *ppvObj = (LPVOID*) lParam;
2482 return CreateIRichEditOle(editor, ppvObj);
2484 case EM_GETPASSWORDCHAR:
2486 return editor->cPasswordMask;
2488 case EM_SETOLECALLBACK:
2489 if(editor->lpOleCallback)
2490 IUnknown_Release(editor->lpOleCallback);
2491 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2492 if(editor->lpOleCallback)
2493 IUnknown_AddRef(editor->lpOleCallback);
2495 case EM_GETWORDBREAKPROC:
2496 return (LRESULT)editor->pfnWordBreak;
2497 case EM_SETWORDBREAKPROC:
2499 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2501 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2502 return (LRESULT)pfnOld;
2504 case EM_SETTEXTMODE:
2509 if ((ret = RichEditANSIWndProc(hWnd, WM_GETTEXTLENGTH, 0, 0)) == 0)
2511 /*Check for valid wParam*/
2512 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2513 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2514 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2518 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2520 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2521 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2523 /*FIXME: Currently no support for undo level and code page options*/
2524 editor->mode = (editor->mode & (~mask)) | changes;
2530 case EM_SETPASSWORDCHAR:
2532 editor->cPasswordMask = wParam;
2533 ME_RewrapRepaint(editor);
2538 return DefWindowProcW(hWnd, msg, wParam, lParam);
2544 /******************************************************************
2545 * RichEdit10ANSIWndProc (RICHED20.9)
2547 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2551 /* FIXME: this is NOT the same as 2.0 version */
2552 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2553 if (msg == WM_NCCREATE)
2555 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2557 editor->bEmulateVersion10 = TRUE;
2558 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2563 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2565 HWND hWnd = editor->hWnd;
2566 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2569 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2571 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2574 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2575 item = item->member.para.next_para;
2578 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2579 item = item->member.para.next_para;
2586 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2588 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2590 WCHAR *pStart = buffer;
2599 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2602 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2609 item = ME_FindItemFwd(item, diRun);
2612 while(nChars && item)
2614 int nLen = ME_StrLen(item->member.run.strText);
2618 if (item->member.run.nFlags & MERF_ENDPARA)
2620 if (!ME_FindItemFwd(item, diRun))
2621 /* No '\r' is appended to the last paragraph. */
2631 /* our end paragraph consists of 2 characters now */
2632 if (editor->bEmulateVersion10)
2637 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2644 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2648 item = ME_FindItemFwd(item, diRun);
2651 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2655 void ME_RegisterEditorClass(HINSTANCE hInstance)
2661 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2662 wcW.lpfnWndProc = RichEditANSIWndProc;
2664 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2665 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2667 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2668 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2669 wcW.lpszMenuName = NULL;
2670 wcW.lpszClassName = wszClassName;
2671 bResult = RegisterClassW(&wcW);
2673 wcW.lpszClassName = wszClassName50;
2674 bResult = RegisterClassW(&wcW);
2677 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2678 wcA.lpfnWndProc = RichEditANSIWndProc;
2680 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2681 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2683 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2684 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2685 wcA.lpszMenuName = NULL;
2686 wcA.lpszClassName = "RichEdit20A";
2687 bResult = RegisterClassA(&wcA);
2689 wcA.lpszClassName = "RichEdit50A";
2690 bResult = RegisterClassA(&wcA);
2694 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2695 /* FIXME: Not implemented */
2696 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2697 hWnd, msg, get_msg_name(msg), wParam, lParam);
2698 return DefWindowProcW(hWnd, msg, wParam, lParam);
2701 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2702 /* FIXME: Not implemented */
2703 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2704 hWnd, msg, get_msg_name(msg), wParam, lParam);
2705 return DefWindowProcW(hWnd, msg, wParam, lParam);
2708 /******************************************************************
2709 * REExtendedRegisterClass (RICHED20.8)
2711 * FIXME undocumented
2712 * Need to check for errors and implement controls and callbacks
2714 LRESULT WINAPI REExtendedRegisterClass(void)
2719 FIXME("semi stub\n");
2723 wcW.hInstance = NULL;
2726 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2727 wcW.lpszMenuName = NULL;
2729 if (!ME_ListBoxRegistered)
2731 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2732 wcW.lpfnWndProc = REListWndProc;
2733 wcW.lpszClassName = wszClassNameListBox;
2734 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2737 if (!ME_ComboBoxRegistered)
2739 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2740 wcW.lpfnWndProc = REComboWndProc;
2741 wcW.lpszClassName = wszClassNameComboBox;
2742 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2746 if (ME_ListBoxRegistered)
2748 if (ME_ComboBoxRegistered)
2754 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2774 int curf_ef, link_ef, def_ef;
2775 int cur_prefx, prefx_cnt;
2776 int sel_min, sel_max;
2779 int URLmin, URLmax = 0;
2782 CHARFORMAT2W cur_format;
2783 CHARFORMAT2W default_format;
2785 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2786 sel_min = ins_pt.cpMin;
2787 sel_max = ins_pt.cpMax;
2788 if (sel_min==sel_max)
2790 if (sel_min!=sel_max)
2791 car_pos = ME_GetTextLength(editor)+1;
2792 cur_format.cbSize = sizeof(cur_format);
2793 default_format.cbSize = sizeof(default_format);
2794 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2795 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2796 link.cbSize = sizeof(link);
2797 link.dwMask = CFM_LINK | CFM_COLOR | CFM_UNDERLINE;
2798 link.dwEffects = CFE_LINK | CFE_UNDERLINE;
2799 link.crTextColor = RGB(0,0,255);
2800 curf_ef = cur_format.dwEffects & link.dwEffects;
2801 def_ef = default_format.dwEffects & link.dwEffects;
2802 link_ef = link.dwEffects & link.dwEffects;
2803 if (curf_ef == link_ef)
2805 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2807 ME_SetSelection(editor, car_pos, car_pos);
2808 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2813 if (curf_ef == def_ef)
2816 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2817 while (cur_prefx<=prefx_cnt)
2821 ft.lpstrText = prefixes[cur_prefx].text;
2822 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2823 URLmax=max(0, car_pos);
2824 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2826 URLmax = ME_GetTextLength(editor)+1;
2828 ft.chrg.cpMin = URLmin;
2829 ft.chrg.cpMax = URLmax;
2830 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2836 url.cpMax=car_pos-1;
2837 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);