Brush.jvm.cs: fixed transform methods, createContext
[mono.git] / mcs / class / System.Drawing / System.Drawing / ImageFormatConverter.cs
1 //
2 // System.Drawing.ImageFormatConverter.cs
3 //
4 // Author:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2002 Ximian, Inc
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.ComponentModel;
36 using System.Globalization;
37 using System.Drawing.Imaging;
38 using System.ComponentModel.Design.Serialization;
39 using System.Reflection;
40
41 namespace System.Drawing
42 {
43         /// <summary>
44         /// Summary description for ImageFormatConverter.
45         /// </summary>
46         public class ImageFormatConverter : TypeConverter
47         {
48                 public ImageFormatConverter ()
49                 {
50                 }
51
52                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type srcType)
53                 {
54                         if (srcType == typeof (string))
55                                 return true;
56                                 
57                         return base.CanConvertFrom (context, srcType);
58                 }
59
60                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destType)
61                 {
62                         if (destType == typeof (string))
63                                 return true;
64                                 
65                         if (destType == typeof (InstanceDescriptor))
66                                 return true;
67
68                         return base.CanConvertTo (context, destType);
69                 }
70                 
71                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object val)
72                 {                       
73                         string strFormat = val as string;
74                         if (strFormat == null)
75                                 return base.ConvertFrom (context, culture, val);
76                         
77                         if (strFormat.Equals (ImageFormat.Bmp.ToString ()))
78                                 return ImageFormat.Bmp;
79                         else if (strFormat.Equals (ImageFormat.Emf.ToString ()))
80                                 return ImageFormat.Emf;
81                         else if (strFormat.Equals (ImageFormat.Exif.ToString ()))
82                                 return ImageFormat.Exif;
83                         else if (strFormat.Equals (ImageFormat.Gif.ToString ()))
84                                 return ImageFormat.Gif;
85                         else if (strFormat.Equals (ImageFormat.Icon.ToString ()))
86                                 return ImageFormat.Icon;
87                         else if (strFormat.Equals (ImageFormat.Jpeg.ToString ()))
88                                 return ImageFormat.Jpeg;
89                         else if (strFormat.Equals (ImageFormat.MemoryBmp.ToString ()))
90                                 return ImageFormat.MemoryBmp;
91                         else if (strFormat.Equals (ImageFormat.Png.ToString ()))
92                                 return ImageFormat.Png;
93                         else if (strFormat.Equals (ImageFormat.Tiff.ToString ()))
94                                 return ImageFormat.Tiff;
95                         else if (strFormat.Equals (ImageFormat.Wmf.ToString ()))
96                                 return ImageFormat.Wmf;
97                                 
98                         return base.ConvertFrom (context, culture, val);
99                 }
100
101                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object val, Type destType )
102                 {
103                         if ((val is ImageFormat) && (destType == typeof (string)))
104                                 return val.ToString ();
105                         
106                         if (destType == typeof (InstanceDescriptor) && val is ImageFormat) {
107                                 ImageFormat c = (ImageFormat) val;
108
109                                 string prop = null;
110                                 if (c.Guid.Equals (ImageFormat.Bmp.Guid))
111                                         prop = "Bmp";
112                                 else if (c.Guid.Equals (ImageFormat.Emf.Guid))
113                                         prop = "Emf";
114                                 else if (c.Guid.Equals (ImageFormat.Exif.Guid))
115                                         prop = "Exif";
116                                 else if (c.Guid.Equals (ImageFormat.Gif.Guid))
117                                         prop = "Gif";
118                                 else if (c.Guid.Equals (ImageFormat.Icon.Guid))
119                                         prop = "Icon";
120                                 else if (c.Guid.Equals (ImageFormat.Jpeg.Guid))
121                                         prop = "Jpeg";
122                                 else if (c.Guid.Equals (ImageFormat.MemoryBmp.Guid))
123                                         prop = "MemoryBmp";
124                                 else if (c.Guid.Equals (ImageFormat.Png.Guid))
125                                         prop = "Png";
126                                 else if (c.Guid.Equals (ImageFormat.Tiff.Guid))
127                                         prop = "Tiff";
128                                 else if (c.Guid.Equals (ImageFormat.Wmf.Guid))
129                                         prop = "Wmf";
130                                 
131                                 if (prop != null){
132                                         return new InstanceDescriptor (typeof (ImageFormat).GetProperty (prop), null);
133                                 } else {
134                                         ConstructorInfo ctor = typeof(ImageFormat).GetConstructor (new Type[] {typeof(Guid)} );
135                                         return new InstanceDescriptor (ctor, new object[] {c.Guid});
136                                 }
137                         }
138                         
139                         return base.ConvertTo (context, culture, val, destType);
140                 }
141
142                 [MonoTODO ("Implement")]
143                 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context )
144                 {
145                         throw new NotImplementedException (); 
146                 }
147
148                 public override bool GetStandardValuesSupported (ITypeDescriptorContext context )
149                 {
150                         return false;
151                 }
152         }
153 }