2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
54 - EM_GETLANGOPTIONS 2.0
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
67 + EM_GETSELTEXT (ANSI&Unicode)
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
94 - EM_SETBIDIOPTIONS 3.0
96 + EM_SETCHARFORMAT (partly done, no ANSI)
98 + EM_SETEVENTMASK (few notifications supported)
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
103 - EM_SETLANGOPTIONS 2.0
106 + EM_SETMODIFY (not sure if implementation is correct)
108 + EM_SETOPTIONS (partially implemented)
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
115 + EM_SETRECTNP (EM_SETRECT without repainting)
117 + EM_SETSCROLLPOS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
151 * EN_CHANGE (sent from the wrong place)
168 * EN_UPDATE (sent from the wrong place)
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
179 - ES_MULTILINE (currently single line controls aren't supported)
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
194 * RICHED20 TODO (incomplete):
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
214 * - ListBox & ComboBox not implemented
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
234 #define STACK_SIZE_DEFAULT 100
235 #define STACK_SIZE_MAX 1000
237 #define TEXT_LIMIT_DEFAULT 32767
239 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
241 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
242 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
244 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
245 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
246 static HCURSOR hLeft
;
248 BOOL me_debug
= FALSE
;
249 HANDLE me_heap
= NULL
;
251 static BOOL ME_ListBoxRegistered
= FALSE
;
252 static BOOL ME_ComboBoxRegistered
= FALSE
;
254 static inline BOOL
is_version_nt(void)
256 return !(GetVersion() & 0x80000000);
259 static ME_TextBuffer
*ME_MakeText(void) {
261 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
263 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
264 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
270 p1
->member
.para
.next_para
= p2
;
271 p2
->member
.para
.prev_para
= p1
;
272 p2
->member
.para
.nCharOfs
= 0;
276 buf
->pCharStyle
= NULL
;
282 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
285 LRESULT total_bytes_read
= 0;
286 BOOL is_read
= FALSE
;
287 DWORD cp
= CP_ACP
, copy
= 0;
288 char conv_buf
[4 + STREAMIN_BUFFER_SIZE
]; /* up to 4 additional UTF-8 bytes */
290 static const char bom_utf8
[] = {0xEF, 0xBB, 0xBF};
292 TRACE("%08x %p\n", dwFormat
, stream
);
296 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
300 ME_StreamInFill(stream
);
301 if (stream
->editstream
->dwError
)
305 total_bytes_read
+= stream
->dwSize
;
308 if (!(dwFormat
& SF_UNICODE
))
310 char * buf
= stream
->buffer
;
311 DWORD size
= stream
->dwSize
, end
;
316 if (stream
->dwSize
>= 3 && !memcmp(stream
->buffer
, bom_utf8
, 3))
328 memcpy(conv_buf
+ copy
, buf
, size
);
333 while ((buf
[end
-1] & 0xC0) == 0x80)
336 --total_bytes_read
; /* strange, but seems to match windows */
338 if (buf
[end
-1] & 0x80)
341 if ((buf
[end
-1] & 0xE0) == 0xC0)
343 if ((buf
[end
-1] & 0xF0) == 0xE0)
345 if ((buf
[end
-1] & 0xF8) == 0xF0)
348 if (size
- end
>= need
)
350 /* we have enough bytes for this sequence */
355 /* need more bytes, so don't transcode this sequence */
363 nWideChars
= MultiByteToWideChar(cp
, 0, buf
, end
, wszText
, STREAMIN_BUFFER_SIZE
);
370 memcpy(conv_buf
, buf
+ end
, size
- end
);
377 nWideChars
= stream
->dwSize
>> 1;
378 pText
= (WCHAR
*)stream
->buffer
;
381 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
382 if (stream
->dwSize
== 0)
386 return total_bytes_read
;
389 static void ME_ApplyBorderProperties(RTF_Info
*info
,
390 ME_BorderRect
*borderRect
,
391 RTFBorder
*borderDef
)
394 ME_Border
*pBorders
[] = {&borderRect
->top
,
398 for (i
= 0; i
< 4; i
++)
400 RTFColor
*colorDef
= info
->colorList
;
401 pBorders
[i
]->width
= borderDef
[i
].width
;
402 colorNum
= borderDef
[i
].color
;
403 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
404 colorDef
= colorDef
->rtfNextColor
;
406 pBorders
[i
]->colorRef
= RGB(
407 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
408 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
409 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
411 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
415 void ME_RTFCharAttrHook(RTF_Info
*info
)
418 fmt
.cbSize
= sizeof(fmt
);
422 switch(info
->rtfMinor
)
425 /* FIXME add more flags once they're implemented */
426 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
427 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
428 fmt
.yHeight
= 12*20; /* 12pt */
429 fmt
.wWeight
= FW_NORMAL
;
430 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
433 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
434 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
435 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
438 fmt
.dwMask
= CFM_ITALIC
;
439 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
442 fmt
.dwMask
= CFM_UNDERLINETYPE
;
443 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
445 case rtfDotUnderline
:
446 fmt
.dwMask
= CFM_UNDERLINETYPE
;
447 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
450 fmt
.dwMask
= CFM_UNDERLINETYPE
;
451 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
453 case rtfWordUnderline
:
454 fmt
.dwMask
= CFM_UNDERLINETYPE
;
455 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
458 fmt
.dwMask
= CFM_UNDERLINETYPE
;
459 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
462 fmt
.dwMask
= CFM_STRIKEOUT
;
463 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
467 case rtfSubScrShrink
:
468 case rtfSuperScrShrink
:
470 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
471 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
472 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
473 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
476 fmt
.dwMask
= CFM_HIDDEN
;
477 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
480 fmt
.dwMask
= CFM_BACKCOLOR
;
482 if (info
->rtfParam
== 0)
483 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
484 else if (info
->rtfParam
!= rtfNoParam
)
486 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
487 if (c
&& c
->rtfCBlue
>= 0)
488 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
490 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
494 fmt
.dwMask
= CFM_COLOR
;
496 if (info
->rtfParam
== 0)
497 fmt
.dwEffects
= CFE_AUTOCOLOR
;
498 else if (info
->rtfParam
!= rtfNoParam
)
500 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
501 if (c
&& c
->rtfCBlue
>= 0)
502 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
504 fmt
.dwEffects
= CFE_AUTOCOLOR
;
509 if (info
->rtfParam
!= rtfNoParam
)
511 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
514 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
515 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
516 fmt
.bCharSet
= f
->rtfFCharSet
;
517 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
518 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
523 fmt
.dwMask
= CFM_SIZE
;
524 if (info
->rtfParam
!= rtfNoParam
)
525 fmt
.yHeight
= info
->rtfParam
*10;
530 RTFFlushOutputBuffer(info
);
531 /* FIXME too slow ? how come ? */
532 style2
= ME_ApplyStyle(info
->style
, &fmt
);
533 ME_ReleaseStyle(info
->style
);
534 info
->style
= style2
;
535 info
->styleChanged
= TRUE
;
539 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
540 the same tags mean different things in different contexts */
541 void ME_RTFParAttrHook(RTF_Info
*info
)
544 fmt
.cbSize
= sizeof(fmt
);
547 switch(info
->rtfMinor
)
549 case rtfParDef
: /* restores default paragraph attributes */
550 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
551 info
->borderType
= RTFBorderParaLeft
;
552 else /* v1.0 - 3.0 */
553 info
->borderType
= RTFBorderParaTop
;
554 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
555 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
557 /* TODO: numbering, shading */
558 fmt
.wAlignment
= PFA_LEFT
;
560 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
561 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
562 fmt
.wBorderSpace
= 0;
563 fmt
.bLineSpacingRule
= 0;
564 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
565 fmt
.dyLineSpacing
= 0;
566 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
568 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
569 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
572 ME_DisplayItem
*para
;
573 /* We are just after a table row. */
574 RTFFlushOutputBuffer(info
);
575 cursor
= info
->editor
->pCursors
[0];
577 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
578 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
580 /* Since the table row end, no text has been inserted, and the \intbl
581 * control word has not be used. We can confirm that we are not in a
584 info
->tableDef
->tableRowStart
= NULL
;
585 info
->canInheritInTbl
= FALSE
;
588 } else { /* v1.0 - v3.0 */
589 fmt
.dwMask
|= PFM_TABLE
;
590 fmt
.wEffects
&= ~PFE_TABLE
;
594 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
596 while (info
->rtfParam
> info
->nestingLevel
) {
597 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
598 ZeroMemory(tableDef
, sizeof(RTFTable
));
599 tableDef
->parent
= info
->tableDef
;
600 info
->tableDef
= tableDef
;
602 RTFFlushOutputBuffer(info
);
603 if (tableDef
->tableRowStart
&&
604 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
606 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
607 para
= para
->member
.para
.next_para
;
608 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
609 tableDef
->tableRowStart
= para
;
613 cursor
= info
->editor
->pCursors
[0];
614 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
615 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
616 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
619 info
->nestingLevel
++;
621 info
->canInheritInTbl
= FALSE
;
626 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
628 if (info
->nestingLevel
< 1)
633 info
->tableDef
= ALLOC_OBJ(RTFTable
);
634 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
636 tableDef
= info
->tableDef
;
637 RTFFlushOutputBuffer(info
);
638 if (tableDef
->tableRowStart
&&
639 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
641 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
642 para
= para
->member
.para
.next_para
;
643 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
644 tableDef
->tableRowStart
= para
;
648 cursor
= info
->editor
->pCursors
[0];
649 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
650 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
651 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
653 info
->nestingLevel
= 1;
654 info
->canInheritInTbl
= TRUE
;
657 } else { /* v1.0 - v3.0 */
658 fmt
.dwMask
|= PFM_TABLE
;
659 fmt
.wEffects
|= PFE_TABLE
;
664 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
665 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
666 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
667 fmt
.dxOffset
= -info
->rtfParam
;
670 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
671 fmt
.dwMask
= PFM_STARTINDENT
;
672 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
675 fmt
.dwMask
= PFM_RIGHTINDENT
;
676 fmt
.dxRightIndent
= info
->rtfParam
;
680 fmt
.dwMask
= PFM_ALIGNMENT
;
681 fmt
.wAlignment
= PFA_LEFT
;
684 fmt
.dwMask
= PFM_ALIGNMENT
;
685 fmt
.wAlignment
= PFA_RIGHT
;
688 fmt
.dwMask
= PFM_ALIGNMENT
;
689 fmt
.wAlignment
= PFA_CENTER
;
692 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
693 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
697 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
698 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
699 fmt
.dwMask
= PFM_TABSTOPS
;
702 fmt
.dwMask
= PFM_KEEP
;
703 fmt
.wEffects
= PFE_KEEP
;
705 case rtfNoWidowControl
:
706 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
707 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
710 fmt
.dwMask
= PFM_KEEPNEXT
;
711 fmt
.wEffects
= PFE_KEEPNEXT
;
714 fmt
.dwMask
= PFM_SPACEAFTER
;
715 fmt
.dySpaceAfter
= info
->rtfParam
;
718 fmt
.dwMask
= PFM_SPACEBEFORE
;
719 fmt
.dySpaceBefore
= info
->rtfParam
;
721 case rtfSpaceBetween
:
722 fmt
.dwMask
= PFM_LINESPACING
;
723 if ((int)info
->rtfParam
> 0)
725 fmt
.dyLineSpacing
= info
->rtfParam
;
726 fmt
.bLineSpacingRule
= 3;
730 fmt
.dyLineSpacing
= info
->rtfParam
;
731 fmt
.bLineSpacingRule
= 4;
734 case rtfSpaceMultiply
:
735 fmt
.dwMask
= PFM_LINESPACING
;
736 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
737 fmt
.bLineSpacingRule
= 5;
740 fmt
.dwMask
= PFM_NUMBERING
;
741 fmt
.wNumbering
= PFN_BULLET
;
744 fmt
.dwMask
= PFM_NUMBERING
;
745 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
747 case rtfParNumDecimal
:
748 fmt
.dwMask
= PFM_NUMBERING
;
749 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
751 case rtfParNumIndent
:
752 fmt
.dwMask
= PFM_NUMBERINGTAB
;
753 fmt
.wNumberingTab
= info
->rtfParam
;
755 case rtfParNumStartAt
:
756 fmt
.dwMask
= PFM_NUMBERINGSTART
;
757 fmt
.wNumberingStart
= info
->rtfParam
;
760 info
->borderType
= RTFBorderParaLeft
;
761 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
762 if (!(fmt
.dwMask
& PFM_BORDER
))
764 fmt
.wBorderSpace
= 0;
765 fmt
.wBorderWidth
= 1;
769 fmt
.dwMask
= PFM_BORDER
;
772 info
->borderType
= RTFBorderParaRight
;
773 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
774 if (!(fmt
.dwMask
& PFM_BORDER
))
776 fmt
.wBorderSpace
= 0;
777 fmt
.wBorderWidth
= 1;
781 fmt
.dwMask
= PFM_BORDER
;
784 info
->borderType
= RTFBorderParaTop
;
785 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
786 if (!(fmt
.dwMask
& PFM_BORDER
))
788 fmt
.wBorderSpace
= 0;
789 fmt
.wBorderWidth
= 1;
793 fmt
.dwMask
= PFM_BORDER
;
795 case rtfBorderBottom
:
796 info
->borderType
= RTFBorderParaBottom
;
797 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
798 if (!(fmt
.dwMask
& PFM_BORDER
))
800 fmt
.wBorderSpace
= 0;
801 fmt
.wBorderWidth
= 1;
805 fmt
.dwMask
= PFM_BORDER
;
807 case rtfBorderSingle
:
808 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
809 /* we assume that borders have been created before (RTF spec) */
810 fmt
.wBorders
&= ~0x700;
811 fmt
.wBorders
|= 1 << 8;
812 fmt
.dwMask
= PFM_BORDER
;
815 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
816 /* we assume that borders have been created before (RTF spec) */
817 fmt
.wBorders
&= ~0x700;
818 fmt
.wBorders
|= 2 << 8;
819 fmt
.dwMask
= PFM_BORDER
;
821 case rtfBorderShadow
:
822 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
823 /* we assume that borders have been created before (RTF spec) */
824 fmt
.wBorders
&= ~0x700;
825 fmt
.wBorders
|= 10 << 8;
826 fmt
.dwMask
= PFM_BORDER
;
828 case rtfBorderDouble
:
829 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
830 /* we assume that borders have been created before (RTF spec) */
831 fmt
.wBorders
&= ~0x700;
832 fmt
.wBorders
|= 7 << 8;
833 fmt
.dwMask
= PFM_BORDER
;
836 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
837 /* we assume that borders have been created before (RTF spec) */
838 fmt
.wBorders
&= ~0x700;
839 fmt
.wBorders
|= 11 << 8;
840 fmt
.dwMask
= PFM_BORDER
;
844 int borderSide
= info
->borderType
& RTFBorderSideMask
;
845 RTFTable
*tableDef
= info
->tableDef
;
846 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
847 /* we assume that borders have been created before (RTF spec) */
848 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
849 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
852 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
854 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
855 border
->width
= info
->rtfParam
;
858 fmt
.dwMask
= PFM_BORDER
;
862 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
863 /* we assume that borders have been created before (RTF spec) */
864 fmt
.wBorderSpace
= info
->rtfParam
;
865 fmt
.dwMask
= PFM_BORDER
;
869 RTFTable
*tableDef
= info
->tableDef
;
870 int borderSide
= info
->borderType
& RTFBorderSideMask
;
871 int borderType
= info
->borderType
& RTFBorderTypeMask
;
874 case RTFBorderTypePara
:
875 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
877 /* v1.0 - 3.0 treat paragraph and row borders the same. */
878 case RTFBorderTypeRow
:
880 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
883 case RTFBorderTypeCell
:
884 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
885 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
892 fmt
.dwMask
= PFM_RTLPARA
;
893 fmt
.wEffects
= PFE_RTLPARA
;
896 fmt
.dwMask
= PFM_RTLPARA
;
901 RTFFlushOutputBuffer(info
);
902 /* FIXME too slow ? how come ?*/
903 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
907 void ME_RTFTblAttrHook(RTF_Info
*info
)
909 switch (info
->rtfMinor
)
913 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
914 info
->borderType
= 0; /* Not sure */
915 else /* v1.0 - 3.0 */
916 info
->borderType
= RTFBorderRowTop
;
917 if (!info
->tableDef
) {
918 info
->tableDef
= ME_MakeTableDef(info
->editor
);
920 ME_InitTableDef(info
->editor
, info
->tableDef
);
929 info
->tableDef
= ME_MakeTableDef(info
->editor
);
931 cellNum
= info
->tableDef
->numCellsDefined
;
932 if (cellNum
>= MAX_TABLE_CELLS
)
934 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
935 if (cellNum
< MAX_TAB_STOPS
) {
936 /* Tab stops were used to store cell positions before v4.1 but v4.1
937 * still seems to set the tabstops without using them. */
938 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
939 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
940 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
941 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
943 info
->tableDef
->numCellsDefined
++;
947 info
->borderType
= RTFBorderRowTop
;
950 info
->borderType
= RTFBorderRowLeft
;
952 case rtfRowBordBottom
:
953 info
->borderType
= RTFBorderRowBottom
;
955 case rtfRowBordRight
:
956 info
->borderType
= RTFBorderRowRight
;
959 info
->borderType
= RTFBorderCellTop
;
961 case rtfCellBordLeft
:
962 info
->borderType
= RTFBorderCellLeft
;
964 case rtfCellBordBottom
:
965 info
->borderType
= RTFBorderCellBottom
;
967 case rtfCellBordRight
:
968 info
->borderType
= RTFBorderCellRight
;
972 info
->tableDef
->gapH
= info
->rtfParam
;
976 info
->tableDef
->leftEdge
= info
->rtfParam
;
981 void ME_RTFSpecialCharHook(RTF_Info
*info
)
983 RTFTable
*tableDef
= info
->tableDef
;
984 switch (info
->rtfMinor
)
987 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
989 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
993 RTFFlushOutputBuffer(info
);
994 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
995 if (tableDef
->tableRowStart
)
997 if (!info
->nestingLevel
&&
998 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1000 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
1001 para
= para
->member
.para
.next_para
;
1002 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1003 tableDef
->tableRowStart
= para
;
1004 info
->nestingLevel
= 1;
1006 ME_InsertTableCellFromCursor(info
->editor
);
1008 } else { /* v1.0 - v3.0 */
1009 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1010 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1011 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
1012 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1015 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1016 tableDef
->numCellsInserted
++;
1021 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
1023 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1026 ME_DisplayItem
*para
, *cell
, *run
;
1031 RTFFlushOutputBuffer(info
);
1032 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
1033 if (!tableDef
->tableRowStart
)
1035 if (!info
->nestingLevel
&&
1036 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1038 para
= tableDef
->tableRowStart
;
1039 para
= para
->member
.para
.next_para
;
1040 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1041 tableDef
->tableRowStart
= para
;
1042 info
->nestingLevel
++;
1044 para
= tableDef
->tableRowStart
;
1045 cell
= ME_FindItemFwd(para
, diCell
);
1046 assert(cell
&& !cell
->member
.cell
.prev_cell
);
1047 if (tableDef
->numCellsDefined
< 1)
1049 /* 2000 twips appears to be the cell size that native richedit uses
1050 * when no cell sizes are specified. */
1051 const int defaultCellSize
= 2000;
1052 int nRightBoundary
= defaultCellSize
;
1053 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1054 while (cell
->member
.cell
.next_cell
) {
1055 cell
= cell
->member
.cell
.next_cell
;
1056 nRightBoundary
+= defaultCellSize
;
1057 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1059 para
= ME_InsertTableCellFromCursor(info
->editor
);
1060 cell
= para
->member
.para
.pCell
;
1061 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1063 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
1065 RTFCell
*cellDef
= &tableDef
->cells
[i
];
1066 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
1067 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
1069 cell
= cell
->member
.cell
.next_cell
;
1072 para
= ME_InsertTableCellFromCursor(info
->editor
);
1073 cell
= para
->member
.para
.pCell
;
1076 /* Cell for table row delimiter is empty */
1077 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1080 run
= ME_FindItemFwd(cell
, diRun
);
1081 if (info
->editor
->pCursors
[0].pRun
!= run
||
1082 info
->editor
->pCursors
[0].nOffset
)
1085 /* Delete inserted cells that aren't defined. */
1086 info
->editor
->pCursors
[1].pRun
= run
;
1087 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1088 info
->editor
->pCursors
[1].nOffset
= 0;
1089 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1090 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1091 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1095 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1096 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1097 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1098 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1100 info
->nestingLevel
--;
1101 if (!info
->nestingLevel
)
1103 if (info
->canInheritInTbl
) {
1104 tableDef
->tableRowStart
= para
;
1106 while (info
->tableDef
) {
1107 tableDef
= info
->tableDef
;
1108 info
->tableDef
= tableDef
->parent
;
1109 heap_free(tableDef
);
1113 info
->tableDef
= tableDef
->parent
;
1114 heap_free(tableDef
);
1116 } else { /* v1.0 - v3.0 */
1118 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1119 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1120 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1121 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1123 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1125 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1128 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1129 tableDef
->numCellsInserted
++;
1131 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1132 if (!tableDef
->numCellsDefined
)
1133 pFmt
->wEffects
&= ~PFE_TABLE
;
1134 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1135 tableDef
->numCellsInserted
= 0;
1141 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1142 ME_DisplayItem
*para
;
1144 RTFFlushOutputBuffer(info
);
1145 para
= info
->editor
->pCursors
[0].pPara
;
1146 pFmt
= para
->member
.para
.pFmt
;
1147 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1149 /* rtfPar is treated like a space within a table. */
1150 info
->rtfClass
= rtfText
;
1151 info
->rtfMajor
= ' ';
1153 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1154 tableDef
->numCellsInserted
= 0;
1160 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1163 LPOLEOBJECT lpObject
= NULL
;
1164 LPSTORAGE lpStorage
= NULL
;
1165 LPOLECLIENTSITE lpClientSite
= NULL
;
1166 LPDATAOBJECT lpDataObject
= NULL
;
1167 LPOLECACHE lpOleCache
= NULL
;
1176 stgm
.tymed
= TYMED_ENHMF
;
1177 stgm
.u
.hEnhMetaFile
= hemf
;
1178 fm
.cfFormat
= CF_ENHMETAFILE
;
1182 stgm
.tymed
= TYMED_GDI
;
1183 stgm
.u
.hBitmap
= hbmp
;
1184 fm
.cfFormat
= CF_BITMAP
;
1186 stgm
.pUnkForRelease
= NULL
;
1189 fm
.dwAspect
= DVASPECT_CONTENT
;
1191 fm
.tymed
= stgm
.tymed
;
1193 if (!info
->lpRichEditOle
)
1195 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1198 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1199 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1200 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1201 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1202 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1203 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1204 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1205 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1209 reobject
.cbStruct
= sizeof(reobject
);
1210 reobject
.cp
= REO_CP_SELECTION
;
1211 reobject
.clsid
= clsid
;
1212 reobject
.poleobj
= lpObject
;
1213 reobject
.pstg
= lpStorage
;
1214 reobject
.polesite
= lpClientSite
;
1215 /* convert from twips to .01 mm */
1216 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1217 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1218 reobject
.dvaspect
= DVASPECT_CONTENT
;
1219 reobject
.dwFlags
= 0; /* FIXME */
1220 reobject
.dwUser
= 0;
1222 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1226 if (lpObject
) IOleObject_Release(lpObject
);
1227 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1228 if (lpStorage
) IStorage_Release(lpStorage
);
1229 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1230 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1235 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1238 BYTE
* buffer
= NULL
;
1239 unsigned bufsz
, bufidx
;
1245 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1248 if (info
->rtfClass
== rtfEOF
)
1251 /* fetch picture type */
1252 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1254 mfp
.mm
= info
->rtfParam
;
1257 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1259 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1262 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1264 gfx
= gfx_enhmetafile
;
1268 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1272 /* fetch picture attributes */
1276 if (info
->rtfClass
== rtfEOF
)
1278 if (info
->rtfClass
== rtfText
)
1280 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1282 ERR("Expected picture attribute (%d %d)\n",
1283 info
->rtfClass
, info
->rtfMajor
);
1286 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1288 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1290 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1292 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1294 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1295 sz
.cx
= info
->rtfParam
;
1296 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1297 sz
.cy
= info
->rtfParam
;
1299 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1301 /* fetch picture data */
1304 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1305 val
= info
->rtfMajor
;
1306 for (flip
= TRUE
;; flip
= !flip
)
1309 if (info
->rtfClass
== rtfEOF
)
1311 HeapFree(GetProcessHeap(), 0, buffer
);
1312 return; /* Warn ?? */
1314 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1316 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1319 if (bufidx
>= bufsz
&&
1320 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1322 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1325 val
= info
->rtfMajor
;
1327 if (flip
) FIXME("wrong hex string\n");
1331 case gfx_enhmetafile
:
1332 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1333 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1336 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1337 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1341 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1343 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1345 /* not quite right, especially for bitfields type of compression */
1346 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1347 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1348 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1349 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1350 bi
, DIB_RGB_COLORS
)))
1351 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1358 HeapFree(GetProcessHeap(), 0, buffer
);
1359 RTFRouteToken (info
); /* feed "}" back to router */
1362 HeapFree(GetProcessHeap(), 0, buffer
);
1364 RTFRouteToken(info
); /* feed "}" back to router */
1367 /* for now, lookup the \result part and use it, whatever the object */
1368 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1373 if (info
->rtfClass
== rtfEOF
)
1375 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1377 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1380 if (info
->rtfClass
== rtfEOF
)
1382 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1386 while (RTFGetToken (info
) != rtfEOF
)
1388 if (info
->rtfClass
== rtfGroup
)
1390 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1391 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1393 RTFRouteToken(info
);
1396 else RTFSkipGroup(info
);
1399 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1401 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1405 RTFRouteToken(info
); /* feed "}" back to router */
1408 static void ME_RTFReadHook(RTF_Info
*info
)
1410 switch(info
->rtfClass
)
1413 switch(info
->rtfMajor
)
1416 if (info
->stackTop
< maxStack
) {
1417 info
->stack
[info
->stackTop
].style
= info
->style
;
1418 ME_AddRefStyle(info
->style
);
1419 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1420 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1423 info
->styleChanged
= FALSE
;
1427 RTFFlushOutputBuffer(info
);
1429 if (info
->stackTop
<= 0)
1430 info
->rtfClass
= rtfEOF
;
1431 if (info
->stackTop
< 0)
1434 ME_ReleaseStyle(info
->style
);
1435 info
->style
= info
->stack
[info
->stackTop
].style
;
1436 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1437 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1446 ME_StreamInFill(ME_InStream
*stream
)
1448 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1449 (BYTE
*)stream
->buffer
,
1450 sizeof(stream
->buffer
),
1451 (LONG
*)&stream
->dwSize
);
1455 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1459 int from
, to
, nUndoMode
;
1460 int nEventMask
= editor
->nEventMask
;
1461 ME_InStream inStream
;
1462 BOOL invalidRTF
= FALSE
;
1463 ME_Cursor
*selStart
, *selEnd
;
1464 LRESULT num_read
= 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1466 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1467 editor
->nEventMask
= 0;
1469 ME_GetSelectionOfs(editor
, &from
, &to
);
1470 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1472 ME_GetSelection(editor
, &selStart
, &selEnd
);
1473 style
= ME_GetSelectionInsertStyle(editor
);
1475 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1477 /* Don't insert text at the end of the table row */
1478 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1479 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1480 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1482 para
= para
->member
.para
.next_para
;
1483 editor
->pCursors
[0].pPara
= para
;
1484 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1485 editor
->pCursors
[0].nOffset
= 0;
1487 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1489 para
= para
->member
.para
.next_para
;
1490 editor
->pCursors
[0].pPara
= para
;
1491 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1492 editor
->pCursors
[0].nOffset
= 0;
1494 editor
->pCursors
[1] = editor
->pCursors
[0];
1495 } else { /* v1.0 - 3.0 */
1496 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1497 ME_IsInTable(editor
->pCursors
[0].pRun
))
1501 style
= editor
->pBuffer
->pDefaultStyle
;
1502 ME_AddRefStyle(style
);
1503 ME_SetSelection(editor
, 0, 0);
1504 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1505 ME_GetTextLength(editor
), FALSE
);
1507 ME_ClearTempStyle(editor
);
1508 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1512 /* Back up undo mode to a local variable */
1513 nUndoMode
= editor
->nUndoMode
;
1515 /* Only create an undo if SFF_SELECTION is set */
1516 if (!(format
& SFF_SELECTION
))
1517 editor
->nUndoMode
= umIgnore
;
1519 inStream
.editstream
= stream
;
1520 inStream
.editstream
->dwError
= 0;
1521 inStream
.dwSize
= 0;
1522 inStream
.dwUsed
= 0;
1524 if (format
& SF_RTF
)
1526 /* Check if it's really RTF, and if it is not, use plain text */
1527 ME_StreamInFill(&inStream
);
1528 if (!inStream
.editstream
->dwError
)
1530 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1531 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1534 inStream
.editstream
->dwError
= -16;
1539 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1541 if (format
& SF_RTF
) {
1542 from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1544 /* setup the RTF parser */
1545 memset(&parser
, 0, sizeof parser
);
1546 RTFSetEditStream(&parser
, &inStream
);
1547 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1548 parser
.editor
= editor
;
1549 parser
.style
= style
;
1550 WriterInit(&parser
);
1552 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1553 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1554 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1555 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1557 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1558 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1562 /* do the parsing */
1564 RTFFlushOutputBuffer(&parser
);
1565 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1566 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1567 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1569 /* Delete any incomplete table row at the end of the rich text. */
1571 ME_DisplayItem
*para
;
1573 parser
.rtfMinor
= rtfRow
;
1574 /* Complete the table row before deleting it.
1575 * By doing it this way we will have the current paragraph format set
1576 * properly to reflect that is not in the complete table, and undo items
1577 * will be added for this change to the current paragraph format. */
1578 if (parser
.nestingLevel
> 0)
1580 while (parser
.nestingLevel
> 1)
1581 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1582 para
= parser
.tableDef
->tableRowStart
;
1583 ME_RTFSpecialCharHook(&parser
);
1585 para
= parser
.tableDef
->tableRowStart
;
1586 ME_RTFSpecialCharHook(&parser
);
1587 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1588 para
= para
->member
.para
.next_para
;
1591 editor
->pCursors
[1].pPara
= para
;
1592 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1593 editor
->pCursors
[1].nOffset
= 0;
1594 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1595 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1596 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1597 if (parser
.tableDef
)
1598 parser
.tableDef
->tableRowStart
= NULL
;
1601 ME_CheckTablesForCorruption(editor
);
1602 RTFDestroy(&parser
);
1603 if (parser
.lpRichEditOle
)
1604 IRichEditOle_Release(parser
.lpRichEditOle
);
1606 if (parser
.stackTop
> 0)
1608 while (--parser
.stackTop
>= 0)
1610 ME_ReleaseStyle(parser
.style
);
1611 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1613 if (!inStream
.editstream
->dwError
)
1614 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1617 /* Remove last line break, as mandated by tests. This is not affected by
1618 CR/LF counters, since RTF streaming presents only \para tokens, which
1619 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1621 if (stripLastCR
&& !(format
& SFF_SELECTION
)) {
1623 ME_GetSelection(editor
, &selStart
, &selEnd
);
1624 newto
= ME_GetCursorOfs(selEnd
);
1625 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1626 WCHAR lastchar
[3] = {'\0', '\0'};
1627 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1628 ME_Cursor linebreakCursor
= *selEnd
;
1630 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1631 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, FALSE
, FALSE
);
1632 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1633 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1637 to
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1638 num_read
= to
- from
;
1640 style
= parser
.style
;
1642 else if (format
& SF_TEXT
)
1643 num_read
= ME_StreamInText(editor
, format
, &inStream
, style
);
1645 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1646 /* put the cursor at the top */
1647 if (!(format
& SFF_SELECTION
))
1648 ME_SetSelection(editor
, 0, 0);
1651 /* Restore saved undo mode */
1652 editor
->nUndoMode
= nUndoMode
;
1654 /* even if we didn't add an undo, we need to commit anything on the stack */
1655 ME_CommitUndo(editor
);
1657 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1658 if (!(format
& SFF_SELECTION
))
1659 ME_EmptyUndoStack(editor
);
1661 ME_ReleaseStyle(style
);
1662 editor
->nEventMask
= nEventMask
;
1663 ME_UpdateRepaint(editor
, FALSE
);
1664 if (!(format
& SFF_SELECTION
)) {
1665 ME_ClearTempStyle(editor
);
1667 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1668 ME_MoveCaret(editor
);
1669 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1670 ME_SendSelChange(editor
);
1671 ME_SendRequestResize(editor
, FALSE
);
1677 typedef struct tagME_RTFStringStreamStruct
1682 } ME_RTFStringStreamStruct
;
1684 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1686 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1689 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1690 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1691 pStruct
->pos
+= count
;
1697 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1700 ME_RTFStringStreamStruct data
;
1702 data
.string
= string
;
1703 data
.length
= strlen(string
);
1705 es
.dwCookie
= (DWORD_PTR
)&data
;
1706 es
.pfnCallback
= ME_ReadFromRTFString
;
1707 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1712 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1714 const int nLen
= lstrlenW(text
);
1715 const int nTextLen
= ME_GetTextLength(editor
);
1718 WCHAR wLastChar
= ' ';
1720 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1721 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1723 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1724 FIXME("Flags 0x%08x not implemented\n",
1725 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1728 if (chrg
->cpMax
== -1)
1731 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1733 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1734 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1739 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1740 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1744 chrgText
->cpMin
= -1;
1745 chrgText
->cpMax
= -1;
1750 /* when searching up, if cpMin < cpMax, then instead of searching
1751 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1752 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1753 * case, it is always bigger than cpMin.
1755 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1759 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1760 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1766 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1769 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1773 if (flags
& FR_DOWN
) /* Forward search */
1775 /* If possible, find the character before where the search starts */
1776 if ((flags
& FR_WHOLEWORD
) && nMin
)
1778 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1779 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1780 ME_MoveCursorChars(editor
, &cursor
, 1);
1782 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1785 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1787 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1788 int nCurStart
= cursor
.nOffset
;
1791 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
), text
[nMatched
], (flags
& FR_MATCHCASE
)))
1793 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1797 if (nMatched
== nLen
)
1799 ME_DisplayItem
*pNextItem
= pCurItem
;
1800 int nNextStart
= nCurStart
;
1803 /* Check to see if next character is a whitespace */
1804 if (flags
& FR_WHOLEWORD
)
1806 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1808 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1809 nNextStart
= -nMatched
;
1813 wNextChar
= *get_text( &pNextItem
->member
.run
, nNextStart
+ nMatched
);
1817 if (isalnumW(wNextChar
))
1821 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1824 chrgText
->cpMin
= cursor
.nOffset
;
1825 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1827 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1828 return cursor
.nOffset
;
1830 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1832 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1833 nCurStart
= -nMatched
;
1837 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
);
1842 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.len
)
1844 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1849 else /* Backward search */
1851 /* If possible, find the character after where the search ends */
1852 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1854 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1855 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1856 ME_MoveCursorChars(editor
, &cursor
, -1);
1858 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1861 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1863 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1864 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1865 int nCurEnd
= cursor
.nOffset
;
1870 ME_PrevRun(&pCurPara
, &pCurItem
);
1871 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1874 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 ),
1875 text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
) ))
1877 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1881 if (nMatched
== nLen
)
1883 ME_DisplayItem
*pPrevItem
= pCurItem
;
1884 int nPrevEnd
= nCurEnd
;
1888 /* Check to see if previous character is a whitespace */
1889 if (flags
& FR_WHOLEWORD
)
1891 if (nPrevEnd
- nMatched
== 0)
1893 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1895 nPrevEnd
= pPrevItem
->member
.run
.len
+ nMatched
;
1899 wPrevChar
= *get_text( &pPrevItem
->member
.run
, nPrevEnd
- nMatched
- 1 );
1903 if (isalnumW(wPrevChar
))
1907 nStart
= pCurPara
->member
.para
.nCharOfs
1908 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1911 chrgText
->cpMin
= nStart
;
1912 chrgText
->cpMax
= nStart
+ nLen
;
1914 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1917 if (nCurEnd
- nMatched
== 0)
1919 ME_PrevRun(&pCurPara
, &pCurItem
);
1920 /* Don't care about pCurItem becoming NULL here; it's already taken
1921 * care of in the exterior loop condition */
1922 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1926 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 );
1931 if (cursor
.nOffset
< 0)
1933 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1934 cursor
.nOffset
= cursor
.pRun
->member
.run
.len
;
1938 TRACE("not found\n");
1940 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1944 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1949 if (!ex
->cb
|| !pText
) return 0;
1951 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1952 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1954 if (ex
->flags
& GT_SELECTION
)
1957 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
1958 start
= editor
->pCursors
[nStartCur
];
1963 ME_SetCursorToStart(editor
, &start
);
1966 if (ex
->codepage
== CP_UNICODE
)
1968 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
1969 &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
1973 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1974 we can just take a bigger buffer? :)
1975 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1976 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1978 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1983 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
1984 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
1986 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
1987 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
1988 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1989 if (rc
) rc
--; /* do not count 0 terminator */
1996 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
1997 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
1999 if (!strText
) return 0;
2001 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, FALSE
, FALSE
);
2004 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2006 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, FALSE
, FALSE
);
2007 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
2008 nLen
+1, NULL
, NULL
);
2014 static int handle_EM_EXSETSEL( ME_TextEditor
*editor
, int to
, int from
)
2018 TRACE("%d - %d\n", to
, from
);
2020 ME_InvalidateSelection( editor
);
2021 end
= ME_SetSelection( editor
, to
, from
);
2022 ME_InvalidateSelection( editor
);
2023 ITextHost_TxShowCaret( editor
->texthost
, FALSE
);
2024 ME_ShowCaret( editor
);
2025 ME_SendSelChange( editor
);
2030 typedef struct tagME_GlobalDestStruct
2034 } ME_GlobalDestStruct
;
2036 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2038 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2043 pDest
= (WORD
*)lpBuff
;
2044 pSrc
= GlobalLock(pData
->hData
);
2045 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2046 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2048 pData
->nLength
+= i
;
2050 GlobalUnlock(pData
->hData
);
2054 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2056 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2061 pSrc
= GlobalLock(pData
->hData
);
2062 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2063 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2065 pData
->nLength
+= i
;
2067 GlobalUnlock(pData
->hData
);
2071 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2075 ME_GlobalDestStruct gds
;
2076 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2079 if (IsClipboardFormatAvailable(nRTFFormat
))
2080 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2081 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2082 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2086 if (!OpenClipboard(editor
->hWnd
))
2088 gds
.hData
= GetClipboardData(cf
);
2090 es
.dwCookie
= (DWORD_PTR
)&gds
;
2091 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2092 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2098 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2100 LPDATAOBJECT dataObj
= NULL
;
2103 if (editor
->cPasswordMask
)
2104 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2106 if(editor
->lpOleCallback
)
2109 range
.cpMin
= ME_GetCursorOfs(start
);
2110 range
.cpMax
= range
.cpMin
+ nChars
;
2111 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2113 if(FAILED(hr
) || !dataObj
)
2114 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2116 hr
= OleSetClipboard(dataObj
);
2117 IDataObject_Release(dataObj
);
2119 return SUCCEEDED(hr
) != 0;
2122 /* helper to send a msg filter notification */
2124 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2128 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2129 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2130 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2131 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2133 msgf
.wParam
= *wParam
;
2134 msgf
.lParam
= *lParam
;
2135 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2137 *wParam
= msgf
.wParam
;
2138 *lParam
= msgf
.lParam
;
2139 msgf
.wParam
= *wParam
;
2144 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2146 ME_DisplayItem
*startPara
, *endPara
;
2147 ME_DisplayItem
*prev_para
;
2148 ME_Cursor
*from
, *to
;
2152 if (!editor
->AutoURLDetect_bEnable
) return;
2154 ME_GetSelection(editor
, &from
, &to
);
2156 /* Find paragraph previous to the one that contains start cursor */
2157 startPara
= from
->pPara
;
2158 prev_para
= startPara
->member
.para
.prev_para
;
2159 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2161 /* Find paragraph that contains end cursor */
2162 endPara
= to
->pPara
->member
.para
.next_para
;
2164 start
.pPara
= startPara
;
2165 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2167 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2169 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2173 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2175 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2176 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2178 if (editor
->bMouseCaptured
)
2180 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2181 editor
->nSelectionType
= stPosition
;
2189 editor
->nUDArrowX
= -1;
2195 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2196 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2200 editor
->nUDArrowX
= -1;
2201 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2202 if (editor
->styleFlags
& ES_READONLY
)
2204 if (ME_IsSelection(editor
))
2206 ME_DeleteSelection(editor
);
2207 ME_CommitUndo(editor
);
2209 else if (nKey
== VK_DELETE
)
2211 /* Delete stops group typing.
2212 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2213 ME_DeleteTextAtCursor(editor
, 1, 1);
2214 ME_CommitUndo(editor
);
2216 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2218 BOOL bDeletionSucceeded
;
2219 /* Backspace can be grouped for a single undo */
2220 ME_ContinueCoalescingTransaction(editor
);
2221 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2222 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2223 /* Deletion was prevented so the cursor is moved back to where it was.
2224 * (e.g. this happens when trying to delete cell boundaries)
2226 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2228 ME_CommitCoalescingUndo(editor
);
2232 ME_MoveCursorFromTableRowStartParagraph(editor
);
2233 ME_UpdateSelectionLinkAttribute(editor
);
2234 ME_UpdateRepaint(editor
, FALSE
);
2235 ME_SendRequestResize(editor
, FALSE
);
2238 if (editor
->bDialogMode
)
2243 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2245 if (editor
->hwndParent
)
2248 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2249 if (HIWORD(dw
) == DC_HASDEFID
)
2251 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2254 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
2255 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2263 if (editor
->styleFlags
& ES_MULTILINE
)
2265 ME_Cursor cursor
= editor
->pCursors
[0];
2266 ME_DisplayItem
*para
= cursor
.pPara
;
2268 const WCHAR endl
= '\r';
2269 const WCHAR endlv10
[] = {'\r','\n'};
2272 if (editor
->styleFlags
& ES_READONLY
) {
2273 MessageBeep(MB_ICONERROR
);
2277 ME_GetSelectionOfs(editor
, &from
, &to
);
2278 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2280 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2281 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2282 /* Add a new table row after this row. */
2283 para
= ME_AppendTableRow(editor
, para
);
2284 para
= para
->member
.para
.next_para
;
2285 editor
->pCursors
[0].pPara
= para
;
2286 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2287 editor
->pCursors
[0].nOffset
= 0;
2288 editor
->pCursors
[1] = editor
->pCursors
[0];
2289 ME_CommitUndo(editor
);
2290 ME_CheckTablesForCorruption(editor
);
2291 ME_UpdateRepaint(editor
, FALSE
);
2294 else if (para
== editor
->pCursors
[1].pPara
&&
2295 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2296 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2297 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2299 /* Insert a newline before the table. */
2300 para
= para
->member
.para
.prev_para
;
2301 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2302 editor
->pCursors
[0].pPara
= para
;
2303 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2304 editor
->pCursors
[1] = editor
->pCursors
[0];
2305 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2306 editor
->pCursors
[0].pRun
->member
.run
.style
);
2307 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2308 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2309 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2310 editor
->pCursors
[0].pPara
= para
;
2311 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2312 editor
->pCursors
[1] = editor
->pCursors
[0];
2313 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2314 ME_CommitCoalescingUndo(editor
);
2315 ME_CheckTablesForCorruption(editor
);
2316 ME_UpdateRepaint(editor
, FALSE
);
2319 } else { /* v1.0 - 3.0 */
2320 ME_DisplayItem
*para
= cursor
.pPara
;
2321 if (ME_IsInTable(para
))
2323 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2326 ME_ContinueCoalescingTransaction(editor
);
2327 para
= ME_AppendTableRow(editor
, para
);
2328 editor
->pCursors
[0].pPara
= para
;
2329 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2330 editor
->pCursors
[0].nOffset
= 0;
2331 editor
->pCursors
[1] = editor
->pCursors
[0];
2332 ME_CommitCoalescingUndo(editor
);
2333 ME_UpdateRepaint(editor
, FALSE
);
2337 ME_ContinueCoalescingTransaction(editor
);
2338 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2339 !ME_IsInTable(para
->member
.para
.prev_para
))
2341 /* Insert newline before table */
2342 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2344 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2345 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2347 editor
->pCursors
[0].nOffset
= 0;
2348 editor
->pCursors
[1] = editor
->pCursors
[0];
2349 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2350 editor
->pCursors
[0].pRun
->member
.run
.style
);
2352 editor
->pCursors
[1] = editor
->pCursors
[0];
2353 para
= ME_AppendTableRow(editor
, para
);
2354 editor
->pCursors
[0].pPara
= para
;
2355 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2356 editor
->pCursors
[0].nOffset
= 0;
2357 editor
->pCursors
[1] = editor
->pCursors
[0];
2359 ME_CommitCoalescingUndo(editor
);
2360 ME_UpdateRepaint(editor
, FALSE
);
2366 style
= ME_GetInsertStyle(editor
, 0);
2367 ME_SaveTempStyle(editor
);
2368 ME_ContinueCoalescingTransaction(editor
);
2370 ME_InsertEndRowFromCursor(editor
, 0);
2372 if (!editor
->bEmulateVersion10
)
2373 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2375 ME_InsertTextFromCursor(editor
, 0, endlv10
, 2, style
);
2376 ME_ReleaseStyle(style
);
2377 ME_CommitCoalescingUndo(editor
);
2380 ME_UpdateSelectionLinkAttribute(editor
);
2381 ME_UpdateRepaint(editor
, FALSE
);
2387 if (editor
->bDialogMode
&& editor
->hwndParent
)
2388 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2391 if (editor
->bDialogMode
&& editor
->hwndParent
)
2392 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2397 handle_EM_EXSETSEL( editor
, 0, -1 );
2403 return ME_Paste(editor
);
2411 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2412 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2415 result
= ME_Copy(editor
, selStart
, nChars
);
2416 if (result
&& nKey
== 'X')
2418 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2419 ME_CommitUndo(editor
);
2420 ME_UpdateRepaint(editor
, TRUE
);
2441 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2442 editor
->nUDArrowX
= -1;
2449 chf
.cbSize
= sizeof(chf
);
2451 ME_GetSelectionCharFormat(editor
, &chf
);
2452 ME_DumpStyleToBuf(&chf
, buf
);
2453 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2457 ME_CheckCharOffsets(editor
);
2464 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2465 LPARAM flags
, BOOL unicode
)
2469 if (editor
->bMouseCaptured
)
2473 wstr
= (WCHAR
)charCode
;
2476 CHAR charA
= charCode
;
2477 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2480 if (editor
->styleFlags
& ES_READONLY
) {
2481 MessageBeep(MB_ICONERROR
);
2482 return 0; /* FIXME really 0 ? */
2485 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2487 ME_Cursor cursor
= editor
->pCursors
[0];
2488 ME_DisplayItem
*para
= cursor
.pPara
;
2490 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2491 ME_GetSelectionOfs(editor
, &from
, &to
);
2493 /* v4.1 allows tabs to be inserted with ctrl key down */
2494 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2496 ME_DisplayItem
*para
;
2497 BOOL bSelectedRow
= FALSE
;
2499 para
= cursor
.pPara
;
2500 if (ME_IsSelection(editor
) &&
2501 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2502 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2503 para
->member
.para
.prev_para
->type
== diParagraph
)
2505 para
= para
->member
.para
.prev_para
;
2506 bSelectedRow
= TRUE
;
2508 if (ME_IsInTable(para
))
2510 ME_TabPressedInTable(editor
, bSelectedRow
);
2511 ME_CommitUndo(editor
);
2514 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2515 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2517 para
= para
->member
.para
.next_para
;
2518 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2519 para
= para
->member
.para
.next_para
;
2520 editor
->pCursors
[0].pPara
= para
;
2521 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2522 editor
->pCursors
[0].nOffset
= 0;
2523 editor
->pCursors
[1] = editor
->pCursors
[0];
2526 } else { /* v1.0 - 3.0 */
2527 if (ME_IsInTable(cursor
.pRun
) &&
2528 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2531 /* Text should not be inserted at the end of the table. */
2536 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2537 /* WM_CHAR is restricted to nTextLimit */
2538 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2540 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2541 ME_SaveTempStyle(editor
);
2542 ME_ContinueCoalescingTransaction(editor
);
2543 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2544 ME_ReleaseStyle(style
);
2545 ME_CommitCoalescingUndo(editor
);
2546 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2549 ME_UpdateSelectionLinkAttribute(editor
);
2550 ME_UpdateRepaint(editor
, FALSE
);
2555 /* Process the message and calculate the new click count.
2557 * returns: The click count if it is mouse down event, else returns 0. */
2558 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2561 static int clickNum
= 0;
2562 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2565 if ((msg
== WM_LBUTTONDBLCLK
) ||
2566 (msg
== WM_RBUTTONDBLCLK
) ||
2567 (msg
== WM_MBUTTONDBLCLK
) ||
2568 (msg
== WM_XBUTTONDBLCLK
))
2570 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2573 if ((msg
== WM_LBUTTONDOWN
) ||
2574 (msg
== WM_RBUTTONDOWN
) ||
2575 (msg
== WM_MBUTTONDOWN
) ||
2576 (msg
== WM_XBUTTONDOWN
))
2578 static MSG prevClickMsg
;
2580 /* Compare the editor instead of the hwnd so that the this
2581 * can still be done for windowless richedit controls. */
2582 clickMsg
.hwnd
= (HWND
)editor
;
2583 clickMsg
.message
= msg
;
2584 clickMsg
.wParam
= wParam
;
2585 clickMsg
.lParam
= lParam
;
2586 clickMsg
.time
= GetMessageTime();
2587 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2588 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2589 if ((clickNum
!= 0) &&
2590 (clickMsg
.message
== prevClickMsg
.message
) &&
2591 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2592 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2593 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2594 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2595 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2601 prevClickMsg
= clickMsg
;
2608 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2614 DWORD messagePos
= GetMessagePos();
2615 pt
.x
= (short)LOWORD(messagePos
);
2616 pt
.y
= (short)HIWORD(messagePos
);
2620 sbi
.cbSize
= sizeof(sbi
);
2621 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2622 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2623 PtInRect(&sbi
.rcScrollBar
, pt
))
2625 ITextHost_TxSetCursor(editor
->texthost
,
2626 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2629 sbi
.cbSize
= sizeof(sbi
);
2630 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2631 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2632 PtInRect(&sbi
.rcScrollBar
, pt
))
2634 ITextHost_TxSetCursor(editor
->texthost
,
2635 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2639 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2641 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2642 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2645 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2646 pt
.y
< editor
->rcFormat
.top
&&
2647 pt
.x
< editor
->rcFormat
.left
)
2649 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2652 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2654 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2655 ITextHost_TxSetCursor(editor
->texthost
,
2656 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2658 ITextHost_TxSetCursor(editor
->texthost
,
2659 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2662 if (pt
.x
< editor
->rcFormat
.left
)
2664 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2667 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2672 run
= &cursor
.pRun
->member
.run
;
2673 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2674 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2676 ITextHost_TxSetCursor(editor
->texthost
,
2677 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2682 if (ME_IsSelection(editor
))
2684 int selStart
, selEnd
;
2685 int offset
= ME_GetCursorOfs(&cursor
);
2687 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2688 if (selStart
<= offset
&& selEnd
>= offset
) {
2689 ITextHost_TxSetCursor(editor
->texthost
,
2690 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2696 ITextHost_TxSetCursor(editor
->texthost
,
2697 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2701 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2703 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2704 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2705 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2706 editor
->rcFormat
.right
-= 1;
2709 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2714 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2716 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2717 if(selrange
.cpMin
== selrange
.cpMax
)
2718 seltype
|= SEL_EMPTY
;
2721 /* FIXME: Handle objects */
2722 seltype
|= SEL_TEXT
;
2723 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2724 seltype
|= SEL_MULTICHAR
;
2726 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2728 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2734 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2736 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2742 ed
->hwndParent
= NULL
;
2743 ed
->sizeWindow
.cx
= ed
->sizeWindow
.cy
= 0;
2744 ed
->texthost
= texthost
;
2746 ed
->bEmulateVersion10
= bEmulateVersion10
;
2748 ITextHost_TxGetPropertyBits(texthost
,
2749 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2750 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2751 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2752 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2753 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2755 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2756 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2757 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2758 ed
->pBuffer
= ME_MakeText();
2759 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2760 ed
->nAvailWidth
= 0; /* wrap to client area */
2761 ME_MakeFirstParagraph(ed
);
2762 /* The four cursors are for:
2763 * 0 - The position where the caret is shown
2764 * 1 - The anchored end of the selection (for normal selection)
2765 * 2 & 3 - The anchored start and end respectively for word, line,
2766 * or paragraph selection.
2769 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2770 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2771 ed
->pCursors
[1] = ed
->pCursors
[0];
2772 ed
->pCursors
[2] = ed
->pCursors
[0];
2773 ed
->pCursors
[3] = ed
->pCursors
[1];
2774 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2775 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2777 ed
->rgbBackColor
= -1;
2778 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2779 ed
->bCaretAtEnd
= FALSE
;
2781 ed
->nModifyStep
= 0;
2782 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2783 list_init( &ed
->undo_stack
);
2784 list_init( &ed
->redo_stack
);
2785 ed
->nUndoStackSize
= 0;
2786 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2787 ed
->nUndoMode
= umAddToUndo
;
2788 ed
->nParagraphs
= 1;
2789 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2790 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2791 ed
->bHideSelection
= FALSE
;
2792 ed
->pfnWordBreak
= NULL
;
2793 ed
->lpOleCallback
= NULL
;
2794 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2795 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2796 ed
->AutoURLDetect_bEnable
= FALSE
;
2797 ed
->bHaveFocus
= FALSE
;
2798 ed
->bDialogMode
= FALSE
;
2799 ed
->bMouseCaptured
= FALSE
;
2800 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2802 ed
->pFontCache
[i
].nRefs
= 0;
2803 ed
->pFontCache
[i
].nAge
= 0;
2804 ed
->pFontCache
[i
].hFont
= NULL
;
2807 ME_CheckCharOffsets(ed
);
2808 ed
->bDefaultFormatRect
= TRUE
;
2809 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2811 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2812 ed
->selofs
= SELECTIONBAR_WIDTH
;
2813 ed
->styleFlags
|= ES_SELECTIONBAR
;
2817 ed
->nSelectionType
= stPosition
;
2819 ed
->cPasswordMask
= 0;
2820 if (props
& TXTBIT_USEPASSWORD
)
2821 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2823 if (props
& TXTBIT_AUTOWORDSEL
)
2824 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2825 if (props
& TXTBIT_MULTILINE
) {
2826 ed
->styleFlags
|= ES_MULTILINE
;
2827 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2829 ed
->bWordWrap
= FALSE
;
2831 if (props
& TXTBIT_READONLY
)
2832 ed
->styleFlags
|= ES_READONLY
;
2833 if (!(props
& TXTBIT_HIDESELECTION
))
2834 ed
->styleFlags
|= ES_NOHIDESEL
;
2835 if (props
& TXTBIT_SAVESELECTION
)
2836 ed
->styleFlags
|= ES_SAVESEL
;
2837 if (props
& TXTBIT_VERTICAL
)
2838 ed
->styleFlags
|= ES_VERTICAL
;
2839 if (props
& TXTBIT_DISABLEDRAG
)
2840 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2842 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2844 /* Default scrollbar information */
2845 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2846 ed
->vert_si
.nMin
= 0;
2847 ed
->vert_si
.nMax
= 0;
2848 ed
->vert_si
.nPage
= 0;
2849 ed
->vert_si
.nPos
= 0;
2851 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2852 ed
->horz_si
.nMin
= 0;
2853 ed
->horz_si
.nMax
= 0;
2854 ed
->horz_si
.nPage
= 0;
2855 ed
->horz_si
.nPos
= 0;
2857 ed
->wheel_remain
= 0;
2859 OleInitialize(NULL
);
2864 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2866 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2867 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2870 ME_ClearTempStyle(editor
);
2871 ME_EmptyUndoStack(editor
);
2874 ME_DestroyDisplayItem(p
);
2877 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2878 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2880 if (editor
->pFontCache
[i
].hFont
)
2881 DeleteObject(editor
->pFontCache
[i
].hFont
);
2883 if (editor
->rgbBackColor
!= -1)
2884 DeleteObject(editor
->hbrBackground
);
2885 if(editor
->lpOleCallback
)
2886 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2887 ITextHost_Release(editor
->texthost
);
2890 IRichEditOle_Release(editor
->reOle
);
2891 editor
->reOle
= NULL
;
2895 FREE_OBJ(editor
->pBuffer
);
2896 FREE_OBJ(editor
->pCursors
);
2901 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2906 case DLL_PROCESS_ATTACH
:
2907 DisableThreadLibraryCalls(hinstDLL
);
2908 me_heap
= HeapCreate (0, 0x10000, 0);
2909 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2910 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2914 case DLL_PROCESS_DETACH
:
2915 if (lpvReserved
) break;
2916 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2917 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2918 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2919 UnregisterClassA("RichEdit50A", 0);
2920 if (ME_ListBoxRegistered
)
2921 UnregisterClassW(REListBox20W
, 0);
2922 if (ME_ComboBoxRegistered
)
2923 UnregisterClassW(REComboBox20W
, 0);
2925 HeapDestroy (me_heap
);
2931 static inline int get_default_line_height( ME_TextEditor
*editor
)
2935 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
2936 height
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
2937 if (height
<= 0) height
= 24;
2942 static inline int calc_wheel_change( int *remain
, int amount_per_click
)
2944 int change
= amount_per_click
* (float)*remain
/ WHEEL_DELTA
;
2945 *remain
-= WHEEL_DELTA
* change
/ amount_per_click
;
2949 static const char * const edit_messages
[] = {
2978 "EM_SETPASSWORDCHAR",
2979 "EM_EMPTYUNDOBUFFER",
2980 "EM_GETFIRSTVISIBLELINE",
2982 "EM_SETWORDBREAKPROC",
2983 "EM_GETWORDBREAKPROC",
2984 "EM_GETPASSWORDCHAR",
2994 static const char * const richedit_messages
[] = {
2999 "EM_EXLINEFROMCHAR",
3005 "EM_GETOLEINTERFACE",
3015 "EM_SETOLECALLBACK",
3017 "EM_SETTARGETDEVICE",
3025 "EM_GETWORDBREAKPROCEX",
3026 "EM_SETWORDBREAKPROCEX",
3028 "EM_UNKNOWN_USER_83",
3033 "EM_STOPGROUPTYPING",
3037 "EM_GETAUTOURLDETECT",
3040 "EM_GETTEXTLENGTHEX",
3043 "EM_UNKNOWN_USER_98",
3044 "EM_UNKNOWN_USER_99",
3045 "EM_SETPUNCTUATION",
3046 "EM_GETPUNCTUATION",
3047 "EM_SETWORDWRAPMODE",
3048 "EM_GETWORDWRAPMODE",
3054 "EM_UNKNOWN_USER_109",
3055 "EM_UNKNOWN_USER_110",
3056 "EM_UNKNOWN_USER_111",
3057 "EM_UNKNOWN_USER_112",
3058 "EM_UNKNOWN_USER_113",
3059 "EM_UNKNOWN_USER_114",
3060 "EM_UNKNOWN_USER_115",
3061 "EM_UNKNOWN_USER_116",
3062 "EM_UNKNOWN_USER_117",
3063 "EM_UNKNOWN_USER_118",
3064 "EM_UNKNOWN_USER_119",
3065 "EM_SETLANGOPTIONS",
3066 "EM_GETLANGOPTIONS",
3067 "EM_GETIMECOMPMODE",
3071 "EM_SETIMEMODEBIAS",
3076 get_msg_name(UINT msg
)
3078 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
3079 return edit_messages
[msg
- EM_GETSEL
];
3080 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
3081 return richedit_messages
[msg
- EM_CANPASTE
];
3085 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3089 ME_Cursor cursor
; /* The start of the clicked text. */
3092 x
= (short)LOWORD(lParam
);
3093 y
= (short)HIWORD(lParam
);
3094 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3095 if (!isExact
) return;
3097 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3098 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3099 { /* The clicked run has CFE_LINK set */
3102 info
.nmhdr
.hwndFrom
= NULL
;
3103 info
.nmhdr
.idFrom
= 0;
3104 info
.nmhdr
.code
= EN_LINK
;
3106 info
.wParam
= wParam
;
3107 info
.lParam
= lParam
;
3110 /* find the first contiguous run with CFE_LINK set */
3111 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3112 for (di
= cursor
.pRun
->prev
;
3113 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3116 info
.chrg
.cpMin
-= di
->member
.run
.len
;
3119 /* find the last contiguous run with CFE_LINK set */
3120 info
.chrg
.cpMax
= ME_GetCursorOfs(&cursor
) + cursor
.pRun
->member
.run
.len
;
3121 for (di
= cursor
.pRun
->next
;
3122 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3125 info
.chrg
.cpMax
+= di
->member
.run
.len
;
3128 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3132 #define UNSUPPORTED_MSG(e) \
3134 FIXME(#e ": stub\n"); \
3135 *phresult = S_FALSE; \
3138 /* Handle messages for windowless and windoweded richedit controls.
3140 * The LRESULT that is returned is a return value for window procs,
3141 * and the phresult parameter is the COM return code needed by the
3142 * text services interface. */
3143 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3144 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3150 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3151 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3152 UNSUPPORTED_MSG(EM_FMTLINES
)
3153 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3154 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3155 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3156 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3157 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3158 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3159 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3160 UNSUPPORTED_MSG(EM_GETREDONAME
)
3161 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3162 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3163 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3164 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3165 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3166 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3167 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3168 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
3169 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3170 UNSUPPORTED_MSG(EM_SETMARGINS
)
3171 UNSUPPORTED_MSG(EM_SETPALETTE
)
3172 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3173 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3174 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3176 /* Messages specific to Richedit controls */
3179 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3181 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3184 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
;
3187 editor
->bDialogMode
= TRUE
;
3188 if (editor
->styleFlags
& ES_MULTILINE
)
3189 code
|= DLGC_WANTMESSAGE
;
3190 if (!(editor
->styleFlags
& ES_SAVESEL
))
3191 code
|= DLGC_HASSETSEL
;
3194 case EM_EMPTYUNDOBUFFER
:
3195 ME_EmptyUndoStack(editor
);
3199 /* Note: wParam/lParam can be NULL */
3201 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3202 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3203 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3204 if ((*pfrom
|*pto
) & 0xFFFF0000)
3206 return MAKELONG(*pfrom
,*pto
);
3210 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3211 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3212 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3215 case EM_SETUNDOLIMIT
:
3217 if ((int)wParam
< 0)
3218 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3220 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3221 /* Setting a max stack size keeps wine from getting killed
3222 for hogging memory. Windows allocates all this memory at once, so
3223 no program would realistically set a value above our maximum. */
3224 return editor
->nUndoLimit
;
3227 return !list_empty( &editor
->undo_stack
);
3229 return !list_empty( &editor
->redo_stack
);
3230 case WM_UNDO
: /* FIXME: actually not the same */
3232 return ME_Undo(editor
);
3234 return ME_Redo(editor
);
3237 /* these flags are equivalent to the ES_* counterparts */
3238 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3239 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3240 DWORD settings
= editor
->styleFlags
& mask
;
3246 /* these flags are equivalent to ES_* counterparts, except for
3247 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3248 * but is still stored in editor->styleFlags. */
3249 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3250 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3251 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3252 DWORD settings
= mask
& editor
->styleFlags
;
3253 DWORD oldSettings
= settings
;
3254 DWORD changedSettings
;
3270 changedSettings
= oldSettings
^ settings
;
3272 if (changedSettings
) {
3273 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3275 if (changedSettings
& ECO_SELECTIONBAR
)
3277 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3278 if (settings
& ECO_SELECTIONBAR
) {
3279 assert(!editor
->selofs
);
3280 editor
->selofs
= SELECTIONBAR_WIDTH
;
3281 editor
->rcFormat
.left
+= editor
->selofs
;
3283 editor
->rcFormat
.left
-= editor
->selofs
;
3286 ME_RewrapRepaint(editor
);
3289 if (changedSettings
& settings
& ECO_VERTICAL
)
3290 FIXME("ECO_VERTICAL not implemented yet!\n");
3291 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3292 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3293 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3294 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3295 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3296 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3297 if (changedSettings
& settings
& ECO_WANTRETURN
)
3298 FIXME("ECO_WANTRETURN not implemented yet!\n");
3299 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3300 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3307 handle_EM_EXSETSEL( editor
, wParam
, lParam
);
3310 case EM_SETSCROLLPOS
:
3312 POINT
*point
= (POINT
*)lParam
;
3313 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3316 case EM_AUTOURLDETECT
:
3318 if (wParam
==1 || wParam
==0)
3320 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3323 return E_INVALIDARG
;
3325 case EM_GETAUTOURLDETECT
:
3327 return editor
->AutoURLDetect_bEnable
;
3331 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3333 return handle_EM_EXSETSEL( editor
, range
.cpMin
, range
.cpMax
);
3335 case EM_SHOWSCROLLBAR
:
3348 flags
= WS_HSCROLL
|WS_VSCROLL
;
3355 editor
->styleFlags
|= flags
;
3356 if (flags
& WS_HSCROLL
)
3357 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3358 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3359 if (flags
& WS_VSCROLL
)
3360 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3361 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3363 editor
->styleFlags
&= ~flags
;
3364 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3371 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3374 BOOL bRtf
, bUnicode
, bSelection
, bUTF8
;
3375 int oldModify
= editor
->nModifyStep
;
3376 static const char utf8_bom
[] = {0xef, 0xbb, 0xbf};
3378 if (!pStruct
) return 0;
3380 /* If we detect ascii rtf at the start of the string,
3381 * we know it isn't unicode. */
3382 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3383 !strncmp((char *)lParam
, "{\\urtf", 6)));
3384 bUnicode
= !bRtf
&& pStruct
->codepage
== CP_UNICODE
;
3385 bUTF8
= (lParam
&& (!strncmp((char *)lParam
, utf8_bom
, 3)));
3387 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3388 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3389 pStruct
->flags
, pStruct
->codepage
);
3391 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3393 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3394 style
= ME_GetSelectionInsertStyle(editor
);
3395 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3398 ME_SetCursorToStart(editor
, &start
);
3399 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3400 style
= editor
->pBuffer
->pDefaultStyle
;
3404 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3406 /* FIXME: The length returned doesn't include the rtf control
3407 * characters, only the actual text. */
3408 len
= lParam
? strlen((char *)lParam
) : 0;
3411 if (bUTF8
&& !bUnicode
) {
3412 wszText
= ME_ToUnicode(CP_UTF8
, (void *)(lParam
+3), &len
);
3413 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3414 ME_EndToUnicode(CP_UTF8
, wszText
);
3416 wszText
= ME_ToUnicode(pStruct
->codepage
, (void *)lParam
, &len
);
3417 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3418 ME_EndToUnicode(pStruct
->codepage
, wszText
);
3423 ME_ReleaseStyle(style
);
3424 ME_UpdateSelectionLinkAttribute(editor
);
3428 ME_SetCursorToStart(editor
, &cursor
);
3429 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3431 ME_CommitUndo(editor
);
3432 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3434 editor
->nModifyStep
= oldModify
;
3435 ME_EmptyUndoStack(editor
);
3437 ME_UpdateRepaint(editor
, FALSE
);
3440 case EM_SETBKGNDCOLOR
:
3443 if (editor
->rgbBackColor
!= -1) {
3444 DeleteObject(editor
->hbrBackground
);
3445 lColor
= editor
->rgbBackColor
;
3447 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3451 editor
->rgbBackColor
= -1;
3452 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3456 editor
->rgbBackColor
= lParam
;
3457 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3459 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3460 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3464 return editor
->nModifyStep
== 0 ? 0 : -1;
3468 editor
->nModifyStep
= 1;
3470 editor
->nModifyStep
= 0;
3474 case EM_SETREADONLY
:
3477 editor
->styleFlags
|= ES_READONLY
;
3479 editor
->styleFlags
&= ~ES_READONLY
;
3482 case EM_SETEVENTMASK
:
3484 DWORD nOldMask
= editor
->nEventMask
;
3486 editor
->nEventMask
= lParam
;
3489 case EM_GETEVENTMASK
:
3490 return editor
->nEventMask
;
3491 case EM_SETCHARFORMAT
:
3493 CHARFORMAT2W buf
, *p
;
3494 BOOL bRepaint
= TRUE
;
3495 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3496 if (p
== NULL
) return 0;
3497 if (wParam
& SCF_ALL
) {
3498 if (editor
->mode
& TM_PLAINTEXT
) {
3499 ME_SetDefaultCharFormat(editor
, p
);
3502 ME_SetCursorToStart(editor
, &start
);
3503 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3504 editor
->nModifyStep
= 1;
3506 } else if (wParam
& SCF_SELECTION
) {
3507 if (editor
->mode
& TM_PLAINTEXT
)
3509 if (wParam
& SCF_WORD
) {
3510 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3513 bRepaint
= ME_IsSelection(editor
);
3514 ME_SetSelectionCharFormat(editor
, p
);
3515 if (bRepaint
) editor
->nModifyStep
= 1;
3516 } else { /* SCF_DEFAULT */
3517 ME_SetDefaultCharFormat(editor
, p
);
3519 ME_CommitUndo(editor
);
3522 ME_WrapMarkedParagraphs(editor
);
3523 ME_UpdateScrollBar(editor
);
3528 case EM_GETCHARFORMAT
:
3530 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3531 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3532 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3533 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3534 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3536 tmp
.cbSize
= sizeof(tmp
);
3538 ME_GetDefaultCharFormat(editor
, &tmp
);
3540 ME_GetSelectionCharFormat(editor
, &tmp
);
3541 ME_CopyToCFAny(dst
, &tmp
);
3544 case EM_SETPARAFORMAT
:
3546 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3547 ME_WrapMarkedParagraphs(editor
);
3548 ME_UpdateScrollBar(editor
);
3550 ME_CommitUndo(editor
);
3553 case EM_GETPARAFORMAT
:
3554 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3555 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3556 case EM_GETFIRSTVISIBLELINE
:
3558 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3559 int y
= editor
->vert_si
.nPos
;
3564 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3565 if (p
->type
== diTextEnd
)
3567 if (p
->type
== diParagraph
) {
3568 ypara
= p
->member
.para
.pt
.y
;
3571 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3572 yend
= ystart
+ p
->member
.row
.nHeight
;
3580 case EM_HIDESELECTION
:
3582 editor
->bHideSelection
= (wParam
!= 0);
3583 ME_InvalidateSelection(editor
);
3588 if (!(editor
->styleFlags
& ES_MULTILINE
))
3590 ME_ScrollDown( editor
, lParam
* get_default_line_height( editor
) );
3596 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3597 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3598 ME_CommitUndo(editor
);
3599 ME_UpdateRepaint(editor
, TRUE
);
3604 int from
, to
, nStartCursor
;
3607 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3608 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &len
);
3609 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3611 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3612 style
= ME_GetSelectionInsertStyle(editor
);
3613 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3614 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3615 ME_ReleaseStyle(style
);
3616 /* drop temporary style if line end */
3618 * FIXME question: does abc\n mean: put abc,
3619 * clear temp style, put \n? (would require a change)
3621 if (len
>0 && wszText
[len
-1] == '\n')
3622 ME_ClearTempStyle(editor
);
3623 ME_EndToUnicode(codepage
, wszText
);
3624 ME_CommitUndo(editor
);
3625 ME_UpdateSelectionLinkAttribute(editor
);
3627 ME_EmptyUndoStack(editor
);
3628 ME_UpdateRepaint(editor
, FALSE
);
3631 case EM_SCROLLCARET
:
3632 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3639 BOOL bRepaint
= LOWORD(lParam
);
3642 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3643 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3644 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3645 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3646 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3647 if (editor
->mode
& TM_RICHTEXT
) {
3649 ME_SetCursorToStart(editor
, &start
);
3650 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3652 ME_SetDefaultCharFormat(editor
, &fmt
);
3654 ME_CommitUndo(editor
);
3655 ME_MarkAllForWrapping(editor
);
3656 ME_WrapMarkedParagraphs(editor
);
3657 ME_UpdateScrollBar(editor
);
3665 ME_SetCursorToStart(editor
, &cursor
);
3666 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3669 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3670 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3671 !strncmp((char *)lParam
, "{\\urtf", 6))
3673 /* Undocumented: WM_SETTEXT supports RTF text */
3674 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3679 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3680 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &textLen
);
3681 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3686 /* uses default style! */
3687 if (!(editor
->styleFlags
& ES_MULTILINE
))
3692 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3695 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3697 ME_EndToUnicode(codepage
, wszText
);
3701 TRACE("WM_SETTEXT - NULL\n");
3702 ME_SetCursorToStart(editor
, &cursor
);
3703 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3704 ME_SetSelection(editor
, 0, 0);
3705 editor
->nModifyStep
= 0;
3706 ME_CommitUndo(editor
);
3707 ME_EmptyUndoStack(editor
);
3708 ME_UpdateRepaint(editor
, FALSE
);
3713 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3714 if (IsClipboardFormatAvailable(nRTFFormat
))
3716 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3721 case WM_MBUTTONDOWN
:
3727 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3728 int nChars
= nTo
- nFrom
;
3729 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3731 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3733 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3734 ME_CommitUndo(editor
);
3735 ME_UpdateRepaint(editor
, TRUE
);
3739 case WM_GETTEXTLENGTH
:
3741 GETTEXTLENGTHEX how
;
3743 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3744 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3745 how
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3746 return ME_GetTextLengthEx(editor
, &how
);
3748 case EM_GETTEXTLENGTHEX
:
3749 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3753 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3754 ex
.flags
= GT_USECRLF
;
3755 ex
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3756 ex
.lpDefaultChar
= NULL
;
3757 ex
.lpUsedDefChar
= NULL
;
3758 return ME_GetTextEx(editor
, &ex
, lParam
);
3761 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3764 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3765 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3766 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3767 nTo
- nFrom
, unicode
);
3769 case EM_GETSCROLLPOS
:
3771 POINT
*point
= (POINT
*)lParam
;
3772 point
->x
= editor
->horz_si
.nPos
;
3773 point
->y
= editor
->vert_si
.nPos
;
3774 /* 16-bit scaled value is returned as stored in scrollinfo */
3775 if (editor
->horz_si
.nMax
> 0xffff)
3776 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3777 if (editor
->vert_si
.nMax
> 0xffff)
3778 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3781 case EM_GETTEXTRANGE
:
3783 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3785 int nStart
= rng
->chrg
.cpMin
;
3786 int nEnd
= rng
->chrg
.cpMax
;
3787 int textlength
= ME_GetTextLength(editor
);
3789 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3790 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3791 if (nStart
< 0) return 0;
3792 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3794 if (nStart
>= nEnd
) return 0;
3796 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3797 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3801 ME_DisplayItem
*run
;
3802 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3803 unsigned int nCharsLeft
= nMaxChars
;
3804 char *dest
= (char *) lParam
;
3805 BOOL wroteNull
= FALSE
;
3807 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3808 unicode
? "Unicode" : "Ansi");
3810 run
= ME_FindRowWithNumber(editor
, wParam
);
3814 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3815 && run
->type
== diRun
)
3817 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3820 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3823 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3825 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
3826 nCharsLeft
, NULL
, NULL
);
3827 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3828 nCharsLeft
-= nCopy
;
3831 /* append line termination, space allowing */
3835 *((WCHAR
*)dest
) = '\0';
3842 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3843 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3845 case EM_GETLINECOUNT
:
3847 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3850 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3852 while (item
!= editor
->pBuffer
->pLast
)
3854 assert(item
->type
== diParagraph
);
3855 prev_para
= ME_FindItemBack(item
, diRun
);
3857 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3859 nRows
+= item
->member
.para
.nRows
;
3860 item
= item
->member
.para
.next_para
;
3862 last_para
= ME_FindItemBack(item
, diRun
);
3864 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3865 if (editor
->bEmulateVersion10
&& prev_para
&&
3866 last_para
->member
.run
.nCharOfs
== 0 &&
3867 prev_para
->member
.run
.len
== 1 &&
3868 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
3870 /* In 1.0 emulation, the last solitary \r at the very end of the text
3871 (if one exists) is NOT a line break.
3872 FIXME: this is an ugly hack. This should have a more regular model. */
3876 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3877 return max(1, nRows
);
3879 case EM_LINEFROMCHAR
:
3882 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3884 return ME_RowNumberFromCharOfs(editor
, wParam
);
3886 case EM_EXLINEFROMCHAR
:
3889 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3891 return ME_RowNumberFromCharOfs(editor
, lParam
);
3895 ME_DisplayItem
*item
, *para
;
3899 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3901 item
= ME_FindRowWithNumber(editor
, wParam
);
3904 para
= ME_GetParagraph(item
);
3905 item
= ME_FindItemFwd(item
, diRun
);
3906 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3907 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3912 ME_DisplayItem
*item
, *item_end
;
3913 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3914 ME_DisplayItem
*para
, *run
;
3916 if (wParam
> ME_GetTextLength(editor
))
3920 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3923 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3924 item
= ME_RowStart(run
);
3925 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3926 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3927 if (item_end
->type
== diStartRow
) {
3928 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3930 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3931 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3932 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
3934 nChars
= nNextLineOfs
- nThisLineOfs
;
3935 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3938 case EM_EXLIMITTEXT
:
3940 if ((int)lParam
< 0)
3943 editor
->nTextLimit
= 65536;
3945 editor
->nTextLimit
= (int) lParam
;
3951 editor
->nTextLimit
= 65536;
3953 editor
->nTextLimit
= (int) wParam
;
3956 case EM_GETLIMITTEXT
:
3958 return editor
->nTextLimit
;
3964 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3965 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3968 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3969 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3970 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3973 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3974 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3982 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3983 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3986 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3987 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3988 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3991 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3992 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3998 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3999 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4001 case EM_FINDTEXTEXW
:
4003 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4004 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4007 if (!wParam
|| !lParam
)
4009 *(int *)wParam
= editor
->nZoomNumerator
;
4010 *(int *)lParam
= editor
->nZoomDenominator
;
4013 return ME_SetZoom(editor
, wParam
, lParam
);
4014 case EM_CHARFROMPOS
:
4017 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
4019 return ME_GetCursorOfs(&cursor
);
4023 case EM_POSFROMCHAR
:
4025 ME_DisplayItem
*pPara
, *pRun
;
4026 int nCharOfs
, nOffset
, nLength
;
4030 /* detect which API version we're dealing with */
4031 if (wParam
>= 0x40000)
4033 nLength
= ME_GetTextLength(editor
);
4034 nCharOfs
= min(nCharOfs
, nLength
);
4035 nCharOfs
= max(nCharOfs
, 0);
4037 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
4038 assert(pRun
->type
== diRun
);
4039 pt
.y
= pRun
->member
.run
.pt
.y
;
4040 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
4041 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
4042 pt
.x
+= editor
->rcFormat
.left
;
4044 pt
.x
-= editor
->horz_si
.nPos
;
4045 pt
.y
-= editor
->vert_si
.nPos
;
4047 if (wParam
>= 0x40000) {
4048 *(POINTL
*)wParam
= pt
;
4050 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
4057 ME_SetDefaultFormatRect(editor
);
4059 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
4060 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
4061 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
4063 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
4064 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
4066 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
4068 if (editor
->styleFlags
& WS_VSCROLL
)
4070 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
4071 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
4073 if (editor
->styleFlags
& WS_HSCROLL
)
4075 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
4076 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
4082 text
= (unicode
? (void*)((CREATESTRUCTW
*)lParam
)->lpszName
4083 : (void*)((CREATESTRUCTA
*)lParam
)->lpszName
);
4089 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
4090 textW
= ME_ToUnicode(codepage
, text
, &len
);
4091 if (!(editor
->styleFlags
& ES_MULTILINE
))
4094 while(textW
[len
] != '\0' && textW
[len
] != '\r' && textW
[len
] != '\n')
4097 ME_InsertTextFromCursor(editor
, 0, textW
, len
, editor
->pBuffer
->pDefaultStyle
);
4098 ME_EndToUnicode(codepage
, textW
);
4099 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4100 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
4103 ME_CommitUndo(editor
);
4104 ME_WrapMarkedParagraphs(editor
);
4105 ME_MoveCaret(editor
);
4109 ME_DestroyEditor(editor
);
4113 return ME_SetCursor(editor
);
4115 case WM_LBUTTONDBLCLK
:
4116 case WM_LBUTTONDOWN
:
4118 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4119 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4120 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4122 ITextHost_TxSetFocus(editor
->texthost
);
4123 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
4124 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
4125 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
4126 editor
->bMouseCaptured
= TRUE
;
4127 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4128 if (!ME_SetCursor(editor
)) goto do_default
;
4132 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4133 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4135 if (editor
->bMouseCaptured
)
4136 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4137 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4138 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4139 if (editor
->bMouseCaptured
)
4140 ME_SetCursor(editor
);
4143 if (editor
->bMouseCaptured
) {
4144 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4145 editor
->bMouseCaptured
= FALSE
;
4147 if (editor
->nSelectionType
== stDocument
)
4148 editor
->nSelectionType
= stPosition
;
4149 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4150 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4154 ME_SetCursor(editor
);
4155 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4159 case WM_RBUTTONDOWN
:
4160 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4161 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4162 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4165 case WM_CONTEXTMENU
:
4166 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4170 editor
->bHaveFocus
= TRUE
;
4171 ME_ShowCaret(editor
);
4172 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4175 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4176 editor
->bHaveFocus
= FALSE
;
4177 editor
->wheel_remain
= 0;
4178 ME_HideCaret(editor
);
4179 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4182 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4185 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4186 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4190 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4191 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4193 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4197 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4198 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4200 return ME_Char(editor
, wParam
, lParam
, unicode
);
4204 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4205 if(wParam
<= 0x000fffff)
4207 if(wParam
> 0xffff) /* convert to surrogates */
4210 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4211 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4213 ME_Char(editor
, wParam
, 0, TRUE
);
4219 case EM_STOPGROUPTYPING
:
4220 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4224 const int scrollUnit
= 7;
4226 switch(LOWORD(wParam
))
4229 ME_ScrollAbs(editor
, 0, 0);
4232 ME_ScrollAbs(editor
,
4233 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4234 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4237 ME_ScrollLeft(editor
, scrollUnit
);
4240 ME_ScrollRight(editor
, scrollUnit
);
4243 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4246 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4249 case SB_THUMBPOSITION
:
4251 int pos
= HIWORD(wParam
);
4252 if (editor
->horz_si
.nMax
> 0xffff)
4253 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4254 ME_HScrollAbs(editor
, pos
);
4260 case EM_SCROLL
: /* fall through */
4264 int lineHeight
= get_default_line_height( editor
);
4266 origNPos
= editor
->vert_si
.nPos
;
4268 switch(LOWORD(wParam
))
4271 ME_ScrollAbs(editor
, 0, 0);
4274 ME_ScrollAbs(editor
,
4275 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4276 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4279 ME_ScrollUp(editor
,lineHeight
);
4282 ME_ScrollDown(editor
,lineHeight
);
4285 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4288 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4291 case SB_THUMBPOSITION
:
4293 int pos
= HIWORD(wParam
);
4294 if (editor
->vert_si
.nMax
> 0xffff)
4295 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4296 ME_VScrollAbs(editor
, pos
);
4300 if (msg
== EM_SCROLL
)
4301 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4309 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4310 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4313 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4315 delta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4317 /* if scrolling changes direction, ignore left overs */
4318 if ((delta
< 0 && editor
->wheel_remain
< 0) ||
4319 (delta
> 0 && editor
->wheel_remain
> 0))
4320 editor
->wheel_remain
+= delta
;
4322 editor
->wheel_remain
= delta
;
4324 if (editor
->wheel_remain
)
4328 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4332 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4334 numerator
+= calc_wheel_change( &editor
->wheel_remain
, 10 );
4335 if (numerator
>= 10 && numerator
<= 500)
4336 ME_SetZoom(editor
, numerator
, 100);
4341 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES
, 0, &max_lines
, 0 );
4343 lines
= calc_wheel_change( &editor
->wheel_remain
, (int)max_lines
);
4345 ME_ScrollDown( editor
, -lines
* get_default_line_height( editor
) );
4352 *((RECT
*)lParam
) = editor
->rcFormat
;
4353 if (editor
->bDefaultFormatRect
)
4354 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4364 RECT
*rc
= (RECT
*)lParam
;
4366 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4367 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4370 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4371 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4372 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4373 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4374 } else if (wParam
== 1) {
4375 /* MSDN incorrectly says a wParam value of 1 causes the
4376 * lParam rect to be used as a relative offset,
4377 * however, the tests show it just prevents min/max bound
4379 editor
->rcFormat
.top
= rc
->top
- border
;
4380 editor
->rcFormat
.left
= rc
->left
- border
;
4381 editor
->rcFormat
.bottom
= rc
->bottom
;
4382 editor
->rcFormat
.right
= rc
->right
+ border
;
4386 editor
->bDefaultFormatRect
= FALSE
;
4390 ME_SetDefaultFormatRect(editor
);
4391 editor
->bDefaultFormatRect
= TRUE
;
4393 ME_MarkAllForWrapping(editor
);
4394 ME_WrapMarkedParagraphs(editor
);
4395 ME_UpdateScrollBar(editor
);
4396 if (msg
!= EM_SETRECTNP
)
4400 case EM_REQUESTRESIZE
:
4401 ME_SendRequestResize(editor
, TRUE
);
4409 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4410 if (editor
->bDefaultFormatRect
) {
4411 ME_SetDefaultFormatRect(editor
);
4413 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4414 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4416 editor
->prevClientRect
= clientRect
;
4417 ME_RewrapRepaint(editor
);
4420 /* IME messages to make richedit controls IME aware */
4421 case WM_IME_SETCONTEXT
:
4422 case WM_IME_CONTROL
:
4424 case WM_IME_COMPOSITIONFULL
:
4426 case WM_IME_STARTCOMPOSITION
:
4428 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4429 ME_DeleteSelection(editor
);
4430 ME_CommitUndo(editor
);
4431 ME_UpdateRepaint(editor
, FALSE
);
4434 case WM_IME_COMPOSITION
:
4438 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4439 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4440 ME_DeleteSelection(editor
);
4441 ME_SaveTempStyle(editor
);
4442 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4444 LPWSTR lpCompStr
= NULL
;
4446 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4448 dwIndex
= GCS_COMPSTR
;
4450 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4451 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4452 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4453 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4454 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4455 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4457 if (dwIndex
== GCS_COMPSTR
)
4458 ME_SetSelection(editor
,editor
->imeStartIndex
,
4459 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4461 ME_ReleaseStyle(style
);
4462 ME_CommitUndo(editor
);
4463 ME_UpdateRepaint(editor
, FALSE
);
4466 case WM_IME_ENDCOMPOSITION
:
4468 ME_DeleteSelection(editor
);
4469 editor
->imeStartIndex
=-1;
4472 case EM_GETOLEINTERFACE
:
4475 if (!CreateIRichEditOle(editor
, (LPVOID
*)&editor
->reOle
))
4477 *(LPVOID
*)lParam
= editor
->reOle
;
4478 IRichEditOle_AddRef(editor
->reOle
);
4481 case EM_GETPASSWORDCHAR
:
4483 return editor
->cPasswordMask
;
4485 case EM_SETOLECALLBACK
:
4486 if(editor
->lpOleCallback
)
4487 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4488 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4489 if(editor
->lpOleCallback
)
4490 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4492 case EM_GETWORDBREAKPROC
:
4493 return (LRESULT
)editor
->pfnWordBreak
;
4494 case EM_SETWORDBREAKPROC
:
4496 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4498 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4499 return (LRESULT
)pfnOld
;
4501 case EM_GETTEXTMODE
:
4502 return editor
->mode
;
4503 case EM_SETTEXTMODE
:
4508 if (ME_GetTextLength(editor
) ||
4509 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4510 return E_UNEXPECTED
;
4512 /* Check for mutually exclusive flags in adjacent bits of wParam */
4513 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4514 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4515 return E_INVALIDARG
;
4517 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4519 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4520 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4521 if (wParam
& TM_PLAINTEXT
) {
4522 /* Clear selection since it should be possible to select the
4523 * end of text run for rich text */
4524 ME_InvalidateSelection(editor
);
4525 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4526 editor
->pCursors
[1] = editor
->pCursors
[0];
4527 /* plain text can only have the default style. */
4528 ME_ClearTempStyle(editor
);
4529 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4530 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4531 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4534 /* FIXME: Currently no support for undo level and code page options */
4535 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4538 case EM_SETPASSWORDCHAR
:
4540 editor
->cPasswordMask
= wParam
;
4541 ME_RewrapRepaint(editor
);
4544 case EM_SETTARGETDEVICE
:
4547 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4548 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4550 editor
->bWordWrap
= new;
4551 editor
->nAvailWidth
= 0; /* wrap to client area */
4552 ME_RewrapRepaint(editor
);
4555 int width
= max(0, lParam
);
4556 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4557 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4559 editor
->nAvailWidth
= width
;
4560 editor
->bWordWrap
= TRUE
;
4561 ME_RewrapRepaint(editor
);
4563 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4568 *phresult
= S_FALSE
;
4574 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4575 LPARAM lParam
, BOOL unicode
)
4577 ME_TextEditor
*editor
;
4579 LRESULT lresult
= 0;
4581 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4582 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4584 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4587 if (msg
== WM_NCCREATE
)
4589 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4590 ITextHost
*texthost
;
4592 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4593 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4594 return texthost
!= NULL
;
4598 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4610 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4611 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4612 ME_SendOldNotify(editor
, EN_UPDATE
);
4613 /* Erase area outside of the formatting rectangle */
4614 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4617 rc
.bottom
= editor
->rcFormat
.top
;
4618 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4619 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4621 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4623 rc
.top
= editor
->rcFormat
.bottom
;
4624 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4625 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4627 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4629 rc
.right
= editor
->rcFormat
.left
;
4630 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4631 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4633 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4635 rc
.left
= editor
->rcFormat
.right
;
4636 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4637 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4640 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4641 EndPaint(editor
->hWnd
, &ps
);
4646 HDC hDC
= (HDC
)wParam
;
4649 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4650 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4656 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4657 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4659 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4660 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4661 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4662 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4665 case EM_SETREADONLY
:
4668 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4669 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4670 dwStyle
&= ~ES_READONLY
;
4672 dwStyle
|= ES_READONLY
;
4673 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4677 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4680 if (hresult
== S_FALSE
)
4681 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4683 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4684 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4689 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4691 BOOL unicode
= TRUE
;
4693 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4694 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4697 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4700 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4702 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4705 /******************************************************************
4706 * RichEditANSIWndProc (RICHED20.10)
4708 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4710 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4713 /******************************************************************
4714 * RichEdit10ANSIWndProc (RICHED20.9)
4716 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4718 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4720 ITextHost
*texthost
;
4721 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4723 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4724 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4725 return texthost
!= NULL
;
4727 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4730 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4732 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4735 /* Fill buffer with srcChars unicode characters from the start cursor.
4737 * buffer: destination buffer
4738 * buflen: length of buffer in characters excluding the NULL terminator.
4739 * start: start of editor text to copy into buffer.
4740 * srcChars: Number of characters to use from the editor text.
4741 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4743 * returns the number of characters written excluding the NULL terminator.
4745 * The written text is always NULL terminated.
4747 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4748 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
,
4751 ME_DisplayItem
*pRun
, *pNextRun
;
4752 const WCHAR
*pStart
= buffer
;
4753 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4757 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4758 if (editor
->bEmulateVersion10
) bCRLF
= FALSE
;
4762 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4764 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4765 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4767 while (srcChars
&& buflen
&& pNextRun
)
4769 int nFlags
= pRun
->member
.run
.nFlags
;
4771 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4773 if (buflen
== 1) break;
4774 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4775 * EM_GETTEXTEX, however, this is done for copying text which
4776 * also uses this function. */
4777 srcChars
-= min(nLen
, srcChars
);
4781 nLen
= min(nLen
, srcChars
);
4785 nLen
= min(nLen
, buflen
);
4788 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4793 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4795 nLen
= pRun
->member
.run
.len
;
4796 str
= get_text( &pRun
->member
.run
, 0 );
4798 /* append '\r' to the last paragraph. */
4799 if (pRun
->next
->type
== diTextEnd
&& bEOP
)
4805 return buffer
- pStart
;
4808 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4813 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4814 wcW
.lpfnWndProc
= RichEditWndProcW
;
4816 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4817 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4819 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4820 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4821 wcW
.lpszMenuName
= NULL
;
4823 if (is_version_nt())
4825 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4826 if (!RegisterClassW(&wcW
)) return FALSE
;
4827 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4828 if (!RegisterClassW(&wcW
)) return FALSE
;
4832 /* WNDCLASSA/W have the same layout */
4833 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4834 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4835 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4836 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4839 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4840 wcA
.lpfnWndProc
= RichEditWndProcA
;
4842 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4843 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4845 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4846 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4847 wcA
.lpszMenuName
= NULL
;
4848 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4849 if (!RegisterClassA(&wcA
)) return FALSE
;
4850 wcA
.lpszClassName
= "RichEdit50A";
4851 if (!RegisterClassA(&wcA
)) return FALSE
;
4856 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4857 /* FIXME: Not implemented */
4858 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4859 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4860 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4863 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4864 /* FIXME: Not implemented */
4865 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4866 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4867 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4870 /******************************************************************
4871 * REExtendedRegisterClass (RICHED20.8)
4873 * FIXME undocumented
4874 * Need to check for errors and implement controls and callbacks
4876 LRESULT WINAPI
REExtendedRegisterClass(void)
4881 FIXME("semi stub\n");
4885 wcW
.hInstance
= NULL
;
4888 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4889 wcW
.lpszMenuName
= NULL
;
4891 if (!ME_ListBoxRegistered
)
4893 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4894 wcW
.lpfnWndProc
= REListWndProc
;
4895 wcW
.lpszClassName
= REListBox20W
;
4896 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4899 if (!ME_ComboBoxRegistered
)
4901 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4902 wcW
.lpfnWndProc
= REComboWndProc
;
4903 wcW
.lpszClassName
= REComboBox20W
;
4904 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4908 if (ME_ListBoxRegistered
)
4910 if (ME_ComboBoxRegistered
)
4916 static BOOL
isurlspecial(WCHAR c
)
4918 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4919 return strchrW( special_chars
, c
) != NULL
;
4923 * This proc takes a selection, and scans it forward in order to select the span
4924 * of a possible URL candidate. A possible URL candidate must start with isalnum
4925 * or one of the following special characters: *|/\+%#@ and must consist entirely
4926 * of the characters allowed to start the URL, plus : (colon) which may occur
4927 * at most once, and not at either end.
4929 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
4930 const ME_Cursor
*start
,
4932 ME_Cursor
*candidate_min
,
4933 ME_Cursor
*candidate_max
)
4935 ME_Cursor cursor
= *start
;
4936 BOOL foundColon
= FALSE
;
4937 BOOL candidateStarted
= FALSE
;
4938 WCHAR lastAcceptedChar
= '\0';
4942 WCHAR
*strStart
= get_text( &cursor
.pRun
->member
.run
, 0 );
4943 WCHAR
*str
= strStart
+ cursor
.nOffset
;
4944 int nLen
= cursor
.pRun
->member
.run
.len
- cursor
.nOffset
;
4947 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4949 /* Find start of candidate */
4950 if (!candidateStarted
)
4955 if (isalnumW(*str
) || isurlspecial(*str
))
4957 cursor
.nOffset
= str
- strStart
;
4958 *candidate_min
= cursor
;
4959 candidateStarted
= TRUE
;
4960 lastAcceptedChar
= *str
++;
4967 /* Find end of candidate */
4968 if (candidateStarted
) {
4972 if (*str
== ':' && !foundColon
) {
4974 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
4975 cursor
.nOffset
= str
- strStart
;
4976 if (lastAcceptedChar
== ':')
4977 ME_MoveCursorChars(editor
, &cursor
, -1);
4978 *candidate_max
= cursor
;
4981 lastAcceptedChar
= *str
++;
4985 /* End of paragraph: skip it if before candidate span, or terminates
4986 current active span */
4987 if (candidateStarted
) {
4988 if (lastAcceptedChar
== ':')
4989 ME_MoveCursorChars(editor
, &cursor
, -1);
4990 *candidate_max
= cursor
;
4995 /* Reaching this point means no span was found, so get next span */
4996 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
4997 if (candidateStarted
) {
4998 /* There are no further runs, so take end of text as end of candidate */
4999 cursor
.nOffset
= str
- strStart
;
5000 if (lastAcceptedChar
== ':')
5001 ME_MoveCursorChars(editor
, &cursor
, -1);
5002 *candidate_max
= cursor
;
5005 *candidate_max
= *candidate_min
= cursor
;
5011 if (candidateStarted
) {
5012 /* There are no further runs, so take end of text as end of candidate */
5013 if (lastAcceptedChar
== ':')
5014 ME_MoveCursorChars(editor
, &cursor
, -1);
5015 *candidate_max
= cursor
;
5018 *candidate_max
= *candidate_min
= cursor
;
5023 * This proc evaluates the selection and returns TRUE if it can be considered an URL
5025 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
5027 #define MAX_PREFIX_LEN 9
5029 const WCHAR text
[MAX_PREFIX_LEN
];
5032 {{'p','r','o','s','p','e','r','o',':'}, 9},
5033 {{'t','e','l','n','e','t',':'}, 7},
5034 {{'g','o','p','h','e','r',':'}, 7},
5035 {{'m','a','i','l','t','o',':'}, 7},
5036 {{'h','t','t','p','s',':'}, 6},
5037 {{'f','i','l','e',':'}, 5},
5038 {{'n','e','w','s',':'}, 5},
5039 {{'w','a','i','s',':'}, 5},
5040 {{'n','n','t','p',':'}, 5},
5041 {{'h','t','t','p',':'}, 5},
5042 {{'w','w','w','.'}, 4},
5043 {{'f','t','p',':'}, 4},
5045 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
5048 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, FALSE
, FALSE
);
5049 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
5051 if (nChars
< prefixes
[i
].length
) continue;
5052 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
5056 #undef MAX_PREFIX_LEN
5060 * This proc walks through the indicated selection and evaluates whether each
5061 * section identified by ME_FindNextURLCandidate and in-between sections have
5062 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5063 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5065 * Since this function can cause runs to be split, do not depend on the value
5066 * of the start cursor at the end of the function.
5068 * nChars may be set to INT_MAX to update to the end of the text.
5070 * Returns TRUE if at least one section was modified.
5072 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
5074 BOOL modified
= FALSE
;
5075 ME_Cursor startCur
= *start
;
5077 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
5082 ME_Cursor candidateStart
, candidateEnd
;
5084 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
5085 &candidateStart
, &candidateEnd
))
5087 /* Section before candidate is not an URL */
5088 int cMin
= ME_GetCursorOfs(&candidateStart
);
5089 int cMax
= ME_GetCursorOfs(&candidateEnd
);
5091 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
5092 candidateStart
= candidateEnd
;
5093 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
5097 /* No more candidates until end of selection */
5101 if (startCur
.pRun
!= candidateStart
.pRun
||
5102 startCur
.nOffset
!= candidateStart
.nOffset
)
5104 /* CFE_LINK effect should be consistently unset */
5105 link
.cbSize
= sizeof(link
);
5106 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5107 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
5109 /* CFE_LINK must be unset from this range */
5110 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5111 link
.cbSize
= sizeof(link
);
5112 link
.dwMask
= CFM_LINK
;
5114 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5115 /* Update candidateEnd since setting character formats may split
5116 * runs, which can cause a cursor to be at an invalid offset within
5118 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
5120 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
5121 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
5126 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
5127 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
5129 /* CFE_LINK effect should be consistently set */
5130 link
.cbSize
= sizeof(link
);
5131 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5132 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
5134 /* CFE_LINK must be set on this range */
5135 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5136 link
.cbSize
= sizeof(link
);
5137 link
.dwMask
= CFM_LINK
;
5138 link
.dwEffects
= CFE_LINK
;
5139 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5143 startCur
= candidateEnd
;
5144 } while (nChars
> 0);