This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / FontUnit.cs
1
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.
21 //
22 /**\r
23  * Namespace: System.Web.UI.WebControls\r
24  * Struct:    FontUnit\r
25  *\r
26  * Author:  Gaurav Vaish\r
27  * Maintainer: gvaish@iitk.ac.in\r
28  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
29  * Implementation: yes\r
30  * Status:  100%\r
31  *\r
32  * (C) Gaurav Vaish (2001)\r
33  */\r
34 \r
35 using System;\r
36 using System.Collections;\r
37 using System.ComponentModel;\r
38 using System.Globalization;\r
39 using System.Web;\r
40 using System.Web.UI;\r
41 \r
42 namespace System.Web.UI.WebControls\r
43 {\r
44         [TypeConverter(typeof(FontUnitConverter))]\r
45         public struct FontUnit\r
46         {\r
47                 public static readonly FontUnit Empty   = new FontUnit();\r
48                 public static readonly FontUnit Large   = new FontUnit(FontSize.Large);\r
49                 public static readonly FontUnit Larger  = new FontUnit(FontSize.Larger);\r
50                 public static readonly FontUnit Medium  = new FontUnit(FontSize.Medium);\r
51                 public static readonly FontUnit Small   = new FontUnit(FontSize.Small);\r
52                 public static readonly FontUnit Smaller = new FontUnit(FontSize.Smaller);\r
53                 public static readonly FontUnit XLarge  = new FontUnit(FontSize.XLarge);\r
54                 public static readonly FontUnit XSmall  = new FontUnit(FontSize.XSmall);\r
55                 public static readonly FontUnit XXLarge = new FontUnit(FontSize.XXLarge);\r
56                 public static readonly FontUnit XXSmall = new FontUnit(FontSize.XXSmall);\r
57 \r
58                 private FontSize type;\r
59                 private Unit     val;\r
60 \r
61                 private static Hashtable sizeTable;\r
62 \r
63                 static FontUnit ()\r
64                 {\r
65                         sizeTable = new Hashtable (CaseInsensitiveHashCodeProvider.Default,\r
66                                                    CaseInsensitiveComparer.Default);\r
67                         sizeTable.Add ("smaller", 2);\r
68                         sizeTable.Add ("larger", 3);\r
69                         sizeTable.Add ("xx-small", 4);\r
70                         sizeTable.Add ("xxsmall", 4);\r
71                         sizeTable.Add ("x-small", 5);\r
72                         sizeTable.Add ("xsmall", 5);\r
73                         sizeTable.Add ("small", 6);\r
74                         sizeTable.Add ("medium", 7);\r
75                         sizeTable.Add ("large", 8);\r
76                         sizeTable.Add ("xlarge", 9);\r
77                         sizeTable.Add ("x-large", 9);\r
78                         sizeTable.Add ("xxlarge", 10);\r
79                         sizeTable.Add ("xx-large", 10);\r
80                 }\r
81 \r
82                 public FontUnit(FontSize type)\r
83                 {\r
84                         if(!Enum.IsDefined(typeof(FontSize), type))\r
85                                 throw new ArgumentException();\r
86                         this.type = type;\r
87                         if(this.type == FontSize.AsUnit)\r
88                         {\r
89                                 val = Unit.Point(10);\r
90                         } else\r
91                         {\r
92                                 val = Unit.Empty;\r
93                         }\r
94                 }\r
95 \r
96                 public FontUnit(int value)\r
97                 {\r
98                         type = FontSize.AsUnit;\r
99                         val = Unit.Point(value);\r
100                 }\r
101 \r
102                 public FontUnit(string value): this(value, CultureInfo.CurrentCulture)\r
103                 {\r
104                 }\r
105 \r
106                 public FontUnit(Unit value)\r
107                 {\r
108                         if(value.IsEmpty)\r
109                         {\r
110                                 type = FontSize.NotSet;\r
111                                 val  = Unit.Empty;\r
112                         } else\r
113                         {\r
114                                 type = FontSize.AsUnit;\r
115                                 val  = value;\r
116                         }\r
117                 }\r
118 \r
119                 public FontUnit(string value, CultureInfo culture)\r
120                 {\r
121                         type = FontSize.NotSet;\r
122                         val  = Unit.Empty;\r
123                         if(value != null && value != String.Empty)\r
124                         {\r
125                                 string low = value.ToLower(culture);\r
126                                 int size = GetTypeFromString(low);\r
127                                 if (size != -1)\r
128                                 {\r
129                                         type = (FontSize)size;\r
130                                 } else {\r
131                                         val = new Unit(value, culture, UnitType.Point);\r
132                                         type = FontSize.AsUnit;\r
133                                 }\r
134                         }\r
135                 }\r
136 \r
137                 private static int GetTypeFromString(string strVal)\r
138                 {\r
139                         if (!(sizeTable.ContainsKey (strVal)))\r
140                                         return -1;\r
141                         return (int) sizeTable [strVal];\r
142                 }\r
143 \r
144                 public static FontUnit Parse(string s)\r
145                 {\r
146                         return Parse(s, CultureInfo.CurrentCulture);\r
147                 }\r
148 \r
149                 public static FontUnit Parse(string s, CultureInfo culture)\r
150                 {\r
151                         return new FontUnit(s, culture);\r
152                 }\r
153 \r
154                 public static FontUnit Point(int n)\r
155                 {\r
156                         return new FontUnit(n);\r
157                 }\r
158 \r
159                 public static bool operator ==(FontUnit left, FontUnit right)\r
160                 {\r
161                         return (left.type == right.type && left.val == right.val);\r
162                 }\r
163 \r
164                 public static bool operator !=(FontUnit left, FontUnit right)\r
165                 {\r
166                         return !(left == right);\r
167                 }\r
168 \r
169                 public static implicit operator FontUnit(int n)\r
170                 {\r
171                         return FontUnit.Point(n);\r
172                 }\r
173 \r
174                 public override bool Equals(object obj)\r
175                 {\r
176                         if(obj!= null && obj is FontUnit)\r
177                         {\r
178                                 FontUnit that = (FontUnit)obj;\r
179                                 return (this.type == that.type && this.val == that.val);\r
180                         }\r
181                         return false;\r
182                 }\r
183 \r
184                 public override int GetHashCode()\r
185                 {\r
186                         return ( (type.GetHashCode() << 2) | val.GetHashCode() );\r
187                 }\r
188 \r
189                 public override string ToString()\r
190                 {\r
191                         return ToString(CultureInfo.CurrentCulture);\r
192                 }\r
193 \r
194                 public string ToString(CultureInfo culture)\r
195                 {\r
196                         if(IsEmpty)\r
197                         {\r
198                                 return String.Empty;\r
199                         }\r
200                         //string strRepr = String.Empty;\r
201                         switch(type)\r
202                         {\r
203                                 case FontSize.AsUnit:  return val.ToString(culture);\r
204                                 case FontSize.XXSmall: return "XX-Small";\r
205                                 case FontSize.XSmall:  return "X-Small";\r
206                                 case FontSize.XLarge:  return "X-Large";\r
207                                 case FontSize.XXLarge: return "XX-Large";\r
208                                 default:               return PropertyConverter.EnumToString(typeof(FontSize), type);\r
209                         }\r
210                 }\r
211 \r
212                 public bool IsEmpty\r
213                 {\r
214                         get\r
215                         {\r
216                                 return (type == FontSize.NotSet);\r
217                         }\r
218                 }\r
219 \r
220                 public FontSize Type\r
221                 {\r
222                         get\r
223                         {\r
224                                 return type;\r
225                         }\r
226                 }\r
227 \r
228                 public Unit Unit\r
229                 {\r
230                         get\r
231                         {\r
232                                 return val;\r
233                         }\r
234                 }\r
235         }\r
236 }\r