2 // System.Web.UI.WebControls.Unit.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Ben Maurer (bmaurer@ximian.com).
8 // (C) 2005 Novell, Inc.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
34 using System.ComponentModel;
36 namespace System.Web.UI.WebControls {
38 [TypeConverter(typeof (UnitConverter))]
45 public static readonly Unit Empty;
47 public Unit (double value, UnitType type)
49 if (value < -32768 || value > 32767)
50 throw new ArgumentOutOfRangeException ("value");
53 if (type == UnitType.Pixel)
54 this.value = (int) value;
59 public Unit (double value) : this (value, UnitType.Pixel)
63 public Unit (int value) : this ((double) value, UnitType.Pixel)
67 internal Unit (string value, char sep)
69 if (value == null || value == String.Empty){
75 int count = value.Length;
79 while (i < count && Char.IsWhiteSpace (value [i]))
81 if (value [i] == '-'){
84 if (!Char.IsDigit (value [i]))
85 throw new ArgumentOutOfRangeException ("value");
89 for (; i < count; i++){
91 if (!Char.IsDigit (c))
93 dv = dv * 10 + ((int) c) - ((int) '0');
98 if (i < count && value [i] == sep){
101 for (; i < count; i++){
103 if (!Char.IsDigit (c))
105 dv = dv + (((int) c) - ((int) '0')) * factor;
111 while (i < count && Char.IsWhiteSpace (value [i]))
115 type = UnitType.Pixel;
119 if (value [i] == '%'){
120 type = UnitType.Percentage;
122 while (i < count && Char.IsWhiteSpace (value [i]))
125 throw new ArgumentOutOfRangeException ("value");
130 while (j < count && Char.IsLetter (value [j]))
132 string code = value.Substring (i, j-i);
133 switch (code.ToLower (CultureInfo.InvariantCulture)){
134 case "in": type = UnitType.Inch; break;
135 case "cm": type = UnitType.Cm; break;
136 case "mm": type = UnitType.Mm; break;
137 case "pt": type = UnitType.Point; break;
138 case "pc": type = UnitType.Pica; break;
139 case "em": type = UnitType.Em; break;
140 case "ex": type = UnitType.Ex; break;
142 type = UnitType.Pixel;
144 throw new FormatException ("Pixel units do not allow floating point values");
147 throw new ArgumentOutOfRangeException ("value");
150 while (j < count && Char.IsWhiteSpace (value [j]))
153 throw new ArgumentOutOfRangeException ("value");
157 public Unit (string value) : this (value, '.')
161 public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
165 internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
169 public bool IsEmpty {
175 public UnitType Type {
178 return UnitType.Pixel;
183 public double Value {
189 public static Unit Parse (string s)
194 public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
196 return new Unit (s, culture);
200 public static Unit Percentage (double n)
202 return new Unit (n, UnitType.Percentage);
205 public static Unit Pixel (int n)
210 public static Unit Point (int n)
212 return new Unit (n, UnitType.Point);
215 public override bool Equals (object obj)
218 Unit other = (Unit) obj;
219 return (other.type == type && other.value == value);
224 public override int GetHashCode ()
226 return Type.GetHashCode () ^ Value.GetHashCode ();
229 public static bool operator == (Unit left, Unit right)
231 return left.Type == right.Type && left.Value == right.Value;
234 public static bool operator != (Unit left, Unit right)
236 return left.Type != right.Type || left.Value != right.Value;
239 public static implicit operator Unit (int n)
244 string GetExtension ()
247 case UnitType.Pixel: return "px";
248 case UnitType.Point: return "pt";
249 case UnitType.Pica: return "pc";
250 case UnitType.Inch: return "in";
251 case UnitType.Mm: return "mm";
252 case UnitType.Cm: return "cm";
253 case UnitType.Percentage: return "%";
254 case UnitType.Em: return "em";
255 case UnitType.Ex: return "ex";
260 public string ToString (CultureInfo culture)
265 string ex = GetExtension ();
267 return String.Format (culture, "{0}{1}", value, ex);
270 public override string ToString ()
272 return ToString (CultureInfo.InvariantCulture);
276 public string ToString (IFormatProvider provider)
281 string ex = GetExtension ();
283 return String.Format (provider, "{0}{1}", value, ex);