2 // System.Web.UI.WebControls.FontUnit.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Ben Maurer (bmaurer@ximian.com).
8 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Threading
;
31 using System
.Globalization
;
32 using System
.ComponentModel
;
33 using System
.Security
.Permissions
;
34 using System
.Web
.Util
;
36 namespace System
.Web
.UI
.WebControls
38 [TypeConverter (typeof (FontUnitConverter
))]
40 public struct FontUnit
45 public static readonly FontUnit Empty
;
46 public static readonly FontUnit Smaller
= new FontUnit (FontSize
.Smaller
);
47 public static readonly FontUnit Larger
= new FontUnit (FontSize
.Larger
);
48 public static readonly FontUnit XXSmall
= new FontUnit (FontSize
.XXSmall
);
49 public static readonly FontUnit XSmall
= new FontUnit (FontSize
.XSmall
);
50 public static readonly FontUnit Small
= new FontUnit (FontSize
.Small
);
51 public static readonly FontUnit Medium
= new FontUnit (FontSize
.Medium
);
52 public static readonly FontUnit Large
= new FontUnit (FontSize
.Large
);
53 public static readonly FontUnit XLarge
= new FontUnit (FontSize
.XLarge
);
54 public static readonly FontUnit XXLarge
= new FontUnit (FontSize
.XXLarge
);
56 static string [] font_size_names
= new string [] {null, null, "Smaller", "Larger", "XX-Small", "X-Small", "Small",
57 "Medium", "Large", "X-Large", "XX-Large" };
59 public FontUnit (FontSize type
)
63 if (t
< 0 || t
> (int)FontSize
.XXLarge
)
64 throw new ArgumentOutOfRangeException ("type");
68 if (type
== FontSize
.AsUnit
)
69 unit
= new Unit (10, UnitType
.Point
);
74 public FontUnit (int value) : this (new Unit (value, UnitType
.Point
))
78 public FontUnit (double value) : this (new Unit (value, UnitType
.Point
))
82 public FontUnit (double value, UnitType type
) : this (new Unit (value, type
))
86 public FontUnit (Unit
value)
88 type
= FontSize
.AsUnit
;
92 public FontUnit (string value) : this (value, Thread
.CurrentThread
.CurrentCulture
)
95 public FontUnit (string value, CultureInfo culture
)
97 if (String
.IsNullOrEmpty (value)) {
98 type
= FontSize
.NotSet
;
103 switch (value.ToLower (Helpers
.InvariantCulture
)) {
105 type
= FontSize
.Smaller
;
108 type
= FontSize
.Larger
;
111 type
= FontSize
.XXSmall
;
114 type
= FontSize
.XXSmall
;
117 type
= FontSize
.XSmall
;
120 type
= FontSize
.XSmall
;
123 type
= FontSize
.Small
;
126 type
= FontSize
.Medium
;
129 type
= FontSize
.Large
;
132 type
= FontSize
.XLarge
;
135 type
= FontSize
.XLarge
;
138 type
= FontSize
.XXLarge
;
141 type
= FontSize
.XXLarge
;
144 type
= FontSize
.AsUnit
;
145 unit
= new Unit (value, culture
);
151 public bool IsEmpty
{
152 get { return type == FontSize.NotSet; }
155 public FontSize Type
{
163 public static FontUnit
Parse (string s
)
165 return new FontUnit (s
);
168 public static FontUnit
Parse (string s
, CultureInfo culture
)
170 return new FontUnit (s
, culture
);
173 public static FontUnit
Point (int n
)
175 return new FontUnit (n
);
178 public override bool Equals (object obj
)
180 if (obj
is FontUnit
) {
181 FontUnit other
= (FontUnit
) obj
;
182 return (other
.type
== type
&& other
.unit
== unit
);
187 public override int GetHashCode ()
189 return type
.GetHashCode () ^ unit
.GetHashCode ();
192 public static bool operator == (FontUnit left
, FontUnit right
)
194 return left
.type
== right
.type
&& left
.unit
== right
.unit
;
197 public static bool operator != (FontUnit left
, FontUnit right
)
199 return left
.type
!= right
.type
|| left
.unit
!= right
.unit
;
202 public static implicit operator FontUnit (int n
)
204 return new FontUnit (n
);
207 public string ToString (IFormatProvider formatProvider
)
209 if (type
== FontSize
.NotSet
)
211 else if (type
== FontSize
.AsUnit
)
212 return unit
.ToString (formatProvider
);
214 return font_size_names
[(int) type
];
217 public string ToString (CultureInfo culture
)
219 if (type
== FontSize
.NotSet
)
222 if (type
== FontSize
.AsUnit
)
223 return unit
.ToString (culture
);
225 return font_size_names
[(int) type
];
228 public override string ToString ()
230 return ToString (CultureInfo
.CurrentCulture
);