22
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Collections.Generic;
31
using System.Xml.Linq;
32
using System.Xml.XPath;
33
using System.Reflection;
29
namespace Mono.TextEditor
36
namespace Mono.TextEditor.Highlighting
32
public enum ChunkProperties {
37
TransparentBackground = 8
40
39
public class ChunkStyle
42
public virtual Cairo.Color CairoColor {
47
public Gdk.Color Color {
49
return (HslColor)CairoColor;
53
bool backColorIsZero = true;
54
Cairo.Color cairoBackgroundColor = new Cairo.Color (0, 0, 0, 0);
55
public virtual Cairo.Color CairoBackgroundColor {
56
get { return cairoBackgroundColor; }
57
set { cairoBackgroundColor = value; backColorIsZero = false; }
60
public bool GotBackgroundColorAssigned {
62
return !backColorIsZero;
66
public Gdk.Color BackgroundColor {
68
return (HslColor)CairoBackgroundColor;
72
public bool TransparentBackround {
74
return (ChunkProperties & ChunkProperties.TransparentBackground) == ChunkProperties.TransparentBackground || backColorIsZero;
78
public virtual ChunkProperties ChunkProperties {
41
public string Name { get; set; }
42
public Cairo.Color Foreground { get; set; }
43
public Cairo.Color Background { get; set; }
45
public bool TransparentForeground {
47
return Foreground.A == 0.0;
51
public bool TransparentBackground {
53
return Background.A == 0.0;
57
[Obsolete("Will be removed - use FontWeight")]
58
public TextWeight Weight {
60
TextWeight weight = TextWeight.None;
61
if (FontWeight == FontWeight.Bold)
62
weight |= TextWeight.Bold;
63
if (FontStyle == FontStyle.Italic)
64
weight |= TextWeight.Italic;
68
if (value.HasFlag (TextWeight.Bold)) {
69
FontWeight = FontWeight.Bold;
71
FontWeight = FontWeight.Normal;
74
if (value.HasFlag (TextWeight.Italic)) {
75
FontStyle = FontStyle.Italic;
77
FontStyle = FontStyle.Normal;
82
public FontWeight FontWeight { get; set; }
84
public FontStyle FontStyle { get; set; }
86
[Obsolete("Will be removed - use FontWeight")]
85
return (ChunkProperties & ChunkProperties.Bold) == ChunkProperties.Bold;
89
return FontWeight == FontWeight.Bold;
93
[Obsolete("Will be removed - use FontStyle")]
89
94
public bool Italic {
91
return (ChunkProperties & ChunkProperties.Italic) == ChunkProperties.Italic;
96
return FontStyle == FontStyle.Italic;
95
100
public bool Underline {
97
return (ChunkProperties & ChunkProperties.Underline) == ChunkProperties.Underline;
101
public virtual string Link {
106
public ChunkStyle (ChunkStyle style)
108
CairoColor = style.CairoColor;
109
if (!style.backColorIsZero)
110
CairoBackgroundColor = style.CairoBackgroundColor;
111
ChunkProperties = style.ChunkProperties;
114
public Pango.Style GetStyle (Pango.Style defaultStyle)
116
return Italic ? Pango.Style.Italic : Pango.Style.Normal;
119
public Pango.Weight GetWeight (Pango.Weight defaultWeight)
121
if (defaultWeight == Pango.Weight.Bold)
122
return Bold ? Pango.Weight.Heavy : Pango.Weight.Bold;
123
return Bold ? Pango.Weight.Bold : Pango.Weight.Normal;
126
104
public ChunkStyle ()
130
public ChunkStyle (Gdk.Color color)
132
this.CairoColor =(HslColor) color;
135
public ChunkStyle (Gdk.Color color, ChunkProperties chunkProperties)
137
this.CairoColor = (HslColor)color;
138
this.ChunkProperties = chunkProperties;
141
public ChunkStyle (Gdk.Color color, Gdk.Color bgColor) : this (color, bgColor, ChunkProperties.None)
145
public ChunkStyle (Gdk.Color color, Gdk.Color bgColor, ChunkProperties chunkProperties)
147
this.CairoColor = (HslColor)color;
148
this.CairoBackgroundColor = (HslColor)bgColor;
149
this.ChunkProperties = chunkProperties;
152
public ChunkStyle (Cairo.Color color, Cairo.Color bgColor, ChunkProperties chunkProperties)
154
this.CairoColor = color;
155
this.CairoBackgroundColor = bgColor;
156
this.ChunkProperties = chunkProperties;
159
public ChunkStyle (Cairo.Color color, ChunkProperties chunkProperties)
161
this.CairoColor = color;
162
this.ChunkProperties = chunkProperties;
165
public override string ToString ()
167
return string.Format ("[ChunkStyle: Color={0}, BackgroundColor={1}, TransparentBackround={2}, ChunkProperties={3}, Link={4}]", CairoColor, CairoBackgroundColor, TransparentBackround, ChunkProperties, Link);
106
Foreground = Background = new Cairo.Color (0, 0, 0, 0);
107
FontWeight = FontWeight.Normal;
108
FontStyle = FontStyle.Normal;
111
public ChunkStyle (ChunkStyle baseStyle)
113
this.Name = baseStyle.Name;
114
this.Foreground = baseStyle.Foreground;
115
this.Background = baseStyle.Background;
116
this.FontWeight = baseStyle.FontWeight;
117
this.FontStyle = baseStyle.FontStyle;
118
this.Underline = baseStyle.Underline;
121
public override bool Equals (object obj)
125
if (ReferenceEquals (this, obj))
127
if (obj.GetType () != typeof(ChunkStyle))
129
ChunkStyle other = (ChunkStyle)obj;
130
return Name == other.Name && Foreground.Equals (other.Foreground) && Background.Equals (other.Background) && FontWeight == other.FontWeight && FontStyle == other.FontStyle;
170
133
public override int GetHashCode ()
172
return CairoColor.GetHashCode () ^ Bold.GetHashCode ();
136
return (Name != null ? Name.GetHashCode () : 0) ^ Foreground.GetHashCode () ^ Background.GetHashCode () ^ FontWeight.GetHashCode () ^ FontStyle.GetHashCode ();
175
public override bool Equals (object o)
140
public static ChunkStyle Create (XElement element, Dictionary<string, Cairo.Color> palette)
177
ChunkStyle c = o as ChunkStyle;
178
return c != null && Bold == c.Bold && Italic == c.Italic && CairoColor.GetHashCode () == c.CairoColor.GetHashCode ();
142
var result = new ChunkStyle ();
144
foreach (var node in element.DescendantNodes ()) {
145
if (node.NodeType == System.Xml.XmlNodeType.Element) {
146
var el = (XElement)node;
147
switch (el.Name.LocalName) {
149
result.Name = el.Value;
152
result.Foreground = ColorScheme.ParsePaletteColor (palette, el.Value);
155
result.Background = ColorScheme.ParsePaletteColor (palette, el.Value);
159
if (!Enum.TryParse<FontWeight> (el.Value, true, out weight))
160
throw new InvalidDataException (el.Value + " is no valid text weight values are: " + string.Join (",", Enum.GetNames (typeof(FontWeight))) );
161
result.FontWeight = weight;
165
if (!Enum.TryParse<FontStyle> (el.Value, true, out style))
166
throw new InvalidDataException (el.Value + " is no valid text weight values are: " + string.Join (",", Enum.GetNames (typeof(FontStyle))) );
167
result.FontStyle = style;
170
throw new InvalidDataException ("Invalid element in text color:" + el.Name);
181
178
public Gdk.GC CreateBgGC (Gdk.Drawable drawable)
183
return new Gdk.GC (drawable) { RgbBgColor = Color, RgbFgColor = BackgroundColor };
180
return new Gdk.GC (drawable) { RgbBgColor = (HslColor)Foreground, RgbFgColor = (HslColor)Background };
186
183
public Gdk.GC CreateFgGC (Gdk.Drawable drawable)
188
return new Gdk.GC (drawable) { RgbBgColor = BackgroundColor, RgbFgColor = Color };
185
return new Gdk.GC (drawable) { RgbBgColor = (HslColor)Background, RgbFgColor = (HslColor)Foreground };
188
static string ColorToString (Cairo.Color cairoColor)
190
return "R:" + cairoColor.R + " G:" + cairoColor.G + " B:" + cairoColor.B + " A:" + cairoColor.A;
193
public override string ToString ()
195
return string.Format ("[ChunkStyle: Name={0}, CairoColor={1}, CairoBackgroundColor={2}, FontWeight={3}, FontStyle={4}]", Name, ColorToString (Foreground), ColorToString (Background), FontWeight, FontStyle);
198
public static ChunkStyle Import (string name, ColorScheme.VSSettingColor vsc)
200
var textColor = new ChunkStyle ();
201
textColor.Name = name;
202
if (!string.IsNullOrEmpty (vsc.Foreground) && vsc.Foreground != "0x02000000") {
203
textColor.Foreground = ColorScheme.ImportVsColor (vsc.Foreground);
204
if (textColor.TransparentForeground && name != "Selected Text" && name != "Selected Text(Inactive)")
205
textColor.Foreground = new Cairo.Color (0, 0, 0);
207
if (!string.IsNullOrEmpty (vsc.Background) && vsc.Background != "0x02000000")
208
textColor.Background = ColorScheme.ImportVsColor (vsc.Background);
210
textColor.FontWeight = FontWeight.Bold;
214
public ChunkStyle Clone ()
216
return (ChunkStyle)this.MemberwiseClone ();