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)); }
/// <summary> /// Creates a HTML fragment for the selected text. /// </summary> public string CreateHtmlFragment(TextArea textArea, HtmlOptions options) { if (textArea == null) { throw new ArgumentNullException("textArea"); } if (options == null) { throw new ArgumentNullException("options"); } IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter; StringBuilder html = new StringBuilder(); bool first = true; foreach (ISegment selectedSegment in this.Segments) { if (first) { first = false; } else { html.AppendLine("<br>"); } html.Append(HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, selectedSegment, options)); } return(html.ToString()); }
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); } }
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); }
protected override void Execute(EditorFrame ef) { TextEditor textEditor = ef.XmlEditor; if (textEditor == null) { throw new Exception("No XmlEditor"); } SaveFileDialog dlgSaveFile = new SaveFileDialog(); dlgSaveFile.Filter = "Html Files|*.html|All Files|*.*"; dlgSaveFile.Title = "Select an Html file to save"; if (string.IsNullOrWhiteSpace(ef.XSDocument.Filename)) { dlgSaveFile.FileName = "undefined.html"; } else { dlgSaveFile.FileName = ef.XSDocument.Filename + ".html"; } if (dlgSaveFile.ShowDialog() == true) { try { IHighlighter highlighter = new DocumentHighlighter( textEditor.Document, textEditor.SyntaxHighlighting.MainRuleSet); string html = HtmlClipboard.CreateHtmlFragment( textEditor.Document, highlighter, null, new HtmlOptions()); File.WriteAllText(dlgSaveFile.FileName, html); } catch (Exception ex) { MessageBox.Show(Application.Current.MainWindow, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public string GetHTMLString() { IHighlighter highlighter = new DocumentHighlighter(TextEditor.Document, TextEditor.SyntaxHighlighting); return(HtmlClipboard.CreateHtmlFragment(TextEditor.Document, highlighter, null, new HtmlOptions())); }