update readme (#21797)
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / FontUnit.cs
blob7282dd10f27f16f1601866f7de7676a5d99402bc
1 //
2 // System.Web.UI.WebControls.FontUnit.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@novell.com)
6 // Ben Maurer (bmaurer@ximian.com).
7 //
8 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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))]
39 [Serializable]
40 public struct FontUnit
42 FontSize type;
43 Unit unit;
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)
61 int t = (int) type;
63 if (t < 0 || t > (int)FontSize.XXLarge)
64 throw new ArgumentOutOfRangeException ("type");
66 this.type = type;
68 if (type == FontSize.AsUnit)
69 unit = new Unit (10, UnitType.Point);
70 else
71 unit = Unit.Empty;
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;
89 unit = value;
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;
99 unit = Unit.Empty;
100 return;
103 switch (value.ToLower (Helpers.InvariantCulture)) {
104 case "smaller":
105 type = FontSize.Smaller;
106 break;
107 case "larger":
108 type = FontSize.Larger;
109 break;
110 case "xxsmall":
111 type = FontSize.XXSmall;
112 break;
113 case "xx-small":
114 type = FontSize.XXSmall;
115 break;
116 case "xsmall":
117 type = FontSize.XSmall;
118 break;
119 case "x-small":
120 type = FontSize.XSmall;
121 break;
122 case "small":
123 type = FontSize.Small;
124 break;
125 case "medium":
126 type = FontSize.Medium;
127 break;
128 case "large":
129 type = FontSize.Large;
130 break;
131 case "xlarge":
132 type = FontSize.XLarge;
133 break;
134 case "x-large":
135 type = FontSize.XLarge;
136 break;
137 case "xxlarge":
138 type = FontSize.XXLarge;
139 break;
140 case "xx-large":
141 type = FontSize.XXLarge;
142 break;
143 default:
144 type = FontSize.AsUnit;
145 unit = new Unit (value, culture);
146 return;
148 unit = Unit.Empty;
151 public bool IsEmpty {
152 get { return type == FontSize.NotSet; }
155 public FontSize Type {
156 get { return type; }
159 public Unit Unit {
160 get { return unit; }
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);
184 return false;
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)
210 return String.Empty;
211 else if (type == FontSize.AsUnit)
212 return unit.ToString (formatProvider);
213 else
214 return font_size_names [(int) type];
217 public string ToString (CultureInfo culture)
219 if (type == FontSize.NotSet)
220 return String.Empty;
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);