PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/AvalonEdit/ICSharpCode.AvalonEdit/Editing/SelectionColorizer.cs

http://github.com/icsharpcode/ILSpy
C# | 74 lines | 48 code | 8 blank | 18 comment | 8 complexity | 7ba57bc4744381bddd77b6e8aafd460c MD5 | raw file
 1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
 2// 
 3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 4// software and associated documentation files (the "Software"), to deal in the Software
 5// without restriction, including without limitation the rights to use, copy, modify, merge,
 6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 7// to whom the Software is furnished to do so, subject to the following conditions:
 8// 
 9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11// 
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using ICSharpCode.AvalonEdit.Rendering;
21#if NREFACTORY
22using ICSharpCode.NRefactory.Editor;
23#endif
24
25namespace ICSharpCode.AvalonEdit.Editing
26{
27	sealed class SelectionColorizer : ColorizingTransformer
28	{
29		TextArea textArea;
30		
31		public SelectionColorizer(TextArea textArea)
32		{
33			if (textArea == null)
34				throw new ArgumentNullException("textArea");
35			this.textArea = textArea;
36		}
37		
38		protected override void Colorize(ITextRunConstructionContext context)
39		{
40			// if SelectionForeground is null, keep the existing foreground color
41			if (textArea.SelectionForeground == null)
42				return;
43			
44			int lineStartOffset = context.VisualLine.FirstDocumentLine.Offset;
45			int lineEndOffset = context.VisualLine.LastDocumentLine.Offset + context.VisualLine.LastDocumentLine.TotalLength;
46			
47			foreach (SelectionSegment segment in textArea.Selection.Segments) {
48				int segmentStart = segment.StartOffset;
49				int segmentEnd = segment.EndOffset;
50				if (segmentEnd <= lineStartOffset)
51					continue;
52				if (segmentStart >= lineEndOffset)
53					continue;
54				int startColumn;
55				if (segmentStart < lineStartOffset)
56					startColumn = 0;
57				else
58					startColumn = context.VisualLine.ValidateVisualColumn(segment.StartOffset, segment.StartVisualColumn, textArea.Selection.EnableVirtualSpace);
59				
60				int endColumn;
61				if (segmentEnd > lineEndOffset)
62					endColumn = textArea.Selection.EnableVirtualSpace ? int.MaxValue : context.VisualLine.VisualLengthWithEndOfLineMarker;
63				else
64					endColumn = context.VisualLine.ValidateVisualColumn(segment.EndOffset, segment.EndVisualColumn, textArea.Selection.EnableVirtualSpace);
65				
66				ChangeVisualElements(
67					startColumn, endColumn,
68					element => {
69						element.TextRunProperties.SetForegroundBrush(textArea.SelectionForeground);
70					});
71			}
72		}
73	}
74}