fix typo
[mcs.git] / class / System.Drawing / System.Drawing / PointConverter.cs
bloba584ca2cd8fcb13e16a513d2992979af6089863e
1 //
2 // System.Drawing.PointConverter.cs
3 //
4 // Authors:
5 // Dennis Hayes (dennish@Raytek.com)
6 // Ravindra (rkumar@novell.com)
7 //
8 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004, 2006, 2008 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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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,
44 Type sourceType)
46 if (sourceType == typeof (string))
47 return true;
49 return base.CanConvertFrom (context, sourceType);
52 public override bool CanConvertTo (ITypeDescriptorContext context,
53 Type destinationType)
55 if (destinationType == typeof (string))
56 return true;
58 if (destinationType == typeof (InstanceDescriptor))
59 return true;
61 return base.CanConvertTo (context, destinationType);
64 public override object ConvertFrom (ITypeDescriptorContext context,
65 CultureInfo culture,
66 object value)
68 if (culture == null)
69 culture = CultureInfo.CurrentCulture;
70 string s = value as string;
71 if (s == null)
72 return base.ConvertFrom (context, culture, value);
74 string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
76 Int32Converter converter = new Int32Converter ();
77 int[] numSubs = new int[subs.Length];
78 for (int i = 0; i < numSubs.Length; i++) {
79 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
82 if (subs.Length != 2)
83 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x, y.\"");
85 return new Point (numSubs[0], numSubs[1]);
88 public override object ConvertTo (ITypeDescriptorContext context,
89 CultureInfo culture,
90 object value,
91 Type destinationType)
93 if (culture == null)
94 culture = CultureInfo.CurrentCulture;
95 // LAMESPEC: "The default implementation calls the object's
96 // ToString method if the object is valid and if the destination
97 // type is string." MS does not behave as per the specs.
98 // Oh well, we have to be compatible with MS.
99 if (value is Point) {
100 Point point = (Point) value;
101 if (destinationType == typeof (string)) {
102 return point.X.ToString (culture) + culture.TextInfo.ListSeparator
103 + " " + point.Y.ToString (culture);
104 } else if (destinationType == typeof (InstanceDescriptor)) {
105 ConstructorInfo ctor = typeof(Point).GetConstructor (new Type[] {typeof(int), typeof(int)} );
106 return new InstanceDescriptor (ctor, new object[] {point.X, point.Y });
110 return base.ConvertTo (context, culture, value, destinationType);
113 public override object CreateInstance (ITypeDescriptorContext context,
114 IDictionary propertyValues)
116 #if NET_2_0
117 object ox = propertyValues ["X"];
118 object oy = propertyValues ["Y"];
119 if ((ox == null) || (oy == null))
120 throw new ArgumentException ("propertyValues");
122 int x = (int) ox;
123 int y = (int) oy;
124 #else
125 int x = (int) propertyValues ["X"];
126 int y = (int) propertyValues ["Y"];
127 #endif
128 return new Point (x, y);
132 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
134 return true;
137 public override PropertyDescriptorCollection GetProperties (
138 ITypeDescriptorContext context,
139 object value, Attribute[] attributes)
141 if (value is Point)
142 return TypeDescriptor.GetProperties (value, attributes);
144 return base.GetProperties (context, value, attributes);
147 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
149 return true;