[System.Drawing] Add support for a null culture argument to the ConvertFrom () method...
[mono.git] / mcs / class / System.Drawing / System.Drawing / SizeConverter.cs
index 6b01d88f5716cd5a8a89d8fc090beff34d0bcddc..ba8d266cac454af73fc51306aa2103ed28c063c7 100644 (file)
@@ -7,11 +7,7 @@
 //     Ravindra (rkumar@novell.com)
 //
 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
-// Copyright (C) 2003 Novell, Inc. http://www.novell.com
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2003,2004,2006,2008 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
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Collections;
 using System.ComponentModel;
 using System.Globalization;
 using System.ComponentModel.Design.Serialization;
 using System.Reflection;
 
-namespace System.Drawing
-{
-       /// <summary>
-       /// Summary description for SizeConverter.
-       /// </summary>
+namespace System.Drawing {
+
        public class SizeConverter : TypeConverter
        {
                public SizeConverter()
@@ -76,6 +68,8 @@ namespace System.Drawing
                                                    CultureInfo culture,
                                                    object value)
                {
+                       if (culture == null)
+                               culture = CultureInfo.CurrentCulture;
                        string s = value as string;
                        if (s == null)
                                return base.ConvertFrom (context, culture, value);
@@ -99,18 +93,21 @@ namespace System.Drawing
                                                  object value,
                                                  Type destinationType)
                {
+                       if (culture == null)
+                               culture = CultureInfo.CurrentCulture;
                        // LAMESPEC: "The default implementation calls the ToString method
                        // of the object if the object is valid and if the destination
                        // type is string." MS does not behave as per the specs.
                        // Oh well, we have to be compatible with MS.
-                       if ((destinationType == typeof (string)) && (value is Size))
-                               return ((Size) value).Width.ToString(culture) + culture.TextInfo.ListSeparator 
-                                       + " " + ((Size) value).Height.ToString(culture);
-                       
-                       if (destinationType == typeof (InstanceDescriptor) && value is Size) {
-                               Size s = (Size) value;
-                               ConstructorInfo ctor = typeof(Size).GetConstructor (new Type[] {typeof(int), typeof(int)});
-                               return new InstanceDescriptor (ctor, new object[] {s.Width, s.Height});
+                       if (value is Size) {
+                               Size size = (Size) value;
+                               if (destinationType == typeof (string)) {
+                                       return size.Width.ToString (culture) + culture.TextInfo.ListSeparator 
+                                               + " " + size.Height.ToString (culture);
+                               } else if (destinationType == typeof (InstanceDescriptor)) {
+                                       ConstructorInfo ctor = typeof(Size).GetConstructor (new Type[] {typeof(int), typeof(int)});
+                                       return new InstanceDescriptor (ctor, new object[] { size.Width, size.Height });
+                               }
                        }
                        
                        return base.ConvertTo (context, culture, value, destinationType);
@@ -119,9 +116,18 @@ namespace System.Drawing
                public override object CreateInstance (ITypeDescriptorContext context,
                                                       IDictionary propertyValues)
                {
+#if NET_2_0
+                       object ow = propertyValues ["Width"];
+                       object oh = propertyValues ["Height"];
+                       if ((ow == null) || (oh == null))
+                               throw new ArgumentException ("propertyValues");
+
+                       int width = (int) ow;
+                       int height = (int) oh;
+#else
                        int width = (int) propertyValues ["Width"];
                        int height = (int) propertyValues ["Height"];
-
+#endif
                        return new Size (width, height);
                }