Allow mono-wrapper executable to be overridden by environment
[mono.git] / mcs / class / System.Drawing / System.Drawing / RectangleConverter.cs
1 //
2 // System.Drawing.RectangleConverter.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Jordi Mas (jordi@ximian.com)
7 //      Ravindra (rkumar@novell.com)
8 //      
9 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 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.ComponentModel;
33 using System.Collections;
34 using System.Globalization;
35 using System.Text;
36 using System.ComponentModel.Design.Serialization;
37 using System.Reflection;
38
39 namespace System.Drawing {
40
41         public class RectangleConverter : TypeConverter
42         {
43                 public RectangleConverter ()
44                 {
45                 }
46                 
47                 public override bool CanConvertFrom (ITypeDescriptorContext context,
48                                                      Type sourceType)
49                 {
50                         if (sourceType == typeof (string))
51                                 return true;
52
53                         return base.CanConvertFrom (context, sourceType);
54                 }
55
56                 public override bool CanConvertTo (ITypeDescriptorContext context,
57                                                    Type destinationType)
58                 {
59                         if (destinationType == typeof (string))
60                                 return true;
61
62                         if (destinationType == typeof (InstanceDescriptor))
63                                 return true;
64
65                         return base.CanConvertTo (context, destinationType);
66                 }
67
68                 public override object ConvertFrom (ITypeDescriptorContext context,
69                                                     CultureInfo culture,
70                                                     object value)
71                 {
72                         string s = value as string;
73                         if (s == null)
74                                 return base.ConvertFrom (context, culture, value);
75
76                         if (culture == null)
77                                 culture = CultureInfo.CurrentCulture;
78                         string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());
79
80                         Int32Converter converter = new Int32Converter ();
81                         int[] numSubs = new int[subs.Length];
82                         for (int i = 0; i < numSubs.Length; i++) {
83                                 numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
84                         }
85
86                         if (subs.Length != 4)
87                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
88
89                         return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
90                 }
91
92                 public override object ConvertTo (ITypeDescriptorContext context,
93                                                   CultureInfo culture,
94                                                   object value,
95                                                   Type destinationType)
96                 {
97                         // LAMESPEC: "The default implementation calls the object's
98                         // ToString method if the object is valid and if the destination
99                         // type is string." MS does not behave as per the specs.
100                         // Oh well, we have to be compatible with MS.
101                         if (value is Rectangle) {
102                                 Rectangle rect = (Rectangle) value;
103                                 if (destinationType == typeof (string)) {
104                                         string separator = culture.TextInfo.ListSeparator;
105                                         StringBuilder sb = new StringBuilder ();
106                                         sb.Append (rect.X.ToString (culture));
107                                         sb.Append (separator);
108                                         sb.Append (" ");
109                                         sb.Append (rect.Y.ToString (culture));
110                                         sb.Append (separator);
111                                         sb.Append (" ");
112                                         sb.Append (rect.Width.ToString (culture));
113                                         sb.Append (separator);
114                                         sb.Append (" ");
115                                         sb.Append (rect.Height.ToString (culture));
116                                         return sb.ToString ();
117                                 } else if (destinationType == typeof (InstanceDescriptor)) {
118                                         ConstructorInfo ctor = typeof(Rectangle).GetConstructor (new Type[] {typeof(int), typeof(int), typeof(int), typeof(int)} );
119                                         return new InstanceDescriptor (ctor, new object[] {rect.X, rect.Y, rect.Width, rect.Height});
120                                 }
121                         }
122
123                         return base.ConvertTo (context, culture, value, destinationType);
124                 }
125
126                 public override object CreateInstance (ITypeDescriptorContext context,
127                                                        IDictionary propertyValues)
128                 {
129                         object ox = propertyValues ["X"];
130                         object oy = propertyValues ["Y"];
131                         object ow = propertyValues ["Width"];
132                         object oh = propertyValues ["Height"];
133                         if ((ox == null) || (oy == null) || (ow == null) || (oh == null))
134                                 throw new ArgumentException ("propertyValues");
135
136                         int x = (int) ox;
137                         int y = (int) oy;
138                         int width = (int) ow;
139                         int height = (int) oh;
140                         return new Rectangle (x, y, width, height);
141                 }
142
143                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
144                 {
145                         return true;
146                 }
147
148                 public override PropertyDescriptorCollection GetProperties (
149                                                         ITypeDescriptorContext context,
150                                                         object value, Attribute[] attributes)
151                 {
152                         if (value is Rectangle)
153                                 return TypeDescriptor.GetProperties (value, attributes);
154
155                         return base.GetProperties (context, value, attributes);
156                 }
157                 
158                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
159                 {
160                         return true;
161                 }
162         }
163 }