Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Highlighting/HighlightingBrush.cs @ 13508

Last change on this file since 13508 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.8 KB
Line 
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 System.Diagnostics;
21using System.Globalization;
22using System.Reflection;
23using System.Runtime.Serialization;
24using System.Windows;
25using System.Windows.Media;
26
27using ICSharpCode.AvalonEdit.Rendering;
28
29namespace ICSharpCode.AvalonEdit.Highlighting
30{
31  /// <summary>
32  /// A brush used for syntax highlighting. Can retrieve a real brush on-demand.
33  /// </summary>
34  [Serializable]
35  public abstract class HighlightingBrush
36  {
37    /// <summary>
38    /// Gets the real brush.
39    /// </summary>
40    /// <param name="context">The construction context. context can be null!</param>
41    public abstract Brush GetBrush(ITextRunConstructionContext context);
42   
43    /// <summary>
44    /// Gets the color of the brush.
45    /// </summary>
46    /// <param name="context">The construction context. context can be null!</param>
47    public virtual Color? GetColor(ITextRunConstructionContext context)
48    {
49      SolidColorBrush scb = GetBrush(context) as SolidColorBrush;
50      if (scb != null)
51        return scb.Color;
52      else
53        return null;
54    }
55  }
56 
57  /// <summary>
58  /// Highlighting brush implementation that takes a frozen brush.
59  /// </summary>
60  [Serializable]
61  public sealed class SimpleHighlightingBrush : HighlightingBrush, ISerializable
62  {
63    readonly SolidColorBrush brush;
64   
65    internal SimpleHighlightingBrush(SolidColorBrush brush)
66    {
67      brush.Freeze();
68      this.brush = brush;
69    }
70   
71    /// <inheritdoc/>
72    public SimpleHighlightingBrush(Color color) : this(new SolidColorBrush(color)) {}
73   
74    /// <inheritdoc/>
75    public override Brush GetBrush(ITextRunConstructionContext context)
76    {
77      return brush;
78    }
79
80    /// <inheritdoc/>
81    public override string ToString()
82    {
83      return brush.ToString();
84    }
85   
86    SimpleHighlightingBrush(SerializationInfo info, StreamingContext context)
87    {
88      this.brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(info.GetString("color")));
89      brush.Freeze();
90    }
91   
92    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
93    {
94      info.AddValue("color", brush.Color.ToString(CultureInfo.InvariantCulture));
95    }
96   
97    /// <inheritdoc/>
98    public override bool Equals(object obj)
99    {
100      SimpleHighlightingBrush other = obj as SimpleHighlightingBrush;
101      if (other == null)
102        return false;
103      return this.brush.Color.Equals(other.brush.Color);
104    }
105   
106    /// <inheritdoc/>
107    public override int GetHashCode()
108    {
109      return brush.Color.GetHashCode();
110    }
111  }
112 
113  /// <summary>
114  /// HighlightingBrush implementation that finds a brush using a resource.
115  /// </summary>
116  [Serializable]
117  sealed class SystemColorHighlightingBrush : HighlightingBrush, ISerializable
118  {
119    readonly PropertyInfo property;
120   
121    public SystemColorHighlightingBrush(PropertyInfo property)
122    {
123      Debug.Assert(property.ReflectedType == typeof(SystemColors));
124      Debug.Assert(typeof(Brush).IsAssignableFrom(property.PropertyType));
125      this.property = property;
126    }
127   
128    public override Brush GetBrush(ITextRunConstructionContext context)
129    {
130      return (Brush)property.GetValue(null, null);
131    }
132   
133    public override string ToString()
134    {
135      return property.Name;
136    }
137   
138    SystemColorHighlightingBrush(SerializationInfo info, StreamingContext context)
139    {
140      property = typeof(SystemColors).GetProperty(info.GetString("propertyName"));
141      if (property == null)
142        throw new ArgumentException("Error deserializing SystemColorHighlightingBrush");
143    }
144   
145    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
146    {
147      info.AddValue("propertyName", property.Name);
148    }
149   
150    public override bool Equals(object obj)
151    {
152      SystemColorHighlightingBrush other = obj as SystemColorHighlightingBrush;
153      if (other == null)
154        return false;
155      return object.Equals(this.property, other.property);
156    }
157   
158    public override int GetHashCode()
159    {
160      return property.GetHashCode();
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.