2 // System.Web.UI.WebControls.Unit.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Ben Maurer (bmaurer@ximian.com).
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Globalization;
31 using System.ComponentModel;
32 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls {
36 [TypeConverter(typeof (UnitConverter))]
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 public static readonly Unit Empty;
48 public Unit (double value, UnitType type)
50 if (value < -32768 || value > 32767)
51 throw new ArgumentOutOfRangeException ("value");
54 if (type == UnitType.Pixel)
55 this.value = (int) value;
60 public Unit (double value) : this (value, UnitType.Pixel)
64 public Unit (int value) : this ((double) value, UnitType.Pixel)
68 internal Unit (string value, char sep)
70 if (value == null || value == String.Empty){
76 // KLUDGE: the parser below should be rewritten
80 int count = value.Length;
84 while (i < count && Char.IsWhiteSpace (value [i]))
86 if (value [i] == '-'){
89 if (!Char.IsDigit (value [i]))
90 throw new ArgumentOutOfRangeException ("value");
91 } else if (!Char.IsDigit (value [i])) {
92 throw new FormatException ();
96 for (; i < count; i++){
98 if (!Char.IsDigit (c))
100 dv = dv * 10 + ((int) c) - ((int) '0');
105 if (i < count && value [i] == sep){
108 for (; i < count; i++){
110 if (!Char.IsDigit (c))
112 dv = dv + (((int) c) - ((int) '0')) * factor;
118 while (i < count && Char.IsWhiteSpace (value [i]))
122 type = UnitType.Pixel;
126 if (value [i] == '%'){
127 type = UnitType.Percentage;
129 while (i < count && Char.IsWhiteSpace (value [i]))
132 throw new ArgumentOutOfRangeException ("value");
137 while (j < count && Char.IsLetter (value [j]))
139 string code = value.Substring (i, j-i);
140 switch (code.ToLower (CultureInfo.InvariantCulture)){
141 case "in": type = UnitType.Inch; break;
142 case "cm": type = UnitType.Cm; break;
143 case "mm": type = UnitType.Mm; break;
144 case "pt": type = UnitType.Point; break;
145 case "pc": type = UnitType.Pica; break;
146 case "em": type = UnitType.Em; break;
147 case "ex": type = UnitType.Ex; break;
149 type = UnitType.Pixel;
151 throw new FormatException ("Pixel units do not allow floating point values");
154 throw new ArgumentOutOfRangeException ("value");
157 while (j < count && Char.IsWhiteSpace (value [j]))
160 throw new ArgumentOutOfRangeException ("value");
164 public Unit (string value) : this (value, '.')
168 public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
172 internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
176 public bool IsEmpty {
182 public UnitType Type {
185 return UnitType.Pixel;
190 public double Value {
196 public static Unit Parse (string s)
201 public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
203 return new Unit (s, culture);
207 public static Unit Percentage (double n)
209 return new Unit (n, UnitType.Percentage);
212 public static Unit Pixel (int n)
217 public static Unit Point (int n)
219 return new Unit (n, UnitType.Point);
222 public override bool Equals (object obj)
225 Unit other = (Unit) obj;
226 return (other.type == type && other.value == value);
231 public override int GetHashCode ()
233 return Type.GetHashCode () ^ Value.GetHashCode ();
236 public static bool operator == (Unit left, Unit right)
238 return left.Type == right.Type && left.Value == right.Value;
241 public static bool operator != (Unit left, Unit right)
243 return left.Type != right.Type || left.Value != right.Value;
246 public static implicit operator Unit (int n)
251 string GetExtension ()
254 case UnitType.Pixel: return "px";
255 case UnitType.Point: return "pt";
256 case UnitType.Pica: return "pc";
257 case UnitType.Inch: return "in";
258 case UnitType.Mm: return "mm";
259 case UnitType.Cm: return "cm";
260 case UnitType.Percentage: return "%";
261 case UnitType.Em: return "em";
262 case UnitType.Ex: return "ex";
267 public string ToString (CultureInfo culture)
272 string ex = GetExtension ();
274 return value.ToString (culture) + ex;
277 public override string ToString ()
279 return ToString (CultureInfo.InvariantCulture);
283 public string ToString (IFormatProvider provider)
288 string ex = GetExtension ();
290 return value.ToString (provider) + ex;