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
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
);
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
);
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
);
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
);
2004 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2006 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, 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
;
2745 ed
->bEmulateVersion10
= bEmulateVersion10
;
2747 ITextHost_TxGetPropertyBits(texthost
,
2748 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2749 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2750 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2751 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2752 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2754 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2755 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2756 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2757 ed
->pBuffer
= ME_MakeText();
2758 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2759 ed
->nAvailWidth
= 0; /* wrap to client area */
2760 ME_MakeFirstParagraph(ed
);
2761 /* The four cursors are for:
2762 * 0 - The position where the caret is shown
2763 * 1 - The anchored end of the selection (for normal selection)
2764 * 2 & 3 - The anchored start and end respectively for word, line,
2765 * or paragraph selection.
2768 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2769 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2770 ed
->pCursors
[1] = ed
->pCursors
[0];
2771 ed
->pCursors
[2] = ed
->pCursors
[0];
2772 ed
->pCursors
[3] = ed
->pCursors
[1];
2773 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2774 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2776 ed
->rgbBackColor
= -1;
2777 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2778 ed
->bCaretAtEnd
= FALSE
;
2780 ed
->nModifyStep
= 0;
2781 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2782 list_init( &ed
->undo_stack
);
2783 list_init( &ed
->redo_stack
);
2784 ed
->nUndoStackSize
= 0;
2785 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2786 ed
->nUndoMode
= umAddToUndo
;
2787 ed
->nParagraphs
= 1;
2788 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2789 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2790 ed
->bHideSelection
= FALSE
;
2791 ed
->pfnWordBreak
= NULL
;
2792 ed
->lpOleCallback
= NULL
;
2793 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2794 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2795 ed
->AutoURLDetect_bEnable
= FALSE
;
2796 ed
->bHaveFocus
= FALSE
;
2797 ed
->bDialogMode
= FALSE
;
2798 ed
->bMouseCaptured
= FALSE
;
2799 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2801 ed
->pFontCache
[i
].nRefs
= 0;
2802 ed
->pFontCache
[i
].nAge
= 0;
2803 ed
->pFontCache
[i
].hFont
= NULL
;
2806 ME_CheckCharOffsets(ed
);
2807 ed
->bDefaultFormatRect
= TRUE
;
2808 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2810 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2811 ed
->selofs
= SELECTIONBAR_WIDTH
;
2812 ed
->styleFlags
|= ES_SELECTIONBAR
;
2816 ed
->nSelectionType
= stPosition
;
2818 ed
->cPasswordMask
= 0;
2819 if (props
& TXTBIT_USEPASSWORD
)
2820 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2822 if (props
& TXTBIT_AUTOWORDSEL
)
2823 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2824 if (props
& TXTBIT_MULTILINE
) {
2825 ed
->styleFlags
|= ES_MULTILINE
;
2826 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2828 ed
->bWordWrap
= FALSE
;
2830 if (props
& TXTBIT_READONLY
)
2831 ed
->styleFlags
|= ES_READONLY
;
2832 if (!(props
& TXTBIT_HIDESELECTION
))
2833 ed
->styleFlags
|= ES_NOHIDESEL
;
2834 if (props
& TXTBIT_SAVESELECTION
)
2835 ed
->styleFlags
|= ES_SAVESEL
;
2836 if (props
& TXTBIT_VERTICAL
)
2837 ed
->styleFlags
|= ES_VERTICAL
;
2838 if (props
& TXTBIT_DISABLEDRAG
)
2839 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2841 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2843 /* Default scrollbar information */
2844 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2845 ed
->vert_si
.nMin
= 0;
2846 ed
->vert_si
.nMax
= 0;
2847 ed
->vert_si
.nPage
= 0;
2848 ed
->vert_si
.nPos
= 0;
2850 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2851 ed
->horz_si
.nMin
= 0;
2852 ed
->horz_si
.nMax
= 0;
2853 ed
->horz_si
.nPage
= 0;
2854 ed
->horz_si
.nPos
= 0;
2856 ed
->wheel_remain
= 0;
2858 OleInitialize(NULL
);
2863 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2865 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2866 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2869 ME_ClearTempStyle(editor
);
2870 ME_EmptyUndoStack(editor
);
2873 ME_DestroyDisplayItem(p
);
2876 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2877 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2879 if (editor
->pFontCache
[i
].hFont
)
2880 DeleteObject(editor
->pFontCache
[i
].hFont
);
2882 if (editor
->rgbBackColor
!= -1)
2883 DeleteObject(editor
->hbrBackground
);
2884 if(editor
->lpOleCallback
)
2885 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2886 ITextHost_Release(editor
->texthost
);
2889 FREE_OBJ(editor
->pBuffer
);
2890 FREE_OBJ(editor
->pCursors
);
2895 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2900 case DLL_PROCESS_ATTACH
:
2901 DisableThreadLibraryCalls(hinstDLL
);
2902 me_heap
= HeapCreate (0, 0x10000, 0);
2903 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2904 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2908 case DLL_PROCESS_DETACH
:
2909 if (lpvReserved
) break;
2910 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2911 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2912 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2913 UnregisterClassA("RichEdit50A", 0);
2914 if (ME_ListBoxRegistered
)
2915 UnregisterClassW(REListBox20W
, 0);
2916 if (ME_ComboBoxRegistered
)
2917 UnregisterClassW(REComboBox20W
, 0);
2919 HeapDestroy (me_heap
);
2925 static inline int get_default_line_height( ME_TextEditor
*editor
)
2929 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
2930 height
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
2931 if (height
<= 0) height
= 24;
2936 static inline int calc_wheel_change( int *remain
, int amount_per_click
)
2938 int change
= amount_per_click
* (float)*remain
/ WHEEL_DELTA
;
2939 *remain
-= WHEEL_DELTA
* change
/ amount_per_click
;
2943 static const char * const edit_messages
[] = {
2972 "EM_SETPASSWORDCHAR",
2973 "EM_EMPTYUNDOBUFFER",
2974 "EM_GETFIRSTVISIBLELINE",
2976 "EM_SETWORDBREAKPROC",
2977 "EM_GETWORDBREAKPROC",
2978 "EM_GETPASSWORDCHAR",
2988 static const char * const richedit_messages
[] = {
2993 "EM_EXLINEFROMCHAR",
2999 "EM_GETOLEINTERFACE",
3009 "EM_SETOLECALLBACK",
3011 "EM_SETTARGETDEVICE",
3019 "EM_GETWORDBREAKPROCEX",
3020 "EM_SETWORDBREAKPROCEX",
3022 "EM_UNKNOWN_USER_83",
3027 "EM_STOPGROUPTYPING",
3031 "EM_GETAUTOURLDETECT",
3034 "EM_GETTEXTLENGTHEX",
3037 "EM_UNKNOWN_USER_98",
3038 "EM_UNKNOWN_USER_99",
3039 "EM_SETPUNCTUATION",
3040 "EM_GETPUNCTUATION",
3041 "EM_SETWORDWRAPMODE",
3042 "EM_GETWORDWRAPMODE",
3048 "EM_UNKNOWN_USER_109",
3049 "EM_UNKNOWN_USER_110",
3050 "EM_UNKNOWN_USER_111",
3051 "EM_UNKNOWN_USER_112",
3052 "EM_UNKNOWN_USER_113",
3053 "EM_UNKNOWN_USER_114",
3054 "EM_UNKNOWN_USER_115",
3055 "EM_UNKNOWN_USER_116",
3056 "EM_UNKNOWN_USER_117",
3057 "EM_UNKNOWN_USER_118",
3058 "EM_UNKNOWN_USER_119",
3059 "EM_SETLANGOPTIONS",
3060 "EM_GETLANGOPTIONS",
3061 "EM_GETIMECOMPMODE",
3065 "EM_SETIMEMODEBIAS",
3070 get_msg_name(UINT msg
)
3072 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
3073 return edit_messages
[msg
- EM_GETSEL
];
3074 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
3075 return richedit_messages
[msg
- EM_CANPASTE
];
3079 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3083 ME_Cursor cursor
; /* The start of the clicked text. */
3086 x
= (short)LOWORD(lParam
);
3087 y
= (short)HIWORD(lParam
);
3088 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3089 if (!isExact
) return;
3091 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3092 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3093 { /* The clicked run has CFE_LINK set */
3094 info
.nmhdr
.hwndFrom
= NULL
;
3095 info
.nmhdr
.idFrom
= 0;
3096 info
.nmhdr
.code
= EN_LINK
;
3098 info
.wParam
= wParam
;
3099 info
.lParam
= lParam
;
3101 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3102 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ cursor
.pRun
->member
.run
.len
;
3103 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3107 #define UNSUPPORTED_MSG(e) \
3109 FIXME(#e ": stub\n"); \
3110 *phresult = S_FALSE; \
3113 /* Handle messages for windowless and windoweded richedit controls.
3115 * The LRESULT that is returned is a return value for window procs,
3116 * and the phresult parameter is the COM return code needed by the
3117 * text services interface. */
3118 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3119 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3125 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3126 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3127 UNSUPPORTED_MSG(EM_FMTLINES
)
3128 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3129 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3130 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3131 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3132 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3133 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3134 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3135 UNSUPPORTED_MSG(EM_GETREDONAME
)
3136 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3137 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3138 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3139 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3140 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3141 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3142 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3143 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
3144 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3145 UNSUPPORTED_MSG(EM_SETMARGINS
)
3146 UNSUPPORTED_MSG(EM_SETPALETTE
)
3147 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3148 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3149 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3151 /* Messages specific to Richedit controls */
3154 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3156 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3159 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
;
3162 editor
->bDialogMode
= TRUE
;
3163 if (editor
->styleFlags
& ES_MULTILINE
)
3164 code
|= DLGC_WANTMESSAGE
;
3165 if (!(editor
->styleFlags
& ES_SAVESEL
))
3166 code
|= DLGC_HASSETSEL
;
3169 case EM_EMPTYUNDOBUFFER
:
3170 ME_EmptyUndoStack(editor
);
3174 /* Note: wParam/lParam can be NULL */
3176 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3177 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3178 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3179 if ((*pfrom
|*pto
) & 0xFFFF0000)
3181 return MAKELONG(*pfrom
,*pto
);
3185 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3186 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3187 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3190 case EM_SETUNDOLIMIT
:
3192 if ((int)wParam
< 0)
3193 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3195 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3196 /* Setting a max stack size keeps wine from getting killed
3197 for hogging memory. Windows allocates all this memory at once, so
3198 no program would realistically set a value above our maximum. */
3199 return editor
->nUndoLimit
;
3202 return !list_empty( &editor
->undo_stack
);
3204 return !list_empty( &editor
->redo_stack
);
3205 case WM_UNDO
: /* FIXME: actually not the same */
3207 return ME_Undo(editor
);
3209 return ME_Redo(editor
);
3212 /* these flags are equivalent to the ES_* counterparts */
3213 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3214 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3215 DWORD settings
= editor
->styleFlags
& mask
;
3221 /* these flags are equivalent to ES_* counterparts, except for
3222 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3223 * but is still stored in editor->styleFlags. */
3224 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3225 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3226 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3227 DWORD settings
= mask
& editor
->styleFlags
;
3228 DWORD oldSettings
= settings
;
3229 DWORD changedSettings
;
3245 changedSettings
= oldSettings
^ settings
;
3247 if (changedSettings
) {
3248 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3250 if (changedSettings
& ECO_SELECTIONBAR
)
3252 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3253 if (settings
& ECO_SELECTIONBAR
) {
3254 assert(!editor
->selofs
);
3255 editor
->selofs
= SELECTIONBAR_WIDTH
;
3256 editor
->rcFormat
.left
+= editor
->selofs
;
3258 editor
->rcFormat
.left
-= editor
->selofs
;
3261 ME_RewrapRepaint(editor
);
3264 if (changedSettings
& settings
& ECO_VERTICAL
)
3265 FIXME("ECO_VERTICAL not implemented yet!\n");
3266 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3267 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3268 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3269 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3270 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3271 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3272 if (changedSettings
& settings
& ECO_WANTRETURN
)
3273 FIXME("ECO_WANTRETURN not implemented yet!\n");
3274 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3275 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3282 handle_EM_EXSETSEL( editor
, wParam
, lParam
);
3285 case EM_SETSCROLLPOS
:
3287 POINT
*point
= (POINT
*)lParam
;
3288 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3291 case EM_AUTOURLDETECT
:
3293 if (wParam
==1 || wParam
==0)
3295 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3298 return E_INVALIDARG
;
3300 case EM_GETAUTOURLDETECT
:
3302 return editor
->AutoURLDetect_bEnable
;
3306 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3308 return handle_EM_EXSETSEL( editor
, range
.cpMin
, range
.cpMax
);
3310 case EM_SHOWSCROLLBAR
:
3323 flags
= WS_HSCROLL
|WS_VSCROLL
;
3330 editor
->styleFlags
|= flags
;
3331 if (flags
& WS_HSCROLL
)
3332 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3333 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3334 if (flags
& WS_VSCROLL
)
3335 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3336 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3338 editor
->styleFlags
&= ~flags
;
3339 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3346 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3349 BOOL bRtf
, bUnicode
, bSelection
, bUTF8
;
3350 int oldModify
= editor
->nModifyStep
;
3351 static const char utf8_bom
[] = {0xef, 0xbb, 0xbf};
3353 if (!pStruct
) return 0;
3355 /* If we detect ascii rtf at the start of the string,
3356 * we know it isn't unicode. */
3357 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3358 !strncmp((char *)lParam
, "{\\urtf", 6)));
3359 bUnicode
= !bRtf
&& pStruct
->codepage
== CP_UNICODE
;
3360 bUTF8
= (lParam
&& (!strncmp((char *)lParam
, utf8_bom
, 3)));
3362 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3363 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3364 pStruct
->flags
, pStruct
->codepage
);
3366 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3368 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3369 style
= ME_GetSelectionInsertStyle(editor
);
3370 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3373 ME_SetCursorToStart(editor
, &start
);
3374 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3375 style
= editor
->pBuffer
->pDefaultStyle
;
3379 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3381 /* FIXME: The length returned doesn't include the rtf control
3382 * characters, only the actual text. */
3383 len
= lParam
? strlen((char *)lParam
) : 0;
3386 if (bUTF8
&& !bUnicode
) {
3387 wszText
= ME_ToUnicode(CP_UTF8
, (void *)(lParam
+3), &len
);
3388 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3389 ME_EndToUnicode(CP_UTF8
, wszText
);
3391 wszText
= ME_ToUnicode(pStruct
->codepage
, (void *)lParam
, &len
);
3392 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3393 ME_EndToUnicode(pStruct
->codepage
, wszText
);
3398 ME_ReleaseStyle(style
);
3399 ME_UpdateSelectionLinkAttribute(editor
);
3403 ME_SetCursorToStart(editor
, &cursor
);
3404 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3406 ME_CommitUndo(editor
);
3407 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3409 editor
->nModifyStep
= oldModify
;
3410 ME_EmptyUndoStack(editor
);
3412 ME_UpdateRepaint(editor
, FALSE
);
3415 case EM_SETBKGNDCOLOR
:
3418 if (editor
->rgbBackColor
!= -1) {
3419 DeleteObject(editor
->hbrBackground
);
3420 lColor
= editor
->rgbBackColor
;
3422 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3426 editor
->rgbBackColor
= -1;
3427 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3431 editor
->rgbBackColor
= lParam
;
3432 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3434 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3435 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3439 return editor
->nModifyStep
== 0 ? 0 : -1;
3443 editor
->nModifyStep
= 1;
3445 editor
->nModifyStep
= 0;
3449 case EM_SETREADONLY
:
3452 editor
->styleFlags
|= ES_READONLY
;
3454 editor
->styleFlags
&= ~ES_READONLY
;
3457 case EM_SETEVENTMASK
:
3459 DWORD nOldMask
= editor
->nEventMask
;
3461 editor
->nEventMask
= lParam
;
3464 case EM_GETEVENTMASK
:
3465 return editor
->nEventMask
;
3466 case EM_SETCHARFORMAT
:
3468 CHARFORMAT2W buf
, *p
;
3469 BOOL bRepaint
= TRUE
;
3470 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3471 if (p
== NULL
) return 0;
3472 if (wParam
& SCF_ALL
) {
3473 if (editor
->mode
& TM_PLAINTEXT
) {
3474 ME_SetDefaultCharFormat(editor
, p
);
3477 ME_SetCursorToStart(editor
, &start
);
3478 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3479 editor
->nModifyStep
= 1;
3481 } else if (wParam
& SCF_SELECTION
) {
3482 if (editor
->mode
& TM_PLAINTEXT
)
3484 if (wParam
& SCF_WORD
) {
3485 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3488 bRepaint
= ME_IsSelection(editor
);
3489 ME_SetSelectionCharFormat(editor
, p
);
3490 if (bRepaint
) editor
->nModifyStep
= 1;
3491 } else { /* SCF_DEFAULT */
3492 ME_SetDefaultCharFormat(editor
, p
);
3494 ME_CommitUndo(editor
);
3497 ME_WrapMarkedParagraphs(editor
);
3498 ME_UpdateScrollBar(editor
);
3503 case EM_GETCHARFORMAT
:
3505 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3506 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3507 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3508 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3509 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3511 tmp
.cbSize
= sizeof(tmp
);
3513 ME_GetDefaultCharFormat(editor
, &tmp
);
3515 ME_GetSelectionCharFormat(editor
, &tmp
);
3516 ME_CopyToCFAny(dst
, &tmp
);
3519 case EM_SETPARAFORMAT
:
3521 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3522 ME_WrapMarkedParagraphs(editor
);
3523 ME_UpdateScrollBar(editor
);
3525 ME_CommitUndo(editor
);
3528 case EM_GETPARAFORMAT
:
3529 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3530 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3531 case EM_GETFIRSTVISIBLELINE
:
3533 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3534 int y
= editor
->vert_si
.nPos
;
3539 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3540 if (p
->type
== diTextEnd
)
3542 if (p
->type
== diParagraph
) {
3543 ypara
= p
->member
.para
.pt
.y
;
3546 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3547 yend
= ystart
+ p
->member
.row
.nHeight
;
3555 case EM_HIDESELECTION
:
3557 editor
->bHideSelection
= (wParam
!= 0);
3558 ME_InvalidateSelection(editor
);
3563 if (!(editor
->styleFlags
& ES_MULTILINE
))
3565 ME_ScrollDown( editor
, lParam
* get_default_line_height( editor
) );
3571 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3572 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3573 ME_CommitUndo(editor
);
3574 ME_UpdateRepaint(editor
, TRUE
);
3579 int from
, to
, nStartCursor
;
3582 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3583 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &len
);
3584 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3586 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3587 style
= ME_GetSelectionInsertStyle(editor
);
3588 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3589 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3590 ME_ReleaseStyle(style
);
3591 /* drop temporary style if line end */
3593 * FIXME question: does abc\n mean: put abc,
3594 * clear temp style, put \n? (would require a change)
3596 if (len
>0 && wszText
[len
-1] == '\n')
3597 ME_ClearTempStyle(editor
);
3598 ME_EndToUnicode(codepage
, wszText
);
3599 ME_CommitUndo(editor
);
3600 ME_UpdateSelectionLinkAttribute(editor
);
3602 ME_EmptyUndoStack(editor
);
3603 ME_UpdateRepaint(editor
, FALSE
);
3606 case EM_SCROLLCARET
:
3607 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3614 BOOL bRepaint
= LOWORD(lParam
);
3617 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3618 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3619 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3620 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3621 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3622 if (editor
->mode
& TM_RICHTEXT
) {
3624 ME_SetCursorToStart(editor
, &start
);
3625 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3627 ME_SetDefaultCharFormat(editor
, &fmt
);
3629 ME_CommitUndo(editor
);
3630 ME_MarkAllForWrapping(editor
);
3631 ME_WrapMarkedParagraphs(editor
);
3632 ME_UpdateScrollBar(editor
);
3640 ME_SetCursorToStart(editor
, &cursor
);
3641 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3644 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3645 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3646 !strncmp((char *)lParam
, "{\\urtf", 6))
3648 /* Undocumented: WM_SETTEXT supports RTF text */
3649 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3654 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3655 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &textLen
);
3656 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3661 /* uses default style! */
3662 if (!(editor
->styleFlags
& ES_MULTILINE
))
3667 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3670 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3672 ME_EndToUnicode(codepage
, wszText
);
3676 TRACE("WM_SETTEXT - NULL\n");
3677 ME_SetCursorToStart(editor
, &cursor
);
3678 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3679 ME_SetSelection(editor
, 0, 0);
3680 editor
->nModifyStep
= 0;
3681 ME_CommitUndo(editor
);
3682 ME_EmptyUndoStack(editor
);
3683 ME_UpdateRepaint(editor
, FALSE
);
3688 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3689 if (IsClipboardFormatAvailable(nRTFFormat
))
3691 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3696 case WM_MBUTTONDOWN
:
3702 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3703 int nChars
= nTo
- nFrom
;
3704 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3706 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3708 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3709 ME_CommitUndo(editor
);
3710 ME_UpdateRepaint(editor
, TRUE
);
3714 case WM_GETTEXTLENGTH
:
3716 GETTEXTLENGTHEX how
;
3718 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3719 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3720 how
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3721 return ME_GetTextLengthEx(editor
, &how
);
3723 case EM_GETTEXTLENGTHEX
:
3724 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3728 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3729 ex
.flags
= GT_USECRLF
;
3730 ex
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3731 ex
.lpDefaultChar
= NULL
;
3732 ex
.lpUsedDefChar
= NULL
;
3733 return ME_GetTextEx(editor
, &ex
, lParam
);
3736 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3739 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3740 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3741 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3742 nTo
- nFrom
, unicode
);
3744 case EM_GETSCROLLPOS
:
3746 POINT
*point
= (POINT
*)lParam
;
3747 point
->x
= editor
->horz_si
.nPos
;
3748 point
->y
= editor
->vert_si
.nPos
;
3749 /* 16-bit scaled value is returned as stored in scrollinfo */
3750 if (editor
->horz_si
.nMax
> 0xffff)
3751 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3752 if (editor
->vert_si
.nMax
> 0xffff)
3753 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3756 case EM_GETTEXTRANGE
:
3758 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3760 int nStart
= rng
->chrg
.cpMin
;
3761 int nEnd
= rng
->chrg
.cpMax
;
3762 int textlength
= ME_GetTextLength(editor
);
3764 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3765 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3766 if (nStart
< 0) return 0;
3767 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3769 if (nStart
>= nEnd
) return 0;
3771 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3772 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3776 ME_DisplayItem
*run
;
3777 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3778 unsigned int nCharsLeft
= nMaxChars
;
3779 char *dest
= (char *) lParam
;
3780 BOOL wroteNull
= FALSE
;
3782 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3783 unicode
? "Unicode" : "Ansi");
3785 run
= ME_FindRowWithNumber(editor
, wParam
);
3789 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3790 && run
->type
== diRun
)
3792 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3795 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3798 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3800 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
3801 nCharsLeft
, NULL
, NULL
);
3802 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3803 nCharsLeft
-= nCopy
;
3806 /* append line termination, space allowing */
3810 *((WCHAR
*)dest
) = '\0';
3817 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3818 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3820 case EM_GETLINECOUNT
:
3822 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3825 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3827 while (item
!= editor
->pBuffer
->pLast
)
3829 assert(item
->type
== diParagraph
);
3830 prev_para
= ME_FindItemBack(item
, diRun
);
3832 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3834 nRows
+= item
->member
.para
.nRows
;
3835 item
= item
->member
.para
.next_para
;
3837 last_para
= ME_FindItemBack(item
, diRun
);
3839 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3840 if (editor
->bEmulateVersion10
&& prev_para
&&
3841 last_para
->member
.run
.nCharOfs
== 0 &&
3842 prev_para
->member
.run
.len
== 1 &&
3843 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
3845 /* In 1.0 emulation, the last solitary \r at the very end of the text
3846 (if one exists) is NOT a line break.
3847 FIXME: this is an ugly hack. This should have a more regular model. */
3851 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3852 return max(1, nRows
);
3854 case EM_LINEFROMCHAR
:
3857 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3859 return ME_RowNumberFromCharOfs(editor
, wParam
);
3861 case EM_EXLINEFROMCHAR
:
3864 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3866 return ME_RowNumberFromCharOfs(editor
, lParam
);
3870 ME_DisplayItem
*item
, *para
;
3874 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3876 item
= ME_FindRowWithNumber(editor
, wParam
);
3879 para
= ME_GetParagraph(item
);
3880 item
= ME_FindItemFwd(item
, diRun
);
3881 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3882 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3887 ME_DisplayItem
*item
, *item_end
;
3888 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3889 ME_DisplayItem
*para
, *run
;
3891 if (wParam
> ME_GetTextLength(editor
))
3895 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3898 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3899 item
= ME_RowStart(run
);
3900 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3901 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3902 if (item_end
->type
== diStartRow
) {
3903 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3905 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3906 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3907 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
3909 nChars
= nNextLineOfs
- nThisLineOfs
;
3910 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3913 case EM_EXLIMITTEXT
:
3915 if ((int)lParam
< 0)
3918 editor
->nTextLimit
= 65536;
3920 editor
->nTextLimit
= (int) lParam
;
3926 editor
->nTextLimit
= 65536;
3928 editor
->nTextLimit
= (int) wParam
;
3931 case EM_GETLIMITTEXT
:
3933 return editor
->nTextLimit
;
3939 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3940 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3943 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3944 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3945 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3948 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3949 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3957 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3958 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3961 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3962 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3963 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3966 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3967 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3973 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3974 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3976 case EM_FINDTEXTEXW
:
3978 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3979 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3982 if (!wParam
|| !lParam
)
3984 *(int *)wParam
= editor
->nZoomNumerator
;
3985 *(int *)lParam
= editor
->nZoomDenominator
;
3988 return ME_SetZoom(editor
, wParam
, lParam
);
3989 case EM_CHARFROMPOS
:
3992 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
3994 return ME_GetCursorOfs(&cursor
);
3998 case EM_POSFROMCHAR
:
4000 ME_DisplayItem
*pPara
, *pRun
;
4001 int nCharOfs
, nOffset
, nLength
;
4005 /* detect which API version we're dealing with */
4006 if (wParam
>= 0x40000)
4008 nLength
= ME_GetTextLength(editor
);
4009 nCharOfs
= min(nCharOfs
, nLength
);
4010 nCharOfs
= max(nCharOfs
, 0);
4012 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
4013 assert(pRun
->type
== diRun
);
4014 pt
.y
= pRun
->member
.run
.pt
.y
;
4015 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
4016 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
4017 pt
.x
+= editor
->rcFormat
.left
;
4019 pt
.x
-= editor
->horz_si
.nPos
;
4020 pt
.y
-= editor
->vert_si
.nPos
;
4022 if (wParam
>= 0x40000) {
4023 *(POINTL
*)wParam
= pt
;
4025 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
4032 ME_SetDefaultFormatRect(editor
);
4034 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
4035 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
4036 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
4038 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
4039 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
4041 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
4043 if (editor
->styleFlags
& WS_VSCROLL
)
4045 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
4046 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
4048 if (editor
->styleFlags
& WS_HSCROLL
)
4050 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
4051 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
4057 text
= (unicode
? (void*)((CREATESTRUCTW
*)lParam
)->lpszName
4058 : (void*)((CREATESTRUCTA
*)lParam
)->lpszName
);
4064 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
4065 textW
= ME_ToUnicode(codepage
, text
, &len
);
4066 if (!(editor
->styleFlags
& ES_MULTILINE
))
4069 while(textW
[len
] != '\0' && textW
[len
] != '\r' && textW
[len
] != '\n')
4072 ME_InsertTextFromCursor(editor
, 0, textW
, len
, editor
->pBuffer
->pDefaultStyle
);
4073 ME_EndToUnicode(codepage
, textW
);
4074 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4075 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
4078 ME_CommitUndo(editor
);
4079 ME_WrapMarkedParagraphs(editor
);
4080 ME_MoveCaret(editor
);
4084 ME_DestroyEditor(editor
);
4088 return ME_SetCursor(editor
);
4090 case WM_LBUTTONDBLCLK
:
4091 case WM_LBUTTONDOWN
:
4093 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4094 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4095 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4097 ITextHost_TxSetFocus(editor
->texthost
);
4098 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
4099 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
4100 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
4101 editor
->bMouseCaptured
= TRUE
;
4102 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4103 if (!ME_SetCursor(editor
)) goto do_default
;
4107 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4108 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4110 if (editor
->bMouseCaptured
)
4111 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4112 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4113 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4114 if (editor
->bMouseCaptured
)
4115 ME_SetCursor(editor
);
4118 if (editor
->bMouseCaptured
) {
4119 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4120 editor
->bMouseCaptured
= FALSE
;
4122 if (editor
->nSelectionType
== stDocument
)
4123 editor
->nSelectionType
= stPosition
;
4124 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4125 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4129 ME_SetCursor(editor
);
4130 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
4134 case WM_RBUTTONDOWN
:
4135 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4136 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4137 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4140 case WM_CONTEXTMENU
:
4141 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4145 editor
->bHaveFocus
= TRUE
;
4146 ME_ShowCaret(editor
);
4147 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4150 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4151 editor
->bHaveFocus
= FALSE
;
4152 editor
->wheel_remain
= 0;
4153 ME_HideCaret(editor
);
4154 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4157 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4160 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4161 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4165 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4166 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4168 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4172 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4173 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4175 return ME_Char(editor
, wParam
, lParam
, unicode
);
4179 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4180 if(wParam
<= 0x000fffff)
4182 if(wParam
> 0xffff) /* convert to surrogates */
4185 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4186 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4188 ME_Char(editor
, wParam
, 0, TRUE
);
4194 case EM_STOPGROUPTYPING
:
4195 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4199 const int scrollUnit
= 7;
4201 switch(LOWORD(wParam
))
4204 ME_ScrollAbs(editor
, 0, 0);
4207 ME_ScrollAbs(editor
,
4208 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4209 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4212 ME_ScrollLeft(editor
, scrollUnit
);
4215 ME_ScrollRight(editor
, scrollUnit
);
4218 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4221 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4224 case SB_THUMBPOSITION
:
4226 int pos
= HIWORD(wParam
);
4227 if (editor
->horz_si
.nMax
> 0xffff)
4228 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4229 ME_HScrollAbs(editor
, pos
);
4235 case EM_SCROLL
: /* fall through */
4239 int lineHeight
= get_default_line_height( editor
);
4241 origNPos
= editor
->vert_si
.nPos
;
4243 switch(LOWORD(wParam
))
4246 ME_ScrollAbs(editor
, 0, 0);
4249 ME_ScrollAbs(editor
,
4250 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4251 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4254 ME_ScrollUp(editor
,lineHeight
);
4257 ME_ScrollDown(editor
,lineHeight
);
4260 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4263 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4266 case SB_THUMBPOSITION
:
4268 int pos
= HIWORD(wParam
);
4269 if (editor
->vert_si
.nMax
> 0xffff)
4270 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4271 ME_VScrollAbs(editor
, pos
);
4275 if (msg
== EM_SCROLL
)
4276 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4284 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4285 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4288 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4290 delta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4292 /* if scrolling changes direction, ignore left overs */
4293 if ((delta
< 0 && editor
->wheel_remain
< 0) ||
4294 (delta
> 0 && editor
->wheel_remain
> 0))
4295 editor
->wheel_remain
+= delta
;
4297 editor
->wheel_remain
= delta
;
4299 if (editor
->wheel_remain
)
4303 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4307 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4309 numerator
+= calc_wheel_change( &editor
->wheel_remain
, 10 );
4310 if (numerator
>= 10 && numerator
<= 500)
4311 ME_SetZoom(editor
, numerator
, 100);
4316 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES
, 0, &max_lines
, 0 );
4318 lines
= calc_wheel_change( &editor
->wheel_remain
, (int)max_lines
);
4320 ME_ScrollDown( editor
, -lines
* get_default_line_height( editor
) );
4327 *((RECT
*)lParam
) = editor
->rcFormat
;
4328 if (editor
->bDefaultFormatRect
)
4329 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4339 RECT
*rc
= (RECT
*)lParam
;
4341 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4342 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4345 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4346 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4347 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4348 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4349 } else if (wParam
== 1) {
4350 /* MSDN incorrectly says a wParam value of 1 causes the
4351 * lParam rect to be used as a relative offset,
4352 * however, the tests show it just prevents min/max bound
4354 editor
->rcFormat
.top
= rc
->top
- border
;
4355 editor
->rcFormat
.left
= rc
->left
- border
;
4356 editor
->rcFormat
.bottom
= rc
->bottom
;
4357 editor
->rcFormat
.right
= rc
->right
+ border
;
4361 editor
->bDefaultFormatRect
= FALSE
;
4365 ME_SetDefaultFormatRect(editor
);
4366 editor
->bDefaultFormatRect
= TRUE
;
4368 ME_MarkAllForWrapping(editor
);
4369 ME_WrapMarkedParagraphs(editor
);
4370 ME_UpdateScrollBar(editor
);
4371 if (msg
!= EM_SETRECTNP
)
4375 case EM_REQUESTRESIZE
:
4376 ME_SendRequestResize(editor
, TRUE
);
4384 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4385 if (editor
->bDefaultFormatRect
) {
4386 ME_SetDefaultFormatRect(editor
);
4388 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4389 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4391 editor
->prevClientRect
= clientRect
;
4392 ME_RewrapRepaint(editor
);
4395 /* IME messages to make richedit controls IME aware */
4396 case WM_IME_SETCONTEXT
:
4397 case WM_IME_CONTROL
:
4399 case WM_IME_COMPOSITIONFULL
:
4401 case WM_IME_STARTCOMPOSITION
:
4403 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4404 ME_DeleteSelection(editor
);
4405 ME_CommitUndo(editor
);
4406 ME_UpdateRepaint(editor
, FALSE
);
4409 case WM_IME_COMPOSITION
:
4413 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4414 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4415 ME_DeleteSelection(editor
);
4416 ME_SaveTempStyle(editor
);
4417 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4419 LPWSTR lpCompStr
= NULL
;
4421 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4423 dwIndex
= GCS_COMPSTR
;
4425 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4426 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4427 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4428 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4429 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4430 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4432 if (dwIndex
== GCS_COMPSTR
)
4433 ME_SetSelection(editor
,editor
->imeStartIndex
,
4434 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4436 ME_ReleaseStyle(style
);
4437 ME_CommitUndo(editor
);
4438 ME_UpdateRepaint(editor
, FALSE
);
4441 case WM_IME_ENDCOMPOSITION
:
4443 ME_DeleteSelection(editor
);
4444 editor
->imeStartIndex
=-1;
4447 case EM_GETOLEINTERFACE
:
4449 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4450 return CreateIRichEditOle(editor
, ppvObj
);
4452 case EM_GETPASSWORDCHAR
:
4454 return editor
->cPasswordMask
;
4456 case EM_SETOLECALLBACK
:
4457 if(editor
->lpOleCallback
)
4458 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4459 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4460 if(editor
->lpOleCallback
)
4461 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4463 case EM_GETWORDBREAKPROC
:
4464 return (LRESULT
)editor
->pfnWordBreak
;
4465 case EM_SETWORDBREAKPROC
:
4467 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4469 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4470 return (LRESULT
)pfnOld
;
4472 case EM_GETTEXTMODE
:
4473 return editor
->mode
;
4474 case EM_SETTEXTMODE
:
4479 if (ME_GetTextLength(editor
) ||
4480 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4481 return E_UNEXPECTED
;
4483 /* Check for mutually exclusive flags in adjacent bits of wParam */
4484 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4485 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4486 return E_INVALIDARG
;
4488 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4490 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4491 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4492 if (wParam
& TM_PLAINTEXT
) {
4493 /* Clear selection since it should be possible to select the
4494 * end of text run for rich text */
4495 ME_InvalidateSelection(editor
);
4496 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4497 editor
->pCursors
[1] = editor
->pCursors
[0];
4498 /* plain text can only have the default style. */
4499 ME_ClearTempStyle(editor
);
4500 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4501 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4502 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4505 /* FIXME: Currently no support for undo level and code page options */
4506 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4509 case EM_SETPASSWORDCHAR
:
4511 editor
->cPasswordMask
= wParam
;
4512 ME_RewrapRepaint(editor
);
4515 case EM_SETTARGETDEVICE
:
4518 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4519 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4521 editor
->bWordWrap
= new;
4522 editor
->nAvailWidth
= 0; /* wrap to client area */
4523 ME_RewrapRepaint(editor
);
4526 int width
= max(0, lParam
);
4527 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4528 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4530 editor
->nAvailWidth
= width
;
4531 editor
->bWordWrap
= TRUE
;
4532 ME_RewrapRepaint(editor
);
4534 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4539 *phresult
= S_FALSE
;
4545 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4546 LPARAM lParam
, BOOL unicode
)
4548 ME_TextEditor
*editor
;
4550 LRESULT lresult
= 0;
4552 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4553 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4555 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4558 if (msg
== WM_NCCREATE
)
4560 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4561 ITextHost
*texthost
;
4563 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4564 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4565 return texthost
!= NULL
;
4569 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4581 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4582 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4583 ME_SendOldNotify(editor
, EN_UPDATE
);
4584 /* Erase area outside of the formatting rectangle */
4585 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4588 rc
.bottom
= editor
->rcFormat
.top
;
4589 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4590 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4592 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4594 rc
.top
= editor
->rcFormat
.bottom
;
4595 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4596 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4598 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4600 rc
.right
= editor
->rcFormat
.left
;
4601 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4602 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4604 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4606 rc
.left
= editor
->rcFormat
.right
;
4607 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4608 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4611 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4612 EndPaint(editor
->hWnd
, &ps
);
4617 HDC hDC
= (HDC
)wParam
;
4620 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4621 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4627 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4628 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4630 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4631 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4632 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4633 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4636 case EM_SETREADONLY
:
4639 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4640 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4641 dwStyle
&= ~ES_READONLY
;
4643 dwStyle
|= ES_READONLY
;
4644 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4648 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4651 if (hresult
== S_FALSE
)
4652 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4654 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4655 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4660 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4662 BOOL unicode
= TRUE
;
4664 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4665 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4668 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4671 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4673 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4676 /******************************************************************
4677 * RichEditANSIWndProc (RICHED20.10)
4679 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4681 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4684 /******************************************************************
4685 * RichEdit10ANSIWndProc (RICHED20.9)
4687 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4689 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4691 ITextHost
*texthost
;
4692 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4694 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4695 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4696 return texthost
!= NULL
;
4698 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4701 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4703 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4706 /* Fill buffer with srcChars unicode characters from the start cursor.
4708 * buffer: destination buffer
4709 * buflen: length of buffer in characters excluding the NULL terminator.
4710 * start: start of editor text to copy into buffer.
4711 * srcChars: Number of characters to use from the editor text.
4712 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4714 * returns the number of characters written excluding the NULL terminator.
4716 * The written text is always NULL terminated.
4718 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4719 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
)
4721 ME_DisplayItem
*pRun
, *pNextRun
;
4722 const WCHAR
*pStart
= buffer
;
4723 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4727 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4728 if (editor
->bEmulateVersion10
) bCRLF
= FALSE
;
4732 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4734 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4735 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4737 /* No '\r' is appended to the last paragraph. */
4738 while (srcChars
&& buflen
&& pNextRun
)
4740 int nFlags
= pRun
->member
.run
.nFlags
;
4742 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4744 if (buflen
== 1) break;
4745 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4746 * EM_GETTEXTEX, however, this is done for copying text which
4747 * also uses this function. */
4748 srcChars
-= min(nLen
, srcChars
);
4752 nLen
= min(nLen
, srcChars
);
4756 nLen
= min(nLen
, buflen
);
4759 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4764 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4766 nLen
= pRun
->member
.run
.len
;
4767 str
= get_text( &pRun
->member
.run
, 0 );
4770 return buffer
- pStart
;
4773 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4778 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4779 wcW
.lpfnWndProc
= RichEditWndProcW
;
4781 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4782 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4784 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4785 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4786 wcW
.lpszMenuName
= NULL
;
4788 if (is_version_nt())
4790 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4791 if (!RegisterClassW(&wcW
)) return FALSE
;
4792 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4793 if (!RegisterClassW(&wcW
)) return FALSE
;
4797 /* WNDCLASSA/W have the same layout */
4798 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4799 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4800 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4801 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4804 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4805 wcA
.lpfnWndProc
= RichEditWndProcA
;
4807 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4808 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4810 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4811 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4812 wcA
.lpszMenuName
= NULL
;
4813 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4814 if (!RegisterClassA(&wcA
)) return FALSE
;
4815 wcA
.lpszClassName
= "RichEdit50A";
4816 if (!RegisterClassA(&wcA
)) return FALSE
;
4821 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4822 /* FIXME: Not implemented */
4823 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4824 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4825 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4828 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4829 /* FIXME: Not implemented */
4830 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4831 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4832 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4835 /******************************************************************
4836 * REExtendedRegisterClass (RICHED20.8)
4838 * FIXME undocumented
4839 * Need to check for errors and implement controls and callbacks
4841 LRESULT WINAPI
REExtendedRegisterClass(void)
4846 FIXME("semi stub\n");
4850 wcW
.hInstance
= NULL
;
4853 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4854 wcW
.lpszMenuName
= NULL
;
4856 if (!ME_ListBoxRegistered
)
4858 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4859 wcW
.lpfnWndProc
= REListWndProc
;
4860 wcW
.lpszClassName
= REListBox20W
;
4861 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4864 if (!ME_ComboBoxRegistered
)
4866 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4867 wcW
.lpfnWndProc
= REComboWndProc
;
4868 wcW
.lpszClassName
= REComboBox20W
;
4869 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4873 if (ME_ListBoxRegistered
)
4875 if (ME_ComboBoxRegistered
)
4881 static BOOL
isurlspecial(WCHAR c
)
4883 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4884 return strchrW( special_chars
, c
) != NULL
;
4888 * This proc takes a selection, and scans it forward in order to select the span
4889 * of a possible URL candidate. A possible URL candidate must start with isalnum
4890 * or one of the following special characters: *|/\+%#@ and must consist entirely
4891 * of the characters allowed to start the URL, plus : (colon) which may occur
4892 * at most once, and not at either end.
4894 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
4895 const ME_Cursor
*start
,
4897 ME_Cursor
*candidate_min
,
4898 ME_Cursor
*candidate_max
)
4900 ME_Cursor cursor
= *start
;
4901 BOOL foundColon
= FALSE
;
4902 BOOL candidateStarted
= FALSE
;
4903 WCHAR lastAcceptedChar
= '\0';
4907 WCHAR
*strStart
= get_text( &cursor
.pRun
->member
.run
, 0 );
4908 WCHAR
*str
= strStart
+ cursor
.nOffset
;
4909 int nLen
= cursor
.pRun
->member
.run
.len
- cursor
.nOffset
;
4912 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4914 /* Find start of candidate */
4915 if (!candidateStarted
)
4920 if (isalnumW(*str
) || isurlspecial(*str
))
4922 cursor
.nOffset
= str
- strStart
;
4923 *candidate_min
= cursor
;
4924 candidateStarted
= TRUE
;
4925 lastAcceptedChar
= *str
++;
4932 /* Find end of candidate */
4933 if (candidateStarted
) {
4937 if (*str
== ':' && !foundColon
) {
4939 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
4940 cursor
.nOffset
= str
- strStart
;
4941 if (lastAcceptedChar
== ':')
4942 ME_MoveCursorChars(editor
, &cursor
, -1);
4943 *candidate_max
= cursor
;
4946 lastAcceptedChar
= *str
++;
4950 /* End of paragraph: skip it if before candidate span, or terminates
4951 current active span */
4952 if (candidateStarted
) {
4953 if (lastAcceptedChar
== ':')
4954 ME_MoveCursorChars(editor
, &cursor
, -1);
4955 *candidate_max
= cursor
;
4960 /* Reaching this point means no span was found, so get next span */
4961 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
4962 if (candidateStarted
) {
4963 /* There are no further runs, so take end of text as end of candidate */
4964 cursor
.nOffset
= str
- strStart
;
4965 if (lastAcceptedChar
== ':')
4966 ME_MoveCursorChars(editor
, &cursor
, -1);
4967 *candidate_max
= cursor
;
4970 *candidate_max
= *candidate_min
= cursor
;
4976 if (candidateStarted
) {
4977 /* There are no further runs, so take end of text as end of candidate */
4978 if (lastAcceptedChar
== ':')
4979 ME_MoveCursorChars(editor
, &cursor
, -1);
4980 *candidate_max
= cursor
;
4983 *candidate_max
= *candidate_min
= cursor
;
4988 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4990 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
4992 #define MAX_PREFIX_LEN 9
4994 const WCHAR text
[MAX_PREFIX_LEN
];
4997 {{'p','r','o','s','p','e','r','o',':'}, 9},
4998 {{'t','e','l','n','e','t',':'}, 7},
4999 {{'g','o','p','h','e','r',':'}, 7},
5000 {{'m','a','i','l','t','o',':'}, 7},
5001 {{'h','t','t','p','s',':'}, 6},
5002 {{'f','i','l','e',':'}, 5},
5003 {{'n','e','w','s',':'}, 5},
5004 {{'w','a','i','s',':'}, 5},
5005 {{'n','n','t','p',':'}, 5},
5006 {{'h','t','t','p',':'}, 5},
5007 {{'w','w','w','.'}, 4},
5008 {{'f','t','p',':'}, 4},
5010 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
5013 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, FALSE
);
5014 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
5016 if (nChars
< prefixes
[i
].length
) continue;
5017 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
5021 #undef MAX_PREFIX_LEN
5025 * This proc walks through the indicated selection and evaluates whether each
5026 * section identified by ME_FindNextURLCandidate and in-between sections have
5027 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5028 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5030 * Since this function can cause runs to be split, do not depend on the value
5031 * of the start cursor at the end of the function.
5033 * nChars may be set to INT_MAX to update to the end of the text.
5035 * Returns TRUE if at least one section was modified.
5037 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
5039 BOOL modified
= FALSE
;
5040 ME_Cursor startCur
= *start
;
5042 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
5047 ME_Cursor candidateStart
, candidateEnd
;
5049 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
5050 &candidateStart
, &candidateEnd
))
5052 /* Section before candidate is not an URL */
5053 int cMin
= ME_GetCursorOfs(&candidateStart
);
5054 int cMax
= ME_GetCursorOfs(&candidateEnd
);
5056 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
5057 candidateStart
= candidateEnd
;
5058 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
5062 /* No more candidates until end of selection */
5066 if (startCur
.pRun
!= candidateStart
.pRun
||
5067 startCur
.nOffset
!= candidateStart
.nOffset
)
5069 /* CFE_LINK effect should be consistently unset */
5070 link
.cbSize
= sizeof(link
);
5071 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5072 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
5074 /* CFE_LINK must be unset from this range */
5075 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5076 link
.cbSize
= sizeof(link
);
5077 link
.dwMask
= CFM_LINK
;
5079 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5080 /* Update candidateEnd since setting character formats may split
5081 * runs, which can cause a cursor to be at an invalid offset within
5083 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
5085 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
5086 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
5091 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
5092 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
5094 /* CFE_LINK effect should be consistently set */
5095 link
.cbSize
= sizeof(link
);
5096 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5097 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
5099 /* CFE_LINK must be set on this range */
5100 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5101 link
.cbSize
= sizeof(link
);
5102 link
.dwMask
= CFM_LINK
;
5103 link
.dwEffects
= CFE_LINK
;
5104 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5108 startCur
= candidateEnd
;
5109 } while (nChars
> 0);