~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/Selection.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Text;
 
7
using System.Windows;
 
8
 
 
9
using ICSharpCode.AvalonEdit.Document;
 
10
using ICSharpCode.AvalonEdit.Highlighting;
 
11
 
 
12
namespace ICSharpCode.AvalonEdit.Editing
 
13
{
 
14
        /// <summary>
 
15
        /// Base class for selections.
 
16
        /// </summary>
 
17
        public abstract class Selection
 
18
        {
 
19
                /// <summary>
 
20
                /// Gets the empty selection.
 
21
                /// </summary>
 
22
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification="Empty selection is immutable")]
 
23
                public static readonly Selection Empty = new SimpleSelection(-1, -1);
 
24
                
 
25
                /// <summary>
 
26
                /// Gets the selected text segments.
 
27
                /// </summary>
 
28
                public abstract IEnumerable<ISegment> Segments { get; }
 
29
                
 
30
                /// <summary>
 
31
                /// Gets the smallest segment that contains all segments in this selection.
 
32
                /// May return null if the selection is empty.
 
33
                /// </summary>
 
34
                public abstract ISegment SurroundingSegment { get; }
 
35
                
 
36
                /// <summary>
 
37
                /// Replaces the selection with the specified text.
 
38
                /// </summary>
 
39
                public abstract void ReplaceSelectionWithText(TextArea textArea, string newText);
 
40
                
 
41
                /// <summary>
 
42
                /// Updates the selection when the document changes.
 
43
                /// </summary>
 
44
                public abstract Selection UpdateOnDocumentChange(DocumentChangeEventArgs e);
 
45
                
 
46
                /// <summary>
 
47
                /// Gets whether the selection is empty.
 
48
                /// </summary>
 
49
                public virtual bool IsEmpty {
 
50
                        get { return Length == 0; }
 
51
                }
 
52
                
 
53
                /// <summary>
 
54
                /// Gets the selection length.
 
55
                /// </summary>
 
56
                public abstract int Length { get; }
 
57
                
 
58
                /// <summary>
 
59
                /// Returns a new selection with the changed end point.
 
60
                /// </summary>
 
61
                /// <exception cref="NotSupportedException">Cannot set endpoint for empty selection</exception>
 
62
                public abstract Selection SetEndpoint(int newEndOffset);
 
63
                
 
64
                /// <summary>
 
65
                /// If this selection is empty, starts a new selection from <paramref name="startOffset"/> to
 
66
                /// <paramref name="newEndOffset"/>, otherwise, changes the endpoint of this selection.
 
67
                /// </summary>
 
68
                public virtual Selection StartSelectionOrSetEndpoint(int startOffset, int newEndOffset)
 
69
                {
 
70
                        if (IsEmpty)
 
71
                                return new SimpleSelection(startOffset, newEndOffset);
 
72
                        else
 
73
                                return SetEndpoint(newEndOffset);
 
74
                }
 
75
                
 
76
                /// <summary>
 
77
                /// Gets whether the selection is multi-line.
 
78
                /// </summary>
 
79
                public virtual bool IsMultiline(TextDocument document)
 
80
                {
 
81
                        if (document == null)
 
82
                                throw new ArgumentNullException("document");
 
83
                        ISegment surroundingSegment = this.SurroundingSegment;
 
84
                        if (surroundingSegment == null)
 
85
                                return false;
 
86
                        int start = surroundingSegment.Offset;
 
87
                        int end = start + surroundingSegment.Length;
 
88
                        return document.GetLineByOffset(start) != document.GetLineByOffset(end);
 
89
                }
 
90
                
 
91
                /// <summary>
 
92
                /// Gets the selected text.
 
93
                /// </summary>
 
94
                public virtual string GetText(TextDocument document)
 
95
                {
 
96
                        if (document == null)
 
97
                                throw new ArgumentNullException("document");
 
98
                        StringBuilder b = null;
 
99
                        string text = null;
 
100
                        foreach (ISegment s in Segments) {
 
101
                                if (text != null) {
 
102
                                        if (b == null)
 
103
                                                b = new StringBuilder(text);
 
104
                                        else
 
105
                                                b.Append(text);
 
106
                                }
 
107
                                text = document.GetText(s);
 
108
                        }
 
109
                        if (b != null) {
 
110
                                if (text != null) b.Append(text);
 
111
                                return b.ToString();
 
112
                        } else {
 
113
                                return text ?? string.Empty;
 
114
                        }
 
115
                }
 
116
                
 
117
                /// <summary>
 
118
                /// Creates a HTML fragment for the selected text.
 
119
                /// </summary>
 
120
                public string CreateHtmlFragment(TextArea textArea, HtmlOptions options)
 
121
                {
 
122
                        if (textArea == null)
 
123
                                throw new ArgumentNullException("textArea");
 
124
                        if (options == null)
 
125
                                throw new ArgumentNullException("options");
 
126
                        IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter;
 
127
                        StringBuilder html = new StringBuilder();
 
128
                        bool first = true;
 
129
                        foreach (ISegment selectedSegment in this.Segments) {
 
130
                                if (first)
 
131
                                        first = false;
 
132
                                else
 
133
                                        html.AppendLine("<br>");
 
134
                                html.Append(HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, selectedSegment, options));
 
135
                        }
 
136
                        return html.ToString();
 
137
                }
 
138
                
 
139
                /// <inheritdoc/>
 
140
                public abstract override bool Equals(object obj);
 
141
                
 
142
                /// <inheritdoc/>
 
143
                public abstract override int GetHashCode();
 
144
                
 
145
                /// <summary>
 
146
                /// Gets whether the specified offset is included in the selection.
 
147
                /// </summary>
 
148
                /// <returns>True, if the selection contains the offset (selection borders inclusive);
 
149
                /// otherwise, false.</returns>
 
150
                public virtual bool Contains(int offset)
 
151
                {
 
152
                        if (this.IsEmpty)
 
153
                                return false;
 
154
                        if (this.SurroundingSegment.Contains(offset)) {
 
155
                                foreach (ISegment s in this.Segments) {
 
156
                                        if (s.Contains(offset)) {
 
157
                                                return true;
 
158
                                        }
 
159
                                }
 
160
                        }
 
161
                        return false;
 
162
                }
 
163
                
 
164
                /// <summary>
 
165
                /// Creates a data object containing the selection's text.
 
166
                /// </summary>
 
167
                public virtual DataObject CreateDataObject(TextArea textArea)
 
168
                {
 
169
                        string text = GetText(textArea.Document);
 
170
                        // Ensure we use the appropriate newline sequence for the OS
 
171
                        DataObject data = new DataObject(TextUtilities.NormalizeNewLines(text, Environment.NewLine));
 
172
                        // we cannot use DataObject.SetText - then we cannot drag to SciTe
 
173
                        // (but dragging to Word works in both cases)
 
174
                        
 
175
                        // Also copy text in HTML format to clipboard - good for pasting text into Word
 
176
                        // or to the SharpDevelop forums.
 
177
                        HtmlClipboard.SetHtml(data, CreateHtmlFragment(textArea, new HtmlOptions(textArea.Options)));
 
178
                        return data;
 
179
                }
 
180
        }
 
181
}