[System.Drawing] NS1.6 compatibility: Use GetTypeInfo () where required (#4861)
[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 sourceType)
53                 {
54                         if (sourceType == typeof (string))
55                                 return true;
56                                 
57                         return base.CanConvertFrom (context, sourceType);
58                 }
59
60                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
61                 {
62                         if (destinationType == typeof (string))
63                                 return true;
64                                 
65                         if (destinationType == typeof (InstanceDescriptor))
66                                 return true;
67
68                         return base.CanConvertTo (context, destinationType);
69                 }
70                 
71                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
72                 {
73                         // we must be able to convert from short names and long names
74                         string strFormat = (value as string);
75                         if (strFormat == null) {
76                                 // case #1, this is not a string
77                                 return base.ConvertFrom (context, culture, value);
78                         } else if (strFormat [0] == '[') {
79                                 // case #2, this is probably a long format (guid)
80                                 if (strFormat.Equals (ImageFormat.Bmp.ToString ()))
81                                         return ImageFormat.Bmp;
82                                 else if (strFormat.Equals (ImageFormat.Emf.ToString ()))
83                                         return ImageFormat.Emf;
84                                 else if (strFormat.Equals (ImageFormat.Exif.ToString ()))
85                                         return ImageFormat.Exif;
86                                 else if (strFormat.Equals (ImageFormat.Gif.ToString ()))
87                                         return ImageFormat.Gif;
88                                 else if (strFormat.Equals (ImageFormat.Icon.ToString ()))
89                                         return ImageFormat.Icon;
90                                 else if (strFormat.Equals (ImageFormat.Jpeg.ToString ()))
91                                         return ImageFormat.Jpeg;
92                                 else if (strFormat.Equals (ImageFormat.MemoryBmp.ToString ()))
93                                         return ImageFormat.MemoryBmp;
94                                 else if (strFormat.Equals (ImageFormat.Png.ToString ()))
95                                         return ImageFormat.Png;
96                                 else if (strFormat.Equals (ImageFormat.Tiff.ToString ()))
97                                         return ImageFormat.Tiff;
98                                 else if (strFormat.Equals (ImageFormat.Wmf.ToString ()))
99                                         return ImageFormat.Wmf;
100                         } else {
101                                 // case #3, this is probably a short format
102                                 if (String.Compare (strFormat, "Bmp", true, CultureInfo.InvariantCulture) == 0)
103                                         return ImageFormat.Bmp;
104                                 else if (String.Compare (strFormat, "Emf", true, CultureInfo.InvariantCulture) == 0)
105                                         return ImageFormat.Emf;
106                                 else if (String.Compare (strFormat, "Exif", true, CultureInfo.InvariantCulture) == 0)
107                                         return ImageFormat.Exif;
108                                 else if (String.Compare (strFormat, "Gif", true, CultureInfo.InvariantCulture) == 0)
109                                         return ImageFormat.Gif;
110                                 else if (String.Compare (strFormat, "Icon", true, CultureInfo.InvariantCulture) == 0)
111                                         return ImageFormat.Icon;
112                                 else if (String.Compare (strFormat, "Jpeg", true, CultureInfo.InvariantCulture) == 0)
113                                         return ImageFormat.Jpeg;
114                                 else if (String.Compare (strFormat, "MemoryBmp", true, CultureInfo.InvariantCulture) == 0)
115                                         return ImageFormat.MemoryBmp;
116                                 else if (String.Compare (strFormat, "Png", true, CultureInfo.InvariantCulture) == 0)
117                                         return ImageFormat.Png;
118                                 else if (String.Compare (strFormat, "Tiff", true, CultureInfo.InvariantCulture) == 0)
119                                         return ImageFormat.Tiff;
120                                 else if (String.Compare (strFormat, "Wmf", true, CultureInfo.InvariantCulture) == 0)
121                                         return ImageFormat.Wmf;
122                         }
123                         // last case, this is an unknown string
124                         return base.ConvertFrom (context, culture, value);
125                 }
126
127                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType )
128                 {
129                         if (value is ImageFormat) {
130                                 ImageFormat c = (ImageFormat) value;
131                                 string prop = null;
132                                 if (c.Guid.Equals (ImageFormat.Bmp.Guid))
133                                         prop = "Bmp";
134                                 else if (c.Guid.Equals (ImageFormat.Emf.Guid))
135                                         prop = "Emf";
136                                 else if (c.Guid.Equals (ImageFormat.Exif.Guid))
137                                         prop = "Exif";
138                                 else if (c.Guid.Equals (ImageFormat.Gif.Guid))
139                                         prop = "Gif";
140                                 else if (c.Guid.Equals (ImageFormat.Icon.Guid))
141                                         prop = "Icon";
142                                 else if (c.Guid.Equals (ImageFormat.Jpeg.Guid))
143                                         prop = "Jpeg";
144                                 else if (c.Guid.Equals (ImageFormat.MemoryBmp.Guid))
145                                         prop = "MemoryBmp";
146                                 else if (c.Guid.Equals (ImageFormat.Png.Guid))
147                                         prop = "Png";
148                                 else if (c.Guid.Equals (ImageFormat.Tiff.Guid))
149                                         prop = "Tiff";
150                                 else if (c.Guid.Equals (ImageFormat.Wmf.Guid))
151                                         prop = "Wmf";
152
153                                 if (destinationType == typeof (string)) {
154                                         return prop != null ? prop : c.ToString ();
155                                 } else if (destinationType == typeof (InstanceDescriptor)) {
156                                         if (prop != null){
157                                                 return new InstanceDescriptor (typeof (ImageFormat).GetTypeInfo ().GetProperty (prop), null);
158                                         } else {
159                                                 ConstructorInfo ctor = typeof(ImageFormat).GetTypeInfo ().GetConstructor (new Type[] {typeof(Guid)} );
160                                                 return new InstanceDescriptor (ctor, new object[] {c.Guid});
161                                         }
162                                 }
163                         }
164                         
165                         return base.ConvertTo (context, culture, value, destinationType);
166                 }
167
168                 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
169                 {
170                         ImageFormat[] list = new ImageFormat [10];
171                         list [0] = ImageFormat.MemoryBmp;
172                         list [1] = ImageFormat.Bmp;
173                         list [2] = ImageFormat.Emf;
174                         list [3] = ImageFormat.Wmf;
175                         list [4] = ImageFormat.Gif;
176                         list [5] = ImageFormat.Jpeg;
177                         list [6] = ImageFormat.Png;
178                         list [7] = ImageFormat.Tiff;
179                         list [8] = ImageFormat.Exif;
180                         list [9] = ImageFormat.Icon;
181                         return new TypeConverter.StandardValuesCollection (list);
182                 }
183
184                 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
185                 {
186                         return true;
187                 }
188         }
189 }