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, 2006, 2008 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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.ComponentModel;
33 using System.Collections;
34 using System.Globalization;
36 using System.ComponentModel.Design.Serialization;
37 using System.Reflection;
39 namespace System.Drawing {
41 public class RectangleConverter : TypeConverter
43 public RectangleConverter ()
47 public override bool CanConvertFrom (ITypeDescriptorContext context,
50 if (sourceType == typeof (string))
53 return base.CanConvertFrom (context, sourceType);
56 public override bool CanConvertTo (ITypeDescriptorContext context,
59 if (destinationType == typeof (string))
62 if (destinationType == typeof (InstanceDescriptor))
65 return base.CanConvertTo (context, destinationType);
68 public override object ConvertFrom (ITypeDescriptorContext context,
72 string s = value as string;
74 return base.ConvertFrom (context, culture, value);
77 culture = CultureInfo.CurrentCulture;
78 string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
80 Int32Converter converter = new Int32Converter ();
81 int[] numSubs = new int[subs.Length];
82 for (int i = 0; i < numSubs.Length; i++) {
83 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
87 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
89 return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
92 public override object ConvertTo (ITypeDescriptorContext context,
97 // LAMESPEC: "The default implementation calls the object's
98 // ToString method if the object is valid and if the destination
99 // type is string." MS does not behave as per the specs.
100 // Oh well, we have to be compatible with MS.
101 if (value is Rectangle) {
102 Rectangle rect = (Rectangle) value;
103 if (destinationType == typeof (string)) {
104 string separator = culture.TextInfo.ListSeparator;
105 StringBuilder sb = new StringBuilder ();
106 sb.Append (rect.X.ToString (culture));
107 sb.Append (separator);
109 sb.Append (rect.Y.ToString (culture));
110 sb.Append (separator);
112 sb.Append (rect.Width.ToString (culture));
113 sb.Append (separator);
115 sb.Append (rect.Height.ToString (culture));
116 return sb.ToString ();
117 } else if (destinationType == typeof (InstanceDescriptor)) {
118 ConstructorInfo ctor = typeof(Rectangle).GetConstructor (new Type[] {typeof(int), typeof(int), typeof(int), typeof(int)} );
119 return new InstanceDescriptor (ctor, new object[] {rect.X, rect.Y, rect.Width, rect.Height});
123 return base.ConvertTo (context, culture, value, destinationType);
126 public override object CreateInstance (ITypeDescriptorContext context,
127 IDictionary propertyValues)
129 object ox = propertyValues ["X"];
130 object oy = propertyValues ["Y"];
131 object ow = propertyValues ["Width"];
132 object oh = propertyValues ["Height"];
133 if ((ox == null) || (oy == null) || (ow == null) || (oh == null))
134 throw new ArgumentException ("propertyValues");
138 int width = (int) ow;
139 int height = (int) oh;
140 return new Rectangle (x, y, width, height);
143 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
148 public override PropertyDescriptorCollection GetProperties (
149 ITypeDescriptorContext context,
150 object value, Attribute[] attributes)
152 if (value is Rectangle)
153 return TypeDescriptor.GetProperties (value, attributes);
155 return base.GetProperties (context, value, attributes);
158 public override bool GetPropertiesSupported (ITypeDescriptorContext context)