// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; using System.Text; using System.Windows; using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Highlighting; namespace ICSharpCode.AvalonEdit.Editing { /// /// Base class for selections. /// public abstract class Selection { /// /// Gets the empty selection. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification="Empty selection is immutable")] public static readonly Selection Empty = new SimpleSelection(-1, -1); /// /// Gets the selected text segments. /// public abstract IEnumerable Segments { get; } /// /// Gets the smallest segment that contains all segments in this selection. /// May return null if the selection is empty. /// public abstract ISegment SurroundingSegment { get; } /// /// Replaces the selection with the specified text. /// public abstract void ReplaceSelectionWithText(TextArea textArea, string newText); /// /// Updates the selection when the document changes. /// public abstract Selection UpdateOnDocumentChange(DocumentChangeEventArgs e); /// /// Gets whether the selection is empty. /// public virtual bool IsEmpty { get { return Length == 0; } } /// /// Gets the selection length. /// public abstract int Length { get; } /// /// Returns a new selection with the changed end point. /// /// Cannot set endpoint for empty selection public abstract Selection SetEndpoint(int newEndOffset); /// /// If this selection is empty, starts a new selection from to /// , otherwise, changes the endpoint of this selection. /// public virtual Selection StartSelectionOrSetEndpoint(int startOffset, int newEndOffset) { if (IsEmpty) return new SimpleSelection(startOffset, newEndOffset); else return SetEndpoint(newEndOffset); } /// /// Gets whether the selection is multi-line. /// public virtual bool IsMultiline(TextDocument document) { if (document == null) throw new ArgumentNullException("document"); ISegment surroundingSegment = this.SurroundingSegment; if (surroundingSegment == null) return false; int start = surroundingSegment.Offset; int end = start + surroundingSegment.Length; return document.GetLineByOffset(start) != document.GetLineByOffset(end); } /// /// Gets the selected text. /// public virtual string GetText(TextDocument document) { if (document == null) throw new ArgumentNullException("document"); StringBuilder b = null; string text = null; foreach (ISegment s in Segments) { if (text != null) { if (b == null) b = new StringBuilder(text); else b.Append(text); } text = document.GetText(s); } if (b != null) { if (text != null) b.Append(text); return b.ToString(); } else { return text ?? string.Empty; } } /// /// Creates a HTML fragment for the selected text. /// 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("
"); html.Append(HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, selectedSegment, options)); } return html.ToString(); } /// public abstract override bool Equals(object obj); /// public abstract override int GetHashCode(); /// /// Gets whether the specified offset is included in the selection. /// /// True, if the selection contains the offset (selection borders inclusive); /// otherwise, false. public virtual bool Contains(int offset) { if (this.IsEmpty) return false; if (this.SurroundingSegment.Contains(offset)) { foreach (ISegment s in this.Segments) { if (s.Contains(offset)) { return true; } } } return false; } /// /// Creates a data object containing the selection's text. /// 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; } } }