2 // System.Drawing.PointConverter.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Ravindra (rkumar@novell.com)
8 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Globalization;
34 using System.ComponentModel.Design.Serialization;
35 using System.Reflection;
37 namespace System.Drawing {
39 public class PointConverter : TypeConverter
41 public PointConverter() { }
43 public override bool CanConvertFrom (ITypeDescriptorContext context,
46 if (sourceType == typeof (string))
49 return base.CanConvertFrom (context, sourceType);
52 public override bool CanConvertTo (ITypeDescriptorContext context,
55 if (destinationType == typeof (string))
58 if (destinationType == typeof (InstanceDescriptor))
61 return base.CanConvertTo (context, destinationType);
64 public override object ConvertFrom (ITypeDescriptorContext context,
68 string s = value as string;
70 return base.ConvertFrom (context, culture, value);
72 string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
74 Int32Converter converter = new Int32Converter ();
75 int[] numSubs = new int[subs.Length];
76 for (int i = 0; i < numSubs.Length; i++) {
77 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
81 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x, y.\"");
83 return new Point (numSubs[0], numSubs[1]);
86 public override object ConvertTo (ITypeDescriptorContext context,
91 // LAMESPEC: "The default implementation calls the object's
92 // ToString method if the object is valid and if the destination
93 // type is string." MS does not behave as per the specs.
94 // Oh well, we have to be compatible with MS.
95 if ((destinationType == typeof (string)) && (value is Point))
96 return ((Point) value).X.ToString(culture) + culture.TextInfo.ListSeparator
97 + " " + ((Point) value).Y.ToString(culture);
99 if (destinationType == typeof (InstanceDescriptor) && value is Point) {
100 Point c = (Point)value;
101 ConstructorInfo ctor = typeof(Point).GetConstructor (new Type[] {typeof(int), typeof(int)} );
102 return new InstanceDescriptor (ctor, new object[] {c.X, c.Y });
105 return base.ConvertTo (context, culture, value, destinationType);
108 public override object CreateInstance (ITypeDescriptorContext context,
109 IDictionary propertyValues)
112 object ox = propertyValues ["X"];
113 object oy = propertyValues ["Y"];
114 if ((ox == null) || (oy == null))
115 throw new ArgumentException ("propertyValues");
120 int x = (int) propertyValues ["X"];
121 int y = (int) propertyValues ["Y"];
123 return new Point (x, y);
127 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
132 public override PropertyDescriptorCollection GetProperties (
133 ITypeDescriptorContext context,
134 object value, Attribute[] attributes)
137 return TypeDescriptor.GetProperties (value, attributes);
139 return base.GetProperties (context, value, attributes);
142 public override bool GetPropertiesSupported (ITypeDescriptorContext context)