**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / SizeConverter.cs
blobb4ca5c24af18491faba214a3b90f6c96b87d87ae
1 //
2 // System.Drawing.SizeConverter.cs
3 //
4 // Authors:
5 // Dennis Hayes (dennish@Raytek.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Ravindra (rkumar@novell.com)
8 //
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:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
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.
36 using System;
37 using System.Collections;
38 using System.ComponentModel;
39 using System.Globalization;
41 namespace System.Drawing
43 /// <summary>
44 /// Summary description for SizeConverter.
45 /// </summary>
46 public class SizeConverter : TypeConverter
48 public SizeConverter()
52 public override bool CanConvertFrom (ITypeDescriptorContext context,
53 Type sourceType)
55 if (sourceType == typeof (string))
56 return true;
58 return base.CanConvertFrom (context, sourceType);
61 public override bool CanConvertTo (ITypeDescriptorContext context,
62 Type destinationType)
64 if (destinationType == typeof (string))
65 return true;
67 return base.CanConvertTo (context, destinationType);
70 public override object ConvertFrom (ITypeDescriptorContext context,
71 CultureInfo culture,
72 object value)
74 string s = value as string;
75 if (s == null)
76 return base.ConvertFrom (context, culture, value);
78 // FIXME: use culture
79 string [] subs = s.Split (',');
80 if (subs.Length != 2)
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,
90 CultureInfo culture,
91 object value,
92 Type destinationType)
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)
115 return true;
118 public override PropertyDescriptorCollection GetProperties (
119 ITypeDescriptorContext context,
120 object value, Attribute[] attributes)
122 if (value is Size)
123 return TypeDescriptor.GetProperties (value, attributes);
125 return base.GetProperties (context, value, attributes);
128 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
130 return true;