2 // System.Web.UI.WebControls.FontUnit.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Ben Maurer (bmaurer@ximian.com).
8 // Copyright (C) 2005 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;
35 namespace System.Web.UI.WebControls {
36 [TypeConverter (typeof (FontUnitConverter))]
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public struct FontUnit {
47 public static readonly FontUnit Empty;
48 public static readonly FontUnit Smaller = new FontUnit (FontSize.Smaller);
49 public static readonly FontUnit Larger = new FontUnit (FontSize.Larger);
50 public static readonly FontUnit XXSmall = new FontUnit (FontSize.XXSmall);
51 public static readonly FontUnit XSmall = new FontUnit (FontSize.XSmall);
52 public static readonly FontUnit Small = new FontUnit (FontSize.Small);
53 public static readonly FontUnit Medium = new FontUnit (FontSize.Medium);
54 public static readonly FontUnit Large = new FontUnit (FontSize.Large);
55 public static readonly FontUnit XLarge = new FontUnit (FontSize.XLarge);
56 public static readonly FontUnit XXLarge = new FontUnit (FontSize.XXLarge);
58 static string [] font_size_names = new string [] {null, null, "Smaller", "Larger", "XX-Small", "X-Small", "Small",
59 "Medium", "Large", "X-Large", "XX-Large" };
61 public FontUnit (FontSize type)
65 if (t < 0 || t > (int)FontSize.XXLarge)
66 throw new ArgumentOutOfRangeException ("type");
70 if (type == FontSize.AsUnit)
71 unit = new Unit (10, UnitType.Point);
76 public FontUnit (int value) : this (new Unit (value, UnitType.Point))
81 public FontUnit (double value) : this (new Unit (value, UnitType.Point))
85 public FontUnit (double value, UnitType type) : this (new Unit (value, type))
90 public FontUnit (Unit value)
92 type = FontSize.AsUnit;
96 public FontUnit (string value) : this (value, Thread.CurrentThread.CurrentCulture) {}
98 public FontUnit (string value, CultureInfo culture)
100 if (value == null || value == String.Empty){
101 type = FontSize.NotSet;
106 switch (value.ToLower (CultureInfo.InvariantCulture)){
107 case "smaller": type = FontSize.Smaller; break;
108 case "larger": type = FontSize.Larger; break;
109 case "xxsmall": type = FontSize.XXSmall; break;
110 case "xx-small": type = FontSize.XXSmall; break;
111 case "xsmall": type = FontSize.XSmall; break;
112 case "x-small": type = FontSize.XSmall; break;
113 case "small": type = FontSize.Small; break;
114 case "medium": type = FontSize.Medium; break;
115 case "large": type = FontSize.Large; break;
116 case "xlarge": type = FontSize.XLarge; break;
117 case "x-large": type = FontSize.XLarge; break;
118 case "xxlarge": type = FontSize.XXLarge; break;
119 case "xx-large": type = FontSize.XXLarge; break;
121 type = FontSize.AsUnit;
122 unit = new Unit (value, culture);
128 public bool IsEmpty {
130 return type == FontSize.NotSet;
134 public FontSize Type {
146 public static FontUnit Parse (string s)
148 return new FontUnit (s);
151 public static FontUnit Parse (string s, CultureInfo culture)
153 return new FontUnit (s, culture);
156 public static FontUnit Point (int n)
158 return new FontUnit (n);
160 public override bool Equals (object obj)
162 if (obj is FontUnit){
163 FontUnit other = (FontUnit) obj;
164 return (other.type == type && other.unit == unit);
169 public override int GetHashCode ()
171 return type.GetHashCode () ^ unit.GetHashCode ();
174 public static bool operator == (FontUnit left, FontUnit right)
176 return left.type == right.type && left.unit == right.unit;
179 public static bool operator != (FontUnit left, FontUnit right)
181 return left.type != right.type || left.unit != right.unit;
184 public static implicit operator FontUnit (int n)
186 return new FontUnit (n);
190 public string ToString (IFormatProvider fmt)
192 if (type == FontSize.NotSet)
194 else if (type == FontSize.AsUnit)
195 return unit.ToString (fmt);
197 return font_size_names [(int) type];
201 public string ToString (CultureInfo culture)
203 if (type == FontSize.NotSet)
206 if (type == FontSize.AsUnit)
207 return unit.ToString (culture);
209 return font_size_names [(int) type];
212 public override string ToString ()
214 return ToString (CultureInfo.CurrentCulture);