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");
86 } else if (!Char.IsDigit (value [i])) {
87 throw new FormatException ();
91 for (; i < count; i++){
93 if (!Char.IsDigit (c))
95 dv = dv * 10 + ((int) c) - ((int) '0');
100 if (i < count && value [i] == sep){
103 for (; i < count; i++){
105 if (!Char.IsDigit (c))
107 dv = dv + (((int) c) - ((int) '0')) * factor;
113 while (i < count && Char.IsWhiteSpace (value [i]))
117 type = UnitType.Pixel;
121 if (value [i] == '%'){
122 type = UnitType.Percentage;
124 while (i < count && Char.IsWhiteSpace (value [i]))
127 throw new ArgumentOutOfRangeException ("value");
132 while (j < count && Char.IsLetter (value [j]))
134 string code = value.Substring (i, j-i);
135 switch (code.ToLower (CultureInfo.InvariantCulture)){
136 case "in": type = UnitType.Inch; break;
137 case "cm": type = UnitType.Cm; break;
138 case "mm": type = UnitType.Mm; break;
139 case "pt": type = UnitType.Point; break;
140 case "pc": type = UnitType.Pica; break;
141 case "em": type = UnitType.Em; break;
142 case "ex": type = UnitType.Ex; break;
144 type = UnitType.Pixel;
146 throw new FormatException ("Pixel units do not allow floating point values");
149 throw new ArgumentOutOfRangeException ("value");
152 while (j < count && Char.IsWhiteSpace (value [j]))
155 throw new ArgumentOutOfRangeException ("value");
159 public Unit (string value) : this (value, '.')
163 public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
167 internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
171 public bool IsEmpty {
177 public UnitType Type {
180 return UnitType.Pixel;
185 public double Value {
191 public static Unit Parse (string s)
196 public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
198 return new Unit (s, culture);
202 public static Unit Percentage (double n)
204 return new Unit (n, UnitType.Percentage);
207 public static Unit Pixel (int n)
212 public static Unit Point (int n)
214 return new Unit (n, UnitType.Point);
217 public override bool Equals (object obj)
220 Unit other = (Unit) obj;
221 return (other.type == type && other.value == value);
226 public override int GetHashCode ()
228 return Type.GetHashCode () ^ Value.GetHashCode ();
231 public static bool operator == (Unit left, Unit right)
233 return left.Type == right.Type && left.Value == right.Value;
236 public static bool operator != (Unit left, Unit right)
238 return left.Type != right.Type || left.Value != right.Value;
241 public static implicit operator Unit (int n)
246 string GetExtension ()
249 case UnitType.Pixel: return "px";
250 case UnitType.Point: return "pt";
251 case UnitType.Pica: return "pc";
252 case UnitType.Inch: return "in";
253 case UnitType.Mm: return "mm";
254 case UnitType.Cm: return "cm";
255 case UnitType.Percentage: return "%";
256 case UnitType.Em: return "em";
257 case UnitType.Ex: return "ex";
262 public string ToString (CultureInfo culture)
267 string ex = GetExtension ();
269 return String.Format (culture, "{0}{1}", value, ex);
272 public override string ToString ()
274 return ToString (CultureInfo.InvariantCulture);
278 public string ToString (IFormatProvider provider)
283 string ex = GetExtension ();
285 return String.Format (provider, "{0}{1}", value, ex);