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
145 - WM_STYLECHANGED (things like read-only flag)
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
, int sel_min
, int sel_max
);
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
, nOfs
, nChars
, TRUE
);
1021 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1022 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1023 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1024 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1026 info
->nestingLevel
--;
1027 if (!info
->nestingLevel
)
1029 if (info
->canInheritInTbl
) {
1030 tableDef
->tableRowStart
= para
;
1032 while (info
->tableDef
) {
1033 tableDef
= info
->tableDef
;
1034 info
->tableDef
= tableDef
->parent
;
1035 heap_free(tableDef
);
1039 info
->tableDef
= tableDef
->parent
;
1040 heap_free(tableDef
);
1042 } else { /* v1.0 - v3.0 */
1044 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1045 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1046 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1047 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1049 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1051 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1054 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1055 tableDef
->numCellsInserted
++;
1057 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1058 if (!tableDef
->numCellsDefined
)
1059 pFmt
->wEffects
&= ~PFE_TABLE
;
1060 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1061 tableDef
->numCellsInserted
= 0;
1067 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1068 ME_DisplayItem
*para
;
1070 RTFFlushOutputBuffer(info
);
1071 para
= info
->editor
->pCursors
[0].pPara
;
1072 pFmt
= para
->member
.para
.pFmt
;
1073 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1075 /* rtfPar is treated like a space within a table. */
1076 info
->rtfClass
= rtfText
;
1077 info
->rtfMajor
= ' ';
1079 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1080 tableDef
->numCellsInserted
= 0;
1086 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1089 LPOLEOBJECT lpObject
= NULL
;
1090 LPSTORAGE lpStorage
= NULL
;
1091 LPOLECLIENTSITE lpClientSite
= NULL
;
1092 LPDATAOBJECT lpDataObject
= NULL
;
1093 LPOLECACHE lpOleCache
= NULL
;
1102 stgm
.tymed
= TYMED_ENHMF
;
1103 stgm
.u
.hEnhMetaFile
= hemf
;
1104 fm
.cfFormat
= CF_ENHMETAFILE
;
1108 stgm
.tymed
= TYMED_GDI
;
1109 stgm
.u
.hBitmap
= hbmp
;
1110 fm
.cfFormat
= CF_BITMAP
;
1112 stgm
.pUnkForRelease
= NULL
;
1115 fm
.dwAspect
= DVASPECT_CONTENT
;
1117 fm
.tymed
= stgm
.tymed
;
1119 if (!info
->lpRichEditOle
)
1121 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1124 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1126 /* FIXME: enable it when rich-edit properly implements this method */
1127 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1128 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1130 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1131 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1132 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1133 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1134 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1138 reobject
.cbStruct
= sizeof(reobject
);
1139 reobject
.cp
= REO_CP_SELECTION
;
1140 reobject
.clsid
= clsid
;
1141 reobject
.poleobj
= lpObject
;
1142 reobject
.pstg
= lpStorage
;
1143 reobject
.polesite
= lpClientSite
;
1144 /* convert from twips to .01 mm */
1145 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1146 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1147 reobject
.dvaspect
= DVASPECT_CONTENT
;
1148 reobject
.dwFlags
= 0; /* FIXME */
1149 reobject
.dwUser
= 0;
1151 /* FIXME: could be simpler */
1152 ret
= IRichEditOle_InsertObject(info
->lpRichEditOle
, &reobject
) == S_OK
;
1155 if (lpObject
) IOleObject_Release(lpObject
);
1156 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1157 if (lpStorage
) IStorage_Release(lpStorage
);
1158 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1159 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1164 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1167 BYTE
* buffer
= NULL
;
1168 unsigned bufsz
, bufidx
;
1174 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1177 if (info
->rtfClass
== rtfEOF
)
1180 /* fetch picture type */
1181 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1183 mfp
.mm
= info
->rtfParam
;
1186 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1188 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1191 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1193 gfx
= gfx_enhmetafile
;
1197 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1201 /* fetch picture attributes */
1205 if (info
->rtfClass
== rtfEOF
)
1207 if (info
->rtfClass
== rtfText
)
1209 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1211 ERR("Expected picture attribute (%d %d)\n",
1212 info
->rtfClass
, info
->rtfMajor
);
1215 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1217 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1219 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1221 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1223 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1224 sz
.cx
= info
->rtfParam
;
1225 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1226 sz
.cy
= info
->rtfParam
;
1228 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1230 /* fetch picture data */
1233 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1234 val
= info
->rtfMajor
;
1235 for (flip
= TRUE
;; flip
= !flip
)
1238 if (info
->rtfClass
== rtfEOF
)
1240 HeapFree(GetProcessHeap(), 0, buffer
);
1241 return; /* Warn ?? */
1243 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1245 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1248 if (bufidx
>= bufsz
&&
1249 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1251 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1254 val
= info
->rtfMajor
;
1256 if (flip
) FIXME("wrong hex string\n");
1260 case gfx_enhmetafile
:
1261 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1262 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1265 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1266 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1270 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1272 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1274 /* not quite right, especially for bitfields type of compression */
1275 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1276 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1277 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1278 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1279 bi
, DIB_RGB_COLORS
)))
1280 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1287 HeapFree(GetProcessHeap(), 0, buffer
);
1288 RTFRouteToken (info
); /* feed "}" back to router */
1291 HeapFree(GetProcessHeap(), 0, buffer
);
1293 RTFRouteToken(info
); /* feed "}" back to router */
1296 /* for now, lookup the \result part and use it, whatever the object */
1297 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1302 if (info
->rtfClass
== rtfEOF
)
1304 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1306 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1309 if (info
->rtfClass
== rtfEOF
)
1311 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1315 while (RTFGetToken (info
) != rtfEOF
)
1317 if (info
->rtfClass
== rtfGroup
)
1319 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1320 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1322 RTFRouteToken(info
);
1325 else RTFSkipGroup(info
);
1328 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1330 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1334 RTFRouteToken(info
); /* feed "}" back to router */
1337 static void ME_RTFReadHook(RTF_Info
*info
)
1339 switch(info
->rtfClass
)
1342 switch(info
->rtfMajor
)
1345 if (info
->stackTop
< maxStack
) {
1346 info
->stack
[info
->stackTop
].style
= info
->style
;
1347 ME_AddRefStyle(info
->style
);
1348 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1349 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1352 info
->styleChanged
= FALSE
;
1356 RTFFlushOutputBuffer(info
);
1358 if (info
->stackTop
<= 0)
1359 info
->rtfClass
= rtfEOF
;
1360 if (info
->stackTop
< 0)
1363 ME_ReleaseStyle(info
->style
);
1364 info
->style
= info
->stack
[info
->stackTop
].style
;
1365 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1366 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1375 ME_StreamInFill(ME_InStream
*stream
)
1377 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1378 (BYTE
*)stream
->buffer
,
1379 sizeof(stream
->buffer
),
1380 (LONG
*)&stream
->dwSize
);
1384 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1388 int from
, to
, to2
, nUndoMode
;
1389 int nEventMask
= editor
->nEventMask
;
1390 ME_InStream inStream
;
1391 BOOL invalidRTF
= FALSE
;
1393 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1394 editor
->nEventMask
= 0;
1396 ME_GetSelection(editor
, &from
, &to
);
1397 if ((format
& SFF_SELECTION
) && (editor
->mode
& TM_RICHTEXT
)) {
1398 style
= ME_GetSelectionInsertStyle(editor
);
1400 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
1402 /* Don't insert text at the end of the table row */
1403 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1404 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1405 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1407 para
= para
->member
.para
.next_para
;
1408 editor
->pCursors
[0].pPara
= para
;
1409 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1410 editor
->pCursors
[0].nOffset
= 0;
1412 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1414 para
= para
->member
.para
.next_para
;
1415 editor
->pCursors
[0].pPara
= para
;
1416 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1417 editor
->pCursors
[0].nOffset
= 0;
1419 editor
->pCursors
[1] = editor
->pCursors
[0];
1420 } else { /* v1.0 - 3.0 */
1421 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1422 ME_IsInTable(editor
->pCursors
[0].pRun
))
1427 style
= editor
->pBuffer
->pDefaultStyle
;
1428 ME_AddRefStyle(style
);
1429 ME_SetSelection(editor
, 0, 0);
1430 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
1432 ME_ClearTempStyle(editor
);
1433 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1437 /* Back up undo mode to a local variable */
1438 nUndoMode
= editor
->nUndoMode
;
1440 /* Only create an undo if SFF_SELECTION is set */
1441 if (!(format
& SFF_SELECTION
))
1442 editor
->nUndoMode
= umIgnore
;
1444 inStream
.editstream
= stream
;
1445 inStream
.editstream
->dwError
= 0;
1446 inStream
.dwSize
= 0;
1447 inStream
.dwUsed
= 0;
1449 if (format
& SF_RTF
)
1451 /* Check if it's really RTF, and if it is not, use plain text */
1452 ME_StreamInFill(&inStream
);
1453 if (!inStream
.editstream
->dwError
)
1455 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1456 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1459 inStream
.editstream
->dwError
= -16;
1464 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1466 if (format
& SF_RTF
) {
1467 /* setup the RTF parser */
1468 memset(&parser
, 0, sizeof parser
);
1469 RTFSetEditStream(&parser
, &inStream
);
1470 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1471 parser
.editor
= editor
;
1472 parser
.style
= style
;
1473 WriterInit(&parser
);
1475 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1476 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1477 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1478 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1480 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1481 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1485 /* do the parsing */
1487 RTFFlushOutputBuffer(&parser
);
1488 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1489 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1490 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1492 /* Delete any incomplete table row at the end of the rich text. */
1494 ME_DisplayItem
*pCell
;
1495 ME_DisplayItem
*para
;
1497 parser
.rtfMinor
= rtfRow
;
1498 /* Complete the table row before deleting it.
1499 * By doing it this way we will have the current paragraph format set
1500 * properly to reflect that is not in the complete table, and undo items
1501 * will be added for this change to the current paragraph format. */
1502 if (parser
.nestingLevel
> 0)
1504 while (parser
.nestingLevel
> 1)
1505 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1506 para
= parser
.tableDef
->tableRowStart
;
1507 ME_RTFSpecialCharHook(&parser
);
1509 para
= parser
.tableDef
->tableRowStart
;
1510 ME_RTFSpecialCharHook(&parser
);
1511 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1512 para
= para
->member
.para
.next_para
;
1514 pCell
= para
->member
.para
.pCell
;
1516 editor
->pCursors
[1].pPara
= para
;
1517 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1518 editor
->pCursors
[1].nOffset
= 0;
1519 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1520 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1521 ME_InternalDeleteText(editor
, nOfs
, nChars
, TRUE
);
1522 if (parser
.tableDef
)
1523 parser
.tableDef
->tableRowStart
= NULL
;
1526 ME_CheckTablesForCorruption(editor
);
1527 RTFDestroy(&parser
);
1528 if (parser
.lpRichEditOle
)
1529 IRichEditOle_Release(parser
.lpRichEditOle
);
1531 if (parser
.stackTop
> 0)
1533 while (--parser
.stackTop
>= 0)
1535 ME_ReleaseStyle(parser
.style
);
1536 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1538 if (!inStream
.editstream
->dwError
)
1539 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1542 /* Remove last line break, as mandated by tests. This is not affected by
1543 CR/LF counters, since RTF streaming presents only \para tokens, which
1544 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1548 ME_GetSelection(editor
, &newfrom
, &newto
);
1549 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1550 WCHAR lastchar
[3] = {'\0', '\0'};
1551 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1553 ME_GetTextW(editor
, lastchar
, newto
- linebreakSize
, linebreakSize
, 0);
1554 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1555 ME_InternalDeleteText(editor
, newto
- linebreakSize
, linebreakSize
, FALSE
);
1560 style
= parser
.style
;
1562 else if (format
& SF_TEXT
)
1563 ME_StreamInText(editor
, format
, &inStream
, style
);
1565 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1566 ME_GetSelection(editor
, &to
, &to2
);
1567 /* put the cursor at the top */
1568 if (!(format
& SFF_SELECTION
))
1569 ME_SetSelection(editor
, 0, 0);
1572 /* Restore saved undo mode */
1573 editor
->nUndoMode
= nUndoMode
;
1575 /* even if we didn't add an undo, we need to commit anything on the stack */
1576 ME_CommitUndo(editor
);
1578 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1579 if (!(format
& SFF_SELECTION
))
1580 ME_EmptyUndoStack(editor
);
1582 ME_ReleaseStyle(style
);
1583 editor
->nEventMask
= nEventMask
;
1584 ME_UpdateRepaint(editor
);
1585 if (!(format
& SFF_SELECTION
)) {
1586 ME_ClearTempStyle(editor
);
1588 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1589 ME_MoveCaret(editor
);
1590 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1591 ME_SendSelChange(editor
);
1592 ME_SendRequestResize(editor
, FALSE
);
1598 typedef struct tagME_RTFStringStreamStruct
1603 } ME_RTFStringStreamStruct
;
1605 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1607 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1610 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1611 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1612 pStruct
->pos
+= count
;
1618 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1621 ME_RTFStringStreamStruct data
;
1623 data
.string
= string
;
1624 data
.length
= strlen(string
);
1626 es
.dwCookie
= (DWORD_PTR
)&data
;
1627 es
.pfnCallback
= ME_ReadFromRTFString
;
1628 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1633 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1635 const int nLen
= lstrlenW(text
);
1636 const int nTextLen
= ME_GetTextLength(editor
);
1639 ME_DisplayItem
*item
;
1640 ME_DisplayItem
*para
;
1641 WCHAR wLastChar
= ' ';
1643 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1644 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1646 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1647 FIXME("Flags 0x%08x not implemented\n",
1648 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1651 if (chrg
->cpMax
== -1)
1654 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1656 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1657 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1662 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1663 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1667 chrgText
->cpMin
= -1;
1668 chrgText
->cpMax
= -1;
1673 /* when searching up, if cpMin < cpMax, then instead of searching
1674 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1675 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1676 * case, it is always bigger than cpMin.
1678 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1682 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1683 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1689 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1692 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1696 if (flags
& FR_DOWN
) /* Forward search */
1698 /* If possible, find the character before where the search starts */
1699 if ((flags
& FR_WHOLEWORD
) && nMin
)
1701 ME_RunOfsFromCharOfs(editor
, nMin
- 1, NULL
, &item
, &nStart
);
1702 wLastChar
= item
->member
.run
.strText
->szData
[nStart
];
1705 ME_RunOfsFromCharOfs(editor
, nMin
, ¶
, &item
, &nStart
);
1708 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
+ nLen
<= nMax
)
1710 ME_DisplayItem
*pCurItem
= item
;
1711 int nCurStart
= nStart
;
1714 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
], text
[nMatched
], (flags
& FR_MATCHCASE
)))
1716 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1720 if (nMatched
== nLen
)
1722 ME_DisplayItem
*pNextItem
= pCurItem
;
1723 int nNextStart
= nCurStart
;
1726 /* Check to see if next character is a whitespace */
1727 if (flags
& FR_WHOLEWORD
)
1729 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.strText
->nLen
)
1731 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1732 nNextStart
= -nMatched
;
1736 wNextChar
= pNextItem
->member
.run
.strText
->szData
[nNextStart
+ nMatched
];
1740 if (isalnumW(wNextChar
))
1744 nStart
+= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
1747 chrgText
->cpMin
= nStart
;
1748 chrgText
->cpMax
= nStart
+ nLen
;
1750 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1753 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.strText
->nLen
)
1755 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1756 nCurStart
= -nMatched
;
1760 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
];
1765 if (nStart
== item
->member
.run
.strText
->nLen
)
1767 item
= ME_FindItemFwd(item
, diRun
);
1768 para
= ME_GetParagraph(item
);
1773 else /* Backward search */
1775 /* If possible, find the character after where the search ends */
1776 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1778 ME_RunOfsFromCharOfs(editor
, nMax
+ 1, NULL
, &item
, &nEnd
);
1779 wLastChar
= item
->member
.run
.strText
->szData
[nEnd
];
1782 ME_RunOfsFromCharOfs(editor
, nMax
, ¶
, &item
, &nEnd
);
1785 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nEnd
- nLen
>= nMin
)
1787 ME_DisplayItem
*pCurItem
= item
;
1793 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1794 nCurEnd
= pCurItem
->member
.run
.strText
->nLen
+ nMatched
;
1797 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1], text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
)))
1799 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1803 if (nMatched
== nLen
)
1805 ME_DisplayItem
*pPrevItem
= pCurItem
;
1806 int nPrevEnd
= nCurEnd
;
1809 /* Check to see if previous character is a whitespace */
1810 if (flags
& FR_WHOLEWORD
)
1812 if (nPrevEnd
- nMatched
== 0)
1814 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1816 nPrevEnd
= pPrevItem
->member
.run
.strText
->nLen
+ nMatched
;
1820 wPrevChar
= pPrevItem
->member
.run
.strText
->szData
[nPrevEnd
- nMatched
- 1];
1824 if (isalnumW(wPrevChar
))
1828 nStart
= ME_GetParagraph(pCurItem
)->member
.para
.nCharOfs
1829 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1832 chrgText
->cpMin
= nStart
;
1833 chrgText
->cpMax
= nStart
+ nLen
;
1835 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1838 if (nCurEnd
- nMatched
== 0)
1840 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1841 /* Don't care about pCurItem becoming NULL here; it's already taken
1842 * care of in the exterior loop condition */
1843 nCurEnd
= pCurItem
->member
.run
.strText
->nLen
+ nMatched
;
1847 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1];
1854 item
= ME_FindItemBack(item
, diRun
);
1855 para
= ME_GetParagraph(item
);
1856 nEnd
= item
->member
.run
.strText
->nLen
;
1860 TRACE("not found\n");
1862 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1866 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1868 int nStart
, nCount
; /* in chars */
1870 if (!ex
->cb
|| !pText
) return 0;
1872 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1873 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1875 if (ex
->flags
& GT_SELECTION
)
1877 ME_GetSelection(editor
, &nStart
, &nCount
);
1883 nCount
= 0x7fffffff;
1885 if (ex
->codepage
== 1200)
1887 nCount
= min(nCount
, ex
->cb
/ sizeof(WCHAR
) - 1);
1888 return ME_GetTextW(editor
, (LPWSTR
)pText
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
1892 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1893 we can just take a bigger buffer? :)
1894 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1895 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1897 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1899 DWORD buflen
= ex
->cb
;
1903 nCount
= min(nCount
, ex
->cb
- 1);
1904 buffer
= heap_alloc((crlfmul
*nCount
+ 1) * sizeof(WCHAR
));
1906 buflen
= ME_GetTextW(editor
, buffer
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
1907 rc
= WideCharToMultiByte(ex
->codepage
, flags
, buffer
, buflen
+1,
1908 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1909 if (rc
) rc
--; /* do not count 0 terminator */
1916 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
1917 int start
, int nLen
, BOOL unicode
)
1920 return ME_GetTextW(editor
, strText
, start
, nLen
, 0);
1922 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
1923 int nChars
= ME_GetTextW(editor
, p
, start
, nLen
, 0);
1924 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
1925 nLen
+1, NULL
, NULL
);
1931 typedef struct tagME_GlobalDestStruct
1935 } ME_GlobalDestStruct
;
1937 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1939 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1944 pDest
= (WORD
*)lpBuff
;
1945 pSrc
= GlobalLock(pData
->hData
);
1946 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1947 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1949 pData
->nLength
+= i
;
1951 GlobalUnlock(pData
->hData
);
1955 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1957 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1962 pSrc
= GlobalLock(pData
->hData
);
1963 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1964 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1966 pData
->nLength
+= i
;
1968 GlobalUnlock(pData
->hData
);
1972 static BOOL
ME_Paste(ME_TextEditor
*editor
)
1976 ME_GlobalDestStruct gds
;
1977 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
1980 if (IsClipboardFormatAvailable(nRTFFormat
))
1981 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
1982 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
1983 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
1987 if (!OpenClipboard(editor
->hWnd
))
1989 gds
.hData
= GetClipboardData(cf
);
1991 es
.dwCookie
= (DWORD_PTR
)&gds
;
1992 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
1993 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
1999 static BOOL
ME_Copy(ME_TextEditor
*editor
, CHARRANGE
*range
)
2001 LPDATAOBJECT dataObj
= NULL
;
2004 if (editor
->cPasswordMask
)
2005 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2007 if(editor
->lpOleCallback
)
2008 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, range
, RECO_COPY
, &dataObj
);
2009 if(FAILED(hr
) || !dataObj
)
2010 hr
= ME_GetDataObject(editor
, range
, &dataObj
);
2012 hr
= OleSetClipboard(dataObj
);
2013 IDataObject_Release(dataObj
);
2015 return SUCCEEDED(hr
) != 0;
2018 /* helper to send a msg filter notification */
2020 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2024 if (!editor
->hWnd
) return FALSE
;
2025 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2026 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2027 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2029 msgf
.wParam
= *wParam
;
2030 msgf
.lParam
= *lParam
;
2031 if (SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2033 *wParam
= msgf
.wParam
;
2034 *lParam
= msgf
.lParam
;
2035 msgf
.wParam
= *wParam
;
2040 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2042 ME_DisplayItem
*startPara
, *endPara
;
2043 ME_DisplayItem
*prev_para
;
2046 if (!editor
->AutoURLDetect_bEnable
) return;
2048 ME_GetSelection(editor
, &from
, &to
);
2050 /* Find paragraph previous to the one that contains start cursor */
2051 ME_RunOfsFromCharOfs(editor
, from
, &startPara
, NULL
, NULL
);
2052 prev_para
= startPara
->member
.para
.prev_para
;
2053 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2055 /* Find paragraph that contains end cursor */
2056 ME_RunOfsFromCharOfs(editor
, to
, &endPara
, NULL
, NULL
);
2057 endPara
= endPara
->member
.para
.next_para
;
2059 ME_UpdateLinkAttribute(editor
,
2060 startPara
->member
.para
.nCharOfs
,
2061 endPara
->member
.para
.nCharOfs
);
2065 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2067 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2068 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2070 if (editor
->bMouseCaptured
)
2072 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2073 editor
->nSelectionType
= stPosition
;
2081 editor
->nUDArrowX
= -1;
2087 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2088 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2092 editor
->nUDArrowX
= -1;
2093 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2094 if (editor
->styleFlags
& ES_READONLY
)
2096 if (ME_IsSelection(editor
))
2098 ME_DeleteSelection(editor
);
2099 ME_CommitUndo(editor
);
2101 else if (nKey
== VK_DELETE
)
2103 /* Delete stops group typing.
2104 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2105 ME_DeleteTextAtCursor(editor
, 1, 1);
2106 ME_CommitUndo(editor
);
2108 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2110 BOOL bDeletionSucceeded
;
2111 /* Backspace can be grouped for a single undo */
2112 ME_ContinueCoalescingTransaction(editor
);
2113 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2114 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2115 /* Deletion was prevented so the cursor is moved back to where it was.
2116 * (e.g. this happens when trying to delete cell boundaries)
2118 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2120 ME_CommitCoalescingUndo(editor
);
2124 ME_MoveCursorFromTableRowStartParagraph(editor
);
2125 ME_UpdateSelectionLinkAttribute(editor
);
2126 ME_UpdateRepaint(editor
);
2127 ME_SendRequestResize(editor
, FALSE
);
2130 if (editor
->styleFlags
& ES_MULTILINE
)
2132 ME_Cursor cursor
= editor
->pCursors
[0];
2133 ME_DisplayItem
*para
= cursor
.pPara
;
2135 const WCHAR endl
= '\r';
2138 if (editor
->styleFlags
& ES_READONLY
) {
2139 MessageBeep(MB_ICONERROR
);
2143 ME_GetSelection(editor
, &from
, &to
);
2144 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2146 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2147 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2148 /* Add a new table row after this row. */
2149 para
= ME_AppendTableRow(editor
, para
);
2150 para
= para
->member
.para
.next_para
;
2151 editor
->pCursors
[0].pPara
= para
;
2152 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2153 editor
->pCursors
[0].nOffset
= 0;
2154 editor
->pCursors
[1] = editor
->pCursors
[0];
2155 ME_CommitUndo(editor
);
2156 ME_CheckTablesForCorruption(editor
);
2157 ME_UpdateRepaint(editor
);
2160 else if (para
== editor
->pCursors
[1].pPara
&&
2161 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2162 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2163 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2165 /* Insert a newline before the table. */
2166 para
= para
->member
.para
.prev_para
;
2167 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2168 editor
->pCursors
[0].pPara
= para
;
2169 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2170 editor
->pCursors
[1] = editor
->pCursors
[0];
2171 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2172 editor
->pCursors
[0].pRun
->member
.run
.style
);
2173 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2174 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2175 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2176 editor
->pCursors
[0].pPara
= para
;
2177 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2178 editor
->pCursors
[1] = editor
->pCursors
[0];
2179 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2180 ME_CommitCoalescingUndo(editor
);
2181 ME_CheckTablesForCorruption(editor
);
2182 ME_UpdateRepaint(editor
);
2185 } else { /* v1.0 - 3.0 */
2186 ME_DisplayItem
*para
= cursor
.pPara
;
2187 if (ME_IsInTable(para
))
2189 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2192 ME_ContinueCoalescingTransaction(editor
);
2193 para
= ME_AppendTableRow(editor
, 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_CommitCoalescingUndo(editor
);
2199 ME_UpdateRepaint(editor
);
2203 ME_ContinueCoalescingTransaction(editor
);
2204 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2205 !ME_IsInTable(para
->member
.para
.prev_para
))
2207 /* Insert newline before table */
2208 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2210 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2211 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2213 editor
->pCursors
[0].nOffset
= 0;
2214 editor
->pCursors
[1] = editor
->pCursors
[0];
2215 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2216 editor
->pCursors
[0].pRun
->member
.run
.style
);
2218 editor
->pCursors
[1] = editor
->pCursors
[0];
2219 para
= ME_AppendTableRow(editor
, para
);
2220 editor
->pCursors
[0].pPara
= para
;
2221 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2222 editor
->pCursors
[0].nOffset
= 0;
2223 editor
->pCursors
[1] = editor
->pCursors
[0];
2225 ME_CommitCoalescingUndo(editor
);
2226 ME_UpdateRepaint(editor
);
2232 style
= ME_GetInsertStyle(editor
, 0);
2233 ME_SaveTempStyle(editor
);
2234 ME_ContinueCoalescingTransaction(editor
);
2236 ME_InsertEndRowFromCursor(editor
, 0);
2238 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2239 ME_ReleaseStyle(style
);
2240 ME_CommitCoalescingUndo(editor
);
2243 ME_UpdateSelectionLinkAttribute(editor
);
2244 ME_UpdateRepaint(editor
);
2252 ME_SetSelection(editor
, 0, -1);
2258 return ME_Paste(editor
);
2267 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
2268 result
= ME_Copy(editor
, &range
);
2269 if (result
&& nKey
== 'X')
2271 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
, FALSE
);
2272 ME_CommitUndo(editor
);
2273 ME_UpdateRepaint(editor
);
2294 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2295 editor
->nUDArrowX
= -1;
2302 chf
.cbSize
= sizeof(chf
);
2304 ME_GetSelectionCharFormat(editor
, &chf
);
2305 ME_DumpStyleToBuf(&chf
, buf
);
2306 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2310 ME_CheckCharOffsets(editor
);
2317 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2318 LPARAM flags
, BOOL unicode
)
2322 if (editor
->bMouseCaptured
)
2326 wstr
= (WCHAR
)charCode
;
2329 CHAR charA
= charCode
;
2330 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2333 if (editor
->styleFlags
& ES_READONLY
) {
2334 MessageBeep(MB_ICONERROR
);
2335 return 0; /* FIXME really 0 ? */
2338 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2340 ME_Cursor cursor
= editor
->pCursors
[0];
2341 ME_DisplayItem
*para
= cursor
.pPara
;
2343 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2344 ME_GetSelection(editor
, &from
, &to
);
2346 /* v4.1 allows tabs to be inserted with ctrl key down */
2347 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2349 ME_DisplayItem
*para
;
2350 BOOL bSelectedRow
= FALSE
;
2352 para
= cursor
.pPara
;
2353 if (ME_IsSelection(editor
) &&
2354 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2355 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2356 para
->member
.para
.prev_para
->type
== diParagraph
)
2358 para
= para
->member
.para
.prev_para
;
2359 bSelectedRow
= TRUE
;
2361 if (ME_IsInTable(para
))
2363 ME_TabPressedInTable(editor
, bSelectedRow
);
2364 ME_CommitUndo(editor
);
2367 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2368 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2370 para
= para
->member
.para
.next_para
;
2371 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2372 para
= para
->member
.para
.next_para
;
2373 editor
->pCursors
[0].pPara
= para
;
2374 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2375 editor
->pCursors
[0].nOffset
= 0;
2376 editor
->pCursors
[1] = editor
->pCursors
[0];
2379 } else { /* v1.0 - 3.0 */
2380 if (ME_IsInTable(cursor
.pRun
) &&
2381 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2384 /* Text should not be inserted at the end of the table. */
2389 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2390 /* WM_CHAR is restricted to nTextLimit */
2391 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2393 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2394 ME_SaveTempStyle(editor
);
2395 ME_ContinueCoalescingTransaction(editor
);
2396 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2397 ME_ReleaseStyle(style
);
2398 ME_CommitCoalescingUndo(editor
);
2399 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2402 ME_UpdateSelectionLinkAttribute(editor
);
2403 ME_UpdateRepaint(editor
);
2408 /* Process the message and calculate the new click count.
2410 * returns: The click count if it is mouse down event, else returns 0. */
2411 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2414 static int clickNum
= 0;
2415 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2418 if ((msg
== WM_LBUTTONDBLCLK
) ||
2419 (msg
== WM_RBUTTONDBLCLK
) ||
2420 (msg
== WM_MBUTTONDBLCLK
) ||
2421 (msg
== WM_XBUTTONDBLCLK
))
2423 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2426 if ((msg
== WM_LBUTTONDOWN
) ||
2427 (msg
== WM_RBUTTONDOWN
) ||
2428 (msg
== WM_MBUTTONDOWN
) ||
2429 (msg
== WM_XBUTTONDOWN
))
2431 static MSG prevClickMsg
;
2433 /* Compare the editor instead of the hwnd so that the this
2434 * can still be done for windowless richedit controls. */
2435 clickMsg
.hwnd
= (HWND
)editor
;
2436 clickMsg
.message
= msg
;
2437 clickMsg
.wParam
= wParam
;
2438 clickMsg
.lParam
= lParam
;
2439 clickMsg
.time
= GetMessageTime();
2440 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2441 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2442 if ((clickNum
!= 0) &&
2443 (clickMsg
.message
== prevClickMsg
.message
) &&
2444 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2445 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2446 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2447 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2448 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2454 prevClickMsg
= clickMsg
;
2461 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2467 DWORD messagePos
= GetMessagePos();
2468 pt
.x
= (short)LOWORD(messagePos
);
2469 pt
.y
= (short)HIWORD(messagePos
);
2473 sbi
.cbSize
= sizeof(sbi
);
2474 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2475 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2476 PtInRect(&sbi
.rcScrollBar
, pt
))
2478 ITextHost_TxSetCursor(editor
->texthost
,
2479 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2482 sbi
.cbSize
= sizeof(sbi
);
2483 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2484 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2485 PtInRect(&sbi
.rcScrollBar
, pt
))
2487 ITextHost_TxSetCursor(editor
->texthost
,
2488 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2492 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2494 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2495 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2498 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2499 pt
.y
< editor
->rcFormat
.top
&&
2500 pt
.x
< editor
->rcFormat
.left
)
2502 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2505 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2507 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2508 ITextHost_TxSetCursor(editor
->texthost
,
2509 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2511 ITextHost_TxSetCursor(editor
->texthost
,
2512 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2515 if (pt
.x
< editor
->rcFormat
.left
)
2517 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2520 offset
= ME_CharFromPos(editor
, pt
.x
, pt
.y
, &isExact
);
2526 ME_CursorFromCharOfs(editor
, offset
, &cursor
);
2527 run
= &cursor
.pRun
->member
.run
;
2528 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2529 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2531 ITextHost_TxSetCursor(editor
->texthost
,
2532 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2537 if (ME_IsSelection(editor
))
2539 int selStart
, selEnd
;
2540 ME_GetSelection(editor
, &selStart
, &selEnd
);
2541 if (selStart
<= offset
&& selEnd
>= offset
) {
2542 ITextHost_TxSetCursor(editor
->texthost
,
2543 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2549 ITextHost_TxSetCursor(editor
->texthost
,
2550 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2554 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2556 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2557 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2558 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2559 editor
->rcFormat
.right
-= 1;
2562 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2567 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2569 ME_GetSelection(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2570 if(selrange
.cpMin
== selrange
.cpMax
)
2571 seltype
|= SEL_EMPTY
;
2574 /* FIXME: Handle objects */
2575 seltype
|= SEL_TEXT
;
2576 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2577 seltype
|= SEL_MULTICHAR
;
2579 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2581 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, GetParent(editor
->hWnd
), NULL
);
2587 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2589 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2595 ed
->texthost
= texthost
;
2596 ed
->bEmulateVersion10
= bEmulateVersion10
;
2597 ITextHost_TxGetPropertyBits(texthost
,
2598 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2599 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2600 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2601 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2602 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2604 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2605 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2606 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2607 ed
->pBuffer
= ME_MakeText();
2608 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2609 ed
->nAvailWidth
= 0; /* wrap to client area */
2610 ME_MakeFirstParagraph(ed
);
2611 /* The four cursors are for:
2612 * 0 - The position where the caret is shown
2613 * 1 - The anchored end of the selection (for normal selection)
2614 * 2 & 3 - The anchored start and end respectively for word, line,
2615 * or paragraph selection.
2618 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2619 ed
->pCursors
[0].pPara
= ed
->pBuffer
->pFirst
->member
.para
.next_para
;
2620 ed
->pCursors
[0].pRun
= ME_FindItemFwd(ed
->pCursors
[0].pPara
, diRun
);
2621 ed
->pCursors
[0].nOffset
= 0;
2622 ed
->pCursors
[1] = ed
->pCursors
[0];
2623 ed
->pCursors
[2] = ed
->pCursors
[0];
2624 ed
->pCursors
[3] = ed
->pCursors
[1];
2625 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2626 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2629 ed
->rgbBackColor
= -1;
2630 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2631 ed
->bCaretAtEnd
= FALSE
;
2633 ed
->nModifyStep
= 0;
2634 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2635 ed
->pUndoStack
= ed
->pRedoStack
= ed
->pUndoStackBottom
= NULL
;
2636 ed
->nUndoStackSize
= 0;
2637 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2638 ed
->nUndoMode
= umAddToUndo
;
2639 ed
->nParagraphs
= 1;
2640 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2641 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diParagraph
);
2642 ed
->bHideSelection
= FALSE
;
2643 ed
->pfnWordBreak
= NULL
;
2644 ed
->lpOleCallback
= NULL
;
2645 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2646 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2647 ed
->AutoURLDetect_bEnable
= FALSE
;
2648 ed
->bHaveFocus
= FALSE
;
2649 ed
->bMouseCaptured
= FALSE
;
2650 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2652 ed
->pFontCache
[i
].nRefs
= 0;
2653 ed
->pFontCache
[i
].nAge
= 0;
2654 ed
->pFontCache
[i
].hFont
= NULL
;
2657 ME_CheckCharOffsets(ed
);
2658 ed
->bDefaultFormatRect
= TRUE
;
2659 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2661 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2662 ed
->selofs
= SELECTIONBAR_WIDTH
;
2663 ed
->styleFlags
|= ES_SELECTIONBAR
;
2667 ed
->nSelectionType
= stPosition
;
2669 ed
->cPasswordMask
= 0;
2670 if (props
& TXTBIT_USEPASSWORD
)
2671 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2673 if (props
& TXTBIT_AUTOWORDSEL
)
2674 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2675 if (props
& TXTBIT_MULTILINE
) {
2676 ed
->styleFlags
|= ES_MULTILINE
;
2677 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2679 ed
->bWordWrap
= FALSE
;
2681 if (props
& TXTBIT_READONLY
)
2682 ed
->styleFlags
|= ES_READONLY
;
2683 if (!(props
& TXTBIT_HIDESELECTION
))
2684 ed
->styleFlags
|= ES_NOHIDESEL
;
2685 if (props
& TXTBIT_SAVESELECTION
)
2686 ed
->styleFlags
|= ES_SAVESEL
;
2687 if (props
& TXTBIT_VERTICAL
)
2688 ed
->styleFlags
|= ES_VERTICAL
;
2689 if (props
& TXTBIT_DISABLEDRAG
)
2690 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2692 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2694 /* Default scrollbar information */
2695 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2696 ed
->vert_si
.nMin
= 0;
2697 ed
->vert_si
.nMax
= 0;
2698 ed
->vert_si
.nPage
= 0;
2699 ed
->vert_si
.nPos
= 0;
2701 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2702 ed
->horz_si
.nMin
= 0;
2703 ed
->horz_si
.nMax
= 0;
2704 ed
->horz_si
.nPage
= 0;
2705 ed
->horz_si
.nPos
= 0;
2707 OleInitialize(NULL
);
2712 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2714 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2715 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2718 ME_ClearTempStyle(editor
);
2719 ME_EmptyUndoStack(editor
);
2722 ME_DestroyDisplayItem(p
);
2725 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2726 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2728 if (editor
->pFontCache
[i
].hFont
)
2729 DeleteObject(editor
->pFontCache
[i
].hFont
);
2731 if (editor
->rgbBackColor
!= -1)
2732 DeleteObject(editor
->hbrBackground
);
2733 if(editor
->lpOleCallback
)
2734 IUnknown_Release(editor
->lpOleCallback
);
2735 IUnknown_Release(editor
->texthost
);
2738 FREE_OBJ(editor
->pBuffer
);
2739 FREE_OBJ(editor
->pCursors
);
2744 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2749 case DLL_PROCESS_ATTACH
:
2750 DisableThreadLibraryCalls(hinstDLL
);
2751 me_heap
= HeapCreate (0, 0x10000, 0);
2752 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2753 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2757 case DLL_PROCESS_DETACH
:
2758 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2759 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2760 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2761 UnregisterClassA("RichEdit50A", 0);
2762 if (ME_ListBoxRegistered
)
2763 UnregisterClassW(REListBox20W
, 0);
2764 if (ME_ComboBoxRegistered
)
2765 UnregisterClassW(REComboBox20W
, 0);
2767 HeapDestroy (me_heap
);
2775 static const char * const edit_messages
[] = {
2804 "EM_SETPASSWORDCHAR",
2805 "EM_EMPTYUNDOBUFFER",
2806 "EM_GETFIRSTVISIBLELINE",
2808 "EM_SETWORDBREAKPROC",
2809 "EM_GETWORDBREAKPROC",
2810 "EM_GETPASSWORDCHAR",
2820 static const char * const richedit_messages
[] = {
2825 "EM_EXLINEFROMCHAR",
2831 "EM_GETOLEINTERFACE",
2841 "EM_SETOLECALLBACK",
2843 "EM_SETTARGETDEVICE",
2851 "EM_GETWORDBREAKPROCEX",
2852 "EM_SETWORDBREAKPROCEX",
2854 "EM_UNKNOWN_USER_83",
2859 "EM_STOPGROUPTYPING",
2863 "EM_GETAUTOURLDETECT",
2866 "EM_GETTEXTLENGTHEX",
2869 "EM_UNKNOWN_USER_98",
2870 "EM_UNKNOWN_USER_99",
2871 "EM_SETPUNCTUATION",
2872 "EM_GETPUNCTUATION",
2873 "EM_SETWORDWRAPMODE",
2874 "EM_GETWORDWRAPMODE",
2880 "EM_UNKNOWN_USER_109",
2881 "EM_UNKNOWN_USER_110",
2882 "EM_UNKNOWN_USER_111",
2883 "EM_UNKNOWN_USER_112",
2884 "EM_UNKNOWN_USER_113",
2885 "EM_UNKNOWN_USER_114",
2886 "EM_UNKNOWN_USER_115",
2887 "EM_UNKNOWN_USER_116",
2888 "EM_UNKNOWN_USER_117",
2889 "EM_UNKNOWN_USER_118",
2890 "EM_UNKNOWN_USER_119",
2891 "EM_SETLANGOPTIONS",
2892 "EM_GETLANGOPTIONS",
2893 "EM_GETIMECOMPMODE",
2897 "EM_SETIMEMODEBIAS",
2902 get_msg_name(UINT msg
)
2904 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
2905 return edit_messages
[msg
- EM_GETSEL
];
2906 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
2907 return richedit_messages
[msg
- EM_CANPASTE
];
2911 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2914 ME_DisplayItem
*para
, *run
;
2916 int nCharOfs
; /* The start of the clicked text. Absolute character offset */
2919 x
= (short)LOWORD(lParam
);
2920 y
= (short)HIWORD(lParam
);
2921 nCharOfs
= ME_CharFromPos(editor
, x
, y
, &isExact
);
2922 if (!isExact
) return;
2924 ME_RunOfsFromCharOfs(editor
, nCharOfs
, ¶
, &run
, NULL
);
2926 if ((run
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
)
2927 && (run
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
))
2928 { /* The clicked run has CFE_LINK set */
2929 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
2930 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2931 info
.nmhdr
.code
= EN_LINK
;
2933 info
.wParam
= wParam
;
2934 info
.lParam
= lParam
;
2935 info
.chrg
.cpMin
= ME_CharOfsFromRunOfs(editor
, para
, run
, 0);
2936 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ run
->member
.run
.strText
->nLen
;
2937 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,info
.nmhdr
.idFrom
, (LPARAM
)&info
);
2941 #define UNSUPPORTED_MSG(e) \
2943 FIXME(#e ": stub\n"); \
2944 *phresult = S_FALSE; \
2947 /* Handle messages for windowless and windoweded richedit controls.
2949 * The LRESULT that is returned is a return value for window procs,
2950 * and the phresult parameter is the COM return code needed by the
2951 * text services interface. */
2952 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2953 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
2959 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
2960 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
2961 UNSUPPORTED_MSG(EM_FMTLINES
)
2962 UNSUPPORTED_MSG(EM_FORMATRANGE
)
2963 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
2964 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
2965 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
2966 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
2967 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
2968 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
2969 UNSUPPORTED_MSG(EM_GETREDONAME
)
2970 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
2971 UNSUPPORTED_MSG(EM_GETUNDONAME
)
2972 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
2973 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
2974 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
2975 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
2976 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
2977 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
2978 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
2979 UNSUPPORTED_MSG(EM_SETMARGINS
)
2980 UNSUPPORTED_MSG(EM_SETPALETTE
)
2981 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
2982 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
2983 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
2984 UNSUPPORTED_MSG(WM_STYLECHANGING
)
2985 UNSUPPORTED_MSG(WM_STYLECHANGED
)
2987 /* Messages specific to Richedit controls */
2990 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
2992 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
2995 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
|DLGC_HASSETSEL
;
2996 if (editor
->styleFlags
& ES_MULTILINE
)
2997 code
|= DLGC_WANTMESSAGE
;
3000 case EM_EMPTYUNDOBUFFER
:
3001 ME_EmptyUndoStack(editor
);
3005 /* Note: wParam/lParam can be NULL */
3007 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3008 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3009 ME_GetSelection(editor
, (int *)pfrom
, (int *)pto
);
3010 if ((*pfrom
|*pto
) & 0xFFFF0000)
3012 return MAKELONG(*pfrom
,*pto
);
3016 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3017 ME_GetSelection(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3018 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3021 case EM_SETUNDOLIMIT
:
3023 if ((int)wParam
< 0)
3024 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3026 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3027 /* Setting a max stack size keeps wine from getting killed
3028 for hogging memory. Windows allocates all this memory at once, so
3029 no program would realistically set a value above our maximum. */
3030 return editor
->nUndoLimit
;
3033 return editor
->pUndoStack
!= NULL
;
3035 return editor
->pRedoStack
!= NULL
;
3036 case WM_UNDO
: /* FIXME: actually not the same */
3038 return ME_Undo(editor
);
3040 return ME_Redo(editor
);
3043 /* these flags are equivalent to the ES_* counterparts */
3044 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3045 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3046 DWORD settings
= editor
->styleFlags
& mask
;
3052 /* these flags are equivalent to ES_* counterparts, except for
3053 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3054 * but is still stored in editor->styleFlags. */
3055 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3056 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3057 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3058 DWORD settings
= mask
& editor
->styleFlags
;
3059 DWORD oldSettings
= settings
;
3060 DWORD changedSettings
;
3076 changedSettings
= oldSettings
^ settings
;
3078 if (changedSettings
) {
3079 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3081 if (changedSettings
& ECO_SELECTIONBAR
)
3083 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3084 if (settings
& ECO_SELECTIONBAR
) {
3085 assert(!editor
->selofs
);
3086 editor
->selofs
= SELECTIONBAR_WIDTH
;
3087 editor
->rcFormat
.left
+= editor
->selofs
;
3089 editor
->rcFormat
.left
-= editor
->selofs
;
3092 ME_RewrapRepaint(editor
);
3095 if (changedSettings
& settings
& ECO_VERTICAL
)
3096 FIXME("ECO_VERTICAL not implemented yet!\n");
3097 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3098 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3099 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3100 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3101 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3102 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3103 if (changedSettings
& settings
& ECO_WANTRETURN
)
3104 FIXME("ECO_WANTRETURN not implemented yet!\n");
3105 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3106 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3113 ME_InvalidateSelection(editor
);
3114 ME_SetSelection(editor
, wParam
, lParam
);
3115 ME_InvalidateSelection(editor
);
3116 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3117 ME_ShowCaret(editor
);
3118 ME_SendSelChange(editor
);
3121 case EM_SETSCROLLPOS
:
3123 POINT
*point
= (POINT
*)lParam
;
3124 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3127 case EM_AUTOURLDETECT
:
3129 if (wParam
==1 || wParam
==0)
3131 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3134 return E_INVALIDARG
;
3136 case EM_GETAUTOURLDETECT
:
3138 return editor
->AutoURLDetect_bEnable
;
3143 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3145 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
3147 ME_InvalidateSelection(editor
);
3148 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
3149 ME_InvalidateSelection(editor
);
3150 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3151 ME_ShowCaret(editor
);
3152 ME_SendSelChange(editor
);
3156 case EM_SHOWSCROLLBAR
:
3169 flags
= WS_HSCROLL
|WS_VSCROLL
;
3176 editor
->styleFlags
|= flags
;
3177 if (flags
& WS_HSCROLL
)
3178 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3179 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3180 if (flags
& WS_VSCROLL
)
3181 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3182 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3184 editor
->styleFlags
&= ~flags
;
3185 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3192 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3196 BOOL bRtf
, bUnicode
, bSelection
;
3197 int oldModify
= editor
->nModifyStep
;
3199 if (!pStruct
) return 0;
3201 /* If we detect ascii rtf at the start of the string,
3202 * we know it isn't unicode. */
3203 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3204 !strncmp((char *)lParam
, "{\\urtf", 6)));
3205 bUnicode
= !bRtf
&& pStruct
->codepage
== 1200;
3207 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3208 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3209 pStruct
->flags
, pStruct
->codepage
);
3211 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3213 ME_GetSelection(editor
, &from
, &to
);
3214 style
= ME_GetSelectionInsertStyle(editor
);
3215 ME_InternalDeleteText(editor
, from
, to
- from
, FALSE
);
3217 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
3218 style
= editor
->pBuffer
->pDefaultStyle
;
3222 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3224 /* FIXME: The length returned is doesn't include the rtf control
3225 * characters, only the actual text. */
3226 len
= lParam
? strlen((char *)lParam
) : 0;
3229 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3230 wszText
= lParam
? ME_ToUnicode(bUnicode
, (void *)lParam
) : NULL
;
3231 len
= wszText
? lstrlenW(wszText
) : 0;
3232 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3236 ME_ReleaseStyle(style
);
3237 ME_UpdateSelectionLinkAttribute(editor
);
3240 ME_UpdateLinkAttribute(editor
, 0, -1);
3242 ME_CommitUndo(editor
);
3243 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3245 editor
->nModifyStep
= oldModify
;
3246 ME_EmptyUndoStack(editor
);
3248 ME_UpdateRepaint(editor
);
3251 case EM_SETBKGNDCOLOR
:
3254 if (editor
->rgbBackColor
!= -1) {
3255 DeleteObject(editor
->hbrBackground
);
3256 lColor
= editor
->rgbBackColor
;
3258 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3262 editor
->rgbBackColor
= -1;
3263 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3267 editor
->rgbBackColor
= lParam
;
3268 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3270 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3271 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3275 return editor
->nModifyStep
== 0 ? 0 : -1;
3279 editor
->nModifyStep
= 1;
3281 editor
->nModifyStep
= 0;
3285 case EM_SETREADONLY
:
3288 editor
->styleFlags
|= ES_READONLY
;
3290 editor
->styleFlags
&= ~ES_READONLY
;
3293 case EM_SETEVENTMASK
:
3295 DWORD nOldMask
= editor
->nEventMask
;
3297 editor
->nEventMask
= lParam
;
3300 case EM_GETEVENTMASK
:
3301 return editor
->nEventMask
;
3302 case EM_SETCHARFORMAT
:
3304 CHARFORMAT2W buf
, *p
;
3305 BOOL bRepaint
= TRUE
;
3306 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3307 if (p
== NULL
) return 0;
3309 ME_SetDefaultCharFormat(editor
, p
);
3310 else if (wParam
== (SCF_WORD
| SCF_SELECTION
)) {
3311 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3313 } else if (wParam
== SCF_ALL
) {
3314 if (editor
->mode
& TM_PLAINTEXT
)
3315 ME_SetDefaultCharFormat(editor
, p
);
3317 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), p
);
3318 editor
->nModifyStep
= 1;
3320 } else if (editor
->mode
& TM_PLAINTEXT
) {
3324 ME_GetSelection(editor
, &from
, &to
);
3325 bRepaint
= (from
!= to
);
3326 ME_SetSelectionCharFormat(editor
, p
);
3327 if (from
!= to
) editor
->nModifyStep
= 1;
3329 ME_CommitUndo(editor
);
3332 ME_WrapMarkedParagraphs(editor
);
3333 ME_UpdateScrollBar(editor
);
3338 case EM_GETCHARFORMAT
:
3340 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3341 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3342 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3343 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3344 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3346 tmp
.cbSize
= sizeof(tmp
);
3348 ME_GetDefaultCharFormat(editor
, &tmp
);
3350 ME_GetSelectionCharFormat(editor
, &tmp
);
3351 ME_CopyToCFAny(dst
, &tmp
);
3354 case EM_SETPARAFORMAT
:
3356 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3357 ME_WrapMarkedParagraphs(editor
);
3358 ME_UpdateScrollBar(editor
);
3360 ME_CommitUndo(editor
);
3363 case EM_GETPARAFORMAT
:
3364 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3365 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3366 case EM_GETFIRSTVISIBLELINE
:
3368 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3369 int y
= editor
->vert_si
.nPos
;
3374 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3375 if (p
->type
== diTextEnd
)
3377 if (p
->type
== diParagraph
) {
3378 ypara
= p
->member
.para
.pt
.y
;
3381 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3382 yend
= ystart
+ p
->member
.row
.nHeight
;
3390 case EM_HIDESELECTION
:
3392 editor
->bHideSelection
= (wParam
!= 0);
3393 ME_InvalidateSelection(editor
);
3398 if (!(editor
->styleFlags
& ES_MULTILINE
))
3400 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
3406 ME_GetSelection(editor
, &from
, &to
);
3407 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
3408 ME_CommitUndo(editor
);
3409 ME_UpdateRepaint(editor
);
3416 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
3417 size_t len
= wszText
? lstrlenW(wszText
) : 0;
3418 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3420 ME_GetSelection(editor
, &from
, &to
);
3421 style
= ME_GetSelectionInsertStyle(editor
);
3422 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
3423 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3424 ME_ReleaseStyle(style
);
3425 /* drop temporary style if line end */
3427 * FIXME question: does abc\n mean: put abc,
3428 * clear temp style, put \n? (would require a change)
3430 if (len
>0 && wszText
[len
-1] == '\n')
3431 ME_ClearTempStyle(editor
);
3432 ME_EndToUnicode(unicode
, wszText
);
3433 ME_CommitUndo(editor
);
3434 ME_UpdateSelectionLinkAttribute(editor
);
3436 ME_EmptyUndoStack(editor
);
3437 ME_UpdateRepaint(editor
);
3440 case EM_SCROLLCARET
:
3441 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3448 BOOL bRepaint
= LOWORD(lParam
);
3451 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3452 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3453 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3454 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3455 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3456 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), &fmt
);
3457 ME_SetDefaultCharFormat(editor
, &fmt
);
3459 ME_CommitUndo(editor
);
3460 ME_MarkAllForWrapping(editor
);
3461 ME_WrapMarkedParagraphs(editor
);
3462 ME_UpdateScrollBar(editor
);
3469 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
3472 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3473 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3474 !strncmp((char *)lParam
, "{\\urtf", 6))
3476 /* Undocumented: WM_SETTEXT supports RTF text */
3477 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3481 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
3482 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3483 if (lstrlenW(wszText
) > 0)
3487 /* uses default style! */
3488 if (!(editor
->styleFlags
& ES_MULTILINE
))
3493 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3496 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3498 ME_EndToUnicode(unicode
, wszText
);
3502 TRACE("WM_SETTEXT - NULL\n");
3503 ME_UpdateLinkAttribute(editor
, 0, -1);
3504 ME_SetSelection(editor
, 0, 0);
3505 editor
->nModifyStep
= 0;
3506 ME_CommitUndo(editor
);
3507 ME_EmptyUndoStack(editor
);
3508 ME_UpdateRepaint(editor
);
3513 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3514 if (IsClipboardFormatAvailable(nRTFFormat
))
3516 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3527 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
3529 if (ME_Copy(editor
, &range
) && msg
== WM_CUT
)
3531 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
, FALSE
);
3532 ME_CommitUndo(editor
);
3533 ME_UpdateRepaint(editor
);
3537 case WM_GETTEXTLENGTH
:
3539 GETTEXTLENGTHEX how
;
3541 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3542 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3543 how
.codepage
= unicode
? 1200 : CP_ACP
;
3544 return ME_GetTextLengthEx(editor
, &how
);
3546 case EM_GETTEXTLENGTHEX
:
3547 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3551 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3552 ex
.flags
= GT_USECRLF
;
3553 ex
.codepage
= unicode
? 1200 : CP_ACP
;
3554 ex
.lpDefaultChar
= NULL
;
3555 ex
.lpUsedDefChar
= NULL
;
3556 return ME_GetTextEx(editor
, &ex
, lParam
);
3559 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3563 ME_GetSelection(editor
, &from
, &to
);
3564 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3565 to
- from
, unicode
);
3567 case EM_GETSCROLLPOS
:
3569 POINT
*point
= (POINT
*)lParam
;
3570 point
->x
= editor
->horz_si
.nPos
;
3571 point
->y
= editor
->vert_si
.nPos
;
3572 /* 16-bit scaled value is returned as stored in scrollinfo */
3573 if (editor
->horz_si
.nMax
> 0xffff)
3574 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3575 if (editor
->vert_si
.nMax
> 0xffff)
3576 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3579 case EM_GETTEXTRANGE
:
3581 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3582 int start
= rng
->chrg
.cpMin
;
3583 int end
= rng
->chrg
.cpMax
;
3584 int textlength
= ME_GetTextLength(editor
);
3585 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3586 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
,
3587 editor
->bEmulateVersion10
, ME_GetTextLength(editor
));
3588 if (start
< 0) return 0;
3589 if ((start
== 0 && end
== -1) || end
> textlength
)
3591 if (start
>= end
) return 0;
3592 return ME_GetTextRange(editor
, rng
->lpstrText
, start
, end
- start
, unicode
);
3596 ME_DisplayItem
*run
;
3597 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3598 unsigned int nCharsLeft
= nMaxChars
;
3599 char *dest
= (char *) lParam
;
3600 BOOL wroteNull
= FALSE
;
3602 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3603 unicode
? "Unicode" : "Ansi");
3605 run
= ME_FindRowWithNumber(editor
, wParam
);
3609 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3610 && run
->type
== diRun
)
3615 strText
= run
->member
.run
.strText
;
3616 nCopy
= min(nCharsLeft
, strText
->nLen
);
3619 memcpy(dest
, strText
->szData
, nCopy
* sizeof(WCHAR
));
3621 nCopy
= WideCharToMultiByte(CP_ACP
, 0, strText
->szData
, nCopy
, dest
,
3622 nCharsLeft
, NULL
, NULL
);
3623 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3624 nCharsLeft
-= nCopy
;
3627 /* append line termination, space allowing */
3631 *((WCHAR
*)dest
) = '\0';
3638 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3639 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3641 case EM_GETLINECOUNT
:
3643 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3646 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3648 while (item
!= editor
->pBuffer
->pLast
)
3650 assert(item
->type
== diParagraph
);
3651 prev_para
= ME_FindItemBack(item
, diRun
);
3653 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3655 nRows
+= item
->member
.para
.nRows
;
3656 item
= item
->member
.para
.next_para
;
3658 last_para
= ME_FindItemBack(item
, diRun
);
3660 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3661 if (editor
->bEmulateVersion10
&& prev_para
&&
3662 last_para
->member
.run
.nCharOfs
== 0 &&
3663 prev_para
->member
.run
.strText
->nLen
== 1 &&
3664 prev_para
->member
.run
.strText
->szData
[0] == '\r')
3666 /* In 1.0 emulation, the last solitary \r at the very end of the text
3667 (if one exists) is NOT a line break.
3668 FIXME: this is an ugly hack. This should have a more regular model. */
3672 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3673 return max(1, nRows
);
3675 case EM_LINEFROMCHAR
:
3678 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3680 return ME_RowNumberFromCharOfs(editor
, wParam
);
3682 case EM_EXLINEFROMCHAR
:
3685 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
3687 return ME_RowNumberFromCharOfs(editor
, lParam
);
3691 ME_DisplayItem
*item
, *para
;
3695 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3697 item
= ME_FindRowWithNumber(editor
, wParam
);
3700 para
= ME_GetParagraph(item
);
3701 item
= ME_FindItemFwd(item
, diRun
);
3702 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3703 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3708 ME_DisplayItem
*item
, *item_end
;
3709 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3710 ME_DisplayItem
*para
, *run
;
3712 if (wParam
> ME_GetTextLength(editor
))
3716 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3719 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3720 item
= ME_RowStart(run
);
3721 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3722 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3723 if (item_end
->type
== diStartRow
) {
3724 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3726 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3727 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3728 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.strText
->nLen
;
3730 nChars
= nNextLineOfs
- nThisLineOfs
;
3731 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3734 case EM_EXLIMITTEXT
:
3736 if ((int)lParam
< 0)
3739 editor
->nTextLimit
= 65536;
3741 editor
->nTextLimit
= (int) lParam
;
3747 editor
->nTextLimit
= 65536;
3749 editor
->nTextLimit
= (int) wParam
;
3752 case EM_GETLIMITTEXT
:
3754 return editor
->nTextLimit
;
3758 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3759 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3763 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3764 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3765 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3771 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3772 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3776 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3777 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3778 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3784 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3785 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3787 case EM_FINDTEXTEXW
:
3789 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3790 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3793 if (!wParam
|| !lParam
)
3795 *(int *)wParam
= editor
->nZoomNumerator
;
3796 *(int *)lParam
= editor
->nZoomDenominator
;
3799 return ME_SetZoom(editor
, wParam
, lParam
);
3800 case EM_CHARFROMPOS
:
3801 return ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
, NULL
);
3802 case EM_POSFROMCHAR
:
3804 ME_DisplayItem
*pPara
, *pRun
;
3805 int nCharOfs
, nOffset
, nLength
;
3809 /* detect which API version we're dealing with */
3810 if (wParam
>= 0x40000)
3812 nLength
= ME_GetTextLength(editor
);
3813 nCharOfs
= min(nCharOfs
, nLength
);
3814 nCharOfs
= max(nCharOfs
, 0);
3816 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
3817 assert(pRun
->type
== diRun
);
3818 pt
.y
= pRun
->member
.run
.pt
.y
;
3819 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
);
3820 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
3821 pt
.x
+= editor
->rcFormat
.left
;
3823 pt
.x
-= editor
->horz_si
.nPos
;
3824 pt
.y
-= editor
->vert_si
.nPos
;
3826 if (wParam
>= 0x40000) {
3827 *(POINTL
*)wParam
= pt
;
3829 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
3835 ME_SetDefaultFormatRect(editor
);
3837 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
3838 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
3839 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
3841 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
3842 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
3844 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3846 if (editor
->styleFlags
& WS_VSCROLL
)
3848 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
3849 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
3851 if (editor
->styleFlags
& WS_HSCROLL
)
3853 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
3854 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
3858 ME_CommitUndo(editor
);
3859 ME_WrapMarkedParagraphs(editor
);
3860 ME_MoveCaret(editor
);
3864 ME_DestroyEditor(editor
);
3868 return ME_SetCursor(editor
);
3870 case WM_LBUTTONDBLCLK
:
3871 case WM_LBUTTONDOWN
:
3873 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3874 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3875 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3877 ITextHost_TxSetFocus(editor
->texthost
);
3878 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
3879 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
3880 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
3881 editor
->bMouseCaptured
= TRUE
;
3882 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3883 if (!ME_SetCursor(editor
)) goto do_default
;
3887 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3888 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3890 if (editor
->bMouseCaptured
)
3891 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
3892 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3893 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3894 if (editor
->bMouseCaptured
)
3895 ME_SetCursor(editor
);
3898 if (editor
->bMouseCaptured
) {
3899 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
3900 editor
->bMouseCaptured
= FALSE
;
3902 if (editor
->nSelectionType
== stDocument
)
3903 editor
->nSelectionType
= stPosition
;
3904 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3905 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3909 ME_SetCursor(editor
);
3910 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3914 case WM_RBUTTONDOWN
:
3915 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3916 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3917 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3920 case WM_CONTEXTMENU
:
3921 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
3925 editor
->bHaveFocus
= TRUE
;
3926 ME_ShowCaret(editor
);
3927 ME_SendOldNotify(editor
, EN_SETFOCUS
);
3930 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3931 editor
->bHaveFocus
= FALSE
;
3932 ME_HideCaret(editor
);
3933 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
3936 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
3939 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
3940 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3944 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
3945 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3947 if (ME_KeyDown(editor
, LOWORD(wParam
)))
3951 return ME_Char(editor
, wParam
, lParam
, unicode
);
3955 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
3956 if(wParam
<= 0x000fffff)
3958 if(wParam
> 0xffff) /* convert to surrogates */
3961 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
3962 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
3964 ME_Char(editor
, wParam
, 0, TRUE
);
3970 case EM_STOPGROUPTYPING
:
3971 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3975 const int scrollUnit
= 7;
3977 switch(LOWORD(wParam
))
3980 ME_ScrollAbs(editor
, 0, 0);
3983 ME_ScrollAbs(editor
,
3984 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
3985 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
3988 ME_ScrollLeft(editor
, scrollUnit
);
3991 ME_ScrollRight(editor
, scrollUnit
);
3994 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
3997 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4000 case SB_THUMBPOSITION
:
4002 int pos
= HIWORD(wParam
);
4003 if (editor
->horz_si
.nMax
> 0xffff)
4004 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4005 ME_HScrollAbs(editor
, pos
);
4011 case EM_SCROLL
: /* fall through */
4017 origNPos
= editor
->vert_si
.nPos
;
4020 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
4021 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
4022 if (lineHeight
<= 0) lineHeight
= 24;
4024 switch(LOWORD(wParam
))
4027 ME_ScrollAbs(editor
, 0, 0);
4030 ME_ScrollAbs(editor
,
4031 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4032 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4035 ME_ScrollUp(editor
,lineHeight
);
4038 ME_ScrollDown(editor
,lineHeight
);
4041 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4044 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4047 case SB_THUMBPOSITION
:
4049 int pos
= HIWORD(wParam
);
4050 if (editor
->vert_si
.nMax
> 0xffff)
4051 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4052 ME_VScrollAbs(editor
, pos
);
4056 if (msg
== EM_SCROLL
)
4057 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4063 UINT pulScrollLines
;
4066 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4067 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4070 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4072 gcWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4074 if (abs(gcWheelDelta
) >= WHEEL_DELTA
)
4078 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4082 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4084 numerator
= numerator
+ (gcWheelDelta
/ WHEEL_DELTA
) * 10;
4085 if (numerator
>= 10 && numerator
<= 500)
4086 ME_SetZoom(editor
, numerator
, 100);
4088 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
4089 /* FIXME follow the original */
4091 ME_ScrollDown(editor
,pulScrollLines
* (-gcWheelDelta
/ WHEEL_DELTA
) * 8);
4098 *((RECT
*)lParam
) = editor
->rcFormat
;
4099 if (editor
->bDefaultFormatRect
)
4100 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4110 RECT
*rc
= (RECT
*)lParam
;
4112 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4113 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4116 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4117 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4118 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4119 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4120 } else if (wParam
== 1) {
4121 /* MSDN incorrectly says a wParam value of 1 causes the
4122 * lParam rect to be used as a relative offset,
4123 * however, the tests show it just prevents min/max bound
4125 editor
->rcFormat
.top
= rc
->top
- border
;
4126 editor
->rcFormat
.left
= rc
->left
- border
;
4127 editor
->rcFormat
.bottom
= rc
->bottom
;
4128 editor
->rcFormat
.right
= rc
->right
+ border
;
4132 editor
->bDefaultFormatRect
= FALSE
;
4136 ME_SetDefaultFormatRect(editor
);
4137 editor
->bDefaultFormatRect
= TRUE
;
4139 ME_MarkAllForWrapping(editor
);
4140 ME_WrapMarkedParagraphs(editor
);
4141 ME_UpdateScrollBar(editor
);
4142 if (msg
!= EM_SETRECTNP
)
4146 case EM_REQUESTRESIZE
:
4147 ME_SendRequestResize(editor
, TRUE
);
4155 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4156 if (editor
->bDefaultFormatRect
) {
4157 ME_SetDefaultFormatRect(editor
);
4159 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4160 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4162 editor
->prevClientRect
= clientRect
;
4163 ME_RewrapRepaint(editor
);
4166 /* IME messages to make richedit controls IME aware */
4167 case WM_IME_SETCONTEXT
:
4168 case WM_IME_CONTROL
:
4170 case WM_IME_COMPOSITIONFULL
:
4172 case WM_IME_STARTCOMPOSITION
:
4174 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4175 ME_DeleteSelection(editor
);
4176 ME_CommitUndo(editor
);
4177 ME_UpdateRepaint(editor
);
4180 case WM_IME_COMPOSITION
:
4184 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4185 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4186 ME_DeleteSelection(editor
);
4187 ME_CommitUndo(editor
);
4188 ME_SaveTempStyle(editor
);
4189 if (lParam
& GCS_RESULTSTR
)
4191 LPWSTR lpCompStr
= NULL
;
4194 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4195 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4196 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpCompStr
, dwBufLen
);
4197 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4198 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4200 else if (lParam
& GCS_COMPSTR
)
4202 LPWSTR lpCompStr
= NULL
;
4205 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4206 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4207 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, dwBufLen
);
4208 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4210 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4211 ME_SetSelection(editor
,editor
->imeStartIndex
,
4212 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4214 ME_ReleaseStyle(style
);
4215 ME_UpdateRepaint(editor
);
4218 case WM_IME_ENDCOMPOSITION
:
4220 ME_DeleteSelection(editor
);
4221 editor
->imeStartIndex
=-1;
4224 case EM_GETOLEINTERFACE
:
4226 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4227 return CreateIRichEditOle(editor
, ppvObj
);
4229 case EM_GETPASSWORDCHAR
:
4231 return editor
->cPasswordMask
;
4233 case EM_SETOLECALLBACK
:
4234 if(editor
->lpOleCallback
)
4235 IUnknown_Release(editor
->lpOleCallback
);
4236 editor
->lpOleCallback
= (LPRICHEDITOLECALLBACK
)lParam
;
4237 if(editor
->lpOleCallback
)
4238 IUnknown_AddRef(editor
->lpOleCallback
);
4240 case EM_GETWORDBREAKPROC
:
4241 return (LRESULT
)editor
->pfnWordBreak
;
4242 case EM_SETWORDBREAKPROC
:
4244 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4246 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4247 return (LRESULT
)pfnOld
;
4249 case EM_GETTEXTMODE
:
4250 return editor
->mode
;
4251 case EM_SETTEXTMODE
:
4256 GETTEXTLENGTHEX how
;
4258 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4259 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
4260 how
.codepage
= unicode
? 1200 : CP_ACP
;
4261 ret
= ME_GetTextLengthEx(editor
, &how
);
4264 /*Check for valid wParam*/
4265 if ((((wParam
& TM_RICHTEXT
) && ((wParam
& TM_PLAINTEXT
) << 1))) ||
4266 (((wParam
& TM_MULTILEVELUNDO
) && ((wParam
& TM_SINGLELEVELUNDO
) << 1))) ||
4267 (((wParam
& TM_MULTICODEPAGE
) && ((wParam
& TM_SINGLECODEPAGE
) << 1))))
4271 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4273 mask
|= (TM_RICHTEXT
| TM_PLAINTEXT
);
4274 changes
|= (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
));
4276 /*FIXME: Currently no support for undo level and code page options*/
4277 editor
->mode
= (editor
->mode
& (~mask
)) | changes
;
4283 case EM_SETPASSWORDCHAR
:
4285 editor
->cPasswordMask
= wParam
;
4286 ME_RewrapRepaint(editor
);
4289 case EM_SETTARGETDEVICE
:
4292 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4293 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4295 editor
->bWordWrap
= new;
4296 editor
->nAvailWidth
= 0; /* wrap to client area */
4297 ME_RewrapRepaint(editor
);
4300 int width
= max(0, lParam
);
4301 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4302 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4304 editor
->nAvailWidth
= width
;
4305 editor
->bWordWrap
= TRUE
;
4306 ME_RewrapRepaint(editor
);
4308 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4313 *phresult
= S_FALSE
;
4319 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4320 LPARAM lParam
, BOOL unicode
)
4322 ME_TextEditor
*editor
;
4324 LRESULT lresult
= 0;
4326 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4327 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4329 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4332 if (msg
== WM_NCCREATE
)
4334 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4335 ITextHost
*texthost
;
4337 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4338 texthost
= ME_CreateTextHost(hWnd
, FALSE
);
4339 return texthost
!= NULL
;
4341 else if (msg
!= WM_NCDESTROY
)
4343 ERR("called with invalid hWnd %p - application bug?\n", hWnd
);
4356 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4357 /* Erase area outside of the formatting rectangle */
4358 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4361 rc
.bottom
= editor
->rcFormat
.top
;
4362 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4363 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4365 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4367 rc
.top
= editor
->rcFormat
.bottom
;
4368 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4369 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4371 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4373 rc
.right
= editor
->rcFormat
.left
;
4374 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4375 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4377 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4379 rc
.left
= editor
->rcFormat
.right
;
4380 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4381 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4384 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
4385 EndPaint(editor
->hWnd
, &ps
);
4390 HDC hDC
= (HDC
)wParam
;
4393 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4394 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4400 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4401 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4403 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4404 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4405 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4406 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4409 case EM_SETREADONLY
:
4412 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4413 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4414 dwStyle
&= ~ES_READONLY
;
4416 dwStyle
|= ES_READONLY
;
4417 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4421 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4424 if (hresult
== S_FALSE
)
4425 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4427 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4428 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4433 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4435 BOOL unicode
= TRUE
;
4437 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4438 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4441 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4444 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4446 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4449 /******************************************************************
4450 * RichEditANSIWndProc (RICHED20.10)
4452 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4454 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4457 /******************************************************************
4458 * RichEdit10ANSIWndProc (RICHED20.9)
4460 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4462 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4464 ITextHost
*texthost
;
4465 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4467 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4468 texthost
= ME_CreateTextHost(hWnd
, TRUE
);
4469 return texthost
!= NULL
;
4471 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4474 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4476 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4479 int ME_CountParagraphsBetween(ME_TextEditor
*editor
, int from
, int to
)
4481 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
4484 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= from
)
4485 item
= item
->member
.para
.next_para
;
4488 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= to
) {
4489 item
= item
->member
.para
.next_para
;
4496 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int nStart
,
4497 int nChars
, int bCRLF
)
4499 ME_DisplayItem
*pRun
;
4500 int nOffset
, nWritten
= 0;
4501 WCHAR
*pStart
= buffer
;
4503 ME_RunOfsFromCharOfs(editor
, nStart
, NULL
, &pRun
, &nOffset
);
4505 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4506 if (editor
->bEmulateVersion10
) bCRLF
= 0;
4508 while (nChars
&& pRun
)
4512 if (pRun
->member
.run
.nFlags
& MERF_ENDCELL
&&
4513 pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4517 } else if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
) {
4518 if (!ME_FindItemFwd(pRun
, diRun
)) {
4519 /* No '\r' is appended to the last paragraph. */
4521 } else if (bCRLF
&& nChars
== 1) {
4525 WCHAR cr_lf
[] = {'\r', '\n', 0};
4533 nLen
= pRun
->member
.run
.strText
->nLen
;
4534 szData
= pRun
->member
.run
.strText
->szData
;
4536 nLen
= min(nChars
, nLen
- nOffset
);
4537 CopyMemory(buffer
, szData
+ nOffset
, sizeof(WCHAR
) * nLen
);
4540 nLen
= min(nChars
, pRun
->member
.run
.strText
->nLen
- nOffset
);
4541 CopyMemory(buffer
, pRun
->member
.run
.strText
->szData
+ nOffset
,
4542 sizeof(WCHAR
) * nLen
);
4549 pRun
= ME_FindItemFwd(pRun
, diRun
);
4552 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
4556 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4561 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4562 wcW
.lpfnWndProc
= RichEditWndProcW
;
4564 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4565 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4567 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4568 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4569 wcW
.lpszMenuName
= NULL
;
4571 if (is_version_nt())
4573 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4574 if (!RegisterClassW(&wcW
)) return FALSE
;
4575 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4576 if (!RegisterClassW(&wcW
)) return FALSE
;
4580 /* WNDCLASSA/W have the same layout */
4581 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4582 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4583 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4584 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4587 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4588 wcA
.lpfnWndProc
= RichEditWndProcA
;
4590 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4591 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4593 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4594 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4595 wcA
.lpszMenuName
= NULL
;
4596 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4597 if (!RegisterClassA(&wcA
)) return FALSE
;
4598 wcA
.lpszClassName
= "RichEdit50A";
4599 if (!RegisterClassA(&wcA
)) return FALSE
;
4604 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4605 /* FIXME: Not implemented */
4606 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4607 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4608 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4611 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4612 /* FIXME: Not implemented */
4613 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4614 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4615 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4618 /******************************************************************
4619 * REExtendedRegisterClass (RICHED20.8)
4621 * FIXME undocumented
4622 * Need to check for errors and implement controls and callbacks
4624 LRESULT WINAPI
REExtendedRegisterClass(void)
4629 FIXME("semi stub\n");
4633 wcW
.hInstance
= NULL
;
4636 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4637 wcW
.lpszMenuName
= NULL
;
4639 if (!ME_ListBoxRegistered
)
4641 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4642 wcW
.lpfnWndProc
= REListWndProc
;
4643 wcW
.lpszClassName
= REListBox20W
;
4644 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4647 if (!ME_ComboBoxRegistered
)
4649 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4650 wcW
.lpfnWndProc
= REComboWndProc
;
4651 wcW
.lpszClassName
= REComboBox20W
;
4652 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4656 if (ME_ListBoxRegistered
)
4658 if (ME_ComboBoxRegistered
)
4664 static BOOL
isurlspecial(WCHAR c
)
4666 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4667 return strchrW( special_chars
, c
) != NULL
;
4671 * This proc takes a selection, and scans it forward in order to select the span
4672 * of a possible URL candidate. A possible URL candidate must start with isalnum
4673 * or one of the following special characters: *|/\+%#@ and must consist entirely
4674 * of the characters allowed to start the URL, plus : (colon) which may occur
4675 * at most once, and not at either end.
4677 * sel_max == -1 indicates scan to end of text.
4679 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
, int sel_min
, int sel_max
,
4680 int * candidate_min
, int * candidate_max
)
4682 ME_DisplayItem
* item
;
4683 ME_DisplayItem
* para
;
4685 BOOL foundColon
= FALSE
;
4686 WCHAR lastAcceptedChar
= '\0';
4688 TRACE("sel_min = %d sel_max = %d\n", sel_min
, sel_max
);
4690 *candidate_min
= *candidate_max
= -1;
4691 ME_RunOfsFromCharOfs(editor
, sel_min
, ¶
, &item
, &nStart
);
4692 TRACE("nStart = %d\n", nStart
);
4693 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4694 while (item
&& para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
< sel_max
)
4696 ME_DisplayItem
* next_item
;
4698 if (!(item
->member
.run
.nFlags
& MERF_ENDPARA
)) {
4699 /* Find start of candidate */
4700 if (*candidate_min
== -1) {
4701 while (nStart
< item
->member
.run
.strText
->nLen
&&
4702 !(isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4703 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]))) {
4706 if (nStart
< item
->member
.run
.strText
->nLen
&&
4707 (isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4708 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]))) {
4709 *candidate_min
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4710 lastAcceptedChar
= item
->member
.run
.strText
->szData
[nStart
];
4715 /* Find end of candidate */
4716 if (*candidate_min
>= 0) {
4717 while (nStart
< item
->member
.run
.strText
->nLen
&&
4718 (isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4719 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]) ||
4720 (!foundColon
&& item
->member
.run
.strText
->szData
[nStart
] == ':') )) {
4721 if (item
->member
.run
.strText
->szData
[nStart
] == ':') foundColon
= TRUE
;
4722 lastAcceptedChar
= item
->member
.run
.strText
->szData
[nStart
];
4725 if (nStart
< item
->member
.run
.strText
->nLen
&&
4726 !(isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4727 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]) )) {
4728 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4730 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4735 /* End of paragraph: skip it if before candidate span, or terminates
4736 current active span */
4737 if (*candidate_min
>= 0) {
4738 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
4739 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4744 /* Reaching this point means no span was found, so get next span */
4745 next_item
= ME_FindItemFwd(item
, diRun
);
4747 if (*candidate_min
>= 0) {
4748 /* There are no further runs, so take end of text as end of candidate */
4749 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4750 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4755 para
= ME_GetParagraph(item
);
4760 if (*candidate_min
>= 0) {
4761 /* There are no further runs, so take end of text as end of candidate */
4762 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4763 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4771 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4773 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, int sel_min
, int sel_max
)
4779 /* Code below depends on these being in decreasing length order! */
4793 LPWSTR bufferW
= NULL
;
4797 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4798 assert(sel_min
<= sel_max
);
4799 for (i
= 0; i
< sizeof(prefixes
) / sizeof(struct prefix_s
); i
++)
4801 if (sel_max
- sel_min
< prefixes
[i
].length
) continue;
4802 if (bufferW
== NULL
) {
4803 bufferW
= heap_alloc((sel_max
- sel_min
+ 1) * sizeof(WCHAR
));
4805 ME_GetTextW(editor
, bufferW
, sel_min
, min(sel_max
- sel_min
, lstrlenA(prefixes
[i
].text
)), 0);
4806 MultiByteToWideChar(CP_ACP
, 0, prefixes
[i
].text
, -1, bufW
, 32);
4807 if (!lstrcmpW(bufW
, bufferW
))
4818 * This proc walks through the indicated selection and evaluates whether each
4819 * section identified by ME_FindNextURLCandidate and in-between sections have
4820 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4821 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4823 * Returns TRUE if at least one section was modified.
4825 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, int sel_min
, int sel_max
)
4827 BOOL modified
= FALSE
;
4830 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
4832 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4839 if (ME_FindNextURLCandidate(editor
, sel_min
, sel_max
, &cMin
, &cMax
))
4841 /* Section before candidate is not an URL */
4842 beforeURL
[0] = sel_min
;
4843 beforeURL
[1] = cMin
;
4845 if (ME_IsCandidateAnURL(editor
, cMin
, cMax
))
4847 inURL
[0] = cMin
; inURL
[1] = cMax
;
4851 beforeURL
[1] = cMax
;
4852 inURL
[0] = inURL
[1] = -1;
4858 /* No more candidates until end of selection */
4859 beforeURL
[0] = sel_min
;
4860 beforeURL
[1] = sel_max
;
4861 inURL
[0] = inURL
[1] = -1;
4865 if (beforeURL
[0] < beforeURL
[1])
4867 /* CFE_LINK effect should be consistently unset */
4868 link
.cbSize
= sizeof(link
);
4869 ME_GetCharFormat(editor
, beforeURL
[0], beforeURL
[1], &link
);
4870 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
4872 /* CFE_LINK must be unset from this range */
4873 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4874 link
.cbSize
= sizeof(link
);
4875 link
.dwMask
= CFM_LINK
;
4877 ME_SetCharFormat(editor
, beforeURL
[0], beforeURL
[1] - beforeURL
[0], &link
);
4881 if (inURL
[0] < inURL
[1])
4883 /* CFE_LINK effect should be consistently set */
4884 link
.cbSize
= sizeof(link
);
4885 ME_GetCharFormat(editor
, inURL
[0], inURL
[1], &link
);
4886 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
4888 /* CFE_LINK must be set on this range */
4889 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4890 link
.cbSize
= sizeof(link
);
4891 link
.dwMask
= CFM_LINK
;
4892 link
.dwEffects
= CFE_LINK
;
4893 ME_SetCharFormat(editor
, inURL
[0], inURL
[1] - inURL
[0], &link
);
4897 } while (sel_min
< sel_max
);