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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
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)
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?
229 #define NO_SHLWAPI_STREAM
234 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
237 HANDLE me_heap = NULL;
239 static BOOL ME_ListBoxRegistered = FALSE;
240 static BOOL ME_ComboBoxRegistered = FALSE;
242 static ME_TextBuffer *ME_MakeText(void) {
244 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
246 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
247 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
253 p1->member.para.next_para = p2;
254 p2->member.para.prev_para = p1;
255 p2->member.para.nCharOfs = 0;
259 buf->pCharStyle = NULL;
265 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
267 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
270 TRACE("%08lx %p\n", dwFormat, stream);
277 ME_StreamInFill(stream);
278 if (stream->editstream->dwError)
284 if (!(dwFormat & SF_UNICODE))
286 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
287 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
292 nWideChars = stream->dwSize >> 1;
293 pText = (WCHAR *)stream->buffer;
296 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
297 if (stream->dwSize == 0)
301 ME_CommitUndo(editor);
302 ME_UpdateRepaint(editor);
306 static void ME_RTFCharAttrHook(RTF_Info *info)
309 fmt.cbSize = sizeof(fmt);
312 switch(info->rtfMinor)
315 /* FIXME add more flags once they're implemented */
316 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
317 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
318 fmt.yHeight = 12*20; /* 12pt */
322 fmt.dwMask = CFM_BOLD;
323 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
326 fmt.dwMask = CFM_ITALIC;
327 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
330 fmt.dwMask = CFM_UNDERLINE;
331 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
332 fmt.bUnderlineType = CFU_CF1UNDERLINE;
335 fmt.dwMask = CFM_UNDERLINE;
339 fmt.dwMask = CFM_STRIKEOUT;
340 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
344 case rtfSubScrShrink:
345 case rtfSuperScrShrink:
347 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
348 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
349 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
350 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
353 fmt.dwMask = CFM_BACKCOLOR;
355 if (info->rtfParam == 0)
356 fmt.dwEffects = CFE_AUTOBACKCOLOR;
357 else if (info->rtfParam != rtfNoParam)
359 RTFColor *c = RTFGetColor(info, info->rtfParam);
360 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
364 fmt.dwMask = CFM_COLOR;
366 if (info->rtfParam == 0)
367 fmt.dwEffects = CFE_AUTOCOLOR;
368 else if (info->rtfParam != rtfNoParam)
370 RTFColor *c = RTFGetColor(info, info->rtfParam);
371 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
375 if (info->rtfParam != rtfNoParam)
377 RTFFont *f = RTFGetFont(info, info->rtfParam);
380 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
381 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
382 fmt.bCharSet = f->rtfFCharSet;
383 fmt.dwMask = CFM_FACE | CFM_CHARSET;
388 fmt.dwMask = CFM_SIZE;
389 if (info->rtfParam != rtfNoParam)
390 fmt.yHeight = info->rtfParam*10;
395 RTFFlushOutputBuffer(info);
396 /* FIXME too slow ? how come ? */
397 style2 = ME_ApplyStyle(info->style, &fmt);
398 ME_ReleaseStyle(info->style);
399 info->style = style2;
400 info->styleChanged = TRUE;
404 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
405 the same tags mean different things in different contexts */
406 static void ME_RTFParAttrHook(RTF_Info *info)
409 fmt.cbSize = sizeof(fmt);
412 switch(info->rtfMinor)
414 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
415 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
416 fmt.wAlignment = PFA_LEFT;
418 fmt.dxOffset = fmt.dxStartIndent = 0;
421 ME_GetSelectionParaFormat(info->editor, &fmt);
422 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
423 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
424 fmt.dxOffset = -info->rtfParam;
427 ME_GetSelectionParaFormat(info->editor, &fmt);
428 fmt.dwMask = PFM_STARTINDENT;
429 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
432 fmt.dwMask = PFM_RIGHTINDENT;
433 fmt.dxRightIndent = info->rtfParam;
437 fmt.dwMask = PFM_ALIGNMENT;
438 fmt.wAlignment = PFA_LEFT;
441 fmt.dwMask = PFM_ALIGNMENT;
442 fmt.wAlignment = PFA_RIGHT;
445 fmt.dwMask = PFM_ALIGNMENT;
446 fmt.wAlignment = PFA_CENTER;
449 ME_GetSelectionParaFormat(info->editor, &fmt);
450 if (!(fmt.dwMask & PFM_TABSTOPS))
452 fmt.dwMask |= PFM_TABSTOPS;
455 if (fmt.cTabCount < MAX_TAB_STOPS)
456 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
460 RTFFlushOutputBuffer(info);
461 /* FIXME too slow ? how come ?*/
462 ME_SetSelectionParaFormat(info->editor, &fmt);
466 static void ME_RTFReadHook(RTF_Info *info) {
467 switch(info->rtfClass)
470 switch(info->rtfMajor)
473 if (info->stackTop < maxStack) {
474 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
475 info->stack[info->stackTop].codePage = info->codePage;
476 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
479 info->styleChanged = FALSE;
484 RTFFlushOutputBuffer(info);
485 if (info->stackTop<=1) {
486 info->rtfClass = rtfEOF;
490 assert(info->stackTop >= 0);
491 if (info->styleChanged)
493 /* FIXME too slow ? how come ? */
494 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
495 ME_ReleaseStyle(info->style);
497 info->codePage = info->stack[info->stackTop].codePage;
498 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
505 switch(info->rtfMajor)
508 ME_RTFCharAttrHook(info);
511 ME_RTFParAttrHook(info);
519 ME_StreamInFill(ME_InStream *stream)
521 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
522 (BYTE *)stream->buffer,
523 sizeof(stream->buffer),
524 (LONG *)&stream->dwSize);
528 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
532 int from, to, to2, nUndoMode;
534 int nEventMask = editor->nEventMask;
535 ME_InStream inStream;
537 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
538 editor->nEventMask = 0;
540 ME_GetSelection(editor, &from, &to);
541 if (format & SFF_SELECTION) {
542 style = ME_GetSelectionInsertStyle(editor);
544 ME_InternalDeleteText(editor, from, to-from);
547 style = editor->pBuffer->pDefaultStyle;
548 ME_AddRefStyle(style);
549 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
550 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
552 ME_ClearTempStyle(editor);
553 /* FIXME restore default paragraph formatting ! */
556 nUndoMode = editor->nUndoMode;
557 editor->nUndoMode = umIgnore;
559 inStream.editstream = stream;
560 inStream.editstream->dwError = 0;
566 /* Check if it's really RTF, and if it is not, use plain text */
567 ME_StreamInFill(&inStream);
568 if (!inStream.editstream->dwError)
570 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
578 if (!inStream.editstream->dwError)
580 if (format & SF_RTF) {
581 /* setup the RTF parser */
582 memset(&parser, 0, sizeof parser);
583 RTFSetEditStream(&parser, &inStream);
584 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
585 parser.hwndEdit = editor->hWnd;
586 parser.editor = editor;
587 parser.style = style;
590 RTFSetReadHook(&parser, ME_RTFReadHook);
595 RTFFlushOutputBuffer(&parser);
598 style = parser.style;
600 else if (format & SF_TEXT)
601 ME_StreamInText(editor, format, &inStream, style);
603 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
604 ME_GetSelection(editor, &to, &to2);
605 /* put the cursor at the top */
606 if (!(format & SFF_SELECTION))
607 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
610 editor->nUndoMode = nUndoMode;
611 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
612 TRACE("from %d to %d\n", from, to);
613 if (pUI && from < to)
618 ME_CommitUndo(editor);
619 ME_ReleaseStyle(style);
620 editor->nEventMask = nEventMask;
623 ME_UpdateRepaint(editor);
625 if (!(format & SFF_SELECTION)) {
626 ME_ClearTempStyle(editor);
628 ME_MoveCaret(editor);
629 ME_SendSelChange(editor);
630 ME_SendRequestResize(editor, FALSE);
637 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
639 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
641 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
642 item = ME_FindItemFwd(item, diParagraph);
647 nOffset -= item->member.para.nCharOfs;
648 if (nItemType == diParagraph) {
650 *nItemOffset = nOffset;
655 item = ME_FindItemFwd(item, diRun);
656 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
658 nOffset -= item->member.run.nCharOfs;
660 *nItemOffset = nOffset;
667 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
670 int nLen = lstrlenW(text);
672 ME_DisplayItem *item;
673 ME_DisplayItem *para;
675 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
676 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
678 if (!(flags & FR_MATCHCASE))
679 FIXME("Case-insensitive search not implemented\n");
680 if (flags & ~(FR_DOWN | FR_MATCHCASE))
681 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
683 if (chrg->cpMax == -1)
686 nMax = ME_GetTextLength(editor);
690 nMin = min(chrg->cpMin, chrg->cpMax);
691 nMax = max(chrg->cpMin, chrg->cpMax);
697 chrgText->cpMin = chrgText->cpMax = ((flags & FR_DOWN) ? nMin : nMax);
698 return chrgText->cpMin;
701 if (flags & FR_DOWN) /* Forward search */
704 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
708 para = ME_GetParagraph(item);
710 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < nMax)
712 ME_DisplayItem *pCurItem = item;
713 int nCurStart = nStart;
716 while (pCurItem && pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
719 if (nMatched == nLen)
721 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
724 chrgText->cpMin = nStart;
725 chrgText->cpMax = nStart + nLen;
727 TRACE("found at %d-%d\n", nStart, nStart + nLen);
730 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
732 pCurItem = ME_FindItemFwd(pCurItem, diRun);
733 para = ME_GetParagraph(pCurItem);
734 nCurStart = -nMatched;
738 if (nStart == ME_StrLen(item->member.run.strText))
740 item = ME_FindItemFwd(item, diRun);
741 para = ME_GetParagraph(item);
746 else /* Backward search */
749 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
753 para = ME_GetParagraph(item);
756 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
758 ME_DisplayItem *pCurItem = item;
762 while (pCurItem && pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1] == text[nLen - nMatched - 1])
765 if (nMatched == nLen)
767 nStart = para->member.para.nCharOfs + item->member.run.nCharOfs + nCurEnd - nMatched;
770 chrgText->cpMin = nStart;
771 chrgText->cpMax = nStart + nLen;
773 TRACE("found at %d-%d\n", nStart, nStart + nLen);
776 if (nCurEnd - nMatched == 0)
778 pCurItem = ME_FindItemBack(pCurItem, diRun);
779 para = ME_GetParagraph(pCurItem);
780 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
786 item = ME_FindItemBack(item, diRun);
787 para = ME_GetParagraph(item);
788 nEnd = ME_StrLen(item->member.run.strText);
792 TRACE("not found\n");
798 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
800 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
801 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
813 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
817 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
818 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
820 if (ME_IsSelection(editor))
821 ME_DeleteSelection(editor);
822 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
823 ME_DeleteTextAtCursor(editor, 1, 1);
826 ME_QueueInvalidateFromCursor(editor, 1);
827 ME_UpdateRepaint(editor);
828 ME_SendRequestResize(editor, FALSE);
839 ME_GetSelectionCharFormat(editor, &chf);
840 ME_DumpStyleToBuf(&chf, buf);
841 MessageBoxA(NULL, buf, "Style dump", MB_OK);
845 ME_CheckCharOffsets(editor);
853 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
854 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
858 ed->bEmulateVersion10 = FALSE;
859 ed->pBuffer = ME_MakeText();
861 ME_MakeFirstParagraph(hDC, ed->pBuffer);
862 ReleaseDC(hWnd, hDC);
863 ed->bCaretShown = FALSE;
865 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
866 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
867 ed->pCursors[0].nOffset = 0;
868 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
869 ed->pCursors[1].nOffset = 0;
870 ed->nLastTotalLength = ed->nTotalLength = 0;
873 ed->rgbBackColor = -1;
874 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
875 ed->bCaretAtEnd = FALSE;
878 ed->pUndoStack = ed->pRedoStack = NULL;
879 ed->nUndoMode = umAddToUndo;
881 ed->nLastSelStart = ed->nLastSelEnd = 0;
883 ed->nZoomNumerator = ed->nZoomDenominator = 0;
885 ed->nInvalidOfs = -1;
886 ed->pfnWordBreak = NULL;
887 GetClientRect(hWnd, &ed->rcFormat);
888 for (i=0; i<HFONT_CACHE_SIZE; i++)
890 ed->pFontCache[i].nRefs = 0;
891 ed->pFontCache[i].nAge = 0;
892 ed->pFontCache[i].hFont = NULL;
894 ME_CheckCharOffsets(ed);
898 typedef struct tagME_GlobalDestStruct
902 } ME_GlobalDestStruct;
904 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
906 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
910 nMaxSize = GlobalSize(pData->hData);
911 if (pData->nLength+cb+1 >= cb)
913 /* round up to 2^17 */
914 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
915 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
917 pDest = (BYTE *)GlobalLock(pData->hData);
918 memcpy(pDest + pData->nLength, lpBuff, cb);
919 pData->nLength += cb;
920 pDest[pData->nLength] = '\0';
921 GlobalUnlock(pData->hData);
927 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
929 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
934 pDest = (WORD *)lpBuff;
935 pSrc = (WORD *)GlobalLock(pData->hData);
936 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
937 pDest[i] = pSrc[pData->nLength+i];
941 GlobalUnlock(pData->hData);
945 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
947 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
952 pSrc = (BYTE *)GlobalLock(pData->hData);
953 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
954 pDest[i] = pSrc[pData->nLength+i];
958 GlobalUnlock(pData->hData);
963 void ME_DestroyEditor(ME_TextEditor *editor)
965 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
966 ME_DisplayItem *p = pFirst, *pNext = NULL;
969 ME_ClearTempStyle(editor);
970 ME_EmptyUndoStack(editor);
973 ME_DestroyDisplayItem(p);
976 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
977 for (i=0; i<HFONT_CACHE_SIZE; i++)
979 if (editor->pFontCache[i].hFont)
980 DeleteObject(editor->pFontCache[i].hFont);
982 DeleteObject(editor->hbrBackground);
987 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
988 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
989 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
990 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
992 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
997 case DLL_PROCESS_ATTACH:
998 DisableThreadLibraryCalls(hinstDLL);
999 me_heap = HeapCreate (0, 0x10000, 0);
1000 ME_RegisterEditorClass(hinstDLL);
1003 case DLL_PROCESS_DETACH:
1004 UnregisterClassW(wszClassName, 0);
1005 UnregisterClassW(wszClassName50, 0);
1006 UnregisterClassA("RichEdit20A", 0);
1007 UnregisterClassA("RichEdit50A", 0);
1008 if (ME_ListBoxRegistered)
1009 UnregisterClassW(wszClassNameListBox, 0);
1010 if (ME_ComboBoxRegistered)
1011 UnregisterClassW(wszClassNameComboBox, 0);
1012 HeapDestroy (me_heap);
1020 #define UNSUPPORTED_MSG(e) \
1022 FIXME(#e ": stub\n"); \
1023 return DefWindowProcW(hWnd, msg, wParam, lParam);
1025 static const char * const edit_messages[] = {
1054 "EM_SETPASSWORDCHAR",
1055 "EM_EMPTYUNDOBUFFER",
1056 "EM_GETFIRSTVISIBLELINE",
1058 "EM_SETWORDBREAKPROC",
1059 "EM_GETWORDBREAKPROC",
1060 "EM_GETPASSWORDCHAR",
1068 static const char * const richedit_messages[] = {
1073 "EM_EXLINEFROMCHAR",
1079 "EM_GETOLEINTERFACE",
1089 "EM_SETOLECALLBACK",
1091 "EM_SETTARGETDEVICE",
1099 "EM_GETWORDBREAKPROCEX",
1100 "EM_SETWORDBREAKPROCEX",
1102 "EM_UNKNOWN_USER_83",
1107 "EM_STOPGROUPTYPING",
1111 "EM_GETAUTOURLDETECT",
1114 "EM_GETTEXTLENGTHEX",
1117 "EM_UNKNOWN_USER_98",
1118 "EM_UNKNOWN_USER_99",
1119 "EM_SETPUNCTUATION",
1120 "EM_GETPUNCTUATION",
1121 "EM_SETWORDWRAPMODE",
1122 "EM_GETWORDWRAPMODE",
1128 "EM_UNKNOWN_USER_109",
1129 "EM_UNKNOWN_USER_110",
1130 "EM_UNKNOWN_USER_111",
1131 "EM_UNKNOWN_USER_112",
1132 "EM_UNKNOWN_USER_113",
1133 "EM_UNKNOWN_USER_114",
1134 "EM_UNKNOWN_USER_115",
1135 "EM_UNKNOWN_USER_116",
1136 "EM_UNKNOWN_USER_117",
1137 "EM_UNKNOWN_USER_118",
1138 "EM_UNKNOWN_USER_119",
1139 "EM_SETLANGOPTIONS",
1140 "EM_GETLANGOPTIONS",
1141 "EM_GETIMECOMPMODE",
1145 "EM_SETIMEMODEBIAS",
1150 get_msg_name(UINT msg)
1152 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1153 return edit_messages[msg - EM_GETSEL];
1154 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1155 return richedit_messages[msg - EM_CANPASTE];
1159 /******************************************************************
1160 * RichEditANSIWndProc (RICHED20.10)
1162 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1164 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1166 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1167 hWnd, msg, get_msg_name(msg), wParam, lParam);
1171 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1172 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1173 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1174 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1175 UNSUPPORTED_MSG(EM_FMTLINES)
1176 UNSUPPORTED_MSG(EM_FORMATRANGE)
1177 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1178 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1179 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1180 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1181 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1182 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1183 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1184 UNSUPPORTED_MSG(EM_GETLINE)
1185 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1186 UNSUPPORTED_MSG(EM_GETOPTIONS)
1187 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1188 UNSUPPORTED_MSG(EM_GETREDONAME)
1189 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1190 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1191 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1192 UNSUPPORTED_MSG(EM_GETUNDONAME)
1193 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1194 UNSUPPORTED_MSG(EM_HIDESELECTION)
1195 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1196 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1197 UNSUPPORTED_MSG(EM_SCROLL)
1198 UNSUPPORTED_MSG(EM_SCROLLCARET)
1199 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1200 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1201 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1202 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1203 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1204 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1205 UNSUPPORTED_MSG(EM_SETOPTIONS)
1206 UNSUPPORTED_MSG(EM_SETPALETTE)
1207 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1208 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1209 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1210 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1211 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1212 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1213 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1214 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1215 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1216 UNSUPPORTED_MSG(WM_SETFONT)
1217 UNSUPPORTED_MSG(WM_STYLECHANGING)
1218 UNSUPPORTED_MSG(WM_STYLECHANGED)
1219 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1221 /* Messages specific to Richedit controls */
1224 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1226 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1229 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1230 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1231 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1236 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1237 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1238 editor = ME_MakeEditor(hWnd);
1239 SetWindowLongW(hWnd, 0, (long)editor);
1240 pcs = 0; /* ignore */
1243 case EM_EMPTYUNDOBUFFER:
1244 ME_EmptyUndoStack(editor);
1248 /* Note: wParam/lParam can be NULL */
1250 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1251 PUINT pto = lParam ? (PUINT)lParam : &to;
1252 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1253 if ((*pfrom|*pto) & 0xFFFF0000)
1255 return MAKELONG(*pfrom,*pto);
1259 CHARRANGE *pRange = (CHARRANGE *)lParam;
1260 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1261 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1265 return editor->pUndoStack != NULL;
1267 return editor->pRedoStack != NULL;
1268 case WM_UNDO: /* FIXME: actually not the same */
1277 ME_InvalidateSelection(editor);
1278 ME_SetSelection(editor, wParam, lParam);
1279 ME_InvalidateSelection(editor);
1280 ME_SendSelChange(editor);
1285 CHARRANGE *pRange = (CHARRANGE *)lParam;
1286 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1287 ME_InvalidateSelection(editor);
1288 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1289 ME_InvalidateSelection(editor);
1290 ME_SendSelChange(editor);
1295 LPWSTR wszText = (LPWSTR)lParam;
1296 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1297 size_t len = lstrlenW(wszText);
1300 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1301 if (pStruct->codepage != 1200) {
1302 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1305 /* FIXME: this should support RTF strings too, according to MSDN */
1306 if (pStruct->flags & ST_SELECTION) {
1307 ME_GetSelection(editor, &from, &to);
1308 style = ME_GetSelectionInsertStyle(editor);
1309 ME_InternalDeleteText(editor, from, to - from);
1310 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1311 ME_ReleaseStyle(style);
1314 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1315 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1318 ME_CommitUndo(editor);
1319 if (!(pStruct->flags & ST_KEEPUNDO))
1320 ME_EmptyUndoStack(editor);
1321 ME_UpdateRepaint(editor);
1324 case EM_SETBKGNDCOLOR:
1326 LRESULT lColor = ME_GetBackColor(editor);
1327 if (editor->rgbBackColor != -1)
1328 DeleteObject(editor->hbrBackground);
1331 editor->rgbBackColor = -1;
1332 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1336 editor->rgbBackColor = lParam;
1337 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1339 if (editor->bRedraw)
1341 InvalidateRect(hWnd, NULL, TRUE);
1347 return editor->nModifyStep == 0 ? 0 : 1;
1351 editor->nModifyStep = 0x80000000;
1353 editor->nModifyStep = 0;
1357 case EM_SETREADONLY:
1359 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1361 nStyle |= ES_READONLY;
1363 nStyle &= ~ES_READONLY;
1364 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1367 case EM_SETEVENTMASK:
1369 DWORD nOldMask = editor->nEventMask;
1371 editor->nEventMask = lParam;
1374 case EM_GETEVENTMASK:
1375 return editor->nEventMask;
1376 case EM_SETCHARFORMAT:
1378 CHARFORMAT2W buf, *p;
1379 BOOL bRepaint = TRUE;
1380 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1382 ME_SetDefaultCharFormat(editor, p);
1383 else if (wParam == (SCF_WORD | SCF_SELECTION))
1384 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1385 else if (wParam == SCF_ALL)
1386 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1389 ME_GetSelection(editor, &from, &to);
1390 bRepaint = (from != to);
1391 ME_SetSelectionCharFormat(editor, p);
1393 ME_CommitUndo(editor);
1395 ME_UpdateRepaint(editor);
1398 case EM_GETCHARFORMAT:
1400 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1401 if (dst->cbSize != sizeof(CHARFORMATA) &&
1402 dst->cbSize != sizeof(CHARFORMATW) &&
1403 dst->cbSize != sizeof(CHARFORMAT2A) &&
1404 dst->cbSize != sizeof(CHARFORMAT2W))
1406 tmp.cbSize = sizeof(tmp);
1408 ME_GetDefaultCharFormat(editor, &tmp);
1410 ME_GetSelectionCharFormat(editor, &tmp);
1411 ME_CopyToCFAny(dst, &tmp);
1414 case EM_SETPARAFORMAT:
1415 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1416 ME_UpdateRepaint(editor);
1417 ME_CommitUndo(editor);
1419 case EM_GETPARAFORMAT:
1420 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1422 case EM_GETFIRSTVISIBLELINE:
1424 ME_DisplayItem *p = editor->pBuffer->pFirst;
1425 int y = editor->nScrollPosY;
1430 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1431 if (p->type == diTextEnd)
1433 if (p->type == diParagraph) {
1434 ypara = p->member.para.nYPos;
1437 ystart = ypara + p->member.row.nYPos;
1438 yend = ystart + p->member.row.nHeight;
1448 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1449 nPos += 8 * lParam; /* FIXME follow the original */
1454 if (nPos != editor->nScrollPosY) {
1455 int dy = editor->nScrollPosY - nPos;
1456 editor->nScrollPosY = nPos;
1457 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1458 if (editor->bRedraw)
1460 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1464 return TRUE; /* Should return false if a single line richedit control */
1469 ME_GetSelection(editor, &from, &to);
1470 ME_InternalDeleteText(editor, from, to-from);
1471 ME_CommitUndo(editor);
1472 ME_UpdateRepaint(editor);
1479 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1480 size_t len = lstrlenW(wszText);
1481 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1483 ME_GetSelection(editor, &from, &to);
1484 style = ME_GetSelectionInsertStyle(editor);
1485 ME_InternalDeleteText(editor, from, to-from);
1486 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1487 ME_ReleaseStyle(style);
1488 ME_EndToUnicode(hWnd, wszText);
1489 /* drop temporary style if line end */
1490 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1491 if (len>0 && wszText[len-1] == '\n')
1492 ME_ClearTempStyle(editor);
1494 ME_CommitUndo(editor);
1496 ME_EmptyUndoStack(editor);
1497 ME_UpdateRepaint(editor);
1502 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1505 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1506 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1507 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1508 if (lstrlenW(wszText) > 0)
1510 /* uses default style! */
1511 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1513 ME_EndToUnicode(hWnd, wszText);
1516 TRACE("WM_SETTEXT - NULL\n");
1517 ME_CommitUndo(editor);
1518 ME_EmptyUndoStack(editor);
1519 ME_SetSelection(editor, 0, 0);
1520 ME_UpdateRepaint(editor);
1525 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1526 if (IsClipboardFormatAvailable(nRTFFormat))
1528 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1536 ME_GlobalDestStruct gds;
1537 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1540 if (IsClipboardFormatAvailable(nRTFFormat))
1541 cf = nRTFFormat, dwFormat = SF_RTF;
1542 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1543 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1547 if (!OpenClipboard(hWnd))
1549 gds.hData = GetClipboardData(cf);
1551 es.dwCookie = (DWORD)&gds;
1552 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1553 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1565 ME_GlobalDestStruct gds;
1567 if (!OpenClipboard(hWnd))
1571 ME_GetSelection(editor, &from, &to);
1572 pars = ME_CountParagraphsBetween(editor, from, to);
1573 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1574 data = (WCHAR *)GlobalLock(hData);
1575 ME_GetTextW(editor, data, from, to-from, TRUE);
1576 GlobalUnlock(hData);
1578 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1580 es.dwCookie = (DWORD)&gds;
1581 es.pfnCallback = ME_AppendToHGLOBAL;
1582 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1583 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1585 SetClipboardData(CF_UNICODETEXT, hData);
1586 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1591 ME_InternalDeleteText(editor, from, to-from);
1592 ME_CommitUndo(editor);
1593 ME_UpdateRepaint(editor);
1597 case WM_GETTEXTLENGTH:
1598 return ME_GetTextLength(editor);
1599 case EM_GETTEXTLENGTHEX:
1600 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1603 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1605 tr.chrg.cpMax = wParam-1;
1606 tr.lpstrText = (WCHAR *)lParam;
1607 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1611 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1614 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1615 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1617 if (ex->flags & GT_SELECTION)
1619 ME_GetSelection(editor, &nStart, &nCount);
1621 nCount = min(nCount, ex->cb - 1);
1626 nCount = ex->cb - 1;
1628 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1630 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1631 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1635 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1636 we can just take a bigger buffer? :) */
1637 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1638 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
1639 DWORD buflen = ex->cb;
1643 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1644 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1646 HeapFree(GetProcessHeap(),0,buffer);
1653 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1654 ME_GetSelection(editor, &from, &to);
1655 tr.chrg.cpMin = from;
1657 tr.lpstrText = (WCHAR *)lParam;
1658 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1660 case EM_GETTEXTRANGE:
1662 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1663 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
1664 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
1665 editor->bEmulateVersion10, ME_GetTextLength(editor));
1666 if (IsWindowUnicode(hWnd))
1667 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1670 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1671 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1672 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1673 /* FIXME this is a potential security hole (buffer overrun)
1674 if you know more about wchar->mbyte conversion please explain
1676 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1681 case EM_GETLINECOUNT:
1683 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1686 while (item != editor->pBuffer->pLast)
1688 assert(item->type == diParagraph);
1689 nRows += item->member.para.nRows;
1690 item = item->member.para.next_para;
1692 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1693 return max(1, nRows);
1695 case EM_LINEFROMCHAR:
1698 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1700 return ME_RowNumberFromCharOfs(editor, wParam);
1702 case EM_EXLINEFROMCHAR:
1704 return ME_RowNumberFromCharOfs(editor, lParam);
1708 ME_DisplayItem *item, *para;
1712 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1714 item = ME_FindRowWithNumber(editor, wParam);
1717 para = ME_GetParagraph(item);
1718 item = ME_FindItemFwd(item, diRun);
1719 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1720 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1725 ME_DisplayItem *item, *item_end;
1726 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1728 if (wParam > ME_GetTextLength(editor))
1732 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1735 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1736 item = ME_RowStart(item);
1737 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1738 item_end = ME_FindItemFwd(item, diStartRow);
1740 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1742 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1743 - (editor->bEmulateVersion10?2:1);
1744 nChars = nNextLineOfs - nThisLineOfs;
1745 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1750 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1751 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1754 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1755 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1756 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1760 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1761 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1764 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1765 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1766 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1770 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1771 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1773 case EM_FINDTEXTEXW:
1775 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1776 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1779 if (!wParam || !lParam)
1781 *(int *)wParam = editor->nZoomNumerator;
1782 *(int *)lParam = editor->nZoomDenominator;
1785 return ME_SetZoom(editor, wParam, lParam);
1786 case EM_CHARFROMPOS:
1787 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1788 case EM_POSFROMCHAR:
1790 ME_DisplayItem *pRun;
1791 int nCharOfs, nOffset, nLength;
1795 /* detect which API version we're dealing with */
1796 if (wParam >= 0x40000)
1798 nLength = ME_GetTextLength(editor);
1800 if (nCharOfs < nLength) {
1801 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1802 assert(pRun->type == diRun);
1803 pt.y = pRun->member.run.pt.y;
1804 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1805 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1808 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1810 if (wParam >= 0x40000) {
1811 *(POINTL *)wParam = pt;
1813 return MAKELONG( pt.x, pt.y );
1816 ME_CommitUndo(editor);
1817 ME_WrapMarkedParagraphs(editor);
1818 ME_MoveCaret(editor);
1821 ME_DestroyEditor(editor);
1822 SetWindowLongW(hWnd, 0, 0);
1824 case WM_LBUTTONDOWN:
1826 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1830 if (GetCapture() == hWnd)
1831 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1834 if (GetCapture() == hWnd)
1837 case WM_LBUTTONDBLCLK:
1838 ME_SelectWord(editor);
1841 if (editor->bRedraw)
1846 hDC = BeginPaint(hWnd, &ps);
1847 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1848 EndPaint(hWnd, &ps);
1852 ME_ShowCaret(editor);
1853 ME_SendOldNotify(editor, EN_SETFOCUS);
1856 ME_HideCaret(editor);
1857 ME_SendOldNotify(editor, EN_KILLFOCUS);
1861 if (editor->bRedraw)
1863 HDC hDC = (HDC)wParam;
1865 if (GetUpdateRect(hWnd,&rc,TRUE))
1867 FillRect(hDC, &rc, editor->hbrBackground);
1873 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1876 if (ME_KeyDown(editor, LOWORD(wParam)))
1881 WCHAR wstr = LOWORD(wParam);
1885 case 3: /* Ctrl-C */
1886 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1890 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1891 MessageBeep(MB_ICONERROR);
1892 return 0; /* FIXME really 0 ? */
1897 case 1: /* Ctrl-A */
1898 ME_SetSelection(editor, 0, -1);
1900 case 22: /* Ctrl-V */
1901 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1903 case 24: /* Ctrl-X */
1904 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1906 case 25: /* Ctrl-Y */
1907 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1909 case 26: /* Ctrl-Z */
1910 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1913 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1914 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1915 ME_Style *style = ME_GetInsertStyle(editor, 0);
1916 ME_SaveTempStyle(editor);
1917 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1918 ME_ReleaseStyle(style);
1919 ME_CommitUndo(editor);
1920 ME_UpdateRepaint(editor);
1926 int nPos = editor->nScrollPosY;
1927 si.cbSize = sizeof(SCROLLINFO);
1928 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1929 GetScrollInfo(hWnd, SB_VERT, &si);
1930 switch(LOWORD(wParam)) {
1932 nPos -= 24; /* FIXME follow the original */
1933 if (nPos<0) nPos = 0;
1937 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1938 nPos += 24; /* FIXME follow the original */
1939 if (nPos>=nEnd) nPos = nEnd;
1943 nPos -= editor->sizeWindow.cy;
1944 if (nPos<0) nPos = 0;
1947 nPos += editor->sizeWindow.cy;
1948 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1951 case SB_THUMBPOSITION:
1952 nPos = si.nTrackPos;
1955 if (nPos != editor->nScrollPosY) {
1956 int dy = editor->nScrollPosY - nPos;
1957 editor->nScrollPosY = nPos;
1958 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1959 if (editor->bRedraw)
1961 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1969 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1970 UINT pulScrollLines;
1971 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1972 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1973 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1974 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1979 if (nPos != editor->nScrollPosY) {
1980 int dy = editor->nScrollPosY - nPos;
1981 editor->nScrollPosY = nPos;
1982 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1983 if (editor->bRedraw)
1985 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1993 *((RECT *)lParam) = editor->rcFormat;
2001 RECT *rc = (RECT *)lParam;
2005 editor->rcFormat.left += rc->left;
2006 editor->rcFormat.top += rc->top;
2007 editor->rcFormat.right += rc->right;
2008 editor->rcFormat.bottom += rc->bottom;
2012 editor->rcFormat = *rc;
2017 GetClientRect(hWnd, &editor->rcFormat);
2019 if (msg != EM_SETRECTNP)
2020 ME_RewrapRepaint(editor);
2023 case EM_REQUESTRESIZE:
2024 ME_SendRequestResize(editor, TRUE);
2027 editor->bRedraw = wParam;
2031 GetClientRect(hWnd, &editor->rcFormat);
2032 ME_RewrapRepaint(editor);
2033 return DefWindowProcW(hWnd, msg, wParam, lParam);
2035 case EM_GETOLEINTERFACE:
2037 LPVOID *ppvObj = (LPVOID*) lParam;
2038 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
2039 return CreateIRichEditOle(ppvObj);
2041 case EM_GETWORDBREAKPROC:
2042 return (LRESULT)editor->pfnWordBreak;
2043 case EM_SETWORDBREAKPROC:
2045 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2047 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2048 return (LRESULT)pfnOld;
2052 return DefWindowProcW(hWnd, msg, wParam, lParam);
2058 /******************************************************************
2059 * RichEdit10ANSIWndProc (RICHED20.9)
2061 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2065 /* FIXME: this is NOT the same as 2.0 version */
2066 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2067 if (msg == WM_NCCREATE)
2069 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2071 editor->bEmulateVersion10 = TRUE;
2072 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2077 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2079 HWND hWnd = editor->hWnd;
2080 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2083 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2085 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2088 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2089 item = item->member.para.next_para;
2092 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2093 item = item->member.para.next_para;
2100 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2102 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2104 WCHAR *pStart = buffer;
2113 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2116 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2123 item = ME_FindItemFwd(item, diRun);
2126 while(nChars && item)
2128 int nLen = ME_StrLen(item->member.run.strText);
2132 if (item->member.run.nFlags & MERF_ENDPARA)
2141 /* our end paragraph consists of 2 characters now */
2142 if (editor->bEmulateVersion10)
2146 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2153 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2157 item = ME_FindItemFwd(item, diRun);
2160 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2164 void ME_RegisterEditorClass(HINSTANCE hInstance)
2170 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2171 wcW.lpfnWndProc = RichEditANSIWndProc;
2174 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2176 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2177 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2178 wcW.lpszMenuName = NULL;
2179 wcW.lpszClassName = wszClassName;
2180 bResult = RegisterClassW(&wcW);
2182 wcW.lpszClassName = wszClassName50;
2183 bResult = RegisterClassW(&wcW);
2186 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2187 wcA.lpfnWndProc = RichEditANSIWndProc;
2190 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2192 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2193 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2194 wcA.lpszMenuName = NULL;
2195 wcA.lpszClassName = "RichEdit20A";
2196 bResult = RegisterClassA(&wcA);
2198 wcA.lpszClassName = "RichEdit50A";
2199 bResult = RegisterClassA(&wcA);
2202 /******************************************************************
2203 * CreateTextServices (RICHED20.4)
2205 * FIXME should be ITextHost instead of void*
2207 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
2211 /* FIXME should support aggregation */
2213 return CLASS_E_NOAGGREGATION;
2215 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
2218 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2219 /* FIXME: Not implemented */
2220 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2221 hWnd, msg, get_msg_name(msg), wParam, lParam);
2222 return DefWindowProcW(hWnd, msg, wParam, lParam);
2225 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2226 /* FIXME: Not implemented */
2227 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2228 hWnd, msg, get_msg_name(msg), wParam, lParam);
2229 return DefWindowProcW(hWnd, msg, wParam, lParam);
2232 /******************************************************************
2233 * REExtendedRegisterClass (RICHED20.8)
2235 * FIXME undocumented
2236 * Need to check for errors and implement controls and callbacks
2238 LRESULT WINAPI REExtendedRegisterClass(void)
2243 FIXME("semi stub\n");
2247 wcW.hInstance = NULL;
2250 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2251 wcW.lpszMenuName = NULL;
2253 if (!ME_ListBoxRegistered)
2255 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2256 wcW.lpfnWndProc = REListWndProc;
2257 wcW.lpszClassName = wszClassNameListBox;
2258 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2261 if (!ME_ComboBoxRegistered)
2263 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2264 wcW.lpfnWndProc = REComboWndProc;
2265 wcW.lpszClassName = wszClassNameComboBox;
2266 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2270 if (ME_ListBoxRegistered)
2272 if (ME_ComboBoxRegistered)