Fixed ConvertTo method in Size/Point/Rectangle converters.
[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 Novell, Inc. http://www.novell.com
11 //
12
13 using System;
14 using System.ComponentModel;
15 using System.Collections;
16 using System.Globalization;
17 using System.Text;
18
19 namespace System.Drawing
20 {
21         /// <summary>
22         /// Summary description for RectangleConverter.
23         /// </summary>
24         public class RectangleConverter : TypeConverter
25         {
26                 public RectangleConverter ()
27                 {
28                 }
29                 
30                 public override bool CanConvertFrom (ITypeDescriptorContext context,
31                                                      Type sourceType)
32                 {
33                         if (sourceType == typeof (string))
34                                 return true;
35
36                         return base.CanConvertFrom (context, sourceType);
37                 }
38
39                 public override bool CanConvertTo (ITypeDescriptorContext context,
40                                                    Type destinationType)
41                 {
42                         if (destinationType == typeof (string))
43                                 return true;
44
45                         return base.CanConvertTo (context, destinationType);
46                 }
47
48                 public override object ConvertFrom (ITypeDescriptorContext context,
49                                                     CultureInfo culture,
50                                                     object value)
51                 {
52                         string s = value as string;
53                         if (s == null)
54                                 return base.ConvertFrom (context, culture, value);
55
56                         string [] subs = s.Split (',');
57                         if (subs.Length != 4)
58                                 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
59
60                         int x = Int32.Parse (subs [0]);
61                         int y = Int32.Parse (subs [1]);
62                         int width = Int32.Parse (subs [2]);
63                         int height = Int32.Parse (subs [3]);
64
65                         return new Rectangle (x, y, width, height);
66                 }
67
68                 public override object ConvertTo (ITypeDescriptorContext context,
69                                                   CultureInfo culture,
70                                                   object value,
71                                                   Type destinationType)
72                 {
73                         if ((destinationType == typeof (string)) && (value is Rectangle)) {
74                                 Rectangle rect = (Rectangle) value;
75                                 StringBuilder sb = new StringBuilder ();
76                                 sb.Append (rect.X); sb.Append (", ");
77                                 sb.Append (rect.Y); sb.Append (", ");
78                                 sb.Append (rect.Width); sb.Append (", ");
79                                 sb.Append (rect.Height);
80                                 return sb.ToString ();
81                         }
82                         
83                         return base.ConvertTo (context, culture, value, destinationType);
84                 }
85
86                 public override object CreateInstance (ITypeDescriptorContext context,
87                                                        IDictionary propertyValues)
88                 {
89                         int x = (int) propertyValues ["X"];
90                         int y = (int) propertyValues ["Y"];
91                         int width = (int) propertyValues ["Width"];
92                         int height = (int) propertyValues ["Height"];
93
94                         return new Rectangle (x, y, width, height);
95                 }
96
97                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
98                 {
99                         return true;
100                 }
101
102                 [MonoTODO]
103                 public override PropertyDescriptorCollection GetProperties (
104                                                         ITypeDescriptorContext context,
105                                                         object value, Attribute[] attributes)
106                 {
107                         throw new NotImplementedException ();
108                 }
109                 
110                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
111                 {
112                         return true;
113                 }
114         }
115 }