**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / FontUnit.cs
blob02a4ebeda336a55450f0f44dc3be194f74409f7a
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 /**
23 * Namespace: System.Web.UI.WebControls
24 * Struct: FontUnit
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
28 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
29 * Implementation: yes
30 * Status: 100%
32 * (C) Gaurav Vaish (2001)
35 using System;
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Globalization;
39 using System.Web;
40 using System.Web.UI;
42 namespace System.Web.UI.WebControls
44 [TypeConverter(typeof(FontUnitConverter))]
45 public struct FontUnit
47 public static readonly FontUnit Empty = new FontUnit();
48 public static readonly FontUnit Large = new FontUnit(FontSize.Large);
49 public static readonly FontUnit Larger = new FontUnit(FontSize.Larger);
50 public static readonly FontUnit Medium = new FontUnit(FontSize.Medium);
51 public static readonly FontUnit Small = new FontUnit(FontSize.Small);
52 public static readonly FontUnit Smaller = new FontUnit(FontSize.Smaller);
53 public static readonly FontUnit XLarge = new FontUnit(FontSize.XLarge);
54 public static readonly FontUnit XSmall = new FontUnit(FontSize.XSmall);
55 public static readonly FontUnit XXLarge = new FontUnit(FontSize.XXLarge);
56 public static readonly FontUnit XXSmall = new FontUnit(FontSize.XXSmall);
58 private FontSize type;
59 private Unit val;
61 private static Hashtable sizeTable;
63 static FontUnit ()
65 sizeTable = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
66 CaseInsensitiveComparer.Default);
67 sizeTable.Add ("smaller", 2);
68 sizeTable.Add ("larger", 3);
69 sizeTable.Add ("xx-small", 4);
70 sizeTable.Add ("xxsmall", 4);
71 sizeTable.Add ("x-small", 5);
72 sizeTable.Add ("xsmall", 5);
73 sizeTable.Add ("small", 6);
74 sizeTable.Add ("medium", 7);
75 sizeTable.Add ("large", 8);
76 sizeTable.Add ("xlarge", 9);
77 sizeTable.Add ("x-large", 9);
78 sizeTable.Add ("xxlarge", 10);
79 sizeTable.Add ("xx-large", 10);
82 public FontUnit(FontSize type)
84 if(!Enum.IsDefined(typeof(FontSize), type))
85 throw new ArgumentException();
86 this.type = type;
87 if(this.type == FontSize.AsUnit)
89 val = Unit.Point(10);
90 } else
92 val = Unit.Empty;
96 public FontUnit(int value)
98 type = FontSize.AsUnit;
99 val = Unit.Point(value);
102 public FontUnit(string value): this(value, CultureInfo.CurrentCulture)
106 public FontUnit(Unit value)
108 if(value.IsEmpty)
110 type = FontSize.NotSet;
111 val = Unit.Empty;
112 } else
114 type = FontSize.AsUnit;
115 val = value;
119 public FontUnit(string value, CultureInfo culture)
121 type = FontSize.NotSet;
122 val = Unit.Empty;
123 if(value != null && value != String.Empty)
125 string low = value.ToLower(culture);
126 int size = GetTypeFromString(low);
127 if (size != -1)
129 type = (FontSize)size;
130 } else {
131 val = new Unit(value, culture, UnitType.Point);
132 type = FontSize.AsUnit;
137 private static int GetTypeFromString(string strVal)
139 if (!(sizeTable.ContainsKey (strVal)))
140 return -1;
141 return (int) sizeTable [strVal];
144 public static FontUnit Parse(string s)
146 return Parse(s, CultureInfo.CurrentCulture);
149 public static FontUnit Parse(string s, CultureInfo culture)
151 return new FontUnit(s, culture);
154 public static FontUnit Point(int n)
156 return new FontUnit(n);
159 public static bool operator ==(FontUnit left, FontUnit right)
161 return (left.type == right.type && left.val == right.val);
164 public static bool operator !=(FontUnit left, FontUnit right)
166 return !(left == right);
169 public static implicit operator FontUnit(int n)
171 return FontUnit.Point(n);
174 public override bool Equals(object obj)
176 if(obj!= null && obj is FontUnit)
178 FontUnit that = (FontUnit)obj;
179 return (this.type == that.type && this.val == that.val);
181 return false;
184 public override int GetHashCode()
186 return ( (type.GetHashCode() << 2) | val.GetHashCode() );
189 public override string ToString()
191 return ToString(CultureInfo.CurrentCulture);
194 public string ToString(CultureInfo culture)
196 if(IsEmpty)
198 return String.Empty;
200 //string strRepr = String.Empty;
201 switch(type)
203 case FontSize.AsUnit: return val.ToString(culture);
204 case FontSize.XXSmall: return "XX-Small";
205 case FontSize.XSmall: return "X-Small";
206 case FontSize.XLarge: return "X-Large";
207 case FontSize.XXLarge: return "XX-Large";
208 default: return PropertyConverter.EnumToString(typeof(FontSize), type);
212 public bool IsEmpty
216 return (type == FontSize.NotSet);
220 public FontSize Type
224 return type;
228 public Unit Unit
232 return val;