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);
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
]);
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
,
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
);
107 sb
.Append (rect
.Y
.ToString (culture
));
108 sb
.Append (separator
);
110 sb
.Append (rect
.Width
.ToString (culture
));
111 sb
.Append (separator
);
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
)
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");
137 int width
= (int) ow
;
138 int height
= (int) oh
;
140 int x
= (int) propertyValues
["X"];
141 int y
= (int) propertyValues
["Y"];
142 int width
= (int) propertyValues
["Width"];
143 int height
= (int) propertyValues
["Height"];
145 return new Rectangle (x
, y
, width
, height
);
148 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context
)
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
)