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