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 /* for now, lookup the \result part and use it, whatever the object */
853 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
858 if (info
->rtfClass
== rtfEOF
)
860 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
862 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
865 if (info
->rtfClass
== rtfEOF
)
867 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
871 while (RTFGetToken (info
) != rtfEOF
)
873 if (info
->rtfClass
== rtfGroup
)
875 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
876 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
881 else RTFSkipGroup(info
);
884 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
886 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
890 RTFRouteToken(info
); /* feed "}" back to router */
893 static void ME_RTFReadHook(RTF_Info
*info
) {
894 switch(info
->rtfClass
)
897 switch(info
->rtfMajor
)
900 if (info
->stackTop
< maxStack
) {
901 memcpy(&info
->stack
[info
->stackTop
].fmt
, &info
->style
->fmt
, sizeof(CHARFORMAT2W
));
902 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
903 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
906 info
->styleChanged
= FALSE
;
911 RTFFlushOutputBuffer(info
);
912 if (info
->stackTop
<=1) {
913 info
->rtfClass
= rtfEOF
;
917 assert(info
->stackTop
>= 0);
918 if (info
->styleChanged
)
920 /* FIXME too slow ? how come ? */
921 s
= ME_ApplyStyle(info
->style
, &info
->stack
[info
->stackTop
].fmt
);
922 ME_ReleaseStyle(info
->style
);
924 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
925 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
932 switch(info
->rtfMajor
)
935 ME_RTFCharAttrHook(info
);
938 ME_RTFParAttrHook(info
);
941 ME_RTFTblAttrHook(info
);
944 if (info
->rtfMinor
== rtfCell
)
946 RTFFlushOutputBuffer(info
);
947 ME_InsertTableCellFromCursor(info
->editor
, 0);
955 ME_StreamInFill(ME_InStream
*stream
)
957 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
958 (BYTE
*)stream
->buffer
,
959 sizeof(stream
->buffer
),
960 (LONG
*)&stream
->dwSize
);
964 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
)
968 int from
, to
, to2
, nUndoMode
;
969 int nEventMask
= editor
->nEventMask
;
970 ME_InStream inStream
;
972 TRACE("stream==%p hWnd==%p format==0x%X\n", stream
, editor
->hWnd
, format
);
973 editor
->nEventMask
= 0;
975 ME_GetSelection(editor
, &from
, &to
);
976 if ((format
& SFF_SELECTION
) && (editor
->mode
& TM_RICHTEXT
)) {
977 style
= ME_GetSelectionInsertStyle(editor
);
979 ME_InternalDeleteText(editor
, from
, to
-from
);
982 style
= editor
->pBuffer
->pDefaultStyle
;
983 ME_AddRefStyle(style
);
984 SendMessageA(editor
->hWnd
, EM_SETSEL
, 0, 0);
985 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
987 ME_ClearTempStyle(editor
);
988 /* FIXME restore default paragraph formatting ! */
992 /* Back up undo mode to a local variable */
993 nUndoMode
= editor
->nUndoMode
;
995 /* Only create an undo if SFF_SELECTION is set */
996 if (!(format
& SFF_SELECTION
))
997 editor
->nUndoMode
= umIgnore
;
999 inStream
.editstream
= stream
;
1000 inStream
.editstream
->dwError
= 0;
1001 inStream
.dwSize
= 0;
1002 inStream
.dwUsed
= 0;
1004 if (format
& SF_RTF
)
1006 /* Check if it's really RTF, and if it is not, use plain text */
1007 ME_StreamInFill(&inStream
);
1008 if (!inStream
.editstream
->dwError
)
1010 if (strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1018 if (!inStream
.editstream
->dwError
)
1020 if (format
& SF_RTF
) {
1021 /* setup the RTF parser */
1022 memset(&parser
, 0, sizeof parser
);
1023 RTFSetEditStream(&parser
, &inStream
);
1024 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1025 parser
.hwndEdit
= editor
->hWnd
;
1026 parser
.editor
= editor
;
1027 parser
.style
= style
;
1028 WriterInit(&parser
);
1030 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1031 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1032 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1035 /* do the parsing */
1037 RTFFlushOutputBuffer(&parser
);
1038 RTFDestroy(&parser
);
1039 if (parser
.lpRichEditOle
)
1040 IRichEditOle_Release(parser
.lpRichEditOle
);
1042 style
= parser
.style
;
1044 else if (format
& SF_TEXT
)
1045 ME_StreamInText(editor
, format
, &inStream
, style
);
1047 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1048 ME_GetSelection(editor
, &to
, &to2
);
1049 /* put the cursor at the top */
1050 if (!(format
& SFF_SELECTION
))
1051 SendMessageA(editor
->hWnd
, EM_SETSEL
, 0, 0);
1054 /* Restore saved undo mode */
1055 editor
->nUndoMode
= nUndoMode
;
1057 /* even if we didn't add an undo, we need to commit anything on the stack */
1058 ME_CommitUndo(editor
);
1060 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1061 if (!(format
& SFF_SELECTION
))
1062 ME_EmptyUndoStack(editor
);
1064 ME_ReleaseStyle(style
);
1065 editor
->nEventMask
= nEventMask
;
1066 if (editor
->bRedraw
)
1068 ME_UpdateRepaint(editor
);
1070 if (!(format
& SFF_SELECTION
)) {
1071 ME_ClearTempStyle(editor
);
1073 ME_MoveCaret(editor
);
1074 ME_SendSelChange(editor
);
1075 ME_SendRequestResize(editor
, FALSE
);
1081 typedef struct tagME_RTFStringStreamStruct
1086 } ME_RTFStringStreamStruct
;
1088 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1090 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1093 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1094 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1095 pStruct
->pos
+= count
;
1101 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1104 ME_RTFStringStreamStruct data
;
1106 data
.string
= string
;
1107 data
.length
= strlen(string
);
1109 es
.dwCookie
= (DWORD
)&data
;
1110 es
.pfnCallback
= ME_ReadFromRTFString
;
1111 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
);
1116 ME_FindItemAtOffset(ME_TextEditor
*editor
, ME_DIType nItemType
, int nOffset
, int *nItemOffset
)
1118 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
1120 while (item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= nOffset
)
1121 item
= ME_FindItemFwd(item
, diParagraph
);
1126 nOffset
-= item
->member
.para
.nCharOfs
;
1127 if (nItemType
== diParagraph
) {
1129 *nItemOffset
= nOffset
;
1134 item
= ME_FindItemFwd(item
, diRun
);
1135 } while (item
&& (item
->member
.run
.nCharOfs
+ ME_StrLen(item
->member
.run
.strText
) <= nOffset
));
1137 nOffset
-= item
->member
.run
.nCharOfs
;
1139 *nItemOffset
= nOffset
;
1146 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1148 const int nLen
= lstrlenW(text
);
1149 const int nTextLen
= ME_GetTextLength(editor
);
1152 ME_DisplayItem
*item
;
1153 ME_DisplayItem
*para
;
1154 WCHAR wLastChar
= ' ';
1156 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1157 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1159 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1160 FIXME("Flags 0x%08x not implemented\n",
1161 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1164 if (chrg
->cpMax
== -1)
1167 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1169 /* when searching up, if cpMin < cpMax, then instead of searching
1170 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1171 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1172 * case, it is always bigger than cpMin.
1174 if (!(flags
& FR_DOWN
))
1178 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1179 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1185 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1188 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1192 if (flags
& FR_DOWN
) /* Forward search */
1194 /* If possible, find the character before where the search starts */
1195 if ((flags
& FR_WHOLEWORD
) && nMin
)
1198 item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1202 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1205 wLastChar
= item
->member
.run
.strText
->szData
[nStart
];
1209 item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1213 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1217 para
= ME_GetParagraph(item
);
1219 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
+ nLen
<= nMax
)
1221 ME_DisplayItem
*pCurItem
= item
;
1222 int nCurStart
= nStart
;
1225 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
], text
[nMatched
], (flags
& FR_MATCHCASE
)))
1227 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1231 if (nMatched
== nLen
)
1233 ME_DisplayItem
*pNextItem
= pCurItem
;
1234 int nNextStart
= nCurStart
;
1237 /* Check to see if next character is a whitespace */
1238 if (flags
& FR_WHOLEWORD
)
1240 if (nCurStart
+ nMatched
== ME_StrLen(pCurItem
->member
.run
.strText
))
1242 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1243 nNextStart
= -nMatched
;
1247 wNextChar
= pNextItem
->member
.run
.strText
->szData
[nNextStart
+ nMatched
];
1251 if (isalnumW(wNextChar
))
1255 nStart
+= para
->member
.para
.nCharOfs
+ pCurItem
->member
.run
.nCharOfs
;
1258 chrgText
->cpMin
= nStart
;
1259 chrgText
->cpMax
= nStart
+ nLen
;
1261 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1264 if (nCurStart
+ nMatched
== ME_StrLen(pCurItem
->member
.run
.strText
))
1266 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1267 para
= ME_GetParagraph(pCurItem
);
1268 nCurStart
= -nMatched
;
1272 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
];
1277 if (nStart
== ME_StrLen(item
->member
.run
.strText
))
1279 item
= ME_FindItemFwd(item
, diRun
);
1280 para
= ME_GetParagraph(item
);
1285 else /* Backward search */
1287 /* If possible, find the character after where the search ends */
1288 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1291 item
= ME_FindItemAtOffset(editor
, diRun
, nEnd
, &nEnd
);
1295 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1298 wLastChar
= item
->member
.run
.strText
->szData
[nEnd
];
1302 item
= ME_FindItemAtOffset(editor
, diRun
, nEnd
, &nEnd
);
1306 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1310 para
= ME_GetParagraph(item
);
1313 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nEnd
- nLen
>= nMin
)
1315 ME_DisplayItem
*pCurItem
= item
;
1319 if (nCurEnd
- nMatched
== 0)
1321 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1322 para
= ME_GetParagraph(pCurItem
);
1323 nCurEnd
= ME_StrLen(pCurItem
->member
.run
.strText
) + nMatched
;
1326 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1], text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
)))
1328 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1332 if (nMatched
== nLen
)
1334 ME_DisplayItem
*pPrevItem
= pCurItem
;
1335 int nPrevEnd
= nCurEnd
;
1338 /* Check to see if previous character is a whitespace */
1339 if (flags
& FR_WHOLEWORD
)
1341 if (nPrevEnd
- nMatched
== 0)
1343 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1345 nPrevEnd
= ME_StrLen(pPrevItem
->member
.run
.strText
) + nMatched
;
1349 wPrevChar
= pPrevItem
->member
.run
.strText
->szData
[nPrevEnd
- nMatched
- 1];
1353 if (isalnumW(wPrevChar
))
1357 nStart
= para
->member
.para
.nCharOfs
+ pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1360 chrgText
->cpMin
= nStart
;
1361 chrgText
->cpMax
= nStart
+ nLen
;
1363 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1366 if (nCurEnd
- nMatched
== 0)
1368 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1369 /* Don't care about pCurItem becoming NULL here; it's already taken
1370 * care of in the exterior loop condition */
1371 para
= ME_GetParagraph(pCurItem
);
1372 nCurEnd
= ME_StrLen(pCurItem
->member
.run
.strText
) + nMatched
;
1376 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1];
1383 item
= ME_FindItemBack(item
, diRun
);
1384 para
= ME_GetParagraph(item
);
1385 nEnd
= ME_StrLen(item
->member
.run
.strText
);
1389 TRACE("not found\n");
1391 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1395 /* helper to send a msg filter notification */
1397 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
1401 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
1402 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1403 msgf
.nmhdr
.code
= EN_MSGFILTER
;
1406 msgf
.wParam
= *wParam
;
1407 msgf
.lParam
= *lParam
;
1408 if (SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
1410 *wParam
= msgf
.wParam
;
1411 *lParam
= msgf
.lParam
;
1412 msgf
.wParam
= *wParam
;
1418 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
1420 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
1421 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
1433 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
1437 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1438 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
)
1440 if (ME_IsSelection(editor
))
1441 ME_DeleteSelection(editor
);
1442 else if (nKey
== VK_DELETE
|| ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
1443 ME_DeleteTextAtCursor(editor
, 1, 1);
1446 ME_CommitUndo(editor
);
1447 ME_UpdateRepaint(editor
);
1448 ME_SendRequestResize(editor
, FALSE
);
1458 chf
.cbSize
= sizeof(chf
);
1460 ME_GetSelectionCharFormat(editor
, &chf
);
1461 ME_DumpStyleToBuf(&chf
, buf
);
1462 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
1466 ME_CheckCharOffsets(editor
);
1473 static void ME_SetCursor(ME_TextEditor
*editor
, int x
)
1475 if (x
< editor
->selofs
|| editor
->linesel
)
1481 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
1486 if(!editor
->lpOleCallback
)
1488 ME_GetSelection(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
1489 if(selrange
.cpMin
== selrange
.cpMax
)
1490 seltype
|= SEL_EMPTY
;
1493 /* FIXME: Handle objects */
1494 seltype
|= SEL_TEXT
;
1495 if(selrange
.cpMax
-selrange
.cpMin
> 1)
1496 seltype
|= SEL_MULTICHAR
;
1498 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
1500 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, GetParent(editor
->hWnd
), NULL
);
1506 ME_TextEditor
*ME_MakeEditor(HWND hWnd
) {
1507 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
1510 ed
->bEmulateVersion10
= FALSE
;
1511 ed
->pBuffer
= ME_MakeText();
1512 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
1513 ME_MakeFirstParagraph(ed
);
1514 ed
->bCaretShown
= FALSE
;
1516 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
1517 ed
->pCursors
[0].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
1518 ed
->pCursors
[0].nOffset
= 0;
1519 ed
->pCursors
[1].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
1520 ed
->pCursors
[1].nOffset
= 0;
1521 ed
->pCursors
[2] = ed
->pCursors
[0];
1522 ed
->pCursors
[3] = ed
->pCursors
[1];
1523 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
1527 ed
->rgbBackColor
= -1;
1528 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
1529 ed
->bCaretAtEnd
= FALSE
;
1531 ed
->nModifyStep
= 0;
1532 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
1533 ed
->pUndoStack
= ed
->pRedoStack
= ed
->pUndoStackBottom
= NULL
;
1534 ed
->nUndoStackSize
= 0;
1535 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
1536 ed
->nUndoMode
= umAddToUndo
;
1537 ed
->nParagraphs
= 1;
1538 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
1539 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diParagraph
);
1541 ed
->bHideSelection
= FALSE
;
1542 ed
->nInvalidOfs
= -1;
1543 ed
->pfnWordBreak
= NULL
;
1544 ed
->lpOleCallback
= NULL
;
1545 ed
->mode
= TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
1546 ed
->AutoURLDetect_bEnable
= FALSE
;
1547 ed
->bHaveFocus
= FALSE
;
1548 GetClientRect(hWnd
, &ed
->rcFormat
);
1549 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
1551 ed
->pFontCache
[i
].nRefs
= 0;
1552 ed
->pFontCache
[i
].nAge
= 0;
1553 ed
->pFontCache
[i
].hFont
= NULL
;
1556 ME_CheckCharOffsets(ed
);
1557 if (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_SELECTIONBAR
)
1563 if (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_PASSWORD
)
1564 ed
->cPasswordMask
= '*';
1566 ed
->cPasswordMask
= 0;
1571 typedef struct tagME_GlobalDestStruct
1575 } ME_GlobalDestStruct
;
1577 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1579 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1584 pDest
= (WORD
*)lpBuff
;
1585 pSrc
= (WORD
*)GlobalLock(pData
->hData
);
1586 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1587 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1589 pData
->nLength
+= i
;
1591 GlobalUnlock(pData
->hData
);
1595 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1597 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1602 pSrc
= (BYTE
*)GlobalLock(pData
->hData
);
1603 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1604 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1606 pData
->nLength
+= i
;
1608 GlobalUnlock(pData
->hData
);
1613 void ME_DestroyEditor(ME_TextEditor
*editor
)
1615 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
1616 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
1619 ME_ClearTempStyle(editor
);
1620 ME_EmptyUndoStack(editor
);
1623 ME_DestroyDisplayItem(p
);
1626 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
1627 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
1629 if (editor
->pFontCache
[i
].hFont
)
1630 DeleteObject(editor
->pFontCache
[i
].hFont
);
1632 if (editor
->rgbBackColor
!= -1)
1633 DeleteObject(editor
->hbrBackground
);
1634 if(editor
->lpOleCallback
)
1635 IUnknown_Release(editor
->lpOleCallback
);
1637 FREE_OBJ(editor
->pBuffer
);
1638 FREE_OBJ(editor
->pCursors
);
1643 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1648 case DLL_PROCESS_ATTACH
:
1649 DisableThreadLibraryCalls(hinstDLL
);
1650 me_heap
= HeapCreate (0, 0x10000, 0);
1651 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
1652 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
1653 hBeam
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
1657 case DLL_PROCESS_DETACH
:
1658 UnregisterClassW(RichEdit20W
, 0);
1659 UnregisterClassW(RichEdit50W
, 0);
1660 UnregisterClassA("RichEdit20A", 0);
1661 UnregisterClassA("RichEdit50A", 0);
1662 if (ME_ListBoxRegistered
)
1663 UnregisterClassW(REListBox20W
, 0);
1664 if (ME_ComboBoxRegistered
)
1665 UnregisterClassW(REComboBox20W
, 0);
1667 HeapDestroy (me_heap
);
1675 #define UNSUPPORTED_MSG(e) \
1677 FIXME(#e ": stub\n"); \
1678 return DefWindowProcW(hWnd, msg, wParam, lParam);
1680 static const char * const edit_messages
[] = {
1709 "EM_SETPASSWORDCHAR",
1710 "EM_EMPTYUNDOBUFFER",
1711 "EM_GETFIRSTVISIBLELINE",
1713 "EM_SETWORDBREAKPROC",
1714 "EM_GETWORDBREAKPROC",
1715 "EM_GETPASSWORDCHAR",
1723 static const char * const richedit_messages
[] = {
1728 "EM_EXLINEFROMCHAR",
1734 "EM_GETOLEINTERFACE",
1744 "EM_SETOLECALLBACK",
1746 "EM_SETTARGETDEVICE",
1754 "EM_GETWORDBREAKPROCEX",
1755 "EM_SETWORDBREAKPROCEX",
1757 "EM_UNKNOWN_USER_83",
1762 "EM_STOPGROUPTYPING",
1766 "EM_GETAUTOURLDETECT",
1769 "EM_GETTEXTLENGTHEX",
1772 "EM_UNKNOWN_USER_98",
1773 "EM_UNKNOWN_USER_99",
1774 "EM_SETPUNCTUATION",
1775 "EM_GETPUNCTUATION",
1776 "EM_SETWORDWRAPMODE",
1777 "EM_GETWORDWRAPMODE",
1783 "EM_UNKNOWN_USER_109",
1784 "EM_UNKNOWN_USER_110",
1785 "EM_UNKNOWN_USER_111",
1786 "EM_UNKNOWN_USER_112",
1787 "EM_UNKNOWN_USER_113",
1788 "EM_UNKNOWN_USER_114",
1789 "EM_UNKNOWN_USER_115",
1790 "EM_UNKNOWN_USER_116",
1791 "EM_UNKNOWN_USER_117",
1792 "EM_UNKNOWN_USER_118",
1793 "EM_UNKNOWN_USER_119",
1794 "EM_SETLANGOPTIONS",
1795 "EM_GETLANGOPTIONS",
1796 "EM_GETIMECOMPMODE",
1800 "EM_SETIMEMODEBIAS",
1805 get_msg_name(UINT msg
)
1807 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
1808 return edit_messages
[msg
- EM_GETSEL
];
1809 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
1810 return richedit_messages
[msg
- EM_CANPASTE
];
1814 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
1815 LPARAM lParam
, BOOL unicode
)
1817 ME_TextEditor
*editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
1819 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1820 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
1822 if (!editor
&& msg
!= WM_NCCREATE
&& msg
!= WM_NCDESTROY
) {
1823 ERR("called with invalid hWnd %p - application bug?\n", hWnd
);
1829 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
1830 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
1831 UNSUPPORTED_MSG(EM_FMTLINES
)
1832 UNSUPPORTED_MSG(EM_FORMATRANGE
)
1833 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
1834 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
1835 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
1836 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1837 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
1838 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1839 UNSUPPORTED_MSG(EM_GETREDONAME
)
1840 UNSUPPORTED_MSG(EM_GETTEXTMODE
)
1841 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
1842 UNSUPPORTED_MSG(EM_GETUNDONAME
)
1843 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
1844 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
1845 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
1846 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
1847 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
1848 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
1849 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
1850 UNSUPPORTED_MSG(EM_SETPALETTE
)
1851 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
1852 UNSUPPORTED_MSG(EM_SETTARGETDEVICE
)
1853 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
1854 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
1855 UNSUPPORTED_MSG(WM_STYLECHANGING
)
1856 UNSUPPORTED_MSG(WM_STYLECHANGED
)
1857 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1859 /* Messages specific to Richedit controls */
1862 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
);
1864 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
1867 UINT code
= DLGC_WANTCHARS
|DLGC_WANTARROWS
;
1868 if(lParam
&& (((LPMSG
)lParam
)->message
== WM_KEYDOWN
))
1870 int vk
= (int)((LPMSG
)lParam
)->wParam
;
1871 /* if style says we want return key */
1872 if((vk
== VK_RETURN
) && (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_WANTRETURN
))
1874 code
|= DLGC_WANTMESSAGE
;
1876 /* we always handle ctrl-tab */
1877 if((vk
== VK_TAB
) && (GetKeyState(VK_CONTROL
) & 0x8000))
1879 code
|= DLGC_WANTMESSAGE
;
1886 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
1887 TRACE("WM_NCCREATE: style 0x%08x\n", pcs
->style
);
1888 editor
= ME_MakeEditor(hWnd
);
1889 SetWindowLongPtrW(hWnd
, 0, (LONG_PTR
)editor
);
1892 case EM_EMPTYUNDOBUFFER
:
1893 ME_EmptyUndoStack(editor
);
1897 /* Note: wParam/lParam can be NULL */
1899 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
1900 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
1901 ME_GetSelection(editor
, (int *)pfrom
, (int *)pto
);
1902 if ((*pfrom
|*pto
) & 0xFFFF0000)
1904 return MAKELONG(*pfrom
,*pto
);
1908 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
1909 ME_GetSelection(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
1910 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
1913 case EM_SETUNDOLIMIT
:
1915 if ((int)wParam
< 0)
1916 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
1918 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
1919 /* Setting a max stack size keeps wine from getting killed
1920 for hogging memory. Windows allocates all this memory at once, so
1921 no program would realistically set a value above our maximum. */
1922 return editor
->nUndoLimit
;
1925 return editor
->pUndoStack
!= NULL
;
1927 return editor
->pRedoStack
!= NULL
;
1928 case WM_UNDO
: /* FIXME: actually not the same */
1937 /* these flags are equivalent to the ES_* counterparts */
1938 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
1939 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
;
1940 DWORD settings
= GetWindowLongW(hWnd
, GWL_STYLE
) & mask
;
1946 /* these flags are equivalent to ES_* counterparts
1947 * ECO_READONLY is already implemented in the code, only requires
1948 * setting the bit to work
1950 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
1951 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
1952 DWORD raw
= GetWindowLongW(hWnd
, GWL_STYLE
);
1953 DWORD settings
= mask
& raw
;
1969 SetWindowLongW(hWnd
, GWL_STYLE
, (raw
& ~mask
) | (settings
& mask
));
1971 if (settings
& ECO_AUTOWORDSELECTION
)
1972 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1973 if (settings
& ECO_SELECTIONBAR
)
1974 editor
->selofs
= 16;
1977 ME_WrapMarkedParagraphs(editor
);
1979 if (settings
& ECO_VERTICAL
)
1980 FIXME("ECO_VERTICAL not implemented yet!\n");
1981 if (settings
& ECO_AUTOHSCROLL
)
1982 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1983 if (settings
& ECO_AUTOVSCROLL
)
1984 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1985 if (settings
& ECO_NOHIDESEL
)
1986 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1987 if (settings
& ECO_WANTRETURN
)
1988 FIXME("ECO_WANTRETURN not implemented yet!\n");
1994 ME_InvalidateSelection(editor
);
1995 ME_SetSelection(editor
, wParam
, lParam
);
1996 ME_InvalidateSelection(editor
);
1997 ME_SendSelChange(editor
);
2000 case EM_SETSCROLLPOS
:
2002 POINT
*point
= (POINT
*)lParam
;
2003 ME_ScrollAbs(editor
, point
->y
);
2006 case EM_AUTOURLDETECT
:
2008 if (wParam
==1 || wParam
==0)
2010 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
2013 return E_INVALIDARG
;
2015 case EM_GETAUTOURLDETECT
:
2017 return editor
->AutoURLDetect_bEnable
;
2022 CHARRANGE range
= *(CHARRANGE
*)lParam
;
2024 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
2026 ME_InvalidateSelection(editor
);
2027 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
2028 ME_InvalidateSelection(editor
);
2029 ME_SendSelChange(editor
);
2033 case EM_SHOWSCROLLBAR
:
2035 ShowScrollBar(editor
->hWnd
, wParam
, lParam
);
2041 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
2045 int oldModify
= editor
->nModifyStep
;
2047 if (!pStruct
) return 0;
2049 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2050 pStruct
->codepage
== 1200 ? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
2051 pStruct
->flags
, pStruct
->codepage
);
2053 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2054 wszText
= lParam
? ME_ToUnicode(pStruct
->codepage
== 1200, (void *)lParam
) : NULL
;
2055 len
= wszText
? lstrlenW(wszText
) : 0;
2057 /* FIXME: this should support RTF strings too, according to MSDN */
2058 if (pStruct
->flags
& ST_SELECTION
) {
2059 ME_GetSelection(editor
, &from
, &to
);
2060 style
= ME_GetSelectionInsertStyle(editor
);
2061 ME_InternalDeleteText(editor
, from
, to
- from
);
2062 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
2063 ME_ReleaseStyle(style
);
2066 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
2067 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
2070 ME_CommitUndo(editor
);
2071 if (!(pStruct
->flags
& ST_KEEPUNDO
))
2073 editor
->nModifyStep
= oldModify
;
2074 ME_EmptyUndoStack(editor
);
2076 ME_UpdateRepaint(editor
);
2079 case EM_SETBKGNDCOLOR
:
2082 if (editor
->rgbBackColor
!= -1) {
2083 DeleteObject(editor
->hbrBackground
);
2084 lColor
= editor
->rgbBackColor
;
2086 else lColor
= GetSysColor(COLOR_WINDOW
);
2090 editor
->rgbBackColor
= -1;
2091 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2095 editor
->rgbBackColor
= lParam
;
2096 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
2098 if (editor
->bRedraw
)
2100 InvalidateRect(hWnd
, NULL
, TRUE
);
2106 return editor
->nModifyStep
== 0 ? 0 : 1;
2110 editor
->nModifyStep
= 1;
2112 editor
->nModifyStep
= 0;
2116 case EM_SETREADONLY
:
2118 long nStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
2120 nStyle
|= ES_READONLY
;
2122 nStyle
&= ~ES_READONLY
;
2123 SetWindowLongW(hWnd
, GWL_STYLE
, nStyle
);
2126 case EM_SETEVENTMASK
:
2128 DWORD nOldMask
= editor
->nEventMask
;
2130 editor
->nEventMask
= lParam
;
2133 case EM_GETEVENTMASK
:
2134 return editor
->nEventMask
;
2135 case EM_SETCHARFORMAT
:
2137 CHARFORMAT2W buf
, *p
;
2138 BOOL bRepaint
= TRUE
;
2139 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
2140 if (p
== NULL
) return 0;
2142 ME_SetDefaultCharFormat(editor
, p
);
2143 else if (wParam
== (SCF_WORD
| SCF_SELECTION
)) {
2144 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2146 } else if (wParam
== SCF_ALL
) {
2147 if (editor
->mode
& TM_PLAINTEXT
)
2148 ME_SetDefaultCharFormat(editor
, p
);
2150 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), p
);
2151 } else if (editor
->mode
& TM_PLAINTEXT
) {
2155 ME_GetSelection(editor
, &from
, &to
);
2156 bRepaint
= (from
!= to
);
2157 ME_SetSelectionCharFormat(editor
, p
);
2159 editor
->nModifyStep
= 1;
2160 ME_CommitUndo(editor
);
2162 ME_RewrapRepaint(editor
);
2165 case EM_GETCHARFORMAT
:
2167 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
2168 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
2169 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
2170 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
2171 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
2173 tmp
.cbSize
= sizeof(tmp
);
2175 ME_GetDefaultCharFormat(editor
, &tmp
);
2177 ME_GetSelectionCharFormat(editor
, &tmp
);
2178 ME_CopyToCFAny(dst
, &tmp
);
2181 case EM_SETPARAFORMAT
:
2182 ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
2183 ME_RewrapRepaint(editor
);
2184 ME_CommitUndo(editor
);
2186 case EM_GETPARAFORMAT
:
2187 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
2189 case EM_GETFIRSTVISIBLELINE
:
2191 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
2192 int y
= ME_GetYScrollPos(editor
);
2197 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
2198 if (p
->type
== diTextEnd
)
2200 if (p
->type
== diParagraph
) {
2201 ypara
= p
->member
.para
.nYPos
;
2204 ystart
= ypara
+ p
->member
.row
.nYPos
;
2205 yend
= ystart
+ p
->member
.row
.nHeight
;
2213 case EM_HIDESELECTION
:
2215 editor
->bHideSelection
= (wParam
!= 0);
2216 ME_InvalidateSelection(editor
);
2221 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
2222 return TRUE
; /* Should return false if a single line richedit control */
2227 ME_GetSelection(editor
, &from
, &to
);
2228 ME_InternalDeleteText(editor
, from
, to
-from
);
2229 ME_CommitUndo(editor
);
2230 ME_UpdateRepaint(editor
);
2237 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
2238 size_t len
= wszText
? lstrlenW(wszText
) : 0;
2239 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
2241 ME_GetSelection(editor
, &from
, &to
);
2242 style
= ME_GetSelectionInsertStyle(editor
);
2243 ME_InternalDeleteText(editor
, from
, to
-from
);
2244 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
2245 ME_ReleaseStyle(style
);
2246 /* drop temporary style if line end */
2248 * FIXME question: does abc\n mean: put abc,
2249 * clear temp style, put \n? (would require a change)
2251 if (len
>0 && wszText
[len
-1] == '\n')
2252 ME_ClearTempStyle(editor
);
2253 ME_EndToUnicode(unicode
, wszText
);
2254 ME_CommitUndo(editor
);
2256 ME_EmptyUndoStack(editor
);
2257 ME_UpdateRepaint(editor
);
2260 case EM_SCROLLCARET
:
2262 int top
, bottom
; /* row's edges relative to document top */
2264 ME_DisplayItem
*para
, *row
;
2266 nPos
= ME_GetYScrollPos(editor
);
2267 row
= ME_RowStart(editor
->pCursors
[0].pRun
);
2268 para
= ME_GetParagraph(row
);
2269 top
= para
->member
.para
.nYPos
+ row
->member
.row
.nYPos
;
2270 bottom
= top
+ row
->member
.row
.nHeight
;
2272 if (top
< nPos
) /* caret above window */
2273 ME_ScrollAbs(editor
, top
);
2274 else if (nPos
+ editor
->sizeWindow
.cy
< bottom
) /*below*/
2275 ME_ScrollAbs(editor
, bottom
- editor
->sizeWindow
.cy
);
2283 BOOL bRepaint
= LOWORD(lParam
);
2286 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
2287 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
2289 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
2290 ReleaseDC(hWnd
, hDC
);
2291 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), &fmt
);
2292 ME_SetDefaultCharFormat(editor
, &fmt
);
2294 ME_CommitUndo(editor
);
2296 ME_RewrapRepaint(editor
);
2301 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
2304 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
2305 if (!unicode
&& !strncmp((char *)lParam
, "{\\rtf", 5))
2307 /* Undocumented: WM_SETTEXT supports RTF text */
2308 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
2312 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
2313 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
2314 if (lstrlenW(wszText
) > 0)
2318 /* uses default style! */
2319 if (!(GetWindowLongW(hWnd
, GWL_STYLE
) & ES_MULTILINE
))
2324 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
2327 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
2329 ME_EndToUnicode(unicode
, wszText
);
2333 TRACE("WM_SETTEXT - NULL\n");
2334 ME_CommitUndo(editor
);
2335 ME_EmptyUndoStack(editor
);
2336 ME_SetSelection(editor
, 0, 0);
2337 editor
->nModifyStep
= 0;
2338 ME_UpdateRepaint(editor
);
2343 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2344 if (IsClipboardFormatAvailable(nRTFFormat
))
2346 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2354 ME_GlobalDestStruct gds
;
2355 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2358 if (IsClipboardFormatAvailable(nRTFFormat
))
2359 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2360 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2361 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2365 if (!OpenClipboard(hWnd
))
2367 gds
.hData
= GetClipboardData(cf
);
2369 es
.dwCookie
= (DWORD
)&gds
;
2370 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2371 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
);
2379 LPDATAOBJECT dataObj
= NULL
;
2383 if (editor
->cPasswordMask
)
2384 return 0; /* Copying or Cutting masked text isn't allowed */
2386 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
2387 if(editor
->lpOleCallback
)
2388 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2389 if(FAILED(hr
) || !dataObj
)
2390 hr
= ME_GetDataObject(editor
, &range
, &dataObj
);
2392 hr
= OleSetClipboard(dataObj
);
2393 IDataObject_Release(dataObj
);
2395 if (SUCCEEDED(hr
) && msg
== WM_CUT
)
2397 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
);
2398 ME_CommitUndo(editor
);
2399 ME_UpdateRepaint(editor
);
2403 case WM_GETTEXTLENGTH
:
2405 GETTEXTLENGTHEX how
;
2407 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
2408 how
.codepage
= unicode
? 1200 : CP_ACP
;
2409 return ME_GetTextLengthEx(editor
, &how
);
2411 case EM_GETTEXTLENGTHEX
:
2412 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
2417 LPSTR bufferA
= NULL
;
2418 LPWSTR bufferW
= NULL
;
2421 bufferW
= heap_alloc((wParam
+ 2) * sizeof(WCHAR
));
2423 bufferA
= heap_alloc(wParam
+ 2);
2425 ex
.cb
= (wParam
+ 2) * (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
2426 ex
.flags
= GT_USECRLF
;
2427 ex
.codepage
= unicode
? 1200 : CP_ACP
;
2428 ex
.lpDefaultChar
= NULL
;
2429 ex
.lpUsedDefChar
= NULL
;
2430 rc
= RichEditWndProc_common(hWnd
, EM_GETTEXTEX
, (WPARAM
)&ex
, unicode
? (LPARAM
)bufferW
: (LPARAM
)bufferA
, unicode
);
2434 memcpy((LPWSTR
)lParam
, bufferW
, wParam
* sizeof(WCHAR
));
2435 if (lstrlenW(bufferW
) >= wParam
) rc
= 0;
2439 memcpy((LPSTR
)lParam
, bufferA
, wParam
);
2440 if (strlen(bufferA
) >= wParam
) rc
= 0;
2448 GETTEXTEX
*ex
= (GETTEXTEX
*)wParam
;
2449 int nStart
, nCount
; /* in chars */
2451 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
2452 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
2454 if (ex
->flags
& GT_SELECTION
)
2456 ME_GetSelection(editor
, &nStart
, &nCount
);
2462 nCount
= 0x7fffffff;
2464 if (ex
->codepage
== 1200)
2466 nCount
= min(nCount
, ex
->cb
/ sizeof(WCHAR
) - 1);
2467 return ME_GetTextW(editor
, (LPWSTR
)lParam
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
2471 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2472 we can just take a bigger buffer? :) */
2473 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
2475 DWORD buflen
= ex
->cb
;
2479 nCount
= min(nCount
, ex
->cb
- 1);
2480 buffer
= heap_alloc((crlfmul
*nCount
+ 1) * sizeof(WCHAR
));
2482 buflen
= ME_GetTextW(editor
, buffer
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
2483 rc
= WideCharToMultiByte(ex
->codepage
, flags
, buffer
, -1, (LPSTR
)lParam
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
2484 if (rc
) rc
--; /* do not count 0 terminator */
2493 TEXTRANGEW tr
; /* W and A differ only by rng->lpstrText */
2494 ME_GetSelection(editor
, &from
, &to
);
2495 tr
.chrg
.cpMin
= from
;
2497 tr
.lpstrText
= (WCHAR
*)lParam
;
2498 return RichEditWndProc_common(hWnd
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
, unicode
);
2500 case EM_GETSCROLLPOS
:
2502 POINT
*point
= (POINT
*)lParam
;
2503 point
->x
= 0; /* FIXME side scrolling not implemented */
2504 point
->y
= ME_GetYScrollPos(editor
);
2507 case EM_GETTEXTRANGE
:
2509 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
2510 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2511 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
,
2512 editor
->bEmulateVersion10
, ME_GetTextLength(editor
));
2514 return ME_GetTextW(editor
, rng
->lpstrText
, rng
->chrg
.cpMin
, rng
->chrg
.cpMax
-rng
->chrg
.cpMin
, editor
->bEmulateVersion10
);
2517 int nLen
= rng
->chrg
.cpMax
-rng
->chrg
.cpMin
;
2518 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2519 int nChars
= ME_GetTextW(editor
, p
, rng
->chrg
.cpMin
, nLen
, editor
->bEmulateVersion10
);
2520 /* FIXME this is a potential security hole (buffer overrun)
2521 if you know more about wchar->mbyte conversion please explain
2523 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)rng
->lpstrText
, nLen
+1, NULL
, NULL
);
2530 ME_DisplayItem
*run
;
2531 const unsigned int nMaxChars
= *(WORD
*) lParam
;
2532 unsigned int nEndChars
, nCharsLeft
= nMaxChars
;
2533 char *dest
= (char *) lParam
;
2534 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2535 we need to know how if we have the extra \n or not */
2536 int nLF
= editor
->bEmulateVersion10
;
2538 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
2539 unicode
? "Unicode" : "Ansi");
2541 run
= ME_FindRowWithNumber(editor
, wParam
);
2545 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
2546 && !(run
->member
.run
.nFlags
& MERF_ENDPARA
))
2550 if (run
->type
!= diRun
)
2552 strText
= run
->member
.run
.strText
;
2553 nCopy
= min(nCharsLeft
, strText
->nLen
);
2556 lstrcpynW((LPWSTR
) dest
, strText
->szData
, nCopy
);
2558 nCopy
= WideCharToMultiByte(CP_ACP
, 0, strText
->szData
, nCopy
, dest
,
2559 nCharsLeft
, NULL
, NULL
);
2560 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
2561 nCharsLeft
-= nCopy
;
2564 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2565 nEndChars
= min(nCharsLeft
, 2 + nLF
);
2566 nCharsLeft
-= nEndChars
;
2569 const WCHAR src
[] = {'\r', '\0'};
2570 const WCHAR src10
[] = {'\r', '\n', '\0'};
2571 lstrcpynW((LPWSTR
) dest
, nLF
? src10
: src
, nEndChars
);
2574 lstrcpynA(dest
, nLF
? "\r\n" : "\r", nEndChars
);
2576 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars
- nCharsLeft
);
2578 if (nEndChars
== 2 + nLF
)
2579 return nMaxChars
- nCharsLeft
- 1; /* don't count \0 */
2581 return nMaxChars
- nCharsLeft
;
2583 case EM_GETLINECOUNT
:
2585 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
2588 while (item
!= editor
->pBuffer
->pLast
)
2590 assert(item
->type
== diParagraph
);
2591 nRows
+= item
->member
.para
.nRows
;
2592 item
= item
->member
.para
.next_para
;
2594 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
2595 return max(1, nRows
);
2597 case EM_LINEFROMCHAR
:
2600 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
, 1));
2602 return ME_RowNumberFromCharOfs(editor
, wParam
);
2604 case EM_EXLINEFROMCHAR
:
2607 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
,1));
2609 return ME_RowNumberFromCharOfs(editor
, lParam
);
2613 ME_DisplayItem
*item
, *para
;
2617 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
2619 item
= ME_FindRowWithNumber(editor
, wParam
);
2622 para
= ME_GetParagraph(item
);
2623 item
= ME_FindItemFwd(item
, diRun
);
2624 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
2625 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
2630 ME_DisplayItem
*item
, *item_end
;
2631 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
2633 if (wParam
> ME_GetTextLength(editor
))
2637 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2640 item
= ME_FindItemAtOffset(editor
, diRun
, wParam
, NULL
);
2641 item
= ME_RowStart(item
);
2642 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, ME_FindItemFwd(item
, diRun
), 0);
2643 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
2644 if (item_end
->type
== diStartRow
)
2645 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, ME_FindItemFwd(item_end
, diRun
), 0);
2647 nNextLineOfs
= ME_FindItemFwd(item
, diParagraphOrEnd
)->member
.para
.nCharOfs
2648 - (editor
->bEmulateVersion10
?2:1);
2649 nChars
= nNextLineOfs
- nThisLineOfs
;
2650 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
2653 case EM_EXLIMITTEXT
:
2655 if ((int)lParam
< 0)
2658 editor
->nTextLimit
= 65536;
2660 editor
->nTextLimit
= (int) lParam
;
2666 editor
->nTextLimit
= 65536;
2668 editor
->nTextLimit
= (int) wParam
;
2671 case EM_GETLIMITTEXT
:
2673 return editor
->nTextLimit
;
2677 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
2678 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
2682 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
2683 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
2684 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
2690 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
2691 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
2695 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
2696 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
2697 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
2703 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
2704 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
2706 case EM_FINDTEXTEXW
:
2708 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
2709 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
2712 if (!wParam
|| !lParam
)
2714 *(int *)wParam
= editor
->nZoomNumerator
;
2715 *(int *)lParam
= editor
->nZoomDenominator
;
2718 return ME_SetZoom(editor
, wParam
, lParam
);
2719 case EM_CHARFROMPOS
:
2720 return ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
);
2721 case EM_POSFROMCHAR
:
2723 ME_DisplayItem
*pRun
;
2724 int nCharOfs
, nOffset
, nLength
;
2728 /* detect which API version we're dealing with */
2729 if (wParam
>= 0x40000)
2731 nLength
= ME_GetTextLength(editor
);
2733 if (nCharOfs
< nLength
) {
2734 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pRun
, &nOffset
);
2735 assert(pRun
->type
== diRun
);
2736 pt
.y
= pRun
->member
.run
.pt
.y
;
2737 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
);
2738 pt
.y
+= ME_GetParagraph(pRun
)->member
.para
.nYPos
;
2741 pt
.y
= editor
->pBuffer
->pLast
->member
.para
.nYPos
;
2743 pt
.x
+= editor
->selofs
;
2744 if (wParam
>= 0x40000) {
2745 *(POINTL
*)wParam
= pt
;
2747 return MAKELONG( pt
.x
, pt
.y
);
2750 if (GetWindowLongW(hWnd
, GWL_STYLE
) & WS_HSCROLL
)
2751 { /* Squelch the default horizontal scrollbar it would make */
2752 ShowScrollBar(editor
->hWnd
, SB_HORZ
, FALSE
);
2754 ME_CommitUndo(editor
);
2755 ME_WrapMarkedParagraphs(editor
);
2756 ME_MoveCaret(editor
);
2759 ME_DestroyEditor(editor
);
2760 SetWindowLongPtrW(hWnd
, 0, 0);
2762 case WM_LBUTTONDOWN
:
2763 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2764 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2767 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
2769 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2770 ME_SetCursor(editor
, LOWORD(lParam
));
2773 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2774 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2776 if (GetCapture() == hWnd
)
2777 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
2778 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2779 ME_SetCursor(editor
, LOWORD(lParam
));
2782 if (GetCapture() == hWnd
)
2784 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2785 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2787 editor
->linesel
= 0;
2788 ME_SetCursor(editor
, LOWORD(lParam
));
2789 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2791 case WM_LBUTTONDBLCLK
:
2792 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2793 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2795 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2796 ME_SelectWord(editor
);
2799 case WM_RBUTTONDOWN
:
2800 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2801 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2804 case WM_CONTEXTMENU
:
2805 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
2809 if (editor
->bRedraw
)
2814 hDC
= BeginPaint(hWnd
, &ps
);
2815 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
2816 EndPaint(hWnd
, &ps
);
2820 editor
->bHaveFocus
= TRUE
;
2821 ME_ShowCaret(editor
);
2822 ME_SendOldNotify(editor
, EN_SETFOCUS
);
2825 ME_HideCaret(editor
);
2826 editor
->bHaveFocus
= FALSE
;
2827 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
2831 if (editor
->bRedraw
)
2833 HDC hDC
= (HDC
)wParam
;
2835 if (GetUpdateRect(hWnd
,&rc
,TRUE
))
2837 FillRect(hDC
, &rc
, editor
->hbrBackground
);
2843 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
2846 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
2847 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2851 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
2852 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2854 if (ME_KeyDown(editor
, LOWORD(wParam
)))
2862 wstr
= (WCHAR
)wParam
;
2865 CHAR charA
= wParam
;
2866 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2868 if (editor
->AutoURLDetect_bEnable
)
2869 ME_AutoURLDetect(editor
, wstr
);
2873 case 1: /* Ctrl-A */
2874 ME_SetSelection(editor
, 0, -1);
2876 case 3: /* Ctrl-C */
2877 SendMessageW(editor
->hWnd
, WM_COPY
, 0, 0);
2881 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
) {
2882 MessageBeep(MB_ICONERROR
);
2883 return 0; /* FIXME really 0 ? */
2888 case 22: /* Ctrl-V */
2889 SendMessageW(editor
->hWnd
, WM_PASTE
, 0, 0);
2891 case 24: /* Ctrl-X */
2892 SendMessageW(editor
->hWnd
, WM_CUT
, 0, 0);
2894 case 25: /* Ctrl-Y */
2895 SendMessageW(editor
->hWnd
, EM_REDO
, 0, 0);
2897 case 26: /* Ctrl-Z */
2898 SendMessageW(editor
->hWnd
, EM_UNDO
, 0, 0);
2901 if (((unsigned)wstr
)>=' '
2902 || (wstr
=='\r' && (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_MULTILINE
))
2904 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2905 /* WM_CHAR is restricted to nTextLimit */
2907 ME_GetSelection(editor
, &from
, &to
);
2908 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2910 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2911 ME_SaveTempStyle(editor
);
2912 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2913 ME_ReleaseStyle(style
);
2914 ME_CommitUndo(editor
);
2916 ME_UpdateRepaint(editor
);
2920 case EM_SCROLL
: /* fall through */
2926 origNPos
= ME_GetYScrollPos(editor
);
2929 if (editor
&& editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
2930 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
2931 if (lineHeight
<= 0) lineHeight
= 24;
2933 switch(LOWORD(wParam
))
2936 ME_ScrollUp(editor
,lineHeight
);
2939 ME_ScrollDown(editor
,lineHeight
);
2942 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
2945 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
2948 case SB_THUMBPOSITION
:
2949 ME_ScrollAbs(editor
,HIWORD(wParam
));
2952 if (msg
== EM_SCROLL
)
2953 return 0x00010000 | (((ME_GetYScrollPos(editor
) - origNPos
)/lineHeight
) & 0xffff);
2959 UINT pulScrollLines
;
2961 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2962 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2965 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
2966 gcWheelDelta
= -GET_WHEEL_DELTA_WPARAM(wParam
);
2968 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
2970 /* FIXME follow the original */
2971 ME_ScrollDown(editor
,pulScrollLines
* (gcWheelDelta
/ WHEEL_DELTA
) * 8);
2977 *((RECT
*)lParam
) = editor
->rcFormat
;
2985 RECT
*rc
= (RECT
*)lParam
;
2989 editor
->rcFormat
.left
+= rc
->left
;
2990 editor
->rcFormat
.top
+= rc
->top
;
2991 editor
->rcFormat
.right
+= rc
->right
;
2992 editor
->rcFormat
.bottom
+= rc
->bottom
;
2996 editor
->rcFormat
= *rc
;
3001 GetClientRect(hWnd
, &editor
->rcFormat
);
3003 if (msg
!= EM_SETRECTNP
)
3004 ME_RewrapRepaint(editor
);
3007 case EM_REQUESTRESIZE
:
3008 ME_SendRequestResize(editor
, TRUE
);
3011 if ((editor
->bRedraw
= wParam
))
3012 ME_RewrapRepaint(editor
);
3016 GetClientRect(hWnd
, &editor
->rcFormat
);
3017 ME_RewrapRepaint(editor
);
3018 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3020 /* IME messages to make richedit controls IME aware */
3021 case WM_IME_SETCONTEXT
:
3022 case WM_IME_CONTROL
:
3024 case WM_IME_COMPOSITIONFULL
:
3026 case WM_IME_STARTCOMPOSITION
:
3028 editor
->imeStartIndex
=ME_GetCursorOfs(editor
,0);
3029 ME_DeleteSelection(editor
);
3030 ME_CommitUndo(editor
);
3031 ME_UpdateRepaint(editor
);
3034 case WM_IME_COMPOSITION
:
3038 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
3039 hIMC
= ImmGetContext(hWnd
);
3040 ME_DeleteSelection(editor
);
3041 ME_CommitUndo(editor
);
3042 ME_SaveTempStyle(editor
);
3043 if (lParam
& GCS_RESULTSTR
)
3045 LPWSTR lpCompStr
= NULL
;
3048 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
3049 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
3050 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpCompStr
, dwBufLen
);
3051 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
3052 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
3054 else if (lParam
& GCS_COMPSTR
)
3056 LPWSTR lpCompStr
= NULL
;
3059 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
3060 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
3061 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, dwBufLen
);
3062 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
3064 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
3065 ME_SetSelection(editor
,editor
->imeStartIndex
,
3066 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
3068 ME_ReleaseStyle(style
);
3069 ME_UpdateRepaint(editor
);
3072 case WM_IME_ENDCOMPOSITION
:
3074 ME_DeleteSelection(editor
);
3075 editor
->imeStartIndex
=-1;
3078 case EM_GETOLEINTERFACE
:
3080 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
3081 return CreateIRichEditOle(editor
, ppvObj
);
3083 case EM_GETPASSWORDCHAR
:
3085 return editor
->cPasswordMask
;
3087 case EM_SETOLECALLBACK
:
3088 if(editor
->lpOleCallback
)
3089 IUnknown_Release(editor
->lpOleCallback
);
3090 editor
->lpOleCallback
= (LPRICHEDITOLECALLBACK
)lParam
;
3091 if(editor
->lpOleCallback
)
3092 IUnknown_AddRef(editor
->lpOleCallback
);
3094 case EM_GETWORDBREAKPROC
:
3095 return (LRESULT
)editor
->pfnWordBreak
;
3096 case EM_SETWORDBREAKPROC
:
3098 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
3100 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
3101 return (LRESULT
)pfnOld
;
3103 case EM_SETTEXTMODE
:
3108 ret
= RichEditWndProc_common(hWnd
, WM_GETTEXTLENGTH
, 0, 0, unicode
);
3111 /*Check for valid wParam*/
3112 if ((((wParam
& TM_RICHTEXT
) && ((wParam
& TM_PLAINTEXT
) << 1))) ||
3113 (((wParam
& TM_MULTILEVELUNDO
) && ((wParam
& TM_SINGLELEVELUNDO
) << 1))) ||
3114 (((wParam
& TM_MULTICODEPAGE
) && ((wParam
& TM_SINGLECODEPAGE
) << 1))))
3118 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
3120 mask
|= (TM_RICHTEXT
| TM_PLAINTEXT
);
3121 changes
|= (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
));
3123 /*FIXME: Currently no support for undo level and code page options*/
3124 editor
->mode
= (editor
->mode
& (~mask
)) | changes
;
3130 case EM_SETPASSWORDCHAR
:
3132 editor
->cPasswordMask
= wParam
;
3133 ME_RewrapRepaint(editor
);
3138 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3143 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3145 BOOL unicode
= TRUE
;
3147 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3148 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
3151 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
3154 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3156 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
3159 /******************************************************************
3160 * RichEditANSIWndProc (RICHED20.10)
3162 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3164 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
3167 /******************************************************************
3168 * RichEdit10ANSIWndProc (RICHED20.9)
3170 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3174 /* FIXME: this is NOT the same as 2.0 version */
3175 result
= RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
3176 if (msg
== WM_NCCREATE
)
3178 ME_TextEditor
*editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
3180 editor
->bEmulateVersion10
= TRUE
;
3181 editor
->pBuffer
->pLast
->member
.para
.nCharOfs
= 2;
3186 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
3188 HWND hWnd
= editor
->hWnd
;
3189 SendMessageA(GetParent(hWnd
), WM_COMMAND
, (nCode
<<16)|GetWindowLongW(hWnd
, GWLP_ID
), (LPARAM
)hWnd
);
3192 void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3195 ME_Cursor tmpCursor
;
3196 int nCharOfs
; /* The start of the clicked text. Absolute character offset */
3201 x
= (short)LOWORD(lParam
);
3202 y
= (short)HIWORD(lParam
);
3203 nCharOfs
= ME_CharFromPos(editor
, x
, y
);
3204 if (nCharOfs
< 0) return;
3206 ME_CursorFromCharOfs(editor
, nCharOfs
, &tmpCursor
);
3207 tmpRun
= &tmpCursor
.pRun
->member
.run
;
3209 if ((tmpRun
->style
->fmt
.dwMask
& CFM_LINK
)
3210 && (tmpRun
->style
->fmt
.dwEffects
& CFE_LINK
))
3211 { /* The clicked run has CFE_LINK set */
3212 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
3213 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
3214 info
.nmhdr
.code
= EN_LINK
;
3216 info
.wParam
= wParam
;
3217 info
.lParam
= lParam
;
3218 info
.chrg
.cpMin
= ME_CharOfsFromRunOfs(editor
,tmpCursor
.pRun
,0);
3219 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ ME_StrVLen(tmpRun
->strText
);
3220 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,info
.nmhdr
.idFrom
, (LPARAM
)&info
);
3224 int ME_CountParagraphsBetween(ME_TextEditor
*editor
, int from
, int to
)
3226 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
3229 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= from
)
3230 item
= item
->member
.para
.next_para
;
3233 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= to
) {
3234 item
= item
->member
.para
.next_para
;
3241 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int nStart
, int nChars
, int bCRLF
)
3243 ME_DisplayItem
*item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
3245 WCHAR
*pStart
= buffer
;
3254 int nLen
= ME_StrLen(item
->member
.run
.strText
) - nStart
;
3257 CopyMemory(buffer
, item
->member
.run
.strText
->szData
+ nStart
, sizeof(WCHAR
)*nLen
);
3264 item
= ME_FindItemFwd(item
, diRun
);
3267 while(nChars
&& item
)
3269 int nLen
= ME_StrLen(item
->member
.run
.strText
);
3273 if (item
->member
.run
.nFlags
& MERF_ENDPARA
)
3275 if (!ME_FindItemFwd(item
, diRun
))
3276 /* No '\r' is appended to the last paragraph. */
3278 else if (bCRLF
&& nChars
== 1) {
3289 /* our end paragraph consists of 2 characters now */
3290 if (editor
->bEmulateVersion10
)
3295 CopyMemory(buffer
, item
->member
.run
.strText
->szData
, sizeof(WCHAR
)*nLen
);
3302 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
3306 item
= ME_FindItemFwd(item
, diRun
);
3309 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
3313 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
3318 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
3319 wcW
.lpfnWndProc
= RichEditWndProcW
;
3321 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
3322 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
3324 wcW
.hCursor
= hBeam
;
3325 wcW
.hbrBackground
= (HBRUSH
)GetStockObject(NULL_BRUSH
);
3326 wcW
.lpszMenuName
= NULL
;
3328 if (is_version_nt())
3330 wcW
.lpszClassName
= RichEdit20W
;
3331 if (!RegisterClassW(&wcW
)) return FALSE
;
3332 wcW
.lpszClassName
= RichEdit50W
;
3333 if (!RegisterClassW(&wcW
)) return FALSE
;
3337 /* WNDCLASSA/W have the same layout */
3338 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
3339 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
3340 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
3341 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
3344 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
3345 wcA
.lpfnWndProc
= RichEditWndProcA
;
3347 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
3348 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
3350 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
3351 wcA
.hbrBackground
= (HBRUSH
)GetStockObject(NULL_BRUSH
);
3352 wcA
.lpszMenuName
= NULL
;
3353 wcA
.lpszClassName
= "RichEdit20A";
3354 if (!RegisterClassA(&wcA
)) return FALSE
;
3355 wcA
.lpszClassName
= "RichEdit50A";
3356 if (!RegisterClassA(&wcA
)) return FALSE
;
3361 LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
3362 /* FIXME: Not implemented */
3363 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3364 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
3365 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3368 LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
3369 /* FIXME: Not implemented */
3370 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3371 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
3372 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3375 /******************************************************************
3376 * REExtendedRegisterClass (RICHED20.8)
3378 * FIXME undocumented
3379 * Need to check for errors and implement controls and callbacks
3381 LRESULT WINAPI
REExtendedRegisterClass(void)
3386 FIXME("semi stub\n");
3390 wcW
.hInstance
= NULL
;
3393 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
3394 wcW
.lpszMenuName
= NULL
;
3396 if (!ME_ListBoxRegistered
)
3398 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
3399 wcW
.lpfnWndProc
= REListWndProc
;
3400 wcW
.lpszClassName
= REListBox20W
;
3401 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
3404 if (!ME_ComboBoxRegistered
)
3406 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
3407 wcW
.lpfnWndProc
= REComboWndProc
;
3408 wcW
.lpszClassName
= REComboBox20W
;
3409 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
3413 if (ME_ListBoxRegistered
)
3415 if (ME_ComboBoxRegistered
)
3421 int ME_AutoURLDetect(ME_TextEditor
*editor
, WCHAR curChar
)
3441 int curf_ef
, link_ef
, def_ef
;
3442 int cur_prefx
, prefx_cnt
;
3443 int sel_min
, sel_max
;
3446 int URLmin
, URLmax
= 0;
3449 CHARFORMAT2W cur_format
;
3450 CHARFORMAT2W default_format
;
3452 RichEditANSIWndProc(editor
->hWnd
, EM_EXGETSEL
, (WPARAM
) 0, (LPARAM
) &ins_pt
);
3453 sel_min
= ins_pt
.cpMin
;
3454 sel_max
= ins_pt
.cpMax
;
3455 if (sel_min
==sel_max
)
3457 if (sel_min
!=sel_max
)
3458 car_pos
= ME_GetTextLength(editor
)+1;
3459 cur_format
.cbSize
= sizeof(cur_format
);
3460 default_format
.cbSize
= sizeof(default_format
);
3461 RichEditANSIWndProc(editor
->hWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
) &cur_format
);
3462 RichEditANSIWndProc(editor
->hWnd
, EM_GETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
) &default_format
);
3463 link
.cbSize
= sizeof(link
);
3464 link
.dwMask
= CFM_LINK
;
3465 link
.dwEffects
= CFE_LINK
;
3466 curf_ef
= cur_format
.dwEffects
& link
.dwEffects
;
3467 def_ef
= default_format
.dwEffects
& link
.dwEffects
;
3468 link_ef
= link
.dwEffects
& link
.dwEffects
;
3469 if (curf_ef
== link_ef
)
3471 if( curChar
== '\n' || curChar
=='\r' || curChar
==' ')
3473 ME_SetSelection(editor
, car_pos
, car_pos
);
3474 RichEditANSIWndProc(editor
->hWnd
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
) &default_format
);
3479 if (curf_ef
== def_ef
)
3482 prefx_cnt
= (sizeof(prefixes
)/sizeof(struct prefix_s
))-1;
3483 while (cur_prefx
<=prefx_cnt
)
3487 ft
.lpstrText
= prefixes
[cur_prefx
].text
;
3488 URLmin
=max(0,(car_pos
-prefixes
[cur_prefx
].length
));
3489 URLmax
=max(0, car_pos
);
3490 if ((car_pos
== 0) && (ME_GetTextLength(editor
) != 0))
3492 URLmax
= ME_GetTextLength(editor
)+1;
3494 ft
.chrg
.cpMin
= URLmin
;
3495 ft
.chrg
.cpMax
= URLmax
;
3496 text_pos
=RichEditANSIWndProc(editor
->hWnd
, EM_FINDTEXT
, FR_DOWN
, (LPARAM
)&ft
);
3502 url
.cpMax
=car_pos
-1;
3503 ME_SetCharFormat(editor
, text_pos
, (URLmax
-text_pos
), &link
);
3504 ME_RewrapRepaint(editor
);