~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor.Highlighting/ChunkStyle.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
1
2
// ChunkStyle.cs
2
3
//
3
4
// Author:
4
 
//   Mike KrĆ¼ger <mkrueger@novell.com>
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
5
6
//
6
 
// Copyright (c) 2007 Novell, Inc (http://www.novell.com)
 
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
7
8
//
8
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
10
// of this software and associated documentation files (the "Software"), to deal
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
24
25
// THE SOFTWARE.
25
 
//
26
 
//
 
26
 
27
27
using System;
 
28
using System.IO;
 
29
using System.Collections.Generic;
 
30
using System.Linq;
 
31
using System.Xml.Linq;
 
32
using System.Xml.XPath;
 
33
using System.Reflection;
 
34
using Xwt.Drawing;
28
35
 
29
 
namespace Mono.TextEditor
 
36
namespace Mono.TextEditor.Highlighting
30
37
{
31
 
        [Flags]
32
 
        public enum ChunkProperties {
33
 
                None = 0,
34
 
                Bold = 1,
35
 
                Italic = 2,
36
 
                Underline = 4,
37
 
                TransparentBackground = 8
38
 
        }
39
 
        
 
38
 
40
39
        public class ChunkStyle
41
40
        {
42
 
                public virtual Cairo.Color CairoColor {
43
 
                        get;
44
 
                        set;
45
 
                }
46
 
                
47
 
                public Gdk.Color Color {
48
 
                        get {
49
 
                                return (HslColor)CairoColor;
50
 
                        }
51
 
                }
52
 
                
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; }
58
 
                }
59
 
                
60
 
                public bool GotBackgroundColorAssigned {
61
 
                        get {
62
 
                                return !backColorIsZero;
63
 
                        }
64
 
                }
65
 
                
66
 
                public Gdk.Color BackgroundColor {
67
 
                        get {
68
 
                                return (HslColor)CairoBackgroundColor;
69
 
                        }
70
 
                }
71
 
                
72
 
                public bool TransparentBackround {
73
 
                        get {
74
 
                                return (ChunkProperties & ChunkProperties.TransparentBackground) == ChunkProperties.TransparentBackground || backColorIsZero; 
75
 
                        }
76
 
                }
77
 
                
78
 
                public virtual ChunkProperties ChunkProperties {
79
 
                        get;
80
 
                        set;
81
 
                }
82
 
 
 
41
                public string Name { get; set; }
 
42
                public Cairo.Color Foreground { get; set; }
 
43
                public Cairo.Color Background { get; set; }
 
44
 
 
45
                public bool TransparentForeground {
 
46
                        get {
 
47
                                return Foreground.A == 0.0;
 
48
 
 
49
                        }
 
50
                }
 
51
                public bool TransparentBackground {
 
52
                        get {
 
53
                                return Background.A == 0.0;
 
54
                        }
 
55
                }
 
56
 
 
57
                [Obsolete("Will be removed - use FontWeight")]
 
58
                public TextWeight Weight { 
 
59
                        get { 
 
60
                                TextWeight weight = TextWeight.None;
 
61
                                if (FontWeight == FontWeight.Bold)
 
62
                                        weight |= TextWeight.Bold;
 
63
                                if (FontStyle == FontStyle.Italic)
 
64
                                        weight |= TextWeight.Italic;
 
65
                                return weight;
 
66
                        } 
 
67
                        set {
 
68
                                if (value.HasFlag (TextWeight.Bold)) {
 
69
                                        FontWeight = FontWeight.Bold;
 
70
                                } else {
 
71
                                        FontWeight = FontWeight.Normal;
 
72
                                }
 
73
 
 
74
                                if (value.HasFlag (TextWeight.Italic)) {
 
75
                                        FontStyle = FontStyle.Italic;
 
76
                                } else {
 
77
                                        FontStyle = FontStyle.Normal;
 
78
                                }
 
79
                        }
 
80
                }
 
81
 
 
82
                public FontWeight FontWeight { get; set; }
 
83
 
 
84
                public FontStyle FontStyle { get; set; }
 
85
 
 
86
                [Obsolete("Will be removed - use FontWeight")]
83
87
                public bool Bold {
84
88
                        get {
85
 
                                return (ChunkProperties & ChunkProperties.Bold) == ChunkProperties.Bold;
 
89
                                return FontWeight == FontWeight.Bold;
86
90
                        }
87
91
                }
