~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/ExpressionHighlightRenderer.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.Diagnostics;
 
6
using System.Windows.Media;
 
7
using System.Collections.Generic;
 
8
 
 
9
using ICSharpCode.AvalonEdit.Document;
 
10
using ICSharpCode.AvalonEdit.Rendering;
 
11
using ICSharpCode.SharpDevelop.Editor;
 
12
using ICSharpCode.SharpDevelop.Refactoring;
 
13
 
 
14
namespace ICSharpCode.AvalonEdit.AddIn
 
15
{
 
16
        /// <summary>
 
17
        /// Highlights expressions (references to expression under current caret).
 
18
        /// </summary>
 
19
        public class ExpressionHighlightRenderer : IBackgroundRenderer
 
20
        {
 
21
                List<Reference> renderedReferences;
 
22
                Pen borderPen;
 
23
                Brush backgroundBrush;
 
24
                TextView textView;
 
25
                readonly Color borderColor = Color.FromArgb(52, 30, 130, 255);  //Color.FromArgb(180, 70, 230, 70))
 
26
                readonly Color fillColor = Color.FromArgb(22, 30, 130, 255);    //Color.FromArgb(40, 60, 255, 60)
 
27
                readonly int borderThickness = 1;
 
28
                readonly int cornerRadius = 1;
 
29
                
 
30
                public void SetHighlight(List<Reference> renderedReferences)
 
31
                {
 
32
                        if (this.renderedReferences != renderedReferences) {
 
33
                                this.renderedReferences = renderedReferences;
 
34
                                textView.InvalidateLayer(this.Layer);
 
35
                        }
 
36
                }
 
37
                
 
38
                public void ClearHighlight()
 
39
                {
 
40
                        this.SetHighlight(null);
 
41
                }
 
42
                
 
43
                public ExpressionHighlightRenderer(TextView textView)
 
44
                {
 
45
                        if (textView == null)
 
46
                                throw new ArgumentNullException("textView");
 
47
                        this.textView = textView;
 
48
                        this.borderPen = new Pen(new SolidColorBrush(borderColor), borderThickness);
 
49
                        this.backgroundBrush = new SolidColorBrush(fillColor);
 
50
                        this.borderPen.Freeze();
 
51
                        this.backgroundBrush.Freeze();
 
52
                        this.textView.BackgroundRenderers.Add(this);
 
53
                }
 
54
                
 
55
                public KnownLayer Layer {
 
56
                        get {
 
57
                                return KnownLayer.Selection;
 
58
                        }
 
59
                }
 
60
                
 
61
                public void Draw(TextView textView, DrawingContext drawingContext)
 
62
                {
 
63
                        if (this.renderedReferences == null)
 
64
                                return;
 
65
                        BackgroundGeometryBuilder builder = new BackgroundGeometryBuilder();
 
66
                        builder.CornerRadius = cornerRadius;
 
67
                        builder.AlignToMiddleOfPixels = true;
 
68
                        foreach (var reference in this.renderedReferences) {
 
69
                                builder.AddSegment(textView, new TextSegment() { 
 
70
                                                        StartOffset = reference.Offset, 
 
71
                                                        Length = reference.Length });
 
72
                                builder.CloseFigure();
 
73
                        }
 
74
                        Geometry geometry = builder.CreateGeometry();
 
75
                        if (geometry != null) {
 
76
                                drawingContext.DrawGeometry(backgroundBrush, borderPen, geometry);
 
77
                        }
 
78
                }
 
79
        }
 
80
}