* support-test-*.cs: Rename from test-*-p2.cs.
[mono.git] / mcs / class / System.Drawing / System.Drawing / RectangleConverter.cs
1 //
2 // System.Drawing.RectangleConverter.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Jordi Mas (jordi@ximian.com)
7 //      Ravindra (rkumar@novell.com)
8 //      
9 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.ComponentModel;
38 using System.Collections;
39 using System.Globalization;
40 using System.Text;
41 using System.ComponentModel.Design.Serialization;
42 using System.Reflection;
43
44 namespace System.Drawing
45 {
46         /// <summary>
47         /// Summary description for RectangleConverter.
48         /// </summary>
49         public class RectangleConverter : TypeConverter
50         {
51                 public RectangleConverter ()
52                 {
53                 }
54                 
55                 public override bool CanConvertFrom (ITypeDescriptorContext context,
56                                                      Type sourceType)
57                 {
58                         if (sourceType == typeof (string))
59                                 return true;
60
61                         return base.CanConvertFrom (context, sourceType);
62                 }
63
64                 public override bool CanConvertTo (ITypeDescriptorContext context,
65                                                    Type destinationType)
66                 {
67                         if (destinationType == typeof (string))
68                                 return true;
69
70                         if (destinationType == typeof (InstanceDescriptor))
71                                 return true;
72
73                         return base.CanConvertTo (context, destinationType);
74                 }
75
76                 public override object ConvertFrom (ITypeDescriptorContext context,
77                                                     CultureInfo culture,
78                                                     object value)
79                 {
80                         string s = value as string;
81                         if (s == null)
82                                 return base.ConvertFrom (context, culture, value);
83
84                         string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
85
86                         Int32Converter converter = new Int32Converter ();
87                         int[] numSubs = new int[subs.Length];
88                         for (int i = 0; i < numSubs.Length; i++) {
89                                 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
90                         }
91
92                         if (subs.Length != 4)
93                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
94
95                         return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
96                 }
97
98                 public override object ConvertTo (ITypeDescriptorContext context,
99                                                   CultureInfo culture,
100                                                   object value,
101                                                   Type destinationType)
102                 {
103                         // LAMESPEC: "The default implementation calls the object's
104                         // ToString method if the object is valid and if the destination
105                         // type is string." MS does not behave as per the specs.
106                         // Oh well, we have to be compatible with MS.
107                         if ((destinationType == typeof (string)) && (value is Rectangle)) {
108                                 string separator = culture.TextInfo.ListSeparator;
109                                 Rectangle rect = (Rectangle) value;
110                                 StringBuilder sb = new StringBuilder ();
111                                 sb.Append (rect.X.ToString (culture));
112                                 sb.Append (separator);
113                                 sb.Append (" ");
114                                 sb.Append (rect.Y.ToString (culture));
115                                 sb.Append (separator);
116                                 sb.Append (" ");
117                                 sb.Append (rect.Width.ToString (culture));
118                                 sb.Append (separator);
119                                 sb.Append (" ");
120                                 sb.Append (rect.Height.ToString (culture));
121                                 return sb.ToString ();
122                         }
123                         
124                         if (destinationType == typeof (InstanceDescriptor) && value is Rectangle) {
125                                 Rectangle c = (Rectangle) value;
126                                 ConstructorInfo ctor = typeof(Rectangle).GetConstructor (new Type[] {typeof(int), typeof(int), typeof(int), typeof(int)} );
127                                 return new InstanceDescriptor (ctor, new object[] {c.X, c.Y, c.Width, c.Height});
128                         }
129
130                         return base.ConvertTo (context, culture, value, destinationType);
131                 }
132
133                 public override object CreateInstance (ITypeDescriptorContext context,
134                                                        IDictionary propertyValues)
135                 {
136                         int x = (int) propertyValues ["X"];
137                         int y = (int) propertyValues ["Y"];
138                         int width = (int) propertyValues ["Width"];
139                         int height = (int) propertyValues ["Height"];
140
141                         return new Rectangle (x, y, width, height);
142                 }
143
144                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
145                 {
146                         return true;
147                 }
148
149                 public override PropertyDescriptorCollection GetProperties (
150                                                         ITypeDescriptorContext context,
151                                                         object value, Attribute[] attributes)
152                 {
153                         if (value is Rectangle)
154                                 return TypeDescriptor.GetProperties (value, attributes);
155
156                         return base.GetProperties (context, value, attributes);
157                 }
158                 
159                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
160                 {
161                         return true;
162                 }
163         }
164 }