2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
54 - EM_GETLANGOPTIONS 2.0
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
67 + EM_GETSELTEXT (ANSI&Unicode)
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
94 - EM_SETBIDIOPTIONS 3.0
96 + EM_SETCHARFORMAT (partly done, no ANSI)
98 + EM_SETEVENTMASK (few notifications supported)
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
103 - EM_SETLANGOPTIONS 2.0
106 + EM_SETMODIFY (not sure if implementation is correct)
108 + EM_SETOPTIONS (partially implemented)
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
115 + EM_SETRECTNP (EM_SETRECT without repainting)
117 + EM_SETSCROLLPOS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
151 * EN_CHANGE (sent from the wrong place)
168 * EN_UPDATE (sent from the wrong place)
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
179 - ES_MULTILINE (currently single line controls aren't supported)
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
194 * RICHED20 TODO (incomplete):
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
214 * - ListBox & ComboBox not implemented
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
227 #define NONAMELESSUNION
232 #define NO_SHLWAPI_STREAM
238 #define STACK_SIZE_DEFAULT 100
239 #define STACK_SIZE_MAX 1000
241 #define TEXT_LIMIT_DEFAULT 32767
243 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
245 static BOOL
ME_RegisterEditorClass(HINSTANCE
);
246 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
);
248 static const WCHAR REListBox20W
[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
249 static const WCHAR REComboBox20W
[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
250 static HCURSOR hLeft
;
252 BOOL me_debug
= FALSE
;
253 HANDLE me_heap
= NULL
;
255 static BOOL ME_ListBoxRegistered
= FALSE
;
256 static BOOL ME_ComboBoxRegistered
= FALSE
;
258 static inline BOOL
is_version_nt(void)
260 return !(GetVersion() & 0x80000000);
263 static ME_TextBuffer
*ME_MakeText(void) {
265 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
267 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
268 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
274 p1
->member
.para
.next_para
= p2
;
275 p2
->member
.para
.prev_para
= p1
;
276 p2
->member
.para
.nCharOfs
= 0;
280 buf
->pCharStyle
= NULL
;
286 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
289 LRESULT total_bytes_read
= 0;
290 BOOL is_read
= FALSE
;
291 DWORD cp
= CP_ACP
, copy
= 0;
292 char conv_buf
[4 + STREAMIN_BUFFER_SIZE
]; /* up to 4 additional UTF-8 bytes */
294 static const char bom_utf8
[] = {0xEF, 0xBB, 0xBF};
296 TRACE("%08x %p\n", dwFormat
, stream
);
300 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
304 ME_StreamInFill(stream
);
305 if (stream
->editstream
->dwError
)
309 total_bytes_read
+= stream
->dwSize
;
312 if (!(dwFormat
& SF_UNICODE
))
314 char * buf
= stream
->buffer
;
315 DWORD size
= stream
->dwSize
, end
;
320 if (stream
->dwSize
>= 3 && !memcmp(stream
->buffer
, bom_utf8
, 3))
332 memcpy(conv_buf
+ copy
, buf
, size
);
337 while ((buf
[end
-1] & 0xC0) == 0x80)
340 --total_bytes_read
; /* strange, but seems to match windows */
342 if (buf
[end
-1] & 0x80)
345 if ((buf
[end
-1] & 0xE0) == 0xC0)
347 if ((buf
[end
-1] & 0xF0) == 0xE0)
349 if ((buf
[end
-1] & 0xF8) == 0xF0)
352 if (size
- end
>= need
)
354 /* we have enough bytes for this sequence */
359 /* need more bytes, so don't transcode this sequence */
367 nWideChars
= MultiByteToWideChar(cp
, 0, buf
, end
, wszText
, STREAMIN_BUFFER_SIZE
);
374 memcpy(conv_buf
, buf
+ end
, size
- end
);
381 nWideChars
= stream
->dwSize
>> 1;
382 pText
= (WCHAR
*)stream
->buffer
;
385 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
386 if (stream
->dwSize
== 0)
390 return total_bytes_read
;
393 static void ME_ApplyBorderProperties(RTF_Info
*info
,
394 ME_BorderRect
*borderRect
,
395 RTFBorder
*borderDef
)
398 ME_Border
*pBorders
[] = {&borderRect
->top
,
402 for (i
= 0; i
< 4; i
++)
404 RTFColor
*colorDef
= info
->colorList
;
405 pBorders
[i
]->width
= borderDef
[i
].width
;
406 colorNum
= borderDef
[i
].color
;
407 while (colorDef
&& colorDef
->rtfCNum
!= colorNum
)
408 colorDef
= colorDef
->rtfNextColor
;
410 pBorders
[i
]->colorRef
= RGB(
411 colorDef
->rtfCRed
>= 0 ? colorDef
->rtfCRed
: 0,
412 colorDef
->rtfCGreen
>= 0 ? colorDef
->rtfCGreen
: 0,
413 colorDef
->rtfCBlue
>= 0 ? colorDef
->rtfCBlue
: 0);
415 pBorders
[i
]->colorRef
= RGB(0, 0, 0);
419 void ME_RTFCharAttrHook(RTF_Info
*info
)
422 fmt
.cbSize
= sizeof(fmt
);
426 switch(info
->rtfMinor
)
429 /* FIXME add more flags once they're implemented */
430 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINETYPE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
431 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
432 fmt
.yHeight
= 12*20; /* 12pt */
433 fmt
.wWeight
= FW_NORMAL
;
434 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
437 fmt
.dwMask
= CFM_BOLD
| CFM_WEIGHT
;
438 fmt
.dwEffects
= info
->rtfParam
? CFE_BOLD
: 0;
439 fmt
.wWeight
= info
->rtfParam
? FW_BOLD
: FW_NORMAL
;
442 fmt
.dwMask
= CFM_ITALIC
;
443 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
446 fmt
.dwMask
= CFM_UNDERLINETYPE
;
447 fmt
.bUnderlineType
= info
->rtfParam
? CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
449 case rtfDotUnderline
:
450 fmt
.dwMask
= CFM_UNDERLINETYPE
;
451 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOTTED
: CFU_UNDERLINENONE
;
454 fmt
.dwMask
= CFM_UNDERLINETYPE
;
455 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEDOUBLE
: CFU_UNDERLINENONE
;
457 case rtfWordUnderline
:
458 fmt
.dwMask
= CFM_UNDERLINETYPE
;
459 fmt
.bUnderlineType
= info
->rtfParam
? CFU_UNDERLINEWORD
: CFU_UNDERLINENONE
;
462 fmt
.dwMask
= CFM_UNDERLINETYPE
;
463 fmt
.bUnderlineType
= CFU_UNDERLINENONE
;
466 fmt
.dwMask
= CFM_STRIKEOUT
;
467 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
471 case rtfSubScrShrink
:
472 case rtfSuperScrShrink
:
474 fmt
.dwMask
= CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
;
475 if (info
->rtfMinor
== rtfSubScrShrink
) fmt
.dwEffects
= CFE_SUBSCRIPT
;
476 if (info
->rtfMinor
== rtfSuperScrShrink
) fmt
.dwEffects
= CFE_SUPERSCRIPT
;
477 if (info
->rtfMinor
== rtfNoSuperSub
) fmt
.dwEffects
= 0;
480 fmt
.dwMask
= CFM_HIDDEN
;
481 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
484 fmt
.dwMask
= CFM_BACKCOLOR
;
486 if (info
->rtfParam
== 0)
487 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
488 else if (info
->rtfParam
!= rtfNoParam
)
490 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
491 if (c
&& c
->rtfCBlue
>= 0)
492 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
494 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
498 fmt
.dwMask
= CFM_COLOR
;
500 if (info
->rtfParam
== 0)
501 fmt
.dwEffects
= CFE_AUTOCOLOR
;
502 else if (info
->rtfParam
!= rtfNoParam
)
504 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
505 if (c
&& c
->rtfCBlue
>= 0)
506 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
508 fmt
.dwEffects
= CFE_AUTOCOLOR
;
513 if (info
->rtfParam
!= rtfNoParam
)
515 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
518 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
519 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
520 fmt
.bCharSet
= f
->rtfFCharSet
;
521 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
522 fmt
.bPitchAndFamily
= f
->rtfFPitch
| (f
->rtfFFamily
<< 4);
527 fmt
.dwMask
= CFM_SIZE
;
528 if (info
->rtfParam
!= rtfNoParam
)
529 fmt
.yHeight
= info
->rtfParam
*10;
534 RTFFlushOutputBuffer(info
);
535 /* FIXME too slow ? how come ? */
536 style2
= ME_ApplyStyle(info
->editor
, info
->style
, &fmt
);
537 ME_ReleaseStyle(info
->style
);
538 info
->style
= style2
;
539 info
->styleChanged
= TRUE
;
543 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
544 the same tags mean different things in different contexts */
545 void ME_RTFParAttrHook(RTF_Info
*info
)
547 switch(info
->rtfMinor
)
549 case rtfParDef
: /* restores default paragraph attributes */
550 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
551 info
->borderType
= RTFBorderParaLeft
;
552 else /* v1.0 - 3.0 */
553 info
->borderType
= RTFBorderParaTop
;
554 info
->fmt
.dwMask
= PFM_ALIGNMENT
| PFM_BORDER
| PFM_LINESPACING
| PFM_TABSTOPS
|
555 PFM_OFFSET
| PFM_RIGHTINDENT
| PFM_SPACEAFTER
| PFM_SPACEBEFORE
|
556 PFM_STARTINDENT
| PFM_RTLPARA
;
557 /* TODO: numbering, shading */
558 info
->fmt
.wAlignment
= PFA_LEFT
;
559 info
->fmt
.cTabCount
= 0;
560 info
->fmt
.dxOffset
= info
->fmt
.dxStartIndent
= info
->fmt
.dxRightIndent
= 0;
561 info
->fmt
.wBorderWidth
= info
->fmt
.wBorders
= 0;
562 info
->fmt
.wBorderSpace
= 0;
563 info
->fmt
.bLineSpacingRule
= 0;
564 info
->fmt
.dySpaceBefore
= info
->fmt
.dySpaceAfter
= 0;
565 info
->fmt
.dyLineSpacing
= 0;
566 info
->fmt
.wEffects
&= ~PFE_RTLPARA
;
567 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
569 if (info
->tableDef
&& info
->tableDef
->tableRowStart
&&
570 info
->tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
573 ME_DisplayItem
*para
;
574 /* We are just after a table row. */
575 RTFFlushOutputBuffer(info
);
576 cursor
= info
->editor
->pCursors
[0];
578 if (para
== info
->tableDef
->tableRowStart
->member
.para
.next_para
579 && !cursor
.nOffset
&& !cursor
.pRun
->member
.run
.nCharOfs
)
581 /* Since the table row end, no text has been inserted, and the \intbl
582 * control word has not be used. We can confirm that we are not in a
585 info
->tableDef
->tableRowStart
= NULL
;
586 info
->canInheritInTbl
= FALSE
;
589 } else { /* v1.0 - v3.0 */
590 info
->fmt
.dwMask
|= PFM_TABLE
;
591 info
->fmt
.wEffects
&= ~PFE_TABLE
;
595 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
597 while (info
->rtfParam
> info
->nestingLevel
) {
598 RTFTable
*tableDef
= ALLOC_OBJ(RTFTable
);
599 ZeroMemory(tableDef
, sizeof(RTFTable
));
600 tableDef
->parent
= info
->tableDef
;
601 info
->tableDef
= tableDef
;
603 RTFFlushOutputBuffer(info
);
604 if (tableDef
->tableRowStart
&&
605 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
607 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
608 para
= para
->member
.para
.next_para
;
609 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
610 tableDef
->tableRowStart
= para
;
614 cursor
= info
->editor
->pCursors
[0];
615 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
616 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
617 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
620 info
->nestingLevel
++;
622 info
->canInheritInTbl
= FALSE
;
627 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
629 if (info
->nestingLevel
< 1)
634 info
->tableDef
= ALLOC_OBJ(RTFTable
);
635 ZeroMemory(info
->tableDef
, sizeof(RTFTable
));
637 tableDef
= info
->tableDef
;
638 RTFFlushOutputBuffer(info
);
639 if (tableDef
->tableRowStart
&&
640 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
642 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
643 para
= para
->member
.para
.next_para
;
644 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
645 tableDef
->tableRowStart
= para
;
649 cursor
= info
->editor
->pCursors
[0];
650 if (cursor
.nOffset
|| cursor
.pRun
->member
.run
.nCharOfs
)
651 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
652 tableDef
->tableRowStart
= ME_InsertTableRowStartFromCursor(info
->editor
);
654 info
->nestingLevel
= 1;
655 info
->canInheritInTbl
= TRUE
;
658 } else { /* v1.0 - v3.0 */
659 info
->fmt
.dwMask
|= PFM_TABLE
;
660 info
->fmt
.wEffects
|= PFE_TABLE
;
666 if ((info
->fmt
.dwMask
& (PFM_STARTINDENT
| PFM_OFFSET
)) != (PFM_STARTINDENT
| PFM_OFFSET
))
669 fmt
.cbSize
= sizeof(fmt
);
670 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
671 info
->fmt
.dwMask
|= PFM_STARTINDENT
| PFM_OFFSET
;
672 info
->fmt
.dxStartIndent
= fmt
.dxStartIndent
;
673 info
->fmt
.dxOffset
= fmt
.dxOffset
;
675 if (info
->rtfMinor
== rtfFirstIndent
)
677 info
->fmt
.dxStartIndent
+= info
->fmt
.dxOffset
+ info
->rtfParam
;
678 info
->fmt
.dxOffset
= -info
->rtfParam
;
681 info
->fmt
.dxStartIndent
= info
->rtfParam
- info
->fmt
.dxOffset
;
684 info
->fmt
.dwMask
|= PFM_RIGHTINDENT
;
685 info
->fmt
.dxRightIndent
= info
->rtfParam
;
689 info
->fmt
.dwMask
|= PFM_ALIGNMENT
;
690 info
->fmt
.wAlignment
= PFA_LEFT
;
693 info
->fmt
.dwMask
|= PFM_ALIGNMENT
;
694 info
->fmt
.wAlignment
= PFA_RIGHT
;
697 info
->fmt
.dwMask
|= PFM_ALIGNMENT
;
698 info
->fmt
.wAlignment
= PFA_CENTER
;
701 if (!(info
->fmt
.dwMask
& PFM_TABSTOPS
))
704 fmt
.cbSize
= sizeof(fmt
);
705 ME_GetSelectionParaFormat(info
->editor
, &fmt
);
706 memcpy(info
->fmt
.rgxTabs
, fmt
.rgxTabs
,
707 fmt
.cTabCount
* sizeof(fmt
.rgxTabs
[0]));
708 info
->fmt
.cTabCount
= fmt
.cTabCount
;
709 info
->fmt
.dwMask
|= PFM_TABSTOPS
;
711 if (info
->fmt
.cTabCount
< MAX_TAB_STOPS
&& info
->rtfParam
< 0x1000000)
712 info
->fmt
.rgxTabs
[info
->fmt
.cTabCount
++] = info
->rtfParam
;
715 info
->fmt
.dwMask
|= PFM_KEEP
;
716 info
->fmt
.wEffects
|= PFE_KEEP
;
718 case rtfNoWidowControl
:
719 info
->fmt
.dwMask
|= PFM_NOWIDOWCONTROL
;
720 info
->fmt
.wEffects
|= PFE_NOWIDOWCONTROL
;
723 info
->fmt
.dwMask
|= PFM_KEEPNEXT
;
724 info
->fmt
.wEffects
|= PFE_KEEPNEXT
;
727 info
->fmt
.dwMask
|= PFM_SPACEAFTER
;
728 info
->fmt
.dySpaceAfter
= info
->rtfParam
;
731 info
->fmt
.dwMask
|= PFM_SPACEBEFORE
;
732 info
->fmt
.dySpaceBefore
= info
->rtfParam
;
734 case rtfSpaceBetween
:
735 info
->fmt
.dwMask
|= PFM_LINESPACING
;
736 if ((int)info
->rtfParam
> 0)
738 info
->fmt
.dyLineSpacing
= info
->rtfParam
;
739 info
->fmt
.bLineSpacingRule
= 3;
743 info
->fmt
.dyLineSpacing
= info
->rtfParam
;
744 info
->fmt
.bLineSpacingRule
= 4;
747 case rtfSpaceMultiply
:
748 info
->fmt
.dwMask
|= PFM_LINESPACING
;
749 info
->fmt
.dyLineSpacing
= info
->rtfParam
* 20;
750 info
->fmt
.bLineSpacingRule
= 5;
753 info
->fmt
.dwMask
|= PFM_NUMBERING
;
754 info
->fmt
.wNumbering
= PFN_BULLET
;
757 info
->fmt
.dwMask
|= PFM_NUMBERING
;
758 info
->fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
760 case rtfParNumDecimal
:
761 info
->fmt
.dwMask
|= PFM_NUMBERING
;
762 info
->fmt
.wNumbering
= 2; /* FIXME: MSDN says it's not used ?? */
764 case rtfParNumIndent
:
765 info
->fmt
.dwMask
|= PFM_NUMBERINGTAB
;
766 info
->fmt
.wNumberingTab
= info
->rtfParam
;
768 case rtfParNumStartAt
:
769 info
->fmt
.dwMask
|= PFM_NUMBERINGSTART
;
770 info
->fmt
.wNumberingStart
= info
->rtfParam
;
773 info
->borderType
= RTFBorderParaLeft
;
774 info
->fmt
.wBorders
|= 1;
775 info
->fmt
.dwMask
|= PFM_BORDER
;
778 info
->borderType
= RTFBorderParaRight
;
779 info
->fmt
.wBorders
|= 2;
780 info
->fmt
.dwMask
|= PFM_BORDER
;
783 info
->borderType
= RTFBorderParaTop
;
784 info
->fmt
.wBorders
|= 4;
785 info
->fmt
.dwMask
|= PFM_BORDER
;
787 case rtfBorderBottom
:
788 info
->borderType
= RTFBorderParaBottom
;
789 info
->fmt
.wBorders
|= 8;
790 info
->fmt
.dwMask
|= PFM_BORDER
;
792 case rtfBorderSingle
:
793 info
->fmt
.wBorders
&= ~0x700;
794 info
->fmt
.wBorders
|= 1 << 8;
795 info
->fmt
.dwMask
|= PFM_BORDER
;
798 info
->fmt
.wBorders
&= ~0x700;
799 info
->fmt
.wBorders
|= 2 << 8;
800 info
->fmt
.dwMask
|= PFM_BORDER
;
802 case rtfBorderShadow
:
803 info
->fmt
.wBorders
&= ~0x700;
804 info
->fmt
.wBorders
|= 10 << 8;
805 info
->fmt
.dwMask
|= PFM_BORDER
;
807 case rtfBorderDouble
:
808 info
->fmt
.wBorders
&= ~0x700;
809 info
->fmt
.wBorders
|= 7 << 8;
810 info
->fmt
.dwMask
|= PFM_BORDER
;
813 info
->fmt
.wBorders
&= ~0x700;
814 info
->fmt
.wBorders
|= 11 << 8;
815 info
->fmt
.dwMask
|= PFM_BORDER
;
819 int borderSide
= info
->borderType
& RTFBorderSideMask
;
820 RTFTable
*tableDef
= info
->tableDef
;
821 if ((info
->borderType
& RTFBorderTypeMask
) == RTFBorderTypeCell
)
824 if (!tableDef
|| tableDef
->numCellsDefined
>= MAX_TABLE_CELLS
)
826 border
= &tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
];
827 border
->width
= info
->rtfParam
;
830 info
->fmt
.wBorderWidth
= info
->rtfParam
;
831 info
->fmt
.dwMask
|= PFM_BORDER
;
835 info
->fmt
.wBorderSpace
= info
->rtfParam
;
836 info
->fmt
.dwMask
|= PFM_BORDER
;
840 RTFTable
*tableDef
= info
->tableDef
;
841 int borderSide
= info
->borderType
& RTFBorderSideMask
;
842 int borderType
= info
->borderType
& RTFBorderTypeMask
;
845 case RTFBorderTypePara
:
846 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
848 /* v1.0 - 3.0 treat paragraph and row borders the same. */
849 case RTFBorderTypeRow
:
851 tableDef
->border
[borderSide
].color
= info
->rtfParam
;
854 case RTFBorderTypeCell
:
855 if (tableDef
&& tableDef
->numCellsDefined
< MAX_TABLE_CELLS
) {
856 tableDef
->cells
[tableDef
->numCellsDefined
].border
[borderSide
].color
= info
->rtfParam
;
863 info
->fmt
.dwMask
|= PFM_RTLPARA
;
864 info
->fmt
.wEffects
|= PFE_RTLPARA
;
867 info
->fmt
.dwMask
= PFM_RTLPARA
;
868 info
->fmt
.wEffects
&= ~PFE_RTLPARA
;
873 void ME_RTFTblAttrHook(RTF_Info
*info
)
875 switch (info
->rtfMinor
)
879 if (!info
->editor
->bEmulateVersion10
) /* v4.1 */
880 info
->borderType
= 0; /* Not sure */
881 else /* v1.0 - 3.0 */
882 info
->borderType
= RTFBorderRowTop
;
883 if (!info
->tableDef
) {
884 info
->tableDef
= ME_MakeTableDef(info
->editor
);
886 ME_InitTableDef(info
->editor
, info
->tableDef
);
895 info
->tableDef
= ME_MakeTableDef(info
->editor
);
897 cellNum
= info
->tableDef
->numCellsDefined
;
898 if (cellNum
>= MAX_TABLE_CELLS
)
900 info
->tableDef
->cells
[cellNum
].rightBoundary
= info
->rtfParam
;
901 if (cellNum
< MAX_TAB_STOPS
) {
902 /* Tab stops were used to store cell positions before v4.1 but v4.1
903 * still seems to set the tabstops without using them. */
904 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
905 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
906 pFmt
->rgxTabs
[cellNum
] &= ~0x00FFFFFF;
907 pFmt
->rgxTabs
[cellNum
] |= 0x00FFFFFF & info
->rtfParam
;
909 info
->tableDef
->numCellsDefined
++;
913 info
->borderType
= RTFBorderRowTop
;
916 info
->borderType
= RTFBorderRowLeft
;
918 case rtfRowBordBottom
:
919 info
->borderType
= RTFBorderRowBottom
;
921 case rtfRowBordRight
:
922 info
->borderType
= RTFBorderRowRight
;
925 info
->borderType
= RTFBorderCellTop
;
927 case rtfCellBordLeft
:
928 info
->borderType
= RTFBorderCellLeft
;
930 case rtfCellBordBottom
:
931 info
->borderType
= RTFBorderCellBottom
;
933 case rtfCellBordRight
:
934 info
->borderType
= RTFBorderCellRight
;
938 info
->tableDef
->gapH
= info
->rtfParam
;
942 info
->tableDef
->leftEdge
= info
->rtfParam
;
947 void ME_RTFSpecialCharHook(RTF_Info
*info
)
949 RTFTable
*tableDef
= info
->tableDef
;
950 switch (info
->rtfMinor
)
953 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
955 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
959 RTFFlushOutputBuffer(info
);
960 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
961 if (tableDef
->tableRowStart
)
963 if (!info
->nestingLevel
&&
964 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
966 ME_DisplayItem
*para
= tableDef
->tableRowStart
;
967 para
= para
->member
.para
.next_para
;
968 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
969 tableDef
->tableRowStart
= para
;
970 info
->nestingLevel
= 1;
972 ME_InsertTableCellFromCursor(info
->editor
);
974 } else { /* v1.0 - v3.0 */
975 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
976 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
977 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
&&
978 tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
981 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
982 tableDef
->numCellsInserted
++;
987 if (info
->editor
->bEmulateVersion10
) /* v1.0 - v3.0 */
989 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
992 ME_DisplayItem
*para
, *cell
, *run
;
997 RTFFlushOutputBuffer(info
);
998 if (!info
->editor
->bEmulateVersion10
) { /* v4.1 */
999 if (!tableDef
->tableRowStart
)
1001 if (!info
->nestingLevel
&&
1002 tableDef
->tableRowStart
->member
.para
.nFlags
& MEPF_ROWEND
)
1004 para
= tableDef
->tableRowStart
;
1005 para
= para
->member
.para
.next_para
;
1006 para
= ME_InsertTableRowStartAtParagraph(info
->editor
, para
);
1007 tableDef
->tableRowStart
= para
;
1008 info
->nestingLevel
++;
1010 para
= tableDef
->tableRowStart
;
1011 cell
= ME_FindItemFwd(para
, diCell
);
1012 assert(cell
&& !cell
->member
.cell
.prev_cell
);
1013 if (tableDef
->numCellsDefined
< 1)
1015 /* 2000 twips appears to be the cell size that native richedit uses
1016 * when no cell sizes are specified. */
1017 const int defaultCellSize
= 2000;
1018 int nRightBoundary
= defaultCellSize
;
1019 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1020 while (cell
->member
.cell
.next_cell
) {
1021 cell
= cell
->member
.cell
.next_cell
;
1022 nRightBoundary
+= defaultCellSize
;
1023 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1025 para
= ME_InsertTableCellFromCursor(info
->editor
);
1026 cell
= para
->member
.para
.pCell
;
1027 cell
->member
.cell
.nRightBoundary
= nRightBoundary
;
1029 for (i
= 0; i
< tableDef
->numCellsDefined
; i
++)
1031 RTFCell
*cellDef
= &tableDef
->cells
[i
];
1032 cell
->member
.cell
.nRightBoundary
= cellDef
->rightBoundary
;
1033 ME_ApplyBorderProperties(info
, &cell
->member
.cell
.border
,
1035 cell
= cell
->member
.cell
.next_cell
;
1038 para
= ME_InsertTableCellFromCursor(info
->editor
);
1039 cell
= para
->member
.para
.pCell
;
1042 /* Cell for table row delimiter is empty */
1043 cell
->member
.cell
.nRightBoundary
= tableDef
->cells
[i
-1].rightBoundary
;
1046 run
= ME_FindItemFwd(cell
, diRun
);
1047 if (info
->editor
->pCursors
[0].pRun
!= run
||
1048 info
->editor
->pCursors
[0].nOffset
)
1051 /* Delete inserted cells that aren't defined. */
1052 info
->editor
->pCursors
[1].pRun
= run
;
1053 info
->editor
->pCursors
[1].pPara
= ME_GetParagraph(run
);
1054 info
->editor
->pCursors
[1].nOffset
= 0;
1055 nOfs
= ME_GetCursorOfs(&info
->editor
->pCursors
[1]);
1056 nChars
= ME_GetCursorOfs(&info
->editor
->pCursors
[0]) - nOfs
;
1057 ME_InternalDeleteText(info
->editor
, &info
->editor
->pCursors
[1],
1061 para
= ME_InsertTableRowEndFromCursor(info
->editor
);
1062 para
->member
.para
.pFmt
->dxOffset
= abs(info
->tableDef
->gapH
);
1063 para
->member
.para
.pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1064 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1066 info
->nestingLevel
--;
1067 if (!info
->nestingLevel
)
1069 if (info
->canInheritInTbl
) {
1070 tableDef
->tableRowStart
= para
;
1072 while (info
->tableDef
) {
1073 tableDef
= info
->tableDef
;
1074 info
->tableDef
= tableDef
->parent
;
1075 heap_free(tableDef
);
1079 info
->tableDef
= tableDef
->parent
;
1080 heap_free(tableDef
);
1082 } else { /* v1.0 - v3.0 */
1084 ME_DisplayItem
*para
= info
->editor
->pCursors
[0].pPara
;
1085 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
1086 pFmt
->dxOffset
= info
->tableDef
->gapH
;
1087 pFmt
->dxStartIndent
= info
->tableDef
->leftEdge
;
1089 ME_ApplyBorderProperties(info
, ¶
->member
.para
.border
,
1091 while (tableDef
->numCellsInserted
< tableDef
->numCellsDefined
)
1094 ME_InsertTextFromCursor(info
->editor
, 0, &tab
, 1, info
->style
);
1095 tableDef
->numCellsInserted
++;
1097 pFmt
->cTabCount
= min(tableDef
->numCellsDefined
, MAX_TAB_STOPS
);
1098 if (!tableDef
->numCellsDefined
)
1099 pFmt
->wEffects
&= ~PFE_TABLE
;
1100 ME_InsertTextFromCursor(info
->editor
, 0, &endl
, 1, info
->style
);
1101 tableDef
->numCellsInserted
= 0;
1107 if (info
->editor
->bEmulateVersion10
) { /* v1.0 - 3.0 */
1108 ME_DisplayItem
*para
;
1110 RTFFlushOutputBuffer(info
);
1111 para
= info
->editor
->pCursors
[0].pPara
;
1112 pFmt
= para
->member
.para
.pFmt
;
1113 if (pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
1115 /* rtfPar is treated like a space within a table. */
1116 info
->rtfClass
= rtfText
;
1117 info
->rtfMajor
= ' ';
1119 else if (info
->rtfMinor
== rtfPar
&& tableDef
)
1120 tableDef
->numCellsInserted
= 0;
1126 static BOOL
ME_RTFInsertOleObject(RTF_Info
*info
, HENHMETAFILE hemf
, HBITMAP hbmp
,
1129 LPOLEOBJECT lpObject
= NULL
;
1130 LPSTORAGE lpStorage
= NULL
;
1131 LPOLECLIENTSITE lpClientSite
= NULL
;
1132 LPDATAOBJECT lpDataObject
= NULL
;
1133 LPOLECACHE lpOleCache
= NULL
;
1142 stgm
.tymed
= TYMED_ENHMF
;
1143 stgm
.u
.hEnhMetaFile
= hemf
;
1144 fm
.cfFormat
= CF_ENHMETAFILE
;
1148 stgm
.tymed
= TYMED_GDI
;
1149 stgm
.u
.hBitmap
= hbmp
;
1150 fm
.cfFormat
= CF_BITMAP
;
1152 stgm
.pUnkForRelease
= NULL
;
1155 fm
.dwAspect
= DVASPECT_CONTENT
;
1157 fm
.tymed
= stgm
.tymed
;
1159 if (!info
->lpRichEditOle
)
1161 CreateIRichEditOle(NULL
, info
->editor
, (VOID
**)&info
->lpRichEditOle
);
1164 if (OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
, (void**)&lpObject
) == S_OK
&&
1165 IRichEditOle_GetClientSite(info
->lpRichEditOle
, &lpClientSite
) == S_OK
&&
1166 IOleObject_SetClientSite(lpObject
, lpClientSite
) == S_OK
&&
1167 IOleObject_GetUserClassID(lpObject
, &clsid
) == S_OK
&&
1168 IOleObject_QueryInterface(lpObject
, &IID_IOleCache
, (void**)&lpOleCache
) == S_OK
&&
1169 IOleCache_Cache(lpOleCache
, &fm
, 0, &conn
) == S_OK
&&
1170 IOleObject_QueryInterface(lpObject
, &IID_IDataObject
, (void**)&lpDataObject
) == S_OK
&&
1171 IDataObject_SetData(lpDataObject
, &fm
, &stgm
, TRUE
) == S_OK
)
1175 reobject
.cbStruct
= sizeof(reobject
);
1176 reobject
.cp
= REO_CP_SELECTION
;
1177 reobject
.clsid
= clsid
;
1178 reobject
.poleobj
= lpObject
;
1179 reobject
.pstg
= lpStorage
;
1180 reobject
.polesite
= lpClientSite
;
1181 /* convert from twips to .01 mm */
1182 reobject
.sizel
.cx
= MulDiv(sz
->cx
, 254, 144);
1183 reobject
.sizel
.cy
= MulDiv(sz
->cy
, 254, 144);
1184 reobject
.dvaspect
= DVASPECT_CONTENT
;
1185 reobject
.dwFlags
= 0; /* FIXME */
1186 reobject
.dwUser
= 0;
1188 ME_InsertOLEFromCursor(info
->editor
, &reobject
, 0);
1192 if (lpObject
) IOleObject_Release(lpObject
);
1193 if (lpClientSite
) IOleClientSite_Release(lpClientSite
);
1194 if (lpStorage
) IStorage_Release(lpStorage
);
1195 if (lpDataObject
) IDataObject_Release(lpDataObject
);
1196 if (lpOleCache
) IOleCache_Release(lpOleCache
);
1201 static void ME_RTFReadShpPictGroup( RTF_Info
*info
)
1209 if (info
->rtfClass
== rtfEOF
) return;
1210 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1212 if (--level
== 0) break;
1214 else if (RTFCheckCM( info
, rtfGroup
, rtfBeginGroup
))
1220 RTFRouteToken( info
);
1221 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1226 RTFRouteToken( info
); /* feed "}" back to router */
1230 static DWORD
read_hex_data( RTF_Info
*info
, BYTE
**out
)
1232 DWORD read
= 0, size
= 1024;
1238 if (info
->rtfClass
!= rtfText
)
1240 ERR("Called with incorrect token\n");
1244 buf
= HeapAlloc( GetProcessHeap(), 0, size
);
1247 val
= info
->rtfMajor
;
1248 for (flip
= TRUE
;; flip
= !flip
)
1250 RTFGetToken( info
);
1251 if (info
->rtfClass
== rtfEOF
)
1253 HeapFree( GetProcessHeap(), 0, buf
);
1256 if (info
->rtfClass
!= rtfText
) break;
1262 buf
= HeapReAlloc( GetProcessHeap(), 0, buf
, size
);
1265 buf
[read
++] = RTFCharToHex(val
) * 16 + RTFCharToHex(info
->rtfMajor
);
1268 val
= info
->rtfMajor
;
1270 if (flip
) FIXME("wrong hex string\n");
1276 static void ME_RTFReadPictGroup(RTF_Info
*info
)
1279 BYTE
*buffer
= NULL
;
1284 enum gfxkind
{gfx_unknown
= 0, gfx_enhmetafile
, gfx_metafile
, gfx_dib
} gfx
= gfx_unknown
;
1292 RTFGetToken( info
);
1294 if (info
->rtfClass
== rtfText
)
1299 size
= read_hex_data( info
, &buffer
);
1303 RTFSkipGroup( info
);
1305 } /* We potentially have a new token so fall through. */
1307 if (info
->rtfClass
== rtfEOF
) return;
1309 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1311 if (--level
== 0) break;
1314 if (RTFCheckCM( info
, rtfGroup
, rtfBeginGroup
))
1319 if (!RTFCheckCM( info
, rtfControl
, rtfPictAttr
))
1321 RTFRouteToken( info
);
1322 if (RTFCheckCM( info
, rtfGroup
, rtfEndGroup
))
1327 if (RTFCheckMM( info
, rtfPictAttr
, rtfWinMetafile
))
1329 mfp
.mm
= info
->rtfParam
;
1332 else if (RTFCheckMM( info
, rtfPictAttr
, rtfDevIndBitmap
))
1334 if (info
->rtfParam
!= 0) FIXME("dibitmap should be 0 (%d)\n", info
->rtfParam
);
1337 else if (RTFCheckMM( info
, rtfPictAttr
, rtfEmfBlip
))
1338 gfx
= gfx_enhmetafile
;
1339 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicWid
))
1340 mfp
.xExt
= info
->rtfParam
;
1341 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicHt
))
1342 mfp
.yExt
= info
->rtfParam
;
1343 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicGoalWid
))
1344 sz
.cx
= info
->rtfParam
;
1345 else if (RTFCheckMM( info
, rtfPictAttr
, rtfPicGoalHt
))
1346 sz
.cy
= info
->rtfParam
;
1348 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1355 case gfx_enhmetafile
:
1356 if ((hemf
= SetEnhMetaFileBits( size
, buffer
)))
1357 ME_RTFInsertOleObject( info
, hemf
, NULL
, &sz
);
1360 if ((hemf
= SetWinMetaFileBits( size
, buffer
, NULL
, &mfp
)))
1361 ME_RTFInsertOleObject( info
, hemf
, NULL
, &sz
);
1365 BITMAPINFO
*bi
= (BITMAPINFO
*)buffer
;
1367 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
1369 /* not quite right, especially for bitfields type of compression */
1370 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1371 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1372 if ((hbmp
= CreateDIBitmap( hdc
, &bi
->bmiHeader
,
1373 CBM_INIT
, (char*)(bi
+ 1) + nc
* sizeof(RGBQUAD
),
1374 bi
, DIB_RGB_COLORS
)) )
1375 ME_RTFInsertOleObject( info
, NULL
, hbmp
, &sz
);
1376 ReleaseDC( 0, hdc
);
1383 HeapFree( GetProcessHeap(), 0, buffer
);
1384 RTFRouteToken( info
); /* feed "}" back to router */
1388 /* for now, lookup the \result part and use it, whatever the object */
1389 static void ME_RTFReadObjectGroup(RTF_Info
*info
)
1394 if (info
->rtfClass
== rtfEOF
)
1396 if (RTFCheckCM(info
, rtfGroup
, rtfEndGroup
))
1398 if (RTFCheckCM(info
, rtfGroup
, rtfBeginGroup
))
1401 if (info
->rtfClass
== rtfEOF
)
1403 if (RTFCheckCMM(info
, rtfControl
, rtfDestination
, rtfObjResult
))
1407 while (RTFGetToken (info
) != rtfEOF
)
1409 if (info
->rtfClass
== rtfGroup
)
1411 if (info
->rtfMajor
== rtfBeginGroup
) level
++;
1412 else if (info
->rtfMajor
== rtfEndGroup
&& --level
< 0) break;
1414 RTFRouteToken(info
);
1417 else RTFSkipGroup(info
);
1420 if (!RTFCheckCM (info
, rtfControl
, rtfObjAttr
))
1422 FIXME("Non supported attribute: %d %d %d\n", info
->rtfClass
, info
->rtfMajor
, info
->rtfMinor
);
1426 RTFRouteToken(info
); /* feed "}" back to router */
1429 static void ME_RTFReadHook(RTF_Info
*info
)
1431 switch(info
->rtfClass
)
1434 switch(info
->rtfMajor
)
1437 if (info
->stackTop
< maxStack
) {
1438 info
->stack
[info
->stackTop
].style
= info
->style
;
1439 ME_AddRefStyle(info
->style
);
1440 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
1441 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
1444 info
->styleChanged
= FALSE
;
1448 RTFFlushOutputBuffer(info
);
1450 if (info
->stackTop
<= 0)
1451 info
->rtfClass
= rtfEOF
;
1452 if (info
->stackTop
< 0)
1455 ME_ReleaseStyle(info
->style
);
1456 info
->style
= info
->stack
[info
->stackTop
].style
;
1457 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
1458 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
1467 ME_StreamInFill(ME_InStream
*stream
)
1469 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
1470 (BYTE
*)stream
->buffer
,
1471 sizeof(stream
->buffer
),
1472 (LONG
*)&stream
->dwSize
);
1476 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
, BOOL stripLastCR
)
1480 int from
, to
, nUndoMode
;
1481 int nEventMask
= editor
->nEventMask
;
1482 ME_InStream inStream
;
1483 BOOL invalidRTF
= FALSE
;
1484 ME_Cursor
*selStart
, *selEnd
;
1485 LRESULT num_read
= 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1487 TRACE("stream==%p editor==%p format==0x%X\n", stream
, editor
, format
);
1488 editor
->nEventMask
= 0;
1490 ME_GetSelectionOfs(editor
, &from
, &to
);
1491 if (format
& SFF_SELECTION
&& editor
->mode
& TM_RICHTEXT
)
1493 ME_GetSelection(editor
, &selStart
, &selEnd
);
1494 style
= ME_GetSelectionInsertStyle(editor
);
1496 ME_InternalDeleteText(editor
, selStart
, to
- from
, FALSE
);
1498 /* Don't insert text at the end of the table row */
1499 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1500 ME_DisplayItem
*para
= editor
->pCursors
->pPara
;
1501 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
1503 para
= para
->member
.para
.next_para
;
1504 editor
->pCursors
[0].pPara
= para
;
1505 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1506 editor
->pCursors
[0].nOffset
= 0;
1508 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
1510 para
= para
->member
.para
.next_para
;
1511 editor
->pCursors
[0].pPara
= para
;
1512 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
1513 editor
->pCursors
[0].nOffset
= 0;
1515 editor
->pCursors
[1] = editor
->pCursors
[0];
1516 } else { /* v1.0 - 3.0 */
1517 if (editor
->pCursors
[0].pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
1518 ME_IsInTable(editor
->pCursors
[0].pRun
))
1522 style
= editor
->pBuffer
->pDefaultStyle
;
1523 ME_AddRefStyle(style
);
1524 ME_SetSelection(editor
, 0, 0);
1525 ME_InternalDeleteText(editor
, &editor
->pCursors
[1],
1526 ME_GetTextLength(editor
), FALSE
);
1528 ME_ClearTempStyle(editor
);
1529 ME_SetDefaultParaFormat(editor
, editor
->pCursors
[0].pPara
->member
.para
.pFmt
);
1533 /* Back up undo mode to a local variable */
1534 nUndoMode
= editor
->nUndoMode
;
1536 /* Only create an undo if SFF_SELECTION is set */
1537 if (!(format
& SFF_SELECTION
))
1538 editor
->nUndoMode
= umIgnore
;
1540 inStream
.editstream
= stream
;
1541 inStream
.editstream
->dwError
= 0;
1542 inStream
.dwSize
= 0;
1543 inStream
.dwUsed
= 0;
1545 if (format
& SF_RTF
)
1547 /* Check if it's really RTF, and if it is not, use plain text */
1548 ME_StreamInFill(&inStream
);
1549 if (!inStream
.editstream
->dwError
)
1551 if ((!editor
->bEmulateVersion10
&& strncmp(inStream
.buffer
, "{\\rtf", 5) && strncmp(inStream
.buffer
, "{\\urtf", 6))
1552 || (editor
->bEmulateVersion10
&& *inStream
.buffer
!= '{'))
1555 inStream
.editstream
->dwError
= -16;
1560 if (!invalidRTF
&& !inStream
.editstream
->dwError
)
1562 if (format
& SF_RTF
) {
1563 from
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1565 /* setup the RTF parser */
1566 memset(&parser
, 0, sizeof parser
);
1567 RTFSetEditStream(&parser
, &inStream
);
1568 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
1569 parser
.editor
= editor
;
1570 parser
.style
= style
;
1571 WriterInit(&parser
);
1573 RTFSetReadHook(&parser
, ME_RTFReadHook
);
1574 RTFSetDestinationCallback(&parser
, rtfShpPict
, ME_RTFReadShpPictGroup
);
1575 RTFSetDestinationCallback(&parser
, rtfPict
, ME_RTFReadPictGroup
);
1576 RTFSetDestinationCallback(&parser
, rtfObject
, ME_RTFReadObjectGroup
);
1577 if (!parser
.editor
->bEmulateVersion10
) /* v4.1 */
1579 RTFSetDestinationCallback(&parser
, rtfNoNestTables
, RTFSkipGroup
);
1580 RTFSetDestinationCallback(&parser
, rtfNestTableProps
, RTFReadGroup
);
1584 /* do the parsing */
1586 RTFFlushOutputBuffer(&parser
);
1587 if (!editor
->bEmulateVersion10
) { /* v4.1 */
1588 if (parser
.tableDef
&& parser
.tableDef
->tableRowStart
&&
1589 (parser
.nestingLevel
> 0 || parser
.canInheritInTbl
))
1591 /* Delete any incomplete table row at the end of the rich text. */
1593 ME_DisplayItem
*para
;
1595 parser
.rtfMinor
= rtfRow
;
1596 /* Complete the table row before deleting it.
1597 * By doing it this way we will have the current paragraph format set
1598 * properly to reflect that is not in the complete table, and undo items
1599 * will be added for this change to the current paragraph format. */
1600 if (parser
.nestingLevel
> 0)
1602 while (parser
.nestingLevel
> 1)
1603 ME_RTFSpecialCharHook(&parser
); /* Decrements nestingLevel */
1604 para
= parser
.tableDef
->tableRowStart
;
1605 ME_RTFSpecialCharHook(&parser
);
1607 para
= parser
.tableDef
->tableRowStart
;
1608 ME_RTFSpecialCharHook(&parser
);
1609 assert(para
->member
.para
.nFlags
& MEPF_ROWEND
);
1610 para
= para
->member
.para
.next_para
;
1613 editor
->pCursors
[1].pPara
= para
;
1614 editor
->pCursors
[1].pRun
= ME_FindItemFwd(para
, diRun
);
1615 editor
->pCursors
[1].nOffset
= 0;
1616 nOfs
= ME_GetCursorOfs(&editor
->pCursors
[1]);
1617 nChars
= ME_GetCursorOfs(&editor
->pCursors
[0]) - nOfs
;
1618 ME_InternalDeleteText(editor
, &editor
->pCursors
[1], nChars
, TRUE
);
1619 if (parser
.tableDef
)
1620 parser
.tableDef
->tableRowStart
= NULL
;
1623 ME_CheckTablesForCorruption(editor
);
1624 RTFDestroy(&parser
);
1625 if (parser
.lpRichEditOle
)
1626 IRichEditOle_Release(parser
.lpRichEditOle
);
1628 if (parser
.stackTop
> 0)
1630 while (--parser
.stackTop
>= 0)
1632 ME_ReleaseStyle(parser
.style
);
1633 parser
.style
= parser
.stack
[parser
.stackTop
].style
;
1635 if (!inStream
.editstream
->dwError
)
1636 inStream
.editstream
->dwError
= HRESULT_FROM_WIN32(ERROR_HANDLE_EOF
);
1639 /* Remove last line break, as mandated by tests. This is not affected by
1640 CR/LF counters, since RTF streaming presents only \para tokens, which
1641 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1643 if (stripLastCR
&& !(format
& SFF_SELECTION
)) {
1645 ME_GetSelection(editor
, &selStart
, &selEnd
);
1646 newto
= ME_GetCursorOfs(selEnd
);
1647 if (newto
> to
+ (editor
->bEmulateVersion10
? 1 : 0)) {
1648 WCHAR lastchar
[3] = {'\0', '\0'};
1649 int linebreakSize
= editor
->bEmulateVersion10
? 2 : 1;
1650 ME_Cursor linebreakCursor
= *selEnd
;
1652 ME_MoveCursorChars(editor
, &linebreakCursor
, -linebreakSize
);
1653 ME_GetTextW(editor
, lastchar
, 2, &linebreakCursor
, linebreakSize
, FALSE
, FALSE
);
1654 if (lastchar
[0] == '\r' && (lastchar
[1] == '\n' || lastchar
[1] == '\0')) {
1655 ME_InternalDeleteText(editor
, &linebreakCursor
, linebreakSize
, FALSE
);
1659 to
= ME_GetCursorOfs(&editor
->pCursors
[0]);
1660 num_read
= to
- from
;
1662 style
= parser
.style
;
1664 else if (format
& SF_TEXT
)
1665 num_read
= ME_StreamInText(editor
, format
, &inStream
, style
);
1667 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1668 /* put the cursor at the top */
1669 if (!(format
& SFF_SELECTION
))
1670 ME_SetSelection(editor
, 0, 0);
1673 /* Restore saved undo mode */
1674 editor
->nUndoMode
= nUndoMode
;
1676 /* even if we didn't add an undo, we need to commit anything on the stack */
1677 ME_CommitUndo(editor
);
1679 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1680 if (!(format
& SFF_SELECTION
))
1681 ME_EmptyUndoStack(editor
);
1683 ME_ReleaseStyle(style
);
1684 editor
->nEventMask
= nEventMask
;
1685 ME_UpdateRepaint(editor
, FALSE
);
1686 if (!(format
& SFF_SELECTION
)) {
1687 ME_ClearTempStyle(editor
);
1689 ITextHost_TxShowCaret(editor
->texthost
, FALSE
);
1690 ME_MoveCaret(editor
);
1691 ITextHost_TxShowCaret(editor
->texthost
, TRUE
);
1692 ME_SendSelChange(editor
);
1693 ME_SendRequestResize(editor
, FALSE
);
1699 typedef struct tagME_RTFStringStreamStruct
1704 } ME_RTFStringStreamStruct
;
1706 static DWORD CALLBACK
ME_ReadFromRTFString(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
1708 ME_RTFStringStreamStruct
*pStruct
= (ME_RTFStringStreamStruct
*)dwCookie
;
1711 count
= min(cb
, pStruct
->length
- pStruct
->pos
);
1712 memmove(lpBuff
, pStruct
->string
+ pStruct
->pos
, count
);
1713 pStruct
->pos
+= count
;
1719 ME_StreamInRTFString(ME_TextEditor
*editor
, BOOL selection
, char *string
)
1722 ME_RTFStringStreamStruct data
;
1724 data
.string
= string
;
1725 data
.length
= strlen(string
);
1727 es
.dwCookie
= (DWORD_PTR
)&data
;
1728 es
.pfnCallback
= ME_ReadFromRTFString
;
1729 ME_StreamIn(editor
, SF_RTF
| (selection
? SFF_SELECTION
: 0), &es
, TRUE
);
1734 ME_FindText(ME_TextEditor
*editor
, DWORD flags
, const CHARRANGE
*chrg
, const WCHAR
*text
, CHARRANGE
*chrgText
)
1736 const int nLen
= lstrlenW(text
);
1737 const int nTextLen
= ME_GetTextLength(editor
);
1740 WCHAR wLastChar
= ' ';
1742 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1743 flags
, chrg
->cpMin
, chrg
->cpMax
, debugstr_w(text
));
1745 if (flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
))
1746 FIXME("Flags 0x%08x not implemented\n",
1747 flags
& ~(FR_DOWN
| FR_MATCHCASE
| FR_WHOLEWORD
));
1750 if (chrg
->cpMax
== -1)
1753 nMax
= chrg
->cpMax
> nTextLen
? nTextLen
: chrg
->cpMax
;
1755 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1756 if (editor
->bEmulateVersion10
&& nMax
== nTextLen
)
1761 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1762 if (editor
->bEmulateVersion10
&& nMax
< nMin
)
1766 chrgText
->cpMin
= -1;
1767 chrgText
->cpMax
= -1;
1772 /* when searching up, if cpMin < cpMax, then instead of searching
1773 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1774 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1775 * case, it is always bigger than cpMin.
1777 if (!editor
->bEmulateVersion10
&& !(flags
& FR_DOWN
))
1781 nMax
= nMin
> nTextLen
? nTextLen
: nMin
;
1782 if (nMin
< nSwap
|| chrg
->cpMax
== -1)
1788 if (!nLen
|| nMin
< 0 || nMax
< 0 || nMax
< nMin
)
1791 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1795 if (flags
& FR_DOWN
) /* Forward search */
1797 /* If possible, find the character before where the search starts */
1798 if ((flags
& FR_WHOLEWORD
) && nMin
)
1800 ME_CursorFromCharOfs(editor
, nMin
- 1, &cursor
);
1801 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1802 ME_MoveCursorChars(editor
, &cursor
, 1);
1804 ME_CursorFromCharOfs(editor
, nMin
, &cursor
);
1807 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) + nLen
<= nMax
)
1809 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1810 int nCurStart
= cursor
.nOffset
;
1813 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
), text
[nMatched
], (flags
& FR_MATCHCASE
)))
1815 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1819 if (nMatched
== nLen
)
1821 ME_DisplayItem
*pNextItem
= pCurItem
;
1822 int nNextStart
= nCurStart
;
1825 /* Check to see if next character is a whitespace */
1826 if (flags
& FR_WHOLEWORD
)
1828 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1830 pNextItem
= ME_FindItemFwd(pCurItem
, diRun
);
1831 nNextStart
= -nMatched
;
1835 wNextChar
= *get_text( &pNextItem
->member
.run
, nNextStart
+ nMatched
);
1839 if (isalnumW(wNextChar
))
1843 cursor
.nOffset
+= cursor
.pPara
->member
.para
.nCharOfs
+ cursor
.pRun
->member
.run
.nCharOfs
;
1846 chrgText
->cpMin
= cursor
.nOffset
;
1847 chrgText
->cpMax
= cursor
.nOffset
+ nLen
;
1849 TRACE("found at %d-%d\n", cursor
.nOffset
, cursor
.nOffset
+ nLen
);
1850 return cursor
.nOffset
;
1852 if (nCurStart
+ nMatched
== pCurItem
->member
.run
.len
)
1854 pCurItem
= ME_FindItemFwd(pCurItem
, diRun
);
1855 nCurStart
= -nMatched
;
1859 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurStart
+ nMatched
);
1864 if (cursor
.nOffset
== cursor
.pRun
->member
.run
.len
)
1866 ME_NextRun(&cursor
.pPara
, &cursor
.pRun
);
1871 else /* Backward search */
1873 /* If possible, find the character after where the search ends */
1874 if ((flags
& FR_WHOLEWORD
) && nMax
< nTextLen
- 1)
1876 ME_CursorFromCharOfs(editor
, nMax
+ 1, &cursor
);
1877 wLastChar
= *get_text( &cursor
.pRun
->member
.run
, cursor
.nOffset
);
1878 ME_MoveCursorChars(editor
, &cursor
, -1);
1880 ME_CursorFromCharOfs(editor
, nMax
, &cursor
);
1883 while (cursor
.pRun
&& ME_GetCursorOfs(&cursor
) - nLen
>= nMin
)
1885 ME_DisplayItem
*pCurItem
= cursor
.pRun
;
1886 ME_DisplayItem
*pCurPara
= cursor
.pPara
;
1887 int nCurEnd
= cursor
.nOffset
;
1892 ME_PrevRun(&pCurPara
, &pCurItem
);
1893 nCurEnd
= pCurItem
->member
.run
.len
;
1896 while (pCurItem
&& ME_CharCompare( *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 ),
1897 text
[nLen
- nMatched
- 1], (flags
& FR_MATCHCASE
) ))
1899 if ((flags
& FR_WHOLEWORD
) && isalnumW(wLastChar
))
1903 if (nMatched
== nLen
)
1905 ME_DisplayItem
*pPrevItem
= pCurItem
;
1906 int nPrevEnd
= nCurEnd
;
1910 /* Check to see if previous character is a whitespace */
1911 if (flags
& FR_WHOLEWORD
)
1913 if (nPrevEnd
- nMatched
== 0)
1915 pPrevItem
= ME_FindItemBack(pCurItem
, diRun
);
1917 nPrevEnd
= pPrevItem
->member
.run
.len
+ nMatched
;
1921 wPrevChar
= *get_text( &pPrevItem
->member
.run
, nPrevEnd
- nMatched
- 1 );
1925 if (isalnumW(wPrevChar
))
1929 nStart
= pCurPara
->member
.para
.nCharOfs
1930 + pCurItem
->member
.run
.nCharOfs
+ nCurEnd
- nMatched
;
1933 chrgText
->cpMin
= nStart
;
1934 chrgText
->cpMax
= nStart
+ nLen
;
1936 TRACE("found at %d-%d\n", nStart
, nStart
+ nLen
);
1939 if (nCurEnd
- nMatched
== 0)
1941 ME_PrevRun(&pCurPara
, &pCurItem
);
1942 /* Don't care about pCurItem becoming NULL here; it's already taken
1943 * care of in the exterior loop condition */
1944 nCurEnd
= pCurItem
->member
.run
.len
+ nMatched
;
1948 wLastChar
= *get_text( &pCurItem
->member
.run
, nCurEnd
- nMatched
- 1 );
1953 if (cursor
.nOffset
< 0)
1955 ME_PrevRun(&cursor
.pPara
, &cursor
.pRun
);
1956 cursor
.nOffset
= cursor
.pRun
->member
.run
.len
;
1960 TRACE("not found\n");
1962 chrgText
->cpMin
= chrgText
->cpMax
= -1;
1966 static int ME_GetTextEx(ME_TextEditor
*editor
, GETTEXTEX
*ex
, LPARAM pText
)
1971 if (!ex
->cb
|| !pText
) return 0;
1973 if (ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
))
1974 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex
->flags
& ~(GT_SELECTION
| GT_USECRLF
));
1976 if (ex
->flags
& GT_SELECTION
)
1979 int nStartCur
= ME_GetSelectionOfs(editor
, &from
, &to
);
1980 start
= editor
->pCursors
[nStartCur
];
1985 ME_SetCursorToStart(editor
, &start
);
1988 if (ex
->codepage
== CP_UNICODE
)
1990 return ME_GetTextW(editor
, (LPWSTR
)pText
, ex
->cb
/ sizeof(WCHAR
) - 1,
1991 &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
1995 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1996 we can just take a bigger buffer? :)
1997 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1998 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2000 int crlfmul
= (ex
->flags
& GT_USECRLF
) ? 2 : 1;
2005 buflen
= min(crlfmul
* nChars
, ex
->cb
- 1);
2006 buffer
= heap_alloc((buflen
+ 1) * sizeof(WCHAR
));
2008 nChars
= ME_GetTextW(editor
, buffer
, buflen
, &start
, nChars
, ex
->flags
& GT_USECRLF
, FALSE
);
2009 rc
= WideCharToMultiByte(ex
->codepage
, 0, buffer
, nChars
+ 1,
2010 (LPSTR
)pText
, ex
->cb
, ex
->lpDefaultChar
, ex
->lpUsedDefChar
);
2011 if (rc
) rc
--; /* do not count 0 terminator */
2018 static int ME_GetTextRange(ME_TextEditor
*editor
, WCHAR
*strText
,
2019 const ME_Cursor
*start
, int nLen
, BOOL unicode
)
2021 if (!strText
) return 0;
2023 return ME_GetTextW(editor
, strText
, INT_MAX
, start
, nLen
, FALSE
, FALSE
);
2026 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
2028 nChars
= ME_GetTextW(editor
, p
, nLen
, start
, nLen
, FALSE
, FALSE
);
2029 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)strText
,
2030 nLen
+1, NULL
, NULL
);
2036 static int handle_EM_EXSETSEL( ME_TextEditor
*editor
, int to
, int from
)
2040 TRACE("%d - %d\n", to
, from
);
2042 ME_InvalidateSelection( editor
);
2043 end
= ME_SetSelection( editor
, to
, from
);
2044 ME_InvalidateSelection( editor
);
2045 ITextHost_TxShowCaret( editor
->texthost
, FALSE
);
2046 ME_ShowCaret( editor
);
2047 ME_SendSelChange( editor
);
2052 typedef struct tagME_GlobalDestStruct
2056 } ME_GlobalDestStruct
;
2058 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2060 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2065 pDest
= (WORD
*)lpBuff
;
2066 pSrc
= GlobalLock(pData
->hData
);
2067 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2068 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2070 pData
->nLength
+= i
;
2072 GlobalUnlock(pData
->hData
);
2076 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
2078 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
2083 pSrc
= GlobalLock(pData
->hData
);
2084 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
2085 pDest
[i
] = pSrc
[pData
->nLength
+i
];
2087 pData
->nLength
+= i
;
2089 GlobalUnlock(pData
->hData
);
2093 static BOOL
ME_Paste(ME_TextEditor
*editor
)
2097 ME_GlobalDestStruct gds
;
2098 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
2101 if (IsClipboardFormatAvailable(nRTFFormat
))
2102 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
2103 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
2104 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
2108 if (!OpenClipboard(editor
->hWnd
))
2110 gds
.hData
= GetClipboardData(cf
);
2112 es
.dwCookie
= (DWORD_PTR
)&gds
;
2113 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
2114 ME_StreamIn(editor
, dwFormat
|SFF_SELECTION
, &es
, FALSE
);
2120 static BOOL
ME_Copy(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
2122 LPDATAOBJECT dataObj
= NULL
;
2125 if (editor
->cPasswordMask
)
2126 return FALSE
; /* Copying or Cutting masked text isn't allowed */
2128 if(editor
->lpOleCallback
)
2131 range
.cpMin
= ME_GetCursorOfs(start
);
2132 range
.cpMax
= range
.cpMin
+ nChars
;
2133 hr
= IRichEditOleCallback_GetClipboardData(editor
->lpOleCallback
, &range
, RECO_COPY
, &dataObj
);
2135 if(FAILED(hr
) || !dataObj
)
2136 hr
= ME_GetDataObject(editor
, start
, nChars
, &dataObj
);
2138 hr
= OleSetClipboard(dataObj
);
2139 IDataObject_Release(dataObj
);
2141 return SUCCEEDED(hr
);
2144 /* helper to send a msg filter notification */
2146 ME_FilterEvent(ME_TextEditor
*editor
, UINT msg
, WPARAM
* wParam
, LPARAM
* lParam
)
2150 if (!editor
->hWnd
|| !editor
->hwndParent
) return FALSE
;
2151 msgf
.nmhdr
.hwndFrom
= editor
->hWnd
;
2152 msgf
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
2153 msgf
.nmhdr
.code
= EN_MSGFILTER
;
2155 msgf
.wParam
= *wParam
;
2156 msgf
.lParam
= *lParam
;
2157 if (SendMessageW(editor
->hwndParent
, WM_NOTIFY
, msgf
.nmhdr
.idFrom
, (LPARAM
)&msgf
))
2159 *wParam
= msgf
.wParam
;
2160 *lParam
= msgf
.lParam
;
2161 msgf
.wParam
= *wParam
;
2166 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor
*editor
)
2168 ME_DisplayItem
*startPara
, *endPara
;
2169 ME_DisplayItem
*prev_para
;
2170 ME_Cursor
*from
, *to
;
2174 if (!editor
->AutoURLDetect_bEnable
) return;
2176 ME_GetSelection(editor
, &from
, &to
);
2178 /* Find paragraph previous to the one that contains start cursor */
2179 startPara
= from
->pPara
;
2180 prev_para
= startPara
->member
.para
.prev_para
;
2181 if (prev_para
->type
== diParagraph
) startPara
= prev_para
;
2183 /* Find paragraph that contains end cursor */
2184 endPara
= to
->pPara
->member
.para
.next_para
;
2186 start
.pPara
= startPara
;
2187 start
.pRun
= ME_FindItemFwd(startPara
, diRun
);
2189 nChars
= endPara
->member
.para
.nCharOfs
- startPara
->member
.para
.nCharOfs
;
2191 ME_UpdateLinkAttribute(editor
, &start
, nChars
);
2195 ME_KeyDown(ME_TextEditor
*editor
, WORD nKey
)
2197 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2198 BOOL shift_is_down
= GetKeyState(VK_SHIFT
) & 0x8000;
2200 if (editor
->bMouseCaptured
)
2202 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
!= VK_MENU
)
2203 editor
->nSelectionType
= stPosition
;
2211 editor
->nUDArrowX
= -1;
2217 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
2218 ME_ArrowKey(editor
, nKey
, shift_is_down
, ctrl_is_down
);
2222 editor
->nUDArrowX
= -1;
2223 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2224 if (editor
->styleFlags
& ES_READONLY
)
2226 if (ME_IsSelection(editor
))
2228 ME_DeleteSelection(editor
);
2229 ME_CommitUndo(editor
);
2231 else if (nKey
== VK_DELETE
)
2233 /* Delete stops group typing.
2234 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2235 ME_DeleteTextAtCursor(editor
, 1, 1);
2236 ME_CommitUndo(editor
);
2238 else if (ME_ArrowKey(editor
, VK_LEFT
, FALSE
, FALSE
))
2240 BOOL bDeletionSucceeded
;
2241 /* Backspace can be grouped for a single undo */
2242 ME_ContinueCoalescingTransaction(editor
);
2243 bDeletionSucceeded
= ME_DeleteTextAtCursor(editor
, 1, 1);
2244 if (!bDeletionSucceeded
&& !editor
->bEmulateVersion10
) { /* v4.1 */
2245 /* Deletion was prevented so the cursor is moved back to where it was.
2246 * (e.g. this happens when trying to delete cell boundaries)
2248 ME_ArrowKey(editor
, VK_RIGHT
, FALSE
, FALSE
);
2250 ME_CommitCoalescingUndo(editor
);
2254 ME_MoveCursorFromTableRowStartParagraph(editor
);
2255 ME_UpdateSelectionLinkAttribute(editor
);
2256 ME_UpdateRepaint(editor
, FALSE
);
2257 ME_SendRequestResize(editor
, FALSE
);
2260 if (editor
->bDialogMode
)
2265 if (!(editor
->styleFlags
& ES_WANTRETURN
))
2267 if (editor
->hwndParent
)
2270 dw
= SendMessageW(editor
->hwndParent
, DM_GETDEFID
, 0, 0);
2271 if (HIWORD(dw
) == DC_HASDEFID
)
2273 HWND hwDefCtrl
= GetDlgItem(editor
->hwndParent
, LOWORD(dw
));
2276 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, (WPARAM
)hwDefCtrl
, TRUE
);
2277 PostMessageW(hwDefCtrl
, WM_KEYDOWN
, VK_RETURN
, 0);
2285 if (editor
->styleFlags
& ES_MULTILINE
)
2287 ME_Cursor cursor
= editor
->pCursors
[0];
2288 ME_DisplayItem
*para
= cursor
.pPara
;
2290 const WCHAR endl
= '\r';
2291 const WCHAR endlv10
[] = {'\r','\n'};
2294 if (editor
->styleFlags
& ES_READONLY
) {
2295 MessageBeep(MB_ICONERROR
);
2299 ME_GetSelectionOfs(editor
, &from
, &to
);
2300 if (editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2302 if (!editor
->bEmulateVersion10
) { /* v4.1 */
2303 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2304 /* Add a new table row after this row. */
2305 para
= ME_AppendTableRow(editor
, para
);
2306 para
= para
->member
.para
.next_para
;
2307 editor
->pCursors
[0].pPara
= para
;
2308 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2309 editor
->pCursors
[0].nOffset
= 0;
2310 editor
->pCursors
[1] = editor
->pCursors
[0];
2311 ME_CommitUndo(editor
);
2312 ME_CheckTablesForCorruption(editor
);
2313 ME_UpdateRepaint(editor
, FALSE
);
2316 else if (para
== editor
->pCursors
[1].pPara
&&
2317 cursor
.nOffset
+ cursor
.pRun
->member
.run
.nCharOfs
== 0 &&
2318 para
->member
.para
.prev_para
->member
.para
.nFlags
& MEPF_ROWSTART
&&
2319 !para
->member
.para
.prev_para
->member
.para
.nCharOfs
)
2321 /* Insert a newline before the table. */
2322 para
= para
->member
.para
.prev_para
;
2323 para
->member
.para
.nFlags
&= ~MEPF_ROWSTART
;
2324 editor
->pCursors
[0].pPara
= para
;
2325 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2326 editor
->pCursors
[1] = editor
->pCursors
[0];
2327 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2328 editor
->pCursors
[0].pRun
->member
.run
.style
);
2329 para
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
2330 ME_SetDefaultParaFormat(editor
, para
->member
.para
.pFmt
);
2331 para
->member
.para
.nFlags
= MEPF_REWRAP
;
2332 editor
->pCursors
[0].pPara
= para
;
2333 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2334 editor
->pCursors
[1] = editor
->pCursors
[0];
2335 para
->member
.para
.next_para
->member
.para
.nFlags
|= MEPF_ROWSTART
;
2336 ME_CommitCoalescingUndo(editor
);
2337 ME_CheckTablesForCorruption(editor
);
2338 ME_UpdateRepaint(editor
, FALSE
);
2341 } else { /* v1.0 - 3.0 */
2342 ME_DisplayItem
*para
= cursor
.pPara
;
2343 if (ME_IsInTable(para
))
2345 if (cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
)
2348 ME_ContinueCoalescingTransaction(editor
);
2349 para
= ME_AppendTableRow(editor
, para
);
2350 editor
->pCursors
[0].pPara
= para
;
2351 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2352 editor
->pCursors
[0].nOffset
= 0;
2353 editor
->pCursors
[1] = editor
->pCursors
[0];
2354 ME_CommitCoalescingUndo(editor
);
2355 ME_UpdateRepaint(editor
, FALSE
);
2359 ME_ContinueCoalescingTransaction(editor
);
2360 if (cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2361 !ME_IsInTable(para
->member
.para
.prev_para
))
2363 /* Insert newline before table */
2364 cursor
.pRun
= ME_FindItemBack(para
, diRun
);
2366 editor
->pCursors
[0].pRun
= cursor
.pRun
;
2367 editor
->pCursors
[0].pPara
= para
->member
.para
.prev_para
;
2369 editor
->pCursors
[0].nOffset
= 0;
2370 editor
->pCursors
[1] = editor
->pCursors
[0];
2371 ME_InsertTextFromCursor(editor
, 0, &endl
, 1,
2372 editor
->pCursors
[0].pRun
->member
.run
.style
);
2374 editor
->pCursors
[1] = editor
->pCursors
[0];
2375 para
= ME_AppendTableRow(editor
, para
);
2376 editor
->pCursors
[0].pPara
= para
;
2377 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2378 editor
->pCursors
[0].nOffset
= 0;
2379 editor
->pCursors
[1] = editor
->pCursors
[0];
2381 ME_CommitCoalescingUndo(editor
);
2382 ME_UpdateRepaint(editor
, FALSE
);
2388 style
= ME_GetInsertStyle(editor
, 0);
2389 ME_SaveTempStyle(editor
);
2390 ME_ContinueCoalescingTransaction(editor
);
2392 ME_InsertEndRowFromCursor(editor
, 0);
2394 if (!editor
->bEmulateVersion10
)
2395 ME_InsertTextFromCursor(editor
, 0, &endl
, 1, style
);
2397 ME_InsertTextFromCursor(editor
, 0, endlv10
, 2, style
);
2398 ME_ReleaseStyle(style
);
2399 ME_CommitCoalescingUndo(editor
);
2402 ME_UpdateSelectionLinkAttribute(editor
);
2403 ME_UpdateRepaint(editor
, FALSE
);
2409 if (editor
->bDialogMode
&& editor
->hwndParent
)
2410 PostMessageW(editor
->hwndParent
, WM_CLOSE
, 0, 0);
2413 if (editor
->bDialogMode
&& editor
->hwndParent
)
2414 SendMessageW(editor
->hwndParent
, WM_NEXTDLGCTL
, shift_is_down
, 0);
2419 handle_EM_EXSETSEL( editor
, 0, -1 );
2425 return ME_Paste(editor
);
2433 int nStartCur
= ME_GetSelectionOfs(editor
, &nOfs
, &nChars
);
2434 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
2437 result
= ME_Copy(editor
, selStart
, nChars
);
2438 if (result
&& nKey
== 'X')
2440 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
2441 ME_CommitUndo(editor
);
2442 ME_UpdateRepaint(editor
, TRUE
);
2463 if (nKey
!= VK_SHIFT
&& nKey
!= VK_CONTROL
&& nKey
&& nKey
!= VK_MENU
)
2464 editor
->nUDArrowX
= -1;
2471 chf
.cbSize
= sizeof(chf
);
2473 ME_GetSelectionCharFormat(editor
, &chf
);
2474 ME_DumpStyleToBuf(&chf
, buf
);
2475 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
2479 ME_CheckCharOffsets(editor
);
2486 static LRESULT
ME_Char(ME_TextEditor
*editor
, WPARAM charCode
,
2487 LPARAM flags
, BOOL unicode
)
2491 if (editor
->bMouseCaptured
)
2495 wstr
= (WCHAR
)charCode
;
2498 CHAR charA
= charCode
;
2499 MultiByteToWideChar(CP_ACP
, 0, &charA
, 1, &wstr
, 1);
2502 if (editor
->styleFlags
& ES_READONLY
) {
2503 MessageBeep(MB_ICONERROR
);
2504 return 0; /* FIXME really 0 ? */
2507 if ((unsigned)wstr
>= ' ' || wstr
== '\t')
2509 ME_Cursor cursor
= editor
->pCursors
[0];
2510 ME_DisplayItem
*para
= cursor
.pPara
;
2512 BOOL ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
2513 ME_GetSelectionOfs(editor
, &from
, &to
);
2515 /* v4.1 allows tabs to be inserted with ctrl key down */
2516 !(ctrl_is_down
&& !editor
->bEmulateVersion10
))
2518 ME_DisplayItem
*para
;
2519 BOOL bSelectedRow
= FALSE
;
2521 para
= cursor
.pPara
;
2522 if (ME_IsSelection(editor
) &&
2523 cursor
.pRun
->member
.run
.nCharOfs
+ cursor
.nOffset
== 0 &&
2524 to
== ME_GetCursorOfs(&editor
->pCursors
[0]) &&
2525 para
->member
.para
.prev_para
->type
== diParagraph
)
2527 para
= para
->member
.para
.prev_para
;
2528 bSelectedRow
= TRUE
;
2530 if (ME_IsInTable(para
))
2532 ME_TabPressedInTable(editor
, bSelectedRow
);
2533 ME_CommitUndo(editor
);
2536 } else if (!editor
->bEmulateVersion10
) { /* v4.1 */
2537 if (para
->member
.para
.nFlags
& MEPF_ROWEND
) {
2539 para
= para
->member
.para
.next_para
;
2540 if (para
->member
.para
.nFlags
& MEPF_ROWSTART
)
2541 para
= para
->member
.para
.next_para
;
2542 editor
->pCursors
[0].pPara
= para
;
2543 editor
->pCursors
[0].pRun
= ME_FindItemFwd(para
, diRun
);
2544 editor
->pCursors
[0].nOffset
= 0;
2545 editor
->pCursors
[1] = editor
->pCursors
[0];
2548 } else { /* v1.0 - 3.0 */
2549 if (ME_IsInTable(cursor
.pRun
) &&
2550 cursor
.pRun
->member
.run
.nFlags
& MERF_ENDPARA
&&
2553 /* Text should not be inserted at the end of the table. */
2558 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2559 /* WM_CHAR is restricted to nTextLimit */
2560 if(editor
->nTextLimit
> ME_GetTextLength(editor
) - (to
-from
))
2562 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
2563 ME_SaveTempStyle(editor
);
2564 ME_ContinueCoalescingTransaction(editor
);
2565 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
2566 ME_ReleaseStyle(style
);
2567 ME_CommitCoalescingUndo(editor
);
2568 ITextHost_TxSetCursor(editor
->texthost
, NULL
, FALSE
);
2571 ME_UpdateSelectionLinkAttribute(editor
);
2572 ME_UpdateRepaint(editor
, FALSE
);
2577 /* Process the message and calculate the new click count.
2579 * returns: The click count if it is mouse down event, else returns 0. */
2580 static int ME_CalculateClickCount(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
2583 static int clickNum
= 0;
2584 if (msg
< WM_MOUSEFIRST
|| msg
> WM_MOUSELAST
)
2587 if ((msg
== WM_LBUTTONDBLCLK
) ||
2588 (msg
== WM_RBUTTONDBLCLK
) ||
2589 (msg
== WM_MBUTTONDBLCLK
) ||
2590 (msg
== WM_XBUTTONDBLCLK
))
2592 msg
-= (WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
);
2595 if ((msg
== WM_LBUTTONDOWN
) ||
2596 (msg
== WM_RBUTTONDOWN
) ||
2597 (msg
== WM_MBUTTONDOWN
) ||
2598 (msg
== WM_XBUTTONDOWN
))
2600 static MSG prevClickMsg
;
2602 /* Compare the editor instead of the hwnd so that the this
2603 * can still be done for windowless richedit controls. */
2604 clickMsg
.hwnd
= (HWND
)editor
;
2605 clickMsg
.message
= msg
;
2606 clickMsg
.wParam
= wParam
;
2607 clickMsg
.lParam
= lParam
;
2608 clickMsg
.time
= GetMessageTime();
2609 clickMsg
.pt
.x
= (short)LOWORD(lParam
);
2610 clickMsg
.pt
.y
= (short)HIWORD(lParam
);
2611 if ((clickNum
!= 0) &&
2612 (clickMsg
.message
== prevClickMsg
.message
) &&
2613 (clickMsg
.hwnd
== prevClickMsg
.hwnd
) &&
2614 (clickMsg
.wParam
== prevClickMsg
.wParam
) &&
2615 (clickMsg
.time
- prevClickMsg
.time
< GetDoubleClickTime()) &&
2616 (abs(clickMsg
.pt
.x
- prevClickMsg
.pt
.x
) < GetSystemMetrics(SM_CXDOUBLECLK
)/2) &&
2617 (abs(clickMsg
.pt
.y
- prevClickMsg
.pt
.y
) < GetSystemMetrics(SM_CYDOUBLECLK
)/2))
2623 prevClickMsg
= clickMsg
;
2630 static BOOL
ME_SetCursor(ME_TextEditor
*editor
)
2636 DWORD messagePos
= GetMessagePos();
2637 pt
.x
= (short)LOWORD(messagePos
);
2638 pt
.y
= (short)HIWORD(messagePos
);
2642 sbi
.cbSize
= sizeof(sbi
);
2643 GetScrollBarInfo(editor
->hWnd
, OBJID_HSCROLL
, &sbi
);
2644 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2645 PtInRect(&sbi
.rcScrollBar
, pt
))
2647 ITextHost_TxSetCursor(editor
->texthost
,
2648 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2651 sbi
.cbSize
= sizeof(sbi
);
2652 GetScrollBarInfo(editor
->hWnd
, OBJID_VSCROLL
, &sbi
);
2653 if (!(sbi
.rgstate
[0] & (STATE_SYSTEM_INVISIBLE
|STATE_SYSTEM_OFFSCREEN
)) &&
2654 PtInRect(&sbi
.rcScrollBar
, pt
))
2656 ITextHost_TxSetCursor(editor
->texthost
,
2657 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2661 ITextHost_TxScreenToClient(editor
->texthost
, &pt
);
2663 if (editor
->nSelectionType
== stLine
&& editor
->bMouseCaptured
) {
2664 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2667 if (!editor
->bEmulateVersion10
/* v4.1 */ &&
2668 pt
.y
< editor
->rcFormat
.top
&&
2669 pt
.x
< editor
->rcFormat
.left
)
2671 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2674 if (pt
.y
< editor
->rcFormat
.top
|| pt
.y
> editor
->rcFormat
.bottom
)
2676 if (editor
->bEmulateVersion10
) /* v1.0 - 3.0 */
2677 ITextHost_TxSetCursor(editor
->texthost
,
2678 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
), FALSE
);
2680 ITextHost_TxSetCursor(editor
->texthost
,
2681 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2684 if (pt
.x
< editor
->rcFormat
.left
)
2686 ITextHost_TxSetCursor(editor
->texthost
, hLeft
, FALSE
);
2689 ME_CharFromPos(editor
, pt
.x
, pt
.y
, &cursor
, &isExact
);
2694 run
= &cursor
.pRun
->member
.run
;
2695 if (run
->style
->fmt
.dwMask
& CFM_LINK
&&
2696 run
->style
->fmt
.dwEffects
& CFE_LINK
)
2698 ITextHost_TxSetCursor(editor
->texthost
,
2699 LoadCursorW(NULL
, (WCHAR
*)IDC_HAND
),
2704 if (ME_IsSelection(editor
))
2706 int selStart
, selEnd
;
2707 int offset
= ME_GetCursorOfs(&cursor
);
2709 ME_GetSelectionOfs(editor
, &selStart
, &selEnd
);
2710 if (selStart
<= offset
&& selEnd
>= offset
) {
2711 ITextHost_TxSetCursor(editor
->texthost
,
2712 LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
),
2718 ITextHost_TxSetCursor(editor
->texthost
,
2719 LoadCursorW(NULL
, (WCHAR
*)IDC_IBEAM
), TRUE
);
2723 static void ME_SetDefaultFormatRect(ME_TextEditor
*editor
)
2725 ITextHost_TxGetClientRect(editor
->texthost
, &editor
->rcFormat
);
2726 editor
->rcFormat
.top
+= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
2727 editor
->rcFormat
.left
+= 1 + editor
->selofs
;
2728 editor
->rcFormat
.right
-= 1;
2731 static BOOL
ME_ShowContextMenu(ME_TextEditor
*editor
, int x
, int y
)
2736 if(!editor
->lpOleCallback
|| !editor
->hWnd
)
2738 ME_GetSelectionOfs(editor
, &selrange
.cpMin
, &selrange
.cpMax
);
2739 if(selrange
.cpMin
== selrange
.cpMax
)
2740 seltype
|= SEL_EMPTY
;
2743 /* FIXME: Handle objects */
2744 seltype
|= SEL_TEXT
;
2745 if(selrange
.cpMax
-selrange
.cpMin
> 1)
2746 seltype
|= SEL_MULTICHAR
;
2748 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor
->lpOleCallback
, seltype
, NULL
, &selrange
, &menu
)))
2750 TrackPopupMenu(menu
, TPM_LEFTALIGN
| TPM_RIGHTBUTTON
, x
, y
, 0, editor
->hwndParent
, NULL
);
2756 ME_TextEditor
*ME_MakeEditor(ITextHost
*texthost
, BOOL bEmulateVersion10
, DWORD csStyle
)
2758 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
2764 ed
->hwndParent
= NULL
;
2765 ed
->sizeWindow
.cx
= ed
->sizeWindow
.cy
= 0;
2766 ed
->texthost
= texthost
;
2768 ed
->bEmulateVersion10
= bEmulateVersion10
;
2770 ed
->alignStyle
= PFA_LEFT
;
2771 if (csStyle
& ES_RIGHT
)
2772 ed
->alignStyle
= PFA_RIGHT
;
2773 if (csStyle
& ES_CENTER
)
2774 ed
->alignStyle
= PFA_CENTER
;
2775 ITextHost_TxGetPropertyBits(texthost
,
2776 (TXTBIT_RICHTEXT
|TXTBIT_MULTILINE
|
2777 TXTBIT_READONLY
|TXTBIT_USEPASSWORD
|
2778 TXTBIT_HIDESELECTION
|TXTBIT_SAVESELECTION
|
2779 TXTBIT_AUTOWORDSEL
|TXTBIT_VERTICAL
|
2780 TXTBIT_WORDWRAP
|TXTBIT_DISABLEDRAG
),
2782 ITextHost_TxGetScrollBars(texthost
, &ed
->styleFlags
);
2783 ed
->styleFlags
&= (WS_VSCROLL
|WS_HSCROLL
|ES_AUTOVSCROLL
|
2784 ES_AUTOHSCROLL
|ES_DISABLENOSCROLL
);
2785 ed
->pBuffer
= ME_MakeText();
2786 ed
->nZoomNumerator
= ed
->nZoomDenominator
= 0;
2787 ed
->nAvailWidth
= 0; /* wrap to client area */
2788 ME_MakeFirstParagraph(ed
);
2789 /* The four cursors are for:
2790 * 0 - The position where the caret is shown
2791 * 1 - The anchored end of the selection (for normal selection)
2792 * 2 & 3 - The anchored start and end respectively for word, line,
2793 * or paragraph selection.
2796 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
2797 ME_SetCursorToStart(ed
, &ed
->pCursors
[0]);
2798 ed
->pCursors
[1] = ed
->pCursors
[0];
2799 ed
->pCursors
[2] = ed
->pCursors
[0];
2800 ed
->pCursors
[3] = ed
->pCursors
[1];
2801 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
2802 ed
->nLastTotalWidth
= ed
->nTotalWidth
= 0;
2804 ed
->rgbBackColor
= -1;
2805 ed
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2806 ed
->bCaretAtEnd
= FALSE
;
2808 ed
->nModifyStep
= 0;
2809 ed
->nTextLimit
= TEXT_LIMIT_DEFAULT
;
2810 list_init( &ed
->undo_stack
);
2811 list_init( &ed
->redo_stack
);
2812 ed
->nUndoStackSize
= 0;
2813 ed
->nUndoLimit
= STACK_SIZE_DEFAULT
;
2814 ed
->nUndoMode
= umAddToUndo
;
2815 ed
->nParagraphs
= 1;
2816 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
2817 ed
->pLastSelStartPara
= ed
->pLastSelEndPara
= ed
->pCursors
[0].pPara
;
2818 ed
->bHideSelection
= FALSE
;
2819 ed
->pfnWordBreak
= NULL
;
2820 ed
->lpOleCallback
= NULL
;
2821 ed
->mode
= TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
;
2822 ed
->mode
|= (props
& TXTBIT_RICHTEXT
) ? TM_RICHTEXT
: TM_PLAINTEXT
;
2823 ed
->AutoURLDetect_bEnable
= FALSE
;
2824 ed
->bHaveFocus
= FALSE
;
2825 ed
->bDialogMode
= FALSE
;
2826 ed
->bMouseCaptured
= FALSE
;
2827 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2829 ed
->pFontCache
[i
].nRefs
= 0;
2830 ed
->pFontCache
[i
].nAge
= 0;
2831 ed
->pFontCache
[i
].hFont
= NULL
;
2834 ME_CheckCharOffsets(ed
);
2835 ed
->bDefaultFormatRect
= TRUE
;
2836 ITextHost_TxGetSelectionBarWidth(ed
->texthost
, &selbarwidth
);
2838 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2839 ed
->selofs
= SELECTIONBAR_WIDTH
;
2840 ed
->styleFlags
|= ES_SELECTIONBAR
;
2844 ed
->nSelectionType
= stPosition
;
2846 ed
->cPasswordMask
= 0;
2847 if (props
& TXTBIT_USEPASSWORD
)
2848 ITextHost_TxGetPasswordChar(texthost
, &ed
->cPasswordMask
);
2850 if (props
& TXTBIT_AUTOWORDSEL
)
2851 ed
->styleFlags
|= ECO_AUTOWORDSELECTION
;
2852 if (props
& TXTBIT_MULTILINE
) {
2853 ed
->styleFlags
|= ES_MULTILINE
;
2854 ed
->bWordWrap
= (props
& TXTBIT_WORDWRAP
) != 0;
2856 ed
->bWordWrap
= FALSE
;
2858 if (props
& TXTBIT_READONLY
)
2859 ed
->styleFlags
|= ES_READONLY
;
2860 if (!(props
& TXTBIT_HIDESELECTION
))
2861 ed
->styleFlags
|= ES_NOHIDESEL
;
2862 if (props
& TXTBIT_SAVESELECTION
)
2863 ed
->styleFlags
|= ES_SAVESEL
;
2864 if (props
& TXTBIT_VERTICAL
)
2865 ed
->styleFlags
|= ES_VERTICAL
;
2866 if (props
& TXTBIT_DISABLEDRAG
)
2867 ed
->styleFlags
|= ES_NOOLEDRAGDROP
;
2869 ed
->notified_cr
.cpMin
= ed
->notified_cr
.cpMax
= 0;
2871 /* Default scrollbar information */
2872 ed
->vert_si
.cbSize
= sizeof(SCROLLINFO
);
2873 ed
->vert_si
.nMin
= 0;
2874 ed
->vert_si
.nMax
= 0;
2875 ed
->vert_si
.nPage
= 0;
2876 ed
->vert_si
.nPos
= 0;
2878 ed
->horz_si
.cbSize
= sizeof(SCROLLINFO
);
2879 ed
->horz_si
.nMin
= 0;
2880 ed
->horz_si
.nMax
= 0;
2881 ed
->horz_si
.nPage
= 0;
2882 ed
->horz_si
.nPos
= 0;
2884 ed
->wheel_remain
= 0;
2886 list_init( &ed
->style_list
);
2887 OleInitialize(NULL
);
2892 void ME_DestroyEditor(ME_TextEditor
*editor
)
2894 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
2895 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
2896 ME_Style
*s
, *cursor2
;
2899 ME_ClearTempStyle(editor
);
2900 ME_EmptyUndoStack(editor
);
2903 ME_DestroyDisplayItem(p
);
2907 LIST_FOR_EACH_ENTRY_SAFE( s
, cursor2
, &editor
->style_list
, ME_Style
, entry
)
2908 ME_DestroyStyle( s
);
2910 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
2911 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
2913 if (editor
->pFontCache
[i
].hFont
)
2914 DeleteObject(editor
->pFontCache
[i
].hFont
);
2916 if (editor
->rgbBackColor
!= -1)
2917 DeleteObject(editor
->hbrBackground
);
2918 if(editor
->lpOleCallback
)
2919 IRichEditOleCallback_Release(editor
->lpOleCallback
);
2920 ITextHost_Release(editor
->texthost
);
2923 IRichEditOle_Release(editor
->reOle
);
2924 editor
->reOle
= NULL
;
2928 FREE_OBJ(editor
->pBuffer
);
2929 FREE_OBJ(editor
->pCursors
);
2934 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
2939 case DLL_PROCESS_ATTACH
:
2940 DisableThreadLibraryCalls(hinstDLL
);
2941 me_heap
= HeapCreate (0, 0x10000, 0);
2942 if (!ME_RegisterEditorClass(hinstDLL
)) return FALSE
;
2943 hLeft
= LoadCursorW(hinstDLL
, MAKEINTRESOURCEW(OCR_REVERSE
));
2947 case DLL_PROCESS_DETACH
:
2948 if (lpvReserved
) break;
2949 UnregisterClassW(RICHEDIT_CLASS20W
, 0);
2950 UnregisterClassW(MSFTEDIT_CLASS
, 0);
2951 UnregisterClassA(RICHEDIT_CLASS20A
, 0);
2952 UnregisterClassA("RichEdit50A", 0);
2953 if (ME_ListBoxRegistered
)
2954 UnregisterClassW(REListBox20W
, 0);
2955 if (ME_ComboBoxRegistered
)
2956 UnregisterClassW(REComboBox20W
, 0);
2958 HeapDestroy (me_heap
);
2965 static inline int get_default_line_height( ME_TextEditor
*editor
)
2969 if (editor
->pBuffer
&& editor
->pBuffer
->pDefaultStyle
)
2970 height
= editor
->pBuffer
->pDefaultStyle
->tm
.tmHeight
;
2971 if (height
<= 0) height
= 24;
2976 static inline int calc_wheel_change( int *remain
, int amount_per_click
)
2978 int change
= amount_per_click
* (float)*remain
/ WHEEL_DELTA
;
2979 *remain
-= WHEEL_DELTA
* change
/ amount_per_click
;
2983 static const char * const edit_messages
[] = {
3012 "EM_SETPASSWORDCHAR",
3013 "EM_EMPTYUNDOBUFFER",
3014 "EM_GETFIRSTVISIBLELINE",
3016 "EM_SETWORDBREAKPROC",
3017 "EM_GETWORDBREAKPROC",
3018 "EM_GETPASSWORDCHAR",
3028 static const char * const richedit_messages
[] = {
3033 "EM_EXLINEFROMCHAR",
3039 "EM_GETOLEINTERFACE",
3049 "EM_SETOLECALLBACK",
3051 "EM_SETTARGETDEVICE",
3059 "EM_GETWORDBREAKPROCEX",
3060 "EM_SETWORDBREAKPROCEX",
3062 "EM_UNKNOWN_USER_83",
3067 "EM_STOPGROUPTYPING",
3071 "EM_GETAUTOURLDETECT",
3074 "EM_GETTEXTLENGTHEX",
3077 "EM_UNKNOWN_USER_98",
3078 "EM_UNKNOWN_USER_99",
3079 "EM_SETPUNCTUATION",
3080 "EM_GETPUNCTUATION",
3081 "EM_SETWORDWRAPMODE",
3082 "EM_GETWORDWRAPMODE",
3088 "EM_UNKNOWN_USER_109",
3089 "EM_UNKNOWN_USER_110",
3090 "EM_UNKNOWN_USER_111",
3091 "EM_UNKNOWN_USER_112",
3092 "EM_UNKNOWN_USER_113",
3093 "EM_UNKNOWN_USER_114",
3094 "EM_UNKNOWN_USER_115",
3095 "EM_UNKNOWN_USER_116",
3096 "EM_UNKNOWN_USER_117",
3097 "EM_UNKNOWN_USER_118",
3098 "EM_UNKNOWN_USER_119",
3099 "EM_SETLANGOPTIONS",
3100 "EM_GETLANGOPTIONS",
3101 "EM_GETIMECOMPMODE",
3105 "EM_SETIMEMODEBIAS",
3110 get_msg_name(UINT msg
)
3112 if (msg
>= EM_GETSEL
&& msg
<= EM_CHARFROMPOS
)
3113 return edit_messages
[msg
- EM_GETSEL
];
3114 if (msg
>= EM_CANPASTE
&& msg
<= EM_GETIMEMODEBIAS
)
3115 return richedit_messages
[msg
- EM_CANPASTE
];
3119 static void ME_LinkNotify(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
3123 ME_Cursor cursor
; /* The start of the clicked text. */
3126 x
= (short)LOWORD(lParam
);
3127 y
= (short)HIWORD(lParam
);
3128 ME_CharFromPos(editor
, x
, y
, &cursor
, &isExact
);
3129 if (!isExact
) return;
3131 if (cursor
.pRun
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
&&
3132 cursor
.pRun
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
)
3133 { /* The clicked run has CFE_LINK set */
3136 info
.nmhdr
.hwndFrom
= NULL
;
3137 info
.nmhdr
.idFrom
= 0;
3138 info
.nmhdr
.code
= EN_LINK
;
3140 info
.wParam
= wParam
;
3141 info
.lParam
= lParam
;
3144 /* find the first contiguous run with CFE_LINK set */
3145 info
.chrg
.cpMin
= ME_GetCursorOfs(&cursor
);
3146 for (di
= cursor
.pRun
->prev
;
3147 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3150 info
.chrg
.cpMin
-= di
->member
.run
.len
;
3153 /* find the last contiguous run with CFE_LINK set */
3154 info
.chrg
.cpMax
= ME_GetCursorOfs(&cursor
) + cursor
.pRun
->member
.run
.len
;
3155 for (di
= cursor
.pRun
->next
;
3156 di
&& di
->type
== diRun
&& (di
->member
.run
.style
->fmt
.dwMask
& CFM_LINK
) && (di
->member
.run
.style
->fmt
.dwEffects
& CFE_LINK
);
3159 info
.chrg
.cpMax
+= di
->member
.run
.len
;
3162 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
3166 void ME_ReplaceSel(ME_TextEditor
*editor
, BOOL can_undo
, const WCHAR
*str
, int len
)
3168 int from
, to
, nStartCursor
;
3171 nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3172 style
= ME_GetSelectionInsertStyle(editor
);
3173 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3174 ME_InsertTextFromCursor(editor
, 0, str
, len
, style
);
3175 ME_ReleaseStyle(style
);
3176 /* drop temporary style if line end */
3178 * FIXME question: does abc\n mean: put abc,
3179 * clear temp style, put \n? (would require a change)
3181 if (len
>0 && str
[len
-1] == '\n')
3182 ME_ClearTempStyle(editor
);
3183 ME_CommitUndo(editor
);
3184 ME_UpdateSelectionLinkAttribute(editor
);
3186 ME_EmptyUndoStack(editor
);
3187 ME_UpdateRepaint(editor
, FALSE
);
3190 static void ME_SetText(ME_TextEditor
*editor
, void *text
, BOOL unicode
)
3192 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3195 LPWSTR wszText
= ME_ToUnicode(codepage
, text
, &textLen
);
3201 /* uses default style! */
3202 if (!(editor
->styleFlags
& ES_MULTILINE
))
3206 while (*p
!= '\0' && *p
!= '\r' && *p
!= '\n') p
++;
3209 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, editor
->pBuffer
->pDefaultStyle
);
3211 ME_EndToUnicode(codepage
, wszText
);
3214 static LRESULT
ME_WmCreate(ME_TextEditor
*editor
, LPARAM lParam
, BOOL unicode
)
3216 CREATESTRUCTW
*createW
= (CREATESTRUCTW
*)lParam
;
3217 CREATESTRUCTA
*createA
= (CREATESTRUCTA
*)lParam
;
3222 text
= unicode
? (void*)createW
->lpszName
: (void*)createA
->lpszName
;
3224 ME_SetDefaultFormatRect(editor
);
3226 max
= (editor
->styleFlags
& ES_DISABLENOSCROLL
) ? 1 : 0;
3227 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_VSCROLL
)
3228 ITextHost_TxSetScrollRange(editor
->texthost
, SB_VERT
, 0, max
, TRUE
);
3230 if (~editor
->styleFlags
& ES_DISABLENOSCROLL
|| editor
->styleFlags
& WS_HSCROLL
)
3231 ITextHost_TxSetScrollRange(editor
->texthost
, SB_HORZ
, 0, max
, TRUE
);
3233 if (editor
->styleFlags
& ES_DISABLENOSCROLL
)
3235 if (editor
->styleFlags
& WS_VSCROLL
)
3237 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_VERT
, ESB_DISABLE_BOTH
);
3238 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
, TRUE
);
3240 if (editor
->styleFlags
& WS_HSCROLL
)
3242 ITextHost_TxEnableScrollBar(editor
->texthost
, SB_HORZ
, ESB_DISABLE_BOTH
);
3243 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
, TRUE
);
3249 ME_SetText(editor
, text
, unicode
);
3250 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
3251 ME_SetCursorToStart(editor
, &editor
->pCursors
[1]);
3254 ME_CommitUndo(editor
);
3255 ME_WrapMarkedParagraphs(editor
);
3256 ME_MoveCaret(editor
);
3261 #define UNSUPPORTED_MSG(e) \
3263 FIXME(#e ": stub\n"); \
3264 *phresult = S_FALSE; \
3267 /* Handle messages for windowless and windowed richedit controls.
3269 * The LRESULT that is returned is a return value for window procs,
3270 * and the phresult parameter is the COM return code needed by the
3271 * text services interface. */
3272 LRESULT
ME_HandleMessage(ME_TextEditor
*editor
, UINT msg
, WPARAM wParam
,
3273 LPARAM lParam
, BOOL unicode
, HRESULT
* phresult
)
3279 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
3280 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
3281 UNSUPPORTED_MSG(EM_FMTLINES
)
3282 UNSUPPORTED_MSG(EM_FORMATRANGE
)
3283 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS
)
3284 UNSUPPORTED_MSG(EM_GETEDITSTYLE
)
3285 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
3286 UNSUPPORTED_MSG(EM_GETIMESTATUS
)
3287 UNSUPPORTED_MSG(EM_SETIMESTATUS
)
3288 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
3289 UNSUPPORTED_MSG(EM_GETREDONAME
)
3290 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS
)
3291 UNSUPPORTED_MSG(EM_GETUNDONAME
)
3292 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
3293 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
3294 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
3295 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS
)
3296 UNSUPPORTED_MSG(EM_SETEDITSTYLE
)
3297 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
3298 UNSUPPORTED_MSG(EM_SETMARGINS
)
3299 UNSUPPORTED_MSG(EM_SETPALETTE
)
3300 UNSUPPORTED_MSG(EM_SETTABSTOPS
)
3301 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS
)
3302 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
3304 /* Messages specific to Richedit controls */
3307 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
, TRUE
);
3309 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
3312 UINT code
= DLGC_WANTCHARS
|DLGC_WANTTAB
|DLGC_WANTARROWS
;
3315 editor
->bDialogMode
= TRUE
;
3316 if (editor
->styleFlags
& ES_MULTILINE
)
3317 code
|= DLGC_WANTMESSAGE
;
3318 if (!(editor
->styleFlags
& ES_SAVESEL
))
3319 code
|= DLGC_HASSETSEL
;
3322 case EM_EMPTYUNDOBUFFER
:
3323 ME_EmptyUndoStack(editor
);
3327 /* Note: wParam/lParam can be NULL */
3329 PUINT pfrom
= wParam
? (PUINT
)wParam
: &from
;
3330 PUINT pto
= lParam
? (PUINT
)lParam
: &to
;
3331 ME_GetSelectionOfs(editor
, (int *)pfrom
, (int *)pto
);
3332 if ((*pfrom
|*pto
) & 0xFFFF0000)
3334 return MAKELONG(*pfrom
,*pto
);
3338 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
3339 ME_GetSelectionOfs(editor
, &pRange
->cpMin
, &pRange
->cpMax
);
3340 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange
->cpMin
, pRange
->cpMax
);
3343 case EM_SETUNDOLIMIT
:
3345 if ((int)wParam
< 0)
3346 editor
->nUndoLimit
= STACK_SIZE_DEFAULT
;
3348 editor
->nUndoLimit
= min(wParam
, STACK_SIZE_MAX
);
3349 /* Setting a max stack size keeps wine from getting killed
3350 for hogging memory. Windows allocates all this memory at once, so
3351 no program would realistically set a value above our maximum. */
3352 return editor
->nUndoLimit
;
3355 return !list_empty( &editor
->undo_stack
);
3357 return !list_empty( &editor
->redo_stack
);
3358 case WM_UNDO
: /* FIXME: actually not the same */
3360 return ME_Undo(editor
);
3362 return ME_Redo(editor
);
3365 /* these flags are equivalent to the ES_* counterparts */
3366 DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3367 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
| ECO_SELECTIONBAR
;
3368 DWORD settings
= editor
->styleFlags
& mask
;
3372 case EM_SETFONTSIZE
:
3375 LONG tmp_size
, size
;
3376 BOOL is_increase
= ((LONG
)wParam
> 0);
3378 if (editor
->mode
& TM_PLAINTEXT
)
3381 cf
.cbSize
= sizeof(cf
);
3382 cf
.dwMask
= CFM_SIZE
;
3383 ME_GetSelectionCharFormat(editor
, &cf
);
3384 tmp_size
= (cf
.yHeight
/ 20) + wParam
;
3388 else if (tmp_size
> 12 && tmp_size
< 28 && tmp_size
% 2)
3389 size
= tmp_size
+ (is_increase
? 1 : -1);
3390 else if (tmp_size
> 28 && tmp_size
< 36)
3391 size
= is_increase
? 36 : 28;
3392 else if (tmp_size
> 36 && tmp_size
< 48)
3393 size
= is_increase
? 48 : 36;
3394 else if (tmp_size
> 48 && tmp_size
< 72)
3395 size
= is_increase
? 72 : 48;
3396 else if (tmp_size
> 72 && tmp_size
< 80)
3397 size
= is_increase
? 80 : 72;
3398 else if (tmp_size
> 80 && tmp_size
< 1638)
3399 size
= 10 * (is_increase
? (tmp_size
/ 10 + 1) : (tmp_size
/ 10));
3400 else if (tmp_size
>= 1638)
3405 cf
.yHeight
= size
* 20; /* convert twips to points */
3406 ME_SetSelectionCharFormat(editor
, &cf
);
3407 ME_CommitUndo(editor
);
3408 ME_WrapMarkedParagraphs(editor
);
3409 ME_UpdateScrollBar(editor
);
3416 /* these flags are equivalent to ES_* counterparts, except for
3417 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3418 * but is still stored in editor->styleFlags. */
3419 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
3420 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
3421 ECO_SELECTIONBAR
| ECO_AUTOWORDSELECTION
;
3422 DWORD settings
= mask
& editor
->styleFlags
;
3423 DWORD oldSettings
= settings
;
3424 DWORD changedSettings
;
3440 changedSettings
= oldSettings
^ settings
;
3442 if (changedSettings
) {
3443 editor
->styleFlags
= (editor
->styleFlags
& ~mask
) | (settings
& mask
);
3445 if (changedSettings
& ECO_SELECTIONBAR
)
3447 ITextHost_TxInvalidateRect(editor
->texthost
, &editor
->rcFormat
, TRUE
);
3448 if (settings
& ECO_SELECTIONBAR
) {
3449 assert(!editor
->selofs
);
3450 editor
->selofs
= SELECTIONBAR_WIDTH
;
3451 editor
->rcFormat
.left
+= editor
->selofs
;
3453 editor
->rcFormat
.left
-= editor
->selofs
;
3456 ME_RewrapRepaint(editor
);
3459 if (changedSettings
& settings
& ECO_VERTICAL
)
3460 FIXME("ECO_VERTICAL not implemented yet!\n");
3461 if (changedSettings
& settings
& ECO_AUTOHSCROLL
)
3462 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3463 if (changedSettings
& settings
& ECO_AUTOVSCROLL
)
3464 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3465 if (changedSettings
& settings
& ECO_NOHIDESEL
)
3466 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3467 if (changedSettings
& settings
& ECO_WANTRETURN
)
3468 FIXME("ECO_WANTRETURN not implemented yet!\n");
3469 if (changedSettings
& settings
& ECO_AUTOWORDSELECTION
)
3470 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3477 return handle_EM_EXSETSEL( editor
, wParam
, lParam
);
3479 case EM_SETSCROLLPOS
:
3481 POINT
*point
= (POINT
*)lParam
;
3482 ME_ScrollAbs(editor
, point
->x
, point
->y
);
3485 case EM_AUTOURLDETECT
:
3487 if (wParam
==1 || wParam
==0)
3489 editor
->AutoURLDetect_bEnable
= (BOOL
)wParam
;
3492 return E_INVALIDARG
;
3494 case EM_GETAUTOURLDETECT
:
3496 return editor
->AutoURLDetect_bEnable
;
3500 CHARRANGE range
= *(CHARRANGE
*)lParam
;
3502 return handle_EM_EXSETSEL( editor
, range
.cpMin
, range
.cpMax
);
3504 case EM_SHOWSCROLLBAR
:
3517 flags
= WS_HSCROLL
|WS_VSCROLL
;
3524 editor
->styleFlags
|= flags
;
3525 if (flags
& WS_HSCROLL
)
3526 ITextHost_TxShowScrollBar(editor
->texthost
, SB_HORZ
,
3527 editor
->nTotalWidth
> editor
->sizeWindow
.cx
);
3528 if (flags
& WS_VSCROLL
)
3529 ITextHost_TxShowScrollBar(editor
->texthost
, SB_VERT
,
3530 editor
->nTotalLength
> editor
->sizeWindow
.cy
);
3532 editor
->styleFlags
&= ~flags
;
3533 ITextHost_TxShowScrollBar(editor
->texthost
, wParam
, FALSE
);
3540 SETTEXTEX
*pStruct
= (SETTEXTEX
*)wParam
;
3543 BOOL bRtf
, bUnicode
, bSelection
, bUTF8
;
3544 int oldModify
= editor
->nModifyStep
;
3545 static const char utf8_bom
[] = {0xef, 0xbb, 0xbf};
3547 if (!pStruct
) return 0;
3549 /* If we detect ascii rtf at the start of the string,
3550 * we know it isn't unicode. */
3551 bRtf
= (lParam
&& (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3552 !strncmp((char *)lParam
, "{\\urtf", 6)));
3553 bUnicode
= !bRtf
&& pStruct
->codepage
== CP_UNICODE
;
3554 bUTF8
= (lParam
&& (!strncmp((char *)lParam
, utf8_bom
, 3)));
3556 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3557 bUnicode
? debugstr_w((LPCWSTR
)lParam
) : debugstr_a((LPCSTR
)lParam
),
3558 pStruct
->flags
, pStruct
->codepage
);
3560 bSelection
= (pStruct
->flags
& ST_SELECTION
) != 0;
3562 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3563 style
= ME_GetSelectionInsertStyle(editor
);
3564 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
- from
, FALSE
);
3567 ME_SetCursorToStart(editor
, &start
);
3568 ME_InternalDeleteText(editor
, &start
, ME_GetTextLength(editor
), FALSE
);
3569 style
= editor
->pBuffer
->pDefaultStyle
;
3573 ME_StreamInRTFString(editor
, bSelection
, (char *)lParam
);
3575 /* FIXME: The length returned doesn't include the rtf control
3576 * characters, only the actual text. */
3577 len
= lParam
? strlen((char *)lParam
) : 0;
3580 if (bUTF8
&& !bUnicode
) {
3581 wszText
= ME_ToUnicode(CP_UTF8
, (void *)(lParam
+3), &len
);
3582 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3583 ME_EndToUnicode(CP_UTF8
, wszText
);
3585 wszText
= ME_ToUnicode(pStruct
->codepage
, (void *)lParam
, &len
);
3586 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
3587 ME_EndToUnicode(pStruct
->codepage
, wszText
);
3592 ME_ReleaseStyle(style
);
3593 ME_UpdateSelectionLinkAttribute(editor
);
3597 ME_SetCursorToStart(editor
, &cursor
);
3598 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3600 ME_CommitUndo(editor
);
3601 if (!(pStruct
->flags
& ST_KEEPUNDO
))
3603 editor
->nModifyStep
= oldModify
;
3604 ME_EmptyUndoStack(editor
);
3606 ME_UpdateRepaint(editor
, FALSE
);
3609 case EM_SETBKGNDCOLOR
:
3612 if (editor
->rgbBackColor
!= -1) {
3613 DeleteObject(editor
->hbrBackground
);
3614 lColor
= editor
->rgbBackColor
;
3616 else lColor
= ITextHost_TxGetSysColor(editor
->texthost
, COLOR_WINDOW
);
3620 editor
->rgbBackColor
= -1;
3621 editor
->hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3625 editor
->rgbBackColor
= lParam
;
3626 editor
->hbrBackground
= CreateSolidBrush(editor
->rgbBackColor
);
3628 ITextHost_TxInvalidateRect(editor
->texthost
, NULL
, TRUE
);
3632 return editor
->nModifyStep
== 0 ? 0 : -1;
3636 editor
->nModifyStep
= 1;
3638 editor
->nModifyStep
= 0;
3642 case EM_SETREADONLY
:
3645 editor
->styleFlags
|= ES_READONLY
;
3647 editor
->styleFlags
&= ~ES_READONLY
;
3650 case EM_SETEVENTMASK
:
3652 DWORD nOldMask
= editor
->nEventMask
;
3654 editor
->nEventMask
= lParam
;
3657 case EM_GETEVENTMASK
:
3658 return editor
->nEventMask
;
3659 case EM_SETCHARFORMAT
:
3661 CHARFORMAT2W buf
, *p
;
3662 BOOL bRepaint
= TRUE
;
3663 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
3664 if (p
== NULL
) return 0;
3665 if (wParam
& SCF_ALL
) {
3666 if (editor
->mode
& TM_PLAINTEXT
) {
3667 ME_SetDefaultCharFormat(editor
, p
);
3670 ME_SetCursorToStart(editor
, &start
);
3671 ME_SetCharFormat(editor
, &start
, NULL
, p
);
3672 editor
->nModifyStep
= 1;
3674 } else if (wParam
& SCF_SELECTION
) {
3675 if (editor
->mode
& TM_PLAINTEXT
)
3677 if (wParam
& SCF_WORD
) {
3679 ME_Cursor end
= editor
->pCursors
[0];
3680 ME_MoveCursorWords(editor
, &end
, +1);
3682 ME_MoveCursorWords(editor
, &start
, -1);
3683 ME_SetCharFormat(editor
, &start
, &end
, p
);
3685 bRepaint
= ME_IsSelection(editor
);
3686 ME_SetSelectionCharFormat(editor
, p
);
3687 if (bRepaint
) editor
->nModifyStep
= 1;
3688 } else { /* SCF_DEFAULT */
3689 ME_SetDefaultCharFormat(editor
, p
);
3691 ME_CommitUndo(editor
);
3694 ME_WrapMarkedParagraphs(editor
);
3695 ME_UpdateScrollBar(editor
);
3700 case EM_GETCHARFORMAT
:
3702 CHARFORMAT2W tmp
, *dst
= (CHARFORMAT2W
*)lParam
;
3703 if (dst
->cbSize
!= sizeof(CHARFORMATA
) &&
3704 dst
->cbSize
!= sizeof(CHARFORMATW
) &&
3705 dst
->cbSize
!= sizeof(CHARFORMAT2A
) &&
3706 dst
->cbSize
!= sizeof(CHARFORMAT2W
))
3708 tmp
.cbSize
= sizeof(tmp
);
3710 ME_GetDefaultCharFormat(editor
, &tmp
);
3712 ME_GetSelectionCharFormat(editor
, &tmp
);
3713 ME_CopyToCFAny(dst
, &tmp
);
3716 case EM_SETPARAFORMAT
:
3718 BOOL result
= ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3719 ME_WrapMarkedParagraphs(editor
);
3720 ME_UpdateScrollBar(editor
);
3722 ME_CommitUndo(editor
);
3725 case EM_GETPARAFORMAT
:
3726 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
3727 return ((PARAFORMAT2
*)lParam
)->dwMask
;
3728 case EM_GETFIRSTVISIBLELINE
:
3730 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
3731 int y
= editor
->vert_si
.nPos
;
3736 p
= ME_FindItemFwd(p
, diStartRowOrParagraphOrEnd
);
3737 if (p
->type
== diTextEnd
)
3739 if (p
->type
== diParagraph
) {
3740 ypara
= p
->member
.para
.pt
.y
;
3743 ystart
= ypara
+ p
->member
.row
.pt
.y
;
3744 yend
= ystart
+ p
->member
.row
.nHeight
;
3752 case EM_HIDESELECTION
:
3754 editor
->bHideSelection
= (wParam
!= 0);
3755 ME_InvalidateSelection(editor
);
3760 if (!(editor
->styleFlags
& ES_MULTILINE
))
3762 ME_ScrollDown( editor
, lParam
* get_default_line_height( editor
) );
3768 int nStartCursor
= ME_GetSelectionOfs(editor
, &from
, &to
);
3769 ME_InternalDeleteText(editor
, &editor
->pCursors
[nStartCursor
], to
-from
, FALSE
);
3770 ME_CommitUndo(editor
);
3771 ME_UpdateRepaint(editor
, TRUE
);
3777 LONG codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3778 LPWSTR wszText
= ME_ToUnicode(codepage
, (void *)lParam
, &len
);
3780 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
3782 ME_ReplaceSel(editor
, !!wParam
, wszText
, len
);
3783 ME_EndToUnicode(codepage
, wszText
);
3786 case EM_SCROLLCARET
:
3787 ME_EnsureVisible(editor
, &editor
->pCursors
[0]);
3794 BOOL bRepaint
= LOWORD(lParam
);
3797 wParam
= (WPARAM
)GetStockObject(SYSTEM_FONT
);
3799 if (!GetObjectW((HGDIOBJ
)wParam
, sizeof(LOGFONTW
), &lf
))
3802 hDC
= ITextHost_TxGetDC(editor
->texthost
);
3803 ME_CharFormatFromLogFont(hDC
, &lf
, &fmt
);
3804 ITextHost_TxReleaseDC(editor
->texthost
, hDC
);
3805 if (editor
->mode
& TM_RICHTEXT
) {
3807 ME_SetCursorToStart(editor
, &start
);
3808 ME_SetCharFormat(editor
, &start
, NULL
, &fmt
);
3810 ME_SetDefaultCharFormat(editor
, &fmt
);
3812 ME_CommitUndo(editor
);
3813 ME_MarkAllForWrapping(editor
);
3814 ME_WrapMarkedParagraphs(editor
);
3815 ME_UpdateScrollBar(editor
);
3823 ME_SetCursorToStart(editor
, &cursor
);
3824 ME_InternalDeleteText(editor
, &cursor
, ME_GetTextLength(editor
), FALSE
);
3827 TRACE("WM_SETTEXT lParam==%lx\n",lParam
);
3828 if (!strncmp((char *)lParam
, "{\\rtf", 5) ||
3829 !strncmp((char *)lParam
, "{\\urtf", 6))
3831 /* Undocumented: WM_SETTEXT supports RTF text */
3832 ME_StreamInRTFString(editor
, 0, (char *)lParam
);
3835 ME_SetText(editor
, (void*)lParam
, unicode
);
3838 TRACE("WM_SETTEXT - NULL\n");
3839 ME_SetCursorToStart(editor
, &cursor
);
3840 ME_UpdateLinkAttribute(editor
, &cursor
, INT_MAX
);
3841 ME_SetSelection(editor
, 0, 0);
3842 editor
->nModifyStep
= 0;
3843 ME_CommitUndo(editor
);
3844 ME_EmptyUndoStack(editor
);
3845 ME_UpdateRepaint(editor
, FALSE
);
3850 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
3851 if (IsClipboardFormatAvailable(nRTFFormat
))
3853 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
3858 case WM_MBUTTONDOWN
:
3864 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3865 int nChars
= nTo
- nFrom
;
3866 ME_Cursor
*selStart
= &editor
->pCursors
[nStartCur
];
3868 if (ME_Copy(editor
, selStart
, nChars
) && msg
== WM_CUT
)
3870 ME_InternalDeleteText(editor
, selStart
, nChars
, FALSE
);
3871 ME_CommitUndo(editor
);
3872 ME_UpdateRepaint(editor
, TRUE
);
3876 case WM_GETTEXTLENGTH
:
3878 GETTEXTLENGTHEX how
;
3880 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3881 how
.flags
= GTL_CLOSE
| (editor
->bEmulateVersion10
? 0 : GTL_USECRLF
) | GTL_NUMCHARS
;
3882 how
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3883 return ME_GetTextLengthEx(editor
, &how
);
3885 case EM_GETTEXTLENGTHEX
:
3886 return ME_GetTextLengthEx(editor
, (GETTEXTLENGTHEX
*)wParam
);
3890 ex
.cb
= wParam
* (unicode
? sizeof(WCHAR
) : sizeof(CHAR
));
3891 ex
.flags
= GT_USECRLF
;
3892 ex
.codepage
= unicode
? CP_UNICODE
: CP_ACP
;
3893 ex
.lpDefaultChar
= NULL
;
3894 ex
.lpUsedDefChar
= NULL
;
3895 return ME_GetTextEx(editor
, &ex
, lParam
);
3898 return ME_GetTextEx(editor
, (GETTEXTEX
*)wParam
, lParam
);
3901 int nFrom
, nTo
, nStartCur
= ME_GetSelectionOfs(editor
, &nFrom
, &nTo
);
3902 ME_Cursor
*from
= &editor
->pCursors
[nStartCur
];
3903 return ME_GetTextRange(editor
, (WCHAR
*)lParam
, from
,
3904 nTo
- nFrom
, unicode
);
3906 case EM_GETSCROLLPOS
:
3908 POINT
*point
= (POINT
*)lParam
;
3909 point
->x
= editor
->horz_si
.nPos
;
3910 point
->y
= editor
->vert_si
.nPos
;
3911 /* 16-bit scaled value is returned as stored in scrollinfo */
3912 if (editor
->horz_si
.nMax
> 0xffff)
3913 point
->x
= MulDiv(point
->x
, 0xffff, editor
->horz_si
.nMax
);
3914 if (editor
->vert_si
.nMax
> 0xffff)
3915 point
->y
= MulDiv(point
->y
, 0xffff, editor
->vert_si
.nMax
);
3918 case EM_GETTEXTRANGE
:
3920 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
3922 int nStart
= rng
->chrg
.cpMin
;
3923 int nEnd
= rng
->chrg
.cpMax
;
3924 int textlength
= ME_GetTextLength(editor
);
3926 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3927 rng
->chrg
.cpMin
, rng
->chrg
.cpMax
, unicode
, textlength
);
3928 if (nStart
< 0) return 0;
3929 if ((nStart
== 0 && nEnd
== -1) || nEnd
> textlength
)
3931 if (nStart
>= nEnd
) return 0;
3933 ME_CursorFromCharOfs(editor
, nStart
, &start
);
3934 return ME_GetTextRange(editor
, rng
->lpstrText
, &start
, nEnd
- nStart
, unicode
);
3938 ME_DisplayItem
*run
;
3939 const unsigned int nMaxChars
= *(WORD
*) lParam
;
3940 unsigned int nCharsLeft
= nMaxChars
;
3941 char *dest
= (char *) lParam
;
3942 BOOL wroteNull
= FALSE
;
3944 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam
, nMaxChars
,
3945 unicode
? "Unicode" : "Ansi");
3947 run
= ME_FindRowWithNumber(editor
, wParam
);
3951 while (nCharsLeft
&& (run
= ME_FindItemFwd(run
, diRunOrStartRow
))
3952 && run
->type
== diRun
)
3954 WCHAR
*str
= get_text( &run
->member
.run
, 0 );
3957 nCopy
= min(nCharsLeft
, run
->member
.run
.len
);
3960 memcpy(dest
, str
, nCopy
* sizeof(WCHAR
));
3962 nCopy
= WideCharToMultiByte(CP_ACP
, 0, str
, nCopy
, dest
,
3963 nCharsLeft
, NULL
, NULL
);
3964 dest
+= nCopy
* (unicode
? sizeof(WCHAR
) : 1);
3965 nCharsLeft
-= nCopy
;
3968 /* append line termination, space allowing */
3972 *((WCHAR
*)dest
) = '\0';
3979 TRACE("EM_GETLINE: got %u characters\n", nMaxChars
- nCharsLeft
);
3980 return nMaxChars
- nCharsLeft
- (wroteNull
? 1 : 0);
3982 case EM_GETLINECOUNT
:
3984 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
->next
;
3987 ME_DisplayItem
*prev_para
= NULL
, *last_para
= NULL
;
3989 while (item
!= editor
->pBuffer
->pLast
)
3991 assert(item
->type
== diParagraph
);
3992 prev_para
= ME_FindItemBack(item
, diRun
);
3994 assert(prev_para
->member
.run
.nFlags
& MERF_ENDPARA
);
3996 nRows
+= item
->member
.para
.nRows
;
3997 item
= item
->member
.para
.next_para
;
3999 last_para
= ME_FindItemBack(item
, diRun
);
4001 assert(last_para
->member
.run
.nFlags
& MERF_ENDPARA
);
4002 if (editor
->bEmulateVersion10
&& prev_para
&&
4003 last_para
->member
.run
.nCharOfs
== 0 &&
4004 prev_para
->member
.run
.len
== 1 &&
4005 *get_text( &prev_para
->member
.run
, 0 ) == '\r')
4007 /* In 1.0 emulation, the last solitary \r at the very end of the text
4008 (if one exists) is NOT a line break.
4009 FIXME: this is an ugly hack. This should have a more regular model. */
4013 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows
);
4014 return max(1, nRows
);
4016 case EM_LINEFROMCHAR
:
4019 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
4021 return ME_RowNumberFromCharOfs(editor
, wParam
);
4023 case EM_EXLINEFROMCHAR
:
4026 return ME_RowNumberFromCharOfs(editor
, ME_GetCursorOfs(&editor
->pCursors
[1]));
4028 return ME_RowNumberFromCharOfs(editor
, lParam
);
4032 ME_DisplayItem
*item
, *para
;
4036 item
= ME_FindItemBack(editor
->pCursors
[0].pRun
, diStartRow
);
4038 item
= ME_FindRowWithNumber(editor
, wParam
);
4041 para
= ME_GetParagraph(item
);
4042 item
= ME_FindItemFwd(item
, diRun
);
4043 nCharOfs
= para
->member
.para
.nCharOfs
+ item
->member
.run
.nCharOfs
;
4044 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs
);
4049 ME_DisplayItem
*item
, *item_end
;
4050 int nChars
= 0, nThisLineOfs
= 0, nNextLineOfs
= 0;
4051 ME_DisplayItem
*para
, *run
;
4053 if (wParam
> ME_GetTextLength(editor
))
4057 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
4060 ME_RunOfsFromCharOfs(editor
, wParam
, ¶
, &run
, NULL
);
4061 item
= ME_RowStart(run
);
4062 nThisLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item
, diRun
), 0);
4063 item_end
= ME_FindItemFwd(item
, diStartRowOrParagraphOrEnd
);
4064 if (item_end
->type
== diStartRow
) {
4065 nNextLineOfs
= ME_CharOfsFromRunOfs(editor
, para
, ME_FindItemFwd(item_end
, diRun
), 0);
4067 ME_DisplayItem
*endRun
= ME_FindItemBack(item_end
, diRun
);
4068 assert(endRun
&& endRun
->member
.run
.nFlags
& MERF_ENDPARA
);
4069 nNextLineOfs
= item_end
->member
.para
.nCharOfs
- endRun
->member
.run
.len
;
4071 nChars
= nNextLineOfs
- nThisLineOfs
;
4072 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam
, nChars
);
4075 case EM_EXLIMITTEXT
:
4077 if ((int)lParam
< 0)
4080 editor
->nTextLimit
= 65536;
4082 editor
->nTextLimit
= (int) lParam
;
4088 editor
->nTextLimit
= 65536;
4090 editor
->nTextLimit
= (int) wParam
;
4093 case EM_GETLIMITTEXT
:
4095 return editor
->nTextLimit
;
4101 FINDTEXTA
*ft
= (FINDTEXTA
*)lParam
;
4102 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, NULL
, 0);
4105 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
4106 MultiByteToWideChar(CP_ACP
, 0, ft
->lpstrText
, -1, tmp
, nChars
);
4107 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, tmp
, NULL
);
4110 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
4111 r
= ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4119 FINDTEXTEXA
*ex
= (FINDTEXTEXA
*)lParam
;
4120 int nChars
= MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, NULL
, 0);
4123 if ((tmp
= ALLOC_N_OBJ(WCHAR
, nChars
)) != NULL
)
4124 MultiByteToWideChar(CP_ACP
, 0, ex
->lpstrText
, -1, tmp
, nChars
);
4125 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, tmp
, &ex
->chrgText
);
4128 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4129 r
= ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4135 FINDTEXTW
*ft
= (FINDTEXTW
*)lParam
;
4136 return ME_FindText(editor
, wParam
, &ft
->chrg
, ft
->lpstrText
, NULL
);
4138 case EM_FINDTEXTEXW
:
4140 FINDTEXTEXW
*ex
= (FINDTEXTEXW
*)lParam
;
4141 return ME_FindText(editor
, wParam
, &ex
->chrg
, ex
->lpstrText
, &ex
->chrgText
);
4144 if (!wParam
|| !lParam
)
4146 *(int *)wParam
= editor
->nZoomNumerator
;
4147 *(int *)lParam
= editor
->nZoomDenominator
;
4150 return ME_SetZoom(editor
, wParam
, lParam
);
4151 case EM_CHARFROMPOS
:
4154 if (ME_CharFromPos(editor
, ((POINTL
*)lParam
)->x
, ((POINTL
*)lParam
)->y
,
4156 return ME_GetCursorOfs(&cursor
);
4160 case EM_POSFROMCHAR
:
4162 ME_DisplayItem
*pPara
, *pRun
;
4163 int nCharOfs
, nOffset
, nLength
;
4167 /* detect which API version we're dealing with */
4168 if (wParam
>= 0x40000)
4170 nLength
= ME_GetTextLength(editor
);
4171 nCharOfs
= min(nCharOfs
, nLength
);
4172 nCharOfs
= max(nCharOfs
, 0);
4174 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pPara
, &pRun
, &nOffset
);
4175 assert(pRun
->type
== diRun
);
4176 pt
.y
= pRun
->member
.run
.pt
.y
;
4177 pt
.x
= pRun
->member
.run
.pt
.x
+ ME_PointFromChar(editor
, &pRun
->member
.run
, nOffset
, TRUE
);
4178 pt
.y
+= pPara
->member
.para
.pt
.y
+ editor
->rcFormat
.top
;
4179 pt
.x
+= editor
->rcFormat
.left
;
4181 pt
.x
-= editor
->horz_si
.nPos
;
4182 pt
.y
-= editor
->vert_si
.nPos
;
4184 if (wParam
>= 0x40000) {
4185 *(POINTL
*)wParam
= pt
;
4187 return (wParam
>= 0x40000) ? 0 : MAKELONG( pt
.x
, pt
.y
);
4190 return ME_WmCreate(editor
, lParam
, unicode
);
4192 ME_DestroyEditor(editor
);
4197 if (wParam
== (WPARAM
)editor
->hWnd
&& GetCursorPos(&cursor_pos
) &&
4198 ScreenToClient(editor
->hWnd
, &cursor_pos
))
4199 ME_LinkNotify(editor
, msg
, 0, MAKELPARAM(cursor_pos
.x
, cursor_pos
.y
));
4200 return ME_SetCursor(editor
);
4202 case WM_LBUTTONDBLCLK
:
4203 case WM_LBUTTONDOWN
:
4205 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4206 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4207 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4209 ITextHost_TxSetFocus(editor
->texthost
);
4210 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
4211 ME_CalculateClickCount(editor
, msg
, wParam
, lParam
));
4212 ITextHost_TxSetCapture(editor
->texthost
, TRUE
);
4213 editor
->bMouseCaptured
= TRUE
;
4214 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4215 if (!ME_SetCursor(editor
)) goto do_default
;
4219 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4220 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4222 if (editor
->bMouseCaptured
)
4223 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
4225 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4226 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4227 if (editor
->bMouseCaptured
)
4228 ME_SetCursor(editor
);
4231 if (editor
->bMouseCaptured
) {
4232 ITextHost_TxSetCapture(editor
->texthost
, FALSE
);
4233 editor
->bMouseCaptured
= FALSE
;
4235 if (editor
->nSelectionType
== stDocument
)
4236 editor
->nSelectionType
= stPosition
;
4237 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4238 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4242 ME_SetCursor(editor
);
4243 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4247 case WM_RBUTTONDOWN
:
4248 case WM_RBUTTONDBLCLK
:
4249 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4250 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4251 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4253 ME_LinkNotify(editor
, msg
, wParam
, lParam
);
4255 case WM_CONTEXTMENU
:
4256 if (!ME_ShowContextMenu(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
)))
4260 editor
->bHaveFocus
= TRUE
;
4261 ME_ShowCaret(editor
);
4262 ME_SendOldNotify(editor
, EN_SETFOCUS
);
4265 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4266 editor
->bHaveFocus
= FALSE
;
4267 editor
->wheel_remain
= 0;
4268 ME_HideCaret(editor
);
4269 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
4272 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
4275 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4276 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4280 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4281 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4283 if (ME_KeyDown(editor
, LOWORD(wParam
)))
4287 if ((editor
->nEventMask
& ENM_KEYEVENTS
) &&
4288 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4290 return ME_Char(editor
, wParam
, lParam
, unicode
);
4294 if(wParam
== UNICODE_NOCHAR
) return TRUE
;
4295 if(wParam
<= 0x000fffff)
4297 if(wParam
> 0xffff) /* convert to surrogates */
4300 ME_Char(editor
, (wParam
>> 10) + 0xd800, 0, TRUE
);
4301 ME_Char(editor
, (wParam
& 0x03ff) + 0xdc00, 0, TRUE
);
4303 ME_Char(editor
, wParam
, 0, TRUE
);
4309 case EM_STOPGROUPTYPING
:
4310 ME_CommitUndo(editor
); /* End coalesced undos for typed characters */
4314 const int scrollUnit
= 7;
4316 switch(LOWORD(wParam
))
4319 ME_ScrollAbs(editor
, 0, 0);
4322 ME_ScrollAbs(editor
,
4323 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4324 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4327 ME_ScrollLeft(editor
, scrollUnit
);
4330 ME_ScrollRight(editor
, scrollUnit
);
4333 ME_ScrollLeft(editor
, editor
->sizeWindow
.cx
);
4336 ME_ScrollRight(editor
, editor
->sizeWindow
.cx
);
4339 case SB_THUMBPOSITION
:
4341 int pos
= HIWORD(wParam
);
4342 if (editor
->horz_si
.nMax
> 0xffff)
4343 pos
= MulDiv(pos
, editor
->horz_si
.nMax
, 0xffff);
4344 ME_HScrollAbs(editor
, pos
);
4350 case EM_SCROLL
: /* fall through */
4354 int lineHeight
= get_default_line_height( editor
);
4356 origNPos
= editor
->vert_si
.nPos
;
4358 switch(LOWORD(wParam
))
4361 ME_ScrollAbs(editor
, 0, 0);
4364 ME_ScrollAbs(editor
,
4365 editor
->horz_si
.nMax
- (int)editor
->horz_si
.nPage
,
4366 editor
->vert_si
.nMax
- (int)editor
->vert_si
.nPage
);
4369 ME_ScrollUp(editor
,lineHeight
);
4372 ME_ScrollDown(editor
,lineHeight
);
4375 ME_ScrollUp(editor
,editor
->sizeWindow
.cy
);
4378 ME_ScrollDown(editor
,editor
->sizeWindow
.cy
);
4381 case SB_THUMBPOSITION
:
4383 int pos
= HIWORD(wParam
);
4384 if (editor
->vert_si
.nMax
> 0xffff)
4385 pos
= MulDiv(pos
, editor
->vert_si
.nMax
, 0xffff);
4386 ME_VScrollAbs(editor
, pos
);
4390 if (msg
== EM_SCROLL
)
4391 return 0x00010000 | (((editor
->vert_si
.nPos
- origNPos
)/lineHeight
) & 0xffff);
4399 if ((editor
->nEventMask
& ENM_MOUSEEVENTS
) &&
4400 !ME_FilterEvent(editor
, msg
, &wParam
, &lParam
))
4403 ctrl_is_down
= GetKeyState(VK_CONTROL
) & 0x8000;
4405 delta
= GET_WHEEL_DELTA_WPARAM(wParam
);
4407 /* if scrolling changes direction, ignore left overs */
4408 if ((delta
< 0 && editor
->wheel_remain
< 0) ||
4409 (delta
> 0 && editor
->wheel_remain
> 0))
4410 editor
->wheel_remain
+= delta
;
4412 editor
->wheel_remain
= delta
;
4414 if (editor
->wheel_remain
)
4418 if (!editor
->nZoomNumerator
|| !editor
->nZoomDenominator
)
4422 numerator
= editor
->nZoomNumerator
* 100 / editor
->nZoomDenominator
;
4424 numerator
+= calc_wheel_change( &editor
->wheel_remain
, 10 );
4425 if (numerator
>= 10 && numerator
<= 500)
4426 ME_SetZoom(editor
, numerator
, 100);
4431 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES
, 0, &max_lines
, 0 );
4433 lines
= calc_wheel_change( &editor
->wheel_remain
, (int)max_lines
);
4435 ME_ScrollDown( editor
, -lines
* get_default_line_height( editor
) );
4442 *((RECT
*)lParam
) = editor
->rcFormat
;
4443 if (editor
->bDefaultFormatRect
)
4444 ((RECT
*)lParam
)->left
-= editor
->selofs
;
4454 RECT
*rc
= (RECT
*)lParam
;
4456 border
= editor
->exStyleFlags
& WS_EX_CLIENTEDGE
? 1 : 0;
4457 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4460 editor
->rcFormat
.top
= max(0, rc
->top
- border
);
4461 editor
->rcFormat
.left
= max(0, rc
->left
- border
);
4462 editor
->rcFormat
.bottom
= min(clientRect
.bottom
, rc
->bottom
);
4463 editor
->rcFormat
.right
= min(clientRect
.right
, rc
->right
+ border
);
4464 } else if (wParam
== 1) {
4465 /* MSDN incorrectly says a wParam value of 1 causes the
4466 * lParam rect to be used as a relative offset,
4467 * however, the tests show it just prevents min/max bound
4469 editor
->rcFormat
.top
= rc
->top
- border
;
4470 editor
->rcFormat
.left
= rc
->left
- border
;
4471 editor
->rcFormat
.bottom
= rc
->bottom
;
4472 editor
->rcFormat
.right
= rc
->right
+ border
;
4476 editor
->bDefaultFormatRect
= FALSE
;
4480 ME_SetDefaultFormatRect(editor
);
4481 editor
->bDefaultFormatRect
= TRUE
;
4483 ME_MarkAllForWrapping(editor
);
4484 ME_WrapMarkedParagraphs(editor
);
4485 ME_UpdateScrollBar(editor
);
4486 if (msg
!= EM_SETRECTNP
)
4490 case EM_REQUESTRESIZE
:
4491 ME_SendRequestResize(editor
, TRUE
);
4495 case WM_WINDOWPOSCHANGED
:
4498 WINDOWPOS
*winpos
= (WINDOWPOS
*)lParam
;
4500 if (winpos
->flags
& SWP_NOCLIENTSIZE
) goto do_default
;
4501 ITextHost_TxGetClientRect(editor
->texthost
, &clientRect
);
4502 if (editor
->bDefaultFormatRect
) {
4503 ME_SetDefaultFormatRect(editor
);
4505 editor
->rcFormat
.right
+= clientRect
.right
- editor
->prevClientRect
.right
;
4506 editor
->rcFormat
.bottom
+= clientRect
.bottom
- editor
->prevClientRect
.bottom
;
4508 editor
->prevClientRect
= clientRect
;
4509 ME_RewrapRepaint(editor
);
4512 /* IME messages to make richedit controls IME aware */
4513 case WM_IME_SETCONTEXT
:
4514 case WM_IME_CONTROL
:
4516 case WM_IME_COMPOSITIONFULL
:
4518 case WM_IME_STARTCOMPOSITION
:
4520 editor
->imeStartIndex
=ME_GetCursorOfs(&editor
->pCursors
[0]);
4521 ME_DeleteSelection(editor
);
4522 ME_CommitUndo(editor
);
4523 ME_UpdateRepaint(editor
, FALSE
);
4526 case WM_IME_COMPOSITION
:
4530 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
4531 hIMC
= ITextHost_TxImmGetContext(editor
->texthost
);
4532 ME_DeleteSelection(editor
);
4533 ME_SaveTempStyle(editor
);
4534 if (lParam
& (GCS_RESULTSTR
|GCS_COMPSTR
))
4536 LPWSTR lpCompStr
= NULL
;
4538 DWORD dwIndex
= lParam
& GCS_RESULTSTR
;
4540 dwIndex
= GCS_COMPSTR
;
4542 dwBufLen
= ImmGetCompositionStringW(hIMC
, dwIndex
, NULL
, 0);
4543 lpCompStr
= HeapAlloc(GetProcessHeap(),0,dwBufLen
+ sizeof(WCHAR
));
4544 ImmGetCompositionStringW(hIMC
, dwIndex
, lpCompStr
, dwBufLen
);
4545 lpCompStr
[dwBufLen
/sizeof(WCHAR
)] = 0;
4546 ME_InsertTextFromCursor(editor
,0,lpCompStr
,dwBufLen
/sizeof(WCHAR
),style
);
4547 HeapFree(GetProcessHeap(), 0, lpCompStr
);
4549 if (dwIndex
== GCS_COMPSTR
)
4550 ME_SetSelection(editor
,editor
->imeStartIndex
,
4551 editor
->imeStartIndex
+ dwBufLen
/sizeof(WCHAR
));
4553 ME_ReleaseStyle(style
);
4554 ME_CommitUndo(editor
);
4555 ME_UpdateRepaint(editor
, FALSE
);
4558 case WM_IME_ENDCOMPOSITION
:
4560 ME_DeleteSelection(editor
);
4561 editor
->imeStartIndex
=-1;
4564 case EM_GETOLEINTERFACE
:
4567 if (!CreateIRichEditOle(NULL
, editor
, (LPVOID
*)&editor
->reOle
))
4569 *(LPVOID
*)lParam
= editor
->reOle
;
4570 IRichEditOle_AddRef(editor
->reOle
);
4573 case EM_GETPASSWORDCHAR
:
4575 return editor
->cPasswordMask
;
4577 case EM_SETOLECALLBACK
:
4578 if(editor
->lpOleCallback
)
4579 IRichEditOleCallback_Release(editor
->lpOleCallback
);
4580 editor
->lpOleCallback
= (IRichEditOleCallback
*)lParam
;
4581 if(editor
->lpOleCallback
)
4582 IRichEditOleCallback_AddRef(editor
->lpOleCallback
);
4584 case EM_GETWORDBREAKPROC
:
4585 return (LRESULT
)editor
->pfnWordBreak
;
4586 case EM_SETWORDBREAKPROC
:
4588 EDITWORDBREAKPROCW pfnOld
= editor
->pfnWordBreak
;
4590 editor
->pfnWordBreak
= (EDITWORDBREAKPROCW
)lParam
;
4591 return (LRESULT
)pfnOld
;
4593 case EM_GETTEXTMODE
:
4594 return editor
->mode
;
4595 case EM_SETTEXTMODE
:
4600 if (ME_GetTextLength(editor
) ||
4601 !list_empty( &editor
->undo_stack
) || !list_empty( &editor
->redo_stack
))
4602 return E_UNEXPECTED
;
4604 /* Check for mutually exclusive flags in adjacent bits of wParam */
4605 if ((wParam
& (TM_RICHTEXT
| TM_MULTILEVELUNDO
| TM_MULTICODEPAGE
)) &
4606 (wParam
& (TM_PLAINTEXT
| TM_SINGLELEVELUNDO
| TM_SINGLECODEPAGE
)) << 1)
4607 return E_INVALIDARG
;
4609 if (wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
))
4611 mask
|= TM_RICHTEXT
| TM_PLAINTEXT
;
4612 changes
|= wParam
& (TM_RICHTEXT
| TM_PLAINTEXT
);
4613 if (wParam
& TM_PLAINTEXT
) {
4614 /* Clear selection since it should be possible to select the
4615 * end of text run for rich text */
4616 ME_InvalidateSelection(editor
);
4617 ME_SetCursorToStart(editor
, &editor
->pCursors
[0]);
4618 editor
->pCursors
[1] = editor
->pCursors
[0];
4619 /* plain text can only have the default style. */
4620 ME_ClearTempStyle(editor
);
4621 ME_AddRefStyle(editor
->pBuffer
->pDefaultStyle
);
4622 ME_ReleaseStyle(editor
->pCursors
[0].pRun
->member
.run
.style
);
4623 editor
->pCursors
[0].pRun
->member
.run
.style
= editor
->pBuffer
->pDefaultStyle
;
4626 /* FIXME: Currently no support for undo level and code page options */
4627 editor
->mode
= (editor
->mode
& ~mask
) | changes
;
4630 case EM_SETPASSWORDCHAR
:
4632 editor
->cPasswordMask
= wParam
;
4633 ME_RewrapRepaint(editor
);
4636 case EM_SETTARGETDEVICE
:
4639 BOOL
new = (lParam
== 0 && (editor
->styleFlags
& ES_MULTILINE
));
4640 if (editor
->nAvailWidth
|| editor
->bWordWrap
!= new)
4642 editor
->bWordWrap
= new;
4643 editor
->nAvailWidth
= 0; /* wrap to client area */
4644 ME_RewrapRepaint(editor
);
4647 int width
= max(0, lParam
);
4648 if ((editor
->styleFlags
& ES_MULTILINE
) &&
4649 (!editor
->bWordWrap
|| editor
->nAvailWidth
!= width
))
4651 editor
->nAvailWidth
= width
;
4652 editor
->bWordWrap
= TRUE
;
4653 ME_RewrapRepaint(editor
);
4655 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4660 *phresult
= S_FALSE
;
4666 static LRESULT
RichEditWndProc_common(HWND hWnd
, UINT msg
, WPARAM wParam
,
4667 LPARAM lParam
, BOOL unicode
)
4669 ME_TextEditor
*editor
;
4671 LRESULT lresult
= 0;
4673 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4674 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
);
4676 editor
= (ME_TextEditor
*)GetWindowLongPtrW(hWnd
, 0);
4679 if (msg
== WM_NCCREATE
)
4681 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4682 ITextHost
*texthost
;
4684 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4685 texthost
= ME_CreateTextHost(hWnd
, pcs
, FALSE
);
4686 return texthost
!= NULL
;
4690 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4702 hDC
= BeginPaint(editor
->hWnd
, &ps
);
4703 if (!editor
->bEmulateVersion10
|| (editor
->nEventMask
& ENM_UPDATE
))
4704 ME_SendOldNotify(editor
, EN_UPDATE
);
4705 /* Erase area outside of the formatting rectangle */
4706 if (ps
.rcPaint
.top
< editor
->rcFormat
.top
)
4709 rc
.bottom
= editor
->rcFormat
.top
;
4710 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4711 ps
.rcPaint
.top
= editor
->rcFormat
.top
;
4713 if (ps
.rcPaint
.bottom
> editor
->rcFormat
.bottom
) {
4715 rc
.top
= editor
->rcFormat
.bottom
;
4716 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4717 ps
.rcPaint
.bottom
= editor
->rcFormat
.bottom
;
4719 if (ps
.rcPaint
.left
< editor
->rcFormat
.left
) {
4721 rc
.right
= editor
->rcFormat
.left
;
4722 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4723 ps
.rcPaint
.left
= editor
->rcFormat
.left
;
4725 if (ps
.rcPaint
.right
> editor
->rcFormat
.right
) {
4727 rc
.left
= editor
->rcFormat
.right
;
4728 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4729 ps
.rcPaint
.right
= editor
->rcFormat
.right
;
4732 ME_PaintContent(editor
, hDC
, &ps
.rcPaint
);
4733 EndPaint(editor
->hWnd
, &ps
);
4738 HDC hDC
= (HDC
)wParam
;
4741 if (GetUpdateRect(editor
->hWnd
, &rc
, TRUE
))
4742 FillRect(hDC
, &rc
, editor
->hbrBackground
);
4748 const DWORD mask
= ECO_VERTICAL
| ECO_AUTOHSCROLL
| ECO_AUTOVSCROLL
|
4749 ECO_NOHIDESEL
| ECO_READONLY
| ECO_WANTRETURN
|
4751 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4752 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4753 dwStyle
= (dwStyle
& ~mask
) | (lresult
& mask
);
4754 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4757 case EM_SETREADONLY
:
4760 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4761 dwStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
4762 dwStyle
&= ~ES_READONLY
;
4764 dwStyle
|= ES_READONLY
;
4765 SetWindowLongW(hWnd
, GWL_STYLE
, dwStyle
);
4769 lresult
= ME_HandleMessage(editor
, msg
, wParam
, lParam
, unicode
, &hresult
);
4772 if (hresult
== S_FALSE
)
4773 lresult
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4775 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4776 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
, unicode
, lresult
);
4781 static LRESULT WINAPI
RichEditWndProcW(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4783 BOOL unicode
= TRUE
;
4785 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4786 if (msg
== WM_GETTEXT
&& (GetVersion() & 0x80000000))
4789 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, unicode
);
4792 static LRESULT WINAPI
RichEditWndProcA(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4794 return RichEditWndProc_common(hWnd
, msg
, wParam
, lParam
, FALSE
);
4797 /******************************************************************
4798 * RichEditANSIWndProc (RICHED20.10)
4800 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4802 return RichEditWndProcA(hWnd
, msg
, wParam
, lParam
);
4805 /******************************************************************
4806 * RichEdit10ANSIWndProc (RICHED20.9)
4808 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
4810 if (msg
== WM_NCCREATE
&& !GetWindowLongPtrW(hWnd
, 0))
4812 ITextHost
*texthost
;
4813 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
4815 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd
, pcs
->style
);
4816 texthost
= ME_CreateTextHost(hWnd
, pcs
, TRUE
);
4817 return texthost
!= NULL
;
4819 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
4822 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
4824 ITextHost_TxNotify(editor
->texthost
, nCode
, NULL
);
4827 /* Fill buffer with srcChars unicode characters from the start cursor.
4829 * buffer: destination buffer
4830 * buflen: length of buffer in characters excluding the NULL terminator.
4831 * start: start of editor text to copy into buffer.
4832 * srcChars: Number of characters to use from the editor text.
4833 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4835 * returns the number of characters written excluding the NULL terminator.
4837 * The written text is always NULL terminated.
4839 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int buflen
,
4840 const ME_Cursor
*start
, int srcChars
, BOOL bCRLF
,
4843 ME_DisplayItem
*pRun
, *pNextRun
;
4844 const WCHAR
*pStart
= buffer
;
4845 const WCHAR cr_lf
[] = {'\r', '\n', 0};
4849 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4850 if (editor
->bEmulateVersion10
) bCRLF
= FALSE
;
4854 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4856 nLen
= pRun
->member
.run
.len
- start
->nOffset
;
4857 str
= get_text( &pRun
->member
.run
, start
->nOffset
);
4859 while (srcChars
&& buflen
&& pNextRun
)
4861 int nFlags
= pRun
->member
.run
.nFlags
;
4863 if (bCRLF
&& nFlags
& MERF_ENDPARA
&& ~nFlags
& MERF_ENDCELL
)
4865 if (buflen
== 1) break;
4866 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4867 * EM_GETTEXTEX, however, this is done for copying text which
4868 * also uses this function. */
4869 srcChars
-= min(nLen
, srcChars
);
4873 nLen
= min(nLen
, srcChars
);
4877 nLen
= min(nLen
, buflen
);
4880 CopyMemory(buffer
, str
, sizeof(WCHAR
) * nLen
);
4885 pNextRun
= ME_FindItemFwd(pRun
, diRun
);
4887 nLen
= pRun
->member
.run
.len
;
4888 str
= get_text( &pRun
->member
.run
, 0 );
4890 /* append '\r' to the last paragraph. */
4891 if (pRun
->next
->type
== diTextEnd
&& bEOP
)
4897 return buffer
- pStart
;
4900 static BOOL
ME_RegisterEditorClass(HINSTANCE hInstance
)
4905 wcW
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4906 wcW
.lpfnWndProc
= RichEditWndProcW
;
4908 wcW
.cbWndExtra
= sizeof(ME_TextEditor
*);
4909 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4911 wcW
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_IBEAM
);
4912 wcW
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4913 wcW
.lpszMenuName
= NULL
;
4915 if (is_version_nt())
4917 wcW
.lpszClassName
= RICHEDIT_CLASS20W
;
4918 if (!RegisterClassW(&wcW
)) return FALSE
;
4919 wcW
.lpszClassName
= MSFTEDIT_CLASS
;
4920 if (!RegisterClassW(&wcW
)) return FALSE
;
4924 /* WNDCLASSA/W have the same layout */
4925 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit20W";
4926 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4927 wcW
.lpszClassName
= (LPCWSTR
)"RichEdit50W";
4928 if (!RegisterClassA((WNDCLASSA
*)&wcW
)) return FALSE
;
4931 wcA
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
| CS_GLOBALCLASS
;
4932 wcA
.lpfnWndProc
= RichEditWndProcA
;
4934 wcA
.cbWndExtra
= sizeof(ME_TextEditor
*);
4935 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
4937 wcA
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_IBEAM
);
4938 wcA
.hbrBackground
= GetStockObject(NULL_BRUSH
);
4939 wcA
.lpszMenuName
= NULL
;
4940 wcA
.lpszClassName
= RICHEDIT_CLASS20A
;
4941 if (!RegisterClassA(&wcA
)) return FALSE
;
4942 wcA
.lpszClassName
= "RichEdit50A";
4943 if (!RegisterClassA(&wcA
)) return FALSE
;
4948 static LRESULT WINAPI
REComboWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4949 /* FIXME: Not implemented */
4950 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4951 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4952 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4955 static LRESULT WINAPI
REListWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
4956 /* FIXME: Not implemented */
4957 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4958 hWnd
, msg
, get_msg_name(msg
), wParam
, lParam
);
4959 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4962 /******************************************************************
4963 * REExtendedRegisterClass (RICHED20.8)
4965 * FIXME undocumented
4966 * Need to check for errors and implement controls and callbacks
4968 LRESULT WINAPI
REExtendedRegisterClass(void)
4973 FIXME("semi stub\n");
4977 wcW
.hInstance
= NULL
;
4980 wcW
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1);
4981 wcW
.lpszMenuName
= NULL
;
4983 if (!ME_ListBoxRegistered
)
4985 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
;
4986 wcW
.lpfnWndProc
= REListWndProc
;
4987 wcW
.lpszClassName
= REListBox20W
;
4988 if (RegisterClassW(&wcW
)) ME_ListBoxRegistered
= TRUE
;
4991 if (!ME_ComboBoxRegistered
)
4993 wcW
.style
= CS_PARENTDC
| CS_DBLCLKS
| CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
4994 wcW
.lpfnWndProc
= REComboWndProc
;
4995 wcW
.lpszClassName
= REComboBox20W
;
4996 if (RegisterClassW(&wcW
)) ME_ComboBoxRegistered
= TRUE
;
5000 if (ME_ListBoxRegistered
)
5002 if (ME_ComboBoxRegistered
)
5008 static int wchar_comp( const void *key
, const void *elem
)
5010 return *(const WCHAR
*)key
- *(const WCHAR
*)elem
;
5013 /* neutral characters end the url if the next non-neutral character is a space character,
5014 otherwise they are included in the url. */
5015 static BOOL
isurlneutral( WCHAR c
)
5017 /* NB this list is sorted */
5018 static const WCHAR neutral_chars
[] = {'!','\"','\'','(',')',',','-','.',':',';','<','>','?','[',']','{','}'};
5020 /* Some shortcuts */
5021 if (isalnum( c
)) return FALSE
;
5022 if (c
> neutral_chars
[sizeof(neutral_chars
) / sizeof(neutral_chars
[0]) - 1]) return FALSE
;
5024 return !!bsearch( &c
, neutral_chars
, sizeof(neutral_chars
) / sizeof(neutral_chars
[0]),
5025 sizeof(c
), wchar_comp
);
5029 * This proc takes a selection, and scans it forward in order to select the span
5030 * of a possible URL candidate. A possible URL candidate must start with isalnum
5031 * or one of the following special characters: *|/\+%#@ and must consist entirely
5032 * of the characters allowed to start the URL, plus : (colon) which may occur
5033 * at most once, and not at either end.
5035 static BOOL
ME_FindNextURLCandidate(ME_TextEditor
*editor
,
5036 const ME_Cursor
*start
,
5038 ME_Cursor
*candidate_min
,
5039 ME_Cursor
*candidate_max
)
5041 ME_Cursor cursor
= *start
, neutral_end
, space_end
;
5042 BOOL candidateStarted
= FALSE
, quoted
= FALSE
;
5047 WCHAR
*str
= get_text( &cursor
.pRun
->member
.run
, 0 );
5048 int run_len
= cursor
.pRun
->member
.run
.len
;
5050 nChars
-= run_len
- cursor
.nOffset
;
5052 /* Find start of candidate */
5053 if (!candidateStarted
)
5055 while (cursor
.nOffset
< run_len
)
5057 c
= str
[cursor
.nOffset
];
5058 if (!isspaceW( c
) && !isurlneutral( c
))
5060 *candidate_min
= cursor
;
5061 candidateStarted
= TRUE
;
5062 neutral_end
.pPara
= NULL
;
5063 space_end
.pPara
= NULL
;
5067 quoted
= (c
== '<');
5072 /* Find end of candidate */
5073 if (candidateStarted
)
5075 while (cursor
.nOffset
< run_len
)
5077 c
= str
[cursor
.nOffset
];
5080 if (quoted
&& c
!= '\r')
5082 if (!space_end
.pPara
)
5084 if (neutral_end
.pPara
)
5085 space_end
= neutral_end
;
5093 else if (isurlneutral( c
))
5095 if (quoted
&& c
== '>')
5097 neutral_end
.pPara
= NULL
;
5098 space_end
.pPara
= NULL
;
5101 if (!neutral_end
.pPara
)
5102 neutral_end
= cursor
;
5105 neutral_end
.pPara
= NULL
;
5112 if (!ME_NextRun(&cursor
.pPara
, &cursor
.pRun
))
5117 if (candidateStarted
)
5119 if (space_end
.pPara
)
5120 *candidate_max
= space_end
;
5121 else if (neutral_end
.pPara
)
5122 *candidate_max
= neutral_end
;
5124 *candidate_max
= cursor
;
5127 *candidate_max
= *candidate_min
= cursor
;
5132 * This proc evaluates the selection and returns TRUE if it can be considered an URL
5134 static BOOL
ME_IsCandidateAnURL(ME_TextEditor
*editor
, const ME_Cursor
*start
, int nChars
)
5136 #define MAX_PREFIX_LEN 9
5138 const WCHAR text
[MAX_PREFIX_LEN
];
5141 {{'p','r','o','s','p','e','r','o',':'}, 9},
5142 {{'t','e','l','n','e','t',':'}, 7},
5143 {{'g','o','p','h','e','r',':'}, 7},
5144 {{'m','a','i','l','t','o',':'}, 7},
5145 {{'h','t','t','p','s',':'}, 6},
5146 {{'f','i','l','e',':'}, 5},
5147 {{'n','e','w','s',':'}, 5},
5148 {{'w','a','i','s',':'}, 5},
5149 {{'n','n','t','p',':'}, 5},
5150 {{'h','t','t','p',':'}, 5},
5151 {{'w','w','w','.'}, 4},
5152 {{'f','t','p',':'}, 4},
5154 WCHAR bufferW
[MAX_PREFIX_LEN
+ 1];
5157 ME_GetTextW(editor
, bufferW
, MAX_PREFIX_LEN
, start
, nChars
, FALSE
, FALSE
);
5158 for (i
= 0; i
< sizeof(prefixes
) / sizeof(*prefixes
); i
++)
5160 if (nChars
< prefixes
[i
].length
) continue;
5161 if (!memcmp(prefixes
[i
].text
, bufferW
, prefixes
[i
].length
* sizeof(WCHAR
)))
5165 #undef MAX_PREFIX_LEN
5169 * This proc walks through the indicated selection and evaluates whether each
5170 * section identified by ME_FindNextURLCandidate and in-between sections have
5171 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5172 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5174 * Since this function can cause runs to be split, do not depend on the value
5175 * of the start cursor at the end of the function.
5177 * nChars may be set to INT_MAX to update to the end of the text.
5179 * Returns TRUE if at least one section was modified.
5181 static BOOL
ME_UpdateLinkAttribute(ME_TextEditor
*editor
, ME_Cursor
*start
, int nChars
)
5183 BOOL modified
= FALSE
;
5184 ME_Cursor startCur
= *start
;
5186 if (!editor
->AutoURLDetect_bEnable
) return FALSE
;
5191 ME_Cursor candidateStart
, candidateEnd
;
5193 if (ME_FindNextURLCandidate(editor
, &startCur
, nChars
,
5194 &candidateStart
, &candidateEnd
))
5196 /* Section before candidate is not an URL */
5197 int cMin
= ME_GetCursorOfs(&candidateStart
);
5198 int cMax
= ME_GetCursorOfs(&candidateEnd
);
5200 if (!ME_IsCandidateAnURL(editor
, &candidateStart
, cMax
- cMin
))
5201 candidateStart
= candidateEnd
;
5202 nChars
-= cMax
- ME_GetCursorOfs(&startCur
);
5206 /* No more candidates until end of selection */
5210 if (startCur
.pRun
!= candidateStart
.pRun
||
5211 startCur
.nOffset
!= candidateStart
.nOffset
)
5213 /* CFE_LINK effect should be consistently unset */
5214 link
.cbSize
= sizeof(link
);
5215 ME_GetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5216 if (!(link
.dwMask
& CFM_LINK
) || (link
.dwEffects
& CFE_LINK
))
5218 /* CFE_LINK must be unset from this range */
5219 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5220 link
.cbSize
= sizeof(link
);
5221 link
.dwMask
= CFM_LINK
;
5223 ME_SetCharFormat(editor
, &startCur
, &candidateStart
, &link
);
5224 /* Update candidateEnd since setting character formats may split
5225 * runs, which can cause a cursor to be at an invalid offset within
5227 while (candidateEnd
.nOffset
>= candidateEnd
.pRun
->member
.run
.len
)
5229 candidateEnd
.nOffset
-= candidateEnd
.pRun
->member
.run
.len
;
5230 candidateEnd
.pRun
= ME_FindItemFwd(candidateEnd
.pRun
, diRun
);
5235 if (candidateStart
.pRun
!= candidateEnd
.pRun
||
5236 candidateStart
.nOffset
!= candidateEnd
.nOffset
)
5238 /* CFE_LINK effect should be consistently set */
5239 link
.cbSize
= sizeof(link
);
5240 ME_GetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5241 if (!(link
.dwMask
& CFM_LINK
) || !(link
.dwEffects
& CFE_LINK
))
5243 /* CFE_LINK must be set on this range */
5244 memset(&link
, 0, sizeof(CHARFORMAT2W
));
5245 link
.cbSize
= sizeof(link
);
5246 link
.dwMask
= CFM_LINK
;
5247 link
.dwEffects
= CFE_LINK
;
5248 ME_SetCharFormat(editor
, &candidateStart
, &candidateEnd
, &link
);
5252 startCur
= candidateEnd
;
5253 } while (nChars
> 0);