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

« back to all changes in this revision

Viewing changes to external/maccore/src/CoreMedia/CMTextMarkupAttributes.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
// 
 
2
// CMTextMarkupAttributes.cs: Implements CMTextMarkup Attributes
 
3
//
 
4
// Authors: Marek Safar (marek.safar@gmail.com)
 
5
//     
 
6
// Copyright 2012 Xamarin Inc
 
7
//
 
8
// Permission is hereby granted, free of charge, to any person obtaining
 
9
// a copy of this software and associated documentation files (the
 
10
// "Software"), to deal in the Software without restriction, including
 
11
// without limitation the rights to use, copy, modify, merge, publish,
 
12
// distribute, sublicense, and/or sell copies of the Software, and to
 
13
// permit persons to whom the Software is furnished to do so, subject to
 
14
// the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be
 
17
// included in all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
20
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
21
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
22
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
23
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
24
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
25
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
26
//
 
27
//
 
28
 
 
29
using System;
 
30
using System.Runtime.InteropServices;
 
31
 
 
32
using MonoMac;
 
33
using MonoMac.Foundation;
 
34
using MonoMac.CoreFoundation;
 
35
using MonoMac.ObjCRuntime;
 
36
 
 
37
namespace MonoMac.CoreMedia {
 
38
 
 
39
        public struct TextMarkupColor
 
40
        {
 
41
                public TextMarkupColor (float red, float green, float blue, float alpha)
 
42
                        : this ()
 
43
                {
 
44
                        if (red < 0 || red > 1.0)
 
45
                                throw new ArgumentOutOfRangeException ("red");
 
46
                        if (green < 0 || green > 1.0)
 
47
                                throw new ArgumentOutOfRangeException ("green");
 
48
                        if (blue < 0 || blue > 1.0)
 
49
                                throw new ArgumentOutOfRangeException ("blue");
 
50
                        if (alpha < 0 || alpha > 1.0)
 
51
                                throw new ArgumentOutOfRangeException ("alpha");
 
52
 
 
53
                        Red = red;
 
54
                        Green = green;
 
55
                        Blue = blue;
 
56
                        Alpha = alpha;
 
57
                }
 
58
 
 
59
                public float Red { get; private set; }
 
60
                public float Green { get; private set; }
 
61
                public float Blue { get; private set; }
 
62
                public float Alpha { get; private set; }
 
63
        }
 
64
 
 
65
        [Since (6,0)]
 
66
        public class CMTextMarkupAttributes : DictionaryContainer
 
67
        {
 
68
                static class Keys
 
69
                {
 
70
                        public static readonly NSString ForegroundColorARGB;
 
71
                        public static readonly NSString BackgroundColorARGB;
 
72
                        public static readonly NSString BoldStyle;
 
73
                        public static readonly NSString ItalicStyle;
 
74
                        public static readonly NSString UnderlineStyle;
 
75
                        public static readonly NSString FontFamilyName;
 
76
                        public static readonly NSString RelativeFontSize;
 
77
 
 
78
                        static Keys ()
 
79
                        {
 
80
                                var handle = Dlfcn.dlopen (Constants.CoreMediaLibrary, 0);
 
81
                                if (handle == IntPtr.Zero)
 
82
                                        return;
 
83
                                try {
 
84
                                        ForegroundColorARGB  = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_ForegroundColorARGB");
 
85
                                        BackgroundColorARGB  = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_BackgroundColorARGB");
 
86
                                        BoldStyle            = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_BoldStyle");
 
87
                                        ItalicStyle          = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_ItalicStyle");
 
88
                                        UnderlineStyle       = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_UnderlineStyle");
 
89
                                        FontFamilyName       = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_FontFamilyName");
 
90
                                        RelativeFontSize     = Dlfcn.GetStringConstant (handle, "kCMTextMarkupAttribute_RelativeFontSize");
 
91
                                } finally {
 
92
                                        Dlfcn.dlclose (handle);
 
93
                                }
 
94
                        }
 
95
                }
 
96
 
 
97
                public CMTextMarkupAttributes ()
 
98
                {
 
99
                }
 
100
 
 
101
#if !COREBUILD
 
102
                public CMTextMarkupAttributes (NSDictionary dictionary)
 
103
                        : base (dictionary)
 
104
                {
 
105
                }
 
106
 
 
107
                public TextMarkupColor? ForegroundColor {
 
108
                        get {
 
109
                                var array = GetArray<NSNumber> (Keys.ForegroundColorARGB);
 
110
                                if (array == null)
 
111
                                        return null;
 
112
 
 
113
                                return new TextMarkupColor (array [1].FloatValue, array [2].FloatValue, array [3].FloatValue, array [0].FloatValue);
 
114
                        }
 
115
                        set {
 
116
                                if (value != null) {
 
117
                                        var v = value.Value;
 
118
                                        SetArrayValue (Keys.ForegroundColorARGB, new [] {
 
119
                                                NSNumber.FromFloat (v.Alpha),
 
120
                                                NSNumber.FromFloat (v.Red),
 
121
                                                NSNumber.FromFloat (v.Green),
 
122
                                                NSNumber.FromFloat (v.Blue)
 
123
                                        });
 
124
                                } else {
 
125
                                        RemoveValue (Keys.ForegroundColorARGB);
 
126
                                }
 
127
                        }
 
128
                }
 
129
 
 
130
                public TextMarkupColor? BackgroundColor {
 
131
                        get {
 
132
                                var array = GetArray<NSNumber> (Keys.BackgroundColorARGB);
 
133
                                if (array == null)
 
134
                                        return null;
 
135
 
 
136
                                return new TextMarkupColor (array [1].FloatValue, array [2].FloatValue, array [3].FloatValue, array [0].FloatValue);
 
137
                        }
 
138
                        set {
 
139
                                if (value != null) {
 
140
                                        var v = value.Value;
 
141
                                        SetArrayValue (Keys.BackgroundColorARGB, new [] {
 
142
                                                NSNumber.FromFloat (v.Alpha),
 
143
                                                NSNumber.FromFloat (v.Red),
 
144
                                                NSNumber.FromFloat (v.Green),
 
145
                                                NSNumber.FromFloat (v.Blue)
 
146
                                        });
 
147
                                } else {
 
148
                                        RemoveValue (Keys.BackgroundColorARGB);
 
149
                                }
 
150
                        }
 
151
                }
 
152
 
 
153
                public bool? Bold {
 
154
                        get {
 
155
                                return GetBoolValue (Keys.BoldStyle);
 
156
                        }
 
157
                        set {
 
158
                                SetBooleanValue (Keys.BoldStyle, value);
 
159
                        }
 
160
                }
 
161
 
 
162
                public bool? Italic {
 
163
                        get {
 
164
                                return GetBoolValue (Keys.ItalicStyle);
 
165
                        }
 
166
                        set {
 
167
                                SetBooleanValue (Keys.ItalicStyle, value);
 
168
                        }
 
169
                }
 
170
 
 
171
                public bool? Underline {
 
172
                        get {
 
173
                                return GetBoolValue (Keys.UnderlineStyle);
 
174
                        }
 
175
                        set {
 
176
                                SetBooleanValue (Keys.UnderlineStyle, value);
 
177
                        }
 
178
                }
 
179
 
 
180
                public string FontFamilyName {
 
181
                        get {
 
182
                                return GetStringValue (Keys.FontFamilyName);
 
183
                        }
 
184
                        set {
 
185
                                SetStringValue (Keys.FontFamilyName, value);
 
186
                        }
 
187
                }
 
188
 
 
189
                public int? RelativeFontSize {
 
190
                        get {
 
191
                                return GetInt32Value (Keys.RelativeFontSize);
 
192
                        }
 
193
                        set {
 
194
                                if (value < 0)
 
195
                                        throw new ArgumentOutOfRangeException ("value");
 
196
 
 
197
                                SetNumberValue (Keys.RelativeFontSize, value);
 
198
                        }
 
199
                }
 
200
#endif
 
201
        }
 
202
}