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 RichEdit20W
[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
247 static const WCHAR RichEdit50W
[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
248 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
249 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
250 static HCURSOR hLeft
;
253 HANDLE me_heap
= NULL
;
255 static BOOL ME_ListBoxRegistered
= FALSE
;
256 static BOOL ME_ComboBoxRegistered
= FALSE
;
258 static inline int is_version_nt(void)
260 return !(GetVersion() & 0x80000000);
263 static ME_TextBuffer
*ME_MakeText(void) {
265 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
267 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
268 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
274 p1
->member
.para
.next_para
= p2
;
275 p2
->member
.para
.prev_para
= p1
;
276 p2
->member
.para
.nCharOfs
= 0;
280 buf
->pCharStyle
= NULL
;
286 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
288 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
291 TRACE("%08x %p\n", dwFormat
, stream
);
298 ME_StreamInFill(stream
);
299 if (stream
->editstream
->dwError
)
305 if (!(dwFormat
& SF_UNICODE
))
307 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
308 nWideChars
= MultiByteToWideChar(CP_ACP
, 0, stream
->buffer
, stream
->dwSize
, wszText
, STREAMIN_BUFFER_SIZE
);
313 nWideChars
= stream
->dwSize
>> 1;
314 pText
= (WCHAR
*)stream
->buffer
;
317 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
318 if (stream
->dwSize
== 0)
322 ME_CommitUndo(editor
);
323 ME_UpdateRepaint(editor
);
327 static void ME_ApplyBorderProperties(RTF_Info
*info
,
328 ME_BorderRect
*borderRect
,
329 RTFBorder
*borderDef
)
332 ME_Border
*pBorders
[] = {&borderRect
->top
,
336 for (i
= 0; i
< 4; i
++)
338 RTFColor
*colorDef
= info
->colorList
;
339 pBorders
[i
]->width
= borderDef
[i
].width
;
340 colorNum
= borderDef
[i
].color
;
341 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
342 colorDef
= colorDef
->rtfNextColor
;
344 pBorders
[i
]->colorRef
= RGB(
345 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
346 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
347 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
349 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
353 void ME_RTFCharAttrHook(RTF_Info
*info
)
356 fmt
.cbSize
= sizeof(fmt
);
360 switch(info
->rtfMinor
)
363 /* FIXME add more flags once they're implemented */
364 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
365 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
366 fmt
.yHeight
= 12*20; /* 12pt */
367 fmt
.wWeight
= FW_NORMAL
;
368 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
371 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
372 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
373 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
376 fmt
.dwMask
= CFM_ITALIC
;
377 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
380 fmt
.dwMask
= CFM_UNDERLINETYPE
;
381 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
383 case rtfDotUnderline
:
384 fmt
.dwMask
= CFM_UNDERLINETYPE
;
385 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
388 fmt
.dwMask
= CFM_UNDERLINETYPE
;
389 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
391 case rtfWordUnderline
:
392 fmt
.dwMask
= CFM_UNDERLINETYPE
;
393 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
396 fmt
.dwMask
= CFM_UNDERLINETYPE
;
397 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
400 fmt
.dwMask
= CFM_STRIKEOUT
;
401 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
405 case rtfSubScrShrink
:
406 case rtfSuperScrShrink
:
408 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
409 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
410 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
411 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
414 fmt
.dwMask
= CFM_HIDDEN
;
415 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
418 fmt
.dwMask
= CFM_BACKCOLOR
;
420 if (info
->rtfParam
== 0)
421 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
422 else if (info
->rtfParam
!= rtfNoParam
)
424 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
425 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
429 fmt
.dwMask
= CFM_COLOR
;
431 if (info
->rtfParam
== 0)
432 fmt
.dwEffects
= CFE_AUTOCOLOR
;
433 else if (info
->rtfParam
!= rtfNoParam
)
435 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
437 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
443 if (info
->rtfParam
!= rtfNoParam
)
445 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
448 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
449 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
450 fmt
.bCharSet
= f
->rtfFCharSet
;
451 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
452 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
457 fmt
.dwMask
= CFM_SIZE
;
458 if (info
->rtfParam
!= rtfNoParam
)
459 fmt
.yHeight
= info
->rtfParam
*10;
464 RTFFlushOutputBuffer(info
);
465 /* FIXME too slow ? how come ? */
466 style2
= ME_ApplyStyle(info
->style
, &fmt
);
467 ME_ReleaseStyle(info
->style
);
468 info
->style
= style2
;
469 info
->styleChanged
= TRUE
;
473 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
474 the same tags mean different things in different contexts */
475 void ME_RTFParAttrHook(RTF_Info
*info
)
478 fmt
.cbSize
= sizeof(fmt
);
481 switch(info
->rtfMinor
)
483 case rtfParDef
: /* restores default paragraph attributes */
484 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
485 info
->borderType
= RTFBorderParaLeft
;
486 else /* v1.0 - 3.0 */
487 info
->borderType
= RTFBorderParaTop
;
488 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
489 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
491 /* TODO: numbering, shading */
492 fmt
.wAlignment
= PFA_LEFT
;
494 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
495 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
496 fmt
.wBorderSpace
= 0;
497 fmt
.bLineSpacingRule
= 0;
498 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
499 fmt
.dyLineSpacing
= 0;
500 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
502 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
503 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
506 ME_DisplayItem
*para
;
507 /* We are just after a table row. */
508 RTFFlushOutputBuffer(info
);
509 cursor
= info
->editor
->pCursors
[0];
511 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
512 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
514 /* Since the table row end, no text has been inserted, and the \intbl
515 * control word has not be used. We can confirm that we are not in a
518 info
->tableDef
->tableRowStart
= NULL
;
519 info
->canInheritInTbl
= FALSE
;
522 } else { /* v1.0 - v3.0 */
523 fmt
.dwMask
|= PFM_TABLE
;
524 fmt
.wEffects
&= ~PFE_TABLE
;
528 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
530 while (info
->rtfParam
> info
->nestingLevel
) {
531 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
532 ZeroMemory(tableDef
, sizeof(RTFTable
));
533 tableDef
->parent
= info
->tableDef
;
534 info
->tableDef
= tableDef
;
536 RTFFlushOutputBuffer(info
);
537 if (tableDef
->tableRowStart
&&
538 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
540 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
541 para
= para
->member
.para
.next_para
;
542 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
543 tableDef
->tableRowStart
= para
;
547 cursor
= info
->editor
->pCursors
[0];
548 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
549 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
550 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
553 info
->nestingLevel
++;
555 info
->canInheritInTbl
= FALSE
;
560 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
562 if (info
->nestingLevel
< 1)
567 info
->tableDef
= ALLOC_OBJ(RTFTable
);
568 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
570 tableDef
= info
->tableDef
;
571 RTFFlushOutputBuffer(info
);
572 if (tableDef
->tableRowStart
&&
573 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
575 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
576 para
= para
->member
.para
.next_para
;
577 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
578 tableDef
->tableRowStart
= para
;
582 cursor
= info
->editor
->pCursors
[0];
583 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
584 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
585 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
587 info
->nestingLevel
= 1;
588 info
->canInheritInTbl
= TRUE
;
591 } else { /* v1.0 - v3.0 */
592 fmt
.dwMask
|= PFM_TABLE
;
593 fmt
.wEffects
|= PFE_TABLE
;
598 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
599 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
600 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
601 fmt
.dxOffset
= -info
->rtfParam
;
604 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
605 fmt
.dwMask
= PFM_STARTINDENT
;
606 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
609 fmt
.dwMask
= PFM_RIGHTINDENT
;
610 fmt
.dxRightIndent
= info
->rtfParam
;
614 fmt
.dwMask
= PFM_ALIGNMENT
;
615 fmt
.wAlignment
= PFA_LEFT
;
618 fmt
.dwMask
= PFM_ALIGNMENT
;
619 fmt
.wAlignment
= PFA_RIGHT
;
622 fmt
.dwMask
= PFM_ALIGNMENT
;
623 fmt
.wAlignment
= PFA_CENTER
;
626 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
627 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
631 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
632 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
633 fmt
.dwMask
= PFM_TABSTOPS
;
636 fmt
.dwMask
= PFM_KEEP
;
637 fmt
.wEffects
= PFE_KEEP
;
639 case rtfNoWidowControl
:
640 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
641 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
644 fmt
.dwMask
= PFM_KEEPNEXT
;
645 fmt
.wEffects
= PFE_KEEPNEXT
;
648 fmt
.dwMask
= PFM_SPACEAFTER
;
649 fmt
.dySpaceAfter
= info
->rtfParam
;
652 fmt
.dwMask
= PFM_SPACEBEFORE
;
653 fmt
.dySpaceBefore
= info
->rtfParam
;
655 case rtfSpaceBetween
:
656 fmt
.dwMask
= PFM_LINESPACING
;
657 if ((int)info
->rtfParam
> 0)
659 fmt
.dyLineSpacing
= info
->rtfParam
;
660 fmt
.bLineSpacingRule
= 3;
664 fmt
.dyLineSpacing
= info
->rtfParam
;
665 fmt
.bLineSpacingRule
= 4;
667 case rtfSpaceMultiply
:
668 fmt
.dwMask
= PFM_LINESPACING
;
669 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
670 fmt
.bLineSpacingRule
= 5;
673 fmt
.dwMask
= PFM_NUMBERING
;
674 fmt
.wNumbering
= PFN_BULLET
;
677 fmt
.dwMask
= PFM_NUMBERING
;
678 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
680 case rtfParNumDecimal
:
681 fmt
.dwMask
= PFM_NUMBERING
;
682 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
684 case rtfParNumIndent
:
685 fmt
.dwMask
= PFM_NUMBERINGTAB
;
686 fmt
.wNumberingTab
= info
->rtfParam
;
688 case rtfParNumStartAt
:
689 fmt
.dwMask
= PFM_NUMBERINGSTART
;
690 fmt
.wNumberingStart
= info
->rtfParam
;
693 info
->borderType
= RTFBorderParaLeft
;
694 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
695 if (!(fmt
.dwMask
& PFM_BORDER
))
697 fmt
.wBorderSpace
= 0;
698 fmt
.wBorderWidth
= 1;
702 fmt
.dwMask
= PFM_BORDER
;
705 info
->borderType
= RTFBorderParaRight
;
706 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
707 if (!(fmt
.dwMask
& PFM_BORDER
))
709 fmt
.wBorderSpace
= 0;
710 fmt
.wBorderWidth
= 1;
714 fmt
.dwMask
= PFM_BORDER
;
717 info
->borderType
= RTFBorderParaTop
;
718 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
719 if (!(fmt
.dwMask
& PFM_BORDER
))
721 fmt
.wBorderSpace
= 0;
722 fmt
.wBorderWidth
= 1;
726 fmt
.dwMask
= PFM_BORDER
;
728 case rtfBorderBottom
:
729 info
->borderType
= RTFBorderParaBottom
;
730 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
731 if (!(fmt
.dwMask
& PFM_BORDER
))
733 fmt
.wBorderSpace
= 0;
734 fmt
.wBorderWidth
= 1;
738 fmt
.dwMask
= PFM_BORDER
;
740 case rtfBorderSingle
:
741 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
742 /* we assume that borders have been created before (RTF spec) */
743 fmt
.wBorders
&= ~0x700;
744 fmt
.wBorders
|= 1 << 8;
745 fmt
.dwMask
= PFM_BORDER
;
748 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
749 /* we assume that borders have been created before (RTF spec) */
750 fmt
.wBorders
&= ~0x700;
751 fmt
.wBorders
|= 2 << 8;
752 fmt
.dwMask
= PFM_BORDER
;
754 case rtfBorderShadow
:
755 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
756 /* we assume that borders have been created before (RTF spec) */
757 fmt
.wBorders
&= ~0x700;
758 fmt
.wBorders
|= 10 << 8;
759 fmt
.dwMask
= PFM_BORDER
;
761 case rtfBorderDouble
:
762 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
763 /* we assume that borders have been created before (RTF spec) */
764 fmt
.wBorders
&= ~0x700;
765 fmt
.wBorders
|= 7 << 8;
766 fmt
.dwMask
= PFM_BORDER
;
769 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
770 /* we assume that borders have been created before (RTF spec) */
771 fmt
.wBorders
&= ~0x700;
772 fmt
.wBorders
|= 11 << 8;
773 fmt
.dwMask
= PFM_BORDER
;
777 int borderSide
= info
->borderType
& RTFBorderSideMask
;
778 RTFTable
*tableDef
= info
->tableDef
;
779 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
780 /* we assume that borders have been created before (RTF spec) */
781 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
782 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
785 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
787 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
788 border
->width
= info
->rtfParam
;
791 fmt
.dwMask
= PFM_BORDER
;
795 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
796 /* we assume that borders have been created before (RTF spec) */
797 fmt
.wBorderSpace
= info
->rtfParam
;
798 fmt
.dwMask
= PFM_BORDER
;
802 RTFTable
*tableDef
= info
->tableDef
;
803 int borderSide
= info
->borderType
& RTFBorderSideMask
;
804 int borderType
= info
->borderType
& RTFBorderTypeMask
;
807 case RTFBorderTypePara
:
808 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
810 /* v1.0 - 3.0 treat paragraph and row borders the same. */
811 case RTFBorderTypeRow
:
813 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
816 case RTFBorderTypeCell
:
817 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
818 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
826 RTFFlushOutputBuffer(info
);
827 /* FIXME too slow ? how come ?*/
828 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
832 void ME_RTFTblAttrHook(RTF_Info
*info
)
834 switch (info
->rtfMinor
)
838 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
839 info
->borderType
= 0; /* Not sure */
840 else /* v1.0 - 3.0 */
841 info
->borderType
= RTFBorderRowTop
;
842 if (!info
->tableDef
) {
843 info
->tableDef
= ME_MakeTableDef(info
->editor
);
845 ME_InitTableDef(info
->editor
, info
->tableDef
);
854 info
->tableDef
= ME_MakeTableDef(info
->editor
);
856 cellNum
= info
->tableDef
->numCellsDefined
;
857 if (cellNum
>= MAX_TABLE_CELLS
)
859 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
860 if (cellNum
< MAX_TAB_STOPS
) {
861 /* Tab stops were used to store cell positions before v4.1 but v4.1
862 * still seems to set the tabstops without using them. */
863 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
864 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
865 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
866 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
868 info
->tableDef
->numCellsDefined
++;
872 info
->borderType
= RTFBorderRowTop
;
875 info
->borderType
= RTFBorderRowLeft
;
877 case rtfRowBordBottom
:
878 info
->borderType
= RTFBorderRowBottom
;
880 case rtfRowBordRight
:
881 info
->borderType
= RTFBorderRowRight
;
884 info
->borderType
= RTFBorderCellTop
;
886 case rtfCellBordLeft
:
887 info
->borderType
= RTFBorderCellLeft
;
889 case rtfCellBordBottom
:
890 info
->borderType
= RTFBorderCellBottom
;
892 case rtfCellBordRight
:
893 info
->borderType
= RTFBorderCellRight
;
897 info
->tableDef
->gapH
= info
->rtfParam
;
901 info
->tableDef
->leftEdge
= info
->rtfParam
;
906 void ME_RTFSpecialCharHook(RTF_Info
*info
)
908 RTFTable
*tableDef
= info
->tableDef
;
909 switch (info
->rtfMinor
)
912 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
914 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
918 RTFFlushOutputBuffer(info
);
919 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
920 if (tableDef
->tableRowStart
)
922 if (!info
->nestingLevel
&&
923 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
925 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
926 para
= para
->member
.para
.next_para
;
927 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
928 tableDef
->tableRowStart
= para
;
929 info
->nestingLevel
= 1;
931 ME_InsertTableCellFromCursor(info
->editor
);
933 } else { /* v1.0 - v3.0 */
934 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
935 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
936 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
937 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
940 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
941 tableDef
->numCellsInserted
++;
946 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
948 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
951 ME_DisplayItem
*para
, *cell
, *run
;
956 RTFFlushOutputBuffer(info
);
957 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
958 if (!tableDef
->tableRowStart
)
960 if (!info
->nestingLevel
&&
961 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
963 para
= tableDef
->tableRowStart
;
964 para
= para
->member
.para
.next_para
;
965 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
966 tableDef
->tableRowStart
= para
;
967 info
->nestingLevel
++;
969 para
= tableDef
->tableRowStart
;
970 cell
= ME_FindItemFwd(para
, diCell
);
971 assert(cell
&& !cell
->member
.cell
.prev_cell
);
972 if (tableDef
->numCellsDefined
< 1)
974 /* 2000 twips appears to be the cell size that native richedit uses
975 * when no cell sizes are specified. */
976 const int defaultCellSize
= 2000;
977 int nRightBoundary
= defaultCellSize
;
978 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
979 while (cell
->member
.cell
.next_cell
) {
980 cell
= cell
->member
.cell
.next_cell
;
981 nRightBoundary
+= defaultCellSize
;
982 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
984 para
= ME_InsertTableCellFromCursor(info
->editor
);
985 cell
= para
->member
.para
.pCell
;
986 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
988 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
990 RTFCell
*cellDef
= &tableDef
->cells
[i
];
991 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
992 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
994 cell
= cell
->member
.cell
.next_cell
;
997 para
= ME_InsertTableCellFromCursor(info
->editor
);
998 cell
= para
->member
.para
.pCell
;
1001 /* Cell for table row delimiter is empty */
1002 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1005 run
= ME_FindItemFwd(cell
, diRun
);
1006 if (info
->editor
->pCursors
[0].pRun
!= run
||
1007 info
->editor
->pCursors
[0].nOffset
)
1010 /* Delete inserted cells that aren't defined. */
1011 info
->editor
->pCursors
[1].pRun
= run
;
1012 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1013 info
->editor
->pCursors
[1].nOffset
= 0;
1014 nOfs
= ME_GetCursorOfs(info
->editor
, 1);
1015 nChars
= ME_GetCursorOfs(info
->editor
, 0) - nOfs
;
1016 ME_InternalDeleteText(info
->editor
, nOfs
, nChars
, TRUE
);
1019 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1020 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1021 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1022 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1024 info
->nestingLevel
--;
1025 if (!info
->nestingLevel
)
1027 if (info
->canInheritInTbl
) {
1028 tableDef
->tableRowStart
= para
;
1030 while (info
->tableDef
) {
1031 tableDef
= info
->tableDef
;
1032 info
->tableDef
= tableDef
->parent
;
1033 heap_free(tableDef
);
1037 info
->tableDef
= tableDef
->parent
;
1038 heap_free(tableDef
);
1040 } else { /* v1.0 - v3.0 */
1042 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1043 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1044 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1045 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1047 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1049 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1052 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1053 tableDef
->numCellsInserted
++;
1055 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1056 if (!tableDef
->numCellsDefined
)
1057 pFmt
->wEffects
&= ~PFE_TABLE
;
1058 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1059 tableDef
->numCellsInserted
= 0;
1065 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1066 ME_DisplayItem
*para
;
1068 RTFFlushOutputBuffer(info
);
1069 para
= info
->editor
->pCursors
[0].pPara
;
1070 pFmt
= para
->member
.para
.pFmt
;
1071 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1073 /* rtfPar is treated like a space within a table. */
1074 info
->rtfClass
= rtfText
;
1075 info
->rtfMajor
= ' ';
1077 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1078 tableDef
->numCellsInserted
= 0;
1084 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1087 LPOLEOBJECT lpObject
= NULL
;
1088 LPSTORAGE lpStorage
= NULL
;
1089 LPOLECLIENTSITE lpClientSite
= NULL
;
1090 LPDATAOBJECT lpDataObject
= NULL
;
1091 LPOLECACHE lpOleCache
= NULL
;
1100 stgm
.tymed
= TYMED_ENHMF
;
1101 stgm
.u
.hEnhMetaFile
= hemf
;
1102 fm
.cfFormat
= CF_ENHMETAFILE
;
1106 stgm
.tymed
= TYMED_GDI
;
1107 stgm
.u
.hBitmap
= hbmp
;
1108 fm
.cfFormat
= CF_BITMAP
;
1110 stgm
.pUnkForRelease
= NULL
;
1113 fm
.dwAspect
= DVASPECT_CONTENT
;
1115 fm
.tymed
= stgm
.tymed
;
1117 if (!info
->lpRichEditOle
)
1119 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1122 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1124 /* FIXME: enable it when rich-edit properly implements this method */
1125 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1126 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1128 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1129 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1130 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1131 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1132 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1136 reobject
.cbStruct
= sizeof(reobject
);
1137 reobject
.cp
= REO_CP_SELECTION
;
1138 reobject
.clsid
= clsid
;
1139 reobject
.poleobj
= lpObject
;
1140 reobject
.pstg
= lpStorage
;
1141 reobject
.polesite
= lpClientSite
;
1142 /* convert from twips to .01 mm */
1143 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1144 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1145 reobject
.dvaspect
= DVASPECT_CONTENT
;
1146 reobject
.dwFlags
= 0; /* FIXME */
1147 reobject
.dwUser
= 0;
1149 /* FIXME: could be simpler */
1150 ret
= IRichEditOle_InsertObject(info
->lpRichEditOle
, &reobject
) == S_OK
;
1153 if (lpObject
) IOleObject_Release(lpObject
);
1154 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1155 if (lpStorage
) IStorage_Release(lpStorage
);
1156 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1157 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1162 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1165 BYTE
* buffer
= NULL
;
1166 unsigned bufsz
, bufidx
;
1172 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1175 if (info
->rtfClass
== rtfEOF
)
1178 /* fetch picture type */
1179 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1181 mfp
.mm
= info
->rtfParam
;
1184 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1186 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1189 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1191 gfx
= gfx_enhmetafile
;
1195 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1199 /* fetch picture attributes */
1203 if (info
->rtfClass
== rtfEOF
)
1205 if (info
->rtfClass
== rtfText
)
1207 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1209 ERR("Expected picture attribute (%d %d)\n",
1210 info
->rtfClass
, info
->rtfMajor
);
1213 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1215 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1217 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1219 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1221 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1222 sz
.cx
= info
->rtfParam
;
1223 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1224 sz
.cy
= info
->rtfParam
;
1226 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1228 /* fetch picture data */
1231 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1232 val
= info
->rtfMajor
;
1233 for (flip
= TRUE
;; flip
= !flip
)
1236 if (info
->rtfClass
== rtfEOF
)
1238 HeapFree(GetProcessHeap(), 0, buffer
);
1239 return; /* Warn ?? */
1241 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1243 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1246 if (bufidx
>= bufsz
&&
1247 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1249 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1252 val
= info
->rtfMajor
;
1254 if (flip
) FIXME("wrong hex string\n");
1258 case gfx_enhmetafile
:
1259 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1260 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1263 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1264 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1268 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1270 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1272 /* not quite right, especially for bitfields type of compression */
1273 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1274 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1275 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1276 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1277 bi
, DIB_RGB_COLORS
)))
1278 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1285 HeapFree(GetProcessHeap(), 0, buffer
);
1286 RTFRouteToken (info
); /* feed "}" back to router */
1289 HeapFree(GetProcessHeap(), 0, buffer
);
1291 RTFRouteToken(info
); /* feed "}" back to router */
1294 /* for now, lookup the \result part and use it, whatever the object */
1295 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1300 if (info
->rtfClass
== rtfEOF
)
1302 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1304 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1307 if (info
->rtfClass
== rtfEOF
)
1309 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1313 while (RTFGetToken (info
) != rtfEOF
)
1315 if (info
->rtfClass
== rtfGroup
)
1317 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1318 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1320 RTFRouteToken(info
);
1323 else RTFSkipGroup(info
);
1326 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1328 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1332 RTFRouteToken(info
); /* feed "}" back to router */
1335 static void ME_RTFReadHook(RTF_Info
*info
)
1337 switch(info
->rtfClass
)
1340 switch(info
->rtfMajor
)
1343 if (info
->stackTop
< maxStack
) {
1344 info
->stack
[info
->stackTop
].fmt
= info
->style
->fmt
;
1345 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1346 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1349 info
->styleChanged
= FALSE
;
1354 RTFFlushOutputBuffer(info
);
1356 if (info
->stackTop
<=0) {
1357 info
->rtfClass
= rtfEOF
;
1360 assert(info
->stackTop
>= 0);
1361 if (info
->styleChanged
)
1363 /* FIXME too slow ? how come ? */
1364 s
= ME_ApplyStyle(info
->style
, &info
->stack
[info
->stackTop
].fmt
);
1365 ME_ReleaseStyle(info
->style
);
1367 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1368 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1378 ME_StreamInFill(ME_InStream
*stream
)
1380 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1381 (BYTE
*)stream
->buffer
,
1382 sizeof(stream
->buffer
),
1383 (LONG
*)&stream
->dwSize
);
1387 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1391 int from
, to
, to2
, nUndoMode
;
1392 int nEventMask
= editor
->nEventMask
;
1393 ME_InStream inStream
;
1394 BOOL invalidRTF
= FALSE
;
1396 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1397 editor
->nEventMask
= 0;
1399 ME_GetSelection(editor
, &from
, &to
);
1400 if ((format
& SFF_SELECTION
) && (editor
->mode
& TM_RICHTEXT
)) {
1401 style
= ME_GetSelectionInsertStyle(editor
);
1403 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
1405 /* Don't insert text at the end of the table row */
1406 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1407 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1408 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1410 para
= para
->member
.para
.next_para
;
1411 editor
->pCursors
[0].pPara
= para
;
1412 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1413 editor
->pCursors
[0].nOffset
= 0;
1415 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1417 para
= para
->member
.para
.next_para
;
1418 editor
->pCursors
[0].pPara
= para
;
1419 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1420 editor
->pCursors
[0].nOffset
= 0;
1422 editor
->pCursors
[1] = editor
->pCursors
[0];
1423 } else { /* v1.0 - 3.0 */
1424 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1425 ME_IsInTable(editor
->pCursors
[0].pRun
))
1430 style
= editor
->pBuffer
->pDefaultStyle
;
1431 ME_AddRefStyle(style
);
1432 ME_SetSelection(editor
, 0, 0);
1433 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
1435 ME_ClearTempStyle(editor
);
1436 ME_SetDefaultParaFormat(editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1440 /* Back up undo mode to a local variable */
1441 nUndoMode
= editor
->nUndoMode
;
1443 /* Only create an undo if SFF_SELECTION is set */
1444 if (!(format
& SFF_SELECTION
))
1445 editor
->nUndoMode
= umIgnore
;
1447 inStream
.editstream
= stream
;
1448 inStream
.editstream
->dwError
= 0;
1449 inStream
.dwSize
= 0;
1450 inStream
.dwUsed
= 0;
1452 if (format
& SF_RTF
)
1454 /* Check if it's really RTF, and if it is not, use plain text */
1455 ME_StreamInFill(&inStream
);
1456 if (!inStream
.editstream
->dwError
)
1458 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1459 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1462 inStream
.editstream
->dwError
= -16;
1467 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1469 if (format
& SF_RTF
) {
1470 /* setup the RTF parser */
1471 memset(&parser
, 0, sizeof parser
);
1472 RTFSetEditStream(&parser
, &inStream
);
1473 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1474 parser
.editor
= editor
;
1475 parser
.style
= style
;
1476 WriterInit(&parser
);
1478 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1479 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1480 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1481 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1483 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1484 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1488 /* do the parsing */
1490 RTFFlushOutputBuffer(&parser
);
1491 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1492 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1493 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1495 /* Delete any incomplete table row at the end of the rich text. */
1497 ME_DisplayItem
*pCell
;
1498 ME_DisplayItem
*para
;
1500 parser
.rtfMinor
= rtfRow
;
1501 /* Complete the table row before deleting it.
1502 * By doing it this way we will have the current paragraph format set
1503 * properly to reflect that is not in the complete table, and undo items
1504 * will be added for this change to the current paragraph format. */
1505 if (parser
.nestingLevel
> 0)
1507 while (parser
.nestingLevel
> 1)
1508 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1509 para
= parser
.tableDef
->tableRowStart
;
1510 ME_RTFSpecialCharHook(&parser
);
1512 para
= parser
.tableDef
->tableRowStart
;
1513 ME_RTFSpecialCharHook(&parser
);
1514 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1515 para
= para
->member
.para
.next_para
;
1517 pCell
= para
->member
.para
.pCell
;
1519 editor
->pCursors
[1].pPara
= para
;
1520 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1521 editor
->pCursors
[1].nOffset
= 0;
1522 nOfs
= ME_GetCursorOfs(editor
, 1);
1523 nChars
= ME_GetCursorOfs(editor
, 0) - nOfs
;
1524 ME_InternalDeleteText(editor
, nOfs
, nChars
, TRUE
);
1525 if (parser
.tableDef
)
1526 parser
.tableDef
->tableRowStart
= NULL
;
1529 ME_CheckTablesForCorruption(editor
);
1530 RTFDestroy(&parser
);
1531 if (parser
.lpRichEditOle
)
1532 IRichEditOle_Release(parser
.lpRichEditOle
);
1534 if (!inStream
.editstream
->dwError
&& parser
.stackTop
> 0)
1535 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1537 /* Remove last line break, as mandated by tests. This is not affected by
1538 CR/LF counters, since RTF streaming presents only \para tokens, which
1539 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1543 ME_GetSelection(editor
, &newfrom
, &newto
);
1544 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1545 WCHAR lastchar
[3] = {'\0', '\0'};
1546 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1548 ME_GetTextW(editor
, lastchar
, newto
- linebreakSize
, linebreakSize
, 0);
1549 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1550 ME_InternalDeleteText(editor
, newto
- linebreakSize
, linebreakSize
, FALSE
);
1555 style
= parser
.style
;
1557 else if (format
& SF_TEXT
)
1558 ME_StreamInText(editor
, format
, &inStream
, style
);
1560 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1561 ME_GetSelection(editor
, &to
, &to2
);
1562 /* put the cursor at the top */
1563 if (!(format
& SFF_SELECTION
))
1564 ME_SetSelection(editor
, 0, 0);
1567 /* Restore saved undo mode */
1568 editor
->nUndoMode
= nUndoMode
;
1570 /* even if we didn't add an undo, we need to commit anything on the stack */
1571 ME_CommitUndo(editor
);
1573 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1574 if (!(format
& SFF_SELECTION
))
1575 ME_EmptyUndoStack(editor
);
1577 ME_ReleaseStyle(style
);
1578 editor
->nEventMask
= nEventMask
;
1579 ME_UpdateRepaint(editor
);
1580 if (!(format
& SFF_SELECTION
)) {
1581 ME_ClearTempStyle(editor
);
1583 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1584 ME_MoveCaret(editor
);
1585 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1586 ME_SendSelChange(editor
);
1587 ME_SendRequestResize(editor
, FALSE
);
1593 typedef struct tagME_RTFStringStreamStruct
1598 } ME_RTFStringStreamStruct
;
1600 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1602 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1605 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1606 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1607 pStruct
->pos
+= count
;
1613 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1616 ME_RTFStringStreamStruct data
;
1618 data
.string
= string
;
1619 data
.length
= strlen(string
);
1621 es
.dwCookie
= (DWORD_PTR
)&data
;
1622 es
.pfnCallback
= ME_ReadFromRTFString
;
1623 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1628 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1630 const int nLen
= lstrlenW(text
);
1631 const int nTextLen
= ME_GetTextLength(editor
);
1634 ME_DisplayItem
*item
;
1635 ME_DisplayItem
*para
;
1636 WCHAR wLastChar
= ' ';
1638 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1639 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1641 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1642 FIXME("Flags 0x%08x not implemented\n",
1643 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1646 if (chrg
->cpMax
== -1)
1649 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1651 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1652 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1657 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1658 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1662 chrgText
->cpMin
= -1;
1663 chrgText
->cpMax
= -1;
1668 /* when searching up, if cpMin < cpMax, then instead of searching
1669 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1670 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1671 * case, it is always bigger than cpMin.
1673 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1677 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1678 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1684 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1687 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1691 if (flags
& FR_DOWN
) /* Forward search */
1693 /* If possible, find the character before where the search starts */
1694 if ((flags
& FR_WHOLEWORD
) && nMin
)
1696 ME_RunOfsFromCharOfs(editor
, nMin
- 1, NULL
, &item
, &nStart
);
1697 wLastChar
= item
->member
.run
.strText
->szData
[nStart
];
1700 ME_RunOfsFromCharOfs(editor
, nMin
, ¶
, &item
, &nStart
);
1703 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
+ nLen
<= nMax
)
1705 ME_DisplayItem
*pCurItem
= item
;
1706 int nCurStart
= nStart
;
1709 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
], text
[nMatched
], (flags
& FR_MATCHCASE
)))
1711 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1715 if (nMatched
== nLen
)
1717 ME_DisplayItem
*pNextItem
= pCurItem
;
1718 int nNextStart
= nCurStart
;
1721 /* Check to see if next character is a whitespace */
1722 if (flags
& FR_WHOLEWORD
)
1724 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.strText
->nLen
)
1726 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1727 nNextStart
= -nMatched
;
1731 wNextChar
= pNextItem
->member
.run
.strText
->szData
[nNextStart
+ nMatched
];
1735 if (isalnumW(wNextChar
))
1739 nStart
+= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
1742 chrgText
->cpMin
= nStart
;
1743 chrgText
->cpMax
= nStart
+ nLen
;
1745 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1748 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.strText
->nLen
)
1750 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1751 nCurStart
= -nMatched
;
1755 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
];
1760 if (nStart
== item
->member
.run
.strText
->nLen
)
1762 item
= ME_FindItemFwd(item
, diRun
);
1763 para
= ME_GetParagraph(item
);
1768 else /* Backward search */
1770 /* If possible, find the character after where the search ends */
1771 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1773 ME_RunOfsFromCharOfs(editor
, nMax
+ 1, NULL
, &item
, &nEnd
);
1774 wLastChar
= item
->member
.run
.strText
->szData
[nEnd
];
1777 ME_RunOfsFromCharOfs(editor
, nMax
, ¶
, &item
, &nEnd
);
1780 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nEnd
- nLen
>= nMin
)
1782 ME_DisplayItem
*pCurItem
= item
;
1788 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1789 nCurEnd
= pCurItem
->member
.run
.strText
->nLen
+ nMatched
;
1792 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1], text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
)))
1794 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1798 if (nMatched
== nLen
)
1800 ME_DisplayItem
*pPrevItem
= pCurItem
;
1801 int nPrevEnd
= nCurEnd
;
1804 /* Check to see if previous character is a whitespace */
1805 if (flags
& FR_WHOLEWORD
)
1807 if (nPrevEnd
- nMatched
== 0)
1809 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1811 nPrevEnd
= pPrevItem
->member
.run
.strText
->nLen
+ nMatched
;
1815 wPrevChar
= pPrevItem
->member
.run
.strText
->szData
[nPrevEnd
- nMatched
- 1];
1819 if (isalnumW(wPrevChar
))
1823 nStart
= ME_GetParagraph(pCurItem
)->member
.para
.nCharOfs
1824 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1827 chrgText
->cpMin
= nStart
;
1828 chrgText
->cpMax
= nStart
+ nLen
;
1830 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1833 if (nCurEnd
- nMatched
== 0)
1835 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1836 /* Don't care about pCurItem becoming NULL here; it's already taken
1837 * care of in the exterior loop condition */
1838 nCurEnd
= pCurItem
->member
.run
.strText
->nLen
+ nMatched
;
1842 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1];
1849 item
= ME_FindItemBack(item
, diRun
);
1850 para
= ME_GetParagraph(item
);
1851 nEnd
= item
->member
.run
.strText
->nLen
;
1855 TRACE("not found\n");
1857 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1861 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1863 int nStart
, nCount
; /* in chars */
1865 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1866 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1868 if (ex
->flags
& GT_SELECTION
)
1870 ME_GetSelection(editor
, &nStart
, &nCount
);
1876 nCount
= 0x7fffffff;
1878 if (ex
->codepage
== 1200)
1880 nCount
= min(nCount
, ex
->cb
/ sizeof(WCHAR
) - 1);
1881 return ME_GetTextW(editor
, (LPWSTR
)pText
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
1885 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1886 we can just take a bigger buffer? :)
1887 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1888 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1890 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1892 DWORD buflen
= ex
->cb
;
1896 nCount
= min(nCount
, ex
->cb
- 1);
1897 buffer
= heap_alloc((crlfmul
*nCount
+ 1) * sizeof(WCHAR
));
1899 buflen
= ME_GetTextW(editor
, buffer
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
1900 rc
= WideCharToMultiByte(ex
->codepage
, flags
, buffer
, buflen
+1,
1901 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1902 if (rc
) rc
--; /* do not count 0 terminator */
1909 static int ME_GetTextRange(ME_TextEditor
*editor
, TEXTRANGEW
*rng
, BOOL unicode
)
1912 return ME_GetTextW(editor
, rng
->lpstrText
, rng
->chrg
.cpMin
,
1913 rng
->chrg
.cpMax
-rng
->chrg
.cpMin
, 0);
1916 int nLen
= rng
->chrg
.cpMax
-rng
->chrg
.cpMin
;
1917 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
1918 int nChars
= ME_GetTextW(editor
, p
, rng
->chrg
.cpMin
, nLen
, 0);
1919 /* FIXME this is a potential security hole (buffer overrun)
1920 if you know more about wchar->mbyte conversion please explain
1922 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)rng
->lpstrText
, nLen
+1, NULL
, NULL
);
1928 typedef struct tagME_GlobalDestStruct
1932 } ME_GlobalDestStruct
;
1934 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1936 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1941 pDest
= (WORD
*)lpBuff
;
1942 pSrc
= GlobalLock(pData
->hData
);
1943 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1944 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1946 pData
->nLength
+= i
;
1948 GlobalUnlock(pData
->hData
);
1952 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1954 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
1959 pSrc
= GlobalLock(pData
->hData
);
1960 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
1961 pDest
[i
] = pSrc
[pData
->nLength
+i
];
1963 pData
->nLength
+= i
;
1965 GlobalUnlock(pData
->hData
);
1969 static BOOL
ME_Paste(ME_TextEditor
*editor
)
1973 ME_GlobalDestStruct gds
;
1974 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
1977 if (IsClipboardFormatAvailable(nRTFFormat
))
1978 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
1979 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
1980 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
1984 if (!OpenClipboard(editor
->hWnd
))
1986 gds
.hData
= GetClipboardData(cf
);
1988 es
.dwCookie
= (DWORD_PTR
)&gds
;
1989 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
1990 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
1996 static BOOL
ME_Copy(ME_TextEditor
*editor
, CHARRANGE
*range
)
1998 LPDATAOBJECT dataObj
= NULL
;
2001 if (editor
->cPasswordMask
)
2002 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2004 if(editor
->lpOleCallback
)
2005 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, range
, RECO_COPY
, &dataObj
);
2006 if(FAILED(hr
) || !dataObj
)
2007 hr
= ME_GetDataObject(editor
, range
, &dataObj
);
2009 hr
= OleSetClipboard(dataObj
);
2010 IDataObject_Release(dataObj
);
2012 return SUCCEEDED(hr
) != 0;
2015 /* helper to send a msg filter notification */
2017 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2021 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2023 msgf
.wParam
= *wParam
;
2024 msgf
.lParam
= *lParam
;
2025 if (ITextHost_TxNotify(editor
->texthost
, msgf
.nmhdr
.code
, &msgf
) == S_OK
)
2027 *wParam
= msgf
.wParam
;
2028 *lParam
= msgf
.lParam
;
2029 msgf
.wParam
= *wParam
;
2034 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2036 ME_DisplayItem
*startPara
, *endPara
;
2037 ME_DisplayItem
*prev_para
;
2040 ME_GetSelection(editor
, &from
, &to
);
2042 /* Find paragraph previous to the one that contains start cursor */
2043 ME_RunOfsFromCharOfs(editor
, from
, &startPara
, NULL
, NULL
);
2044 prev_para
= startPara
->member
.para
.prev_para
;
2045 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2047 /* Find paragraph that contains end cursor */
2048 ME_RunOfsFromCharOfs(editor
, to
, &endPara
, NULL
, NULL
);
2049 endPara
= endPara
->member
.para
.next_para
;
2051 ME_UpdateLinkAttribute(editor
,
2052 startPara
->member
.para
.nCharOfs
,
2053 endPara
->member
.para
.nCharOfs
);
2057 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2059 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2060 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2062 if (editor
->bMouseCaptured
)
2064 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2065 editor
->nSelectionType
= stPosition
;
2073 editor
->nUDArrowX
= -1;
2079 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2080 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2084 editor
->nUDArrowX
= -1;
2085 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2086 if (editor
->styleFlags
& ES_READONLY
)
2088 if (ME_IsSelection(editor
))
2090 ME_DeleteSelection(editor
);
2091 ME_CommitUndo(editor
);
2093 else if (nKey
== VK_DELETE
)
2095 /* Delete stops group typing.
2096 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2097 ME_DeleteTextAtCursor(editor
, 1, 1);
2098 ME_CommitUndo(editor
);
2100 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2102 BOOL bDeletionSucceeded
;
2103 /* Backspace can be grouped for a single undo */
2104 ME_ContinueCoalescingTransaction(editor
);
2105 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2106 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2107 /* Deletion was prevented so the cursor is moved back to where it was.
2108 * (e.g. this happens when trying to delete cell boundaries)
2110 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2112 ME_CommitCoalescingUndo(editor
);
2116 ME_MoveCursorFromTableRowStartParagraph(editor
);
2117 ME_UpdateSelectionLinkAttribute(editor
);
2118 ME_UpdateRepaint(editor
);
2119 ME_SendRequestResize(editor
, FALSE
);
2122 if (editor
->styleFlags
& ES_MULTILINE
)
2124 ME_Cursor cursor
= editor
->pCursors
[0];
2125 ME_DisplayItem
*para
= cursor
.pPara
;
2127 const WCHAR endl
= '\r';
2130 if (editor
->styleFlags
& ES_READONLY
) {
2131 MessageBeep(MB_ICONERROR
);
2135 ME_GetSelection(editor
, &from
, &to
);
2136 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2138 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2139 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2140 /* Add a new table row after this row. */
2141 para
= ME_AppendTableRow(editor
, para
);
2142 para
= para
->member
.para
.next_para
;
2143 editor
->pCursors
[0].pPara
= para
;
2144 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2145 editor
->pCursors
[0].nOffset
= 0;
2146 editor
->pCursors
[1] = editor
->pCursors
[0];
2147 ME_CommitUndo(editor
);
2148 ME_CheckTablesForCorruption(editor
);
2149 ME_UpdateRepaint(editor
);
2152 else if (para
== editor
->pCursors
[1].pPara
&&
2153 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2154 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2155 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2157 /* Insert a newline before the table. */
2158 para
= para
->member
.para
.prev_para
;
2159 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2160 editor
->pCursors
[0].pPara
= para
;
2161 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2162 editor
->pCursors
[1] = editor
->pCursors
[0];
2163 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2164 editor
->pCursors
[0].pRun
->member
.run
.style
);
2165 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2166 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2167 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2168 editor
->pCursors
[0].pPara
= para
;
2169 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2170 editor
->pCursors
[1] = editor
->pCursors
[0];
2171 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2172 ME_CommitCoalescingUndo(editor
);
2173 ME_CheckTablesForCorruption(editor
);
2174 ME_UpdateRepaint(editor
);
2177 } else { /* v1.0 - 3.0 */
2178 ME_DisplayItem
*para
= cursor
.pPara
;
2179 if (ME_IsInTable(para
))
2181 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2184 ME_ContinueCoalescingTransaction(editor
);
2185 para
= ME_AppendTableRow(editor
, para
);
2186 editor
->pCursors
[0].pPara
= para
;
2187 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2188 editor
->pCursors
[0].nOffset
= 0;
2189 editor
->pCursors
[1] = editor
->pCursors
[0];
2190 ME_CommitCoalescingUndo(editor
);
2191 ME_UpdateRepaint(editor
);
2195 ME_ContinueCoalescingTransaction(editor
);
2196 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2197 !ME_IsInTable(para
->member
.para
.prev_para
))
2199 /* Insert newline before table */
2200 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2202 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2203 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2205 editor
->pCursors
[0].nOffset
= 0;
2206 editor
->pCursors
[1] = editor
->pCursors
[0];
2207 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2208 editor
->pCursors
[0].pRun
->member
.run
.style
);
2210 editor
->pCursors
[1] = editor
->pCursors
[0];
2211 para
= ME_AppendTableRow(editor
, para
);
2212 editor
->pCursors
[0].pPara
= para
;
2213 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2214 editor
->pCursors
[0].nOffset
= 0;
2215 editor
->pCursors
[1] = editor
->pCursors
[0];
2217 ME_CommitCoalescingUndo(editor
);
2218 ME_UpdateRepaint(editor
);
2224 style
= ME_GetInsertStyle(editor
, 0);
2225 ME_SaveTempStyle(editor
);
2226 ME_ContinueCoalescingTransaction(editor
);
2228 ME_InsertEndRowFromCursor(editor
, 0);
2230 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2231 ME_ReleaseStyle(style
);
2232 ME_CommitCoalescingUndo(editor
);
2235 if (editor
->AutoURLDetect_bEnable
)
2236 ME_UpdateSelectionLinkAttribute(editor
);
2238 ME_UpdateRepaint(editor
);
2246 ME_SetSelection(editor
, 0, -1);
2252 return ME_Paste(editor
);
2261 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
2262 result
= ME_Copy(editor
, &range
);
2263 if (result
&& nKey
== 'X')
2265 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
, FALSE
);
2266 ME_CommitUndo(editor
);
2267 ME_UpdateRepaint(editor
);
2288 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2289 editor
->nUDArrowX
= -1;
2296 chf
.cbSize
= sizeof(chf
);
2298 ME_GetSelectionCharFormat(editor
, &chf
);
2299 ME_DumpStyleToBuf(&chf
, buf
);
2300 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2304 ME_CheckCharOffsets(editor
);
2311 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2312 LPARAM flags
, BOOL unicode
)
2316 if (editor
->bMouseCaptured
)
2320 wstr
= (WCHAR
)charCode
;
2323 CHAR charA
= charCode
;
2324 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2327 if (editor
->styleFlags
& ES_READONLY
) {
2328 MessageBeep(MB_ICONERROR
);
2329 return 0; /* FIXME really 0 ? */
2332 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2334 ME_Cursor cursor
= editor
->pCursors
[0];
2335 ME_DisplayItem
*para
= cursor
.pPara
;
2337 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2338 ME_GetSelection(editor
, &from
, &to
);
2340 /* v4.1 allows tabs to be inserted with ctrl key down */
2341 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2343 ME_DisplayItem
*para
;
2344 BOOL bSelectedRow
= FALSE
;
2346 para
= cursor
.pPara
;
2347 if (ME_IsSelection(editor
) &&
2348 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2349 to
== ME_GetCursorOfs(editor
, 0) &&
2350 para
->member
.para
.prev_para
->type
== diParagraph
)
2352 para
= para
->member
.para
.prev_para
;
2353 bSelectedRow
= TRUE
;
2355 if (ME_IsInTable(para
))
2357 ME_TabPressedInTable(editor
, bSelectedRow
);
2358 ME_CommitUndo(editor
);
2361 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2362 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2364 para
= para
->member
.para
.next_para
;
2365 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2366 para
= para
->member
.para
.next_para
;
2367 editor
->pCursors
[0].pPara
= para
;
2368 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2369 editor
->pCursors
[0].nOffset
= 0;
2370 editor
->pCursors
[1] = editor
->pCursors
[0];
2373 } else { /* v1.0 - 3.0 */
2374 if (ME_IsInTable(cursor
.pRun
) &&
2375 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2378 /* Text should not be inserted at the end of the table. */
2383 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2384 /* WM_CHAR is restricted to nTextLimit */
2385 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2387 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2388 ME_SaveTempStyle(editor
);
2389 ME_ContinueCoalescingTransaction(editor
);
2390 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2391 ME_ReleaseStyle(style
);
2392 ME_CommitCoalescingUndo(editor
);
2393 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2396 if (editor
->AutoURLDetect_bEnable
) ME_UpdateSelectionLinkAttribute(editor
);
2398 ME_UpdateRepaint(editor
);
2403 /* Process the message and calculate the new click count.
2405 * returns: The click count if it is mouse down event, else returns 0. */
2406 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2409 static int clickNum
= 0;
2410 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2413 if ((msg
== WM_LBUTTONDBLCLK
) ||
2414 (msg
== WM_RBUTTONDBLCLK
) ||
2415 (msg
== WM_MBUTTONDBLCLK
) ||
2416 (msg
== WM_XBUTTONDBLCLK
))
2418 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2421 if ((msg
== WM_LBUTTONDOWN
) ||
2422 (msg
== WM_RBUTTONDOWN
) ||
2423 (msg
== WM_MBUTTONDOWN
) ||
2424 (msg
== WM_XBUTTONDOWN
))
2426 static MSG prevClickMsg
;
2428 /* Compare the editor instead of the hwnd so that the this
2429 * can still be done for windowless richedit controls. */
2430 clickMsg
.hwnd
= (HWND
)editor
;
2431 clickMsg
.message
= msg
;
2432 clickMsg
.wParam
= wParam
;
2433 clickMsg
.lParam
= lParam
;
2434 clickMsg
.time
= GetMessageTime();
2435 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2436 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2437 if ((clickNum
!= 0) &&
2438 (clickMsg
.message
== prevClickMsg
.message
) &&
2439 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2440 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2441 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2442 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2443 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2449 prevClickMsg
= clickMsg
;
2456 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2462 DWORD messagePos
= GetMessagePos();
2463 pt
.x
= (short)LOWORD(messagePos
);
2464 pt
.y
= (short)HIWORD(messagePos
);
2468 sbi
.cbSize
= sizeof(sbi
);
2469 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2470 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2471 PtInRect(&sbi
.rcScrollBar
, pt
))
2473 ITextHost_TxSetCursor(editor
->texthost
,
2474 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2477 sbi
.cbSize
= sizeof(sbi
);
2478 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2479 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2480 PtInRect(&sbi
.rcScrollBar
, pt
))
2482 ITextHost_TxSetCursor(editor
->texthost
,
2483 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2487 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2489 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2490 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2493 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2494 pt
.y
< editor
->rcFormat
.top
&&
2495 pt
.x
< editor
->rcFormat
.left
)
2497 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2500 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2502 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2503 ITextHost_TxSetCursor(editor
->texthost
,
2504 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2506 ITextHost_TxSetCursor(editor
->texthost
,
2507 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2510 if (pt
.x
< editor
->rcFormat
.left
)
2512 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2515 offset
= ME_CharFromPos(editor
, pt
.x
, pt
.y
, &isExact
);
2518 if (editor
->AutoURLDetect_bEnable
)
2522 ME_CursorFromCharOfs(editor
, offset
, &cursor
);
2523 run
= &cursor
.pRun
->member
.run
;
2524 if (editor
->AutoURLDetect_bEnable
&&
2525 run
->style
->fmt
.dwMask
& CFM_LINK
&&
2526 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2528 ITextHost_TxSetCursor(editor
->texthost
,
2529 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2534 if (ME_IsSelection(editor
))
2536 int selStart
, selEnd
;
2537 ME_GetSelection(editor
, &selStart
, &selEnd
);
2538 if (selStart
<= offset
&& selEnd
>= offset
) {
2539 ITextHost_TxSetCursor(editor
->texthost
,
2540 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2546 ITextHost_TxSetCursor(editor
->texthost
,
2547 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2551 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2553 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2554 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2555 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2556 editor
->rcFormat
.right
-= 1;
2559 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2564 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2566 ME_GetSelection(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2567 if(selrange
.cpMin
== selrange
.cpMax
)
2568 seltype
|= SEL_EMPTY
;
2571 /* FIXME: Handle objects */
2572 seltype
|= SEL_TEXT
;
2573 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2574 seltype
|= SEL_MULTICHAR
;
2576 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2578 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, GetParent(editor
->hWnd
), NULL
);
2584 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
)
2586 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2592 ed
->texthost
= texthost
;
2593 ed
->bEmulateVersion10
= bEmulateVersion10
;
2594 ITextHost_TxGetPropertyBits(texthost
,
2595 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2596 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2597 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2598 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2599 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2601 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2602 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2603 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2604 ed
->pBuffer
= ME_MakeText();
2605 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2606 ME_MakeFirstParagraph(ed
);
2607 /* The four cursors are for:
2608 * 0 - The position where the caret is shown
2609 * 1 - The anchored end of the selection (for normal selection)
2610 * 2 & 3 - The anchored start and end respectively for word, line,
2611 * or paragraph selection.
2614 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2615 ed
->pCursors
[0].pPara
= ed
->pBuffer
->pFirst
->member
.para
.next_para
;
2616 ed
->pCursors
[0].pRun
= ME_FindItemFwd(ed
->pCursors
[0].pPara
, diRun
);
2617 ed
->pCursors
[0].nOffset
= 0;
2618 ed
->pCursors
[1] = ed
->pCursors
[0];
2619 ed
->pCursors
[2] = ed
->pCursors
[0];
2620 ed
->pCursors
[3] = ed
->pCursors
[1];
2621 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2622 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2625 ed
->rgbBackColor
= -1;
2626 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2627 ed
->bCaretAtEnd
= FALSE
;
2629 ed
->nModifyStep
= 0;
2630 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2631 ed
->pUndoStack
= ed
->pRedoStack
= ed
->pUndoStackBottom
= NULL
;
2632 ed
->nUndoStackSize
= 0;
2633 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2634 ed
->nUndoMode
= umAddToUndo
;
2635 ed
->nParagraphs
= 1;
2636 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2637 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diParagraph
);
2638 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2639 ed
->bHideSelection
= FALSE
;
2640 ed
->pfnWordBreak
= NULL
;
2641 ed
->lpOleCallback
= NULL
;
2642 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2643 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2644 ed
->AutoURLDetect_bEnable
= FALSE
;
2645 ed
->bHaveFocus
= FALSE
;
2646 ed
->bMouseCaptured
= FALSE
;
2647 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2649 ed
->pFontCache
[i
].nRefs
= 0;
2650 ed
->pFontCache
[i
].nAge
= 0;
2651 ed
->pFontCache
[i
].hFont
= NULL
;
2654 ME_CheckCharOffsets(ed
);
2655 ed
->bDefaultFormatRect
= TRUE
;
2656 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2657 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2658 ed
->selofs
= selbarwidth
? SELECTIONBAR_WIDTH
: 0;
2659 ed
->nSelectionType
= stPosition
;
2661 ed
->cPasswordMask
= 0;
2662 if (props
& TXTBIT_USEPASSWORD
)
2663 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2665 if (props
& TXTBIT_AUTOWORDSEL
)
2666 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2667 if (props
& TXTBIT_MULTILINE
)
2668 ed
->styleFlags
|= ES_MULTILINE
;
2669 if (props
& TXTBIT_READONLY
)
2670 ed
->styleFlags
|= ES_READONLY
;
2671 if (!(props
& TXTBIT_HIDESELECTION
))
2672 ed
->styleFlags
|= ES_NOHIDESEL
;
2673 if (props
& TXTBIT_SAVESELECTION
)
2674 ed
->styleFlags
|= ES_SAVESEL
;
2675 if (props
& TXTBIT_VERTICAL
)
2676 ed
->styleFlags
|= ES_VERTICAL
;
2677 if (props
& TXTBIT_DISABLEDRAG
)
2678 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2680 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2682 /* Default scrollbar information */
2683 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2684 ed
->vert_si
.nMin
= 0;
2685 ed
->vert_si
.nMax
= 0;
2686 ed
->vert_si
.nPage
= 0;
2687 ed
->vert_si
.nPos
= 0;
2689 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2690 ed
->horz_si
.nMin
= 0;
2691 ed
->horz_si
.nMax
= 0;
2692 ed
->horz_si
.nPage
= 0;
2693 ed
->horz_si
.nPos
= 0;
2695 OleInitialize(NULL
);
2700 static void ME_DestroyEditor(ME_TextEditor
*editor
)
2702 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2703 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2706 ME_ClearTempStyle(editor
);
2707 ME_EmptyUndoStack(editor
);
2710 ME_DestroyDisplayItem(p
);
2713 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2714 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2716 if (editor
->pFontCache
[i
].hFont
)
2717 DeleteObject(editor
->pFontCache
[i
].hFont
);
2719 if (editor
->rgbBackColor
!= -1)
2720 DeleteObject(editor
->hbrBackground
);
2721 if(editor
->lpOleCallback
)
2722 IUnknown_Release(editor
->lpOleCallback
);
2723 IUnknown_Release(editor
->texthost
);
2726 FREE_OBJ(editor
->pBuffer
);
2727 FREE_OBJ(editor
->pCursors
);
2732 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2737 case DLL_PROCESS_ATTACH
:
2738 DisableThreadLibraryCalls(hinstDLL
);
2739 me_heap
= HeapCreate (0, 0x10000, 0);
2740 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2741 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2745 case DLL_PROCESS_DETACH
:
2746 UnregisterClassW(RichEdit20W
, 0);
2747 UnregisterClassW(RichEdit50W
, 0);
2748 UnregisterClassA("RichEdit20A", 0);
2749 UnregisterClassA("RichEdit50A", 0);
2750 if (ME_ListBoxRegistered
)
2751 UnregisterClassW(REListBox20W
, 0);
2752 if (ME_ComboBoxRegistered
)
2753 UnregisterClassW(REComboBox20W
, 0);
2755 HeapDestroy (me_heap
);
2763 static const char * const edit_messages
[] = {
2792 "EM_SETPASSWORDCHAR",
2793 "EM_EMPTYUNDOBUFFER",
2794 "EM_GETFIRSTVISIBLELINE",
2796 "EM_SETWORDBREAKPROC",
2797 "EM_GETWORDBREAKPROC",
2798 "EM_GETPASSWORDCHAR",
2808 static const char * const richedit_messages
[] = {
2813 "EM_EXLINEFROMCHAR",
2819 "EM_GETOLEINTERFACE",
2829 "EM_SETOLECALLBACK",
2831 "EM_SETTARGETDEVICE",
2839 "EM_GETWORDBREAKPROCEX",
2840 "EM_SETWORDBREAKPROCEX",
2842 "EM_UNKNOWN_USER_83",
2847 "EM_STOPGROUPTYPING",
2851 "EM_GETAUTOURLDETECT",
2854 "EM_GETTEXTLENGTHEX",
2857 "EM_UNKNOWN_USER_98",
2858 "EM_UNKNOWN_USER_99",
2859 "EM_SETPUNCTUATION",
2860 "EM_GETPUNCTUATION",
2861 "EM_SETWORDWRAPMODE",
2862 "EM_GETWORDWRAPMODE",
2868 "EM_UNKNOWN_USER_109",
2869 "EM_UNKNOWN_USER_110",
2870 "EM_UNKNOWN_USER_111",
2871 "EM_UNKNOWN_USER_112",
2872 "EM_UNKNOWN_USER_113",
2873 "EM_UNKNOWN_USER_114",
2874 "EM_UNKNOWN_USER_115",
2875 "EM_UNKNOWN_USER_116",
2876 "EM_UNKNOWN_USER_117",
2877 "EM_UNKNOWN_USER_118",
2878 "EM_UNKNOWN_USER_119",
2879 "EM_SETLANGOPTIONS",
2880 "EM_GETLANGOPTIONS",
2881 "EM_GETIMECOMPMODE",
2885 "EM_SETIMEMODEBIAS",
2890 get_msg_name(UINT msg
)
2892 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
2893 return edit_messages
[msg
- EM_GETSEL
];
2894 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
2895 return richedit_messages
[msg
- EM_CANPASTE
];
2899 void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2902 ME_DisplayItem
*para
, *run
;
2904 int nCharOfs
; /* The start of the clicked text. Absolute character offset */
2907 x
= (short)LOWORD(lParam
);
2908 y
= (short)HIWORD(lParam
);
2909 nCharOfs
= ME_CharFromPos(editor
, x
, y
, &isExact
);
2910 if (!isExact
) return;
2912 ME_RunOfsFromCharOfs(editor
, nCharOfs
, ¶
, &run
, NULL
);
2914 if ((run
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
)
2915 && (run
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
))
2916 { /* The clicked run has CFE_LINK set */
2917 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
2918 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2919 info
.nmhdr
.code
= EN_LINK
;
2921 info
.wParam
= wParam
;
2922 info
.lParam
= lParam
;
2923 info
.chrg
.cpMin
= ME_CharOfsFromRunOfs(editor
, para
, run
, 0);
2924 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ run
->member
.run
.strText
->nLen
;
2925 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,info
.nmhdr
.idFrom
, (LPARAM
)&info
);
2929 #define UNSUPPORTED_MSG(e) \
2931 FIXME(#e ": stub\n"); \
2932 *phresult = S_FALSE; \
2935 /* Handle messages for windowless and windoweded richedit controls.
2937 * The LRESULT that is returned is a return value for window procs,
2938 * and the phresult parameter is the COM return code needed by the
2939 * text services interface. */
2940 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2941 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
2947 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
2948 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
2949 UNSUPPORTED_MSG(EM_FMTLINES
)
2950 UNSUPPORTED_MSG(EM_FORMATRANGE
)
2951 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
2952 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
2953 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
2954 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
2955 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
2956 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
2957 UNSUPPORTED_MSG(EM_GETREDONAME
)
2958 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
2959 UNSUPPORTED_MSG(EM_GETUNDONAME
)
2960 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
2961 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
2962 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
2963 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
2964 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
2965 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
2966 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
2967 UNSUPPORTED_MSG(EM_SETMARGINS
)
2968 UNSUPPORTED_MSG(EM_SETPALETTE
)
2969 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
2970 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
2971 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
2972 UNSUPPORTED_MSG(WM_STYLECHANGING
)
2973 UNSUPPORTED_MSG(WM_STYLECHANGED
)
2975 /* Messages specific to Richedit controls */
2978 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
2980 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
2983 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
|DLGC_HASSETSEL
;
2984 if (editor
->styleFlags
& ES_MULTILINE
)
2985 code
|= DLGC_WANTMESSAGE
;
2988 case EM_EMPTYUNDOBUFFER
:
2989 ME_EmptyUndoStack(editor
);
2993 /* Note: wParam/lParam can be NULL */
2995 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
2996 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
2997 ME_GetSelection(editor
, (int *)pfrom
, (int *)pto
);
2998 if ((*pfrom
|*pto
) & 0xFFFF0000)
3000 return MAKELONG(*pfrom
,*pto
);
3004 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3005 ME_GetSelection(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3006 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3009 case EM_SETUNDOLIMIT
:
3011 if ((int)wParam
< 0)
3012 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3014 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3015 /* Setting a max stack size keeps wine from getting killed
3016 for hogging memory. Windows allocates all this memory at once, so
3017 no program would realistically set a value above our maximum. */
3018 return editor
->nUndoLimit
;
3021 return editor
->pUndoStack
!= NULL
;
3023 return editor
->pRedoStack
!= NULL
;
3024 case WM_UNDO
: /* FIXME: actually not the same */
3026 return ME_Undo(editor
);
3028 return ME_Redo(editor
);
3031 /* these flags are equivalent to the ES_* counterparts */
3032 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3033 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3034 DWORD settings
= editor
->styleFlags
& mask
;
3040 /* these flags are equivalent to ES_* counterparts, except for
3041 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3042 * but is still stored in editor->styleFlags. */
3043 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3044 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3045 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3046 DWORD settings
= mask
& editor
->styleFlags
;
3047 DWORD oldSettings
= settings
;
3048 DWORD changedSettings
;
3064 changedSettings
= oldSettings
^ settings
;
3066 if (oldSettings
^ settings
) {
3067 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3069 if (settings
& ECO_SELECTIONBAR
) {
3070 editor
->selofs
= SELECTIONBAR_WIDTH
;
3071 editor
->rcFormat
.left
+= SELECTIONBAR_WIDTH
;
3074 editor
->rcFormat
.left
-= SELECTIONBAR_WIDTH
;
3076 ME_WrapMarkedParagraphs(editor
);
3079 if (settings
& ECO_VERTICAL
)
3080 FIXME("ECO_VERTICAL not implemented yet!\n");
3081 if (settings
& ECO_AUTOHSCROLL
)
3082 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3083 if (settings
& ECO_AUTOVSCROLL
)
3084 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3085 if (settings
& ECO_NOHIDESEL
)
3086 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3087 if (settings
& ECO_WANTRETURN
)
3088 FIXME("ECO_WANTRETURN not implemented yet!\n");
3089 if (settings
& ECO_AUTOWORDSELECTION
)
3090 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3096 ME_InvalidateSelection(editor
);
3097 ME_SetSelection(editor
, wParam
, lParam
);
3098 ME_InvalidateSelection(editor
);
3099 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3100 ME_ShowCaret(editor
);
3101 ME_SendSelChange(editor
);
3104 case EM_SETSCROLLPOS
:
3106 POINT
*point
= (POINT
*)lParam
;
3107 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3110 case EM_AUTOURLDETECT
:
3112 if (wParam
==1 || wParam
==0)
3114 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3117 return E_INVALIDARG
;
3119 case EM_GETAUTOURLDETECT
:
3121 return editor
->AutoURLDetect_bEnable
;
3126 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3128 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
3130 ME_InvalidateSelection(editor
);
3131 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
3132 ME_InvalidateSelection(editor
);
3133 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
3134 ME_ShowCaret(editor
);
3135 ME_SendSelChange(editor
);
3139 case EM_SHOWSCROLLBAR
:
3152 flags
= WS_HSCROLL
|WS_VSCROLL
;
3159 editor
->styleFlags
|= flags
;
3161 editor
->styleFlags
&= flags
;
3163 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, lParam
);
3169 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3173 BOOL bRtf
, bUnicode
, bSelection
;
3174 int oldModify
= editor
->nModifyStep
;
3176 if (!pStruct
) return 0;
3178 /* If we detect ascii rtf at the start of the string,
3179 * we know it isn't unicode. */
3180 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3181 !strncmp((char *)lParam
, "{\\urtf}", 6)));
3182 bUnicode
= !bRtf
&& pStruct
->codepage
== 1200;
3184 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3185 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3186 pStruct
->flags
, pStruct
->codepage
);
3188 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3190 ME_GetSelection(editor
, &from
, &to
);
3191 style
= ME_GetSelectionInsertStyle(editor
);
3192 ME_InternalDeleteText(editor
, from
, to
- from
, FALSE
);
3194 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
3195 style
= editor
->pBuffer
->pDefaultStyle
;
3199 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3201 /* FIXME: The length returned is doesn't include the rtf control
3202 * characters, only the actual text. */
3203 len
= lParam
? strlen((char *)lParam
) : 0;
3206 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3207 wszText
= lParam
? ME_ToUnicode(bUnicode
, (void *)lParam
) : NULL
;
3208 len
= wszText
? lstrlenW(wszText
) : 0;
3209 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3213 ME_ReleaseStyle(style
);
3214 if (editor
->AutoURLDetect_bEnable
)
3215 ME_UpdateSelectionLinkAttribute(editor
);
3218 if (editor
->AutoURLDetect_bEnable
)
3219 ME_UpdateLinkAttribute(editor
, 0, -1);
3221 ME_CommitUndo(editor
);
3222 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3224 editor
->nModifyStep
= oldModify
;
3225 ME_EmptyUndoStack(editor
);
3227 ME_UpdateRepaint(editor
);
3230 case EM_SETBKGNDCOLOR
:
3233 if (editor
->rgbBackColor
!= -1) {
3234 DeleteObject(editor
->hbrBackground
);
3235 lColor
= editor
->rgbBackColor
;
3237 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3241 editor
->rgbBackColor
= -1;
3242 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3246 editor
->rgbBackColor
= lParam
;
3247 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3249 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3250 ITextHost_TxViewChange(editor
->texthost
, TRUE
);
3254 return editor
->nModifyStep
== 0 ? 0 : -1;
3258 editor
->nModifyStep
= 1;
3260 editor
->nModifyStep
= 0;
3264 case EM_SETREADONLY
:
3267 editor
->styleFlags
|= ES_READONLY
;
3269 editor
->styleFlags
&= ~ES_READONLY
;
3272 case EM_SETEVENTMASK
:
3274 DWORD nOldMask
= editor
->nEventMask
;
3276 editor
->nEventMask
= lParam
;
3279 case EM_GETEVENTMASK
:
3280 return editor
->nEventMask
;
3281 case EM_SETCHARFORMAT
:
3283 CHARFORMAT2W buf
, *p
;
3284 BOOL bRepaint
= TRUE
;
3285 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3286 if (p
== NULL
) return 0;
3288 ME_SetDefaultCharFormat(editor
, p
);
3289 else if (wParam
== (SCF_WORD
| SCF_SELECTION
)) {
3290 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3292 } else if (wParam
== SCF_ALL
) {
3293 if (editor
->mode
& TM_PLAINTEXT
)
3294 ME_SetDefaultCharFormat(editor
, p
);
3296 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), p
);
3297 editor
->nModifyStep
= 1;
3299 } else if (editor
->mode
& TM_PLAINTEXT
) {
3303 ME_GetSelection(editor
, &from
, &to
);
3304 bRepaint
= (from
!= to
);
3305 ME_SetSelectionCharFormat(editor
, p
);
3306 if (from
!= to
) editor
->nModifyStep
= 1;
3308 ME_CommitUndo(editor
);
3311 ME_WrapMarkedParagraphs(editor
);
3312 ME_UpdateScrollBar(editor
);
3317 case EM_GETCHARFORMAT
:
3319 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3320 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3321 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3322 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3323 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3325 tmp
.cbSize
= sizeof(tmp
);
3327 ME_GetDefaultCharFormat(editor
, &tmp
);
3329 ME_GetSelectionCharFormat(editor
, &tmp
);
3330 ME_CopyToCFAny(dst
, &tmp
);
3333 case EM_SETPARAFORMAT
:
3335 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3336 ME_WrapMarkedParagraphs(editor
);
3337 ME_UpdateScrollBar(editor
);
3339 ME_CommitUndo(editor
);
3342 case EM_GETPARAFORMAT
:
3343 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3344 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3345 case EM_GETFIRSTVISIBLELINE
:
3347 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3348 int y
= editor
->vert_si
.nPos
;
3353 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3354 if (p
->type
== diTextEnd
)
3356 if (p
->type
== diParagraph
) {
3357 ypara
= p
->member
.para
.pt
.y
;
3360 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3361 yend
= ystart
+ p
->member
.row
.nHeight
;
3369 case EM_HIDESELECTION
:
3371 editor
->bHideSelection
= (wParam
!= 0);
3372 ME_InvalidateSelection(editor
);
3377 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
3378 return TRUE
; /* Should return false if a single line richedit control */
3383 ME_GetSelection(editor
, &from
, &to
);
3384 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
3385 ME_CommitUndo(editor
);
3386 ME_UpdateRepaint(editor
);
3393 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
3394 size_t len
= wszText
? lstrlenW(wszText
) : 0;
3395 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3397 ME_GetSelection(editor
, &from
, &to
);
3398 style
= ME_GetSelectionInsertStyle(editor
);
3399 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
3400 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3401 ME_ReleaseStyle(style
);
3402 /* drop temporary style if line end */
3404 * FIXME question: does abc\n mean: put abc,
3405 * clear temp style, put \n? (would require a change)
3407 if (len
>0 && wszText
[len
-1] == '\n')
3408 ME_ClearTempStyle(editor
);
3409 ME_EndToUnicode(unicode
, wszText
);
3410 ME_CommitUndo(editor
);
3411 if (editor
->AutoURLDetect_bEnable
) ME_UpdateSelectionLinkAttribute(editor
);
3413 ME_EmptyUndoStack(editor
);
3414 ME_UpdateRepaint(editor
);
3417 case EM_SCROLLCARET
:
3418 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3425 BOOL bRepaint
= LOWORD(lParam
);
3428 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3429 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3430 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3431 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3432 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3433 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), &fmt
);
3434 ME_SetDefaultCharFormat(editor
, &fmt
);
3436 ME_CommitUndo(editor
);
3437 ME_MarkAllForWrapping(editor
);
3438 ME_WrapMarkedParagraphs(editor
);
3439 ME_UpdateScrollBar(editor
);
3446 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
3449 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3450 if (!unicode
&& !strncmp((char *)lParam
, "{\\rtf", 5))
3452 /* Undocumented: WM_SETTEXT supports RTF text */
3453 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3457 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
3458 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3459 if (lstrlenW(wszText
) > 0)
3463 /* uses default style! */
3464 if (!(editor
->styleFlags
& ES_MULTILINE
))
3469 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3472 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3474 ME_EndToUnicode(unicode
, wszText
);
3478 TRACE("WM_SETTEXT - NULL\n");
3479 if (editor
->AutoURLDetect_bEnable
)
3481 ME_UpdateLinkAttribute(editor
, 0, -1);
3483 ME_SetSelection(editor
, 0, 0);
3484 editor
->nModifyStep
= 0;
3485 ME_CommitUndo(editor
);
3486 ME_EmptyUndoStack(editor
);
3487 ME_UpdateRepaint(editor
);
3492 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3493 if (IsClipboardFormatAvailable(nRTFFormat
))
3495 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3506 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
3508 if (ME_Copy(editor
, &range
) && msg
== WM_CUT
)
3510 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
, FALSE
);
3511 ME_CommitUndo(editor
);
3512 ME_UpdateRepaint(editor
);
3516 case WM_GETTEXTLENGTH
:
3518 GETTEXTLENGTHEX how
;
3520 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3521 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3522 how
.codepage
= unicode
? 1200 : CP_ACP
;
3523 return ME_GetTextLengthEx(editor
, &how
);
3525 case EM_GETTEXTLENGTHEX
:
3526 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3531 LPSTR bufferA
= NULL
;
3532 LPWSTR bufferW
= NULL
;
3535 bufferW
= heap_alloc((wParam
+ 2) * sizeof(WCHAR
));
3537 bufferA
= heap_alloc(wParam
+ 2);
3539 ex
.cb
= (wParam
+ 2) * (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3540 ex
.flags
= GT_USECRLF
;
3541 ex
.codepage
= unicode
? 1200 : CP_ACP
;
3542 ex
.lpDefaultChar
= NULL
;
3543 ex
.lpUsedDefChar
= NULL
;
3545 rc
= ME_GetTextEx(editor
, &ex
, unicode
? (LPARAM
)bufferW
: (LPARAM
)bufferA
);
3549 memcpy((LPWSTR
)lParam
, bufferW
, wParam
* sizeof(WCHAR
));
3550 if (strlenW(bufferW
) >= wParam
) rc
= 0;
3554 memcpy((LPSTR
)lParam
, bufferA
, wParam
);
3555 if (strlen(bufferA
) >= wParam
) rc
= 0;
3562 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3566 TEXTRANGEW tr
; /* W and A differ only by rng->lpstrText */
3567 ME_GetSelection(editor
, &from
, &to
);
3568 tr
.chrg
.cpMin
= from
;
3570 tr
.lpstrText
= (WCHAR
*)lParam
;
3571 return ME_GetTextRange(editor
, &tr
, unicode
);
3573 case EM_GETSCROLLPOS
:
3575 POINT
*point
= (POINT
*)lParam
;
3576 point
->x
= editor
->horz_si
.nPos
;
3577 point
->y
= editor
->vert_si
.nPos
;
3580 case EM_GETTEXTRANGE
:
3582 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3583 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3584 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
,
3585 editor
->bEmulateVersion10
, ME_GetTextLength(editor
));
3586 return ME_GetTextRange(editor
, rng
, unicode
);
3590 ME_DisplayItem
*run
;
3591 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3592 unsigned int nCharsLeft
= nMaxChars
;
3593 char *dest
= (char *) lParam
;
3594 BOOL wroteNull
= FALSE
;
3596 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3597 unicode
? "Unicode" : "Ansi");
3599 run
= ME_FindRowWithNumber(editor
, wParam
);
3603 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3604 && run
->type
== diRun
)
3609 strText
= run
->member
.run
.strText
;
3610 nCopy
= min(nCharsLeft
, strText
->nLen
);
3613 memcpy(dest
, strText
->szData
, nCopy
* sizeof(WCHAR
));
3615 nCopy
= WideCharToMultiByte(CP_ACP
, 0, strText
->szData
, nCopy
, dest
,
3616 nCharsLeft
, NULL
, NULL
);
3617 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3618 nCharsLeft
-= nCopy
;
3621 /* append line termination, space allowing */
3625 *((WCHAR
*)dest
) = '\0';
3632 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3633 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3635 case EM_GETLINECOUNT
:
3637 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3640 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3642 while (item
!= editor
->pBuffer
->pLast
)
3644 assert(item
->type
== diParagraph
);
3645 prev_para
= ME_FindItemBack(item
, diRun
);
3647 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3649 nRows
+= item
->member
.para
.nRows
;
3650 item
= item
->member
.para
.next_para
;
3652 last_para
= ME_FindItemBack(item
, diRun
);
3654 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3655 if (editor
->bEmulateVersion10
&& prev_para
&&
3656 last_para
->member
.run
.nCharOfs
== 0 &&
3657 prev_para
->member
.run
.strText
->nLen
== 1 &&
3658 prev_para
->member
.run
.strText
->szData
[0] == '\r')
3660 /* In 1.0 emulation, the last solitary \r at the very end of the text
3661 (if one exists) is NOT a line break.
3662 FIXME: this is an ugly hack. This should have a more regular model. */
3666 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3667 return max(1, nRows
);
3669 case EM_LINEFROMCHAR
:
3672 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
, 1));
3674 return ME_RowNumberFromCharOfs(editor
, wParam
);
3676 case EM_EXLINEFROMCHAR
:
3679 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
,1));
3681 return ME_RowNumberFromCharOfs(editor
, lParam
);
3685 ME_DisplayItem
*item
, *para
;
3689 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3691 item
= ME_FindRowWithNumber(editor
, wParam
);
3694 para
= ME_GetParagraph(item
);
3695 item
= ME_FindItemFwd(item
, diRun
);
3696 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3697 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3702 ME_DisplayItem
*item
, *item_end
;
3703 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3704 ME_DisplayItem
*para
, *run
;
3706 if (wParam
> ME_GetTextLength(editor
))
3710 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3713 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
3714 item
= ME_RowStart(run
);
3715 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
3716 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3717 if (item_end
->type
== diStartRow
) {
3718 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
3720 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
3721 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
3722 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.strText
->nLen
;
3724 nChars
= nNextLineOfs
- nThisLineOfs
;
3725 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3728 case EM_EXLIMITTEXT
:
3730 if ((int)lParam
< 0)
3733 editor
->nTextLimit
= 65536;
3735 editor
->nTextLimit
= (int) lParam
;
3741 editor
->nTextLimit
= 65536;
3743 editor
->nTextLimit
= (int) wParam
;
3746 case EM_GETLIMITTEXT
:
3748 return editor
->nTextLimit
;
3752 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3753 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3757 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3758 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3759 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3765 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3766 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3770 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3771 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3772 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3778 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3779 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3781 case EM_FINDTEXTEXW
:
3783 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3784 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3787 if (!wParam
|| !lParam
)
3789 *(int *)wParam
= editor
->nZoomNumerator
;
3790 *(int *)lParam
= editor
->nZoomDenominator
;
3793 return ME_SetZoom(editor
, wParam
, lParam
);
3794 case EM_CHARFROMPOS
:
3795 return ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
, NULL
);
3796 case EM_POSFROMCHAR
:
3798 ME_DisplayItem
*pPara
, *pRun
;
3799 int nCharOfs
, nOffset
, nLength
;
3803 /* detect which API version we're dealing with */
3804 if (wParam
>= 0x40000)
3806 nLength
= ME_GetTextLength(editor
);
3807 nCharOfs
= min(nCharOfs
, nLength
);
3808 nCharOfs
= max(nCharOfs
, 0);
3810 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
3811 assert(pRun
->type
== diRun
);
3812 pt
.y
= pRun
->member
.run
.pt
.y
;
3813 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
);
3814 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
3815 pt
.x
+= editor
->rcFormat
.left
;
3817 pt
.x
-= editor
->horz_si
.nPos
;
3818 pt
.y
-= editor
->vert_si
.nPos
;
3820 if (wParam
>= 0x40000) {
3821 *(POINTL
*)wParam
= pt
;
3823 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
3829 ME_SetDefaultFormatRect(editor
);
3831 si
.cbSize
= sizeof(si
);
3832 si
.fMask
= SIF_PAGE
| SIF_RANGE
;
3833 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3834 si
.fMask
|= SIF_DISABLENOSCROLL
;
3835 si
.nMax
= (si
.fMask
& SIF_DISABLENOSCROLL
) ? 1 : 0;
3839 SetScrollInfo(editor
->hWnd
, SB_VERT
, &si
, TRUE
);
3840 SetScrollInfo(editor
->hWnd
, SB_HORZ
, &si
, TRUE
);
3842 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, si
.nMin
, si
.nMax
, TRUE
);
3843 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, si
.nMin
, si
.nMax
, TRUE
);
3846 ME_CommitUndo(editor
);
3847 ME_WrapMarkedParagraphs(editor
);
3848 ME_MoveCaret(editor
);
3852 ME_DestroyEditor(editor
);
3856 return ME_SetCursor(editor
);
3858 case WM_LBUTTONDBLCLK
:
3859 case WM_LBUTTONDOWN
:
3861 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3862 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3863 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3865 ITextHost_TxSetFocus(editor
->texthost
);
3866 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
3867 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
3868 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
3869 editor
->bMouseCaptured
= TRUE
;
3870 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3871 if (!ME_SetCursor(editor
)) goto do_default
;
3875 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3876 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3878 if (editor
->bMouseCaptured
)
3879 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
3880 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3881 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3882 if (editor
->bMouseCaptured
)
3883 ME_SetCursor(editor
);
3886 if (editor
->bMouseCaptured
) {
3887 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
3888 editor
->bMouseCaptured
= FALSE
;
3890 if (editor
->nSelectionType
== stDocument
)
3891 editor
->nSelectionType
= stPosition
;
3892 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3893 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3897 ME_SetCursor(editor
);
3898 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3902 case WM_RBUTTONDOWN
:
3903 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3904 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3905 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3908 case WM_CONTEXTMENU
:
3909 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
3913 editor
->bHaveFocus
= TRUE
;
3914 ME_ShowCaret(editor
);
3915 ME_SendOldNotify(editor
, EN_SETFOCUS
);
3918 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3919 editor
->bHaveFocus
= FALSE
;
3920 ME_HideCaret(editor
);
3921 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
3924 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
3927 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
3928 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3932 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
3933 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3935 if (ME_KeyDown(editor
, LOWORD(wParam
)))
3939 return ME_Char(editor
, wParam
, lParam
, unicode
);
3943 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
3944 if(wParam
<= 0x000fffff)
3946 if(wParam
> 0xffff) /* convert to surrogates */
3949 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
3950 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
3952 ME_Char(editor
, wParam
, 0, TRUE
);
3958 case EM_STOPGROUPTYPING
:
3959 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3963 const int scrollUnit
= 7;
3965 switch(LOWORD(wParam
))
3968 ME_ScrollAbs(editor
, 0, 0);
3971 ME_ScrollAbs(editor
,
3972 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
3973 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
3976 ME_ScrollLeft(editor
, scrollUnit
);
3979 ME_ScrollRight(editor
, scrollUnit
);
3982 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
3985 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
3988 case SB_THUMBPOSITION
:
3991 sbi
.cbSize
= sizeof(sbi
);
3992 sbi
.fMask
= SIF_TRACKPOS
;
3993 /* Try to get 32-bit track position value. */
3994 if (!GetScrollInfo(editor
->hWnd
, SB_HORZ
, &sbi
))
3995 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
3996 sbi
.nTrackPos
= HIWORD(wParam
);
3998 ME_HScrollAbs(editor
, sbi
.nTrackPos
);
4004 case EM_SCROLL
: /* fall through */
4010 origNPos
= editor
->vert_si
.nPos
;
4013 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
4014 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
4015 if (lineHeight
<= 0) lineHeight
= 24;
4017 switch(LOWORD(wParam
))
4020 ME_ScrollAbs(editor
, 0, 0);
4023 ME_ScrollAbs(editor
,
4024 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4025 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4028 ME_ScrollUp(editor
,lineHeight
);
4031 ME_ScrollDown(editor
,lineHeight
);
4034 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4037 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4040 case SB_THUMBPOSITION
:
4043 sbi
.cbSize
= sizeof(sbi
);
4044 sbi
.fMask
= SIF_TRACKPOS
;
4045 /* Try to get 32-bit track position value. */
4046 if (!GetScrollInfo(editor
->hWnd
, SB_VERT
, &sbi
))
4047 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4048 sbi
.nTrackPos
= HIWORD(wParam
);
4050 ME_VScrollAbs(editor
, sbi
.nTrackPos
);
4054 if (msg
== EM_SCROLL
)
4055 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4061 UINT pulScrollLines
;
4064 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4065 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4068 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4070 gcWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4072 if (abs(gcWheelDelta
) >= WHEEL_DELTA
)
4076 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4080 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4082 numerator
= numerator
+ (gcWheelDelta
/ WHEEL_DELTA
) * 10;
4083 if (numerator
>= 10 && numerator
<= 500)
4084 ME_SetZoom(editor
, numerator
, 100);
4086 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
4087 /* FIXME follow the original */
4089 ME_ScrollDown(editor
,pulScrollLines
* (-gcWheelDelta
/ WHEEL_DELTA
) * 8);
4096 *((RECT
*)lParam
) = editor
->rcFormat
;
4097 if (editor
->bDefaultFormatRect
)
4098 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4108 RECT
*rc
= (RECT
*)lParam
;
4110 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4111 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4114 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4115 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4116 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4117 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4118 } else if (wParam
== 1) {
4119 /* MSDN incorrectly says a wParam value of 1 causes the
4120 * lParam rect to be used as a relative offset,
4121 * however, the tests show it just prevents min/max bound
4123 editor
->rcFormat
.top
= rc
->top
- border
;
4124 editor
->rcFormat
.left
= rc
->left
- border
;
4125 editor
->rcFormat
.bottom
= rc
->bottom
;
4126 editor
->rcFormat
.right
= rc
->right
+ border
;
4130 editor
->bDefaultFormatRect
= FALSE
;
4134 ME_SetDefaultFormatRect(editor
);
4135 editor
->bDefaultFormatRect
= TRUE
;
4137 ME_MarkAllForWrapping(editor
);
4138 ME_WrapMarkedParagraphs(editor
);
4139 ME_UpdateScrollBar(editor
);
4140 if (msg
!= EM_SETRECTNP
)
4144 case EM_REQUESTRESIZE
:
4145 ME_SendRequestResize(editor
, TRUE
);
4153 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4154 if (editor
->bDefaultFormatRect
) {
4155 ME_SetDefaultFormatRect(editor
);
4157 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4158 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4160 editor
->prevClientRect
= clientRect
;
4161 ME_RewrapRepaint(editor
);
4164 /* IME messages to make richedit controls IME aware */
4165 case WM_IME_SETCONTEXT
:
4166 case WM_IME_CONTROL
:
4168 case WM_IME_COMPOSITIONFULL
:
4170 case WM_IME_STARTCOMPOSITION
:
4172 editor
->imeStartIndex
=ME_GetCursorOfs(editor
,0);
4173 ME_DeleteSelection(editor
);
4174 ME_CommitUndo(editor
);
4175 ME_UpdateRepaint(editor
);
4178 case WM_IME_COMPOSITION
:
4182 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4183 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4184 ME_DeleteSelection(editor
);
4185 ME_CommitUndo(editor
);
4186 ME_SaveTempStyle(editor
);
4187 if (lParam
& GCS_RESULTSTR
)
4189 LPWSTR lpCompStr
= NULL
;
4192 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4193 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4194 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpCompStr
, dwBufLen
);
4195 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4196 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4198 else if (lParam
& GCS_COMPSTR
)
4200 LPWSTR lpCompStr
= NULL
;
4203 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4204 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4205 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, dwBufLen
);
4206 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4208 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4209 ME_SetSelection(editor
,editor
->imeStartIndex
,
4210 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4212 ME_ReleaseStyle(style
);
4213 ME_UpdateRepaint(editor
);
4216 case WM_IME_ENDCOMPOSITION
:
4218 ME_DeleteSelection(editor
);
4219 editor
->imeStartIndex
=-1;
4222 case EM_GETOLEINTERFACE
:
4224 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4225 return CreateIRichEditOle(editor
, ppvObj
);
4227 case EM_GETPASSWORDCHAR
:
4229 return editor
->cPasswordMask
;
4231 case EM_SETOLECALLBACK
:
4232 if(editor
->lpOleCallback
)
4233 IUnknown_Release(editor
->lpOleCallback
);
4234 editor
->lpOleCallback
= (LPRICHEDITOLECALLBACK
)lParam
;
4235 if(editor
->lpOleCallback
)
4236 IUnknown_AddRef(editor
->lpOleCallback
);
4238 case EM_GETWORDBREAKPROC
:
4239 return (LRESULT
)editor
->pfnWordBreak
;
4240 case EM_SETWORDBREAKPROC
:
4242 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4244 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4245 return (LRESULT
)pfnOld
;
4247 case EM_GETTEXTMODE
:
4248 return editor
->mode
;
4249 case EM_SETTEXTMODE
:
4254 GETTEXTLENGTHEX how
;
4256 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4257 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
4258 how
.codepage
= unicode
? 1200 : CP_ACP
;
4259 ret
= ME_GetTextLengthEx(editor
, &how
);
4262 /*Check for valid wParam*/
4263 if ((((wParam
& TM_RICHTEXT
) && ((wParam
& TM_PLAINTEXT
) << 1))) ||
4264 (((wParam
& TM_MULTILEVELUNDO
) && ((wParam
& TM_SINGLELEVELUNDO
) << 1))) ||
4265 (((wParam
& TM_MULTICODEPAGE
) && ((wParam
& TM_SINGLECODEPAGE
) << 1))))
4269 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4271 mask
|= (TM_RICHTEXT
| TM_PLAINTEXT
);
4272 changes
|= (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
));
4274 /*FIXME: Currently no support for undo level and code page options*/
4275 editor
->mode
= (editor
->mode
& (~mask
)) | changes
;
4281 case EM_SETPASSWORDCHAR
:
4283 editor
->cPasswordMask
= wParam
;
4284 ME_RewrapRepaint(editor
);
4287 case EM_SETTARGETDEVICE
:
4290 BOOL
new = (lParam
== 0);
4291 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4293 editor
->bWordWrap
= new;
4294 editor
->nAvailWidth
= 0; /* wrap to client area */
4295 ME_RewrapRepaint(editor
);
4298 int width
= max(0, lParam
);
4299 if (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
)
4301 editor
->nAvailWidth
= width
;
4302 editor
->bWordWrap
= TRUE
;
4303 ME_RewrapRepaint(editor
);
4305 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4310 *phresult
= S_FALSE
;
4316 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4317 LPARAM lParam
, BOOL unicode
)
4319 ME_TextEditor
*editor
;
4321 LRESULT lresult
= 0;
4323 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4324 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4326 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4329 if (msg
== WM_NCCREATE
)
4331 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4332 ITextHost
*texthost
;
4334 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4335 texthost
= ME_CreateTextHost(hWnd
, FALSE
);
4336 return texthost
!= NULL
;
4338 else if (msg
!= WM_NCDESTROY
)
4340 ERR("called with invalid hWnd %p - application bug?\n", hWnd
);
4353 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4354 /* Erase area outside of the formatting rectangle */
4355 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4358 rc
.bottom
= editor
->rcFormat
.top
;
4359 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4360 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4362 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4364 rc
.top
= editor
->rcFormat
.bottom
;
4365 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4366 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4368 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4370 rc
.right
= editor
->rcFormat
.left
;
4371 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4372 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4374 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4376 rc
.left
= editor
->rcFormat
.right
;
4377 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4378 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4381 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
4382 EndPaint(editor
->hWnd
, &ps
);
4387 HDC hDC
= (HDC
)wParam
;
4390 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4391 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4397 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4398 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4400 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4401 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4402 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4403 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4406 case EM_SETREADONLY
:
4409 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4410 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4411 dwStyle
&= ~ES_READONLY
;
4413 dwStyle
|= ES_READONLY
;
4414 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4418 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4421 if (hresult
== S_FALSE
)
4422 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4424 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4425 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4430 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4432 BOOL unicode
= TRUE
;
4434 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4435 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4438 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4441 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4443 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4446 /******************************************************************
4447 * RichEditANSIWndProc (RICHED20.10)
4449 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4451 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4454 /******************************************************************
4455 * RichEdit10ANSIWndProc (RICHED20.9)
4457 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4459 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4461 ITextHost
*texthost
;
4462 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4464 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4465 texthost
= ME_CreateTextHost(hWnd
, TRUE
);
4466 return texthost
!= NULL
;
4468 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4471 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4473 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4476 int ME_CountParagraphsBetween(ME_TextEditor
*editor
, int from
, int to
)
4478 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
4481 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= from
)
4482 item
= item
->member
.para
.next_para
;
4485 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= to
) {
4486 item
= item
->member
.para
.next_para
;
4493 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int nStart
,
4494 int nChars
, int bCRLF
)
4496 ME_DisplayItem
*pRun
;
4497 int nOffset
, nWritten
= 0;
4498 WCHAR
*pStart
= buffer
;
4500 ME_RunOfsFromCharOfs(editor
, nStart
, NULL
, &pRun
, &nOffset
);
4502 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4503 if (editor
->bEmulateVersion10
) bCRLF
= 0;
4505 while (nChars
&& pRun
)
4509 if (pRun
->member
.run
.nFlags
& MERF_ENDCELL
&&
4510 pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
4514 } else if (pRun
->member
.run
.nFlags
& MERF_ENDPARA
) {
4515 if (!ME_FindItemFwd(pRun
, diRun
)) {
4516 /* No '\r' is appended to the last paragraph. */
4518 } else if (bCRLF
&& nChars
== 1) {
4522 WCHAR cr_lf
[] = {'\r', '\n', 0};
4530 nLen
= pRun
->member
.run
.strText
->nLen
;
4531 szData
= pRun
->member
.run
.strText
->szData
;
4533 nLen
= min(nChars
, nLen
- nOffset
);
4534 CopyMemory(buffer
, szData
+ nOffset
, sizeof(WCHAR
) * nLen
);
4537 nLen
= min(nChars
, pRun
->member
.run
.strText
->nLen
- nOffset
);
4538 CopyMemory(buffer
, pRun
->member
.run
.strText
->szData
+ nOffset
,
4539 sizeof(WCHAR
) * nLen
);
4546 pRun
= ME_FindItemFwd(pRun
, diRun
);
4549 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
4553 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4558 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4559 wcW
.lpfnWndProc
= RichEditWndProcW
;
4561 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4562 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4564 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4565 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4566 wcW
.lpszMenuName
= NULL
;
4568 if (is_version_nt())
4570 wcW
.lpszClassName
= RichEdit20W
;
4571 if (!RegisterClassW(&wcW
)) return FALSE
;
4572 wcW
.lpszClassName
= RichEdit50W
;
4573 if (!RegisterClassW(&wcW
)) return FALSE
;
4577 /* WNDCLASSA/W have the same layout */
4578 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4579 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4580 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4581 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4584 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4585 wcA
.lpfnWndProc
= RichEditWndProcA
;
4587 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4588 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4590 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4591 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4592 wcA
.lpszMenuName
= NULL
;
4593 wcA
.lpszClassName
= "RichEdit20A";
4594 if (!RegisterClassA(&wcA
)) return FALSE
;
4595 wcA
.lpszClassName
= "RichEdit50A";
4596 if (!RegisterClassA(&wcA
)) return FALSE
;
4601 LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4602 /* FIXME: Not implemented */
4603 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4604 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4605 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4608 LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4609 /* FIXME: Not implemented */
4610 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4611 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4612 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4615 /******************************************************************
4616 * REExtendedRegisterClass (RICHED20.8)
4618 * FIXME undocumented
4619 * Need to check for errors and implement controls and callbacks
4621 LRESULT WINAPI
REExtendedRegisterClass(void)
4626 FIXME("semi stub\n");
4630 wcW
.hInstance
= NULL
;
4633 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4634 wcW
.lpszMenuName
= NULL
;
4636 if (!ME_ListBoxRegistered
)
4638 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4639 wcW
.lpfnWndProc
= REListWndProc
;
4640 wcW
.lpszClassName
= REListBox20W
;
4641 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4644 if (!ME_ComboBoxRegistered
)
4646 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4647 wcW
.lpfnWndProc
= REComboWndProc
;
4648 wcW
.lpszClassName
= REComboBox20W
;
4649 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4653 if (ME_ListBoxRegistered
)
4655 if (ME_ComboBoxRegistered
)
4661 static BOOL
isurlspecial(WCHAR c
)
4663 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4664 return strchrW( special_chars
, c
) != NULL
;
4668 * This proc takes a selection, and scans it forward in order to select the span
4669 * of a possible URL candidate. A possible URL candidate must start with isalnum
4670 * or one of the following special characters: *|/\+%#@ and must consist entirely
4671 * of the characters allowed to start the URL, plus : (colon) which may occur
4672 * at most once, and not at either end.
4674 * sel_max == -1 indicates scan to end of text.
4676 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
, int sel_min
, int sel_max
,
4677 int * candidate_min
, int * candidate_max
)
4679 ME_DisplayItem
* item
;
4680 ME_DisplayItem
* para
;
4682 BOOL foundColon
= FALSE
;
4683 WCHAR lastAcceptedChar
= '\0';
4685 TRACE("sel_min = %d sel_max = %d\n", sel_min
, sel_max
);
4687 *candidate_min
= *candidate_max
= -1;
4688 ME_RunOfsFromCharOfs(editor
, sel_min
, ¶
, &item
, &nStart
);
4689 TRACE("nStart = %d\n", nStart
);
4690 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4691 while (item
&& para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
< sel_max
)
4693 ME_DisplayItem
* next_item
;
4695 if (!(item
->member
.run
.nFlags
& MERF_ENDPARA
)) {
4696 /* Find start of candidate */
4697 if (*candidate_min
== -1) {
4698 while (nStart
< item
->member
.run
.strText
->nLen
&&
4699 !(isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4700 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]))) {
4703 if (nStart
< item
->member
.run
.strText
->nLen
&&
4704 (isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4705 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]))) {
4706 *candidate_min
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4707 lastAcceptedChar
= item
->member
.run
.strText
->szData
[nStart
];
4712 /* Find end of candidate */
4713 if (*candidate_min
>= 0) {
4714 while (nStart
< item
->member
.run
.strText
->nLen
&&
4715 (isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4716 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]) ||
4717 (!foundColon
&& item
->member
.run
.strText
->szData
[nStart
] == ':') )) {
4718 if (item
->member
.run
.strText
->szData
[nStart
] == ':') foundColon
= TRUE
;
4719 lastAcceptedChar
= item
->member
.run
.strText
->szData
[nStart
];
4722 if (nStart
< item
->member
.run
.strText
->nLen
&&
4723 !(isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4724 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]) )) {
4725 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4727 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4732 /* End of paragraph: skip it if before candidate span, or terminates
4733 current active span */
4734 if (*candidate_min
>= 0) {
4735 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
4736 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4741 /* Reaching this point means no span was found, so get next span */
4742 next_item
= ME_FindItemFwd(item
, diRun
);
4744 if (*candidate_min
>= 0) {
4745 /* There are no further runs, so take end of text as end of candidate */
4746 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4747 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4752 para
= ME_GetParagraph(item
);
4757 if (*candidate_min
>= 0) {
4758 /* There are no further runs, so take end of text as end of candidate */
4759 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4760 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4768 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4770 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, int sel_min
, int sel_max
)
4776 /* Code below depends on these being in decreasing length order! */
4790 LPWSTR bufferW
= NULL
;
4794 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4795 assert(sel_min
<= sel_max
);
4796 for (i
= 0; i
< sizeof(prefixes
) / sizeof(struct prefix_s
); i
++)
4798 if (sel_max
- sel_min
< prefixes
[i
].length
) continue;
4799 if (bufferW
== NULL
) {
4800 bufferW
= heap_alloc((sel_max
- sel_min
+ 1) * sizeof(WCHAR
));
4802 ME_GetTextW(editor
, bufferW
, sel_min
, min(sel_max
- sel_min
, lstrlenA(prefixes
[i
].text
)), 0);
4803 MultiByteToWideChar(CP_ACP
, 0, prefixes
[i
].text
, -1, bufW
, 32);
4804 if (!lstrcmpW(bufW
, bufferW
))
4815 * This proc walks through the indicated selection and evaluates whether each
4816 * section identified by ME_FindNextURLCandidate and in-between sections have
4817 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4818 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4820 * Returns TRUE if at least one section was modified.
4822 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, int sel_min
, int sel_max
)
4824 BOOL modified
= FALSE
;
4827 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4834 if (ME_FindNextURLCandidate(editor
, sel_min
, sel_max
, &cMin
, &cMax
))
4836 /* Section before candidate is not an URL */
4837 beforeURL
[0] = sel_min
;
4838 beforeURL
[1] = cMin
;
4840 if (ME_IsCandidateAnURL(editor
, cMin
, cMax
))
4842 inURL
[0] = cMin
; inURL
[1] = cMax
;
4846 beforeURL
[1] = cMax
;
4847 inURL
[0] = inURL
[1] = -1;
4853 /* No more candidates until end of selection */
4854 beforeURL
[0] = sel_min
;
4855 beforeURL
[1] = sel_max
;
4856 inURL
[0] = inURL
[1] = -1;
4860 if (beforeURL
[0] < beforeURL
[1])
4862 /* CFE_LINK effect should be consistently unset */
4863 link
.cbSize
= sizeof(link
);
4864 ME_GetCharFormat(editor
, beforeURL
[0], beforeURL
[1], &link
);
4865 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
4867 /* CFE_LINK must be unset from this range */
4868 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4869 link
.cbSize
= sizeof(link
);
4870 link
.dwMask
= CFM_LINK
;
4872 ME_SetCharFormat(editor
, beforeURL
[0], beforeURL
[1] - beforeURL
[0], &link
);
4876 if (inURL
[0] < inURL
[1])
4878 /* CFE_LINK effect should be consistently set */
4879 link
.cbSize
= sizeof(link
);
4880 ME_GetCharFormat(editor
, inURL
[0], inURL
[1], &link
);
4881 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
4883 /* CFE_LINK must be set on this range */
4884 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4885 link
.cbSize
= sizeof(link
);
4886 link
.dwMask
= CFM_LINK
;
4887 link
.dwEffects
= CFE_LINK
;
4888 ME_SetCharFormat(editor
, inURL
[0], inURL
[1] - inURL
[0], &link
);
4892 } while (sel_min
< sel_max
);