Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Drawing / System.Drawing / SizeFConverter.cs
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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.
22 //
23 // Authors:
24 //
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>
29 //
30 //
31
32 #if NET_2_0
33
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;
40
41 namespace System.Drawing
42 {
43         public class SizeFConverter : TypeConverter
44         {
45                 public SizeFConverter ()
46                 {
47
48                 }
49
50                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
51                 {
52                         if (sourceType == typeof (string))
53                                 return true;
54
55                         return base.CanConvertFrom (context, sourceType);
56                 }
57
58                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
59                 {
60                         if (destinationType == typeof (string))
61                                 return true;
62
63                         if (destinationType == typeof (InstanceDescriptor))
64                                 return true;
65
66                         return base.CanConvertTo (context, destinationType);
67                 }
68
69                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
70                 {
71                         string s = value as string;
72                         if (s == null)
73                                 return base.ConvertFrom (context, culture, value);
74
75                         string[] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
76
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]);
81                         }
82
83                         if (subs.Length != 2)
84                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");
85
86                         return new SizeF (numSubs[0], numSubs[1]);
87
88                 }
89
90                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
91                 {
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});
100                                 }
101                         }
102
103                         return base.ConvertTo (context, culture, value, destinationType);
104                 }
105
106                 public override object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
107                 {
108                         float w = (float) propertyValues ["Width"];
109                         float h = (float) propertyValues ["Height"];
110                         return new SizeF (w, h);
111                 }
112
113                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
114                 {
115                         return true;
116                 }
117
118                 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
119                 {
120                         if (value is SizeF)
121                                 return TypeDescriptor.GetProperties (value, attributes);
122
123                         return base.GetProperties (context, value, attributes);
124                 }
125
126                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
127                 {
128                         return true;
129                 }
130         }
131 }
132
133 #endif
134