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;
33 using System.Web.Util;
35 namespace System.Web.UI.WebControls {
37 [TypeConverter(typeof (UnitConverter))]
52 public static readonly Unit Empty;
54 public Unit (double value, UnitType type)
56 if (value < -32768 || value > 32767)
57 throw new ArgumentOutOfRangeException ("value");
60 if (type == UnitType.Pixel)
61 this.value = (int) value;
67 public Unit (double value) : this (value, UnitType.Pixel)
71 public Unit (int value) : this ((double) value, UnitType.Pixel)
75 internal Unit (string input, char sep)
77 if (input == null || input == String.Empty){
85 double dv = 0, factor = .1;
87 int count = input.Length;
88 int sign = 1, unitStart = -1, unitLen = 0, wsCount = 0;
90 ParsingStage ps = ParsingStage.Trim;
91 bool done = false, haveSep = false, haveDigits = false, isWhiteSpace;
93 while (!done && i < count) {
97 case ParsingStage.Trim:
98 if (Char.IsWhiteSpace (c)) {
102 ps = ParsingStage.SignOrSep;
105 case ParsingStage.SignOrSep:
110 ps = ParsingStage.DigitOrSep;
117 ps = ParsingStage.DigitOrUnit;
122 if (Char.IsDigit (c)) {
123 ps = ParsingStage.DigitOrSep;
127 throw new FormatException ();
129 case ParsingStage.DigitOrSep:
130 if (Char.IsDigit (c)) {
131 dv = dv * 10 + ((int) c) - ((int)'0');
139 throw new ArgumentOutOfRangeException ("input");
145 ps = ParsingStage.DigitOrUnit;
149 isWhiteSpace = Char.IsWhiteSpace (c);
150 if (isWhiteSpace || c == '%' || Char.IsLetter (c)) {
153 throw new ArgumentOutOfRangeException ("input");
165 ps = ParsingStage.DigitOrUnit;
167 ps = ParsingStage.Unit;
172 throw new FormatException ();
174 case ParsingStage.DigitOrUnit:
182 isWhiteSpace = Char.IsWhiteSpace (c);
183 if (isWhiteSpace || Char.IsLetter (c)) {
190 ps = ParsingStage.Unit;
195 if (Char.IsDigit (c)) {
197 throw new ArgumentOutOfRangeException ();
199 dv = dv + (((int) c) - ((int) '0')) * factor;
205 throw new FormatException ();
207 case ParsingStage.Unit:
208 if (c == '%' || Char.IsLetter (c)) {
214 if (unitLen == 0 && Char.IsWhiteSpace (c)) {
226 if (unitStart >= 0) {
227 int unitTail = unitStart + unitLen;
228 if (unitTail < count) {
229 for (int j = unitTail; j < count; j++) {
230 if (!Char.IsWhiteSpace (input [j]))
231 throw new ArgumentOutOfRangeException ("input");
235 if (unitLen == 1 && input [unitStart] == '%')
236 type = UnitType.Percentage;
238 switch (input.Substring (unitStart, unitLen).ToLower (Helpers.InvariantCulture)) {
239 case "in": type = UnitType.Inch; break;
240 case "cm": type = UnitType.Cm; break;
241 case "mm": type = UnitType.Mm; break;
242 case "pt": type = UnitType.Point; break;
243 case "pc": type = UnitType.Pica; break;
244 case "em": type = UnitType.Em; break;
245 case "ex": type = UnitType.Ex; break;
247 type = UnitType.Pixel;
250 throw new ArgumentOutOfRangeException ("value");
254 type = UnitType.Pixel;
256 if (haveSep && type == UnitType.Pixel)
257 throw new FormatException ("Pixel units do not allow floating point values");
261 public Unit (string value) : this (value, '.')
265 public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
269 internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
273 public bool IsEmpty {
279 public UnitType Type {
282 return UnitType.Pixel;
287 public double Value {
293 public static Unit Parse (string s)
298 public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
300 return new Unit (s, culture);
304 public static Unit Percentage (double n)
306 return new Unit (n, UnitType.Percentage);
309 public static Unit Pixel (int n)
314 public static Unit Point (int n)
316 return new Unit (n, UnitType.Point);
319 public override bool Equals (object obj)
322 Unit other = (Unit) obj;
323 return (other.type == type && other.value == value && valueSet == other.valueSet);
328 public override int GetHashCode ()
330 return Type.GetHashCode () ^ Value.GetHashCode ();
333 public static bool operator == (Unit left, Unit right)
335 return left.Type == right.Type && left.Value == right.Value && left.valueSet == right.valueSet;
338 public static bool operator != (Unit left, Unit right)
340 return left.Type != right.Type || left.Value != right.Value || left.valueSet != right.valueSet;
343 public static implicit operator Unit (int n)
348 internal static string GetExtension (UnitType type)
351 case UnitType.Pixel: return "px";
352 case UnitType.Point: return "pt";
353 case UnitType.Pica: return "pc";
354 case UnitType.Inch: return "in";
355 case UnitType.Mm: return "mm";
356 case UnitType.Cm: return "cm";
357 case UnitType.Percentage: return "%";
358 case UnitType.Em: return "em";
359 case UnitType.Ex: return "ex";
360 default: return String.Empty;
364 public string ToString (CultureInfo culture)
369 string ex = GetExtension (type);
371 return value.ToString (culture) + ex;
374 public override string ToString ()
376 return ToString (Helpers.InvariantCulture);
379 public string ToString (IFormatProvider provider)
384 string ex = GetExtension (type);
386 return value.ToString (provider) + ex;