2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
54 - EM_GETLANGOPTIONS 2.0
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
67 + EM_GETSELTEXT (ANSI&Unicode)
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
94 - EM_SETBIDIOPTIONS 3.0
96 + EM_SETCHARFORMAT (partly done, no ANSI)
98 + EM_SETEVENTMASK (few notifications supported)
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
103 - EM_SETLANGOPTIONS 2.0
106 + EM_SETMODIFY (not sure if implementation is correct)
108 + EM_SETOPTIONS (partially implemented)
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
115 + EM_SETRECTNP (EM_SETRECT without repainting)
117 + EM_SETSCROLLPOS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
151 * EN_CHANGE (sent from the wrong place)
168 * EN_UPDATE (sent from the wrong place)
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
179 - ES_MULTILINE (currently single line controls aren't supported)
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
194 * RICHED20 TODO (incomplete):
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
214 * - ListBox & ComboBox not implemented
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
230 #define NO_SHLWAPI_STREAM
236 #define STACK_SIZE_DEFAULT 100
237 #define STACK_SIZE_MAX 1000
239 #define TEXT_LIMIT_DEFAULT 32767
241 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
243 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
244 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
246 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
247 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
248 static HCURSOR hLeft
;
251 HANDLE me_heap
= NULL
;
253 static BOOL ME_ListBoxRegistered
= FALSE
;
254 static BOOL ME_ComboBoxRegistered
= FALSE
;
256 static inline int is_version_nt(void)
258 return !(GetVersion() & 0x80000000);
261 static ME_TextBuffer
*ME_MakeText(void) {
263 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
265 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
266 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
272 p1
->member
.para
.next_para
= p2
;
273 p2
->member
.para
.prev_para
= p1
;
274 p2
->member
.para
.nCharOfs
= 0;
278 buf
->pCharStyle
= NULL
;
284 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
286 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
289 TRACE("%08x %p\n", dwFormat
, stream
);
296 ME_StreamInFill(stream
);
297 if (stream
->editstream
->dwError
)
303 if (!(dwFormat
& SF_UNICODE
))
305 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
306 nWideChars
= MultiByteToWideChar(CP_ACP
, 0, stream
->buffer
, stream
->dwSize
, wszText
, STREAMIN_BUFFER_SIZE
);
311 nWideChars
= stream
->dwSize
>> 1;
312 pText
= (WCHAR
*)stream
->buffer
;
315 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
316 if (stream
->dwSize
== 0)
320 ME_CommitUndo(editor
);
321 ME_UpdateRepaint(editor
);
325 static void ME_ApplyBorderProperties(RTF_Info
*info
,
326 ME_BorderRect
*borderRect
,
327 RTFBorder
*borderDef
)
330 ME_Border
*pBorders
[] = {&borderRect
->top
,
334 for (i
= 0; i
< 4; i
++)
336 RTFColor
*colorDef
= info
->colorList
;
337 pBorders
[i
]->width
= borderDef
[i
].width
;
338 colorNum
= borderDef
[i
].color
;
339 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
340 colorDef
= colorDef
->rtfNextColor
;
342 pBorders
[i
]->colorRef
= RGB(
343 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
344 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
345 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
347 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
351 void ME_RTFCharAttrHook(RTF_Info
*info
)
354 fmt
.cbSize
= sizeof(fmt
);
358 switch(info
->rtfMinor
)
361 /* FIXME add more flags once they're implemented */
362 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
363 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
364 fmt
.yHeight
= 12*20; /* 12pt */
365 fmt
.wWeight
= FW_NORMAL
;
366 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
369 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
370 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
371 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
374 fmt
.dwMask
= CFM_ITALIC
;
375 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
378 fmt
.dwMask
= CFM_UNDERLINETYPE
;
379 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
381 case rtfDotUnderline
:
382 fmt
.dwMask
= CFM_UNDERLINETYPE
;
383 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
386 fmt
.dwMask
= CFM_UNDERLINETYPE
;
387 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
389 case rtfWordUnderline
:
390 fmt
.dwMask
= CFM_UNDERLINETYPE
;
391 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
394 fmt
.dwMask
= CFM_UNDERLINETYPE
;
395 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
398 fmt
.dwMask
= CFM_STRIKEOUT
;
399 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
403 case rtfSubScrShrink
:
404 case rtfSuperScrShrink
:
406 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
407 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
408 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
409 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
412 fmt
.dwMask
= CFM_HIDDEN
;
413 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
416 fmt
.dwMask
= CFM_BACKCOLOR
;
418 if (info
->rtfParam
== 0)
419 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
420 else if (info
->rtfParam
!= rtfNoParam
)
422 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
423 if (c
&& c
->rtfCBlue
>= 0)
424 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
426 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
430 fmt
.dwMask
= CFM_COLOR
;
432 if (info
->rtfParam
== 0)
433 fmt
.dwEffects
= CFE_AUTOCOLOR
;
434 else if (info
->rtfParam
!= rtfNoParam
)
436 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
437 if (c
&& c
->rtfCBlue
>= 0)
438 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
440 fmt
.dwEffects
= CFE_AUTOCOLOR
;
445 if (info
->rtfParam
!= rtfNoParam
)
447 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
450 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
451 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
452 fmt
.bCharSet
= f
->rtfFCharSet
;
453 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
454 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
459 fmt
.dwMask
= CFM_SIZE
;
460 if (info
->rtfParam
!= rtfNoParam
)
461 fmt
.yHeight
= info
->rtfParam
*10;
466 RTFFlushOutputBuffer(info
);
467 /* FIXME too slow ? how come ? */
468 style2
= ME_ApplyStyle(info
->style
, &fmt
);
469 ME_ReleaseStyle(info
->style
);
470 info
->style
= style2
;
471 info
->styleChanged
= TRUE
;
475 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
476 the same tags mean different things in different contexts */
477 void ME_RTFParAttrHook(RTF_Info
*info
)
480 fmt
.cbSize
= sizeof(fmt
);
483 switch(info
->rtfMinor
)
485 case rtfParDef
: /* restores default paragraph attributes */
486 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
487 info
->borderType
= RTFBorderParaLeft
;
488 else /* v1.0 - 3.0 */
489 info
->borderType
= RTFBorderParaTop
;
490 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
491 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
493 /* TODO: numbering, shading */
494 fmt
.wAlignment
= PFA_LEFT
;
496 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
497 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
498 fmt
.wBorderSpace
= 0;
499 fmt
.bLineSpacingRule
= 0;
500 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
501 fmt
.dyLineSpacing
= 0;
502 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
504 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
505 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
508 ME_DisplayItem
*para
;
509 /* We are just after a table row. */
510 RTFFlushOutputBuffer(info
);
511 cursor
= info
->editor
->pCursors
[0];
513 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
514 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
516 /* Since the table row end, no text has been inserted, and the \intbl
517 * control word has not be used. We can confirm that we are not in a
520 info
->tableDef
->tableRowStart
= NULL
;
521 info
->canInheritInTbl
= FALSE
;
524 } else { /* v1.0 - v3.0 */
525 fmt
.dwMask
|= PFM_TABLE
;
526 fmt
.wEffects
&= ~PFE_TABLE
;
530 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
532 while (info
->rtfParam
> info
->nestingLevel
) {
533 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
534 ZeroMemory(tableDef
, sizeof(RTFTable
));
535 tableDef
->parent
= info
->tableDef
;
536 info
->tableDef
= tableDef
;
538 RTFFlushOutputBuffer(info
);
539 if (tableDef
->tableRowStart
&&
540 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
542 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
543 para
= para
->member
.para
.next_para
;
544 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
545 tableDef
->tableRowStart
= para
;
549 cursor
= info
->editor
->pCursors
[0];
550 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
551 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
552 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
555 info
->nestingLevel
++;
557 info
->canInheritInTbl
= FALSE
;
562 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
564 if (info
->nestingLevel
< 1)
569 info
->tableDef
= ALLOC_OBJ(RTFTable
);
570 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
572 tableDef
= info
->tableDef
;
573 RTFFlushOutputBuffer(info
);
574 if (tableDef
->tableRowStart
&&
575 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
577 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
578 para
= para
->member
.para
.next_para
;
579 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
580 tableDef
->tableRowStart
= para
;
584 cursor
= info
->editor
->pCursors
[0];
585 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
586 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
587 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
589 info
->nestingLevel
= 1;
590 info
->canInheritInTbl
= TRUE
;
593 } else { /* v1.0 - v3.0 */
594 fmt
.dwMask
|= PFM_TABLE
;
595 fmt
.wEffects
|= PFE_TABLE
;
600 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
601 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
602 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
603 fmt
.dxOffset
= -info
->rtfParam
;
606 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
607 fmt
.dwMask
= PFM_STARTINDENT
;
608 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
611 fmt
.dwMask
= PFM_RIGHTINDENT
;
612 fmt
.dxRightIndent
= info
->rtfParam
;
616 fmt
.dwMask
= PFM_ALIGNMENT
;
617 fmt
.wAlignment
= PFA_LEFT
;
620 fmt
.dwMask
= PFM_ALIGNMENT
;
621 fmt
.wAlignment
= PFA_RIGHT
;
624 fmt
.dwMask
= PFM_ALIGNMENT
;
625 fmt
.wAlignment
= PFA_CENTER
;
628 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
629 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
633 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
634 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
635 fmt
.dwMask
= PFM_TABSTOPS
;
638 fmt
.dwMask
= PFM_KEEP
;
639 fmt
.wEffects
= PFE_KEEP
;
641 case rtfNoWidowControl
:
642 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
643 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
646 fmt
.dwMask
= PFM_KEEPNEXT
;
647 fmt
.wEffects
= PFE_KEEPNEXT
;
650 fmt
.dwMask
= PFM_SPACEAFTER
;
651 fmt
.dySpaceAfter
= info
->rtfParam
;
654 fmt
.dwMask
= PFM_SPACEBEFORE
;
655 fmt
.dySpaceBefore
= info
->rtfParam
;
657 case rtfSpaceBetween
:
658 fmt
.dwMask
= PFM_LINESPACING
;
659 if ((int)info
->rtfParam
> 0)
661 fmt
.dyLineSpacing
= info
->rtfParam
;
662 fmt
.bLineSpacingRule
= 3;
666 fmt
.dyLineSpacing
= info
->rtfParam
;
667 fmt
.bLineSpacingRule
= 4;
669 case rtfSpaceMultiply
:
670 fmt
.dwMask
= PFM_LINESPACING
;
671 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
672 fmt
.bLineSpacingRule
= 5;
675 fmt
.dwMask
= PFM_NUMBERING
;
676 fmt
.wNumbering
= PFN_BULLET
;
679 fmt
.dwMask
= PFM_NUMBERING
;
680 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
682 case rtfParNumDecimal
:
683 fmt
.dwMask
= PFM_NUMBERING
;
684 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
686 case rtfParNumIndent
:
687 fmt
.dwMask
= PFM_NUMBERINGTAB
;
688 fmt
.wNumberingTab
= info
->rtfParam
;
690 case rtfParNumStartAt
:
691 fmt
.dwMask
= PFM_NUMBERINGSTART
;
692 fmt
.wNumberingStart
= info
->rtfParam
;
695 info
->borderType
= RTFBorderParaLeft
;
696 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
697 if (!(fmt
.dwMask
& PFM_BORDER
))
699 fmt
.wBorderSpace
= 0;
700 fmt
.wBorderWidth
= 1;
704 fmt
.dwMask
= PFM_BORDER
;
707 info
->borderType
= RTFBorderParaRight
;
708 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
709 if (!(fmt
.dwMask
& PFM_BORDER
))
711 fmt
.wBorderSpace
= 0;
712 fmt
.wBorderWidth
= 1;
716 fmt
.dwMask
= PFM_BORDER
;
719 info
->borderType
= RTFBorderParaTop
;
720 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
721 if (!(fmt
.dwMask
& PFM_BORDER
))
723 fmt
.wBorderSpace
= 0;
724 fmt
.wBorderWidth
= 1;
728 fmt
.dwMask
= PFM_BORDER
;
730 case rtfBorderBottom
:
731 info
->borderType
= RTFBorderParaBottom
;
732 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
733 if (!(fmt
.dwMask
& PFM_BORDER
))
735 fmt
.wBorderSpace
= 0;
736 fmt
.wBorderWidth
= 1;
740 fmt
.dwMask
= PFM_BORDER
;
742 case rtfBorderSingle
:
743 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
744 /* we assume that borders have been created before (RTF spec) */
745 fmt
.wBorders
&= ~0x700;
746 fmt
.wBorders
|= 1 << 8;
747 fmt
.dwMask
= PFM_BORDER
;
750 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
751 /* we assume that borders have been created before (RTF spec) */
752 fmt
.wBorders
&= ~0x700;
753 fmt
.wBorders
|= 2 << 8;
754 fmt
.dwMask
= PFM_BORDER
;
756 case rtfBorderShadow
:
757 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
758 /* we assume that borders have been created before (RTF spec) */
759 fmt
.wBorders
&= ~0x700;
760 fmt
.wBorders
|= 10 << 8;
761 fmt
.dwMask
= PFM_BORDER
;
763 case rtfBorderDouble
:
764 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
765 /* we assume that borders have been created before (RTF spec) */
766 fmt
.wBorders
&= ~0x700;
767 fmt
.wBorders
|= 7 << 8;
768 fmt
.dwMask
= PFM_BORDER
;
771 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
772 /* we assume that borders have been created before (RTF spec) */
773 fmt
.wBorders
&= ~0x700;
774 fmt
.wBorders
|= 11 << 8;
775 fmt
.dwMask
= PFM_BORDER
;
779 int borderSide
= info
->borderType
& RTFBorderSideMask
;
780 RTFTable
*tableDef
= info
->tableDef
;
781 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
782 /* we assume that borders have been created before (RTF spec) */
783 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
784 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
787 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
789 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
790 border
->width
= info
->rtfParam
;
793 fmt
.dwMask
= PFM_BORDER
;
797 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
798 /* we assume that borders have been created before (RTF spec) */
799 fmt
.wBorderSpace
= info
->rtfParam
;
800 fmt
.dwMask
= PFM_BORDER
;
804 RTFTable
*tableDef
= info
->tableDef
;
805 int borderSide
= info
->borderType
& RTFBorderSideMask
;
806 int borderType
= info
->borderType
& RTFBorderTypeMask
;
809 case RTFBorderTypePara
:
810 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
812 /* v1.0 - 3.0 treat paragraph and row borders the same. */
813 case RTFBorderTypeRow
:
815 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
818 case RTFBorderTypeCell
:
819 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
820 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
828 RTFFlushOutputBuffer(info
);
829 /* FIXME too slow ? how come ?*/
830 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
834 void ME_RTFTblAttrHook(RTF_Info
*info
)
836 switch (info
->rtfMinor
)
840 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
841 info
->borderType
= 0; /* Not sure */
842 else /* v1.0 - 3.0 */
843 info
->borderType
= RTFBorderRowTop
;
844 if (!info
->tableDef
) {
845 info
->tableDef
= ME_MakeTableDef(info
->editor
);
847 ME_InitTableDef(info
->editor
, info
->tableDef
);
856 info
->tableDef
= ME_MakeTableDef(info
->editor
);
858 cellNum
= info
->tableDef
->numCellsDefined
;
859 if (cellNum
>= MAX_TABLE_CELLS
)
861 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
862 if (cellNum
< MAX_TAB_STOPS
) {
863 /* Tab stops were used to store cell positions before v4.1 but v4.1
864 * still seems to set the tabstops without using them. */
865 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
866 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
867 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
868 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
870 info
->tableDef
->numCellsDefined
++;
874 info
->borderType
= RTFBorderRowTop
;
877 info
->borderType
= RTFBorderRowLeft
;
879 case rtfRowBordBottom
:
880 info
->borderType
= RTFBorderRowBottom
;
882 case rtfRowBordRight
:
883 info
->borderType
= RTFBorderRowRight
;
886 info
->borderType
= RTFBorderCellTop
;
888 case rtfCellBordLeft
:
889 info
->borderType
= RTFBorderCellLeft
;
891 case rtfCellBordBottom
:
892 info
->borderType
= RTFBorderCellBottom
;
894 case rtfCellBordRight
:
895 info
->borderType
= RTFBorderCellRight
;
899 info
->tableDef
->gapH
= info
->rtfParam
;
903 info
->tableDef
->leftEdge
= info
->rtfParam
;
908 void ME_RTFSpecialCharHook(RTF_Info
*info
)
910 RTFTable
*tableDef
= info
->tableDef
;
911 switch (info
->rtfMinor
)
914 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
916 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
920 RTFFlushOutputBuffer(info
);
921 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
922 if (tableDef
->tableRowStart
)
924 if (!info
->nestingLevel
&&
925 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
927 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
928 para
= para
->member
.para
.next_para
;
929 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
930 tableDef
->tableRowStart
= para
;
931 info
->nestingLevel
= 1;
933 ME_InsertTableCellFromCursor(info
->editor
);
935 } else { /* v1.0 - v3.0 */
936 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
937 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
938 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
939 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
942 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
943 tableDef
->numCellsInserted
++;
948 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
950 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
953 ME_DisplayItem
*para
, *cell
, *run
;
958 RTFFlushOutputBuffer(info
);
959 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
960 if (!tableDef
->tableRowStart
)
962 if (!info
->nestingLevel
&&
963 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
965 para
= tableDef
->tableRowStart
;
966 para
= para
->member
.para
.next_para
;
967 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
968 tableDef
->tableRowStart
= para
;
969 info
->nestingLevel
++;
971 para
= tableDef
->tableRowStart
;
972 cell
= ME_FindItemFwd(para
, diCell
);
973 assert(cell
&& !cell
->member
.cell
.prev_cell
);
974 if (tableDef
->numCellsDefined
< 1)
976 /* 2000 twips appears to be the cell size that native richedit uses
977 * when no cell sizes are specified. */
978 const int defaultCellSize
= 2000;
979 int nRightBoundary
= defaultCellSize
;
980 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
981 while (cell
->member
.cell
.next_cell
) {
982 cell
= cell
->member
.cell
.next_cell
;
983 nRightBoundary
+= defaultCellSize
;
984 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
986 para
= ME_InsertTableCellFromCursor(info
->editor
);
987 cell
= para
->member
.para
.pCell
;
988 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
990 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
992 RTFCell
*cellDef
= &tableDef
->cells
[i
];
993 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
994 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
996 cell
= cell
->member
.cell
.next_cell
;
999 para
= ME_InsertTableCellFromCursor(info
->editor
);
1000 cell
= para
->member
.para
.pCell
;
1003 /* Cell for table row delimiter is empty */
1004 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1007 run
= ME_FindItemFwd(cell
, diRun
);
1008 if (info
->editor
->pCursors
[0].pRun
!= run
||
1009 info
->editor
->pCursors
[0].nOffset
)
1012 /* Delete inserted cells that aren't defined. */
1013 info
->editor
->pCursors
[1].pRun
= run
;
1014 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1015 info
->editor
->pCursors
[1].nOffset
= 0;
1016 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1017 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1018 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1022 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1023 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1024 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1025 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1027 info
->nestingLevel
--;
1028 if (!info
->nestingLevel
)
1030 if (info
->canInheritInTbl
) {
1031 tableDef
->tableRowStart
= para
;
1033 while (info
->tableDef
) {
1034 tableDef
= info
->tableDef
;
1035 info
->tableDef
= tableDef
->parent
;
1036 heap_free(tableDef
);
1040 info
->tableDef
= tableDef
->parent
;
1041 heap_free(tableDef
);
1043 } else { /* v1.0 - v3.0 */
1045 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1046 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1047 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1048 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1050 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1052 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1055 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1056 tableDef
->numCellsInserted
++;
1058 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1059 if (!tableDef
->numCellsDefined
)
1060 pFmt
->wEffects
&= ~PFE_TABLE
;
1061 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1062 tableDef
->numCellsInserted
= 0;
1068 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1069 ME_DisplayItem
*para
;
1071 RTFFlushOutputBuffer(info
);
1072 para
= info
->editor
->pCursors
[0].pPara
;
1073 pFmt
= para
->member
.para
.pFmt
;
1074 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1076 /* rtfPar is treated like a space within a table. */
1077 info
->rtfClass
= rtfText
;
1078 info
->rtfMajor
= ' ';
1080 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1081 tableDef
->numCellsInserted
= 0;
1087 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1090 LPOLEOBJECT lpObject
= NULL
;
1091 LPSTORAGE lpStorage
= NULL
;
1092 LPOLECLIENTSITE lpClientSite
= NULL
;
1093 LPDATAOBJECT lpDataObject
= NULL
;
1094 LPOLECACHE lpOleCache
= NULL
;
1103 stgm
.tymed
= TYMED_ENHMF
;
1104 stgm
.u
.hEnhMetaFile
= hemf
;
1105 fm
.cfFormat
= CF_ENHMETAFILE
;
1109 stgm
.tymed
= TYMED_GDI
;
1110 stgm
.u
.hBitmap
= hbmp
;
1111 fm
.cfFormat
= CF_BITMAP
;
1113 stgm
.pUnkForRelease
= NULL
;
1116 fm
.dwAspect
= DVASPECT_CONTENT
;
1118 fm
.tymed
= stgm
.tymed
;
1120 if (!info
->lpRichEditOle
)
1122 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1125 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1127 /* FIXME: enable it when rich-edit properly implements this method */
1128 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1129 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1131 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1132 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1133 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1134 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1135 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1139 reobject
.cbStruct
= sizeof(reobject
);
1140 reobject
.cp
= REO_CP_SELECTION
;
1141 reobject
.clsid
= clsid
;
1142 reobject
.poleobj
= lpObject
;
1143 reobject
.pstg
= lpStorage
;
1144 reobject
.polesite
= lpClientSite
;
1145 /* convert from twips to .01 mm */
1146 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1147 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1148 reobject
.dvaspect
= DVASPECT_CONTENT
;
1149 reobject
.dwFlags
= 0; /* FIXME */
1150 reobject
.dwUser
= 0;
1152 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1156 if (lpObject
) IOleObject_Release(lpObject
);
1157 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1158 if (lpStorage
) IStorage_Release(lpStorage
);
1159 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1160 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1165 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1168 BYTE
* buffer
= NULL
;
1169 unsigned bufsz
, bufidx
;
1175 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1178 if (info
->rtfClass
== rtfEOF
)
1181 /* fetch picture type */
1182 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1184 mfp
.mm
= info
->rtfParam
;
1187 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1189 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1192 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1194 gfx
= gfx_enhmetafile
;
1198 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1202 /* fetch picture attributes */
1206 if (info
->rtfClass
== rtfEOF
)
1208 if (info
->rtfClass
== rtfText
)
1210 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1212 ERR("Expected picture attribute (%d %d)\n",
1213 info
->rtfClass
, info
->rtfMajor
);
1216 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1218 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1220 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1222 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1224 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1225 sz
.cx
= info
->rtfParam
;
1226 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1227 sz
.cy
= info
->rtfParam
;
1229 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1231 /* fetch picture data */
1234 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1235 val
= info
->rtfMajor
;
1236 for (flip
= TRUE
;; flip
= !flip
)
1239 if (info
->rtfClass
== rtfEOF
)
1241 HeapFree(GetProcessHeap(), 0, buffer
);
1242 return; /* Warn ?? */
1244 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1246 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1249 if (bufidx
>= bufsz
&&
1250 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1252 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1255 val
= info
->rtfMajor
;
1257 if (flip
) FIXME("wrong hex string\n");
1261 case gfx_enhmetafile
:
1262 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1263 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1266 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1267 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1271 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1273 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1275 /* not quite right, especially for bitfields type of compression */
1276 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1277 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1278 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1279 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1280 bi
, DIB_RGB_COLORS
)))
1281 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1288 HeapFree(GetProcessHeap(), 0, buffer
);
1289 RTFRouteToken (info
); /* feed "}" back to router */
1292 HeapFree(GetProcessHeap(), 0, buffer
);
1294 RTFRouteToken(info
); /* feed "}" back to router */
1297 /* for now, lookup the \result part and use it, whatever the object */
1298 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1303 if (info
->rtfClass
== rtfEOF
)
1305 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1307 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1310 if (info
->rtfClass
== rtfEOF
)
1312 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1316 while (RTFGetToken (info
) != rtfEOF
)
1318 if (info
->rtfClass
== rtfGroup
)
1320 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1321 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1323 RTFRouteToken(info
);
1326 else RTFSkipGroup(info
);
1329 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1331 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1335 RTFRouteToken(info
); /* feed "}" back to router */
1338 static void ME_RTFReadHook(RTF_Info
*info
)
1340 switch(info
->rtfClass
)
1343 switch(info
->rtfMajor
)
1346 if (info
->stackTop
< maxStack
) {
1347 info
->stack
[info
->stackTop
].style
= info
->style
;
1348 ME_AddRefStyle(info
->style
);
1349 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1350 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1353 info
->styleChanged
= FALSE
;
1357 RTFFlushOutputBuffer(info
);
1359 if (info
->stackTop
<= 0)
1360 info
->rtfClass
= rtfEOF
;
1361 if (info
->stackTop
< 0)
1364 ME_ReleaseStyle(info
->style
);
1365 info
->style
= info
->stack
[info
->stackTop
].style
;
1366 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1367 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1376 ME_StreamInFill(ME_InStream
*stream
)
1378 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1379 (BYTE
*)stream
->buffer
,
1380 sizeof(stream
->buffer
),
1381 (LONG
*)&stream
->dwSize
);
1385 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1389 int from
, to
, nUndoMode
;
1390 int nEventMask
= editor
->nEventMask
;
1391 ME_InStream inStream
;
1392 BOOL invalidRTF
= FALSE
;
1393 ME_Cursor
*selStart
, *selEnd
;
1395 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1396 editor
->nEventMask
= 0;
1398 ME_GetSelectionOfs(editor
, &from
, &to
);
1399 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1401 ME_GetSelection(editor
, &selStart
, &selEnd
);
1402 style
= ME_GetSelectionInsertStyle(editor
);
1404 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1406 /* Don't insert text at the end of the table row */
1407 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1408 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1409 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1411 para
= para
->member
.para
.next_para
;
1412 editor
->pCursors
[0].pPara
= para
;
1413 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1414 editor
->pCursors
[0].nOffset
= 0;
1416 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1418 para
= para
->member
.para
.next_para
;
1419 editor
->pCursors
[0].pPara
= para
;
1420 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1421 editor
->pCursors
[0].nOffset
= 0;
1423 editor
->pCursors
[1] = editor
->pCursors
[0];
1424 } else { /* v1.0 - 3.0 */
1425 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1426 ME_IsInTable(editor
->pCursors
[0].pRun
))
1430 style
= editor
->pBuffer
->pDefaultStyle
;
1431 ME_AddRefStyle(style
);
1432 ME_SetSelection(editor
, 0, 0);
1433 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1434 ME_GetTextLength(editor
), FALSE
);
1436 ME_ClearTempStyle(editor
);
1437 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1441 /* Back up undo mode to a local variable */
1442 nUndoMode
= editor
->nUndoMode
;
1444 /* Only create an undo if SFF_SELECTION is set */
1445 if (!(format
& SFF_SELECTION
))
1446 editor
->nUndoMode
= umIgnore
;
1448 inStream
.editstream
= stream
;
1449 inStream
.editstream
->dwError
= 0;
1450 inStream
.dwSize
= 0;
1451 inStream
.dwUsed
= 0;
1453 if (format
& SF_RTF
)
1455 /* Check if it's really RTF, and if it is not, use plain text */
1456 ME_StreamInFill(&inStream
);
1457 if (!inStream
.editstream
->dwError
)
1459 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1460 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1463 inStream
.editstream
->dwError
= -16;
1468 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1470 if (format
& SF_RTF
) {
1471 /* setup the RTF parser */
1472 memset(&parser
, 0, sizeof parser
);
1473 RTFSetEditStream(&parser
, &inStream
);
1474 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1475 parser
.editor
= editor
;
1476 parser
.style
= style
;
1477 WriterInit(&parser
);
1479 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1480 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1481 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1482 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1484 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1485 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1489 /* do the parsing */
1491 RTFFlushOutputBuffer(&parser
);
1492 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1493 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1494 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1496 /* Delete any incomplete table row at the end of the rich text. */
1498 ME_DisplayItem
*pCell
;
1499 ME_DisplayItem
*para
;
1501 parser
.rtfMinor
= rtfRow
;
1502 /* Complete the table row before deleting it.
1503 * By doing it this way we will have the current paragraph format set
1504 * properly to reflect that is not in the complete table, and undo items
1505 * will be added for this change to the current paragraph format. */
1506 if (parser
.nestingLevel
> 0)
1508 while (parser
.nestingLevel
> 1)
1509 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1510 para
= parser
.tableDef
->tableRowStart
;
1511 ME_RTFSpecialCharHook(&parser
);
1513 para
= parser
.tableDef
->tableRowStart
;
1514 ME_RTFSpecialCharHook(&parser
);
1515 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1516 para
= para
->member
.para
.next_para
;
1518 pCell
= para
->member
.para
.pCell
;
1520 editor
->pCursors
[1].pPara
= para
;
1521 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1522 editor
->pCursors
[1].nOffset
= 0;
1523 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1524 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1525 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1526 if (parser
.tableDef
)
1527 parser
.tableDef
->tableRowStart
= NULL
;
1530 ME_CheckTablesForCorruption(editor
);
1531 RTFDestroy(&parser
);
1532 if (parser
.lpRichEditOle
)
1533 IRichEditOle_Release(parser
.lpRichEditOle
);
1535 if (parser
.stackTop
> 0)
1537 while (--parser
.stackTop
>= 0)
1539 ME_ReleaseStyle(parser
.style
);
1540 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1542 if (!inStream
.editstream
->dwError
)
1543 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1546 /* Remove last line break, as mandated by tests. This is not affected by
1547 CR/LF counters, since RTF streaming presents only \para tokens, which
1548 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1552 ME_GetSelection(editor
, &selStart
, &selEnd
);
1553 newto
= ME_GetCursorOfs(selEnd
);
1554 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1555 WCHAR lastchar
[3] = {'\0', '\0'};
1556 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1557 ME_Cursor linebreakCursor
= *selEnd
;
1559 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1560 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, 0);
1561 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1562 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1567 style
= parser
.style
;
1569 else if (format
& SF_TEXT
)
1570 ME_StreamInText(editor
, format
, &inStream
, style
);
1572 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1573 /* put the cursor at the top */
1574 if (!(format
& SFF_SELECTION
))
1575 ME_SetSelection(editor
, 0, 0);
1578 /* Restore saved undo mode */
1579 editor
->nUndoMode
= nUndoMode
;
1581 /* even if we didn't add an undo, we need to commit anything on the stack */
1582 ME_CommitUndo(editor
);
1584 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1585 if (!(format
& SFF_SELECTION
))
1586 ME_EmptyUndoStack(editor
);
1588 ME_ReleaseStyle(style
);
1589 editor
->nEventMask
= nEventMask
;
1590 ME_UpdateRepaint(editor
);
1591 if (!(format
& SFF_SELECTION
)) {
1592 ME_ClearTempStyle(editor
);
1594 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1595 ME_MoveCaret(editor
);
1596 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1597 ME_SendSelChange(editor
);
1598 ME_SendRequestResize(editor
, FALSE
);
1604 typedef struct tagME_RTFStringStreamStruct
1609 } ME_RTFStringStreamStruct
;
1611 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1613 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1616 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1617 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1618 pStruct
->pos
+= count
;
1624 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1627 ME_RTFStringStreamStruct data
;
1629 data
.string
= string
;
1630 data
.length
= strlen(string
);
1632 es
.dwCookie
= (DWORD_PTR
)&data
;
1633 es
.pfnCallback
= ME_ReadFromRTFString
;
1634 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1639 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1641 const int nLen
= lstrlenW(text
);
1642 const int nTextLen
= ME_GetTextLength(editor
);
1645 WCHAR wLastChar
= ' ';
1647 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1648 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1650 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1651 FIXME("Flags 0x%08x not implemented\n",
1652 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1655 if (chrg
->cpMax
== -1)
1658 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1660 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1661 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1666 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1667 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1671 chrgText
->cpMin
= -1;
1672 chrgText
->cpMax
= -1;
1677 /* when searching up, if cpMin < cpMax, then instead of searching
1678 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1679 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1680 * case, it is always bigger than cpMin.
1682 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1686 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1687 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1693 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1696 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1700 if (flags
& FR_DOWN
) /* Forward search */
1702 /* If possible, find the character before where the search starts */
1703 if ((flags
& FR_WHOLEWORD
) && nMin
)
1705 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1706 wLastChar
= cursor
.pRun
->member
.run
.strText
->szData
[cursor
.nOffset
];
1707 ME_MoveCursorChars(editor
, &cursor
, 1);
1709 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1712 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1714 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1715 int nCurStart
= cursor
.nOffset
;
1718 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
], text
[nMatched
], (flags
& FR_MATCHCASE
)))
1720 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1724 if (nMatched
== nLen
)
1726 ME_DisplayItem
*pNextItem
= pCurItem
;
1727 int nNextStart
= nCurStart
;
1730 /* Check to see if next character is a whitespace */
1731 if (flags
& FR_WHOLEWORD
)
1733 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.strText
->nLen
)
1735 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1736 nNextStart
= -nMatched
;
1740 wNextChar
= pNextItem
->member
.run
.strText
->szData
[nNextStart
+ nMatched
];
1744 if (isalnumW(wNextChar
))
1748 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1751 chrgText
->cpMin
= cursor
.nOffset
;
1752 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1754 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1755 return cursor
.nOffset
;
1757 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.strText
->nLen
)
1759 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1760 nCurStart
= -nMatched
;
1764 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
];
1769 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.strText
->nLen
)
1771 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1776 else /* Backward search */
1778 /* If possible, find the character after where the search ends */
1779 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1781 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1782 wLastChar
= cursor
.pRun
->member
.run
.strText
->szData
[cursor
.nOffset
];
1783 ME_MoveCursorChars(editor
, &cursor
, -1);
1785 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1788 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1790 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1791 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1792 int nCurEnd
= cursor
.nOffset
;
1797 ME_PrevRun(&pCurPara
, &pCurItem
);
1798 nCurEnd
= pCurItem
->member
.run
.strText
->nLen
+ nMatched
;
1801 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1], text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
)))
1803 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1807 if (nMatched
== nLen
)
1809 ME_DisplayItem
*pPrevItem
= pCurItem
;
1810 int nPrevEnd
= nCurEnd
;
1814 /* Check to see if previous character is a whitespace */
1815 if (flags
& FR_WHOLEWORD
)
1817 if (nPrevEnd
- nMatched
== 0)
1819 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1821 nPrevEnd
= pPrevItem
->member
.run
.strText
->nLen
+ nMatched
;
1825 wPrevChar
= pPrevItem
->member
.run
.strText
->szData
[nPrevEnd
- nMatched
- 1];
1829 if (isalnumW(wPrevChar
))
1833 nStart
= pCurPara
->member
.para
.nCharOfs
1834 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1837 chrgText
->cpMin
= nStart
;
1838 chrgText
->cpMax
= nStart
+ nLen
;
1840 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1843 if (nCurEnd
- nMatched
== 0)
1845 ME_PrevRun(&pCurPara
, &pCurItem
);
1846 /* Don't care about pCurItem becoming NULL here; it's already taken
1847 * care of in the exterior loop condition */
1848 nCurEnd
= pCurItem
->member
.run
.strText
->nLen
+ nMatched
;
1852 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1];
1857 if (cursor
.nOffset
< 0)
1859 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1860 cursor
.nOffset
= cursor
.pRun
->member
.run
.strText
->nLen
;
1864 TRACE("not found\n");
1866 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1870 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1875 if (!ex
->cb
|| !pText
) return 0;
1877 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1878 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1880 if (ex
->flags
& GT_SELECTION
)
1883 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
1884 start
= editor
->pCursors
[nStartCur
];
1889 ME_SetCursorToStart(editor
, &start
);
1892 if (ex
->codepage
== 1200)
1894 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
1895 &start
, nChars
, ex
->flags
& GT_USECRLF
);
1899 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1900 we can just take a bigger buffer? :)
1901 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1902 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1904 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1909 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
1910 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
1912 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
);
1913 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
1914 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1915 if (rc
) rc
--; /* do not count 0 terminator */
1922 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
1923 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
1925 if (!strText
) return 0;
1927 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, 0);
1930 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
1932 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, 0);
1933 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
1934 nLen
+1, NULL
, NULL
);
1940 typedef struct tagME_GlobalDestStruct
1944 } ME_GlobalDestStruct
;
1946 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1948 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1953 pDest
= (WORD
*)lpBuff
;
1954 pSrc
= GlobalLock(pData
->hData
);
1955 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1956 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1958 pData
->nLength
+= i
;
1960 GlobalUnlock(pData
->hData
);
1964 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1966 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1971 pSrc
= GlobalLock(pData
->hData
);
1972 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1973 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1975 pData
->nLength
+= i
;
1977 GlobalUnlock(pData
->hData
);
1981 static BOOL
ME_Paste(ME_TextEditor
*editor
)
1985 ME_GlobalDestStruct gds
;
1986 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
1989 if (IsClipboardFormatAvailable(nRTFFormat
))
1990 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
1991 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
1992 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
1996 if (!OpenClipboard(editor
->hWnd
))
1998 gds
.hData
= GetClipboardData(cf
);
2000 es
.dwCookie
= (DWORD_PTR
)&gds
;
2001 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2002 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2008 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2010 LPDATAOBJECT dataObj
= NULL
;
2013 if (editor
->cPasswordMask
)
2014 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2016 if(editor
->lpOleCallback
)
2019 range
.cpMin
= ME_GetCursorOfs(start
);
2020 range
.cpMax
= range
.cpMin
+ nChars
;
2021 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2023 if(FAILED(hr
) || !dataObj
)
2024 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2026 hr
= OleSetClipboard(dataObj
);
2027 IDataObject_Release(dataObj
);
2029 return SUCCEEDED(hr
) != 0;
2032 /* helper to send a msg filter notification */
2034 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2038 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2039 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2040 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2041 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2043 msgf
.wParam
= *wParam
;
2044 msgf
.lParam
= *lParam
;
2045 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2047 *wParam
= msgf
.wParam
;
2048 *lParam
= msgf
.lParam
;
2049 msgf
.wParam
= *wParam
;
2054 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2056 ME_DisplayItem
*startPara
, *endPara
;
2057 ME_DisplayItem
*prev_para
;
2058 ME_Cursor
*from
, *to
;
2062 if (!editor
->AutoURLDetect_bEnable
) return;
2064 ME_GetSelection(editor
, &from
, &to
);
2066 /* Find paragraph previous to the one that contains start cursor */
2067 startPara
= from
->pPara
;
2068 prev_para
= startPara
->member
.para
.prev_para
;
2069 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2071 /* Find paragraph that contains end cursor */
2072 endPara
= to
->pPara
->member
.para
.next_para
;
2074 start
.pPara
= startPara
;
2075 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2077 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2079 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2083 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2085 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2086 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2088 if (editor
->bMouseCaptured
)
2090 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2091 editor
->nSelectionType
= stPosition
;
2099 editor
->nUDArrowX
= -1;
2105 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2106 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2110 editor
->nUDArrowX
= -1;
2111 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2112 if (editor
->styleFlags
& ES_READONLY
)
2114 if (ME_IsSelection(editor
))
2116 ME_DeleteSelection(editor
);
2117 ME_CommitUndo(editor
);
2119 else if (nKey
== VK_DELETE
)
2121 /* Delete stops group typing.
2122 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2123 ME_DeleteTextAtCursor(editor
, 1, 1);
2124 ME_CommitUndo(editor
);
2126 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2128 BOOL bDeletionSucceeded
;
2129 /* Backspace can be grouped for a single undo */
2130 ME_ContinueCoalescingTransaction(editor
);
2131 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2132 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2133 /* Deletion was prevented so the cursor is moved back to where it was.
2134 * (e.g. this happens when trying to delete cell boundaries)
2136 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2138 ME_CommitCoalescingUndo(editor
);
2142 ME_MoveCursorFromTableRowStartParagraph(editor
);
2143 ME_UpdateSelectionLinkAttribute(editor
);
2144 ME_UpdateRepaint(editor
);
2145 ME_SendRequestResize(editor
, FALSE
);
2148 if (editor
->bDialogMode
)
2153 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2155 if (editor
->hwndParent
)
2158 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2159 if (HIWORD(dw
) == DC_HASDEFID
)
2161 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2164 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, (LPARAM
)TRUE
);
2165 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2173 if (editor
->styleFlags
& ES_MULTILINE
)
2175 ME_Cursor cursor
= editor
->pCursors
[0];
2176 ME_DisplayItem
*para
= cursor
.pPara
;
2178 const WCHAR endl
= '\r';
2181 if (editor
->styleFlags
& ES_READONLY
) {
2182 MessageBeep(MB_ICONERROR
);
2186 ME_GetSelectionOfs(editor
, &from
, &to
);
2187 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2189 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2190 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2191 /* Add a new table row after this row. */
2192 para
= ME_AppendTableRow(editor
, para
);
2193 para
= para
->member
.para
.next_para
;
2194 editor
->pCursors
[0].pPara
= para
;
2195 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2196 editor
->pCursors
[0].nOffset
= 0;
2197 editor
->pCursors
[1] = editor
->pCursors
[0];
2198 ME_CommitUndo(editor
);
2199 ME_CheckTablesForCorruption(editor
);
2200 ME_UpdateRepaint(editor
);
2203 else if (para
== editor
->pCursors
[1].pPara
&&
2204 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2205 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2206 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2208 /* Insert a newline before the table. */
2209 para
= para
->member
.para
.prev_para
;
2210 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2211 editor
->pCursors
[0].pPara
= para
;
2212 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2213 editor
->pCursors
[1] = editor
->pCursors
[0];
2214 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2215 editor
->pCursors
[0].pRun
->member
.run
.style
);
2216 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2217 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2218 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2219 editor
->pCursors
[0].pPara
= para
;
2220 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2221 editor
->pCursors
[1] = editor
->pCursors
[0];
2222 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2223 ME_CommitCoalescingUndo(editor
);
2224 ME_CheckTablesForCorruption(editor
);
2225 ME_UpdateRepaint(editor
);
2228 } else { /* v1.0 - 3.0 */
2229 ME_DisplayItem
*para
= cursor
.pPara
;
2230 if (ME_IsInTable(para
))
2232 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2235 ME_ContinueCoalescingTransaction(editor
);
2236 para
= ME_AppendTableRow(editor
, para
);
2237 editor
->pCursors
[0].pPara
= para
;
2238 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2239 editor
->pCursors
[0].nOffset
= 0;
2240 editor
->pCursors
[1] = editor
->pCursors
[0];
2241 ME_CommitCoalescingUndo(editor
);
2242 ME_UpdateRepaint(editor
);
2246 ME_ContinueCoalescingTransaction(editor
);
2247 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2248 !ME_IsInTable(para
->member
.para
.prev_para
))
2250 /* Insert newline before table */
2251 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2253 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2254 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2256 editor
->pCursors
[0].nOffset
= 0;
2257 editor
->pCursors
[1] = editor
->pCursors
[0];
2258 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2259 editor
->pCursors
[0].pRun
->member
.run
.style
);
2261 editor
->pCursors
[1] = editor
->pCursors
[0];
2262 para
= ME_AppendTableRow(editor
, para
);
2263 editor
->pCursors
[0].pPara
= para
;
2264 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2265 editor
->pCursors
[0].nOffset
= 0;
2266 editor
->pCursors
[1] = editor
->pCursors
[0];
2268 ME_CommitCoalescingUndo(editor
);
2269 ME_UpdateRepaint(editor
);
2275 style
= ME_GetInsertStyle(editor
, 0);
2276 ME_SaveTempStyle(editor
);
2277 ME_ContinueCoalescingTransaction(editor
);
2279 ME_InsertEndRowFromCursor(editor
, 0);
2281 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2282 ME_ReleaseStyle(style
);
2283 ME_CommitCoalescingUndo(editor
);
2286 ME_UpdateSelectionLinkAttribute(editor
);
2287 ME_UpdateRepaint(editor
);
2293 if (editor
->bDialogMode
&& editor
->hwndParent
)
2294 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2297 if (editor
->bDialogMode
&& editor
->hwndParent
)
2298 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2303 ME_SetSelection(editor
, 0, -1);
2309 return ME_Paste(editor
);
2317 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2318 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2321 result
= ME_Copy(editor
, selStart
, nChars
);
2322 if (result
&& nKey
== 'X')
2324 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2325 ME_CommitUndo(editor
);
2326 ME_UpdateRepaint(editor
);
2347 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2348 editor
->nUDArrowX
= -1;
2355 chf
.cbSize
= sizeof(chf
);
2357 ME_GetSelectionCharFormat(editor
, &chf
);
2358 ME_DumpStyleToBuf(&chf
, buf
);
2359 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2363 ME_CheckCharOffsets(editor
);
2370 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2371 LPARAM flags
, BOOL unicode
)
2375 if (editor
->bMouseCaptured
)
2379 wstr
= (WCHAR
)charCode
;
2382 CHAR charA
= charCode
;
2383 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2386 if (editor
->styleFlags
& ES_READONLY
) {
2387 MessageBeep(MB_ICONERROR
);
2388 return 0; /* FIXME really 0 ? */
2391 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2393 ME_Cursor cursor
= editor
->pCursors
[0];
2394 ME_DisplayItem
*para
= cursor
.pPara
;
2396 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2397 ME_GetSelectionOfs(editor
, &from
, &to
);
2399 /* v4.1 allows tabs to be inserted with ctrl key down */
2400 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2402 ME_DisplayItem
*para
;
2403 BOOL bSelectedRow
= FALSE
;
2405 para
= cursor
.pPara
;
2406 if (ME_IsSelection(editor
) &&
2407 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2408 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2409 para
->member
.para
.prev_para
->type
== diParagraph
)
2411 para
= para
->member
.para
.prev_para
;
2412 bSelectedRow
= TRUE
;
2414 if (ME_IsInTable(para
))
2416 ME_TabPressedInTable(editor
, bSelectedRow
);
2417 ME_CommitUndo(editor
);
2420 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2421 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2423 para
= para
->member
.para
.next_para
;
2424 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2425 para
= para
->member
.para
.next_para
;
2426 editor
->pCursors
[0].pPara
= para
;
2427 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2428 editor
->pCursors
[0].nOffset
= 0;
2429 editor
->pCursors
[1] = editor
->pCursors
[0];
2432 } else { /* v1.0 - 3.0 */
2433 if (ME_IsInTable(cursor
.pRun
) &&
2434 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2437 /* Text should not be inserted at the end of the table. */
2442 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2443 /* WM_CHAR is restricted to nTextLimit */
2444 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2446 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2447 ME_SaveTempStyle(editor
);
2448 ME_ContinueCoalescingTransaction(editor
);
2449 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2450 ME_ReleaseStyle(style
);
2451 ME_CommitCoalescingUndo(editor
);
2452 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2455 ME_UpdateSelectionLinkAttribute(editor
);
2456 ME_UpdateRepaint(editor
);
2461 /* Process the message and calculate the new click count.
2463 * returns: The click count if it is mouse down event, else returns 0. */
2464 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2467 static int clickNum
= 0;
2468 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2471 if ((msg
== WM_LBUTTONDBLCLK
) ||
2472 (msg
== WM_RBUTTONDBLCLK
) ||
2473 (msg
== WM_MBUTTONDBLCLK
) ||
2474 (msg
== WM_XBUTTONDBLCLK
))
2476 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2479 if ((msg
== WM_LBUTTONDOWN
) ||
2480 (msg
== WM_RBUTTONDOWN
) ||
2481 (msg
== WM_MBUTTONDOWN
) ||
2482 (msg
== WM_XBUTTONDOWN
))
2484 static MSG prevClickMsg
;
2486 /* Compare the editor instead of the hwnd so that the this
2487 * can still be done for windowless richedit controls. */
2488 clickMsg
.hwnd
= (HWND
)editor
;
2489 clickMsg
.message
= msg
;
2490 clickMsg
.wParam
= wParam
;
2491 clickMsg
.lParam
= lParam
;
2492 clickMsg
.time
= GetMessageTime();
2493 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2494 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2495 if ((clickNum
!= 0) &&
2496 (clickMsg
.message
== prevClickMsg
.message
) &&
2497 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2498 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2499 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2500 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2501 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2507 prevClickMsg
= clickMsg
;
2514 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2520 DWORD messagePos
= GetMessagePos();
2521 pt
.x
= (short)LOWORD(messagePos
);
2522 pt
.y
= (short)HIWORD(messagePos
);
2526 sbi
.cbSize
= sizeof(sbi
);
2527 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2528 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2529 PtInRect(&sbi
.rcScrollBar
, pt
))
2531 ITextHost_TxSetCursor(editor
->texthost
,
2532 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2535 sbi
.cbSize
= sizeof(sbi
);
2536 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2537 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2538 PtInRect(&sbi
.rcScrollBar
, pt
))
2540 ITextHost_TxSetCursor(editor
->texthost
,
2541 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2545 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2547 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2548 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2551 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2552 pt
.y
< editor
->rcFormat
.top
&&
2553 pt
.x
< editor
->rcFormat
.left
)
2555 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2558 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2560 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2561 ITextHost_TxSetCursor(editor
->texthost
,
2562 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2564 ITextHost_TxSetCursor(editor
->texthost
,
2565 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2568 if (pt
.x
< editor
->rcFormat
.left
)
2570 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2573 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2578 run
= &cursor
.pRun
->member
.run
;
2579 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2580 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2582 ITextHost_TxSetCursor(editor
->texthost
,
2583 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2588 if (ME_IsSelection(editor
))
2590 int selStart
, selEnd
;
2591 int offset
= ME_GetCursorOfs(&cursor
);
2593 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2594 if (selStart
<= offset
&& selEnd
>= offset
) {
2595 ITextHost_TxSetCursor(editor
->texthost
,
2596 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2602 ITextHost_TxSetCursor(editor
->texthost
,
2603 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2607 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2609 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2610 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2611 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2612 editor
->rcFormat
.right
-= 1;
2615 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2620 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2622 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2623 if(selrange
.cpMin
== selrange
.cpMax
)
2624 seltype
|= SEL_EMPTY
;
2627 /* FIXME: Handle objects */
2628 seltype
|= SEL_TEXT
;
2629 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2630 seltype
|= SEL_MULTICHAR
;
2632 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2634 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2640 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2642 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2648 ed
->hwndParent
= NULL
;
2649 ed
->texthost
= texthost
;
2650 ed
->bEmulateVersion10
= bEmulateVersion10
;
2652 ITextHost_TxGetPropertyBits(texthost
,
2653 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2654 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2655 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2656 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2657 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2659 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2660 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2661 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2662 ed
->pBuffer
= ME_MakeText();
2663 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2664 ed
->nAvailWidth
= 0; /* wrap to client area */
2665 ME_MakeFirstParagraph(ed
);
2666 /* The four cursors are for:
2667 * 0 - The position where the caret is shown
2668 * 1 - The anchored end of the selection (for normal selection)
2669 * 2 & 3 - The anchored start and end respectively for word, line,
2670 * or paragraph selection.
2673 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2674 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2675 ed
->pCursors
[1] = ed
->pCursors
[0];
2676 ed
->pCursors
[2] = ed
->pCursors
[0];
2677 ed
->pCursors
[3] = ed
->pCursors
[1];
2678 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2679 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2682 ed
->rgbBackColor
= -1;
2683 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2684 ed
->bCaretAtEnd
= FALSE
;
2686 ed
->nModifyStep
= 0;
2687 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2688 ed
->pUndoStack
= ed
->pRedoStack
= ed
->pUndoStackBottom
= NULL
;
2689 ed
->nUndoStackSize
= 0;
2690 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2691 ed
->nUndoMode
= umAddToUndo
;
2692 ed
->nParagraphs
= 1;
2693 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2694 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2695 ed
->bHideSelection
= FALSE
;
2696 ed
->pfnWordBreak
= NULL
;
2697 ed
->lpOleCallback
= NULL
;
2698 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2699 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2700 ed
->AutoURLDetect_bEnable
= FALSE
;
2701 ed
->bHaveFocus
= FALSE
;
2702 ed
->bDialogMode
= FALSE
;
2703 ed
->bMouseCaptured
= FALSE
;
2704 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2706 ed
->pFontCache
[i
].nRefs
= 0;
2707 ed
->pFontCache
[i
].nAge
= 0;
2708 ed
->pFontCache
[i
].hFont
= NULL
;
2711 ME_CheckCharOffsets(ed
);
2712 ed
->bDefaultFormatRect
= TRUE
;
2713 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2715 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2716 ed
->selofs
= SELECTIONBAR_WIDTH
;
2717 ed
->styleFlags
|= ES_SELECTIONBAR
;
2721 ed
->nSelectionType
= stPosition
;
2723 ed
->cPasswordMask
= 0;
2724 if (props
& TXTBIT_USEPASSWORD
)
2725 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2727 if (props
& TXTBIT_AUTOWORDSEL
)
2728 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2729 if (props
& TXTBIT_MULTILINE
) {
2730 ed
->styleFlags
|= ES_MULTILINE
;
2731 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2733 ed
->bWordWrap
= FALSE
;
2735 if (props
& TXTBIT_READONLY
)
2736 ed
->styleFlags
|= ES_READONLY
;
2737 if (!(props
& TXTBIT_HIDESELECTION
))
2738 ed
->styleFlags
|= ES_NOHIDESEL
;
2739 if (props
& TXTBIT_SAVESELECTION
)
2740 ed
->styleFlags
|= ES_SAVESEL
;
2741 if (props
& TXTBIT_VERTICAL
)
2742 ed
->styleFlags
|= ES_VERTICAL
;
2743 if (props
& TXTBIT_DISABLEDRAG
)
2744 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2746 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2748 /* Default scrollbar information */
2749 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2750 ed
->vert_si
.nMin
= 0;
2751 ed
->vert_si
.nMax
= 0;
2752 ed
->vert_si
.nPage
= 0;
2753 ed
->vert_si
.nPos
= 0;
2755 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2756 ed
->horz_si
.nMin
= 0;
2757 ed
->horz_si
.nMax
= 0;
2758 ed
->horz_si
.nPage
= 0;
2759 ed
->horz_si
.nPos
= 0;
2761 OleInitialize(NULL
);
2766 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2768 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2769 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2772 ME_ClearTempStyle(editor
);
2773 ME_EmptyUndoStack(editor
);
2776 ME_DestroyDisplayItem(p
);
2779 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2780 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2782 if (editor
->pFontCache
[i
].hFont
)
2783 DeleteObject(editor
->pFontCache
[i
].hFont
);
2785 if (editor
->rgbBackColor
!= -1)
2786 DeleteObject(editor
->hbrBackground
);
2787 if(editor
->lpOleCallback
)
2788 IUnknown_Release(editor
->lpOleCallback
);
2789 IUnknown_Release(editor
->texthost
);
2792 FREE_OBJ(editor
->pBuffer
);
2793 FREE_OBJ(editor
->pCursors
);
2798 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2803 case DLL_PROCESS_ATTACH
:
2804 DisableThreadLibraryCalls(hinstDLL
);
2805 me_heap
= HeapCreate (0, 0x10000, 0);
2806 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2807 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2811 case DLL_PROCESS_DETACH
:
2812 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2813 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2814 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2815 UnregisterClassA("RichEdit50A", 0);
2816 if (ME_ListBoxRegistered
)
2817 UnregisterClassW(REListBox20W
, 0);
2818 if (ME_ComboBoxRegistered
)
2819 UnregisterClassW(REComboBox20W
, 0);
2821 HeapDestroy (me_heap
);
2829 static const char * const edit_messages
[] = {
2858 "EM_SETPASSWORDCHAR",
2859 "EM_EMPTYUNDOBUFFER",
2860 "EM_GETFIRSTVISIBLELINE",
2862 "EM_SETWORDBREAKPROC",
2863 "EM_GETWORDBREAKPROC",
2864 "EM_GETPASSWORDCHAR",
2874 static const char * const richedit_messages
[] = {
2879 "EM_EXLINEFROMCHAR",
2885 "EM_GETOLEINTERFACE",
2895 "EM_SETOLECALLBACK",
2897 "EM_SETTARGETDEVICE",
2905 "EM_GETWORDBREAKPROCEX",
2906 "EM_SETWORDBREAKPROCEX",
2908 "EM_UNKNOWN_USER_83",
2913 "EM_STOPGROUPTYPING",
2917 "EM_GETAUTOURLDETECT",
2920 "EM_GETTEXTLENGTHEX",
2923 "EM_UNKNOWN_USER_98",
2924 "EM_UNKNOWN_USER_99",
2925 "EM_SETPUNCTUATION",
2926 "EM_GETPUNCTUATION",
2927 "EM_SETWORDWRAPMODE",
2928 "EM_GETWORDWRAPMODE",
2934 "EM_UNKNOWN_USER_109",
2935 "EM_UNKNOWN_USER_110",
2936 "EM_UNKNOWN_USER_111",
2937 "EM_UNKNOWN_USER_112",
2938 "EM_UNKNOWN_USER_113",
2939 "EM_UNKNOWN_USER_114",
2940 "EM_UNKNOWN_USER_115",
2941 "EM_UNKNOWN_USER_116",
2942 "EM_UNKNOWN_USER_117",
2943 "EM_UNKNOWN_USER_118",
2944 "EM_UNKNOWN_USER_119",
2945 "EM_SETLANGOPTIONS",
2946 "EM_GETLANGOPTIONS",
2947 "EM_GETIMECOMPMODE",
2951 "EM_SETIMEMODEBIAS",
2956 get_msg_name(UINT msg
)
2958 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
2959 return edit_messages
[msg
- EM_GETSEL
];
2960 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
2961 return richedit_messages
[msg
- EM_CANPASTE
];
2965 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2969 ME_Cursor cursor
; /* The start of the clicked text. */
2972 x
= (short)LOWORD(lParam
);
2973 y
= (short)HIWORD(lParam
);
2974 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
2975 if (!isExact
) return;
2977 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
2978 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
2979 { /* The clicked run has CFE_LINK set */
2980 info
.nmhdr
.hwndFrom
= NULL
;
2981 info
.nmhdr
.idFrom
= 0;
2982 info
.nmhdr
.code
= EN_LINK
;
2984 info
.wParam
= wParam
;
2985 info
.lParam
= lParam
;
2987 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
2988 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ cursor
.pRun
->member
.run
.strText
->nLen
;
2989 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
2993 #define UNSUPPORTED_MSG(e) \
2995 FIXME(#e ": stub\n"); \
2996 *phresult = S_FALSE; \
2999 /* Handle messages for windowless and windoweded richedit controls.
3001 * The LRESULT that is returned is a return value for window procs,
3002 * and the phresult parameter is the COM return code needed by the
3003 * text services interface. */
3004 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3005 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3011 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3012 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3013 UNSUPPORTED_MSG(EM_FMTLINES
)
3014 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3015 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3016 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3017 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3018 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3019 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3020 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3021 UNSUPPORTED_MSG(EM_GETREDONAME
)
3022 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3023 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3024 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3025 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3026 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3027 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3028 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3029 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
3030 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3031 UNSUPPORTED_MSG(EM_SETMARGINS
)
3032 UNSUPPORTED_MSG(EM_SETPALETTE
)
3033 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3034 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3035 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3037 /* Messages specific to Richedit controls */
3040 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3042 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3045 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
|DLGC_HASSETSEL
;
3047 editor
->bDialogMode
= TRUE
;
3048 if (editor
->styleFlags
& ES_MULTILINE
)
3049 code
|= DLGC_WANTMESSAGE
;
3052 case EM_EMPTYUNDOBUFFER
:
3053 ME_EmptyUndoStack(editor
);
3057 /* Note: wParam/lParam can be NULL */
3059 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3060 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3061 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3062 if ((*pfrom
|*pto
) & 0xFFFF0000)
3064 return MAKELONG(*pfrom
,*pto
);
3068 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3069 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3070 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3073 case EM_SETUNDOLIMIT
:
3075 if ((int)wParam
< 0)
3076 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3078 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3079 /* Setting a max stack size keeps wine from getting killed
3080 for hogging memory. Windows allocates all this memory at once, so
3081 no program would realistically set a value above our maximum. */
3082 return editor
->nUndoLimit
;
3085 return editor
->pUndoStack
!= NULL
;
3087 return editor
->pRedoStack
!= NULL
;
3088 case WM_UNDO
: /* FIXME: actually not the same */
3090 return ME_Undo(editor
);
3092 return ME_Redo(editor
);
3095 /* these flags are equivalent to the ES_* counterparts */
3096 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3097 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3098 DWORD settings
= editor
->styleFlags
& mask
;
3104 /* these flags are equivalent to ES_* counterparts, except for
3105 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3106 * but is still stored in editor->styleFlags. */
3107 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3108 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3109 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3110 DWORD settings
= mask
& editor
->styleFlags
;
3111 DWORD oldSettings
= settings
;
3112 DWORD changedSettings
;
3128 changedSettings
= oldSettings
^ settings
;
3130 if (changedSettings
) {
3131 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3133 if (changedSettings
& ECO_SELECTIONBAR
)
3135 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3136 if (settings
& ECO_SELECTIONBAR
) {
3137 assert(!editor
->selofs
);
3138 editor
->selofs
= SELECTIONBAR_WIDTH
;
3139 editor
->rcFormat
.left
+= editor
->selofs
;
3141 editor
->rcFormat
.left
-= editor
->selofs
;
3144 ME_RewrapRepaint(editor
);
3147 if (changedSettings
& settings
& ECO_VERTICAL
)
3148 FIXME("ECO_VERTICAL not implemented yet!\n");
3149 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3150 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3151 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3152 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3153 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3154 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3155 if (changedSettings
& settings
& ECO_WANTRETURN
)
3156 FIXME("ECO_WANTRETURN not implemented yet!\n");
3157 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3158 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3165 ME_InvalidateSelection(editor
);
3166 ME_SetSelection(editor
, wParam
, lParam
);
3167 ME_InvalidateSelection(editor
);
3168 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3169 ME_ShowCaret(editor
);
3170 ME_SendSelChange(editor
);
3173 case EM_SETSCROLLPOS
:
3175 POINT
*point
= (POINT
*)lParam
;
3176 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3179 case EM_AUTOURLDETECT
:
3181 if (wParam
==1 || wParam
==0)
3183 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3186 return E_INVALIDARG
;
3188 case EM_GETAUTOURLDETECT
:
3190 return editor
->AutoURLDetect_bEnable
;
3195 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3197 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
3199 ME_InvalidateSelection(editor
);
3200 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
3201 ME_InvalidateSelection(editor
);
3202 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3203 ME_ShowCaret(editor
);
3204 ME_SendSelChange(editor
);
3208 case EM_SHOWSCROLLBAR
:
3221 flags
= WS_HSCROLL
|WS_VSCROLL
;
3228 editor
->styleFlags
|= flags
;
3229 if (flags
& WS_HSCROLL
)
3230 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3231 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3232 if (flags
& WS_VSCROLL
)
3233 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3234 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3236 editor
->styleFlags
&= ~flags
;
3237 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3244 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3248 BOOL bRtf
, bUnicode
, bSelection
;
3249 int oldModify
= editor
->nModifyStep
;
3251 if (!pStruct
) return 0;
3253 /* If we detect ascii rtf at the start of the string,
3254 * we know it isn't unicode. */
3255 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3256 !strncmp((char *)lParam
, "{\\urtf", 6)));
3257 bUnicode
= !bRtf
&& pStruct
->codepage
== 1200;
3259 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3260 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3261 pStruct
->flags
, pStruct
->codepage
);
3263 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3265 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3266 style
= ME_GetSelectionInsertStyle(editor
);
3267 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3270 ME_SetCursorToStart(editor
, &start
);
3271 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3272 style
= editor
->pBuffer
->pDefaultStyle
;
3276 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3278 /* FIXME: The length returned is doesn't include the rtf control
3279 * characters, only the actual text. */
3280 len
= lParam
? strlen((char *)lParam
) : 0;
3283 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3284 wszText
= lParam
? ME_ToUnicode(bUnicode
, (void *)lParam
) : NULL
;
3285 len
= wszText
? lstrlenW(wszText
) : 0;
3286 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3287 ME_EndToUnicode(bUnicode
, wszText
);
3291 ME_ReleaseStyle(style
);
3292 ME_UpdateSelectionLinkAttribute(editor
);
3296 ME_SetCursorToStart(editor
, &cursor
);
3297 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3299 ME_CommitUndo(editor
);
3300 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3302 editor
->nModifyStep
= oldModify
;
3303 ME_EmptyUndoStack(editor
);
3305 ME_UpdateRepaint(editor
);
3308 case EM_SETBKGNDCOLOR
:
3311 if (editor
->rgbBackColor
!= -1) {
3312 DeleteObject(editor
->hbrBackground
);
3313 lColor
= editor
->rgbBackColor
;
3315 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3319 editor
->rgbBackColor
= -1;
3320 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3324 editor
->rgbBackColor
= lParam
;
3325 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3327 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3328 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3332 return editor
->nModifyStep
== 0 ? 0 : -1;
3336 editor
->nModifyStep
= 1;
3338 editor
->nModifyStep
= 0;
3342 case EM_SETREADONLY
:
3345 editor
->styleFlags
|= ES_READONLY
;
3347 editor
->styleFlags
&= ~ES_READONLY
;
3350 case EM_SETEVENTMASK
:
3352 DWORD nOldMask
= editor
->nEventMask
;
3354 editor
->nEventMask
= lParam
;
3357 case EM_GETEVENTMASK
:
3358 return editor
->nEventMask
;
3359 case EM_SETCHARFORMAT
:
3361 CHARFORMAT2W buf
, *p
;
3362 BOOL bRepaint
= TRUE
;
3363 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3364 if (p
== NULL
) return 0;
3366 ME_SetDefaultCharFormat(editor
, p
);
3367 else if (wParam
== (SCF_WORD
| SCF_SELECTION
)) {
3368 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3370 } else if (wParam
== SCF_ALL
) {
3371 if (editor
->mode
& TM_PLAINTEXT
)
3372 ME_SetDefaultCharFormat(editor
, p
);
3375 ME_SetCursorToStart(editor
, &start
);
3376 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3377 editor
->nModifyStep
= 1;
3379 } else if (editor
->mode
& TM_PLAINTEXT
) {
3382 bRepaint
= ME_IsSelection(editor
);
3383 ME_SetSelectionCharFormat(editor
, p
);
3384 if (bRepaint
) editor
->nModifyStep
= 1;
3386 ME_CommitUndo(editor
);
3389 ME_WrapMarkedParagraphs(editor
);
3390 ME_UpdateScrollBar(editor
);
3395 case EM_GETCHARFORMAT
:
3397 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3398 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3399 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3400 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3401 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3403 tmp
.cbSize
= sizeof(tmp
);
3405 ME_GetDefaultCharFormat(editor
, &tmp
);
3407 ME_GetSelectionCharFormat(editor
, &tmp
);
3408 ME_CopyToCFAny(dst
, &tmp
);
3411 case EM_SETPARAFORMAT
:
3413 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3414 ME_WrapMarkedParagraphs(editor
);
3415 ME_UpdateScrollBar(editor
);
3417 ME_CommitUndo(editor
);
3420 case EM_GETPARAFORMAT
:
3421 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3422 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3423 case EM_GETFIRSTVISIBLELINE
:
3425 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3426 int y
= editor
->vert_si
.nPos
;
3431 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3432 if (p
->type
== diTextEnd
)
3434 if (p
->type
== diParagraph
) {
3435 ypara
= p
->member
.para
.pt
.y
;
3438 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3439 yend
= ystart
+ p
->member
.row
.nHeight
;
3447 case EM_HIDESELECTION
:
3449 editor
->bHideSelection
= (wParam
!= 0);
3450 ME_InvalidateSelection(editor
);
3455 if (!(editor
->styleFlags
& ES_MULTILINE
))
3457 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
3463 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3464 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3465 ME_CommitUndo(editor
);
3466 ME_UpdateRepaint(editor
);
3471 int from
, to
, nStartCursor
;
3473 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
3474 size_t len
= wszText
? lstrlenW(wszText
) : 0;
3475 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3477 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3478 style
= ME_GetSelectionInsertStyle(editor
);
3479 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3480 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3481 ME_ReleaseStyle(style
);
3482 /* drop temporary style if line end */
3484 * FIXME question: does abc\n mean: put abc,
3485 * clear temp style, put \n? (would require a change)
3487 if (len
>0 && wszText
[len
-1] == '\n')
3488 ME_ClearTempStyle(editor
);
3489 ME_EndToUnicode(unicode
, wszText
);
3490 ME_CommitUndo(editor
);
3491 ME_UpdateSelectionLinkAttribute(editor
);
3493 ME_EmptyUndoStack(editor
);
3494 ME_UpdateRepaint(editor
);
3497 case EM_SCROLLCARET
:
3498 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3505 BOOL bRepaint
= LOWORD(lParam
);
3509 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3510 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3511 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3512 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3513 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3514 ME_SetCursorToStart(editor
, &start
);
3515 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3516 ME_SetDefaultCharFormat(editor
, &fmt
);
3518 ME_CommitUndo(editor
);
3519 ME_MarkAllForWrapping(editor
);
3520 ME_WrapMarkedParagraphs(editor
);
3521 ME_UpdateScrollBar(editor
);
3529 ME_SetCursorToStart(editor
, &cursor
);
3530 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3533 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3534 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3535 !strncmp((char *)lParam
, "{\\urtf", 6))
3537 /* Undocumented: WM_SETTEXT supports RTF text */
3538 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3542 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
3543 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3544 if (lstrlenW(wszText
) > 0)
3548 /* uses default style! */
3549 if (!(editor
->styleFlags
& ES_MULTILINE
))
3554 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3557 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3559 ME_EndToUnicode(unicode
, wszText
);
3563 TRACE("WM_SETTEXT - NULL\n");
3564 ME_SetCursorToStart(editor
, &cursor
);
3565 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3566 ME_SetSelection(editor
, 0, 0);
3567 editor
->nModifyStep
= 0;
3568 ME_CommitUndo(editor
);
3569 ME_EmptyUndoStack(editor
);
3570 ME_UpdateRepaint(editor
);
3575 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3576 if (IsClipboardFormatAvailable(nRTFFormat
))
3578 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3588 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3589 int nChars
= nTo
- nFrom
;
3590 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3592 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3594 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3595 ME_CommitUndo(editor
);
3596 ME_UpdateRepaint(editor
);
3600 case WM_GETTEXTLENGTH
:
3602 GETTEXTLENGTHEX how
;
3604 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3605 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3606 how
.codepage
= unicode
? 1200 : CP_ACP
;
3607 return ME_GetTextLengthEx(editor
, &how
);
3609 case EM_GETTEXTLENGTHEX
:
3610 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3614 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3615 ex
.flags
= GT_USECRLF
;
3616 ex
.codepage
= unicode
? 1200 : CP_ACP
;
3617 ex
.lpDefaultChar
= NULL
;
3618 ex
.lpUsedDefChar
= NULL
;
3619 return ME_GetTextEx(editor
, &ex
, lParam
);
3622 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3625 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3626 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3627 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3628 nTo
- nFrom
, unicode
);
3630 case EM_GETSCROLLPOS
:
3632 POINT
*point
= (POINT
*)lParam
;
3633 point
->x
= editor
->horz_si
.nPos
;
3634 point
->y
= editor
->vert_si
.nPos
;
3635 /* 16-bit scaled value is returned as stored in scrollinfo */
3636 if (editor
->horz_si
.nMax
> 0xffff)
3637 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3638 if (editor
->vert_si
.nMax
> 0xffff)
3639 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3642 case EM_GETTEXTRANGE
:
3644 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3646 int nStart
= rng
->chrg
.cpMin
;
3647 int nEnd
= rng
->chrg
.cpMax
;
3648 int textlength
= ME_GetTextLength(editor
);
3650 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3651 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3652 if (nStart
< 0) return 0;
3653 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3655 if (nStart
>= nEnd
) return 0;
3657 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3658 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3662 ME_DisplayItem
*run
;
3663 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3664 unsigned int nCharsLeft
= nMaxChars
;
3665 char *dest
= (char *) lParam
;
3666 BOOL wroteNull
= FALSE
;
3668 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3669 unicode
? "Unicode" : "Ansi");
3671 run
= ME_FindRowWithNumber(editor
, wParam
);
3675 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3676 && run
->type
== diRun
)
3681 strText
= run
->member
.run
.strText
;
3682 nCopy
= min(nCharsLeft
, strText
->nLen
);
3685 memcpy(dest
, strText
->szData
, nCopy
* sizeof(WCHAR
));
3687 nCopy
= WideCharToMultiByte(CP_ACP
, 0, strText
->szData
, nCopy
, dest
,
3688 nCharsLeft
, NULL
, NULL
);
3689 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3690 nCharsLeft
-= nCopy
;
3693 /* append line termination, space allowing */
3697 *((WCHAR
*)dest
) = '\0';
3704 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3705 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3707 case EM_GETLINECOUNT
:
3709 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3712 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3714 while (item
!= editor
->pBuffer
->pLast
)
3716 assert(item
->type
== diParagraph
);
3717 prev_para
= ME_FindItemBack(item
, diRun
);
3719 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3721 nRows
+= item
->member
.para
.nRows
;
3722 item
= item
->member
.para
.next_para
;
3724 last_para
= ME_FindItemBack(item
, diRun
);
3726 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3727 if (editor
->bEmulateVersion10
&& prev_para
&&
3728 last_para
->member
.run
.nCharOfs
== 0 &&
3729 prev_para
->member
.run
.strText
->nLen
== 1 &&
3730 prev_para
->member
.run
.strText
->szData
[0] == '\r')
3732 /* In 1.0 emulation, the last solitary \r at the very end of the text
3733 (if one exists) is NOT a line break.
3734 FIXME: this is an ugly hack. This should have a more regular model. */
3738 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3739 return max(1, nRows
);
3741 case EM_LINEFROMCHAR
:
3744 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3746 return ME_RowNumberFromCharOfs(editor
, wParam
);
3748 case EM_EXLINEFROMCHAR
:
3751 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3753 return ME_RowNumberFromCharOfs(editor
, lParam
);
3757 ME_DisplayItem
*item
, *para
;
3761 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3763 item
= ME_FindRowWithNumber(editor
, wParam
);
3766 para
= ME_GetParagraph(item
);
3767 item
= ME_FindItemFwd(item
, diRun
);
3768 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3769 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3774 ME_DisplayItem
*item
, *item_end
;
3775 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3776 ME_DisplayItem
*para
, *run
;
3778 if (wParam
> ME_GetTextLength(editor
))
3782 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3785 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3786 item
= ME_RowStart(run
);
3787 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3788 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3789 if (item_end
->type
== diStartRow
) {
3790 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3792 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3793 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3794 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.strText
->nLen
;
3796 nChars
= nNextLineOfs
- nThisLineOfs
;
3797 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3800 case EM_EXLIMITTEXT
:
3802 if ((int)lParam
< 0)
3805 editor
->nTextLimit
= 65536;
3807 editor
->nTextLimit
= (int) lParam
;
3813 editor
->nTextLimit
= 65536;
3815 editor
->nTextLimit
= (int) wParam
;
3818 case EM_GETLIMITTEXT
:
3820 return editor
->nTextLimit
;
3824 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3825 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3829 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3830 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3831 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3837 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3838 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3842 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3843 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3844 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3850 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3851 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3853 case EM_FINDTEXTEXW
:
3855 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3856 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3859 if (!wParam
|| !lParam
)
3861 *(int *)wParam
= editor
->nZoomNumerator
;
3862 *(int *)lParam
= editor
->nZoomDenominator
;
3865 return ME_SetZoom(editor
, wParam
, lParam
);
3866 case EM_CHARFROMPOS
:
3869 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
3871 return ME_GetCursorOfs(&cursor
);
3875 case EM_POSFROMCHAR
:
3877 ME_DisplayItem
*pPara
, *pRun
;
3878 int nCharOfs
, nOffset
, nLength
;
3882 /* detect which API version we're dealing with */
3883 if (wParam
>= 0x40000)
3885 nLength
= ME_GetTextLength(editor
);
3886 nCharOfs
= min(nCharOfs
, nLength
);
3887 nCharOfs
= max(nCharOfs
, 0);
3889 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
3890 assert(pRun
->type
== diRun
);
3891 pt
.y
= pRun
->member
.run
.pt
.y
;
3892 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
);
3893 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
3894 pt
.x
+= editor
->rcFormat
.left
;
3896 pt
.x
-= editor
->horz_si
.nPos
;
3897 pt
.y
-= editor
->vert_si
.nPos
;
3899 if (wParam
>= 0x40000) {
3900 *(POINTL
*)wParam
= pt
;
3902 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
3908 ME_SetDefaultFormatRect(editor
);
3910 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
3911 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
3912 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
3914 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
3915 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
3917 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3919 if (editor
->styleFlags
& WS_VSCROLL
)
3921 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
3922 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
3924 if (editor
->styleFlags
& WS_HSCROLL
)
3926 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
3927 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
3931 ME_CommitUndo(editor
);
3932 ME_WrapMarkedParagraphs(editor
);
3933 ME_MoveCaret(editor
);
3937 ME_DestroyEditor(editor
);
3941 return ME_SetCursor(editor
);
3943 case WM_LBUTTONDBLCLK
:
3944 case WM_LBUTTONDOWN
:
3946 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3947 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3948 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3950 ITextHost_TxSetFocus(editor
->texthost
);
3951 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
3952 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
3953 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
3954 editor
->bMouseCaptured
= TRUE
;
3955 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3956 if (!ME_SetCursor(editor
)) goto do_default
;
3960 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3961 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3963 if (editor
->bMouseCaptured
)
3964 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
3965 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3966 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3967 if (editor
->bMouseCaptured
)
3968 ME_SetCursor(editor
);
3971 if (editor
->bMouseCaptured
) {
3972 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
3973 editor
->bMouseCaptured
= FALSE
;
3975 if (editor
->nSelectionType
== stDocument
)
3976 editor
->nSelectionType
= stPosition
;
3977 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3978 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3982 ME_SetCursor(editor
);
3983 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3987 case WM_RBUTTONDOWN
:
3988 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3989 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3990 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3993 case WM_CONTEXTMENU
:
3994 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
3998 editor
->bHaveFocus
= TRUE
;
3999 ME_ShowCaret(editor
);
4000 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4003 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4004 editor
->bHaveFocus
= FALSE
;
4005 ME_HideCaret(editor
);
4006 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4009 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4012 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4013 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4017 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4018 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4020 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4024 return ME_Char(editor
, wParam
, lParam
, unicode
);
4028 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4029 if(wParam
<= 0x000fffff)
4031 if(wParam
> 0xffff) /* convert to surrogates */
4034 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4035 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4037 ME_Char(editor
, wParam
, 0, TRUE
);
4043 case EM_STOPGROUPTYPING
:
4044 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4048 const int scrollUnit
= 7;
4050 switch(LOWORD(wParam
))
4053 ME_ScrollAbs(editor
, 0, 0);
4056 ME_ScrollAbs(editor
,
4057 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4058 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4061 ME_ScrollLeft(editor
, scrollUnit
);
4064 ME_ScrollRight(editor
, scrollUnit
);
4067 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4070 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4073 case SB_THUMBPOSITION
:
4075 int pos
= HIWORD(wParam
);
4076 if (editor
->horz_si
.nMax
> 0xffff)
4077 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4078 ME_HScrollAbs(editor
, pos
);
4084 case EM_SCROLL
: /* fall through */
4090 origNPos
= editor
->vert_si
.nPos
;
4093 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
4094 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
4095 if (lineHeight
<= 0) lineHeight
= 24;
4097 switch(LOWORD(wParam
))
4100 ME_ScrollAbs(editor
, 0, 0);
4103 ME_ScrollAbs(editor
,
4104 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4105 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4108 ME_ScrollUp(editor
,lineHeight
);
4111 ME_ScrollDown(editor
,lineHeight
);
4114 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4117 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4120 case SB_THUMBPOSITION
:
4122 int pos
= HIWORD(wParam
);
4123 if (editor
->vert_si
.nMax
> 0xffff)
4124 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4125 ME_VScrollAbs(editor
, pos
);
4129 if (msg
== EM_SCROLL
)
4130 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4136 UINT pulScrollLines
;
4139 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4140 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4143 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4145 gcWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4147 if (abs(gcWheelDelta
) >= WHEEL_DELTA
)
4151 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4155 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4157 numerator
= numerator
+ (gcWheelDelta
/ WHEEL_DELTA
) * 10;
4158 if (numerator
>= 10 && numerator
<= 500)
4159 ME_SetZoom(editor
, numerator
, 100);
4161 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
4162 /* FIXME follow the original */
4164 ME_ScrollDown(editor
,pulScrollLines
* (-gcWheelDelta
/ WHEEL_DELTA
) * 8);
4171 *((RECT
*)lParam
) = editor
->rcFormat
;
4172 if (editor
->bDefaultFormatRect
)
4173 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4183 RECT
*rc
= (RECT
*)lParam
;
4185 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4186 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4189 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4190 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4191 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4192 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4193 } else if (wParam
== 1) {
4194 /* MSDN incorrectly says a wParam value of 1 causes the
4195 * lParam rect to be used as a relative offset,
4196 * however, the tests show it just prevents min/max bound
4198 editor
->rcFormat
.top
= rc
->top
- border
;
4199 editor
->rcFormat
.left
= rc
->left
- border
;
4200 editor
->rcFormat
.bottom
= rc
->bottom
;
4201 editor
->rcFormat
.right
= rc
->right
+ border
;
4205 editor
->bDefaultFormatRect
= FALSE
;
4209 ME_SetDefaultFormatRect(editor
);
4210 editor
->bDefaultFormatRect
= TRUE
;
4212 ME_MarkAllForWrapping(editor
);
4213 ME_WrapMarkedParagraphs(editor
);
4214 ME_UpdateScrollBar(editor
);
4215 if (msg
!= EM_SETRECTNP
)
4219 case EM_REQUESTRESIZE
:
4220 ME_SendRequestResize(editor
, TRUE
);
4228 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4229 if (editor
->bDefaultFormatRect
) {
4230 ME_SetDefaultFormatRect(editor
);
4232 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4233 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4235 editor
->prevClientRect
= clientRect
;
4236 ME_RewrapRepaint(editor
);
4239 /* IME messages to make richedit controls IME aware */
4240 case WM_IME_SETCONTEXT
:
4241 case WM_IME_CONTROL
:
4243 case WM_IME_COMPOSITIONFULL
:
4245 case WM_IME_STARTCOMPOSITION
:
4247 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4248 ME_DeleteSelection(editor
);
4249 ME_CommitUndo(editor
);
4250 ME_UpdateRepaint(editor
);
4253 case WM_IME_COMPOSITION
:
4257 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4258 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4259 ME_DeleteSelection(editor
);
4260 ME_CommitUndo(editor
);
4261 ME_SaveTempStyle(editor
);
4262 if (lParam
& GCS_RESULTSTR
)
4264 LPWSTR lpCompStr
= NULL
;
4267 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4268 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4269 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpCompStr
, dwBufLen
);
4270 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4271 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4273 else if (lParam
& GCS_COMPSTR
)
4275 LPWSTR lpCompStr
= NULL
;
4278 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4279 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4280 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, dwBufLen
);
4281 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4283 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4284 ME_SetSelection(editor
,editor
->imeStartIndex
,
4285 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4287 ME_ReleaseStyle(style
);
4288 ME_UpdateRepaint(editor
);
4291 case WM_IME_ENDCOMPOSITION
:
4293 ME_DeleteSelection(editor
);
4294 editor
->imeStartIndex
=-1;
4297 case EM_GETOLEINTERFACE
:
4299 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4300 return CreateIRichEditOle(editor
, ppvObj
);
4302 case EM_GETPASSWORDCHAR
:
4304 return editor
->cPasswordMask
;
4306 case EM_SETOLECALLBACK
:
4307 if(editor
->lpOleCallback
)
4308 IUnknown_Release(editor
->lpOleCallback
);
4309 editor
->lpOleCallback
= (LPRICHEDITOLECALLBACK
)lParam
;
4310 if(editor
->lpOleCallback
)
4311 IUnknown_AddRef(editor
->lpOleCallback
);
4313 case EM_GETWORDBREAKPROC
:
4314 return (LRESULT
)editor
->pfnWordBreak
;
4315 case EM_SETWORDBREAKPROC
:
4317 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4319 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4320 return (LRESULT
)pfnOld
;
4322 case EM_GETTEXTMODE
:
4323 return editor
->mode
;
4324 case EM_SETTEXTMODE
:
4329 GETTEXTLENGTHEX how
;
4331 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4332 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
4333 how
.codepage
= unicode
? 1200 : CP_ACP
;
4334 ret
= ME_GetTextLengthEx(editor
, &how
);
4337 /*Check for valid wParam*/
4338 if ((((wParam
& TM_RICHTEXT
) && ((wParam
& TM_PLAINTEXT
) << 1))) ||
4339 (((wParam
& TM_MULTILEVELUNDO
) && ((wParam
& TM_SINGLELEVELUNDO
) << 1))) ||
4340 (((wParam
& TM_MULTICODEPAGE
) && ((wParam
& TM_SINGLECODEPAGE
) << 1))))
4344 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4346 mask
|= (TM_RICHTEXT
| TM_PLAINTEXT
);
4347 changes
|= (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
));
4349 /*FIXME: Currently no support for undo level and code page options*/
4350 editor
->mode
= (editor
->mode
& (~mask
)) | changes
;
4356 case EM_SETPASSWORDCHAR
:
4358 editor
->cPasswordMask
= wParam
;
4359 ME_RewrapRepaint(editor
);
4362 case EM_SETTARGETDEVICE
:
4365 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4366 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4368 editor
->bWordWrap
= new;
4369 editor
->nAvailWidth
= 0; /* wrap to client area */
4370 ME_RewrapRepaint(editor
);
4373 int width
= max(0, lParam
);
4374 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4375 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4377 editor
->nAvailWidth
= width
;
4378 editor
->bWordWrap
= TRUE
;
4379 ME_RewrapRepaint(editor
);
4381 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4386 *phresult
= S_FALSE
;
4392 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4393 LPARAM lParam
, BOOL unicode
)
4395 ME_TextEditor
*editor
;
4397 LRESULT lresult
= 0;
4399 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4400 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4402 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4405 if (msg
== WM_NCCREATE
)
4407 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4408 ITextHost
*texthost
;
4410 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4411 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4412 return texthost
!= NULL
;
4414 else if (msg
!= WM_NCDESTROY
)
4416 ERR("called with invalid hWnd %p - application bug?\n", hWnd
);
4429 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4430 /* Erase area outside of the formatting rectangle */
4431 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4434 rc
.bottom
= editor
->rcFormat
.top
;
4435 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4436 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4438 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4440 rc
.top
= editor
->rcFormat
.bottom
;
4441 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4442 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4444 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4446 rc
.right
= editor
->rcFormat
.left
;
4447 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4448 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4450 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4452 rc
.left
= editor
->rcFormat
.right
;
4453 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4454 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4457 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
4458 EndPaint(editor
->hWnd
, &ps
);
4463 HDC hDC
= (HDC
)wParam
;
4466 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4467 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4473 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4474 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4476 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4477 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4478 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4479 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4482 case EM_SETREADONLY
:
4485 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4486 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4487 dwStyle
&= ~ES_READONLY
;
4489 dwStyle
|= ES_READONLY
;
4490 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4494 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4497 if (hresult
== S_FALSE
)
4498 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4500 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4501 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4506 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4508 BOOL unicode
= TRUE
;
4510 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4511 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4514 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4517 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4519 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4522 /******************************************************************
4523 * RichEditANSIWndProc (RICHED20.10)
4525 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4527 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4530 /******************************************************************
4531 * RichEdit10ANSIWndProc (RICHED20.9)
4533 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4535 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4537 ITextHost
*texthost
;
4538 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4540 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4541 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4542 return texthost
!= NULL
;
4544 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4547 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4549 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4552 /* Fill buffer with srcChars unicode characters from the start cursor.
4554 * buffer: destination buffer
4555 * buflen: length of buffer in characters excluding the NULL terminator.
4556 * start: start of editor text to copy into buffer.
4557 * srcChars: Number of characters to use from the editor text.
4558 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4560 * returns the number of characters written excluding the NULL terminator.
4562 * The written text is always NULL terminated.
4564 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4565 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
)
4567 ME_DisplayItem
*pRun
, *pNextRun
;
4568 const WCHAR
*pStart
= buffer
;
4569 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4573 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4574 if (editor
->bEmulateVersion10
) bCRLF
= 0;
4578 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4580 nLen
= pRun
->member
.run
.strText
->nLen
- start
->nOffset
;
4581 str
= pRun
->member
.run
.strText
->szData
+ start
->nOffset
;
4583 /* No '\r' is appended to the last paragraph. */
4584 while (srcChars
&& buflen
&& pNextRun
)
4586 int nFlags
= pRun
->member
.run
.nFlags
;
4588 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4590 if (buflen
== 1) break;
4591 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4592 * EM_GETTEXTEX, however, this is done for copying text which
4593 * also uses this function. */
4594 srcChars
-= min(nLen
, srcChars
);
4598 nLen
= min(nLen
, srcChars
);
4602 nLen
= min(nLen
, buflen
);
4605 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4610 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4612 nLen
= pRun
->member
.run
.strText
->nLen
;
4613 str
= pRun
->member
.run
.strText
->szData
;
4616 return buffer
- pStart
;
4619 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4624 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4625 wcW
.lpfnWndProc
= RichEditWndProcW
;
4627 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4628 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4630 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4631 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4632 wcW
.lpszMenuName
= NULL
;
4634 if (is_version_nt())
4636 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4637 if (!RegisterClassW(&wcW
)) return FALSE
;
4638 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4639 if (!RegisterClassW(&wcW
)) return FALSE
;
4643 /* WNDCLASSA/W have the same layout */
4644 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4645 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4646 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4647 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4650 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4651 wcA
.lpfnWndProc
= RichEditWndProcA
;
4653 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4654 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4656 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4657 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4658 wcA
.lpszMenuName
= NULL
;
4659 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4660 if (!RegisterClassA(&wcA
)) return FALSE
;
4661 wcA
.lpszClassName
= "RichEdit50A";
4662 if (!RegisterClassA(&wcA
)) return FALSE
;
4667 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4668 /* FIXME: Not implemented */
4669 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4670 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4671 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4674 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4675 /* FIXME: Not implemented */
4676 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4677 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4678 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4681 /******************************************************************
4682 * REExtendedRegisterClass (RICHED20.8)
4684 * FIXME undocumented
4685 * Need to check for errors and implement controls and callbacks
4687 LRESULT WINAPI
REExtendedRegisterClass(void)
4692 FIXME("semi stub\n");
4696 wcW
.hInstance
= NULL
;
4699 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4700 wcW
.lpszMenuName
= NULL
;
4702 if (!ME_ListBoxRegistered
)
4704 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4705 wcW
.lpfnWndProc
= REListWndProc
;
4706 wcW
.lpszClassName
= REListBox20W
;
4707 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4710 if (!ME_ComboBoxRegistered
)
4712 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4713 wcW
.lpfnWndProc
= REComboWndProc
;
4714 wcW
.lpszClassName
= REComboBox20W
;
4715 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4719 if (ME_ListBoxRegistered
)
4721 if (ME_ComboBoxRegistered
)
4727 static BOOL
isurlspecial(WCHAR c
)
4729 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4730 return strchrW( special_chars
, c
) != NULL
;
4734 * This proc takes a selection, and scans it forward in order to select the span
4735 * of a possible URL candidate. A possible URL candidate must start with isalnum
4736 * or one of the following special characters: *|/\+%#@ and must consist entirely
4737 * of the characters allowed to start the URL, plus : (colon) which may occur
4738 * at most once, and not at either end.
4740 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
4741 const ME_Cursor
*start
,
4743 ME_Cursor
*candidate_min
,
4744 ME_Cursor
*candidate_max
)
4746 ME_Cursor cursor
= *start
;
4747 BOOL foundColon
= FALSE
;
4748 BOOL candidateStarted
= FALSE
;
4749 WCHAR lastAcceptedChar
= '\0';
4753 WCHAR
*strStart
= cursor
.pRun
->member
.run
.strText
->szData
;
4754 WCHAR
*str
= strStart
+ cursor
.nOffset
;
4755 int nLen
= cursor
.pRun
->member
.run
.strText
->nLen
- cursor
.nOffset
;
4758 if (~cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4760 /* Find start of candidate */
4761 if (!candidateStarted
)
4766 if (isalnumW(*str
) || isurlspecial(*str
))
4768 cursor
.nOffset
= str
- strStart
;
4769 *candidate_min
= cursor
;
4770 candidateStarted
= TRUE
;
4771 lastAcceptedChar
= *str
++;
4778 /* Find end of candidate */
4779 if (candidateStarted
) {
4783 if (*str
== ':' && !foundColon
) {
4785 } else if (!isalnumW(*str
) && !isurlspecial(*str
)) {
4786 cursor
.nOffset
= str
- strStart
;
4787 if (lastAcceptedChar
== ':')
4788 ME_MoveCursorChars(editor
, &cursor
, -1);
4789 *candidate_max
= cursor
;
4792 lastAcceptedChar
= *str
++;
4796 /* End of paragraph: skip it if before candidate span, or terminates
4797 current active span */
4798 if (candidateStarted
) {
4799 if (lastAcceptedChar
== ':')
4800 ME_MoveCursorChars(editor
, &cursor
, -1);
4801 *candidate_max
= cursor
;
4806 /* Reaching this point means no span was found, so get next span */
4807 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
)) {
4808 if (candidateStarted
) {
4809 /* There are no further runs, so take end of text as end of candidate */
4810 cursor
.nOffset
= str
- strStart
;
4811 if (lastAcceptedChar
== ':')
4812 ME_MoveCursorChars(editor
, &cursor
, -1);
4813 *candidate_max
= cursor
;
4816 *candidate_max
= *candidate_min
= cursor
;
4822 if (candidateStarted
) {
4823 /* There are no further runs, so take end of text as end of candidate */
4824 if (lastAcceptedChar
== ':')
4825 ME_MoveCursorChars(editor
, &cursor
, -1);
4826 *candidate_max
= cursor
;
4829 *candidate_max
= *candidate_min
= cursor
;
4834 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4836 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
4838 #define MAX_PREFIX_LEN 9
4840 const WCHAR text
[MAX_PREFIX_LEN
];
4843 {{'p','r','o','s','p','e','r','o',':'}, 9},
4844 {{'t','e','l','n','e','t',':'}, 7},
4845 {{'g','o','p','h','e','r',':'}, 7},
4846 {{'m','a','i','l','t','o',':'}, 7},
4847 {{'h','t','t','p','s',':'}, 6},
4848 {{'f','i','l','e',':'}, 5},
4849 {{'n','e','w','s',':'}, 5},
4850 {{'w','a','i','s',':'}, 5},
4851 {{'n','n','t','p',':'}, 5},
4852 {{'h','t','t','p',':'}, 5},
4853 {{'w','w','w','.'}, 4},
4854 {{'f','t','p',':'}, 4},
4856 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
4859 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, 0);
4860 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
4862 if (nChars
< prefixes
[i
].length
) continue;
4863 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
4867 #undef MAX_PREFIX_LEN
4871 * This proc walks through the indicated selection and evaluates whether each
4872 * section identified by ME_FindNextURLCandidate and in-between sections have
4873 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4874 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4876 * Since this function can cause runs to be split, do not depend on the value
4877 * of the start cursor at the end of the function.
4879 * nChars may be set to INT_MAX to update to the end of the text.
4881 * Returns TRUE if at least one section was modified.
4883 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
4885 BOOL modified
= FALSE
;
4886 ME_Cursor startCur
= *start
;
4888 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
4893 ME_Cursor candidateStart
, candidateEnd
;
4895 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
4896 &candidateStart
, &candidateEnd
))
4898 /* Section before candidate is not an URL */
4899 int cMin
= ME_GetCursorOfs(&candidateStart
);
4900 int cMax
= ME_GetCursorOfs(&candidateEnd
);
4902 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
4903 candidateStart
= candidateEnd
;
4904 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
4908 /* No more candidates until end of selection */
4912 if (startCur
.pRun
!= candidateStart
.pRun
||
4913 startCur
.nOffset
!= candidateStart
.nOffset
)
4915 /* CFE_LINK effect should be consistently unset */
4916 link
.cbSize
= sizeof(link
);
4917 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
4918 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
4920 /* CFE_LINK must be unset from this range */
4921 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4922 link
.cbSize
= sizeof(link
);
4923 link
.dwMask
= CFM_LINK
;
4925 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
4926 /* Update candidateEnd since setting character formats may split
4927 * runs, which can cause a cursor to be at an invalid offset within
4929 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.strText
->nLen
)
4931 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.strText
->nLen
;
4932 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
4937 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
4938 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
4940 /* CFE_LINK effect should be consistently set */
4941 link
.cbSize
= sizeof(link
);
4942 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
4943 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
4945 /* CFE_LINK must be set on this range */
4946 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4947 link
.cbSize
= sizeof(link
);
4948 link
.dwMask
= CFM_LINK
;
4949 link
.dwEffects
= CFE_LINK
;
4950 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
4954 startCur
= candidateEnd
;
4955 } while (nChars
> 0);