2007-12-19 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / PointConverter.cs
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 Novell, Inc (http://www.novell.com)
10 //
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.
29 //
30
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Globalization;
34 using System.ComponentModel.Design.Serialization;
35 using System.Reflection;
36
37 namespace System.Drawing {
38
39         public class PointConverter : TypeConverter
40         {
41                 public PointConverter() { }
42
43                 public override bool CanConvertFrom (ITypeDescriptorContext context,
44                                                      Type sourceType)
45                 {
46                         if (sourceType == typeof (string))
47                                 return true;
48
49                         return base.CanConvertFrom (context, sourceType);
50                 }
51
52                 public override bool CanConvertTo (ITypeDescriptorContext context,
53                                                    Type destinationType)
54                 {
55                         if (destinationType == typeof (string))
56                                 return true;
57
58                         if (destinationType == typeof (InstanceDescriptor))
59                                 return true;
60
61                         return base.CanConvertTo (context, destinationType);
62                 }
63
64                 public override object ConvertFrom (ITypeDescriptorContext context,
65                                                     CultureInfo culture,
66                                                     object value)
67                 {
68                         string s = value as string;
69                         if (s == null)
70                                 return base.ConvertFrom (context, culture, value);
71
72                         string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
73
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]);
78                         }
79
80                         if (subs.Length != 2)
81                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x, y.\"");
82
83                         return new Point (numSubs[0], numSubs[1]);
84                 }
85
86                 public override object ConvertTo (ITypeDescriptorContext context,
87                                                   CultureInfo culture,
88                                                   object value,
89                                                   Type destinationType)
90                 {
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);
98                         
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 });
103                         }
104
105                         return base.ConvertTo (context, culture, value, destinationType);
106                 }
107
108                 public override object CreateInstance (ITypeDescriptorContext context,
109                                                        IDictionary propertyValues)
110                 {
111 #if NET_2_0
112                         object ox = propertyValues ["X"];
113                         object oy = propertyValues ["Y"];
114                         if ((ox == null) || (oy == null))
115                                 throw new ArgumentException ("propertyValues");
116
117                         int x = (int) ox;
118                         int y = (int) oy;
119 #else
120                         int x = (int) propertyValues ["X"];
121                         int y = (int) propertyValues ["Y"];
122 #endif
123                         return new Point (x, y);
124                 }
125
126
127                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
128                 {
129                         return true;
130                 }
131
132                 public override PropertyDescriptorCollection GetProperties (
133                                                         ITypeDescriptorContext context,
134                                                         object value, Attribute[] attributes)
135                 {
136                         if (value is Point)
137                                 return TypeDescriptor.GetProperties (value, attributes);
138
139                         return base.GetProperties (context, value, attributes);
140                 }
141                 
142                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
143                 {
144                         return true;
145                 }
146
147         }
148 }