2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 API implementation status:
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
41 - EM_GETCHARFORMAT (partly done)
43 - EM_GETFIRSTVISIBLELINE
44 - EM_GETIMECOLOR 1.0asian
45 - EM_GETIMECOMPMODE 2.0
46 - EM_GETIMEOPTIONS 1.0asian
48 - EM_GETLANGOPTIONS 2.0
51 - EM_GETLINECOUNT returns number of rows, not of paragraphs
56 - EM_GETPUNCTUATION 1.0asian
60 + EM_GETSELTEXT (ANSI&Unicode)
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
66 - EM_GETWORDBREAKPROCEX
67 - EM_GETWORDWRAPMODE 1.0asian
78 + EM_REPLACESEL (proper style?) ANSI&Unicode
83 - EM_SETCHARFORMAT (partly done, no ANSI)
84 + EM_SETEVENTMASK (few notifications supported)
85 - EM_SETIMECOLOR 1.0asian
86 - EM_SETIMEOPTIONS 1.0asian
87 - EM_SETLANGOPTIONS 2.0
89 + EM_SETMODIFY (not sure if implementation is correct)
93 - EM_SETPUNCTUATION 1.0asian
94 + EM_SETREADONLY no beep on modification attempt
96 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
100 - EM_SETUNDOLIMIT 2.0
101 - EM_SETWORDBREAKPROC
102 - EM_SETWORDBREAKPROCEX
103 - EM_SETWORDWRAPMODE 1.0asian
104 - EM_STOPGROUPTYPING 2.0
105 + EM_STREAMIN (can't fall back to text when the RTF isn't really RTF)
112 + WM_GETDLGCODE (the current implementation is incomplete)
113 + WM_GETTEXT (ANSI&Unicode)
114 + WM_GETTEXTLENGTH (ANSI version sucks)
117 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
119 - WM_STYLECHANGED (things like read-only flag)
124 * EN_CHANGE (sent from the wrong place)
141 * EN_UPDATE (sent from the wrong place)
149 - ES_DISABLENOSCROLL (scrollbar is always visible)
150 - ES_EX_NOCALLOLEINIT
152 - ES_MULTILINE (currently single line controls aren't supported)
154 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
160 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
167 * RICHED20 TODO (incomplete):
169 * - messages/styles/notifications listed above
171 * - add remaining CHARFORMAT/PARAFORMAT fields
172 * - right/center align should strip spaces from the beginning
173 * - more advanced navigation (Ctrl-arrows)
175 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
176 * - COM interface (looks like a major pain in the TODO list)
177 * - calculate heights of pictures (half-done)
178 * - horizontal scrolling (not even started)
179 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
181 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
182 * - italic caret with italic fonts
184 * - most notifications aren't sent at all (the most important ones are)
185 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
186 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
187 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
189 * Bugs that are probably fixed, but not so easy to verify:
190 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
191 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
192 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
193 * - caret shouldn't be displayed when selection isn't empty
194 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
195 * - undo for setting default format (done, might be buggy)
196 * - styles might be not released properly (looks like they work like charm, but who knows?
204 #define NO_SHLWAPI_STREAM
209 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
212 HANDLE me_heap
= NULL
;
214 ME_TextBuffer
*ME_MakeText() {
216 ME_TextBuffer
*buf
= ALLOC_OBJ(ME_TextBuffer
);
218 ME_DisplayItem
*p1
= ME_MakeDI(diTextStart
);
219 ME_DisplayItem
*p2
= ME_MakeDI(diTextEnd
);
225 p1
->member
.para
.next_para
= p2
;
226 p2
->member
.para
.prev_para
= p1
;
227 p2
->member
.para
.nCharOfs
= 0;
231 buf
->pCharStyle
= NULL
;
237 static LRESULT
ME_StreamInText(ME_TextEditor
*editor
, DWORD dwFormat
, ME_InStream
*stream
, ME_Style
*style
)
239 WCHAR wszText
[STREAMIN_BUFFER_SIZE
+1];
242 TRACE("%08lx %p\n", dwFormat
, stream
);
249 ME_StreamInFill(stream
);
250 if (stream
->editstream
->dwError
)
256 if (!(dwFormat
& SF_UNICODE
))
258 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
259 nWideChars
= MultiByteToWideChar(CP_ACP
, 0, stream
->buffer
, stream
->dwSize
, wszText
, STREAMIN_BUFFER_SIZE
);
264 nWideChars
= stream
->dwSize
>> 1;
265 pText
= (WCHAR
*)stream
->buffer
;
268 ME_InsertTextFromCursor(editor
, 0, pText
, nWideChars
, style
);
269 if (stream
->dwSize
< STREAMIN_BUFFER_SIZE
)
272 ME_CommitUndo(editor
);
277 void ME_RTFCharAttrHook(RTF_Info
*info
)
280 fmt
.cbSize
= sizeof(fmt
);
283 switch(info
->rtfMinor
)
286 /* FIXME add more flags once they're implemented */
287 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
| CFM_STRIKEOUT
| CFM_COLOR
| CFM_BACKCOLOR
| CFM_SIZE
| CFM_WEIGHT
;
288 fmt
.dwEffects
= CFE_AUTOCOLOR
| CFE_AUTOBACKCOLOR
;
289 fmt
.yHeight
= 12*20; /* 12pt */
293 fmt
.dwMask
= CFM_BOLD
;
294 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
297 fmt
.dwMask
= CFM_ITALIC
;
298 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
301 fmt
.dwMask
= CFM_UNDERLINE
;
302 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
305 fmt
.dwMask
= CFM_STRIKEOUT
;
306 fmt
.dwEffects
= info
->rtfParam
? fmt
.dwMask
: 0;
309 fmt
.dwMask
= CFM_BACKCOLOR
;
311 if (info
->rtfParam
== 0)
312 fmt
.dwEffects
= CFE_AUTOBACKCOLOR
;
313 else if (info
->rtfParam
!= rtfNoParam
)
315 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
316 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
320 fmt
.dwMask
= CFM_COLOR
;
322 if (info
->rtfParam
== 0)
323 fmt
.dwEffects
= CFE_AUTOCOLOR
;
324 else if (info
->rtfParam
!= rtfNoParam
)
326 RTFColor
*c
= RTFGetColor(info
, info
->rtfParam
);
327 fmt
.crTextColor
= (c
->rtfCBlue
<<16)|(c
->rtfCGreen
<<8)|(c
->rtfCRed
);
331 if (info
->rtfParam
!= rtfNoParam
)
333 RTFFont
*f
= RTFGetFont(info
, info
->rtfParam
);
336 MultiByteToWideChar(CP_ACP
, 0, f
->rtfFName
, -1, fmt
.szFaceName
, sizeof(fmt
.szFaceName
)/sizeof(WCHAR
));
337 fmt
.szFaceName
[sizeof(fmt
.szFaceName
)/sizeof(WCHAR
)-1] = '\0';
338 fmt
.bCharSet
= f
->rtfFCharSet
;
339 fmt
.dwMask
= CFM_FACE
| CFM_CHARSET
;
344 fmt
.dwMask
= CFM_SIZE
;
345 if (info
->rtfParam
!= rtfNoParam
)
346 fmt
.yHeight
= info
->rtfParam
*10;
351 RTFFlushOutputBuffer(info
);
352 /* FIXME too slow ? how come ? */
353 style2
= ME_ApplyStyle(info
->style
, &fmt
);
354 ME_ReleaseStyle(info
->style
);
355 info
->style
= style2
;
359 void ME_RTFParAttrHook(RTF_Info
*info
)
362 fmt
.cbSize
= sizeof(fmt
);
365 switch(info
->rtfMinor
)
367 case rtfParDef
: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
368 fmt
.dwMask
= PFM_ALIGNMENT
;
369 fmt
.wAlignment
= PFA_LEFT
;
373 fmt
.dwMask
= PFM_ALIGNMENT
;
374 fmt
.wAlignment
= PFA_LEFT
;
377 fmt
.dwMask
= PFM_ALIGNMENT
;
378 fmt
.wAlignment
= PFA_RIGHT
;
381 fmt
.dwMask
= PFM_ALIGNMENT
;
382 fmt
.wAlignment
= PFA_CENTER
;
386 RTFFlushOutputBuffer(info
);
387 /* FIXME too slow ? how come ?*/
388 ME_SetSelectionParaFormat(info
->editor
, &fmt
);
392 void ME_RTFReadHook(RTF_Info
*info
) {
393 switch(info
->rtfClass
)
396 switch(info
->rtfMajor
)
399 if (info
->stackTop
< maxStack
) {
400 memcpy(&info
->stack
[info
->stackTop
].fmt
, &info
->style
->fmt
, sizeof(CHARFORMAT2W
));
401 info
->stack
[info
->stackTop
].codePage
= info
->codePage
;
402 info
->stack
[info
->stackTop
].unicodeLength
= info
->unicodeLength
;
409 RTFFlushOutputBuffer(info
);
411 /* FIXME too slow ? how come ? */
412 s
= ME_ApplyStyle(info
->style
, &info
->stack
[info
->stackTop
].fmt
);
413 ME_ReleaseStyle(info
->style
);
415 info
->codePage
= info
->stack
[info
->stackTop
].codePage
;
416 info
->unicodeLength
= info
->stack
[info
->stackTop
].unicodeLength
;
422 switch(info
->rtfMajor
)
425 ME_RTFCharAttrHook(info
);
428 ME_RTFParAttrHook(info
);
436 ME_StreamInFill(ME_InStream
*stream
)
438 stream
->editstream
->dwError
= stream
->editstream
->pfnCallback(stream
->editstream
->dwCookie
,
440 sizeof(stream
->buffer
),
445 static LRESULT
ME_StreamIn(ME_TextEditor
*editor
, DWORD format
, EDITSTREAM
*stream
)
449 int from
, to
, to2
, nUndoMode
;
451 int nEventMask
= editor
->nEventMask
;
452 ME_InStream inStream
;
454 TRACE("%p %p\n", stream
, editor
->hWnd
);
455 editor
->nEventMask
= 0;
457 ME_GetSelection(editor
, &from
, &to
);
458 if (format
& SFF_SELECTION
) {
459 style
= ME_GetSelectionInsertStyle(editor
);
461 ME_InternalDeleteText(editor
, from
, to
-from
);
464 style
= editor
->pBuffer
->pDefaultStyle
;
465 ME_AddRefStyle(style
);
466 SendMessageA(editor
->hWnd
, EM_SETSEL
, 0, 0);
467 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
469 ME_ClearTempStyle(editor
);
470 /* FIXME restore default paragraph formatting ! */
473 nUndoMode
= editor
->nUndoMode
;
474 editor
->nUndoMode
= umIgnore
;
476 inStream
.editstream
= stream
;
477 inStream
.editstream
->dwError
= 0;
483 /* Check if it's really RTF, and if it is not, use plain text */
484 ME_StreamInFill(&inStream
);
485 if (!inStream
.editstream
->dwError
)
487 if (strncmp(inStream
.buffer
, "{\\rtf1", 6) && strncmp(inStream
.buffer
, "{\\urtf", 6))
495 if (!inStream
.editstream
->dwError
)
497 if (format
& SF_RTF
) {
498 /* setup the RTF parser */
499 memset(&parser
, 0, sizeof parser
);
500 RTFSetEditStream(&parser
, &inStream
);
501 parser
.rtfFormat
= format
&(SF_TEXT
|SF_RTF
);
502 parser
.hwndEdit
= editor
->hWnd
;
503 parser
.editor
= editor
;
504 parser
.style
= style
;
507 RTFSetReadHook(&parser
, ME_RTFReadHook
);
512 RTFFlushOutputBuffer(&parser
);
515 style
= parser
.style
;
517 else if (format
& SF_TEXT
)
518 ME_StreamInText(editor
, format
, &inStream
, style
);
520 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
521 ME_GetSelection(editor
, &to
, &to2
);
522 /* put the cursor at the top */
523 if (!(format
& SFF_SELECTION
))
524 SendMessageA(editor
->hWnd
, EM_SETSEL
, 0, 0);
527 /* FIXME where to put cursor now ? */
531 editor
->nUndoMode
= nUndoMode
;
532 pUI
= ME_AddUndoItem(editor
, diUndoDeleteRun
, NULL
);
533 TRACE("from %d to %d\n", from
, to
);
534 if (pUI
&& from
< to
)
539 ME_CommitUndo(editor
);
540 ME_ReleaseStyle(style
);
541 editor
->nEventMask
= nEventMask
;
542 InvalidateRect(editor
->hWnd
, NULL
, TRUE
);
543 ME_UpdateRepaint(editor
);
544 if (!(format
& SFF_SELECTION
)) {
545 ME_ClearTempStyle(editor
);
547 ME_MoveCaret(editor
);
548 ME_SendSelChange(editor
);
555 ME_FindItemAtOffset(ME_TextEditor
*editor
, ME_DIType nItemType
, int nOffset
, int *nItemOffset
)
557 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
559 while (item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= nOffset
)
560 item
= ME_FindItemFwd(item
, diParagraph
);
565 nOffset
-= item
->member
.para
.nCharOfs
;
566 if (nItemType
== diParagraph
) {
568 *nItemOffset
= nOffset
;
573 item
= ME_FindItemFwd(item
, diRun
);
574 } while (item
&& (item
->member
.run
.nCharOfs
+ ME_StrLen(item
->member
.run
.strText
) <= nOffset
));
576 nOffset
-= item
->member
.run
.nCharOfs
;
578 *nItemOffset
= nOffset
;
584 ME_TextEditor
*ME_MakeEditor(HWND hWnd
) {
585 ME_TextEditor
*ed
= ALLOC_OBJ(ME_TextEditor
);
589 ed
->pBuffer
= ME_MakeText();
591 ME_MakeFirstParagraph(hDC
, ed
->pBuffer
);
592 ReleaseDC(hWnd
, hDC
);
593 ed
->bCaretShown
= FALSE
;
595 ed
->pCursors
= ALLOC_N_OBJ(ME_Cursor
, ed
->nCursors
);
596 ed
->pCursors
[0].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
597 ed
->pCursors
[0].nOffset
= 0;
598 ed
->pCursors
[1].pRun
= ME_FindItemFwd(ed
->pBuffer
->pFirst
, diRun
);
599 ed
->pCursors
[1].nOffset
= 0;
600 ed
->nLastTotalLength
= ed
->nTotalLength
= 0;
603 ed
->rgbBackColor
= -1;
604 ed
->bCaretAtEnd
= FALSE
;
607 ed
->pUndoStack
= ed
->pRedoStack
= NULL
;
608 ed
->nUndoMode
= umAddToUndo
;
610 ed
->nLastSelStart
= ed
->nLastSelEnd
= 0;
612 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
614 ed
->pFontCache
[i
].nRefs
= 0;
615 ed
->pFontCache
[i
].nAge
= 0;
616 ed
->pFontCache
[i
].hFont
= NULL
;
618 ME_CheckCharOffsets(ed
);
622 typedef struct tagME_GlobalDestStruct
626 } ME_GlobalDestStruct
;
628 static DWORD CALLBACK
ME_AppendToHGLOBAL(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
630 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
634 nMaxSize
= GlobalSize(pData
->hData
);
635 if (pData
->nLength
+cb
+1 >= cb
)
637 /* round up to 2^17 */
638 int nNewSize
= (((nMaxSize
+cb
+1)|0x1FFFF)+1) & 0xFFFE0000;
639 pData
->hData
= GlobalReAlloc(pData
->hData
, nNewSize
, 0);
641 pDest
= (BYTE
*)GlobalLock(pData
->hData
);
642 memcpy(pDest
+ pData
->nLength
, lpBuff
, cb
);
643 pData
->nLength
+= cb
;
644 pDest
[pData
->nLength
] = '\0';
645 GlobalUnlock(pData
->hData
);
651 static DWORD CALLBACK
ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
653 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
658 pDest
= (WORD
*)lpBuff
;
659 pSrc
= (WORD
*)GlobalLock(pData
->hData
);
660 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
661 pDest
[i
] = pSrc
[pData
->nLength
+i
];
665 GlobalUnlock(pData
->hData
);
669 static DWORD CALLBACK
ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie
, LPBYTE lpBuff
, LONG cb
, LONG
*pcb
)
671 ME_GlobalDestStruct
*pData
= (ME_GlobalDestStruct
*)dwCookie
;
676 pSrc
= (BYTE
*)GlobalLock(pData
->hData
);
677 for (i
= 0; i
<cb
&& pSrc
[pData
->nLength
+i
]; i
++) {
678 pDest
[i
] = pSrc
[pData
->nLength
+i
];
682 GlobalUnlock(pData
->hData
);
686 void ME_DestroyEditor(ME_TextEditor
*editor
)
688 ME_DisplayItem
*pFirst
= editor
->pBuffer
->pFirst
;
689 ME_DisplayItem
*p
= pFirst
, *pNext
= NULL
;
692 ME_ClearTempStyle(editor
);
693 ME_EmptyUndoStack(editor
);
696 ME_DestroyDisplayItem(p
);
699 ME_ReleaseStyle(editor
->pBuffer
->pDefaultStyle
);
700 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
702 if (editor
->pFontCache
[i
].hFont
)
703 DeleteObject(editor
->pFontCache
[i
].hFont
);
709 #define UNSUPPORTED_MSG(e) \
711 FIXME(#e ": stub\n"); \
712 return DefWindowProcW(hWnd, msg, wParam, lParam);
714 /******************************************************************
715 * RichEditANSIWndProc (RICHED20.10)
717 LRESULT WINAPI
RichEditANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
) {
721 ME_TextEditor
*editor
= (ME_TextEditor
*)GetWindowLongW(hWnd
, 0);
722 TRACE("msg %d %08x %08lx\n", msg
, wParam
, lParam
);
725 UNSUPPORTED_MSG(EM_AUTOURLDETECT
)
726 UNSUPPORTED_MSG(EM_CHARFROMPOS
)
727 UNSUPPORTED_MSG(EM_DISPLAYBAND
)
728 UNSUPPORTED_MSG(EM_EXLIMITTEXT
)
729 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR
)
730 UNSUPPORTED_MSG(EM_FINDTEXT
)
731 UNSUPPORTED_MSG(EM_FINDTEXTEX
)
732 UNSUPPORTED_MSG(EM_FINDWORDBREAK
)
733 UNSUPPORTED_MSG(EM_FMTLINES
)
734 UNSUPPORTED_MSG(EM_FORMATRANGE
)
735 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE
)
736 UNSUPPORTED_MSG(EM_GETIMECOMPMODE
)
737 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
738 UNSUPPORTED_MSG(EM_GETLANGOPTIONS
)
739 UNSUPPORTED_MSG(EM_GETLIMITTEXT
)
740 UNSUPPORTED_MSG(EM_GETLINE
)
741 UNSUPPORTED_MSG(EM_GETLINECOUNT
)
742 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
743 UNSUPPORTED_MSG(EM_GETOPTIONS
)
744 UNSUPPORTED_MSG(EM_GETRECT
)
745 UNSUPPORTED_MSG(EM_GETREDONAME
)
746 UNSUPPORTED_MSG(EM_GETTEXTMODE
)
747 UNSUPPORTED_MSG(EM_GETUNDONAME
)
748 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC
)
749 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX
)
750 UNSUPPORTED_MSG(EM_HIDESELECTION
)
751 UNSUPPORTED_MSG(EM_LIMITTEXT
) /* also known as EM_SETLIMITTEXT */
752 UNSUPPORTED_MSG(EM_LINEFROMCHAR
)
753 UNSUPPORTED_MSG(EM_LINEINDEX
)
754 UNSUPPORTED_MSG(EM_LINELENGTH
)
755 UNSUPPORTED_MSG(EM_LINESCROLL
)
756 UNSUPPORTED_MSG(EM_PASTESPECIAL
)
757 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
758 UNSUPPORTED_MSG(EM_REQUESTRESIZE
)
759 UNSUPPORTED_MSG(EM_SCROLL
)
760 UNSUPPORTED_MSG(EM_SCROLLCARET
)
761 UNSUPPORTED_MSG(EM_SELECTIONTYPE
)
762 UNSUPPORTED_MSG(EM_SETLANGOPTIONS
)
763 UNSUPPORTED_MSG(EM_SETOLECALLBACK
)
764 UNSUPPORTED_MSG(EM_SETOPTIONS
)
765 UNSUPPORTED_MSG(EM_SETRECT
)
766 UNSUPPORTED_MSG(EM_SETRECTNP
)
767 UNSUPPORTED_MSG(EM_SETTARGETDEVICE
)
768 UNSUPPORTED_MSG(EM_SETTEXTMODE
)
769 UNSUPPORTED_MSG(EM_SETUNDOLIMIT
)
770 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC
)
771 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX
)
772 UNSUPPORTED_MSG(WM_SETFONT
)
773 UNSUPPORTED_MSG(WM_STYLECHANGING
)
774 UNSUPPORTED_MSG(WM_STYLECHANGED
)
775 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
777 /* Messages specific to Richedit controls */
780 return ME_StreamIn(editor
, wParam
, (EDITSTREAM
*)lParam
);
782 return ME_StreamOut(editor
, wParam
, (EDITSTREAM
*)lParam
);
785 UINT code
= DLGC_WANTCHARS
|DLGC_WANTARROWS
;
786 if (GetWindowLongW(hWnd
, GWL_STYLE
)&ES_WANTRETURN
)
787 code
|= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
792 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
793 editor
= ME_MakeEditor(hWnd
);
794 SetWindowLongW(hWnd
, 0, (long)editor
);
795 pcs
= 0; /* ignore */
798 case EM_EMPTYUNDOBUFFER
:
799 ME_EmptyUndoStack(editor
);
803 ME_GetSelection(editor
, (int *)wParam
, (int *)lParam
);
804 if (!((wParam
|lParam
) & 0xFFFF0000))
805 return (lParam
<<16)|wParam
;
810 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
811 ME_GetSelection(editor
, (int *)&pRange
->cpMin
, (int *)&pRange
->cpMax
);
815 return editor
->pUndoStack
!= NULL
;
817 return editor
->pRedoStack
!= NULL
;
826 ME_SetSelection(editor
, wParam
, lParam
);
828 ME_SendSelChange(editor
);
833 CHARRANGE
*pRange
= (CHARRANGE
*)lParam
;
834 ME_SetSelection(editor
, pRange
->cpMin
, pRange
->cpMax
);
837 ME_SendSelChange(editor
);
840 case EM_SETBKGNDCOLOR
:
842 LRESULT lColor
= ME_GetBackColor(editor
);
844 editor
->rgbBackColor
= -1;
846 editor
->rgbBackColor
= lParam
;
847 InvalidateRect(hWnd
, NULL
, TRUE
);
852 return editor
->nModifyStep
== 0 ? 0 : 1;
856 editor
->nModifyStep
= 0x80000000;
858 editor
->nModifyStep
= 0;
864 long nStyle
= GetWindowLongW(hWnd
, GWL_STYLE
);
866 nStyle
|= ES_READONLY
;
868 nStyle
&= ~ES_READONLY
;
869 SetWindowLongW(hWnd
, GWL_STYLE
, nStyle
);
873 case EM_SETEVENTMASK
:
874 editor
->nEventMask
= lParam
;
876 case EM_GETEVENTMASK
:
877 return editor
->nEventMask
;
878 case EM_SETCHARFORMAT
:
880 CHARFORMAT2W buf
, *p
;
881 BOOL bRepaint
= TRUE
;
882 p
= ME_ToCF2W(&buf
, (CHARFORMAT2W
*)lParam
);
884 ME_SetDefaultCharFormat(editor
, p
);
885 else if (wParam
== (SCF_WORD
| SCF_SELECTION
))
886 FIXME("word selection not supported\n");
887 else if (wParam
== SCF_ALL
)
888 ME_SetCharFormat(editor
, 0, ME_GetTextLength(editor
), p
);
891 ME_GetSelection(editor
, &from
, &to
);
892 bRepaint
= (from
!= to
);
893 ME_SetSelectionCharFormat(editor
, p
);
895 ME_CommitUndo(editor
);
897 ME_UpdateRepaint(editor
);
900 case EM_GETCHARFORMAT
:
903 tmp
.cbSize
= sizeof(tmp
);
905 ME_GetDefaultCharFormat(editor
, &tmp
);
907 ME_GetSelectionCharFormat(editor
, &tmp
);
908 ME_CopyToCFAny((CHARFORMAT2W
*)lParam
, &tmp
);
911 case EM_SETPARAFORMAT
:
912 ME_SetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
913 ME_UpdateRepaint(editor
);
914 ME_CommitUndo(editor
);
916 case EM_GETPARAFORMAT
:
917 ME_GetSelectionParaFormat(editor
, (PARAFORMAT2
*)lParam
);
922 ME_GetSelection(editor
, &from
, &to
);
923 ME_InternalDeleteText(editor
, from
, to
-from
);
924 ME_CommitUndo(editor
);
925 ME_UpdateRepaint(editor
);
932 LPWSTR wszText
= ME_ToUnicode(hWnd
, (void *)lParam
);
933 size_t len
= lstrlenW(wszText
);
934 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText
));
936 ME_GetSelection(editor
, &from
, &to
);
937 style
= ME_GetSelectionInsertStyle(editor
);
938 ME_InternalDeleteText(editor
, from
, to
-from
);
939 ME_InsertTextFromCursor(editor
, 0, wszText
, len
, style
);
940 ME_ReleaseStyle(style
);
941 ME_EndToUnicode(hWnd
, wszText
);
942 /* drop temporary style if line end */
943 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
944 if (len
>0 && wszText
[len
-1] == '\n')
945 ME_ClearTempStyle(editor
);
947 ME_CommitUndo(editor
);
949 ME_EmptyUndoStack(editor
);
950 ME_UpdateRepaint(editor
);
955 LPWSTR wszText
= ME_ToUnicode(hWnd
, (void *)lParam
);
956 TRACE("WM_SETTEXT - %s\n", (char *)(wszText
)); /* debugstr_w() */
957 ME_InternalDeleteText(editor
, 0, ME_GetTextLength(editor
));
958 /* uses default style! */
959 ME_InsertTextFromCursor(editor
, 0, wszText
, -1, editor
->pBuffer
->pDefaultStyle
);
960 ME_EndToUnicode(hWnd
, wszText
);
961 ME_CommitUndo(editor
);
962 ME_EmptyUndoStack(editor
);
963 ME_UpdateRepaint(editor
);
968 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
969 if (IsClipboardFormatAvailable(nRTFFormat
))
971 if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
979 ME_GlobalDestStruct gds
;
980 UINT nRTFFormat
= RegisterClipboardFormatA("Rich Text Format");
983 if (IsClipboardFormatAvailable(nRTFFormat
))
984 cf
= nRTFFormat
, dwFormat
= SF_RTF
;
985 else if (IsClipboardFormatAvailable(CF_UNICODETEXT
))
986 cf
= CF_UNICODETEXT
, dwFormat
= SF_TEXT
|SF_UNICODE
;
990 if (!OpenClipboard(hWnd
))
992 gds
.hData
= GetClipboardData(cf
);
994 es
.dwCookie
= (DWORD
)&gds
;
995 es
.pfnCallback
= dwFormat
== SF_RTF
? ME_ReadFromHGLOBALRTF
: ME_ReadFromHGLOBALUnicode
;
996 SendMessageW(hWnd
, EM_STREAMIN
, dwFormat
|SFF_SELECTION
, (LPARAM
)&es
);
1008 ME_GlobalDestStruct gds
;
1010 if (!OpenClipboard(hWnd
))
1014 ME_GetSelection(editor
, &from
, &to
);
1015 pars
= ME_CountParagraphsBetween(editor
, from
, to
);
1016 hData
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(WCHAR
)*(to
-from
+pars
+1));
1017 data
= (WCHAR
*)GlobalLock(hData
);
1018 ME_GetTextW(editor
, data
, from
, to
-from
, TRUE
);
1019 GlobalUnlock(hData
);
1021 gds
.hData
= GlobalAlloc(GMEM_MOVEABLE
, 0);
1023 es
.dwCookie
= (DWORD
)&gds
;
1024 es
.pfnCallback
= ME_AppendToHGLOBAL
;
1025 SendMessageW(hWnd
, EM_STREAMOUT
, SFF_SELECTION
|SF_RTF
, (LPARAM
)&es
);
1026 GlobalReAlloc(gds
.hData
, gds
.nLength
+1, 0);
1028 SetClipboardData(CF_UNICODETEXT
, hData
);
1029 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds
.hData
);
1034 ME_InternalDeleteText(editor
, from
, to
-from
);
1035 ME_CommitUndo(editor
);
1036 ME_UpdateRepaint(editor
);
1040 case WM_GETTEXTLENGTH
:
1041 return ME_GetTextLength(editor
);
1044 TEXTRANGEW tr
; /* W and A differ only by rng->lpstrText */
1046 tr
.chrg
.cpMax
= wParam
-1;
1047 tr
.lpstrText
= (WCHAR
*)lParam
;
1048 return RichEditANSIWndProc(hWnd
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
1053 TEXTRANGEW tr
; /* W and A differ only by rng->lpstrText */
1054 ME_GetSelection(editor
, &from
, &to
);
1055 tr
.chrg
.cpMin
= from
;
1057 tr
.lpstrText
= (WCHAR
*)lParam
;
1058 return RichEditANSIWndProc(hWnd
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
1060 case EM_GETTEXTRANGE
:
1062 TEXTRANGEW
*rng
= (TEXTRANGEW
*)lParam
;
1063 if (IsWindowUnicode(hWnd
))
1064 return ME_GetTextW(editor
, rng
->lpstrText
, rng
->chrg
.cpMin
, rng
->chrg
.cpMax
-rng
->chrg
.cpMin
, FALSE
);
1067 int nLen
= rng
->chrg
.cpMax
-rng
->chrg
.cpMin
;
1068 WCHAR
*p
= ALLOC_N_OBJ(WCHAR
, nLen
+1);
1069 int nChars
= ME_GetTextW(editor
, p
, rng
->chrg
.cpMin
, nLen
, FALSE
);
1070 /* FIXME this is a potential security hole (buffer overrun)
1071 if you know more about wchar->mbyte conversion please explain
1073 WideCharToMultiByte(CP_ACP
, 0, p
, nChars
+1, (char *)rng
->lpstrText
, nLen
+1, NULL
, NULL
);
1077 return ME_GetTextW(editor
, rng
->lpstrText
, rng
->chrg
.cpMin
, rng
->chrg
.cpMax
-rng
->chrg
.cpMin
, FALSE
);
1080 ME_CommitUndo(editor
);
1081 ME_WrapMarkedParagraphs(editor
);
1082 ME_MoveCaret(editor
);
1085 ME_DestroyEditor(editor
);
1086 SetWindowLongW(hWnd
, 0, 0);
1088 case WM_LBUTTONDOWN
:
1090 ME_LButtonDown(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
1094 if (GetCapture() == hWnd
)
1095 ME_MouseMove(editor
, (short)LOWORD(lParam
), (short)HIWORD(lParam
));
1098 if (GetCapture() == hWnd
)
1102 hDC
= BeginPaint(hWnd
, &ps
);
1103 ME_PaintContent(editor
, hDC
, FALSE
, &ps
.rcPaint
);
1104 EndPaint(hWnd
, &ps
);
1107 ME_ShowCaret(editor
);
1108 ME_SendOldNotify(editor
, EN_SETFOCUS
);
1111 ME_HideCaret(editor
);
1112 ME_SendOldNotify(editor
, EN_KILLFOCUS
);
1116 HDC hDC
= (HDC
)wParam
;
1118 COLORREF rgbBG
= ME_GetBackColor(editor
);
1119 if (GetUpdateRect(hWnd
,&rc
,TRUE
))
1121 HBRUSH hbr
= CreateSolidBrush(rgbBG
);
1122 FillRect(hDC
, &rc
, hbr
);
1128 TRACE("editor wnd command = %d\n", LOWORD(wParam
));
1131 if (ME_ArrowKey(editor
, LOWORD(wParam
), GetKeyState(VK_CONTROL
)<0)) {
1132 ME_CommitUndo(editor
);
1133 ME_EnsureVisible(editor
, editor
->pCursors
[0].pRun
);
1135 ME_MoveCaret(editor
);
1139 if (GetKeyState(VK_CONTROL
)<0)
1141 if (LOWORD(wParam
)=='W')
1145 ME_GetSelectionCharFormat(editor
, &chf
);
1146 ME_DumpStyleToBuf(&chf
, buf
);
1147 MessageBoxA(NULL
, buf
, "Style dump", MB_OK
);
1149 if (LOWORD(wParam
)=='Q')
1151 ME_CheckCharOffsets(editor
);
1158 if (GetWindowLongW(editor
->hWnd
, GWL_STYLE
) & ES_READONLY
) {
1159 MessageBeep(MB_ICONERROR
);
1160 return 0; /* FIXME really 0 ? */
1162 wstr
= LOWORD(wParam
);
1163 if (((unsigned)wstr
)>=' ' || wstr
=='\r') {
1164 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1165 ME_Style
*style
= ME_GetInsertStyle(editor
, 0);
1166 ME_SaveTempStyle(editor
);
1167 ME_InsertTextFromCursor(editor
, 0, &wstr
, 1, style
);
1168 ME_ReleaseStyle(style
);
1169 ME_CommitUndo(editor
);
1170 ME_UpdateRepaint(editor
);
1176 int nPos
= editor
->nScrollPosY
;
1177 si
.cbSize
= sizeof(SCROLLINFO
);
1178 si
.fMask
= SIF_PAGE
|SIF_POS
|SIF_RANGE
|SIF_TRACKPOS
;
1179 GetScrollInfo(hWnd
, SB_VERT
, &si
);
1180 switch(LOWORD(wParam
)) {
1182 nPos
-= 24; /* FIXME follow the original */
1183 if (nPos
<0) nPos
= 0;
1187 int nEnd
= editor
->nTotalLength
- editor
->sizeWindow
.cy
;
1188 nPos
+= 24; /* FIXME follow the original */
1189 if (nPos
>=nEnd
) nPos
= nEnd
;
1193 nPos
-= editor
->sizeWindow
.cy
;
1194 if (nPos
<0) nPos
= 0;
1197 nPos
+= editor
->sizeWindow
.cy
;
1198 if (nPos
>=editor
->nTotalLength
) nPos
= editor
->nTotalLength
-1;
1201 case SB_THUMBPOSITION
:
1202 nPos
= si
.nTrackPos
;
1205 if (nPos
!= editor
->nScrollPosY
) {
1206 ScrollWindow(hWnd
, 0, editor
->nScrollPosY
-nPos
, NULL
, NULL
);
1207 editor
->nScrollPosY
= nPos
;
1208 SetScrollPos(hWnd
, SB_VERT
, nPos
, TRUE
);
1215 int gcWheelDelta
= 0, nPos
= editor
->nScrollPosY
;
1216 UINT pulScrollLines
;
1218 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES
,0, &pulScrollLines
, 0);
1219 gcWheelDelta
-= GET_WHEEL_DELTA_WPARAM(wParam
);
1220 if (abs(gcWheelDelta
) >= WHEEL_DELTA
&& pulScrollLines
)
1221 nPos
+= pulScrollLines
* (gcWheelDelta
/ WHEEL_DELTA
) * 8;
1222 if(nPos
>=editor
->nTotalLength
)
1223 nPos
= editor
->nTotalLength
- 1;
1226 if (nPos
!= editor
->nScrollPosY
) {
1227 ScrollWindow(hWnd
, 0, editor
->nScrollPosY
-nPos
, NULL
, NULL
);
1228 editor
->nScrollPosY
= nPos
;
1229 SetScrollPos(hWnd
, SB_VERT
, nPos
, TRUE
);
1236 ME_MarkAllForWrapping(editor
);
1237 ME_WrapMarkedParagraphs(editor
);
1238 ME_UpdateScrollBar(editor
);
1240 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1242 case EM_GETOLEINTERFACE
:
1244 LPVOID
*ppvObj
= (LPVOID
*) lParam
;
1245 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj
);
1246 return CreateIRichEditOle(ppvObj
);
1250 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1255 /******************************************************************
1256 * RichEdit10ANSIWndProc (RICHED20.9)
1258 LRESULT WINAPI
RichEdit10ANSIWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1260 /* FIXME: this is NOT the same as 2.0 version */
1261 return RichEditANSIWndProc(hWnd
, msg
, wParam
, lParam
);
1264 void ME_SendOldNotify(ME_TextEditor
*editor
, int nCode
)
1266 HWND hWnd
= editor
->hWnd
;
1267 SendMessageA(GetParent(hWnd
), WM_COMMAND
, (nCode
<<16)|GetWindowLongW(hWnd
, GWLP_ID
), (LPARAM
)hWnd
);
1270 int ME_CountParagraphsBetween(ME_TextEditor
*editor
, int from
, int to
)
1272 ME_DisplayItem
*item
= ME_FindItemFwd(editor
->pBuffer
->pFirst
, diParagraph
);
1275 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= from
)
1276 item
= item
->member
.para
.next_para
;
1279 while(item
&& item
->member
.para
.next_para
->member
.para
.nCharOfs
<= to
) {
1280 item
= item
->member
.para
.next_para
;
1287 int ME_GetTextW(ME_TextEditor
*editor
, WCHAR
*buffer
, int nStart
, int nChars
, int bCRLF
)
1289 ME_DisplayItem
*item
= ME_FindItemAtOffset(editor
, diRun
, nStart
, &nStart
);
1300 int nLen
= ME_StrLen(item
->member
.run
.strText
) - nStart
;
1303 CopyMemory(buffer
, item
->member
.run
.strText
->szData
+ nStart
, sizeof(WCHAR
)*nLen
);
1310 item
= ME_FindItemFwd(item
, diRun
);
1313 while(nChars
&& item
)
1315 int nLen
= ME_StrLen(item
->member
.run
.strText
);
1319 if (item
->member
.run
.nFlags
& MERF_ENDPARA
)
1329 CopyMemory(buffer
, item
->member
.run
.strText
->szData
, sizeof(WCHAR
)*nLen
);
1339 item
= ME_FindItemFwd(item
, diRun
);
1345 static WCHAR wszClassName
[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1346 static WCHAR wszClassName50
[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1348 void ME_RegisterEditorClass(HINSTANCE hInstance
)
1354 wcW
.style
= CS_HREDRAW
| CS_VREDRAW
;
1355 wcW
.lpfnWndProc
= RichEditANSIWndProc
;
1358 wcW
.hInstance
= NULL
; /* hInstance would register DLL-local class */
1360 wcW
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
1361 wcW
.hbrBackground
= (HBRUSH
)GetStockObject(NULL_BRUSH
);
1362 wcW
.lpszMenuName
= NULL
;
1363 wcW
.lpszClassName
= wszClassName
;
1364 bResult
= RegisterClassW(&wcW
);
1366 wcW
.lpszClassName
= wszClassName50
;
1367 bResult
= RegisterClassW(&wcW
);
1370 wcA
.style
= CS_HREDRAW
| CS_VREDRAW
;
1371 wcA
.lpfnWndProc
= RichEditANSIWndProc
;
1374 wcA
.hInstance
= NULL
; /* hInstance would register DLL-local class */
1376 wcA
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_IBEAM
));
1377 wcA
.hbrBackground
= (HBRUSH
)GetStockObject(NULL_BRUSH
);
1378 wcA
.lpszMenuName
= NULL
;
1379 wcA
.lpszClassName
= "RichEdit20A";
1380 bResult
= RegisterClassA(&wcA
);
1382 wcA
.lpszClassName
= "RichEdit50A";
1383 bResult
= RegisterClassA(&wcA
);
1387 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1392 case DLL_PROCESS_ATTACH
:
1393 DisableThreadLibraryCalls(hinstDLL
);
1394 me_heap
= HeapCreate (0, 0x10000, 0);
1395 ME_RegisterEditorClass(hinstDLL
);
1398 case DLL_PROCESS_DETACH
:
1399 UnregisterClassW(wszClassName
, 0);
1400 UnregisterClassW(wszClassName50
, 0);
1401 UnregisterClassA("RichEdit20A", 0);
1402 UnregisterClassA("RichEdit50A", 0);
1403 HeapDestroy (me_heap
);
1410 /******************************************************************
1411 * CreateTextServices (RICHED20.4)
1413 * FIXME should be ITextHost instead of void*
1415 HRESULT WINAPI
CreateTextServices(IUnknown
*punkOuter
, void *pITextHost
,
1419 /* FIXME should support aggregation */
1421 return CLASS_E_NOAGGREGATION
;
1423 return E_FAIL
; /* E_NOTIMPL isn't allowed by MSDN */
1426 /******************************************************************
1427 * REExtendedRegisterClass (RICHED20.8)
1429 * FIXME undocumented
1431 void WINAPI
REExtendedRegisterClass(void)