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
.wBorderSpace
= 0;
463 fmt
.bLineSpacingRule
= 0;
464 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
465 fmt
.dyLineSpacing
= 0;
466 RTFFlushOutputBuffer(info
);
467 ME_GetParagraph(info
->editor
->pCursors
[0].pRun
)->member
.para
.bTable
= FALSE
;
471 ME_DisplayItem
*para
;
473 RTFFlushOutputBuffer(info
);
474 para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
475 assert(para
->member
.para
.pCells
);
476 para
->member
.para
.bTable
= TRUE
;
480 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
481 fmt
.dwMask
|= PFM_STARTINDENT
| PFM_OFFSET
;
482 fmt
.dxStartIndent
+= info
->rtfParam
;
483 fmt
.dxOffset
= -info
->rtfParam
;
486 /* we assume rtfLeftIndent is always specified before rtfFirstIndent */
487 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
488 fmt
.dwMask
|= PFM_STARTINDENT
;
489 fmt
.dxStartIndent
= info
->rtfParam
;
493 fmt
.dwMask
= PFM_RIGHTINDENT
;
494 fmt
.dxRightIndent
= info
->rtfParam
;
498 fmt
.dwMask
= PFM_ALIGNMENT
;
499 fmt
.wAlignment
= PFA_LEFT
;
502 fmt
.dwMask
= PFM_ALIGNMENT
;
503 fmt
.wAlignment
= PFA_RIGHT
;
506 fmt
.dwMask
= PFM_ALIGNMENT
;
507 fmt
.wAlignment
= PFA_CENTER
;
510 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
511 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
513 fmt
.dwMask
|= PFM_TABSTOPS
;
516 if (fmt
.cTabCount
< MAX_TAB_STOPS
)
517 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
520 fmt
.dwMask
= PFM_KEEP
;
521 fmt
.wEffects
= PFE_KEEP
;
523 case rtfNoWidowControl
:
524 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
525 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
528 fmt
.dwMask
= PFM_KEEPNEXT
;
529 fmt
.wEffects
= PFE_KEEPNEXT
;
532 fmt
.dwMask
= PFM_SPACEAFTER
;
533 fmt
.dySpaceAfter
= info
->rtfParam
;
536 fmt
.dwMask
= PFM_SPACEBEFORE
;
537 fmt
.dySpaceBefore
= info
->rtfParam
;
539 case rtfSpaceBetween
:
540 fmt
.dwMask
= PFM_LINESPACING
;
541 if ((int)info
->rtfParam
> 0)
543 fmt
.dyLineSpacing
= info
->rtfParam
;
544 fmt
.bLineSpacingRule
= 3;
548 fmt
.dyLineSpacing
= info
->rtfParam
;
549 fmt
.bLineSpacingRule
= 4;
551 case rtfSpaceMultiply
:
552 fmt
.dwMask
= PFM_LINESPACING
;
553 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
554 fmt
.bLineSpacingRule
= 5;
557 fmt
.dwMask
= PFM_NUMBERING
;
558 fmt
.wNumbering
= PFN_BULLET
;
561 fmt
.dwMask
= PFM_NUMBERING
;
562 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
564 case rtfParNumDecimal
:
565 fmt
.dwMask
= PFM_NUMBERING
;
566 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
568 case rtfParNumIndent
:
569 fmt
.dwMask
= PFM_NUMBERINGTAB
;
570 fmt
.wNumberingTab
= info
->rtfParam
;
572 case rtfParNumStartAt
:
573 fmt
.dwMask
= PFM_NUMBERINGSTART
;
574 fmt
.wNumberingStart
= info
->rtfParam
;
577 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
578 if (!(fmt
.dwMask
& PFM_BORDER
))
580 fmt
.dwMask
|= PFM_BORDER
;
581 fmt
.wBorderSpace
= 0;
582 fmt
.wBorderWidth
= 1;
588 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
589 if (!(fmt
.dwMask
& PFM_BORDER
))
591 fmt
.dwMask
|= PFM_BORDER
;
592 fmt
.wBorderSpace
= 0;
593 fmt
.wBorderWidth
= 1;
599 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
600 if (!(fmt
.dwMask
& PFM_BORDER
))
602 fmt
.dwMask
|= PFM_BORDER
;
603 fmt
.wBorderSpace
= 0;
604 fmt
.wBorderWidth
= 1;
609 case rtfBorderBottom
:
610 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
611 if (!(fmt
.dwMask
& PFM_BORDER
))
613 fmt
.dwMask
|= PFM_BORDER
;
614 fmt
.wBorderSpace
= 0;
615 fmt
.wBorderWidth
= 1;
620 case rtfBorderSingle
:
621 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
622 /* we assume that borders have been created before (RTF spec) */
623 fmt
.wBorders
&= ~0x70;
624 fmt
.wBorders
|= 1 << 8;
627 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
628 /* we assume that borders have been created before (RTF spec) */
629 fmt
.wBorders
&= ~0x70;
630 fmt
.wBorders
|= 2 << 8;
632 case rtfBorderShadow
:
633 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
634 /* we assume that borders have been created before (RTF spec) */
635 fmt
.wBorders
&= ~0x70;
636 fmt
.wBorders
|= 10 << 8;
638 case rtfBorderDouble
:
639 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
640 /* we assume that borders have been created before (RTF spec) */
641 fmt
.wBorders
&= ~0x70;
642 fmt
.wBorders
|= 7 << 8;
645 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
646 /* we assume that borders have been created before (RTF spec) */
647 fmt
.wBorders
&= ~0x70;
648 fmt
.wBorders
|= 11 << 8;
651 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
652 /* we assume that borders have been created before (RTF spec) */
653 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
656 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
657 /* we assume that borders have been created before (RTF spec) */
658 fmt
.wBorderSpace
= info
->rtfParam
;
662 RTFFlushOutputBuffer(info
);
663 /* FIXME too slow ? how come ?*/
664 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
668 static void ME_RTFTblAttrHook(RTF_Info
*info
)
670 ME_DisplayItem
*para
;
672 switch (info
->rtfMinor
)
675 RTFFlushOutputBuffer(info
);
676 para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
678 /* Release possibly inherited cell definitions */
679 ME_DestroyTableCellList(para
);
681 para
->member
.para
.pCells
= ALLOC_OBJ(ME_TableCell
);
682 para
->member
.para
.pCells
->nRightBoundary
= 0;
683 para
->member
.para
.pCells
->next
= NULL
;
684 para
->member
.para
.pLastCell
= para
->member
.para
.pCells
;
687 RTFFlushOutputBuffer(info
);
688 para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
690 if (para
->member
.para
.pLastCell
->nRightBoundary
)
692 ME_TableCell
*pCell
= ALLOC_OBJ(ME_TableCell
);
695 para
->member
.para
.pLastCell
->next
= pCell
;
696 para
->member
.para
.pLastCell
= pCell
;
698 para
->member
.para
.pLastCell
->nRightBoundary
= info
->rtfParam
;
703 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
706 LPOLEOBJECT lpObject
= NULL
;
707 LPSTORAGE lpStorage
= NULL
;
708 LPOLECLIENTSITE lpClientSite
= NULL
;
709 LPDATAOBJECT lpDataObject
= NULL
;
710 LPOLECACHE lpOleCache
= NULL
;
719 stgm
.tymed
= TYMED_ENHMF
;
720 stgm
.u
.hEnhMetaFile
= hemf
;
721 fm
.cfFormat
= CF_ENHMETAFILE
;
725 stgm
.tymed
= TYMED_GDI
;
726 stgm
.u
.hBitmap
= hbmp
;
727 fm
.cfFormat
= CF_BITMAP
;
729 stgm
.pUnkForRelease
= NULL
;
732 fm
.dwAspect
= DVASPECT_CONTENT
;
734 fm
.tymed
= stgm
.tymed
;
736 if (!info
->lpRichEditOle
)
738 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
741 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
743 /* FIXME: enable it when rich-edit properly implements this method */
744 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
745 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
747 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
748 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
749 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
750 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
751 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
755 reobject
.cbStruct
= sizeof(reobject
);
756 reobject
.cp
= REO_CP_SELECTION
;
757 reobject
.clsid
= clsid
;
758 reobject
.poleobj
= lpObject
;
759 reobject
.pstg
= lpStorage
;
760 reobject
.polesite
= lpClientSite
;
761 /* convert from twips to .01 mm */
762 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
763 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
764 reobject
.dvaspect
= DVASPECT_CONTENT
;
765 reobject
.dwFlags
= 0; /* FIXME */
768 /* FIXME: could be simpler */
769 ret
= IRichEditOle_InsertObject(info
->lpRichEditOle
, &reobject
) == S_OK
;
772 if (lpObject
) IOleObject_Release(lpObject
);
773 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
774 if (lpStorage
) IStorage_Release(lpStorage
);
775 if (lpDataObject
) IDataObject_Release(lpDataObject
);
776 if (lpOleCache
) IOleCache_Release(lpOleCache
);
781 static void ME_RTFReadPictGroup(RTF_Info
*info
)
785 unsigned bufsz
, bufidx
;
791 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
794 if (info
->rtfClass
== rtfEOF
)
797 /* fetch picture type */
798 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
800 mfp
.mm
= info
->rtfParam
;
803 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
805 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
808 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
810 gfx
= gfx_enhmetafile
;
814 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
818 /* fetch picture attributes */
822 if (info
->rtfClass
== rtfEOF
)
824 if (info
->rtfClass
== rtfText
)
826 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
828 ERR("Expected picture attribute (%d %d)\n",
829 info
->rtfClass
, info
->rtfMajor
);
832 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
834 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
836 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
838 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
840 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
841 sz
.cx
= info
->rtfParam
;
842 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
843 sz
.cy
= info
->rtfParam
;
845 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
847 /* fetch picture data */
850 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
851 val
= info
->rtfMajor
;
852 for (flip
= TRUE
;; flip
= !flip
)
855 if (info
->rtfClass
== rtfEOF
)
857 HeapFree(GetProcessHeap(), 0, buffer
);
858 return; /* Warn ?? */
860 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
862 if (info
->rtfClass
!= rtfText
) goto skip_group
;
865 if (bufidx
>= bufsz
&&
866 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
868 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
871 val
= info
->rtfMajor
;
873 if (flip
) FIXME("wrong hex string\n");
877 case gfx_enhmetafile
:
878 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
879 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
882 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
883 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
887 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
889 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
891 /* not quite right, especially for bitfields type of compression */
892 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
893 nc
= 1 << bi
->bmiHeader
.biBitCount
;
894 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
895 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
896 bi
, DIB_RGB_COLORS
)))
897 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
904 HeapFree(GetProcessHeap(), 0, buffer
);
905 RTFRouteToken (info
); /* feed "}" back to router */
908 HeapFree(GetProcessHeap(), 0, buffer
);
910 RTFRouteToken(info
); /* feed "}" back to router */
913 /* for now, lookup the \result part and use it, whatever the object */
914 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
919 if (info
->rtfClass
== rtfEOF
)
921 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
923 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
926 if (info
->rtfClass
== rtfEOF
)
928 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
932 while (RTFGetToken (info
) != rtfEOF
)
934 if (info
->rtfClass
== rtfGroup
)
936 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
937 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
942 else RTFSkipGroup(info
);
945 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
947 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
951 RTFRouteToken(info
); /* feed "}" back to router */
954 static void ME_RTFReadHook(RTF_Info
*info
) {
955 switch(info
->rtfClass
)
958 switch(info
->rtfMajor
)
961 if (info
->stackTop
< maxStack
) {
962 info
->stack
[info
->stackTop
].fmt
= info
->style
->fmt
;
963 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
964 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
967 info
->styleChanged
= FALSE
;
972 RTFFlushOutputBuffer(info
);
973 if (info
->stackTop
<=1) {
974 info
->rtfClass
= rtfEOF
;
978 assert(info
->stackTop
>= 0);
979 if (info
->styleChanged
)
981 /* FIXME too slow ? how come ? */
982 s
= ME_ApplyStyle(info
->style
, &info
->stack
[info
->stackTop
].fmt
);
983 ME_ReleaseStyle(info
->style
);
985 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
986 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
993 switch(info
->rtfMajor
)
996 ME_RTFCharAttrHook(info
);
999 ME_RTFParAttrHook(info
);
1002 ME_RTFTblAttrHook(info
);
1004 case rtfSpecialChar
:
1005 if (info
->rtfMinor
== rtfCell
)
1007 RTFFlushOutputBuffer(info
);
1008 ME_InsertTableCellFromCursor(info
->editor
, 0);
1016 ME_StreamInFill(ME_InStream
*stream
)
1018 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1019 (BYTE
*)stream
->buffer
,
1020 sizeof(stream
->buffer
),
1021 (LONG
*)&stream
->dwSize
);
1025 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
)
1029 int from
, to
, to2
, nUndoMode
;
1030 int nEventMask
= editor
->nEventMask
;
1031 ME_InStream inStream
;
1033 TRACE("stream==%p hWnd==%p format==0x%X\n", stream
, editor
->hWnd
, format
);
1034 editor
->nEventMask
= 0;
1036 ME_GetSelection(editor
, &from
, &to
);
1037 if ((format
& SFF_SELECTION
) && (editor
->mode
& TM_RICHTEXT
)) {
1038 style
= ME_GetSelectionInsertStyle(editor
);
1040 ME_InternalDeleteText(editor
, from
, to
-from
);
1043 style
= editor
->pBuffer
->pDefaultStyle
;
1044 ME_AddRefStyle(style
);
1045 SendMessageA(editor
->hWnd
, EM_SETSEL
, 0, 0);
1046 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
1048 ME_ClearTempStyle(editor
);
1049 /* FIXME restore default paragraph formatting ! */
1053 /* Back up undo mode to a local variable */
1054 nUndoMode
= editor
->nUndoMode
;
1056 /* Only create an undo if SFF_SELECTION is set */
1057 if (!(format
& SFF_SELECTION
))
1058 editor
->nUndoMode
= umIgnore
;
1060 inStream
.editstream
= stream
;
1061 inStream
.editstream
->dwError
= 0;
1062 inStream
.dwSize
= 0;
1063 inStream
.dwUsed
= 0;
1065 if (format
& SF_RTF
)
1067 /* Check if it's really RTF, and if it is not, use plain text */
1068 ME_StreamInFill(&inStream
);
1069 if (!inStream
.editstream
->dwError
)
1071 if (strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1079 if (!inStream
.editstream
->dwError
)
1081 if (format
& SF_RTF
) {
1082 /* setup the RTF parser */
1083 memset(&parser
, 0, sizeof parser
);
1084 RTFSetEditStream(&parser
, &inStream
);
1085 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1086 parser
.hwndEdit
= editor
->hWnd
;
1087 parser
.editor
= editor
;
1088 parser
.style
= style
;
1089 WriterInit(&parser
);
1091 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1092 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1093 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1096 /* do the parsing */
1098 RTFFlushOutputBuffer(&parser
);
1099 RTFDestroy(&parser
);
1100 if (parser
.lpRichEditOle
)
1101 IRichEditOle_Release(parser
.lpRichEditOle
);
1103 style
= parser
.style
;
1105 else if (format
& SF_TEXT
)
1106 ME_StreamInText(editor
, format
, &inStream
, style
);
1108 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1109 ME_GetSelection(editor
, &to
, &to2
);
1110 /* put the cursor at the top */
1111 if (!(format
& SFF_SELECTION
))
1112 SendMessageA(editor
->hWnd
, EM_SETSEL
, 0, 0);
1115 /* Restore saved undo mode */
1116 editor
->nUndoMode
= nUndoMode
;
1118 /* even if we didn't add an undo, we need to commit anything on the stack */
1119 ME_CommitUndo(editor
);
1121 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1122 if (!(format
& SFF_SELECTION
))
1123 ME_EmptyUndoStack(editor
);
1125 ME_ReleaseStyle(style
);
1126 editor
->nEventMask
= nEventMask
;
1127 if (editor
->bRedraw
)
1129 ME_UpdateRepaint(editor
);
1131 if (!(format
& SFF_SELECTION
)) {
1132 ME_ClearTempStyle(editor
);
1134 ME_MoveCaret(editor
);
1135 ME_SendSelChange(editor
);
1136 ME_SendRequestResize(editor
, FALSE
);
1142 typedef struct tagME_RTFStringStreamStruct
1147 } ME_RTFStringStreamStruct
;
1149 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1151 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1154 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1155 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1156 pStruct
->pos
+= count
;
1162 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1165 ME_RTFStringStreamStruct data
;
1167 data
.string
= string
;
1168 data
.length
= strlen(string
);
1170 es
.dwCookie
= (DWORD
)&data
;
1171 es
.pfnCallback
= ME_ReadFromRTFString
;
1172 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
);
1177 ME_FindItemAtOffset(ME_TextEditor
*editor
, ME_DIType nItemType
, int nOffset
, int *nItemOffset
)
1179 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
1181 while (item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= nOffset
)
1182 item
= ME_FindItemFwd(item
, diParagraph
);
1187 nOffset
-= item
->member
.para
.nCharOfs
;
1188 if (nItemType
== diParagraph
) {
1190 *nItemOffset
= nOffset
;
1195 item
= ME_FindItemFwd(item
, diRun
);
1196 } while (item
&& (item
->member
.run
.nCharOfs
+ ME_StrLen(item
->member
.run
.strText
) <= nOffset
));
1198 nOffset
-= item
->member
.run
.nCharOfs
;
1200 *nItemOffset
= nOffset
;
1207 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1209 const int nLen
= lstrlenW(text
);
1210 const int nTextLen
= ME_GetTextLength(editor
);
1213 ME_DisplayItem
*item
;
1214 ME_DisplayItem
*para
;
1215 WCHAR wLastChar
= ' ';
1217 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1218 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1220 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1221 FIXME("Flags 0x%08x not implemented\n",
1222 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1225 if (chrg
->cpMax
== -1)
1228 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1230 /* when searching up, if cpMin < cpMax, then instead of searching
1231 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1232 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1233 * case, it is always bigger than cpMin.
1235 if (!(flags
& FR_DOWN
))
1239 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1240 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1246 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1249 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1253 if (flags
& FR_DOWN
) /* Forward search */
1255 /* If possible, find the character before where the search starts */
1256 if ((flags
& FR_WHOLEWORD
) && nMin
)
1259 item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1263 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1266 wLastChar
= item
->member
.run
.strText
->szData
[nStart
];
1270 item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1274 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1278 para
= ME_GetParagraph(item
);
1280 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
+ nLen
<= nMax
)
1282 ME_DisplayItem
*pCurItem
= item
;
1283 int nCurStart
= nStart
;
1286 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
], text
[nMatched
], (flags
& FR_MATCHCASE
)))
1288 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1292 if (nMatched
== nLen
)
1294 ME_DisplayItem
*pNextItem
= pCurItem
;
1295 int nNextStart
= nCurStart
;
1298 /* Check to see if next character is a whitespace */
1299 if (flags
& FR_WHOLEWORD
)
1301 if (nCurStart
+ nMatched
== ME_StrLen(pCurItem
->member
.run
.strText
))
1303 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1304 nNextStart
= -nMatched
;
1308 wNextChar
= pNextItem
->member
.run
.strText
->szData
[nNextStart
+ nMatched
];
1312 if (isalnumW(wNextChar
))
1316 nStart
+= para
->member
.para
.nCharOfs
+ pCurItem
->member
.run
.nCharOfs
;
1319 chrgText
->cpMin
= nStart
;
1320 chrgText
->cpMax
= nStart
+ nLen
;
1322 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1325 if (nCurStart
+ nMatched
== ME_StrLen(pCurItem
->member
.run
.strText
))
1327 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1328 para
= ME_GetParagraph(pCurItem
);
1329 nCurStart
= -nMatched
;
1333 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
];
1338 if (nStart
== ME_StrLen(item
->member
.run
.strText
))
1340 item
= ME_FindItemFwd(item
, diRun
);
1341 para
= ME_GetParagraph(item
);
1346 else /* Backward search */
1348 /* If possible, find the character after where the search ends */
1349 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1352 item
= ME_FindItemAtOffset(editor
, diRun
, nEnd
, &nEnd
);
1356 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1359 wLastChar
= item
->member
.run
.strText
->szData
[nEnd
];
1363 item
= ME_FindItemAtOffset(editor
, diRun
, nEnd
, &nEnd
);
1367 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1371 para
= ME_GetParagraph(item
);
1374 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nEnd
- nLen
>= nMin
)
1376 ME_DisplayItem
*pCurItem
= item
;
1380 if (nCurEnd
- nMatched
== 0)
1382 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1383 para
= ME_GetParagraph(pCurItem
);
1384 nCurEnd
= ME_StrLen(pCurItem
->member
.run
.strText
) + nMatched
;
1387 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1], text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
)))
1389 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1393 if (nMatched
== nLen
)
1395 ME_DisplayItem
*pPrevItem
= pCurItem
;
1396 int nPrevEnd
= nCurEnd
;
1399 /* Check to see if previous character is a whitespace */
1400 if (flags
& FR_WHOLEWORD
)
1402 if (nPrevEnd
- nMatched
== 0)
1404 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1406 nPrevEnd
= ME_StrLen(pPrevItem
->member
.run
.strText
) + nMatched
;
1410 wPrevChar
= pPrevItem
->member
.run
.strText
->szData
[nPrevEnd
- nMatched
- 1];
1414 if (isalnumW(wPrevChar
))
1418 nStart
= para
->member
.para
.nCharOfs
+ pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1421 chrgText
->cpMin
= nStart
;
1422 chrgText
->cpMax
= nStart
+ nLen
;
1424 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1427 if (nCurEnd
- nMatched
== 0)
1429 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1430 /* Don't care about pCurItem becoming NULL here; it's already taken
1431 * care of in the exterior loop condition */
1432 para
= ME_GetParagraph(pCurItem
);
1433 nCurEnd
= ME_StrLen(pCurItem
->member
.run
.strText
) + nMatched
;
1437 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1];
1444 item
= ME_FindItemBack(item
, diRun
);
1445 para
= ME_GetParagraph(item
);
1446 nEnd
= ME_StrLen(item
->member
.run
.strText
);
1450 TRACE("not found\n");
1452 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1456 /* helper to send a msg filter notification */
1458 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
1462 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
1463 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
1464 msgf
.nmhdr
.code
= EN_MSGFILTER
;
1467 msgf
.wParam
= *wParam
;
1468 msgf
.lParam
= *lParam
;
1469 if (SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
1471 *wParam
= msgf
.wParam
;
1472 *lParam
= msgf
.lParam
;
1473 msgf
.wParam
= *wParam
;
1479 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
1481 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
1482 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
1494 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
1498 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1499 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
)
1501 if (ME_IsSelection(editor
))
1502 ME_DeleteSelection(editor
);
1503 else if (nKey
== VK_DELETE
|| ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
1504 ME_DeleteTextAtCursor(editor
, 1, 1);
1507 ME_CommitUndo(editor
);
1508 ME_UpdateRepaint(editor
);
1509 ME_SendRequestResize(editor
, FALSE
);
1519 chf
.cbSize
= sizeof(chf
);
1521 ME_GetSelectionCharFormat(editor
, &chf
);
1522 ME_DumpStyleToBuf(&chf
, buf
);
1523 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
1527 ME_CheckCharOffsets(editor
);
1534 static BOOL
ME_SetCursor(ME_TextEditor
*editor
, int x
)
1536 if ((GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_SELECTIONBAR
) &&
1537 (x
< editor
->selofs
|| editor
->linesel
))
1545 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
1550 if(!editor
->lpOleCallback
)
1552 ME_GetSelection(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
1553 if(selrange
.cpMin
== selrange
.cpMax
)
1554 seltype
|= SEL_EMPTY
;
1557 /* FIXME: Handle objects */
1558 seltype
|= SEL_TEXT
;
1559 if(selrange
.cpMax
-selrange
.cpMin
> 1)
1560 seltype
|= SEL_MULTICHAR
;
1562 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
1564 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, GetParent(editor
->hWnd
), NULL
);
1570 ME_TextEditor
*ME_MakeEditor(HWND hWnd
) {
1571 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
1574 ed
->bEmulateVersion10
= FALSE
;
1575 ed
->pBuffer
= ME_MakeText();
1576 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
1577 ME_MakeFirstParagraph(ed
);
1578 ed
->bCaretShown
= FALSE
;
1580 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
1581 ed
->pCursors
[0].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
1582 ed
->pCursors
[0].nOffset
= 0;
1583 ed
->pCursors
[1].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
1584 ed
->pCursors
[1].nOffset
= 0;
1585 ed
->pCursors
[2] = ed
->pCursors
[0];
1586 ed
->pCursors
[3] = ed
->pCursors
[1];
1587 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
1591 ed
->rgbBackColor
= -1;
1592 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
1593 ed
->bCaretAtEnd
= FALSE
;
1595 ed
->nModifyStep
= 0;
1596 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
1597 ed
->pUndoStack
= ed
->pRedoStack
= ed
->pUndoStackBottom
= NULL
;
1598 ed
->nUndoStackSize
= 0;
1599 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
1600 ed
->nUndoMode
= umAddToUndo
;
1601 ed
->nParagraphs
= 1;
1602 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
1603 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diParagraph
);
1605 ed
->bWordWrap
= (GetWindowLongW(hWnd
, GWL_STYLE
) & WS_HSCROLL
) ? FALSE
: TRUE
;
1606 ed
->bHideSelection
= FALSE
;
1607 ed
->nInvalidOfs
= -1;
1608 ed
->pfnWordBreak
= NULL
;
1609 ed
->lpOleCallback
= NULL
;
1610 ed
->mode
= TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
1611 ed
->AutoURLDetect_bEnable
= FALSE
;
1612 ed
->bHaveFocus
= FALSE
;
1613 GetClientRect(hWnd
, &ed
->rcFormat
);
1614 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
1616 ed
->pFontCache
[i
].nRefs
= 0;
1617 ed
->pFontCache
[i
].nAge
= 0;
1618 ed
->pFontCache
[i
].hFont
= NULL
;
1621 ME_CheckCharOffsets(ed
);
1622 if (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_SELECTIONBAR
)
1628 if (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_PASSWORD
)
1629 ed
->cPasswordMask
= '*';
1631 ed
->cPasswordMask
= 0;
1636 typedef struct tagME_GlobalDestStruct
1640 } ME_GlobalDestStruct
;
1642 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1644 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1649 pDest
= (WORD
*)lpBuff
;
1650 pSrc
= (WORD
*)GlobalLock(pData
->hData
);
1651 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1652 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1654 pData
->nLength
+= i
;
1656 GlobalUnlock(pData
->hData
);
1660 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1662 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1667 pSrc
= (BYTE
*)GlobalLock(pData
->hData
);
1668 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1669 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1671 pData
->nLength
+= i
;
1673 GlobalUnlock(pData
->hData
);
1678 void ME_DestroyEditor(ME_TextEditor
*editor
)
1680 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
1681 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
1684 ME_ClearTempStyle(editor
);
1685 ME_EmptyUndoStack(editor
);
1688 ME_DestroyDisplayItem(p
);
1691 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
1692 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
1694 if (editor
->pFontCache
[i
].hFont
)
1695 DeleteObject(editor
->pFontCache
[i
].hFont
);
1697 if (editor
->rgbBackColor
!= -1)
1698 DeleteObject(editor
->hbrBackground
);
1699 if(editor
->lpOleCallback
)
1700 IUnknown_Release(editor
->lpOleCallback
);
1702 FREE_OBJ(editor
->pBuffer
);
1703 FREE_OBJ(editor
->pCursors
);
1708 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1713 case DLL_PROCESS_ATTACH
:
1714 DisableThreadLibraryCalls(hinstDLL
);
1715 me_heap
= HeapCreate (0, 0x10000, 0);
1716 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
1717 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
1721 case DLL_PROCESS_DETACH
:
1722 UnregisterClassW(RichEdit20W
, 0);
1723 UnregisterClassW(RichEdit50W
, 0);
1724 UnregisterClassA("RichEdit20A", 0);
1725 UnregisterClassA("RichEdit50A", 0);
1726 if (ME_ListBoxRegistered
)
1727 UnregisterClassW(REListBox20W
, 0);
1728 if (ME_ComboBoxRegistered
)
1729 UnregisterClassW(REComboBox20W
, 0);
1731 HeapDestroy (me_heap
);
1739 #define UNSUPPORTED_MSG(e) \
1741 FIXME(#e ": stub\n"); \
1742 return DefWindowProcW(hWnd, msg, wParam, lParam);
1744 static const char * const edit_messages
[] = {
1773 "EM_SETPASSWORDCHAR",
1774 "EM_EMPTYUNDOBUFFER",
1775 "EM_GETFIRSTVISIBLELINE",
1777 "EM_SETWORDBREAKPROC",
1778 "EM_GETWORDBREAKPROC",
1779 "EM_GETPASSWORDCHAR",
1787 static const char * const richedit_messages
[] = {
1792 "EM_EXLINEFROMCHAR",
1798 "EM_GETOLEINTERFACE",
1808 "EM_SETOLECALLBACK",
1810 "EM_SETTARGETDEVICE",
1818 "EM_GETWORDBREAKPROCEX",
1819 "EM_SETWORDBREAKPROCEX",
1821 "EM_UNKNOWN_USER_83",
1826 "EM_STOPGROUPTYPING",
1830 "EM_GETAUTOURLDETECT",
1833 "EM_GETTEXTLENGTHEX",
1836 "EM_UNKNOWN_USER_98",
1837 "EM_UNKNOWN_USER_99",
1838 "EM_SETPUNCTUATION",
1839 "EM_GETPUNCTUATION",
1840 "EM_SETWORDWRAPMODE",
1841 "EM_GETWORDWRAPMODE",
1847 "EM_UNKNOWN_USER_109",
1848 "EM_UNKNOWN_USER_110",
1849 "EM_UNKNOWN_USER_111",
1850 "EM_UNKNOWN_USER_112",
1851 "EM_UNKNOWN_USER_113",
1852 "EM_UNKNOWN_USER_114",
1853 "EM_UNKNOWN_USER_115",
1854 "EM_UNKNOWN_USER_116",
1855 "EM_UNKNOWN_USER_117",
1856 "EM_UNKNOWN_USER_118",
1857 "EM_UNKNOWN_USER_119",
1858 "EM_SETLANGOPTIONS",
1859 "EM_GETLANGOPTIONS",
1860 "EM_GETIMECOMPMODE",
1864 "EM_SETIMEMODEBIAS",
1869 get_msg_name(UINT msg
)
1871 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
1872 return edit_messages
[msg
- EM_GETSEL
];
1873 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
1874 return richedit_messages
[msg
- EM_CANPASTE
];
1878 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
1879 LPARAM lParam
, BOOL unicode
)
1881 ME_TextEditor
*editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
1883 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1884 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
1886 if (!editor
&& msg
!= WM_NCCREATE
&& msg
!= WM_NCDESTROY
) {
1887 ERR("called with invalid hWnd %p - application bug?\n", hWnd
);
1893 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
1894 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
1895 UNSUPPORTED_MSG(EM_FMTLINES
)
1896 UNSUPPORTED_MSG(EM_FORMATRANGE
)
1897 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
1898 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
1899 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
1900 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1901 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
1902 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1903 UNSUPPORTED_MSG(EM_GETREDONAME
)
1904 UNSUPPORTED_MSG(EM_GETTEXTMODE
)
1905 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
1906 UNSUPPORTED_MSG(EM_GETUNDONAME
)
1907 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
1908 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
1909 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
1910 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
1911 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
1912 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
1913 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
1914 UNSUPPORTED_MSG(EM_SETPALETTE
)
1915 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
1916 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
1917 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
1918 UNSUPPORTED_MSG(WM_STYLECHANGING
)
1919 UNSUPPORTED_MSG(WM_STYLECHANGED
)
1920 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1922 /* Messages specific to Richedit controls */
1925 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
);
1927 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
1930 UINT code
= DLGC_WANTCHARS
|DLGC_WANTARROWS
;
1931 if(lParam
&& (((LPMSG
)lParam
)->message
== WM_KEYDOWN
))
1933 int vk
= (int)((LPMSG
)lParam
)->wParam
;
1934 /* if style says we want return key */
1935 if((vk
== VK_RETURN
) && (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_WANTRETURN
))
1937 code
|= DLGC_WANTMESSAGE
;
1939 /* we always handle ctrl-tab */
1940 if((vk
== VK_TAB
) && (GetKeyState(VK_CONTROL
) & 0x8000))
1942 code
|= DLGC_WANTMESSAGE
;
1949 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
1950 TRACE("WM_NCCREATE: style 0x%08x\n", pcs
->style
);
1951 editor
= ME_MakeEditor(hWnd
);
1952 SetWindowLongPtrW(hWnd
, 0, (LONG_PTR
)editor
);
1955 case EM_EMPTYUNDOBUFFER
:
1956 ME_EmptyUndoStack(editor
);
1960 /* Note: wParam/lParam can be NULL */
1962 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
1963 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
1964 ME_GetSelection(editor
, (int *)pfrom
, (int *)pto
);
1965 if ((*pfrom
|*pto
) & 0xFFFF0000)
1967 return MAKELONG(*pfrom
,*pto
);
1971 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
1972 ME_GetSelection(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
1973 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
1976 case EM_SETUNDOLIMIT
:
1978 if ((int)wParam
< 0)
1979 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
1981 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
1982 /* Setting a max stack size keeps wine from getting killed
1983 for hogging memory. Windows allocates all this memory at once, so
1984 no program would realistically set a value above our maximum. */
1985 return editor
->nUndoLimit
;
1988 return editor
->pUndoStack
!= NULL
;
1990 return editor
->pRedoStack
!= NULL
;
1991 case WM_UNDO
: /* FIXME: actually not the same */
2000 /* these flags are equivalent to the ES_* counterparts */
2001 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
2002 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
;
2003 DWORD settings
= GetWindowLongW(hWnd
, GWL_STYLE
) & mask
;
2009 /* these flags are equivalent to ES_* counterparts
2010 * ECO_READONLY is already implemented in the code, only requires
2011 * setting the bit to work
2013 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
2014 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
2015 DWORD raw
= GetWindowLongW(hWnd
, GWL_STYLE
);
2016 DWORD settings
= mask
& raw
;
2032 SetWindowLongW(hWnd
, GWL_STYLE
, (raw
& ~mask
) | (settings
& mask
));
2034 if (settings
& ECO_AUTOWORDSELECTION
)
2035 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
2036 if (settings
& ECO_SELECTIONBAR
)
2037 editor
->selofs
= 16;
2040 ME_WrapMarkedParagraphs(editor
);
2042 if (settings
& ECO_VERTICAL
)
2043 FIXME("ECO_VERTICAL not implemented yet!\n");
2044 if (settings
& ECO_AUTOHSCROLL
)
2045 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
2046 if (settings
& ECO_AUTOVSCROLL
)
2047 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
2048 if (settings
& ECO_NOHIDESEL
)
2049 FIXME("ECO_NOHIDESEL not implemented yet!\n");
2050 if (settings
& ECO_WANTRETURN
)
2051 FIXME("ECO_WANTRETURN not implemented yet!\n");
2057 ME_InvalidateSelection(editor
);
2058 ME_SetSelection(editor
, wParam
, lParam
);
2059 ME_InvalidateSelection(editor
);
2060 ME_SendSelChange(editor
);
2063 case EM_SETSCROLLPOS
:
2065 POINT
*point
= (POINT
*)lParam
;
2066 ME_ScrollAbs(editor
, point
->y
);
2069 case EM_AUTOURLDETECT
:
2071 if (wParam
==1 || wParam
==0)
2073 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
2076 return E_INVALIDARG
;
2078 case EM_GETAUTOURLDETECT
:
2080 return editor
->AutoURLDetect_bEnable
;
2085 CHARRANGE range
= *(CHARRANGE
*)lParam
;
2087 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
2089 ME_InvalidateSelection(editor
);
2090 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
2091 ME_InvalidateSelection(editor
);
2092 ME_SendSelChange(editor
);
2096 case EM_SHOWSCROLLBAR
:
2098 ShowScrollBar(editor
->hWnd
, wParam
, lParam
);
2104 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
2108 int oldModify
= editor
->nModifyStep
;
2110 if (!pStruct
) return 0;
2112 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2113 pStruct
->codepage
== 1200 ? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
2114 pStruct
->flags
, pStruct
->codepage
);
2116 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2117 wszText
= lParam
? ME_ToUnicode(pStruct
->codepage
== 1200, (void *)lParam
) : NULL
;
2118 len
= wszText
? lstrlenW(wszText
) : 0;
2120 /* FIXME: this should support RTF strings too, according to MSDN */
2121 if (pStruct
->flags
& ST_SELECTION
) {
2122 ME_GetSelection(editor
, &from
, &to
);
2123 style
= ME_GetSelectionInsertStyle(editor
);
2124 ME_InternalDeleteText(editor
, from
, to
- from
);
2125 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
2126 ME_ReleaseStyle(style
);
2129 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
2130 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
2133 ME_CommitUndo(editor
);
2134 if (!(pStruct
->flags
& ST_KEEPUNDO
))
2136 editor
->nModifyStep
= oldModify
;
2137 ME_EmptyUndoStack(editor
);
2139 ME_UpdateRepaint(editor
);
2142 case EM_SETBKGNDCOLOR
:
2145 if (editor
->rgbBackColor
!= -1) {
2146 DeleteObject(editor
->hbrBackground
);
2147 lColor
= editor
->rgbBackColor
;
2149 else lColor
= GetSysColor(COLOR_WINDOW
);
2153 editor
->rgbBackColor
= -1;
2154 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2158 editor
->rgbBackColor
= lParam
;
2159 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
2161 if (editor
->bRedraw
)
2163 InvalidateRect(hWnd
, NULL
, TRUE
);
2169 return editor
->nModifyStep
== 0 ? 0 : 1;
2173 editor
->nModifyStep
= 1;
2175 editor
->nModifyStep
= 0;
2179 case EM_SETREADONLY
:
2181 long nStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
2183 nStyle
|= ES_READONLY
;
2185 nStyle
&= ~ES_READONLY
;
2186 SetWindowLongW(hWnd
, GWL_STYLE
, nStyle
);
2189 case EM_SETEVENTMASK
:
2191 DWORD nOldMask
= editor
->nEventMask
;
2193 editor
->nEventMask
= lParam
;
2196 case EM_GETEVENTMASK
:
2197 return editor
->nEventMask
;
2198 case EM_SETCHARFORMAT
:
2200 CHARFORMAT2W buf
, *p
;
2201 BOOL bRepaint
= TRUE
;
2202 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
2203 if (p
== NULL
) return 0;
2205 ME_SetDefaultCharFormat(editor
, p
);
2206 else if (wParam
== (SCF_WORD
| SCF_SELECTION
)) {
2207 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2209 } else if (wParam
== SCF_ALL
) {
2210 if (editor
->mode
& TM_PLAINTEXT
)
2211 ME_SetDefaultCharFormat(editor
, p
);
2213 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), p
);
2214 } else if (editor
->mode
& TM_PLAINTEXT
) {
2218 ME_GetSelection(editor
, &from
, &to
);
2219 bRepaint
= (from
!= to
);
2220 ME_SetSelectionCharFormat(editor
, p
);
2222 editor
->nModifyStep
= 1;
2223 ME_CommitUndo(editor
);
2225 ME_RewrapRepaint(editor
);
2228 case EM_GETCHARFORMAT
:
2230 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
2231 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
2232 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
2233 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
2234 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
2236 tmp
.cbSize
= sizeof(tmp
);
2238 ME_GetDefaultCharFormat(editor
, &tmp
);
2240 ME_GetSelectionCharFormat(editor
, &tmp
);
2241 ME_CopyToCFAny(dst
, &tmp
);
2244 case EM_SETPARAFORMAT
:
2245 ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
2246 ME_RewrapRepaint(editor
);
2247 ME_CommitUndo(editor
);
2249 case EM_GETPARAFORMAT
:
2250 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
2252 case EM_GETFIRSTVISIBLELINE
:
2254 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
2255 int y
= ME_GetYScrollPos(editor
);
2260 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
2261 if (p
->type
== diTextEnd
)
2263 if (p
->type
== diParagraph
) {
2264 ypara
= p
->member
.para
.nYPos
;
2267 ystart
= ypara
+ p
->member
.row
.nYPos
;
2268 yend
= ystart
+ p
->member
.row
.nHeight
;
2276 case EM_HIDESELECTION
:
2278 editor
->bHideSelection
= (wParam
!= 0);
2279 ME_InvalidateSelection(editor
);
2284 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
2285 return TRUE
; /* Should return false if a single line richedit control */
2290 ME_GetSelection(editor
, &from
, &to
);
2291 ME_InternalDeleteText(editor
, from
, to
-from
);
2292 ME_CommitUndo(editor
);
2293 ME_UpdateRepaint(editor
);
2300 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
2301 size_t len
= wszText
? lstrlenW(wszText
) : 0;
2302 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
2304 ME_GetSelection(editor
, &from
, &to
);
2305 style
= ME_GetSelectionInsertStyle(editor
);
2306 ME_InternalDeleteText(editor
, from
, to
-from
);
2307 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
2308 ME_ReleaseStyle(style
);
2309 /* drop temporary style if line end */
2311 * FIXME question: does abc\n mean: put abc,
2312 * clear temp style, put \n? (would require a change)
2314 if (len
>0 && wszText
[len
-1] == '\n')
2315 ME_ClearTempStyle(editor
);
2316 ME_EndToUnicode(unicode
, wszText
);
2317 ME_CommitUndo(editor
);
2319 ME_EmptyUndoStack(editor
);
2320 ME_UpdateRepaint(editor
);
2323 case EM_SCROLLCARET
:
2325 int top
, bottom
; /* row's edges relative to document top */
2327 ME_DisplayItem
*para
, *row
;
2329 nPos
= ME_GetYScrollPos(editor
);
2330 row
= ME_RowStart(editor
->pCursors
[0].pRun
);
2331 para
= ME_GetParagraph(row
);
2332 top
= para
->member
.para
.nYPos
+ row
->member
.row
.nYPos
;
2333 bottom
= top
+ row
->member
.row
.nHeight
;
2335 if (top
< nPos
) /* caret above window */
2336 ME_ScrollAbs(editor
, top
);
2337 else if (nPos
+ editor
->sizeWindow
.cy
< bottom
) /*below*/
2338 ME_ScrollAbs(editor
, bottom
- editor
->sizeWindow
.cy
);
2346 BOOL bRepaint
= LOWORD(lParam
);
2349 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
2350 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
2352 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
2353 ReleaseDC(hWnd
, hDC
);
2354 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), &fmt
);
2355 ME_SetDefaultCharFormat(editor
, &fmt
);
2357 ME_CommitUndo(editor
);
2359 ME_RewrapRepaint(editor
);
2364 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
2367 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
2368 if (!unicode
&& !strncmp((char *)lParam
, "{\\rtf", 5))
2370 /* Undocumented: WM_SETTEXT supports RTF text */
2371 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
2375 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
2376 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
2377 if (lstrlenW(wszText
) > 0)
2381 /* uses default style! */
2382 if (!(GetWindowLongW(hWnd
, GWL_STYLE
) & ES_MULTILINE
))
2387 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
2390 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
2392 ME_EndToUnicode(unicode
, wszText
);
2396 TRACE("WM_SETTEXT - NULL\n");
2397 ME_CommitUndo(editor
);
2398 ME_EmptyUndoStack(editor
);
2399 ME_SetSelection(editor
, 0, 0);
2400 editor
->nModifyStep
= 0;
2401 ME_UpdateRepaint(editor
);
2406 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2407 if (IsClipboardFormatAvailable(nRTFFormat
))
2409 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2417 ME_GlobalDestStruct gds
;
2418 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2421 if (IsClipboardFormatAvailable(nRTFFormat
))
2422 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2423 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2424 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2428 if (!OpenClipboard(hWnd
))
2430 gds
.hData
= GetClipboardData(cf
);
2432 es
.dwCookie
= (DWORD
)&gds
;
2433 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2434 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
);
2442 LPDATAOBJECT dataObj
= NULL
;
2446 if (editor
->cPasswordMask
)
2447 return 0; /* Copying or Cutting masked text isn't allowed */
2449 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
2450 if(editor
->lpOleCallback
)
2451 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2452 if(FAILED(hr
) || !dataObj
)
2453 hr
= ME_GetDataObject(editor
, &range
, &dataObj
);
2455 hr
= OleSetClipboard(dataObj
);
2456 IDataObject_Release(dataObj
);
2458 if (SUCCEEDED(hr
) && msg
== WM_CUT
)
2460 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
);
2461 ME_CommitUndo(editor
);
2462 ME_UpdateRepaint(editor
);
2466 case WM_GETTEXTLENGTH
:
2468 GETTEXTLENGTHEX how
;
2470 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
2471 how
.codepage
= unicode
? 1200 : CP_ACP
;
2472 return ME_GetTextLengthEx(editor
, &how
);
2474 case EM_GETTEXTLENGTHEX
:
2475 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
2480 LPSTR bufferA
= NULL
;
2481 LPWSTR bufferW
= NULL
;
2484 bufferW
= heap_alloc((wParam
+ 2) * sizeof(WCHAR
));
2486 bufferA
= heap_alloc(wParam
+ 2);
2488 ex
.cb
= (wParam
+ 2) * (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
2489 ex
.flags
= GT_USECRLF
;
2490 ex
.codepage
= unicode
? 1200 : CP_ACP
;
2491 ex
.lpDefaultChar
= NULL
;
2492 ex
.lpUsedDefChar
= NULL
;
2493 rc
= RichEditWndProc_common(hWnd
, EM_GETTEXTEX
, (WPARAM
)&ex
, unicode
? (LPARAM
)bufferW
: (LPARAM
)bufferA
, unicode
);
2497 memcpy((LPWSTR
)lParam
, bufferW
, wParam
* sizeof(WCHAR
));
2498 if (lstrlenW(bufferW
) >= wParam
) rc
= 0;
2502 memcpy((LPSTR
)lParam
, bufferA
, wParam
);
2503 if (strlen(bufferA
) >= wParam
) rc
= 0;
2511 GETTEXTEX
*ex
= (GETTEXTEX
*)wParam
;
2512 int nStart
, nCount
; /* in chars */
2514 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
2515 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
2517 if (ex
->flags
& GT_SELECTION
)
2519 ME_GetSelection(editor
, &nStart
, &nCount
);
2525 nCount
= 0x7fffffff;
2527 if (ex
->codepage
== 1200)
2529 nCount
= min(nCount
, ex
->cb
/ sizeof(WCHAR
) - 1);
2530 return ME_GetTextW(editor
, (LPWSTR
)lParam
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
2534 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2535 we can just take a bigger buffer? :) */
2536 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
2538 DWORD buflen
= ex
->cb
;
2542 nCount
= min(nCount
, ex
->cb
- 1);
2543 buffer
= heap_alloc((crlfmul
*nCount
+ 1) * sizeof(WCHAR
));
2545 buflen
= ME_GetTextW(editor
, buffer
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
2546 rc
= WideCharToMultiByte(ex
->codepage
, flags
, buffer
, -1, (LPSTR
)lParam
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
2547 if (rc
) rc
--; /* do not count 0 terminator */
2556 TEXTRANGEW tr
; /* W and A differ only by rng->lpstrText */
2557 ME_GetSelection(editor
, &from
, &to
);
2558 tr
.chrg
.cpMin
= from
;
2560 tr
.lpstrText
= (WCHAR
*)lParam
;
2561 return RichEditWndProc_common(hWnd
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
, unicode
);
2563 case EM_GETSCROLLPOS
:
2565 POINT
*point
= (POINT
*)lParam
;
2566 point
->x
= 0; /* FIXME side scrolling not implemented */
2567 point
->y
= ME_GetYScrollPos(editor
);
2570 case EM_GETTEXTRANGE
:
2572 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
2573 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2574 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
,
2575 editor
->bEmulateVersion10
, ME_GetTextLength(editor
));
2577 return ME_GetTextW(editor
, rng
->lpstrText
, rng
->chrg
.cpMin
, rng
->chrg
.cpMax
-rng
->chrg
.cpMin
, editor
->bEmulateVersion10
);
2580 int nLen
= rng
->chrg
.cpMax
-rng
->chrg
.cpMin
;
2581 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2582 int nChars
= ME_GetTextW(editor
, p
, rng
->chrg
.cpMin
, nLen
, editor
->bEmulateVersion10
);
2583 /* FIXME this is a potential security hole (buffer overrun)
2584 if you know more about wchar->mbyte conversion please explain
2586 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)rng
->lpstrText
, nLen
+1, NULL
, NULL
);
2593 ME_DisplayItem
*run
;
2594 const unsigned int nMaxChars
= *(WORD
*) lParam
;
2595 unsigned int nEndChars
, nCharsLeft
= nMaxChars
;
2596 char *dest
= (char *) lParam
;
2597 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2598 we need to know how if we have the extra \n or not */
2599 int nLF
= editor
->bEmulateVersion10
;
2601 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
2602 unicode
? "Unicode" : "Ansi");
2604 run
= ME_FindRowWithNumber(editor
, wParam
);
2608 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
2609 && !(run
->member
.run
.nFlags
& MERF_ENDPARA
))
2613 if (run
->type
!= diRun
)
2615 strText
= run
->member
.run
.strText
;
2616 nCopy
= min(nCharsLeft
, strText
->nLen
);
2619 lstrcpynW((LPWSTR
) dest
, strText
->szData
, nCopy
);
2621 nCopy
= WideCharToMultiByte(CP_ACP
, 0, strText
->szData
, nCopy
, dest
,
2622 nCharsLeft
, NULL
, NULL
);
2623 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
2624 nCharsLeft
-= nCopy
;
2627 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2628 nEndChars
= min(nCharsLeft
, 2 + nLF
);
2629 nCharsLeft
-= nEndChars
;
2632 const WCHAR src
[] = {'\r', '\0'};
2633 const WCHAR src10
[] = {'\r', '\n', '\0'};
2634 lstrcpynW((LPWSTR
) dest
, nLF
? src10
: src
, nEndChars
);
2637 lstrcpynA(dest
, nLF
? "\r\n" : "\r", nEndChars
);
2639 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars
- nCharsLeft
);
2641 if (nEndChars
== 2 + nLF
)
2642 return nMaxChars
- nCharsLeft
- 1; /* don't count \0 */
2644 return nMaxChars
- nCharsLeft
;
2646 case EM_GETLINECOUNT
:
2648 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
2651 while (item
!= editor
->pBuffer
->pLast
)
2653 assert(item
->type
== diParagraph
);
2654 nRows
+= item
->member
.para
.nRows
;
2655 item
= item
->member
.para
.next_para
;
2657 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
2658 return max(1, nRows
);
2660 case EM_LINEFROMCHAR
:
2663 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
, 1));
2665 return ME_RowNumberFromCharOfs(editor
, wParam
);
2667 case EM_EXLINEFROMCHAR
:
2670 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
,1));
2672 return ME_RowNumberFromCharOfs(editor
, lParam
);
2676 ME_DisplayItem
*item
, *para
;
2680 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
2682 item
= ME_FindRowWithNumber(editor
, wParam
);
2685 para
= ME_GetParagraph(item
);
2686 item
= ME_FindItemFwd(item
, diRun
);
2687 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
2688 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
2693 ME_DisplayItem
*item
, *item_end
;
2694 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
2696 if (wParam
> ME_GetTextLength(editor
))
2700 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2703 item
= ME_FindItemAtOffset(editor
, diRun
, wParam
, NULL
);
2704 item
= ME_RowStart(item
);
2705 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, ME_FindItemFwd(item
, diRun
), 0);
2706 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
2707 if (item_end
->type
== diStartRow
)
2708 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, ME_FindItemFwd(item_end
, diRun
), 0);
2710 nNextLineOfs
= ME_FindItemFwd(item
, diParagraphOrEnd
)->member
.para
.nCharOfs
2711 - (editor
->bEmulateVersion10
?2:1);
2712 nChars
= nNextLineOfs
- nThisLineOfs
;
2713 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
2716 case EM_EXLIMITTEXT
:
2718 if ((int)lParam
< 0)
2721 editor
->nTextLimit
= 65536;
2723 editor
->nTextLimit
= (int) lParam
;
2729 editor
->nTextLimit
= 65536;
2731 editor
->nTextLimit
= (int) wParam
;
2734 case EM_GETLIMITTEXT
:
2736 return editor
->nTextLimit
;
2740 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
2741 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
2745 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
2746 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
2747 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
2753 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
2754 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
2758 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
2759 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
2760 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
2766 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
2767 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
2769 case EM_FINDTEXTEXW
:
2771 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
2772 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
2775 if (!wParam
|| !lParam
)
2777 *(int *)wParam
= editor
->nZoomNumerator
;
2778 *(int *)lParam
= editor
->nZoomDenominator
;
2781 return ME_SetZoom(editor
, wParam
, lParam
);
2782 case EM_CHARFROMPOS
:
2783 return ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
);
2784 case EM_POSFROMCHAR
:
2786 ME_DisplayItem
*pRun
;
2787 int nCharOfs
, nOffset
, nLength
;
2791 /* detect which API version we're dealing with */
2792 if (wParam
>= 0x40000)
2794 nLength
= ME_GetTextLength(editor
);
2796 if (nCharOfs
< nLength
) {
2797 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pRun
, &nOffset
);
2798 assert(pRun
->type
== diRun
);
2799 pt
.y
= pRun
->member
.run
.pt
.y
;
2800 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
);
2801 pt
.y
+= ME_GetParagraph(pRun
)->member
.para
.nYPos
;
2804 pt
.y
= editor
->pBuffer
->pLast
->member
.para
.nYPos
;
2806 pt
.x
+= editor
->selofs
;
2807 if (wParam
>= 0x40000) {
2808 *(POINTL
*)wParam
= pt
;
2810 return MAKELONG( pt
.x
, pt
.y
);
2813 if (GetWindowLongW(hWnd
, GWL_STYLE
) & WS_HSCROLL
)
2814 { /* Squelch the default horizontal scrollbar it would make */
2815 ShowScrollBar(editor
->hWnd
, SB_HORZ
, FALSE
);
2817 ME_CommitUndo(editor
);
2818 ME_WrapMarkedParagraphs(editor
);
2819 ME_MoveCaret(editor
);
2822 ME_DestroyEditor(editor
);
2823 SetWindowLongPtrW(hWnd
, 0, 0);
2825 case WM_LBUTTONDOWN
:
2826 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2827 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2830 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
2832 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2833 if (!ME_SetCursor(editor
, LOWORD(lParam
))) goto do_default
;
2836 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2837 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2839 if (GetCapture() == hWnd
)
2840 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
2841 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2842 if (!ME_SetCursor(editor
, LOWORD(lParam
))) goto do_default
;
2845 if (GetCapture() == hWnd
)
2847 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2848 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2853 editor
->linesel
= 0;
2854 ret
= ME_SetCursor(editor
, LOWORD(lParam
));
2855 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2856 if (!ret
) goto do_default
;
2859 case WM_LBUTTONDBLCLK
:
2860 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2861 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2863 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
2864 ME_SelectWord(editor
);
2867 case WM_RBUTTONDOWN
:
2868 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
2869 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2872 case WM_CONTEXTMENU
:
2873 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
2877 if (editor
->bRedraw
)
2882 hDC
= BeginPaint(hWnd
, &ps
);
2883 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
2884 EndPaint(hWnd
, &ps
);
2888 editor
->bHaveFocus
= TRUE
;
2889 ME_ShowCaret(editor
);
2890 ME_SendOldNotify(editor
, EN_SETFOCUS
);
2893 ME_HideCaret(editor
);
2894 editor
->bHaveFocus
= FALSE
;
2895 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
2899 if (editor
->bRedraw
)
2901 HDC hDC
= (HDC
)wParam
;
2903 if (GetUpdateRect(hWnd
,&rc
,TRUE
))
2905 FillRect(hDC
, &rc
, editor
->hbrBackground
);
2911 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
2914 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
2915 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2919 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
2920 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
2922 if (ME_KeyDown(editor
, LOWORD(wParam
)))
2930 wstr
= (WCHAR
)wParam
;
2933 CHAR charA
= wParam
;
2934 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2936 if (editor
->AutoURLDetect_bEnable
)
2937 ME_AutoURLDetect(editor
, wstr
);
2941 case 1: /* Ctrl-A */
2942 ME_SetSelection(editor
, 0, -1);
2944 case 3: /* Ctrl-C */
2945 SendMessageW(editor
->hWnd
, WM_COPY
, 0, 0);
2949 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
) {
2950 MessageBeep(MB_ICONERROR
);
2951 return 0; /* FIXME really 0 ? */
2956 case 22: /* Ctrl-V */
2957 SendMessageW(editor
->hWnd
, WM_PASTE
, 0, 0);
2959 case 24: /* Ctrl-X */
2960 SendMessageW(editor
->hWnd
, WM_CUT
, 0, 0);
2962 case 25: /* Ctrl-Y */
2963 SendMessageW(editor
->hWnd
, EM_REDO
, 0, 0);
2965 case 26: /* Ctrl-Z */
2966 SendMessageW(editor
->hWnd
, EM_UNDO
, 0, 0);
2969 if (((unsigned)wstr
)>=' '
2970 || (wstr
=='\r' && (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_MULTILINE
))
2972 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2973 /* WM_CHAR is restricted to nTextLimit */
2975 ME_GetSelection(editor
, &from
, &to
);
2976 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2978 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2979 ME_SaveTempStyle(editor
);
2980 if (wstr
== '\r' && (GetKeyState(VK_SHIFT
) & 0x8000))
2981 ME_InsertEndRowFromCursor(editor
, 0);
2983 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2984 ME_ReleaseStyle(style
);
2985 ME_CommitUndo(editor
);
2987 ME_UpdateRepaint(editor
);
2991 case EM_SCROLL
: /* fall through */
2997 origNPos
= ME_GetYScrollPos(editor
);
3000 if (editor
&& editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
3001 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
3002 if (lineHeight
<= 0) lineHeight
= 24;
3004 switch(LOWORD(wParam
))
3007 ME_ScrollUp(editor
,lineHeight
);
3010 ME_ScrollDown(editor
,lineHeight
);
3013 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
3016 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
3019 case SB_THUMBPOSITION
:
3020 ME_ScrollAbs(editor
,HIWORD(wParam
));
3023 if (msg
== EM_SCROLL
)
3024 return 0x00010000 | (((ME_GetYScrollPos(editor
) - origNPos
)/lineHeight
) & 0xffff);
3030 UINT pulScrollLines
;
3032 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3033 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3036 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
3037 gcWheelDelta
= -GET_WHEEL_DELTA_WPARAM(wParam
);
3039 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
3041 /* FIXME follow the original */
3042 ME_ScrollDown(editor
,pulScrollLines
* (gcWheelDelta
/ WHEEL_DELTA
) * 8);
3048 *((RECT
*)lParam
) = editor
->rcFormat
;
3056 RECT
*rc
= (RECT
*)lParam
;
3060 editor
->rcFormat
.left
+= rc
->left
;
3061 editor
->rcFormat
.top
+= rc
->top
;
3062 editor
->rcFormat
.right
+= rc
->right
;
3063 editor
->rcFormat
.bottom
+= rc
->bottom
;
3067 editor
->rcFormat
= *rc
;
3072 GetClientRect(hWnd
, &editor
->rcFormat
);
3074 if (msg
!= EM_SETRECTNP
)
3075 ME_RewrapRepaint(editor
);
3078 case EM_REQUESTRESIZE
:
3079 ME_SendRequestResize(editor
, TRUE
);
3082 if ((editor
->bRedraw
= wParam
))
3083 ME_RewrapRepaint(editor
);
3087 GetClientRect(hWnd
, &editor
->rcFormat
);
3088 ME_RewrapRepaint(editor
);
3089 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3091 /* IME messages to make richedit controls IME aware */
3092 case WM_IME_SETCONTEXT
:
3093 case WM_IME_CONTROL
:
3095 case WM_IME_COMPOSITIONFULL
:
3097 case WM_IME_STARTCOMPOSITION
:
3099 editor
->imeStartIndex
=ME_GetCursorOfs(editor
,0);
3100 ME_DeleteSelection(editor
);
3101 ME_CommitUndo(editor
);
3102 ME_UpdateRepaint(editor
);
3105 case WM_IME_COMPOSITION
:
3109 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
3110 hIMC
= ImmGetContext(hWnd
);
3111 ME_DeleteSelection(editor
);
3112 ME_CommitUndo(editor
);
3113 ME_SaveTempStyle(editor
);
3114 if (lParam
& GCS_RESULTSTR
)
3116 LPWSTR lpCompStr
= NULL
;
3119 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
3120 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
3121 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpCompStr
, dwBufLen
);
3122 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
3123 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
3125 else if (lParam
& GCS_COMPSTR
)
3127 LPWSTR lpCompStr
= NULL
;
3130 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
3131 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
3132 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, dwBufLen
);
3133 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
3135 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
3136 ME_SetSelection(editor
,editor
->imeStartIndex
,
3137 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
3139 ME_ReleaseStyle(style
);
3140 ME_UpdateRepaint(editor
);
3143 case WM_IME_ENDCOMPOSITION
:
3145 ME_DeleteSelection(editor
);
3146 editor
->imeStartIndex
=-1;
3149 case EM_GETOLEINTERFACE
:
3151 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
3152 return CreateIRichEditOle(editor
, ppvObj
);
3154 case EM_GETPASSWORDCHAR
:
3156 return editor
->cPasswordMask
;
3158 case EM_SETOLECALLBACK
:
3159 if(editor
->lpOleCallback
)
3160 IUnknown_Release(editor
->lpOleCallback
);
3161 editor
->lpOleCallback
= (LPRICHEDITOLECALLBACK
)lParam
;
3162 if(editor
->lpOleCallback
)
3163 IUnknown_AddRef(editor
->lpOleCallback
);
3165 case EM_GETWORDBREAKPROC
:
3166 return (LRESULT
)editor
->pfnWordBreak
;
3167 case EM_SETWORDBREAKPROC
:
3169 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
3171 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
3172 return (LRESULT
)pfnOld
;
3174 case EM_SETTEXTMODE
:
3179 ret
= RichEditWndProc_common(hWnd
, WM_GETTEXTLENGTH
, 0, 0, unicode
);
3182 /*Check for valid wParam*/
3183 if ((((wParam
& TM_RICHTEXT
) && ((wParam
& TM_PLAINTEXT
) << 1))) ||
3184 (((wParam
& TM_MULTILEVELUNDO
) && ((wParam
& TM_SINGLELEVELUNDO
) << 1))) ||
3185 (((wParam
& TM_MULTICODEPAGE
) && ((wParam
& TM_SINGLECODEPAGE
) << 1))))
3189 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
3191 mask
|= (TM_RICHTEXT
| TM_PLAINTEXT
);
3192 changes
|= (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
));
3194 /*FIXME: Currently no support for undo level and code page options*/
3195 editor
->mode
= (editor
->mode
& (~mask
)) | changes
;
3201 case EM_SETPASSWORDCHAR
:
3203 editor
->cPasswordMask
= wParam
;
3204 ME_RewrapRepaint(editor
);
3207 case EM_SETTARGETDEVICE
:
3210 editor
->bWordWrap
= (lParam
== 0);
3212 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
3216 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3221 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3223 BOOL unicode
= TRUE
;
3225 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3226 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
3229 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
3232 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3234 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
3237 /******************************************************************
3238 * RichEditANSIWndProc (RICHED20.10)
3240 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3242 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
3245 /******************************************************************
3246 * RichEdit10ANSIWndProc (RICHED20.9)
3248 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3252 /* FIXME: this is NOT the same as 2.0 version */
3253 result
= RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
3254 if (msg
== WM_NCCREATE
)
3256 ME_TextEditor
*editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
3258 editor
->bEmulateVersion10
= TRUE
;
3259 editor
->pBuffer
->pLast
->member
.para
.nCharOfs
= 2;
3264 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
3266 HWND hWnd
= editor
->hWnd
;
3267 SendMessageA(GetParent(hWnd
), WM_COMMAND
, (nCode
<<16)|GetWindowLongW(hWnd
, GWLP_ID
), (LPARAM
)hWnd
);
3270 void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3273 ME_Cursor tmpCursor
;
3274 int nCharOfs
; /* The start of the clicked text. Absolute character offset */
3279 x
= (short)LOWORD(lParam
);
3280 y
= (short)HIWORD(lParam
);
3281 nCharOfs
= ME_CharFromPos(editor
, x
, y
);
3282 if (nCharOfs
< 0) return;
3284 ME_CursorFromCharOfs(editor
, nCharOfs
, &tmpCursor
);
3285 tmpRun
= &tmpCursor
.pRun
->member
.run
;
3287 if ((tmpRun
->style
->fmt
.dwMask
& CFM_LINK
)
3288 && (tmpRun
->style
->fmt
.dwEffects
& CFE_LINK
))
3289 { /* The clicked run has CFE_LINK set */
3290 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
3291 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
3292 info
.nmhdr
.code
= EN_LINK
;
3294 info
.wParam
= wParam
;
3295 info
.lParam
= lParam
;
3296 info
.chrg
.cpMin
= ME_CharOfsFromRunOfs(editor
,tmpCursor
.pRun
,0);
3297 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ ME_StrVLen(tmpRun
->strText
);
3298 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,info
.nmhdr
.idFrom
, (LPARAM
)&info
);
3302 int ME_CountParagraphsBetween(ME_TextEditor
*editor
, int from
, int to
)
3304 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
3307 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= from
)
3308 item
= item
->member
.para
.next_para
;
3311 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= to
) {
3312 item
= item
->member
.para
.next_para
;
3319 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int nStart
, int nChars
, int bCRLF
)
3321 ME_DisplayItem
*item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
3323 WCHAR
*pStart
= buffer
;
3332 int nLen
= ME_StrLen(item
->member
.run
.strText
) - nStart
;
3335 CopyMemory(buffer
, item
->member
.run
.strText
->szData
+ nStart
, sizeof(WCHAR
)*nLen
);
3342 item
= ME_FindItemFwd(item
, diRun
);
3345 while(nChars
&& item
)
3347 int nLen
= ME_StrLen(item
->member
.run
.strText
);
3351 if (item
->member
.run
.nFlags
& MERF_ENDPARA
)
3353 if (!ME_FindItemFwd(item
, diRun
))
3354 /* No '\r' is appended to the last paragraph. */
3356 else if (bCRLF
&& nChars
== 1) {
3367 /* our end paragraph consists of 2 characters now */
3368 if (editor
->bEmulateVersion10
)
3373 CopyMemory(buffer
, item
->member
.run
.strText
->szData
, sizeof(WCHAR
)*nLen
);
3380 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
3384 item
= ME_FindItemFwd(item
, diRun
);
3387 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
3391 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
3396 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
3397 wcW
.lpfnWndProc
= RichEditWndProcW
;
3399 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
3400 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
3402 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
3403 wcW
.hbrBackground
= (HBRUSH
)GetStockObject(NULL_BRUSH
);
3404 wcW
.lpszMenuName
= NULL
;
3406 if (is_version_nt())
3408 wcW
.lpszClassName
= RichEdit20W
;
3409 if (!RegisterClassW(&wcW
)) return FALSE
;
3410 wcW
.lpszClassName
= RichEdit50W
;
3411 if (!RegisterClassW(&wcW
)) return FALSE
;
3415 /* WNDCLASSA/W have the same layout */
3416 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
3417 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
3418 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
3419 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
3422 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
3423 wcA
.lpfnWndProc
= RichEditWndProcA
;
3425 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
3426 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
3428 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
3429 wcA
.hbrBackground
= (HBRUSH
)GetStockObject(NULL_BRUSH
);
3430 wcA
.lpszMenuName
= NULL
;
3431 wcA
.lpszClassName
= "RichEdit20A";
3432 if (!RegisterClassA(&wcA
)) return FALSE
;
3433 wcA
.lpszClassName
= "RichEdit50A";
3434 if (!RegisterClassA(&wcA
)) return FALSE
;
3439 LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
3440 /* FIXME: Not implemented */
3441 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3442 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
3443 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3446 LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
3447 /* FIXME: Not implemented */
3448 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3449 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
3450 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
3453 /******************************************************************
3454 * REExtendedRegisterClass (RICHED20.8)
3456 * FIXME undocumented
3457 * Need to check for errors and implement controls and callbacks
3459 LRESULT WINAPI
REExtendedRegisterClass(void)
3464 FIXME("semi stub\n");
3468 wcW
.hInstance
= NULL
;
3471 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
3472 wcW
.lpszMenuName
= NULL
;
3474 if (!ME_ListBoxRegistered
)
3476 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
3477 wcW
.lpfnWndProc
= REListWndProc
;
3478 wcW
.lpszClassName
= REListBox20W
;
3479 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
3482 if (!ME_ComboBoxRegistered
)
3484 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
3485 wcW
.lpfnWndProc
= REComboWndProc
;
3486 wcW
.lpszClassName
= REComboBox20W
;
3487 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
3491 if (ME_ListBoxRegistered
)
3493 if (ME_ComboBoxRegistered
)
3499 int ME_AutoURLDetect(ME_TextEditor
*editor
, WCHAR curChar
)
3519 int curf_ef
, link_ef
, def_ef
;
3520 int cur_prefx
, prefx_cnt
;
3521 int sel_min
, sel_max
;
3524 int URLmin
, URLmax
= 0;
3527 CHARFORMAT2W cur_format
;
3528 CHARFORMAT2W default_format
;
3530 RichEditANSIWndProc(editor
->hWnd
, EM_EXGETSEL
, (WPARAM
) 0, (LPARAM
) &ins_pt
);
3531 sel_min
= ins_pt
.cpMin
;
3532 sel_max
= ins_pt
.cpMax
;
3533 if (sel_min
==sel_max
)
3535 if (sel_min
!=sel_max
)
3536 car_pos
= ME_GetTextLength(editor
)+1;
3537 cur_format
.cbSize
= sizeof(cur_format
);
3538 default_format
.cbSize
= sizeof(default_format
);
3539 RichEditANSIWndProc(editor
->hWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
) &cur_format
);
3540 RichEditANSIWndProc(editor
->hWnd
, EM_GETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
) &default_format
);
3541 link
.cbSize
= sizeof(link
);