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:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
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.
23 * Namespace: System.Web.UI.WebControls
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
28 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
32 * (C) Gaurav Vaish (2001)
36 using System
.Collections
;
37 using System
.ComponentModel
;
38 using System
.Globalization
;
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
;
61 private static Hashtable sizeTable
;
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();
87 if(this.type
== FontSize
.AsUnit
)
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)
110 type
= FontSize
.NotSet
;
114 type
= FontSize
.AsUnit
;
119 public FontUnit(string value, CultureInfo culture
)
121 type
= FontSize
.NotSet
;
123 if(value != null && value != String
.Empty
)
125 string low
= value.ToLower(culture
);
126 int size
= GetTypeFromString(low
);
129 type
= (FontSize
)size
;
131 val
= new Unit(value, culture
, UnitType
.Point
);
132 type
= FontSize
.AsUnit
;
137 private static int GetTypeFromString(string strVal
)
139 if (!(sizeTable
.ContainsKey (strVal
)))
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
);
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
)
200 //string strRepr = String.Empty;
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
);
216 return (type
== FontSize
.NotSet
);