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
117 - EM_SETTARGETDEVICE (partial)
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;
250 HANDLE me_heap = NULL;
252 static BOOL ME_ListBoxRegistered = FALSE;
253 static BOOL ME_ComboBoxRegistered = FALSE;
255 static inline int is_version_nt(void)
257 return !(GetVersion() & 0x80000000);
260 static ME_TextBuffer *ME_MakeText(void) {
262 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
264 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
265 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
271 p1->member.para.next_para = p2;
272 p2->member.para.prev_para = p1;
273 p2->member.para.nCharOfs = 0;
277 buf->pCharStyle = NULL;
283 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
285 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
288 TRACE("%08x %p\n", dwFormat, stream);
295 ME_StreamInFill(stream);
296 if (stream->editstream->dwError)
302 if (!(dwFormat & SF_UNICODE))
304 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
305 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
310 nWideChars = stream->dwSize >> 1;
311 pText = (WCHAR *)stream->buffer;
314 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
315 if (stream->dwSize == 0)
319 ME_CommitUndo(editor);
320 ME_UpdateRepaint(editor);
324 static void ME_RTFCharAttrHook(RTF_Info *info)
327 fmt.cbSize = sizeof(fmt);
331 switch(info->rtfMinor)
334 /* FIXME add more flags once they're implemented */
335 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
336 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
337 fmt.yHeight = 12*20; /* 12pt */
338 fmt.wWeight = FW_NORMAL;
339 fmt.bUnderlineType = CFU_UNDERLINENONE;
342 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
343 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
344 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
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 = f->rtfFPitch | (f->rtfFFamily << 4);
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_BORDER | PFM_LINESPACING | PFM_TABSTOPS | PFM_OFFSET |
456 PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE | PFM_STARTINDENT;
457 /* TODO: numbering, shading */
458 fmt.wAlignment = PFA_LEFT;
460 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
461 fmt.wBorderWidth = fmt.wBorders = 0;
462 fmt.bLineSpacingRule = 0;
463 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
464 RTFFlushOutputBuffer(info);
465 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
469 ME_DisplayItem *para;
471 RTFFlushOutputBuffer(info);
472 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
473 assert(para->member.para.pCells);
474 para->member.para.bTable = TRUE;
478 ME_GetSelectionParaFormat(info->editor, &fmt);
479 fmt.dwMask |= PFM_STARTINDENT | PFM_OFFSET;
480 fmt.dxStartIndent += info->rtfParam;
481 fmt.dxOffset = -info->rtfParam;
484 /* we assume rtfLeftIndent is always specified before rtfFirstIndent */
485 ME_GetSelectionParaFormat(info->editor, &fmt);
486 fmt.dwMask |= PFM_STARTINDENT;
487 fmt.dxStartIndent = info->rtfParam;
491 fmt.dwMask = PFM_RIGHTINDENT;
492 fmt.dxRightIndent = info->rtfParam;
496 fmt.dwMask = PFM_ALIGNMENT;
497 fmt.wAlignment = PFA_LEFT;
500 fmt.dwMask = PFM_ALIGNMENT;
501 fmt.wAlignment = PFA_RIGHT;
504 fmt.dwMask = PFM_ALIGNMENT;
505 fmt.wAlignment = PFA_CENTER;
508 ME_GetSelectionParaFormat(info->editor, &fmt);
509 if (!(fmt.dwMask & PFM_TABSTOPS))
511 fmt.dwMask |= PFM_TABSTOPS;
514 if (fmt.cTabCount < MAX_TAB_STOPS)
515 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
518 fmt.dwMask = PFM_KEEP;
519 fmt.wEffects = PFE_KEEP;
521 case rtfNoWidowControl:
522 fmt.dwMask = PFM_NOWIDOWCONTROL;
523 fmt.wEffects = PFE_NOWIDOWCONTROL;
526 fmt.dwMask = PFM_KEEPNEXT;
527 fmt.wEffects = PFE_KEEPNEXT;
530 fmt.dwMask = PFM_SPACEAFTER;
531 fmt.dySpaceAfter = info->rtfParam;
534 fmt.dwMask = PFM_SPACEBEFORE;
535 fmt.dySpaceBefore = info->rtfParam;
537 case rtfSpaceBetween:
538 fmt.dwMask = PFM_LINESPACING;
539 if ((int)info->rtfParam > 0)
541 fmt.dyLineSpacing = info->rtfParam;
542 fmt.bLineSpacingRule = 3;
546 fmt.dyLineSpacing = info->rtfParam;
547 fmt.bLineSpacingRule = 4;
549 case rtfSpaceMultiply:
550 fmt.dwMask = PFM_LINESPACING;
551 fmt.dyLineSpacing = info->rtfParam * 20;
552 fmt.bLineSpacingRule = 5;
555 fmt.dwMask = PFM_NUMBERING;
556 fmt.wNumbering = PFN_BULLET;
559 fmt.dwMask = PFM_NUMBERING;
560 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
562 case rtfParNumDecimal:
563 fmt.dwMask = PFM_NUMBERING;
564 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
566 case rtfParNumIndent:
567 fmt.dwMask = PFM_NUMBERINGTAB;
568 fmt.wNumberingTab = info->rtfParam;
570 case rtfParNumStartAt:
571 fmt.dwMask = PFM_NUMBERINGSTART;
572 fmt.wNumberingStart = info->rtfParam;
575 ME_GetSelectionParaFormat(info->editor, &fmt);
576 if (!(fmt.dwMask & PFM_BORDER))
578 fmt.dwMask |= PFM_BORDER;
579 fmt.wBorderSpace = 0;
580 fmt.wBorderWidth = 1;
586 ME_GetSelectionParaFormat(info->editor, &fmt);
587 if (!(fmt.dwMask & PFM_BORDER))
589 fmt.dwMask |= PFM_BORDER;
590 fmt.wBorderSpace = 0;
591 fmt.wBorderWidth = 1;
597 ME_GetSelectionParaFormat(info->editor, &fmt);
598 if (!(fmt.dwMask & PFM_BORDER))
600 fmt.dwMask |= PFM_BORDER;
601 fmt.wBorderSpace = 0;
602 fmt.wBorderWidth = 1;
607 case rtfBorderBottom:
608 ME_GetSelectionParaFormat(info->editor, &fmt);
609 if (!(fmt.dwMask & PFM_BORDER))
611 fmt.dwMask |= PFM_BORDER;
612 fmt.wBorderSpace = 0;
613 fmt.wBorderWidth = 1;
618 case rtfBorderSingle:
619 ME_GetSelectionParaFormat(info->editor, &fmt);
620 /* we assume that borders have been created before (RTF spec) */
621 fmt.wBorders &= ~0x70;
622 fmt.wBorders |= 1 << 8;
625 ME_GetSelectionParaFormat(info->editor, &fmt);
626 /* we assume that borders have been created before (RTF spec) */
627 fmt.wBorders &= ~0x70;
628 fmt.wBorders |= 2 << 8;
630 case rtfBorderShadow:
631 ME_GetSelectionParaFormat(info->editor, &fmt);
632 /* we assume that borders have been created before (RTF spec) */
633 fmt.wBorders &= ~0x70;
634 fmt.wBorders |= 10 << 8;
636 case rtfBorderDouble:
637 ME_GetSelectionParaFormat(info->editor, &fmt);
638 /* we assume that borders have been created before (RTF spec) */
639 fmt.wBorders &= ~0x70;
640 fmt.wBorders |= 7 << 8;
643 ME_GetSelectionParaFormat(info->editor, &fmt);
644 /* we assume that borders have been created before (RTF spec) */
645 fmt.wBorders &= ~0x70;
646 fmt.wBorders |= 11 << 8;
649 ME_GetSelectionParaFormat(info->editor, &fmt);
650 /* we assume that borders have been created before (RTF spec) */
651 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
654 ME_GetSelectionParaFormat(info->editor, &fmt);
655 /* we assume that borders have been created before (RTF spec) */
656 fmt.wBorderSpace = info->rtfParam;
660 RTFFlushOutputBuffer(info);
661 /* FIXME too slow ? how come ?*/
662 ME_SetSelectionParaFormat(info->editor, &fmt);
666 static void ME_RTFTblAttrHook(RTF_Info *info)
668 ME_DisplayItem *para;
670 switch (info->rtfMinor)
673 RTFFlushOutputBuffer(info);
674 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
676 /* Release possibly inherited cell definitions */
677 ME_DestroyTableCellList(para);
679 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
680 para->member.para.pCells->nRightBoundary = 0;
681 para->member.para.pCells->next = NULL;
682 para->member.para.pLastCell = para->member.para.pCells;
685 RTFFlushOutputBuffer(info);
686 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
688 if (para->member.para.pLastCell->nRightBoundary)
690 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
693 para->member.para.pLastCell->next = pCell;
694 para->member.para.pLastCell = pCell;
696 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
701 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
704 LPOLEOBJECT lpObject = NULL;
705 LPSTORAGE lpStorage = NULL;
706 LPOLECLIENTSITE lpClientSite = NULL;
707 LPDATAOBJECT lpDataObject = NULL;
708 LPOLECACHE lpOleCache = NULL;
717 stgm.tymed = TYMED_ENHMF;
718 stgm.u.hEnhMetaFile = hemf;
719 fm.cfFormat = CF_ENHMETAFILE;
723 stgm.tymed = TYMED_GDI;
724 stgm.u.hBitmap = hbmp;
725 fm.cfFormat = CF_BITMAP;
727 stgm.pUnkForRelease = NULL;
730 fm.dwAspect = DVASPECT_CONTENT;
732 fm.tymed = stgm.tymed;
734 if (!info->lpRichEditOle)
736 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
739 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
741 /* FIXME: enable it when rich-edit properly implements this method */
742 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
743 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
745 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
746 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
747 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
748 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
749 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
753 reobject.cbStruct = sizeof(reobject);
754 reobject.cp = REO_CP_SELECTION;
755 reobject.clsid = clsid;
756 reobject.poleobj = lpObject;
757 reobject.pstg = lpStorage;
758 reobject.polesite = lpClientSite;
759 /* convert from twips to .01 mm */
760 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
761 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
762 reobject.dvaspect = DVASPECT_CONTENT;
763 reobject.dwFlags = 0; /* FIXME */
766 /* FIXME: could be simpler */
767 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
770 if (lpObject) IOleObject_Release(lpObject);
771 if (lpClientSite) IOleClientSite_Release(lpClientSite);
772 if (lpStorage) IStorage_Release(lpStorage);
773 if (lpDataObject) IDataObject_Release(lpDataObject);
774 if (lpOleCache) IOleCache_Release(lpOleCache);
779 static void ME_RTFReadPictGroup(RTF_Info *info)
783 unsigned bufsz, bufidx;
789 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
792 if (info->rtfClass == rtfEOF)
795 /* fetch picture type */
796 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
798 mfp.mm = info->rtfParam;
801 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
803 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
806 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
808 gfx = gfx_enhmetafile;
812 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
816 /* fetch picture attributes */
820 if (info->rtfClass == rtfEOF)
822 if (info->rtfClass == rtfText)
824 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
826 ERR("Expected picture attribute (%d %d)\n",
827 info->rtfClass, info->rtfMajor);
830 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
832 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
834 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
836 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
838 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
839 sz.cx = info->rtfParam;
840 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
841 sz.cy = info->rtfParam;
843 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
845 /* fetch picture data */
848 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
849 val = info->rtfMajor;
850 for (flip = TRUE;; flip = !flip)
853 if (info->rtfClass == rtfEOF)
855 HeapFree(GetProcessHeap(), 0, buffer);
856 return; /* Warn ?? */
858 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
860 if (info->rtfClass != rtfText) goto skip_group;
863 if (bufidx >= bufsz &&
864 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
866 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
869 val = info->rtfMajor;
871 if (flip) FIXME("wrong hex string\n");
875 case gfx_enhmetafile:
876 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
877 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
880 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
881 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
885 BITMAPINFO* bi = (BITMAPINFO*)buffer;
887 unsigned nc = bi->bmiHeader.biClrUsed;
889 /* not quite right, especially for bitfields type of compression */
890 if (!nc && bi->bmiHeader.biBitCount <= 8)
891 nc = 1 << bi->bmiHeader.biBitCount;
892 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
893 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
894 bi, DIB_RGB_COLORS)))
895 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
902 HeapFree(GetProcessHeap(), 0, buffer);
903 RTFRouteToken (info); /* feed "}" back to router */
906 HeapFree(GetProcessHeap(), 0, buffer);
908 RTFRouteToken(info); /* feed "}" back to router */
911 /* for now, lookup the \result part and use it, whatever the object */
912 static void ME_RTFReadObjectGroup(RTF_Info *info)
917 if (info->rtfClass == rtfEOF)
919 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
921 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
924 if (info->rtfClass == rtfEOF)
926 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
930 while (RTFGetToken (info) != rtfEOF)
932 if (info->rtfClass == rtfGroup)
934 if (info->rtfMajor == rtfBeginGroup) level++;
935 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
940 else RTFSkipGroup(info);
943 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
945 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
949 RTFRouteToken(info); /* feed "}" back to router */
952 static void ME_RTFReadHook(RTF_Info *info) {
953 switch(info->rtfClass)
956 switch(info->rtfMajor)
959 if (info->stackTop < maxStack) {
960 info->stack[info->stackTop].fmt = info->style->fmt;
961 info->stack[info->stackTop].codePage = info->codePage;
962 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
965 info->styleChanged = FALSE;
970 RTFFlushOutputBuffer(info);
971 if (info->stackTop<=1) {
972 info->rtfClass = rtfEOF;
976 assert(info->stackTop >= 0);
977 if (info->styleChanged)
979 /* FIXME too slow ? how come ? */
980 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
981 ME_ReleaseStyle(info->style);
983 info->codePage = info->stack[info->stackTop].codePage;
984 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
991 switch(info->rtfMajor)
994 ME_RTFCharAttrHook(info);
997 ME_RTFParAttrHook(info);
1000 ME_RTFTblAttrHook(info);
1002 case rtfSpecialChar:
1003 if (info->rtfMinor == rtfCell)
1005 RTFFlushOutputBuffer(info);
1006 ME_InsertTableCellFromCursor(info->editor, 0);
1014 ME_StreamInFill(ME_InStream *stream)
1016 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1017 (BYTE *)stream->buffer,
1018 sizeof(stream->buffer),
1019 (LONG *)&stream->dwSize);
1023 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
1027 int from, to, to2, nUndoMode;
1028 int nEventMask = editor->nEventMask;
1029 ME_InStream inStream;
1031 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1032 editor->nEventMask = 0;
1034 ME_GetSelection(editor, &from, &to);
1035 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1036 style = ME_GetSelectionInsertStyle(editor);
1038 ME_InternalDeleteText(editor, from, to-from);
1041 style = editor->pBuffer->pDefaultStyle;
1042 ME_AddRefStyle(style);
1043 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1044 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1046 ME_ClearTempStyle(editor);
1047 /* FIXME restore default paragraph formatting ! */
1051 /* Back up undo mode to a local variable */
1052 nUndoMode = editor->nUndoMode;
1054 /* Only create an undo if SFF_SELECTION is set */
1055 if (!(format & SFF_SELECTION))
1056 editor->nUndoMode = umIgnore;
1058 inStream.editstream = stream;
1059 inStream.editstream->dwError = 0;
1060 inStream.dwSize = 0;
1061 inStream.dwUsed = 0;
1063 if (format & SF_RTF)
1065 /* Check if it's really RTF, and if it is not, use plain text */
1066 ME_StreamInFill(&inStream);
1067 if (!inStream.editstream->dwError)
1069 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1077 if (!inStream.editstream->dwError)
1079 if (format & SF_RTF) {
1080 /* setup the RTF parser */
1081 memset(&parser, 0, sizeof parser);
1082 RTFSetEditStream(&parser, &inStream);
1083 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1084 parser.hwndEdit = editor->hWnd;
1085 parser.editor = editor;
1086 parser.style = style;
1087 WriterInit(&parser);
1089 RTFSetReadHook(&parser, ME_RTFReadHook);
1090 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1091 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1094 /* do the parsing */
1096 RTFFlushOutputBuffer(&parser);
1097 RTFDestroy(&parser);
1098 if (parser.lpRichEditOle)
1099 IRichEditOle_Release(parser.lpRichEditOle);
1101 style = parser.style;
1103 else if (format & SF_TEXT)
1104 ME_StreamInText(editor, format, &inStream, style);
1106 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1107 ME_GetSelection(editor, &to, &to2);
1108 /* put the cursor at the top */
1109 if (!(format & SFF_SELECTION))
1110 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1113 /* Restore saved undo mode */
1114 editor->nUndoMode = nUndoMode;
1116 /* even if we didn't add an undo, we need to commit anything on the stack */
1117 ME_CommitUndo(editor);
1119 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1120 if (!(format & SFF_SELECTION))
1121 ME_EmptyUndoStack(editor);
1123 ME_ReleaseStyle(style);
1124 editor->nEventMask = nEventMask;
1125 if (editor->bRedraw)
1127 ME_UpdateRepaint(editor);
1129 if (!(format & SFF_SELECTION)) {
1130 ME_ClearTempStyle(editor);
1132 ME_MoveCaret(editor);
1133 ME_SendSelChange(editor);
1134 ME_SendRequestResize(editor, FALSE);
1140 typedef struct tagME_RTFStringStreamStruct
1145 } ME_RTFStringStreamStruct;
1147 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1149 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1152 count = min(cb, pStruct->length - pStruct->pos);
1153 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1154 pStruct->pos += count;
1160 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1163 ME_RTFStringStreamStruct data;
1165 data.string = string;
1166 data.length = strlen(string);
1168 es.dwCookie = (DWORD)&data;
1169 es.pfnCallback = ME_ReadFromRTFString;
1170 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
1175 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1177 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1179 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1180 item = ME_FindItemFwd(item, diParagraph);
1185 nOffset -= item->member.para.nCharOfs;
1186 if (nItemType == diParagraph) {
1188 *nItemOffset = nOffset;
1193 item = ME_FindItemFwd(item, diRun);
1194 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
1196 nOffset -= item->member.run.nCharOfs;
1198 *nItemOffset = nOffset;
1205 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1207 const int nLen = lstrlenW(text);
1208 const int nTextLen = ME_GetTextLength(editor);
1211 ME_DisplayItem *item;
1212 ME_DisplayItem *para;
1213 WCHAR wLastChar = ' ';
1215 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1216 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1218 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1219 FIXME("Flags 0x%08x not implemented\n",
1220 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1223 if (chrg->cpMax == -1)
1226 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1228 /* when searching up, if cpMin < cpMax, then instead of searching
1229 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1230 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1231 * case, it is always bigger than cpMin.
1233 if (!(flags & FR_DOWN))
1237 nMax = nMin > nTextLen ? nTextLen : nMin;
1238 if (nMin < nSwap || chrg->cpMax == -1)
1244 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1247 chrgText->cpMin = chrgText->cpMax = -1;
1251 if (flags & FR_DOWN) /* Forward search */
1253 /* If possible, find the character before where the search starts */
1254 if ((flags & FR_WHOLEWORD) && nMin)
1257 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1261 chrgText->cpMin = chrgText->cpMax = -1;
1264 wLastChar = item->member.run.strText->szData[nStart];
1268 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1272 chrgText->cpMin = chrgText->cpMax = -1;
1276 para = ME_GetParagraph(item);
1278 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1280 ME_DisplayItem *pCurItem = item;
1281 int nCurStart = nStart;
1284 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1286 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1290 if (nMatched == nLen)
1292 ME_DisplayItem *pNextItem = pCurItem;
1293 int nNextStart = nCurStart;
1296 /* Check to see if next character is a whitespace */
1297 if (flags & FR_WHOLEWORD)
1299 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1301 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1302 nNextStart = -nMatched;
1306 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1310 if (isalnumW(wNextChar))
1314 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
1317 chrgText->cpMin = nStart;
1318 chrgText->cpMax = nStart + nLen;
1320 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1323 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1325 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1326 para = ME_GetParagraph(pCurItem);
1327 nCurStart = -nMatched;
1331 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1336 if (nStart == ME_StrLen(item->member.run.strText))
1338 item = ME_FindItemFwd(item, diRun);
1339 para = ME_GetParagraph(item);
1344 else /* Backward search */
1346 /* If possible, find the character after where the search ends */
1347 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1350 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1354 chrgText->cpMin = chrgText->cpMax = -1;
1357 wLastChar = item->member.run.strText->szData[nEnd];
1361 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1365 chrgText->cpMin = chrgText->cpMax = -1;
1369 para = ME_GetParagraph(item);
1372 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1374 ME_DisplayItem *pCurItem = item;
1378 if (nCurEnd - nMatched == 0)
1380 pCurItem = ME_FindItemBack(pCurItem, diRun);
1381 para = ME_GetParagraph(pCurItem);
1382 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1385 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1387 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1391 if (nMatched == nLen)
1393 ME_DisplayItem *pPrevItem = pCurItem;
1394 int nPrevEnd = nCurEnd;
1397 /* Check to see if previous character is a whitespace */
1398 if (flags & FR_WHOLEWORD)
1400 if (nPrevEnd - nMatched == 0)
1402 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1404 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1408 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1412 if (isalnumW(wPrevChar))
1416 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1419 chrgText->cpMin = nStart;
1420 chrgText->cpMax = nStart + nLen;
1422 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1425 if (nCurEnd - nMatched == 0)
1427 pCurItem = ME_FindItemBack(pCurItem, diRun);
1428 /* Don't care about pCurItem becoming NULL here; it's already taken
1429 * care of in the exterior loop condition */
1430 para = ME_GetParagraph(pCurItem);
1431 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1435 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1442 item = ME_FindItemBack(item, diRun);
1443 para = ME_GetParagraph(item);
1444 nEnd = ME_StrLen(item->member.run.strText);
1448 TRACE("not found\n");
1450 chrgText->cpMin = chrgText->cpMax = -1;
1454 /* helper to send a msg filter notification */
1456 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1460 msgf.nmhdr.hwndFrom = editor->hWnd;
1461 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1462 msgf.nmhdr.code = EN_MSGFILTER;
1465 msgf.wParam = *wParam;
1466 msgf.lParam = *lParam;
1467 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1469 *wParam = msgf.wParam;
1470 *lParam = msgf.lParam;
1471 msgf.wParam = *wParam;
1477 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1479 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1480 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1492 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1496 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1497 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1499 if (ME_IsSelection(editor))
1500 ME_DeleteSelection(editor);
1501 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1502 ME_DeleteTextAtCursor(editor, 1, 1);
1505 ME_CommitUndo(editor);
1506 ME_UpdateRepaint(editor);
1507 ME_SendRequestResize(editor, FALSE);
1517 chf.cbSize = sizeof(chf);
1519 ME_GetSelectionCharFormat(editor, &chf);
1520 ME_DumpStyleToBuf(&chf, buf);
1521 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1525 ME_CheckCharOffsets(editor);
1532 static BOOL ME_SetCursor(ME_TextEditor *editor, int x)
1534 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_SELECTIONBAR) &&
1535 (x < editor->selofs || editor->linesel))
1543 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1548 if(!editor->lpOleCallback)
1550 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
1551 if(selrange.cpMin == selrange.cpMax)
1552 seltype |= SEL_EMPTY;
1555 /* FIXME: Handle objects */
1556 seltype |= SEL_TEXT;
1557 if(selrange.cpMax-selrange.cpMin > 1)
1558 seltype |= SEL_MULTICHAR;
1560 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1562 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1568 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1569 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1572 ed->bEmulateVersion10 = FALSE;
1573 ed->pBuffer = ME_MakeText();
1574 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1575 ME_MakeFirstParagraph(ed);
1576 ed->bCaretShown = FALSE;
1578 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1579 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1580 ed->pCursors[0].nOffset = 0;
1581 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1582 ed->pCursors[1].nOffset = 0;
1583 ed->pCursors[2] = ed->pCursors[0];
1584 ed->pCursors[3] = ed->pCursors[1];
1585 ed->nLastTotalLength = ed->nTotalLength = 0;
1589 ed->rgbBackColor = -1;
1590 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1591 ed->bCaretAtEnd = FALSE;
1593 ed->nModifyStep = 0;
1594 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1595 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1596 ed->nUndoStackSize = 0;
1597 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1598 ed->nUndoMode = umAddToUndo;
1599 ed->nParagraphs = 1;
1600 ed->nLastSelStart = ed->nLastSelEnd = 0;
1601 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1603 ed->bWordWrap = FALSE;
1604 ed->bHideSelection = FALSE;
1605 ed->nInvalidOfs = -1;
1606 ed->pfnWordBreak = NULL;
1607 ed->lpOleCallback = NULL;
1608 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1609 ed->AutoURLDetect_bEnable = FALSE;
1610 ed->bHaveFocus = FALSE;
1611 GetClientRect(hWnd, &ed->rcFormat);
1612 for (i=0; i<HFONT_CACHE_SIZE; i++)
1614 ed->pFontCache[i].nRefs = 0;
1615 ed->pFontCache[i].nAge = 0;
1616 ed->pFontCache[i].hFont = NULL;
1619 ME_CheckCharOffsets(ed);
1620 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1626 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1627 ed->cPasswordMask = '*';
1629 ed->cPasswordMask = 0;
1634 typedef struct tagME_GlobalDestStruct
1638 } ME_GlobalDestStruct;
1640 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1642 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1647 pDest = (WORD *)lpBuff;
1648 pSrc = (WORD *)GlobalLock(pData->hData);
1649 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1650 pDest[i] = pSrc[pData->nLength+i];
1652 pData->nLength += i;
1654 GlobalUnlock(pData->hData);
1658 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1660 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1665 pSrc = (BYTE *)GlobalLock(pData->hData);
1666 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1667 pDest[i] = pSrc[pData->nLength+i];
1669 pData->nLength += i;
1671 GlobalUnlock(pData->hData);
1676 void ME_DestroyEditor(ME_TextEditor *editor)
1678 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1679 ME_DisplayItem *p = pFirst, *pNext = NULL;
1682 ME_ClearTempStyle(editor);
1683 ME_EmptyUndoStack(editor);
1686 ME_DestroyDisplayItem(p);
1689 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1690 for (i=0; i<HFONT_CACHE_SIZE; i++)
1692 if (editor->pFontCache[i].hFont)
1693 DeleteObject(editor->pFontCache[i].hFont);
1695 if (editor->rgbBackColor != -1)
1696 DeleteObject(editor->hbrBackground);
1697 if(editor->lpOleCallback)
1698 IUnknown_Release(editor->lpOleCallback);
1700 FREE_OBJ(editor->pBuffer);
1701 FREE_OBJ(editor->pCursors);
1706 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1711 case DLL_PROCESS_ATTACH:
1712 DisableThreadLibraryCalls(hinstDLL);
1713 me_heap = HeapCreate (0, 0x10000, 0);
1714 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1715 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1719 case DLL_PROCESS_DETACH:
1720 UnregisterClassW(RichEdit20W, 0);
1721 UnregisterClassW(RichEdit50W, 0);
1722 UnregisterClassA("RichEdit20A", 0);
1723 UnregisterClassA("RichEdit50A", 0);
1724 if (ME_ListBoxRegistered)
1725 UnregisterClassW(REListBox20W, 0);
1726 if (ME_ComboBoxRegistered)
1727 UnregisterClassW(REComboBox20W, 0);
1729 HeapDestroy (me_heap);
1737 #define UNSUPPORTED_MSG(e) \
1739 FIXME(#e ": stub\n"); \
1740 return DefWindowProcW(hWnd, msg, wParam, lParam);
1742 static const char * const edit_messages[] = {
1771 "EM_SETPASSWORDCHAR",
1772 "EM_EMPTYUNDOBUFFER",
1773 "EM_GETFIRSTVISIBLELINE",
1775 "EM_SETWORDBREAKPROC",
1776 "EM_GETWORDBREAKPROC",
1777 "EM_GETPASSWORDCHAR",
1785 static const char * const richedit_messages[] = {
1790 "EM_EXLINEFROMCHAR",
1796 "EM_GETOLEINTERFACE",
1806 "EM_SETOLECALLBACK",
1808 "EM_SETTARGETDEVICE",
1816 "EM_GETWORDBREAKPROCEX",
1817 "EM_SETWORDBREAKPROCEX",
1819 "EM_UNKNOWN_USER_83",
1824 "EM_STOPGROUPTYPING",
1828 "EM_GETAUTOURLDETECT",
1831 "EM_GETTEXTLENGTHEX",
1834 "EM_UNKNOWN_USER_98",
1835 "EM_UNKNOWN_USER_99",
1836 "EM_SETPUNCTUATION",
1837 "EM_GETPUNCTUATION",
1838 "EM_SETWORDWRAPMODE",
1839 "EM_GETWORDWRAPMODE",
1845 "EM_UNKNOWN_USER_109",
1846 "EM_UNKNOWN_USER_110",
1847 "EM_UNKNOWN_USER_111",
1848 "EM_UNKNOWN_USER_112",
1849 "EM_UNKNOWN_USER_113",
1850 "EM_UNKNOWN_USER_114",
1851 "EM_UNKNOWN_USER_115",
1852 "EM_UNKNOWN_USER_116",
1853 "EM_UNKNOWN_USER_117",
1854 "EM_UNKNOWN_USER_118",
1855 "EM_UNKNOWN_USER_119",
1856 "EM_SETLANGOPTIONS",
1857 "EM_GETLANGOPTIONS",
1858 "EM_GETIMECOMPMODE",
1862 "EM_SETIMEMODEBIAS",
1867 get_msg_name(UINT msg)
1869 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1870 return edit_messages[msg - EM_GETSEL];
1871 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1872 return richedit_messages[msg - EM_CANPASTE];
1876 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1877 LPARAM lParam, BOOL unicode)
1879 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1881 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1882 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1884 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1885 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1891 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1892 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1893 UNSUPPORTED_MSG(EM_FMTLINES)
1894 UNSUPPORTED_MSG(EM_FORMATRANGE)
1895 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1896 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1897 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1898 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1899 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1900 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1901 UNSUPPORTED_MSG(EM_GETREDONAME)
1902 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1903 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1904 UNSUPPORTED_MSG(EM_GETUNDONAME)
1905 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1906 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1907 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1908 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1909 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1910 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1911 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1912 UNSUPPORTED_MSG(EM_SETPALETTE)
1913 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1914 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1915 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1916 UNSUPPORTED_MSG(WM_STYLECHANGING)
1917 UNSUPPORTED_MSG(WM_STYLECHANGED)
1918 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1920 /* Messages specific to Richedit controls */
1923 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1925 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1928 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1929 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1931 int vk = (int)((LPMSG)lParam)->wParam;
1932 /* if style says we want return key */
1933 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1935 code |= DLGC_WANTMESSAGE;
1937 /* we always handle ctrl-tab */
1938 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1940 code |= DLGC_WANTMESSAGE;
1947 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1948 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1949 editor = ME_MakeEditor(hWnd);
1950 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1953 case EM_EMPTYUNDOBUFFER:
1954 ME_EmptyUndoStack(editor);
1958 /* Note: wParam/lParam can be NULL */
1960 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1961 PUINT pto = lParam ? (PUINT)lParam : &to;
1962 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1963 if ((*pfrom|*pto) & 0xFFFF0000)
1965 return MAKELONG(*pfrom,*pto);
1969 CHARRANGE *pRange = (CHARRANGE *)lParam;
1970 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
1971 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1974 case EM_SETUNDOLIMIT:
1976 if ((int)wParam < 0)
1977 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1979 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1980 /* Setting a max stack size keeps wine from getting killed
1981 for hogging memory. Windows allocates all this memory at once, so
1982 no program would realistically set a value above our maximum. */
1983 return editor->nUndoLimit;
1986 return editor->pUndoStack != NULL;
1988 return editor->pRedoStack != NULL;
1989 case WM_UNDO: /* FIXME: actually not the same */
1998 /* these flags are equivalent to the ES_* counterparts */
1999 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2000 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
2001 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
2007 /* these flags are equivalent to ES_* counterparts
2008 * ECO_READONLY is already implemented in the code, only requires
2009 * setting the bit to work
2011 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2012 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
2013 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
2014 DWORD settings = mask & raw;
2030 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
2032 if (settings & ECO_AUTOWORDSELECTION)
2033 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
2034 if (settings & ECO_SELECTIONBAR)
2035 editor->selofs = 16;
2038 ME_WrapMarkedParagraphs(editor);
2040 if (settings & ECO_VERTICAL)
2041 FIXME("ECO_VERTICAL not implemented yet!\n");
2042 if (settings & ECO_AUTOHSCROLL)
2043 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
2044 if (settings & ECO_AUTOVSCROLL)
2045 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
2046 if (settings & ECO_NOHIDESEL)
2047 FIXME("ECO_NOHIDESEL not implemented yet!\n");
2048 if (settings & ECO_WANTRETURN)
2049 FIXME("ECO_WANTRETURN not implemented yet!\n");
2055 ME_InvalidateSelection(editor);
2056 ME_SetSelection(editor, wParam, lParam);
2057 ME_InvalidateSelection(editor);
2058 ME_SendSelChange(editor);
2061 case EM_SETSCROLLPOS:
2063 POINT *point = (POINT *)lParam;
2064 ME_ScrollAbs(editor, point->y);
2067 case EM_AUTOURLDETECT:
2069 if (wParam==1 || wParam ==0)
2071 editor->AutoURLDetect_bEnable = (BOOL)wParam;
2074 return E_INVALIDARG;
2076 case EM_GETAUTOURLDETECT:
2078 return editor->AutoURLDetect_bEnable;
2083 CHARRANGE range = *(CHARRANGE *)lParam;
2085 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
2087 ME_InvalidateSelection(editor);
2088 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
2089 ME_InvalidateSelection(editor);
2090 ME_SendSelChange(editor);
2094 case EM_SHOWSCROLLBAR:
2096 ShowScrollBar(editor->hWnd, wParam, lParam);
2102 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
2106 int oldModify = editor->nModifyStep;
2108 if (!pStruct) return 0;
2110 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2111 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
2112 pStruct->flags, pStruct->codepage);
2114 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2115 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
2116 len = wszText ? lstrlenW(wszText) : 0;
2118 /* FIXME: this should support RTF strings too, according to MSDN */
2119 if (pStruct->flags & ST_SELECTION) {
2120 ME_GetSelection(editor, &from, &to);
2121 style = ME_GetSelectionInsertStyle(editor);
2122 ME_InternalDeleteText(editor, from, to - from);
2123 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2124 ME_ReleaseStyle(style);
2127 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2128 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2131 ME_CommitUndo(editor);
2132 if (!(pStruct->flags & ST_KEEPUNDO))
2134 editor->nModifyStep = oldModify;
2135 ME_EmptyUndoStack(editor);
2137 ME_UpdateRepaint(editor);
2140 case EM_SETBKGNDCOLOR:
2143 if (editor->rgbBackColor != -1) {
2144 DeleteObject(editor->hbrBackground);
2145 lColor = editor->rgbBackColor;
2147 else lColor = GetSysColor(COLOR_WINDOW);
2151 editor->rgbBackColor = -1;
2152 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2156 editor->rgbBackColor = lParam;
2157 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
2159 if (editor->bRedraw)
2161 InvalidateRect(hWnd, NULL, TRUE);
2167 return editor->nModifyStep == 0 ? 0 : 1;
2171 editor->nModifyStep = 1;
2173 editor->nModifyStep = 0;
2177 case EM_SETREADONLY:
2179 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
2181 nStyle |= ES_READONLY;
2183 nStyle &= ~ES_READONLY;
2184 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
2187 case EM_SETEVENTMASK:
2189 DWORD nOldMask = editor->nEventMask;
2191 editor->nEventMask = lParam;
2194 case EM_GETEVENTMASK:
2195 return editor->nEventMask;
2196 case EM_SETCHARFORMAT:
2198 CHARFORMAT2W buf, *p;
2199 BOOL bRepaint = TRUE;
2200 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
2201 if (p == NULL) return 0;
2203 ME_SetDefaultCharFormat(editor, p);
2204 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
2205 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2207 } else if (wParam == SCF_ALL) {
2208 if (editor->mode & TM_PLAINTEXT)
2209 ME_SetDefaultCharFormat(editor, p);
2211 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
2212 } else if (editor->mode & TM_PLAINTEXT) {
2216 ME_GetSelection(editor, &from, &to);
2217 bRepaint = (from != to);
2218 ME_SetSelectionCharFormat(editor, p);
2220 editor->nModifyStep = 1;
2221 ME_CommitUndo(editor);
2223 ME_RewrapRepaint(editor);
2226 case EM_GETCHARFORMAT:
2228 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
2229 if (dst->cbSize != sizeof(CHARFORMATA) &&
2230 dst->cbSize != sizeof(CHARFORMATW) &&
2231 dst->cbSize != sizeof(CHARFORMAT2A) &&
2232 dst->cbSize != sizeof(CHARFORMAT2W))
2234 tmp.cbSize = sizeof(tmp);
2236 ME_GetDefaultCharFormat(editor, &tmp);
2238 ME_GetSelectionCharFormat(editor, &tmp);
2239 ME_CopyToCFAny(dst, &tmp);
2242 case EM_SETPARAFORMAT:
2243 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2244 ME_RewrapRepaint(editor);
2245 ME_CommitUndo(editor);
2247 case EM_GETPARAFORMAT:
2248 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2250 case EM_GETFIRSTVISIBLELINE:
2252 ME_DisplayItem *p = editor->pBuffer->pFirst;
2253 int y = ME_GetYScrollPos(editor);
2258 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
2259 if (p->type == diTextEnd)
2261 if (p->type == diParagraph) {
2262 ypara = p->member.para.nYPos;
2265 ystart = ypara + p->member.row.nYPos;
2266 yend = ystart + p->member.row.nHeight;
2274 case EM_HIDESELECTION:
2276 editor->bHideSelection = (wParam != 0);
2277 ME_InvalidateSelection(editor);
2282 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
2283 return TRUE; /* Should return false if a single line richedit control */
2288 ME_GetSelection(editor, &from, &to);
2289 ME_InternalDeleteText(editor, from, to-from);
2290 ME_CommitUndo(editor);
2291 ME_UpdateRepaint(editor);
2298 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
2299 size_t len = wszText ? lstrlenW(wszText) : 0;
2300 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
2302 ME_GetSelection(editor, &from, &to);
2303 style = ME_GetSelectionInsertStyle(editor);
2304 ME_InternalDeleteText(editor, from, to-from);
2305 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2306 ME_ReleaseStyle(style);
2307 /* drop temporary style if line end */
2309 * FIXME question: does abc\n mean: put abc,
2310 * clear temp style, put \n? (would require a change)
2312 if (len>0 && wszText[len-1] == '\n')
2313 ME_ClearTempStyle(editor);
2314 ME_EndToUnicode(unicode, wszText);
2315 ME_CommitUndo(editor);
2317 ME_EmptyUndoStack(editor);
2318 ME_UpdateRepaint(editor);
2321 case EM_SCROLLCARET:
2323 int top, bottom; /* row's edges relative to document top */
2325 ME_DisplayItem *para, *row;
2327 nPos = ME_GetYScrollPos(editor);
2328 row = ME_RowStart(editor->pCursors[0].pRun);
2329 para = ME_GetParagraph(row);
2330 top = para->member.para.nYPos + row->member.row.nYPos;
2331 bottom = top + row->member.row.nHeight;
2333 if (top < nPos) /* caret above window */
2334 ME_ScrollAbs(editor, top);
2335 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
2336 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
2344 BOOL bRepaint = LOWORD(lParam);
2347 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
2348 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
2350 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
2351 ReleaseDC(hWnd, hDC);
2352 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
2353 ME_SetDefaultCharFormat(editor, &fmt);
2355 ME_CommitUndo(editor);
2357 ME_RewrapRepaint(editor);
2362 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2365 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
2366 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
2368 /* Undocumented: WM_SETTEXT supports RTF text */
2369 ME_StreamInRTFString(editor, 0, (char *)lParam);
2373 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
2374 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
2375 if (lstrlenW(wszText) > 0)
2379 /* uses default style! */
2380 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2385 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
2388 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2390 ME_EndToUnicode(unicode, wszText);
2394 TRACE("WM_SETTEXT - NULL\n");
2395 ME_CommitUndo(editor);
2396 ME_EmptyUndoStack(editor);
2397 ME_SetSelection(editor, 0, 0);
2398 editor->nModifyStep = 0;
2399 ME_UpdateRepaint(editor);
2404 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2405 if (IsClipboardFormatAvailable(nRTFFormat))
2407 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2415 ME_GlobalDestStruct gds;
2416 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2419 if (IsClipboardFormatAvailable(nRTFFormat))
2420 cf = nRTFFormat, dwFormat = SF_RTF;
2421 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2422 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2426 if (!OpenClipboard(hWnd))
2428 gds.hData = GetClipboardData(cf);
2430 es.dwCookie = (DWORD)&gds;
2431 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2432 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
2440 LPDATAOBJECT dataObj = NULL;
2444 if (editor->cPasswordMask)
2445 return 0; /* Copying or Cutting masked text isn't allowed */
2447 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2448 if(editor->lpOleCallback)
2449 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2450 if(FAILED(hr) || !dataObj)
2451 hr = ME_GetDataObject(editor, &range, &dataObj);
2453 hr = OleSetClipboard(dataObj);
2454 IDataObject_Release(dataObj);
2456 if (SUCCEEDED(hr) && msg == WM_CUT)
2458 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2459 ME_CommitUndo(editor);
2460 ME_UpdateRepaint(editor);
2464 case WM_GETTEXTLENGTH:
2466 GETTEXTLENGTHEX how;
2468 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2469 how.codepage = unicode ? 1200 : CP_ACP;
2470 return ME_GetTextLengthEx(editor, &how);
2472 case EM_GETTEXTLENGTHEX:
2473 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2478 LPSTR bufferA = NULL;
2479 LPWSTR bufferW = NULL;
2482 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2484 bufferA = heap_alloc(wParam + 2);
2486 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
2487 ex.flags = GT_USECRLF;
2488 ex.codepage = unicode ? 1200 : CP_ACP;
2489 ex.lpDefaultChar = NULL;
2490 ex.lpUsedDefChar = NULL;
2491 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2495 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
2496 if (lstrlenW(bufferW) >= wParam) rc = 0;
2500 memcpy((LPSTR)lParam, bufferA, wParam);
2501 if (strlen(bufferA) >= wParam) rc = 0;
2509 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2510 int nStart, nCount; /* in chars */
2512 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2513 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2515 if (ex->flags & GT_SELECTION)
2517 ME_GetSelection(editor, &nStart, &nCount);
2523 nCount = 0x7fffffff;
2525 if (ex->codepage == 1200)
2527 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2528 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2532 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2533 we can just take a bigger buffer? :) */
2534 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2536 DWORD buflen = ex->cb;
2540 nCount = min(nCount, ex->cb - 1);
2541 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2543 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2544 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2545 if (rc) rc--; /* do not count 0 terminator */
2554 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2555 ME_GetSelection(editor, &from, &to);
2556 tr.chrg.cpMin = from;
2558 tr.lpstrText = (WCHAR *)lParam;
2559 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2561 case EM_GETSCROLLPOS:
2563 POINT *point = (POINT *)lParam;
2564 point->x = 0; /* FIXME side scrolling not implemented */
2565 point->y = ME_GetYScrollPos(editor);
2568 case EM_GETTEXTRANGE:
2570 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2571 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2572 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2573 editor->bEmulateVersion10, ME_GetTextLength(editor));
2575 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2578 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2579 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2580 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2581 /* FIXME this is a potential security hole (buffer overrun)
2582 if you know more about wchar->mbyte conversion please explain
2584 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2591 ME_DisplayItem *run;
2592 const unsigned int nMaxChars = *(WORD *) lParam;
2593 unsigned int nEndChars, nCharsLeft = nMaxChars;
2594 char *dest = (char *) lParam;
2595 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2596 we need to know how if we have the extra \n or not */
2597 int nLF = editor->bEmulateVersion10;
2599 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2600 unicode ? "Unicode" : "Ansi");
2602 run = ME_FindRowWithNumber(editor, wParam);
2606 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2607 && !(run->member.run.nFlags & MERF_ENDPARA))
2611 if (run->type != diRun)
2613 strText = run->member.run.strText;
2614 nCopy = min(nCharsLeft, strText->nLen);
2617 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2619 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2620 nCharsLeft, NULL, NULL);
2621 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2622 nCharsLeft -= nCopy;
2625 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2626 nEndChars = min(nCharsLeft, 2 + nLF);
2627 nCharsLeft -= nEndChars;
2630 const WCHAR src[] = {'\r', '\0'};
2631 const WCHAR src10[] = {'\r', '\n', '\0'};
2632 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2635 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2637 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2639 if (nEndChars == 2 + nLF)
2640 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2642 return nMaxChars - nCharsLeft;
2644 case EM_GETLINECOUNT:
2646 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2649 while (item != editor->pBuffer->pLast)
2651 assert(item->type == diParagraph);
2652 nRows += item->member.para.nRows;
2653 item = item->member.para.next_para;
2655 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2656 return max(1, nRows);
2658 case EM_LINEFROMCHAR:
2661 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2663 return ME_RowNumberFromCharOfs(editor, wParam);
2665 case EM_EXLINEFROMCHAR:
2668 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2670 return ME_RowNumberFromCharOfs(editor, lParam);
2674 ME_DisplayItem *item, *para;
2678 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2680 item = ME_FindRowWithNumber(editor, wParam);
2683 para = ME_GetParagraph(item);
2684 item = ME_FindItemFwd(item, diRun);
2685 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2686 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2691 ME_DisplayItem *item, *item_end;
2692 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2694 if (wParam > ME_GetTextLength(editor))
2698 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2701 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2702 item = ME_RowStart(item);
2703 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2704 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2705 if (item_end->type == diStartRow)
2706 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2708 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2709 - (editor->bEmulateVersion10?2:1);
2710 nChars = nNextLineOfs - nThisLineOfs;
2711 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2714 case EM_EXLIMITTEXT:
2716 if ((int)lParam < 0)
2719 editor->nTextLimit = 65536;
2721 editor->nTextLimit = (int) lParam;
2727 editor->nTextLimit = 65536;
2729 editor->nTextLimit = (int) wParam;
2732 case EM_GETLIMITTEXT:
2734 return editor->nTextLimit;
2738 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2739 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2743 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2744 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2745 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2751 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2752 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2756 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2757 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2758 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2764 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2765 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2767 case EM_FINDTEXTEXW:
2769 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2770 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2773 if (!wParam || !lParam)
2775 *(int *)wParam = editor->nZoomNumerator;
2776 *(int *)lParam = editor->nZoomDenominator;
2779 return ME_SetZoom(editor, wParam, lParam);
2780 case EM_CHARFROMPOS:
2781 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2782 case EM_POSFROMCHAR:
2784 ME_DisplayItem *pRun;
2785 int nCharOfs, nOffset, nLength;
2789 /* detect which API version we're dealing with */
2790 if (wParam >= 0x40000)
2792 nLength = ME_GetTextLength(editor);
2794 if (nCharOfs < nLength) {
2795 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2796 assert(pRun->type == diRun);
2797 pt.y = pRun->member.run.pt.y;
2798 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2799 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2802 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2804 pt.x += editor->selofs;
2805 if (wParam >= 0x40000) {
2806 *(POINTL *)wParam = pt;
2808 return MAKELONG( pt.x, pt.y );
2811 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2812 { /* Squelch the default horizontal scrollbar it would make */
2813 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2815 ME_CommitUndo(editor);
2816 ME_WrapMarkedParagraphs(editor);
2817 ME_MoveCaret(editor);
2820 ME_DestroyEditor(editor);
2821 SetWindowLongPtrW(hWnd, 0, 0);
2823 case WM_LBUTTONDOWN:
2824 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2825 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2828 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2830 ME_LinkNotify(editor,msg,wParam,lParam);
2831 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2834 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2835 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2837 if (GetCapture() == hWnd)
2838 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2839 ME_LinkNotify(editor,msg,wParam,lParam);
2840 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2843 if (GetCapture() == hWnd)
2845 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2846 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2851 editor->linesel = 0;
2852 ret = ME_SetCursor(editor, LOWORD(lParam));
2853 ME_LinkNotify(editor,msg,wParam,lParam);
2854 if (!ret) goto do_default;
2857 case WM_LBUTTONDBLCLK:
2858 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2859 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2861 ME_LinkNotify(editor,msg,wParam,lParam);
2862 ME_SelectWord(editor);
2865 case WM_RBUTTONDOWN:
2866 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2867 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2870 case WM_CONTEXTMENU:
2871 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2875 if (editor->bRedraw)
2880 hDC = BeginPaint(hWnd, &ps);
2881 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2882 EndPaint(hWnd, &ps);
2886 editor->bHaveFocus = TRUE;
2887 ME_ShowCaret(editor);
2888 ME_SendOldNotify(editor, EN_SETFOCUS);
2891 ME_HideCaret(editor);
2892 editor->bHaveFocus = FALSE;
2893 ME_SendOldNotify(editor, EN_KILLFOCUS);
2897 if (editor->bRedraw)
2899 HDC hDC = (HDC)wParam;
2901 if (GetUpdateRect(hWnd,&rc,TRUE))
2903 FillRect(hDC, &rc, editor->hbrBackground);
2909 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2912 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2913 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2917 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2918 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2920 if (ME_KeyDown(editor, LOWORD(wParam)))
2928 wstr = (WCHAR)wParam;
2931 CHAR charA = wParam;
2932 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2934 if (editor->AutoURLDetect_bEnable)
2935 ME_AutoURLDetect(editor, wstr);
2939 case 1: /* Ctrl-A */
2940 ME_SetSelection(editor, 0, -1);
2942 case 3: /* Ctrl-C */
2943 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2947 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2948 MessageBeep(MB_ICONERROR);
2949 return 0; /* FIXME really 0 ? */
2954 case 22: /* Ctrl-V */
2955 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2957 case 24: /* Ctrl-X */
2958 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2960 case 25: /* Ctrl-Y */
2961 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2963 case 26: /* Ctrl-Z */
2964 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2967 if (((unsigned)wstr)>=' '
2968 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2970 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2971 /* WM_CHAR is restricted to nTextLimit */
2973 ME_GetSelection(editor, &from, &to);
2974 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2976 ME_Style *style = ME_GetInsertStyle(editor, 0);
2977 ME_SaveTempStyle(editor);
2978 if (wstr == '\r' && (GetKeyState(VK_SHIFT) & 0x8000))
2979 ME_InsertEndRowFromCursor(editor, 0);
2981 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2982 ME_ReleaseStyle(style);
2983 ME_CommitUndo(editor);
2985 ME_UpdateRepaint(editor);
2989 case EM_SCROLL: /* fall through */
2995 origNPos = ME_GetYScrollPos(editor);
2998 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2999 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3000 if (lineHeight <= 0) lineHeight = 24;
3002 switch(LOWORD(wParam))
3005 ME_ScrollUp(editor,lineHeight);
3008 ME_ScrollDown(editor,lineHeight);
3011 ME_ScrollUp(editor,editor->sizeWindow.cy);
3014 ME_ScrollDown(editor,editor->sizeWindow.cy);
3017 case SB_THUMBPOSITION:
3018 ME_ScrollAbs(editor,HIWORD(wParam));
3021 if (msg == EM_SCROLL)
3022 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
3028 UINT pulScrollLines;
3030 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3031 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3034 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
3035 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
3037 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
3039 /* FIXME follow the original */
3040 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
3046 *((RECT *)lParam) = editor->rcFormat;
3054 RECT *rc = (RECT *)lParam;
3058 editor->rcFormat.left += rc->left;
3059 editor->rcFormat.top += rc->top;
3060 editor->rcFormat.right += rc->right;
3061 editor->rcFormat.bottom += rc->bottom;
3065 editor->rcFormat = *rc;
3070 GetClientRect(hWnd, &editor->rcFormat);
3072 if (msg != EM_SETRECTNP)
3073 ME_RewrapRepaint(editor);
3076 case EM_REQUESTRESIZE:
3077 ME_SendRequestResize(editor, TRUE);
3080 if ((editor->bRedraw = wParam))
3081 ME_RewrapRepaint(editor);
3085 GetClientRect(hWnd, &editor->rcFormat);
3086 ME_RewrapRepaint(editor);
3087 return DefWindowProcW(hWnd, msg, wParam, lParam);
3089 /* IME messages to make richedit controls IME aware */
3090 case WM_IME_SETCONTEXT:
3091 case WM_IME_CONTROL:
3093 case WM_IME_COMPOSITIONFULL:
3095 case WM_IME_STARTCOMPOSITION:
3097 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
3098 ME_DeleteSelection(editor);
3099 ME_CommitUndo(editor);
3100 ME_UpdateRepaint(editor);
3103 case WM_IME_COMPOSITION:
3107 ME_Style *style = ME_GetInsertStyle(editor, 0);
3108 hIMC = ImmGetContext(hWnd);
3109 ME_DeleteSelection(editor);
3110 ME_CommitUndo(editor);
3111 ME_SaveTempStyle(editor);
3112 if (lParam & GCS_RESULTSTR)
3114 LPWSTR lpCompStr = NULL;
3117 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3118 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3119 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
3120 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3121 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3123 else if (lParam & GCS_COMPSTR)
3125 LPWSTR lpCompStr = NULL;
3128 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
3129 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3130 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
3131 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3133 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3134 ME_SetSelection(editor,editor->imeStartIndex,
3135 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
3137 ME_ReleaseStyle(style);
3138 ME_UpdateRepaint(editor);
3141 case WM_IME_ENDCOMPOSITION:
3143 ME_DeleteSelection(editor);
3144 editor->imeStartIndex=-1;
3147 case EM_GETOLEINTERFACE:
3149 LPVOID *ppvObj = (LPVOID*) lParam;
3150 return CreateIRichEditOle(editor, ppvObj);
3152 case EM_GETPASSWORDCHAR:
3154 return editor->cPasswordMask;
3156 case EM_SETOLECALLBACK:
3157 if(editor->lpOleCallback)
3158 IUnknown_Release(editor->lpOleCallback);
3159 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
3160 if(editor->lpOleCallback)
3161 IUnknown_AddRef(editor->lpOleCallback);
3163 case EM_GETWORDBREAKPROC:
3164 return (LRESULT)editor->pfnWordBreak;
3165 case EM_SETWORDBREAKPROC:
3167 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
3169 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
3170 return (LRESULT)pfnOld;
3172 case EM_SETTEXTMODE:
3177 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
3180 /*Check for valid wParam*/
3181 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
3182 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
3183 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
3187 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
3189 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
3190 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
3192 /*FIXME: Currently no support for undo level and code page options*/
3193 editor->mode = (editor->mode & (~mask)) | changes;
3199 case EM_SETPASSWORDCHAR:
3201 editor->cPasswordMask = wParam;
3202 ME_RewrapRepaint(editor);
3205 case EM_SETTARGETDEVICE:
3210 case 0: editor->bWordWrap = TRUE; break;
3211 case 1: editor->bWordWrap = FALSE; break;
3212 default: FIXME("Unknown option to EM_SETTARGETDEVICE(NULL,%ld)\n", lParam);
3215 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
3219 return DefWindowProcW(hWnd, msg, wParam, lParam);
3224 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3226 BOOL unicode = TRUE;
3228 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3229 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
3232 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
3235 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3237 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
3240 /******************************************************************
3241 * RichEditANSIWndProc (RICHED20.10)
3243 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3245 return RichEditWndProcA(hWnd, msg, wParam, lParam);
3248 /******************************************************************
3249 * RichEdit10ANSIWndProc (RICHED20.9)
3251 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3255 /* FIXME: this is NOT the same as 2.0 version */
3256 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
3257 if (msg == WM_NCCREATE)
3259 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
3261 editor->bEmulateVersion10 = TRUE;
3262 editor->pBuffer->pLast->member.para.nCharOfs = 2;
3267 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
3269 HWND hWnd = editor->hWnd;
3270 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
3273 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3276 ME_Cursor tmpCursor;
3277 int nCharOfs; /* The start of the clicked text. Absolute character offset */
3282 x = (short)LOWORD(lParam);
3283 y = (short)HIWORD(lParam);
3284 nCharOfs = ME_CharFromPos(editor, x, y);
3285 if (nCharOfs < 0) return;
3287 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
3288 tmpRun = &tmpCursor.pRun->member.run;
3290 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
3291 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
3292 { /* The clicked run has CFE_LINK set */
3293 info.nmhdr.hwndFrom = editor->hWnd;
3294 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
3295 info.nmhdr.code = EN_LINK;
3297 info.wParam = wParam;
3298 info.lParam = lParam;
3299 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
3300 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
3301 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
3305 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
3307 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
3310 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
3311 item = item->member.para.next_para;
3314 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
3315 item = item->member.para.next_para;
3322 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
3324 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
3326 WCHAR *pStart = buffer;
3335 int nLen = ME_StrLen(item->member.run.strText) - nStart;
3338 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
3345 item = ME_FindItemFwd(item, diRun);
3348 while(nChars && item)
3350 int nLen = ME_StrLen(item->member.run.strText);
3354 if (item->member.run.nFlags & MERF_ENDPARA)
3356 if (!ME_FindItemFwd(item, diRun))
3357 /* No '\r' is appended to the last paragraph. */
3359 else if (bCRLF && nChars == 1) {
3370 /* our end paragraph consists of 2 characters now */
3371 if (editor->bEmulateVersion10)
3376 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
3383 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3387 item = ME_FindItemFwd(item, diRun);
3390 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3394 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
3399 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3400 wcW.lpfnWndProc = RichEditWndProcW;
3402 wcW.cbWndExtra = sizeof(ME_TextEditor *);
3403 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
3405 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3406 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3407 wcW.lpszMenuName = NULL;
3409 if (is_version_nt())
3411 wcW.lpszClassName = RichEdit20W;
3412 if (!RegisterClassW(&wcW)) return FALSE;
3413 wcW.lpszClassName = RichEdit50W;
3414 if (!RegisterClassW(&wcW)) return FALSE;
3418 /* WNDCLASSA/W have the same layout */
3419 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
3420 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3421 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
3422 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3425 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3426 wcA.lpfnWndProc = RichEditWndProcA;
3428 wcA.cbWndExtra = sizeof(ME_TextEditor *);
3429 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
3431 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3432 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3433 wcA.lpszMenuName = NULL;
3434 wcA.lpszClassName = "RichEdit20A";
3435 if (!RegisterClassA(&wcA)) return FALSE;
3436 wcA.lpszClassName = "RichEdit50A";
3437 if (!RegisterClassA(&wcA)) return FALSE;
3442 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3443 /* FIXME: Not implemented */
3444 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3445 hWnd, msg, get_msg_name(msg), wParam, lParam);
3446 return DefWindowProcW(hWnd, msg, wParam, lParam);
3449 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3450 /* FIXME: Not implemented */
3451 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3452 hWnd, msg, get_msg_name(msg), wParam, lParam);
3453 return DefWindowProcW(hWnd, msg, wParam, lParam);
3456 /******************************************************************
3457 * REExtendedRegisterClass (RICHED20.8)
3459 * FIXME undocumented
3460 * Need to check for errors and implement controls and callbacks
3462 LRESULT WINAPI REExtendedRegisterClass(void)
3467 FIXME("semi stub\n");
3471 wcW.hInstance = NULL;
3474 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
3475 wcW.lpszMenuName = NULL;
3477 if (!ME_ListBoxRegistered)
3479 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
3480 wcW.lpfnWndProc = REListWndProc;
3481 wcW.lpszClassName = REListBox20W;
3482 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
3485 if (!ME_ComboBoxRegistered)
3487 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
3488 wcW.lpfnWndProc = REComboWndProc;
3489 wcW.lpszClassName = REComboBox20W;
3490 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
3494 if (ME_ListBoxRegistered)
3496 if (ME_ComboBoxRegistered)
3502 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
3522 int curf_ef, link_ef, def_ef;
3523 int cur_prefx, prefx_cnt;
3524 int sel_min, sel_max;
3527 int URLmin, URLmax = 0;
3530 CHARFORMAT2W cur_format;
3531 CHARFORMAT2W default_format;
3533 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
3534 sel_min = ins_pt.cpMin;
3535 sel_max = ins_pt.cpMax;
3536 if (sel_min==sel_max)
3538 if (sel_min!=sel_max)
3539 car_pos = ME_GetTextLength(editor)+1;
3540 cur_format.cbSize = sizeof(cur_format);
3541 default_format.cbSize = sizeof(default_format);
3542 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
3543 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
3544 link.cbSize = sizeof(link);
3545 link.dwMask = CFM_LINK;
3546 link.dwEffects = CFE_LINK;
3547 curf_ef = cur_format.dwEffects & link.dwEffects;
3548 def_ef = default_format.dwEffects & link.dwEffects;
3549 link_ef = link.dwEffects & link.dwEffects;
3550 if (curf_ef == link_ef)
3552 if( curChar == '\n' || curChar=='\r' || curChar==' ')
3554 ME_SetSelection(editor, car_pos, car_pos);
3555 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
3560 if (curf_ef == def_ef)
3563 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
3564 while (cur_prefx<=prefx_cnt)
3568 ft.lpstrText = prefixes[cur_prefx].text;
3569 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
3570 URLmax=max(0, car_pos);
3571 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
3573 URLmax = ME_GetTextLength(editor)+1;
3575 ft.chrg.cpMin = URLmin;
3576 ft.chrg.cpMax = URLmax;
3577 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
3583 url.cpMax=car_pos-1;
3584 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
3585 ME_RewrapRepaint(editor);