Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.11/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Highlighting/HighlightingColor.cs @ 13398

Last change on this file since 13398 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 8.2 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.Globalization;
21using System.Runtime.Serialization;
22using System.Security.Permissions;
23using System.Text;
24using System.Windows;
25using System.Windows.Media;
26using ICSharpCode.AvalonEdit.Utils;
27
28namespace ICSharpCode.AvalonEdit.Highlighting
29{
30  /// <summary>
31  /// A highlighting color is a set of font properties and foreground and background color.
32  /// </summary>
33  [Serializable]
34  public class HighlightingColor : ISerializable, IFreezable, ICloneable, IEquatable<HighlightingColor>
35  {
36    internal static readonly HighlightingColor Empty = FreezableHelper.FreezeAndReturn(new HighlightingColor());
37   
38    string name;
39    FontWeight? fontWeight;
40    FontStyle? fontStyle;
41    HighlightingBrush foreground;
42    HighlightingBrush background;
43    bool frozen;
44   
45    /// <summary>
46    /// Gets/Sets the name of the color.
47    /// </summary>
48    public string Name {
49      get {
50        return name;
51      }
52      set {
53        if (frozen)
54          throw new InvalidOperationException();
55        name = value;
56      }
57    }
58   
59    /// <summary>
60    /// Gets/sets the font weight. Null if the highlighting color does not change the font weight.
61    /// </summary>
62    public FontWeight? FontWeight {
63      get {
64        return fontWeight;
65      }
66      set {
67        if (frozen)
68          throw new InvalidOperationException();
69        fontWeight = value;
70      }
71    }
72   
73    /// <summary>
74    /// Gets/sets the font style. Null if the highlighting color does not change the font style.
75    /// </summary>
76    public FontStyle? FontStyle {
77      get {
78        return fontStyle;
79      }
80      set {
81        if (frozen)
82          throw new InvalidOperationException();
83        fontStyle = value;
84      }
85    }
86   
87    /// <summary>
88    /// Gets/sets the foreground color applied by the highlighting.
89    /// </summary>
90    public HighlightingBrush Foreground {
91      get {
92        return foreground;
93      }
94      set {
95        if (frozen)
96          throw new InvalidOperationException();
97        foreground = value;
98      }
99    }
100   
101    /// <summary>
102    /// Gets/sets the background color applied by the highlighting.
103    /// </summary>
104    public HighlightingBrush Background {
105      get {
106        return background;
107      }
108      set {
109        if (frozen)
110          throw new InvalidOperationException();
111        background = value;
112      }
113    }
114   
115    /// <summary>
116    /// Creates a new HighlightingColor instance.
117    /// </summary>
118    public HighlightingColor()
119    {
120    }
121   
122    /// <summary>
123    /// Deserializes a HighlightingColor.
124    /// </summary>
125    protected HighlightingColor(SerializationInfo info, StreamingContext context)
126    {
127      if (info == null)
128        throw new ArgumentNullException("info");
129      this.Name = info.GetString("Name");
130      if (info.GetBoolean("HasWeight"))
131        this.FontWeight = System.Windows.FontWeight.FromOpenTypeWeight(info.GetInt32("Weight"));
132      if (info.GetBoolean("HasStyle"))
133        this.FontStyle = (FontStyle?)new FontStyleConverter().ConvertFromInvariantString(info.GetString("Style"));
134      this.Foreground = (HighlightingBrush)info.GetValue("Foreground", typeof(HighlightingBrush));
135      this.Background = (HighlightingBrush)info.GetValue("Background", typeof(HighlightingBrush));
136    }
137   
138    /// <summary>
139    /// Serializes this HighlightingColor instance.
140    /// </summary>
141    #if DOTNET4
142    [System.Security.SecurityCritical]
143    #else
144    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
145    #endif
146    public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
147    {
148      if (info == null)
149        throw new ArgumentNullException("info");
150      info.AddValue("Name", this.Name);
151      info.AddValue("HasWeight", this.FontWeight.HasValue);
152      if (this.FontWeight.HasValue)
153        info.AddValue("Weight", this.FontWeight.Value.ToOpenTypeWeight());
154      info.AddValue("HasStyle", this.FontStyle.HasValue);
155      if (this.FontStyle.HasValue)
156        info.AddValue("Style", this.FontStyle.Value.ToString());
157      info.AddValue("Foreground", this.Foreground);
158      info.AddValue("Background", this.Background);
159    }
160   
161    /// <summary>
162    /// Gets CSS code for the color.
163    /// </summary>
164    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "CSS usually uses lowercase, and all possible values are English-only")]
165    public virtual string ToCss()
166    {
167      StringBuilder b = new StringBuilder();
168      if (Foreground != null) {
169        Color? c = Foreground.GetColor(null);
170        if (c != null) {
171          b.AppendFormat(CultureInfo.InvariantCulture, "color: #{0:x2}{1:x2}{2:x2}; ", c.Value.R, c.Value.G, c.Value.B);
172        }
173      }
174      if (FontWeight != null) {
175        b.Append("font-weight: ");
176        b.Append(FontWeight.Value.ToString().ToLowerInvariant());
177        b.Append("; ");
178      }
179      if (FontStyle != null) {
180        b.Append("font-style: ");
181        b.Append(FontStyle.Value.ToString().ToLowerInvariant());
182        b.Append("; ");
183      }
184      return b.ToString();
185    }
186   
187    /// <inheritdoc/>
188    public override string ToString()
189    {
190      return "[" + GetType().Name + " " + (string.IsNullOrEmpty(this.Name) ? ToCss() : this.Name) + "]";
191    }
192   
193    /// <summary>
194    /// Prevent further changes to this highlighting color.
195    /// </summary>
196    public virtual void Freeze()
197    {
198      frozen = true;
199    }
200   
201    /// <summary>
202    /// Gets whether this HighlightingColor instance is frozen.
203    /// </summary>
204    public bool IsFrozen {
205      get { return frozen; }
206    }
207   
208    /// <summary>
209    /// Clones this highlighting color.
210    /// If this color is frozen, the clone will be unfrozen.
211    /// </summary>
212    public virtual HighlightingColor Clone()
213    {
214      HighlightingColor c = (HighlightingColor)MemberwiseClone();
215      c.frozen = false;
216      return c;
217    }
218   
219    object ICloneable.Clone()
220    {
221      return Clone();
222    }
223   
224    /// <inheritdoc/>
225    public override sealed bool Equals(object obj)
226    {
227      return Equals(obj as HighlightingColor);
228    }
229   
230    /// <inheritdoc/>
231    public virtual bool Equals(HighlightingColor other)
232    {
233      if (other == null)
234        return false;
235      return this.name == other.name && this.fontWeight == other.fontWeight && this.fontStyle == other.fontStyle && object.Equals(this.foreground, other.foreground) && object.Equals(this.background, other.background);
236    }
237   
238    /// <inheritdoc/>
239    public override int GetHashCode()
240    {
241      int hashCode = 0;
242      unchecked {
243        if (name != null)
244          hashCode += 1000000007 * name.GetHashCode();
245        hashCode += 1000000009 * fontWeight.GetHashCode();
246        hashCode += 1000000021 * fontStyle.GetHashCode();
247        if (foreground != null)
248          hashCode += 1000000033 * foreground.GetHashCode();
249        if (background != null)
250          hashCode += 1000000087 * background.GetHashCode();
251      }
252      return hashCode;
253    }
254   
255    /// <summary>
256    /// Overwrites the properties in this HighlightingColor with those from the given color;
257    /// but maintains the current values where the properties of the given color return <c>null</c>.
258    /// </summary>
259    public void MergeWith(HighlightingColor color)
260    {
261      FreezableHelper.ThrowIfFrozen(this);
262      if (color.fontWeight != null)
263        this.fontWeight = color.fontWeight;
264      if (color.fontStyle != null)
265        this.fontStyle = color.fontStyle;
266      if (color.foreground != null)
267        this.foreground = color.foreground;
268      if (color.background != null)
269        this.background = color.background;
270    }
271   
272    internal bool IsEmptyForMerge {
273      get {
274        return fontWeight == null && fontStyle == null && foreground == null && background == null;
275      }
276    }
277  }
278}
Note: See TracBrowser for help on using the repository browser.