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,2004,2006 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.Collections;
33 using System.ComponentModel;
34 using System.Globalization;
35 using System.ComponentModel.Design.Serialization;
36 using System.Reflection;
38 namespace System.Drawing {
40 public class SizeConverter : TypeConverter
42 public SizeConverter()
46 public override bool CanConvertFrom (ITypeDescriptorContext context,
49 if (sourceType == typeof (string))
52 return base.CanConvertFrom (context, sourceType);
55 public override bool CanConvertTo (ITypeDescriptorContext context,
58 if (destinationType == typeof (string))
61 if (destinationType == typeof (InstanceDescriptor))
64 return base.CanConvertTo (context, destinationType);
67 public override object ConvertFrom (ITypeDescriptorContext context,
71 string s = value as string;
73 return base.ConvertFrom (context, culture, value);
75 string[] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
77 Int32Converter converter = new Int32Converter ();
78 int[] numSubs = new int[subs.Length];
79 for (int i = 0; i < numSubs.Length; i++) {
80 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
84 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");
86 return new Size (numSubs[0], numSubs[1]);
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.ToString(culture) + culture.TextInfo.ListSeparator
100 + " " + ((Size) value).Height.ToString(culture);
102 if (destinationType == typeof (InstanceDescriptor) && value is Size) {
103 Size s = (Size) value;
104 ConstructorInfo ctor = typeof(Size).GetConstructor (new Type[] {typeof(int), typeof(int)});
105 return new InstanceDescriptor (ctor, new object[] {s.Width, s.Height});
108 return base.ConvertTo (context, culture, value, destinationType);
111 public override object CreateInstance (ITypeDescriptorContext context,
112 IDictionary propertyValues)
115 object ow = propertyValues ["Width"];
116 object oh = propertyValues ["Height"];
117 if ((ow == null) || (oh == null))
118 throw new ArgumentException ("propertyValues");
120 int width = (int) ow;
121 int height = (int) oh;
123 int width = (int) propertyValues ["Width"];
124 int height = (int) propertyValues ["Height"];
126 return new Size (width, height);
129 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
134 public override PropertyDescriptorCollection GetProperties (
135 ITypeDescriptorContext context,
136 object value, Attribute[] attributes)
139 return TypeDescriptor.GetProperties (value, attributes);
141 return base.GetProperties (context, value, attributes);
144 public override bool GetPropertiesSupported (ITypeDescriptorContext context)