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;
40 using System.ComponentModel.Design.Serialization;
41 using System.Reflection;
43 namespace System.Drawing
46 /// Summary description for SizeConverter.
48 public class SizeConverter : TypeConverter
50 public SizeConverter()
54 public override bool CanConvertFrom (ITypeDescriptorContext context,
57 if (sourceType == typeof (string))
60 return base.CanConvertFrom (context, sourceType);
63 public override bool CanConvertTo (ITypeDescriptorContext context,
66 if (destinationType == typeof (string))
69 if (destinationType == typeof (InstanceDescriptor))
72 return base.CanConvertTo (context, destinationType);
75 public override object ConvertFrom (ITypeDescriptorContext context,
79 string s = value as string;
81 return base.ConvertFrom (context, culture, value);
83 string[] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
85 Int32Converter converter = new Int32Converter ();
86 int[] numSubs = new int[subs.Length];
87 for (int i = 0; i < numSubs.Length; i++) {
88 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
92 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");
94 return new Size (numSubs[0], numSubs[1]);
97 public override object ConvertTo (ITypeDescriptorContext context,
100 Type destinationType)
102 // LAMESPEC: "The default implementation calls the ToString method
103 // of the object if the object is valid and if the destination
104 // type is string." MS does not behave as per the specs.
105 // Oh well, we have to be compatible with MS.
106 if ((destinationType == typeof (string)) && (value is Size))
107 return ((Size) value).Width.ToString(culture) + culture.TextInfo.ListSeparator
108 + " " + ((Size) value).Height.ToString(culture);
110 if (destinationType == typeof (InstanceDescriptor) && value is Size) {
111 Size s = (Size) value;
112 ConstructorInfo ctor = typeof(Size).GetConstructor (new Type[] {typeof(int), typeof(int)});
113 return new InstanceDescriptor (ctor, new object[] {s.Width, s.Height});
116 return base.ConvertTo (context, culture, value, destinationType);
119 public override object CreateInstance (ITypeDescriptorContext context,
120 IDictionary propertyValues)
122 int width = (int) propertyValues ["Width"];
123 int height = (int) propertyValues ["Height"];
125 return new Size (width, height);
128 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
133 public override PropertyDescriptorCollection GetProperties (
134 ITypeDescriptorContext context,
135 object value, Attribute[] attributes)
138 return TypeDescriptor.GetProperties (value, attributes);
140 return base.GetProperties (context, value, attributes);
143 public override bool GetPropertiesSupported (ITypeDescriptorContext context)