2 // System.Drawing.RectangleConverter.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Jordi Mas (jordi@ximian.com)
7 // Ravindra (rkumar@novell.com)
9 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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.
37 using System.ComponentModel;
38 using System.Collections;
39 using System.Globalization;
41 using System.ComponentModel.Design.Serialization;
42 using System.Reflection;
44 namespace System.Drawing
47 /// Summary description for RectangleConverter.
49 public class RectangleConverter : TypeConverter
51 public RectangleConverter ()
55 public override bool CanConvertFrom (ITypeDescriptorContext context,
58 if (sourceType == typeof (string))
61 return base.CanConvertFrom (context, sourceType);
64 public override bool CanConvertTo (ITypeDescriptorContext context,
67 if (destinationType == typeof (string))
70 if (destinationType == typeof (InstanceDescriptor))
73 return base.CanConvertTo (context, destinationType);
76 public override object ConvertFrom (ITypeDescriptorContext context,
80 string s = value as string;
82 return base.ConvertFrom (context, culture, value);
84 string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
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]);
93 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
95 return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
98 public override object ConvertTo (ITypeDescriptorContext context,
101 Type destinationType)
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);
114 sb.Append (rect.Y.ToString (culture));
115 sb.Append (separator);
117 sb.Append (rect.Width.ToString (culture));
118 sb.Append (separator);
120 sb.Append (rect.Height.ToString (culture));
121 return sb.ToString ();
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});
130 return base.ConvertTo (context, culture, value, destinationType);
133 public override object CreateInstance (ITypeDescriptorContext context,
134 IDictionary propertyValues)
136 int x = (int) propertyValues ["X"];
137 int y = (int) propertyValues ["Y"];
138 int width = (int) propertyValues ["Width"];
139 int height = (int) propertyValues ["Height"];
141 return new Rectangle (x, y, width, height);
144 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
149 public override PropertyDescriptorCollection GetProperties (
150 ITypeDescriptorContext context,
151 object value, Attribute[] attributes)
153 if (value is Rectangle)
154 return TypeDescriptor.GetProperties (value, attributes);
156 return base.GetProperties (context, value, attributes);
159 public override bool GetPropertiesSupported (ITypeDescriptorContext context)