2005-10-04 Zoltan Varga <vargaz@freemail.hu>
[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 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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.
30 //
31
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Globalization;
36 using System.ComponentModel.Design.Serialization;
37 using System.Reflection;
38
39 namespace System.Drawing
40 {
41         /// <summary>
42         /// Summary description for PointConverter.
43         /// </summary>
44         public class PointConverter : TypeConverter
45         {
46                 public PointConverter() { }
47
48                 public override bool CanConvertFrom (ITypeDescriptorContext context,
49                                                      Type sourceType)
50                 {
51                         if (sourceType == typeof (string))
52                                 return true;
53
54                         return base.CanConvertFrom (context, sourceType);
55                 }
56
57                 public override bool CanConvertTo (ITypeDescriptorContext context,
58                                                    Type destinationType)
59                 {
60                         if (destinationType == typeof (string))
61                                 return true;
62
63                         if (destinationType == typeof (InstanceDescriptor))
64                                 return true;
65
66                         return base.CanConvertTo (context, destinationType);
67                 }
68
69                 public override object ConvertFrom (ITypeDescriptorContext context,
70                                                     CultureInfo culture,
71                                                     object value)
72                 {
73                         string s = value as string;
74                         if (s == null)
75                                 return base.ConvertFrom (context, culture, value);
76
77                         string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
78
79                         Int32Converter converter = new Int32Converter ();
80                         int[] numSubs = new int[subs.Length];
81                         for (int i = 0; i < numSubs.Length; i++) {
82                                 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
83                         }
84
85                         if (subs.Length != 2)
86                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x, y.\"");
87
88                         return new Point (numSubs[0], numSubs[1]);
89                 }
90
91                 public override object ConvertTo (ITypeDescriptorContext context,
92                                                   CultureInfo culture,
93                                                   object value,
94                                                   Type destinationType)
95                 {
96                         // LAMESPEC: "The default implementation calls the object's
97                         // ToString method if the object is valid and if the destination
98                         // type is string." MS does not behave as per the specs.
99                         // Oh well, we have to be compatible with MS.
100                         if ((destinationType == typeof (string)) && (value is Point))
101                                 return ((Point) value).X.ToString(culture) + culture.TextInfo.ListSeparator 
102                                         + " " + ((Point) value).Y.ToString(culture);
103                         
104                         if (destinationType == typeof (InstanceDescriptor) && value is Point) {
105                                 Point c = (Point)value;
106                                 ConstructorInfo ctor = typeof(Point).GetConstructor (new Type[] {typeof(int), typeof(int)} );
107                                 return new InstanceDescriptor (ctor, new object[] {c.X, c.Y });
108                         }
109
110                         return base.ConvertTo (context, culture, value, destinationType);
111                 }
112
113                 public override object CreateInstance (ITypeDescriptorContext context,
114                                                        IDictionary propertyValues)
115                 {
116                         int x = (int) propertyValues ["X"];
117                         int y = (int) propertyValues ["Y"];
118
119                         return new Point (x, y);
120                 }
121
122
123                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
124                 {
125                         return true;
126                 }
127
128                 public override PropertyDescriptorCollection GetProperties (
129                                                         ITypeDescriptorContext context,
130                                                         object value, Attribute[] attributes)
131                 {
132                         if (value is Point)
133                                 return TypeDescriptor.GetProperties (value, attributes);
134
135                         return base.GetProperties (context, value, attributes);
136                 }
137                 
138                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
139                 {
140                         return true;
141                 }
142
143         }
144 }