**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / PointConverter.cs
blob9811f226855defdc45d9188d99e13cf22cc87342
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 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Globalization;
37 namespace System.Drawing
39 /// <summary>
40 /// Summary description for PointConverter.
41 /// </summary>
42 public class PointConverter : TypeConverter
44 public PointConverter() { }
46 public override bool CanConvertFrom (ITypeDescriptorContext context,
47 Type sourceType)
49 if (sourceType == typeof (string))
50 return true;
52 return base.CanConvertFrom (context, sourceType);
55 public override bool CanConvertTo (ITypeDescriptorContext context,
56 Type destinationType)
58 if (destinationType == typeof (string))
59 return true;
61 return base.CanConvertTo (context, destinationType);
64 public override object ConvertFrom (ITypeDescriptorContext context,
65 CultureInfo culture,
66 object value)
68 string s = value as string;
69 if (s == null)
70 return base.ConvertFrom (context, culture, value);
72 // FIXME: use culture
73 string [] subs = s.Split (',');
74 if (subs.Length != 2)
75 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x, y.\"");
77 int x = Int32.Parse (subs [0]);
78 int y = Int32.Parse (subs [1]);
80 return new Point (x, y);
83 public override object ConvertTo (ITypeDescriptorContext context,
84 CultureInfo culture,
85 object value,
86 Type destinationType)
88 // LAMESPEC: "The default implementation calls the object's
89 // ToString method if the object is valid and if the destination
90 // type is string." MS does not behave as per the specs.
91 // Oh well, we have to be compatible with MS.
92 if ((destinationType == typeof (string)) && (value is Point))
93 return ((Point) value).X + ", " + ((Point) value).Y;
95 return base.ConvertTo (context, culture, value, destinationType);
98 public override object CreateInstance (ITypeDescriptorContext context,
99 IDictionary propertyValues)
101 int x = (int) propertyValues ["X"];
102 int y = (int) propertyValues ["Y"];
104 return new Point (x, y);
108 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
110 return true;
113 public override PropertyDescriptorCollection GetProperties (
114 ITypeDescriptorContext context,
115 object value, Attribute[] attributes)
117 if (value is Point)
118 return TypeDescriptor.GetProperties (value, attributes);
120 return base.GetProperties (context, value, attributes);
123 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
125 return true;