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)
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
103 - EM_SETLANGOPTIONS 2.0
106 + EM_SETMODIFY (not sure if implementation is correct)
108 + EM_SETOPTIONS (partially implemented)
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
115 + EM_SETRECTNP (EM_SETRECT without repainting)
117 + EM_SETSCROLLPOS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
151 * EN_CHANGE (sent from the wrong place)
168 * EN_UPDATE (sent from the wrong place)
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
179 - ES_MULTILINE (currently single line controls aren't supported)
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
194 * RICHED20 TODO (incomplete):
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
214 * - ListBox & ComboBox not implemented
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
230 #define NO_SHLWAPI_STREAM
236 #define STACK_SIZE_DEFAULT 100
237 #define STACK_SIZE_MAX 1000
239 #define TEXT_LIMIT_DEFAULT 32767
241 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
243 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
244 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
246 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
247 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
248 static HCURSOR hLeft
;
250 BOOL me_debug
= FALSE
;
251 HANDLE me_heap
= NULL
;
253 static BOOL ME_ListBoxRegistered
= FALSE
;
254 static BOOL ME_ComboBoxRegistered
= FALSE
;
256 static inline BOOL
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
)
287 LRESULT total_bytes_read
= 0;
288 BOOL is_read
= FALSE
;
289 DWORD cp
= CP_ACP
, copy
= 0;
290 char conv_buf
[4 + STREAMIN_BUFFER_SIZE
]; /* up to 4 additional UTF-8 bytes */
292 static const char bom_utf8
[] = {0xEF, 0xBB, 0xBF};
294 TRACE("%08x %p\n", dwFormat
, stream
);
298 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
302 ME_StreamInFill(stream
);
303 if (stream
->editstream
->dwError
)
307 total_bytes_read
+= stream
->dwSize
;
310 if (!(dwFormat
& SF_UNICODE
))
312 char * buf
= stream
->buffer
;
313 DWORD size
= stream
->dwSize
, end
;
318 if (stream
->dwSize
>= 3 && !memcmp(stream
->buffer
, bom_utf8
, 3))
330 memcpy(conv_buf
+ copy
, buf
, size
);
335 while ((buf
[end
-1] & 0xC0) == 0x80)
338 --total_bytes_read
; /* strange, but seems to match windows */
340 if (buf
[end
-1] & 0x80)
343 if ((buf
[end
-1] & 0xE0) == 0xC0)
345 if ((buf
[end
-1] & 0xF0) == 0xE0)
347 if ((buf
[end
-1] & 0xF8) == 0xF0)
350 if (size
- end
>= need
)
352 /* we have enough bytes for this sequence */
357 /* need more bytes, so don't transcode this sequence */
365 nWideChars
= MultiByteToWideChar(cp
, 0, buf
, end
, wszText
, STREAMIN_BUFFER_SIZE
);
372 memcpy(conv_buf
, buf
+ end
, size
- end
);
379 nWideChars
= stream
->dwSize
>> 1;
380 pText
= (WCHAR
*)stream
->buffer
;
383 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
384 if (stream
->dwSize
== 0)
388 return total_bytes_read
;
391 static void ME_ApplyBorderProperties(RTF_Info
*info
,
392 ME_BorderRect
*borderRect
,
393 RTFBorder
*borderDef
)
396 ME_Border
*pBorders
[] = {&borderRect
->top
,
400 for (i
= 0; i
< 4; i
++)
402 RTFColor
*colorDef
= info
->colorList
;
403 pBorders
[i
]->width
= borderDef
[i
].width
;
404 colorNum
= borderDef
[i
].color
;
405 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
406 colorDef
= colorDef
->rtfNextColor
;
408 pBorders
[i
]->colorRef
= RGB(
409 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
410 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
411 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
413 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
417 void ME_RTFCharAttrHook(RTF_Info
*info
)
420 fmt
.cbSize
= sizeof(fmt
);
424 switch(info
->rtfMinor
)
427 /* FIXME add more flags once they're implemented */
428 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
429 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
430 fmt
.yHeight
= 12*20; /* 12pt */
431 fmt
.wWeight
= FW_NORMAL
;
432 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
435 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
436 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
437 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
440 fmt
.dwMask
= CFM_ITALIC
;
441 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
444 fmt
.dwMask
= CFM_UNDERLINETYPE
;
445 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
447 case rtfDotUnderline
:
448 fmt
.dwMask
= CFM_UNDERLINETYPE
;
449 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
452 fmt
.dwMask
= CFM_UNDERLINETYPE
;
453 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
455 case rtfWordUnderline
:
456 fmt
.dwMask
= CFM_UNDERLINETYPE
;
457 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
460 fmt
.dwMask
= CFM_UNDERLINETYPE
;
461 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
464 fmt
.dwMask
= CFM_STRIKEOUT
;
465 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
469 case rtfSubScrShrink
:
470 case rtfSuperScrShrink
:
472 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
473 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
474 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
475 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
478 fmt
.dwMask
= CFM_HIDDEN
;
479 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
482 fmt
.dwMask
= CFM_BACKCOLOR
;
484 if (info
->rtfParam
== 0)
485 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
486 else if (info
->rtfParam
!= rtfNoParam
)
488 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
489 if (c
&& c
->rtfCBlue
>= 0)
490 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
492 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
496 fmt
.dwMask
= CFM_COLOR
;
498 if (info
->rtfParam
== 0)
499 fmt
.dwEffects
= CFE_AUTOCOLOR
;
500 else if (info
->rtfParam
!= rtfNoParam
)
502 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
503 if (c
&& c
->rtfCBlue
>= 0)
504 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
506 fmt
.dwEffects
= CFE_AUTOCOLOR
;
511 if (info
->rtfParam
!= rtfNoParam
)
513 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
516 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
517 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
518 fmt
.bCharSet
= f
->rtfFCharSet
;
519 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
520 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
525 fmt
.dwMask
= CFM_SIZE
;
526 if (info
->rtfParam
!= rtfNoParam
)
527 fmt
.yHeight
= info
->rtfParam
*10;
532 RTFFlushOutputBuffer(info
);
533 /* FIXME too slow ? how come ? */
534 style2
= ME_ApplyStyle(info
->style
, &fmt
);
535 ME_ReleaseStyle(info
->style
);
536 info
->style
= style2
;
537 info
->styleChanged
= TRUE
;
541 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
542 the same tags mean different things in different contexts */
543 void ME_RTFParAttrHook(RTF_Info
*info
)
546 fmt
.cbSize
= sizeof(fmt
);
549 switch(info
->rtfMinor
)
551 case rtfParDef
: /* restores default paragraph attributes */
552 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
553 info
->borderType
= RTFBorderParaLeft
;
554 else /* v1.0 - 3.0 */
555 info
->borderType
= RTFBorderParaTop
;
556 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
557 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
559 /* TODO: numbering, shading */
560 fmt
.wAlignment
= PFA_LEFT
;
562 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
563 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
564 fmt
.wBorderSpace
= 0;
565 fmt
.bLineSpacingRule
= 0;
566 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
567 fmt
.dyLineSpacing
= 0;
568 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
570 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
571 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
574 ME_DisplayItem
*para
;
575 /* We are just after a table row. */
576 RTFFlushOutputBuffer(info
);
577 cursor
= info
->editor
->pCursors
[0];
579 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
580 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
582 /* Since the table row end, no text has been inserted, and the \intbl
583 * control word has not be used. We can confirm that we are not in a
586 info
->tableDef
->tableRowStart
= NULL
;
587 info
->canInheritInTbl
= FALSE
;
590 } else { /* v1.0 - v3.0 */
591 fmt
.dwMask
|= PFM_TABLE
;
592 fmt
.wEffects
&= ~PFE_TABLE
;
596 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
598 while (info
->rtfParam
> info
->nestingLevel
) {
599 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
600 ZeroMemory(tableDef
, sizeof(RTFTable
));
601 tableDef
->parent
= info
->tableDef
;
602 info
->tableDef
= tableDef
;
604 RTFFlushOutputBuffer(info
);
605 if (tableDef
->tableRowStart
&&
606 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
608 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
609 para
= para
->member
.para
.next_para
;
610 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
611 tableDef
->tableRowStart
= para
;
615 cursor
= info
->editor
->pCursors
[0];
616 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
617 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
618 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
621 info
->nestingLevel
++;
623 info
->canInheritInTbl
= FALSE
;
628 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
630 if (info
->nestingLevel
< 1)
635 info
->tableDef
= ALLOC_OBJ(RTFTable
);
636 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
638 tableDef
= info
->tableDef
;
639 RTFFlushOutputBuffer(info
);
640 if (tableDef
->tableRowStart
&&
641 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
643 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
644 para
= para
->member
.para
.next_para
;
645 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
646 tableDef
->tableRowStart
= para
;
650 cursor
= info
->editor
->pCursors
[0];
651 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
652 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
653 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
655 info
->nestingLevel
= 1;
656 info
->canInheritInTbl
= TRUE
;
659 } else { /* v1.0 - v3.0 */
660 fmt
.dwMask
|= PFM_TABLE
;
661 fmt
.wEffects
|= PFE_TABLE
;
666 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
667 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
668 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
669 fmt
.dxOffset
= -info
->rtfParam
;
672 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
673 fmt
.dwMask
= PFM_STARTINDENT
;
674 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
677 fmt
.dwMask
= PFM_RIGHTINDENT
;
678 fmt
.dxRightIndent
= info
->rtfParam
;
682 fmt
.dwMask
= PFM_ALIGNMENT
;
683 fmt
.wAlignment
= PFA_LEFT
;
686 fmt
.dwMask
= PFM_ALIGNMENT
;
687 fmt
.wAlignment
= PFA_RIGHT
;
690 fmt
.dwMask
= PFM_ALIGNMENT
;
691 fmt
.wAlignment
= PFA_CENTER
;
694 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
695 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
699 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
700 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
701 fmt
.dwMask
= PFM_TABSTOPS
;
704 fmt
.dwMask
= PFM_KEEP
;
705 fmt
.wEffects
= PFE_KEEP
;
707 case rtfNoWidowControl
:
708 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
709 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
712 fmt
.dwMask
= PFM_KEEPNEXT
;
713 fmt
.wEffects
= PFE_KEEPNEXT
;
716 fmt
.dwMask
= PFM_SPACEAFTER
;
717 fmt
.dySpaceAfter
= info
->rtfParam
;
720 fmt
.dwMask
= PFM_SPACEBEFORE
;
721 fmt
.dySpaceBefore
= info
->rtfParam
;
723 case rtfSpaceBetween
:
724 fmt
.dwMask
= PFM_LINESPACING
;
725 if ((int)info
->rtfParam
> 0)
727 fmt
.dyLineSpacing
= info
->rtfParam
;
728 fmt
.bLineSpacingRule
= 3;
732 fmt
.dyLineSpacing
= info
->rtfParam
;
733 fmt
.bLineSpacingRule
= 4;
736 case rtfSpaceMultiply
:
737 fmt
.dwMask
= PFM_LINESPACING
;
738 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
739 fmt
.bLineSpacingRule
= 5;
742 fmt
.dwMask
= PFM_NUMBERING
;
743 fmt
.wNumbering
= PFN_BULLET
;
746 fmt
.dwMask
= PFM_NUMBERING
;
747 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
749 case rtfParNumDecimal
:
750 fmt
.dwMask
= PFM_NUMBERING
;
751 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
753 case rtfParNumIndent
:
754 fmt
.dwMask
= PFM_NUMBERINGTAB
;
755 fmt
.wNumberingTab
= info
->rtfParam
;
757 case rtfParNumStartAt
:
758 fmt
.dwMask
= PFM_NUMBERINGSTART
;
759 fmt
.wNumberingStart
= info
->rtfParam
;
762 info
->borderType
= RTFBorderParaLeft
;
763 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
764 if (!(fmt
.dwMask
& PFM_BORDER
))
766 fmt
.wBorderSpace
= 0;
767 fmt
.wBorderWidth
= 1;
771 fmt
.dwMask
= PFM_BORDER
;
774 info
->borderType
= RTFBorderParaRight
;
775 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
776 if (!(fmt
.dwMask
& PFM_BORDER
))
778 fmt
.wBorderSpace
= 0;
779 fmt
.wBorderWidth
= 1;
783 fmt
.dwMask
= PFM_BORDER
;
786 info
->borderType
= RTFBorderParaTop
;
787 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
788 if (!(fmt
.dwMask
& PFM_BORDER
))
790 fmt
.wBorderSpace
= 0;
791 fmt
.wBorderWidth
= 1;
795 fmt
.dwMask
= PFM_BORDER
;
797 case rtfBorderBottom
:
798 info
->borderType
= RTFBorderParaBottom
;
799 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
800 if (!(fmt
.dwMask
& PFM_BORDER
))
802 fmt
.wBorderSpace
= 0;
803 fmt
.wBorderWidth
= 1;
807 fmt
.dwMask
= PFM_BORDER
;
809 case rtfBorderSingle
:
810 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
811 /* we assume that borders have been created before (RTF spec) */
812 fmt
.wBorders
&= ~0x700;
813 fmt
.wBorders
|= 1 << 8;
814 fmt
.dwMask
= PFM_BORDER
;
817 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
818 /* we assume that borders have been created before (RTF spec) */
819 fmt
.wBorders
&= ~0x700;
820 fmt
.wBorders
|= 2 << 8;
821 fmt
.dwMask
= PFM_BORDER
;
823 case rtfBorderShadow
:
824 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
825 /* we assume that borders have been created before (RTF spec) */
826 fmt
.wBorders
&= ~0x700;
827 fmt
.wBorders
|= 10 << 8;
828 fmt
.dwMask
= PFM_BORDER
;
830 case rtfBorderDouble
:
831 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
832 /* we assume that borders have been created before (RTF spec) */
833 fmt
.wBorders
&= ~0x700;
834 fmt
.wBorders
|= 7 << 8;
835 fmt
.dwMask
= PFM_BORDER
;
838 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
839 /* we assume that borders have been created before (RTF spec) */
840 fmt
.wBorders
&= ~0x700;
841 fmt
.wBorders
|= 11 << 8;
842 fmt
.dwMask
= PFM_BORDER
;
846 int borderSide
= info
->borderType
& RTFBorderSideMask
;
847 RTFTable
*tableDef
= info
->tableDef
;
848 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
849 /* we assume that borders have been created before (RTF spec) */
850 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
851 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
854 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
856 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
857 border
->width
= info
->rtfParam
;
860 fmt
.dwMask
= PFM_BORDER
;
864 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
865 /* we assume that borders have been created before (RTF spec) */
866 fmt
.wBorderSpace
= info
->rtfParam
;
867 fmt
.dwMask
= PFM_BORDER
;
871 RTFTable
*tableDef
= info
->tableDef
;
872 int borderSide
= info
->borderType
& RTFBorderSideMask
;
873 int borderType
= info
->borderType
& RTFBorderTypeMask
;
876 case RTFBorderTypePara
:
877 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
879 /* v1.0 - 3.0 treat paragraph and row borders the same. */
880 case RTFBorderTypeRow
:
882 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
885 case RTFBorderTypeCell
:
886 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
887 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
894 fmt
.dwMask
= PFM_RTLPARA
;
895 fmt
.wEffects
= PFE_RTLPARA
;
898 fmt
.dwMask
= PFM_RTLPARA
;
903 RTFFlushOutputBuffer(info
);
904 /* FIXME too slow ? how come ?*/
905 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
909 void ME_RTFTblAttrHook(RTF_Info
*info
)
911 switch (info
->rtfMinor
)
915 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
916 info
->borderType
= 0; /* Not sure */
917 else /* v1.0 - 3.0 */
918 info
->borderType
= RTFBorderRowTop
;
919 if (!info
->tableDef
) {
920 info
->tableDef
= ME_MakeTableDef(info
->editor
);
922 ME_InitTableDef(info
->editor
, info
->tableDef
);
931 info
->tableDef
= ME_MakeTableDef(info
->editor
);
933 cellNum
= info
->tableDef
->numCellsDefined
;
934 if (cellNum
>= MAX_TABLE_CELLS
)
936 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
937 if (cellNum
< MAX_TAB_STOPS
) {
938 /* Tab stops were used to store cell positions before v4.1 but v4.1
939 * still seems to set the tabstops without using them. */
940 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
941 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
942 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
943 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
945 info
->tableDef
->numCellsDefined
++;
949 info
->borderType
= RTFBorderRowTop
;
952 info
->borderType
= RTFBorderRowLeft
;
954 case rtfRowBordBottom
:
955 info
->borderType
= RTFBorderRowBottom
;
957 case rtfRowBordRight
:
958 info
->borderType
= RTFBorderRowRight
;
961 info
->borderType
= RTFBorderCellTop
;
963 case rtfCellBordLeft
:
964 info
->borderType
= RTFBorderCellLeft
;
966 case rtfCellBordBottom
:
967 info
->borderType
= RTFBorderCellBottom
;
969 case rtfCellBordRight
:
970 info
->borderType
= RTFBorderCellRight
;
974 info
->tableDef
->gapH
= info
->rtfParam
;
978 info
->tableDef
->leftEdge
= info
->rtfParam
;
983 void ME_RTFSpecialCharHook(RTF_Info
*info
)
985 RTFTable
*tableDef
= info
->tableDef
;
986 switch (info
->rtfMinor
)
989 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
991 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
995 RTFFlushOutputBuffer(info
);
996 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
997 if (tableDef
->tableRowStart
)
999 if (!info
->nestingLevel
&&
1000 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1002 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
1003 para
= para
->member
.para
.next_para
;
1004 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1005 tableDef
->tableRowStart
= para
;
1006 info
->nestingLevel
= 1;
1008 ME_InsertTableCellFromCursor(info
->editor
);
1010 } else { /* v1.0 - v3.0 */
1011 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1012 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1013 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
1014 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1017 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1018 tableDef
->numCellsInserted
++;
1023 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
1025 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1028 ME_DisplayItem
*para
, *cell
, *run
;
1033 RTFFlushOutputBuffer(info
);
1034 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
1035 if (!tableDef
->tableRowStart
)
1037 if (!info
->nestingLevel
&&
1038 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1040 para
= tableDef
->tableRowStart
;
1041 para
= para
->member
.para
.next_para
;
1042 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1043 tableDef
->tableRowStart
= para
;
1044 info
->nestingLevel
++;
1046 para
= tableDef
->tableRowStart
;
1047 cell
= ME_FindItemFwd(para
, diCell
);
1048 assert(cell
&& !cell
->member
.cell
.prev_cell
);
1049 if (tableDef
->numCellsDefined
< 1)
1051 /* 2000 twips appears to be the cell size that native richedit uses
1052 * when no cell sizes are specified. */
1053 const int defaultCellSize
= 2000;
1054 int nRightBoundary
= defaultCellSize
;
1055 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1056 while (cell
->member
.cell
.next_cell
) {
1057 cell
= cell
->member
.cell
.next_cell
;
1058 nRightBoundary
+= defaultCellSize
;
1059 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1061 para
= ME_InsertTableCellFromCursor(info
->editor
);
1062 cell
= para
->member
.para
.pCell
;
1063 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1065 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
1067 RTFCell
*cellDef
= &tableDef
->cells
[i
];
1068 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
1069 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
1071 cell
= cell
->member
.cell
.next_cell
;
1074 para
= ME_InsertTableCellFromCursor(info
->editor
);
1075 cell
= para
->member
.para
.pCell
;
1078 /* Cell for table row delimiter is empty */
1079 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1082 run
= ME_FindItemFwd(cell
, diRun
);
1083 if (info
->editor
->pCursors
[0].pRun
!= run
||
1084 info
->editor
->pCursors
[0].nOffset
)
1087 /* Delete inserted cells that aren't defined. */
1088 info
->editor
->pCursors
[1].pRun
= run
;
1089 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1090 info
->editor
->pCursors
[1].nOffset
= 0;
1091 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1092 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1093 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1097 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1098 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1099 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1100 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1102 info
->nestingLevel
--;
1103 if (!info
->nestingLevel
)
1105 if (info
->canInheritInTbl
) {
1106 tableDef
->tableRowStart
= para
;
1108 while (info
->tableDef
) {
1109 tableDef
= info
->tableDef
;
1110 info
->tableDef
= tableDef
->parent
;
1111 heap_free(tableDef
);
1115 info
->tableDef
= tableDef
->parent
;
1116 heap_free(tableDef
);
1118 } else { /* v1.0 - v3.0 */
1120 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1121 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1122 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1123 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1125 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1127 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1130 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1131 tableDef
->numCellsInserted
++;
1133 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1134 if (!tableDef
->numCellsDefined
)
1135 pFmt
->wEffects
&= ~PFE_TABLE
;
1136 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1137 tableDef
->numCellsInserted
= 0;
1143 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1144 ME_DisplayItem
*para
;
1146 RTFFlushOutputBuffer(info
);
1147 para
= info
->editor
->pCursors
[0].pPara
;
1148 pFmt
= para
->member
.para
.pFmt
;
1149 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1151 /* rtfPar is treated like a space within a table. */
1152 info
->rtfClass
= rtfText
;
1153 info
->rtfMajor
= ' ';
1155 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1156 tableDef
->numCellsInserted
= 0;
1162 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1165 LPOLEOBJECT lpObject
= NULL
;
1166 LPSTORAGE lpStorage
= NULL
;
1167 LPOLECLIENTSITE lpClientSite
= NULL
;
1168 LPDATAOBJECT lpDataObject
= NULL
;
1169 LPOLECACHE lpOleCache
= NULL
;
1178 stgm
.tymed
= TYMED_ENHMF
;
1179 stgm
.u
.hEnhMetaFile
= hemf
;
1180 fm
.cfFormat
= CF_ENHMETAFILE
;
1184 stgm
.tymed
= TYMED_GDI
;
1185 stgm
.u
.hBitmap
= hbmp
;
1186 fm
.cfFormat
= CF_BITMAP
;
1188 stgm
.pUnkForRelease
= NULL
;
1191 fm
.dwAspect
= DVASPECT_CONTENT
;
1193 fm
.tymed
= stgm
.tymed
;
1195 if (!info
->lpRichEditOle
)
1197 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1200 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1202 /* FIXME: enable it when rich-edit properly implements this method */
1203 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1204 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1206 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1207 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1208 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1209 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1210 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1214 reobject
.cbStruct
= sizeof(reobject
);
1215 reobject
.cp
= REO_CP_SELECTION
;
1216 reobject
.clsid
= clsid
;
1217 reobject
.poleobj
= lpObject
;
1218 reobject
.pstg
= lpStorage
;
1219 reobject
.polesite
= lpClientSite
;
1220 /* convert from twips to .01 mm */
1221 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1222 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1223 reobject
.dvaspect
= DVASPECT_CONTENT
;
1224 reobject
.dwFlags
= 0; /* FIXME */
1225 reobject
.dwUser
= 0;
1227 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1231 if (lpObject
) IOleObject_Release(lpObject
);
1232 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1233 if (lpStorage
) IStorage_Release(lpStorage
);
1234 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1235 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1240 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1243 BYTE
* buffer
= NULL
;
1244 unsigned bufsz
, bufidx
;
1250 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1253 if (info
->rtfClass
== rtfEOF
)
1256 /* fetch picture type */
1257 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1259 mfp
.mm
= info
->rtfParam
;
1262 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1264 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1267 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1269 gfx
= gfx_enhmetafile
;
1273 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1277 /* fetch picture attributes */
1281 if (info
->rtfClass
== rtfEOF
)
1283 if (info
->rtfClass
== rtfText
)
1285 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1287 ERR("Expected picture attribute (%d %d)\n",
1288 info
->rtfClass
, info
->rtfMajor
);
1291 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1293 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1295 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1297 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1299 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1300 sz
.cx
= info
->rtfParam
;
1301 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1302 sz
.cy
= info
->rtfParam
;
1304 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1306 /* fetch picture data */
1309 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1310 val
= info
->rtfMajor
;
1311 for (flip
= TRUE
;; flip
= !flip
)
1314 if (info
->rtfClass
== rtfEOF
)
1316 HeapFree(GetProcessHeap(), 0, buffer
);
1317 return; /* Warn ?? */
1319 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1321 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1324 if (bufidx
>= bufsz
&&
1325 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1327 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1330 val
= info
->rtfMajor
;
1332 if (flip
) FIXME("wrong hex string\n");
1336 case gfx_enhmetafile
:
1337 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1338 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1341 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1342 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1346 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1348 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1350 /* not quite right, especially for bitfields type of compression */
1351 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1352 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1353 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1354 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1355 bi
, DIB_RGB_COLORS
)))
1356 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1363 HeapFree(GetProcessHeap(), 0, buffer
);
1364 RTFRouteToken (info
); /* feed "}" back to router */
1367 HeapFree(GetProcessHeap(), 0, buffer
);
1369 RTFRouteToken(info
); /* feed "}" back to router */
1372 /* for now, lookup the \result part and use it, whatever the object */
1373 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1378 if (info
->rtfClass
== rtfEOF
)
1380 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1382 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1385 if (info
->rtfClass
== rtfEOF
)
1387 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1391 while (RTFGetToken (info
) != rtfEOF
)
1393 if (info
->rtfClass
== rtfGroup
)
1395 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1396 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1398 RTFRouteToken(info
);
1401 else RTFSkipGroup(info
);
1404 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1406 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1410 RTFRouteToken(info
); /* feed "}" back to router */
1413 static void ME_RTFReadHook(RTF_Info
*info
)
1415 switch(info
->rtfClass
)
1418 switch(info
->rtfMajor
)
1421 if (info
->stackTop
< maxStack
) {
1422 info
->stack
[info
->stackTop
].style
= info
->style
;
1423 ME_AddRefStyle(info
->style
);
1424 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1425 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1428 info
->styleChanged
= FALSE
;
1432 RTFFlushOutputBuffer(info
);
1434 if (info
->stackTop
<= 0)
1435 info
->rtfClass
= rtfEOF
;
1436 if (info
->stackTop
< 0)
1439 ME_ReleaseStyle(info
->style
);
1440 info
->style
= info
->stack
[info
->stackTop
].style
;
1441 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1442 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1451 ME_StreamInFill(ME_InStream
*stream
)
1453 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1454 (BYTE
*)stream
->buffer
,
1455 sizeof(stream
->buffer
),
1456 (LONG
*)&stream
->dwSize
);
1460 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1464 int from
, to
, nUndoMode
;
1465 int nEventMask
= editor
->nEventMask
;
1466 ME_InStream inStream
;
1467 BOOL invalidRTF
= FALSE
;
1468 ME_Cursor
*selStart
, *selEnd
;
1469 LRESULT num_read
= 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1471 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1472 editor
->nEventMask
= 0;
1474 ME_GetSelectionOfs(editor
, &from
, &to
);
1475 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1477 ME_GetSelection(editor
, &selStart
, &selEnd
);
1478 style
= ME_GetSelectionInsertStyle(editor
);
1480 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1482 /* Don't insert text at the end of the table row */
1483 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1484 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1485 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1487 para
= para
->member
.para
.next_para
;
1488 editor
->pCursors
[0].pPara
= para
;
1489 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1490 editor
->pCursors
[0].nOffset
= 0;
1492 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1494 para
= para
->member
.para
.next_para
;
1495 editor
->pCursors
[0].pPara
= para
;
1496 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1497 editor
->pCursors
[0].nOffset
= 0;
1499 editor
->pCursors
[1] = editor
->pCursors
[0];
1500 } else { /* v1.0 - 3.0 */
1501 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1502 ME_IsInTable(editor
->pCursors
[0].pRun
))
1506 style
= editor
->pBuffer
->pDefaultStyle
;
1507 ME_AddRefStyle(style
);
1508 ME_SetSelection(editor
, 0, 0);
1509 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1510 ME_GetTextLength(editor
), FALSE
);
1512 ME_ClearTempStyle(editor
);
1513 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1517 /* Back up undo mode to a local variable */
1518 nUndoMode
= editor
->nUndoMode
;
1520 /* Only create an undo if SFF_SELECTION is set */
1521 if (!(format
& SFF_SELECTION
))
1522 editor
->nUndoMode
= umIgnore
;
1524 inStream
.editstream
= stream
;
1525 inStream
.editstream
->dwError
= 0;
1526 inStream
.dwSize
= 0;
1527 inStream
.dwUsed
= 0;
1529 if (format
& SF_RTF
)
1531 /* Check if it's really RTF, and if it is not, use plain text */
1532 ME_StreamInFill(&inStream
);
1533 if (!inStream
.editstream
->dwError
)
1535 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1536 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1539 inStream
.editstream
->dwError
= -16;
1544 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1546 if (format
& SF_RTF
) {
1547 from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1549 /* setup the RTF parser */
1550 memset(&parser
, 0, sizeof parser
);
1551 RTFSetEditStream(&parser
, &inStream
);
1552 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1553 parser
.editor
= editor
;
1554 parser
.style
= style
;
1555 WriterInit(&parser
);
1557 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1558 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1559 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1560 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1562 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1563 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1567 /* do the parsing */
1569 RTFFlushOutputBuffer(&parser
);
1570 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1571 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1572 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1574 /* Delete any incomplete table row at the end of the rich text. */
1576 ME_DisplayItem
*para
;
1578 parser
.rtfMinor
= rtfRow
;
1579 /* Complete the table row before deleting it.
1580 * By doing it this way we will have the current paragraph format set
1581 * properly to reflect that is not in the complete table, and undo items
1582 * will be added for this change to the current paragraph format. */
1583 if (parser
.nestingLevel
> 0)
1585 while (parser
.nestingLevel
> 1)
1586 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1587 para
= parser
.tableDef
->tableRowStart
;
1588 ME_RTFSpecialCharHook(&parser
);
1590 para
= parser
.tableDef
->tableRowStart
;
1591 ME_RTFSpecialCharHook(&parser
);
1592 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1593 para
= para
->member
.para
.next_para
;
1596 editor
->pCursors
[1].pPara
= para
;
1597 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1598 editor
->pCursors
[1].nOffset
= 0;
1599 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1600 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1601 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1602 if (parser
.tableDef
)
1603 parser
.tableDef
->tableRowStart
= NULL
;
1606 ME_CheckTablesForCorruption(editor
);
1607 RTFDestroy(&parser
);
1608 if (parser
.lpRichEditOle
)
1609 IRichEditOle_Release(parser
.lpRichEditOle
);
1611 if (parser
.stackTop
> 0)
1613 while (--parser
.stackTop
>= 0)
1615 ME_ReleaseStyle(parser
.style
);
1616 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1618 if (!inStream
.editstream
->dwError
)
1619 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1622 /* Remove last line break, as mandated by tests. This is not affected by
1623 CR/LF counters, since RTF streaming presents only \para tokens, which
1624 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1628 ME_GetSelection(editor
, &selStart
, &selEnd
);
1629 newto
= ME_GetCursorOfs(selEnd
);
1630 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1631 WCHAR lastchar
[3] = {'\0', '\0'};
1632 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1633 ME_Cursor linebreakCursor
= *selEnd
;
1635 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1636 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, FALSE
);
1637 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1638 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1642 to
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1643 num_read
= to
- from
;
1645 style
= parser
.style
;
1647 else if (format
& SF_TEXT
)
1648 num_read
= ME_StreamInText(editor
, format
, &inStream
, style
);
1650 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1651 /* put the cursor at the top */
1652 if (!(format
& SFF_SELECTION
))
1653 ME_SetSelection(editor
, 0, 0);
1656 /* Restore saved undo mode */
1657 editor
->nUndoMode
= nUndoMode
;
1659 /* even if we didn't add an undo, we need to commit anything on the stack */
1660 ME_CommitUndo(editor
);
1662 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1663 if (!(format
& SFF_SELECTION
))
1664 ME_EmptyUndoStack(editor
);
1666 ME_ReleaseStyle(style
);
1667 editor
->nEventMask
= nEventMask
;
1668 ME_UpdateRepaint(editor
, FALSE
);
1669 if (!(format
& SFF_SELECTION
)) {
1670 ME_ClearTempStyle(editor
);
1672 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1673 ME_MoveCaret(editor
);
1674 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1675 ME_SendSelChange(editor
);
1676 ME_SendRequestResize(editor
, FALSE
);
1682 typedef struct tagME_RTFStringStreamStruct
1687 } ME_RTFStringStreamStruct
;
1689 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1691 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1694 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1695 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1696 pStruct
->pos
+= count
;
1702 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1705 ME_RTFStringStreamStruct data
;
1707 data
.string
= string
;
1708 data
.length
= strlen(string
);
1710 es
.dwCookie
= (DWORD_PTR
)&data
;
1711 es
.pfnCallback
= ME_ReadFromRTFString
;
1712 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1717 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1719 const int nLen
= lstrlenW(text
);
1720 const int nTextLen
= ME_GetTextLength(editor
);
1723 WCHAR wLastChar
= ' ';
1725 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1726 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1728 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1729 FIXME("Flags 0x%08x not implemented\n",
1730 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1733 if (chrg
->cpMax
== -1)
1736 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1738 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1739 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1744 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1745 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1749 chrgText
->cpMin
= -1;
1750 chrgText
->cpMax
= -1;
1755 /* when searching up, if cpMin < cpMax, then instead of searching
1756 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1757 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1758 * case, it is always bigger than cpMin.
1760 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1764 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1765 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1771 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1774 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1778 if (flags
& FR_DOWN
) /* Forward search */
1780 /* If possible, find the character before where the search starts */
1781 if ((flags
& FR_WHOLEWORD
) && nMin
)
1783 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1784 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1785 ME_MoveCursorChars(editor
, &cursor
, 1);
1787 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1790 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1792 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1793 int nCurStart
= cursor
.nOffset
;
1796 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
), text
[nMatched
], (flags
& FR_MATCHCASE
)))
1798 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1802 if (nMatched
== nLen
)
1804 ME_DisplayItem
*pNextItem
= pCurItem
;
1805 int nNextStart
= nCurStart
;
1808 /* Check to see if next character is a whitespace */
1809 if (flags
& FR_WHOLEWORD
)
1811 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1813 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1814 nNextStart
= -nMatched
;
1818 wNextChar
= *get_text( &pNextItem
->member
.run
, nNextStart
+ nMatched
);
1822 if (isalnumW(wNextChar
))
1826 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1829 chrgText
->cpMin
= cursor
.nOffset
;
1830 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1832 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1833 return cursor
.nOffset
;
1835 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1837 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1838 nCurStart
= -nMatched
;
1842 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
);
1847 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.len
)
1849 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1854 else /* Backward search */
1856 /* If possible, find the character after where the search ends */
1857 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1859 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1860 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1861 ME_MoveCursorChars(editor
, &cursor
, -1);
1863 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1866 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1868 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1869 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1870 int nCurEnd
= cursor
.nOffset
;
1875 ME_PrevRun(&pCurPara
, &pCurItem
);
1876 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1879 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 ),
1880 text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
) ))
1882 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1886 if (nMatched
== nLen
)
1888 ME_DisplayItem
*pPrevItem
= pCurItem
;
1889 int nPrevEnd
= nCurEnd
;
1893 /* Check to see if previous character is a whitespace */
1894 if (flags
& FR_WHOLEWORD
)
1896 if (nPrevEnd
- nMatched
== 0)
1898 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1900 nPrevEnd
= pPrevItem
->member
.run
.len
+ nMatched
;
1904 wPrevChar
= *get_text( &pPrevItem
->member
.run
, nPrevEnd
- nMatched
- 1 );
1908 if (isalnumW(wPrevChar
))
1912 nStart
= pCurPara
->member
.para
.nCharOfs
1913 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1916 chrgText
->cpMin
= nStart
;
1917 chrgText
->cpMax
= nStart
+ nLen
;
1919 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1922 if (nCurEnd
- nMatched
== 0)
1924 ME_PrevRun(&pCurPara
, &pCurItem
);
1925 /* Don't care about pCurItem becoming NULL here; it's already taken
1926 * care of in the exterior loop condition */
1927 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1931 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 );
1936 if (cursor
.nOffset
< 0)
1938 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1939 cursor
.nOffset
= cursor
.pRun
->member
.run
.len
;
1943 TRACE("not found\n");
1945 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1949 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1954 if (!ex
->cb
|| !pText
) return 0;
1956 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1957 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1959 if (ex
->flags
& GT_SELECTION
)
1962 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
1963 start
= editor
->pCursors
[nStartCur
];
1968 ME_SetCursorToStart(editor
, &start
);
1971 if (ex
->codepage
== CP_UNICODE
)
1973 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
1974 &start
, nChars
, ex
->flags
& GT_USECRLF
);
1978 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1979 we can just take a bigger buffer? :)
1980 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1981 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1983 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1988 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
1989 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
1991 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
);
1992 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
1993 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1994 if (rc
) rc
--; /* do not count 0 terminator */
2001 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
2002 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
2004 if (!strText
) return 0;
2006 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, FALSE
);
2009 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2011 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, FALSE
);
2012 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
2013 nLen
+1, NULL
, NULL
);
2019 static int handle_EM_EXSETSEL( ME_TextEditor
*editor
, int to
, int from
)
2023 TRACE("%d - %d\n", to
, from
);
2025 ME_InvalidateSelection( editor
);
2026 end
= ME_SetSelection( editor
, to
, from
);
2027 ME_InvalidateSelection( editor
);
2028 ITextHost_TxShowCaret( editor
->texthost
, FALSE
);
2029 ME_ShowCaret( editor
);
2030 ME_SendSelChange( editor
);
2035 typedef struct tagME_GlobalDestStruct
2039 } ME_GlobalDestStruct
;
2041 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2043 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2048 pDest
= (WORD
*)lpBuff
;
2049 pSrc
= GlobalLock(pData
->hData
);
2050 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2051 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2053 pData
->nLength
+= i
;
2055 GlobalUnlock(pData
->hData
);
2059 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2061 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2066 pSrc
= GlobalLock(pData
->hData
);
2067 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2068 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2070 pData
->nLength
+= i
;
2072 GlobalUnlock(pData
->hData
);
2076 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2080 ME_GlobalDestStruct gds
;
2081 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2084 if (IsClipboardFormatAvailable(nRTFFormat
))
2085 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2086 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2087 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2091 if (!OpenClipboard(editor
->hWnd
))
2093 gds
.hData
= GetClipboardData(cf
);
2095 es
.dwCookie
= (DWORD_PTR
)&gds
;
2096 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2097 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2103 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2105 LPDATAOBJECT dataObj
= NULL
;
2108 if (editor
->cPasswordMask
)
2109 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2111 if(editor
->lpOleCallback
)
2114 range
.cpMin
= ME_GetCursorOfs(start
);
2115 range
.cpMax
= range
.cpMin
+ nChars
;
2116 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2118 if(FAILED(hr
) || !dataObj
)
2119 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2121 hr
= OleSetClipboard(dataObj
);
2122 IDataObject_Release(dataObj
);
2124 return SUCCEEDED(hr
) != 0;
2127 /* helper to send a msg filter notification */
2129 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2133 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2134 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2135 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2136 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2138 msgf
.wParam
= *wParam
;
2139 msgf
.lParam
= *lParam
;
2140 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2142 *wParam
= msgf
.wParam
;
2143 *lParam
= msgf
.lParam
;
2144 msgf
.wParam
= *wParam
;
2149 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2151 ME_DisplayItem
*startPara
, *endPara
;
2152 ME_DisplayItem
*prev_para
;
2153 ME_Cursor
*from
, *to
;
2157 if (!editor
->AutoURLDetect_bEnable
) return;
2159 ME_GetSelection(editor
, &from
, &to
);
2161 /* Find paragraph previous to the one that contains start cursor */
2162 startPara
= from
->pPara
;
2163 prev_para
= startPara
->member
.para
.prev_para
;
2164 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2166 /* Find paragraph that contains end cursor */
2167 endPara
= to
->pPara
->member
.para
.next_para
;
2169 start
.pPara
= startPara
;
2170 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2172 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2174 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2178 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2180 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2181 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2183 if (editor
->bMouseCaptured
)
2185 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2186 editor
->nSelectionType
= stPosition
;
2194 editor
->nUDArrowX
= -1;
2200 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2201 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2205 editor
->nUDArrowX
= -1;
2206 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2207 if (editor
->styleFlags
& ES_READONLY
)
2209 if (ME_IsSelection(editor
))
2211 ME_DeleteSelection(editor
);
2212 ME_CommitUndo(editor
);
2214 else if (nKey
== VK_DELETE
)
2216 /* Delete stops group typing.
2217 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2218 ME_DeleteTextAtCursor(editor
, 1, 1);
2219 ME_CommitUndo(editor
);
2221 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2223 BOOL bDeletionSucceeded
;
2224 /* Backspace can be grouped for a single undo */
2225 ME_ContinueCoalescingTransaction(editor
);
2226 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2227 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2228 /* Deletion was prevented so the cursor is moved back to where it was.
2229 * (e.g. this happens when trying to delete cell boundaries)
2231 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2233 ME_CommitCoalescingUndo(editor
);
2237 ME_MoveCursorFromTableRowStartParagraph(editor
);
2238 ME_UpdateSelectionLinkAttribute(editor
);
2239 ME_UpdateRepaint(editor
, FALSE
);
2240 ME_SendRequestResize(editor
, FALSE
);
2243 if (editor
->bDialogMode
)
2248 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2250 if (editor
->hwndParent
)
2253 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2254 if (HIWORD(dw
) == DC_HASDEFID
)
2256 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2259 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
2260 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2268 if (editor
->styleFlags
& ES_MULTILINE
)
2270 ME_Cursor cursor
= editor
->pCursors
[0];
2271 ME_DisplayItem
*para
= cursor
.pPara
;
2273 const WCHAR endl
= '\r';
2274 const WCHAR endlv10
[] = {'\r','\n'};
2277 if (editor
->styleFlags
& ES_READONLY
) {
2278 MessageBeep(MB_ICONERROR
);
2282 ME_GetSelectionOfs(editor
, &from
, &to
);
2283 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2285 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2286 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2287 /* Add a new table row after this row. */
2288 para
= ME_AppendTableRow(editor
, para
);
2289 para
= para
->member
.para
.next_para
;
2290 editor
->pCursors
[0].pPara
= para
;
2291 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2292 editor
->pCursors
[0].nOffset
= 0;
2293 editor
->pCursors
[1] = editor
->pCursors
[0];
2294 ME_CommitUndo(editor
);
2295 ME_CheckTablesForCorruption(editor
);
2296 ME_UpdateRepaint(editor
, FALSE
);
2299 else if (para
== editor
->pCursors
[1].pPara
&&
2300 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2301 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2302 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2304 /* Insert a newline before the table. */
2305 para
= para
->member
.para
.prev_para
;
2306 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2307 editor
->pCursors
[0].pPara
= para
;
2308 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2309 editor
->pCursors
[1] = editor
->pCursors
[0];
2310 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2311 editor
->pCursors
[0].pRun
->member
.run
.style
);
2312 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2313 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2314 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2315 editor
->pCursors
[0].pPara
= para
;
2316 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2317 editor
->pCursors
[1] = editor
->pCursors
[0];
2318 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2319 ME_CommitCoalescingUndo(editor
);
2320 ME_CheckTablesForCorruption(editor
);
2321 ME_UpdateRepaint(editor
, FALSE
);
2324 } else { /* v1.0 - 3.0 */
2325 ME_DisplayItem
*para
= cursor
.pPara
;
2326 if (ME_IsInTable(para
))
2328 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2331 ME_ContinueCoalescingTransaction(editor
);
2332 para
= ME_AppendTableRow(editor
, para
);
2333 editor
->pCursors
[0].pPara
= para
;
2334 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2335 editor
->pCursors
[0].nOffset
= 0;
2336 editor
->pCursors
[1] = editor
->pCursors
[0];
2337 ME_CommitCoalescingUndo(editor
);
2338 ME_UpdateRepaint(editor
, FALSE
);
2342 ME_ContinueCoalescingTransaction(editor
);
2343 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2344 !ME_IsInTable(para
->member
.para
.prev_para
))
2346 /* Insert newline before table */
2347 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2349 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2350 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2352 editor
->pCursors
[0].nOffset
= 0;
2353 editor
->pCursors
[1] = editor
->pCursors
[0];
2354 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2355 editor
->pCursors
[0].pRun
->member
.run
.style
);
2357 editor
->pCursors
[1] = editor
->pCursors
[0];
2358 para
= ME_AppendTableRow(editor
, para
);
2359 editor
->pCursors
[0].pPara
= para
;
2360 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2361 editor
->pCursors
[0].nOffset
= 0;
2362 editor
->pCursors
[1] = editor
->pCursors
[0];
2364 ME_CommitCoalescingUndo(editor
);
2365 ME_UpdateRepaint(editor
, FALSE
);
2371 style
= ME_GetInsertStyle(editor
, 0);
2372 ME_SaveTempStyle(editor
);
2373 ME_ContinueCoalescingTransaction(editor
);
2375 ME_InsertEndRowFromCursor(editor
, 0);
2377 if (!editor
->bEmulateVersion10
)
2378 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2380 ME_InsertTextFromCursor(editor
, 0, endlv10
, 2, style
);
2381 ME_ReleaseStyle(style
);
2382 ME_CommitCoalescingUndo(editor
);
2385 ME_UpdateSelectionLinkAttribute(editor
);
2386 ME_UpdateRepaint(editor
, FALSE
);
2392 if (editor
->bDialogMode
&& editor
->hwndParent
)
2393 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2396 if (editor
->bDialogMode
&& editor
->hwndParent
)
2397 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2402 handle_EM_EXSETSEL( editor
, 0, -1 );
2408 return ME_Paste(editor
);
2416 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2417 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2420 result
= ME_Copy(editor
, selStart
, nChars
);
2421 if (result
&& nKey
== 'X')
2423 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2424 ME_CommitUndo(editor
);
2425 ME_UpdateRepaint(editor
, TRUE
);
2446 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2447 editor
->nUDArrowX
= -1;
2454 chf
.cbSize
= sizeof(chf
);
2456 ME_GetSelectionCharFormat(editor
, &chf
);
2457 ME_DumpStyleToBuf(&chf
, buf
);
2458 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2462 ME_CheckCharOffsets(editor
);
2469 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2470 LPARAM flags
, BOOL unicode
)
2474 if (editor
->bMouseCaptured
)
2478 wstr
= (WCHAR
)charCode
;
2481 CHAR charA
= charCode
;
2482 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2485 if (editor
->styleFlags
& ES_READONLY
) {
2486 MessageBeep(MB_ICONERROR
);
2487 return 0; /* FIXME really 0 ? */
2490 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2492 ME_Cursor cursor
= editor
->pCursors
[0];
2493 ME_DisplayItem
*para
= cursor
.pPara
;
2495 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2496 ME_GetSelectionOfs(editor
, &from
, &to
);
2498 /* v4.1 allows tabs to be inserted with ctrl key down */
2499 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2501 ME_DisplayItem
*para
;
2502 BOOL bSelectedRow
= FALSE
;
2504 para
= cursor
.pPara
;
2505 if (ME_IsSelection(editor
) &&
2506 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2507 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2508 para
->member
.para
.prev_para
->type
== diParagraph
)
2510 para
= para
->member
.para
.prev_para
;
2511 bSelectedRow
= TRUE
;
2513 if (ME_IsInTable(para
))
2515 ME_TabPressedInTable(editor
, bSelectedRow
);
2516 ME_CommitUndo(editor
);
2519 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2520 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2522 para
= para
->member
.para
.next_para
;
2523 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2524 para
= para
->member
.para
.next_para
;
2525 editor
->pCursors
[0].pPara
= para
;
2526 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2527 editor
->pCursors
[0].nOffset
= 0;
2528 editor
->pCursors
[1] = editor
->pCursors
[0];
2531 } else { /* v1.0 - 3.0 */
2532 if (ME_IsInTable(cursor
.pRun
) &&
2533 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2536 /* Text should not be inserted at the end of the table. */
2541 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2542 /* WM_CHAR is restricted to nTextLimit */
2543 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2545 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2546 ME_SaveTempStyle(editor
);
2547 ME_ContinueCoalescingTransaction(editor
);
2548 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2549 ME_ReleaseStyle(style
);
2550 ME_CommitCoalescingUndo(editor
);
2551 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2554 ME_UpdateSelectionLinkAttribute(editor
);
2555 ME_UpdateRepaint(editor
, FALSE
);
2560 /* Process the message and calculate the new click count.
2562 * returns: The click count if it is mouse down event, else returns 0. */
2563 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2566 static int clickNum
= 0;
2567 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2570 if ((msg
== WM_LBUTTONDBLCLK
) ||
2571 (msg
== WM_RBUTTONDBLCLK
) ||
2572 (msg
== WM_MBUTTONDBLCLK
) ||
2573 (msg
== WM_XBUTTONDBLCLK
))
2575 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2578 if ((msg
== WM_LBUTTONDOWN
) ||
2579 (msg
== WM_RBUTTONDOWN
) ||
2580 (msg
== WM_MBUTTONDOWN
) ||
2581 (msg
== WM_XBUTTONDOWN
))
2583 static MSG prevClickMsg
;
2585 /* Compare the editor instead of the hwnd so that the this
2586 * can still be done for windowless richedit controls. */
2587 clickMsg
.hwnd
= (HWND
)editor
;
2588 clickMsg
.message
= msg
;
2589 clickMsg
.wParam
= wParam
;
2590 clickMsg
.lParam
= lParam
;
2591 clickMsg
.time
= GetMessageTime();
2592 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2593 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2594 if ((clickNum
!= 0) &&
2595 (clickMsg
.message
== prevClickMsg
.message
) &&
2596 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2597 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2598 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2599 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2600 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2606 prevClickMsg
= clickMsg
;
2613 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2619 DWORD messagePos
= GetMessagePos();
2620 pt
.x
= (short)LOWORD(messagePos
);
2621 pt
.y
= (short)HIWORD(messagePos
);
2625 sbi
.cbSize
= sizeof(sbi
);
2626 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2627 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2628 PtInRect(&sbi
.rcScrollBar
, pt
))
2630 ITextHost_TxSetCursor(editor
->texthost
,
2631 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2634 sbi
.cbSize
= sizeof(sbi
);
2635 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2636 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2637 PtInRect(&sbi
.rcScrollBar
, pt
))
2639 ITextHost_TxSetCursor(editor
->texthost
,
2640 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2644 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2646 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2647 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2650 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2651 pt
.y
< editor
->rcFormat
.top
&&
2652 pt
.x
< editor
->rcFormat
.left
)
2654 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2657 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2659 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2660 ITextHost_TxSetCursor(editor
->texthost
,
2661 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2663 ITextHost_TxSetCursor(editor
->texthost
,
2664 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2667 if (pt
.x
< editor
->rcFormat
.left
)
2669 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2672 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2677 run
= &cursor
.pRun
->member
.run
;
2678 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2679 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2681 ITextHost_TxSetCursor(editor
->texthost
,
2682 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2687 if (ME_IsSelection(editor
))
2689 int selStart
, selEnd
;
2690 int offset
= ME_GetCursorOfs(&cursor
);
2692 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2693 if (selStart
<= offset
&& selEnd
>= offset
) {
2694 ITextHost_TxSetCursor(editor
->texthost
,
2695 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2701 ITextHost_TxSetCursor(editor
->texthost
,
2702 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2706 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2708 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2709 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2710 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2711 editor
->rcFormat
.right
-= 1;
2714 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2719 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2721 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2722 if(selrange
.cpMin
== selrange
.cpMax
)
2723 seltype
|= SEL_EMPTY
;
2726 /* FIXME: Handle objects */
2727 seltype
|= SEL_TEXT
;
2728 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2729 seltype
|= SEL_MULTICHAR
;
2731 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2733 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2739 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2741 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2747 ed
->hwndParent
= NULL
;
2748 ed
->sizeWindow
.cx
= ed
->sizeWindow
.cy
= 0;
2749 ed
->texthost
= texthost
;
2750 ed
->bEmulateVersion10
= bEmulateVersion10
;
2752 ITextHost_TxGetPropertyBits(texthost
,
2753 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2754 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2755 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2756 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2757 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2759 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2760 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2761 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2762 ed
->pBuffer
= ME_MakeText();
2763 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2764 ed
->nAvailWidth
= 0; /* wrap to client area */
2765 ME_MakeFirstParagraph(ed
);
2766 /* The four cursors are for:
2767 * 0 - The position where the caret is shown
2768 * 1 - The anchored end of the selection (for normal selection)
2769 * 2 & 3 - The anchored start and end respectively for word, line,
2770 * or paragraph selection.
2773 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2774 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2775 ed
->pCursors
[1] = ed
->pCursors
[0];
2776 ed
->pCursors
[2] = ed
->pCursors
[0];
2777 ed
->pCursors
[3] = ed
->pCursors
[1];
2778 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2779 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2781 ed
->rgbBackColor
= -1;
2782 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2783 ed
->bCaretAtEnd
= FALSE
;
2785 ed
->nModifyStep
= 0;
2786 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2787 list_init( &ed
->undo_stack
);
2788 list_init( &ed
->redo_stack
);
2789 ed
->nUndoStackSize
= 0;
2790 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2791 ed
->nUndoMode
= umAddToUndo
;
2792 ed
->nParagraphs
= 1;
2793 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2794 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2795 ed
->bHideSelection
= FALSE
;
2796 ed
->pfnWordBreak
= NULL
;
2797 ed
->lpOleCallback
= NULL
;
2798 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2799 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2800 ed
->AutoURLDetect_bEnable
= FALSE
;
2801 ed
->bHaveFocus
= FALSE
;
2802 ed
->bDialogMode
= FALSE
;
2803 ed
->bMouseCaptured
= FALSE
;
2804 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2806 ed
->pFontCache
[i
].nRefs
= 0;
2807 ed
->pFontCache
[i
].nAge
= 0;
2808 ed
->pFontCache
[i
].hFont
= NULL
;
2811 ME_CheckCharOffsets(ed
);
2812 ed
->bDefaultFormatRect
= TRUE
;
2813 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2815 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2816 ed
->selofs
= SELECTIONBAR_WIDTH
;
2817 ed
->styleFlags
|= ES_SELECTIONBAR
;
2821 ed
->nSelectionType
= stPosition
;
2823 ed
->cPasswordMask
= 0;
2824 if (props
& TXTBIT_USEPASSWORD
)
2825 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2827 if (props
& TXTBIT_AUTOWORDSEL
)
2828 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2829 if (props
& TXTBIT_MULTILINE
) {
2830 ed
->styleFlags
|= ES_MULTILINE
;
2831 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2833 ed
->bWordWrap
= FALSE
;
2835 if (props
& TXTBIT_READONLY
)
2836 ed
->styleFlags
|= ES_READONLY
;
2837 if (!(props
& TXTBIT_HIDESELECTION
))
2838 ed
->styleFlags
|= ES_NOHIDESEL
;
2839 if (props
& TXTBIT_SAVESELECTION
)
2840 ed
->styleFlags
|= ES_SAVESEL
;
2841 if (props
& TXTBIT_VERTICAL
)
2842 ed
->styleFlags
|= ES_VERTICAL
;
2843 if (props
& TXTBIT_DISABLEDRAG
)
2844 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2846 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2848 /* Default scrollbar information */
2849 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2850 ed
->vert_si
.nMin
= 0;
2851 ed
->vert_si
.nMax
= 0;
2852 ed
->vert_si
.nPage
= 0;
2853 ed
->vert_si
.nPos
= 0;
2855 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2856 ed
->horz_si
.nMin
= 0;
2857 ed
->horz_si
.nMax
= 0;
2858 ed
->horz_si
.nPage
= 0;
2859 ed
->horz_si
.nPos
= 0;
2861 OleInitialize(NULL
);
2866 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2868 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2869 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2872 ME_ClearTempStyle(editor
);
2873 ME_EmptyUndoStack(editor
);
2876 ME_DestroyDisplayItem(p
);
2879 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2880 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2882 if (editor
->pFontCache
[i
].hFont
)
2883 DeleteObject(editor
->pFontCache
[i
].hFont
);
2885 if (editor
->rgbBackColor
!= -1)
2886 DeleteObject(editor
->hbrBackground
);
2887 if(editor
->lpOleCallback
)
2888 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2889 ITextHost_Release(editor
->texthost
);
2892 FREE_OBJ(editor
->pBuffer
);
2893 FREE_OBJ(editor
->pCursors
);
2898 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2903 case DLL_PROCESS_ATTACH
:
2904 DisableThreadLibraryCalls(hinstDLL
);
2905 me_heap
= HeapCreate (0, 0x10000, 0);
2906 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2907 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2911 case DLL_PROCESS_DETACH
:
2912 if (lpvReserved
) break;
2913 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2914 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2915 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2916 UnregisterClassA("RichEdit50A", 0);
2917 if (ME_ListBoxRegistered
)
2918 UnregisterClassW(REListBox20W
, 0);
2919 if (ME_ComboBoxRegistered
)
2920 UnregisterClassW(REComboBox20W
, 0);
2922 HeapDestroy (me_heap
);
2929 static const char * const edit_messages
[] = {
2958 "EM_SETPASSWORDCHAR",
2959 "EM_EMPTYUNDOBUFFER",
2960 "EM_GETFIRSTVISIBLELINE",
2962 "EM_SETWORDBREAKPROC",
2963 "EM_GETWORDBREAKPROC",
2964 "EM_GETPASSWORDCHAR",
2974 static const char * const richedit_messages
[] = {
2979 "EM_EXLINEFROMCHAR",
2985 "EM_GETOLEINTERFACE",
2995 "EM_SETOLECALLBACK",
2997 "EM_SETTARGETDEVICE",
3005 "EM_GETWORDBREAKPROCEX",
3006 "EM_SETWORDBREAKPROCEX",
3008 "EM_UNKNOWN_USER_83",
3013 "EM_STOPGROUPTYPING",
3017 "EM_GETAUTOURLDETECT",
3020 "EM_GETTEXTLENGTHEX",
3023 "EM_UNKNOWN_USER_98",
3024 "EM_UNKNOWN_USER_99",
3025 "EM_SETPUNCTUATION",
3026 "EM_GETPUNCTUATION",
3027 "EM_SETWORDWRAPMODE",
3028 "EM_GETWORDWRAPMODE",
3034 "EM_UNKNOWN_USER_109",
3035 "EM_UNKNOWN_USER_110",
3036 "EM_UNKNOWN_USER_111",
3037 "EM_UNKNOWN_USER_112",
3038 "EM_UNKNOWN_USER_113",
3039 "EM_UNKNOWN_USER_114",
3040 "EM_UNKNOWN_USER_115",
3041 "EM_UNKNOWN_USER_116",
3042 "EM_UNKNOWN_USER_117",
3043 "EM_UNKNOWN_USER_118",
3044 "EM_UNKNOWN_USER_119",
3045 "EM_SETLANGOPTIONS",
3046 "EM_GETLANGOPTIONS",
3047 "EM_GETIMECOMPMODE",
3051 "EM_SETIMEMODEBIAS",
3056 get_msg_name(UINT msg
)
3058 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
3059 return edit_messages
[msg
- EM_GETSEL
];
3060 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
3061 return richedit_messages
[msg
- EM_CANPASTE
];
3065 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3069 ME_Cursor cursor
; /* The start of the clicked text. */
3072 x
= (short)LOWORD(lParam
);
3073 y
= (short)HIWORD(lParam
);
3074 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3075 if (!isExact
) return;
3077 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3078 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3079 { /* The clicked run has CFE_LINK set */
3080 info
.nmhdr
.hwndFrom
= NULL
;
3081 info
.nmhdr
.idFrom
= 0;
3082 info
.nmhdr
.code
= EN_LINK
;
3084 info
.wParam
= wParam
;
3085 info
.lParam
= lParam
;
3087 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3088 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ cursor
.pRun
->member
.run
.len
;
3089 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3093 #define UNSUPPORTED_MSG(e) \
3095 FIXME(#e ": stub\n"); \
3096 *phresult = S_FALSE; \
3099 /* Handle messages for windowless and windoweded richedit controls.
3101 * The LRESULT that is returned is a return value for window procs,
3102 * and the phresult parameter is the COM return code needed by the
3103 * text services interface. */
3104 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3105 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3111 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3112 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3113 UNSUPPORTED_MSG(EM_FMTLINES
)
3114 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3115 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3116 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3117 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3118 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3119 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3120 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3121 UNSUPPORTED_MSG(EM_GETREDONAME
)
3122 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3123 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3124 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3125 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3126 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3127 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3128 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3129 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
3130 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3131 UNSUPPORTED_MSG(EM_SETMARGINS
)
3132 UNSUPPORTED_MSG(EM_SETPALETTE
)
3133 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3134 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3135 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3137 /* Messages specific to Richedit controls */
3140 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3142 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3145 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
|DLGC_HASSETSEL
;
3147 editor
->bDialogMode
= TRUE
;
3148 if (editor
->styleFlags
& ES_MULTILINE
)
3149 code
|= DLGC_WANTMESSAGE
;
3152 case EM_EMPTYUNDOBUFFER
:
3153 ME_EmptyUndoStack(editor
);
3157 /* Note: wParam/lParam can be NULL */
3159 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3160 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3161 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3162 if ((*pfrom
|*pto
) & 0xFFFF0000)
3164 return MAKELONG(*pfrom
,*pto
);
3168 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3169 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3170 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3173 case EM_SETUNDOLIMIT
:
3175 if ((int)wParam
< 0)
3176 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3178 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3179 /* Setting a max stack size keeps wine from getting killed
3180 for hogging memory. Windows allocates all this memory at once, so
3181 no program would realistically set a value above our maximum. */
3182 return editor
->nUndoLimit
;
3185 return !list_empty( &editor
->undo_stack
);
3187 return !list_empty( &editor
->redo_stack
);
3188 case WM_UNDO
: /* FIXME: actually not the same */
3190 return ME_Undo(editor
);
3192 return ME_Redo(editor
);
3195 /* these flags are equivalent to the ES_* counterparts */
3196 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3197 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3198 DWORD settings
= editor
->styleFlags
& mask
;
3204 /* these flags are equivalent to ES_* counterparts, except for
3205 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3206 * but is still stored in editor->styleFlags. */
3207 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3208 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3209 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3210 DWORD settings
= mask
& editor
->styleFlags
;
3211 DWORD oldSettings
= settings
;
3212 DWORD changedSettings
;
3228 changedSettings
= oldSettings
^ settings
;
3230 if (changedSettings
) {
3231 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3233 if (changedSettings
& ECO_SELECTIONBAR
)
3235 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3236 if (settings
& ECO_SELECTIONBAR
) {
3237 assert(!editor
->selofs
);
3238 editor
->selofs
= SELECTIONBAR_WIDTH
;
3239 editor
->rcFormat
.left
+= editor
->selofs
;
3241 editor
->rcFormat
.left
-= editor
->selofs
;
3244 ME_RewrapRepaint(editor
);
3247 if (changedSettings
& settings
& ECO_VERTICAL
)
3248 FIXME("ECO_VERTICAL not implemented yet!\n");
3249 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3250 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3251 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3252 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3253 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3254 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3255 if (changedSettings
& settings
& ECO_WANTRETURN
)
3256 FIXME("ECO_WANTRETURN not implemented yet!\n");
3257 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3258 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3265 handle_EM_EXSETSEL( editor
, wParam
, lParam
);
3268 case EM_SETSCROLLPOS
:
3270 POINT
*point
= (POINT
*)lParam
;
3271 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3274 case EM_AUTOURLDETECT
:
3276 if (wParam
==1 || wParam
==0)
3278 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3281 return E_INVALIDARG
;
3283 case EM_GETAUTOURLDETECT
:
3285 return editor
->AutoURLDetect_bEnable
;
3289 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3291 return handle_EM_EXSETSEL( editor
, range
.cpMin
, range
.cpMax
);
3293 case EM_SHOWSCROLLBAR
:
3306 flags
= WS_HSCROLL
|WS_VSCROLL
;
3313 editor
->styleFlags
|= flags
;
3314 if (flags
& WS_HSCROLL
)
3315 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3316 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3317 if (flags
& WS_VSCROLL
)
3318 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3319 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3321 editor
->styleFlags
&= ~flags
;
3322 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3329 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3332 BOOL bRtf
, bUnicode
, bSelection
, bUTF8
;
3333 int oldModify
= editor
->nModifyStep
;
3334 static const char utf8_bom
[] = {0xef, 0xbb, 0xbf};
3336 if (!pStruct
) return 0;
3338 /* If we detect ascii rtf at the start of the string,
3339 * we know it isn't unicode. */
3340 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3341 !strncmp((char *)lParam
, "{\\urtf", 6)));
3342 bUnicode
= !bRtf
&& pStruct
->codepage
== CP_UNICODE
;
3343 bUTF8
= (lParam
&& (!strncmp((char *)lParam
, utf8_bom
, 3)));
3345 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3346 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3347 pStruct
->flags
, pStruct
->codepage
);
3349 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3351 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3352 style
= ME_GetSelectionInsertStyle(editor
);
3353 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3356 ME_SetCursorToStart(editor
, &start
);
3357 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3358 style
= editor
->pBuffer
->pDefaultStyle
;
3362 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3364 /* FIXME: The length returned doesn't include the rtf control
3365 * characters, only the actual text. */
3366 len
= lParam
? strlen((char *)lParam
) : 0;
3369 if (bUTF8
&& !bUnicode
) {
3370 wszText
= ME_ToUnicode(CP_UTF8
, (void *)(lParam
+3), &len
);
3371 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3372 ME_EndToUnicode(CP_UTF8
, wszText
);
3374 wszText
= ME_ToUnicode(pStruct
->codepage
, (void *)lParam
, &len
);
3375 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3376 ME_EndToUnicode(pStruct
->codepage
, wszText
);
3381 ME_ReleaseStyle(style
);
3382 ME_UpdateSelectionLinkAttribute(editor
);
3386 ME_SetCursorToStart(editor
, &cursor
);
3387 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3389 ME_CommitUndo(editor
);
3390 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3392 editor
->nModifyStep
= oldModify
;
3393 ME_EmptyUndoStack(editor
);
3395 ME_UpdateRepaint(editor
, FALSE
);
3398 case EM_SETBKGNDCOLOR
:
3401 if (editor
->rgbBackColor
!= -1) {
3402 DeleteObject(editor
->hbrBackground
);
3403 lColor
= editor
->rgbBackColor
;
3405 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3409 editor
->rgbBackColor
= -1;
3410 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3414 editor
->rgbBackColor
= lParam
;
3415 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3417 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3418 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3422 return editor
->nModifyStep
== 0 ? 0 : -1;
3426 editor
->nModifyStep
= 1;
3428 editor
->nModifyStep
= 0;
3432 case EM_SETREADONLY
:
3435 editor
->styleFlags
|= ES_READONLY
;
3437 editor
->styleFlags
&= ~ES_READONLY
;
3440 case EM_SETEVENTMASK
:
3442 DWORD nOldMask
= editor
->nEventMask
;
3444 editor
->nEventMask
= lParam
;
3447 case EM_GETEVENTMASK
:
3448 return editor
->nEventMask
;
3449 case EM_SETCHARFORMAT
:
3451 CHARFORMAT2W buf
, *p
;
3452 BOOL bRepaint
= TRUE
;
3453 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3454 if (p
== NULL
) return 0;
3455 if (wParam
& SCF_ALL
) {
3456 if (editor
->mode
& TM_PLAINTEXT
) {
3457 ME_SetDefaultCharFormat(editor
, p
);
3460 ME_SetCursorToStart(editor
, &start
);
3461 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3462 editor
->nModifyStep
= 1;
3464 } else if (wParam
& SCF_SELECTION
) {
3465 if (editor
->mode
& TM_PLAINTEXT
)
3467 if (wParam
& SCF_WORD
) {
3468 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3471 bRepaint
= ME_IsSelection(editor
);
3472 ME_SetSelectionCharFormat(editor
, p
);
3473 if (bRepaint
) editor
->nModifyStep
= 1;
3474 } else { /* SCF_DEFAULT */
3475 ME_SetDefaultCharFormat(editor
, p
);
3477 ME_CommitUndo(editor
);
3480 ME_WrapMarkedParagraphs(editor
);
3481 ME_UpdateScrollBar(editor
);
3486 case EM_GETCHARFORMAT
:
3488 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3489 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3490 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3491 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3492 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3494 tmp
.cbSize
= sizeof(tmp
);
3496 ME_GetDefaultCharFormat(editor
, &tmp
);
3498 ME_GetSelectionCharFormat(editor
, &tmp
);
3499 ME_CopyToCFAny(dst
, &tmp
);
3502 case EM_SETPARAFORMAT
:
3504 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3505 ME_WrapMarkedParagraphs(editor
);
3506 ME_UpdateScrollBar(editor
);
3508 ME_CommitUndo(editor
);
3511 case EM_GETPARAFORMAT
:
3512 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3513 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3514 case EM_GETFIRSTVISIBLELINE
:
3516 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3517 int y
= editor
->vert_si
.nPos
;
3522 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3523 if (p
->type
== diTextEnd
)
3525 if (p
->type
== diParagraph
) {
3526 ypara
= p
->member
.para
.pt
.y
;
3529 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3530 yend
= ystart
+ p
->member
.row
.nHeight
;
3538 case EM_HIDESELECTION
:
3540 editor
->bHideSelection
= (wParam
!= 0);
3541 ME_InvalidateSelection(editor
);
3546 if (!(editor
->styleFlags
& ES_MULTILINE
))
3548 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
3554 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3555 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3556 ME_CommitUndo(editor
);
3557 ME_UpdateRepaint(editor
, TRUE
);
3562 int from
, to
, nStartCursor
;
3565 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3566 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &len
);
3567 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3569 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3570 style
= ME_GetSelectionInsertStyle(editor
);
3571 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3572 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3573 ME_ReleaseStyle(style
);
3574 /* drop temporary style if line end */
3576 * FIXME question: does abc\n mean: put abc,
3577 * clear temp style, put \n? (would require a change)
3579 if (len
>0 && wszText
[len
-1] == '\n')
3580 ME_ClearTempStyle(editor
);
3581 ME_EndToUnicode(codepage
, wszText
);
3582 ME_CommitUndo(editor
);
3583 ME_UpdateSelectionLinkAttribute(editor
);
3585 ME_EmptyUndoStack(editor
);
3586 ME_UpdateRepaint(editor
, FALSE
);
3589 case EM_SCROLLCARET
:
3590 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3597 BOOL bRepaint
= LOWORD(lParam
);
3600 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3601 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3602 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3603 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3604 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3605 if (editor
->mode
& TM_RICHTEXT
) {
3607 ME_SetCursorToStart(editor
, &start
);
3608 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3610 ME_SetDefaultCharFormat(editor
, &fmt
);
3612 ME_CommitUndo(editor
);
3613 ME_MarkAllForWrapping(editor
);
3614 ME_WrapMarkedParagraphs(editor
);
3615 ME_UpdateScrollBar(editor
);
3623 ME_SetCursorToStart(editor
, &cursor
);
3624 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3627 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3628 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3629 !strncmp((char *)lParam
, "{\\urtf", 6))
3631 /* Undocumented: WM_SETTEXT supports RTF text */
3632 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3637 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3638 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &textLen
);
3639 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3644 /* uses default style! */
3645 if (!(editor
->styleFlags
& ES_MULTILINE
))
3650 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3653 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3655 ME_EndToUnicode(codepage
, wszText
);
3659 TRACE("WM_SETTEXT - NULL\n");
3660 ME_SetCursorToStart(editor
, &cursor
);
3661 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3662 ME_SetSelection(editor
, 0, 0);
3663 editor
->nModifyStep
= 0;
3664 ME_CommitUndo(editor
);
3665 ME_EmptyUndoStack(editor
);
3666 ME_UpdateRepaint(editor
, FALSE
);
3671 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3672 if (IsClipboardFormatAvailable(nRTFFormat
))
3674 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3679 case WM_MBUTTONDOWN
:
3685 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3686 int nChars
= nTo
- nFrom
;
3687 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3689 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3691 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3692 ME_CommitUndo(editor
);
3693 ME_UpdateRepaint(editor
, TRUE
);
3697 case WM_GETTEXTLENGTH
:
3699 GETTEXTLENGTHEX how
;
3701 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3702 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3703 how
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3704 return ME_GetTextLengthEx(editor
, &how
);
3706 case EM_GETTEXTLENGTHEX
:
3707 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3711 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3712 ex
.flags
= GT_USECRLF
;
3713 ex
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3714 ex
.lpDefaultChar
= NULL
;
3715 ex
.lpUsedDefChar
= NULL
;
3716 return ME_GetTextEx(editor
, &ex
, lParam
);
3719 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3722 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3723 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3724 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3725 nTo
- nFrom
, unicode
);
3727 case EM_GETSCROLLPOS
:
3729 POINT
*point
= (POINT
*)lParam
;
3730 point
->x
= editor
->horz_si
.nPos
;
3731 point
->y
= editor
->vert_si
.nPos
;
3732 /* 16-bit scaled value is returned as stored in scrollinfo */
3733 if (editor
->horz_si
.nMax
> 0xffff)
3734 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3735 if (editor
->vert_si
.nMax
> 0xffff)
3736 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3739 case EM_GETTEXTRANGE
:
3741 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3743 int nStart
= rng
->chrg
.cpMin
;
3744 int nEnd
= rng
->chrg
.cpMax
;
3745 int textlength
= ME_GetTextLength(editor
);
3747 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3748 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3749 if (nStart
< 0) return 0;
3750 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3752 if (nStart
>= nEnd
) return 0;
3754 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3755 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3759 ME_DisplayItem
*run
;
3760 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3761 unsigned int nCharsLeft
= nMaxChars
;
3762 char *dest
= (char *) lParam
;
3763 BOOL wroteNull
= FALSE
;
3765 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3766 unicode
? "Unicode" : "Ansi");
3768 run
= ME_FindRowWithNumber(editor
, wParam
);
3772 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3773 && run
->type
== diRun
)
3775 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3778 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3781 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3783 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
3784 nCharsLeft
, NULL
, NULL
);
3785 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3786 nCharsLeft
-= nCopy
;
3789 /* append line termination, space allowing */
3793 *((WCHAR
*)dest
) = '\0';
3800 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3801 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3803 case EM_GETLINECOUNT
:
3805 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3808 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3810 while (item
!= editor
->pBuffer
->pLast
)
3812 assert(item
->type
== diParagraph
);
3813 prev_para
= ME_FindItemBack(item
, diRun
);
3815 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3817 nRows
+= item
->member
.para
.nRows
;
3818 item
= item
->member
.para
.next_para
;
3820 last_para
= ME_FindItemBack(item
, diRun
);
3822 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3823 if (editor
->bEmulateVersion10
&& prev_para
&&
3824 last_para
->member
.run
.nCharOfs
== 0 &&
3825 prev_para
->member
.run
.len
== 1 &&
3826 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
3828 /* In 1.0 emulation, the last solitary \r at the very end of the text
3829 (if one exists) is NOT a line break.
3830 FIXME: this is an ugly hack. This should have a more regular model. */
3834 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3835 return max(1, nRows
);
3837 case EM_LINEFROMCHAR
:
3840 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3842 return ME_RowNumberFromCharOfs(editor
, wParam
);
3844 case EM_EXLINEFROMCHAR
:
3847 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3849 return ME_RowNumberFromCharOfs(editor
, lParam
);
3853 ME_DisplayItem
*item
, *para
;
3857 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3859 item
= ME_FindRowWithNumber(editor
, wParam
);
3862 para
= ME_GetParagraph(item
);
3863 item
= ME_FindItemFwd(item
, diRun
);
3864 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3865 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3870 ME_DisplayItem
*item
, *item_end
;
3871 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3872 ME_DisplayItem
*para
, *run
;
3874 if (wParam
> ME_GetTextLength(editor
))
3878 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3881 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3882 item
= ME_RowStart(run
);
3883 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3884 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3885 if (item_end
->type
== diStartRow
) {
3886 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3888 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3889 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3890 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
3892 nChars
= nNextLineOfs
- nThisLineOfs
;
3893 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3896 case EM_EXLIMITTEXT
:
3898 if ((int)lParam
< 0)
3901 editor
->nTextLimit
= 65536;
3903 editor
->nTextLimit
= (int) lParam
;
3909 editor
->nTextLimit
= 65536;
3911 editor
->nTextLimit
= (int) wParam
;
3914 case EM_GETLIMITTEXT
:
3916 return editor
->nTextLimit
;
3922 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3923 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3926 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3927 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3928 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3931 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3932 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3940 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3941 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3944 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3945 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3946 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3949 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3950 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3956 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3957 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3959 case EM_FINDTEXTEXW
:
3961 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3962 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3965 if (!wParam
|| !lParam
)
3967 *(int *)wParam
= editor
->nZoomNumerator
;
3968 *(int *)lParam
= editor
->nZoomDenominator
;
3971 return ME_SetZoom(editor
, wParam
, lParam
);
3972 case EM_CHARFROMPOS
:
3975 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
3977 return ME_GetCursorOfs(&cursor
);
3981 case EM_POSFROMCHAR
:
3983 ME_DisplayItem
*pPara
, *pRun
;
3984 int nCharOfs
, nOffset
, nLength
;
3988 /* detect which API version we're dealing with */
3989 if (wParam
>= 0x40000)
3991 nLength
= ME_GetTextLength(editor
);
3992 nCharOfs
= min(nCharOfs
, nLength
);
3993 nCharOfs
= max(nCharOfs
, 0);
3995 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
3996 assert(pRun
->type
== diRun
);
3997 pt
.y
= pRun
->member
.run
.pt
.y
;
3998 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
3999 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
4000 pt
.x
+= editor
->rcFormat
.left
;
4002 pt
.x
-= editor
->horz_si
.nPos
;
4003 pt
.y
-= editor
->vert_si
.nPos
;
4005 if (wParam
>= 0x40000) {
4006 *(POINTL
*)wParam
= pt
;
4008 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
4015 ME_SetDefaultFormatRect(editor
);
4017 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
4018 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
4019 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
4021 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
4022 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
4024 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
4026 if (editor
->styleFlags
& WS_VSCROLL
)
4028 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
4029 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
4031 if (editor
->styleFlags
& WS_HSCROLL
)
4033 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
4034 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
4040 text
= (unicode
? (void*)((CREATESTRUCTW
*)lParam
)->lpszName
4041 : (void*)((CREATESTRUCTA
*)lParam
)->lpszName
);
4047 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
4048 textW
= ME_ToUnicode(codepage
, text
, &len
);
4049 if (!(editor
->styleFlags
& ES_MULTILINE
))
4052 while(textW
[len
] != '\0' && textW
[len
] != '\r' && textW
[len
] != '\n')
4055 ME_InsertTextFromCursor(editor
, 0, textW
, len
, editor
->pBuffer
->pDefaultStyle
);
4056 ME_EndToUnicode(codepage
, textW
);
4057 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4058 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
4061 ME_CommitUndo(editor
);
4062 ME_WrapMarkedParagraphs(editor
);
4063 ME_MoveCaret(editor
);
4067 ME_DestroyEditor(editor
);
4071 return ME_SetCursor(editor
);
4073 case WM_LBUTTONDBLCLK
:
4074 case WM_LBUTTONDOWN
:
4076 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4077 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4078 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4080 ITextHost_TxSetFocus(editor
->texthost
);
4081 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
4082 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
4083 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
4084 editor
->bMouseCaptured
= TRUE
;
4085 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4086 if (!ME_SetCursor(editor
)) goto do_default
;
4090 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4091 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4093 if (editor
->bMouseCaptured
)
4094 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4095 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4096 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4097 if (editor
->bMouseCaptured
)
4098 ME_SetCursor(editor
);
4101 if (editor
->bMouseCaptured
) {
4102 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4103 editor
->bMouseCaptured
= FALSE
;
4105 if (editor
->nSelectionType
== stDocument
)
4106 editor
->nSelectionType
= stPosition
;
4107 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4108 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4112 ME_SetCursor(editor
);
4113 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4117 case WM_RBUTTONDOWN
:
4118 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4119 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4120 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4123 case WM_CONTEXTMENU
:
4124 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4128 editor
->bHaveFocus
= TRUE
;
4129 ME_ShowCaret(editor
);
4130 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4133 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4134 editor
->bHaveFocus
= FALSE
;
4135 ME_HideCaret(editor
);
4136 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4139 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4142 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4143 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4147 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4148 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4150 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4154 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4155 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4157 return ME_Char(editor
, wParam
, lParam
, unicode
);
4161 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4162 if(wParam
<= 0x000fffff)
4164 if(wParam
> 0xffff) /* convert to surrogates */
4167 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4168 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4170 ME_Char(editor
, wParam
, 0, TRUE
);
4176 case EM_STOPGROUPTYPING
:
4177 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4181 const int scrollUnit
= 7;
4183 switch(LOWORD(wParam
))
4186 ME_ScrollAbs(editor
, 0, 0);
4189 ME_ScrollAbs(editor
,
4190 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4191 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4194 ME_ScrollLeft(editor
, scrollUnit
);
4197 ME_ScrollRight(editor
, scrollUnit
);
4200 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4203 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4206 case SB_THUMBPOSITION
:
4208 int pos
= HIWORD(wParam
);
4209 if (editor
->horz_si
.nMax
> 0xffff)
4210 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4211 ME_HScrollAbs(editor
, pos
);
4217 case EM_SCROLL
: /* fall through */
4223 origNPos
= editor
->vert_si
.nPos
;
4226 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
4227 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
4228 if (lineHeight
<= 0) lineHeight
= 24;
4230 switch(LOWORD(wParam
))
4233 ME_ScrollAbs(editor
, 0, 0);
4236 ME_ScrollAbs(editor
,
4237 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4238 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4241 ME_ScrollUp(editor
,lineHeight
);
4244 ME_ScrollDown(editor
,lineHeight
);
4247 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4250 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4253 case SB_THUMBPOSITION
:
4255 int pos
= HIWORD(wParam
);
4256 if (editor
->vert_si
.nMax
> 0xffff)
4257 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4258 ME_VScrollAbs(editor
, pos
);
4262 if (msg
== EM_SCROLL
)
4263 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4269 UINT pulScrollLines
;
4272 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4273 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4276 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4278 gcWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4280 if (abs(gcWheelDelta
) >= WHEEL_DELTA
)
4284 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4288 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4290 numerator
= numerator
+ (gcWheelDelta
/ WHEEL_DELTA
) * 10;
4291 if (numerator
>= 10 && numerator
<= 500)
4292 ME_SetZoom(editor
, numerator
, 100);
4294 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
4295 /* FIXME follow the original */
4297 ME_ScrollDown(editor
,pulScrollLines
* (-gcWheelDelta
/ WHEEL_DELTA
) * 8);
4304 *((RECT
*)lParam
) = editor
->rcFormat
;
4305 if (editor
->bDefaultFormatRect
)
4306 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4316 RECT
*rc
= (RECT
*)lParam
;
4318 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4319 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4322 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4323 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4324 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4325 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4326 } else if (wParam
== 1) {
4327 /* MSDN incorrectly says a wParam value of 1 causes the
4328 * lParam rect to be used as a relative offset,
4329 * however, the tests show it just prevents min/max bound
4331 editor
->rcFormat
.top
= rc
->top
- border
;
4332 editor
->rcFormat
.left
= rc
->left
- border
;
4333 editor
->rcFormat
.bottom
= rc
->bottom
;
4334 editor
->rcFormat
.right
= rc
->right
+ border
;
4338 editor
->bDefaultFormatRect
= FALSE
;
4342 ME_SetDefaultFormatRect(editor
);
4343 editor
->bDefaultFormatRect
= TRUE
;
4345 ME_MarkAllForWrapping(editor
);
4346 ME_WrapMarkedParagraphs(editor
);
4347 ME_UpdateScrollBar(editor
);
4348 if (msg
!= EM_SETRECTNP
)
4352 case EM_REQUESTRESIZE
:
4353 ME_SendRequestResize(editor
, TRUE
);
4361 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4362 if (editor
->bDefaultFormatRect
) {
4363 ME_SetDefaultFormatRect(editor
);
4365 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4366 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4368 editor
->prevClientRect
= clientRect
;
4369 ME_RewrapRepaint(editor
);
4372 /* IME messages to make richedit controls IME aware */
4373 case WM_IME_SETCONTEXT
:
4374 case WM_IME_CONTROL
:
4376 case WM_IME_COMPOSITIONFULL
:
4378 case WM_IME_STARTCOMPOSITION
:
4380 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4381 ME_DeleteSelection(editor
);
4382 ME_CommitUndo(editor
);
4383 ME_UpdateRepaint(editor
, FALSE
);
4386 case WM_IME_COMPOSITION
:
4390 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4391 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4392 ME_DeleteSelection(editor
);
4393 ME_SaveTempStyle(editor
);
4394 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4396 LPWSTR lpCompStr
= NULL
;
4398 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4400 dwIndex
= GCS_COMPSTR
;
4402 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4403 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4404 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4405 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4406 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4407 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4409 if (dwIndex
== GCS_COMPSTR
)
4410 ME_SetSelection(editor
,editor
->imeStartIndex
,
4411 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4413 ME_ReleaseStyle(style
);
4414 ME_CommitUndo(editor
);
4415 ME_UpdateRepaint(editor
, FALSE
);
4418 case WM_IME_ENDCOMPOSITION
:
4420 ME_DeleteSelection(editor
);
4421 editor
->imeStartIndex
=-1;
4424 case EM_GETOLEINTERFACE
:
4426 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4427 return CreateIRichEditOle(editor
, ppvObj
);
4429 case EM_GETPASSWORDCHAR
:
4431 return editor
->cPasswordMask
;
4433 case EM_SETOLECALLBACK
:
4434 if(editor
->lpOleCallback
)
4435 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4436 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4437 if(editor
->lpOleCallback
)
4438 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4440 case EM_GETWORDBREAKPROC
:
4441 return (LRESULT
)editor
->pfnWordBreak
;
4442 case EM_SETWORDBREAKPROC
:
4444 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4446 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4447 return (LRESULT
)pfnOld
;
4449 case EM_GETTEXTMODE
:
4450 return editor
->mode
;
4451 case EM_SETTEXTMODE
:
4456 if (ME_GetTextLength(editor
) ||
4457 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4458 return E_UNEXPECTED
;
4460 /* Check for mutually exclusive flags in adjacent bits of wParam */
4461 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4462 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4463 return E_INVALIDARG
;
4465 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4467 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4468 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4469 if (wParam
& TM_PLAINTEXT
) {
4470 /* Clear selection since it should be possible to select the
4471 * end of text run for rich text */
4472 ME_InvalidateSelection(editor
);
4473 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4474 editor
->pCursors
[1] = editor
->pCursors
[0];
4475 /* plain text can only have the default style. */
4476 ME_ClearTempStyle(editor
);
4477 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4478 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4479 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4482 /* FIXME: Currently no support for undo level and code page options */
4483 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4486 case EM_SETPASSWORDCHAR
:
4488 editor
->cPasswordMask
= wParam
;
4489 ME_RewrapRepaint(editor
);
4492 case EM_SETTARGETDEVICE
:
4495 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4496 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4498 editor
->bWordWrap
= new;
4499 editor
->nAvailWidth
= 0; /* wrap to client area */
4500 ME_RewrapRepaint(editor
);
4503 int width
= max(0, lParam
);
4504 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4505 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4507 editor
->nAvailWidth
= width
;
4508 editor
->bWordWrap
= TRUE
;
4509 ME_RewrapRepaint(editor
);
4511 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4516 *phresult
= S_FALSE
;
4522 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4523 LPARAM lParam
, BOOL unicode
)
4525 ME_TextEditor
*editor
;
4527 LRESULT lresult
= 0;
4529 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4530 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4532 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4535 if (msg
== WM_NCCREATE
)
4537 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4538 ITextHost
*texthost
;
4540 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4541 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4542 return texthost
!= NULL
;
4546 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4558 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4559 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4560 ME_SendOldNotify(editor
, EN_UPDATE
);
4561 /* Erase area outside of the formatting rectangle */
4562 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4565 rc
.bottom
= editor
->rcFormat
.top
;
4566 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4567 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4569 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4571 rc
.top
= editor
->rcFormat
.bottom
;
4572 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4573 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4575 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4577 rc
.right
= editor
->rcFormat
.left
;
4578 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4579 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4581 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4583 rc
.left
= editor
->rcFormat
.right
;
4584 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4585 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4588 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4589 EndPaint(editor
->hWnd
, &ps
);
4594 HDC hDC
= (HDC
)wParam
;
4597 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4598 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4604 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4605 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4607 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4608 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4609 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4610 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4613 case EM_SETREADONLY
:
4616 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4617 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4618 dwStyle
&= ~ES_READONLY
;
4620 dwStyle
|= ES_READONLY
;
4621 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4625 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4628 if (hresult
== S_FALSE
)
4629 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4631 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4632 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4637 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4639 BOOL unicode
= TRUE
;
4641 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4642 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4645 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4648 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4650 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4653 /******************************************************************
4654 * RichEditANSIWndProc (RICHED20.10)
4656 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4658 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4661 /******************************************************************
4662 * RichEdit10ANSIWndProc (RICHED20.9)
4664 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4666 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4668 ITextHost
*texthost
;
4669 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4671 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4672 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4673 return texthost
!= NULL
;
4675 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4678 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4680 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4683 /* Fill buffer with srcChars unicode characters from the start cursor.
4685 * buffer: destination buffer
4686 * buflen: length of buffer in characters excluding the NULL terminator.
4687 * start: start of editor text to copy into buffer.
4688 * srcChars: Number of characters to use from the editor text.
4689 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4691 * returns the number of characters written excluding the NULL terminator.
4693 * The written text is always NULL terminated.
4695 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4696 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
)
4698 ME_DisplayItem
*pRun
, *pNextRun
;
4699 const WCHAR
*pStart
= buffer
;
4700 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4704 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4705 if (editor
->bEmulateVersion10
) bCRLF
= FALSE
;
4709 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4711 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4712 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4714 /* No '\r' is appended to the last paragraph. */
4715 while (srcChars
&& buflen
&& pNextRun
)
4717 int nFlags
= pRun
->member
.run
.nFlags
;
4719 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4721 if (buflen
== 1) break;
4722 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4723 * EM_GETTEXTEX, however, this is done for copying text which
4724 * also uses this function. */
4725 srcChars
-= min(nLen
, srcChars
);
4729 nLen
= min(nLen
, srcChars
);
4733 nLen
= min(nLen
, buflen
);
4736 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4741 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4743 nLen
= pRun
->member
.run
.len
;
4744 str
= get_text( &pRun
->member
.run
, 0 );
4747 return buffer
- pStart
;
4750 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4755 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4756 wcW
.lpfnWndProc
= RichEditWndProcW
;
4758 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4759 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4761 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4762 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4763 wcW
.lpszMenuName
= NULL
;
4765 if (is_version_nt())
4767 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4768 if (!RegisterClassW(&wcW
)) return FALSE
;
4769 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4770 if (!RegisterClassW(&wcW
)) return FALSE
;
4774 /* WNDCLASSA/W have the same layout */
4775 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4776 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4777 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4778 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4781 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4782 wcA
.lpfnWndProc
= RichEditWndProcA
;
4784 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4785 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4787 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4788 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4789 wcA
.lpszMenuName
= NULL
;
4790 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4791 if (!RegisterClassA(&wcA
)) return FALSE
;
4792 wcA
.lpszClassName
= "RichEdit50A";
4793 if (!RegisterClassA(&wcA
)) return FALSE
;
4798 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4799 /* FIXME: Not implemented */
4800 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4801 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4802 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4805 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4806 /* FIXME: Not implemented */
4807 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4808 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4809 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4812 /******************************************************************
4813 * REExtendedRegisterClass (RICHED20.8)
4815 * FIXME undocumented
4816 * Need to check for errors and implement controls and callbacks
4818 LRESULT WINAPI
REExtendedRegisterClass(void)
4823 FIXME("semi stub\n");
4827 wcW
.hInstance
= NULL
;
4830 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4831 wcW
.lpszMenuName
= NULL
;
4833 if (!ME_ListBoxRegistered
)
4835 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4836 wcW
.lpfnWndProc
= REListWndProc
;
4837 wcW
.lpszClassName
= REListBox20W
;
4838 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4841 if (!ME_ComboBoxRegistered
)
4843 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4844 wcW
.lpfnWndProc
= REComboWndProc
;
4845 wcW
.lpszClassName
= REComboBox20W
;
4846 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4850 if (ME_ListBoxRegistered
)
4852 if (ME_ComboBoxRegistered
)
4858 static BOOL
isurlspecial(WCHAR c
)
4860 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4861 return strchrW( special_chars
, c
) != NULL
;
4865 * This proc takes a selection, and scans it forward in order to select the span
4866 * of a possible URL candidate. A possible URL candidate must start with isalnum
4867 * or one of the following special characters: *|/\+%#@ and must consist entirely
4868 * of the characters allowed to start the URL, plus : (colon) which may occur
4869 * at most once, and not at either end.
4871 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
4872 const ME_Cursor
*start
,
4874 ME_Cursor
*candidate_min
,
4875 ME_Cursor
*candidate_max
)
4877 ME_Cursor cursor
= *start
;
4878 BOOL foundColon
= FALSE
;
4879 BOOL candidateStarted
= FALSE
;
4880 WCHAR lastAcceptedChar
= '\0';
4884 WCHAR
*strStart
= get_text( &cursor
.pRun
->member
.run
, 0 );
4885 WCHAR
*str
= strStart
+ cursor
.nOffset
;
4886 int nLen
= cursor
.pRun
->member
.run
.len
- cursor
.nOffset
;
4889 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4891 /* Find start of candidate */
4892 if (!candidateStarted
)
4897 if (isalnumW(*str
) || isurlspecial(*str
))
4899 cursor
.nOffset
= str
- strStart
;
4900 *candidate_min
= cursor
;
4901 candidateStarted
= TRUE
;
4902 lastAcceptedChar
= *str
++;
4909 /* Find end of candidate */
4910 if (candidateStarted
) {
4914 if (*str
== ':' && !foundColon
) {
4916 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
4917 cursor
.nOffset
= str
- strStart
;
4918 if (lastAcceptedChar
== ':')
4919 ME_MoveCursorChars(editor
, &cursor
, -1);
4920 *candidate_max
= cursor
;
4923 lastAcceptedChar
= *str
++;
4927 /* End of paragraph: skip it if before candidate span, or terminates
4928 current active span */
4929 if (candidateStarted
) {
4930 if (lastAcceptedChar
== ':')
4931 ME_MoveCursorChars(editor
, &cursor
, -1);
4932 *candidate_max
= cursor
;
4937 /* Reaching this point means no span was found, so get next span */
4938 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
4939 if (candidateStarted
) {
4940 /* There are no further runs, so take end of text as end of candidate */
4941 cursor
.nOffset
= str
- strStart
;
4942 if (lastAcceptedChar
== ':')
4943 ME_MoveCursorChars(editor
, &cursor
, -1);
4944 *candidate_max
= cursor
;
4947 *candidate_max
= *candidate_min
= cursor
;
4953 if (candidateStarted
) {
4954 /* There are no further runs, so take end of text as end of candidate */
4955 if (lastAcceptedChar
== ':')
4956 ME_MoveCursorChars(editor
, &cursor
, -1);
4957 *candidate_max
= cursor
;
4960 *candidate_max
= *candidate_min
= cursor
;
4965 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4967 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
4969 #define MAX_PREFIX_LEN 9
4971 const WCHAR text
[MAX_PREFIX_LEN
];
4974 {{'p','r','o','s','p','e','r','o',':'}, 9},
4975 {{'t','e','l','n','e','t',':'}, 7},
4976 {{'g','o','p','h','e','r',':'}, 7},
4977 {{'m','a','i','l','t','o',':'}, 7},
4978 {{'h','t','t','p','s',':'}, 6},
4979 {{'f','i','l','e',':'}, 5},
4980 {{'n','e','w','s',':'}, 5},
4981 {{'w','a','i','s',':'}, 5},
4982 {{'n','n','t','p',':'}, 5},
4983 {{'h','t','t','p',':'}, 5},
4984 {{'w','w','w','.'}, 4},
4985 {{'f','t','p',':'}, 4},
4987 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
4990 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, FALSE
);
4991 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
4993 if (nChars
< prefixes
[i
].length
) continue;
4994 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
4998 #undef MAX_PREFIX_LEN
5002 * This proc walks through the indicated selection and evaluates whether each
5003 * section identified by ME_FindNextURLCandidate and in-between sections have
5004 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5005 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5007 * Since this function can cause runs to be split, do not depend on the value
5008 * of the start cursor at the end of the function.
5010 * nChars may be set to INT_MAX to update to the end of the text.
5012 * Returns TRUE if at least one section was modified.
5014 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
5016 BOOL modified
= FALSE
;
5017 ME_Cursor startCur
= *start
;
5019 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
5024 ME_Cursor candidateStart
, candidateEnd
;
5026 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
5027 &candidateStart
, &candidateEnd
))
5029 /* Section before candidate is not an URL */
5030 int cMin
= ME_GetCursorOfs(&candidateStart
);
5031 int cMax
= ME_GetCursorOfs(&candidateEnd
);
5033 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
5034 candidateStart
= candidateEnd
;
5035 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
5039 /* No more candidates until end of selection */
5043 if (startCur
.pRun
!= candidateStart
.pRun
||
5044 startCur
.nOffset
!= candidateStart
.nOffset
)
5046 /* CFE_LINK effect should be consistently unset */
5047 link
.cbSize
= sizeof(link
);
5048 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5049 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
5051 /* CFE_LINK must be unset from this range */
5052 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5053 link
.cbSize
= sizeof(link
);
5054 link
.dwMask
= CFM_LINK
;
5056 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5057 /* Update candidateEnd since setting character formats may split
5058 * runs, which can cause a cursor to be at an invalid offset within
5060 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
5062 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
5063 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
5068 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
5069 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
5071 /* CFE_LINK effect should be consistently set */
5072 link
.cbSize
= sizeof(link
);
5073 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5074 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
5076 /* CFE_LINK must be set on this range */
5077 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5078 link
.cbSize
= sizeof(link
);
5079 link
.dwMask
= CFM_LINK
;
5080 link
.dwEffects
= CFE_LINK
;
5081 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5085 startCur
= candidateEnd
;
5086 } while (nChars
> 0);