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