3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Namespace: System.Web.UI.WebControls
\r
26 * Author: Gaurav Vaish
\r
27 * Maintainer: gvaish@iitk.ac.in
\r
28 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
\r
29 * Implementation: yes
\r
32 * (C) Gaurav Vaish (2001)
\r
36 using System.Globalization;
\r
37 using System.ComponentModel;
\r
39 using System.Web.UI;
\r
41 namespace System.Web.UI.WebControls
\r
43 [TypeConverter(typeof(UnitConverter))]
\r
47 public static readonly Unit Empty = new Unit();
\r
49 private static int Min = -32768;
\r
50 private static int Max = +32767;
\r
52 private UnitType type;
\r
55 public static Unit Parse(string s)
\r
60 public static Unit Parse(string s, CultureInfo culture)
\r
62 return new Unit(s, culture);
\r
65 public static Unit Percentage(double n)
\r
67 return new Unit (n, UnitType.Percentage);
\r
70 public static Unit Pixel(int n)
\r
72 return new Unit (n, UnitType.Pixel);
\r
75 public static Unit Point(int n)
\r
77 return new Unit(n, UnitType.Point);
\r
80 public static bool operator ==(Unit left, Unit right)
\r
82 return (left.type == right.type && left.val == right.val);
\r
85 public static bool operator !=(Unit left, Unit right)
\r
87 return !(left == right);
\r
90 public static implicit operator Unit(int n)
\r
95 public Unit(double value)
\r
97 if(value < Min || value > Max)
\r
99 throw new ArgumentOutOfRangeException();
\r
102 type = UnitType.Pixel;
\r
105 public Unit(int value)
\r
107 if(value < Min || value > Max)
\r
109 throw new ArgumentOutOfRangeException();
\r
112 type = UnitType.Pixel;
\r
115 public Unit(string value): this(value, CultureInfo.CurrentCulture)
\r
119 public Unit(double value, UnitType type)
\r
121 if(value < Min || value > Max)
\r
123 throw new ArgumentOutOfRangeException();
\r
129 public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
\r
133 internal Unit(string value, CultureInfo culture, UnitType defType)
\r
136 if (value == null || (valueTrim = value.Trim ()).Length == 0) {
\r
138 type = (UnitType)0;
\r
142 if (culture == null)
\r
143 culture = CultureInfo.CurrentCulture;
\r
145 string strVal = valueTrim.ToLower ();
\r
146 int length = strVal.Length;
\r
149 for (int i = 0; i < length; i++) {
\r
151 if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
\r
156 throw new ArgumentException("No digits in 'value'");
\r
159 if (start < length) {
\r
160 type = GetTypeFromString (strVal.Substring (start).Trim ());
\r
167 string numbers = strVal.Substring (0, start);
\r
168 if (type == UnitType.Pixel)
\r
169 val = (double) Int32.Parse (numbers, culture);
\r
171 val = (double) Single.Parse (numbers, culture);
\r
172 } catch (Exception) {
\r
173 throw new FormatException ("Error parsing " + value);
\r
176 if (val < Min || val > Max)
\r
177 throw new ArgumentOutOfRangeException ();
\r
180 private static UnitType GetTypeFromString(string s)
\r
182 if(s == null || s.Length == 0)
\r
183 return UnitType.Pixel;
\r
187 return UnitType.Pixel;
\r
189 return UnitType.Point;
\r
191 return UnitType.Pica;
\r
193 return UnitType.Inch;
\r
195 return UnitType.Mm;
\r
197 return UnitType.Cm;
\r
199 return UnitType.Percentage;
\r
201 return UnitType.Em;
\r
203 return UnitType.Ex;
\r
205 return UnitType.Pixel;
\r
209 private string GetStringFromPixel(UnitType ut)
\r
213 case UnitType.Pixel:
\r
215 case UnitType.Point:
\r
217 case UnitType.Pica:
\r
219 case UnitType.Inch:
\r
225 case UnitType.Percentage:
\r
232 return String.Empty;
\r
236 public bool IsEmpty
\r
240 return (type == 0);
\r
244 public UnitType Type
\r
249 return UnitType.Pixel;
\r
254 public double Value
\r
262 public override bool Equals(object obj)
\r
264 if(obj != null && obj is Unit)
\r
266 Unit that = (Unit)obj;
\r
267 return ( this.type == that.type && this.val == that.val );
\r
272 public override int GetHashCode()
\r
274 return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
\r
277 public override string ToString()
\r
280 return String.Empty;
\r
281 return ( val.ToString() + GetStringFromPixel(type) );
\r
284 public string ToString(CultureInfo culture)
\r
287 return String.Empty;
\r
289 return (val.ToString(culture) + GetStringFromPixel(type));