New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing / RectangleConverter.cs
index d1b4b99c6b05bc8e34f3ce410469c7b961b29b32..b9c96e5a96e3c16c85a790475f58a5b81207fe38 100644 (file)
@@ -7,11 +7,7 @@
 //     Ravindra (rkumar@novell.com)
 //     
 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
-// Copyright (C) 2004 Novell, Inc. http://www.novell.com
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2006 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.ComponentModel;
 using System.Collections;
 using System.Globalization;
 using System.Text;
+using System.ComponentModel.Design.Serialization;
+using System.Reflection;
+
+namespace System.Drawing {
 
-namespace System.Drawing
-{
-       /// <summary>
-       /// Summary description for RectangleConverter.
-       /// </summary>
        public class RectangleConverter : TypeConverter
        {
                public RectangleConverter ()
@@ -65,6 +59,9 @@ namespace System.Drawing
                        if (destinationType == typeof (string))
                                return true;
 
+                       if (destinationType == typeof (InstanceDescriptor))
+                               return true;
+
                        return base.CanConvertTo (context, destinationType);
                }
 
@@ -76,17 +73,18 @@ namespace System.Drawing
                        if (s == null)
                                return base.ConvertFrom (context, culture, value);
 
-                       // FIXME: use culture
-                       string [] subs = s.Split (',');
+                       string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
+
+                       Int32Converter converter = new Int32Converter ();
+                       int[] numSubs = new int[subs.Length];
+                       for (int i = 0; i < numSubs.Length; i++) {
+                               numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
+                       }
+
                        if (subs.Length != 4)
                                throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
 
-                       int x = Int32.Parse (subs [0]);
-                       int y = Int32.Parse (subs [1]);
-                       int width = Int32.Parse (subs [2]);
-                       int height = Int32.Parse (subs [3]);
-
-                       return new Rectangle (x, y, width, height);
+                       return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
                }
 
                public override object ConvertTo (ITypeDescriptorContext context,
@@ -97,29 +95,54 @@ namespace System.Drawing
                        // LAMESPEC: "The default implementation calls the object's
                        // ToString method if the object is valid and if the destination
                        // type is string." MS does not behave as per the specs.
-                       // Oh well, it is just a string and there is no harm in behaving
-                       // like MS.
+                       // Oh well, we have to be compatible with MS.
                        if ((destinationType == typeof (string)) && (value is Rectangle)) {
+                               string separator = culture.TextInfo.ListSeparator;
                                Rectangle rect = (Rectangle) value;
                                StringBuilder sb = new StringBuilder ();
-                               sb.Append (rect.X); sb.Append (", ");
-                               sb.Append (rect.Y); sb.Append (", ");
-                               sb.Append (rect.Width); sb.Append (", ");
-                               sb.Append (rect.Height);
+                               sb.Append (rect.X.ToString (culture));
+                               sb.Append (separator);
+                               sb.Append (" ");
+                               sb.Append (rect.Y.ToString (culture));
+                               sb.Append (separator);
+                               sb.Append (" ");
+                               sb.Append (rect.Width.ToString (culture));
+                               sb.Append (separator);
+                               sb.Append (" ");
+                               sb.Append (rect.Height.ToString (culture));
                                return sb.ToString ();
                        }
                        
+                       if (destinationType == typeof (InstanceDescriptor) && value is Rectangle) {
+                               Rectangle c = (Rectangle) value;
+                               ConstructorInfo ctor = typeof(Rectangle).GetConstructor (new Type[] {typeof(int), typeof(int), typeof(int), typeof(int)} );
+                               return new InstanceDescriptor (ctor, new object[] {c.X, c.Y, c.Width, c.Height});
+                       }
+
                        return base.ConvertTo (context, culture, value, destinationType);
                }
 
                public override object CreateInstance (ITypeDescriptorContext context,
                                                       IDictionary propertyValues)
                {
+#if NET_2_0
+                       object ox = propertyValues ["X"];
+                       object oy = propertyValues ["Y"];
+                       object ow = propertyValues ["Width"];
+                       object oh = propertyValues ["Height"];
+                       if ((ox == null) || (oy == null) || (ow == null) || (oh == null))
+                               throw new ArgumentException ("propertyValues");
+
+                       int x = (int) ox;
+                       int y = (int) oy;
+                       int width = (int) ow;
+                       int height = (int) oh;
+#else
                        int x = (int) propertyValues ["X"];
                        int y = (int) propertyValues ["Y"];
                        int width = (int) propertyValues ["Width"];
                        int height = (int) propertyValues ["Height"];
-
+#endif
                        return new Rectangle (x, y, width, height);
                }