Code Coverage Statistics for Source File

c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Document\Selection\DefaultSelection.cs

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
133
Highlight: Uncovered Code Covered Code
L V Source
1
// <file>
2
//     <copyright see="prj:///doc/copyright.txt"/>
3
//     <license see="prj:///doc/license.txt"/>
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
//     <version>$Revision: 2679 $</version>
6
// </file>
7
8
using System;
9
using System.Diagnostics;
10
using System.Drawing;
11
12
namespace ICSharpCode.TextEditor.Document
13
{
14
	/// <summary>
15
	/// Default implementation of the <see cref="ICSharpCode.TextEditor.Document.ISelection"/> interface.
16
	/// </summary>
17
	public class DefaultSelection : ISelection
18
	{
19
		IDocument document;
20
		bool      isRectangularSelection;
21
		TextLocation     startPosition;
22
		TextLocation     endPosition;
23
		
24
		public TextLocation StartPosition {
25
			get {
26
				return startPosition;
27
			}
28
			set {
29
				DefaultDocument.ValidatePosition(document, value);
30
				startPosition = value;
31
			}
32
		}
33
		
34
		public TextLocation EndPosition {
35
			get {
36
				return endPosition;
37
			}
38
			set {
39
				DefaultDocument.ValidatePosition(document, value);
40
				endPosition = value;
41
			}
42
		}
43
		
44
		public int Offset {
45
			get {
46
				return document.PositionToOffset(startPosition);
47
			}
48
		}
49
		
50
		public int EndOffset {
51
			get {
52
				return document.PositionToOffset(endPosition);
53
			}
54
		}
55
		
56
		public int Length {
57
			get {
58
				return EndOffset - Offset;
59
			}
60
		}
61
		
62
		/// <value>
63
		/// Returns true, if the selection is empty
64
		/// </value>
65
		public bool IsEmpty {
66
			get {
67
				return startPosition == endPosition;
68
			}
69
		}
70
		
71
		/// <value>
72
		/// Returns true, if the selection is rectangular
73
		/// </value>
74
		// TODO : make this unused property used.
75
		public bool IsRectangularSelection {
76
			get {
77
				return isRectangularSelection;
78
			}
79
			set {
80
				isRectangularSelection = value;
81
			}
82
		}
83
		
84
		/// <value>
85
		/// The text which is selected by this selection.
86
		/// </value>
87
		public string SelectedText {
88
			get {
89
				if (document != null) {
90
					if (Length < 0) {
91
						return null;
92
					}
93
					return document.GetText(Offset, Length);
94
				}
95
				return null;
96
			}
97
		}
98
		
99
		/// <summary>
100
		/// Creates a new instance of <see cref="DefaultSelection"/>
101
		/// </summary>
102
		public DefaultSelection(IDocument document, TextLocation startPosition, TextLocation endPosition)
103
		{
104
			DefaultDocument.ValidatePosition(document, startPosition);
105
			DefaultDocument.ValidatePosition(document, endPosition);
106
			Debug.Assert(startPosition <= endPosition);
107
			this.document      = document;
108
			this.startPosition = startPosition;
109
			this.endPosition   = endPosition;
110
		}
111
		
112
		/// <summary>
113
		/// Converts a <see cref="DefaultSelection"/> instance to string (for debug purposes)
114
		/// </summary>
115
		public override string ToString()
116
		{
117
			return String.Format("[DefaultSelection : StartPosition={0}, EndPosition={1}]", startPosition, endPosition);
118
		}
119
		public bool ContainsPosition(TextLocation position)
120
		{
121
			if (this.IsEmpty)
122
				return false;
123
			return startPosition.Y < position.Y && position.Y  < endPosition.Y ||
124
				startPosition.Y == position.Y && startPosition.X <= position.X && (startPosition.Y != endPosition.Y || position.X <= endPosition.X) ||
125
				endPosition.Y == position.Y && startPosition.Y != endPosition.Y && position.X <= endPosition.X;
126
		}
127
		
128
		public bool ContainsOffset(int offset)
129
		{
130
			return Offset <= offset && offset <= EndOffset;
131
		}
132
	}
133
}