2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Drawing / System.Drawing / SizeFConverter.cs
blob5dcb8f6d7748f94d792372410e2c3d02f2fe1979
1 //
2 // Copyright (C) 2005, 2008 Novell, Inc (http://www.novell.com)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Authors:
25 // Dennis Hayes (dennish@Raytek.com)
26 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
27 // Ravindra (rkumar@novell.com)
28 // Jordi Mas i Hernandez <jordimash@gmail.com>
32 #if NET_2_0
34 using System;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
38 using System.ComponentModel.Design.Serialization;
39 using System.Reflection;
41 namespace System.Drawing
43 public class SizeFConverter : TypeConverter
45 public SizeFConverter ()
50 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
52 if (sourceType == typeof (string))
53 return true;
55 return base.CanConvertFrom (context, sourceType);
58 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
60 if (destinationType == typeof (string))
61 return true;
63 if (destinationType == typeof (InstanceDescriptor))
64 return true;
66 return base.CanConvertTo (context, destinationType);
69 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
71 string s = value as string;
72 if (s == null)
73 return base.ConvertFrom (context, culture, value);
75 string[] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
77 SingleConverter converter = new SingleConverter ();
78 float[] numSubs = new float[subs.Length];
79 for (int i = 0; i < numSubs.Length; i++) {
80 numSubs[i] = (float) converter.ConvertFromString (context, culture, subs[i]);
83 if (subs.Length != 2)
84 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");
86 return new SizeF (numSubs[0], numSubs[1]);
90 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
92 if (value is SizeF) {
93 SizeF size = (SizeF) value;
94 if (destinationType == typeof (string)) {
95 return size.Width.ToString (culture) + culture.TextInfo.ListSeparator
96 + " " + size.Height.ToString (culture);
97 } else if (destinationType == typeof (InstanceDescriptor)) {
98 ConstructorInfo ctor = typeof(SizeF).GetConstructor (new Type[] {typeof(float), typeof(float)});
99 return new InstanceDescriptor (ctor, new object[] { size.Width, size.Height});
103 return base.ConvertTo (context, culture, value, destinationType);
106 public override object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
108 float w = (float) propertyValues ["Width"];
109 float h = (float) propertyValues ["Height"];
110 return new SizeF (w, h);
113 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
115 return true;
118 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
120 if (value is SizeF)
121 return TypeDescriptor.GetProperties (value, attributes);
123 return base.GetProperties (context, value, attributes);
126 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
128 return true;
133 #endif