2 // System.Drawing.SizeConverter.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Ravindra (rkumar@novell.com)
9 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2003 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
.Collections
;
38 using System
.ComponentModel
;
39 using System
.Globalization
;
41 namespace System
.Drawing
44 /// Summary description for SizeConverter.
46 public class SizeConverter
: TypeConverter
48 public SizeConverter()
52 public override bool CanConvertFrom (ITypeDescriptorContext context
,
55 if (sourceType
== typeof (string))
58 return base.CanConvertFrom (context
, sourceType
);
61 public override bool CanConvertTo (ITypeDescriptorContext context
,
64 if (destinationType
== typeof (string))
67 return base.CanConvertTo (context
, destinationType
);
70 public override object ConvertFrom (ITypeDescriptorContext context
,
74 string s
= value as string;
76 return base.ConvertFrom (context
, culture
, value);
79 string [] subs
= s
.Split (',');
81 throw new ArgumentException ("Failed to parse Text(" + s
+ ") expected text in the format \"Width,Height.\"");
83 int width
= Int32
.Parse (subs
[0]);
84 int height
= Int32
.Parse (subs
[1]);
86 return new Size (width
, height
);
89 public override object ConvertTo (ITypeDescriptorContext context
,
94 // LAMESPEC: "The default implementation calls the ToString method
95 // of the object if the object is valid and if the destination
96 // type is string." MS does not behave as per the specs.
97 // Oh well, we have to be compatible with MS.
98 if ((destinationType
== typeof (string)) && (value is Size
))
99 return ((Size
) value).Width
+ ", " + ((Size
) value).Height
;
101 return base.ConvertTo (context
, culture
, value, destinationType
);
104 public override object CreateInstance (ITypeDescriptorContext context
,
105 IDictionary propertyValues
)
107 int width
= (int) propertyValues
["Width"];
108 int height
= (int) propertyValues
["Height"];
110 return new Size (width
, height
);
113 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context
)
118 public override PropertyDescriptorCollection
GetProperties (
119 ITypeDescriptorContext context
,
120 object value, Attribute
[] attributes
)
123 return TypeDescriptor
.GetProperties (value, attributes
);
125 return base.GetProperties (context
, value, attributes
);
128 public override bool GetPropertiesSupported (ITypeDescriptorContext context
)