2 * Namespace: System.Web.UI.WebControls
\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
11 * (C) Gaurav Vaish (2001)
\r
15 using System.Globalization;
\r
16 using System.ComponentModel;
\r
18 using System.Web.UI;
\r
20 namespace System.Web.UI.WebControls
\r
22 [TypeConverter(typeof(UnitConverter))]
\r
25 public static readonly Unit Empty = new Unit();
\r
27 private static int Min = -32768;
\r
28 private static int Max = +32767;
\r
30 private UnitType type;
\r
33 public static Unit Parse(string s)
\r
38 public static Unit Parse(string s, CultureInfo culture)
\r
40 return new Unit(s, culture);
\r
43 public static Unit Percentage(double n)
\r
45 return new Unit (n, UnitType.Percentage);
\r
48 public static Unit Pixel(int n)
\r
50 return new Unit (n, UnitType.Pixel);
\r
53 public static Unit Point(int n)
\r
55 return new Unit(n, UnitType.Point);
\r
58 public static bool operator ==(Unit left, Unit right)
\r
60 return (left.type == right.type && left.val == right.val);
\r
63 public static bool operator !=(Unit left, Unit right)
\r
65 return !(left == right);
\r
68 public static implicit operator Unit(int n)
\r
73 public Unit(double value)
\r
75 if(value < Min || value > Max)
\r
77 throw new ArgumentOutOfRangeException();
\r
80 type = UnitType.Pixel;
\r
83 public Unit(int value)
\r
85 if(value < Min || value > Max)
\r
87 throw new ArgumentOutOfRangeException();
\r
90 type = UnitType.Pixel;
\r
93 public Unit(string value): this(value, CultureInfo.CurrentCulture)
\r
97 public Unit(double value, UnitType type)
\r
99 if(value < Min || value > Max)
\r
101 throw new ArgumentOutOfRangeException();
\r
107 public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
\r
111 internal Unit(string value, CultureInfo culture, UnitType defType)
\r
114 if (value == null || (valueTrim = value.Trim ()).Length == 0) {
\r
116 type = UnitType.Pixel;
\r
120 if (culture == null)
\r
121 culture = CultureInfo.CurrentCulture;
\r
123 string strVal = valueTrim.ToLower ();
\r
124 int length = strVal.Length;
\r
127 for (int i = 0; i < length; i++) {
\r
129 if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
\r
134 throw new ArgumentException("No digits in 'value'");
\r
137 if (start < length) {
\r
138 type = GetTypeFromString (strVal.Substring (start).Trim ());
\r
145 string numbers = strVal.Substring (0, start);
\r
146 if (type == UnitType.Pixel)
\r
147 val = (double) Int32.Parse (numbers, culture);
\r
149 val = (double) Single.Parse (numbers, culture);
\r
150 } catch (Exception) {
\r
151 throw new FormatException ("Error parsing " + value);
\r
154 if (val < Min || val > Max)
\r
155 throw new ArgumentOutOfRangeException ();
\r
158 private static UnitType GetTypeFromString(string s)
\r
160 if(s == null || s.Length == 0)
\r
161 return UnitType.Pixel;
\r
162 s = s.ToLower().Trim();
\r
163 string[] uTypes = {
\r
175 foreach(string cType in uTypes)
\r
178 return (UnitType)Enum.ToObject(typeof(UnitType), (i + 1));
\r
181 return UnitType.Pixel;
\r
184 private string GetStringFromPixel(UnitType ut)
\r
186 string[] uTypes = {
\r
197 if( !Enum.IsDefined(typeof(UnitType), ut) )
\r
199 return uTypes[(int)ut - 1];
\r
202 public bool IsEmpty
\r
206 return (type == 0);
\r
210 public UnitType Type
\r
215 return UnitType.Pixel;
\r
220 public double Value
\r
228 public override bool Equals(object obj)
\r
230 if(obj != null && obj is Unit)
\r
232 Unit that = (Unit)obj;
\r
233 return ( this.type == that.type && this.val == that.val );
\r
238 public override int GetHashCode()
\r
240 return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
\r
243 public override string ToString()
\r
246 return String.Empty;
\r
247 return ( val.ToString() + GetStringFromPixel(type) );
\r
250 public string ToString(CultureInfo culture)
\r
253 return String.Empty;
\r
254 return ( val.ToString(culture.NumberFormat) + GetStringFromPixel(type) );
\r