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