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
46 public static readonly Unit Empty = new Unit();
\r
48 private static int Min = -32768;
\r
49 private static int Max = +32767;
\r
51 private UnitType type;
\r
54 public static Unit Parse(string s)
\r
59 public static Unit Parse(string s, CultureInfo culture)
\r
61 return new Unit(s, culture);
\r
64 public static Unit Percentage(double n)
\r
66 return new Unit (n, UnitType.Percentage);
\r
69 public static Unit Pixel(int n)
\r
71 return new Unit (n, UnitType.Pixel);
\r
74 public static Unit Point(int n)
\r
76 return new Unit(n, UnitType.Point);
\r
79 public static bool operator ==(Unit left, Unit right)
\r
81 return (left.type == right.type && left.val == right.val);
\r
84 public static bool operator !=(Unit left, Unit right)
\r
86 return !(left == right);
\r
89 public static implicit operator Unit(int n)
\r
94 public Unit(double value)
\r
96 if(value < Min || value > Max)
\r
98 throw new ArgumentOutOfRangeException();
\r
101 type = UnitType.Pixel;
\r
104 public Unit(int value)
\r
106 if(value < Min || value > Max)
\r
108 throw new ArgumentOutOfRangeException();
\r
111 type = UnitType.Pixel;
\r
114 public Unit(string value): this(value, CultureInfo.CurrentCulture)
\r
118 public Unit(double value, UnitType type)
\r
120 if(value < Min || value > Max)
\r
122 throw new ArgumentOutOfRangeException();
\r
128 public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
\r
132 internal Unit(string value, CultureInfo culture, UnitType defType)
\r
135 if (value == null || (valueTrim = value.Trim ()).Length == 0) {
\r
137 type = (UnitType)0;
\r
141 if (culture == null)
\r
142 culture = CultureInfo.CurrentCulture;
\r
144 string strVal = valueTrim.ToLower ();
\r
145 int length = strVal.Length;
\r
148 for (int i = 0; i < length; i++) {
\r
150 if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
\r
155 throw new ArgumentException("No digits in 'value'");
\r
158 if (start < length) {
\r
159 type = GetTypeFromString (strVal.Substring (start).Trim ());
\r
166 string numbers = strVal.Substring (0, start);
\r
167 if (type == UnitType.Pixel)
\r
168 val = (double) Int32.Parse (numbers, culture);
\r
170 val = (double) Single.Parse (numbers, culture);
\r
171 } catch (Exception) {
\r
172 throw new FormatException ("Error parsing " + value);
\r
175 if (val < Min || val > Max)
\r
176 throw new ArgumentOutOfRangeException ();
\r
179 private static UnitType GetTypeFromString(string s)
\r
181 if(s == null || s.Length == 0)
\r
182 return UnitType.Pixel;
\r
186 return UnitType.Pixel;
\r
188 return UnitType.Point;
\r
190 return UnitType.Pica;
\r
192 return UnitType.Inch;
\r
194 return UnitType.Mm;
\r
196 return UnitType.Cm;
\r
198 return UnitType.Percentage;
\r
200 return UnitType.Em;
\r
202 return UnitType.Ex;
\r
204 return UnitType.Pixel;
\r
208 private string GetStringFromPixel(UnitType ut)
\r
212 case UnitType.Pixel:
\r
214 case UnitType.Point:
\r
216 case UnitType.Pica:
\r
218 case UnitType.Inch:
\r
224 case UnitType.Percentage:
\r
231 return String.Empty;
\r
235 public bool IsEmpty
\r
239 return (type == 0);
\r
243 public UnitType Type
\r
248 return UnitType.Pixel;
\r
253 public double Value
\r
261 public override bool Equals(object obj)
\r
263 if(obj != null && obj is Unit)
\r
265 Unit that = (Unit)obj;
\r
266 return ( this.type == that.type && this.val == that.val );
\r
271 public override int GetHashCode()
\r
273 return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
\r
276 public override string ToString()
\r
279 return String.Empty;
\r
280 return ( val.ToString() + GetStringFromPixel(type) );
\r
283 public string ToString(CultureInfo culture)
\r
286 return String.Empty;
\r
287 return ( val.ToString(culture.NumberFormat) + GetStringFromPixel(type) );
\r