88
 
 
 
92
                
 
93
                [Obsolete("Will be removed - use FontStyle")]
89
94
                public bool Italic {
90
95
                        get {
91
 
                                return (ChunkProperties & ChunkProperties.Italic) == ChunkProperties.Italic;
 
96
                                return FontStyle == FontStyle.Italic;
92
97
                        }
93
98
                }
94
 
 
 
99
                
95
100
                public bool Underline {
96
 
                        get {
97
 
                                return (ChunkProperties & ChunkProperties.Underline) == ChunkProperties.Underline;
98
 
                        }
99
 
                }
100
 
 
101
 
                public virtual string Link {
102
 
                        get;
103
 
                        set;
104
 
                }
105
 
                
106
 
                public ChunkStyle (ChunkStyle style)
107
 
                {
108
 
                        CairoColor           = style.CairoColor;
109
 
                        if (!style.backColorIsZero)
110
 
                                CairoBackgroundColor = style.CairoBackgroundColor;
111
 
                        ChunkProperties      = style.ChunkProperties;
112
 
                }
113
 
 
114
 
                public Pango.Style GetStyle (Pango.Style defaultStyle)
115
 
                {
116
 
                        return Italic ? Pango.Style.Italic : Pango.Style.Normal;
117
 
                }
118
 
                
119
 
                public Pango.Weight GetWeight (Pango.Weight defaultWeight)
120
 
                {
121
 
                        if (defaultWeight == Pango.Weight.Bold)
122
 
                                return Bold ? Pango.Weight.Heavy : Pango.Weight.Bold;
123
 
                        return Bold ? Pango.Weight.Bold : Pango.Weight.Normal;
124
 
                }
125
 
                
 
101
                        get; set;
 
102
                }
 
103
 
126
104
                public ChunkStyle ()
127
105
                {
128
 
                }
129
 
                
130
 
                public ChunkStyle (Gdk.Color color)
131
 
                {
132
 
                        this.CairoColor =(HslColor) color;
133
 
                }
134
 
                
135
 
                public ChunkStyle (Gdk.Color color, ChunkProperties chunkProperties)
136
 
                {
137
 
                        this.CairoColor      = (HslColor)color;
138
 
                        this.ChunkProperties = chunkProperties;
139
 
                }
140
 
                
141
 
                public ChunkStyle (Gdk.Color color, Gdk.Color bgColor) : this (color, bgColor, ChunkProperties.None)
142
 
                {
143
 
                }
144
 
                
145
 
                public ChunkStyle (Gdk.Color color, Gdk.Color bgColor, ChunkProperties chunkProperties)
146
 
                {
147
 
                        this.CairoColor           = (HslColor)color;
148
 
                        this.CairoBackgroundColor = (HslColor)bgColor;
149
 
                        this.ChunkProperties = chunkProperties;
150
 
                }
151
 
                
152
 
                public ChunkStyle (Cairo.Color color, Cairo.Color bgColor, ChunkProperties chunkProperties)
153
 
                {
154
 
                        this.CairoColor           = color;
155
 
                        this.CairoBackgroundColor = bgColor;
156
 
                        this.ChunkProperties = chunkProperties;
157
 
                }
158
 
                
159
 
                public ChunkStyle (Cairo.Color color, ChunkProperties chunkProperties)
160
 
                {
161
 
                        this.CairoColor           = color;
162
 
                        this.ChunkProperties = chunkProperties;
163
 
                }
164
 
 
165
 
                public override string ToString ()
166
 
                {
167
 
                        return string.Format ("[ChunkStyle: Color={0}, BackgroundColor={1}, TransparentBackround={2}, ChunkProperties={3}, Link={4}]", CairoColor, CairoBackgroundColor, TransparentBackround, ChunkProperties, Link);
168
 
                }
169
 
                
 
106
                        Foreground = Background = new Cairo.Color (0, 0, 0, 0);
 
107
                        FontWeight = FontWeight.Normal;
 
108
                        FontStyle = FontStyle.Normal;
 
109
                }
 
110
 
 
111
                public ChunkStyle (ChunkStyle baseStyle)
 
112
                {
 
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;
 
119
                }
 
120
 
 
121
                public override bool Equals (object obj)
 
