github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

NoahRic / GradientSelection

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (5)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (5)
    • 2.0
    • 1.2
    • 1.1
    • 0.2
    • 0.1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A Visual Studio 2010 extension to make the selection display as a gradient in rich-client mode. — Read more

  cancel

http://blogs.msdn.com/noahric

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Updated for RC/RTM, bumped version to 2.0. 
NoahRic (author)
Mon Feb 08 22:09:05 -0800 2010
commit  c00a865c08ee7b3a62da1764997a0cfab0e98f9d
tree    43caec76fb1eb8989905e622df1880c4a3e04098
parent  d39b83349d63d833063aadc9c9036801e2c7a38b
GradientSelection / ViewCreationListener.cs ViewCreationListener.cs
100644 143 lines (121 sloc) 4.771 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
using System.Diagnostics;
using System;
 
namespace GradientSelection
{
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("any")]
[TextViewRole(PredefinedTextViewRoles.Document)]
    internal sealed class ViewCreationListener : IWpfTextViewCreationListener
    {
[Import]
        IEditorFormatMapService formatMapService = null;
 
        /// <summary>
        /// When a text view is created, update its format map.
        /// </summary>
        public void TextViewCreated(IWpfTextView textView)
        {
            textView.Properties.GetOrCreateSingletonProperty(() =>
                     new FormatMapWatcher(textView, formatMapService.GetEditorFormatMap(textView)));
        }
    }
 
    internal sealed class FormatMapWatcher
    {
        bool inUpdate = false;
        IEditorFormatMap formatMap;
        ITextView view;
 
        static Brush gradientBrush;
        static Pen gradientBorder;
 
        static FormatMapWatcher()
        {
            Color highlight = SystemColors.HighlightColor;
            Color darkColor = Color.FromArgb(96, highlight.R, highlight.G, highlight.B);
            Color lightColor = Color.FromArgb(48, highlight.R, highlight.G, highlight.B);
 
            gradientBrush = new LinearGradientBrush(new GradientStopCollection() { new GradientStop(darkColor, 0.0),
                                                                                   new GradientStop(lightColor, 0.5),
                                                                                   new GradientStop(darkColor, 1.0) },
                                                    90.0);
            gradientBorder = new Pen(SystemColors.HighlightBrush, 1) { LineJoin = PenLineJoin.Round };
 
            gradientBrush.Freeze();
            gradientBorder.Freeze();
        }
 
        public FormatMapWatcher(ITextView view, IEditorFormatMap formatMap)
        {
            this.formatMap = formatMap;
            this.view = view;
            
            this.SetAppropriateBrush();
 
            formatMap.FormatMappingChanged += (sender, args) => SetAppropriateBrush();
 
            // Track the rich client option changing, to set and clear the gradient brush
            view.Options.OptionChanged += (sender, args) =>
            {
                if (args.OptionId == DefaultWpfViewOptions.EnableSimpleGraphicsId.Name)
                {
                    SetAppropriateBrush();
                }
            };
        }
 
        void SetAppropriateBrush()
        {
            if (inUpdate)
                return;
 
            try
            {
                inUpdate = true;
 
                if (!view.Options.GetOptionValue(DefaultWpfViewOptions.EnableSimpleGraphicsId))
                {
                    SetGradientBrush();
                }
                else
                {
                    ClearGradientBrush();
                }
            }
            finally
            {
                inUpdate = false;
            }
        }
 
        void SetGradientBrush()
        {
            // Change the selected text properties to use a gradient brush and an outline pen
            var properties = formatMap.GetProperties("Selected Text");
            
            bool modified = false;
 
            if (properties[EditorFormatDefinition.BackgroundBrushId] != gradientBrush)
            {
                modified = true;
                properties[EditorFormatDefinition.BackgroundBrushId] = gradientBrush;
            }
            if (properties["BackgroundPen"] != gradientBorder)
            {
                modified = true;
                properties["BackgroundPen"] = gradientBorder;
            }
 
            if (modified)
                formatMap.SetProperties("Selected Text", properties);
        }
 
        void ClearGradientBrush()
        {
            // Clear out the gradient brush and outline pen
            var properties = formatMap.GetProperties("Selected Text");
 
            bool modified = false;
 
            if (properties[EditorFormatDefinition.BackgroundBrushId] == gradientBrush)
            {
                modified = true;
                properties.Remove(EditorFormatDefinition.BackgroundBrushId);
            }
            if (properties["BackgroundPen"] == gradientBorder)
            {
                modified = true;
                properties.Remove("BackgroundPen");
            }
 
            if (modified)
                formatMap.SetProperties("Selected Text", properties);
        }
    }
}
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server