2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Drawing / System.Drawing / RectangleConverter.cs
blobf2ee8afa20aad34e77f5836cc4f36f556dd7f960
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, 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:
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.
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Globalization;
35 using System.Text;
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,
48 Type sourceType)
50 if (sourceType == typeof (string))
51 return true;
53 return base.CanConvertFrom (context, sourceType);
56 public override bool CanConvertTo (ITypeDescriptorContext context,
57 Type destinationType)
59 if (destinationType == typeof (string))
60 return true;
62 if (destinationType == typeof (InstanceDescriptor))
63 return true;
65 return base.CanConvertTo (context, destinationType);
68 public override object ConvertFrom (ITypeDescriptorContext context,
69 CultureInfo culture,
70 object value)
72 string s = value as string;
73 if (s == null)
74 return base.ConvertFrom (context, culture, value);
76 string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
78 Int32Converter converter = new Int32Converter ();
79 int[] numSubs = new int[subs.Length];
80 for (int i = 0; i < numSubs.Length; i++) {
81 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
84 if (subs.Length != 4)
85 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
87 return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
90 public override object ConvertTo (ITypeDescriptorContext context,
91 CultureInfo culture,
92 object value,
93 Type destinationType)
95 // LAMESPEC: "The default implementation calls the object's
96 // ToString method if the object is valid and if the destination
97 // type is string." MS does not behave as per the specs.
98 // Oh well, we have to be compatible with MS.
99 if (value is Rectangle) {
100 Rectangle rect = (Rectangle) value;
101 if (destinationType == typeof (string)) {
102 string separator = culture.TextInfo.ListSeparator;
103 StringBuilder sb = new StringBuilder ();
104 sb.Append (rect.X.ToString (culture));
105 sb.Append (separator);
106 sb.Append (" ");
107 sb.Append (rect.Y.ToString (culture));
108 sb.Append (separator);
109 sb.Append (" ");
110 sb.Append (rect.Width.ToString (culture));
111 sb.Append (separator);
112 sb.Append (" ");
113 sb.Append (rect.Height.ToString (culture));
114 return sb.ToString ();
115 } else if (destinationType == typeof (InstanceDescriptor)) {
116 ConstructorInfo ctor = typeof(Rectangle).GetConstructor (new Type[] {typeof(int), typeof(int), typeof(int), typeof(int)} );
117 return new InstanceDescriptor (ctor, new object[] {rect.X, rect.Y, rect.Width, rect.Height});
121 return base.ConvertTo (context, culture, value, destinationType);
124 public override object CreateInstance (ITypeDescriptorContext context,
125 IDictionary propertyValues)
127 #if NET_2_0
128 object ox = propertyValues ["X"];
129 object oy = propertyValues ["Y"];
130 object ow = propertyValues ["Width"];
131 object oh = propertyValues ["Height"];
132 if ((ox == null) || (oy == null) || (ow == null) || (oh == null))
133 throw new ArgumentException ("propertyValues");
135 int x = (int) ox;
136 int y = (int) oy;
137 int width = (int) ow;
138 int height = (int) oh;
139 #else
140 int x = (int) propertyValues ["X"];
141 int y = (int) propertyValues ["Y"];
142 int width = (int) propertyValues ["Width"];
143 int height = (int) propertyValues ["Height"];
144 #endif
145 return new Rectangle (x, y, width, height);
148 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
150 return true;
153 public override PropertyDescriptorCollection GetProperties (
154 ITypeDescriptorContext context,
155 object value, Attribute[] attributes)
157 if (value is Rectangle)
158 return TypeDescriptor.GetProperties (value, attributes);
160 return base.GetProperties (context, value, attributes);
163 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
165 return true;