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
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
54 - EM_GETLANGOPTIONS 2.0
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0 (only Y value valid)
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
94 - EM_SETBIDIOPTIONS 3.0
96 + EM_SETCHARFORMAT (partly done, no ANSI)
98 + EM_SETEVENTMASK (few notifications supported)
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
102 - EM_SETLANGOPTIONS 2.0
104 + EM_SETMODIFY (not sure if implementation is correct)
106 + EM_SETOPTIONS (partially implemented)
109 + EM_SETPASSWORDCHAR 2.0
110 - EM_SETPUNCTUATION 1.0asian
111 + EM_SETREADONLY no beep on modification attempt
113 + EM_SETRECTNP (EM_SETRECT without repainting)
115 + EM_SETSCROLLPOS 3.0
118 + EM_SETTEXTEX 3.0 (no rich text insertion handling, proper style?)
120 - EM_SETTYPOGRAPHYOPTIONS 3.0
121 + EM_SETUNDOLIMIT 2.0
122 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
123 - EM_SETWORDBREAKPROCEX
124 - EM_SETWORDWRAPMODE 1.0asian
126 + EM_SHOWSCROLLBAR 2.0
127 - EM_STOPGROUPTYPING 2.0
135 + WM_GETDLGCODE (the current implementation is incomplete)
136 + WM_GETTEXT (ANSI&Unicode)
137 + WM_GETTEXTLENGTH (ANSI version sucks)
140 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
142 - WM_STYLECHANGED (things like read-only flag)
147 * EN_CHANGE (sent from the wrong place)
164 * EN_UPDATE (sent from the wrong place)
172 + ES_DISABLENOSCROLL (scrollbar is always visible)
173 - ES_EX_NOCALLOLEINIT
175 - ES_MULTILINE (currently single line controls aren't supported)
177 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
183 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
190 * RICHED20 TODO (incomplete):
192 * - messages/styles/notifications listed above
194 * - add remaining CHARFORMAT/PARAFORMAT fields
195 * - right/center align should strip spaces from the beginning
196 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
197 * - COM interface (looks like a major pain in the TODO list)
198 * - calculate heights of pictures (half-done)
199 * - horizontal scrolling (not even started)
200 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
202 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
203 * - italic caret with italic fonts
205 * - most notifications aren't sent at all (the most important ones are)
206 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
207 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
208 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
209 * - full justification
212 * - ListBox & ComboBox not implemented
214 * Bugs that are probably fixed, but not so easy to verify:
215 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
216 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
217 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
218 * - caret shouldn't be displayed when selection isn't empty
219 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
220 * - undo for setting default format (done, might be buggy)
221 * - styles might be not released properly (looks like they work like charm, but who knows?
228 #define NO_SHLWAPI_STREAM
234 #define STACK_SIZE_DEFAULT 100
235 #define STACK_SIZE_MAX 1000
237 #define TEXT_LIMIT_DEFAULT 32767
239 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
241 static BOOL ME_RegisterEditorClass(HINSTANCE);
243 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
244 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
245 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
246 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 static HCURSOR hLeft;
248 static HCURSOR hBeam;
251 HANDLE me_heap = NULL;
253 static BOOL ME_ListBoxRegistered = FALSE;
254 static BOOL ME_ComboBoxRegistered = FALSE;
256 static inline int is_version_nt(void)
258 return !(GetVersion() & 0x80000000);
261 static ME_TextBuffer *ME_MakeText(void) {
263 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
265 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
266 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
272 p1->member.para.next_para = p2;
273 p2->member.para.prev_para = p1;
274 p2->member.para.nCharOfs = 0;
278 buf->pCharStyle = NULL;
284 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
286 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
289 TRACE("%08x %p\n", dwFormat, stream);
296 ME_StreamInFill(stream);
297 if (stream->editstream->dwError)
303 if (!(dwFormat & SF_UNICODE))
305 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
306 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
311 nWideChars = stream->dwSize >> 1;
312 pText = (WCHAR *)stream->buffer;
315 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
316 if (stream->dwSize == 0)
320 ME_CommitUndo(editor);
321 ME_UpdateRepaint(editor);
325 static void ME_RTFCharAttrHook(RTF_Info *info)
328 fmt.cbSize = sizeof(fmt);
332 switch(info->rtfMinor)
335 /* FIXME add more flags once they're implemented */
336 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
337 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
338 fmt.yHeight = 12*20; /* 12pt */
340 fmt.bUnderlineType = CFU_UNDERLINENONE;
343 fmt.dwMask = CFM_BOLD;
344 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
347 fmt.dwMask = CFM_ITALIC;
348 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
351 fmt.dwMask = CFM_UNDERLINETYPE;
352 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
354 case rtfDotUnderline:
355 fmt.dwMask = CFM_UNDERLINETYPE;
356 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
359 fmt.dwMask = CFM_UNDERLINETYPE;
360 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
362 case rtfWordUnderline:
363 fmt.dwMask = CFM_UNDERLINETYPE;
364 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
367 fmt.dwMask = CFM_UNDERLINETYPE;
368 fmt.bUnderlineType = CFU_UNDERLINENONE;
371 fmt.dwMask = CFM_STRIKEOUT;
372 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
376 case rtfSubScrShrink:
377 case rtfSuperScrShrink:
379 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
380 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
381 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
382 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
385 fmt.dwMask = CFM_HIDDEN;
386 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
389 fmt.dwMask = CFM_BACKCOLOR;
391 if (info->rtfParam == 0)
392 fmt.dwEffects = CFE_AUTOBACKCOLOR;
393 else if (info->rtfParam != rtfNoParam)
395 RTFColor *c = RTFGetColor(info, info->rtfParam);
396 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
400 fmt.dwMask = CFM_COLOR;
402 if (info->rtfParam == 0)
403 fmt.dwEffects = CFE_AUTOCOLOR;
404 else if (info->rtfParam != rtfNoParam)
406 RTFColor *c = RTFGetColor(info, info->rtfParam);
408 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
414 if (info->rtfParam != rtfNoParam)
416 RTFFont *f = RTFGetFont(info, info->rtfParam);
419 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
420 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
421 fmt.bCharSet = f->rtfFCharSet;
422 fmt.dwMask = CFM_FACE | CFM_CHARSET;
423 fmt.bPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
428 fmt.dwMask = CFM_SIZE;
429 if (info->rtfParam != rtfNoParam)
430 fmt.yHeight = info->rtfParam*10;
435 RTFFlushOutputBuffer(info);
436 /* FIXME too slow ? how come ? */
437 style2 = ME_ApplyStyle(info->style, &fmt);
438 ME_ReleaseStyle(info->style);
439 info->style = style2;
440 info->styleChanged = TRUE;
444 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
445 the same tags mean different things in different contexts */
446 static void ME_RTFParAttrHook(RTF_Info *info)
449 fmt.cbSize = sizeof(fmt);
452 switch(info->rtfMinor)
454 case rtfParDef: /* restores default paragraph attributes */
455 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
456 fmt.wAlignment = PFA_LEFT;
458 fmt.dxOffset = fmt.dxStartIndent = 0;
459 RTFFlushOutputBuffer(info);
460 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
464 ME_DisplayItem *para;
466 RTFFlushOutputBuffer(info);
467 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
468 assert(para->member.para.pCells);
469 para->member.para.bTable = TRUE;
473 ME_GetSelectionParaFormat(info->editor, &fmt);
474 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
475 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
476 fmt.dxOffset = -info->rtfParam;
479 ME_GetSelectionParaFormat(info->editor, &fmt);
480 fmt.dwMask = PFM_STARTINDENT;
481 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
484 fmt.dwMask = PFM_RIGHTINDENT;
485 fmt.dxRightIndent = info->rtfParam;
489 fmt.dwMask = PFM_ALIGNMENT;
490 fmt.wAlignment = PFA_LEFT;
493 fmt.dwMask = PFM_ALIGNMENT;
494 fmt.wAlignment = PFA_RIGHT;
497 fmt.dwMask = PFM_ALIGNMENT;
498 fmt.wAlignment = PFA_CENTER;
501 ME_GetSelectionParaFormat(info->editor, &fmt);
502 if (!(fmt.dwMask & PFM_TABSTOPS))
504 fmt.dwMask |= PFM_TABSTOPS;
507 if (fmt.cTabCount < MAX_TAB_STOPS)
508 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
511 fmt.dwMask = PFM_KEEP;
512 fmt.wEffects = PFE_KEEP;
514 case rtfNoWidowControl:
515 fmt.dwMask = PFM_NOWIDOWCONTROL;
516 fmt.wEffects = PFE_NOWIDOWCONTROL;
519 fmt.dwMask = PFM_KEEPNEXT;
520 fmt.wEffects = PFE_KEEPNEXT;
523 fmt.dwMask = PFM_SPACEAFTER;
524 fmt.dySpaceAfter = info->rtfParam;
527 fmt.dwMask = PFM_SPACEBEFORE;
528 fmt.dySpaceBefore = info->rtfParam;
530 case rtfSpaceBetween:
531 fmt.dwMask = PFM_LINESPACING;
532 if ((int)info->rtfParam > 0)
534 fmt.dyLineSpacing = info->rtfParam;
535 fmt.bLineSpacingRule = 3;
539 fmt.dyLineSpacing = info->rtfParam;
540 fmt.bLineSpacingRule = 4;
542 case rtfSpaceMultiply:
543 fmt.dwMask = PFM_LINESPACING;
544 fmt.dyLineSpacing = info->rtfParam * 20;
545 fmt.bLineSpacingRule = 5;
548 fmt.dwMask = PFM_NUMBERING;
549 fmt.wNumbering = PFN_BULLET;
552 fmt.dwMask = PFM_NUMBERING;
553 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
555 case rtfParNumDecimal:
556 fmt.dwMask = PFM_NUMBERING;
557 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
559 case rtfParNumIndent:
560 fmt.dwMask = PFM_NUMBERINGTAB;
561 fmt.wNumberingTab = info->rtfParam;
563 case rtfParNumStartAt:
564 fmt.dwMask = PFM_NUMBERINGSTART;
565 fmt.wNumberingStart = info->rtfParam;
568 ME_GetSelectionParaFormat(info->editor, &fmt);
569 if (!(fmt.dwMask & PFM_BORDER))
571 fmt.dwMask |= PFM_BORDER;
572 fmt.wBorderSpace = 0;
573 fmt.wBorderWidth = 1;
579 ME_GetSelectionParaFormat(info->editor, &fmt);
580 if (!(fmt.dwMask & PFM_BORDER))
582 fmt.dwMask |= PFM_BORDER;
583 fmt.wBorderSpace = 0;
584 fmt.wBorderWidth = 1;
590 ME_GetSelectionParaFormat(info->editor, &fmt);
591 if (!(fmt.dwMask & PFM_BORDER))
593 fmt.dwMask |= PFM_BORDER;
594 fmt.wBorderSpace = 0;
595 fmt.wBorderWidth = 1;
600 case rtfBorderBottom:
601 ME_GetSelectionParaFormat(info->editor, &fmt);
602 if (!(fmt.dwMask & PFM_BORDER))
604 fmt.dwMask |= PFM_BORDER;
605 fmt.wBorderSpace = 0;
606 fmt.wBorderWidth = 1;
611 case rtfBorderSingle:
612 ME_GetSelectionParaFormat(info->editor, &fmt);
613 /* we assume that borders have been created before (RTF spec) */
614 fmt.wBorders &= ~0x70;
615 fmt.wBorders |= 1 << 8;
618 ME_GetSelectionParaFormat(info->editor, &fmt);
619 /* we assume that borders have been created before (RTF spec) */
620 fmt.wBorders &= ~0x70;
621 fmt.wBorders |= 2 << 8;
623 case rtfBorderShadow:
624 ME_GetSelectionParaFormat(info->editor, &fmt);
625 /* we assume that borders have been created before (RTF spec) */
626 fmt.wBorders &= ~0x70;
627 fmt.wBorders |= 10 << 8;
629 case rtfBorderDouble:
630 ME_GetSelectionParaFormat(info->editor, &fmt);
631 /* we assume that borders have been created before (RTF spec) */
632 fmt.wBorders &= ~0x70;
633 fmt.wBorders |= 7 << 8;
636 ME_GetSelectionParaFormat(info->editor, &fmt);
637 /* we assume that borders have been created before (RTF spec) */
638 fmt.wBorders &= ~0x70;
639 fmt.wBorders |= 11 << 8;
642 ME_GetSelectionParaFormat(info->editor, &fmt);
643 /* we assume that borders have been created before (RTF spec) */
644 fmt.wBorders &= ~0x70;
645 fmt.wBorders |= ((info->rtfParam / 15) & 7) << 8;
648 ME_GetSelectionParaFormat(info->editor, &fmt);
649 /* we assume that borders have been created before (RTF spec) */
650 fmt.wBorderSpace = info->rtfParam;
654 RTFFlushOutputBuffer(info);
655 /* FIXME too slow ? how come ?*/
656 ME_SetSelectionParaFormat(info->editor, &fmt);
660 static void ME_RTFTblAttrHook(RTF_Info *info)
662 ME_DisplayItem *para;
664 switch (info->rtfMinor)
667 RTFFlushOutputBuffer(info);
668 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
670 /* Release possibly inherited cell definitions */
671 ME_DestroyTableCellList(para);
673 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
674 para->member.para.pCells->nRightBoundary = 0;
675 para->member.para.pCells->next = NULL;
676 para->member.para.pLastCell = para->member.para.pCells;
679 RTFFlushOutputBuffer(info);
680 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
682 if (para->member.para.pLastCell->nRightBoundary)
684 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
687 para->member.para.pLastCell->next = pCell;
688 para->member.para.pLastCell = pCell;
690 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
695 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE h, const SIZEL* sz)
697 LPOLEOBJECT lpObject = NULL;
698 LPSTORAGE lpStorage = NULL;
699 LPOLECLIENTSITE lpClientSite = NULL;
700 LPDATAOBJECT lpDataObject = NULL;
701 LPOLECACHE lpOleCache = NULL;
708 stgm.tymed = TYMED_ENHMF;
709 stgm.u.hEnhMetaFile = h;
710 stgm.pUnkForRelease = NULL;
712 fm.cfFormat = CF_ENHMETAFILE;
714 fm.dwAspect = DVASPECT_CONTENT;
716 fm.tymed = stgm.tymed;
718 if (!info->lpRichEditOle)
720 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
723 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
725 /* FIXME: enable it when rich-edit properly implements this method */
726 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
727 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
729 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
730 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
731 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
732 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
733 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
737 reobject.cbStruct = sizeof(reobject);
738 reobject.cp = REO_CP_SELECTION;
739 reobject.clsid = clsid;
740 reobject.poleobj = lpObject;
741 reobject.pstg = lpStorage;
742 reobject.polesite = lpClientSite;
743 /* convert from twips to .01 mm */
744 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
745 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
746 reobject.dvaspect = DVASPECT_CONTENT;
747 reobject.dwFlags = 0; /* FIXME */
750 /* FIXME: could be simpler */
751 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
754 if (lpObject) IOleObject_Release(lpObject);
755 if (lpClientSite) IOleClientSite_Release(lpClientSite);
756 if (lpStorage) IStorage_Release(lpStorage);
757 if (lpDataObject) IDataObject_Release(lpDataObject);
758 if (lpOleCache) IOleCache_Release(lpOleCache);
763 static void ME_RTFReadPictGroup(RTF_Info *info)
767 unsigned bufsz, bufidx;
774 if (info->rtfClass == rtfEOF)
777 /* fetch picture type */
778 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
780 mfp.mm = info->rtfParam;
784 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
788 /* fetch picture attributes */
792 if (info->rtfClass == rtfEOF)
794 if (info->rtfClass == rtfText)
796 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
798 ERR("Expected picture attribute (%d %d)\n",
799 info->rtfClass, info->rtfMajor);
802 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
803 mfp.xExt = info->rtfParam;
804 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
805 mfp.yExt = info->rtfParam;
806 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
807 sz.cx = info->rtfParam;
808 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
809 sz.cy = info->rtfParam;
811 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
813 /* fetch picture data */
816 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
817 val = info->rtfMajor;
818 for (flip = TRUE;; flip = !flip)
821 if (info->rtfClass == rtfEOF)
823 HeapFree(GetProcessHeap(), 0, buffer);
824 return; /* Warn ?? */
826 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
828 if (info->rtfClass != rtfText) goto skip_group;
831 if (bufidx >= bufsz &&
832 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
834 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
837 val = info->rtfMajor;
839 if (flip) FIXME("wrong hex string\n");
841 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
842 ME_RTFInsertOleObject(info, hemf, &sz);
843 HeapFree(GetProcessHeap(), 0, buffer);
844 RTFRouteToken (info); /* feed "}" back to router */
847 HeapFree(GetProcessHeap(), 0, buffer);
849 RTFRouteToken(info); /* feed "}" back to router */
852 static void ME_RTFReadHook(RTF_Info *info) {
853 switch(info->rtfClass)
856 switch(info->rtfMajor)
859 if (info->stackTop < maxStack) {
860 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
861 info->stack[info->stackTop].codePage = info->codePage;
862 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
865 info->styleChanged = FALSE;
870 RTFFlushOutputBuffer(info);
871 if (info->stackTop<=1) {
872 info->rtfClass = rtfEOF;
876 assert(info->stackTop >= 0);
877 if (info->styleChanged)
879 /* FIXME too slow ? how come ? */
880 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
881 ME_ReleaseStyle(info->style);
883 info->codePage = info->stack[info->stackTop].codePage;
884 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
891 switch(info->rtfMajor)
894 ME_RTFCharAttrHook(info);
897 ME_RTFParAttrHook(info);
900 ME_RTFTblAttrHook(info);
903 if (info->rtfMinor == rtfCell)
905 RTFFlushOutputBuffer(info);
906 ME_InsertTableCellFromCursor(info->editor, 0);
914 ME_StreamInFill(ME_InStream *stream)
916 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
917 (BYTE *)stream->buffer,
918 sizeof(stream->buffer),
919 (LONG *)&stream->dwSize);
923 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
927 int from, to, to2, nUndoMode;
928 int nEventMask = editor->nEventMask;
929 ME_InStream inStream;
931 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
932 editor->nEventMask = 0;
934 ME_GetSelection(editor, &from, &to);
935 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
936 style = ME_GetSelectionInsertStyle(editor);
938 ME_InternalDeleteText(editor, from, to-from);
941 style = editor->pBuffer->pDefaultStyle;
942 ME_AddRefStyle(style);
943 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
944 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
946 ME_ClearTempStyle(editor);
947 /* FIXME restore default paragraph formatting ! */
951 /* Back up undo mode to a local variable */
952 nUndoMode = editor->nUndoMode;
954 /* Only create an undo if SFF_SELECTION is set */
955 if (!(format & SFF_SELECTION))
956 editor->nUndoMode = umIgnore;
958 inStream.editstream = stream;
959 inStream.editstream->dwError = 0;
965 /* Check if it's really RTF, and if it is not, use plain text */
966 ME_StreamInFill(&inStream);
967 if (!inStream.editstream->dwError)
969 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
977 if (!inStream.editstream->dwError)
979 if (format & SF_RTF) {
980 /* setup the RTF parser */
981 memset(&parser, 0, sizeof parser);
982 RTFSetEditStream(&parser, &inStream);
983 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
984 parser.hwndEdit = editor->hWnd;
985 parser.editor = editor;
986 parser.style = style;
989 RTFSetReadHook(&parser, ME_RTFReadHook);
990 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
995 RTFFlushOutputBuffer(&parser);
997 if (parser.lpRichEditOle)
998 IRichEditOle_Release(parser.lpRichEditOle);
1000 style = parser.style;
1002 else if (format & SF_TEXT)
1003 ME_StreamInText(editor, format, &inStream, style);
1005 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1006 ME_GetSelection(editor, &to, &to2);
1007 /* put the cursor at the top */
1008 if (!(format & SFF_SELECTION))
1009 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1012 /* Restore saved undo mode */
1013 editor->nUndoMode = nUndoMode;
1015 /* even if we didn't add an undo, we need to commit anything on the stack */
1016 ME_CommitUndo(editor);
1018 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1019 if (!(format & SFF_SELECTION))
1020 ME_EmptyUndoStack(editor);
1022 ME_ReleaseStyle(style);
1023 editor->nEventMask = nEventMask;
1024 if (editor->bRedraw)
1026 ME_UpdateRepaint(editor);
1028 if (!(format & SFF_SELECTION)) {
1029 ME_ClearTempStyle(editor);
1031 ME_MoveCaret(editor);
1032 ME_SendSelChange(editor);
1033 ME_SendRequestResize(editor, FALSE);
1039 typedef struct tagME_RTFStringStreamStruct
1044 } ME_RTFStringStreamStruct;
1046 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1048 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1051 count = min(cb, pStruct->length - pStruct->pos);
1052 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1053 pStruct->pos += count;
1059 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1062 ME_RTFStringStreamStruct data;
1064 data.string = string;
1065 data.length = strlen(string);
1067 es.dwCookie = (DWORD)&data;
1068 es.pfnCallback = ME_ReadFromRTFString;
1069 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
1074 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1076 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1078 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1079 item = ME_FindItemFwd(item, diParagraph);
1084 nOffset -= item->member.para.nCharOfs;
1085 if (nItemType == diParagraph) {
1087 *nItemOffset = nOffset;
1092 item = ME_FindItemFwd(item, diRun);
1093 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
1095 nOffset -= item->member.run.nCharOfs;
1097 *nItemOffset = nOffset;
1104 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1106 const int nLen = lstrlenW(text);
1107 const int nTextLen = ME_GetTextLength(editor);
1110 ME_DisplayItem *item;
1111 ME_DisplayItem *para;
1112 WCHAR wLastChar = ' ';
1114 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1115 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1117 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1118 FIXME("Flags 0x%08x not implemented\n",
1119 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1122 if (chrg->cpMax == -1)
1125 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1127 /* when searching up, if cpMin < cpMax, then instead of searching
1128 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1129 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1130 * case, it is always bigger than cpMin.
1132 if (!(flags & FR_DOWN))
1136 nMax = nMin > nTextLen ? nTextLen : nMin;
1137 if (nMin < nSwap || chrg->cpMax == -1)
1143 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1146 chrgText->cpMin = chrgText->cpMax = -1;
1150 if (flags & FR_DOWN) /* Forward search */
1152 /* If possible, find the character before where the search starts */
1153 if ((flags & FR_WHOLEWORD) && nMin)
1156 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1160 chrgText->cpMin = chrgText->cpMax = -1;
1163 wLastChar = item->member.run.strText->szData[nStart];
1167 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1171 chrgText->cpMin = chrgText->cpMax = -1;
1175 para = ME_GetParagraph(item);
1177 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1179 ME_DisplayItem *pCurItem = item;
1180 int nCurStart = nStart;
1183 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1185 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1189 if (nMatched == nLen)
1191 ME_DisplayItem *pNextItem = pCurItem;
1192 int nNextStart = nCurStart;
1195 /* Check to see if next character is a whitespace */
1196 if (flags & FR_WHOLEWORD)
1198 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1200 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1201 nNextStart = -nMatched;
1205 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1209 if (isalnumW(wNextChar))
1213 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
1216 chrgText->cpMin = nStart;
1217 chrgText->cpMax = nStart + nLen;
1219 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1222 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1224 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1225 para = ME_GetParagraph(pCurItem);
1226 nCurStart = -nMatched;
1230 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1235 if (nStart == ME_StrLen(item->member.run.strText))
1237 item = ME_FindItemFwd(item, diRun);
1238 para = ME_GetParagraph(item);
1243 else /* Backward search */
1245 /* If possible, find the character after where the search ends */
1246 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1249 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1253 chrgText->cpMin = chrgText->cpMax = -1;
1256 wLastChar = item->member.run.strText->szData[nEnd];
1260 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1264 chrgText->cpMin = chrgText->cpMax = -1;
1268 para = ME_GetParagraph(item);
1271 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1273 ME_DisplayItem *pCurItem = item;
1277 if (nCurEnd - nMatched == 0)
1279 pCurItem = ME_FindItemBack(pCurItem, diRun);
1280 para = ME_GetParagraph(pCurItem);
1281 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1284 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1286 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1290 if (nMatched == nLen)
1292 ME_DisplayItem *pPrevItem = pCurItem;
1293 int nPrevEnd = nCurEnd;
1296 /* Check to see if previous character is a whitespace */
1297 if (flags & FR_WHOLEWORD)
1299 if (nPrevEnd - nMatched == 0)
1301 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1303 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1307 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1311 if (isalnumW(wPrevChar))
1315 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1318 chrgText->cpMin = nStart;
1319 chrgText->cpMax = nStart + nLen;
1321 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1324 if (nCurEnd - nMatched == 0)
1326 pCurItem = ME_FindItemBack(pCurItem, diRun);
1327 /* Don't care about pCurItem becoming NULL here; it's already taken
1328 * care of in the exterior loop condition */
1329 para = ME_GetParagraph(pCurItem);
1330 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1334 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1341 item = ME_FindItemBack(item, diRun);
1342 para = ME_GetParagraph(item);
1343 nEnd = ME_StrLen(item->member.run.strText);
1347 TRACE("not found\n");
1349 chrgText->cpMin = chrgText->cpMax = -1;
1353 /* helper to send a msg filter notification */
1355 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1359 msgf.nmhdr.hwndFrom = editor->hWnd;
1360 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1361 msgf.nmhdr.code = EN_MSGFILTER;
1364 msgf.wParam = *wParam;
1365 msgf.lParam = *lParam;
1366 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1368 *wParam = msgf.wParam;
1369 *lParam = msgf.lParam;
1370 msgf.wParam = *wParam;
1376 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1378 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1379 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1391 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1395 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1396 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1398 if (ME_IsSelection(editor))
1399 ME_DeleteSelection(editor);
1400 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1401 ME_DeleteTextAtCursor(editor, 1, 1);
1404 ME_CommitUndo(editor);
1405 ME_UpdateRepaint(editor);
1406 ME_SendRequestResize(editor, FALSE);
1416 chf.cbSize = sizeof(chf);
1418 ME_GetSelectionCharFormat(editor, &chf);
1419 ME_DumpStyleToBuf(&chf, buf);
1420 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1424 ME_CheckCharOffsets(editor);
1431 static void ME_SetCursor(ME_TextEditor *editor, int x)
1433 if (x < editor->selofs || editor->linesel)
1439 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1444 if(!editor->lpOleCallback)
1446 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
1447 if(selrange.cpMin == selrange.cpMax)
1448 seltype |= SEL_EMPTY;
1451 /* FIXME: Handle objects */
1452 seltype |= SEL_TEXT;
1453 if(selrange.cpMax-selrange.cpMin > 1)
1454 seltype |= SEL_MULTICHAR;
1456 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1458 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1464 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1465 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1468 ed->bEmulateVersion10 = FALSE;
1469 ed->pBuffer = ME_MakeText();
1470 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1471 ME_MakeFirstParagraph(ed);
1472 ed->bCaretShown = FALSE;
1474 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1475 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1476 ed->pCursors[0].nOffset = 0;
1477 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1478 ed->pCursors[1].nOffset = 0;
1479 ed->pCursors[2] = ed->pCursors[0];
1480 ed->pCursors[3] = ed->pCursors[1];
1481 ed->nLastTotalLength = ed->nTotalLength = 0;
1485 ed->rgbBackColor = -1;
1486 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1487 ed->bCaretAtEnd = FALSE;
1489 ed->nModifyStep = 0;
1490 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1491 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1492 ed->nUndoStackSize = 0;
1493 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1494 ed->nUndoMode = umAddToUndo;
1495 ed->nParagraphs = 1;
1496 ed->nLastSelStart = ed->nLastSelEnd = 0;
1497 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1499 ed->bHideSelection = FALSE;
1500 ed->nInvalidOfs = -1;
1501 ed->pfnWordBreak = NULL;
1502 ed->lpOleCallback = NULL;
1503 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1504 ed->AutoURLDetect_bEnable = FALSE;
1505 ed->bHaveFocus = FALSE;
1506 GetClientRect(hWnd, &ed->rcFormat);
1507 for (i=0; i<HFONT_CACHE_SIZE; i++)
1509 ed->pFontCache[i].nRefs = 0;
1510 ed->pFontCache[i].nAge = 0;
1511 ed->pFontCache[i].hFont = NULL;
1514 ME_CheckCharOffsets(ed);
1515 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1521 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1522 ed->cPasswordMask = '*';
1524 ed->cPasswordMask = 0;
1529 typedef struct tagME_GlobalDestStruct
1533 } ME_GlobalDestStruct;
1535 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1537 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1542 pDest = (WORD *)lpBuff;
1543 pSrc = (WORD *)GlobalLock(pData->hData);
1544 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1545 pDest[i] = pSrc[pData->nLength+i];
1547 pData->nLength += i;
1549 GlobalUnlock(pData->hData);
1553 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1555 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1560 pSrc = (BYTE *)GlobalLock(pData->hData);
1561 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1562 pDest[i] = pSrc[pData->nLength+i];
1564 pData->nLength += i;
1566 GlobalUnlock(pData->hData);
1571 void ME_DestroyEditor(ME_TextEditor *editor)
1573 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1574 ME_DisplayItem *p = pFirst, *pNext = NULL;
1577 ME_ClearTempStyle(editor);
1578 ME_EmptyUndoStack(editor);
1581 ME_DestroyDisplayItem(p);
1584 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1585 for (i=0; i<HFONT_CACHE_SIZE; i++)
1587 if (editor->pFontCache[i].hFont)
1588 DeleteObject(editor->pFontCache[i].hFont);
1590 if (editor->rgbBackColor != -1)
1591 DeleteObject(editor->hbrBackground);
1592 if(editor->lpOleCallback)
1593 IUnknown_Release(editor->lpOleCallback);
1595 FREE_OBJ(editor->pBuffer);
1596 FREE_OBJ(editor->pCursors);
1601 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1606 case DLL_PROCESS_ATTACH:
1607 DisableThreadLibraryCalls(hinstDLL);
1608 me_heap = HeapCreate (0, 0x10000, 0);
1609 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1610 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1611 hBeam = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1615 case DLL_PROCESS_DETACH:
1616 UnregisterClassW(RichEdit20W, 0);
1617 UnregisterClassW(RichEdit50W, 0);
1618 UnregisterClassA("RichEdit20A", 0);
1619 UnregisterClassA("RichEdit50A", 0);
1620 if (ME_ListBoxRegistered)
1621 UnregisterClassW(REListBox20W, 0);
1622 if (ME_ComboBoxRegistered)
1623 UnregisterClassW(REComboBox20W, 0);
1625 HeapDestroy (me_heap);
1633 #define UNSUPPORTED_MSG(e) \
1635 FIXME(#e ": stub\n"); \
1636 return DefWindowProcW(hWnd, msg, wParam, lParam);
1638 static const char * const edit_messages[] = {
1667 "EM_SETPASSWORDCHAR",
1668 "EM_EMPTYUNDOBUFFER",
1669 "EM_GETFIRSTVISIBLELINE",
1671 "EM_SETWORDBREAKPROC",
1672 "EM_GETWORDBREAKPROC",
1673 "EM_GETPASSWORDCHAR",
1681 static const char * const richedit_messages[] = {
1686 "EM_EXLINEFROMCHAR",
1692 "EM_GETOLEINTERFACE",
1702 "EM_SETOLECALLBACK",
1704 "EM_SETTARGETDEVICE",
1712 "EM_GETWORDBREAKPROCEX",
1713 "EM_SETWORDBREAKPROCEX",
1715 "EM_UNKNOWN_USER_83",
1720 "EM_STOPGROUPTYPING",
1724 "EM_GETAUTOURLDETECT",
1727 "EM_GETTEXTLENGTHEX",
1730 "EM_UNKNOWN_USER_98",
1731 "EM_UNKNOWN_USER_99",
1732 "EM_SETPUNCTUATION",
1733 "EM_GETPUNCTUATION",
1734 "EM_SETWORDWRAPMODE",
1735 "EM_GETWORDWRAPMODE",
1741 "EM_UNKNOWN_USER_109",
1742 "EM_UNKNOWN_USER_110",
1743 "EM_UNKNOWN_USER_111",
1744 "EM_UNKNOWN_USER_112",
1745 "EM_UNKNOWN_USER_113",
1746 "EM_UNKNOWN_USER_114",
1747 "EM_UNKNOWN_USER_115",
1748 "EM_UNKNOWN_USER_116",
1749 "EM_UNKNOWN_USER_117",
1750 "EM_UNKNOWN_USER_118",
1751 "EM_UNKNOWN_USER_119",
1752 "EM_SETLANGOPTIONS",
1753 "EM_GETLANGOPTIONS",
1754 "EM_GETIMECOMPMODE",
1758 "EM_SETIMEMODEBIAS",
1763 get_msg_name(UINT msg)
1765 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1766 return edit_messages[msg - EM_GETSEL];
1767 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1768 return richedit_messages[msg - EM_CANPASTE];
1772 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1773 LPARAM lParam, BOOL unicode)
1775 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1777 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1778 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1780 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1781 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1787 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1788 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1789 UNSUPPORTED_MSG(EM_FMTLINES)
1790 UNSUPPORTED_MSG(EM_FORMATRANGE)
1791 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1792 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1793 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1794 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1795 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1796 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1797 UNSUPPORTED_MSG(EM_GETREDONAME)
1798 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1799 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1800 UNSUPPORTED_MSG(EM_GETUNDONAME)
1801 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1802 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1803 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1804 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1805 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1806 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1807 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1808 UNSUPPORTED_MSG(EM_SETPALETTE)
1809 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1810 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1811 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1812 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1813 UNSUPPORTED_MSG(WM_STYLECHANGING)
1814 UNSUPPORTED_MSG(WM_STYLECHANGED)
1815 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1817 /* Messages specific to Richedit controls */
1820 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1822 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1825 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1826 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1828 int vk = (int)((LPMSG)lParam)->wParam;
1829 /* if style says we want return key */
1830 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1832 code |= DLGC_WANTMESSAGE;
1834 /* we always handle ctrl-tab */
1835 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1837 code |= DLGC_WANTMESSAGE;
1844 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1845 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1846 editor = ME_MakeEditor(hWnd);
1847 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1850 case EM_EMPTYUNDOBUFFER:
1851 ME_EmptyUndoStack(editor);
1855 /* Note: wParam/lParam can be NULL */
1857 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1858 PUINT pto = lParam ? (PUINT)lParam : &to;
1859 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1860 if ((*pfrom|*pto) & 0xFFFF0000)
1862 return MAKELONG(*pfrom,*pto);
1866 CHARRANGE *pRange = (CHARRANGE *)lParam;
1867 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
1868 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1871 case EM_SETUNDOLIMIT:
1873 if ((int)wParam < 0)
1874 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1876 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1877 /* Setting a max stack size keeps wine from getting killed
1878 for hogging memory. Windows allocates all this memory at once, so
1879 no program would realistically set a value above our maximum. */
1880 return editor->nUndoLimit;
1883 return editor->pUndoStack != NULL;
1885 return editor->pRedoStack != NULL;
1886 case WM_UNDO: /* FIXME: actually not the same */
1895 /* these flags are equivalent to the ES_* counterparts */
1896 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1897 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1898 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1904 /* these flags are equivalent to ES_* counterparts
1905 * ECO_READONLY is already implemented in the code, only requires
1906 * setting the bit to work
1908 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1909 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
1910 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1911 DWORD settings = mask & raw;
1927 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1929 if (settings & ECO_AUTOWORDSELECTION)
1930 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1931 if (settings & ECO_SELECTIONBAR)
1932 editor->selofs = 16;
1935 ME_WrapMarkedParagraphs(editor);
1937 if (settings & ECO_VERTICAL)
1938 FIXME("ECO_VERTICAL not implemented yet!\n");
1939 if (settings & ECO_AUTOHSCROLL)
1940 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1941 if (settings & ECO_AUTOVSCROLL)
1942 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1943 if (settings & ECO_NOHIDESEL)
1944 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1945 if (settings & ECO_WANTRETURN)
1946 FIXME("ECO_WANTRETURN not implemented yet!\n");
1952 ME_InvalidateSelection(editor);
1953 ME_SetSelection(editor, wParam, lParam);
1954 ME_InvalidateSelection(editor);
1955 ME_SendSelChange(editor);
1958 case EM_SETSCROLLPOS:
1960 POINT *point = (POINT *)lParam;
1961 ME_ScrollAbs(editor, point->y);
1964 case EM_AUTOURLDETECT:
1966 if (wParam==1 || wParam ==0)
1968 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1971 return E_INVALIDARG;
1973 case EM_GETAUTOURLDETECT:
1975 return editor->AutoURLDetect_bEnable;
1980 CHARRANGE range = *(CHARRANGE *)lParam;
1982 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1984 ME_InvalidateSelection(editor);
1985 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1986 ME_InvalidateSelection(editor);
1987 ME_SendSelChange(editor);
1991 case EM_SHOWSCROLLBAR:
1993 ShowScrollBar(editor->hWnd, wParam, lParam);
1999 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
2003 int oldModify = editor->nModifyStep;
2005 if (!pStruct) return 0;
2007 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2008 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
2009 pStruct->flags, pStruct->codepage);
2011 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2012 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
2013 len = wszText ? lstrlenW(wszText) : 0;
2015 /* FIXME: this should support RTF strings too, according to MSDN */
2016 if (pStruct->flags & ST_SELECTION) {
2017 ME_GetSelection(editor, &from, &to);
2018 style = ME_GetSelectionInsertStyle(editor);
2019 ME_InternalDeleteText(editor, from, to - from);
2020 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2021 ME_ReleaseStyle(style);
2024 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2025 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2028 ME_CommitUndo(editor);
2029 if (!(pStruct->flags & ST_KEEPUNDO))
2031 editor->nModifyStep = oldModify;
2032 ME_EmptyUndoStack(editor);
2034 ME_UpdateRepaint(editor);
2037 case EM_SETBKGNDCOLOR:
2040 if (editor->rgbBackColor != -1) {
2041 DeleteObject(editor->hbrBackground);
2042 lColor = editor->rgbBackColor;
2044 else lColor = GetSysColor(COLOR_WINDOW);
2048 editor->rgbBackColor = -1;
2049 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2053 editor->rgbBackColor = lParam;
2054 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
2056 if (editor->bRedraw)
2058 InvalidateRect(hWnd, NULL, TRUE);
2064 return editor->nModifyStep == 0 ? 0 : 1;
2068 editor->nModifyStep = 1;
2070 editor->nModifyStep = 0;
2074 case EM_SETREADONLY:
2076 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
2078 nStyle |= ES_READONLY;
2080 nStyle &= ~ES_READONLY;
2081 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
2084 case EM_SETEVENTMASK:
2086 DWORD nOldMask = editor->nEventMask;
2088 editor->nEventMask = lParam;
2091 case EM_GETEVENTMASK:
2092 return editor->nEventMask;
2093 case EM_SETCHARFORMAT:
2095 CHARFORMAT2W buf, *p;
2096 BOOL bRepaint = TRUE;
2097 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
2098 if (p == NULL) return 0;
2100 ME_SetDefaultCharFormat(editor, p);
2101 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
2102 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2104 } else if (wParam == SCF_ALL) {
2105 if (editor->mode & TM_PLAINTEXT)
2106 ME_SetDefaultCharFormat(editor, p);
2108 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
2109 } else if (editor->mode & TM_PLAINTEXT) {
2113 ME_GetSelection(editor, &from, &to);
2114 bRepaint = (from != to);
2115 ME_SetSelectionCharFormat(editor, p);
2117 editor->nModifyStep = 1;
2118 ME_CommitUndo(editor);
2120 ME_RewrapRepaint(editor);
2123 case EM_GETCHARFORMAT:
2125 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
2126 if (dst->cbSize != sizeof(CHARFORMATA) &&
2127 dst->cbSize != sizeof(CHARFORMATW) &&
2128 dst->cbSize != sizeof(CHARFORMAT2A) &&
2129 dst->cbSize != sizeof(CHARFORMAT2W))
2131 tmp.cbSize = sizeof(tmp);
2133 ME_GetDefaultCharFormat(editor, &tmp);
2135 ME_GetSelectionCharFormat(editor, &tmp);
2136 ME_CopyToCFAny(dst, &tmp);
2139 case EM_SETPARAFORMAT:
2140 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2141 ME_RewrapRepaint(editor);
2142 ME_CommitUndo(editor);
2144 case EM_GETPARAFORMAT:
2145 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2147 case EM_GETFIRSTVISIBLELINE:
2149 ME_DisplayItem *p = editor->pBuffer->pFirst;
2150 int y = ME_GetYScrollPos(editor);
2155 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
2156 if (p->type == diTextEnd)
2158 if (p->type == diParagraph) {
2159 ypara = p->member.para.nYPos;
2162 ystart = ypara + p->member.row.nYPos;
2163 yend = ystart + p->member.row.nHeight;
2171 case EM_HIDESELECTION:
2173 editor->bHideSelection = (wParam != 0);
2174 ME_InvalidateSelection(editor);
2179 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
2180 return TRUE; /* Should return false if a single line richedit control */
2185 ME_GetSelection(editor, &from, &to);
2186 ME_InternalDeleteText(editor, from, to-from);
2187 ME_CommitUndo(editor);
2188 ME_UpdateRepaint(editor);
2195 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
2196 size_t len = wszText ? lstrlenW(wszText) : 0;
2197 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
2199 ME_GetSelection(editor, &from, &to);
2200 style = ME_GetSelectionInsertStyle(editor);
2201 ME_InternalDeleteText(editor, from, to-from);
2202 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2203 ME_ReleaseStyle(style);
2204 /* drop temporary style if line end */
2206 * FIXME question: does abc\n mean: put abc,
2207 * clear temp style, put \n? (would require a change)
2209 if (len>0 && wszText[len-1] == '\n')
2210 ME_ClearTempStyle(editor);
2211 ME_EndToUnicode(unicode, wszText);
2212 ME_CommitUndo(editor);
2214 ME_EmptyUndoStack(editor);
2215 ME_UpdateRepaint(editor);
2218 case EM_SCROLLCARET:
2220 int top, bottom; /* row's edges relative to document top */
2222 ME_DisplayItem *para, *row;
2224 nPos = ME_GetYScrollPos(editor);
2225 row = ME_RowStart(editor->pCursors[0].pRun);
2226 para = ME_GetParagraph(row);
2227 top = para->member.para.nYPos + row->member.row.nYPos;
2228 bottom = top + row->member.row.nHeight;
2230 if (top < nPos) /* caret above window */
2231 ME_ScrollAbs(editor, top);
2232 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
2233 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
2241 BOOL bRepaint = LOWORD(lParam);
2244 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
2245 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
2247 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
2248 ReleaseDC(hWnd, hDC);
2249 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
2250 ME_SetDefaultCharFormat(editor, &fmt);
2252 ME_CommitUndo(editor);
2254 ME_RewrapRepaint(editor);
2259 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2262 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
2263 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
2265 /* Undocumented: WM_SETTEXT supports RTF text */
2266 ME_StreamInRTFString(editor, 0, (char *)lParam);
2270 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
2271 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
2272 if (lstrlenW(wszText) > 0)
2276 /* uses default style! */
2277 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2282 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
2285 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2287 ME_EndToUnicode(unicode, wszText);
2291 TRACE("WM_SETTEXT - NULL\n");
2292 ME_CommitUndo(editor);
2293 ME_EmptyUndoStack(editor);
2294 ME_SetSelection(editor, 0, 0);
2295 editor->nModifyStep = 0;
2296 ME_UpdateRepaint(editor);
2301 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2302 if (IsClipboardFormatAvailable(nRTFFormat))
2304 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2312 ME_GlobalDestStruct gds;
2313 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2316 if (IsClipboardFormatAvailable(nRTFFormat))
2317 cf = nRTFFormat, dwFormat = SF_RTF;
2318 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2319 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2323 if (!OpenClipboard(hWnd))
2325 gds.hData = GetClipboardData(cf);
2327 es.dwCookie = (DWORD)&gds;
2328 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2329 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
2337 LPDATAOBJECT dataObj = NULL;
2341 if (editor->cPasswordMask)
2342 return 0; /* Copying or Cutting masked text isn't allowed */
2344 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2345 if(editor->lpOleCallback)
2346 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2347 if(FAILED(hr) || !dataObj)
2348 hr = ME_GetDataObject(editor, &range, &dataObj);
2350 hr = OleSetClipboard(dataObj);
2351 IDataObject_Release(dataObj);
2353 if (SUCCEEDED(hr) && msg == WM_CUT)
2355 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2356 ME_CommitUndo(editor);
2357 ME_UpdateRepaint(editor);
2361 case WM_GETTEXTLENGTH:
2363 GETTEXTLENGTHEX how;
2365 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2366 how.codepage = unicode ? 1200 : CP_ACP;
2367 return ME_GetTextLengthEx(editor, &how);
2369 case EM_GETTEXTLENGTHEX:
2370 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2375 LPSTR bufferA = NULL;
2376 LPWSTR bufferW = NULL;
2379 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2381 bufferA = heap_alloc(wParam + 2);
2383 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
2384 ex.flags = GT_USECRLF;
2385 ex.codepage = unicode ? 1200 : CP_ACP;
2386 ex.lpDefaultChar = NULL;
2387 ex.lpUsedDefaultChar = NULL;
2388 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2392 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
2393 if (lstrlenW(bufferW) >= wParam) rc = 0;
2397 memcpy((LPSTR)lParam, bufferA, wParam);
2398 if (strlen(bufferA) >= wParam) rc = 0;
2406 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2407 int nStart, nCount; /* in chars */
2409 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2410 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2412 if (ex->flags & GT_SELECTION)
2414 ME_GetSelection(editor, &nStart, &nCount);
2420 nCount = 0x7fffffff;
2422 if (ex->codepage == 1200)
2424 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2425 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2429 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2430 we can just take a bigger buffer? :) */
2431 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2433 DWORD buflen = ex->cb;
2437 nCount = min(nCount, ex->cb - 1);
2438 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2440 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2441 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2442 if (rc) rc--; /* do not count 0 terminator */
2451 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2452 ME_GetSelection(editor, &from, &to);
2453 tr.chrg.cpMin = from;
2455 tr.lpstrText = (WCHAR *)lParam;
2456 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2458 case EM_GETSCROLLPOS:
2460 POINT *point = (POINT *)lParam;
2461 point->x = 0; /* FIXME side scrolling not implemented */
2462 point->y = ME_GetYScrollPos(editor);
2465 case EM_GETTEXTRANGE:
2467 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2468 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2469 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2470 editor->bEmulateVersion10, ME_GetTextLength(editor));
2472 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2475 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2476 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2477 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2478 /* FIXME this is a potential security hole (buffer overrun)
2479 if you know more about wchar->mbyte conversion please explain
2481 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2488 ME_DisplayItem *run;
2489 const unsigned int nMaxChars = *(WORD *) lParam;
2490 unsigned int nEndChars, nCharsLeft = nMaxChars;
2491 char *dest = (char *) lParam;
2492 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2493 we need to know how if we have the extra \n or not */
2494 int nLF = editor->bEmulateVersion10;
2496 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2497 unicode ? "Unicode" : "Ansi");
2499 run = ME_FindRowWithNumber(editor, wParam);
2503 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2504 && !(run->member.run.nFlags & MERF_ENDPARA))
2508 if (run->type != diRun)
2510 strText = run->member.run.strText;
2511 nCopy = min(nCharsLeft, strText->nLen);
2514 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2516 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2517 nCharsLeft, NULL, NULL);
2518 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2519 nCharsLeft -= nCopy;
2522 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2523 nEndChars = min(nCharsLeft, 2 + nLF);
2524 nCharsLeft -= nEndChars;
2527 const WCHAR src[] = {'\r', '\0'};
2528 const WCHAR src10[] = {'\r', '\n', '\0'};
2529 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2532 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2534 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2536 if (nEndChars == 2 + nLF)
2537 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2539 return nMaxChars - nCharsLeft;
2541 case EM_GETLINECOUNT:
2543 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2546 while (item != editor->pBuffer->pLast)
2548 assert(item->type == diParagraph);
2549 nRows += item->member.para.nRows;
2550 item = item->member.para.next_para;
2552 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2553 return max(1, nRows);
2555 case EM_LINEFROMCHAR:
2558 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2560 return ME_RowNumberFromCharOfs(editor, wParam);
2562 case EM_EXLINEFROMCHAR:
2565 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2567 return ME_RowNumberFromCharOfs(editor, lParam);
2571 ME_DisplayItem *item, *para;
2575 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2577 item = ME_FindRowWithNumber(editor, wParam);
2580 para = ME_GetParagraph(item);
2581 item = ME_FindItemFwd(item, diRun);
2582 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2583 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2588 ME_DisplayItem *item, *item_end;
2589 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2591 if (wParam > ME_GetTextLength(editor))
2595 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2598 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2599 item = ME_RowStart(item);
2600 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2601 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2602 if (item_end->type == diStartRow)
2603 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2605 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2606 - (editor->bEmulateVersion10?2:1);
2607 nChars = nNextLineOfs - nThisLineOfs;
2608 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2611 case EM_EXLIMITTEXT:
2613 if ((int)lParam < 0)
2616 editor->nTextLimit = 65536;
2618 editor->nTextLimit = (int) lParam;
2624 editor->nTextLimit = 65536;
2626 editor->nTextLimit = (int) wParam;
2629 case EM_GETLIMITTEXT:
2631 return editor->nTextLimit;
2635 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2636 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2640 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2641 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2642 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2648 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2649 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2653 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2654 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2655 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2661 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2662 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2664 case EM_FINDTEXTEXW:
2666 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2667 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2670 if (!wParam || !lParam)
2672 *(int *)wParam = editor->nZoomNumerator;
2673 *(int *)lParam = editor->nZoomDenominator;
2676 return ME_SetZoom(editor, wParam, lParam);
2677 case EM_CHARFROMPOS:
2678 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2679 case EM_POSFROMCHAR:
2681 ME_DisplayItem *pRun;
2682 int nCharOfs, nOffset, nLength;
2686 /* detect which API version we're dealing with */
2687 if (wParam >= 0x40000)
2689 nLength = ME_GetTextLength(editor);
2691 if (nCharOfs < nLength) {
2692 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2693 assert(pRun->type == diRun);
2694 pt.y = pRun->member.run.pt.y;
2695 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2696 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2699 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2701 pt.x += editor->selofs;
2702 if (wParam >= 0x40000) {
2703 *(POINTL *)wParam = pt;
2705 return MAKELONG( pt.x, pt.y );
2708 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2709 { /* Squelch the default horizontal scrollbar it would make */
2710 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2712 ME_CommitUndo(editor);
2713 ME_WrapMarkedParagraphs(editor);
2714 ME_MoveCaret(editor);
2717 ME_DestroyEditor(editor);
2718 SetWindowLongPtrW(hWnd, 0, 0);
2720 case WM_LBUTTONDOWN:
2721 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2722 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2725 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2727 ME_LinkNotify(editor,msg,wParam,lParam);
2728 ME_SetCursor(editor, LOWORD(lParam));
2731 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2732 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2734 if (GetCapture() == hWnd)
2735 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2736 ME_LinkNotify(editor,msg,wParam,lParam);
2737 ME_SetCursor(editor, LOWORD(lParam));
2740 if (GetCapture() == hWnd)
2742 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2743 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2745 editor->linesel = 0;
2746 ME_SetCursor(editor, LOWORD(lParam));
2747 ME_LinkNotify(editor,msg,wParam,lParam);
2749 case WM_LBUTTONDBLCLK:
2750 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2751 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2753 ME_LinkNotify(editor,msg,wParam,lParam);
2754 ME_SelectWord(editor);
2757 case WM_RBUTTONDOWN:
2758 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2759 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2762 case WM_CONTEXTMENU:
2763 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2767 if (editor->bRedraw)
2772 hDC = BeginPaint(hWnd, &ps);
2773 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2774 EndPaint(hWnd, &ps);
2778 editor->bHaveFocus = TRUE;
2779 ME_ShowCaret(editor);
2780 ME_SendOldNotify(editor, EN_SETFOCUS);
2783 ME_HideCaret(editor);
2784 editor->bHaveFocus = FALSE;
2785 ME_SendOldNotify(editor, EN_KILLFOCUS);
2789 if (editor->bRedraw)
2791 HDC hDC = (HDC)wParam;
2793 if (GetUpdateRect(hWnd,&rc,TRUE))
2795 FillRect(hDC, &rc, editor->hbrBackground);
2801 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2804 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2805 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2809 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2810 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2812 if (ME_KeyDown(editor, LOWORD(wParam)))
2820 wstr = (WCHAR)wParam;
2823 CHAR charA = wParam;
2824 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2826 if (editor->AutoURLDetect_bEnable)
2827 ME_AutoURLDetect(editor, wstr);
2831 case 1: /* Ctrl-A */
2832 ME_SetSelection(editor, 0, -1);
2834 case 3: /* Ctrl-C */
2835 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2839 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2840 MessageBeep(MB_ICONERROR);
2841 return 0; /* FIXME really 0 ? */
2846 case 22: /* Ctrl-V */
2847 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2849 case 24: /* Ctrl-X */
2850 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2852 case 25: /* Ctrl-Y */
2853 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2855 case 26: /* Ctrl-Z */
2856 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2859 if (((unsigned)wstr)>=' '
2860 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2862 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2863 /* WM_CHAR is restricted to nTextLimit */
2865 ME_GetSelection(editor, &from, &to);
2866 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2868 ME_Style *style = ME_GetInsertStyle(editor, 0);
2869 ME_SaveTempStyle(editor);
2870 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2871 ME_ReleaseStyle(style);
2872 ME_CommitUndo(editor);
2874 ME_UpdateRepaint(editor);
2878 case EM_SCROLL: /* fall through */
2884 origNPos = ME_GetYScrollPos(editor);
2887 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2888 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2889 if (lineHeight <= 0) lineHeight = 24;
2891 switch(LOWORD(wParam))
2894 ME_ScrollUp(editor,lineHeight);
2897 ME_ScrollDown(editor,lineHeight);
2900 ME_ScrollUp(editor,editor->sizeWindow.cy);
2903 ME_ScrollDown(editor,editor->sizeWindow.cy);
2906 case SB_THUMBPOSITION:
2907 ME_ScrollAbs(editor,HIWORD(wParam));
2910 if (msg == EM_SCROLL)
2911 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2917 UINT pulScrollLines;
2919 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2920 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2923 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2924 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2926 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2928 /* FIXME follow the original */
2929 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2935 *((RECT *)lParam) = editor->rcFormat;
2943 RECT *rc = (RECT *)lParam;
2947 editor->rcFormat.left += rc->left;
2948 editor->rcFormat.top += rc->top;
2949 editor->rcFormat.right += rc->right;
2950 editor->rcFormat.bottom += rc->bottom;
2954 editor->rcFormat = *rc;
2959 GetClientRect(hWnd, &editor->rcFormat);
2961 if (msg != EM_SETRECTNP)
2962 ME_RewrapRepaint(editor);
2965 case EM_REQUESTRESIZE:
2966 ME_SendRequestResize(editor, TRUE);
2969 if ((editor->bRedraw = wParam))
2970 ME_RewrapRepaint(editor);
2974 GetClientRect(hWnd, &editor->rcFormat);
2975 ME_RewrapRepaint(editor);
2976 return DefWindowProcW(hWnd, msg, wParam, lParam);
2978 /* IME messages to make richedit controls IME aware */
2979 case WM_IME_SETCONTEXT:
2980 case WM_IME_CONTROL:
2982 case WM_IME_COMPOSITIONFULL:
2984 case WM_IME_STARTCOMPOSITION:
2986 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
2987 ME_DeleteSelection(editor);
2988 ME_CommitUndo(editor);
2989 ME_UpdateRepaint(editor);
2992 case WM_IME_COMPOSITION:
2996 ME_Style *style = ME_GetInsertStyle(editor, 0);
2997 hIMC = ImmGetContext(hWnd);
2998 ME_DeleteSelection(editor);
2999 ME_CommitUndo(editor);
3000 ME_SaveTempStyle(editor);
3001 if (lParam & GCS_RESULTSTR)
3003 LPWSTR lpCompStr = NULL;
3006 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3007 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3008 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
3009 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3010 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3012 else if (lParam & GCS_COMPSTR)
3014 LPWSTR lpCompStr = NULL;
3017 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
3018 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3019 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
3020 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3022 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3023 ME_SetSelection(editor,editor->imeStartIndex,
3024 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
3026 ME_ReleaseStyle(style);
3027 ME_UpdateRepaint(editor);
3030 case WM_IME_ENDCOMPOSITION:
3032 ME_DeleteSelection(editor);
3033 editor->imeStartIndex=-1;
3036 case EM_GETOLEINTERFACE:
3038 LPVOID *ppvObj = (LPVOID*) lParam;
3039 return CreateIRichEditOle(editor, ppvObj);
3041 case EM_GETPASSWORDCHAR:
3043 return editor->cPasswordMask;
3045 case EM_SETOLECALLBACK:
3046 if(editor->lpOleCallback)
3047 IUnknown_Release(editor->lpOleCallback);
3048 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
3049 if(editor->lpOleCallback)
3050 IUnknown_AddRef(editor->lpOleCallback);
3052 case EM_GETWORDBREAKPROC:
3053 return (LRESULT)editor->pfnWordBreak;
3054 case EM_SETWORDBREAKPROC:
3056 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
3058 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
3059 return (LRESULT)pfnOld;
3061 case EM_SETTEXTMODE:
3066 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
3069 /*Check for valid wParam*/
3070 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
3071 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
3072 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
3076 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
3078 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
3079 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
3081 /*FIXME: Currently no support for undo level and code page options*/
3082 editor->mode = (editor->mode & (~mask)) | changes;
3088 case EM_SETPASSWORDCHAR:
3090 editor->cPasswordMask = wParam;
3091 ME_RewrapRepaint(editor);
3096 return DefWindowProcW(hWnd, msg, wParam, lParam);
3101 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3103 BOOL unicode = TRUE;
3105 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3106 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
3109 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
3112 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3114 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
3117 /******************************************************************
3118 * RichEditANSIWndProc (RICHED20.10)
3120 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3122 return RichEditWndProcA(hWnd, msg, wParam, lParam);
3125 /******************************************************************
3126 * RichEdit10ANSIWndProc (RICHED20.9)
3128 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3132 /* FIXME: this is NOT the same as 2.0 version */
3133 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
3134 if (msg == WM_NCCREATE)
3136 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
3138 editor->bEmulateVersion10 = TRUE;
3139 editor->pBuffer->pLast->member.para.nCharOfs = 2;
3144 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
3146 HWND hWnd = editor->hWnd;
3147 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
3150 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3153 ME_Cursor tmpCursor;
3154 int nCharOfs; /* The start of the clicked text. Absolute character offset */
3159 x = (short)LOWORD(lParam);
3160 y = (short)HIWORD(lParam);
3161 nCharOfs = ME_CharFromPos(editor, x, y);
3162 if (nCharOfs < 0) return;
3164 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
3165 tmpRun = &tmpCursor.pRun->member.run;
3167 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
3168 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
3169 { /* The clicked run has CFE_LINK set */
3170 info.nmhdr.hwndFrom = editor->hWnd;
3171 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
3172 info.nmhdr.code = EN_LINK;
3174 info.wParam = wParam;
3175 info.lParam = lParam;
3176 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
3177 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
3178 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
3182 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
3184 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
3187 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
3188 item = item->member.para.next_para;
3191 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
3192 item = item->member.para.next_para;
3199 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
3201 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
3203 WCHAR *pStart = buffer;
3212 int nLen = ME_StrLen(item->member.run.strText) - nStart;
3215 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
3222 item = ME_FindItemFwd(item, diRun);
3225 while(nChars && item)
3227 int nLen = ME_StrLen(item->member.run.strText);
3231 if (item->member.run.nFlags & MERF_ENDPARA)
3233 if (!ME_FindItemFwd(item, diRun))
3234 /* No '\r' is appended to the last paragraph. */
3236 else if (bCRLF && nChars == 1) {
3247 /* our end paragraph consists of 2 characters now */
3248 if (editor->bEmulateVersion10)
3253 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
3260 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3264 item = ME_FindItemFwd(item, diRun);
3267 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3271 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
3276 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3277 wcW.lpfnWndProc = RichEditWndProcW;
3279 wcW.cbWndExtra = sizeof(ME_TextEditor *);
3280 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
3282 wcW.hCursor = hBeam;
3283 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3284 wcW.lpszMenuName = NULL;
3286 if (is_version_nt())
3288 wcW.lpszClassName = RichEdit20W;
3289 if (!RegisterClassW(&wcW)) return FALSE;
3290 wcW.lpszClassName = RichEdit50W;
3291 if (!RegisterClassW(&wcW)) return FALSE;
3295 /* WNDCLASSA/W have the same layout */
3296 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
3297 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3298 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
3299 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3302 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3303 wcA.lpfnWndProc = RichEditWndProcA;
3305 wcA.cbWndExtra = sizeof(ME_TextEditor *);
3306 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
3308 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3309 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3310 wcA.lpszMenuName = NULL;
3311 wcA.lpszClassName = "RichEdit20A";
3312 if (!RegisterClassA(&wcA)) return FALSE;
3313 wcA.lpszClassName = "RichEdit50A";
3314 if (!RegisterClassA(&wcA)) return FALSE;
3319 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3320 /* FIXME: Not implemented */
3321 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3322 hWnd, msg, get_msg_name(msg), wParam, lParam);
3323 return DefWindowProcW(hWnd, msg, wParam, lParam);
3326 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3327 /* FIXME: Not implemented */
3328 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3329 hWnd, msg, get_msg_name(msg), wParam, lParam);
3330 return DefWindowProcW(hWnd, msg, wParam, lParam);
3333 /******************************************************************
3334 * REExtendedRegisterClass (RICHED20.8)
3336 * FIXME undocumented
3337 * Need to check for errors and implement controls and callbacks
3339 LRESULT WINAPI REExtendedRegisterClass(void)
3344 FIXME("semi stub\n");
3348 wcW.hInstance = NULL;
3351 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
3352 wcW.lpszMenuName = NULL;
3354 if (!ME_ListBoxRegistered)
3356 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
3357 wcW.lpfnWndProc = REListWndProc;
3358 wcW.lpszClassName = REListBox20W;
3359 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
3362 if (!ME_ComboBoxRegistered)
3364 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
3365 wcW.lpfnWndProc = REComboWndProc;
3366 wcW.lpszClassName = REComboBox20W;
3367 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
3371 if (ME_ListBoxRegistered)
3373 if (ME_ComboBoxRegistered)
3379 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
3399 int curf_ef, link_ef, def_ef;
3400 int cur_prefx, prefx_cnt;
3401 int sel_min, sel_max;
3404 int URLmin, URLmax = 0;
3407 CHARFORMAT2W cur_format;
3408 CHARFORMAT2W default_format;
3410 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
3411 sel_min = ins_pt.cpMin;
3412 sel_max = ins_pt.cpMax;
3413 if (sel_min==sel_max)
3415 if (sel_min!=sel_max)
3416 car_pos = ME_GetTextLength(editor)+1;
3417 cur_format.cbSize = sizeof(cur_format);
3418 default_format.cbSize = sizeof(default_format);
3419 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
3420 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
3421 link.cbSize = sizeof(link);
3422 link.dwMask = CFM_LINK;
3423 link.dwEffects = CFE_LINK;
3424 curf_ef = cur_format.dwEffects & link.dwEffects;
3425 def_ef = default_format.dwEffects & link.dwEffects;
3426 link_ef = link.dwEffects & link.dwEffects;
3427 if (curf_ef == link_ef)
3429 if( curChar == '\n' || curChar=='\r' || curChar==' ')
3431 ME_SetSelection(editor, car_pos, car_pos);
3432 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
3437 if (curf_ef == def_ef)
3440 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
3441 while (cur_prefx<=prefx_cnt)
3445 ft.lpstrText = prefixes[cur_prefx].text;
3446 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
3447 URLmax=max(0, car_pos);
3448 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
3450 URLmax = ME_GetTextLength(editor)+1;
3452 ft.chrg.cpMin = URLmin;
3453 ft.chrg.cpMax = URLmax;
3454 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
3460 url.cpMax=car_pos-1;
3461 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
3462 ME_RewrapRepaint(editor);