New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Unit.cs
index a6c5a3c732da8bd3798a4094e5b4ba7e8e387ce5..830f6477b099fecb4e596f56ec1ad62184d623be 100644 (file)
@@ -1,4 +1,11 @@
-
+//
+// System.Web.UI.WebControls.Unit.cs
+//
+// Authors:
+//   Miguel de Icaza (miguel@novell.com)
+//   Ben Maurer (bmaurer@ximian.com).
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-/**\r
- * Namespace: System.Web.UI.WebControls\r
- * Struct:    Unit\r
- *\r
- * Author:  Gaurav Vaish\r
- * Maintainer: gvaish@iitk.ac.in\r
- * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
- * Implementation: yes\r
- * Status:  100%\r
- *\r
- * (C) Gaurav Vaish (2001)\r
- */\r
-\r
-using System;\r
-using System.Globalization;\r
-using System.ComponentModel;\r
-using System.Web;\r
-using System.Web.UI;\r
-\r
-namespace System.Web.UI.WebControls\r
-{\r
-       [TypeConverter(typeof(UnitConverter))]\r
-       public struct Unit\r
-       {\r
-               public static readonly Unit Empty = new Unit();\r
-\r
-               private static int Min = -32768;\r
-               private static int Max = +32767;\r
-\r
-               private UnitType type;\r
-               private double   val;\r
-\r
-               public static Unit Parse(string s)\r
-               {\r
-                       return new Unit(s);\r
-               }\r
-\r
-               public static Unit Parse(string s, CultureInfo culture)\r
-               {\r
-                       return new Unit(s, culture);\r
-               }\r
-\r
-               public static Unit Percentage(double n)\r
-               {\r
-                       return new Unit (n, UnitType.Percentage);\r
-               }\r
-\r
-               public static Unit Pixel(int n)\r
-               {\r
-                       return new Unit (n, UnitType.Pixel);\r
-               }\r
-\r
-               public static Unit Point(int n)\r
-               {\r
-                       return new Unit(n, UnitType.Point);\r
-               }\r
-\r
-               public static bool operator ==(Unit left, Unit right)\r
-               {\r
-                       return (left.type == right.type && left.val == right.val);\r
-               }\r
-\r
-               public static bool operator !=(Unit left, Unit right)\r
-               {\r
-                       return !(left == right);\r
-               }\r
-\r
-               public static implicit operator Unit(int n)\r
-               {\r
-                       return new Unit(n);\r
-               }\r
-\r
-               public Unit(double value)\r
-               {\r
-                       if(value < Min || value > Max)\r
-                       {\r
-                               throw new ArgumentOutOfRangeException();\r
-                       }\r
-                       val = value;\r
-                       type = UnitType.Pixel;\r
-               }\r
-\r
-               public Unit(int value)\r
-               {\r
-                       if(value < Min || value > Max)\r
-                       {\r
-                               throw new ArgumentOutOfRangeException();\r
-                       }\r
-                       val = value;\r
-                       type = UnitType.Pixel;\r
-               }\r
-\r
-               public Unit(string value): this(value, CultureInfo.CurrentCulture)\r
-               {\r
-               }\r
-\r
-               public Unit(double value, UnitType type)\r
-               {\r
-                       if(value < Min || value > Max)\r
-                       {\r
-                               throw new ArgumentOutOfRangeException();\r
-                       }\r
-                       val = value;\r
-                       this.type = type;\r
-               }\r
-\r
-               public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)\r
-               {\r
-               }\r
-\r
-               internal Unit(string value, CultureInfo culture, UnitType defType)\r
-               {\r
-                       string valueTrim;\r
-                       if (value == null || (valueTrim = value.Trim ()).Length == 0) {\r
-                               val = 0;\r
-                               type = (UnitType)0;\r
-                               return;\r
-                       }\r
-\r
-                       if (culture == null)\r
-                               culture = CultureInfo.CurrentCulture;\r
-\r
-                       string strVal = valueTrim.ToLower ();\r
-                       int length = strVal.Length;\r
-                       char c;\r
-                       int start = -1;\r
-                       for (int i = 0; i < length; i++) {\r
-                               c = strVal [i];\r
-                               if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )\r
-                                       start = i;\r
-                       }\r
-                       \r
-                       if (start == -1)\r
-                               throw new ArgumentException("No digits in 'value'");\r
-                       \r
-                       start++;\r
-                       if (start < length) {\r
-                               type = GetTypeFromString (strVal.Substring (start).Trim ());\r
-                               val  = 0;\r
-                       } else {\r
-                               type = defType;\r
-                       }\r
-\r
-                       try {\r
-                               string numbers = strVal.Substring (0, start);\r
-                               if (type == UnitType.Pixel)\r
-                                       val = (double) Int32.Parse (numbers, culture);\r
-                               else\r
-                                       val = (double) Single.Parse (numbers, culture);\r
-                       } catch (Exception) {\r
-                               throw new FormatException ("Error parsing " + value);\r
-                       }\r
-\r
-                       if (val < Min || val > Max)\r
-                               throw new ArgumentOutOfRangeException ();\r
-               }\r
-\r
-               private static UnitType GetTypeFromString(string s)\r
-               {\r
-                       if(s == null || s.Length == 0)\r
-                               return UnitType.Pixel;\r
-                       switch(s)\r
-                       {\r
-                               case "px":\r
-                                       return UnitType.Pixel;\r
-                               case "pt":\r
-                                       return UnitType.Point;\r
-                               case "pc":\r
-                                       return UnitType.Pica;\r
-                               case "in":\r
-                                       return UnitType.Inch;\r
-                               case "mm":\r
-                                       return UnitType.Mm;\r
-                               case "cm":\r
-                                       return UnitType.Cm;\r
-                               case "%":\r
-                                       return UnitType.Percentage;\r
-                               case "em":\r
-                                       return UnitType.Em;\r
-                               case "ex":\r
-                                       return UnitType.Ex;\r
-                               default:\r
-                                       return UnitType.Pixel;\r
-                       }\r
-               }\r
-\r
-               private string GetStringFromPixel(UnitType ut)\r
-               {\r
-                       switch(ut)\r
-                       {\r
-                               case UnitType.Pixel:\r
-                                       return "px";\r
-                               case UnitType.Point:\r
-                                       return "pt";\r
-                               case UnitType.Pica:\r
-                                       return "pc";\r
-                               case UnitType.Inch:\r
-                                       return "in";\r
-                               case UnitType.Mm:\r
-                                       return "mm";\r
-                               case UnitType.Cm:\r
-                                       return "cm";\r
-                               case UnitType.Percentage:\r
-                                       return "%";\r
-                               case UnitType.Em:\r
-                                       return "em";\r
-                               case UnitType.Ex:\r
-                                       return "ex";\r
-                               default:\r
-                                       return String.Empty;\r
-                       }\r
-               }\r
-\r
-               public bool IsEmpty\r
-               {\r
-                       get\r
-                       {\r
-                               return (type == 0);\r
-                       }\r
-               }\r
-\r
-               public UnitType Type\r
-               {\r
-                       get\r
-                       {\r
-                               if(IsEmpty)\r
-                                       return UnitType.Pixel;\r
-                               return type;\r
-                       }\r
-               }\r
-\r
-               public double Value\r
-               {\r
-                       get\r
-                       {\r
-                               return val;\r
-                       }\r
-               }\r
-\r
-               public override bool Equals(object obj)\r
-               {\r
-                       if(obj != null && obj is Unit)\r
-                       {\r
-                               Unit that = (Unit)obj;\r
-                               return ( this.type == that.type && this.val == that.val );\r
-                       }\r
-                       return false;\r
-               }\r
-\r
-               public override int GetHashCode()\r
-               {\r
-                       return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );\r
-               }\r
-\r
-               public override string ToString()\r
-               {\r
-                       if(IsEmpty)\r
-                               return String.Empty;\r
-                       return ( val.ToString() + GetStringFromPixel(type) );\r
-               }\r
-\r
-               public string ToString(CultureInfo culture)\r
-               {\r
-                       if(IsEmpty)\r
-                               return String.Empty;\r
-                       return ( val.ToString(culture.NumberFormat) + GetStringFromPixel(type) );\r
-               }\r
-       }\r
-}\r
+
+using System.Globalization;
+using System.ComponentModel;
+using System.Security.Permissions;
+
+namespace System.Web.UI.WebControls {
+
+       [TypeConverter(typeof (UnitConverter))]
+#if NET_2_0
+       [Serializable]
+#else
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+#endif
+       public struct Unit {
+               UnitType type;
+               double value;
+               public static readonly Unit Empty;
+               
+               public Unit (double value, UnitType type)
+               {
+                       if (value < -32768 || value > 32767)
+                               throw new ArgumentOutOfRangeException ("value");
+
+                       this.type = type;
+                       if (type == UnitType.Pixel)
+                               this.value = (int) value;
+                       else
+                               this.value = value;
+               }
+
+               public Unit (double value) : this (value, UnitType.Pixel)
+               {
+               }
+               
+               public Unit (int value) : this ((double) value, UnitType.Pixel)
+               {
+               }
+
+               internal Unit (string value, char sep)
+               {
+                       if (value == null || value == String.Empty){
+                               type = (UnitType) 0;
+                               this.value = 0.0;
+                               return;
+                       }
+
+                       int count = value.Length;
+                       int i = 0;
+                       int sign = 1;
+                       
+                       while (i < count && Char.IsWhiteSpace (value [i]))
+                               i++;
+                       if (value [i] == '-'){
+                               sign = -1;
+                               i++;
+                               if (!Char.IsDigit (value [i]))
+                                       throw new ArgumentOutOfRangeException ("value");
+                       } else if (!Char.IsDigit (value [i])) {
+                               throw new FormatException ();
+                       }
+
+                       double dv = 0;
+                       for (; i < count; i++){
+                               char c = value [i];
+                               if (!Char.IsDigit (c))
+                                       break;
+                               dv = dv * 10 + ((int) c) - ((int) '0');
+                       }
+                       dv *= sign;
+                       this.value = dv;
+                       dv = 0;
+                       if (i < count && value [i] == sep){
+                               i++;
+                               double factor = .1;
+                               for (; i < count; i++){
+                                       char c = value [i];
+                                       if (!Char.IsDigit (c))
+                                               break;
+                                       dv = dv + (((int) c) - ((int) '0')) * factor;
+                                       factor = factor *.1;
+                               }
+                               this.value += dv;
+                       }
+                       
+                       while (i < count && Char.IsWhiteSpace (value [i]))
+                               i++;
+
+                       if (i == count){
+                               type = UnitType.Pixel;
+                               return;
+                       }
+
+                       if (value [i] == '%'){
+                               type = UnitType.Percentage;
+                               i++;
+                               while (i < count && Char.IsWhiteSpace (value [i]))
+                                       i++;
+                               if (i != count)
+                                       throw new ArgumentOutOfRangeException ("value");
+                               return;
+                       }
+                       
+                       int j = i;
+                       while (j < count && Char.IsLetter (value [j]))
+                               j++;
+                       string code = value.Substring (i, j-i);
+                       switch (code.ToLower (CultureInfo.InvariantCulture)){
+                       case "in": type = UnitType.Inch; break;
+                       case "cm": type = UnitType.Cm; break;
+                       case "mm": type = UnitType.Mm; break;
+                       case "pt": type = UnitType.Point; break;
+                       case "pc": type = UnitType.Pica; break;
+                       case "em": type = UnitType.Em; break;
+                       case "ex": type = UnitType.Ex; break;
+                       case "px":
+                               type = UnitType.Pixel;
+                               if (dv != 0)
+                                       throw new FormatException ("Pixel units do not allow floating point values");
+                               break;
+                       default:
+                               throw new ArgumentOutOfRangeException ("value");
+                       }
+
+                       while (j < count && Char.IsWhiteSpace (value [j]))
+                               j++;
+                       if (j != count)
+                               throw new ArgumentOutOfRangeException ("value");
+               }
+
+               
+               public Unit (string value) : this (value, '.')
+               {
+               }
+
+               public Unit (string value, CultureInfo culture) : this (value, culture.NumberFormat.NumberDecimalSeparator [0])
+               {
+               }
+
+               internal Unit (string value, CultureInfo culture, UnitType t) : this (value, '.')
+               {
+               }
+               
+               public bool IsEmpty {
+                       get {
+                               return type == 0;
+                       }
+               }
+
+               public UnitType Type {
+                       get {
+                               if (type == 0)
+                                       return UnitType.Pixel;
+                               return type;
+                       }
+               }
+
+               public double Value {
+                       get {
+                               return value;
+                       }
+               }
+               
+               public static Unit Parse (string s)
+               {
+                       return new Unit (s);
+               }
+
+               public static System.Web.UI.WebControls.Unit Parse (string s, System.Globalization.CultureInfo culture)
+               {
+                       return new Unit (s, culture);
+               }
+               
+
+               public static Unit Percentage (double n)
+               {
+                       return new Unit (n, UnitType.Percentage);
+               }
+               
+               public static Unit Pixel (int n)
+               {
+                       return new Unit (n);
+               }
+               
+               public static Unit Point (int n)
+               {
+                       return new Unit (n, UnitType.Point);
+               }
+                               
+               public override bool Equals (object obj)
+               {
+                       if (obj is Unit){
+                               Unit other = (Unit) obj;
+                               return (other.type == type && other.value == value);
+                       }
+                       return false;
+               }
+               
+               public override int GetHashCode ()
+               {
+                       return Type.GetHashCode () ^ Value.GetHashCode ();
+               }
+               
+               public static bool operator == (Unit left, Unit right)
+               {
+                       return left.Type == right.Type && left.Value == right.Value;
+               }
+
+               public static bool operator != (Unit left, Unit right)
+               {
+                       return left.Type != right.Type || left.Value != right.Value;
+               }
+               
+               public static implicit operator Unit (int n)
+               {
+                       return new Unit (n);
+               }
+
+               string GetExtension ()
+               {
+                       switch (type){
+                       case UnitType.Pixel: return "px";
+                       case UnitType.Point: return "pt";
+                       case UnitType.Pica: return "pc";
+                       case UnitType.Inch: return "in";
+                       case UnitType.Mm: return "mm";
+                       case UnitType.Cm: return "cm";
+                       case UnitType.Percentage: return "%";
+                       case UnitType.Em: return "em";
+                       case UnitType.Ex: return "ex";
+                       default: return "";
+                       }
+               }
+
+               public string ToString (CultureInfo culture)
+               {
+                       if (type == 0)
+                               return "";
+                       
+                       string ex = GetExtension ();
+                       
+                       return String.Format (culture, "{0}{1}", value, ex);
+               }
+                       
+               public override string ToString ()
+               {
+                       return ToString (CultureInfo.InvariantCulture);
+               }
+
+#if NET_2_0
+               public string ToString (IFormatProvider provider)
+               {
+                       if (type == 0)
+                               return "";
+
+                       string ex = GetExtension ();
+
+                       return String.Format (provider, "{0}{1}", value, ex);
+               }
+#endif
+       }
+
+}