2001-12-28 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(val.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)fs;\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): Parse(s, CultureInfo.CurrentCulture)\r
120                 {\r
121                 }\r
122                 \r
123                 public static FontUnit Parse(string s, CultureInfo culture)\r
124                 {\r
125                         return new FontUnit(s, culture);\r
126                 }\r
127                 \r
128                 public static FontUnit Point(int n)\r
129                 {\r
130                         return new FontUnit(n);\r
131                 }\r
132                 \r
133                 public static bool operator ==(FontUnit left, FontUnit right)\r
134                 {\r
135                         return (left.type == right.type && left.val == right.val);\r
136                 }\r
137                 \r
138                 public static bool operator !=(FontUnit left, FontUnit right)\r
139                 {\r
140                         return !(left == right);\r
141                 }\r
142                 \r
143                 public static implicit operator FontUnit(int n)\r
144                 {\r
145                         return FontUnit.Point(n);\r
146                 }\r
147                 \r
148                 public override bool Equals(object obj)\r
149                 {\r
150                         if(obj!= null && obj is FontUnit)\r
151                         {\r
152                                 FontUnit that = (FontUnit)obj;\r
153                                 return (this.type == that.type && this.val == that.val);\r
154                         }\r
155                         return false;\r
156                 }\r
157                 \r
158                 public override int GetHashCode()\r
159                 {\r
160                         return ( (type.GetHashCode() << 2) | val.GetHashCode() );\r
161                 }\r
162                 \r
163                 public override string ToString(): ToString(CultureInfo.CurrentCulture)\r
164                 {\r
165                 }\r
166                 \r
167                 public override string ToString(CultureInfo culture)\r
168                 {\r
169                         if(IsEmpty)\r
170                         {\r
171                                 return String.Empty;\r
172                         }\r
173                         //string strRepr = String.Empty;\r
174                         switch(type)\r
175                         {\r
176                                 case FontSize.AsUnit:  return val.ToString(culture);\r
177                                 case FontSize.XXSmall: return "XX-Small";\r
178                                 case FontSize.XSmall:  return "X-Small";\r
179                                 case FontSize.XLarge:  return "X-Large";\r
180                                 case FontSize.XXLarge: return "XX-Large";\r
181                                 default:               return PropertyConverter.EnumToString(typeof(FontSize), type);\r
182                         }\r
183                 }\r
184                 \r
185                 public bool IsEmpty\r
186                 {\r
187                         get\r
188                         {\r
189                                 return (type == FontSize.NotSet);\r
190                         }\r
191                 }\r
192                 \r
193                 public FontSize Type\r
194                 {\r
195                         get\r
196                         {\r
197                                 return type;\r
198                         }\r
199                 }\r
200                 \r
201                 public Unit Unit\r
202                 {\r
203                         get\r
204                         {\r
205                                 return val;\r
206                         }\r
207                 }\r
208         }\r
209 }\r