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?
234 #define STACK_SIZE_DEFAULT 100
235 #define STACK_SIZE_MAX 1000
237 #define TEXT_LIMIT_DEFAULT 32767
239 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
241 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
242 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
244 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
245 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
246 static HCURSOR hLeft
;
249 HANDLE me_heap
= NULL
;
251 static BOOL ME_ListBoxRegistered
= FALSE
;
252 static BOOL ME_ComboBoxRegistered
= FALSE
;
254 static inline int is_version_nt(void)
256 return !(GetVersion() & 0x80000000);
259 static ME_TextBuffer
*ME_MakeText(void) {
261 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
263 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
264 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
270 p1
->member
.para
.next_para
= p2
;
271 p2
->member
.para
.prev_para
= p1
;
272 p2
->member
.para
.nCharOfs
= 0;
276 buf
->pCharStyle
= NULL
;
282 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
285 LRESULT total_bytes_read
= 0;
286 BOOL is_read
= FALSE
;
287 static const char bom_utf8
[] = {0xEF, 0xBB, 0xBF};
289 TRACE("%08x %p\n", dwFormat
, stream
);
293 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
297 ME_StreamInFill(stream
);
298 if (stream
->editstream
->dwError
)
302 total_bytes_read
+= stream
->dwSize
;
305 if (!(dwFormat
& SF_UNICODE
))
307 char * buf
= stream
->buffer
;
308 DWORD size
= stream
->dwSize
;
314 if (stream
->dwSize
>= 3 && !memcmp(stream
->buffer
, bom_utf8
, 3))
322 nWideChars
= MultiByteToWideChar(cp
, 0, buf
, size
, wszText
, STREAMIN_BUFFER_SIZE
);
327 nWideChars
= stream
->dwSize
>> 1;
328 pText
= (WCHAR
*)stream
->buffer
;
331 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
332 if (stream
->dwSize
== 0)
336 return total_bytes_read
;
339 static void ME_ApplyBorderProperties(RTF_Info
*info
,
340 ME_BorderRect
*borderRect
,
341 RTFBorder
*borderDef
)
344 ME_Border
*pBorders
[] = {&borderRect
->top
,
348 for (i
= 0; i
< 4; i
++)
350 RTFColor
*colorDef
= info
->colorList
;
351 pBorders
[i
]->width
= borderDef
[i
].width
;
352 colorNum
= borderDef
[i
].color
;
353 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
354 colorDef
= colorDef
->rtfNextColor
;
356 pBorders
[i
]->colorRef
= RGB(
357 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
358 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
359 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
361 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
365 void ME_RTFCharAttrHook(RTF_Info
*info
)
368 fmt
.cbSize
= sizeof(fmt
);
372 switch(info
->rtfMinor
)
375 /* FIXME add more flags once they're implemented */
376 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
377 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
378 fmt
.yHeight
= 12*20; /* 12pt */
379 fmt
.wWeight
= FW_NORMAL
;
380 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
383 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
384 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
385 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
388 fmt
.dwMask
= CFM_ITALIC
;
389 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
392 fmt
.dwMask
= CFM_UNDERLINETYPE
;
393 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
395 case rtfDotUnderline
:
396 fmt
.dwMask
= CFM_UNDERLINETYPE
;
397 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
400 fmt
.dwMask
= CFM_UNDERLINETYPE
;
401 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
403 case rtfWordUnderline
:
404 fmt
.dwMask
= CFM_UNDERLINETYPE
;
405 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
408 fmt
.dwMask
= CFM_UNDERLINETYPE
;
409 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
412 fmt
.dwMask
= CFM_STRIKEOUT
;
413 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
417 case rtfSubScrShrink
:
418 case rtfSuperScrShrink
:
420 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
421 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
422 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
423 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
426 fmt
.dwMask
= CFM_HIDDEN
;
427 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
430 fmt
.dwMask
= CFM_BACKCOLOR
;
432 if (info
->rtfParam
== 0)
433 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
434 else if (info
->rtfParam
!= rtfNoParam
)
436 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
437 if (c
&& c
->rtfCBlue
>= 0)
438 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
440 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
444 fmt
.dwMask
= CFM_COLOR
;
446 if (info
->rtfParam
== 0)
447 fmt
.dwEffects
= CFE_AUTOCOLOR
;
448 else if (info
->rtfParam
!= rtfNoParam
)
450 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
451 if (c
&& c
->rtfCBlue
>= 0)
452 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
454 fmt
.dwEffects
= CFE_AUTOCOLOR
;
459 if (info
->rtfParam
!= rtfNoParam
)
461 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
464 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
465 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
466 fmt
.bCharSet
= f
->rtfFCharSet
;
467 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
468 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
473 fmt
.dwMask
= CFM_SIZE
;
474 if (info
->rtfParam
!= rtfNoParam
)
475 fmt
.yHeight
= info
->rtfParam
*10;
480 RTFFlushOutputBuffer(info
);
481 /* FIXME too slow ? how come ? */
482 style2
= ME_ApplyStyle(info
->style
, &fmt
);
483 ME_ReleaseStyle(info
->style
);
484 info
->style
= style2
;
485 info
->styleChanged
= TRUE
;
489 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
490 the same tags mean different things in different contexts */
491 void ME_RTFParAttrHook(RTF_Info
*info
)
494 fmt
.cbSize
= sizeof(fmt
);
497 switch(info
->rtfMinor
)
499 case rtfParDef
: /* restores default paragraph attributes */
500 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
501 info
->borderType
= RTFBorderParaLeft
;
502 else /* v1.0 - 3.0 */
503 info
->borderType
= RTFBorderParaTop
;
504 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
505 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
507 /* TODO: numbering, shading */
508 fmt
.wAlignment
= PFA_LEFT
;
510 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
511 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
512 fmt
.wBorderSpace
= 0;
513 fmt
.bLineSpacingRule
= 0;
514 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
515 fmt
.dyLineSpacing
= 0;
516 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
518 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
519 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
522 ME_DisplayItem
*para
;
523 /* We are just after a table row. */
524 RTFFlushOutputBuffer(info
);
525 cursor
= info
->editor
->pCursors
[0];
527 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
528 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
530 /* Since the table row end, no text has been inserted, and the \intbl
531 * control word has not be used. We can confirm that we are not in a
534 info
->tableDef
->tableRowStart
= NULL
;
535 info
->canInheritInTbl
= FALSE
;
538 } else { /* v1.0 - v3.0 */
539 fmt
.dwMask
|= PFM_TABLE
;
540 fmt
.wEffects
&= ~PFE_TABLE
;
544 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
546 while (info
->rtfParam
> info
->nestingLevel
) {
547 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
548 ZeroMemory(tableDef
, sizeof(RTFTable
));
549 tableDef
->parent
= info
->tableDef
;
550 info
->tableDef
= tableDef
;
552 RTFFlushOutputBuffer(info
);
553 if (tableDef
->tableRowStart
&&
554 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
556 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
557 para
= para
->member
.para
.next_para
;
558 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
559 tableDef
->tableRowStart
= para
;
563 cursor
= info
->editor
->pCursors
[0];
564 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
565 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
566 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
569 info
->nestingLevel
++;
571 info
->canInheritInTbl
= FALSE
;
576 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
578 if (info
->nestingLevel
< 1)
583 info
->tableDef
= ALLOC_OBJ(RTFTable
);
584 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
586 tableDef
= info
->tableDef
;
587 RTFFlushOutputBuffer(info
);
588 if (tableDef
->tableRowStart
&&
589 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
591 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
592 para
= para
->member
.para
.next_para
;
593 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
594 tableDef
->tableRowStart
= para
;
598 cursor
= info
->editor
->pCursors
[0];
599 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
600 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
601 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
603 info
->nestingLevel
= 1;
604 info
->canInheritInTbl
= TRUE
;
607 } else { /* v1.0 - v3.0 */
608 fmt
.dwMask
|= PFM_TABLE
;
609 fmt
.wEffects
|= PFE_TABLE
;
614 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
615 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
616 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
617 fmt
.dxOffset
= -info
->rtfParam
;
620 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
621 fmt
.dwMask
= PFM_STARTINDENT
;
622 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
625 fmt
.dwMask
= PFM_RIGHTINDENT
;
626 fmt
.dxRightIndent
= info
->rtfParam
;
630 fmt
.dwMask
= PFM_ALIGNMENT
;
631 fmt
.wAlignment
= PFA_LEFT
;
634 fmt
.dwMask
= PFM_ALIGNMENT
;
635 fmt
.wAlignment
= PFA_RIGHT
;
638 fmt
.dwMask
= PFM_ALIGNMENT
;
639 fmt
.wAlignment
= PFA_CENTER
;
642 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
643 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
647 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
648 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
649 fmt
.dwMask
= PFM_TABSTOPS
;
652 fmt
.dwMask
= PFM_KEEP
;
653 fmt
.wEffects
= PFE_KEEP
;
655 case rtfNoWidowControl
:
656 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
657 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
660 fmt
.dwMask
= PFM_KEEPNEXT
;
661 fmt
.wEffects
= PFE_KEEPNEXT
;
664 fmt
.dwMask
= PFM_SPACEAFTER
;
665 fmt
.dySpaceAfter
= info
->rtfParam
;
668 fmt
.dwMask
= PFM_SPACEBEFORE
;
669 fmt
.dySpaceBefore
= info
->rtfParam
;
671 case rtfSpaceBetween
:
672 fmt
.dwMask
= PFM_LINESPACING
;
673 if ((int)info
->rtfParam
> 0)
675 fmt
.dyLineSpacing
= info
->rtfParam
;
676 fmt
.bLineSpacingRule
= 3;
680 fmt
.dyLineSpacing
= info
->rtfParam
;
681 fmt
.bLineSpacingRule
= 4;
684 case rtfSpaceMultiply
:
685 fmt
.dwMask
= PFM_LINESPACING
;
686 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
687 fmt
.bLineSpacingRule
= 5;
690 fmt
.dwMask
= PFM_NUMBERING
;
691 fmt
.wNumbering
= PFN_BULLET
;
694 fmt
.dwMask
= PFM_NUMBERING
;
695 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
697 case rtfParNumDecimal
:
698 fmt
.dwMask
= PFM_NUMBERING
;
699 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
701 case rtfParNumIndent
:
702 fmt
.dwMask
= PFM_NUMBERINGTAB
;
703 fmt
.wNumberingTab
= info
->rtfParam
;
705 case rtfParNumStartAt
:
706 fmt
.dwMask
= PFM_NUMBERINGSTART
;
707 fmt
.wNumberingStart
= info
->rtfParam
;
710 info
->borderType
= RTFBorderParaLeft
;
711 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
712 if (!(fmt
.dwMask
& PFM_BORDER
))
714 fmt
.wBorderSpace
= 0;
715 fmt
.wBorderWidth
= 1;
719 fmt
.dwMask
= PFM_BORDER
;
722 info
->borderType
= RTFBorderParaRight
;
723 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
724 if (!(fmt
.dwMask
& PFM_BORDER
))
726 fmt
.wBorderSpace
= 0;
727 fmt
.wBorderWidth
= 1;
731 fmt
.dwMask
= PFM_BORDER
;
734 info
->borderType
= RTFBorderParaTop
;
735 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
736 if (!(fmt
.dwMask
& PFM_BORDER
))
738 fmt
.wBorderSpace
= 0;
739 fmt
.wBorderWidth
= 1;
743 fmt
.dwMask
= PFM_BORDER
;
745 case rtfBorderBottom
:
746 info
->borderType
= RTFBorderParaBottom
;
747 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
748 if (!(fmt
.dwMask
& PFM_BORDER
))
750 fmt
.wBorderSpace
= 0;
751 fmt
.wBorderWidth
= 1;
755 fmt
.dwMask
= PFM_BORDER
;
757 case rtfBorderSingle
:
758 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
759 /* we assume that borders have been created before (RTF spec) */
760 fmt
.wBorders
&= ~0x700;
761 fmt
.wBorders
|= 1 << 8;
762 fmt
.dwMask
= PFM_BORDER
;
765 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
766 /* we assume that borders have been created before (RTF spec) */
767 fmt
.wBorders
&= ~0x700;
768 fmt
.wBorders
|= 2 << 8;
769 fmt
.dwMask
= PFM_BORDER
;
771 case rtfBorderShadow
:
772 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
773 /* we assume that borders have been created before (RTF spec) */
774 fmt
.wBorders
&= ~0x700;
775 fmt
.wBorders
|= 10 << 8;
776 fmt
.dwMask
= PFM_BORDER
;
778 case rtfBorderDouble
:
779 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
780 /* we assume that borders have been created before (RTF spec) */
781 fmt
.wBorders
&= ~0x700;
782 fmt
.wBorders
|= 7 << 8;
783 fmt
.dwMask
= PFM_BORDER
;
786 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
787 /* we assume that borders have been created before (RTF spec) */
788 fmt
.wBorders
&= ~0x700;
789 fmt
.wBorders
|= 11 << 8;
790 fmt
.dwMask
= PFM_BORDER
;
794 int borderSide
= info
->borderType
& RTFBorderSideMask
;
795 RTFTable
*tableDef
= info
->tableDef
;
796 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
797 /* we assume that borders have been created before (RTF spec) */
798 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
799 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
802 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
804 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
805 border
->width
= info
->rtfParam
;
808 fmt
.dwMask
= PFM_BORDER
;
812 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
813 /* we assume that borders have been created before (RTF spec) */
814 fmt
.wBorderSpace
= info
->rtfParam
;
815 fmt
.dwMask
= PFM_BORDER
;
819 RTFTable
*tableDef
= info
->tableDef
;
820 int borderSide
= info
->borderType
& RTFBorderSideMask
;
821 int borderType
= info
->borderType
& RTFBorderTypeMask
;
824 case RTFBorderTypePara
:
825 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
827 /* v1.0 - 3.0 treat paragraph and row borders the same. */
828 case RTFBorderTypeRow
:
830 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
833 case RTFBorderTypeCell
:
834 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
835 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
842 fmt
.dwMask
= PFM_RTLPARA
;
843 fmt
.wEffects
= PFE_RTLPARA
;
846 fmt
.dwMask
= PFM_RTLPARA
;
851 RTFFlushOutputBuffer(info
);
852 /* FIXME too slow ? how come ?*/
853 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
857 void ME_RTFTblAttrHook(RTF_Info
*info
)
859 switch (info
->rtfMinor
)
863 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
864 info
->borderType
= 0; /* Not sure */
865 else /* v1.0 - 3.0 */
866 info
->borderType
= RTFBorderRowTop
;
867 if (!info
->tableDef
) {
868 info
->tableDef
= ME_MakeTableDef(info
->editor
);
870 ME_InitTableDef(info
->editor
, info
->tableDef
);
879 info
->tableDef
= ME_MakeTableDef(info
->editor
);
881 cellNum
= info
->tableDef
->numCellsDefined
;
882 if (cellNum
>= MAX_TABLE_CELLS
)
884 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
885 if (cellNum
< MAX_TAB_STOPS
) {
886 /* Tab stops were used to store cell positions before v4.1 but v4.1
887 * still seems to set the tabstops without using them. */
888 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
889 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
890 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
891 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
893 info
->tableDef
->numCellsDefined
++;
897 info
->borderType
= RTFBorderRowTop
;
900 info
->borderType
= RTFBorderRowLeft
;
902 case rtfRowBordBottom
:
903 info
->borderType
= RTFBorderRowBottom
;
905 case rtfRowBordRight
:
906 info
->borderType
= RTFBorderRowRight
;
909 info
->borderType
= RTFBorderCellTop
;
911 case rtfCellBordLeft
:
912 info
->borderType
= RTFBorderCellLeft
;
914 case rtfCellBordBottom
:
915 info
->borderType
= RTFBorderCellBottom
;
917 case rtfCellBordRight
:
918 info
->borderType
= RTFBorderCellRight
;
922 info
->tableDef
->gapH
= info
->rtfParam
;
926 info
->tableDef
->leftEdge
= info
->rtfParam
;
931 void ME_RTFSpecialCharHook(RTF_Info
*info
)
933 RTFTable
*tableDef
= info
->tableDef
;
934 switch (info
->rtfMinor
)
937 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
939 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
943 RTFFlushOutputBuffer(info
);
944 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
945 if (tableDef
->tableRowStart
)
947 if (!info
->nestingLevel
&&
948 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
950 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
951 para
= para
->member
.para
.next_para
;
952 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
953 tableDef
->tableRowStart
= para
;
954 info
->nestingLevel
= 1;
956 ME_InsertTableCellFromCursor(info
->editor
);
958 } else { /* v1.0 - v3.0 */
959 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
960 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
961 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
962 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
965 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
966 tableDef
->numCellsInserted
++;
971 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
973 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
976 ME_DisplayItem
*para
, *cell
, *run
;
981 RTFFlushOutputBuffer(info
);
982 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
983 if (!tableDef
->tableRowStart
)
985 if (!info
->nestingLevel
&&
986 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
988 para
= tableDef
->tableRowStart
;
989 para
= para
->member
.para
.next_para
;
990 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
991 tableDef
->tableRowStart
= para
;
992 info
->nestingLevel
++;
994 para
= tableDef
->tableRowStart
;
995 cell
= ME_FindItemFwd(para
, diCell
);
996 assert(cell
&& !cell
->member
.cell
.prev_cell
);
997 if (tableDef
->numCellsDefined
< 1)
999 /* 2000 twips appears to be the cell size that native richedit uses
1000 * when no cell sizes are specified. */
1001 const int defaultCellSize
= 2000;
1002 int nRightBoundary
= defaultCellSize
;
1003 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1004 while (cell
->member
.cell
.next_cell
) {
1005 cell
= cell
->member
.cell
.next_cell
;
1006 nRightBoundary
+= defaultCellSize
;
1007 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1009 para
= ME_InsertTableCellFromCursor(info
->editor
);
1010 cell
= para
->member
.para
.pCell
;
1011 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1013 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
1015 RTFCell
*cellDef
= &tableDef
->cells
[i
];
1016 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
1017 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
1019 cell
= cell
->member
.cell
.next_cell
;
1022 para
= ME_InsertTableCellFromCursor(info
->editor
);
1023 cell
= para
->member
.para
.pCell
;
1026 /* Cell for table row delimiter is empty */
1027 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1030 run
= ME_FindItemFwd(cell
, diRun
);
1031 if (info
->editor
->pCursors
[0].pRun
!= run
||
1032 info
->editor
->pCursors
[0].nOffset
)
1035 /* Delete inserted cells that aren't defined. */
1036 info
->editor
->pCursors
[1].pRun
= run
;
1037 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1038 info
->editor
->pCursors
[1].nOffset
= 0;
1039 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1040 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1041 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1045 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1046 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1047 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1048 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1050 info
->nestingLevel
--;
1051 if (!info
->nestingLevel
)
1053 if (info
->canInheritInTbl
) {
1054 tableDef
->tableRowStart
= para
;
1056 while (info
->tableDef
) {
1057 tableDef
= info
->tableDef
;
1058 info
->tableDef
= tableDef
->parent
;
1059 heap_free(tableDef
);
1063 info
->tableDef
= tableDef
->parent
;
1064 heap_free(tableDef
);
1066 } else { /* v1.0 - v3.0 */
1068 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1069 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1070 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1071 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1073 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1075 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1078 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1079 tableDef
->numCellsInserted
++;
1081 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1082 if (!tableDef
->numCellsDefined
)
1083 pFmt
->wEffects
&= ~PFE_TABLE
;
1084 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1085 tableDef
->numCellsInserted
= 0;
1091 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1092 ME_DisplayItem
*para
;
1094 RTFFlushOutputBuffer(info
);
1095 para
= info
->editor
->pCursors
[0].pPara
;
1096 pFmt
= para
->member
.para
.pFmt
;
1097 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1099 /* rtfPar is treated like a space within a table. */
1100 info
->rtfClass
= rtfText
;
1101 info
->rtfMajor
= ' ';
1103 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1104 tableDef
->numCellsInserted
= 0;
1110 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1113 LPOLEOBJECT lpObject
= NULL
;
1114 LPSTORAGE lpStorage
= NULL
;
1115 LPOLECLIENTSITE lpClientSite
= NULL
;
1116 LPDATAOBJECT lpDataObject
= NULL
;
1117 LPOLECACHE lpOleCache
= NULL
;
1126 stgm
.tymed
= TYMED_ENHMF
;
1127 stgm
.u
.hEnhMetaFile
= hemf
;
1128 fm
.cfFormat
= CF_ENHMETAFILE
;
1132 stgm
.tymed
= TYMED_GDI
;
1133 stgm
.u
.hBitmap
= hbmp
;
1134 fm
.cfFormat
= CF_BITMAP
;
1136 stgm
.pUnkForRelease
= NULL
;
1139 fm
.dwAspect
= DVASPECT_CONTENT
;
1141 fm
.tymed
= stgm
.tymed
;
1143 if (!info
->lpRichEditOle
)
1145 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1148 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1150 /* FIXME: enable it when rich-edit properly implements this method */
1151 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1152 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1154 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1155 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1156 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1157 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1158 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1162 reobject
.cbStruct
= sizeof(reobject
);
1163 reobject
.cp
= REO_CP_SELECTION
;
1164 reobject
.clsid
= clsid
;
1165 reobject
.poleobj
= lpObject
;
1166 reobject
.pstg
= lpStorage
;
1167 reobject
.polesite
= lpClientSite
;
1168 /* convert from twips to .01 mm */
1169 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1170 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1171 reobject
.dvaspect
= DVASPECT_CONTENT
;
1172 reobject
.dwFlags
= 0; /* FIXME */
1173 reobject
.dwUser
= 0;
1175 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1179 if (lpObject
) IOleObject_Release(lpObject
);
1180 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1181 if (lpStorage
) IStorage_Release(lpStorage
);
1182 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1183 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1188 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1191 BYTE
* buffer
= NULL
;
1192 unsigned bufsz
, bufidx
;
1198 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1201 if (info
->rtfClass
== rtfEOF
)
1204 /* fetch picture type */
1205 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1207 mfp
.mm
= info
->rtfParam
;
1210 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1212 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1215 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1217 gfx
= gfx_enhmetafile
;
1221 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1225 /* fetch picture attributes */
1229 if (info
->rtfClass
== rtfEOF
)
1231 if (info
->rtfClass
== rtfText
)
1233 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1235 ERR("Expected picture attribute (%d %d)\n",
1236 info
->rtfClass
, info
->rtfMajor
);
1239 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1241 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1243 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1245 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1247 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1248 sz
.cx
= info
->rtfParam
;
1249 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1250 sz
.cy
= info
->rtfParam
;
1252 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1254 /* fetch picture data */
1257 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1258 val
= info
->rtfMajor
;
1259 for (flip
= TRUE
;; flip
= !flip
)
1262 if (info
->rtfClass
== rtfEOF
)
1264 HeapFree(GetProcessHeap(), 0, buffer
);
1265 return; /* Warn ?? */
1267 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1269 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1272 if (bufidx
>= bufsz
&&
1273 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1275 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1278 val
= info
->rtfMajor
;
1280 if (flip
) FIXME("wrong hex string\n");
1284 case gfx_enhmetafile
:
1285 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1286 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1289 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1290 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1294 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1296 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1298 /* not quite right, especially for bitfields type of compression */
1299 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1300 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1301 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1302 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1303 bi
, DIB_RGB_COLORS
)))
1304 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1311 HeapFree(GetProcessHeap(), 0, buffer
);
1312 RTFRouteToken (info
); /* feed "}" back to router */
1315 HeapFree(GetProcessHeap(), 0, buffer
);
1317 RTFRouteToken(info
); /* feed "}" back to router */
1320 /* for now, lookup the \result part and use it, whatever the object */
1321 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1326 if (info
->rtfClass
== rtfEOF
)
1328 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1330 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1333 if (info
->rtfClass
== rtfEOF
)
1335 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1339 while (RTFGetToken (info
) != rtfEOF
)
1341 if (info
->rtfClass
== rtfGroup
)
1343 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1344 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1346 RTFRouteToken(info
);
1349 else RTFSkipGroup(info
);
1352 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1354 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1358 RTFRouteToken(info
); /* feed "}" back to router */
1361 static void ME_RTFReadHook(RTF_Info
*info
)
1363 switch(info
->rtfClass
)
1366 switch(info
->rtfMajor
)
1369 if (info
->stackTop
< maxStack
) {
1370 info
->stack
[info
->stackTop
].style
= info
->style
;
1371 ME_AddRefStyle(info
->style
);
1372 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1373 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1376 info
->styleChanged
= FALSE
;
1380 RTFFlushOutputBuffer(info
);
1382 if (info
->stackTop
<= 0)
1383 info
->rtfClass
= rtfEOF
;
1384 if (info
->stackTop
< 0)
1387 ME_ReleaseStyle(info
->style
);
1388 info
->style
= info
->stack
[info
->stackTop
].style
;
1389 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1390 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1399 ME_StreamInFill(ME_InStream
*stream
)
1401 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1402 (BYTE
*)stream
->buffer
,
1403 sizeof(stream
->buffer
),
1404 (LONG
*)&stream
->dwSize
);
1408 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1412 int from
, to
, nUndoMode
;
1413 int nEventMask
= editor
->nEventMask
;
1414 ME_InStream inStream
;
1415 BOOL invalidRTF
= FALSE
;
1416 ME_Cursor
*selStart
, *selEnd
;
1417 LRESULT num_read
= 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1419 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1420 editor
->nEventMask
= 0;
1422 ME_GetSelectionOfs(editor
, &from
, &to
);
1423 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1425 ME_GetSelection(editor
, &selStart
, &selEnd
);
1426 style
= ME_GetSelectionInsertStyle(editor
);
1428 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1430 /* Don't insert text at the end of the table row */
1431 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1432 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1433 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1435 para
= para
->member
.para
.next_para
;
1436 editor
->pCursors
[0].pPara
= para
;
1437 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1438 editor
->pCursors
[0].nOffset
= 0;
1440 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1442 para
= para
->member
.para
.next_para
;
1443 editor
->pCursors
[0].pPara
= para
;
1444 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1445 editor
->pCursors
[0].nOffset
= 0;
1447 editor
->pCursors
[1] = editor
->pCursors
[0];
1448 } else { /* v1.0 - 3.0 */
1449 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1450 ME_IsInTable(editor
->pCursors
[0].pRun
))
1454 style
= editor
->pBuffer
->pDefaultStyle
;
1455 ME_AddRefStyle(style
);
1456 ME_SetSelection(editor
, 0, 0);
1457 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1458 ME_GetTextLength(editor
), FALSE
);
1460 ME_ClearTempStyle(editor
);
1461 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1465 /* Back up undo mode to a local variable */
1466 nUndoMode
= editor
->nUndoMode
;
1468 /* Only create an undo if SFF_SELECTION is set */
1469 if (!(format
& SFF_SELECTION
))
1470 editor
->nUndoMode
= umIgnore
;
1472 inStream
.editstream
= stream
;
1473 inStream
.editstream
->dwError
= 0;
1474 inStream
.dwSize
= 0;
1475 inStream
.dwUsed
= 0;
1477 if (format
& SF_RTF
)
1479 /* Check if it's really RTF, and if it is not, use plain text */
1480 ME_StreamInFill(&inStream
);
1481 if (!inStream
.editstream
->dwError
)
1483 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1484 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1487 inStream
.editstream
->dwError
= -16;
1492 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1494 if (format
& SF_RTF
) {
1495 from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1497 /* setup the RTF parser */
1498 memset(&parser
, 0, sizeof parser
);
1499 RTFSetEditStream(&parser
, &inStream
);
1500 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1501 parser
.editor
= editor
;
1502 parser
.style
= style
;
1503 WriterInit(&parser
);
1505 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1506 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1507 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1508 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1510 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1511 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1515 /* do the parsing */
1517 RTFFlushOutputBuffer(&parser
);
1518 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1519 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1520 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1522 /* Delete any incomplete table row at the end of the rich text. */
1524 ME_DisplayItem
*para
;
1526 parser
.rtfMinor
= rtfRow
;
1527 /* Complete the table row before deleting it.
1528 * By doing it this way we will have the current paragraph format set
1529 * properly to reflect that is not in the complete table, and undo items
1530 * will be added for this change to the current paragraph format. */
1531 if (parser
.nestingLevel
> 0)
1533 while (parser
.nestingLevel
> 1)
1534 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1535 para
= parser
.tableDef
->tableRowStart
;
1536 ME_RTFSpecialCharHook(&parser
);
1538 para
= parser
.tableDef
->tableRowStart
;
1539 ME_RTFSpecialCharHook(&parser
);
1540 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1541 para
= para
->member
.para
.next_para
;
1544 editor
->pCursors
[1].pPara
= para
;
1545 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1546 editor
->pCursors
[1].nOffset
= 0;
1547 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1548 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1549 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1550 if (parser
.tableDef
)
1551 parser
.tableDef
->tableRowStart
= NULL
;
1554 ME_CheckTablesForCorruption(editor
);
1555 RTFDestroy(&parser
);
1556 if (parser
.lpRichEditOle
)
1557 IRichEditOle_Release(parser
.lpRichEditOle
);
1559 if (parser
.stackTop
> 0)
1561 while (--parser
.stackTop
>= 0)
1563 ME_ReleaseStyle(parser
.style
);
1564 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1566 if (!inStream
.editstream
->dwError
)
1567 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1570 /* Remove last line break, as mandated by tests. This is not affected by
1571 CR/LF counters, since RTF streaming presents only \para tokens, which
1572 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1576 ME_GetSelection(editor
, &selStart
, &selEnd
);
1577 newto
= ME_GetCursorOfs(selEnd
);
1578 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1579 WCHAR lastchar
[3] = {'\0', '\0'};
1580 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1581 ME_Cursor linebreakCursor
= *selEnd
;
1583 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1584 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, 0);
1585 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1586 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1590 to
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1591 num_read
= to
- from
;
1593 style
= parser
.style
;
1595 else if (format
& SF_TEXT
)
1596 num_read
= ME_StreamInText(editor
, format
, &inStream
, style
);
1598 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1599 /* put the cursor at the top */
1600 if (!(format
& SFF_SELECTION
))
1601 ME_SetSelection(editor
, 0, 0);
1604 /* Restore saved undo mode */
1605 editor
->nUndoMode
= nUndoMode
;
1607 /* even if we didn't add an undo, we need to commit anything on the stack */
1608 ME_CommitUndo(editor
);
1610 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1611 if (!(format
& SFF_SELECTION
))
1612 ME_EmptyUndoStack(editor
);
1614 ME_ReleaseStyle(style
);
1615 editor
->nEventMask
= nEventMask
;
1616 ME_UpdateRepaint(editor
, FALSE
);
1617 if (!(format
& SFF_SELECTION
)) {
1618 ME_ClearTempStyle(editor
);
1620 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1621 ME_MoveCaret(editor
);
1622 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1623 ME_SendSelChange(editor
);
1624 ME_SendRequestResize(editor
, FALSE
);
1630 typedef struct tagME_RTFStringStreamStruct
1635 } ME_RTFStringStreamStruct
;
1637 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1639 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1642 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1643 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1644 pStruct
->pos
+= count
;
1650 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1653 ME_RTFStringStreamStruct data
;
1655 data
.string
= string
;
1656 data
.length
= strlen(string
);
1658 es
.dwCookie
= (DWORD_PTR
)&data
;
1659 es
.pfnCallback
= ME_ReadFromRTFString
;
1660 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1665 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1667 const int nLen
= lstrlenW(text
);
1668 const int nTextLen
= ME_GetTextLength(editor
);
1671 WCHAR wLastChar
= ' ';
1673 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1674 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1676 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1677 FIXME("Flags 0x%08x not implemented\n",
1678 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1681 if (chrg
->cpMax
== -1)
1684 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1686 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1687 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1692 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1693 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1697 chrgText
->cpMin
= -1;
1698 chrgText
->cpMax
= -1;
1703 /* when searching up, if cpMin < cpMax, then instead of searching
1704 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1705 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1706 * case, it is always bigger than cpMin.
1708 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1712 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1713 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1719 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1722 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1726 if (flags
& FR_DOWN
) /* Forward search */
1728 /* If possible, find the character before where the search starts */
1729 if ((flags
& FR_WHOLEWORD
) && nMin
)
1731 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1732 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1733 ME_MoveCursorChars(editor
, &cursor
, 1);
1735 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1738 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1740 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1741 int nCurStart
= cursor
.nOffset
;
1744 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
), text
[nMatched
], (flags
& FR_MATCHCASE
)))
1746 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1750 if (nMatched
== nLen
)
1752 ME_DisplayItem
*pNextItem
= pCurItem
;
1753 int nNextStart
= nCurStart
;
1756 /* Check to see if next character is a whitespace */
1757 if (flags
& FR_WHOLEWORD
)
1759 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1761 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1762 nNextStart
= -nMatched
;
1766 wNextChar
= *get_text( &pNextItem
->member
.run
, nNextStart
+ nMatched
);
1770 if (isalnumW(wNextChar
))
1774 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1777 chrgText
->cpMin
= cursor
.nOffset
;
1778 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1780 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1781 return cursor
.nOffset
;
1783 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1785 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1786 nCurStart
= -nMatched
;
1790 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
);
1795 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.len
)
1797 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1802 else /* Backward search */
1804 /* If possible, find the character after where the search ends */
1805 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1807 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1808 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1809 ME_MoveCursorChars(editor
, &cursor
, -1);
1811 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1814 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1816 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1817 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1818 int nCurEnd
= cursor
.nOffset
;
1823 ME_PrevRun(&pCurPara
, &pCurItem
);
1824 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1827 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 ),
1828 text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
) ))
1830 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1834 if (nMatched
== nLen
)
1836 ME_DisplayItem
*pPrevItem
= pCurItem
;
1837 int nPrevEnd
= nCurEnd
;
1841 /* Check to see if previous character is a whitespace */
1842 if (flags
& FR_WHOLEWORD
)
1844 if (nPrevEnd
- nMatched
== 0)
1846 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1848 nPrevEnd
= pPrevItem
->member
.run
.len
+ nMatched
;
1852 wPrevChar
= *get_text( &pPrevItem
->member
.run
, nPrevEnd
- nMatched
- 1 );
1856 if (isalnumW(wPrevChar
))
1860 nStart
= pCurPara
->member
.para
.nCharOfs
1861 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1864 chrgText
->cpMin
= nStart
;
1865 chrgText
->cpMax
= nStart
+ nLen
;
1867 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1870 if (nCurEnd
- nMatched
== 0)
1872 ME_PrevRun(&pCurPara
, &pCurItem
);
1873 /* Don't care about pCurItem becoming NULL here; it's already taken
1874 * care of in the exterior loop condition */
1875 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1879 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 );
1884 if (cursor
.nOffset
< 0)
1886 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1887 cursor
.nOffset
= cursor
.pRun
->member
.run
.len
;
1891 TRACE("not found\n");
1893 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1897 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1902 if (!ex
->cb
|| !pText
) return 0;
1904 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1905 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1907 if (ex
->flags
& GT_SELECTION
)
1910 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
1911 start
= editor
->pCursors
[nStartCur
];
1916 ME_SetCursorToStart(editor
, &start
);
1919 if (ex
->codepage
== 1200)
1921 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
1922 &start
, nChars
, ex
->flags
& GT_USECRLF
);
1926 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1927 we can just take a bigger buffer? :)
1928 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1929 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1931 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1936 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
1937 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
1939 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
);
1940 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
1941 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1942 if (rc
) rc
--; /* do not count 0 terminator */
1949 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
1950 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
1952 if (!strText
) return 0;
1954 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, 0);
1957 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
1959 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, 0);
1960 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
1961 nLen
+1, NULL
, NULL
);
1967 typedef struct tagME_GlobalDestStruct
1971 } ME_GlobalDestStruct
;
1973 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1975 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1980 pDest
= (WORD
*)lpBuff
;
1981 pSrc
= GlobalLock(pData
->hData
);
1982 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1983 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1985 pData
->nLength
+= i
;
1987 GlobalUnlock(pData
->hData
);
1991 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1993 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1998 pSrc
= GlobalLock(pData
->hData
);
1999 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2000 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2002 pData
->nLength
+= i
;
2004 GlobalUnlock(pData
->hData
);
2008 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2012 ME_GlobalDestStruct gds
;
2013 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2016 if (IsClipboardFormatAvailable(nRTFFormat
))
2017 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2018 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2019 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2023 if (!OpenClipboard(editor
->hWnd
))
2025 gds
.hData
= GetClipboardData(cf
);
2027 es
.dwCookie
= (DWORD_PTR
)&gds
;
2028 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2029 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2035 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2037 LPDATAOBJECT dataObj
= NULL
;
2040 if (editor
->cPasswordMask
)
2041 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2043 if(editor
->lpOleCallback
)
2046 range
.cpMin
= ME_GetCursorOfs(start
);
2047 range
.cpMax
= range
.cpMin
+ nChars
;
2048 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2050 if(FAILED(hr
) || !dataObj
)
2051 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2053 hr
= OleSetClipboard(dataObj
);
2054 IDataObject_Release(dataObj
);
2056 return SUCCEEDED(hr
) != 0;
2059 /* helper to send a msg filter notification */
2061 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2065 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2066 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2067 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2068 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2070 msgf
.wParam
= *wParam
;
2071 msgf
.lParam
= *lParam
;
2072 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2074 *wParam
= msgf
.wParam
;
2075 *lParam
= msgf
.lParam
;
2076 msgf
.wParam
= *wParam
;
2081 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2083 ME_DisplayItem
*startPara
, *endPara
;
2084 ME_DisplayItem
*prev_para
;
2085 ME_Cursor
*from
, *to
;
2089 if (!editor
->AutoURLDetect_bEnable
) return;
2091 ME_GetSelection(editor
, &from
, &to
);
2093 /* Find paragraph previous to the one that contains start cursor */
2094 startPara
= from
->pPara
;
2095 prev_para
= startPara
->member
.para
.prev_para
;
2096 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2098 /* Find paragraph that contains end cursor */
2099 endPara
= to
->pPara
->member
.para
.next_para
;
2101 start
.pPara
= startPara
;
2102 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2104 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2106 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2110 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2112 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2113 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2115 if (editor
->bMouseCaptured
)
2117 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2118 editor
->nSelectionType
= stPosition
;
2126 editor
->nUDArrowX
= -1;
2132 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2133 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2137 editor
->nUDArrowX
= -1;
2138 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2139 if (editor
->styleFlags
& ES_READONLY
)
2141 if (ME_IsSelection(editor
))
2143 ME_DeleteSelection(editor
);
2144 ME_CommitUndo(editor
);
2146 else if (nKey
== VK_DELETE
)
2148 /* Delete stops group typing.
2149 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2150 ME_DeleteTextAtCursor(editor
, 1, 1);
2151 ME_CommitUndo(editor
);
2153 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2155 BOOL bDeletionSucceeded
;
2156 /* Backspace can be grouped for a single undo */
2157 ME_ContinueCoalescingTransaction(editor
);
2158 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2159 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2160 /* Deletion was prevented so the cursor is moved back to where it was.
2161 * (e.g. this happens when trying to delete cell boundaries)
2163 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2165 ME_CommitCoalescingUndo(editor
);
2169 ME_MoveCursorFromTableRowStartParagraph(editor
);
2170 ME_UpdateSelectionLinkAttribute(editor
);
2171 ME_UpdateRepaint(editor
, FALSE
);
2172 ME_SendRequestResize(editor
, FALSE
);
2175 if (editor
->bDialogMode
)
2180 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2182 if (editor
->hwndParent
)
2185 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2186 if (HIWORD(dw
) == DC_HASDEFID
)
2188 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2191 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
2192 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2200 if (editor
->styleFlags
& ES_MULTILINE
)
2202 ME_Cursor cursor
= editor
->pCursors
[0];
2203 ME_DisplayItem
*para
= cursor
.pPara
;
2205 const WCHAR endl
= '\r';
2206 const WCHAR endlv10
[] = {'\r','\n'};
2209 if (editor
->styleFlags
& ES_READONLY
) {
2210 MessageBeep(MB_ICONERROR
);
2214 ME_GetSelectionOfs(editor
, &from
, &to
);
2215 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2217 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2218 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2219 /* Add a new table row after this row. */
2220 para
= ME_AppendTableRow(editor
, para
);
2221 para
= para
->member
.para
.next_para
;
2222 editor
->pCursors
[0].pPara
= para
;
2223 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2224 editor
->pCursors
[0].nOffset
= 0;
2225 editor
->pCursors
[1] = editor
->pCursors
[0];
2226 ME_CommitUndo(editor
);
2227 ME_CheckTablesForCorruption(editor
);
2228 ME_UpdateRepaint(editor
, FALSE
);
2231 else if (para
== editor
->pCursors
[1].pPara
&&
2232 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2233 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2234 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2236 /* Insert a newline before the table. */
2237 para
= para
->member
.para
.prev_para
;
2238 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2239 editor
->pCursors
[0].pPara
= para
;
2240 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2241 editor
->pCursors
[1] = editor
->pCursors
[0];
2242 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2243 editor
->pCursors
[0].pRun
->member
.run
.style
);
2244 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2245 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2246 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2247 editor
->pCursors
[0].pPara
= para
;
2248 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2249 editor
->pCursors
[1] = editor
->pCursors
[0];
2250 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2251 ME_CommitCoalescingUndo(editor
);
2252 ME_CheckTablesForCorruption(editor
);
2253 ME_UpdateRepaint(editor
, FALSE
);
2256 } else { /* v1.0 - 3.0 */
2257 ME_DisplayItem
*para
= cursor
.pPara
;
2258 if (ME_IsInTable(para
))
2260 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2263 ME_ContinueCoalescingTransaction(editor
);
2264 para
= ME_AppendTableRow(editor
, para
);
2265 editor
->pCursors
[0].pPara
= para
;
2266 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2267 editor
->pCursors
[0].nOffset
= 0;
2268 editor
->pCursors
[1] = editor
->pCursors
[0];
2269 ME_CommitCoalescingUndo(editor
);
2270 ME_UpdateRepaint(editor
, FALSE
);
2274 ME_ContinueCoalescingTransaction(editor
);
2275 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2276 !ME_IsInTable(para
->member
.para
.prev_para
))
2278 /* Insert newline before table */
2279 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2281 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2282 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2284 editor
->pCursors
[0].nOffset
= 0;
2285 editor
->pCursors
[1] = editor
->pCursors
[0];
2286 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2287 editor
->pCursors
[0].pRun
->member
.run
.style
);
2289 editor
->pCursors
[1] = editor
->pCursors
[0];
2290 para
= ME_AppendTableRow(editor
, para
);
2291 editor
->pCursors
[0].pPara
= para
;
2292 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2293 editor
->pCursors
[0].nOffset
= 0;
2294 editor
->pCursors
[1] = editor
->pCursors
[0];
2296 ME_CommitCoalescingUndo(editor
);
2297 ME_UpdateRepaint(editor
, FALSE
);
2303 style
= ME_GetInsertStyle(editor
, 0);
2304 ME_SaveTempStyle(editor
);
2305 ME_ContinueCoalescingTransaction(editor
);
2307 ME_InsertEndRowFromCursor(editor
, 0);
2309 if (!editor
->bEmulateVersion10
)
2310 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2312 ME_InsertTextFromCursor(editor
, 0, endlv10
, 2, style
);
2313 ME_ReleaseStyle(style
);
2314 ME_CommitCoalescingUndo(editor
);
2317 ME_UpdateSelectionLinkAttribute(editor
);
2318 ME_UpdateRepaint(editor
, FALSE
);
2324 if (editor
->bDialogMode
&& editor
->hwndParent
)
2325 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2328 if (editor
->bDialogMode
&& editor
->hwndParent
)
2329 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2334 ME_SetSelection(editor
, 0, -1);
2340 return ME_Paste(editor
);
2348 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2349 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2352 result
= ME_Copy(editor
, selStart
, nChars
);
2353 if (result
&& nKey
== 'X')
2355 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2356 ME_CommitUndo(editor
);
2357 ME_UpdateRepaint(editor
, TRUE
);
2378 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2379 editor
->nUDArrowX
= -1;
2386 chf
.cbSize
= sizeof(chf
);
2388 ME_GetSelectionCharFormat(editor
, &chf
);
2389 ME_DumpStyleToBuf(&chf
, buf
);
2390 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2394 ME_CheckCharOffsets(editor
);
2401 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2402 LPARAM flags
, BOOL unicode
)
2406 if (editor
->bMouseCaptured
)
2410 wstr
= (WCHAR
)charCode
;
2413 CHAR charA
= charCode
;
2414 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2417 if (editor
->styleFlags
& ES_READONLY
) {
2418 MessageBeep(MB_ICONERROR
);
2419 return 0; /* FIXME really 0 ? */
2422 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2424 ME_Cursor cursor
= editor
->pCursors
[0];
2425 ME_DisplayItem
*para
= cursor
.pPara
;
2427 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2428 ME_GetSelectionOfs(editor
, &from
, &to
);
2430 /* v4.1 allows tabs to be inserted with ctrl key down */
2431 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2433 ME_DisplayItem
*para
;
2434 BOOL bSelectedRow
= FALSE
;
2436 para
= cursor
.pPara
;
2437 if (ME_IsSelection(editor
) &&
2438 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2439 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2440 para
->member
.para
.prev_para
->type
== diParagraph
)
2442 para
= para
->member
.para
.prev_para
;
2443 bSelectedRow
= TRUE
;
2445 if (ME_IsInTable(para
))
2447 ME_TabPressedInTable(editor
, bSelectedRow
);
2448 ME_CommitUndo(editor
);
2451 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2452 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2454 para
= para
->member
.para
.next_para
;
2455 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2456 para
= para
->member
.para
.next_para
;
2457 editor
->pCursors
[0].pPara
= para
;
2458 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2459 editor
->pCursors
[0].nOffset
= 0;
2460 editor
->pCursors
[1] = editor
->pCursors
[0];
2463 } else { /* v1.0 - 3.0 */
2464 if (ME_IsInTable(cursor
.pRun
) &&
2465 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2468 /* Text should not be inserted at the end of the table. */
2473 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2474 /* WM_CHAR is restricted to nTextLimit */
2475 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2477 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2478 ME_SaveTempStyle(editor
);
2479 ME_ContinueCoalescingTransaction(editor
);
2480 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2481 ME_ReleaseStyle(style
);
2482 ME_CommitCoalescingUndo(editor
);
2483 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2486 ME_UpdateSelectionLinkAttribute(editor
);
2487 ME_UpdateRepaint(editor
, FALSE
);
2492 /* Process the message and calculate the new click count.
2494 * returns: The click count if it is mouse down event, else returns 0. */
2495 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2498 static int clickNum
= 0;
2499 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2502 if ((msg
== WM_LBUTTONDBLCLK
) ||
2503 (msg
== WM_RBUTTONDBLCLK
) ||
2504 (msg
== WM_MBUTTONDBLCLK
) ||
2505 (msg
== WM_XBUTTONDBLCLK
))
2507 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2510 if ((msg
== WM_LBUTTONDOWN
) ||
2511 (msg
== WM_RBUTTONDOWN
) ||
2512 (msg
== WM_MBUTTONDOWN
) ||
2513 (msg
== WM_XBUTTONDOWN
))
2515 static MSG prevClickMsg
;
2517 /* Compare the editor instead of the hwnd so that the this
2518 * can still be done for windowless richedit controls. */
2519 clickMsg
.hwnd
= (HWND
)editor
;
2520 clickMsg
.message
= msg
;
2521 clickMsg
.wParam
= wParam
;
2522 clickMsg
.lParam
= lParam
;
2523 clickMsg
.time
= GetMessageTime();
2524 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2525 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2526 if ((clickNum
!= 0) &&
2527 (clickMsg
.message
== prevClickMsg
.message
) &&
2528 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2529 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2530 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2531 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2532 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2538 prevClickMsg
= clickMsg
;
2545 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2551 DWORD messagePos
= GetMessagePos();
2552 pt
.x
= (short)LOWORD(messagePos
);
2553 pt
.y
= (short)HIWORD(messagePos
);
2557 sbi
.cbSize
= sizeof(sbi
);
2558 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2559 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2560 PtInRect(&sbi
.rcScrollBar
, pt
))
2562 ITextHost_TxSetCursor(editor
->texthost
,
2563 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2566 sbi
.cbSize
= sizeof(sbi
);
2567 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2568 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2569 PtInRect(&sbi
.rcScrollBar
, pt
))
2571 ITextHost_TxSetCursor(editor
->texthost
,
2572 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2576 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2578 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2579 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2582 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2583 pt
.y
< editor
->rcFormat
.top
&&
2584 pt
.x
< editor
->rcFormat
.left
)
2586 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2589 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2591 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2592 ITextHost_TxSetCursor(editor
->texthost
,
2593 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2595 ITextHost_TxSetCursor(editor
->texthost
,
2596 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2599 if (pt
.x
< editor
->rcFormat
.left
)
2601 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2604 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2609 run
= &cursor
.pRun
->member
.run
;
2610 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2611 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2613 ITextHost_TxSetCursor(editor
->texthost
,
2614 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2619 if (ME_IsSelection(editor
))
2621 int selStart
, selEnd
;
2622 int offset
= ME_GetCursorOfs(&cursor
);
2624 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2625 if (selStart
<= offset
&& selEnd
>= offset
) {
2626 ITextHost_TxSetCursor(editor
->texthost
,
2627 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2633 ITextHost_TxSetCursor(editor
->texthost
,
2634 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2638 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2640 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2641 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2642 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2643 editor
->rcFormat
.right
-= 1;
2646 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2651 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2653 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2654 if(selrange
.cpMin
== selrange
.cpMax
)
2655 seltype
|= SEL_EMPTY
;
2658 /* FIXME: Handle objects */
2659 seltype
|= SEL_TEXT
;
2660 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2661 seltype
|= SEL_MULTICHAR
;
2663 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2665 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2671 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2673 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2679 ed
->hwndParent
= NULL
;
2680 ed
->sizeWindow
.cx
= ed
->sizeWindow
.cy
= 0;
2681 ed
->texthost
= texthost
;
2682 ed
->bEmulateVersion10
= bEmulateVersion10
;
2684 ITextHost_TxGetPropertyBits(texthost
,
2685 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2686 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2687 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2688 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2689 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2691 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2692 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2693 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2694 ed
->pBuffer
= ME_MakeText();
2695 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2696 ed
->nAvailWidth
= 0; /* wrap to client area */
2697 ME_MakeFirstParagraph(ed
);
2698 /* The four cursors are for:
2699 * 0 - The position where the caret is shown
2700 * 1 - The anchored end of the selection (for normal selection)
2701 * 2 & 3 - The anchored start and end respectively for word, line,
2702 * or paragraph selection.
2705 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2706 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2707 ed
->pCursors
[1] = ed
->pCursors
[0];
2708 ed
->pCursors
[2] = ed
->pCursors
[0];
2709 ed
->pCursors
[3] = ed
->pCursors
[1];
2710 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2711 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2713 ed
->rgbBackColor
= -1;
2714 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2715 ed
->bCaretAtEnd
= FALSE
;
2717 ed
->nModifyStep
= 0;
2718 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2719 list_init( &ed
->undo_stack
);
2720 list_init( &ed
->redo_stack
);
2721 ed
->nUndoStackSize
= 0;
2722 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2723 ed
->nUndoMode
= umAddToUndo
;
2724 ed
->nParagraphs
= 1;
2725 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2726 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2727 ed
->bHideSelection
= FALSE
;
2728 ed
->pfnWordBreak
= NULL
;
2729 ed
->lpOleCallback
= NULL
;
2730 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2731 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2732 ed
->AutoURLDetect_bEnable
= FALSE
;
2733 ed
->bHaveFocus
= FALSE
;
2734 ed
->bDialogMode
= FALSE
;
2735 ed
->bMouseCaptured
= FALSE
;
2736 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2738 ed
->pFontCache
[i
].nRefs
= 0;
2739 ed
->pFontCache
[i
].nAge
= 0;
2740 ed
->pFontCache
[i
].hFont
= NULL
;
2743 ME_CheckCharOffsets(ed
);
2744 ed
->bDefaultFormatRect
= TRUE
;
2745 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2747 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2748 ed
->selofs
= SELECTIONBAR_WIDTH
;
2749 ed
->styleFlags
|= ES_SELECTIONBAR
;
2753 ed
->nSelectionType
= stPosition
;
2755 ed
->cPasswordMask
= 0;
2756 if (props
& TXTBIT_USEPASSWORD
)
2757 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2759 if (props
& TXTBIT_AUTOWORDSEL
)
2760 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2761 if (props
& TXTBIT_MULTILINE
) {
2762 ed
->styleFlags
|= ES_MULTILINE
;
2763 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2765 ed
->bWordWrap
= FALSE
;
2767 if (props
& TXTBIT_READONLY
)
2768 ed
->styleFlags
|= ES_READONLY
;
2769 if (!(props
& TXTBIT_HIDESELECTION
))
2770 ed
->styleFlags
|= ES_NOHIDESEL
;
2771 if (props
& TXTBIT_SAVESELECTION
)
2772 ed
->styleFlags
|= ES_SAVESEL
;
2773 if (props
& TXTBIT_VERTICAL
)
2774 ed
->styleFlags
|= ES_VERTICAL
;
2775 if (props
& TXTBIT_DISABLEDRAG
)
2776 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2778 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2780 /* Default scrollbar information */
2781 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2782 ed
->vert_si
.nMin
= 0;
2783 ed
->vert_si
.nMax
= 0;
2784 ed
->vert_si
.nPage
= 0;
2785 ed
->vert_si
.nPos
= 0;
2787 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2788 ed
->horz_si
.nMin
= 0;
2789 ed
->horz_si
.nMax
= 0;
2790 ed
->horz_si
.nPage
= 0;
2791 ed
->horz_si
.nPos
= 0;
2793 OleInitialize(NULL
);
2798 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2800 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2801 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2804 ME_ClearTempStyle(editor
);
2805 ME_EmptyUndoStack(editor
);
2808 ME_DestroyDisplayItem(p
);
2811 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2812 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2814 if (editor
->pFontCache
[i
].hFont
)
2815 DeleteObject(editor
->pFontCache
[i
].hFont
);
2817 if (editor
->rgbBackColor
!= -1)
2818 DeleteObject(editor
->hbrBackground
);
2819 if(editor
->lpOleCallback
)
2820 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2821 ITextHost_Release(editor
->texthost
);
2824 FREE_OBJ(editor
->pBuffer
);
2825 FREE_OBJ(editor
->pCursors
);
2830 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2835 case DLL_PROCESS_ATTACH
:
2836 DisableThreadLibraryCalls(hinstDLL
);
2837 me_heap
= HeapCreate (0, 0x10000, 0);
2838 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2839 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2843 case DLL_PROCESS_DETACH
:
2844 if (lpvReserved
) break;
2845 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2846 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2847 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2848 UnregisterClassA("RichEdit50A", 0);
2849 if (ME_ListBoxRegistered
)
2850 UnregisterClassW(REListBox20W
, 0);
2851 if (ME_ComboBoxRegistered
)
2852 UnregisterClassW(REComboBox20W
, 0);
2854 HeapDestroy (me_heap
);
2861 static const char * const edit_messages
[] = {
2890 "EM_SETPASSWORDCHAR",
2891 "EM_EMPTYUNDOBUFFER",
2892 "EM_GETFIRSTVISIBLELINE",
2894 "EM_SETWORDBREAKPROC",
2895 "EM_GETWORDBREAKPROC",
2896 "EM_GETPASSWORDCHAR",
2906 static const char * const richedit_messages
[] = {
2911 "EM_EXLINEFROMCHAR",
2917 "EM_GETOLEINTERFACE",
2927 "EM_SETOLECALLBACK",
2929 "EM_SETTARGETDEVICE",
2937 "EM_GETWORDBREAKPROCEX",
2938 "EM_SETWORDBREAKPROCEX",
2940 "EM_UNKNOWN_USER_83",
2945 "EM_STOPGROUPTYPING",
2949 "EM_GETAUTOURLDETECT",
2952 "EM_GETTEXTLENGTHEX",
2955 "EM_UNKNOWN_USER_98",
2956 "EM_UNKNOWN_USER_99",
2957 "EM_SETPUNCTUATION",
2958 "EM_GETPUNCTUATION",
2959 "EM_SETWORDWRAPMODE",
2960 "EM_GETWORDWRAPMODE",
2966 "EM_UNKNOWN_USER_109",
2967 "EM_UNKNOWN_USER_110",
2968 "EM_UNKNOWN_USER_111",
2969 "EM_UNKNOWN_USER_112",
2970 "EM_UNKNOWN_USER_113",
2971 "EM_UNKNOWN_USER_114",
2972 "EM_UNKNOWN_USER_115",
2973 "EM_UNKNOWN_USER_116",
2974 "EM_UNKNOWN_USER_117",
2975 "EM_UNKNOWN_USER_118",
2976 "EM_UNKNOWN_USER_119",
2977 "EM_SETLANGOPTIONS",
2978 "EM_GETLANGOPTIONS",
2979 "EM_GETIMECOMPMODE",
2983 "EM_SETIMEMODEBIAS",
2988 get_msg_name(UINT msg
)
2990 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
2991 return edit_messages
[msg
- EM_GETSEL
];
2992 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
2993 return richedit_messages
[msg
- EM_CANPASTE
];
2997 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3001 ME_Cursor cursor
; /* The start of the clicked text. */
3004 x
= (short)LOWORD(lParam
);
3005 y
= (short)HIWORD(lParam
);
3006 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3007 if (!isExact
) return;
3009 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3010 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3011 { /* The clicked run has CFE_LINK set */
3012 info
.nmhdr
.hwndFrom
= NULL
;
3013 info
.nmhdr
.idFrom
= 0;
3014 info
.nmhdr
.code
= EN_LINK
;
3016 info
.wParam
= wParam
;
3017 info
.lParam
= lParam
;
3019 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3020 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ cursor
.pRun
->member
.run
.len
;
3021 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3025 #define UNSUPPORTED_MSG(e) \
3027 FIXME(#e ": stub\n"); \
3028 *phresult = S_FALSE; \
3031 /* Handle messages for windowless and windoweded richedit controls.
3033 * The LRESULT that is returned is a return value for window procs,
3034 * and the phresult parameter is the COM return code needed by the
3035 * text services interface. */
3036 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3037 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3043 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3044 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3045 UNSUPPORTED_MSG(EM_FMTLINES
)
3046 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3047 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3048 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3049 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3050 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3051 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3052 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3053 UNSUPPORTED_MSG(EM_GETREDONAME
)
3054 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3055 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3056 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3057 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3058 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3059 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3060 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3061 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
3062 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3063 UNSUPPORTED_MSG(EM_SETMARGINS
)
3064 UNSUPPORTED_MSG(EM_SETPALETTE
)
3065 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3066 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3067 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3069 /* Messages specific to Richedit controls */
3072 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3074 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3077 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
|DLGC_HASSETSEL
;
3079 editor
->bDialogMode
= TRUE
;
3080 if (editor
->styleFlags
& ES_MULTILINE
)
3081 code
|= DLGC_WANTMESSAGE
;
3084 case EM_EMPTYUNDOBUFFER
:
3085 ME_EmptyUndoStack(editor
);
3089 /* Note: wParam/lParam can be NULL */
3091 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3092 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3093 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3094 if ((*pfrom
|*pto
) & 0xFFFF0000)
3096 return MAKELONG(*pfrom
,*pto
);
3100 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3101 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3102 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3105 case EM_SETUNDOLIMIT
:
3107 if ((int)wParam
< 0)
3108 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3110 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3111 /* Setting a max stack size keeps wine from getting killed
3112 for hogging memory. Windows allocates all this memory at once, so
3113 no program would realistically set a value above our maximum. */
3114 return editor
->nUndoLimit
;
3117 return !list_empty( &editor
->undo_stack
);
3119 return !list_empty( &editor
->redo_stack
);
3120 case WM_UNDO
: /* FIXME: actually not the same */
3122 return ME_Undo(editor
);
3124 return ME_Redo(editor
);
3127 /* these flags are equivalent to the ES_* counterparts */
3128 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3129 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3130 DWORD settings
= editor
->styleFlags
& mask
;
3136 /* these flags are equivalent to ES_* counterparts, except for
3137 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3138 * but is still stored in editor->styleFlags. */
3139 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3140 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3141 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3142 DWORD settings
= mask
& editor
->styleFlags
;
3143 DWORD oldSettings
= settings
;
3144 DWORD changedSettings
;
3160 changedSettings
= oldSettings
^ settings
;
3162 if (changedSettings
) {
3163 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3165 if (changedSettings
& ECO_SELECTIONBAR
)
3167 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3168 if (settings
& ECO_SELECTIONBAR
) {
3169 assert(!editor
->selofs
);
3170 editor
->selofs
= SELECTIONBAR_WIDTH
;
3171 editor
->rcFormat
.left
+= editor
->selofs
;
3173 editor
->rcFormat
.left
-= editor
->selofs
;
3176 ME_RewrapRepaint(editor
);
3179 if (changedSettings
& settings
& ECO_VERTICAL
)
3180 FIXME("ECO_VERTICAL not implemented yet!\n");
3181 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3182 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3183 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3184 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3185 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3186 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3187 if (changedSettings
& settings
& ECO_WANTRETURN
)
3188 FIXME("ECO_WANTRETURN not implemented yet!\n");
3189 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3190 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3197 ME_InvalidateSelection(editor
);
3198 ME_SetSelection(editor
, wParam
, lParam
);
3199 ME_InvalidateSelection(editor
);
3200 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3201 ME_ShowCaret(editor
);
3202 ME_SendSelChange(editor
);
3205 case EM_SETSCROLLPOS
:
3207 POINT
*point
= (POINT
*)lParam
;
3208 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3211 case EM_AUTOURLDETECT
:
3213 if (wParam
==1 || wParam
==0)
3215 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3218 return E_INVALIDARG
;
3220 case EM_GETAUTOURLDETECT
:
3222 return editor
->AutoURLDetect_bEnable
;
3227 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3229 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
3231 ME_InvalidateSelection(editor
);
3232 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
3233 ME_InvalidateSelection(editor
);
3234 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3235 ME_ShowCaret(editor
);
3236 ME_SendSelChange(editor
);
3240 case EM_SHOWSCROLLBAR
:
3253 flags
= WS_HSCROLL
|WS_VSCROLL
;
3260 editor
->styleFlags
|= flags
;
3261 if (flags
& WS_HSCROLL
)
3262 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3263 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3264 if (flags
& WS_VSCROLL
)
3265 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3266 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3268 editor
->styleFlags
&= ~flags
;
3269 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3276 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3280 BOOL bRtf
, bUnicode
, bSelection
;
3281 int oldModify
= editor
->nModifyStep
;
3283 if (!pStruct
) return 0;
3285 /* If we detect ascii rtf at the start of the string,
3286 * we know it isn't unicode. */
3287 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3288 !strncmp((char *)lParam
, "{\\urtf", 6)));
3289 bUnicode
= !bRtf
&& pStruct
->codepage
== 1200;
3291 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3292 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3293 pStruct
->flags
, pStruct
->codepage
);
3295 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3297 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3298 style
= ME_GetSelectionInsertStyle(editor
);
3299 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3302 ME_SetCursorToStart(editor
, &start
);
3303 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3304 style
= editor
->pBuffer
->pDefaultStyle
;
3308 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3310 /* FIXME: The length returned doesn't include the rtf control
3311 * characters, only the actual text. */
3312 len
= lParam
? strlen((char *)lParam
) : 0;
3315 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3316 wszText
= lParam
? ME_ToUnicode(bUnicode
, (void *)lParam
) : NULL
;
3317 len
= wszText
? lstrlenW(wszText
) : 0;
3318 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3319 ME_EndToUnicode(bUnicode
, wszText
);
3323 ME_ReleaseStyle(style
);
3324 ME_UpdateSelectionLinkAttribute(editor
);
3328 ME_SetCursorToStart(editor
, &cursor
);
3329 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3331 ME_CommitUndo(editor
);
3332 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3334 editor
->nModifyStep
= oldModify
;
3335 ME_EmptyUndoStack(editor
);
3337 ME_UpdateRepaint(editor
, FALSE
);
3340 case EM_SETBKGNDCOLOR
:
3343 if (editor
->rgbBackColor
!= -1) {
3344 DeleteObject(editor
->hbrBackground
);
3345 lColor
= editor
->rgbBackColor
;
3347 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3351 editor
->rgbBackColor
= -1;
3352 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3356 editor
->rgbBackColor
= lParam
;
3357 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3359 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3360 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3364 return editor
->nModifyStep
== 0 ? 0 : -1;
3368 editor
->nModifyStep
= 1;
3370 editor
->nModifyStep
= 0;
3374 case EM_SETREADONLY
:
3377 editor
->styleFlags
|= ES_READONLY
;
3379 editor
->styleFlags
&= ~ES_READONLY
;
3382 case EM_SETEVENTMASK
:
3384 DWORD nOldMask
= editor
->nEventMask
;
3386 editor
->nEventMask
= lParam
;
3389 case EM_GETEVENTMASK
:
3390 return editor
->nEventMask
;
3391 case EM_SETCHARFORMAT
:
3393 CHARFORMAT2W buf
, *p
;
3394 BOOL bRepaint
= TRUE
;
3395 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3396 if (p
== NULL
) return 0;
3397 if (wParam
& SCF_ALL
) {
3398 if (editor
->mode
& TM_PLAINTEXT
) {
3399 ME_SetDefaultCharFormat(editor
, p
);
3402 ME_SetCursorToStart(editor
, &start
);
3403 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3404 editor
->nModifyStep
= 1;
3406 } else if (wParam
& SCF_SELECTION
) {
3407 if (editor
->mode
& TM_PLAINTEXT
)
3409 if (wParam
& SCF_WORD
) {
3410 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3413 bRepaint
= ME_IsSelection(editor
);
3414 ME_SetSelectionCharFormat(editor
, p
);
3415 if (bRepaint
) editor
->nModifyStep
= 1;
3416 } else { /* SCF_DEFAULT */
3417 ME_SetDefaultCharFormat(editor
, p
);
3419 ME_CommitUndo(editor
);
3422 ME_WrapMarkedParagraphs(editor
);
3423 ME_UpdateScrollBar(editor
);
3428 case EM_GETCHARFORMAT
:
3430 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3431 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3432 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3433 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3434 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3436 tmp
.cbSize
= sizeof(tmp
);
3438 ME_GetDefaultCharFormat(editor
, &tmp
);
3440 ME_GetSelectionCharFormat(editor
, &tmp
);
3441 ME_CopyToCFAny(dst
, &tmp
);
3444 case EM_SETPARAFORMAT
:
3446 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3447 ME_WrapMarkedParagraphs(editor
);
3448 ME_UpdateScrollBar(editor
);
3450 ME_CommitUndo(editor
);
3453 case EM_GETPARAFORMAT
:
3454 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3455 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3456 case EM_GETFIRSTVISIBLELINE
:
3458 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3459 int y
= editor
->vert_si
.nPos
;
3464 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3465 if (p
->type
== diTextEnd
)
3467 if (p
->type
== diParagraph
) {
3468 ypara
= p
->member
.para
.pt
.y
;
3471 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3472 yend
= ystart
+ p
->member
.row
.nHeight
;
3480 case EM_HIDESELECTION
:
3482 editor
->bHideSelection
= (wParam
!= 0);
3483 ME_InvalidateSelection(editor
);
3488 if (!(editor
->styleFlags
& ES_MULTILINE
))
3490 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
3496 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3497 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3498 ME_CommitUndo(editor
);
3499 ME_UpdateRepaint(editor
, TRUE
);
3504 int from
, to
, nStartCursor
;
3506 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
3507 size_t len
= wszText
? lstrlenW(wszText
) : 0;
3508 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3510 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3511 style
= ME_GetSelectionInsertStyle(editor
);
3512 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3513 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3514 ME_ReleaseStyle(style
);
3515 /* drop temporary style if line end */
3517 * FIXME question: does abc\n mean: put abc,
3518 * clear temp style, put \n? (would require a change)
3520 if (len
>0 && wszText
[len
-1] == '\n')
3521 ME_ClearTempStyle(editor
);
3522 ME_EndToUnicode(unicode
, wszText
);
3523 ME_CommitUndo(editor
);
3524 ME_UpdateSelectionLinkAttribute(editor
);
3526 ME_EmptyUndoStack(editor
);
3527 ME_UpdateRepaint(editor
, FALSE
);
3530 case EM_SCROLLCARET
:
3531 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3538 BOOL bRepaint
= LOWORD(lParam
);
3541 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3542 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3543 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3544 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3545 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3546 if (editor
->mode
& TM_RICHTEXT
) {
3548 ME_SetCursorToStart(editor
, &start
);
3549 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3551 ME_SetDefaultCharFormat(editor
, &fmt
);
3553 ME_CommitUndo(editor
);
3554 ME_MarkAllForWrapping(editor
);
3555 ME_WrapMarkedParagraphs(editor
);
3556 ME_UpdateScrollBar(editor
);
3564 ME_SetCursorToStart(editor
, &cursor
);
3565 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3568 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3569 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3570 !strncmp((char *)lParam
, "{\\urtf", 6))
3572 /* Undocumented: WM_SETTEXT supports RTF text */
3573 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3577 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
3578 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3579 if (lstrlenW(wszText
) > 0)
3583 /* uses default style! */
3584 if (!(editor
->styleFlags
& ES_MULTILINE
))
3589 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3592 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3594 ME_EndToUnicode(unicode
, wszText
);
3598 TRACE("WM_SETTEXT - NULL\n");
3599 ME_SetCursorToStart(editor
, &cursor
);
3600 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3601 ME_SetSelection(editor
, 0, 0);
3602 editor
->nModifyStep
= 0;
3603 ME_CommitUndo(editor
);
3604 ME_EmptyUndoStack(editor
);
3605 ME_UpdateRepaint(editor
, FALSE
);
3610 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3611 if (IsClipboardFormatAvailable(nRTFFormat
))
3613 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3618 case WM_MBUTTONDOWN
:
3624 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3625 int nChars
= nTo
- nFrom
;
3626 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3628 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3630 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3631 ME_CommitUndo(editor
);
3632 ME_UpdateRepaint(editor
, TRUE
);
3636 case WM_GETTEXTLENGTH
:
3638 GETTEXTLENGTHEX how
;
3640 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3641 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3642 how
.codepage
= unicode
? 1200 : CP_ACP
;
3643 return ME_GetTextLengthEx(editor
, &how
);
3645 case EM_GETTEXTLENGTHEX
:
3646 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3650 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3651 ex
.flags
= GT_USECRLF
;
3652 ex
.codepage
= unicode
? 1200 : CP_ACP
;
3653 ex
.lpDefaultChar
= NULL
;
3654 ex
.lpUsedDefChar
= NULL
;
3655 return ME_GetTextEx(editor
, &ex
, lParam
);
3658 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3661 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3662 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3663 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3664 nTo
- nFrom
, unicode
);
3666 case EM_GETSCROLLPOS
:
3668 POINT
*point
= (POINT
*)lParam
;
3669 point
->x
= editor
->horz_si
.nPos
;
3670 point
->y
= editor
->vert_si
.nPos
;
3671 /* 16-bit scaled value is returned as stored in scrollinfo */
3672 if (editor
->horz_si
.nMax
> 0xffff)
3673 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3674 if (editor
->vert_si
.nMax
> 0xffff)
3675 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3678 case EM_GETTEXTRANGE
:
3680 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3682 int nStart
= rng
->chrg
.cpMin
;
3683 int nEnd
= rng
->chrg
.cpMax
;
3684 int textlength
= ME_GetTextLength(editor
);
3686 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3687 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3688 if (nStart
< 0) return 0;
3689 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3691 if (nStart
>= nEnd
) return 0;
3693 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3694 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3698 ME_DisplayItem
*run
;
3699 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3700 unsigned int nCharsLeft
= nMaxChars
;
3701 char *dest
= (char *) lParam
;
3702 BOOL wroteNull
= FALSE
;
3704 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3705 unicode
? "Unicode" : "Ansi");
3707 run
= ME_FindRowWithNumber(editor
, wParam
);
3711 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3712 && run
->type
== diRun
)
3714 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3717 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3720 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3722 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
3723 nCharsLeft
, NULL
, NULL
);
3724 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3725 nCharsLeft
-= nCopy
;
3728 /* append line termination, space allowing */
3732 *((WCHAR
*)dest
) = '\0';
3739 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3740 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3742 case EM_GETLINECOUNT
:
3744 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3747 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3749 while (item
!= editor
->pBuffer
->pLast
)
3751 assert(item
->type
== diParagraph
);
3752 prev_para
= ME_FindItemBack(item
, diRun
);
3754 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3756 nRows
+= item
->member
.para
.nRows
;
3757 item
= item
->member
.para
.next_para
;
3759 last_para
= ME_FindItemBack(item
, diRun
);
3761 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3762 if (editor
->bEmulateVersion10
&& prev_para
&&
3763 last_para
->member
.run
.nCharOfs
== 0 &&
3764 prev_para
->member
.run
.len
== 1 &&
3765 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
3767 /* In 1.0 emulation, the last solitary \r at the very end of the text
3768 (if one exists) is NOT a line break.
3769 FIXME: this is an ugly hack. This should have a more regular model. */
3773 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3774 return max(1, nRows
);
3776 case EM_LINEFROMCHAR
:
3779 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3781 return ME_RowNumberFromCharOfs(editor
, wParam
);
3783 case EM_EXLINEFROMCHAR
:
3786 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3788 return ME_RowNumberFromCharOfs(editor
, lParam
);
3792 ME_DisplayItem
*item
, *para
;
3796 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3798 item
= ME_FindRowWithNumber(editor
, wParam
);
3801 para
= ME_GetParagraph(item
);
3802 item
= ME_FindItemFwd(item
, diRun
);
3803 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3804 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3809 ME_DisplayItem
*item
, *item_end
;
3810 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3811 ME_DisplayItem
*para
, *run
;
3813 if (wParam
> ME_GetTextLength(editor
))
3817 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3820 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3821 item
= ME_RowStart(run
);
3822 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3823 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3824 if (item_end
->type
== diStartRow
) {
3825 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3827 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3828 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3829 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
3831 nChars
= nNextLineOfs
- nThisLineOfs
;
3832 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3835 case EM_EXLIMITTEXT
:
3837 if ((int)lParam
< 0)
3840 editor
->nTextLimit
= 65536;
3842 editor
->nTextLimit
= (int) lParam
;
3848 editor
->nTextLimit
= 65536;
3850 editor
->nTextLimit
= (int) wParam
;
3853 case EM_GETLIMITTEXT
:
3855 return editor
->nTextLimit
;
3861 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3862 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3865 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3866 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3867 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3870 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3871 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3879 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3880 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3883 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3884 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3885 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3888 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3889 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3895 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3896 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3898 case EM_FINDTEXTEXW
:
3900 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3901 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3904 if (!wParam
|| !lParam
)
3906 *(int *)wParam
= editor
->nZoomNumerator
;
3907 *(int *)lParam
= editor
->nZoomDenominator
;
3910 return ME_SetZoom(editor
, wParam
, lParam
);
3911 case EM_CHARFROMPOS
:
3914 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
3916 return ME_GetCursorOfs(&cursor
);
3920 case EM_POSFROMCHAR
:
3922 ME_DisplayItem
*pPara
, *pRun
;
3923 int nCharOfs
, nOffset
, nLength
;
3927 /* detect which API version we're dealing with */
3928 if (wParam
>= 0x40000)
3930 nLength
= ME_GetTextLength(editor
);
3931 nCharOfs
= min(nCharOfs
, nLength
);
3932 nCharOfs
= max(nCharOfs
, 0);
3934 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
3935 assert(pRun
->type
== diRun
);
3936 pt
.y
= pRun
->member
.run
.pt
.y
;
3937 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
3938 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
3939 pt
.x
+= editor
->rcFormat
.left
;
3941 pt
.x
-= editor
->horz_si
.nPos
;
3942 pt
.y
-= editor
->vert_si
.nPos
;
3944 if (wParam
>= 0x40000) {
3945 *(POINTL
*)wParam
= pt
;
3947 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
3953 ME_SetDefaultFormatRect(editor
);
3955 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
3956 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
3957 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
3959 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
3960 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
3962 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3964 if (editor
->styleFlags
& WS_VSCROLL
)
3966 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
3967 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
3969 if (editor
->styleFlags
& WS_HSCROLL
)
3971 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
3972 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
3976 ME_CommitUndo(editor
);
3977 ME_WrapMarkedParagraphs(editor
);
3978 ME_MoveCaret(editor
);
3982 ME_DestroyEditor(editor
);
3986 return ME_SetCursor(editor
);
3988 case WM_LBUTTONDBLCLK
:
3989 case WM_LBUTTONDOWN
:
3991 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3992 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3993 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3995 ITextHost_TxSetFocus(editor
->texthost
);
3996 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
3997 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
3998 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
3999 editor
->bMouseCaptured
= TRUE
;
4000 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4001 if (!ME_SetCursor(editor
)) goto do_default
;
4005 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4006 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4008 if (editor
->bMouseCaptured
)
4009 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4010 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4011 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4012 if (editor
->bMouseCaptured
)
4013 ME_SetCursor(editor
);
4016 if (editor
->bMouseCaptured
) {
4017 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4018 editor
->bMouseCaptured
= FALSE
;
4020 if (editor
->nSelectionType
== stDocument
)
4021 editor
->nSelectionType
= stPosition
;
4022 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4023 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4027 ME_SetCursor(editor
);
4028 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4032 case WM_RBUTTONDOWN
:
4033 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4034 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4035 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4038 case WM_CONTEXTMENU
:
4039 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4043 editor
->bHaveFocus
= TRUE
;
4044 ME_ShowCaret(editor
);
4045 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4048 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4049 editor
->bHaveFocus
= FALSE
;
4050 ME_HideCaret(editor
);
4051 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4054 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4057 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4058 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4062 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4063 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4065 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4069 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4070 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4072 return ME_Char(editor
, wParam
, lParam
, unicode
);
4076 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4077 if(wParam
<= 0x000fffff)
4079 if(wParam
> 0xffff) /* convert to surrogates */
4082 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4083 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4085 ME_Char(editor
, wParam
, 0, TRUE
);
4091 case EM_STOPGROUPTYPING
:
4092 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4096 const int scrollUnit
= 7;
4098 switch(LOWORD(wParam
))
4101 ME_ScrollAbs(editor
, 0, 0);
4104 ME_ScrollAbs(editor
,
4105 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4106 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4109 ME_ScrollLeft(editor
, scrollUnit
);
4112 ME_ScrollRight(editor
, scrollUnit
);
4115 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4118 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4121 case SB_THUMBPOSITION
:
4123 int pos
= HIWORD(wParam
);
4124 if (editor
->horz_si
.nMax
> 0xffff)
4125 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4126 ME_HScrollAbs(editor
, pos
);
4132 case EM_SCROLL
: /* fall through */
4138 origNPos
= editor
->vert_si
.nPos
;
4141 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
4142 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
4143 if (lineHeight
<= 0) lineHeight
= 24;
4145 switch(LOWORD(wParam
))
4148 ME_ScrollAbs(editor
, 0, 0);
4151 ME_ScrollAbs(editor
,
4152 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4153 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4156 ME_ScrollUp(editor
,lineHeight
);
4159 ME_ScrollDown(editor
,lineHeight
);
4162 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4165 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4168 case SB_THUMBPOSITION
:
4170 int pos
= HIWORD(wParam
);
4171 if (editor
->vert_si
.nMax
> 0xffff)
4172 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4173 ME_VScrollAbs(editor
, pos
);
4177 if (msg
== EM_SCROLL
)
4178 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4184 UINT pulScrollLines
;
4187 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4188 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4191 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4193 gcWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4195 if (abs(gcWheelDelta
) >= WHEEL_DELTA
)
4199 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4203 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4205 numerator
= numerator
+ (gcWheelDelta
/ WHEEL_DELTA
) * 10;
4206 if (numerator
>= 10 && numerator
<= 500)
4207 ME_SetZoom(editor
, numerator
, 100);
4209 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
4210 /* FIXME follow the original */
4212 ME_ScrollDown(editor
,pulScrollLines
* (-gcWheelDelta
/ WHEEL_DELTA
) * 8);
4219 *((RECT
*)lParam
) = editor
->rcFormat
;
4220 if (editor
->bDefaultFormatRect
)
4221 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4231 RECT
*rc
= (RECT
*)lParam
;
4233 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4234 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4237 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4238 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4239 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4240 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4241 } else if (wParam
== 1) {
4242 /* MSDN incorrectly says a wParam value of 1 causes the
4243 * lParam rect to be used as a relative offset,
4244 * however, the tests show it just prevents min/max bound
4246 editor
->rcFormat
.top
= rc
->top
- border
;
4247 editor
->rcFormat
.left
= rc
->left
- border
;
4248 editor
->rcFormat
.bottom
= rc
->bottom
;
4249 editor
->rcFormat
.right
= rc
->right
+ border
;
4253 editor
->bDefaultFormatRect
= FALSE
;
4257 ME_SetDefaultFormatRect(editor
);
4258 editor
->bDefaultFormatRect
= TRUE
;
4260 ME_MarkAllForWrapping(editor
);
4261 ME_WrapMarkedParagraphs(editor
);
4262 ME_UpdateScrollBar(editor
);
4263 if (msg
!= EM_SETRECTNP
)
4267 case EM_REQUESTRESIZE
:
4268 ME_SendRequestResize(editor
, TRUE
);
4276 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4277 if (editor
->bDefaultFormatRect
) {
4278 ME_SetDefaultFormatRect(editor
);
4280 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4281 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4283 editor
->prevClientRect
= clientRect
;
4284 ME_RewrapRepaint(editor
);
4287 /* IME messages to make richedit controls IME aware */
4288 case WM_IME_SETCONTEXT
:
4289 case WM_IME_CONTROL
:
4291 case WM_IME_COMPOSITIONFULL
:
4293 case WM_IME_STARTCOMPOSITION
:
4295 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4296 ME_DeleteSelection(editor
);
4297 ME_CommitUndo(editor
);
4298 ME_UpdateRepaint(editor
, FALSE
);
4301 case WM_IME_COMPOSITION
:
4305 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4306 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4307 ME_DeleteSelection(editor
);
4308 ME_SaveTempStyle(editor
);
4309 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4311 LPWSTR lpCompStr
= NULL
;
4313 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4315 dwIndex
= GCS_COMPSTR
;
4317 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4318 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4319 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4320 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4321 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4322 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4324 if (dwIndex
== GCS_COMPSTR
)
4325 ME_SetSelection(editor
,editor
->imeStartIndex
,
4326 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4328 ME_ReleaseStyle(style
);
4329 ME_CommitUndo(editor
);
4330 ME_UpdateRepaint(editor
, FALSE
);
4333 case WM_IME_ENDCOMPOSITION
:
4335 ME_DeleteSelection(editor
);
4336 editor
->imeStartIndex
=-1;
4339 case EM_GETOLEINTERFACE
:
4341 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4342 return CreateIRichEditOle(editor
, ppvObj
);
4344 case EM_GETPASSWORDCHAR
:
4346 return editor
->cPasswordMask
;
4348 case EM_SETOLECALLBACK
:
4349 if(editor
->lpOleCallback
)
4350 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4351 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4352 if(editor
->lpOleCallback
)
4353 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4355 case EM_GETWORDBREAKPROC
:
4356 return (LRESULT
)editor
->pfnWordBreak
;
4357 case EM_SETWORDBREAKPROC
:
4359 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4361 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4362 return (LRESULT
)pfnOld
;
4364 case EM_GETTEXTMODE
:
4365 return editor
->mode
;
4366 case EM_SETTEXTMODE
:
4371 if (ME_GetTextLength(editor
) ||
4372 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4373 return E_UNEXPECTED
;
4375 /* Check for mutually exclusive flags in adjacent bits of wParam */
4376 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4377 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4378 return E_INVALIDARG
;
4380 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4382 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4383 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4384 if (wParam
& TM_PLAINTEXT
) {
4385 /* Clear selection since it should be possible to select the
4386 * end of text run for rich text */
4387 ME_InvalidateSelection(editor
);
4388 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4389 editor
->pCursors
[1] = editor
->pCursors
[0];
4390 /* plain text can only have the default style. */
4391 ME_ClearTempStyle(editor
);
4392 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4393 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4394 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4397 /* FIXME: Currently no support for undo level and code page options */
4398 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4401 case EM_SETPASSWORDCHAR
:
4403 editor
->cPasswordMask
= wParam
;
4404 ME_RewrapRepaint(editor
);
4407 case EM_SETTARGETDEVICE
:
4410 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4411 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4413 editor
->bWordWrap
= new;
4414 editor
->nAvailWidth
= 0; /* wrap to client area */
4415 ME_RewrapRepaint(editor
);
4418 int width
= max(0, lParam
);
4419 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4420 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4422 editor
->nAvailWidth
= width
;
4423 editor
->bWordWrap
= TRUE
;
4424 ME_RewrapRepaint(editor
);
4426 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4431 *phresult
= S_FALSE
;
4437 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4438 LPARAM lParam
, BOOL unicode
)
4440 ME_TextEditor
*editor
;
4442 LRESULT lresult
= 0;
4444 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4445 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4447 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4450 if (msg
== WM_NCCREATE
)
4452 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4453 ITextHost
*texthost
;
4455 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4456 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4457 return texthost
!= NULL
;
4461 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4473 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4474 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4475 ME_SendOldNotify(editor
, EN_UPDATE
);
4476 /* Erase area outside of the formatting rectangle */
4477 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4480 rc
.bottom
= editor
->rcFormat
.top
;
4481 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4482 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4484 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4486 rc
.top
= editor
->rcFormat
.bottom
;
4487 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4488 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4490 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4492 rc
.right
= editor
->rcFormat
.left
;
4493 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4494 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4496 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4498 rc
.left
= editor
->rcFormat
.right
;
4499 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4500 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4503 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4504 EndPaint(editor
->hWnd
, &ps
);
4509 HDC hDC
= (HDC
)wParam
;
4512 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4513 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4519 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4520 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4522 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4523 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4524 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4525 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4528 case EM_SETREADONLY
:
4531 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4532 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4533 dwStyle
&= ~ES_READONLY
;
4535 dwStyle
|= ES_READONLY
;
4536 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4540 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4543 if (hresult
== S_FALSE
)
4544 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4546 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4547 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4552 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4554 BOOL unicode
= TRUE
;
4556 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4557 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4560 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4563 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4565 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4568 /******************************************************************
4569 * RichEditANSIWndProc (RICHED20.10)
4571 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4573 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4576 /******************************************************************
4577 * RichEdit10ANSIWndProc (RICHED20.9)
4579 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4581 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4583 ITextHost
*texthost
;
4584 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4586 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4587 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4588 return texthost
!= NULL
;
4590 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4593 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4595 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4598 /* Fill buffer with srcChars unicode characters from the start cursor.
4600 * buffer: destination buffer
4601 * buflen: length of buffer in characters excluding the NULL terminator.
4602 * start: start of editor text to copy into buffer.
4603 * srcChars: Number of characters to use from the editor text.
4604 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4606 * returns the number of characters written excluding the NULL terminator.
4608 * The written text is always NULL terminated.
4610 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4611 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
)
4613 ME_DisplayItem
*pRun
, *pNextRun
;
4614 const WCHAR
*pStart
= buffer
;
4615 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4619 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4620 if (editor
->bEmulateVersion10
) bCRLF
= 0;
4624 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4626 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4627 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4629 /* No '\r' is appended to the last paragraph. */
4630 while (srcChars
&& buflen
&& pNextRun
)
4632 int nFlags
= pRun
->member
.run
.nFlags
;
4634 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4636 if (buflen
== 1) break;
4637 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4638 * EM_GETTEXTEX, however, this is done for copying text which
4639 * also uses this function. */
4640 srcChars
-= min(nLen
, srcChars
);
4644 nLen
= min(nLen
, srcChars
);
4648 nLen
= min(nLen
, buflen
);
4651 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4656 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4658 nLen
= pRun
->member
.run
.len
;
4659 str
= get_text( &pRun
->member
.run
, 0 );
4662 return buffer
- pStart
;
4665 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4670 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4671 wcW
.lpfnWndProc
= RichEditWndProcW
;
4673 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4674 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4676 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4677 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4678 wcW
.lpszMenuName
= NULL
;
4680 if (is_version_nt())
4682 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4683 if (!RegisterClassW(&wcW
)) return FALSE
;
4684 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4685 if (!RegisterClassW(&wcW
)) return FALSE
;
4689 /* WNDCLASSA/W have the same layout */
4690 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4691 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4692 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4693 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4696 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4697 wcA
.lpfnWndProc
= RichEditWndProcA
;
4699 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4700 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4702 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4703 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4704 wcA
.lpszMenuName
= NULL
;
4705 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4706 if (!RegisterClassA(&wcA
)) return FALSE
;
4707 wcA
.lpszClassName
= "RichEdit50A";
4708 if (!RegisterClassA(&wcA
)) return FALSE
;
4713 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4714 /* FIXME: Not implemented */
4715 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4716 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4717 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4720 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4721 /* FIXME: Not implemented */
4722 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4723 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4724 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4727 /******************************************************************
4728 * REExtendedRegisterClass (RICHED20.8)
4730 * FIXME undocumented
4731 * Need to check for errors and implement controls and callbacks
4733 LRESULT WINAPI
REExtendedRegisterClass(void)
4738 FIXME("semi stub\n");
4742 wcW
.hInstance
= NULL
;
4745 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4746 wcW
.lpszMenuName
= NULL
;
4748 if (!ME_ListBoxRegistered
)
4750 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4751 wcW
.lpfnWndProc
= REListWndProc
;
4752 wcW
.lpszClassName
= REListBox20W
;
4753 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4756 if (!ME_ComboBoxRegistered
)
4758 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4759 wcW
.lpfnWndProc
= REComboWndProc
;
4760 wcW
.lpszClassName
= REComboBox20W
;
4761 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4765 if (ME_ListBoxRegistered
)
4767 if (ME_ComboBoxRegistered
)
4773 static BOOL
isurlspecial(WCHAR c
)
4775 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4776 return strchrW( special_chars
, c
) != NULL
;
4780 * This proc takes a selection, and scans it forward in order to select the span
4781 * of a possible URL candidate. A possible URL candidate must start with isalnum
4782 * or one of the following special characters: *|/\+%#@ and must consist entirely
4783 * of the characters allowed to start the URL, plus : (colon) which may occur
4784 * at most once, and not at either end.
4786 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
4787 const ME_Cursor
*start
,
4789 ME_Cursor
*candidate_min
,
4790 ME_Cursor
*candidate_max
)
4792 ME_Cursor cursor
= *start
;
4793 BOOL foundColon
= FALSE
;
4794 BOOL candidateStarted
= FALSE
;
4795 WCHAR lastAcceptedChar
= '\0';
4799 WCHAR
*strStart
= get_text( &cursor
.pRun
->member
.run
, 0 );
4800 WCHAR
*str
= strStart
+ cursor
.nOffset
;
4801 int nLen
= cursor
.pRun
->member
.run
.len
- cursor
.nOffset
;
4804 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4806 /* Find start of candidate */
4807 if (!candidateStarted
)
4812 if (isalnumW(*str
) || isurlspecial(*str
))
4814 cursor
.nOffset
= str
- strStart
;
4815 *candidate_min
= cursor
;
4816 candidateStarted
= TRUE
;
4817 lastAcceptedChar
= *str
++;
4824 /* Find end of candidate */
4825 if (candidateStarted
) {
4829 if (*str
== ':' && !foundColon
) {
4831 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
4832 cursor
.nOffset
= str
- strStart
;
4833 if (lastAcceptedChar
== ':')
4834 ME_MoveCursorChars(editor
, &cursor
, -1);
4835 *candidate_max
= cursor
;
4838 lastAcceptedChar
= *str
++;
4842 /* End of paragraph: skip it if before candidate span, or terminates
4843 current active span */
4844 if (candidateStarted
) {
4845 if (lastAcceptedChar
== ':')
4846 ME_MoveCursorChars(editor
, &cursor
, -1);
4847 *candidate_max
= cursor
;
4852 /* Reaching this point means no span was found, so get next span */
4853 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
4854 if (candidateStarted
) {
4855 /* There are no further runs, so take end of text as end of candidate */
4856 cursor
.nOffset
= str
- strStart
;
4857 if (lastAcceptedChar
== ':')
4858 ME_MoveCursorChars(editor
, &cursor
, -1);
4859 *candidate_max
= cursor
;
4862 *candidate_max
= *candidate_min
= cursor
;
4868 if (candidateStarted
) {
4869 /* There are no further runs, so take end of text as end of candidate */
4870 if (lastAcceptedChar
== ':')
4871 ME_MoveCursorChars(editor
, &cursor
, -1);
4872 *candidate_max
= cursor
;
4875 *candidate_max
= *candidate_min
= cursor
;
4880 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4882 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
4884 #define MAX_PREFIX_LEN 9
4886 const WCHAR text
[MAX_PREFIX_LEN
];
4889 {{'p','r','o','s','p','e','r','o',':'}, 9},
4890 {{'t','e','l','n','e','t',':'}, 7},
4891 {{'g','o','p','h','e','r',':'}, 7},
4892 {{'m','a','i','l','t','o',':'}, 7},
4893 {{'h','t','t','p','s',':'}, 6},
4894 {{'f','i','l','e',':'}, 5},
4895 {{'n','e','w','s',':'}, 5},
4896 {{'w','a','i','s',':'}, 5},
4897 {{'n','n','t','p',':'}, 5},
4898 {{'h','t','t','p',':'}, 5},
4899 {{'w','w','w','.'}, 4},
4900 {{'f','t','p',':'}, 4},
4902 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
4905 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, 0);
4906 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
4908 if (nChars
< prefixes
[i
].length
) continue;
4909 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
4913 #undef MAX_PREFIX_LEN
4917 * This proc walks through the indicated selection and evaluates whether each
4918 * section identified by ME_FindNextURLCandidate and in-between sections have
4919 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4920 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4922 * Since this function can cause runs to be split, do not depend on the value
4923 * of the start cursor at the end of the function.
4925 * nChars may be set to INT_MAX to update to the end of the text.
4927 * Returns TRUE if at least one section was modified.
4929 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
4931 BOOL modified
= FALSE
;
4932 ME_Cursor startCur
= *start
;
4934 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
4939 ME_Cursor candidateStart
, candidateEnd
;
4941 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
4942 &candidateStart
, &candidateEnd
))
4944 /* Section before candidate is not an URL */
4945 int cMin
= ME_GetCursorOfs(&candidateStart
);
4946 int cMax
= ME_GetCursorOfs(&candidateEnd
);
4948 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
4949 candidateStart
= candidateEnd
;
4950 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
4954 /* No more candidates until end of selection */
4958 if (startCur
.pRun
!= candidateStart
.pRun
||
4959 startCur
.nOffset
!= candidateStart
.nOffset
)
4961 /* CFE_LINK effect should be consistently unset */
4962 link
.cbSize
= sizeof(link
);
4963 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
4964 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
4966 /* CFE_LINK must be unset from this range */
4967 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4968 link
.cbSize
= sizeof(link
);
4969 link
.dwMask
= CFM_LINK
;
4971 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
4972 /* Update candidateEnd since setting character formats may split
4973 * runs, which can cause a cursor to be at an invalid offset within
4975 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
4977 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
4978 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
4983 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
4984 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
4986 /* CFE_LINK effect should be consistently set */
4987 link
.cbSize
= sizeof(link
);
4988 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
4989 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
4991 /* CFE_LINK must be set on this range */
4992 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4993 link
.cbSize
= sizeof(link
);
4994 link
.dwMask
= CFM_LINK
;
4995 link
.dwEffects
= CFE_LINK
;
4996 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5000 startCur
= candidateEnd
;
5001 } while (nChars
> 0);