Clipboard Operations
SyntaxEditor makes use of the Windows clipboard as a temporary repository for data for cut/copy/paste operations. SyntaxEditor places text on the clipboard using the DataFormats.UnicodeText
and DataFormats.Text
formats.
Tip
Clipboard and Drag and Drop operations share many of the same concepts. Refer to the Drag and Drop topic for details specific to working with drag-and-drop.
Cutting and Copying Text
The IEditorView.CutLineToClipboard and CopyToClipboard methods may be used to cut and copy text to the clipboard respectively. The text that is placed on the clipboard is the currently selected text designated by the IEditorView.Selection.
If the selection length is zero, or in other words, no text is selected, a feature of SyntaxEditor is to place the entire line of text on the clipboard of which the caret is currently on. You can control whether blank lines will overwrite the clipboard by using the CanCutCopyBlankLineWhenNoSelection property.
There are CutAndAppendToClipboard and CopyAndAppendToClipboard methods on IEditorView as well, that append to the clipboard instead of replacing the contents of the clipboard.
This code demonstrates how to copy the currently selected text to the clipboard:
editor.ActiveView.CopyToClipboard();
Cutting and Copying with HTML and RTF Export
The text that is copied can also be copied to the clipboard with HTML and RTF exported highlighting. Then, if you paste the text in an application such as Word, the text will appear highlighted.
The SyntaxEditor.CanCutCopyDragWithHtml property controls whether HTML exporting is performed whenever cutting or copying to the clipboard.
The SyntaxEditor.CanCutCopyDragWithRtf property controls whether RTF exporting is performed whenever cutting or copying to the clipboard.
Pasting Text
The IEditorView.PasteFromClipboard method allows text on the clipboard to be pasted into the document, thereby replacing any text that is currently selected within the view from which the method is called.
If text was copied from a SyntaxEditor when the selection length was zero (see above) the entire line of text that was copied will be inserted above the line in which the caret is currently located. In this situation, the caret is not moved.
This code demonstrates how to paste text from the clipboard:
editor.ActiveView.PasteFromClipboard();
Customizing Text to be Cut or Copied
Sometimes it is useful to be able to customize the text, or objects, to be cut or copied. The SyntaxEditor.CutCopyDrag event that is raised before text is cut or copied to the clipboard, and also before a drag occurs (see the Drag and Drop topic for additional details).
In its event arguments, it passes the IDataStore that is to be copied as well as the type of operation that will be performed. When the event is raised, the IDataStore has already been initialized with DataFormats.UnicodeText
and DataFormats.Text
entries based on the current selection in the editor. The IDataStore can be modified to customize what is sent to the clipboard.
Note
The IDataStore interface is used instead of IDataObject
. IDataStore is designed to use many of the same method signatures that IDataObject
does and provides a consistent API across our supported platforms.
Customizing Text to be Pasted
Just like the SyntaxEditor.CutCopyDrag event, an event is provided to allow for customization of text that is to be pasted or dropped onto the editor (see the Drag and Drop topic for additional details). The PasteDragDrop event is raised in several situations:
- Paste operations
- Paste completion
- CanPaste checks
- Drag enter
- Drag over
- Drag/drop
This event passes the IDataStore that is to be pasted and the source of the event. The IDataStore is similar to IDataObject
(see note above) and provides access to any clipboard data (such as non-text formats) that was used to trigger the event.
The PasteDragDropEventArgs.Text property can be set to the text to be inserted. It also can be set to null
to insert nothing.
Tip
For CanPaste actions, the PasteDragDropEventArgs.Text property can be set to any non-null
value to indicate that an object can be pasted. The actual value only has to be assigned for the Paste actions.
Tracking Clipboard Change Events
Since the SyntaxEditor.CutCopyDrag event is raised any time text is cut or copied from the control, it can also be used to maintain an external clipboard-setting history in your application. This is useful for maintaining a clipboard ring for your application.