2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
54 - EM_GETLANGOPTIONS 2.0
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
67 + EM_GETSELTEXT (ANSI&Unicode)
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
94 - EM_SETBIDIOPTIONS 3.0
96 + EM_SETCHARFORMAT (partly done, no ANSI)
98 + EM_SETEVENTMASK (few notifications supported)
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
103 - EM_SETLANGOPTIONS 2.0
106 + EM_SETMODIFY (not sure if implementation is correct)
108 + EM_SETOPTIONS (partially implemented)
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
115 + EM_SETRECTNP (EM_SETRECT without repainting)
117 + EM_SETSCROLLPOS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
151 * EN_CHANGE (sent from the wrong place)
168 * EN_UPDATE (sent from the wrong place)
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
179 - ES_MULTILINE (currently single line controls aren't supported)
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
194 * RICHED20 TODO (incomplete):
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
214 * - ListBox & ComboBox not implemented
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
230 #include <undocuser.h>
235 #define STACK_SIZE_DEFAULT 100
236 #define STACK_SIZE_MAX 1000
238 #define TEXT_LIMIT_DEFAULT 32767
240 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
242 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
243 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
245 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
246 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 static HCURSOR hLeft
;
249 BOOL me_debug
= FALSE
;
250 HANDLE me_heap
= NULL
;
252 static BOOL ME_ListBoxRegistered
= FALSE
;
253 static BOOL ME_ComboBoxRegistered
= FALSE
;
255 static inline BOOL
is_version_nt(void)
257 return !(GetVersion() & 0x80000000);
260 static ME_TextBuffer
*ME_MakeText(void) {
262 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
264 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
265 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
271 p1
->member
.para
.next_para
= p2
;
272 p2
->member
.para
.prev_para
= p1
;
273 p2
->member
.para
.nCharOfs
= 0;
277 buf
->pCharStyle
= NULL
;
283 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
286 LRESULT total_bytes_read
= 0;
287 BOOL is_read
= FALSE
;
288 DWORD cp
= CP_ACP
, copy
= 0;
289 char conv_buf
[4 + STREAMIN_BUFFER_SIZE
]; /* up to 4 additional UTF-8 bytes */
291 static const char bom_utf8
[] = {0xEF, 0xBB, 0xBF};
293 TRACE("%08x %p\n", dwFormat
, stream
);
297 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
301 ME_StreamInFill(stream
);
302 if (stream
->editstream
->dwError
)
306 total_bytes_read
+= stream
->dwSize
;
309 if (!(dwFormat
& SF_UNICODE
))
311 char * buf
= stream
->buffer
;
312 DWORD size
= stream
->dwSize
, end
;
317 if (stream
->dwSize
>= 3 && !memcmp(stream
->buffer
, bom_utf8
, 3))
329 memcpy(conv_buf
+ copy
, buf
, size
);
334 while ((buf
[end
-1] & 0xC0) == 0x80)
337 --total_bytes_read
; /* strange, but seems to match windows */
339 if (buf
[end
-1] & 0x80)
342 if ((buf
[end
-1] & 0xE0) == 0xC0)
344 if ((buf
[end
-1] & 0xF0) == 0xE0)
346 if ((buf
[end
-1] & 0xF8) == 0xF0)
349 if (size
- end
>= need
)
351 /* we have enough bytes for this sequence */
356 /* need more bytes, so don't transcode this sequence */
364 nWideChars
= MultiByteToWideChar(cp
, 0, buf
, end
, wszText
, STREAMIN_BUFFER_SIZE
);
371 memcpy(conv_buf
, buf
+ end
, size
- end
);
378 nWideChars
= stream
->dwSize
>> 1;
379 pText
= (WCHAR
*)stream
->buffer
;
382 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
383 if (stream
->dwSize
== 0)
387 return total_bytes_read
;
390 static void ME_ApplyBorderProperties(RTF_Info
*info
,
391 ME_BorderRect
*borderRect
,
392 RTFBorder
*borderDef
)
395 ME_Border
*pBorders
[] = {&borderRect
->top
,
399 for (i
= 0; i
< 4; i
++)
401 RTFColor
*colorDef
= info
->colorList
;
402 pBorders
[i
]->width
= borderDef
[i
].width
;
403 colorNum
= borderDef
[i
].color
;
404 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
405 colorDef
= colorDef
->rtfNextColor
;
407 pBorders
[i
]->colorRef
= RGB(
408 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
409 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
410 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
412 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
416 void ME_RTFCharAttrHook(RTF_Info
*info
)
419 fmt
.cbSize
= sizeof(fmt
);
423 switch(info
->rtfMinor
)
426 /* FIXME add more flags once they're implemented */
427 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
428 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
429 fmt
.yHeight
= 12*20; /* 12pt */
430 fmt
.wWeight
= FW_NORMAL
;
431 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
434 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
435 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
436 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
439 fmt
.dwMask
= CFM_ITALIC
;
440 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
443 fmt
.dwMask
= CFM_UNDERLINETYPE
;
444 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
446 case rtfDotUnderline
:
447 fmt
.dwMask
= CFM_UNDERLINETYPE
;
448 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
451 fmt
.dwMask
= CFM_UNDERLINETYPE
;
452 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
454 case rtfWordUnderline
:
455 fmt
.dwMask
= CFM_UNDERLINETYPE
;
456 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
459 fmt
.dwMask
= CFM_UNDERLINETYPE
;
460 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
463 fmt
.dwMask
= CFM_STRIKEOUT
;
464 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
468 case rtfSubScrShrink
:
469 case rtfSuperScrShrink
:
471 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
472 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
473 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
474 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
477 fmt
.dwMask
= CFM_HIDDEN
;
478 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
481 fmt
.dwMask
= CFM_BACKCOLOR
;
483 if (info
->rtfParam
== 0)
484 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
485 else if (info
->rtfParam
!= rtfNoParam
)
487 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
488 if (c
&& c
->rtfCBlue
>= 0)
489 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
491 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
495 fmt
.dwMask
= CFM_COLOR
;
497 if (info
->rtfParam
== 0)
498 fmt
.dwEffects
= CFE_AUTOCOLOR
;
499 else if (info
->rtfParam
!= rtfNoParam
)
501 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
502 if (c
&& c
->rtfCBlue
>= 0)
503 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
505 fmt
.dwEffects
= CFE_AUTOCOLOR
;
510 if (info
->rtfParam
!= rtfNoParam
)
512 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
515 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
516 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
517 fmt
.bCharSet
= f
->rtfFCharSet
;
518 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
519 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
524 fmt
.dwMask
= CFM_SIZE
;
525 if (info
->rtfParam
!= rtfNoParam
)
526 fmt
.yHeight
= info
->rtfParam
*10;
531 RTFFlushOutputBuffer(info
);
532 /* FIXME too slow ? how come ? */
533 style2
= ME_ApplyStyle(info
->editor
, info
->style
, &fmt
);
534 ME_ReleaseStyle(info
->style
);
535 info
->style
= style2
;
536 info
->styleChanged
= TRUE
;
540 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
541 the same tags mean different things in different contexts */
542 void ME_RTFParAttrHook(RTF_Info
*info
)
545 fmt
.cbSize
= sizeof(fmt
);
549 switch(info
->rtfMinor
)
551 case rtfParDef
: /* restores default paragraph attributes */
552 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
553 info
->borderType
= RTFBorderParaLeft
;
554 else /* v1.0 - 3.0 */
555 info
->borderType
= RTFBorderParaTop
;
556 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
557 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
559 /* TODO: numbering, shading */
560 fmt
.wAlignment
= PFA_LEFT
;
562 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
563 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
564 fmt
.wBorderSpace
= 0;
565 fmt
.bLineSpacingRule
= 0;
566 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
567 fmt
.dyLineSpacing
= 0;
568 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
570 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
571 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
574 ME_DisplayItem
*para
;
575 /* We are just after a table row. */
576 RTFFlushOutputBuffer(info
);
577 cursor
= info
->editor
->pCursors
[0];
579 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
580 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
582 /* Since the table row end, no text has been inserted, and the \intbl
583 * control word has not be used. We can confirm that we are not in a
586 info
->tableDef
->tableRowStart
= NULL
;
587 info
->canInheritInTbl
= FALSE
;
590 } else { /* v1.0 - v3.0 */
591 fmt
.dwMask
|= PFM_TABLE
;
592 fmt
.wEffects
&= ~PFE_TABLE
;
596 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
598 while (info
->rtfParam
> info
->nestingLevel
) {
599 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
600 ZeroMemory(tableDef
, sizeof(RTFTable
));
601 tableDef
->parent
= info
->tableDef
;
602 info
->tableDef
= tableDef
;
604 RTFFlushOutputBuffer(info
);
605 if (tableDef
->tableRowStart
&&
606 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
608 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
609 para
= para
->member
.para
.next_para
;
610 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
611 tableDef
->tableRowStart
= para
;
615 cursor
= info
->editor
->pCursors
[0];
616 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
617 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
618 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
621 info
->nestingLevel
++;
623 info
->canInheritInTbl
= FALSE
;
628 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
630 if (info
->nestingLevel
< 1)
635 info
->tableDef
= ALLOC_OBJ(RTFTable
);
636 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
638 tableDef
= info
->tableDef
;
639 RTFFlushOutputBuffer(info
);
640 if (tableDef
->tableRowStart
&&
641 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
643 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
644 para
= para
->member
.para
.next_para
;
645 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
646 tableDef
->tableRowStart
= para
;
650 cursor
= info
->editor
->pCursors
[0];
651 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
652 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
653 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
655 info
->nestingLevel
= 1;
656 info
->canInheritInTbl
= TRUE
;
659 } else { /* v1.0 - v3.0 */
660 fmt
.dwMask
|= PFM_TABLE
;
661 fmt
.wEffects
|= PFE_TABLE
;
666 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
667 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
668 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
669 fmt
.dxOffset
= -info
->rtfParam
;
672 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
673 fmt
.dwMask
= PFM_STARTINDENT
;
674 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
677 fmt
.dwMask
= PFM_RIGHTINDENT
;
678 fmt
.dxRightIndent
= info
->rtfParam
;
682 fmt
.dwMask
= PFM_ALIGNMENT
;
683 fmt
.wAlignment
= PFA_LEFT
;
686 fmt
.dwMask
= PFM_ALIGNMENT
;
687 fmt
.wAlignment
= PFA_RIGHT
;
690 fmt
.dwMask
= PFM_ALIGNMENT
;
691 fmt
.wAlignment
= PFA_CENTER
;
694 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
695 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
699 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
700 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
701 fmt
.dwMask
= PFM_TABSTOPS
;
704 fmt
.dwMask
= PFM_KEEP
;
705 fmt
.wEffects
= PFE_KEEP
;
707 case rtfNoWidowControl
:
708 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
709 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
712 fmt
.dwMask
= PFM_KEEPNEXT
;
713 fmt
.wEffects
= PFE_KEEPNEXT
;
716 fmt
.dwMask
= PFM_SPACEAFTER
;
717 fmt
.dySpaceAfter
= info
->rtfParam
;
720 fmt
.dwMask
= PFM_SPACEBEFORE
;
721 fmt
.dySpaceBefore
= info
->rtfParam
;
723 case rtfSpaceBetween
:
724 fmt
.dwMask
= PFM_LINESPACING
;
725 if ((int)info
->rtfParam
> 0)
727 fmt
.dyLineSpacing
= info
->rtfParam
;
728 fmt
.bLineSpacingRule
= 3;
732 fmt
.dyLineSpacing
= info
->rtfParam
;
733 fmt
.bLineSpacingRule
= 4;
736 case rtfSpaceMultiply
:
737 fmt
.dwMask
= PFM_LINESPACING
;
738 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
739 fmt
.bLineSpacingRule
= 5;
742 fmt
.dwMask
= PFM_NUMBERING
;
743 fmt
.wNumbering
= PFN_BULLET
;
746 fmt
.dwMask
= PFM_NUMBERING
;
747 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
749 case rtfParNumDecimal
:
750 fmt
.dwMask
= PFM_NUMBERING
;
751 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
753 case rtfParNumIndent
:
754 fmt
.dwMask
= PFM_NUMBERINGTAB
;
755 fmt
.wNumberingTab
= info
->rtfParam
;
757 case rtfParNumStartAt
:
758 fmt
.dwMask
= PFM_NUMBERINGSTART
;
759 fmt
.wNumberingStart
= info
->rtfParam
;
762 info
->borderType
= RTFBorderParaLeft
;
763 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
764 if (!(fmt
.dwMask
& PFM_BORDER
))
766 fmt
.wBorderSpace
= 0;
767 fmt
.wBorderWidth
= 1;
771 fmt
.dwMask
= PFM_BORDER
;
774 info
->borderType
= RTFBorderParaRight
;
775 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
776 if (!(fmt
.dwMask
& PFM_BORDER
))
778 fmt
.wBorderSpace
= 0;
779 fmt
.wBorderWidth
= 1;
783 fmt
.dwMask
= PFM_BORDER
;
786 info
->borderType
= RTFBorderParaTop
;
787 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
788 if (!(fmt
.dwMask
& PFM_BORDER
))
790 fmt
.wBorderSpace
= 0;
791 fmt
.wBorderWidth
= 1;
795 fmt
.dwMask
= PFM_BORDER
;
797 case rtfBorderBottom
:
798 info
->borderType
= RTFBorderParaBottom
;
799 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
800 if (!(fmt
.dwMask
& PFM_BORDER
))
802 fmt
.wBorderSpace
= 0;
803 fmt
.wBorderWidth
= 1;
807 fmt
.dwMask
= PFM_BORDER
;
809 case rtfBorderSingle
:
810 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
811 /* we assume that borders have been created before (RTF spec) */
812 fmt
.wBorders
&= ~0x700;
813 fmt
.wBorders
|= 1 << 8;
814 fmt
.dwMask
= PFM_BORDER
;
817 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
818 /* we assume that borders have been created before (RTF spec) */
819 fmt
.wBorders
&= ~0x700;
820 fmt
.wBorders
|= 2 << 8;
821 fmt
.dwMask
= PFM_BORDER
;
823 case rtfBorderShadow
:
824 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
825 /* we assume that borders have been created before (RTF spec) */
826 fmt
.wBorders
&= ~0x700;
827 fmt
.wBorders
|= 10 << 8;
828 fmt
.dwMask
= PFM_BORDER
;
830 case rtfBorderDouble
:
831 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
832 /* we assume that borders have been created before (RTF spec) */
833 fmt
.wBorders
&= ~0x700;
834 fmt
.wBorders
|= 7 << 8;
835 fmt
.dwMask
= PFM_BORDER
;
838 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
839 /* we assume that borders have been created before (RTF spec) */
840 fmt
.wBorders
&= ~0x700;
841 fmt
.wBorders
|= 11 << 8;
842 fmt
.dwMask
= PFM_BORDER
;
846 int borderSide
= info
->borderType
& RTFBorderSideMask
;
847 RTFTable
*tableDef
= info
->tableDef
;
848 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
849 /* we assume that borders have been created before (RTF spec) */
850 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
851 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
854 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
856 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
857 border
->width
= info
->rtfParam
;
860 fmt
.dwMask
= PFM_BORDER
;
864 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
865 /* we assume that borders have been created before (RTF spec) */
866 fmt
.wBorderSpace
= info
->rtfParam
;
867 fmt
.dwMask
= PFM_BORDER
;
871 RTFTable
*tableDef
= info
->tableDef
;
872 int borderSide
= info
->borderType
& RTFBorderSideMask
;
873 int borderType
= info
->borderType
& RTFBorderTypeMask
;
876 case RTFBorderTypePara
:
877 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
879 /* v1.0 - 3.0 treat paragraph and row borders the same. */
880 case RTFBorderTypeRow
:
882 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
885 case RTFBorderTypeCell
:
886 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
887 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
894 fmt
.dwMask
= PFM_RTLPARA
;
895 fmt
.wEffects
= PFE_RTLPARA
;
898 fmt
.dwMask
= PFM_RTLPARA
;
903 RTFFlushOutputBuffer(info
);
904 /* FIXME too slow ? how come ?*/
905 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
909 void ME_RTFTblAttrHook(RTF_Info
*info
)
911 switch (info
->rtfMinor
)
915 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
916 info
->borderType
= 0; /* Not sure */
917 else /* v1.0 - 3.0 */
918 info
->borderType
= RTFBorderRowTop
;
919 if (!info
->tableDef
) {
920 info
->tableDef
= ME_MakeTableDef(info
->editor
);
922 ME_InitTableDef(info
->editor
, info
->tableDef
);
931 info
->tableDef
= ME_MakeTableDef(info
->editor
);
933 cellNum
= info
->tableDef
->numCellsDefined
;
934 if (cellNum
>= MAX_TABLE_CELLS
)
936 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
937 if (cellNum
< MAX_TAB_STOPS
) {
938 /* Tab stops were used to store cell positions before v4.1 but v4.1
939 * still seems to set the tabstops without using them. */
940 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
941 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
942 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
943 pFmt
->rgxTabs
[cellNum
] |= 0x00FFFFFF & info
->rtfParam
;
945 info
->tableDef
->numCellsDefined
++;
949 info
->borderType
= RTFBorderRowTop
;
952 info
->borderType
= RTFBorderRowLeft
;
954 case rtfRowBordBottom
:
955 info
->borderType
= RTFBorderRowBottom
;
957 case rtfRowBordRight
:
958 info
->borderType
= RTFBorderRowRight
;
961 info
->borderType
= RTFBorderCellTop
;
963 case rtfCellBordLeft
:
964 info
->borderType
= RTFBorderCellLeft
;
966 case rtfCellBordBottom
:
967 info
->borderType
= RTFBorderCellBottom
;
969 case rtfCellBordRight
:
970 info
->borderType
= RTFBorderCellRight
;
974 info
->tableDef
->gapH
= info
->rtfParam
;
978 info
->tableDef
->leftEdge
= info
->rtfParam
;
983 void ME_RTFSpecialCharHook(RTF_Info
*info
)
985 RTFTable
*tableDef
= info
->tableDef
;
986 switch (info
->rtfMinor
)
989 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
991 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
995 RTFFlushOutputBuffer(info
);
996 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
997 if (tableDef
->tableRowStart
)
999 if (!info
->nestingLevel
&&
1000 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1002 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
1003 para
= para
->member
.para
.next_para
;
1004 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1005 tableDef
->tableRowStart
= para
;
1006 info
->nestingLevel
= 1;
1008 ME_InsertTableCellFromCursor(info
->editor
);
1010 } else { /* v1.0 - v3.0 */
1011 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1012 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1013 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
1014 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1017 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1018 tableDef
->numCellsInserted
++;
1023 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
1025 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1028 ME_DisplayItem
*para
, *cell
, *run
;
1033 RTFFlushOutputBuffer(info
);
1034 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
1035 if (!tableDef
->tableRowStart
)
1037 if (!info
->nestingLevel
&&
1038 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1040 para
= tableDef
->tableRowStart
;
1041 para
= para
->member
.para
.next_para
;
1042 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1043 tableDef
->tableRowStart
= para
;
1044 info
->nestingLevel
++;
1046 para
= tableDef
->tableRowStart
;
1047 cell
= ME_FindItemFwd(para
, diCell
);
1048 assert(cell
&& !cell
->member
.cell
.prev_cell
);
1049 if (tableDef
->numCellsDefined
< 1)
1051 /* 2000 twips appears to be the cell size that native richedit uses
1052 * when no cell sizes are specified. */
1053 const int defaultCellSize
= 2000;
1054 int nRightBoundary
= defaultCellSize
;
1055 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1056 while (cell
->member
.cell
.next_cell
) {
1057 cell
= cell
->member
.cell
.next_cell
;
1058 nRightBoundary
+= defaultCellSize
;
1059 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1061 para
= ME_InsertTableCellFromCursor(info
->editor
);
1062 cell
= para
->member
.para
.pCell
;
1063 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1065 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
1067 RTFCell
*cellDef
= &tableDef
->cells
[i
];
1068 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
1069 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
1071 cell
= cell
->member
.cell
.next_cell
;
1074 para
= ME_InsertTableCellFromCursor(info
->editor
);
1075 cell
= para
->member
.para
.pCell
;
1078 /* Cell for table row delimiter is empty */
1079 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1082 run
= ME_FindItemFwd(cell
, diRun
);
1083 if (info
->editor
->pCursors
[0].pRun
!= run
||
1084 info
->editor
->pCursors
[0].nOffset
)
1087 /* Delete inserted cells that aren't defined. */
1088 info
->editor
->pCursors
[1].pRun
= run
;
1089 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1090 info
->editor
->pCursors
[1].nOffset
= 0;
1091 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1092 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1093 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1097 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1098 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1099 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1100 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1102 info
->nestingLevel
--;
1103 if (!info
->nestingLevel
)
1105 if (info
->canInheritInTbl
) {
1106 tableDef
->tableRowStart
= para
;
1108 while (info
->tableDef
) {
1109 tableDef
= info
->tableDef
;
1110 info
->tableDef
= tableDef
->parent
;
1111 heap_free(tableDef
);
1115 info
->tableDef
= tableDef
->parent
;
1116 heap_free(tableDef
);
1118 } else { /* v1.0 - v3.0 */
1120 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1121 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1122 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1123 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1125 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1127 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1130 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1131 tableDef
->numCellsInserted
++;
1133 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1134 if (!tableDef
->numCellsDefined
)
1135 pFmt
->wEffects
&= ~PFE_TABLE
;
1136 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1137 tableDef
->numCellsInserted
= 0;
1143 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1144 ME_DisplayItem
*para
;
1146 RTFFlushOutputBuffer(info
);
1147 para
= info
->editor
->pCursors
[0].pPara
;
1148 pFmt
= para
->member
.para
.pFmt
;
1149 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1151 /* rtfPar is treated like a space within a table. */
1152 info
->rtfClass
= rtfText
;
1153 info
->rtfMajor
= ' ';
1155 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1156 tableDef
->numCellsInserted
= 0;
1162 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1165 LPOLEOBJECT lpObject
= NULL
;
1166 LPSTORAGE lpStorage
= NULL
;
1167 LPOLECLIENTSITE lpClientSite
= NULL
;
1168 LPDATAOBJECT lpDataObject
= NULL
;
1169 LPOLECACHE lpOleCache
= NULL
;
1178 stgm
.tymed
= TYMED_ENHMF
;
1179 stgm
.u
.hEnhMetaFile
= hemf
;
1180 fm
.cfFormat
= CF_ENHMETAFILE
;
1184 stgm
.tymed
= TYMED_GDI
;
1185 stgm
.u
.hBitmap
= hbmp
;
1186 fm
.cfFormat
= CF_BITMAP
;
1188 stgm
.pUnkForRelease
= NULL
;
1191 fm
.dwAspect
= DVASPECT_CONTENT
;
1193 fm
.tymed
= stgm
.tymed
;
1195 if (!info
->lpRichEditOle
)
1197 CreateIRichEditOle(NULL
, info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1200 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1201 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1202 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1203 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1204 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1205 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1206 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1207 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1211 reobject
.cbStruct
= sizeof(reobject
);
1212 reobject
.cp
= REO_CP_SELECTION
;
1213 reobject
.clsid
= clsid
;
1214 reobject
.poleobj
= lpObject
;
1215 reobject
.pstg
= lpStorage
;
1216 reobject
.polesite
= lpClientSite
;
1217 /* convert from twips to .01 mm */
1218 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1219 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1220 reobject
.dvaspect
= DVASPECT_CONTENT
;
1221 reobject
.dwFlags
= 0; /* FIXME */
1222 reobject
.dwUser
= 0;
1224 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1228 if (lpObject
) IOleObject_Release(lpObject
);
1229 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1230 if (lpStorage
) IStorage_Release(lpStorage
);
1231 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1232 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1237 static void ME_RTFReadShpPictGroup( RTF_Info
*info
)
1245 if (info
->rtfClass
== rtfEOF
) return;
1246 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1248 if (--level
== 0) break;
1250 else if (RTFCheckCM( info
, rtfGroup
, rtfBeginGroup
))
1256 RTFRouteToken( info
);
1257 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1262 RTFRouteToken( info
); /* feed "}" back to router */
1266 static DWORD
read_hex_data( RTF_Info
*info
, BYTE
**out
)
1268 DWORD read
= 0, size
= 1024;
1274 if (info
->rtfClass
!= rtfText
)
1276 ERR("Called with incorrect token\n");
1280 buf
= HeapAlloc( GetProcessHeap(), 0, size
);
1283 val
= info
->rtfMajor
;
1284 for (flip
= TRUE
;; flip
= !flip
)
1286 RTFGetToken( info
);
1287 if (info
->rtfClass
== rtfEOF
)
1289 HeapFree( GetProcessHeap(), 0, buf
);
1292 if (info
->rtfClass
!= rtfText
) break;
1298 buf
= HeapReAlloc( GetProcessHeap(), 0, buf
, size
);
1301 buf
[read
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1304 val
= info
->rtfMajor
;
1306 if (flip
) FIXME("wrong hex string\n");
1312 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1315 BYTE
*buffer
= NULL
;
1320 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1328 RTFGetToken( info
);
1330 if (info
->rtfClass
== rtfText
)
1335 size
= read_hex_data( info
, &buffer
);
1339 RTFSkipGroup( info
);
1341 } /* We potentially have a new token so fall through. */
1343 if (info
->rtfClass
== rtfEOF
) return;
1345 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1347 if (--level
== 0) break;
1350 if (RTFCheckCM( info
, rtfGroup
, rtfBeginGroup
))
1355 if (!RTFCheckCM( info
, rtfControl
, rtfPictAttr
))
1357 RTFRouteToken( info
);
1358 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1363 if (RTFCheckMM( info
, rtfPictAttr
, rtfWinMetafile
))
1365 mfp
.mm
= info
->rtfParam
;
1368 else if (RTFCheckMM( info
, rtfPictAttr
, rtfDevIndBitmap
))
1370 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1373 else if (RTFCheckMM( info
, rtfPictAttr
, rtfEmfBlip
))
1374 gfx
= gfx_enhmetafile
;
1375 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicWid
))
1376 mfp
.xExt
= info
->rtfParam
;
1377 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicHt
))
1378 mfp
.yExt
= info
->rtfParam
;
1379 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicGoalWid
))
1380 sz
.cx
= info
->rtfParam
;
1381 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicGoalHt
))
1382 sz
.cy
= info
->rtfParam
;
1384 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1391 case gfx_enhmetafile
:
1392 if ((hemf
= SetEnhMetaFileBits( size
, buffer
)))
1393 ME_RTFInsertOleObject( info
, hemf
, NULL
, &sz
);
1396 if ((hemf
= SetWinMetaFileBits( size
, buffer
, NULL
, &mfp
)))
1397 ME_RTFInsertOleObject( info
, hemf
, NULL
, &sz
);
1401 BITMAPINFO
*bi
= (BITMAPINFO
*)buffer
;
1403 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1405 /* not quite right, especially for bitfields type of compression */
1406 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1407 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1408 if ((hbmp
= CreateDIBitmap( hdc
, &bi
->bmiHeader
,
1409 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1410 bi
, DIB_RGB_COLORS
)) )
1411 ME_RTFInsertOleObject( info
, NULL
, hbmp
, &sz
);
1412 ReleaseDC( 0, hdc
);
1419 HeapFree( GetProcessHeap(), 0, buffer
);
1420 RTFRouteToken( info
); /* feed "}" back to router */
1424 /* for now, lookup the \result part and use it, whatever the object */
1425 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1430 if (info
->rtfClass
== rtfEOF
)
1432 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1434 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1437 if (info
->rtfClass
== rtfEOF
)
1439 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1443 while (RTFGetToken (info
) != rtfEOF
)
1445 if (info
->rtfClass
== rtfGroup
)
1447 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1448 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1450 RTFRouteToken(info
);
1453 else RTFSkipGroup(info
);
1456 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1458 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1462 RTFRouteToken(info
); /* feed "}" back to router */
1465 static void ME_RTFReadHook(RTF_Info
*info
)
1467 switch(info
->rtfClass
)
1470 switch(info
->rtfMajor
)
1473 if (info
->stackTop
< maxStack
) {
1474 info
->stack
[info
->stackTop
].style
= info
->style
;
1475 ME_AddRefStyle(info
->style
);
1476 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1477 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1480 info
->styleChanged
= FALSE
;
1484 RTFFlushOutputBuffer(info
);
1486 if (info
->stackTop
<= 0)
1487 info
->rtfClass
= rtfEOF
;
1488 if (info
->stackTop
< 0)
1491 ME_ReleaseStyle(info
->style
);
1492 info
->style
= info
->stack
[info
->stackTop
].style
;
1493 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1494 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1503 ME_StreamInFill(ME_InStream
*stream
)
1505 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1506 (BYTE
*)stream
->buffer
,
1507 sizeof(stream
->buffer
),
1508 (LONG
*)&stream
->dwSize
);
1512 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1516 int from
, to
, nUndoMode
;
1517 int nEventMask
= editor
->nEventMask
;
1518 ME_InStream inStream
;
1519 BOOL invalidRTF
= FALSE
;
1520 ME_Cursor
*selStart
, *selEnd
;
1521 LRESULT num_read
= 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1523 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1524 editor
->nEventMask
= 0;
1526 ME_GetSelectionOfs(editor
, &from
, &to
);
1527 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1529 ME_GetSelection(editor
, &selStart
, &selEnd
);
1530 style
= ME_GetSelectionInsertStyle(editor
);
1532 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1534 /* Don't insert text at the end of the table row */
1535 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1536 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1537 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1539 para
= para
->member
.para
.next_para
;
1540 editor
->pCursors
[0].pPara
= para
;
1541 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1542 editor
->pCursors
[0].nOffset
= 0;
1544 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1546 para
= para
->member
.para
.next_para
;
1547 editor
->pCursors
[0].pPara
= para
;
1548 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1549 editor
->pCursors
[0].nOffset
= 0;
1551 editor
->pCursors
[1] = editor
->pCursors
[0];
1552 } else { /* v1.0 - 3.0 */
1553 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1554 ME_IsInTable(editor
->pCursors
[0].pRun
))
1558 style
= editor
->pBuffer
->pDefaultStyle
;
1559 ME_AddRefStyle(style
);
1560 ME_SetSelection(editor
, 0, 0);
1561 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1562 ME_GetTextLength(editor
), FALSE
);
1564 ME_ClearTempStyle(editor
);
1565 ME_SetDefaultParaFormat(editor
, editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1569 /* Back up undo mode to a local variable */
1570 nUndoMode
= editor
->nUndoMode
;
1572 /* Only create an undo if SFF_SELECTION is set */
1573 if (!(format
& SFF_SELECTION
))
1574 editor
->nUndoMode
= umIgnore
;
1576 inStream
.editstream
= stream
;
1577 inStream
.editstream
->dwError
= 0;
1578 inStream
.dwSize
= 0;
1579 inStream
.dwUsed
= 0;
1581 if (format
& SF_RTF
)
1583 /* Check if it's really RTF, and if it is not, use plain text */
1584 ME_StreamInFill(&inStream
);
1585 if (!inStream
.editstream
->dwError
)
1587 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1588 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1591 inStream
.editstream
->dwError
= -16;
1596 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1598 if (format
& SF_RTF
) {
1599 from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1601 /* setup the RTF parser */
1602 memset(&parser
, 0, sizeof parser
);
1603 RTFSetEditStream(&parser
, &inStream
);
1604 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1605 parser
.editor
= editor
;
1606 parser
.style
= style
;
1607 WriterInit(&parser
);
1609 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1610 RTFSetDestinationCallback(&parser
, rtfShpPict
, ME_RTFReadShpPictGroup
);
1611 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1612 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1613 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1615 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1616 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1620 /* do the parsing */
1622 RTFFlushOutputBuffer(&parser
);
1623 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1624 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1625 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1627 /* Delete any incomplete table row at the end of the rich text. */
1629 ME_DisplayItem
*para
;
1631 parser
.rtfMinor
= rtfRow
;
1632 /* Complete the table row before deleting it.
1633 * By doing it this way we will have the current paragraph format set
1634 * properly to reflect that is not in the complete table, and undo items
1635 * will be added for this change to the current paragraph format. */
1636 if (parser
.nestingLevel
> 0)
1638 while (parser
.nestingLevel
> 1)
1639 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1640 para
= parser
.tableDef
->tableRowStart
;
1641 ME_RTFSpecialCharHook(&parser
);
1643 para
= parser
.tableDef
->tableRowStart
;
1644 ME_RTFSpecialCharHook(&parser
);
1645 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1646 para
= para
->member
.para
.next_para
;
1649 editor
->pCursors
[1].pPara
= para
;
1650 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1651 editor
->pCursors
[1].nOffset
= 0;
1652 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1653 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1654 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1655 if (parser
.tableDef
)
1656 parser
.tableDef
->tableRowStart
= NULL
;
1659 ME_CheckTablesForCorruption(editor
);
1660 RTFDestroy(&parser
);
1661 if (parser
.lpRichEditOle
)
1662 IRichEditOle_Release(parser
.lpRichEditOle
);
1664 if (parser
.stackTop
> 0)
1666 while (--parser
.stackTop
>= 0)
1668 ME_ReleaseStyle(parser
.style
);
1669 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1671 if (!inStream
.editstream
->dwError
)
1672 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1675 /* Remove last line break, as mandated by tests. This is not affected by
1676 CR/LF counters, since RTF streaming presents only \para tokens, which
1677 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1679 if (stripLastCR
&& !(format
& SFF_SELECTION
)) {
1681 ME_GetSelection(editor
, &selStart
, &selEnd
);
1682 newto
= ME_GetCursorOfs(selEnd
);
1683 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1684 WCHAR lastchar
[3] = {'\0', '\0'};
1685 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1686 ME_Cursor linebreakCursor
= *selEnd
;
1688 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1689 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, FALSE
, FALSE
);
1690 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1691 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1695 to
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1696 num_read
= to
- from
;
1698 style
= parser
.style
;
1700 else if (format
& SF_TEXT
)
1701 num_read
= ME_StreamInText(editor
, format
, &inStream
, style
);
1703 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1704 /* put the cursor at the top */
1705 if (!(format
& SFF_SELECTION
))
1706 ME_SetSelection(editor
, 0, 0);
1709 /* Restore saved undo mode */
1710 editor
->nUndoMode
= nUndoMode
;
1712 /* even if we didn't add an undo, we need to commit anything on the stack */
1713 ME_CommitUndo(editor
);
1715 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1716 if (!(format
& SFF_SELECTION
))
1717 ME_EmptyUndoStack(editor
);
1719 ME_ReleaseStyle(style
);
1720 editor
->nEventMask
= nEventMask
;
1721 ME_UpdateRepaint(editor
, FALSE
);
1722 if (!(format
& SFF_SELECTION
)) {
1723 ME_ClearTempStyle(editor
);
1725 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1726 ME_MoveCaret(editor
);
1727 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1728 ME_SendSelChange(editor
);
1729 ME_SendRequestResize(editor
, FALSE
);
1735 typedef struct tagME_RTFStringStreamStruct
1740 } ME_RTFStringStreamStruct
;
1742 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1744 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1747 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1748 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1749 pStruct
->pos
+= count
;
1755 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1758 ME_RTFStringStreamStruct data
;
1760 data
.string
= string
;
1761 data
.length
= strlen(string
);
1763 es
.dwCookie
= (DWORD_PTR
)&data
;
1764 es
.pfnCallback
= ME_ReadFromRTFString
;
1765 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, TRUE
);
1770 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1772 const int nLen
= lstrlenW(text
);
1773 const int nTextLen
= ME_GetTextLength(editor
);
1776 WCHAR wLastChar
= ' ';
1778 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1779 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1781 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1782 FIXME("Flags 0x%08x not implemented\n",
1783 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1786 if (chrg
->cpMax
== -1)
1789 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1791 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1792 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1797 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1798 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1802 chrgText
->cpMin
= -1;
1803 chrgText
->cpMax
= -1;
1808 /* when searching up, if cpMin < cpMax, then instead of searching
1809 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1810 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1811 * case, it is always bigger than cpMin.
1813 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1817 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1818 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1824 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1827 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1831 if (flags
& FR_DOWN
) /* Forward search */
1833 /* If possible, find the character before where the search starts */
1834 if ((flags
& FR_WHOLEWORD
) && nMin
)
1836 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1837 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1838 ME_MoveCursorChars(editor
, &cursor
, 1);
1840 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1843 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1845 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1846 int nCurStart
= cursor
.nOffset
;
1849 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
), text
[nMatched
], (flags
& FR_MATCHCASE
)))
1851 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1855 if (nMatched
== nLen
)
1857 ME_DisplayItem
*pNextItem
= pCurItem
;
1858 int nNextStart
= nCurStart
;
1861 /* Check to see if next character is a whitespace */
1862 if (flags
& FR_WHOLEWORD
)
1864 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1866 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1867 nNextStart
= -nMatched
;
1871 wNextChar
= *get_text( &pNextItem
->member
.run
, nNextStart
+ nMatched
);
1875 if (isalnumW(wNextChar
))
1879 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1882 chrgText
->cpMin
= cursor
.nOffset
;
1883 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1885 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1886 return cursor
.nOffset
;
1888 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1890 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1891 nCurStart
= -nMatched
;
1895 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
);
1900 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.len
)
1902 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1907 else /* Backward search */
1909 /* If possible, find the character after where the search ends */
1910 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1912 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1913 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1914 ME_MoveCursorChars(editor
, &cursor
, -1);
1916 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1919 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1921 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1922 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1923 int nCurEnd
= cursor
.nOffset
;
1928 ME_PrevRun(&pCurPara
, &pCurItem
);
1929 nCurEnd
= pCurItem
->member
.run
.len
;
1932 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 ),
1933 text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
) ))
1935 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1939 if (nMatched
== nLen
)
1941 ME_DisplayItem
*pPrevItem
= pCurItem
;
1942 int nPrevEnd
= nCurEnd
;
1946 /* Check to see if previous character is a whitespace */
1947 if (flags
& FR_WHOLEWORD
)
1949 if (nPrevEnd
- nMatched
== 0)
1951 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1953 nPrevEnd
= pPrevItem
->member
.run
.len
+ nMatched
;
1957 wPrevChar
= *get_text( &pPrevItem
->member
.run
, nPrevEnd
- nMatched
- 1 );
1961 if (isalnumW(wPrevChar
))
1965 nStart
= pCurPara
->member
.para
.nCharOfs
1966 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1969 chrgText
->cpMin
= nStart
;
1970 chrgText
->cpMax
= nStart
+ nLen
;
1972 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1975 if (nCurEnd
- nMatched
== 0)
1977 ME_PrevRun(&pCurPara
, &pCurItem
);
1978 /* Don't care about pCurItem becoming NULL here; it's already taken
1979 * care of in the exterior loop condition */
1980 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1984 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 );
1989 if (cursor
.nOffset
< 0)
1991 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1992 cursor
.nOffset
= cursor
.pRun
->member
.run
.len
;
1996 TRACE("not found\n");
1998 chrgText
->cpMin
= chrgText
->cpMax
= -1;
2002 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
2007 if (!ex
->cb
|| !pText
) return 0;
2009 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
2010 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
2012 if (ex
->flags
& GT_SELECTION
)
2015 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
2016 start
= editor
->pCursors
[nStartCur
];
2021 ME_SetCursorToStart(editor
, &start
);
2024 if (ex
->codepage
== CP_UNICODE
)
2026 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
2027 &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
2031 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2032 we can just take a bigger buffer? :)
2033 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
2034 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2036 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
2041 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
2042 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
2044 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
2045 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
2046 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
2047 if (rc
) rc
--; /* do not count 0 terminator */
2054 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
2055 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
2057 if (!strText
) return 0;
2059 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, FALSE
, FALSE
);
2062 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2064 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, FALSE
, FALSE
);
2065 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
2066 nLen
+1, NULL
, NULL
);
2072 static int handle_EM_EXSETSEL( ME_TextEditor
*editor
, int to
, int from
)
2076 TRACE("%d - %d\n", to
, from
);
2078 ME_InvalidateSelection( editor
);
2079 end
= ME_SetSelection( editor
, to
, from
);
2080 ME_InvalidateSelection( editor
);
2081 ITextHost_TxShowCaret( editor
->texthost
, FALSE
);
2082 ME_ShowCaret( editor
);
2083 ME_SendSelChange( editor
);
2088 typedef struct tagME_GlobalDestStruct
2092 } ME_GlobalDestStruct
;
2094 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2096 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2101 pDest
= (WORD
*)lpBuff
;
2102 pSrc
= GlobalLock(pData
->hData
);
2103 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2104 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2106 pData
->nLength
+= i
;
2108 GlobalUnlock(pData
->hData
);
2112 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2114 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2119 pSrc
= GlobalLock(pData
->hData
);
2120 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2121 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2123 pData
->nLength
+= i
;
2125 GlobalUnlock(pData
->hData
);
2129 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2133 ME_GlobalDestStruct gds
;
2134 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2137 if (IsClipboardFormatAvailable(nRTFFormat
))
2138 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2139 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2140 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2144 if (!OpenClipboard(editor
->hWnd
))
2146 gds
.hData
= GetClipboardData(cf
);
2148 es
.dwCookie
= (DWORD_PTR
)&gds
;
2149 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2150 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2156 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2158 LPDATAOBJECT dataObj
= NULL
;
2161 if (editor
->cPasswordMask
)
2162 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2164 if(editor
->lpOleCallback
)
2167 range
.cpMin
= ME_GetCursorOfs(start
);
2168 range
.cpMax
= range
.cpMin
+ nChars
;
2169 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2171 if(FAILED(hr
) || !dataObj
)
2172 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2174 hr
= OleSetClipboard(dataObj
);
2175 IDataObject_Release(dataObj
);
2177 return SUCCEEDED(hr
);
2180 /* helper to send a msg filter notification */
2182 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2186 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2187 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2188 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2189 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2191 msgf
.wParam
= *wParam
;
2192 msgf
.lParam
= *lParam
;
2193 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2195 *wParam
= msgf
.wParam
;
2196 *lParam
= msgf
.lParam
;
2197 msgf
.wParam
= *wParam
;
2202 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2204 ME_DisplayItem
*startPara
, *endPara
;
2205 ME_DisplayItem
*prev_para
;
2206 ME_Cursor
*from
, *to
;
2210 if (!editor
->AutoURLDetect_bEnable
) return;
2212 ME_GetSelection(editor
, &from
, &to
);
2214 /* Find paragraph previous to the one that contains start cursor */
2215 startPara
= from
->pPara
;
2216 prev_para
= startPara
->member
.para
.prev_para
;
2217 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2219 /* Find paragraph that contains end cursor */
2220 endPara
= to
->pPara
->member
.para
.next_para
;
2222 start
.pPara
= startPara
;
2223 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2225 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2227 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2231 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2233 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2234 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2236 if (editor
->bMouseCaptured
)
2238 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2239 editor
->nSelectionType
= stPosition
;
2247 editor
->nUDArrowX
= -1;
2253 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2254 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2258 editor
->nUDArrowX
= -1;
2259 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2260 if (editor
->styleFlags
& ES_READONLY
)
2262 if (ME_IsSelection(editor
))
2264 ME_DeleteSelection(editor
);
2265 ME_CommitUndo(editor
);
2267 else if (nKey
== VK_DELETE
)
2269 /* Delete stops group typing.
2270 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2271 ME_DeleteTextAtCursor(editor
, 1, 1);
2272 ME_CommitUndo(editor
);
2274 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2276 BOOL bDeletionSucceeded
;
2277 /* Backspace can be grouped for a single undo */
2278 ME_ContinueCoalescingTransaction(editor
);
2279 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2280 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2281 /* Deletion was prevented so the cursor is moved back to where it was.
2282 * (e.g. this happens when trying to delete cell boundaries)
2284 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2286 ME_CommitCoalescingUndo(editor
);
2290 ME_MoveCursorFromTableRowStartParagraph(editor
);
2291 ME_UpdateSelectionLinkAttribute(editor
);
2292 ME_UpdateRepaint(editor
, FALSE
);
2293 ME_SendRequestResize(editor
, FALSE
);
2296 if (editor
->bDialogMode
)
2301 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2303 if (editor
->hwndParent
)
2306 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2307 if (HIWORD(dw
) == DC_HASDEFID
)
2309 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2312 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
2313 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2321 if (editor
->styleFlags
& ES_MULTILINE
)
2323 ME_Cursor cursor
= editor
->pCursors
[0];
2324 ME_DisplayItem
*para
= cursor
.pPara
;
2326 const WCHAR endl
= '\r';
2327 const WCHAR endlv10
[] = {'\r','\n'};
2330 if (editor
->styleFlags
& ES_READONLY
) {
2331 MessageBeep(MB_ICONERROR
);
2335 ME_GetSelectionOfs(editor
, &from
, &to
);
2336 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2338 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2339 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2340 /* Add a new table row after this row. */
2341 para
= ME_AppendTableRow(editor
, para
);
2342 para
= para
->member
.para
.next_para
;
2343 editor
->pCursors
[0].pPara
= para
;
2344 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2345 editor
->pCursors
[0].nOffset
= 0;
2346 editor
->pCursors
[1] = editor
->pCursors
[0];
2347 ME_CommitUndo(editor
);
2348 ME_CheckTablesForCorruption(editor
);
2349 ME_UpdateRepaint(editor
, FALSE
);
2352 else if (para
== editor
->pCursors
[1].pPara
&&
2353 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2354 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2355 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2357 /* Insert a newline before the table. */
2358 para
= para
->member
.para
.prev_para
;
2359 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2360 editor
->pCursors
[0].pPara
= para
;
2361 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2362 editor
->pCursors
[1] = editor
->pCursors
[0];
2363 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2364 editor
->pCursors
[0].pRun
->member
.run
.style
);
2365 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2366 ME_SetDefaultParaFormat(editor
, para
->member
.para
.pFmt
);
2367 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2368 editor
->pCursors
[0].pPara
= para
;
2369 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2370 editor
->pCursors
[1] = editor
->pCursors
[0];
2371 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2372 ME_CommitCoalescingUndo(editor
);
2373 ME_CheckTablesForCorruption(editor
);
2374 ME_UpdateRepaint(editor
, FALSE
);
2377 } else { /* v1.0 - 3.0 */
2378 ME_DisplayItem
*para
= cursor
.pPara
;
2379 if (ME_IsInTable(para
))
2381 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2384 ME_ContinueCoalescingTransaction(editor
);
2385 para
= ME_AppendTableRow(editor
, para
);
2386 editor
->pCursors
[0].pPara
= para
;
2387 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2388 editor
->pCursors
[0].nOffset
= 0;
2389 editor
->pCursors
[1] = editor
->pCursors
[0];
2390 ME_CommitCoalescingUndo(editor
);
2391 ME_UpdateRepaint(editor
, FALSE
);
2395 ME_ContinueCoalescingTransaction(editor
);
2396 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2397 !ME_IsInTable(para
->member
.para
.prev_para
))
2399 /* Insert newline before table */
2400 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2402 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2403 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2405 editor
->pCursors
[0].nOffset
= 0;
2406 editor
->pCursors
[1] = editor
->pCursors
[0];
2407 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2408 editor
->pCursors
[0].pRun
->member
.run
.style
);
2410 editor
->pCursors
[1] = editor
->pCursors
[0];
2411 para
= ME_AppendTableRow(editor
, para
);
2412 editor
->pCursors
[0].pPara
= para
;
2413 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2414 editor
->pCursors
[0].nOffset
= 0;
2415 editor
->pCursors
[1] = editor
->pCursors
[0];
2417 ME_CommitCoalescingUndo(editor
);
2418 ME_UpdateRepaint(editor
, FALSE
);
2424 style
= ME_GetInsertStyle(editor
, 0);
2425 ME_SaveTempStyle(editor
);
2426 ME_ContinueCoalescingTransaction(editor
);
2428 ME_InsertEndRowFromCursor(editor
, 0);
2430 if (!editor
->bEmulateVersion10
)
2431 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2433 ME_InsertTextFromCursor(editor
, 0, endlv10
, 2, style
);
2434 ME_ReleaseStyle(style
);
2435 ME_CommitCoalescingUndo(editor
);
2438 ME_UpdateSelectionLinkAttribute(editor
);
2439 ME_UpdateRepaint(editor
, FALSE
);
2445 if (editor
->bDialogMode
&& editor
->hwndParent
)
2446 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2449 if (editor
->bDialogMode
&& editor
->hwndParent
)
2450 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2455 handle_EM_EXSETSEL( editor
, 0, -1 );
2461 return ME_Paste(editor
);
2469 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2470 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2473 result
= ME_Copy(editor
, selStart
, nChars
);
2474 if (result
&& nKey
== 'X')
2476 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2477 ME_CommitUndo(editor
);
2478 ME_UpdateRepaint(editor
, TRUE
);
2499 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2500 editor
->nUDArrowX
= -1;
2507 chf
.cbSize
= sizeof(chf
);
2509 ME_GetSelectionCharFormat(editor
, &chf
);
2510 ME_DumpStyleToBuf(&chf
, buf
);
2511 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2515 ME_CheckCharOffsets(editor
);
2522 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2523 LPARAM flags
, BOOL unicode
)
2527 if (editor
->bMouseCaptured
)
2531 wstr
= (WCHAR
)charCode
;
2534 CHAR charA
= charCode
;
2535 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2538 if (editor
->styleFlags
& ES_READONLY
) {
2539 MessageBeep(MB_ICONERROR
);
2540 return 0; /* FIXME really 0 ? */
2543 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2545 ME_Cursor cursor
= editor
->pCursors
[0];
2546 ME_DisplayItem
*para
= cursor
.pPara
;
2548 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2549 ME_GetSelectionOfs(editor
, &from
, &to
);
2551 /* v4.1 allows tabs to be inserted with ctrl key down */
2552 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2554 ME_DisplayItem
*para
;
2555 BOOL bSelectedRow
= FALSE
;
2557 para
= cursor
.pPara
;
2558 if (ME_IsSelection(editor
) &&
2559 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2560 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2561 para
->member
.para
.prev_para
->type
== diParagraph
)
2563 para
= para
->member
.para
.prev_para
;
2564 bSelectedRow
= TRUE
;
2566 if (ME_IsInTable(para
))
2568 ME_TabPressedInTable(editor
, bSelectedRow
);
2569 ME_CommitUndo(editor
);
2572 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2573 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2575 para
= para
->member
.para
.next_para
;
2576 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2577 para
= para
->member
.para
.next_para
;
2578 editor
->pCursors
[0].pPara
= para
;
2579 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2580 editor
->pCursors
[0].nOffset
= 0;
2581 editor
->pCursors
[1] = editor
->pCursors
[0];
2584 } else { /* v1.0 - 3.0 */
2585 if (ME_IsInTable(cursor
.pRun
) &&
2586 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2589 /* Text should not be inserted at the end of the table. */
2594 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2595 /* WM_CHAR is restricted to nTextLimit */
2596 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2598 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2599 ME_SaveTempStyle(editor
);
2600 ME_ContinueCoalescingTransaction(editor
);
2601 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2602 ME_ReleaseStyle(style
);
2603 ME_CommitCoalescingUndo(editor
);
2604 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2607 ME_UpdateSelectionLinkAttribute(editor
);
2608 ME_UpdateRepaint(editor
, FALSE
);
2613 /* Process the message and calculate the new click count.
2615 * returns: The click count if it is mouse down event, else returns 0. */
2616 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2619 static int clickNum
= 0;
2620 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2623 if ((msg
== WM_LBUTTONDBLCLK
) ||
2624 (msg
== WM_RBUTTONDBLCLK
) ||
2625 (msg
== WM_MBUTTONDBLCLK
) ||
2626 (msg
== WM_XBUTTONDBLCLK
))
2628 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2631 if ((msg
== WM_LBUTTONDOWN
) ||
2632 (msg
== WM_RBUTTONDOWN
) ||
2633 (msg
== WM_MBUTTONDOWN
) ||
2634 (msg
== WM_XBUTTONDOWN
))
2636 static MSG prevClickMsg
;
2638 /* Compare the editor instead of the hwnd so that the this
2639 * can still be done for windowless richedit controls. */
2640 clickMsg
.hwnd
= (HWND
)editor
;
2641 clickMsg
.message
= msg
;
2642 clickMsg
.wParam
= wParam
;
2643 clickMsg
.lParam
= lParam
;
2644 clickMsg
.time
= GetMessageTime();
2645 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2646 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2647 if ((clickNum
!= 0) &&
2648 (clickMsg
.message
== prevClickMsg
.message
) &&
2649 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2650 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2651 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2652 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2653 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2659 prevClickMsg
= clickMsg
;
2666 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2672 DWORD messagePos
= GetMessagePos();
2673 pt
.x
= (short)LOWORD(messagePos
);
2674 pt
.y
= (short)HIWORD(messagePos
);
2678 sbi
.cbSize
= sizeof(sbi
);
2679 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2680 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2681 PtInRect(&sbi
.rcScrollBar
, pt
))
2683 ITextHost_TxSetCursor(editor
->texthost
,
2684 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2687 sbi
.cbSize
= sizeof(sbi
);
2688 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2689 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2690 PtInRect(&sbi
.rcScrollBar
, pt
))
2692 ITextHost_TxSetCursor(editor
->texthost
,
2693 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2697 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2699 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2700 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2703 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2704 pt
.y
< editor
->rcFormat
.top
&&
2705 pt
.x
< editor
->rcFormat
.left
)
2707 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2710 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2712 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2713 ITextHost_TxSetCursor(editor
->texthost
,
2714 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2716 ITextHost_TxSetCursor(editor
->texthost
,
2717 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2720 if (pt
.x
< editor
->rcFormat
.left
)
2722 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2725 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2730 run
= &cursor
.pRun
->member
.run
;
2731 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2732 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2734 ITextHost_TxSetCursor(editor
->texthost
,
2735 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2740 if (ME_IsSelection(editor
))
2742 int selStart
, selEnd
;
2743 int offset
= ME_GetCursorOfs(&cursor
);
2745 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2746 if (selStart
<= offset
&& selEnd
>= offset
) {
2747 ITextHost_TxSetCursor(editor
->texthost
,
2748 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2754 ITextHost_TxSetCursor(editor
->texthost
,
2755 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2759 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2761 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2762 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2763 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2764 editor
->rcFormat
.right
-= 1;
2767 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2772 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2774 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2775 if(selrange
.cpMin
== selrange
.cpMax
)
2776 seltype
|= SEL_EMPTY
;
2779 /* FIXME: Handle objects */
2780 seltype
|= SEL_TEXT
;
2781 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2782 seltype
|= SEL_MULTICHAR
;
2784 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2786 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2792 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
, DWORD csStyle
)
2794 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2800 ed
->hwndParent
= NULL
;
2801 ed
->sizeWindow
.cx
= ed
->sizeWindow
.cy
= 0;
2802 ed
->texthost
= texthost
;
2804 ed
->bEmulateVersion10
= bEmulateVersion10
;
2806 ed
->alignStyle
= PFA_LEFT
;
2807 if (csStyle
& ES_RIGHT
)
2808 ed
->alignStyle
= PFA_RIGHT
;
2809 if (csStyle
& ES_CENTER
)
2810 ed
->alignStyle
= PFA_CENTER
;
2811 ITextHost_TxGetPropertyBits(texthost
,
2812 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2813 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2814 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2815 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2816 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2818 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2819 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2820 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2821 ed
->pBuffer
= ME_MakeText();
2822 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2823 ed
->nAvailWidth
= 0; /* wrap to client area */
2824 ME_MakeFirstParagraph(ed
);
2825 /* The four cursors are for:
2826 * 0 - The position where the caret is shown
2827 * 1 - The anchored end of the selection (for normal selection)
2828 * 2 & 3 - The anchored start and end respectively for word, line,
2829 * or paragraph selection.
2832 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2833 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2834 ed
->pCursors
[1] = ed
->pCursors
[0];
2835 ed
->pCursors
[2] = ed
->pCursors
[0];
2836 ed
->pCursors
[3] = ed
->pCursors
[1];
2837 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2838 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2840 ed
->rgbBackColor
= -1;
2841 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2842 ed
->bCaretAtEnd
= FALSE
;
2844 ed
->nModifyStep
= 0;
2845 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2846 list_init( &ed
->undo_stack
);
2847 list_init( &ed
->redo_stack
);
2848 ed
->nUndoStackSize
= 0;
2849 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2850 ed
->nUndoMode
= umAddToUndo
;
2851 ed
->nParagraphs
= 1;
2852 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2853 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2854 ed
->bHideSelection
= FALSE
;
2855 ed
->pfnWordBreak
= NULL
;
2856 ed
->lpOleCallback
= NULL
;
2857 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2858 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2859 ed
->AutoURLDetect_bEnable
= FALSE
;
2860 ed
->bHaveFocus
= FALSE
;
2861 ed
->bDialogMode
= FALSE
;
2862 ed
->bMouseCaptured
= FALSE
;
2863 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2865 ed
->pFontCache
[i
].nRefs
= 0;
2866 ed
->pFontCache
[i
].nAge
= 0;
2867 ed
->pFontCache
[i
].hFont
= NULL
;
2870 ME_CheckCharOffsets(ed
);
2871 ed
->bDefaultFormatRect
= TRUE
;
2872 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2874 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2875 ed
->selofs
= SELECTIONBAR_WIDTH
;
2876 ed
->styleFlags
|= ES_SELECTIONBAR
;
2880 ed
->nSelectionType
= stPosition
;
2882 ed
->cPasswordMask
= 0;
2883 if (props
& TXTBIT_USEPASSWORD
)
2884 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2886 if (props
& TXTBIT_AUTOWORDSEL
)
2887 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2888 if (props
& TXTBIT_MULTILINE
) {
2889 ed
->styleFlags
|= ES_MULTILINE
;
2890 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2892 ed
->bWordWrap
= FALSE
;
2894 if (props
& TXTBIT_READONLY
)
2895 ed
->styleFlags
|= ES_READONLY
;
2896 if (!(props
& TXTBIT_HIDESELECTION
))
2897 ed
->styleFlags
|= ES_NOHIDESEL
;
2898 if (props
& TXTBIT_SAVESELECTION
)
2899 ed
->styleFlags
|= ES_SAVESEL
;
2900 if (props
& TXTBIT_VERTICAL
)
2901 ed
->styleFlags
|= ES_VERTICAL
;
2902 if (props
& TXTBIT_DISABLEDRAG
)
2903 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2905 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2907 /* Default scrollbar information */
2908 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2909 ed
->vert_si
.nMin
= 0;
2910 ed
->vert_si
.nMax
= 0;
2911 ed
->vert_si
.nPage
= 0;
2912 ed
->vert_si
.nPos
= 0;
2914 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2915 ed
->horz_si
.nMin
= 0;
2916 ed
->horz_si
.nMax
= 0;
2917 ed
->horz_si
.nPage
= 0;
2918 ed
->horz_si
.nPos
= 0;
2920 ed
->wheel_remain
= 0;
2922 list_init( &ed
->style_list
);
2923 OleInitialize(NULL
);
2928 void ME_DestroyEditor(ME_TextEditor
*editor
)
2930 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2931 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2932 ME_Style
*s
, *cursor2
;
2935 ME_ClearTempStyle(editor
);
2936 ME_EmptyUndoStack(editor
);
2939 ME_DestroyDisplayItem(p
);
2943 LIST_FOR_EACH_ENTRY_SAFE( s
, cursor2
, &editor
->style_list
, ME_Style
, entry
)
2944 ME_DestroyStyle( s
);
2946 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2947 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2949 if (editor
->pFontCache
[i
].hFont
)
2950 DeleteObject(editor
->pFontCache
[i
].hFont
);
2952 if (editor
->rgbBackColor
!= -1)
2953 DeleteObject(editor
->hbrBackground
);
2954 if(editor
->lpOleCallback
)
2955 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2956 ITextHost_Release(editor
->texthost
);
2959 IRichEditOle_Release(editor
->reOle
);
2960 editor
->reOle
= NULL
;
2964 FREE_OBJ(editor
->pBuffer
);
2965 FREE_OBJ(editor
->pCursors
);
2970 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2975 case DLL_PROCESS_ATTACH
:
2976 DisableThreadLibraryCalls(hinstDLL
);
2977 me_heap
= HeapCreate (0, 0x10000, 0);
2978 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2979 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2983 case DLL_PROCESS_DETACH
:
2984 if (lpvReserved
) break;
2985 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2986 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2987 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2988 UnregisterClassA("RichEdit50A", 0);
2989 if (ME_ListBoxRegistered
)
2990 UnregisterClassW(REListBox20W
, 0);
2991 if (ME_ComboBoxRegistered
)
2992 UnregisterClassW(REComboBox20W
, 0);
2994 HeapDestroy (me_heap
);
3001 static inline int get_default_line_height( ME_TextEditor
*editor
)
3005 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
3006 height
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
3007 if (height
<= 0) height
= 24;
3012 static inline int calc_wheel_change( int *remain
, int amount_per_click
)
3014 int change
= amount_per_click
* (float)*remain
/ WHEEL_DELTA
;
3015 *remain
-= WHEEL_DELTA
* change
/ amount_per_click
;
3019 static const char * const edit_messages
[] = {
3048 "EM_SETPASSWORDCHAR",
3049 "EM_EMPTYUNDOBUFFER",
3050 "EM_GETFIRSTVISIBLELINE",
3052 "EM_SETWORDBREAKPROC",
3053 "EM_GETWORDBREAKPROC",
3054 "EM_GETPASSWORDCHAR",
3064 static const char * const richedit_messages
[] = {
3069 "EM_EXLINEFROMCHAR",
3075 "EM_GETOLEINTERFACE",
3085 "EM_SETOLECALLBACK",
3087 "EM_SETTARGETDEVICE",
3095 "EM_GETWORDBREAKPROCEX",
3096 "EM_SETWORDBREAKPROCEX",
3098 "EM_UNKNOWN_USER_83",
3103 "EM_STOPGROUPTYPING",
3107 "EM_GETAUTOURLDETECT",
3110 "EM_GETTEXTLENGTHEX",
3113 "EM_UNKNOWN_USER_98",
3114 "EM_UNKNOWN_USER_99",
3115 "EM_SETPUNCTUATION",
3116 "EM_GETPUNCTUATION",
3117 "EM_SETWORDWRAPMODE",
3118 "EM_GETWORDWRAPMODE",
3124 "EM_UNKNOWN_USER_109",
3125 "EM_UNKNOWN_USER_110",
3126 "EM_UNKNOWN_USER_111",
3127 "EM_UNKNOWN_USER_112",
3128 "EM_UNKNOWN_USER_113",
3129 "EM_UNKNOWN_USER_114",
3130 "EM_UNKNOWN_USER_115",
3131 "EM_UNKNOWN_USER_116",
3132 "EM_UNKNOWN_USER_117",
3133 "EM_UNKNOWN_USER_118",
3134 "EM_UNKNOWN_USER_119",
3135 "EM_SETLANGOPTIONS",
3136 "EM_GETLANGOPTIONS",
3137 "EM_GETIMECOMPMODE",
3141 "EM_SETIMEMODEBIAS",
3146 get_msg_name(UINT msg
)
3148 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
3149 return edit_messages
[msg
- EM_GETSEL
];
3150 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
3151 return richedit_messages
[msg
- EM_CANPASTE
];
3155 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3159 ME_Cursor cursor
; /* The start of the clicked text. */
3162 x
= (short)LOWORD(lParam
);
3163 y
= (short)HIWORD(lParam
);
3164 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3165 if (!isExact
) return;
3167 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3168 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3169 { /* The clicked run has CFE_LINK set */
3172 info
.nmhdr
.hwndFrom
= NULL
;
3173 info
.nmhdr
.idFrom
= 0;
3174 info
.nmhdr
.code
= EN_LINK
;
3176 info
.wParam
= wParam
;
3177 info
.lParam
= lParam
;
3180 /* find the first contiguous run with CFE_LINK set */
3181 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3182 for (di
= cursor
.pRun
->prev
;
3183 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3186 info
.chrg
.cpMin
-= di
->member
.run
.len
;
3189 /* find the last contiguous run with CFE_LINK set */
3190 info
.chrg
.cpMax
= ME_GetCursorOfs(&cursor
) + cursor
.pRun
->member
.run
.len
;
3191 for (di
= cursor
.pRun
->next
;
3192 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3195 info
.chrg
.cpMax
+= di
->member
.run
.len
;
3198 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3202 void ME_ReplaceSel(ME_TextEditor
*editor
, BOOL can_undo
, const WCHAR
*str
, int len
)
3204 int from
, to
, nStartCursor
;
3207 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3208 style
= ME_GetSelectionInsertStyle(editor
);
3209 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3210 ME_InsertTextFromCursor(editor
, 0, str
, len
, style
);
3211 ME_ReleaseStyle(style
);
3212 /* drop temporary style if line end */
3214 * FIXME question: does abc\n mean: put abc,
3215 * clear temp style, put \n? (would require a change)
3217 if (len
>0 && str
[len
-1] == '\n')
3218 ME_ClearTempStyle(editor
);
3219 ME_CommitUndo(editor
);
3220 ME_UpdateSelectionLinkAttribute(editor
);
3222 ME_EmptyUndoStack(editor
);
3223 ME_UpdateRepaint(editor
, FALSE
);
3226 static void ME_SetText(ME_TextEditor
*editor
, void *text
, BOOL unicode
)
3228 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3231 LPWSTR wszText
= ME_ToUnicode(codepage
, text
, &textLen
);
3237 /* uses default style! */
3238 if (!(editor
->styleFlags
& ES_MULTILINE
))
3242 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3245 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3247 ME_EndToUnicode(codepage
, wszText
);
3250 static LRESULT
ME_WmCreate(ME_TextEditor
*editor
, LPARAM lParam
, BOOL unicode
)
3252 CREATESTRUCTW
*createW
= (CREATESTRUCTW
*)lParam
;
3253 CREATESTRUCTA
*createA
= (CREATESTRUCTA
*)lParam
;
3258 text
= unicode
? (void*)createW
->lpszName
: (void*)createA
->lpszName
;
3260 ME_SetDefaultFormatRect(editor
);
3262 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
3263 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
3264 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
3266 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
3267 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
3269 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3271 if (editor
->styleFlags
& WS_VSCROLL
)
3273 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
3274 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
3276 if (editor
->styleFlags
& WS_HSCROLL
)
3278 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
3279 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
3285 ME_SetText(editor
, text
, unicode
);
3286 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
3287 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
3290 ME_CommitUndo(editor
);
3291 ME_WrapMarkedParagraphs(editor
);
3292 ME_MoveCaret(editor
);
3297 #define UNSUPPORTED_MSG(e) \
3299 FIXME(#e ": stub\n"); \
3300 *phresult = S_FALSE; \
3303 /* Handle messages for windowless and windowed richedit controls.
3305 * The LRESULT that is returned is a return value for window procs,
3306 * and the phresult parameter is the COM return code needed by the
3307 * text services interface. */
3308 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3309 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3315 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3316 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3317 UNSUPPORTED_MSG(EM_FMTLINES
)
3318 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3319 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3320 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3321 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3322 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3323 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3324 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3325 UNSUPPORTED_MSG(EM_GETREDONAME
)
3326 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3327 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3328 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3329 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3330 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3331 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3332 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3333 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3334 UNSUPPORTED_MSG(EM_SETMARGINS
)
3335 UNSUPPORTED_MSG(EM_SETPALETTE
)
3336 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3337 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3338 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3340 /* Messages specific to Richedit controls */
3343 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3345 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3348 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
;
3351 editor
->bDialogMode
= TRUE
;
3352 if (editor
->styleFlags
& ES_MULTILINE
)
3353 code
|= DLGC_WANTMESSAGE
;
3354 if (!(editor
->styleFlags
& ES_SAVESEL
))
3355 code
|= DLGC_HASSETSEL
;
3358 case EM_EMPTYUNDOBUFFER
:
3359 ME_EmptyUndoStack(editor
);
3363 /* Note: wParam/lParam can be NULL */
3365 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3366 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3367 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3368 if ((*pfrom
|*pto
) & 0xFFFF0000)
3370 return MAKELONG(*pfrom
,*pto
);
3374 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3375 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3376 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3379 case EM_SETUNDOLIMIT
:
3381 if ((int)wParam
< 0)
3382 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3384 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3385 /* Setting a max stack size keeps wine from getting killed
3386 for hogging memory. Windows allocates all this memory at once, so
3387 no program would realistically set a value above our maximum. */
3388 return editor
->nUndoLimit
;
3391 return !list_empty( &editor
->undo_stack
);
3393 return !list_empty( &editor
->redo_stack
);
3394 case WM_UNDO
: /* FIXME: actually not the same */
3396 return ME_Undo(editor
);
3398 return ME_Redo(editor
);
3401 /* these flags are equivalent to the ES_* counterparts */
3402 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3403 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3404 DWORD settings
= editor
->styleFlags
& mask
;
3408 case EM_SETFONTSIZE
:
3411 LONG tmp_size
, size
;
3412 BOOL is_increase
= ((LONG
)wParam
> 0);
3414 if (editor
->mode
& TM_PLAINTEXT
)
3417 cf
.cbSize
= sizeof(cf
);
3418 cf
.dwMask
= CFM_SIZE
;
3419 ME_GetSelectionCharFormat(editor
, &cf
);
3420 tmp_size
= (cf
.yHeight
/ 20) + wParam
;
3424 else if (tmp_size
> 12 && tmp_size
< 28 && tmp_size
% 2)
3425 size
= tmp_size
+ (is_increase
? 1 : -1);
3426 else if (tmp_size
> 28 && tmp_size
< 36)
3427 size
= is_increase
? 36 : 28;
3428 else if (tmp_size
> 36 && tmp_size
< 48)
3429 size
= is_increase
? 48 : 36;
3430 else if (tmp_size
> 48 && tmp_size
< 72)
3431 size
= is_increase
? 72 : 48;
3432 else if (tmp_size
> 72 && tmp_size
< 80)
3433 size
= is_increase
? 80 : 72;
3434 else if (tmp_size
> 80 && tmp_size
< 1638)
3435 size
= 10 * (is_increase
? (tmp_size
/ 10 + 1) : (tmp_size
/ 10));
3436 else if (tmp_size
>= 1638)
3441 cf
.yHeight
= size
* 20; /* convert twips to points */
3442 ME_SetSelectionCharFormat(editor
, &cf
);
3443 ME_CommitUndo(editor
);
3444 ME_WrapMarkedParagraphs(editor
);
3445 ME_UpdateScrollBar(editor
);
3452 /* these flags are equivalent to ES_* counterparts, except for
3453 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3454 * but is still stored in editor->styleFlags. */
3455 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3456 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3457 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3458 DWORD settings
= mask
& editor
->styleFlags
;
3459 DWORD oldSettings
= settings
;
3460 DWORD changedSettings
;
3476 changedSettings
= oldSettings
^ settings
;
3478 if (changedSettings
) {
3479 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3481 if (changedSettings
& ECO_SELECTIONBAR
)
3483 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3484 if (settings
& ECO_SELECTIONBAR
) {
3485 assert(!editor
->selofs
);
3486 editor
->selofs
= SELECTIONBAR_WIDTH
;
3487 editor
->rcFormat
.left
+= editor
->selofs
;
3489 editor
->rcFormat
.left
-= editor
->selofs
;
3492 ME_RewrapRepaint(editor
);
3495 if (changedSettings
& settings
& ECO_VERTICAL
)
3496 FIXME("ECO_VERTICAL not implemented yet!\n");
3497 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3498 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3499 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3500 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3501 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3502 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3503 if (changedSettings
& settings
& ECO_WANTRETURN
)
3504 FIXME("ECO_WANTRETURN not implemented yet!\n");
3505 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3506 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3513 return handle_EM_EXSETSEL( editor
, wParam
, lParam
);
3515 case EM_SETSCROLLPOS
:
3517 POINT
*point
= (POINT
*)lParam
;
3518 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3521 case EM_AUTOURLDETECT
:
3523 if (wParam
==1 || wParam
==0)
3525 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3528 return E_INVALIDARG
;
3530 case EM_GETAUTOURLDETECT
:
3532 return editor
->AutoURLDetect_bEnable
;
3536 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3538 return handle_EM_EXSETSEL( editor
, range
.cpMin
, range
.cpMax
);
3540 case EM_SHOWSCROLLBAR
:
3553 flags
= WS_HSCROLL
|WS_VSCROLL
;
3560 editor
->styleFlags
|= flags
;
3561 if (flags
& WS_HSCROLL
)
3562 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3563 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3564 if (flags
& WS_VSCROLL
)
3565 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3566 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3568 editor
->styleFlags
&= ~flags
;
3569 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3576 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3579 BOOL bRtf
, bUnicode
, bSelection
, bUTF8
;
3580 int oldModify
= editor
->nModifyStep
;
3581 static const char utf8_bom
[] = {0xef, 0xbb, 0xbf};
3583 if (!pStruct
) return 0;
3585 /* If we detect ascii rtf at the start of the string,
3586 * we know it isn't unicode. */
3587 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3588 !strncmp((char *)lParam
, "{\\urtf", 6)));
3589 bUnicode
= !bRtf
&& pStruct
->codepage
== CP_UNICODE
;
3590 bUTF8
= (lParam
&& (!strncmp((char *)lParam
, utf8_bom
, 3)));
3592 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3593 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3594 pStruct
->flags
, pStruct
->codepage
);
3596 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3598 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3599 style
= ME_GetSelectionInsertStyle(editor
);
3600 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3603 ME_SetCursorToStart(editor
, &start
);
3604 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3605 style
= editor
->pBuffer
->pDefaultStyle
;
3609 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3611 /* FIXME: The length returned doesn't include the rtf control
3612 * characters, only the actual text. */
3613 len
= lParam
? strlen((char *)lParam
) : 0;
3616 if (bUTF8
&& !bUnicode
) {
3617 wszText
= ME_ToUnicode(CP_UTF8
, (void *)(lParam
+3), &len
);
3618 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3619 ME_EndToUnicode(CP_UTF8
, wszText
);
3621 wszText
= ME_ToUnicode(pStruct
->codepage
, (void *)lParam
, &len
);
3622 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3623 ME_EndToUnicode(pStruct
->codepage
, wszText
);
3628 ME_ReleaseStyle(style
);
3629 ME_UpdateSelectionLinkAttribute(editor
);
3633 ME_SetCursorToStart(editor
, &cursor
);
3634 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3636 ME_CommitUndo(editor
);
3637 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3639 editor
->nModifyStep
= oldModify
;
3640 ME_EmptyUndoStack(editor
);
3642 ME_UpdateRepaint(editor
, FALSE
);
3645 case EM_SETBKGNDCOLOR
:
3648 if (editor
->rgbBackColor
!= -1) {
3649 DeleteObject(editor
->hbrBackground
);
3650 lColor
= editor
->rgbBackColor
;
3652 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3656 editor
->rgbBackColor
= -1;
3657 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3661 editor
->rgbBackColor
= lParam
;
3662 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3664 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3665 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3669 return editor
->nModifyStep
== 0 ? 0 : -1;
3673 editor
->nModifyStep
= 1;
3675 editor
->nModifyStep
= 0;
3679 case EM_SETREADONLY
:
3682 editor
->styleFlags
|= ES_READONLY
;
3684 editor
->styleFlags
&= ~ES_READONLY
;
3687 case EM_SETEVENTMASK
:
3689 DWORD nOldMask
= editor
->nEventMask
;
3691 editor
->nEventMask
= lParam
;
3694 case EM_GETEVENTMASK
:
3695 return editor
->nEventMask
;
3696 case EM_SETCHARFORMAT
:
3698 CHARFORMAT2W buf
, *p
;
3699 BOOL bRepaint
= TRUE
;
3700 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3701 if (p
== NULL
) return 0;
3702 if (wParam
& SCF_ALL
) {
3703 if (editor
->mode
& TM_PLAINTEXT
) {
3704 ME_SetDefaultCharFormat(editor
, p
);
3707 ME_SetCursorToStart(editor
, &start
);
3708 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3709 editor
->nModifyStep
= 1;
3711 } else if (wParam
& SCF_SELECTION
) {
3712 if (editor
->mode
& TM_PLAINTEXT
)
3714 if (wParam
& SCF_WORD
) {
3716 ME_Cursor end
= editor
->pCursors
[0];
3717 ME_MoveCursorWords(editor
, &end
, +1);
3719 ME_MoveCursorWords(editor
, &start
, -1);
3720 ME_SetCharFormat(editor
, &start
, &end
, p
);
3722 bRepaint
= ME_IsSelection(editor
);
3723 ME_SetSelectionCharFormat(editor
, p
);
3724 if (bRepaint
) editor
->nModifyStep
= 1;
3725 } else { /* SCF_DEFAULT */
3726 ME_SetDefaultCharFormat(editor
, p
);
3728 ME_CommitUndo(editor
);
3731 ME_WrapMarkedParagraphs(editor
);
3732 ME_UpdateScrollBar(editor
);
3737 case EM_GETCHARFORMAT
:
3739 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3740 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3741 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3742 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3743 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3745 tmp
.cbSize
= sizeof(tmp
);
3747 ME_GetDefaultCharFormat(editor
, &tmp
);
3749 ME_GetSelectionCharFormat(editor
, &tmp
);
3750 ME_CopyToCFAny(dst
, &tmp
);
3753 case EM_SETPARAFORMAT
:
3755 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3756 ME_WrapMarkedParagraphs(editor
);
3757 ME_UpdateScrollBar(editor
);
3759 ME_CommitUndo(editor
);
3762 case EM_GETPARAFORMAT
:
3763 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3764 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3765 case EM_GETFIRSTVISIBLELINE
:
3767 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3768 int y
= editor
->vert_si
.nPos
;
3773 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3774 if (p
->type
== diTextEnd
)
3776 if (p
->type
== diParagraph
) {
3777 ypara
= p
->member
.para
.pt
.y
;
3780 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3781 yend
= ystart
+ p
->member
.row
.nHeight
;
3789 case EM_HIDESELECTION
:
3791 editor
->bHideSelection
= (wParam
!= 0);
3792 ME_InvalidateSelection(editor
);
3797 if (!(editor
->styleFlags
& ES_MULTILINE
))
3799 ME_ScrollDown( editor
, lParam
* get_default_line_height( editor
) );
3805 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3806 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3807 ME_CommitUndo(editor
);
3808 ME_UpdateRepaint(editor
, TRUE
);
3814 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3815 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &len
);
3817 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3819 ME_ReplaceSel(editor
, !!wParam
, wszText
, len
);
3820 ME_EndToUnicode(codepage
, wszText
);
3823 case EM_SCROLLCARET
:
3824 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3831 BOOL bRepaint
= LOWORD(lParam
);
3834 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3836 if (!GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
))
3839 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3840 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3841 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3842 if (editor
->mode
& TM_RICHTEXT
) {
3844 ME_SetCursorToStart(editor
, &start
);
3845 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3847 ME_SetDefaultCharFormat(editor
, &fmt
);
3849 ME_CommitUndo(editor
);
3850 ME_MarkAllForWrapping(editor
);
3851 ME_WrapMarkedParagraphs(editor
);
3852 ME_UpdateScrollBar(editor
);
3860 ME_SetCursorToStart(editor
, &cursor
);
3861 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3864 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3865 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3866 !strncmp((char *)lParam
, "{\\urtf", 6))
3868 /* Undocumented: WM_SETTEXT supports RTF text */
3869 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3872 ME_SetText(editor
, (void*)lParam
, unicode
);
3875 TRACE("WM_SETTEXT - NULL\n");
3876 ME_SetCursorToStart(editor
, &cursor
);
3877 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3878 ME_SetSelection(editor
, 0, 0);
3879 editor
->nModifyStep
= 0;
3880 ME_CommitUndo(editor
);
3881 ME_EmptyUndoStack(editor
);
3882 ME_UpdateRepaint(editor
, FALSE
);
3887 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3888 if (IsClipboardFormatAvailable(nRTFFormat
))
3890 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3895 case WM_MBUTTONDOWN
:
3901 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3902 int nChars
= nTo
- nFrom
;
3903 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3905 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3907 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3908 ME_CommitUndo(editor
);
3909 ME_UpdateRepaint(editor
, TRUE
);
3913 case WM_GETTEXTLENGTH
:
3915 GETTEXTLENGTHEX how
;
3917 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3918 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3919 how
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3920 return ME_GetTextLengthEx(editor
, &how
);
3922 case EM_GETTEXTLENGTHEX
:
3923 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3927 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3928 ex
.flags
= GT_USECRLF
;
3929 ex
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3930 ex
.lpDefaultChar
= NULL
;
3931 ex
.lpUsedDefChar
= NULL
;
3932 return ME_GetTextEx(editor
, &ex
, lParam
);
3935 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3938 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3939 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3940 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3941 nTo
- nFrom
, unicode
);
3943 case EM_GETSCROLLPOS
:
3945 POINT
*point
= (POINT
*)lParam
;
3946 point
->x
= editor
->horz_si
.nPos
;
3947 point
->y
= editor
->vert_si
.nPos
;
3948 /* 16-bit scaled value is returned as stored in scrollinfo */
3949 if (editor
->horz_si
.nMax
> 0xffff)
3950 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3951 if (editor
->vert_si
.nMax
> 0xffff)
3952 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3955 case EM_GETTEXTRANGE
:
3957 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3959 int nStart
= rng
->chrg
.cpMin
;
3960 int nEnd
= rng
->chrg
.cpMax
;
3961 int textlength
= ME_GetTextLength(editor
);
3963 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3964 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3965 if (nStart
< 0) return 0;
3966 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3968 if (nStart
>= nEnd
) return 0;
3970 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3971 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3975 ME_DisplayItem
*run
;
3976 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3977 unsigned int nCharsLeft
= nMaxChars
;
3978 char *dest
= (char *) lParam
;
3979 BOOL wroteNull
= FALSE
;
3981 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3982 unicode
? "Unicode" : "Ansi");
3984 run
= ME_FindRowWithNumber(editor
, wParam
);
3988 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3989 && run
->type
== diRun
)
3991 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3994 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3997 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3999 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
4000 nCharsLeft
, NULL
, NULL
);
4001 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
4002 nCharsLeft
-= nCopy
;
4005 /* append line termination, space allowing */
4009 *((WCHAR
*)dest
) = '\0';
4016 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
4017 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
4019 case EM_GETLINECOUNT
:
4021 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
4024 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
4026 while (item
!= editor
->pBuffer
->pLast
)
4028 assert(item
->type
== diParagraph
);
4029 prev_para
= ME_FindItemBack(item
, diRun
);
4031 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
4033 nRows
+= item
->member
.para
.nRows
;
4034 item
= item
->member
.para
.next_para
;
4036 last_para
= ME_FindItemBack(item
, diRun
);
4038 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
4039 if (editor
->bEmulateVersion10
&& prev_para
&&
4040 last_para
->member
.run
.nCharOfs
== 0 &&
4041 prev_para
->member
.run
.len
== 1 &&
4042 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
4044 /* In 1.0 emulation, the last solitary \r at the very end of the text
4045 (if one exists) is NOT a line break.
4046 FIXME: this is an ugly hack. This should have a more regular model. */
4050 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
4051 return max(1, nRows
);
4053 case EM_LINEFROMCHAR
:
4056 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
4058 return ME_RowNumberFromCharOfs(editor
, wParam
);
4060 case EM_EXLINEFROMCHAR
:
4063 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
4065 return ME_RowNumberFromCharOfs(editor
, lParam
);
4069 ME_DisplayItem
*item
, *para
;
4073 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
4075 item
= ME_FindRowWithNumber(editor
, wParam
);
4078 para
= ME_GetParagraph(item
);
4079 item
= ME_FindItemFwd(item
, diRun
);
4080 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
4081 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
4086 ME_DisplayItem
*item
, *item_end
;
4087 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
4088 ME_DisplayItem
*para
, *run
;
4090 if (wParam
> ME_GetTextLength(editor
))
4094 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
4097 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
4098 item
= ME_RowStart(run
);
4099 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
4100 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
4101 if (item_end
->type
== diStartRow
) {
4102 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
4104 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
4105 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
4106 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
4108 nChars
= nNextLineOfs
- nThisLineOfs
;
4109 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
4112 case EM_EXLIMITTEXT
:
4114 if ((int)lParam
< 0)
4117 editor
->nTextLimit
= 65536;
4119 editor
->nTextLimit
= (int) lParam
;
4125 editor
->nTextLimit
= 65536;
4127 editor
->nTextLimit
= (int) wParam
;
4130 case EM_GETLIMITTEXT
:
4132 return editor
->nTextLimit
;
4138 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
4139 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
4142 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
4143 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
4144 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
4147 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
4148 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4156 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
4157 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
4160 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
4161 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
4162 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
4165 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4166 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4172 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
4173 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4175 case EM_FINDTEXTEXW
:
4177 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4178 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4181 if (!wParam
|| !lParam
)
4183 *(int *)wParam
= editor
->nZoomNumerator
;
4184 *(int *)lParam
= editor
->nZoomDenominator
;
4187 return ME_SetZoom(editor
, wParam
, lParam
);
4188 case EM_CHARFROMPOS
:
4191 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
4193 return ME_GetCursorOfs(&cursor
);
4197 case EM_POSFROMCHAR
:
4199 ME_DisplayItem
*pPara
, *pRun
;
4200 int nCharOfs
, nOffset
, nLength
;
4204 /* detect which API version we're dealing with */
4205 if (wParam
>= 0x40000)
4207 nLength
= ME_GetTextLength(editor
);
4208 nCharOfs
= min(nCharOfs
, nLength
);
4209 nCharOfs
= max(nCharOfs
, 0);
4211 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
4212 assert(pRun
->type
== diRun
);
4213 pt
.y
= pRun
->member
.run
.pt
.y
;
4214 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
4215 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
4216 pt
.x
+= editor
->rcFormat
.left
;
4218 pt
.x
-= editor
->horz_si
.nPos
;
4219 pt
.y
-= editor
->vert_si
.nPos
;
4221 if (wParam
>= 0x40000) {
4222 *(POINTL
*)wParam
= pt
;
4224 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
4227 return ME_WmCreate(editor
, lParam
, unicode
);
4229 ME_DestroyEditor(editor
);
4234 if (wParam
== (WPARAM
)editor
->hWnd
&& GetCursorPos(&cursor_pos
) &&
4235 ScreenToClient(editor
->hWnd
, &cursor_pos
))
4236 ME_LinkNotify(editor
, msg
, 0, MAKELPARAM(cursor_pos
.x
, cursor_pos
.y
));
4237 return ME_SetCursor(editor
);
4239 case WM_LBUTTONDBLCLK
:
4240 case WM_LBUTTONDOWN
:
4242 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4243 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4244 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4246 ITextHost_TxSetFocus(editor
->texthost
);
4247 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
4248 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
4249 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
4250 editor
->bMouseCaptured
= TRUE
;
4251 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4252 if (!ME_SetCursor(editor
)) goto do_default
;
4256 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4257 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4259 if (editor
->bMouseCaptured
)
4260 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4262 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4263 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4264 if (editor
->bMouseCaptured
)
4265 ME_SetCursor(editor
);
4268 if (editor
->bMouseCaptured
) {
4269 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4270 editor
->bMouseCaptured
= FALSE
;
4272 if (editor
->nSelectionType
== stDocument
)
4273 editor
->nSelectionType
= stPosition
;
4274 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4275 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4279 ME_SetCursor(editor
);
4280 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4284 case WM_RBUTTONDOWN
:
4285 case WM_RBUTTONDBLCLK
:
4286 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4287 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4288 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4290 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4292 case WM_CONTEXTMENU
:
4293 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4297 editor
->bHaveFocus
= TRUE
;
4298 ME_ShowCaret(editor
);
4299 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4302 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4303 editor
->bHaveFocus
= FALSE
;
4304 editor
->wheel_remain
= 0;
4305 ME_HideCaret(editor
);
4306 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4309 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4312 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4313 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4317 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4318 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4320 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4324 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4325 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4327 return ME_Char(editor
, wParam
, lParam
, unicode
);
4331 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4332 if(wParam
<= 0x000fffff)
4334 if(wParam
> 0xffff) /* convert to surrogates */
4337 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4338 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4340 ME_Char(editor
, wParam
, 0, TRUE
);
4346 case EM_STOPGROUPTYPING
:
4347 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4351 const int scrollUnit
= 7;
4353 switch(LOWORD(wParam
))
4356 ME_ScrollAbs(editor
, 0, 0);
4359 ME_ScrollAbs(editor
,
4360 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4361 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4364 ME_ScrollLeft(editor
, scrollUnit
);
4367 ME_ScrollRight(editor
, scrollUnit
);
4370 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4373 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4376 case SB_THUMBPOSITION
:
4378 int pos
= HIWORD(wParam
);
4379 if (editor
->horz_si
.nMax
> 0xffff)
4380 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4381 ME_HScrollAbs(editor
, pos
);
4387 case EM_SCROLL
: /* fall through */
4391 int lineHeight
= get_default_line_height( editor
);
4393 origNPos
= editor
->vert_si
.nPos
;
4395 switch(LOWORD(wParam
))
4398 ME_ScrollAbs(editor
, 0, 0);
4401 ME_ScrollAbs(editor
,
4402 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4403 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4406 ME_ScrollUp(editor
,lineHeight
);
4409 ME_ScrollDown(editor
,lineHeight
);
4412 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4415 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4418 case SB_THUMBPOSITION
:
4420 int pos
= HIWORD(wParam
);
4421 if (editor
->vert_si
.nMax
> 0xffff)
4422 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4423 ME_VScrollAbs(editor
, pos
);
4427 if (msg
== EM_SCROLL
)
4428 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4436 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4437 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4440 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4442 delta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4444 /* if scrolling changes direction, ignore left overs */
4445 if ((delta
< 0 && editor
->wheel_remain
< 0) ||
4446 (delta
> 0 && editor
->wheel_remain
> 0))
4447 editor
->wheel_remain
+= delta
;
4449 editor
->wheel_remain
= delta
;
4451 if (editor
->wheel_remain
)
4455 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4459 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4461 numerator
+= calc_wheel_change( &editor
->wheel_remain
, 10 );
4462 if (numerator
>= 10 && numerator
<= 500)
4463 ME_SetZoom(editor
, numerator
, 100);
4468 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES
, 0, &max_lines
, 0 );
4470 lines
= calc_wheel_change( &editor
->wheel_remain
, (int)max_lines
);
4472 ME_ScrollDown( editor
, -lines
* get_default_line_height( editor
) );
4479 *((RECT
*)lParam
) = editor
->rcFormat
;
4480 if (editor
->bDefaultFormatRect
)
4481 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4491 RECT
*rc
= (RECT
*)lParam
;
4493 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4494 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4497 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4498 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4499 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4500 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4501 } else if (wParam
== 1) {
4502 /* MSDN incorrectly says a wParam value of 1 causes the
4503 * lParam rect to be used as a relative offset,
4504 * however, the tests show it just prevents min/max bound
4506 editor
->rcFormat
.top
= rc
->top
- border
;
4507 editor
->rcFormat
.left
= rc
->left
- border
;
4508 editor
->rcFormat
.bottom
= rc
->bottom
;
4509 editor
->rcFormat
.right
= rc
->right
+ border
;
4513 editor
->bDefaultFormatRect
= FALSE
;
4517 ME_SetDefaultFormatRect(editor
);
4518 editor
->bDefaultFormatRect
= TRUE
;
4520 ME_MarkAllForWrapping(editor
);
4521 ME_WrapMarkedParagraphs(editor
);
4522 ME_UpdateScrollBar(editor
);
4523 if (msg
!= EM_SETRECTNP
)
4527 case EM_REQUESTRESIZE
:
4528 ME_SendRequestResize(editor
, TRUE
);
4532 case WM_WINDOWPOSCHANGED
:
4535 WINDOWPOS
*winpos
= (WINDOWPOS
*)lParam
;
4537 if (winpos
->flags
& SWP_NOCLIENTSIZE
) goto do_default
;
4538 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4539 if (editor
->bDefaultFormatRect
) {
4540 ME_SetDefaultFormatRect(editor
);
4542 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4543 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4545 editor
->prevClientRect
= clientRect
;
4546 ME_RewrapRepaint(editor
);
4549 /* IME messages to make richedit controls IME aware */
4550 case WM_IME_SETCONTEXT
:
4551 case WM_IME_CONTROL
:
4553 case WM_IME_COMPOSITIONFULL
:
4555 case WM_IME_STARTCOMPOSITION
:
4557 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4558 ME_DeleteSelection(editor
);
4559 ME_CommitUndo(editor
);
4560 ME_UpdateRepaint(editor
, FALSE
);
4563 case WM_IME_COMPOSITION
:
4567 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4568 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4569 ME_DeleteSelection(editor
);
4570 ME_SaveTempStyle(editor
);
4571 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4573 LPWSTR lpCompStr
= NULL
;
4575 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4577 dwIndex
= GCS_COMPSTR
;
4579 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4580 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4581 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4582 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4583 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4584 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4586 if (dwIndex
== GCS_COMPSTR
)
4587 ME_SetSelection(editor
,editor
->imeStartIndex
,
4588 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4590 ME_ReleaseStyle(style
);
4591 ME_CommitUndo(editor
);
4592 ME_UpdateRepaint(editor
, FALSE
);
4595 case WM_IME_ENDCOMPOSITION
:
4597 ME_DeleteSelection(editor
);
4598 editor
->imeStartIndex
=-1;
4601 case EM_GETOLEINTERFACE
:
4604 if (!CreateIRichEditOle(NULL
, editor
, (LPVOID
*)&editor
->reOle
))
4606 *(LPVOID
*)lParam
= editor
->reOle
;
4607 IRichEditOle_AddRef(editor
->reOle
);
4610 case EM_GETPASSWORDCHAR
:
4612 return editor
->cPasswordMask
;
4614 case EM_SETOLECALLBACK
:
4615 if(editor
->lpOleCallback
)
4616 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4617 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4618 if(editor
->lpOleCallback
)
4619 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4621 case EM_GETWORDBREAKPROC
:
4622 return (LRESULT
)editor
->pfnWordBreak
;
4623 case EM_SETWORDBREAKPROC
:
4625 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4627 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4628 return (LRESULT
)pfnOld
;
4630 case EM_GETTEXTMODE
:
4631 return editor
->mode
;
4632 case EM_SETTEXTMODE
:
4637 if (ME_GetTextLength(editor
) ||
4638 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4639 return E_UNEXPECTED
;
4641 /* Check for mutually exclusive flags in adjacent bits of wParam */
4642 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4643 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4644 return E_INVALIDARG
;
4646 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4648 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4649 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4650 if (wParam
& TM_PLAINTEXT
) {
4651 /* Clear selection since it should be possible to select the
4652 * end of text run for rich text */
4653 ME_InvalidateSelection(editor
);
4654 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4655 editor
->pCursors
[1] = editor
->pCursors
[0];
4656 /* plain text can only have the default style. */
4657 ME_ClearTempStyle(editor
);
4658 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4659 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4660 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4663 /* FIXME: Currently no support for undo level and code page options */
4664 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4667 case EM_SETPASSWORDCHAR
:
4669 editor
->cPasswordMask
= wParam
;
4670 ME_RewrapRepaint(editor
);
4673 case EM_SETTARGETDEVICE
:
4676 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4677 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4679 editor
->bWordWrap
= new;
4680 editor
->nAvailWidth
= 0; /* wrap to client area */
4681 ME_RewrapRepaint(editor
);
4684 int width
= max(0, lParam
);
4685 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4686 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4688 editor
->nAvailWidth
= width
;
4689 editor
->bWordWrap
= TRUE
;
4690 ME_RewrapRepaint(editor
);
4692 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4697 *phresult
= S_FALSE
;
4703 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4704 LPARAM lParam
, BOOL unicode
)
4706 ME_TextEditor
*editor
;
4708 LRESULT lresult
= 0;
4710 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4711 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4713 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4716 if (msg
== WM_NCCREATE
)
4718 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4719 ITextHost
*texthost
;
4721 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4722 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4723 return texthost
!= NULL
;
4727 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4739 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4740 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4741 ME_SendOldNotify(editor
, EN_UPDATE
);
4742 /* Erase area outside of the formatting rectangle */
4743 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4746 rc
.bottom
= editor
->rcFormat
.top
;
4747 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4748 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4750 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4752 rc
.top
= editor
->rcFormat
.bottom
;
4753 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4754 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4756 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4758 rc
.right
= editor
->rcFormat
.left
;
4759 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4760 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4762 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4764 rc
.left
= editor
->rcFormat
.right
;
4765 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4766 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4769 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4770 EndPaint(editor
->hWnd
, &ps
);
4775 HDC hDC
= (HDC
)wParam
;
4778 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4779 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4785 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4786 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4788 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4789 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4790 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4791 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4794 case EM_SETREADONLY
:
4797 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4798 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4799 dwStyle
&= ~ES_READONLY
;
4801 dwStyle
|= ES_READONLY
;
4802 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4806 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4809 if (hresult
== S_FALSE
)
4810 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4812 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4813 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4818 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4820 BOOL unicode
= TRUE
;
4822 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4823 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4826 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4829 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4831 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4834 /******************************************************************
4835 * RichEditANSIWndProc (RICHED20.10)
4837 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4839 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4842 /******************************************************************
4843 * RichEdit10ANSIWndProc (RICHED20.9)
4845 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4847 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4849 ITextHost
*texthost
;
4850 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4852 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4853 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4854 return texthost
!= NULL
;
4856 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4859 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4861 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4864 /* Fill buffer with srcChars unicode characters from the start cursor.
4866 * buffer: destination buffer
4867 * buflen: length of buffer in characters excluding the NULL terminator.
4868 * start: start of editor text to copy into buffer.
4869 * srcChars: Number of characters to use from the editor text.
4870 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4872 * returns the number of characters written excluding the NULL terminator.
4874 * The written text is always NULL terminated.
4876 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4877 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
,
4880 ME_DisplayItem
*pRun
, *pNextRun
;
4881 const WCHAR
*pStart
= buffer
;
4882 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4886 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4887 if (editor
->bEmulateVersion10
) bCRLF
= FALSE
;
4891 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4893 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4894 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4896 while (srcChars
&& buflen
&& pNextRun
)
4898 int nFlags
= pRun
->member
.run
.nFlags
;
4900 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4902 if (buflen
== 1) break;
4903 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4904 * EM_GETTEXTEX, however, this is done for copying text which
4905 * also uses this function. */
4906 srcChars
-= min(nLen
, srcChars
);
4910 nLen
= min(nLen
, srcChars
);
4914 nLen
= min(nLen
, buflen
);
4917 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4922 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4924 nLen
= pRun
->member
.run
.len
;
4925 str
= get_text( &pRun
->member
.run
, 0 );
4927 /* append '\r' to the last paragraph. */
4928 if (pRun
->next
->type
== diTextEnd
&& bEOP
)
4934 return buffer
- pStart
;
4937 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4942 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4943 wcW
.lpfnWndProc
= RichEditWndProcW
;
4945 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4946 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4948 wcW
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_IBEAM
);
4949 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4950 wcW
.lpszMenuName
= NULL
;
4952 if (is_version_nt())
4954 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4955 if (!RegisterClassW(&wcW
)) return FALSE
;
4956 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4957 if (!RegisterClassW(&wcW
)) return FALSE
;
4961 /* WNDCLASSA/W have the same layout */
4962 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4963 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4964 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4965 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4968 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4969 wcA
.lpfnWndProc
= RichEditWndProcA
;
4971 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4972 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4974 wcA
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_IBEAM
);
4975 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4976 wcA
.lpszMenuName
= NULL
;
4977 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4978 if (!RegisterClassA(&wcA
)) return FALSE
;
4979 wcA
.lpszClassName
= "RichEdit50A";
4980 if (!RegisterClassA(&wcA
)) return FALSE
;
4985 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4986 /* FIXME: Not implemented */
4987 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4988 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4989 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4992 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4993 /* FIXME: Not implemented */
4994 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4995 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4996 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4999 /******************************************************************
5000 * REExtendedRegisterClass (RICHED20.8)
5002 * FIXME undocumented
5003 * Need to check for errors and implement controls and callbacks
5005 LRESULT WINAPI
REExtendedRegisterClass(void)
5010 FIXME("semi stub\n");
5014 wcW
.hInstance
= NULL
;
5017 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
5018 wcW
.lpszMenuName
= NULL
;
5020 if (!ME_ListBoxRegistered
)
5022 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
5023 wcW
.lpfnWndProc
= REListWndProc
;
5024 wcW
.lpszClassName
= REListBox20W
;
5025 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
5028 if (!ME_ComboBoxRegistered
)
5030 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
5031 wcW
.lpfnWndProc
= REComboWndProc
;
5032 wcW
.lpszClassName
= REComboBox20W
;
5033 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
5037 if (ME_ListBoxRegistered
)
5039 if (ME_ComboBoxRegistered
)
5045 static BOOL
isurlspecial(WCHAR c
)
5047 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
5048 return strchrW( special_chars
, c
) != NULL
;
5052 * This proc takes a selection, and scans it forward in order to select the span
5053 * of a possible URL candidate. A possible URL candidate must start with isalnum
5054 * or one of the following special characters: *|/\+%#@ and must consist entirely
5055 * of the characters allowed to start the URL, plus : (colon) which may occur
5056 * at most once, and not at either end.
5058 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
5059 const ME_Cursor
*start
,
5061 ME_Cursor
*candidate_min
,
5062 ME_Cursor
*candidate_max
)
5064 ME_Cursor cursor
= *start
;
5065 BOOL foundColon
= FALSE
;
5066 BOOL candidateStarted
= FALSE
;
5067 WCHAR lastAcceptedChar
= '\0';
5071 WCHAR
*strStart
= get_text( &cursor
.pRun
->member
.run
, 0 );
5072 WCHAR
*str
= strStart
+ cursor
.nOffset
;
5073 int nLen
= cursor
.pRun
->member
.run
.len
- cursor
.nOffset
;
5076 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
5078 /* Find start of candidate */
5079 if (!candidateStarted
)
5084 if (isalnumW(*str
) || isurlspecial(*str
))
5086 cursor
.nOffset
= str
- strStart
;
5087 *candidate_min
= cursor
;
5088 candidateStarted
= TRUE
;
5089 lastAcceptedChar
= *str
++;
5096 /* Find end of candidate */
5097 if (candidateStarted
) {
5101 if (*str
== ':' && !foundColon
) {
5103 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
5104 cursor
.nOffset
= str
- strStart
;
5105 if (lastAcceptedChar
== ':')
5106 ME_MoveCursorChars(editor
, &cursor
, -1);
5107 *candidate_max
= cursor
;
5110 lastAcceptedChar
= *str
++;
5114 /* End of paragraph: skip it if before candidate span, or terminates
5115 current active span */
5116 if (candidateStarted
) {
5117 if (lastAcceptedChar
== ':')
5118 ME_MoveCursorChars(editor
, &cursor
, -1);
5119 *candidate_max
= cursor
;
5124 /* Reaching this point means no span was found, so get next span */
5125 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
5126 if (candidateStarted
) {
5127 /* There are no further runs, so take end of text as end of candidate */
5128 cursor
.nOffset
= str
- strStart
;
5129 if (lastAcceptedChar
== ':')
5130 ME_MoveCursorChars(editor
, &cursor
, -1);
5131 *candidate_max
= cursor
;
5134 *candidate_max
= *candidate_min
= cursor
;
5140 if (candidateStarted
) {
5141 /* There are no further runs, so take end of text as end of candidate */
5142 if (lastAcceptedChar
== ':')
5143 ME_MoveCursorChars(editor
, &cursor
, -1);
5144 *candidate_max
= cursor
;
5147 *candidate_max
= *candidate_min
= cursor
;
5152 * This proc evaluates the selection and returns TRUE if it can be considered an URL
5154 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
5156 #define MAX_PREFIX_LEN 9
5158 const WCHAR text
[MAX_PREFIX_LEN
];
5161 {{'p','r','o','s','p','e','r','o',':'}, 9},
5162 {{'t','e','l','n','e','t',':'}, 7},
5163 {{'g','o','p','h','e','r',':'}, 7},
5164 {{'m','a','i','l','t','o',':'}, 7},
5165 {{'h','t','t','p','s',':'}, 6},
5166 {{'f','i','l','e',':'}, 5},
5167 {{'n','e','w','s',':'}, 5},
5168 {{'w','a','i','s',':'}, 5},
5169 {{'n','n','t','p',':'}, 5},
5170 {{'h','t','t','p',':'}, 5},
5171 {{'w','w','w','.'}, 4},
5172 {{'f','t','p',':'}, 4},
5174 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
5177 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, FALSE
, FALSE
);
5178 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
5180 if (nChars
< prefixes
[i
].length
) continue;
5181 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
5185 #undef MAX_PREFIX_LEN
5189 * This proc walks through the indicated selection and evaluates whether each
5190 * section identified by ME_FindNextURLCandidate and in-between sections have
5191 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5192 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5194 * Since this function can cause runs to be split, do not depend on the value
5195 * of the start cursor at the end of the function.
5197 * nChars may be set to INT_MAX to update to the end of the text.
5199 * Returns TRUE if at least one section was modified.
5201 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
5203 BOOL modified
= FALSE
;
5204 ME_Cursor startCur
= *start
;
5206 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
5211 ME_Cursor candidateStart
, candidateEnd
;
5213 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
5214 &candidateStart
, &candidateEnd
))
5216 /* Section before candidate is not an URL */
5217 int cMin
= ME_GetCursorOfs(&candidateStart
);
5218 int cMax
= ME_GetCursorOfs(&candidateEnd
);
5220 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
5221 candidateStart
= candidateEnd
;
5222 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
5226 /* No more candidates until end of selection */
5230 if (startCur
.pRun
!= candidateStart
.pRun
||
5231 startCur
.nOffset
!= candidateStart
.nOffset
)
5233 /* CFE_LINK effect should be consistently unset */
5234 link
.cbSize
= sizeof(link
);
5235 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5236 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
5238 /* CFE_LINK must be unset from this range */
5239 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5240 link
.cbSize
= sizeof(link
);
5241 link
.dwMask
= CFM_LINK
;
5243 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5244 /* Update candidateEnd since setting character formats may split
5245 * runs, which can cause a cursor to be at an invalid offset within
5247 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
5249 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
5250 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
5255 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
5256 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
5258 /* CFE_LINK effect should be consistently set */
5259 link
.cbSize
= sizeof(link
);
5260 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5261 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
5263 /* CFE_LINK must be set on this range */
5264 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5265 link
.cbSize
= sizeof(link
);
5266 link
.dwMask
= CFM_LINK
;
5267 link
.dwEffects
= CFE_LINK
;
5268 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5272 startCur
= candidateEnd
;
5273 } while (nChars
> 0);