2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Unit.cs
1 //
2 // System.Web.UI.WebControls.Unit.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@novell.com)
6 //   Ben Maurer (bmaurer@ximian.com).
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls {
35
36         [TypeConverter(typeof (UnitConverter))]
37 #if NET_2_0
38         [Serializable]
39 #else
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 #endif
43         public struct Unit {
44                 UnitType type;
45                 double value;
46                 public static readonly Unit Empty;
47                 
48                 public Unit (double value, UnitType type)
49                 {
50                         if (value < -32768 || value > 32767)
51                                 throw new ArgumentOutOfRangeException ("value");
52
53                         this.type = type;
54                         if (type == UnitType.Pixel)
55                                 this.value = (int) value;
56                         else
57                                 this.value = value;
58                 }
59
60                 public Unit (double value) : this (value, UnitType.Pixel)
61                 {
62                 }
63                 
64                 public Unit (int value) : this ((double) value, UnitType.Pixel)
65                 {
66                 }
67
68                 internal Unit (string value, char sep)
69                 {
70                         if (value == null || value == String.Empty){
71                                 type = (UnitType) 0;
72                                 this.value = 0.0;
73                                 return;
74                         }
75
76                         // KLUDGE: the parser below should be rewritten
77                         if (value [0] == sep)
78                                 value = "0" + value;
79                         
80                         int count = value.Length;
81                         int i = 0;
82                         int sign = 1;
83                         
84                         while (i < count && Char.IsWhiteSpace (value [i]))
85                                 i++;
86                         if (value [i] == '-'){
87                                 sign = -1;
88                                 i++;
89                                 if (!Char.IsDigit (value [i]))
90                                         throw new ArgumentOutOfRangeException ("value");
91                         } else if (!Char.IsDigit (value [i])) {
92                                 throw new FormatException ();
93                         }
94
95                         double dv = 0;
96                         for (; i < count; i++){
97                                 char c = value [i];
98                                 if (!Char.IsDigit (c))
99                                         break;
100                                 dv = dv * 10 + ((int) c) - ((int) '0');
101                         }
102                         dv *= sign;
103                         this.value = dv;
104                         dv = 0;
105                         if (i < count && value [i] == sep){
106                                 i++;
107                                 double factor = .1;
108                                 for (; i < count; i++){
109                                         char c = value [i];
110                                         if (!Char.IsDigit (c))
111                                                 break;
112                                         dv = dv + (((int) c) - ((int) '0')) * factor;
113                                         factor = factor *.1;
114                                 }
115                                 this.value += dv;
116                         }
117                         
118                         while (i < count && Char.IsWhiteSpace (value [i]))
119                                 i++;
120
121                         if (i == count){
122                                 type = UnitType.Pixel;
123                                 return;
124                         }
125
126                         if (value [i] == '%'){
127                                 type = UnitType.Percentage;
128                                 i++;
129                                 while (i < count && Char.IsWhiteSpace (value [i]))
130                                         i++;
131                                 if (i != count)
132                                         throw new ArgumentOutOfRangeException ("value");
133                                 return;
134                         }
135                         
136                         int j = i;
137                         while (j < count && Char.IsLetter (value [j]))
138                                 j++;
139                         string code = value.Substring (i, j-i);
140                         switch (code.ToLower (CultureInfo.InvariantCulture)){
141                         case "in": type = UnitType.Inch; break;
142                         case "cm": type = UnitType.Cm; break;
143                         case "mm": type = UnitType.Mm; break;
144                         case "pt": type = UnitType.Point; break;
145                         case "pc": type = UnitType.Pica; break;
146                         case "em": type = UnitType.Em; break;
147                         case "ex": type = UnitType.Ex; break;
148                         case "px":
149                                 type = UnitType.Pixel;
150                                 if (dv != 0)
151                                         throw new FormatException ("Pixel units do not allow floating point values");
152                                 break;
153                         default:
154                                 throw new ArgumentOutOfRangeException ("value");
155                         }
156
157                         while (j < count && Char.IsWhiteSpace (value [j]))
158                                 j++;
159                         if (j != count)
160                                 throw new ArgumentOutOfRangeException ("value");
161                 }
162
163                 
164                 public Unit (string value) : this (value, '.')
165                 {
166                 }
167
168                 public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
169                 {
170                 }
171
172                 internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
173                 {
174                 }
175                 
176                 public bool IsEmpty {
177                         get {
178                                 return type == 0;
179                         }
180                 }
181
182                 public UnitType Type {
183                         get {
184                                 if (type == 0)
185                                         return UnitType.Pixel;
186                                 return type;
187                         }
188                 }
189
190                 public double Value {
191                         get {
192                                 return value;
193                         }
194                 }
195                 
196                 public static Unit Parse (string s)
197                 {
198                         return new Unit (s);
199                 }
200
201                 public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
202                 {
203                         return new Unit (s, culture);
204                 }
205                 
206
207                 public static Unit Percentage (double n)
208                 {
209                         return new Unit (n, UnitType.Percentage);
210                 }
211                 
212                 public static Unit Pixel (int n)
213                 {
214                         return new Unit (n);
215                 }
216                 
217                 public static Unit Point (int n)
218                 {
219                         return new Unit (n, UnitType.Point);
220                 }
221                                 
222                 public override bool Equals (object obj)
223                 {
224                         if (obj is Unit){
225                                 Unit other = (Unit) obj;
226                                 return (other.type == type && other.value == value);
227                         }
228                         return false;
229                 }
230                 
231                 public override int GetHashCode ()
232                 {
233                         return Type.GetHashCode () ^ Value.GetHashCode ();
234                 }
235                 
236                 public static bool operator == (Unit left, Unit right)
237                 {
238                         return left.Type == right.Type && left.Value == right.Value;
239                 }
240
241                 public static bool operator != (Unit left, Unit right)
242                 {
243                         return left.Type != right.Type || left.Value != right.Value;
244                 }
245                 
246                 public static implicit operator Unit (int n)
247                 {
248                         return new Unit (n);
249                 }
250
251                 string GetExtension ()
252                 {
253                         switch (type){
254                         case UnitType.Pixel: return "px";
255                         case UnitType.Point: return "pt";
256                         case UnitType.Pica: return "pc";
257                         case UnitType.Inch: return "in";
258                         case UnitType.Mm: return "mm";
259                         case UnitType.Cm: return "cm";
260                         case UnitType.Percentage: return "%";
261                         case UnitType.Em: return "em";
262                         case UnitType.Ex: return "ex";
263                         default: return "";
264                         }
265                 }
266
267                 public string ToString (CultureInfo culture)
268                 {
269                         if (type == 0)
270                                 return "";
271                         
272                         string ex = GetExtension ();
273                         
274                         return value.ToString (culture) + ex;
275                 }
276                         
277                 public override string ToString ()
278                 {
279                         return ToString (CultureInfo.InvariantCulture);
280                 }
281
282 #if NET_2_0
283                 public string ToString (IFormatProvider provider)
284                 {
285                         if (type == 0)
286                                 return "";
287
288                         string ex = GetExtension ();
289
290                         return value.ToString (provider) + ex;
291                 }
292 #endif
293         }
294
295 }