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)
68 + EM_GETSCROLLPOS 3.0 (only Y value valid)
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)
142 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 - WM_STYLECHANGED (things like read-only flag)
149 * EN_CHANGE (sent from the wrong place)
166 * EN_UPDATE (sent from the wrong place)
174 + ES_DISABLENOSCROLL (scrollbar is always visible)
175 - ES_EX_NOCALLOLEINIT
177 - ES_MULTILINE (currently single line controls aren't supported)
179 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
185 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
192 * RICHED20 TODO (incomplete):
194 * - messages/styles/notifications listed above
195 * - add remaining CHARFORMAT/PARAFORMAT fields
196 * - right/center align should strip spaces from the beginning
197 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
198 * - COM interface (looks like a major pain in the TODO list)
199 * - calculate heights of pictures (half-done)
200 * - horizontal scrolling (not even started)
201 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
203 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
204 * - italic caret with italic fonts
206 * - most notifications aren't sent at all (the most important ones are)
207 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
208 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
209 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
210 * - full justification
213 * - ListBox & ComboBox not implemented
215 * Bugs that are probably fixed, but not so easy to verify:
216 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
217 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
218 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
219 * - caret shouldn't be displayed when selection isn't empty
220 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
221 * - undo for setting default format (done, might be buggy)
222 * - styles might be not released properly (looks like they work like charm, but who knows?
229 #define NO_SHLWAPI_STREAM
235 #define STACK_SIZE_DEFAULT 100
236 #define STACK_SIZE_MAX 1000
238 #define TEXT_LIMIT_DEFAULT 32767
240 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
242 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
244 static const WCHAR RichEdit20W
[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
245 static const WCHAR RichEdit50W
[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
246 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
247 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
248 static HCURSOR hLeft
;
251 HANDLE me_heap
= NULL
;
253 static BOOL ME_ListBoxRegistered
= FALSE
;
254 static BOOL ME_ComboBoxRegistered
= FALSE
;
256 static inline int is_version_nt(void)
258 return !(GetVersion() & 0x80000000);
261 static ME_TextBuffer
*ME_MakeText(void) {
263 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
265 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
266 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
272 p1
->member
.para
.next_para
= p2
;
273 p2
->member
.para
.prev_para
= p1
;
274 p2
->member
.para
.nCharOfs
= 0;
278 buf
->pCharStyle
= NULL
;
284 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
286 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
289 TRACE("%08x %p\n", dwFormat
, stream
);
296 ME_StreamInFill(stream
);
297 if (stream
->editstream
->dwError
)
303 if (!(dwFormat
& SF_UNICODE
))
305 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
306 nWideChars
= MultiByteToWideChar(CP_ACP
, 0, stream
->buffer
, stream
->dwSize
, wszText
, STREAMIN_BUFFER_SIZE
);
311 nWideChars
= stream
->dwSize
>> 1;
312 pText
= (WCHAR
*)stream
->buffer
;
315 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
316 if (stream
->dwSize
== 0)
320 ME_CommitUndo(editor
);
321 ME_UpdateRepaint(editor
);
325 static void ME_ApplyBorderProperties(RTF_Info
*info
,
326 ME_BorderRect
*borderRect
,
327 RTFBorder
*borderDef
)
330 ME_Border
*pBorders
[] = {&borderRect
->top
,
334 for (i
= 0; i
< 4; i
++)
336 RTFColor
*colorDef
= info
->colorList
;
337 pBorders
[i
]->width
= borderDef
[i
].width
;
338 colorNum
= borderDef
[i
].color
;
339 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
340 colorDef
= colorDef
->rtfNextColor
;
342 pBorders
[i
]->colorRef
= RGB(
343 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
344 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
345 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
347 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
351 void ME_RTFCharAttrHook(RTF_Info
*info
)
354 fmt
.cbSize
= sizeof(fmt
);
358 switch(info
->rtfMinor
)
361 /* FIXME add more flags once they're implemented */
362 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
363 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
364 fmt
.yHeight
= 12*20; /* 12pt */
365 fmt
.wWeight
= FW_NORMAL
;
366 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
369 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
370 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
371 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
374 fmt
.dwMask
= CFM_ITALIC
;
375 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
378 fmt
.dwMask
= CFM_UNDERLINETYPE
;
379 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
381 case rtfDotUnderline
:
382 fmt
.dwMask
= CFM_UNDERLINETYPE
;
383 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
386 fmt
.dwMask
= CFM_UNDERLINETYPE
;
387 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
389 case rtfWordUnderline
:
390 fmt
.dwMask
= CFM_UNDERLINETYPE
;
391 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
394 fmt
.dwMask
= CFM_UNDERLINETYPE
;
395 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
398 fmt
.dwMask
= CFM_STRIKEOUT
;
399 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
403 case rtfSubScrShrink
:
404 case rtfSuperScrShrink
:
406 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
407 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
408 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
409 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
412 fmt
.dwMask
= CFM_HIDDEN
;
413 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
416 fmt
.dwMask
= CFM_BACKCOLOR
;
418 if (info
->rtfParam
== 0)
419 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
420 else if (info
->rtfParam
!= rtfNoParam
)
422 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
423 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
427 fmt
.dwMask
= CFM_COLOR
;
429 if (info
->rtfParam
== 0)
430 fmt
.dwEffects
= CFE_AUTOCOLOR
;
431 else if (info
->rtfParam
!= rtfNoParam
)
433 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
435 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
441 if (info
->rtfParam
!= rtfNoParam
)
443 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
446 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
447 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
448 fmt
.bCharSet
= f
->rtfFCharSet
;
449 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
450 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
455 fmt
.dwMask
= CFM_SIZE
;
456 if (info
->rtfParam
!= rtfNoParam
)
457 fmt
.yHeight
= info
->rtfParam
*10;
462 RTFFlushOutputBuffer(info
);
463 /* FIXME too slow ? how come ? */
464 style2
= ME_ApplyStyle(info
->style
, &fmt
);
465 ME_ReleaseStyle(info
->style
);
466 info
->style
= style2
;
467 info
->styleChanged
= TRUE
;
471 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
472 the same tags mean different things in different contexts */
473 void ME_RTFParAttrHook(RTF_Info
*info
)
476 fmt
.cbSize
= sizeof(fmt
);
479 switch(info
->rtfMinor
)
481 case rtfParDef
: /* restores default paragraph attributes */
482 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
483 info
->borderType
= RTFBorderParaLeft
;
484 else /* v1.0 - 3.0 */
485 info
->borderType
= RTFBorderParaTop
;
486 fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
487 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
489 /* TODO: numbering, shading */
490 fmt
.wAlignment
= PFA_LEFT
;
492 fmt
.dxOffset
= fmt
.dxStartIndent
= fmt
.dxRightIndent
= 0;
493 fmt
.wBorderWidth
= fmt
.wBorders
= 0;
494 fmt
.wBorderSpace
= 0;
495 fmt
.bLineSpacingRule
= 0;
496 fmt
.dySpaceBefore
= fmt
.dySpaceAfter
= 0;
497 fmt
.dyLineSpacing
= 0;
498 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
500 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
501 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
504 ME_DisplayItem
*para
;
505 /* We are just after a table row. */
506 RTFFlushOutputBuffer(info
);
507 cursor
= info
->editor
->pCursors
[0];
508 para
= ME_GetParagraph(cursor
.pRun
);
509 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
510 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
512 /* Since the table row end, no text has been inserted, and the \intbl
513 * control word has not be used. We can confirm that we are not in a
516 info
->tableDef
->tableRowStart
= NULL
;
517 info
->canInheritInTbl
= FALSE
;
520 } else { /* v1.0 - v3.0 */
521 fmt
.dwMask
|= PFM_TABLE
;
522 fmt
.wEffects
&= ~PFE_TABLE
;
526 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
528 while (info
->rtfParam
> info
->nestingLevel
) {
529 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
530 ZeroMemory(tableDef
, sizeof(RTFTable
));
531 tableDef
->parent
= info
->tableDef
;
532 info
->tableDef
= tableDef
;
534 RTFFlushOutputBuffer(info
);
535 if (tableDef
->tableRowStart
&&
536 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
538 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
539 para
= para
->member
.para
.next_para
;
540 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
541 tableDef
->tableRowStart
= para
;
545 cursor
= info
->editor
->pCursors
[0];
546 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
547 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
548 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
551 info
->nestingLevel
++;
553 info
->canInheritInTbl
= FALSE
;
558 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
560 if (info
->nestingLevel
< 1)
565 info
->tableDef
= ALLOC_OBJ(RTFTable
);
566 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
568 tableDef
= info
->tableDef
;
569 RTFFlushOutputBuffer(info
);
570 if (tableDef
->tableRowStart
&&
571 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
573 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
574 para
= para
->member
.para
.next_para
;
575 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
576 tableDef
->tableRowStart
= para
;
580 cursor
= info
->editor
->pCursors
[0];
581 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
582 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
583 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
585 info
->nestingLevel
= 1;
586 info
->canInheritInTbl
= TRUE
;
589 } else { /* v1.0 - v3.0 */
590 fmt
.dwMask
|= PFM_TABLE
;
591 fmt
.wEffects
|= PFE_TABLE
;
596 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
597 fmt
.dwMask
= PFM_STARTINDENT
| PFM_OFFSET
;
598 fmt
.dxStartIndent
+= fmt
.dxOffset
+ info
->rtfParam
;
599 fmt
.dxOffset
= -info
->rtfParam
;
602 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
603 fmt
.dwMask
= PFM_STARTINDENT
;
604 fmt
.dxStartIndent
= info
->rtfParam
- fmt
.dxOffset
;
607 fmt
.dwMask
= PFM_RIGHTINDENT
;
608 fmt
.dxRightIndent
= info
->rtfParam
;
612 fmt
.dwMask
= PFM_ALIGNMENT
;
613 fmt
.wAlignment
= PFA_LEFT
;
616 fmt
.dwMask
= PFM_ALIGNMENT
;
617 fmt
.wAlignment
= PFA_RIGHT
;
620 fmt
.dwMask
= PFM_ALIGNMENT
;
621 fmt
.wAlignment
= PFA_CENTER
;
624 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
625 if (!(fmt
.dwMask
& PFM_TABSTOPS
))
629 if (fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
630 fmt
.rgxTabs
[fmt
.cTabCount
++] = info
->rtfParam
;
631 fmt
.dwMask
= PFM_TABSTOPS
;
634 fmt
.dwMask
= PFM_KEEP
;
635 fmt
.wEffects
= PFE_KEEP
;
637 case rtfNoWidowControl
:
638 fmt
.dwMask
= PFM_NOWIDOWCONTROL
;
639 fmt
.wEffects
= PFE_NOWIDOWCONTROL
;
642 fmt
.dwMask
= PFM_KEEPNEXT
;
643 fmt
.wEffects
= PFE_KEEPNEXT
;
646 fmt
.dwMask
= PFM_SPACEAFTER
;
647 fmt
.dySpaceAfter
= info
->rtfParam
;
650 fmt
.dwMask
= PFM_SPACEBEFORE
;
651 fmt
.dySpaceBefore
= info
->rtfParam
;
653 case rtfSpaceBetween
:
654 fmt
.dwMask
= PFM_LINESPACING
;
655 if ((int)info
->rtfParam
> 0)
657 fmt
.dyLineSpacing
= info
->rtfParam
;
658 fmt
.bLineSpacingRule
= 3;
662 fmt
.dyLineSpacing
= info
->rtfParam
;
663 fmt
.bLineSpacingRule
= 4;
665 case rtfSpaceMultiply
:
666 fmt
.dwMask
= PFM_LINESPACING
;
667 fmt
.dyLineSpacing
= info
->rtfParam
* 20;
668 fmt
.bLineSpacingRule
= 5;
671 fmt
.dwMask
= PFM_NUMBERING
;
672 fmt
.wNumbering
= PFN_BULLET
;
675 fmt
.dwMask
= PFM_NUMBERING
;
676 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
678 case rtfParNumDecimal
:
679 fmt
.dwMask
= PFM_NUMBERING
;
680 fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
682 case rtfParNumIndent
:
683 fmt
.dwMask
= PFM_NUMBERINGTAB
;
684 fmt
.wNumberingTab
= info
->rtfParam
;
686 case rtfParNumStartAt
:
687 fmt
.dwMask
= PFM_NUMBERINGSTART
;
688 fmt
.wNumberingStart
= info
->rtfParam
;
691 info
->borderType
= RTFBorderParaLeft
;
692 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
693 if (!(fmt
.dwMask
& PFM_BORDER
))
695 fmt
.wBorderSpace
= 0;
696 fmt
.wBorderWidth
= 1;
700 fmt
.dwMask
= PFM_BORDER
;
703 info
->borderType
= RTFBorderParaRight
;
704 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
705 if (!(fmt
.dwMask
& PFM_BORDER
))
707 fmt
.wBorderSpace
= 0;
708 fmt
.wBorderWidth
= 1;
712 fmt
.dwMask
= PFM_BORDER
;
715 info
->borderType
= RTFBorderParaTop
;
716 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
717 if (!(fmt
.dwMask
& PFM_BORDER
))
719 fmt
.wBorderSpace
= 0;
720 fmt
.wBorderWidth
= 1;
724 fmt
.dwMask
= PFM_BORDER
;
726 case rtfBorderBottom
:
727 info
->borderType
= RTFBorderParaBottom
;
728 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
729 if (!(fmt
.dwMask
& PFM_BORDER
))
731 fmt
.wBorderSpace
= 0;
732 fmt
.wBorderWidth
= 1;
736 fmt
.dwMask
= PFM_BORDER
;
738 case rtfBorderSingle
:
739 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
740 /* we assume that borders have been created before (RTF spec) */
741 fmt
.wBorders
&= ~0x700;
742 fmt
.wBorders
|= 1 << 8;
743 fmt
.dwMask
= PFM_BORDER
;
746 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
747 /* we assume that borders have been created before (RTF spec) */
748 fmt
.wBorders
&= ~0x700;
749 fmt
.wBorders
|= 2 << 8;
750 fmt
.dwMask
= PFM_BORDER
;
752 case rtfBorderShadow
:
753 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
754 /* we assume that borders have been created before (RTF spec) */
755 fmt
.wBorders
&= ~0x700;
756 fmt
.wBorders
|= 10 << 8;
757 fmt
.dwMask
= PFM_BORDER
;
759 case rtfBorderDouble
:
760 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
761 /* we assume that borders have been created before (RTF spec) */
762 fmt
.wBorders
&= ~0x700;
763 fmt
.wBorders
|= 7 << 8;
764 fmt
.dwMask
= PFM_BORDER
;
767 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
768 /* we assume that borders have been created before (RTF spec) */
769 fmt
.wBorders
&= ~0x700;
770 fmt
.wBorders
|= 11 << 8;
771 fmt
.dwMask
= PFM_BORDER
;
775 int borderSide
= info
->borderType
& RTFBorderSideMask
;
776 RTFTable
*tableDef
= info
->tableDef
;
777 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
778 /* we assume that borders have been created before (RTF spec) */
779 fmt
.wBorderWidth
|= ((info
->rtfParam
/ 15) & 7) << 8;
780 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
783 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
785 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
786 border
->width
= info
->rtfParam
;
789 fmt
.dwMask
= PFM_BORDER
;
793 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
794 /* we assume that borders have been created before (RTF spec) */
795 fmt
.wBorderSpace
= info
->rtfParam
;
796 fmt
.dwMask
= PFM_BORDER
;
800 RTFTable
*tableDef
= info
->tableDef
;
801 int borderSide
= info
->borderType
& RTFBorderSideMask
;
802 int borderType
= info
->borderType
& RTFBorderTypeMask
;
805 case RTFBorderTypePara
:
806 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
808 /* v1.0 - 3.0 treat paragraph and row borders the same. */
809 case RTFBorderTypeRow
:
811 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
814 case RTFBorderTypeCell
:
815 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
816 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
824 RTFFlushOutputBuffer(info
);
825 /* FIXME too slow ? how come ?*/
826 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
830 void ME_RTFTblAttrHook(RTF_Info
*info
)
832 switch (info
->rtfMinor
)
836 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
837 info
->borderType
= 0; /* Not sure */
838 else /* v1.0 - 3.0 */
839 info
->borderType
= RTFBorderRowTop
;
840 if (!info
->tableDef
) {
841 info
->tableDef
= ME_MakeTableDef(info
->editor
);
843 ME_InitTableDef(info
->editor
, info
->tableDef
);
852 info
->tableDef
= ME_MakeTableDef(info
->editor
);
854 cellNum
= info
->tableDef
->numCellsDefined
;
855 if (cellNum
>= MAX_TABLE_CELLS
)
857 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
858 if (cellNum
< MAX_TAB_STOPS
) {
859 /* Tab stops were used to store cell positions before v4.1 but v4.1
860 * still seems to set the tabstops without using them. */
861 ME_DisplayItem
*para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
862 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
863 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
864 pFmt
->rgxTabs
[cellNum
] = 0x00FFFFFF & info
->rtfParam
;
866 info
->tableDef
->numCellsDefined
++;
870 info
->borderType
= RTFBorderRowTop
;
873 info
->borderType
= RTFBorderRowLeft
;
875 case rtfRowBordBottom
:
876 info
->borderType
= RTFBorderRowBottom
;
878 case rtfRowBordRight
:
879 info
->borderType
= RTFBorderRowRight
;
882 info
->borderType
= RTFBorderCellTop
;
884 case rtfCellBordLeft
:
885 info
->borderType
= RTFBorderCellLeft
;
887 case rtfCellBordBottom
:
888 info
->borderType
= RTFBorderCellBottom
;
890 case rtfCellBordRight
:
891 info
->borderType
= RTFBorderCellRight
;
895 info
->tableDef
->gapH
= info
->rtfParam
;
899 info
->tableDef
->leftEdge
= info
->rtfParam
;
904 void ME_RTFSpecialCharHook(RTF_Info
*info
)
906 RTFTable
*tableDef
= info
->tableDef
;
907 switch (info
->rtfMinor
)
910 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
912 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
916 RTFFlushOutputBuffer(info
);
917 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
918 if (tableDef
->tableRowStart
)
920 if (!info
->nestingLevel
&&
921 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
923 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
924 para
= para
->member
.para
.next_para
;
925 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
926 tableDef
->tableRowStart
= para
;
927 info
->nestingLevel
= 1;
929 ME_InsertTableCellFromCursor(info
->editor
);
931 } else { /* v1.0 - v3.0 */
932 ME_DisplayItem
*para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
933 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
934 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
935 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
938 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
939 tableDef
->numCellsInserted
++;
944 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
946 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
949 ME_DisplayItem
*para
, *cell
, *run
;
954 RTFFlushOutputBuffer(info
);
955 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
956 if (!tableDef
->tableRowStart
)
958 if (!info
->nestingLevel
&&
959 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
961 para
= tableDef
->tableRowStart
;
962 para
= para
->member
.para
.next_para
;
963 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
964 tableDef
->tableRowStart
= para
;
965 info
->nestingLevel
++;
967 para
= tableDef
->tableRowStart
;
968 cell
= ME_FindItemFwd(para
, diCell
);
969 assert(cell
&& !cell
->member
.cell
.prev_cell
);
970 if (tableDef
->numCellsDefined
< 1)
972 /* 2000 twips appears to be the cell size that native richedit uses
973 * when no cell sizes are specified. */
974 const int defaultCellSize
= 2000;
975 int nRightBoundary
= defaultCellSize
;
976 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
977 while (cell
->member
.cell
.next_cell
) {
978 cell
= cell
->member
.cell
.next_cell
;
979 nRightBoundary
+= defaultCellSize
;
980 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
982 para
= ME_InsertTableCellFromCursor(info
->editor
);
983 cell
= para
->member
.para
.pCell
;
984 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
986 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
988 RTFCell
*cellDef
= &tableDef
->cells
[i
];
989 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
990 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
992 cell
= cell
->member
.cell
.next_cell
;
995 para
= ME_InsertTableCellFromCursor(info
->editor
);
996 cell
= para
->member
.para
.pCell
;
999 /* Cell for table row delimiter is empty */
1000 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1003 run
= ME_FindItemFwd(cell
, diRun
);
1004 if (info
->editor
->pCursors
[0].pRun
!= run
||
1005 info
->editor
->pCursors
[0].nOffset
)
1008 /* Delete inserted cells that aren't defined. */
1009 info
->editor
->pCursors
[1].pRun
= run
;
1010 info
->editor
->pCursors
[1].nOffset
= 0;
1011 nOfs
= ME_GetCursorOfs(info
->editor
, 1);
1012 nChars
= ME_GetCursorOfs(info
->editor
, 0) - nOfs
;
1013 ME_InternalDeleteText(info
->editor
, nOfs
, nChars
, TRUE
);
1016 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1017 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1018 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1019 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1021 info
->nestingLevel
--;
1022 if (!info
->nestingLevel
)
1024 if (info
->canInheritInTbl
) {
1025 tableDef
->tableRowStart
= para
;
1027 while (info
->tableDef
) {
1028 tableDef
= info
->tableDef
;
1029 info
->tableDef
= tableDef
->parent
;
1030 heap_free(tableDef
);
1034 info
->tableDef
= tableDef
->parent
;
1035 heap_free(tableDef
);
1037 } else { /* v1.0 - v3.0 */
1039 ME_DisplayItem
*para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
1040 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1041 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1042 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1044 para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
1045 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1047 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1050 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1051 tableDef
->numCellsInserted
++;
1053 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1054 if (!tableDef
->numCellsDefined
)
1055 pFmt
->wEffects
&= ~PFE_TABLE
;
1056 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1057 tableDef
->numCellsInserted
= 0;
1063 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1064 ME_DisplayItem
*para
;
1066 RTFFlushOutputBuffer(info
);
1067 para
= ME_GetParagraph(info
->editor
->pCursors
[0].pRun
);
1068 pFmt
= para
->member
.para
.pFmt
;
1069 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1071 /* rtfPar is treated like a space within a table. */
1072 info
->rtfClass
= rtfText
;
1073 info
->rtfMajor
= ' ';
1075 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1076 tableDef
->numCellsInserted
= 0;
1082 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1085 LPOLEOBJECT lpObject
= NULL
;
1086 LPSTORAGE lpStorage
= NULL
;
1087 LPOLECLIENTSITE lpClientSite
= NULL
;
1088 LPDATAOBJECT lpDataObject
= NULL
;
1089 LPOLECACHE lpOleCache
= NULL
;
1098 stgm
.tymed
= TYMED_ENHMF
;
1099 stgm
.u
.hEnhMetaFile
= hemf
;
1100 fm
.cfFormat
= CF_ENHMETAFILE
;
1104 stgm
.tymed
= TYMED_GDI
;
1105 stgm
.u
.hBitmap
= hbmp
;
1106 fm
.cfFormat
= CF_BITMAP
;
1108 stgm
.pUnkForRelease
= NULL
;
1111 fm
.dwAspect
= DVASPECT_CONTENT
;
1113 fm
.tymed
= stgm
.tymed
;
1115 if (!info
->lpRichEditOle
)
1117 CreateIRichEditOle(info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1120 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1122 /* FIXME: enable it when rich-edit properly implements this method */
1123 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1124 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1126 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1127 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1128 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1129 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1130 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1134 reobject
.cbStruct
= sizeof(reobject
);
1135 reobject
.cp
= REO_CP_SELECTION
;
1136 reobject
.clsid
= clsid
;
1137 reobject
.poleobj
= lpObject
;
1138 reobject
.pstg
= lpStorage
;
1139 reobject
.polesite
= lpClientSite
;
1140 /* convert from twips to .01 mm */
1141 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1142 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1143 reobject
.dvaspect
= DVASPECT_CONTENT
;
1144 reobject
.dwFlags
= 0; /* FIXME */
1145 reobject
.dwUser
= 0;
1147 /* FIXME: could be simpler */
1148 ret
= IRichEditOle_InsertObject(info
->lpRichEditOle
, &reobject
) == S_OK
;
1151 if (lpObject
) IOleObject_Release(lpObject
);
1152 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1153 if (lpStorage
) IStorage_Release(lpStorage
);
1154 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1155 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1160 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1163 BYTE
* buffer
= NULL
;
1164 unsigned bufsz
, bufidx
;
1170 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1173 if (info
->rtfClass
== rtfEOF
)
1176 /* fetch picture type */
1177 if (RTFCheckMM (info
, rtfPictAttr
, rtfWinMetafile
))
1179 mfp
.mm
= info
->rtfParam
;
1182 else if (RTFCheckMM (info
, rtfPictAttr
, rtfDevIndBitmap
))
1184 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1187 else if (RTFCheckMM (info
, rtfPictAttr
, rtfEmfBlip
))
1189 gfx
= gfx_enhmetafile
;
1193 FIXME("%d %d\n", info
->rtfMajor
, info
->rtfMinor
);
1197 /* fetch picture attributes */
1201 if (info
->rtfClass
== rtfEOF
)
1203 if (info
->rtfClass
== rtfText
)
1205 if (!RTFCheckCM (info
, rtfControl
, rtfPictAttr
))
1207 ERR("Expected picture attribute (%d %d)\n",
1208 info
->rtfClass
, info
->rtfMajor
);
1211 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicWid
))
1213 if (gfx
== gfx_metafile
) mfp
.xExt
= info
->rtfParam
;
1215 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicHt
))
1217 if (gfx
== gfx_metafile
) mfp
.yExt
= info
->rtfParam
;
1219 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalWid
))
1220 sz
.cx
= info
->rtfParam
;
1221 else if (RTFCheckMM (info
, rtfPictAttr
, rtfPicGoalHt
))
1222 sz
.cy
= info
->rtfParam
;
1224 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1226 /* fetch picture data */
1229 buffer
= HeapAlloc(GetProcessHeap(), 0, bufsz
);
1230 val
= info
->rtfMajor
;
1231 for (flip
= TRUE
;; flip
= !flip
)
1234 if (info
->rtfClass
== rtfEOF
)
1236 HeapFree(GetProcessHeap(), 0, buffer
);
1237 return; /* Warn ?? */
1239 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1241 if (info
->rtfClass
!= rtfText
) goto skip_group
;
1244 if (bufidx
>= bufsz
&&
1245 !(buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, bufsz
+= 1024)))
1247 buffer
[bufidx
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1250 val
= info
->rtfMajor
;
1252 if (flip
) FIXME("wrong hex string\n");
1256 case gfx_enhmetafile
:
1257 if ((hemf
= SetEnhMetaFileBits(bufidx
, buffer
)))
1258 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1261 if ((hemf
= SetWinMetaFileBits(bufidx
, buffer
, NULL
, &mfp
)))
1262 ME_RTFInsertOleObject(info
, hemf
, NULL
, &sz
);
1266 BITMAPINFO
* bi
= (BITMAPINFO
*)buffer
;
1268 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1270 /* not quite right, especially for bitfields type of compression */
1271 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1272 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1273 if ((hbmp
= CreateDIBitmap(hdc
, &bi
->bmiHeader
,
1274 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1275 bi
, DIB_RGB_COLORS
)))
1276 ME_RTFInsertOleObject(info
, NULL
, hbmp
, &sz
);
1283 HeapFree(GetProcessHeap(), 0, buffer
);
1284 RTFRouteToken (info
); /* feed "}" back to router */
1287 HeapFree(GetProcessHeap(), 0, buffer
);
1289 RTFRouteToken(info
); /* feed "}" back to router */
1292 /* for now, lookup the \result part and use it, whatever the object */
1293 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1298 if (info
->rtfClass
== rtfEOF
)
1300 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1302 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1305 if (info
->rtfClass
== rtfEOF
)
1307 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1311 while (RTFGetToken (info
) != rtfEOF
)
1313 if (info
->rtfClass
== rtfGroup
)
1315 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1316 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1318 RTFRouteToken(info
);
1321 else RTFSkipGroup(info
);
1324 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1326 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1330 RTFRouteToken(info
); /* feed "}" back to router */
1333 static void ME_RTFReadHook(RTF_Info
*info
)
1335 switch(info
->rtfClass
)
1338 switch(info
->rtfMajor
)
1341 if (info
->stackTop
< maxStack
) {
1342 info
->stack
[info
->stackTop
].fmt
= info
->style
->fmt
;
1343 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1344 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1347 info
->styleChanged
= FALSE
;
1352 RTFFlushOutputBuffer(info
);
1354 if (info
->stackTop
<=0) {
1355 info
->rtfClass
= rtfEOF
;
1358 assert(info
->stackTop
>= 0);
1359 if (info
->styleChanged
)
1361 /* FIXME too slow ? how come ? */
1362 s
= ME_ApplyStyle(info
->style
, &info
->stack
[info
->stackTop
].fmt
);
1363 ME_ReleaseStyle(info
->style
);
1365 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1366 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1376 ME_StreamInFill(ME_InStream
*stream
)
1378 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1379 (BYTE
*)stream
->buffer
,
1380 sizeof(stream
->buffer
),
1381 (LONG
*)&stream
->dwSize
);
1385 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1389 int from
, to
, to2
, nUndoMode
;
1390 int nEventMask
= editor
->nEventMask
;
1391 ME_InStream inStream
;
1392 BOOL invalidRTF
= FALSE
;
1394 TRACE("stream==%p hWnd==%p format==0x%X\n", stream
, editor
->hWnd
, format
);
1395 editor
->nEventMask
= 0;
1397 ME_GetSelection(editor
, &from
, &to
);
1398 if ((format
& SFF_SELECTION
) && (editor
->mode
& TM_RICHTEXT
)) {
1399 style
= ME_GetSelectionInsertStyle(editor
);
1401 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
1403 /* Don't insert text at the end of the table row */
1404 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1405 ME_DisplayItem
*para
= ME_GetParagraph(editor
->pCursors
->pRun
);
1406 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1408 para
= para
->member
.para
.next_para
;
1409 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1410 editor
->pCursors
[0].nOffset
= 0;
1412 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1414 para
= para
->member
.para
.next_para
;
1415 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1416 editor
->pCursors
[0].nOffset
= 0;
1418 editor
->pCursors
[1] = editor
->pCursors
[0];
1419 } else { /* v1.0 - 3.0 */
1420 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1421 ME_IsInTable(editor
->pCursors
[0].pRun
))
1426 ME_DisplayItem
*para_item
;
1427 style
= editor
->pBuffer
->pDefaultStyle
;
1428 ME_AddRefStyle(style
);
1429 ME_SetSelection(editor
, 0, 0);
1430 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
1432 ME_ClearTempStyle(editor
);
1434 para_item
= ME_GetParagraph(editor
->pCursors
[0].pRun
);
1435 ME_SetDefaultParaFormat(para_item
->member
.para
.pFmt
);
1439 /* Back up undo mode to a local variable */
1440 nUndoMode
= editor
->nUndoMode
;
1442 /* Only create an undo if SFF_SELECTION is set */
1443 if (!(format
& SFF_SELECTION
))
1444 editor
->nUndoMode
= umIgnore
;
1446 inStream
.editstream
= stream
;
1447 inStream
.editstream
->dwError
= 0;
1448 inStream
.dwSize
= 0;
1449 inStream
.dwUsed
= 0;
1451 if (format
& SF_RTF
)
1453 /* Check if it's really RTF, and if it is not, use plain text */
1454 ME_StreamInFill(&inStream
);
1455 if (!inStream
.editstream
->dwError
)
1457 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1458 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1461 inStream
.editstream
->dwError
= -16;
1466 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1468 if (format
& SF_RTF
) {
1469 /* setup the RTF parser */
1470 memset(&parser
, 0, sizeof parser
);
1471 RTFSetEditStream(&parser
, &inStream
);
1472 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1473 parser
.editor
= editor
;
1474 parser
.style
= style
;
1475 WriterInit(&parser
);
1477 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1478 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1479 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1480 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1482 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1483 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1487 /* do the parsing */
1489 RTFFlushOutputBuffer(&parser
);
1490 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1491 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1492 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1494 /* Delete any incomplete table row at the end of the rich text. */
1496 ME_DisplayItem
*pCell
;
1497 ME_DisplayItem
*para
;
1499 parser
.rtfMinor
= rtfRow
;
1500 /* Complete the table row before deleting it.
1501 * By doing it this way we will have the current paragraph format set
1502 * properly to reflect that is not in the complete table, and undo items
1503 * will be added for this change to the current paragraph format. */
1504 if (parser
.nestingLevel
> 0)
1506 while (parser
.nestingLevel
> 1)
1507 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1508 para
= parser
.tableDef
->tableRowStart
;
1509 ME_RTFSpecialCharHook(&parser
);
1511 para
= parser
.tableDef
->tableRowStart
;
1512 ME_RTFSpecialCharHook(&parser
);
1513 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1514 para
= para
->member
.para
.next_para
;
1516 pCell
= para
->member
.para
.pCell
;
1518 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1519 editor
->pCursors
[1].nOffset
= 0;
1520 nOfs
= ME_GetCursorOfs(editor
, 1);
1521 nChars
= ME_GetCursorOfs(editor
, 0) - nOfs
;
1522 ME_InternalDeleteText(editor
, nOfs
, nChars
, TRUE
);
1523 if (parser
.tableDef
)
1524 parser
.tableDef
->tableRowStart
= NULL
;
1527 ME_CheckTablesForCorruption(editor
);
1528 RTFDestroy(&parser
);
1529 if (parser
.lpRichEditOle
)
1530 IRichEditOle_Release(parser
.lpRichEditOle
);
1532 if (!inStream
.editstream
->dwError
&& parser
.stackTop
> 0)
1533 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1535 /* Remove last line break, as mandated by tests. This is not affected by
1536 CR/LF counters, since RTF streaming presents only \para tokens, which
1537 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1541 ME_GetSelection(editor
, &newfrom
, &newto
);
1542 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1543 WCHAR lastchar
[3] = {'\0', '\0'};
1544 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1546 ME_GetTextW(editor
, lastchar
, newto
- linebreakSize
, linebreakSize
, 0);
1547 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1548 ME_InternalDeleteText(editor
, newto
- linebreakSize
, linebreakSize
, FALSE
);
1553 style
= parser
.style
;
1555 else if (format
& SF_TEXT
)
1556 ME_StreamInText(editor
, format
, &inStream
, style
);
1558 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1559 ME_GetSelection(editor
, &to
, &to2
);
1560 /* put the cursor at the top */
1561 if (!(format
& SFF_SELECTION
))
1562 ME_SetSelection(editor
, 0, 0);
1565 /* Restore saved undo mode */
1566 editor
->nUndoMode
= nUndoMode
;
1568 /* even if we didn't add an undo, we need to commit anything on the stack */
1569 ME_CommitUndo(editor
);
1571 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1572 if (!(format
& SFF_SELECTION
))
1573 ME_EmptyUndoStack(editor
);
1575 ME_ReleaseStyle(style
);
1576 editor
->nEventMask
= nEventMask
;
1577 ME_UpdateRepaint(editor
);
1578 if (!(format
& SFF_SELECTION
)) {
1579 ME_ClearTempStyle(editor
);
1581 HideCaret(editor
->hWnd
);
1582 ME_MoveCaret(editor
);
1583 ShowCaret(editor
->hWnd
);
1584 ME_SendSelChange(editor
);
1585 ME_SendRequestResize(editor
, FALSE
);
1591 typedef struct tagME_RTFStringStreamStruct
1596 } ME_RTFStringStreamStruct
;
1598 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1600 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1603 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1604 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1605 pStruct
->pos
+= count
;
1611 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1614 ME_RTFStringStreamStruct data
;
1616 data
.string
= string
;
1617 data
.length
= strlen(string
);
1619 es
.dwCookie
= (DWORD_PTR
)&data
;
1620 es
.pfnCallback
= ME_ReadFromRTFString
;
1621 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, FALSE
);
1626 ME_FindItemAtOffset(ME_TextEditor
*editor
, ME_DIType nItemType
, int nOffset
, int *nItemOffset
)
1628 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
1631 while (item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= nOffset
)
1632 item
= ME_FindItemFwd(item
, diParagraph
);
1637 nOffset
-= item
->member
.para
.nCharOfs
;
1638 if (nItemType
== diParagraph
) {
1640 *nItemOffset
= nOffset
;
1645 item
= ME_FindItemFwd(item
, diRun
);
1646 runLength
= ME_StrLen(item
->member
.run
.strText
);
1647 if (item
->member
.run
.nFlags
& MERF_ENDPARA
)
1648 runLength
= item
->member
.run
.nCR
+ item
->member
.run
.nLF
;
1649 } while (item
&& (item
->member
.run
.nCharOfs
+ runLength
<= nOffset
));
1651 nOffset
-= item
->member
.run
.nCharOfs
;
1653 /* Special case: nOffset may not point exactly at the division between the
1654 \r and the \n in 1.0 emulation. If such a case happens, it is sent
1655 into the next run, if one exists
1657 if ( item
->member
.run
.nFlags
& MERF_ENDPARA
1658 && nOffset
== item
->member
.run
.nCR
1659 && item
->member
.run
.nLF
> 0) {
1660 ME_DisplayItem
*nextItem
;
1661 nextItem
= ME_FindItemFwd(item
, diRun
);
1668 *nItemOffset
= nOffset
;
1675 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1677 const int nLen
= lstrlenW(text
);
1678 const int nTextLen
= ME_GetTextLength(editor
);
1681 ME_DisplayItem
*item
;
1682 ME_DisplayItem
*para
;
1683 WCHAR wLastChar
= ' ';
1685 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1686 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1688 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1689 FIXME("Flags 0x%08x not implemented\n",
1690 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1693 if (chrg
->cpMax
== -1)
1696 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1698 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1699 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1704 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1705 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1709 chrgText
->cpMin
= -1;
1710 chrgText
->cpMax
= -1;
1715 /* when searching up, if cpMin < cpMax, then instead of searching
1716 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1717 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1718 * case, it is always bigger than cpMin.
1720 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1724 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1725 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1731 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1734 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1738 if (flags
& FR_DOWN
) /* Forward search */
1740 /* If possible, find the character before where the search starts */
1741 if ((flags
& FR_WHOLEWORD
) && nMin
)
1744 item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1748 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1751 wLastChar
= item
->member
.run
.strText
->szData
[nStart
];
1755 item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1759 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1763 para
= ME_GetParagraph(item
);
1765 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
+ nLen
<= nMax
)
1767 ME_DisplayItem
*pCurItem
= item
;
1768 int nCurStart
= nStart
;
1771 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
], text
[nMatched
], (flags
& FR_MATCHCASE
)))
1773 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1777 if (nMatched
== nLen
)
1779 ME_DisplayItem
*pNextItem
= pCurItem
;
1780 int nNextStart
= nCurStart
;
1783 /* Check to see if next character is a whitespace */
1784 if (flags
& FR_WHOLEWORD
)
1786 if (nCurStart
+ nMatched
== ME_StrLen(pCurItem
->member
.run
.strText
))
1788 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1789 nNextStart
= -nMatched
;
1793 wNextChar
= pNextItem
->member
.run
.strText
->szData
[nNextStart
+ nMatched
];
1797 if (isalnumW(wNextChar
))
1801 nStart
+= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
1804 chrgText
->cpMin
= nStart
;
1805 chrgText
->cpMax
= nStart
+ nLen
;
1807 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1810 if (nCurStart
+ nMatched
== ME_StrLen(pCurItem
->member
.run
.strText
))
1812 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1813 para
= ME_GetParagraph(pCurItem
);
1814 nCurStart
= -nMatched
;
1818 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurStart
+ nMatched
];
1823 if (nStart
== ME_StrLen(item
->member
.run
.strText
))
1825 item
= ME_FindItemFwd(item
, diRun
);
1826 para
= ME_GetParagraph(item
);
1831 else /* Backward search */
1833 /* If possible, find the character after where the search ends */
1834 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1837 item
= ME_FindItemAtOffset(editor
, diRun
, nEnd
, &nEnd
);
1841 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1844 wLastChar
= item
->member
.run
.strText
->szData
[nEnd
];
1848 item
= ME_FindItemAtOffset(editor
, diRun
, nEnd
, &nEnd
);
1852 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1856 para
= ME_GetParagraph(item
);
1859 && para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nEnd
- nLen
>= nMin
)
1861 ME_DisplayItem
*pCurItem
= item
;
1865 if (nCurEnd
- nMatched
== 0)
1867 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1868 para
= ME_GetParagraph(pCurItem
);
1869 nCurEnd
= ME_StrLen(pCurItem
->member
.run
.strText
) + nMatched
;
1872 while (pCurItem
&& ME_CharCompare(pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1], text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
)))
1874 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1878 if (nMatched
== nLen
)
1880 ME_DisplayItem
*pPrevItem
= pCurItem
;
1881 int nPrevEnd
= nCurEnd
;
1884 /* Check to see if previous character is a whitespace */
1885 if (flags
& FR_WHOLEWORD
)
1887 if (nPrevEnd
- nMatched
== 0)
1889 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1891 nPrevEnd
= ME_StrLen(pPrevItem
->member
.run
.strText
) + nMatched
;
1895 wPrevChar
= pPrevItem
->member
.run
.strText
->szData
[nPrevEnd
- nMatched
- 1];
1899 if (isalnumW(wPrevChar
))
1903 nStart
= para
->member
.para
.nCharOfs
+ pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1906 chrgText
->cpMin
= nStart
;
1907 chrgText
->cpMax
= nStart
+ nLen
;
1909 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1912 if (nCurEnd
- nMatched
== 0)
1914 pCurItem
= ME_FindItemBack(pCurItem
, diRun
);
1915 /* Don't care about pCurItem becoming NULL here; it's already taken
1916 * care of in the exterior loop condition */
1917 para
= ME_GetParagraph(pCurItem
);
1918 nCurEnd
= ME_StrLen(pCurItem
->member
.run
.strText
) + nMatched
;
1922 wLastChar
= pCurItem
->member
.run
.strText
->szData
[nCurEnd
- nMatched
- 1];
1929 item
= ME_FindItemBack(item
, diRun
);
1930 para
= ME_GetParagraph(item
);
1931 nEnd
= ME_StrLen(item
->member
.run
.strText
);
1935 TRACE("not found\n");
1937 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1941 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1943 int nStart
, nCount
; /* in chars */
1945 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1946 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1948 if (ex
->flags
& GT_SELECTION
)
1950 ME_GetSelection(editor
, &nStart
, &nCount
);
1956 nCount
= 0x7fffffff;
1958 if (ex
->codepage
== 1200)
1960 nCount
= min(nCount
, ex
->cb
/ sizeof(WCHAR
) - 1);
1961 return ME_GetTextW(editor
, (LPWSTR
)pText
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
1965 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1966 we can just take a bigger buffer? :)
1967 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1968 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1970 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
1972 DWORD buflen
= ex
->cb
;
1976 nCount
= min(nCount
, ex
->cb
- 1);
1977 buffer
= heap_alloc((crlfmul
*nCount
+ 1) * sizeof(WCHAR
));
1979 buflen
= ME_GetTextW(editor
, buffer
, nStart
, nCount
, ex
->flags
& GT_USECRLF
);
1980 rc
= WideCharToMultiByte(ex
->codepage
, flags
, buffer
, buflen
+1,
1981 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
1982 if (rc
) rc
--; /* do not count 0 terminator */
1989 static int ME_GetTextRange(ME_TextEditor
*editor
, TEXTRANGEW
*rng
, BOOL unicode
)
1992 return ME_GetTextW(editor
, rng
->lpstrText
, rng
->chrg
.cpMin
,
1993 rng
->chrg
.cpMax
-rng
->chrg
.cpMin
, 0);
1996 int nLen
= rng
->chrg
.cpMax
-rng
->chrg
.cpMin
;
1997 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
1998 int nChars
= ME_GetTextW(editor
, p
, rng
->chrg
.cpMin
, nLen
, 0);
1999 /* FIXME this is a potential security hole (buffer overrun)
2000 if you know more about wchar->mbyte conversion please explain
2002 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)rng
->lpstrText
, nLen
+1, NULL
, NULL
);
2008 typedef struct tagME_GlobalDestStruct
2012 } ME_GlobalDestStruct
;
2014 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2016 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2021 pDest
= (WORD
*)lpBuff
;
2022 pSrc
= GlobalLock(pData
->hData
);
2023 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2024 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2026 pData
->nLength
+= i
;
2028 GlobalUnlock(pData
->hData
);
2032 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2034 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2039 pSrc
= GlobalLock(pData
->hData
);
2040 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2041 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2043 pData
->nLength
+= i
;
2045 GlobalUnlock(pData
->hData
);
2049 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2053 ME_GlobalDestStruct gds
;
2054 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2057 if (IsClipboardFormatAvailable(nRTFFormat
))
2058 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2059 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2060 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2064 if (!OpenClipboard(editor
->hWnd
))
2066 gds
.hData
= GetClipboardData(cf
);
2068 es
.dwCookie
= (DWORD_PTR
)&gds
;
2069 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2070 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2076 static BOOL
ME_Copy(ME_TextEditor
*editor
, CHARRANGE
*range
)
2078 LPDATAOBJECT dataObj
= NULL
;
2081 if (editor
->cPasswordMask
)
2082 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2084 if(editor
->lpOleCallback
)
2085 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, range
, RECO_COPY
, &dataObj
);
2086 if(FAILED(hr
) || !dataObj
)
2087 hr
= ME_GetDataObject(editor
, range
, &dataObj
);
2089 hr
= OleSetClipboard(dataObj
);
2090 IDataObject_Release(dataObj
);
2092 return SUCCEEDED(hr
) != 0;
2095 /* helper to send a msg filter notification */
2097 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2101 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2102 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2103 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2106 msgf
.wParam
= *wParam
;
2107 msgf
.lParam
= *lParam
;
2108 if (SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2110 *wParam
= msgf
.wParam
;
2111 *lParam
= msgf
.lParam
;
2112 msgf
.wParam
= *wParam
;
2118 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2120 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2121 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2123 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2124 editor
->nSelectionType
= stPosition
;
2132 editor
->nUDArrowX
= -1;
2138 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2139 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2143 editor
->nUDArrowX
= -1;
2144 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2145 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
)
2147 if (ME_IsSelection(editor
))
2149 ME_DeleteSelection(editor
);
2150 ME_CommitUndo(editor
);
2152 else if (nKey
== VK_DELETE
)
2154 /* Delete stops group typing.
2155 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2156 ME_DeleteTextAtCursor(editor
, 1, 1);
2157 ME_CommitUndo(editor
);
2159 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2161 BOOL bDeletionSucceeded
;
2162 /* Backspace can be grouped for a single undo */
2163 ME_ContinueCoalescingTransaction(editor
);
2164 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2165 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2166 /* Deletion was prevented so the cursor is moved back to where it was.
2167 * (e.g. this happens when trying to delete cell boundaries)
2169 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2171 ME_CommitCoalescingUndo(editor
);
2175 ME_MoveCursorFromTableRowStartParagraph(editor
);
2176 ME_UpdateSelectionLinkAttribute(editor
);
2177 ME_UpdateRepaint(editor
);
2178 ME_SendRequestResize(editor
, FALSE
);
2183 ME_SetSelection(editor
, 0, -1);
2189 return ME_Paste(editor
);
2198 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
2199 result
= ME_Copy(editor
, &range
);
2200 if (result
&& nKey
== 'X')
2202 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
, FALSE
);
2203 ME_CommitUndo(editor
);
2204 ME_UpdateRepaint(editor
);
2225 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2226 editor
->nUDArrowX
= -1;
2233 chf
.cbSize
= sizeof(chf
);
2235 ME_GetSelectionCharFormat(editor
, &chf
);
2236 ME_DumpStyleToBuf(&chf
, buf
);
2237 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2241 ME_CheckCharOffsets(editor
);
2248 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2249 LPARAM flags
, BOOL unicode
)
2254 wstr
= (WCHAR
)charCode
;
2257 CHAR charA
= charCode
;
2258 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2261 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
) {
2262 MessageBeep(MB_ICONERROR
);
2263 return 0; /* FIXME really 0 ? */
2266 if (((unsigned)wstr
)>=' '
2267 || (wstr
=='\r' && (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_MULTILINE
))
2269 ME_Cursor cursor
= editor
->pCursors
[0];
2270 ME_DisplayItem
*para
= ME_GetParagraph(cursor
.pRun
);
2272 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2273 ME_GetSelection(editor
, &from
, &to
);
2275 /* v4.1 allows tabs to be inserted with ctrl key down */
2276 && !(ctrl_is_down
&& !editor
->bEmulateVersion10
)
2279 ME_DisplayItem
*para
;
2280 BOOL bSelectedRow
= FALSE
;
2282 para
= ME_GetParagraph(cursor
.pRun
);
2283 if (ME_IsSelection(editor
) &&
2284 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2285 to
== ME_GetCursorOfs(editor
, 0) &&
2286 para
->member
.para
.prev_para
->type
== diParagraph
)
2288 para
= para
->member
.para
.prev_para
;
2289 bSelectedRow
= TRUE
;
2291 if (ME_IsInTable(para
))
2293 ME_TabPressedInTable(editor
, bSelectedRow
);
2294 ME_CommitUndo(editor
);
2297 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2298 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2300 /* Add a new table row after this row. */
2301 para
= ME_AppendTableRow(editor
, para
);
2302 para
= para
->member
.para
.next_para
;
2303 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2304 editor
->pCursors
[0].nOffset
= 0;
2305 editor
->pCursors
[1] = editor
->pCursors
[0];
2306 ME_CommitUndo(editor
);
2307 ME_CheckTablesForCorruption(editor
);
2308 ME_UpdateRepaint(editor
);
2310 } else if (from
== to
) {
2311 para
= para
->member
.para
.next_para
;
2312 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2313 para
= para
->member
.para
.next_para
;
2314 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2315 editor
->pCursors
[0].nOffset
= 0;
2316 editor
->pCursors
[1] = editor
->pCursors
[0];
2319 else if (para
== ME_GetParagraph(editor
->pCursors
[1].pRun
) &&
2320 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2321 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2322 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2324 /* Insert a newline before the table. */
2326 para
= para
->member
.para
.prev_para
;
2327 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2328 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2329 editor
->pCursors
[1] = editor
->pCursors
[0];
2330 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2331 editor
->pCursors
[0].pRun
->member
.run
.style
);
2332 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2333 ME_SetDefaultParaFormat(para
->member
.para
.pFmt
);
2334 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2335 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2336 editor
->pCursors
[1] = editor
->pCursors
[0];
2337 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2338 ME_CommitCoalescingUndo(editor
);
2339 ME_CheckTablesForCorruption(editor
);
2340 ME_UpdateRepaint(editor
);
2343 } else { /* v1.0 - 3.0 */
2344 ME_DisplayItem
*para
= ME_GetParagraph(cursor
.pRun
);
2345 if (ME_IsInTable(cursor
.pRun
))
2347 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2351 ME_ContinueCoalescingTransaction(editor
);
2352 para
= ME_AppendTableRow(editor
, para
);
2353 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2354 editor
->pCursors
[0].nOffset
= 0;
2355 editor
->pCursors
[1] = editor
->pCursors
[0];
2356 ME_CommitCoalescingUndo(editor
);
2357 ME_UpdateRepaint(editor
);
2359 /* Text should not be inserted at the end of the table. */
2364 } else if (wstr
== '\r') {
2365 ME_ContinueCoalescingTransaction(editor
);
2366 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2367 !ME_IsInTable(para
->member
.para
.prev_para
))
2369 /* Insert newline before table */
2371 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2373 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2374 editor
->pCursors
[0].nOffset
= 0;
2375 editor
->pCursors
[1] = editor
->pCursors
[0];
2376 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2377 editor
->pCursors
[0].pRun
->member
.run
.style
);
2379 editor
->pCursors
[1] = editor
->pCursors
[0];
2380 para
= ME_AppendTableRow(editor
, para
);
2381 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2382 editor
->pCursors
[0].nOffset
= 0;
2383 editor
->pCursors
[1] = editor
->pCursors
[0];
2385 ME_CommitCoalescingUndo(editor
);
2386 ME_UpdateRepaint(editor
);
2391 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2392 /* WM_CHAR is restricted to nTextLimit */
2393 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2395 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2396 ME_SaveTempStyle(editor
);
2397 ME_ContinueCoalescingTransaction(editor
);
2398 if (wstr
== '\r' && (GetKeyState(VK_SHIFT
) & 0x8000))
2399 ME_InsertEndRowFromCursor(editor
, 0);
2401 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2402 ME_ReleaseStyle(style
);
2403 ME_CommitCoalescingUndo(editor
);
2407 if (editor
->AutoURLDetect_bEnable
) ME_UpdateSelectionLinkAttribute(editor
);
2409 ME_UpdateRepaint(editor
);
2414 /* Process the message and calculate the new click count.
2416 * returns: The click count if it is mouse down event, else returns 0. */
2417 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2420 static int clickNum
= 0;
2421 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2424 if ((msg
== WM_LBUTTONDBLCLK
) ||
2425 (msg
== WM_RBUTTONDBLCLK
) ||
2426 (msg
== WM_MBUTTONDBLCLK
) ||
2427 (msg
== WM_XBUTTONDBLCLK
))
2429 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2432 if ((msg
== WM_LBUTTONDOWN
) ||
2433 (msg
== WM_RBUTTONDOWN
) ||
2434 (msg
== WM_MBUTTONDOWN
) ||
2435 (msg
== WM_XBUTTONDOWN
))
2437 static MSG prevClickMsg
;
2439 /* Compare the editor instead of the hwnd so that the this
2440 * can still be done for windowless richedit controls. */
2441 clickMsg
.hwnd
= (HWND
)editor
;
2442 clickMsg
.message
= msg
;
2443 clickMsg
.wParam
= wParam
;
2444 clickMsg
.lParam
= lParam
;
2445 clickMsg
.time
= GetMessageTime();
2446 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2447 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2448 if ((clickNum
!= 0) &&
2449 (clickMsg
.message
== prevClickMsg
.message
) &&
2450 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2451 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2452 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2453 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2454 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2460 prevClickMsg
= clickMsg
;
2467 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2473 DWORD messagePos
= GetMessagePos();
2474 pt
.x
= (short)LOWORD(messagePos
);
2475 pt
.y
= (short)HIWORD(messagePos
);
2477 sbi
.cbSize
= sizeof(sbi
);
2478 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2479 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2480 PtInRect(&sbi
.rcScrollBar
, pt
))
2482 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
2485 sbi
.cbSize
= sizeof(sbi
);
2486 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2487 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2488 PtInRect(&sbi
.rcScrollBar
, pt
))
2490 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
2493 ScreenToClient(editor
->hWnd
, &pt
);
2495 if (editor
->nSelectionType
== stLine
&& GetCapture() == editor
->hWnd
) {
2499 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2500 pt
.y
< editor
->rcFormat
.top
&&
2501 pt
.x
< editor
->rcFormat
.left
)
2506 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2508 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2509 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
2511 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
));
2514 if (pt
.x
< editor
->rcFormat
.left
)
2519 offset
= ME_CharFromPos(editor
, pt
.x
, pt
.y
, &isExact
);
2522 if (editor
->AutoURLDetect_bEnable
)
2526 ME_CursorFromCharOfs(editor
, offset
, &cursor
);
2527 run
= &cursor
.pRun
->member
.run
;
2528 if (editor
->AutoURLDetect_bEnable
&&
2529 run
->style
->fmt
.dwMask
& CFM_LINK
&&
2530 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2532 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
));
2536 if (ME_IsSelection(editor
))
2538 int selStart
, selEnd
;
2539 ME_GetSelection(editor
, &selStart
, &selEnd
);
2540 if (selStart
<= offset
&& selEnd
>= offset
) {
2541 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
2546 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
));
2550 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2552 DWORD exstyle
= GetWindowLongW(editor
->hWnd
, GWL_EXSTYLE
);
2554 GetClientRect(editor
->hWnd
, &editor
->rcFormat
);
2555 editor
->rcFormat
.top
+= (exstyle
& WS_EX_CLIENTEDGE
? 1 : 0);
2556 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2557 editor
->rcFormat
.right
-= 1;
2560 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2565 if(!editor
->lpOleCallback
)
2567 ME_GetSelection(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2568 if(selrange
.cpMin
== selrange
.cpMax
)
2569 seltype
|= SEL_EMPTY
;
2572 /* FIXME: Handle objects */
2573 seltype
|= SEL_TEXT
;
2574 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2575 seltype
|= SEL_MULTICHAR
;
2577 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2579 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, GetParent(editor
->hWnd
), NULL
);
2585 ME_TextEditor
*ME_MakeEditor(HWND hWnd
) {
2586 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2589 ed
->bEmulateVersion10
= FALSE
;
2590 ed
->pBuffer
= ME_MakeText();
2591 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2592 ME_MakeFirstParagraph(ed
);
2593 /* The four cursors are for:
2594 * 0 - The position where the caret is shown
2595 * 1 - The anchored end of the selection (for normal selection)
2596 * 2 & 3 - The anchored start and end respectively for word, line,
2597 * or paragraph selection.
2600 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2601 ed
->pCursors
[0].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
2602 ed
->pCursors
[0].nOffset
= 0;
2603 ed
->pCursors
[1].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
2604 ed
->pCursors
[1].nOffset
= 0;
2605 ed
->pCursors
[2] = ed
->pCursors
[0];
2606 ed
->pCursors
[3] = ed
->pCursors
[1];
2607 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2610 ed
->rgbBackColor
= -1;
2611 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2612 ed
->bCaretAtEnd
= FALSE
;
2614 ed
->nModifyStep
= 0;
2615 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2616 ed
->pUndoStack
= ed
->pRedoStack
= ed
->pUndoStackBottom
= NULL
;
2617 ed
->nUndoStackSize
= 0;
2618 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2619 ed
->nUndoMode
= umAddToUndo
;
2620 ed
->nParagraphs
= 1;
2621 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2622 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diParagraph
);
2623 ed
->bWordWrap
= (GetWindowLongW(hWnd
, GWL_STYLE
) & (WS_HSCROLL
|ES_AUTOHSCROLL
)) ? FALSE
: TRUE
;
2624 ed
->bHideSelection
= FALSE
;
2625 ed
->nInvalidOfs
= -1;
2626 ed
->pfnWordBreak
= NULL
;
2627 ed
->lpOleCallback
= NULL
;
2628 ed
->mode
= TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2629 ed
->AutoURLDetect_bEnable
= FALSE
;
2630 ed
->bHaveFocus
= FALSE
;
2631 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2633 ed
->pFontCache
[i
].nRefs
= 0;
2634 ed
->pFontCache
[i
].nAge
= 0;
2635 ed
->pFontCache
[i
].hFont
= NULL
;
2638 ME_CheckCharOffsets(ed
);
2639 if (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_SELECTIONBAR
)
2640 ed
->selofs
= SELECTIONBAR_WIDTH
;
2643 ed
->bDefaultFormatRect
= TRUE
;
2644 ed
->nSelectionType
= stPosition
;
2646 if (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_PASSWORD
)
2647 ed
->cPasswordMask
= '*';
2649 ed
->cPasswordMask
= 0;
2651 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2653 /* Default vertical scrollbar information */
2654 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2655 ed
->vert_si
.nMin
= 0;
2656 ed
->vert_si
.nMax
= 0;
2657 ed
->vert_si
.nPage
= 0;
2658 ed
->vert_si
.nPos
= 0;
2660 OleInitialize(NULL
);
2665 void ME_DestroyEditor(ME_TextEditor
*editor
)
2667 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2668 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2671 ME_ClearTempStyle(editor
);
2672 ME_EmptyUndoStack(editor
);
2675 ME_DestroyDisplayItem(p
);
2678 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2679 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2681 if (editor
->pFontCache
[i
].hFont
)
2682 DeleteObject(editor
->pFontCache
[i
].hFont
);
2684 if (editor
->rgbBackColor
!= -1)
2685 DeleteObject(editor
->hbrBackground
);
2686 if(editor
->lpOleCallback
)
2687 IUnknown_Release(editor
->lpOleCallback
);
2688 SetWindowLongPtrW(editor
->hWnd
, 0, 0);
2691 FREE_OBJ(editor
->pBuffer
);
2692 FREE_OBJ(editor
->pCursors
);
2697 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2702 case DLL_PROCESS_ATTACH
:
2703 DisableThreadLibraryCalls(hinstDLL
);
2704 me_heap
= HeapCreate (0, 0x10000, 0);
2705 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2706 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2710 case DLL_PROCESS_DETACH
:
2711 UnregisterClassW(RichEdit20W
, 0);
2712 UnregisterClassW(RichEdit50W
, 0);
2713 UnregisterClassA("RichEdit20A", 0);
2714 UnregisterClassA("RichEdit50A", 0);
2715 if (ME_ListBoxRegistered
)
2716 UnregisterClassW(REListBox20W
, 0);
2717 if (ME_ComboBoxRegistered
)
2718 UnregisterClassW(REComboBox20W
, 0);
2720 HeapDestroy (me_heap
);
2728 static const char * const edit_messages
[] = {
2757 "EM_SETPASSWORDCHAR",
2758 "EM_EMPTYUNDOBUFFER",
2759 "EM_GETFIRSTVISIBLELINE",
2761 "EM_SETWORDBREAKPROC",
2762 "EM_GETWORDBREAKPROC",
2763 "EM_GETPASSWORDCHAR",
2773 static const char * const richedit_messages
[] = {
2778 "EM_EXLINEFROMCHAR",
2784 "EM_GETOLEINTERFACE",
2794 "EM_SETOLECALLBACK",
2796 "EM_SETTARGETDEVICE",
2804 "EM_GETWORDBREAKPROCEX",
2805 "EM_SETWORDBREAKPROCEX",
2807 "EM_UNKNOWN_USER_83",
2812 "EM_STOPGROUPTYPING",
2816 "EM_GETAUTOURLDETECT",
2819 "EM_GETTEXTLENGTHEX",
2822 "EM_UNKNOWN_USER_98",
2823 "EM_UNKNOWN_USER_99",
2824 "EM_SETPUNCTUATION",
2825 "EM_GETPUNCTUATION",
2826 "EM_SETWORDWRAPMODE",
2827 "EM_GETWORDWRAPMODE",
2833 "EM_UNKNOWN_USER_109",
2834 "EM_UNKNOWN_USER_110",
2835 "EM_UNKNOWN_USER_111",
2836 "EM_UNKNOWN_USER_112",
2837 "EM_UNKNOWN_USER_113",
2838 "EM_UNKNOWN_USER_114",
2839 "EM_UNKNOWN_USER_115",
2840 "EM_UNKNOWN_USER_116",
2841 "EM_UNKNOWN_USER_117",
2842 "EM_UNKNOWN_USER_118",
2843 "EM_UNKNOWN_USER_119",
2844 "EM_SETLANGOPTIONS",
2845 "EM_GETLANGOPTIONS",
2846 "EM_GETIMECOMPMODE",
2850 "EM_SETIMEMODEBIAS",
2855 get_msg_name(UINT msg
)
2857 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
2858 return edit_messages
[msg
- EM_GETSEL
];
2859 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
2860 return richedit_messages
[msg
- EM_CANPASTE
];
2864 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
2865 LPARAM lParam
, BOOL unicode
)
2867 ME_TextEditor
*editor
;
2871 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
2872 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
2874 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
2877 if (msg
== WM_NCCREATE
)
2879 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
2880 TRACE("WM_NCCREATE: style 0x%08x\n", pcs
->style
);
2881 editor
= ME_MakeEditor(hWnd
);
2882 SetWindowLongPtrW(hWnd
, 0, (LONG_PTR
)editor
);
2885 else if (msg
!= WM_NCDESTROY
)
2887 ERR("called with invalid hWnd %p - application bug?\n", hWnd
);
2892 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
2894 if (hresult
== S_FALSE
)
2895 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2897 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
2898 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
2903 #define UNSUPPORTED_MSG(e) \
2905 FIXME(#e ": stub\n"); \
2906 *phresult = S_FALSE; \
2909 /* Handle messages for windowless and windoweded richedit controls.
2911 * The LRESULT that is returned is a return value for window procs,
2912 * and the phresult parameter is the COM return code needed by the
2913 * text services interface. */
2914 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2915 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
2921 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
2922 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
2923 UNSUPPORTED_MSG(EM_FMTLINES
)
2924 UNSUPPORTED_MSG(EM_FORMATRANGE
)
2925 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
2926 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
2927 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
2928 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
2929 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
2930 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
2931 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
2932 UNSUPPORTED_MSG(EM_GETREDONAME
)
2933 UNSUPPORTED_MSG(EM_GETTEXTMODE
)
2934 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
2935 UNSUPPORTED_MSG(EM_GETUNDONAME
)
2936 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
2937 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
2938 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
2939 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
2940 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
2941 UNSUPPORTED_MSG(EM_SETFONTSIZE
)
2942 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
2943 UNSUPPORTED_MSG(EM_SETMARGINS
)
2944 UNSUPPORTED_MSG(EM_SETPALETTE
)
2945 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
2946 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
2947 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
2948 UNSUPPORTED_MSG(WM_STYLECHANGING
)
2949 UNSUPPORTED_MSG(WM_STYLECHANGED
)
2951 /* Messages specific to Richedit controls */
2954 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
2956 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
2959 UINT code
= DLGC_WANTCHARS
|DLGC_WANTARROWS
;
2960 if(lParam
&& (((LPMSG
)lParam
)->message
== WM_KEYDOWN
))
2962 int vk
= (int)((LPMSG
)lParam
)->wParam
;
2963 /* if style says we want return key */
2964 if((vk
== VK_RETURN
) && (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_WANTRETURN
))
2966 code
|= DLGC_WANTMESSAGE
;
2968 /* we always handle ctrl-tab */
2969 if((vk
== VK_TAB
) && (GetKeyState(VK_CONTROL
) & 0x8000))
2971 code
|= DLGC_WANTMESSAGE
;
2976 case EM_EMPTYUNDOBUFFER
:
2977 ME_EmptyUndoStack(editor
);
2981 /* Note: wParam/lParam can be NULL */
2983 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
2984 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
2985 ME_GetSelection(editor
, (int *)pfrom
, (int *)pto
);
2986 if ((*pfrom
|*pto
) & 0xFFFF0000)
2988 return MAKELONG(*pfrom
,*pto
);
2992 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
2993 ME_GetSelection(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
2994 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
2997 case EM_SETUNDOLIMIT
:
2999 if ((int)wParam
< 0)
3000 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3002 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3003 /* Setting a max stack size keeps wine from getting killed
3004 for hogging memory. Windows allocates all this memory at once, so
3005 no program would realistically set a value above our maximum. */
3006 return editor
->nUndoLimit
;
3009 return editor
->pUndoStack
!= NULL
;
3011 return editor
->pRedoStack
!= NULL
;
3012 case WM_UNDO
: /* FIXME: actually not the same */
3014 return ME_Undo(editor
);
3016 return ME_Redo(editor
);
3019 /* these flags are equivalent to the ES_* counterparts */
3020 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3021 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3022 DWORD settings
= GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & mask
;
3028 /* these flags are equivalent to ES_* counterparts
3029 * ECO_READONLY is already implemented in the code, only requires
3030 * setting the bit to work
3032 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3033 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3034 DWORD raw
= GetWindowLongW(editor
->hWnd
, GWL_STYLE
);
3035 DWORD settings
= mask
& raw
;
3036 DWORD oldSettings
= settings
;
3037 DWORD changedSettings
;
3053 SetWindowLongW(editor
->hWnd
, GWL_STYLE
, (raw
& ~mask
) | (settings
& mask
));
3055 changedSettings
= oldSettings
^ settings
;
3057 if (settings
& ECO_AUTOWORDSELECTION
)
3058 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3060 if (oldSettings
^ settings
) {
3061 if (settings
& ECO_SELECTIONBAR
) {
3062 editor
->selofs
= SELECTIONBAR_WIDTH
;
3063 editor
->rcFormat
.left
+= SELECTIONBAR_WIDTH
;
3066 editor
->rcFormat
.left
-= SELECTIONBAR_WIDTH
;
3068 ME_WrapMarkedParagraphs(editor
);
3071 if (settings
& ECO_VERTICAL
)
3072 FIXME("ECO_VERTICAL not implemented yet!\n");
3073 if (settings
& ECO_AUTOHSCROLL
)
3074 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3075 if (settings
& ECO_AUTOVSCROLL
)
3076 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3077 if (settings
& ECO_NOHIDESEL
)
3078 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3079 if (settings
& ECO_WANTRETURN
)
3080 FIXME("ECO_WANTRETURN not implemented yet!\n");
3086 ME_InvalidateSelection(editor
);
3087 ME_SetSelection(editor
, wParam
, lParam
);
3088 ME_InvalidateSelection(editor
);
3089 HideCaret(editor
->hWnd
);
3090 ME_ShowCaret(editor
);
3091 ME_SendSelChange(editor
);
3094 case EM_SETSCROLLPOS
:
3096 POINT
*point
= (POINT
*)lParam
;
3097 ME_ScrollAbs(editor
, point
->y
);
3100 case EM_AUTOURLDETECT
:
3102 if (wParam
==1 || wParam
==0)
3104 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3107 return E_INVALIDARG
;
3109 case EM_GETAUTOURLDETECT
:
3111 return editor
->AutoURLDetect_bEnable
;
3116 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3118 TRACE("EM_EXSETSEL (%d,%d)\n", range
.cpMin
, range
.cpMax
);
3120 ME_InvalidateSelection(editor
);
3121 end
= ME_SetSelection(editor
, range
.cpMin
, range
.cpMax
);
3122 ME_InvalidateSelection(editor
);
3123 HideCaret(editor
->hWnd
);
3124 ME_ShowCaret(editor
);
3125 ME_SendSelChange(editor
);
3129 case EM_SHOWSCROLLBAR
:
3131 ShowScrollBar(editor
->hWnd
, wParam
, lParam
);
3137 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3141 int oldModify
= editor
->nModifyStep
;
3143 if (!pStruct
) return 0;
3145 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3146 pStruct
->codepage
== 1200 ? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3147 pStruct
->flags
, pStruct
->codepage
);
3149 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3150 wszText
= lParam
? ME_ToUnicode(pStruct
->codepage
== 1200, (void *)lParam
) : NULL
;
3151 len
= wszText
? lstrlenW(wszText
) : 0;
3153 if (pStruct
->flags
& ST_SELECTION
) {
3154 ME_GetSelection(editor
, &from
, &to
);
3155 style
= ME_GetSelectionInsertStyle(editor
);
3156 ME_InternalDeleteText(editor
, from
, to
- from
, FALSE
);
3157 if (pStruct
->codepage
!= 1200 && lParam
&&
3158 (!strncmp((char *)lParam
, "{\\rtf", 5) || !strncmp((char *)lParam
, "{\\urtf}", 6)))
3159 ME_StreamInRTFString(editor
, 1, (char *)lParam
);
3160 else ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3161 ME_ReleaseStyle(style
);
3163 if (editor
->AutoURLDetect_bEnable
) ME_UpdateSelectionLinkAttribute(editor
);
3166 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
3167 if (pStruct
->codepage
!= 1200 && lParam
&&
3168 (!strncmp((char *)lParam
, "{\\rtf", 5) || !strncmp((char *)lParam
, "{\\urtf}", 6)))
3169 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3170 else ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3173 if (editor
->AutoURLDetect_bEnable
) ME_UpdateLinkAttribute(editor
, 0, -1);
3175 ME_CommitUndo(editor
);
3176 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3178 editor
->nModifyStep
= oldModify
;
3179 ME_EmptyUndoStack(editor
);
3181 ME_UpdateRepaint(editor
);
3184 case EM_SETBKGNDCOLOR
:
3187 if (editor
->rgbBackColor
!= -1) {
3188 DeleteObject(editor
->hbrBackground
);
3189 lColor
= editor
->rgbBackColor
;
3191 else lColor
= GetSysColor(COLOR_WINDOW
);
3195 editor
->rgbBackColor
= -1;
3196 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3200 editor
->rgbBackColor
= lParam
;
3201 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3203 InvalidateRect(editor
->hWnd
, NULL
, TRUE
);
3204 UpdateWindow(editor
->hWnd
);
3208 return editor
->nModifyStep
== 0 ? 0 : -1;
3212 editor
->nModifyStep
= 1;
3214 editor
->nModifyStep
= 0;
3218 case EM_SETREADONLY
:
3220 long nStyle
= GetWindowLongW(editor
->hWnd
, GWL_STYLE
);
3222 nStyle
|= ES_READONLY
;
3224 nStyle
&= ~ES_READONLY
;
3225 SetWindowLongW(editor
->hWnd
, GWL_STYLE
, nStyle
);
3228 case EM_SETEVENTMASK
:
3230 DWORD nOldMask
= editor
->nEventMask
;
3232 editor
->nEventMask
= lParam
;
3235 case EM_GETEVENTMASK
:
3236 return editor
->nEventMask
;
3237 case EM_SETCHARFORMAT
:
3239 CHARFORMAT2W buf
, *p
;
3240 BOOL bRepaint
= TRUE
;
3241 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3242 if (p
== NULL
) return 0;
3244 ME_SetDefaultCharFormat(editor
, p
);
3245 else if (wParam
== (SCF_WORD
| SCF_SELECTION
)) {
3246 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3248 } else if (wParam
== SCF_ALL
) {
3249 if (editor
->mode
& TM_PLAINTEXT
)
3250 ME_SetDefaultCharFormat(editor
, p
);
3252 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), p
);
3253 editor
->nModifyStep
= 1;
3255 } else if (editor
->mode
& TM_PLAINTEXT
) {
3259 ME_GetSelection(editor
, &from
, &to
);
3260 bRepaint
= (from
!= to
);
3261 ME_SetSelectionCharFormat(editor
, p
);
3262 if (from
!= to
) editor
->nModifyStep
= 1;
3264 ME_CommitUndo(editor
);
3267 ME_WrapMarkedParagraphs(editor
);
3268 ME_UpdateScrollBar(editor
);
3273 case EM_GETCHARFORMAT
:
3275 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3276 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3277 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3278 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3279 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3281 tmp
.cbSize
= sizeof(tmp
);
3283 ME_GetDefaultCharFormat(editor
, &tmp
);
3285 ME_GetSelectionCharFormat(editor
, &tmp
);
3286 ME_CopyToCFAny(dst
, &tmp
);
3289 case EM_SETPARAFORMAT
:
3291 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3292 ME_WrapMarkedParagraphs(editor
);
3293 ME_UpdateScrollBar(editor
);
3295 ME_CommitUndo(editor
);
3298 case EM_GETPARAFORMAT
:
3299 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3300 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3301 case EM_GETFIRSTVISIBLELINE
:
3303 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3304 int y
= ME_GetYScrollPos(editor
);
3309 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3310 if (p
->type
== diTextEnd
)
3312 if (p
->type
== diParagraph
) {
3313 ypara
= p
->member
.para
.pt
.y
;
3316 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3317 yend
= ystart
+ p
->member
.row
.nHeight
;
3325 case EM_HIDESELECTION
:
3327 editor
->bHideSelection
= (wParam
!= 0);
3328 ME_InvalidateSelection(editor
);
3333 ME_ScrollDown(editor
, lParam
* 8); /* FIXME follow the original */
3334 return TRUE
; /* Should return false if a single line richedit control */
3339 ME_GetSelection(editor
, &from
, &to
);
3340 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
3341 ME_CommitUndo(editor
);
3342 ME_UpdateRepaint(editor
);
3349 LPWSTR wszText
= lParam
? ME_ToUnicode(unicode
, (void *)lParam
) : NULL
;
3350 size_t len
= wszText
? lstrlenW(wszText
) : 0;
3351 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3353 ME_GetSelection(editor
, &from
, &to
);
3354 style
= ME_GetSelectionInsertStyle(editor
);
3355 ME_InternalDeleteText(editor
, from
, to
-from
, FALSE
);
3356 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3357 ME_ReleaseStyle(style
);
3358 /* drop temporary style if line end */
3360 * FIXME question: does abc\n mean: put abc,
3361 * clear temp style, put \n? (would require a change)
3363 if (len
>0 && wszText
[len
-1] == '\n')
3364 ME_ClearTempStyle(editor
);
3365 ME_EndToUnicode(unicode
, wszText
);
3366 ME_CommitUndo(editor
);
3367 if (editor
->AutoURLDetect_bEnable
) ME_UpdateSelectionLinkAttribute(editor
);
3369 ME_EmptyUndoStack(editor
);
3370 ME_UpdateRepaint(editor
);
3373 case EM_SCROLLCARET
:
3374 ME_EnsureVisible(editor
, editor
->pCursors
[0].pRun
);
3381 BOOL bRepaint
= LOWORD(lParam
);
3384 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3385 GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
);
3386 hDC
= GetDC(editor
->hWnd
);
3387 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3388 ReleaseDC(editor
->hWnd
, hDC
);
3389 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), &fmt
);
3390 ME_SetDefaultCharFormat(editor
, &fmt
);
3392 ME_CommitUndo(editor
);
3394 ME_RewrapRepaint(editor
);
3399 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
), FALSE
);
3402 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3403 if (!unicode
&& !strncmp((char *)lParam
, "{\\rtf", 5))
3405 /* Undocumented: WM_SETTEXT supports RTF text */
3406 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3410 LPWSTR wszText
= ME_ToUnicode(unicode
, (void *)lParam
);
3411 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText
)); /* debugstr_w() */
3412 if (lstrlenW(wszText
) > 0)
3416 /* uses default style! */
3417 if (!(GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_MULTILINE
))
3422 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3425 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3427 ME_EndToUnicode(unicode
, wszText
);
3431 TRACE("WM_SETTEXT - NULL\n");
3432 if (editor
->AutoURLDetect_bEnable
)
3434 ME_UpdateLinkAttribute(editor
, 0, -1);
3436 ME_SetSelection(editor
, 0, 0);
3437 editor
->nModifyStep
= 0;
3438 ME_CommitUndo(editor
);
3439 ME_EmptyUndoStack(editor
);
3440 ME_UpdateRepaint(editor
);
3445 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3446 if (IsClipboardFormatAvailable(nRTFFormat
))
3448 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3459 ME_GetSelection(editor
, &range
.cpMin
, &range
.cpMax
);
3461 if (ME_Copy(editor
, &range
) && msg
== WM_CUT
)
3463 ME_InternalDeleteText(editor
, range
.cpMin
, range
.cpMax
-range
.cpMin
, FALSE
);
3464 ME_CommitUndo(editor
);
3465 ME_UpdateRepaint(editor
);
3469 case WM_GETTEXTLENGTH
:
3471 GETTEXTLENGTHEX how
;
3473 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3474 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3475 how
.codepage
= unicode
? 1200 : CP_ACP
;
3476 return ME_GetTextLengthEx(editor
, &how
);
3478 case EM_GETTEXTLENGTHEX
:
3479 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3484 LPSTR bufferA
= NULL
;
3485 LPWSTR bufferW
= NULL
;
3488 bufferW
= heap_alloc((wParam
+ 2) * sizeof(WCHAR
));
3490 bufferA
= heap_alloc(wParam
+ 2);
3492 ex
.cb
= (wParam
+ 2) * (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3493 ex
.flags
= GT_USECRLF
;
3494 ex
.codepage
= unicode
? 1200 : CP_ACP
;
3495 ex
.lpDefaultChar
= NULL
;
3496 ex
.lpUsedDefChar
= NULL
;
3498 rc
= ME_GetTextEx(editor
, &ex
, unicode
? (LPARAM
)bufferW
: (LPARAM
)bufferA
);
3502 memcpy((LPWSTR
)lParam
, bufferW
, wParam
* sizeof(WCHAR
));
3503 if (strlenW(bufferW
) >= wParam
) rc
= 0;
3507 memcpy((LPSTR
)lParam
, bufferA
, wParam
);
3508 if (strlen(bufferA
) >= wParam
) rc
= 0;
3515 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3519 TEXTRANGEW tr
; /* W and A differ only by rng->lpstrText */
3520 ME_GetSelection(editor
, &from
, &to
);
3521 tr
.chrg
.cpMin
= from
;
3523 tr
.lpstrText
= (WCHAR
*)lParam
;
3524 return ME_GetTextRange(editor
, &tr
, unicode
);
3526 case EM_GETSCROLLPOS
:
3528 POINT
*point
= (POINT
*)lParam
;
3529 point
->x
= 0; /* FIXME side scrolling not implemented */
3530 point
->y
= ME_GetYScrollPos(editor
);
3533 case EM_GETTEXTRANGE
:
3535 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3536 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3537 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
,
3538 editor
->bEmulateVersion10
, ME_GetTextLength(editor
));
3539 return ME_GetTextRange(editor
, rng
, unicode
);
3543 ME_DisplayItem
*run
;
3544 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3545 unsigned int nCharsLeft
= nMaxChars
;
3546 char *dest
= (char *) lParam
;
3547 BOOL wroteNull
= FALSE
;
3549 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3550 unicode
? "Unicode" : "Ansi");
3552 run
= ME_FindRowWithNumber(editor
, wParam
);
3556 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3557 && !(run
->member
.run
.nFlags
& MERF_ENDPARA
))
3561 if (run
->type
!= diRun
)
3563 strText
= run
->member
.run
.strText
;
3564 nCopy
= min(nCharsLeft
, strText
->nLen
);
3567 memcpy(dest
, strText
->szData
, nCopy
* sizeof(WCHAR
));
3569 nCopy
= WideCharToMultiByte(CP_ACP
, 0, strText
->szData
, nCopy
, dest
,
3570 nCharsLeft
, NULL
, NULL
);
3571 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3572 nCharsLeft
-= nCopy
;
3575 /* append line termination, space allowing */
3578 if (run
&& (run
->member
.run
.nFlags
& MERF_ENDPARA
))
3581 /* Write as many \r as encoded in end-of-paragraph, space allowing */
3582 for (i
= 0; i
< run
->member
.run
.nCR
&& nCharsLeft
> 0; i
++, nCharsLeft
--)
3584 *((WCHAR
*)dest
) = '\r';
3585 dest
+= unicode
? sizeof(WCHAR
) : 1;
3587 /* Write as many \n as encoded in end-of-paragraph, space allowing */
3588 for (i
= 0; i
< run
->member
.run
.nLF
&& nCharsLeft
> 0; i
++, nCharsLeft
--)
3590 *((WCHAR
*)dest
) = '\n';
3591 dest
+= unicode
? sizeof(WCHAR
) : 1;
3597 *((WCHAR
*)dest
) = '\0';
3605 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3606 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3608 case EM_GETLINECOUNT
:
3610 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3613 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3615 while (item
!= editor
->pBuffer
->pLast
)
3617 assert(item
->type
== diParagraph
);
3618 prev_para
= ME_FindItemBack(item
, diRun
);
3620 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3622 nRows
+= item
->member
.para
.nRows
;
3623 item
= item
->member
.para
.next_para
;
3625 last_para
= ME_FindItemBack(item
, diRun
);
3627 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3628 if (editor
->bEmulateVersion10
&& prev_para
&& last_para
->member
.run
.nCharOfs
== 0
3629 && prev_para
->member
.run
.nCR
== 1 && prev_para
->member
.run
.nLF
== 0)
3631 /* In 1.0 emulation, the last solitary \r at the very end of the text
3632 (if one exists) is NOT a line break.
3633 FIXME: this is an ugly hack. This should have a more regular model. */
3637 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
3638 return max(1, nRows
);
3640 case EM_LINEFROMCHAR
:
3643 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
, 1));
3645 return ME_RowNumberFromCharOfs(editor
, wParam
);
3647 case EM_EXLINEFROMCHAR
:
3650 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(editor
,1));
3652 return ME_RowNumberFromCharOfs(editor
, lParam
);
3656 ME_DisplayItem
*item
, *para
;
3660 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
3662 item
= ME_FindRowWithNumber(editor
, wParam
);
3665 para
= ME_GetParagraph(item
);
3666 item
= ME_FindItemFwd(item
, diRun
);
3667 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
3668 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
3673 ME_DisplayItem
*item
, *item_end
;
3674 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
3676 if (wParam
> ME_GetTextLength(editor
))
3680 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3683 item
= ME_FindItemAtOffset(editor
, diRun
, wParam
, NULL
);
3684 item
= ME_RowStart(item
);
3685 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, ME_FindItemFwd(item
, diRun
), 0);
3686 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
3687 if (item_end
->type
== diStartRow
)
3688 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, ME_FindItemFwd(item_end
, diRun
), 0);
3691 ME_DisplayItem
*endPara
;
3693 nNextLineOfs
= ME_FindItemFwd(item
, diParagraphOrEnd
)->member
.para
.nCharOfs
;
3694 endPara
= ME_FindItemFwd(item
, diParagraphOrEnd
);
3695 endPara
= ME_FindItemBack(endPara
, diRun
);
3697 assert(endPara
->type
== diRun
);
3698 assert(endPara
->member
.run
.nFlags
& MERF_ENDPARA
);
3699 nNextLineOfs
-= endPara
->member
.run
.nCR
+ endPara
->member
.run
.nLF
;
3701 nChars
= nNextLineOfs
- nThisLineOfs
;
3702 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
3705 case EM_EXLIMITTEXT
:
3707 if ((int)lParam
< 0)
3710 editor
->nTextLimit
= 65536;
3712 editor
->nTextLimit
= (int) lParam
;
3718 editor
->nTextLimit
= 65536;
3720 editor
->nTextLimit
= (int) wParam
;
3723 case EM_GETLIMITTEXT
:
3725 return editor
->nTextLimit
;
3729 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
3730 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
3734 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3735 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
3736 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
3742 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
3743 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
3747 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
3748 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
3749 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
3755 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
3756 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
3758 case EM_FINDTEXTEXW
:
3760 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
3761 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
3764 if (!wParam
|| !lParam
)
3766 *(int *)wParam
= editor
->nZoomNumerator
;
3767 *(int *)lParam
= editor
->nZoomDenominator
;
3770 return ME_SetZoom(editor
, wParam
, lParam
);
3771 case EM_CHARFROMPOS
:
3772 return ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
, NULL
);
3773 case EM_POSFROMCHAR
:
3775 ME_DisplayItem
*pRun
;
3776 int nCharOfs
, nOffset
, nLength
;
3781 /* detect which API version we're dealing with */
3782 if (wParam
>= 0x40000)
3784 nLength
= ME_GetTextLength(editor
);
3785 nCharOfs
= min(nCharOfs
, nLength
);
3786 nCharOfs
= max(nCharOfs
, 0);
3788 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pRun
, &nOffset
);
3789 assert(pRun
->type
== diRun
);
3790 pt
.y
= pRun
->member
.run
.pt
.y
;
3791 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
);
3792 pt
.y
+= ME_GetParagraph(pRun
)->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
3793 pt
.x
+= editor
->rcFormat
.left
;
3795 pt
.y
-= editor
->vert_si
.nPos
;
3796 si
.cbSize
= sizeof(si
);
3798 if (GetScrollInfo(editor
->hWnd
, SB_HORZ
, &si
)) pt
.x
-= si
.nPos
;
3800 if (wParam
>= 0x40000) {
3801 *(POINTL
*)wParam
= pt
;
3803 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
3809 ME_SetDefaultFormatRect(editor
);
3810 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & WS_HSCROLL
)
3811 { /* Squelch the default horizontal scrollbar it would make */
3812 ShowScrollBar(editor
->hWnd
, SB_HORZ
, FALSE
);
3815 si
.cbSize
= sizeof(si
);
3816 si
.fMask
= SIF_PAGE
| SIF_RANGE
;
3817 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_DISABLENOSCROLL
)
3818 si
.fMask
|= SIF_DISABLENOSCROLL
;
3819 si
.nMax
= (si
.fMask
& SIF_DISABLENOSCROLL
) ? 1 : 0;
3822 SetScrollInfo(editor
->hWnd
, SB_VERT
, &si
, TRUE
);
3824 ME_CommitUndo(editor
);
3825 ME_WrapMarkedParagraphs(editor
);
3826 ME_MoveCaret(editor
);
3830 ME_DestroyEditor(editor
);
3834 return ME_SetCursor(editor
);
3836 case WM_LBUTTONDBLCLK
:
3837 case WM_LBUTTONDOWN
:
3839 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3840 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3841 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3843 SetFocus(editor
->hWnd
);
3844 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
3845 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
3846 SetCapture(editor
->hWnd
);
3847 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3848 if (!ME_SetCursor(editor
)) goto do_default
;
3852 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3853 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3855 if (GetCapture() == editor
->hWnd
)
3856 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
3857 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3858 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3859 if (GetCapture() == editor
->hWnd
)
3860 ME_SetCursor(editor
);
3863 if (GetCapture() == editor
->hWnd
)
3865 if (editor
->nSelectionType
== stDocument
)
3866 editor
->nSelectionType
= stPosition
;
3867 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3868 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3872 ME_SetCursor(editor
);
3873 ME_LinkNotify(editor
,msg
,wParam
,lParam
);
3877 case WM_RBUTTONDOWN
:
3878 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3879 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
3880 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3883 case WM_CONTEXTMENU
:
3884 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
3893 hDC
= BeginPaint(editor
->hWnd
, &ps
);
3894 /* Erase area outside of the formatting rectangle */
3895 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
3898 rc
.bottom
= editor
->rcFormat
.top
;
3899 FillRect(hDC
, &rc
, editor
->hbrBackground
);
3900 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
3902 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
3904 rc
.top
= editor
->rcFormat
.bottom
;
3905 FillRect(hDC
, &rc
, editor
->hbrBackground
);
3906 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
3908 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
3910 rc
.right
= editor
->rcFormat
.left
;
3911 FillRect(hDC
, &rc
, editor
->hbrBackground
);
3912 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
3914 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
3916 rc
.left
= editor
->rcFormat
.right
;
3917 FillRect(hDC
, &rc
, editor
->hbrBackground
);
3918 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
3921 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
3922 EndPaint(editor
->hWnd
, &ps
);
3926 editor
->bHaveFocus
= TRUE
;
3927 ME_ShowCaret(editor
);
3928 ME_SendOldNotify(editor
, EN_SETFOCUS
);
3931 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3932 editor
->bHaveFocus
= FALSE
;
3933 ME_HideCaret(editor
);
3934 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
3938 HDC hDC
= (HDC
)wParam
;
3940 if (GetUpdateRect(editor
->hWnd
,&rc
,TRUE
))
3942 FillRect(hDC
, &rc
, editor
->hbrBackground
);
3947 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
3950 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
3951 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3955 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
3956 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
3958 if (ME_KeyDown(editor
, LOWORD(wParam
)))
3962 return ME_Char(editor
, wParam
, lParam
, unicode
);
3966 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
3967 if(wParam
<= 0x000fffff)
3969 if(wParam
> 0xffff) /* convert to surrogates */
3972 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
3973 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
3975 ME_Char(editor
, wParam
, 0, TRUE
);
3981 case EM_STOPGROUPTYPING
:
3982 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
3984 case EM_SCROLL
: /* fall through */
3990 origNPos
= ME_GetYScrollPos(editor
);
3993 if (editor
&& editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
3994 lineHeight
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
3995 if (lineHeight
<= 0) lineHeight
= 24;
3997 switch(LOWORD(wParam
))
4000 ME_ScrollUp(editor
,lineHeight
);
4003 ME_ScrollDown(editor
,lineHeight
);
4006 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4009 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4012 case SB_THUMBPOSITION
:
4015 sbi
.cbSize
= sizeof(sbi
);
4016 sbi
.fMask
= SIF_TRACKPOS
;
4017 /* Try to get 32-bit track position value. */
4018 if (!GetScrollInfo(editor
->hWnd
, SB_VERT
, &sbi
))
4019 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4020 sbi
.nTrackPos
= HIWORD(wParam
);
4022 ME_ScrollAbs(editor
, sbi
.nTrackPos
);
4026 if (msg
== EM_SCROLL
)
4027 return 0x00010000 | (((ME_GetYScrollPos(editor
) - origNPos
)/lineHeight
) & 0xffff);
4033 UINT pulScrollLines
;
4035 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4036 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4039 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
4040 gcWheelDelta
= -GET_WHEEL_DELTA_WPARAM(wParam
);
4042 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
4044 /* FIXME follow the original */
4045 ME_ScrollDown(editor
,pulScrollLines
* (gcWheelDelta
/ WHEEL_DELTA
) * 8);
4051 *((RECT
*)lParam
) = editor
->rcFormat
;
4052 if (editor
->bDefaultFormatRect
)
4053 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4061 DWORD exstyle
= GetWindowLongW(editor
->hWnd
, GWL_EXSTYLE
);
4062 int border
= (exstyle
& WS_EX_CLIENTEDGE
) ? 1 : 0;
4064 RECT
*rc
= (RECT
*)lParam
;
4066 GetClientRect(editor
->hWnd
, &clientRect
);
4069 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4070 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4071 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4072 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4073 } else if (wParam
== 1) {
4074 /* MSDN incorrectly says a wParam value of 1 causes the
4075 * lParam rect to be used as a relative offset,
4076 * however, the tests show it just prevents min/max bound
4078 editor
->rcFormat
.top
= rc
->top
- border
;
4079 editor
->rcFormat
.left
= rc
->left
- border
;
4080 editor
->rcFormat
.bottom
= rc
->bottom
;
4081 editor
->rcFormat
.right
= rc
->right
+ border
;
4085 editor
->bDefaultFormatRect
= FALSE
;
4089 ME_SetDefaultFormatRect(editor
);
4090 editor
->bDefaultFormatRect
= TRUE
;
4092 if (msg
!= EM_SETRECTNP
)
4093 ME_RewrapRepaint(editor
);
4096 case EM_REQUESTRESIZE
:
4097 ME_SendRequestResize(editor
, TRUE
);
4105 GetClientRect(editor
->hWnd
, &clientRect
);
4106 if (editor
->bDefaultFormatRect
) {
4107 ME_SetDefaultFormatRect(editor
);
4109 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4110 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4112 editor
->prevClientRect
= clientRect
;
4113 ME_RewrapRepaint(editor
);
4116 /* IME messages to make richedit controls IME aware */
4117 case WM_IME_SETCONTEXT
:
4118 case WM_IME_CONTROL
:
4120 case WM_IME_COMPOSITIONFULL
:
4122 case WM_IME_STARTCOMPOSITION
:
4124 editor
->imeStartIndex
=ME_GetCursorOfs(editor
,0);
4125 ME_DeleteSelection(editor
);
4126 ME_CommitUndo(editor
);
4127 ME_UpdateRepaint(editor
);
4130 case WM_IME_COMPOSITION
:
4134 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4135 hIMC
= ImmGetContext(editor
->hWnd
);
4136 ME_DeleteSelection(editor
);
4137 ME_CommitUndo(editor
);
4138 ME_SaveTempStyle(editor
);
4139 if (lParam
& GCS_RESULTSTR
)
4141 LPWSTR lpCompStr
= NULL
;
4144 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
4145 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4146 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, lpCompStr
, dwBufLen
);
4147 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4148 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4150 else if (lParam
& GCS_COMPSTR
)
4152 LPWSTR lpCompStr
= NULL
;
4155 dwBufLen
= ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, NULL
, 0);
4156 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4157 ImmGetCompositionStringW(hIMC
, GCS_COMPSTR
, lpCompStr
, dwBufLen
);
4158 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4160 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4161 ME_SetSelection(editor
,editor
->imeStartIndex
,
4162 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4164 ME_ReleaseStyle(style
);
4165 ME_UpdateRepaint(editor
);
4168 case WM_IME_ENDCOMPOSITION
:
4170 ME_DeleteSelection(editor
);
4171 editor
->imeStartIndex
=-1;
4174 case EM_GETOLEINTERFACE
:
4176 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
4177 return CreateIRichEditOle(editor
, ppvObj
);
4179 case EM_GETPASSWORDCHAR
:
4181 return editor
->cPasswordMask
;
4183 case EM_SETOLECALLBACK
:
4184 if(editor
->lpOleCallback
)
4185 IUnknown_Release(editor
->lpOleCallback
);
4186 editor
->lpOleCallback
= (LPRICHEDITOLECALLBACK
)lParam
;
4187 if(editor
->lpOleCallback
)
4188 IUnknown_AddRef(editor
->lpOleCallback
);
4190 case EM_GETWORDBREAKPROC
:
4191 return (LRESULT
)editor
->pfnWordBreak
;
4192 case EM_SETWORDBREAKPROC
:
4194 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4196 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4197 return (LRESULT
)pfnOld
;
4199 case EM_SETTEXTMODE
:
4204 GETTEXTLENGTHEX how
;
4206 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4207 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
4208 how
.codepage
= unicode
? 1200 : CP_ACP
;
4209 ret
= ME_GetTextLengthEx(editor
, &how
);
4212 /*Check for valid wParam*/
4213 if ((((wParam
& TM_RICHTEXT
) && ((wParam
& TM_PLAINTEXT
) << 1))) ||
4214 (((wParam
& TM_MULTILEVELUNDO
) && ((wParam
& TM_SINGLELEVELUNDO
) << 1))) ||
4215 (((wParam
& TM_MULTICODEPAGE
) && ((wParam
& TM_SINGLECODEPAGE
) << 1))))
4219 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4221 mask
|= (TM_RICHTEXT
| TM_PLAINTEXT
);
4222 changes
|= (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
));
4224 /*FIXME: Currently no support for undo level and code page options*/
4225 editor
->mode
= (editor
->mode
& (~mask
)) | changes
;
4231 case EM_SETPASSWORDCHAR
:
4233 editor
->cPasswordMask
= wParam
;
4234 ME_RewrapRepaint(editor
);
4237 case EM_SETTARGETDEVICE
:
4240 BOOL
new = (lParam
== 0);
4241 if (editor
->bWordWrap
!= new)
4243 editor
->bWordWrap
= new;
4244 ME_RewrapRepaint(editor
);
4247 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
4251 *phresult
= S_FALSE
;
4257 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4259 BOOL unicode
= TRUE
;
4261 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4262 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4265 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4268 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4270 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4273 /******************************************************************
4274 * RichEditANSIWndProc (RICHED20.10)
4276 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4278 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4281 /******************************************************************
4282 * RichEdit10ANSIWndProc (RICHED20.9)
4284 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4288 /* FIXME: this is NOT the same as 2.0 version */
4289 result
= RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4290 if (msg
== WM_NCCREATE
)
4292 ME_TextEditor
*editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4294 TRACE("Emulating version 1.0 (hWnd=%p)\n", hWnd
);
4295 editor
->bEmulateVersion10
= TRUE
;
4296 editor
->bWordWrap
= (GetWindowLongW(hWnd
, GWL_STYLE
) & ES_AUTOHSCROLL
) ? FALSE
: TRUE
;
4297 editor
->pBuffer
->pLast
->member
.para
.nCharOfs
= 2;
4298 assert(editor
->pBuffer
->pLast
->prev
->type
== diRun
);
4299 assert(editor
->pBuffer
->pLast
->prev
->member
.run
.nFlags
& MERF_ENDPARA
);
4300 editor
->pBuffer
->pLast
->prev
->member
.run
.nLF
= 1;
4305 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4307 HWND hWnd
= editor
->hWnd
;
4308 SendMessageA(GetParent(hWnd
), WM_COMMAND
, MAKEWPARAM(GetWindowLongW(hWnd
, GWLP_ID
), nCode
), (LPARAM
)hWnd
);
4311 void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4314 ME_Cursor tmpCursor
;
4316 int nCharOfs
; /* The start of the clicked text. Absolute character offset */
4321 x
= (short)LOWORD(lParam
);
4322 y
= (short)HIWORD(lParam
);
4323 nCharOfs
= ME_CharFromPos(editor
, x
, y
, &isExact
);
4324 if (!isExact
) return;
4326 ME_CursorFromCharOfs(editor
, nCharOfs
, &tmpCursor
);
4327 tmpRun
= &tmpCursor
.pRun
->member
.run
;
4329 if ((tmpRun
->style
->fmt
.dwMask
& CFM_LINK
)
4330 && (tmpRun
->style
->fmt
.dwEffects
& CFE_LINK
))
4331 { /* The clicked run has CFE_LINK set */
4332 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
4333 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
4334 info
.nmhdr
.code
= EN_LINK
;
4336 info
.wParam
= wParam
;
4337 info
.lParam
= lParam
;
4338 info
.chrg
.cpMin
= ME_CharOfsFromRunOfs(editor
,tmpCursor
.pRun
,0);
4339 info
.chrg
.cpMax
= info
.chrg
.cpMin
+ ME_StrVLen(tmpRun
->strText
);
4340 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,info
.nmhdr
.idFrom
, (LPARAM
)&info
);
4344 int ME_CountParagraphsBetween(ME_TextEditor
*editor
, int from
, int to
)
4346 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
4349 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= from
)
4350 item
= item
->member
.para
.next_para
;
4353 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= to
) {
4354 item
= item
->member
.para
.next_para
;
4361 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int nStart
, int nChars
, int bCRLF
)
4363 ME_DisplayItem
*item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
4365 WCHAR
*pStart
= buffer
;
4372 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4373 if (editor
->bEmulateVersion10
) bCRLF
= 0;
4377 int nLen
= ME_StrLen(item
->member
.run
.strText
) - nStart
;
4380 CopyMemory(buffer
, item
->member
.run
.strText
->szData
+ nStart
, sizeof(WCHAR
)*nLen
);
4389 item
= ME_FindItemFwd(item
, diRun
);
4392 while(nChars
&& item
)
4394 int nLen
= ME_StrLen(item
->member
.run
.strText
);
4395 if (item
->member
.run
.nFlags
& MERF_ENDPARA
)
4396 nLen
= item
->member
.run
.nCR
+ item
->member
.run
.nLF
;
4400 if (item
->member
.run
.nFlags
& MERF_ENDCELL
&&
4401 item
->member
.run
.nFlags
& MERF_ENDPARA
)
4405 else if (item
->member
.run
.nFlags
& MERF_ENDPARA
)
4407 if (!ME_FindItemFwd(item
, diRun
))
4408 /* No '\r' is appended to the last paragraph. */
4410 else if (bCRLF
&& nChars
== 1) {
4416 /* richedit 2.0 case - actual line-break is \r but should report \r\n */
4417 if (ME_GetParagraph(item
)->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
))
4422 *buffer
= '\n'; /* Later updated by nLen==1 at the end of the loop */
4432 /* richedit 2.0 verbatim has only \r. richedit 1.0 should honor encodings */
4434 while (nChars
- i
> 0 && i
< item
->member
.run
.nCR
)
4436 buffer
[i
] = '\r'; i
++;
4439 while (nChars
- i
- j
> 0 && j
< item
->member
.run
.nLF
)
4441 buffer
[i
+j
] = '\n'; j
++;
4447 CopyMemory(buffer
, item
->member
.run
.strText
->szData
, sizeof(WCHAR
)*nLen
);
4454 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
4458 item
= ME_FindItemFwd(item
, diRun
);
4461 TRACE("nWritten=%d, actual=%d\n", nWritten
, buffer
-pStart
);
4465 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4470 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4471 wcW
.lpfnWndProc
= RichEditWndProcW
;
4473 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4474 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4476 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4477 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4478 wcW
.lpszMenuName
= NULL
;
4480 if (is_version_nt())
4482 wcW
.lpszClassName
= RichEdit20W
;
4483 if (!RegisterClassW(&wcW
)) return FALSE
;
4484 wcW
.lpszClassName
= RichEdit50W
;
4485 if (!RegisterClassW(&wcW
)) return FALSE
;
4489 /* WNDCLASSA/W have the same layout */
4490 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4491 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4492 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4493 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4496 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4497 wcA
.lpfnWndProc
= RichEditWndProcA
;
4499 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4500 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4502 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
4503 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4504 wcA
.lpszMenuName
= NULL
;
4505 wcA
.lpszClassName
= "RichEdit20A";
4506 if (!RegisterClassA(&wcA
)) return FALSE
;
4507 wcA
.lpszClassName
= "RichEdit50A";
4508 if (!RegisterClassA(&wcA
)) return FALSE
;
4513 LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4514 /* FIXME: Not implemented */
4515 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4516 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4517 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4520 LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4521 /* FIXME: Not implemented */
4522 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4523 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4524 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4527 /******************************************************************
4528 * REExtendedRegisterClass (RICHED20.8)
4530 * FIXME undocumented
4531 * Need to check for errors and implement controls and callbacks
4533 LRESULT WINAPI
REExtendedRegisterClass(void)
4538 FIXME("semi stub\n");
4542 wcW
.hInstance
= NULL
;
4545 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4546 wcW
.lpszMenuName
= NULL
;
4548 if (!ME_ListBoxRegistered
)
4550 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4551 wcW
.lpfnWndProc
= REListWndProc
;
4552 wcW
.lpszClassName
= REListBox20W
;
4553 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4556 if (!ME_ComboBoxRegistered
)
4558 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4559 wcW
.lpfnWndProc
= REComboWndProc
;
4560 wcW
.lpszClassName
= REComboBox20W
;
4561 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
4565 if (ME_ListBoxRegistered
)
4567 if (ME_ComboBoxRegistered
)
4573 static BOOL
isurlspecial(WCHAR c
)
4575 static const WCHAR special_chars
[] = {'.','/','%','@','*','|','\\','+','#',0};
4576 return strchrW( special_chars
, c
) != NULL
;
4580 * This proc takes a selection, and scans it forward in order to select the span
4581 * of a possible URL candidate. A possible URL candidate must start with isalnum
4582 * or one of the following special characters: *|/\+%#@ and must consist entirely
4583 * of the characters allowed to start the URL, plus : (colon) which may occur
4584 * at most once, and not at either end.
4586 * sel_max == -1 indicates scan to end of text.
4588 BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
, int sel_min
, int sel_max
,
4589 int * candidate_min
, int * candidate_max
)
4591 ME_DisplayItem
* item
;
4592 ME_DisplayItem
* para
;
4594 BOOL foundColon
= FALSE
;
4595 WCHAR lastAcceptedChar
= '\0';
4597 TRACE("sel_min = %d sel_max = %d\n", sel_min
, sel_max
);
4599 *candidate_min
= *candidate_max
= -1;
4600 item
= ME_FindItemAtOffset(editor
, diRun
, sel_min
, &nStart
);
4601 if (!item
) return FALSE
;
4602 TRACE("nStart = %d\n", nStart
);
4603 para
= ME_GetParagraph(item
);
4604 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4605 while (item
&& para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
< sel_max
)
4607 ME_DisplayItem
* next_item
;
4609 if (!(item
->member
.run
.nFlags
& MERF_ENDPARA
)) {
4610 /* Find start of candidate */
4611 if (*candidate_min
== -1) {
4612 while (nStart
< ME_StrLen(item
->member
.run
.strText
) &&
4613 !(isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4614 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]))) {
4617 if (nStart
< ME_StrLen(item
->member
.run
.strText
) &&
4618 (isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4619 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]))) {
4620 *candidate_min
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4621 lastAcceptedChar
= item
->member
.run
.strText
->szData
[nStart
];
4626 /* Find end of candidate */
4627 if (*candidate_min
>= 0) {
4628 while (nStart
< ME_StrLen(item
->member
.run
.strText
) &&
4629 (isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4630 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]) ||
4631 (!foundColon
&& item
->member
.run
.strText
->szData
[nStart
] == ':') )) {
4632 if (item
->member
.run
.strText
->szData
[nStart
] == ':') foundColon
= TRUE
;
4633 lastAcceptedChar
= item
->member
.run
.strText
->szData
[nStart
];
4636 if (nStart
< ME_StrLen(item
->member
.run
.strText
) &&
4637 !(isalnumW(item
->member
.run
.strText
->szData
[nStart
]) ||
4638 isurlspecial(item
->member
.run
.strText
->szData
[nStart
]) )) {
4639 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4641 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4646 /* End of paragraph: skip it if before candidate span, or terminates
4647 current active span */
4648 if (*candidate_min
>= 0) {
4649 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
4650 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4655 /* Reaching this point means no span was found, so get next span */
4656 next_item
= ME_FindItemFwd(item
, diRun
);
4658 if (*candidate_min
>= 0) {
4659 /* There are no further runs, so take end of text as end of candidate */
4660 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4661 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4666 para
= ME_GetParagraph(item
);
4671 if (*candidate_min
>= 0) {
4672 /* There are no further runs, so take end of text as end of candidate */
4673 *candidate_max
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
+ nStart
;
4674 if (lastAcceptedChar
== ':') (*candidate_max
)--;
4682 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4684 BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, int sel_min
, int sel_max
)
4690 /* Code below depends on these being in decreasing length order! */
4704 LPWSTR bufferW
= NULL
;
4708 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4709 assert(sel_min
<= sel_max
);
4710 for (i
= 0; i
< sizeof(prefixes
) / sizeof(struct prefix_s
); i
++)
4712 if (sel_max
- sel_min
< prefixes
[i
].length
) continue;
4713 if (bufferW
== NULL
) {
4714 bufferW
= heap_alloc((sel_max
- sel_min
+ 1) * sizeof(WCHAR
));
4716 ME_GetTextW(editor
, bufferW
, sel_min
, min(sel_max
- sel_min
, lstrlenA(prefixes
[i
].text
)), 0);
4717 MultiByteToWideChar(CP_ACP
, 0, prefixes
[i
].text
, -1, bufW
, 32);
4718 if (!lstrcmpW(bufW
, bufferW
))
4729 * This proc walks through the indicated selection and evaluates whether each
4730 * section identified by ME_FindNextURLCandidate and in-between sections have
4731 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4732 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4734 * Returns TRUE if at least one section was modified.
4736 BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, int sel_min
, int sel_max
)
4738 BOOL modified
= FALSE
;
4741 if (sel_max
== -1) sel_max
= ME_GetTextLength(editor
);
4748 if (ME_FindNextURLCandidate(editor
, sel_min
, sel_max
, &cMin
, &cMax
))
4750 /* Section before candidate is not an URL */
4751 beforeURL
[0] = sel_min
;
4752 beforeURL
[1] = cMin
;
4754 if (ME_IsCandidateAnURL(editor
, cMin
, cMax
))
4756 inURL
[0] = cMin
; inURL
[1] = cMax
;
4760 beforeURL
[1] = cMax
;
4761 inURL
[0] = inURL
[1] = -1;
4767 /* No more candidates until end of selection */
4768 beforeURL
[0] = sel_min
;
4769 beforeURL
[1] = sel_max
;
4770 inURL
[0] = inURL
[1] = -1;
4774 if (beforeURL
[0] < beforeURL
[1])
4776 /* CFE_LINK effect should be consistently unset */
4777 link
.cbSize
= sizeof(link
);
4778 ME_GetCharFormat(editor
, beforeURL
[0], beforeURL
[1], &link
);
4779 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
4781 /* CFE_LINK must be unset from this range */
4782 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4783 link
.cbSize
= sizeof(link
);
4784 link
.dwMask
= CFM_LINK
;
4786 ME_SetCharFormat(editor
, beforeURL
[0], beforeURL
[1] - beforeURL
[0], &link
);
4790 if (inURL
[0] < inURL
[1])
4792 /* CFE_LINK effect should be consistently set */
4793 link
.cbSize
= sizeof(link
);
4794 ME_GetCharFormat(editor
, inURL
[0], inURL
[1], &link
);
4795 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
4797 /* CFE_LINK must be set on this range */
4798 memset(&link
, 0, sizeof(CHARFORMAT2W
));
4799 link
.cbSize
= sizeof(link
);
4800 link
.dwMask
= CFM_LINK
;
4801 link
.dwEffects
= CFE_LINK
;
4802 ME_SetCharFormat(editor
, inURL
[0], inURL
[1] - inURL
[0], &link
);
4806 } while (sel_min
< sel_max
);
4810 void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
4812 ME_DisplayItem
* startPara
, * endPara
;
4813 ME_DisplayItem
* item
;
4817 ME_GetSelection(editor
, &from
, &to
);
4818 if (from
> to
) from
^= to
, to
^=from
, from
^= to
;
4819 startPara
= NULL
; endPara
= NULL
;
4821 /* Find paragraph previous to the one that contains start cursor */
4822 item
= ME_FindItemAtOffset(editor
, diRun
, from
, &dummy
);
4824 startPara
= ME_FindItemBack(item
, diParagraph
);
4825 item
= ME_FindItemBack(startPara
, diParagraph
);
4826 if (item
) startPara
= item
;
4829 /* Find paragraph that contains end cursor */
4830 item
= ME_FindItemAtOffset(editor
, diRun
, to
, &dummy
);
4832 endPara
= ME_FindItemFwd(item
, diParagraph
);
4835 if (startPara
&& endPara
) {
4836 ME_UpdateLinkAttribute(editor
,
4837 startPara
->member
.para
.nCharOfs
,
4838 endPara
->member
.para
.nCharOfs
);
4839 } else if (startPara
) {
4840 ME_UpdateLinkAttribute(editor
,
4841 startPara
->member
.para
.nCharOfs
,