2 // System.Web.UI.WebControls.Unit.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Ben Maurer (bmaurer@ximian.com).
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Globalization;
31 using System.ComponentModel;
32 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls {
36 [TypeConverter(typeof (UnitConverter))]
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 public static readonly Unit Empty;
48 public Unit (double value, UnitType type)
50 if (value < -32768 || value > 32767)
51 throw new ArgumentOutOfRangeException ("value");
54 if (type == UnitType.Pixel)
55 this.value = (int) value;
60 public Unit (double value) : this (value, UnitType.Pixel)
64 public Unit (int value) : this ((double) value, UnitType.Pixel)
68 internal Unit (string value, char sep)
70 if (value == null || value == String.Empty){
76 int count = value.Length;
80 while (i < count && Char.IsWhiteSpace (value [i]))
82 if (value [i] == '-'){
85 if (!Char.IsDigit (value [i]))
86 throw new ArgumentOutOfRangeException ("value");
87 } else if (!Char.IsDigit (value [i])) {
88 throw new FormatException ();
92 for (; i < count; i++){
94 if (!Char.IsDigit (c))
96 dv = dv * 10 + ((int) c) - ((int) '0');
101 if (i < count && value [i] == sep){
104 for (; i < count; i++){
106 if (!Char.IsDigit (c))
108 dv = dv + (((int) c) - ((int) '0')) * factor;
114 while (i < count && Char.IsWhiteSpace (value [i]))
118 type = UnitType.Pixel;
122 if (value [i] == '%'){
123 type = UnitType.Percentage;
125 while (i < count && Char.IsWhiteSpace (value [i]))
128 throw new ArgumentOutOfRangeException ("value");
133 while (j < count && Char.IsLetter (value [j]))
135 string code = value.Substring (i, j-i);
136 switch (code.ToLower (CultureInfo.InvariantCulture)){
137 case "in": type = UnitType.Inch; break;
138 case "cm": type = UnitType.Cm; break;
139 case "mm": type = UnitType.Mm; break;
140 case "pt": type = UnitType.Point; break;
141 case "pc": type = UnitType.Pica; break;
142 case "em": type = UnitType.Em; break;
143 case "ex": type = UnitType.Ex; break;
145 type = UnitType.Pixel;
147 throw new FormatException ("Pixel units do not allow floating point values");
150 throw new ArgumentOutOfRangeException ("value");
153 while (j < count && Char.IsWhiteSpace (value [j]))
156 throw new ArgumentOutOfRangeException ("value");
160 public Unit (string value) : this (value, '.')
164 public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
168 internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
172 public bool IsEmpty {
178 public UnitType Type {
181 return UnitType.Pixel;
186 public double Value {
192 public static Unit Parse (string s)
197 public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
199 return new Unit (s, culture);
203 public static Unit Percentage (double n)
205 return new Unit (n, UnitType.Percentage);
208 public static Unit Pixel (int n)
213 public static Unit Point (int n)
215 return new Unit (n, UnitType.Point);
218 public override bool Equals (object obj)
221 Unit other = (Unit) obj;
222 return (other.type == type && other.value == value);
227 public override int GetHashCode ()
229 return Type.GetHashCode () ^ Value.GetHashCode ();
232 public static bool operator == (Unit left, Unit right)
234 return left.Type == right.Type && left.Value == right.Value;
237 public static bool operator != (Unit left, Unit right)
239 return left.Type != right.Type || left.Value != right.Value;
242 public static implicit operator Unit (int n)
247 string GetExtension ()
250 case UnitType.Pixel: return "px";
251 case UnitType.Point: return "pt";
252 case UnitType.Pica: return "pc";
253 case UnitType.Inch: return "in";
254 case UnitType.Mm: return "mm";
255 case UnitType.Cm: return "cm";
256 case UnitType.Percentage: return "%";
257 case UnitType.Em: return "em";
258 case UnitType.Ex: return "ex";
263 public string ToString (CultureInfo culture)
268 string ex = GetExtension ();
270 return String.Format (culture, "{0}{1}", value, ex);
273 public override string ToString ()
275 return ToString (CultureInfo.InvariantCulture);
279 public string ToString (IFormatProvider provider)
284 string ex = GetExtension ();
286 return String.Format (provider, "{0}{1}", value, ex);