~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/SelectionColorizer.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.Windows;
 
6
using ICSharpCode.AvalonEdit.Document;
 
7
using ICSharpCode.AvalonEdit.Rendering;
 
8
 
 
9
namespace ICSharpCode.AvalonEdit.Editing
 
10
{
 
11
        sealed class SelectionColorizer : ColorizingTransformer
 
12
        {
 
13
                TextArea textArea;
 
14
                
 
15
                public SelectionColorizer(TextArea textArea)
 
16
                {
 
17
                        if (textArea == null)
 
18
                                throw new ArgumentNullException("textArea");
 
19
                        this.textArea = textArea;
 
20
                }
 
21
                
 
22
                protected override void Colorize(ITextRunConstructionContext context)
 
23
                {
 
24
                        // if SelectionForeground is null, keep the existing foreground color
 
25
                        if (textArea.SelectionForeground == null)
 
26
                                return;
 
27
                        
 
28
                        int lineStartOffset = context.VisualLine.FirstDocumentLine.Offset;
 
29
                        int lineEndOffset = context.VisualLine.LastDocumentLine.Offset + context.VisualLine.LastDocumentLine.TotalLength;
 
30
                        
 
31
                        foreach (ISegment segment in textArea.Selection.Segments) {
 
32
                                int segmentStart = segment.Offset;
 
33
                                int segmentEnd = segment.Offset + segment.Length;
 
34
                                if (segmentEnd <= lineStartOffset)
 
35
                                        continue;
 
36
                                if (segmentStart >= lineEndOffset)
 
37
                                        continue;
 
38
                                int startColumn = context.VisualLine.GetVisualColumn(Math.Max(0, segmentStart - lineStartOffset));
 
39
                                int endColumn = context.VisualLine.GetVisualColumn(segmentEnd - lineStartOffset);
 
40
                                ChangeVisualElements(
 
41
                                        startColumn, endColumn,
 
42
                                        element => {
 
43
                                                element.TextRunProperties.SetForegroundBrush(textArea.SelectionForeground);
 
44
                                        });
 
45
                        }
 
46
                }
 
47
        }
 
48
}