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 #include <undocuser.h>
235 #define STACK_SIZE_DEFAULT 100
236 #define STACK_SIZE_MAX 1000
238 #define TEXT_LIMIT_DEFAULT 32767
240 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
242 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
243 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
245 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
246 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 static HCURSOR hLeft
;
249 BOOL me_debug
= FALSE
;
250 HANDLE me_heap
= NULL
;
252 static BOOL ME_ListBoxRegistered
= FALSE
;
253 static BOOL ME_ComboBoxRegistered
= FALSE
;
255 static inline BOOL
is_version_nt(void)
257 return !(GetVersion() & 0x80000000);
260 static ME_TextBuffer
*ME_MakeText(void) {
262 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
264 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
265 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
271 p1
->member
.para
.next_para
= p2
;
272 p2
->member
.para
.prev_para
= p1
;
273 p2
->member
.para
.nCharOfs
= 0;
277 buf
->pCharStyle
= NULL
;
283 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
286 LRESULT total_bytes_read
= 0;
287 BOOL is_read
= FALSE
;
288 DWORD cp
= CP_ACP
, copy
= 0;
289 char conv_buf
[4 + STREAMIN_BUFFER_SIZE
]; /* up to 4 additional UTF-8 bytes */
291 static const char bom_utf8
[] = {0xEF, 0xBB, 0xBF};
293 TRACE("%08x %p\n", dwFormat
, stream
);
297 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
301 ME_StreamInFill(stream
);
302 if (stream
->editstream
->dwError
)
306 total_bytes_read
+= stream
->dwSize
;
309 if (!(dwFormat
& SF_UNICODE
))
311 char * buf
= stream
->buffer
;
312 DWORD size
= stream
->dwSize
, end
;
317 if (stream
->dwSize
>= 3 && !memcmp(stream
->buffer
, bom_utf8
, 3))
329 memcpy(conv_buf
+ copy
, buf
, size
);
334 while ((buf
[end
-1] & 0xC0) == 0x80)
337 --total_bytes_read
; /* strange, but seems to match windows */
339 if (buf
[end
-1] & 0x80)
342 if ((buf
[end
-1] & 0xE0) == 0xC0)
344 if ((buf
[end
-1] & 0xF0) == 0xE0)
346 if ((buf
[end
-1] & 0xF8) == 0xF0)
349 if (size
- end
>= need
)
351 /* we have enough bytes for this sequence */
356 /* need more bytes, so don't transcode this sequence */
364 nWideChars
= MultiByteToWideChar(cp
, 0, buf
, end
, wszText
, STREAMIN_BUFFER_SIZE
);
371 memcpy(conv_buf
, buf
+ end
, size
- end
);
378 nWideChars
= stream
->dwSize
>> 1;
379 pText
= (WCHAR
*)stream
->buffer
;
382 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
383 if (stream
->dwSize
== 0)
387 return total_bytes_read
;
390 static void ME_ApplyBorderProperties(RTF_Info
*info
,
391 ME_BorderRect
*borderRect
,
392 RTFBorder
*borderDef
)
395 ME_Border
*pBorders
[] = {&borderRect
->top
,
399 for (i
= 0; i
< 4; i
++)
401 RTFColor
*colorDef
= info
->colorList
;
402 pBorders
[i
]->width
= borderDef
[i
].width
;
403 colorNum
= borderDef
[i
].color
;
404 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
405 colorDef
= colorDef
->rtfNextColor
;
407 pBorders
[i
]->colorRef
= RGB(
408 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
409 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
410 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
412 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
416 void ME_RTFCharAttrHook(RTF_Info
*info
)
419 fmt
.cbSize
= sizeof(fmt
);
423 switch(info
->rtfMinor
)
426 /* FIXME add more flags once they're implemented */
427 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
428 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
429 fmt
.yHeight
= 12*20; /* 12pt */
430 fmt
.wWeight
= FW_NORMAL
;
431 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
434 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
435 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
436 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
439 fmt
.dwMask
= CFM_ITALIC
;
440 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
443 fmt
.dwMask
= CFM_UNDERLINETYPE
;
444 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
446 case rtfDotUnderline
:
447 fmt
.dwMask
= CFM_UNDERLINETYPE
;
448 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
451 fmt
.dwMask
= CFM_UNDERLINETYPE
;
452 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
454 case rtfWordUnderline
:
455 fmt
.dwMask
= CFM_UNDERLINETYPE
;
456 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
459 fmt
.dwMask
= CFM_UNDERLINETYPE
;
460 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
463 fmt
.dwMask
= CFM_STRIKEOUT
;
464 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
468 case rtfSubScrShrink
:
469 case rtfSuperScrShrink
:
471 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
472 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
473 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
474 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
477 fmt
.dwMask
= CFM_HIDDEN
;
478 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
481 fmt
.dwMask
= CFM_BACKCOLOR
;
483 if (info
->rtfParam
== 0)
484 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
485 else if (info
->rtfParam
!= rtfNoParam
)
487 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
488 if (c
&& c
->rtfCBlue
>= 0)
489 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
491 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
495 fmt
.dwMask
= CFM_COLOR
;
497 if (info
->rtfParam
== 0)
498 fmt
.dwEffects
= CFE_AUTOCOLOR
;
499 else if (info
->rtfParam
!= rtfNoParam
)
501 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
502 if (c
&& c
->rtfCBlue
>= 0)
503 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
505 fmt
.dwEffects
= CFE_AUTOCOLOR
;
510 if (info
->rtfParam
!= rtfNoParam
)
512 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
515 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
516 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
517 fmt
.bCharSet
= f
->rtfFCharSet
;
518 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
519 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
524 fmt
.dwMask
= CFM_SIZE
;
525 if (info
->rtfParam
!= rtfNoParam
)
526 fmt
.yHeight
= info
->rtfParam
*10;
531 RTFFlushOutputBuffer(info
);
532 /* FIXME too slow ? how come ? */
533 style2
= ME_ApplyStyle(info
->style
, &fmt
);
534 ME_ReleaseStyle(info
->style
);
535 info
->style
= style2
;
536 info
->styleChanged
= TRUE
;
540 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
541 the same tags mean different things in different contexts */
542 void ME_RTFParAttrHook(RTF_Info
*info
)
545 fmt
.cbSize
= sizeof(fmt
);
548 switch(info
->rtfMinor
)
550 case rtfParDef
: /* restores default paragraph attributes */
551 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
552 info
->borderType
= RTFBorderParaLeft
;
553 else /* v1.0 - 3.0 */
554 info
->borderType
= RTFBorderParaTop
;
555 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
556 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
558 /* TODO: numbering, shading */
559 fmt
.wAlignment
= PFA_LEFT
;
561 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
562 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
563 fmt
.wBorderSpace
= 0;
564 fmt
.bLineSpacingRule
= 0;
565 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
566 fmt
.dyLineSpacing
= 0;
567 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
569 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
570 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
573 ME_DisplayItem
*para
;
574 /* We are just after a table row. */
575 RTFFlushOutputBuffer(info
);
576 cursor
= info
->editor
->pCursors
[0];
578 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
579 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
581 /* Since the table row end, no text has been inserted, and the \intbl
582 * control word has not be used. We can confirm that we are not in a
585 info
->tableDef
->tableRowStart
= NULL
;
586 info
->canInheritInTbl
= FALSE
;
589 } else { /* v1.0 - v3.0 */
590 fmt
.dwMask
|= PFM_TABLE
;
591 fmt
.wEffects
&= ~PFE_TABLE
;
595 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
597 while (info
->rtfParam
> info
->nestingLevel
) {
598 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
599 ZeroMemory(tableDef
, sizeof(RTFTable
));
600 tableDef
->parent
= info
->tableDef
;
601 info
->tableDef
= tableDef
;
603 RTFFlushOutputBuffer(info
);
604 if (tableDef
->tableRowStart
&&
605 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
607 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
608 para
= para
->member
.para
.next_para
;
609 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
610 tableDef
->tableRowStart
= para
;
614 cursor
= info
->editor
->pCursors
[0];
615 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
616 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
617 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
620 info
->nestingLevel
++;
622 info
->canInheritInTbl
= FALSE
;
627 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
629 if (info
->nestingLevel
< 1)
634 info
->tableDef
= ALLOC_OBJ(RTFTable
);
635 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
637 tableDef
= info
->tableDef
;
638 RTFFlushOutputBuffer(info
);
639 if (tableDef
->tableRowStart
&&
640 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
642 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
643 para
= para
->member
.para
.next_para
;
644 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
645 tableDef
->tableRowStart
= para
;
649 cursor
= info
->editor
->pCursors
[0];
650 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
651 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
652 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
654 info
->nestingLevel
= 1;
655 info
->canInheritInTbl
= TRUE
;
658 } else { /* v1.0 - v3.0 */
659 fmt
.dwMask
|= PFM_TABLE
;
660 fmt
.wEffects
|= PFE_TABLE
;
665 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
666 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
667 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
668 fmt
.dxOffset
= -info
->rtfParam
;
671 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
672 fmt
.dwMask
= PFM_STARTINDENT
;
673 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
676 fmt
.dwMask
= PFM_RIGHTINDENT
;
677 fmt
.dxRightIndent
= info
->rtfParam
;
681 fmt
.dwMask
= PFM_ALIGNMENT
;
682 fmt
.wAlignment
= PFA_LEFT
;
685 fmt
.dwMask
= PFM_ALIGNMENT
;
686 fmt
.wAlignment
= PFA_RIGHT
;
689 fmt
.dwMask
= PFM_ALIGNMENT
;
690 fmt
.wAlignment
= PFA_CENTER
;
693 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
694 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
698 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
699 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
700 fmt
.dwMask
= PFM_TABSTOPS
;
703 fmt
.dwMask
= PFM_KEEP
;
704 fmt
.wEffects
= PFE_KEEP
;
706 case rtfNoWidowControl
:
707 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
708 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
711 fmt
.dwMask
= PFM_KEEPNEXT
;
712 fmt
.wEffects
= PFE_KEEPNEXT
;
715 fmt
.dwMask
= PFM_SPACEAFTER
;
716 fmt
.dySpaceAfter
= info
->rtfParam
;
719 fmt
.dwMask
= PFM_SPACEBEFORE
;
720 fmt
.dySpaceBefore
= info
->rtfParam
;
722 case rtfSpaceBetween
:
723 fmt
.dwMask
= PFM_LINESPACING
;
724 if ((int)info
->rtfParam
> 0)
726 fmt
.dyLineSpacing
= info
->rtfParam
;
727 fmt
.bLineSpacingRule
= 3;
731 fmt
.dyLineSpacing
= info
->rtfParam
;
732 fmt
.bLineSpacingRule
= 4;
735 case rtfSpaceMultiply
:
736 fmt
.dwMask
= PFM_LINESPACING
;
737 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
738 fmt
.bLineSpacingRule
= 5;
741 fmt
.dwMask
= PFM_NUMBERING
;
742 fmt
.wNumbering
= PFN_BULLET
;
745 fmt
.dwMask
= PFM_NUMBERING
;
746 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
748 case rtfParNumDecimal
:
749 fmt
.dwMask
= PFM_NUMBERING
;
750 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
752 case rtfParNumIndent
:
753 fmt
.dwMask
= PFM_NUMBERINGTAB
;
754 fmt
.wNumberingTab
= info
->rtfParam
;
756 case rtfParNumStartAt
:
757 fmt
.dwMask
= PFM_NUMBERINGSTART
;
758 fmt
.wNumberingStart
= info
->rtfParam
;
761 info
->borderType
= RTFBorderParaLeft
;
762 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
763 if (!(fmt
.dwMask
& PFM_BORDER
))
765 fmt
.wBorderSpace
= 0;
766 fmt
.wBorderWidth
= 1;
770 fmt
.dwMask
= PFM_BORDER
;
773 info
->borderType
= RTFBorderParaRight
;
774 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
775 if (!(fmt
.dwMask
& PFM_BORDER
))
777 fmt
.wBorderSpace
= 0;
778 fmt
.wBorderWidth
= 1;
782 fmt
.dwMask
= PFM_BORDER
;
785 info
->borderType
= RTFBorderParaTop
;
786 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
787 if (!(fmt
.dwMask
& PFM_BORDER
))
789 fmt
.wBorderSpace
= 0;
790 fmt
.wBorderWidth
= 1;
794 fmt
.dwMask
= PFM_BORDER
;
796 case rtfBorderBottom
:
797 info
->borderType
= RTFBorderParaBottom
;
798 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
799 if (!(fmt
.dwMask
& PFM_BORDER
))
801 fmt
.wBorderSpace
= 0;
802 fmt
.wBorderWidth
= 1;
806 fmt
.dwMask
= PFM_BORDER
;
808 case rtfBorderSingle
:
809 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
810 /* we assume that borders have been created before (RTF spec) */
811 fmt
.wBorders
&= ~0x700;
812 fmt
.wBorders
|= 1 << 8;
813 fmt
.dwMask
= PFM_BORDER
;
816 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
817 /* we assume that borders have been created before (RTF spec) */
818 fmt
.wBorders
&= ~0x700;
819 fmt
.wBorders
|= 2 << 8;
820 fmt
.dwMask
= PFM_BORDER
;
822 case rtfBorderShadow
:
823 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
824 /* we assume that borders have been created before (RTF spec) */
825 fmt
.wBorders
&= ~0x700;
826 fmt
.wBorders
|= 10 << 8;
827 fmt
.dwMask
= PFM_BORDER
;
829 case rtfBorderDouble
:
830 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
831 /* we assume that borders have been created before (RTF spec) */
832 fmt
.wBorders
&= ~0x700;
833 fmt
.wBorders
|= 7 << 8;
834 fmt
.dwMask
= PFM_BORDER
;
837 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
838 /* we assume that borders have been created before (RTF spec) */
839 fmt
.wBorders
&= ~0x700;
840 fmt
.wBorders
|= 11 << 8;
841 fmt
.dwMask
= PFM_BORDER
;
845 int borderSide
= info
->borderType
& RTFBorderSideMask
;
846 RTFTable
*tableDef
= info
->tableDef
;
847 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
848 /* we assume that borders have been created before (RTF spec) */
849 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
850 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
853 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
855 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
856 border
->width
= info
->rtfParam
;
859 fmt
.dwMask
= PFM_BORDER
;
863 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
864 /* we assume that borders have been created before (RTF spec) */
865 fmt
.wBorderSpace
= info
->rtfParam
;
866 fmt
.dwMask
= PFM_BORDER
;
870 RTFTable
*tableDef
= info
->tableDef
;
871 int borderSide
= info
->borderType
& RTFBorderSideMask
;
872 int borderType
= info
->borderType
& RTFBorderTypeMask
;
875 case RTFBorderTypePara
:
876 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
878 /* v1.0 - 3.0 treat paragraph and row borders the same. */
879 case RTFBorderTypeRow
:
881 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
884 case RTFBorderTypeCell
:
885 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
886 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
893 fmt
.dwMask
= PFM_RTLPARA
;
894 fmt
.wEffects
= PFE_RTLPARA
;
897 fmt
.dwMask
= PFM_RTLPARA
;
902 RTFFlushOutputBuffer(info
);
903 /* FIXME too slow ? how come ?*/
904 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
908 void ME_RTFTblAttrHook(RTF_Info
*info
)
910 switch (info
->rtfMinor
)
914 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
915 info
->borderType
= 0; /* Not sure */
916 else /* v1.0 - 3.0 */
917 info
->borderType
= RTFBorderRowTop
;
918 if (!info
->tableDef
) {
919 info
->tableDef
= ME_MakeTableDef(info
->editor
);
921 ME_InitTableDef(info
->editor
, info
->tableDef
);
930 info
->tableDef
= ME_MakeTableDef(info
->editor
);
932 cellNum
= info
->tableDef
->numCellsDefined
;
933 if (cellNum
>= MAX_TABLE_CELLS
)
935 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
936 if (cellNum
< MAX_TAB_STOPS
) {
937 /* Tab stops were used to store cell positions before v4.1 but v4.1
938 * still seems to set the tabstops without using them. */
939 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
940 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
941 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
942 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
944 info
->tableDef
->numCellsDefined
++;
948 info
->borderType
= RTFBorderRowTop
;
951 info
->borderType
= RTFBorderRowLeft
;
953 case rtfRowBordBottom
:
954 info
->borderType
= RTFBorderRowBottom
;
956 case rtfRowBordRight
:
957 info
->borderType
= RTFBorderRowRight
;
960 info
->borderType
= RTFBorderCellTop
;
962 case rtfCellBordLeft
:
963 info
->borderType
= RTFBorderCellLeft
;
965 case rtfCellBordBottom
:
966 info
->borderType
= RTFBorderCellBottom
;
968 case rtfCellBordRight
:
969 info
->borderType
= RTFBorderCellRight
;
973 info
->tableDef
->gapH
= info
->rtfParam
;
977 info
->tableDef
->leftEdge
= info
->rtfParam
;
982 void ME_RTFSpecialCharHook(RTF_Info
*info
)
984 RTFTable
*tableDef
= info
->tableDef
;
985 switch (info
->rtfMinor
)
988 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
990 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
994 RTFFlushOutputBuffer(info
);
995 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
996 if (tableDef
->tableRowStart
)
998 if (!info
->nestingLevel
&&
999 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1001 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
1002 para
= para
->member
.para
.next_para
;
1003 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1004 tableDef
->tableRowStart
= para
;
1005 info
->nestingLevel
= 1;
1007 ME_InsertTableCellFromCursor(info
->editor
);
1009 } else { /* v1.0 - v3.0 */
1010 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1011 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1012 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
1013 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1016 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1017 tableDef
->numCellsInserted
++;
1022 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
1024 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1027 ME_DisplayItem
*para
, *cell
, *run
;
1032 RTFFlushOutputBuffer(info
);
1033 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
1034 if (!tableDef
->tableRowStart
)
1036 if (!info
->nestingLevel
&&
1037 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1039 para
= tableDef
->tableRowStart
;
1040 para
= para
->member
.para
.next_para
;
1041 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1042 tableDef
->tableRowStart
= para
;
1043 info
->nestingLevel
++;
1045 para
= tableDef
->tableRowStart
;
1046 cell
= ME_FindItemFwd(para
, diCell
);
1047 assert(cell
&& !cell
->member
.cell
.prev_cell
);
1048 if (tableDef
->numCellsDefined
< 1)
1050 /* 2000 twips appears to be the cell size that native richedit uses
1051 * when no cell sizes are specified. */
1052 const int defaultCellSize
= 2000;
1053 int nRightBoundary
= defaultCellSize
;
1054 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1055 while (cell
->member
.cell
.next_cell
) {
1056 cell
= cell
->member
.cell
.next_cell
;
1057 nRightBoundary
+= defaultCellSize
;
1058 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1060 para
= ME_InsertTableCellFromCursor(info
->editor
);
1061 cell
= para
->member
.para
.pCell
;
1062 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1064 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
1066 RTFCell
*cellDef
= &tableDef
->cells
[i
];
1067 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
1068 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
1070 cell
= cell
->member
.cell
.next_cell
;
1073 para
= ME_InsertTableCellFromCursor(info
->editor
);
1074 cell
= para
->member
.para
.pCell
;
1077 /* Cell for table row delimiter is empty */
1078 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1081 run
= ME_FindItemFwd(cell
, diRun
);
1082 if (info
->editor
->pCursors
[0].pRun
!= run
||
1083 info
->editor
->pCursors
[0].nOffset
)
1086 /* Delete inserted cells that aren't defined. */
1087 info
->editor
->pCursors
[1].pRun
= run
;
1088 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1089 info
->editor
->pCursors
[1].nOffset
= 0;
1090 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1091 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1092 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1096 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1097 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1098 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1099 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1101 info
->nestingLevel
--;
1102 if (!info
->nestingLevel
)
1104 if (info
->canInheritInTbl
) {
1105 tableDef
->tableRowStart
= para
;
1107 while (info
->tableDef
) {
1108 tableDef
= info
->tableDef
;
1109 info
->tableDef
= tableDef
->parent
;
1110 heap_free(tableDef
);
1114 info
->tableDef
= tableDef
->parent
;
1115 heap_free(tableDef
);
1117 } else { /* v1.0 - v3.0 */
1119 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1120 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1121 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1122 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1124 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1126 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1129 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1130 tableDef
->numCellsInserted
++;
1132 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1133 if (!tableDef
->numCellsDefined
)
1134 pFmt
->wEffects
&= ~PFE_TABLE
;
1135 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1136 tableDef
->numCellsInserted
= 0;
1142 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1143 ME_DisplayItem
*para
;
1145 RTFFlushOutputBuffer(info
);
1146 para
= info
->editor
->pCursors
[0].pPara
;
1147 pFmt
= para
->member
.para
.pFmt
;
1148 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1150 /* rtfPar is treated like a space within a table. */
1151 info
->rtfClass
= rtfText
;
1152 info
->rtfMajor
= ' ';
1154 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1155 tableDef
->numCellsInserted
= 0;
1161 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1164 LPOLEOBJECT lpObject
= NULL
;
1165 LPSTORAGE lpStorage
= NULL
;
1166 LPOLECLIENTSITE lpClientSite
= NULL
;
1167 LPDATAOBJECT lpDataObject
= NULL
;
1168 LPOLECACHE lpOleCache
= NULL
;
1177 stgm
.tymed
= TYMED_ENHMF
;
1178 stgm
.u
.hEnhMetaFile
= hemf
;
1179 fm
.cfFormat
= CF_ENHMETAFILE
;
1183 stgm
.tymed
= TYMED_GDI
;
1184 stgm
.u
.hBitmap
= hbmp
;
1185 fm
.cfFormat
= CF_BITMAP
;
1187 stgm
.pUnkForRelease
= NULL
;
1190 fm
.dwAspect
= DVASPECT_CONTENT
;
1192 fm
.tymed
= stgm
.tymed
;
1194 if (!info
->lpRichEditOle
)
1196 CreateIRichEditOle(NULL
, info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1199 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1200 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1201 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1202 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1203 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1204 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1205 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1206 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1210 reobject
.cbStruct
= sizeof(reobject
);
1211 reobject
.cp
= REO_CP_SELECTION
;
1212 reobject
.clsid
= clsid
;
1213 reobject
.poleobj
= lpObject
;
1214 reobject
.pstg
= lpStorage
;
1215 reobject
.polesite
= lpClientSite
;
1216 /* convert from twips to .01 mm */
1217 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1218 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1219 reobject
.dvaspect
= DVASPECT_CONTENT
;
1220 reobject
.dwFlags
= 0; /* FIXME */
1221 reobject
.dwUser
= 0;
1223 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1227 if (lpObject
) IOleObject_Release(lpObject
);
1228 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1229 if (lpStorage
) IStorage_Release(lpStorage
);
1230 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1231 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1236 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1239 BYTE
* buffer
= NULL
;
1240 unsigned bufsz
, bufidx
;
1246 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1249 if (info
->rtfClass
== rtfEOF
)
1252 /* fetch picture type */
1253 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1255 mfp
.mm
= info
->rtfParam
;
1258 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1260 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1263 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1265 gfx
= gfx_enhmetafile
;
1269 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1273 /* fetch picture attributes */
1277 if (info
->rtfClass
== rtfEOF
)
1279 if (info
->rtfClass
== rtfText
)
1281 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1283 ERR("Expected picture attribute (%d %d)\n",
1284 info
->rtfClass
, info
->rtfMajor
);
1287 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1289 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1291 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1293 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1295 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1296 sz
.cx
= info
->rtfParam
;
1297 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1298 sz
.cy
= info
->rtfParam
;
1300 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1302 /* fetch picture data */
1305 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1306 val
= info
->rtfMajor
;
1307 for (flip
= TRUE
;; flip
= !flip
)
1310 if (info
->rtfClass
== rtfEOF
)
1312 HeapFree(GetProcessHeap(), 0, buffer
);
1313 return; /* Warn ?? */
1315 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1317 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1320 if (bufidx
>= bufsz
&&
1321 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1323 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1326 val
= info
->rtfMajor
;
1328 if (flip
) FIXME("wrong hex string\n");
1332 case gfx_enhmetafile
:
1333 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1334 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1337 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1338 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1342 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1344 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1346 /* not quite right, especially for bitfields type of compression */
1347 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1348 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1349 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1350 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1351 bi
, DIB_RGB_COLORS
)))
1352 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1359 HeapFree(GetProcessHeap(), 0, buffer
);
1360 RTFRouteToken (info
); /* feed "}" back to router */
1363 HeapFree(GetProcessHeap(), 0, buffer
);
1365 RTFRouteToken(info
); /* feed "}" back to router */
1368 /* for now, lookup the \result part and use it, whatever the object */
1369 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1374 if (info
->rtfClass
== rtfEOF
)
1376 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1378 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1381 if (info
->rtfClass
== rtfEOF
)
1383 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1387 while (RTFGetToken (info
) != rtfEOF
)
1389 if (info
->rtfClass
== rtfGroup
)
1391 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1392 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1394 RTFRouteToken(info
);
1397 else RTFSkipGroup(info
);
1400 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1402 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1406 RTFRouteToken(info
); /* feed "}" back to router */
1409 static void ME_RTFReadHook(RTF_Info
*info
)
1411 switch(info
->rtfClass
)
1414 switch(info
->rtfMajor
)
1417 if (info
->stackTop
< maxStack
) {
1418 info
->stack
[info
->stackTop
].style
= info
->style
;
1419 ME_AddRefStyle(info
->style
);
1420 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1421 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1424 info
->styleChanged
= FALSE
;
1428 RTFFlushOutputBuffer(info
);
1430 if (info
->stackTop
<= 0)
1431 info
->rtfClass
= rtfEOF
;
1432 if (info
->stackTop
< 0)
1435 ME_ReleaseStyle(info
->style
);
1436 info
->style
= info
->stack
[info
->stackTop
].style
;
1437 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1438 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1447 ME_StreamInFill(ME_InStream
*stream
)
1449 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1450 (BYTE
*)stream
->buffer
,
1451 sizeof(stream
->buffer
),
1452 (LONG
*)&stream
->dwSize
);
1456 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1460 int from
, to
, nUndoMode
;
1461 int nEventMask
= editor
->nEventMask
;
1462 ME_InStream inStream
;
1463 BOOL invalidRTF
= FALSE
;
1464 ME_Cursor
*selStart
, *selEnd
;
1465 LRESULT num_read
= 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1467 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1468 editor
->nEventMask
= 0;
1470 ME_GetSelectionOfs(editor
, &from
, &to
);
1471 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1473 ME_GetSelection(editor
, &selStart
, &selEnd
);
1474 style
= ME_GetSelectionInsertStyle(editor
);
1476 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1478 /* Don't insert text at the end of the table row */
1479 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1480 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1481 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1483 para
= para
->member
.para
.next_para
;
1484 editor
->pCursors
[0].pPara
= para
;
1485 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1486 editor
->pCursors
[0].nOffset
= 0;
1488 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1490 para
= para
->member
.para
.next_para
;
1491 editor
->pCursors
[0].pPara
= para
;
1492 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1493 editor
->pCursors
[0].nOffset
= 0;
1495 editor
->pCursors
[1] = editor
->pCursors
[0];
1496 } else { /* v1.0 - 3.0 */
1497 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1498 ME_IsInTable(editor
->pCursors
[0].pRun
))
1502 style
= editor
->pBuffer
->pDefaultStyle
;
1503 ME_AddRefStyle(style
);
1504 ME_SetSelection(editor
, 0, 0);
1505 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1506 ME_GetTextLength(editor
), FALSE
);
1508 ME_ClearTempStyle(editor
);
1509 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1513 /* Back up undo mode to a local variable */
1514 nUndoMode
= editor
->nUndoMode
;
1516 /* Only create an undo if SFF_SELECTION is set */
1517 if (!(format
& SFF_SELECTION
))
1518 editor
->nUndoMode
= umIgnore
;
1520 inStream
.editstream
= stream
;
1521 inStream
.editstream
->dwError
= 0;
1522 inStream
.dwSize
= 0;
1523 inStream
.dwUsed
= 0;
1525 if (format
& SF_RTF
)
1527 /* Check if it's really RTF, and if it is not, use plain text */
1528 ME_StreamInFill(&inStream
);
1529 if (!inStream
.editstream
->dwError
)
1531 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1532 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1535 inStream
.editstream
->dwError
= -16;
1540 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1542 if (format
& SF_RTF
) {
1543 from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1545 /* setup the RTF parser */
1546 memset(&parser
, 0, sizeof parser
);
1547 RTFSetEditStream(&parser
, &inStream
);
1548 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1549 parser
.editor
= editor
;
1550 parser
.style
= style
;
1551 WriterInit(&parser
);
1553 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1554 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1555 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1556 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1558 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1559 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1563 /* do the parsing */
1565 RTFFlushOutputBuffer(&parser
);
1566 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1567 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1568 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1570 /* Delete any incomplete table row at the end of the rich text. */
1572 ME_DisplayItem
*para
;
1574 parser
.rtfMinor
= rtfRow
;
1575 /* Complete the table row before deleting it.
1576 * By doing it this way we will have the current paragraph format set
1577 * properly to reflect that is not in the complete table, and undo items
1578 * will be added for this change to the current paragraph format. */
1579 if (parser
.nestingLevel
> 0)
1581 while (parser
.nestingLevel
> 1)
1582 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1583 para
= parser
.tableDef
->tableRowStart
;
1584 ME_RTFSpecialCharHook(&parser
);
1586 para
= parser
.tableDef
->tableRowStart
;
1587 ME_RTFSpecialCharHook(&parser
);
1588 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1589 para
= para
->member
.para
.next_para
;
1592 editor
->pCursors
[1].pPara
= para
;
1593 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1594 editor
->pCursors
[1].nOffset
= 0;
1595 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1596 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1597 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1598 if (parser
.tableDef
)
1599 parser
.tableDef
->tableRowStart
= NULL
;
1602 ME_CheckTablesForCorruption(editor
);
1603 RTFDestroy(&parser
);
1604 if (parser
.lpRichEditOle
)
1605 IRichEditOle_Release(parser
.lpRichEditOle
);
1607 if (parser
.stackTop
> 0)
1609 while (--parser
.stackTop
>= 0)
1611 ME_ReleaseStyle(parser
.style
);
1612 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1614 if (!inStream
.editstream
->dwError
)
1615 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1618 /* Remove last line break, as mandated by tests. This is not affected by
1619 CR/LF counters, since RTF streaming presents only \para tokens, which
1620 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1622 if (stripLastCR
&& !(format
& SFF_SELECTION
)) {
1624 ME_GetSelection(editor
, &selStart
, &selEnd
);
1625 newto
= ME_GetCursorOfs(selEnd
);
1626 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1627 WCHAR lastchar
[3] = {'\0', '\0'};
1628 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1629 ME_Cursor linebreakCursor
= *selEnd
;
1631 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1632 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, FALSE
, FALSE
);
1633 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1634 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1638 to
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1639 num_read
= to
- from
;
1641 style
= parser
.style
;
1643 else if (format
& SF_TEXT
)
1644 num_read
= ME_StreamInText(editor
, format
, &inStream
, style
);
1646 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1647 /* put the cursor at the top */
1648 if (!(format
& SFF_SELECTION
))
1649 ME_SetSelection(editor
, 0, 0);
1652 /* Restore saved undo mode */
1653 editor
->nUndoMode
= nUndoMode
;
1655 /* even if we didn't add an undo, we need to commit anything on the stack */
1656 ME_CommitUndo(editor
);
1658 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1659 if (!(format
& SFF_SELECTION
))
1660 ME_EmptyUndoStack(editor
);
1662 ME_ReleaseStyle(style
);
1663 editor
->nEventMask
= nEventMask
;
1664 ME_UpdateRepaint(editor
, FALSE
);
1665 if (!(format
& SFF_SELECTION
)) {
1666 ME_ClearTempStyle(editor
);
1668 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1669 ME_MoveCaret(editor
);
1670 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1671 ME_SendSelChange(editor
);
1672 ME_SendRequestResize(editor
, FALSE
);
1678 typedef struct tagME_RTFStringStreamStruct
1683 } ME_RTFStringStreamStruct
;
1685 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1687 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1690 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1691 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1692 pStruct
->pos
+= count
;
1698 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1701 ME_RTFStringStreamStruct data
;
1703 data
.string
= string
;
1704 data
.length
= strlen(string
);
1706 es
.dwCookie
= (DWORD_PTR
)&data
;
1707 es
.pfnCallback
= ME_ReadFromRTFString
;
1708 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, TRUE
);
1713 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1715 const int nLen
= lstrlenW(text
);
1716 const int nTextLen
= ME_GetTextLength(editor
);
1719 WCHAR wLastChar
= ' ';
1721 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1722 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1724 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1725 FIXME("Flags 0x%08x not implemented\n",
1726 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1729 if (chrg
->cpMax
== -1)
1732 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1734 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1735 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1740 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1741 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1745 chrgText
->cpMin
= -1;
1746 chrgText
->cpMax
= -1;
1751 /* when searching up, if cpMin < cpMax, then instead of searching
1752 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1753 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1754 * case, it is always bigger than cpMin.
1756 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1760 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1761 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1767 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1770 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1774 if (flags
& FR_DOWN
) /* Forward search */
1776 /* If possible, find the character before where the search starts */
1777 if ((flags
& FR_WHOLEWORD
) && nMin
)
1779 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1780 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1781 ME_MoveCursorChars(editor
, &cursor
, 1);
1783 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1786 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1788 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1789 int nCurStart
= cursor
.nOffset
;
1792 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
), text
[nMatched
], (flags
& FR_MATCHCASE
)))
1794 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1798 if (nMatched
== nLen
)
1800 ME_DisplayItem
*pNextItem
= pCurItem
;
1801 int nNextStart
= nCurStart
;
1804 /* Check to see if next character is a whitespace */
1805 if (flags
& FR_WHOLEWORD
)
1807 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1809 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1810 nNextStart
= -nMatched
;
1814 wNextChar
= *get_text( &pNextItem
->member
.run
, nNextStart
+ nMatched
);
1818 if (isalnumW(wNextChar
))
1822 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1825 chrgText
->cpMin
= cursor
.nOffset
;
1826 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1828 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1829 return cursor
.nOffset
;
1831 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1833 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1834 nCurStart
= -nMatched
;
1838 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
);
1843 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.len
)
1845 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1850 else /* Backward search */
1852 /* If possible, find the character after where the search ends */
1853 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1855 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1856 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1857 ME_MoveCursorChars(editor
, &cursor
, -1);
1859 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1862 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1864 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1865 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1866 int nCurEnd
= cursor
.nOffset
;
1871 ME_PrevRun(&pCurPara
, &pCurItem
);
1872 nCurEnd
= pCurItem
->member
.run
.len
;
1875 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 ),
1876 text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
) ))
1878 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1882 if (nMatched
== nLen
)
1884 ME_DisplayItem
*pPrevItem
= pCurItem
;
1885 int nPrevEnd
= nCurEnd
;
1889 /* Check to see if previous character is a whitespace */
1890 if (flags
& FR_WHOLEWORD
)
1892 if (nPrevEnd
- nMatched
== 0)
1894 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1896 nPrevEnd
= pPrevItem
->member
.run
.len
+ nMatched
;
1900 wPrevChar
= *get_text( &pPrevItem
->member
.run
, nPrevEnd
- nMatched
- 1 );
1904 if (isalnumW(wPrevChar
))
1908 nStart
= pCurPara
->member
.para
.nCharOfs
1909 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1912 chrgText
->cpMin
= nStart
;
1913 chrgText
->cpMax
= nStart
+ nLen
;
1915 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1918 if (nCurEnd
- nMatched
== 0)
1920 ME_PrevRun(&pCurPara
, &pCurItem
);
1921 /* Don't care about pCurItem becoming NULL here; it's already taken
1922 * care of in the exterior loop condition */
1923 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1927 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 );
1932 if (cursor
.nOffset
< 0)
1934 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1935 cursor
.nOffset
= cursor
.pRun
->member
.run
.len
;
1939 TRACE("not found\n");
1941 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1945 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1950 if (!ex
->cb
|| !pText
) return 0;
1952 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1953 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1955 if (ex
->flags
& GT_SELECTION
)
1958 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
1959 start
= editor
->pCursors
[nStartCur
];
1964 ME_SetCursorToStart(editor
, &start
);
1967 if (ex
->codepage
== CP_UNICODE
)
1969 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
1970 &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
1974 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1975 we can just take a bigger buffer? :)
1976 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1977 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1979 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1984 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
1985 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
1987 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
1988 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
1989 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1990 if (rc
) rc
--; /* do not count 0 terminator */
1997 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
1998 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
2000 if (!strText
) return 0;
2002 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, FALSE
, FALSE
);
2005 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2007 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, FALSE
, FALSE
);
2008 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
2009 nLen
+1, NULL
, NULL
);
2015 static int handle_EM_EXSETSEL( ME_TextEditor
*editor
, int to
, int from
)
2019 TRACE("%d - %d\n", to
, from
);
2021 ME_InvalidateSelection( editor
);
2022 end
= ME_SetSelection( editor
, to
, from
);
2023 ME_InvalidateSelection( editor
);
2024 ITextHost_TxShowCaret( editor
->texthost
, FALSE
);
2025 ME_ShowCaret( editor
);
2026 ME_SendSelChange( editor
);
2031 typedef struct tagME_GlobalDestStruct
2035 } ME_GlobalDestStruct
;
2037 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2039 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2044 pDest
= (WORD
*)lpBuff
;
2045 pSrc
= GlobalLock(pData
->hData
);
2046 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2047 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2049 pData
->nLength
+= i
;
2051 GlobalUnlock(pData
->hData
);
2055 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2057 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2062 pSrc
= GlobalLock(pData
->hData
);
2063 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2064 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2066 pData
->nLength
+= i
;
2068 GlobalUnlock(pData
->hData
);
2072 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2076 ME_GlobalDestStruct gds
;
2077 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2080 if (IsClipboardFormatAvailable(nRTFFormat
))
2081 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2082 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2083 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2087 if (!OpenClipboard(editor
->hWnd
))
2089 gds
.hData
= GetClipboardData(cf
);
2091 es
.dwCookie
= (DWORD_PTR
)&gds
;
2092 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2093 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2099 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2101 LPDATAOBJECT dataObj
= NULL
;
2104 if (editor
->cPasswordMask
)
2105 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2107 if(editor
->lpOleCallback
)
2110 range
.cpMin
= ME_GetCursorOfs(start
);
2111 range
.cpMax
= range
.cpMin
+ nChars
;
2112 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2114 if(FAILED(hr
) || !dataObj
)
2115 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2117 hr
= OleSetClipboard(dataObj
);
2118 IDataObject_Release(dataObj
);
2120 return SUCCEEDED(hr
);
2123 /* helper to send a msg filter notification */
2125 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2129 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2130 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2131 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2132 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2134 msgf
.wParam
= *wParam
;
2135 msgf
.lParam
= *lParam
;
2136 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2138 *wParam
= msgf
.wParam
;
2139 *lParam
= msgf
.lParam
;
2140 msgf
.wParam
= *wParam
;
2145 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2147 ME_DisplayItem
*startPara
, *endPara
;
2148 ME_DisplayItem
*prev_para
;
2149 ME_Cursor
*from
, *to
;
2153 if (!editor
->AutoURLDetect_bEnable
) return;
2155 ME_GetSelection(editor
, &from
, &to
);
2157 /* Find paragraph previous to the one that contains start cursor */
2158 startPara
= from
->pPara
;
2159 prev_para
= startPara
->member
.para
.prev_para
;
2160 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2162 /* Find paragraph that contains end cursor */
2163 endPara
= to
->pPara
->member
.para
.next_para
;
2165 start
.pPara
= startPara
;
2166 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2168 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2170 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2174 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2176 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2177 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2179 if (editor
->bMouseCaptured
)
2181 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2182 editor
->nSelectionType
= stPosition
;
2190 editor
->nUDArrowX
= -1;
2196 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2197 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2201 editor
->nUDArrowX
= -1;
2202 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2203 if (editor
->styleFlags
& ES_READONLY
)
2205 if (ME_IsSelection(editor
))
2207 ME_DeleteSelection(editor
);
2208 ME_CommitUndo(editor
);
2210 else if (nKey
== VK_DELETE
)
2212 /* Delete stops group typing.
2213 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2214 ME_DeleteTextAtCursor(editor
, 1, 1);
2215 ME_CommitUndo(editor
);
2217 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2219 BOOL bDeletionSucceeded
;
2220 /* Backspace can be grouped for a single undo */
2221 ME_ContinueCoalescingTransaction(editor
);
2222 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2223 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2224 /* Deletion was prevented so the cursor is moved back to where it was.
2225 * (e.g. this happens when trying to delete cell boundaries)
2227 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2229 ME_CommitCoalescingUndo(editor
);
2233 ME_MoveCursorFromTableRowStartParagraph(editor
);
2234 ME_UpdateSelectionLinkAttribute(editor
);
2235 ME_UpdateRepaint(editor
, FALSE
);
2236 ME_SendRequestResize(editor
, FALSE
);
2239 if (editor
->bDialogMode
)
2244 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2246 if (editor
->hwndParent
)
2249 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2250 if (HIWORD(dw
) == DC_HASDEFID
)
2252 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2255 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
2256 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2264 if (editor
->styleFlags
& ES_MULTILINE
)
2266 ME_Cursor cursor
= editor
->pCursors
[0];
2267 ME_DisplayItem
*para
= cursor
.pPara
;
2269 const WCHAR endl
= '\r';
2270 const WCHAR endlv10
[] = {'\r','\n'};
2273 if (editor
->styleFlags
& ES_READONLY
) {
2274 MessageBeep(MB_ICONERROR
);
2278 ME_GetSelectionOfs(editor
, &from
, &to
);
2279 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2281 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2282 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2283 /* Add a new table row after this row. */
2284 para
= ME_AppendTableRow(editor
, para
);
2285 para
= para
->member
.para
.next_para
;
2286 editor
->pCursors
[0].pPara
= para
;
2287 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2288 editor
->pCursors
[0].nOffset
= 0;
2289 editor
->pCursors
[1] = editor
->pCursors
[0];
2290 ME_CommitUndo(editor
);
2291 ME_CheckTablesForCorruption(editor
);
2292 ME_UpdateRepaint(editor
, FALSE
);
2295 else if (para
== editor
->pCursors
[1].pPara
&&
2296 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2297 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2298 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2300 /* Insert a newline before the table. */
2301 para
= para
->member
.para
.prev_para
;
2302 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2303 editor
->pCursors
[0].pPara
= para
;
2304 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2305 editor
->pCursors
[1] = editor
->pCursors
[0];
2306 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2307 editor
->pCursors
[0].pRun
->member
.run
.style
);
2308 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2309 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2310 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2311 editor
->pCursors
[0].pPara
= para
;
2312 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2313 editor
->pCursors
[1] = editor
->pCursors
[0];
2314 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2315 ME_CommitCoalescingUndo(editor
);
2316 ME_CheckTablesForCorruption(editor
);
2317 ME_UpdateRepaint(editor
, FALSE
);
2320 } else { /* v1.0 - 3.0 */
2321 ME_DisplayItem
*para
= cursor
.pPara
;
2322 if (ME_IsInTable(para
))
2324 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2327 ME_ContinueCoalescingTransaction(editor
);
2328 para
= ME_AppendTableRow(editor
, para
);
2329 editor
->pCursors
[0].pPara
= para
;
2330 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2331 editor
->pCursors
[0].nOffset
= 0;
2332 editor
->pCursors
[1] = editor
->pCursors
[0];
2333 ME_CommitCoalescingUndo(editor
);
2334 ME_UpdateRepaint(editor
, FALSE
);
2338 ME_ContinueCoalescingTransaction(editor
);
2339 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2340 !ME_IsInTable(para
->member
.para
.prev_para
))
2342 /* Insert newline before table */
2343 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2345 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2346 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2348 editor
->pCursors
[0].nOffset
= 0;
2349 editor
->pCursors
[1] = editor
->pCursors
[0];
2350 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2351 editor
->pCursors
[0].pRun
->member
.run
.style
);
2353 editor
->pCursors
[1] = editor
->pCursors
[0];
2354 para
= ME_AppendTableRow(editor
, para
);
2355 editor
->pCursors
[0].pPara
= para
;
2356 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2357 editor
->pCursors
[0].nOffset
= 0;
2358 editor
->pCursors
[1] = editor
->pCursors
[0];
2360 ME_CommitCoalescingUndo(editor
);
2361 ME_UpdateRepaint(editor
, FALSE
);
2367 style
= ME_GetInsertStyle(editor
, 0);
2368 ME_SaveTempStyle(editor
);
2369 ME_ContinueCoalescingTransaction(editor
);
2371 ME_InsertEndRowFromCursor(editor
, 0);
2373 if (!editor
->bEmulateVersion10
)
2374 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2376 ME_InsertTextFromCursor(editor
, 0, endlv10
, 2, style
);
2377 ME_ReleaseStyle(style
);
2378 ME_CommitCoalescingUndo(editor
);
2381 ME_UpdateSelectionLinkAttribute(editor
);
2382 ME_UpdateRepaint(editor
, FALSE
);
2388 if (editor
->bDialogMode
&& editor
->hwndParent
)
2389 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2392 if (editor
->bDialogMode
&& editor
->hwndParent
)
2393 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2398 handle_EM_EXSETSEL( editor
, 0, -1 );
2404 return ME_Paste(editor
);
2412 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2413 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2416 result
= ME_Copy(editor
, selStart
, nChars
);
2417 if (result
&& nKey
== 'X')
2419 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2420 ME_CommitUndo(editor
);
2421 ME_UpdateRepaint(editor
, TRUE
);
2442 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2443 editor
->nUDArrowX
= -1;
2450 chf
.cbSize
= sizeof(chf
);
2452 ME_GetSelectionCharFormat(editor
, &chf
);
2453 ME_DumpStyleToBuf(&chf
, buf
);
2454 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2458 ME_CheckCharOffsets(editor
);
2465 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2466 LPARAM flags
, BOOL unicode
)
2470 if (editor
->bMouseCaptured
)
2474 wstr
= (WCHAR
)charCode
;
2477 CHAR charA
= charCode
;
2478 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2481 if (editor
->styleFlags
& ES_READONLY
) {
2482 MessageBeep(MB_ICONERROR
);
2483 return 0; /* FIXME really 0 ? */
2486 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2488 ME_Cursor cursor
= editor
->pCursors
[0];
2489 ME_DisplayItem
*para
= cursor
.pPara
;
2491 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2492 ME_GetSelectionOfs(editor
, &from
, &to
);
2494 /* v4.1 allows tabs to be inserted with ctrl key down */
2495 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2497 ME_DisplayItem
*para
;
2498 BOOL bSelectedRow
= FALSE
;
2500 para
= cursor
.pPara
;
2501 if (ME_IsSelection(editor
) &&
2502 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2503 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2504 para
->member
.para
.prev_para
->type
== diParagraph
)
2506 para
= para
->member
.para
.prev_para
;
2507 bSelectedRow
= TRUE
;
2509 if (ME_IsInTable(para
))
2511 ME_TabPressedInTable(editor
, bSelectedRow
);
2512 ME_CommitUndo(editor
);
2515 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2516 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2518 para
= para
->member
.para
.next_para
;
2519 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2520 para
= para
->member
.para
.next_para
;
2521 editor
->pCursors
[0].pPara
= para
;
2522 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2523 editor
->pCursors
[0].nOffset
= 0;
2524 editor
->pCursors
[1] = editor
->pCursors
[0];
2527 } else { /* v1.0 - 3.0 */
2528 if (ME_IsInTable(cursor
.pRun
) &&
2529 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2532 /* Text should not be inserted at the end of the table. */
2537 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2538 /* WM_CHAR is restricted to nTextLimit */
2539 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2541 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2542 ME_SaveTempStyle(editor
);
2543 ME_ContinueCoalescingTransaction(editor
);
2544 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2545 ME_ReleaseStyle(style
);
2546 ME_CommitCoalescingUndo(editor
);
2547 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2550 ME_UpdateSelectionLinkAttribute(editor
);
2551 ME_UpdateRepaint(editor
, FALSE
);
2556 /* Process the message and calculate the new click count.
2558 * returns: The click count if it is mouse down event, else returns 0. */
2559 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2562 static int clickNum
= 0;
2563 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2566 if ((msg
== WM_LBUTTONDBLCLK
) ||
2567 (msg
== WM_RBUTTONDBLCLK
) ||
2568 (msg
== WM_MBUTTONDBLCLK
) ||
2569 (msg
== WM_XBUTTONDBLCLK
))
2571 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2574 if ((msg
== WM_LBUTTONDOWN
) ||
2575 (msg
== WM_RBUTTONDOWN
) ||
2576 (msg
== WM_MBUTTONDOWN
) ||
2577 (msg
== WM_XBUTTONDOWN
))
2579 static MSG prevClickMsg
;
2581 /* Compare the editor instead of the hwnd so that the this
2582 * can still be done for windowless richedit controls. */
2583 clickMsg
.hwnd
= (HWND
)editor
;
2584 clickMsg
.message
= msg
;
2585 clickMsg
.wParam
= wParam
;
2586 clickMsg
.lParam
= lParam
;
2587 clickMsg
.time
= GetMessageTime();
2588 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2589 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2590 if ((clickNum
!= 0) &&
2591 (clickMsg
.message
== prevClickMsg
.message
) &&
2592 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2593 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2594 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2595 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2596 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2602 prevClickMsg
= clickMsg
;
2609 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2615 DWORD messagePos
= GetMessagePos();
2616 pt
.x
= (short)LOWORD(messagePos
);
2617 pt
.y
= (short)HIWORD(messagePos
);
2621 sbi
.cbSize
= sizeof(sbi
);
2622 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2623 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2624 PtInRect(&sbi
.rcScrollBar
, pt
))
2626 ITextHost_TxSetCursor(editor
->texthost
,
2627 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2630 sbi
.cbSize
= sizeof(sbi
);
2631 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2632 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2633 PtInRect(&sbi
.rcScrollBar
, pt
))
2635 ITextHost_TxSetCursor(editor
->texthost
,
2636 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2640 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2642 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2643 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2646 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2647 pt
.y
< editor
->rcFormat
.top
&&
2648 pt
.x
< editor
->rcFormat
.left
)
2650 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2653 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2655 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2656 ITextHost_TxSetCursor(editor
->texthost
,
2657 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2659 ITextHost_TxSetCursor(editor
->texthost
,
2660 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2663 if (pt
.x
< editor
->rcFormat
.left
)
2665 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2668 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2673 run
= &cursor
.pRun
->member
.run
;
2674 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2675 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2677 ITextHost_TxSetCursor(editor
->texthost
,
2678 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2683 if (ME_IsSelection(editor
))
2685 int selStart
, selEnd
;
2686 int offset
= ME_GetCursorOfs(&cursor
);
2688 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2689 if (selStart
<= offset
&& selEnd
>= offset
) {
2690 ITextHost_TxSetCursor(editor
->texthost
,
2691 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2697 ITextHost_TxSetCursor(editor
->texthost
,
2698 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2702 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2704 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2705 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2706 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2707 editor
->rcFormat
.right
-= 1;
2710 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2715 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2717 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2718 if(selrange
.cpMin
== selrange
.cpMax
)
2719 seltype
|= SEL_EMPTY
;
2722 /* FIXME: Handle objects */
2723 seltype
|= SEL_TEXT
;
2724 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2725 seltype
|= SEL_MULTICHAR
;
2727 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2729 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2735 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2737 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2743 ed
->hwndParent
= NULL
;
2744 ed
->sizeWindow
.cx
= ed
->sizeWindow
.cy
= 0;
2745 ed
->texthost
= texthost
;
2747 ed
->bEmulateVersion10
= bEmulateVersion10
;
2749 ITextHost_TxGetPropertyBits(texthost
,
2750 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2751 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2752 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2753 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2754 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2756 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2757 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2758 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2759 ed
->pBuffer
= ME_MakeText();
2760 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2761 ed
->nAvailWidth
= 0; /* wrap to client area */
2762 ME_MakeFirstParagraph(ed
);
2763 /* The four cursors are for:
2764 * 0 - The position where the caret is shown
2765 * 1 - The anchored end of the selection (for normal selection)
2766 * 2 & 3 - The anchored start and end respectively for word, line,
2767 * or paragraph selection.
2770 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2771 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2772 ed
->pCursors
[1] = ed
->pCursors
[0];
2773 ed
->pCursors
[2] = ed
->pCursors
[0];
2774 ed
->pCursors
[3] = ed
->pCursors
[1];
2775 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2776 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2778 ed
->rgbBackColor
= -1;
2779 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2780 ed
->bCaretAtEnd
= FALSE
;
2782 ed
->nModifyStep
= 0;
2783 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2784 list_init( &ed
->undo_stack
);
2785 list_init( &ed
->redo_stack
);
2786 ed
->nUndoStackSize
= 0;
2787 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2788 ed
->nUndoMode
= umAddToUndo
;
2789 ed
->nParagraphs
= 1;
2790 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2791 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2792 ed
->bHideSelection
= FALSE
;
2793 ed
->pfnWordBreak
= NULL
;
2794 ed
->lpOleCallback
= NULL
;
2795 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2796 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2797 ed
->AutoURLDetect_bEnable
= FALSE
;
2798 ed
->bHaveFocus
= FALSE
;
2799 ed
->bDialogMode
= FALSE
;
2800 ed
->bMouseCaptured
= FALSE
;
2801 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2803 ed
->pFontCache
[i
].nRefs
= 0;
2804 ed
->pFontCache
[i
].nAge
= 0;
2805 ed
->pFontCache
[i
].hFont
= NULL
;
2808 ME_CheckCharOffsets(ed
);
2809 ed
->bDefaultFormatRect
= TRUE
;
2810 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2812 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2813 ed
->selofs
= SELECTIONBAR_WIDTH
;
2814 ed
->styleFlags
|= ES_SELECTIONBAR
;
2818 ed
->nSelectionType
= stPosition
;
2820 ed
->cPasswordMask
= 0;
2821 if (props
& TXTBIT_USEPASSWORD
)
2822 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2824 if (props
& TXTBIT_AUTOWORDSEL
)
2825 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2826 if (props
& TXTBIT_MULTILINE
) {
2827 ed
->styleFlags
|= ES_MULTILINE
;
2828 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2830 ed
->bWordWrap
= FALSE
;
2832 if (props
& TXTBIT_READONLY
)
2833 ed
->styleFlags
|= ES_READONLY
;
2834 if (!(props
& TXTBIT_HIDESELECTION
))
2835 ed
->styleFlags
|= ES_NOHIDESEL
;
2836 if (props
& TXTBIT_SAVESELECTION
)
2837 ed
->styleFlags
|= ES_SAVESEL
;
2838 if (props
& TXTBIT_VERTICAL
)
2839 ed
->styleFlags
|= ES_VERTICAL
;
2840 if (props
& TXTBIT_DISABLEDRAG
)
2841 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2843 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2845 /* Default scrollbar information */
2846 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2847 ed
->vert_si
.nMin
= 0;
2848 ed
->vert_si
.nMax
= 0;
2849 ed
->vert_si
.nPage
= 0;
2850 ed
->vert_si
.nPos
= 0;
2852 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2853 ed
->horz_si
.nMin
= 0;
2854 ed
->horz_si
.nMax
= 0;
2855 ed
->horz_si
.nPage
= 0;
2856 ed
->horz_si
.nPos
= 0;
2858 ed
->wheel_remain
= 0;
2860 OleInitialize(NULL
);
2865 void ME_DestroyEditor(ME_TextEditor
*editor
)
2867 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2868 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2871 ME_ClearTempStyle(editor
);
2872 ME_EmptyUndoStack(editor
);
2875 ME_DestroyDisplayItem(p
);
2878 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2879 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2881 if (editor
->pFontCache
[i
].hFont
)
2882 DeleteObject(editor
->pFontCache
[i
].hFont
);
2884 if (editor
->rgbBackColor
!= -1)
2885 DeleteObject(editor
->hbrBackground
);
2886 if(editor
->lpOleCallback
)
2887 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2888 ITextHost_Release(editor
->texthost
);
2891 IRichEditOle_Release(editor
->reOle
);
2892 editor
->reOle
= NULL
;
2896 FREE_OBJ(editor
->pBuffer
);
2897 FREE_OBJ(editor
->pCursors
);
2902 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2907 case DLL_PROCESS_ATTACH
:
2908 DisableThreadLibraryCalls(hinstDLL
);
2909 me_heap
= HeapCreate (0, 0x10000, 0);
2910 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2911 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2915 case DLL_PROCESS_DETACH
:
2916 if (lpvReserved
) break;
2917 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2918 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2919 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2920 UnregisterClassA("RichEdit50A", 0);
2921 if (ME_ListBoxRegistered
)
2922 UnregisterClassW(REListBox20W
, 0);
2923 if (ME_ComboBoxRegistered
)
2924 UnregisterClassW(REComboBox20W
, 0);
2926 HeapDestroy (me_heap
);
2933 static inline int get_default_line_height( ME_TextEditor
*editor
)
2937 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
2938 height
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
2939 if (height
<= 0) height
= 24;
2944 static inline int calc_wheel_change( int *remain
, int amount_per_click
)
2946 int change
= amount_per_click
* (float)*remain
/ WHEEL_DELTA
;
2947 *remain
-= WHEEL_DELTA
* change
/ amount_per_click
;
2951 static const char * const edit_messages
[] = {
2980 "EM_SETPASSWORDCHAR",
2981 "EM_EMPTYUNDOBUFFER",
2982 "EM_GETFIRSTVISIBLELINE",
2984 "EM_SETWORDBREAKPROC",
2985 "EM_GETWORDBREAKPROC",
2986 "EM_GETPASSWORDCHAR",
2996 static const char * const richedit_messages
[] = {
3001 "EM_EXLINEFROMCHAR",
3007 "EM_GETOLEINTERFACE",
3017 "EM_SETOLECALLBACK",
3019 "EM_SETTARGETDEVICE",
3027 "EM_GETWORDBREAKPROCEX",
3028 "EM_SETWORDBREAKPROCEX",
3030 "EM_UNKNOWN_USER_83",
3035 "EM_STOPGROUPTYPING",
3039 "EM_GETAUTOURLDETECT",
3042 "EM_GETTEXTLENGTHEX",
3045 "EM_UNKNOWN_USER_98",
3046 "EM_UNKNOWN_USER_99",
3047 "EM_SETPUNCTUATION",
3048 "EM_GETPUNCTUATION",
3049 "EM_SETWORDWRAPMODE",
3050 "EM_GETWORDWRAPMODE",
3056 "EM_UNKNOWN_USER_109",
3057 "EM_UNKNOWN_USER_110",
3058 "EM_UNKNOWN_USER_111",
3059 "EM_UNKNOWN_USER_112",
3060 "EM_UNKNOWN_USER_113",
3061 "EM_UNKNOWN_USER_114",
3062 "EM_UNKNOWN_USER_115",
3063 "EM_UNKNOWN_USER_116",
3064 "EM_UNKNOWN_USER_117",
3065 "EM_UNKNOWN_USER_118",
3066 "EM_UNKNOWN_USER_119",
3067 "EM_SETLANGOPTIONS",
3068 "EM_GETLANGOPTIONS",
3069 "EM_GETIMECOMPMODE",
3073 "EM_SETIMEMODEBIAS",
3078 get_msg_name(UINT msg
)
3080 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
3081 return edit_messages
[msg
- EM_GETSEL
];
3082 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
3083 return richedit_messages
[msg
- EM_CANPASTE
];
3087 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3091 ME_Cursor cursor
; /* The start of the clicked text. */
3094 x
= (short)LOWORD(lParam
);
3095 y
= (short)HIWORD(lParam
);
3096 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3097 if (!isExact
) return;
3099 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3100 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3101 { /* The clicked run has CFE_LINK set */
3104 info
.nmhdr
.hwndFrom
= NULL
;
3105 info
.nmhdr
.idFrom
= 0;
3106 info
.nmhdr
.code
= EN_LINK
;
3108 info
.wParam
= wParam
;
3109 info
.lParam
= lParam
;
3112 /* find the first contiguous run with CFE_LINK set */
3113 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3114 for (di
= cursor
.pRun
->prev
;
3115 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3118 info
.chrg
.cpMin
-= di
->member
.run
.len
;
3121 /* find the last contiguous run with CFE_LINK set */
3122 info
.chrg
.cpMax
= ME_GetCursorOfs(&cursor
) + cursor
.pRun
->member
.run
.len
;
3123 for (di
= cursor
.pRun
->next
;
3124 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3127 info
.chrg
.cpMax
+= di
->member
.run
.len
;
3130 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3134 void ME_ReplaceSel(ME_TextEditor
*editor
, BOOL can_undo
, const WCHAR
*str
, int len
)
3136 int from
, to
, nStartCursor
;
3139 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3140 style
= ME_GetSelectionInsertStyle(editor
);
3141 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3142 ME_InsertTextFromCursor(editor
, 0, str
, len
, style
);
3143 ME_ReleaseStyle(style
);
3144 /* drop temporary style if line end */
3146 * FIXME question: does abc\n mean: put abc,
3147 * clear temp style, put \n? (would require a change)
3149 if (len
>0 && str
[len
-1] == '\n')
3150 ME_ClearTempStyle(editor
);
3151 ME_CommitUndo(editor
);
3152 ME_UpdateSelectionLinkAttribute(editor
);
3154 ME_EmptyUndoStack(editor
);
3155 ME_UpdateRepaint(editor
, FALSE
);
3158 static void ME_SetText(ME_TextEditor
*editor
, void *text
, BOOL unicode
)
3160 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3163 LPWSTR wszText
= ME_ToUnicode(codepage
, text
, &textLen
);
3169 /* uses default style! */
3170 if (!(editor
->styleFlags
& ES_MULTILINE
))
3174 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3177 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3179 ME_EndToUnicode(codepage
, wszText
);
3182 static LRESULT
ME_WmCreate(ME_TextEditor
*editor
, LPARAM lParam
, BOOL unicode
)
3184 CREATESTRUCTW
*createW
= (CREATESTRUCTW
*)lParam
;
3185 CREATESTRUCTA
*createA
= (CREATESTRUCTA
*)lParam
;
3190 text
= unicode
? (void*)createW
->lpszName
: (void*)createA
->lpszName
;
3192 ME_SetDefaultFormatRect(editor
);
3194 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
3195 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
3196 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
3198 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
3199 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
3201 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3203 if (editor
->styleFlags
& WS_VSCROLL
)
3205 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
3206 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
3208 if (editor
->styleFlags
& WS_HSCROLL
)
3210 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
3211 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
3217 ME_SetText(editor
, text
, unicode
);
3218 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
3219 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
3222 ME_CommitUndo(editor
);
3223 ME_WrapMarkedParagraphs(editor
);
3224 ME_MoveCaret(editor
);
3229 #define UNSUPPORTED_MSG(e) \
3231 FIXME(#e ": stub\n"); \
3232 *phresult = S_FALSE; \
3235 /* Handle messages for windowless and windowed richedit controls.
3237 * The LRESULT that is returned is a return value for window procs,
3238 * and the phresult parameter is the COM return code needed by the
3239 * text services interface. */
3240 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3241 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3247 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3248 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3249 UNSUPPORTED_MSG(EM_FMTLINES
)
3250 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3251 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3252 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3253 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3254 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3255 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3256 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3257 UNSUPPORTED_MSG(EM_GETREDONAME
)
3258 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3259 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3260 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3261 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3262 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3263 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3264 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3265 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
3266 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3267 UNSUPPORTED_MSG(EM_SETMARGINS
)
3268 UNSUPPORTED_MSG(EM_SETPALETTE
)
3269 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3270 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3271 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3273 /* Messages specific to Richedit controls */
3276 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3278 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3281 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
;
3284 editor
->bDialogMode
= TRUE
;
3285 if (editor
->styleFlags
& ES_MULTILINE
)
3286 code
|= DLGC_WANTMESSAGE
;
3287 if (!(editor
->styleFlags
& ES_SAVESEL
))
3288 code
|= DLGC_HASSETSEL
;
3291 case EM_EMPTYUNDOBUFFER
:
3292 ME_EmptyUndoStack(editor
);
3296 /* Note: wParam/lParam can be NULL */
3298 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3299 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3300 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3301 if ((*pfrom
|*pto
) & 0xFFFF0000)
3303 return MAKELONG(*pfrom
,*pto
);
3307 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3308 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3309 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3312 case EM_SETUNDOLIMIT
:
3314 if ((int)wParam
< 0)
3315 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3317 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3318 /* Setting a max stack size keeps wine from getting killed
3319 for hogging memory. Windows allocates all this memory at once, so
3320 no program would realistically set a value above our maximum. */
3321 return editor
->nUndoLimit
;
3324 return !list_empty( &editor
->undo_stack
);
3326 return !list_empty( &editor
->redo_stack
);
3327 case WM_UNDO
: /* FIXME: actually not the same */
3329 return ME_Undo(editor
);
3331 return ME_Redo(editor
);
3334 /* these flags are equivalent to the ES_* counterparts */
3335 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3336 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3337 DWORD settings
= editor
->styleFlags
& mask
;
3343 /* these flags are equivalent to ES_* counterparts, except for
3344 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3345 * but is still stored in editor->styleFlags. */
3346 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3347 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3348 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3349 DWORD settings
= mask
& editor
->styleFlags
;
3350 DWORD oldSettings
= settings
;
3351 DWORD changedSettings
;
3367 changedSettings
= oldSettings
^ settings
;
3369 if (changedSettings
) {
3370 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3372 if (changedSettings
& ECO_SELECTIONBAR
)
3374 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3375 if (settings
& ECO_SELECTIONBAR
) {
3376 assert(!editor
->selofs
);
3377 editor
->selofs
= SELECTIONBAR_WIDTH
;
3378 editor
->rcFormat
.left
+= editor
->selofs
;
3380 editor
->rcFormat
.left
-= editor
->selofs
;
3383 ME_RewrapRepaint(editor
);
3386 if (changedSettings
& settings
& ECO_VERTICAL
)
3387 FIXME("ECO_VERTICAL not implemented yet!\n");
3388 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3389 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3390 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3391 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3392 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3393 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3394 if (changedSettings
& settings
& ECO_WANTRETURN
)
3395 FIXME("ECO_WANTRETURN not implemented yet!\n");
3396 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3397 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3404 return handle_EM_EXSETSEL( editor
, wParam
, lParam
);
3406 case EM_SETSCROLLPOS
:
3408 POINT
*point
= (POINT
*)lParam
;
3409 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3412 case EM_AUTOURLDETECT
:
3414 if (wParam
==1 || wParam
==0)
3416 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3419 return E_INVALIDARG
;
3421 case EM_GETAUTOURLDETECT
:
3423 return editor
->AutoURLDetect_bEnable
;
3427 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3429 return handle_EM_EXSETSEL( editor
, range
.cpMin
, range
.cpMax
);
3431 case EM_SHOWSCROLLBAR
:
3444 flags
= WS_HSCROLL
|WS_VSCROLL
;
3451 editor
->styleFlags
|= flags
;
3452 if (flags
& WS_HSCROLL
)
3453 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3454 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3455 if (flags
& WS_VSCROLL
)
3456 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3457 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3459 editor
->styleFlags
&= ~flags
;
3460 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3467 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3470 BOOL bRtf
, bUnicode
, bSelection
, bUTF8
;
3471 int oldModify
= editor
->nModifyStep
;
3472 static const char utf8_bom
[] = {0xef, 0xbb, 0xbf};
3474 if (!pStruct
) return 0;
3476 /* If we detect ascii rtf at the start of the string,
3477 * we know it isn't unicode. */
3478 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3479 !strncmp((char *)lParam
, "{\\urtf", 6)));
3480 bUnicode
= !bRtf
&& pStruct
->codepage
== CP_UNICODE
;
3481 bUTF8
= (lParam
&& (!strncmp((char *)lParam
, utf8_bom
, 3)));
3483 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3484 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3485 pStruct
->flags
, pStruct
->codepage
);
3487 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3489 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3490 style
= ME_GetSelectionInsertStyle(editor
);
3491 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3494 ME_SetCursorToStart(editor
, &start
);
3495 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3496 style
= editor
->pBuffer
->pDefaultStyle
;
3500 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3502 /* FIXME: The length returned doesn't include the rtf control
3503 * characters, only the actual text. */
3504 len
= lParam
? strlen((char *)lParam
) : 0;
3507 if (bUTF8
&& !bUnicode
) {
3508 wszText
= ME_ToUnicode(CP_UTF8
, (void *)(lParam
+3), &len
);
3509 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3510 ME_EndToUnicode(CP_UTF8
, wszText
);
3512 wszText
= ME_ToUnicode(pStruct
->codepage
, (void *)lParam
, &len
);
3513 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3514 ME_EndToUnicode(pStruct
->codepage
, wszText
);
3519 ME_ReleaseStyle(style
);
3520 ME_UpdateSelectionLinkAttribute(editor
);
3524 ME_SetCursorToStart(editor
, &cursor
);
3525 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3527 ME_CommitUndo(editor
);
3528 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3530 editor
->nModifyStep
= oldModify
;
3531 ME_EmptyUndoStack(editor
);
3533 ME_UpdateRepaint(editor
, FALSE
);
3536 case EM_SETBKGNDCOLOR
:
3539 if (editor
->rgbBackColor
!= -1) {
3540 DeleteObject(editor
->hbrBackground
);
3541 lColor
= editor
->rgbBackColor
;
3543 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3547 editor
->rgbBackColor
= -1;
3548 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3552 editor
->rgbBackColor
= lParam
;
3553 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3555 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3556 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3560 return editor
->nModifyStep
== 0 ? 0 : -1;
3564 editor
->nModifyStep
= 1;
3566 editor
->nModifyStep
= 0;
3570 case EM_SETREADONLY
:
3573 editor
->styleFlags
|= ES_READONLY
;
3575 editor
->styleFlags
&= ~ES_READONLY
;
3578 case EM_SETEVENTMASK
:
3580 DWORD nOldMask
= editor
->nEventMask
;
3582 editor
->nEventMask
= lParam
;
3585 case EM_GETEVENTMASK
:
3586 return editor
->nEventMask
;
3587 case EM_SETCHARFORMAT
:
3589 CHARFORMAT2W buf
, *p
;
3590 BOOL bRepaint
= TRUE
;
3591 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3592 if (p
== NULL
) return 0;
3593 if (wParam
& SCF_ALL
) {
3594 if (editor
->mode
& TM_PLAINTEXT
) {
3595 ME_SetDefaultCharFormat(editor
, p
);
3598 ME_SetCursorToStart(editor
, &start
);
3599 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3600 editor
->nModifyStep
= 1;
3602 } else if (wParam
& SCF_SELECTION
) {
3603 if (editor
->mode
& TM_PLAINTEXT
)
3605 if (wParam
& SCF_WORD
) {
3607 ME_Cursor end
= editor
->pCursors
[0];
3608 ME_MoveCursorWords(editor
, &end
, +1);
3610 ME_MoveCursorWords(editor
, &start
, -1);
3611 ME_SetCharFormat(editor
, &start
, &end
, p
);
3613 bRepaint
= ME_IsSelection(editor
);
3614 ME_SetSelectionCharFormat(editor
, p
);
3615 if (bRepaint
) editor
->nModifyStep
= 1;
3616 } else { /* SCF_DEFAULT */
3617 ME_SetDefaultCharFormat(editor
, p
);
3619 ME_CommitUndo(editor
);
3622 ME_WrapMarkedParagraphs(editor
);
3623 ME_UpdateScrollBar(editor
);
3628 case EM_GETCHARFORMAT
:
3630 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3631 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3632 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3633 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3634 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3636 tmp
.cbSize
= sizeof(tmp
);
3638 ME_GetDefaultCharFormat(editor
, &tmp
);
3640 ME_GetSelectionCharFormat(editor
, &tmp
);
3641 ME_CopyToCFAny(dst
, &tmp
);
3644 case EM_SETPARAFORMAT
:
3646 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3647 ME_WrapMarkedParagraphs(editor
);
3648 ME_UpdateScrollBar(editor
);
3650 ME_CommitUndo(editor
);
3653 case EM_GETPARAFORMAT
:
3654 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3655 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3656 case EM_GETFIRSTVISIBLELINE
:
3658 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3659 int y
= editor
->vert_si
.nPos
;
3664 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3665 if (p
->type
== diTextEnd
)
3667 if (p
->type
== diParagraph
) {
3668 ypara
= p
->member
.para
.pt
.y
;
3671 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3672 yend
= ystart
+ p
->member
.row
.nHeight
;
3680 case EM_HIDESELECTION
:
3682 editor
->bHideSelection
= (wParam
!= 0);
3683 ME_InvalidateSelection(editor
);
3688 if (!(editor
->styleFlags
& ES_MULTILINE
))
3690 ME_ScrollDown( editor
, lParam
* get_default_line_height( editor
) );
3696 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3697 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3698 ME_CommitUndo(editor
);
3699 ME_UpdateRepaint(editor
, TRUE
);
3705 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3706 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &len
);
3708 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3710 ME_ReplaceSel(editor
, !!wParam
, wszText
, len
);
3711 ME_EndToUnicode(codepage
, wszText
);
3714 case EM_SCROLLCARET
:
3715 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3722 BOOL bRepaint
= LOWORD(lParam
);
3725 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3726 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3727 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3728 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3729 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3730 if (editor
->mode
& TM_RICHTEXT
) {
3732 ME_SetCursorToStart(editor
, &start
);
3733 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3735 ME_SetDefaultCharFormat(editor
, &fmt
);
3737 ME_CommitUndo(editor
);
3738 ME_MarkAllForWrapping(editor
);
3739 ME_WrapMarkedParagraphs(editor
);
3740 ME_UpdateScrollBar(editor
);
3748 ME_SetCursorToStart(editor
, &cursor
);
3749 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3752 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3753 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3754 !strncmp((char *)lParam
, "{\\urtf", 6))
3756 /* Undocumented: WM_SETTEXT supports RTF text */
3757 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3760 ME_SetText(editor
, (void*)lParam
, unicode
);
3763 TRACE("WM_SETTEXT - NULL\n");
3764 ME_SetCursorToStart(editor
, &cursor
);
3765 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3766 ME_SetSelection(editor
, 0, 0);
3767 editor
->nModifyStep
= 0;
3768 ME_CommitUndo(editor
);
3769 ME_EmptyUndoStack(editor
);
3770 ME_UpdateRepaint(editor
, FALSE
);
3775 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3776 if (IsClipboardFormatAvailable(nRTFFormat
))
3778 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3783 case WM_MBUTTONDOWN
:
3789 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3790 int nChars
= nTo
- nFrom
;
3791 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3793 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3795 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3796 ME_CommitUndo(editor
);
3797 ME_UpdateRepaint(editor
, TRUE
);
3801 case WM_GETTEXTLENGTH
:
3803 GETTEXTLENGTHEX how
;
3805 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3806 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3807 how
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3808 return ME_GetTextLengthEx(editor
, &how
);
3810 case EM_GETTEXTLENGTHEX
:
3811 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3815 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3816 ex
.flags
= GT_USECRLF
;
3817 ex
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3818 ex
.lpDefaultChar
= NULL
;
3819 ex
.lpUsedDefChar
= NULL
;
3820 return ME_GetTextEx(editor
, &ex
, lParam
);
3823 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3826 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3827 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3828 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3829 nTo
- nFrom
, unicode
);
3831 case EM_GETSCROLLPOS
:
3833 POINT
*point
= (POINT
*)lParam
;
3834 point
->x
= editor
->horz_si
.nPos
;
3835 point
->y
= editor
->vert_si
.nPos
;
3836 /* 16-bit scaled value is returned as stored in scrollinfo */
3837 if (editor
->horz_si
.nMax
> 0xffff)
3838 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3839 if (editor
->vert_si
.nMax
> 0xffff)
3840 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3843 case EM_GETTEXTRANGE
:
3845 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3847 int nStart
= rng
->chrg
.cpMin
;
3848 int nEnd
= rng
->chrg
.cpMax
;
3849 int textlength
= ME_GetTextLength(editor
);
3851 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3852 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3853 if (nStart
< 0) return 0;
3854 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3856 if (nStart
>= nEnd
) return 0;
3858 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3859 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3863 ME_DisplayItem
*run
;
3864 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3865 unsigned int nCharsLeft
= nMaxChars
;
3866 char *dest
= (char *) lParam
;
3867 BOOL wroteNull
= FALSE
;
3869 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3870 unicode
? "Unicode" : "Ansi");
3872 run
= ME_FindRowWithNumber(editor
, wParam
);
3876 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3877 && run
->type
== diRun
)
3879 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3882 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3885 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3887 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
3888 nCharsLeft
, NULL
, NULL
);
3889 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3890 nCharsLeft
-= nCopy
;
3893 /* append line termination, space allowing */
3897 *((WCHAR
*)dest
) = '\0';
3904 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3905 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3907 case EM_GETLINECOUNT
:
3909 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3912 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3914 while (item
!= editor
->pBuffer
->pLast
)
3916 assert(item
->type
== diParagraph
);
3917 prev_para
= ME_FindItemBack(item
, diRun
);
3919 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3921 nRows
+= item
->member
.para
.nRows
;
3922 item
= item
->member
.para
.next_para
;
3924 last_para
= ME_FindItemBack(item
, diRun
);
3926 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3927 if (editor
->bEmulateVersion10
&& prev_para
&&
3928 last_para
->member
.run
.nCharOfs
== 0 &&
3929 prev_para
->member
.run
.len
== 1 &&
3930 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
3932 /* In 1.0 emulation, the last solitary \r at the very end of the text
3933 (if one exists) is NOT a line break.
3934 FIXME: this is an ugly hack. This should have a more regular model. */
3938 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3939 return max(1, nRows
);
3941 case EM_LINEFROMCHAR
:
3944 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3946 return ME_RowNumberFromCharOfs(editor
, wParam
);
3948 case EM_EXLINEFROMCHAR
:
3951 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3953 return ME_RowNumberFromCharOfs(editor
, lParam
);
3957 ME_DisplayItem
*item
, *para
;
3961 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3963 item
= ME_FindRowWithNumber(editor
, wParam
);
3966 para
= ME_GetParagraph(item
);
3967 item
= ME_FindItemFwd(item
, diRun
);
3968 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3969 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3974 ME_DisplayItem
*item
, *item_end
;
3975 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3976 ME_DisplayItem
*para
, *run
;
3978 if (wParam
> ME_GetTextLength(editor
))
3982 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3985 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3986 item
= ME_RowStart(run
);
3987 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3988 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3989 if (item_end
->type
== diStartRow
) {
3990 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3992 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3993 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3994 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
3996 nChars
= nNextLineOfs
- nThisLineOfs
;
3997 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
4000 case EM_EXLIMITTEXT
:
4002 if ((int)lParam
< 0)
4005 editor
->nTextLimit
= 65536;
4007 editor
->nTextLimit
= (int) lParam
;
4013 editor
->nTextLimit
= 65536;
4015 editor
->nTextLimit
= (int) wParam
;
4018 case EM_GETLIMITTEXT
:
4020 return editor
->nTextLimit
;
4026 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
4027 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
4030 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
4031 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
4032 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
4035 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
4036 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4044 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
4045 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
4048 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
4049 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
4050 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
4053 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4054 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4060 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
4061 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4063 case EM_FINDTEXTEXW
:
4065 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4066 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4069 if (!wParam
|| !lParam
)
4071 *(int *)wParam
= editor
->nZoomNumerator
;
4072 *(int *)lParam
= editor
->nZoomDenominator
;
4075 return ME_SetZoom(editor
, wParam
, lParam
);
4076 case EM_CHARFROMPOS
:
4079 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
4081 return ME_GetCursorOfs(&cursor
);
4085 case EM_POSFROMCHAR
:
4087 ME_DisplayItem
*pPara
, *pRun
;
4088 int nCharOfs
, nOffset
, nLength
;
4092 /* detect which API version we're dealing with */
4093 if (wParam
>= 0x40000)
4095 nLength
= ME_GetTextLength(editor
);
4096 nCharOfs
= min(nCharOfs
, nLength
);
4097 nCharOfs
= max(nCharOfs
, 0);
4099 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
4100 assert(pRun
->type
== diRun
);
4101 pt
.y
= pRun
->member
.run
.pt
.y
;
4102 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
4103 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
4104 pt
.x
+= editor
->rcFormat
.left
;
4106 pt
.x
-= editor
->horz_si
.nPos
;
4107 pt
.y
-= editor
->vert_si
.nPos
;
4109 if (wParam
>= 0x40000) {
4110 *(POINTL
*)wParam
= pt
;
4112 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
4115 return ME_WmCreate(editor
, lParam
, unicode
);
4117 ME_DestroyEditor(editor
);
4122 if (wParam
== (WPARAM
)editor
->hWnd
&& GetCursorPos(&cursor_pos
) &&
4123 ScreenToClient(editor
->hWnd
, &cursor_pos
))
4124 ME_LinkNotify(editor
, msg
, 0, MAKELPARAM(cursor_pos
.x
, cursor_pos
.y
));
4125 return ME_SetCursor(editor
);
4127 case WM_LBUTTONDBLCLK
:
4128 case WM_LBUTTONDOWN
:
4130 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4131 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4132 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4134 ITextHost_TxSetFocus(editor
->texthost
);
4135 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
4136 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
4137 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
4138 editor
->bMouseCaptured
= TRUE
;
4139 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4140 if (!ME_SetCursor(editor
)) goto do_default
;
4144 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4145 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4147 if (editor
->bMouseCaptured
)
4148 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4150 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4151 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4152 if (editor
->bMouseCaptured
)
4153 ME_SetCursor(editor
);
4156 if (editor
->bMouseCaptured
) {
4157 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4158 editor
->bMouseCaptured
= FALSE
;
4160 if (editor
->nSelectionType
== stDocument
)
4161 editor
->nSelectionType
= stPosition
;
4162 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4163 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4167 ME_SetCursor(editor
);
4168 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4172 case WM_RBUTTONDOWN
:
4173 case WM_RBUTTONDBLCLK
:
4174 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4175 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4176 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4178 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4180 case WM_CONTEXTMENU
:
4181 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4185 editor
->bHaveFocus
= TRUE
;
4186 ME_ShowCaret(editor
);
4187 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4190 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4191 editor
->bHaveFocus
= FALSE
;
4192 editor
->wheel_remain
= 0;
4193 ME_HideCaret(editor
);
4194 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4197 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4200 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4201 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4205 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4206 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4208 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4212 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4213 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4215 return ME_Char(editor
, wParam
, lParam
, unicode
);
4219 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4220 if(wParam
<= 0x000fffff)
4222 if(wParam
> 0xffff) /* convert to surrogates */
4225 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4226 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4228 ME_Char(editor
, wParam
, 0, TRUE
);
4234 case EM_STOPGROUPTYPING
:
4235 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4239 const int scrollUnit
= 7;
4241 switch(LOWORD(wParam
))
4244 ME_ScrollAbs(editor
, 0, 0);
4247 ME_ScrollAbs(editor
,
4248 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4249 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4252 ME_ScrollLeft(editor
, scrollUnit
);
4255 ME_ScrollRight(editor
, scrollUnit
);
4258 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4261 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4264 case SB_THUMBPOSITION
:
4266 int pos
= HIWORD(wParam
);
4267 if (editor
->horz_si
.nMax
> 0xffff)
4268 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4269 ME_HScrollAbs(editor
, pos
);
4275 case EM_SCROLL
: /* fall through */
4279 int lineHeight
= get_default_line_height( editor
);
4281 origNPos
= editor
->vert_si
.nPos
;
4283 switch(LOWORD(wParam
))
4286 ME_ScrollAbs(editor
, 0, 0);
4289 ME_ScrollAbs(editor
,
4290 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4291 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4294 ME_ScrollUp(editor
,lineHeight
);
4297 ME_ScrollDown(editor
,lineHeight
);
4300 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4303 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4306 case SB_THUMBPOSITION
:
4308 int pos
= HIWORD(wParam
);
4309 if (editor
->vert_si
.nMax
> 0xffff)
4310 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4311 ME_VScrollAbs(editor
, pos
);
4315 if (msg
== EM_SCROLL
)
4316 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4324 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4325 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4328 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4330 delta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4332 /* if scrolling changes direction, ignore left overs */
4333 if ((delta
< 0 && editor
->wheel_remain
< 0) ||
4334 (delta
> 0 && editor
->wheel_remain
> 0))
4335 editor
->wheel_remain
+= delta
;
4337 editor
->wheel_remain
= delta
;
4339 if (editor
->wheel_remain
)
4343 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4347 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4349 numerator
+= calc_wheel_change( &editor
->wheel_remain
, 10 );
4350 if (numerator
>= 10 && numerator
<= 500)
4351 ME_SetZoom(editor
, numerator
, 100);
4356 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES
, 0, &max_lines
, 0 );
4358 lines
= calc_wheel_change( &editor
->wheel_remain
, (int)max_lines
);
4360 ME_ScrollDown( editor
, -lines
* get_default_line_height( editor
) );
4367 *((RECT
*)lParam
) = editor
->rcFormat
;
4368 if (editor
->bDefaultFormatRect
)
4369 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4379 RECT
*rc
= (RECT
*)lParam
;
4381 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4382 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4385 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4386 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4387 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4388 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4389 } else if (wParam
== 1) {
4390 /* MSDN incorrectly says a wParam value of 1 causes the
4391 * lParam rect to be used as a relative offset,
4392 * however, the tests show it just prevents min/max bound
4394 editor
->rcFormat
.top
= rc
->top
- border
;
4395 editor
->rcFormat
.left
= rc
->left
- border
;
4396 editor
->rcFormat
.bottom
= rc
->bottom
;
4397 editor
->rcFormat
.right
= rc
->right
+ border
;
4401 editor
->bDefaultFormatRect
= FALSE
;
4405 ME_SetDefaultFormatRect(editor
);
4406 editor
->bDefaultFormatRect
= TRUE
;
4408 ME_MarkAllForWrapping(editor
);
4409 ME_WrapMarkedParagraphs(editor
);
4410 ME_UpdateScrollBar(editor
);
4411 if (msg
!= EM_SETRECTNP
)
4415 case EM_REQUESTRESIZE
:
4416 ME_SendRequestResize(editor
, TRUE
);
4420 case WM_WINDOWPOSCHANGED
:
4423 WINDOWPOS
*winpos
= (WINDOWPOS
*)lParam
;
4425 if (winpos
->flags
& SWP_NOCLIENTSIZE
) goto do_default
;
4426 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4427 if (editor
->bDefaultFormatRect
) {
4428 ME_SetDefaultFormatRect(editor
);
4430 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4431 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4433 editor
->prevClientRect
= clientRect
;
4434 ME_RewrapRepaint(editor
);
4437 /* IME messages to make richedit controls IME aware */
4438 case WM_IME_SETCONTEXT
:
4439 case WM_IME_CONTROL
:
4441 case WM_IME_COMPOSITIONFULL
:
4443 case WM_IME_STARTCOMPOSITION
:
4445 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4446 ME_DeleteSelection(editor
);
4447 ME_CommitUndo(editor
);
4448 ME_UpdateRepaint(editor
, FALSE
);
4451 case WM_IME_COMPOSITION
:
4455 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4456 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4457 ME_DeleteSelection(editor
);
4458 ME_SaveTempStyle(editor
);
4459 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4461 LPWSTR lpCompStr
= NULL
;
4463 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4465 dwIndex
= GCS_COMPSTR
;
4467 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4468 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4469 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4470 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4471 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4472 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4474 if (dwIndex
== GCS_COMPSTR
)
4475 ME_SetSelection(editor
,editor
->imeStartIndex
,
4476 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4478 ME_ReleaseStyle(style
);
4479 ME_CommitUndo(editor
);
4480 ME_UpdateRepaint(editor
, FALSE
);
4483 case WM_IME_ENDCOMPOSITION
:
4485 ME_DeleteSelection(editor
);
4486 editor
->imeStartIndex
=-1;
4489 case EM_GETOLEINTERFACE
:
4492 if (!CreateIRichEditOle(NULL
, editor
, (LPVOID
*)&editor
->reOle
))
4494 *(LPVOID
*)lParam
= editor
->reOle
;
4495 IRichEditOle_AddRef(editor
->reOle
);
4498 case EM_GETPASSWORDCHAR
:
4500 return editor
->cPasswordMask
;
4502 case EM_SETOLECALLBACK
:
4503 if(editor
->lpOleCallback
)
4504 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4505 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4506 if(editor
->lpOleCallback
)
4507 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4509 case EM_GETWORDBREAKPROC
:
4510 return (LRESULT
)editor
->pfnWordBreak
;
4511 case EM_SETWORDBREAKPROC
:
4513 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4515 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4516 return (LRESULT
)pfnOld
;
4518 case EM_GETTEXTMODE
:
4519 return editor
->mode
;
4520 case EM_SETTEXTMODE
:
4525 if (ME_GetTextLength(editor
) ||
4526 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4527 return E_UNEXPECTED
;
4529 /* Check for mutually exclusive flags in adjacent bits of wParam */
4530 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4531 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4532 return E_INVALIDARG
;
4534 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4536 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4537 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4538 if (wParam
& TM_PLAINTEXT
) {
4539 /* Clear selection since it should be possible to select the
4540 * end of text run for rich text */
4541 ME_InvalidateSelection(editor
);
4542 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4543 editor
->pCursors
[1] = editor
->pCursors
[0];
4544 /* plain text can only have the default style. */
4545 ME_ClearTempStyle(editor
);
4546 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4547 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4548 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4551 /* FIXME: Currently no support for undo level and code page options */
4552 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4555 case EM_SETPASSWORDCHAR
:
4557 editor
->cPasswordMask
= wParam
;
4558 ME_RewrapRepaint(editor
);
4561 case EM_SETTARGETDEVICE
:
4564 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4565 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4567 editor
->bWordWrap
= new;
4568 editor
->nAvailWidth
= 0; /* wrap to client area */
4569 ME_RewrapRepaint(editor
);
4572 int width
= max(0, lParam
);
4573 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4574 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4576 editor
->nAvailWidth
= width
;
4577 editor
->bWordWrap
= TRUE
;
4578 ME_RewrapRepaint(editor
);
4580 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4585 *phresult
= S_FALSE
;
4591 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4592 LPARAM lParam
, BOOL unicode
)
4594 ME_TextEditor
*editor
;
4596 LRESULT lresult
= 0;
4598 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4599 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4601 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4604 if (msg
== WM_NCCREATE
)
4606 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4607 ITextHost
*texthost
;
4609 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4610 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4611 return texthost
!= NULL
;
4615 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4627 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4628 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4629 ME_SendOldNotify(editor
, EN_UPDATE
);
4630 /* Erase area outside of the formatting rectangle */
4631 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4634 rc
.bottom
= editor
->rcFormat
.top
;
4635 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4636 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4638 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4640 rc
.top
= editor
->rcFormat
.bottom
;
4641 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4642 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4644 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4646 rc
.right
= editor
->rcFormat
.left
;
4647 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4648 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4650 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4652 rc
.left
= editor
->rcFormat
.right
;
4653 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4654 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4657 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4658 EndPaint(editor
->hWnd
, &ps
);
4663 HDC hDC
= (HDC
)wParam
;
4666 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4667 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4673 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4674 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4676 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4677 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4678 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4679 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4682 case EM_SETREADONLY
:
4685 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4686 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4687 dwStyle
&= ~ES_READONLY
;
4689 dwStyle
|= ES_READONLY
;
4690 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4694 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4697 if (hresult
== S_FALSE
)
4698 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4700 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4701 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4706 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4708 BOOL unicode
= TRUE
;
4710 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4711 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4714 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4717 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4719 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4722 /******************************************************************
4723 * RichEditANSIWndProc (RICHED20.10)
4725 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4727 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4730 /******************************************************************
4731 * RichEdit10ANSIWndProc (RICHED20.9)
4733 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4735 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4737 ITextHost
*texthost
;
4738 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4740 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4741 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4742 return texthost
!= NULL
;
4744 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4747 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4749 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4752 /* Fill buffer with srcChars unicode characters from the start cursor.
4754 * buffer: destination buffer
4755 * buflen: length of buffer in characters excluding the NULL terminator.
4756 * start: start of editor text to copy into buffer.
4757 * srcChars: Number of characters to use from the editor text.
4758 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4760 * returns the number of characters written excluding the NULL terminator.
4762 * The written text is always NULL terminated.
4764 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4765 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
,
4768 ME_DisplayItem
*pRun
, *pNextRun
;
4769 const WCHAR
*pStart
= buffer
;
4770 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4774 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4775 if (editor
->bEmulateVersion10
) bCRLF
= FALSE
;
4779 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4781 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4782 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4784 while (srcChars
&& buflen
&& pNextRun
)
4786 int nFlags
= pRun
->member
.run
.nFlags
;
4788 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4790 if (buflen
== 1) break;
4791 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4792 * EM_GETTEXTEX, however, this is done for copying text which
4793 * also uses this function. */
4794 srcChars
-= min(nLen
, srcChars
);
4798 nLen
= min(nLen
, srcChars
);
4802 nLen
= min(nLen
, buflen
);
4805 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4810 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4812 nLen
= pRun
->member
.run
.len
;
4813 str
= get_text( &pRun
->member
.run
, 0 );
4815 /* append '\r' to the last paragraph. */
4816 if (pRun
->next
->type
== diTextEnd
&& bEOP
)
4822 return buffer
- pStart
;
4825 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4830 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4831 wcW
.lpfnWndProc
= RichEditWndProcW
;
4833 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4834 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4836 wcW
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_IBEAM
);
4837 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4838 wcW
.lpszMenuName
= NULL
;
4840 if (is_version_nt())
4842 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4843 if (!RegisterClassW(&wcW
)) return FALSE
;
4844 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4845 if (!RegisterClassW(&wcW
)) return FALSE
;
4849 /* WNDCLASSA/W have the same layout */
4850 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4851 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4852 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4853 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4856 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4857 wcA
.lpfnWndProc
= RichEditWndProcA
;
4859 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4860 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4862 wcA
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_IBEAM
);
4863 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4864 wcA
.lpszMenuName
= NULL
;
4865 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4866 if (!RegisterClassA(&wcA
)) return FALSE
;
4867 wcA
.lpszClassName
= "RichEdit50A";
4868 if (!RegisterClassA(&wcA
)) return FALSE
;
4873 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4874 /* FIXME: Not implemented */
4875 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4876 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4877 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4880 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4881 /* FIXME: Not implemented */
4882 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4883 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4884 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4887 /******************************************************************
4888 * REExtendedRegisterClass (RICHED20.8)
4890 * FIXME undocumented
4891 * Need to check for errors and implement controls and callbacks
4893 LRESULT WINAPI
REExtendedRegisterClass(void)
4898 FIXME("semi stub\n");
4902 wcW
.hInstance
= NULL
;
4905 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4906 wcW
.lpszMenuName
= NULL
;
4908 if (!ME_ListBoxRegistered
)
4910 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4911 wcW
.lpfnWndProc
= REListWndProc
;
4912 wcW
.lpszClassName
= REListBox20W
;
4913 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4916 if (!ME_ComboBoxRegistered
)
4918 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4919 wcW
.lpfnWndProc
= REComboWndProc
;
4920 wcW
.lpszClassName
= REComboBox20W
;
4921 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4925 if (ME_ListBoxRegistered
)
4927 if (ME_ComboBoxRegistered
)
4933 static BOOL
isurlspecial(WCHAR c
)
4935 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4936 return strchrW( special_chars
, c
) != NULL
;
4940 * This proc takes a selection, and scans it forward in order to select the span
4941 * of a possible URL candidate. A possible URL candidate must start with isalnum
4942 * or one of the following special characters: *|/\+%#@ and must consist entirely
4943 * of the characters allowed to start the URL, plus : (colon) which may occur
4944 * at most once, and not at either end.
4946 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
4947 const ME_Cursor
*start
,
4949 ME_Cursor
*candidate_min
,
4950 ME_Cursor
*candidate_max
)
4952 ME_Cursor cursor
= *start
;
4953 BOOL foundColon
= FALSE
;
4954 BOOL candidateStarted
= FALSE
;
4955 WCHAR lastAcceptedChar
= '\0';
4959 WCHAR
*strStart
= get_text( &cursor
.pRun
->member
.run
, 0 );
4960 WCHAR
*str
= strStart
+ cursor
.nOffset
;
4961 int nLen
= cursor
.pRun
->member
.run
.len
- cursor
.nOffset
;
4964 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4966 /* Find start of candidate */
4967 if (!candidateStarted
)
4972 if (isalnumW(*str
) || isurlspecial(*str
))
4974 cursor
.nOffset
= str
- strStart
;
4975 *candidate_min
= cursor
;
4976 candidateStarted
= TRUE
;
4977 lastAcceptedChar
= *str
++;
4984 /* Find end of candidate */
4985 if (candidateStarted
) {
4989 if (*str
== ':' && !foundColon
) {
4991 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
4992 cursor
.nOffset
= str
- strStart
;
4993 if (lastAcceptedChar
== ':')
4994 ME_MoveCursorChars(editor
, &cursor
, -1);
4995 *candidate_max
= cursor
;
4998 lastAcceptedChar
= *str
++;
5002 /* End of paragraph: skip it if before candidate span, or terminates
5003 current active span */
5004 if (candidateStarted
) {
5005 if (lastAcceptedChar
== ':')
5006 ME_MoveCursorChars(editor
, &cursor
, -1);
5007 *candidate_max
= cursor
;
5012 /* Reaching this point means no span was found, so get next span */
5013 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
5014 if (candidateStarted
) {
5015 /* There are no further runs, so take end of text as end of candidate */
5016 cursor
.nOffset
= str
- strStart
;
5017 if (lastAcceptedChar
== ':')
5018 ME_MoveCursorChars(editor
, &cursor
, -1);
5019 *candidate_max
= cursor
;
5022 *candidate_max
= *candidate_min
= cursor
;
5028 if (candidateStarted
) {
5029 /* There are no further runs, so take end of text as end of candidate */
5030 if (lastAcceptedChar
== ':')
5031 ME_MoveCursorChars(editor
, &cursor
, -1);
5032 *candidate_max
= cursor
;
5035 *candidate_max
= *candidate_min
= cursor
;
5040 * This proc evaluates the selection and returns TRUE if it can be considered an URL
5042 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
5044 #define MAX_PREFIX_LEN 9
5046 const WCHAR text
[MAX_PREFIX_LEN
];
5049 {{'p','r','o','s','p','e','r','o',':'}, 9},
5050 {{'t','e','l','n','e','t',':'}, 7},
5051 {{'g','o','p','h','e','r',':'}, 7},
5052 {{'m','a','i','l','t','o',':'}, 7},
5053 {{'h','t','t','p','s',':'}, 6},
5054 {{'f','i','l','e',':'}, 5},
5055 {{'n','e','w','s',':'}, 5},
5056 {{'w','a','i','s',':'}, 5},
5057 {{'n','n','t','p',':'}, 5},
5058 {{'h','t','t','p',':'}, 5},
5059 {{'w','w','w','.'}, 4},
5060 {{'f','t','p',':'}, 4},
5062 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
5065 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, FALSE
, FALSE
);
5066 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
5068 if (nChars
< prefixes
[i
].length
) continue;
5069 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
5073 #undef MAX_PREFIX_LEN
5077 * This proc walks through the indicated selection and evaluates whether each
5078 * section identified by ME_FindNextURLCandidate and in-between sections have
5079 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5080 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5082 * Since this function can cause runs to be split, do not depend on the value
5083 * of the start cursor at the end of the function.
5085 * nChars may be set to INT_MAX to update to the end of the text.
5087 * Returns TRUE if at least one section was modified.
5089 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
5091 BOOL modified
= FALSE
;
5092 ME_Cursor startCur
= *start
;
5094 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
5099 ME_Cursor candidateStart
, candidateEnd
;
5101 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
5102 &candidateStart
, &candidateEnd
))
5104 /* Section before candidate is not an URL */
5105 int cMin
= ME_GetCursorOfs(&candidateStart
);
5106 int cMax
= ME_GetCursorOfs(&candidateEnd
);
5108 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
5109 candidateStart
= candidateEnd
;
5110 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
5114 /* No more candidates until end of selection */
5118 if (startCur
.pRun
!= candidateStart
.pRun
||
5119 startCur
.nOffset
!= candidateStart
.nOffset
)
5121 /* CFE_LINK effect should be consistently unset */
5122 link
.cbSize
= sizeof(link
);
5123 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5124 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
5126 /* CFE_LINK must be unset from this range */
5127 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5128 link
.cbSize
= sizeof(link
);
5129 link
.dwMask
= CFM_LINK
;
5131 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5132 /* Update candidateEnd since setting character formats may split
5133 * runs, which can cause a cursor to be at an invalid offset within
5135 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
5137 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
5138 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
5143 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
5144 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
5146 /* CFE_LINK effect should be consistently set */
5147 link
.cbSize
= sizeof(link
);
5148 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5149 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
5151 /* CFE_LINK must be set on this range */
5152 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5153 link
.cbSize
= sizeof(link
);
5154 link
.dwMask
= CFM_LINK
;
5155 link
.dwEffects
= CFE_LINK
;
5156 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5160 startCur
= candidateEnd
;
5161 } while (nChars
> 0);