/// <summary> /// Creates a data object containing the selection's text. /// </summary> public virtual DataObject CreateDataObject(TextArea textArea) { DataObject data = new DataObject(); // Ensure we use the appropriate newline sequence for the OS string text = TextUtilities.NormalizeNewLines(GetText(), Environment.NewLine); // Enable drag/drop to Word, Notepad++ and others if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.UnicodeText)) { data.SetText(text); } // Enable drag/drop to SciTe: // We cannot use SetText, thus we need to use typeof(string).FullName as data format. // new DataObject(object) calls SetData(object), which in turn calls SetData(Type, data), // which then uses Type.FullName as format. // We immitate that behavior here as well: if (EditingCommandHandler.ConfirmDataFormat(textArea, data, typeof(string).FullName)) { data.SetData(typeof(string).FullName, text); } // Also copy text in HTML format to clipboard - good for pasting text into Word // or to the SharpDevelop forums. if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.Html)) { HtmlClipboard.SetHtml(data, CreateHtmlFragment(new HtmlOptions(textArea.Options))); } return(data); }
const string LineSelectedType = "MSDEVLineSelect"; // This is the type VS 2003 and 2005 use for flagging a whole line copy static void CopyWholeLine(TextArea textArea, DocumentLine line) { ISegment wholeLine = new SimpleSegment(line.Offset, line.TotalLength); string text = textArea.Document.GetText(wholeLine); // Ensure we use the appropriate newline sequence for the OS text = TextUtilities.NormalizeNewLines(text, Environment.NewLine); DataObject data = new DataObject(text); // Also copy text in HTML format to clipboard - good for pasting text into Word // or to the SharpDevelop forums. IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter; HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, wholeLine, new HtmlOptions(textArea.Options))); MemoryStream lineSelected = new MemoryStream(1); lineSelected.WriteByte(1); data.SetData(LineSelectedType, lineSelected, false); try { Clipboard.SetDataObject(data, true); } catch (ExternalException) { // Apparently this exception sometimes happens randomly. // The MS controls just ignore it, so we'll do the same. return; } textArea.OnTextCopied(new TextEventArgs(text)); }
private void btnCopy_Click(object sender, RoutedEventArgs e) { if (_edtXPath.SelectionLength > 0) { string text = _edtXPath.SelectedText; Clipboard.SetText(text); } else { DataObject dobj = new DataObject(); dobj.SetText(_edtXPath.Text); try { IHighlighter highlighter = new DocumentHighlighter( _edtXPath.Document, _edtXPath.SyntaxHighlighting.MainRuleSet); string html = HtmlClipboard.CreateHtmlFragment( _edtXPath.Document, highlighter, null, new HtmlOptions()); HtmlClipboard.SetHtml(dobj, html); } catch { //ignore } Clipboard.SetDataObject(dobj); } }
static void CopySelectedText(TextArea textArea) { string text = textArea.Selection.GetText(textArea.Document); // Ensure we use the appropriate newline sequence for the OS DataObject data = new DataObject(NewLineFinder.NormalizeNewLines(text, Environment.NewLine)); // Also copy text in HTML format to clipboard - good for pasting text into Word // or to the SharpDevelop forums. HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragmentForSelection(textArea, new HtmlOptions(textArea.Options))); Clipboard.SetDataObject(data, true); }
/// <summary> /// Creates a data object containing the selection's text. /// </summary> public virtual DataObject CreateDataObject(TextArea textArea) { string text = GetText(textArea.Document); // Ensure we use the appropriate newline sequence for the OS DataObject data = new DataObject(TextUtilities.NormalizeNewLines(text, Environment.NewLine)); // we cannot use DataObject.SetText - then we cannot drag to SciTe // (but dragging to Word works in both cases) // Also copy text in HTML format to clipboard - good for pasting text into Word // or to the SharpDevelop forums. HtmlClipboard.SetHtml(data, CreateHtmlFragment(textArea, new HtmlOptions(textArea.Options))); return data; }
const string LineSelectedType = "MSDEVLineSelect"; // This is the type VS 2003 and 2005 use for flagging a whole line copy static void CopyWholeLine(TextArea textArea, DocumentLine line) { ISegment wholeLine = new SimpleSegment(line.Offset, line.TotalLength); string text = textArea.Document.GetText(wholeLine); // Ensure we use the appropriate newline sequence for the OS DataObject data = new DataObject(NewLineFinder.NormalizeNewLines(text, Environment.NewLine)); // Also copy text in HTML format to clipboard - good for pasting text into Word // or to the SharpDevelop forums. DocumentHighlighter highlighter = textArea.GetService(typeof(DocumentHighlighter)) as DocumentHighlighter; HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, wholeLine, new HtmlOptions(textArea.Options))); MemoryStream lineSelected = new MemoryStream(1); lineSelected.WriteByte(1); data.SetData(LineSelectedType, lineSelected, false); Clipboard.SetDataObject(data, true); }
void StartDrag() { // prevent nested StartDrag calls mode = SelectionMode.Drag; // mouse capture and Drag'n'Drop doesn't mix textArea.ReleaseMouseCapture(); string text = textArea.Selection.GetText(textArea.Document); text = NewLineFinder.NormalizeNewLines(text, Environment.NewLine); DataObject dataObject = new DataObject(text); // we cannot use DataObject.SetText - then we cannot drag to SciTe // (but dragging to Word works in both cases) // also copy as HTML - adds syntax highlighting when dragging to Word string htmlFragment = HtmlClipboard.CreateHtmlFragmentForSelection(textArea, new HtmlOptions(textArea.Options)); HtmlClipboard.SetHtml(dataObject, htmlFragment); DragDropEffects allowedEffects = DragDropEffects.All; var deleteOnMove = textArea.Selection.Segments.Select(s => new AnchorSegment(textArea.Document, s)).ToList(); object dragDescriptor = new object(); this.currentDragDescriptor = dragDescriptor; DragDropEffects resultEffect; using (textArea.AllowCaretOutsideSelection()) { var oldCaretPosition = textArea.Caret.Position; try { Debug.WriteLine("DoDragDrop with allowedEffects=" + allowedEffects); resultEffect = DragDrop.DoDragDrop(textArea, dataObject, allowedEffects); Debug.WriteLine("DoDragDrop done, resultEffect=" + resultEffect); } catch (COMException ex) { // ignore COM errors - don't crash on badly implemented drop targets Debug.WriteLine("DoDragDrop failed: " + ex.ToString()); return; } if (resultEffect == DragDropEffects.None) { // reset caret if drag was aborted textArea.Caret.Position = oldCaretPosition; } } this.currentDragDescriptor = null; if (deleteOnMove != null && resultEffect == DragDropEffects.Move) { bool draggedInsideSingleDocument = (dragDescriptor == textArea.Document.UndoStack.LastGroupDescriptor); if (draggedInsideSingleDocument) { textArea.Document.UndoStack.StartContinuedUndoGroup(null); } textArea.Document.BeginUpdate(); try { foreach (ISegment s in deleteOnMove) { textArea.Document.Remove(s.Offset, s.Length); } } finally { textArea.Document.EndUpdate(); if (draggedInsideSingleDocument) { textArea.Document.UndoStack.EndUndoGroup(); } } } }