[System.Drawing] Add support for a null culture argument to the ConvertFrom () method...
[mono.git] / mcs / class / System.Drawing / System.Drawing / SizeConverter.cs
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,2004,2006,2008 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Globalization;
35 using System.ComponentModel.Design.Serialization;
36 using System.Reflection;
37
38 namespace System.Drawing {
39
40         public class SizeConverter : TypeConverter
41         {
42                 public SizeConverter()
43                 {
44                 }
45
46                 public override bool CanConvertFrom (ITypeDescriptorContext context,
47                                                      Type sourceType)
48                 {
49                         if (sourceType == typeof (string))
50                                 return true;
51
52                         return base.CanConvertFrom (context, sourceType);
53                 }
54
55                 public override bool CanConvertTo (ITypeDescriptorContext context,
56                                                    Type destinationType)
57                 {
58                         if (destinationType == typeof (string))
59                                 return true;
60
61                         if (destinationType == typeof (InstanceDescriptor))
62                                 return true;
63
64                         return base.CanConvertTo (context, destinationType);
65                 }
66
67                 public override object ConvertFrom (ITypeDescriptorContext context,
68                                                     CultureInfo culture,
69                                                     object value)
70                 {
71                         if (culture == null)
72                                 culture = CultureInfo.CurrentCulture;
73                         string s = value as string;
74                         if (s == null)
75                                 return base.ConvertFrom (context, culture, value);
76
77                         string[] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
78
79                         Int32Converter converter = new Int32Converter ();
80                         int[] numSubs = new int[subs.Length];
81                         for (int i = 0; i < numSubs.Length; i++) {
82                                 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
83                         }
84
85                         if (subs.Length != 2)
86                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");
87
88                         return new Size (numSubs[0], numSubs[1]);
89                 }
90
91                 public override object ConvertTo (ITypeDescriptorContext context,
92                                                   CultureInfo culture,
93                                                   object value,
94                                                   Type destinationType)
95                 {
96                         if (culture == null)
97                                 culture = CultureInfo.CurrentCulture;
98                         // LAMESPEC: "The default implementation calls the ToString method
99                         // of the object if the object is valid and if the destination
100                         // type is string." MS does not behave as per the specs.
101                         // Oh well, we have to be compatible with MS.
102                         if (value is Size) {
103                                 Size size = (Size) value;
104                                 if (destinationType == typeof (string)) {
105                                         return size.Width.ToString (culture) + culture.TextInfo.ListSeparator 
106                                                 + " " + size.Height.ToString (culture);
107                                 } else if (destinationType == typeof (InstanceDescriptor)) {
108                                         ConstructorInfo ctor = typeof(Size).GetConstructor (new Type[] {typeof(int), typeof(int)});
109                                         return new InstanceDescriptor (ctor, new object[] { size.Width, size.Height });
110                                 }
111                         }
112                         
113                         return base.ConvertTo (context, culture, value, destinationType);
114                 }
115
116                 public override object CreateInstance (ITypeDescriptorContext context,
117                                                        IDictionary propertyValues)
118                 {
119 #if NET_2_0
120                         object ow = propertyValues ["Width"];
121                         object oh = propertyValues ["Height"];
122                         if ((ow == null) || (oh == null))
123                                 throw new ArgumentException ("propertyValues");
124
125                         int width = (int) ow;
126                         int height = (int) oh;
127 #else
128                         int width = (int) propertyValues ["Width"];
129                         int height = (int) propertyValues ["Height"];
130 #endif
131                         return new Size (width, height);
132                 }
133
134                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
135                 {
136                         return true;
137                 }
138
139                 public override PropertyDescriptorCollection GetProperties (
140                                                         ITypeDescriptorContext context,
141                                                         object value, Attribute[] attributes)
142                 {
143                         if (value is Size)
144                                 return TypeDescriptor.GetProperties (value, attributes);
145
146                         return base.GetProperties (context, value, attributes);
147                 }
148                 
149                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
150                 {
151                         return true;
152                 }
153         }
154 }