Code Coverage Statistics for Source File

c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Document\HighlightingStrategy\HighlightColor.cs

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
280
Highlight: Uncovered Code Covered Code
L V Source
1
// <file>
2
//     <copyright see="prj:///doc/copyright.txt"/>
3
//     <license see="prj:///doc/license.txt"/>
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
//     <version>$Revision: 1965 $</version>
6
// </file>
7
8
using System;
9
using System.Diagnostics;
10
using System.Drawing;
11
using System.Globalization;
12
using System.Reflection;
13
using System.Xml;
14
15
namespace ICSharpCode.TextEditor.Document
16
{
17
	/// <summary>
18
	/// A color used for highlighting
19
	/// </summary>
20
	public class HighlightColor
21
	{
22
		bool   systemColor     = false;
23
		string systemColorName = null;
24
		
25
		bool   systemBgColor     = false;
26
		string systemBgColorName = null;
27
		
28
		Color  color;
29
		Color  backgroundcolor = System.Drawing.Color.WhiteSmoke;
30
		
31
		bool   bold   = false;
32
		bool   italic = false;
33
		bool   hasForeground = false;
34
		bool   hasBackground = false;
35
		
36
		public bool HasForeground {
37
			get {
38
				return hasForeground;
39
			}
40
		}
41
		
42
		public bool HasBackground {
43
			get {
44
				return hasBackground;
45
			}
46
		}
47
		
48
		
49
		/// <value>
50
		/// If true the font will be displayed bold style
51
		/// </value>
52
		public bool Bold {
53
			get {
54
				return bold;
55
			}
56
		}
57
		
58
		/// <value>
59
		/// If true the font will be displayed italic style
60
		/// </value>
61
		public bool Italic {
62
			get {
63
				return italic;
64
			}
65
		}
66
		
67
		/// <value>
68
		/// The background color used
69
		/// </value>
70
		public Color BackgroundColor {
71
			get {
72
				if (!systemBgColor) {
73
					return backgroundcolor;
74
				}
75
				return ParseColorString(systemBgColorName);
76
			}
77
		}
78
		
79
		/// <value>
80
		/// The foreground color used
81
		/// </value>
82
		public Color Color {
83
			get {
84
				if (!systemColor) {
85
					return color;
86
				}
87
				return ParseColorString(systemColorName);
88
			}
89
		}
90
		
91
		/// <value>
92
		/// The font used
93
		/// </value>
94
		public Font GetFont(FontContainer fontContainer)
95
		{
96
			if (Bold) {
97
				return Italic ? fontContainer.BoldItalicFont : fontContainer.BoldFont;
98
			}
99
			return Italic ? fontContainer.ItalicFont : fontContainer.RegularFont;
100
		}
101
		
102
		Color ParseColorString(string colorName)
103
		{
104
			string[] cNames = colorName.Split('*');
105
			PropertyInfo myPropInfo = typeof(System.Drawing.SystemColors).GetProperty(cNames[0], BindingFlags.Public |
106
			                                                                          BindingFlags.Instance |
107
			                                                                          BindingFlags.Static);
108
			Color c = (Color)myPropInfo.GetValue(null, null);
109
			
110
			if (cNames.Length == 2) {
111
				// hack : can't figure out how to parse doubles with '.' (culture info might set the '.' to ',')
112
				double factor = Double.Parse(cNames[1]) / 100;
113
				c = Color.FromArgb((int)((double)c.R * factor), (int)((double)c.G * factor), (int)((double)c.B * factor));
114
			}
115
			
116
			return c;
117
		}
118
		
119
		/// <summary>
120
		/// Creates a new instance of <see cref="HighlightColor"/>
121
		/// </summary>
122
		public HighlightColor(XmlElement el)
123
		{
124
			Debug.Assert(el != null, "ICSharpCode.TextEditor.Document.SyntaxColor(XmlElement el) : el == null");
125
			if (el.Attributes["bold"] != null) {
126
				bold = Boolean.Parse(el.Attributes["bold"].InnerText);
127
			}
128
			
129
			if (el.Attributes["italic"] != null) {
130
				italic = Boolean.Parse(el.Attributes["italic"].InnerText);
131
			}
132
			
133
			if (el.Attributes["color"] != null) {
134
				string c = el.Attributes["color"].InnerText;
135
				if (c[0] == '#') {
136
					color = ParseColor(c);
137
				} else if (c.StartsWith("SystemColors.")) {
138
					systemColor     = true;
139
					systemColorName = c.Substring("SystemColors.".Length);
140
				} else {
141
					color = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
142
				}
143
				hasForeground = true;
144
			} else {
145
				color = Color.Transparent; // to set it to the default value.
146
			}
147
			
148
			if (el.Attributes["bgcolor"] != null) {
149
				string c = el.Attributes["bgcolor"].InnerText;
150
				if (c[0] == '#') {
151
					backgroundcolor = ParseColor(c);
152
				} else if (c.StartsWith("SystemColors.")) {
153
					systemBgColor     = true;
154
					systemBgColorName = c.Substring("SystemColors.".Length);
155
				} else {
156
					backgroundcolor = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
157
				}
158
				hasBackground = true;
159
			}
160
		}
161
		
162
		/// <summary>
163
		/// Creates a new instance of <see cref="HighlightColor"/>
164
		/// </summary>
165
		public HighlightColor(XmlElement el, HighlightColor defaultColor)
166
		{
167
			Debug.Assert(el != null, "ICSharpCode.TextEditor.Document.SyntaxColor(XmlElement el) : el == null");
168
			if (el.Attributes["bold"] != null) {
169
				bold = Boolean.Parse(el.Attributes["bold"].InnerText);
170
			} else {
171
				bold = defaultColor.Bold;
172
			}
173
			
174
			if (el.Attributes["italic"] != null) {
175
				italic = Boolean.Parse(el.Attributes["italic"].InnerText);
176
			} else {
177
				italic = defaultColor.Italic;
178
			}
179
			
180
			if (el.Attributes["color"] != null) {
181
				string c = el.Attributes["color"].InnerText;
182
				if (c[0] == '#') {
183
					color = ParseColor(c);
184
				} else if (c.StartsWith("SystemColors.")) {
185
					systemColor     = true;
186
					systemColorName = c.Substring("SystemColors.".Length);
187
				} else {
188
					color = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
189
				}
190
				hasForeground = true;
191
			} else {
192
				color = defaultColor.color;
193
			}
194
			
195
			if (el.Attributes["bgcolor"] != null) {
196
				string c = el.Attributes["bgcolor"].InnerText;
197
				if (c[0] == '#') {
198
					backgroundcolor = ParseColor(c);
199
				} else if (c.StartsWith("SystemColors.")) {
200
					systemBgColor     = true;
201
					systemBgColorName = c.Substring("SystemColors.".Length);
202
				} else {
203
					backgroundcolor = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
204
				}
205
				hasBackground = true;
206
			} else {
207
				backgroundcolor = defaultColor.BackgroundColor;
208
			}
209
		}
210
		
211
		/// <summary>
212
		/// Creates a new instance of <see cref="HighlightColor"/>
213
		/// </summary>
214
		public HighlightColor(Color color, bool bold, bool italic)
215
		{
216
			hasForeground = true;
217
			this.color  = color;
218
			this.bold   = bold;
219
			this.italic = italic;
220
		}
221
		
222
		/// <summary>
223
		/// Creates a new instance of <see cref="HighlightColor"/>
224
		/// </summary>
225
		public HighlightColor(Color color, Color backgroundcolor, bool bold, bool italic)
226
		{
227
			hasForeground = true;
228
			hasBackground  = true;
229
			this.color            = color;
230
			this.backgroundcolor  = backgroundcolor;
231
			this.bold             = bold;
232
			this.italic           = italic;
233
		}
234
		
235
		
236
		/// <summary>
237
		/// Creates a new instance of <see cref="HighlightColor"/>
238
		/// </summary>
239
		public HighlightColor(string systemColor, string systemBackgroundColor, bool bold, bool italic)
240
		{
241
			hasForeground = true;
242
			hasBackground  = true;
243
			
244
			this.systemColor  = true;
245
			systemColorName   = systemColor;
246
			
247
			systemBgColor     = true;
248
			systemBgColorName = systemBackgroundColor;
249
			
250
			this.bold         = bold;
251
			this.italic       = italic;
252
		}
253
		
254
		static Color ParseColor(string c)
255
		{
256
			int a = 255;
257
			int offset = 0;
258
			if (c.Length > 7) {
259
				offset = 2;
260
				a = Int32.Parse(c.Substring(1,2), NumberStyles.HexNumber);
261
			}
262
			
263
			int r = Int32.Parse(c.Substring(1 + offset,2), NumberStyles.HexNumber);
264
			int g = Int32.Parse(c.Substring(3 + offset,2), NumberStyles.HexNumber);
265
			int b = Int32.Parse(c.Substring(5 + offset,2), NumberStyles.HexNumber);
266
			return Color.FromArgb(a, r, g, b);
267
		}
268
		
269
		/// <summary>
270
		/// Converts a <see cref="HighlightColor"/> instance to string (for debug purposes)
271
		/// </summary>
272
		public override string ToString()
273
		{
274
			return "[HighlightColor: Bold = " + Bold +
275
				", Italic = " + Italic +
276
				", Color = " + Color +
277
				", BackgroundColor = " + BackgroundColor + "]";
278
		}
279
	}
280
}