2 // Copyright (C) 2005, 2008 Novell, Inc (http://www.novell.com)
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.
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>
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))
55 return base.CanConvertFrom (context
, sourceType
);
58 public override bool CanConvertTo (ITypeDescriptorContext context
, Type destinationType
)
60 if (destinationType
== typeof (string))
63 if (destinationType
== typeof (InstanceDescriptor
))
66 return base.CanConvertTo (context
, destinationType
);
69 public override object ConvertFrom (ITypeDescriptorContext context
, CultureInfo culture
, object value)
71 string s
= value as string;
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
]);
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
)
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
)
118 public override PropertyDescriptorCollection
GetProperties (ITypeDescriptorContext context
, object value, Attribute
[] attributes
)
121 return TypeDescriptor
.GetProperties (value, attributes
);
123 return base.GetProperties (context
, value, attributes
);
126 public override bool GetPropertiesSupported (ITypeDescriptorContext context
)