122
                {
 
123
                        if (obj == null)
 
124
                                return false;
 
125
                        if (ReferenceEquals (this, obj))
 
126
                                return true;
 
127
                        if (obj.GetType () != typeof(ChunkStyle))
 
128
                                return false;
 
129
                        ChunkStyle other = (ChunkStyle)obj;
 
130
                        return Name == other.Name && Foreground.Equals (other.Foreground) && Background.Equals (other.Background) && FontWeight == other.FontWeight && FontStyle == other.FontStyle;
 
131
                }
 
132
 
170
133
                public override int GetHashCode ()
171
134
                {
172
 
                        return CairoColor.GetHashCode () ^ Bold.GetHashCode ();
 
135
                        unchecked {
 
136
                                return (Name != null ? Name.GetHashCode () : 0) ^ Foreground.GetHashCode () ^ Background.GetHashCode () ^ FontWeight.GetHashCode () ^ FontStyle.GetHashCode ();
 
137
                        }
173
138
                }
174
139
 
175
 
                public override bool Equals (object o)
 
140
                public static ChunkStyle Create (XElement element, Dictionary<string, Cairo.Color> palette)
176
141
                {
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 ();
 
143
 
 
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) {
 
148
                                        case "name":
 
149
                                                result.Name = el.Value;
 
150
                                                break;
 
151
                                        case "fore":
 
152
                                                result.Foreground = ColorScheme.ParsePaletteColor (palette, el.Value);
 
153
                                                break;
 
154
                                        case "back":
 
155
                                                result.Background = ColorScheme.ParsePaletteColor (palette, el.Value);
 
156
                                                break;
 
157
                                        case "weight":
 
158
                                                FontWeight weight;
 
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;
 
162
                                                break;
 
163
                                        case "style":
 
164
                                                FontStyle style;
 
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;
 
168
                                                break;
 
169
                                        default:
 
170
                                                throw new InvalidDataException ("Invalid element in text color:" + el.Name);
 
171
                                        }
 
172
                                }
 
173
                        }
 
174
 
 
175
                        return result;
179
176
                }
180
 
                
 
177
 
181
178
                public Gdk.GC CreateBgGC (Gdk.Drawable drawable)
182
179
                {
183
 
                        return new Gdk.GC (drawable) { RgbBgColor = Color, RgbFgColor = BackgroundColor };
 
180
                        return new Gdk.GC (drawable) { RgbBgColor = (HslColor)Foreground, RgbFgColor = (HslColor)Background };
184
181
                }
185
182
                
186
183
                public Gdk.GC CreateFgGC (Gdk.Drawable drawable)
187
184
                {
188
 
                        return new Gdk.GC (drawable) { RgbBgColor = BackgroundColor, RgbFgColor = Color };
 
185
                        return new Gdk.GC (drawable) { RgbBgColor = (HslColor)Background, RgbFgColor = (HslColor)Foreground };
 
186
                }
 
187
 
 
188
                static string ColorToString (Cairo.Color cairoColor)
 
189
                {
 
190
                        return "R:" + cairoColor.R + " G:" + cairoColor.G + " B:" + cairoColor.B + " A:" + cairoColor.A;
 
191
                }
 
192
 
 
193
                public override string ToString ()
 
194
                {
 
195
                        return string.Format ("[ChunkStyle: Name={0}, CairoColor={1}, CairoBackgroundColor={2}, FontWeight={3}, FontStyle={4}]", Name, ColorToString (Foreground), ColorToString (Background), FontWeight, FontStyle);
 
196
                }
 
197
 
 
198
                public static ChunkStyle Import (string name, ColorScheme.VSSettingColor vsc)
 
199
                {
 
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);
 
206
                        }
 
207
                        if (!string.IsNullOrEmpty (vsc.Background) && vsc.Background != "0x02000000")
 
208
                                textColor.Background = ColorScheme.ImportVsColor (vsc.Background);
 
209
                        if (vsc.BoldFont)
 
210
                                textColor.FontWeight = FontWeight.Bold;
 
211
                        return textColor;
 
212
                }
 
213
 
 
214
                public ChunkStyle Clone ()
 
215
                {
 
216
                        return (ChunkStyle)this.MemberwiseClone ();
189
217
                }
190
218
        }
 
219
        
191
220
}