* Style.cs: fixed restoring FontInfo from ViewState
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / UnitConverter.cs
index 0251f2508e48b28454e3ff6cd8efd0c78f561c48..3a2660ae1b623092273b23bdfbfba965f9a3a4d8 100644 (file)
@@ -1,3 +1,12 @@
+//
+// System.Web.UI.WebControls.Unit.cs
+//
+// Authors:
+//   Miguel de Icaza (miguel@novell.com)
+//   Ben Maurer (bmaurer@ximian.com).
+//
+// (C) 2005 Novell, Inc.
+//
 
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // 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
- * Class:     UnitConverter\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 (2002)\r
- */\r
-\r
-using System;\r
-using System.Globalization;\r
-using System.ComponentModel;\r
-using System.Web;\r
-using System.Web.UI;\r
-using System.ComponentModel.Design.Serialization;\r
-using System.Reflection;\r
-\r
-namespace System.Web.UI.WebControls\r
-{\r
-       public class UnitConverter : TypeConverter\r
-       {\r
-               public UnitConverter(): base()\r
-               {\r
-               }\r
-\r
-               public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\r
-               {\r
-                       if(sourceType == typeof(string))\r
-                               return true;\r
-                       return CanConvertFrom(context, sourceType);\r
-               }\r
-\r
-#if NET_2_0\r
-               public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)\r
-               {\r
-                       if (destinationType == typeof (string))\r
-                               return true;\r
-\r
-                       if (destinationType == typeof (InstanceDescriptor))\r
-                               return true;\r
-\r
-                       return base.CanConvertTo (context, destinationType);\r
-               }\r
-#endif\r
-\r
-               public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\r
-               {\r
-                       if(value == null)\r
-                               return null;\r
-                       if(value is string)\r
-                       {\r
-                               string val = ((string)value).Trim();\r
-                               if(val.Length == 0)\r
-                               {\r
-                                       return Unit.Empty;\r
-                               }\r
-                               return (culture == null ? Unit.Parse(val) : Unit.Parse(val, culture));\r
-                       }\r
-                       return ConvertFrom(context, culture, value);\r
-               }\r
-\r
-               public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\r
-               {\r
-                       if(destinationType == typeof(string))\r
-                       {\r
-                               Unit val = (Unit)value;\r
-                               if(val == Unit.Empty)\r
-                               {\r
-                                       return String.Empty;\r
-                               }\r
-                               return val.ToString(culture);\r
-                       }\r
-\r
-#if NET_2_0\r
-                       if (destinationType == typeof (InstanceDescriptor) && value is Unit) {\r
-                               Unit s = (Unit) value;
-                               ConstructorInfo ci = typeof(Unit).GetConstructor (new Type[] { typeof(double), typeof(UnitType) });\r
-                               return new InstanceDescriptor (ci, new object[] { s.Value, s.Type });\r
-                       }\r
 
-                       if (destinationType == typeof (InstanceDescriptor) && value is string) {\r
-                               Unit s = Unit.Parse ((string)value, CultureInfo.InvariantCulture);
-                               ConstructorInfo ci = typeof(Unit).GetConstructor (new Type[] { typeof(double), typeof(UnitType) });\r
-                               return new InstanceDescriptor (ci, new object[] { s.Value, s.Type });\r
-                       }
-#endif\r
-                       \r
-                       return ConvertTo(context, culture, value, destinationType);\r
-               }\r
-       }\r
-}\r
+using System.Globalization;
+using System.ComponentModel;
+using System.Security.Permissions;
+
+namespace System.Web.UI.WebControls {
+
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       public class UnitConverter : TypeConverter {
+
+               public UnitConverter ()
+               {
+               }
+
+               public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
+               {
+                       if (sourceType == typeof (string))
+                               return true;
+
+                       return base.CanConvertFrom (context, sourceType);
+               }
+               
+               public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
+               {
+                       if (value is Unit && destinationType == typeof (string))
+                               return ((Unit) value).ToString (culture);
+
+                       return base.ConvertTo (context, culture, value, destinationType);
+               }
+               
+               public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
+               {
+                       if (value == null)
+                               return null;
+                                       
+                       Type t = value.GetType ();
+                       if (t == typeof (string))
+                               return new Unit ((string) value, culture);
+
+                       return base.ConvertFrom (context, culture, value);
+               }
+       }
+}