Add this for backwards compatibility
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontConverter.cs
1 //
2 // System.Drawing.FontConverter.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Ravindra (rkumar@novell.com)
8 //
9 // Copyright (C) 2002,2003 Ximian, Inc.  http://www.ximian.com
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Text;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
38 using System.Drawing.Text;
39 using System.ComponentModel.Design.Serialization;\r
40 using System.Reflection;\r
41
42 namespace System.Drawing
43 {
44         public class FontConverter : TypeConverter
45         {
46                 public FontConverter ()
47                 {
48                 }
49
50                 ~FontConverter ()
51                 {
52                                 
53                 }       
54
55                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
56                 {
57                         if (sourceType == typeof (string))
58                                 return true;
59
60                         return base.CanConvertFrom (context, sourceType);
61                 }
62
63                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
64                 {
65                         if (destinationType == typeof (String))
66                                 return true;
67
68                         if (destinationType == typeof (InstanceDescriptor))
69                                 return true;
70
71                         return base.CanConvertTo (context, destinationType);
72                 }
73
74                 public override object ConvertTo (ITypeDescriptorContext context,
75                         CultureInfo culture,
76                         object value,
77                         Type destinationType)
78                 {
79                         if ((destinationType == typeof (string)) && (value is Font)) {
80                                 Font font = (Font) value;
81                                 StringBuilder sb = new StringBuilder ();
82                                 sb.Append (font.Name).Append (", ");
83                                 sb.Append (font.Size);
84
85                                 switch (font.Unit) {
86                                         // MS throws ArgumentException, if unit is set 
87                                         // to GraphicsUnit.Display
88                                         // Don't know what to append for GraphicsUnit.Display
89                                 case GraphicsUnit.Display:
90                                         sb.Append ("display"); break;
91
92                                 case GraphicsUnit.Document:
93                                         sb.Append ("doc"); break;
94
95                                 case GraphicsUnit.Point:
96                                         sb.Append ("pt"); break;
97
98                                 case GraphicsUnit.Inch:
99                                         sb.Append ("in"); break;
100
101                                 case GraphicsUnit.Millimeter:
102                                         sb.Append ("mm"); break;
103
104                                 case GraphicsUnit.Pixel:
105                                         sb.Append ("px"); break;
106
107                                 case GraphicsUnit.World:
108                                         sb.Append ("world"); break;
109                                 }
110
111                                 if (font.Style != FontStyle.Regular)
112                                         sb.Append (", style=").Append (font.Style);
113
114                                 return sb.ToString ();
115                         }
116
117                         if ((destinationType == typeof (InstanceDescriptor)) && (value is Font)) {
118                                 Font font = (Font) value;
119                                 ConstructorInfo met = typeof(Font).GetConstructor (new Type[] {typeof(string), typeof(float), typeof(FontStyle), typeof(GraphicsUnit)});\r
120                                 object[] args = new object[4];
121                                 args [0] = font.Name;
122                                 args [1] = font.Size;
123                                 args [2] = font.Style;\r
124                                 args [3] = font.Unit;
125                                 return new InstanceDescriptor (met, args);
126                         }
127                         
128                         return base.ConvertTo (context, culture, value, destinationType);
129                 }
130
131                 public override object ConvertFrom (ITypeDescriptorContext context,
132                         CultureInfo culture,
133                         object value)
134                 {
135                         string fontFamily = value as string;
136                         if (fontFamily == null)
137                                 return base.ConvertFrom (context, culture, value);
138
139                         // MS creates a font from the given family with
140                         // emSize = 8.
141                         return new Font (fontFamily, 8);
142                 }
143
144                 public override object CreateInstance (ITypeDescriptorContext context,
145                         IDictionary propertyValues)
146                 {
147                         Object value;
148                         byte charSet = 1;
149                         float size = 8;
150                         String name = null;
151                         bool vertical = false;
152                         FontStyle style = FontStyle.Regular;
153                         FontFamily fontFamily = null;
154                         GraphicsUnit unit = GraphicsUnit.Point;
155
156                         if ((value = propertyValues ["GdiCharSet"]) != null)
157                                 charSet = (byte) value;
158
159                         if ((value = propertyValues ["Size"]) != null)
160                                 size = (float) value;
161
162                         if ((value = propertyValues ["Unit"]) != null)
163                                 unit = (GraphicsUnit) value;
164
165                         if ((value = propertyValues ["Name"]) != null)
166                                 name = (String) value;
167
168                         if ((value = propertyValues ["GdiVerticalFont"]) != null)
169                                 vertical = (bool) value;
170
171                         if ((value = propertyValues ["Bold"]) != null) {
172                                 bool bold = (bool) value;
173                                 if (bold == true)
174                                         style |= FontStyle.Bold;
175                         }
176
177                         if ((value = propertyValues ["Italic"]) != null) {
178                                 bool italic = (bool) value;
179                                 if (italic == true)
180                                         style |= FontStyle.Italic;
181                         }
182
183                         if ((value = propertyValues ["Strikeout"]) != null) {
184                                 bool strike = (bool) value;
185                                 if (strike == true)
186                                         style |= FontStyle.Strikeout;
187                         }
188
189                         if ((value = propertyValues ["Underline"]) != null) {
190                                 bool underline = (bool) value;
191                                 if (underline == true)
192                                         style |= FontStyle.Underline;
193                         }
194
195                         /* ?? Should default font be culture dependent ?? */
196                         if (name == null)
197                                 fontFamily = new FontFamily ("Tahoma");
198                         else {
199                                 name = name.ToLower ();
200                                 FontCollection collection = new InstalledFontCollection ();
201                                 FontFamily [] installedFontList = collection.Families;
202                                 foreach (FontFamily font in installedFontList) {
203                                         if (name == font.Name.ToLower ()) {
204                                                 fontFamily = font;
205                                                 break;
206                                         }
207                                 }
208
209                                 // font family not found in installed fonts
210                                 if (fontFamily == null) {
211                                         collection = new PrivateFontCollection ();
212                                         FontFamily [] privateFontList = collection.Families;
213                                         foreach (FontFamily font in privateFontList) {
214                                                 if (name == font.Name.ToLower ()) {
215                                                         fontFamily = font;
216                                                         break;
217                                                 }
218                                         }
219                                 }
220
221                                 // font family not found in private fonts also
222                                 if (fontFamily == null)
223                                         fontFamily = FontFamily.GenericSansSerif;
224                         }
225
226                         return new Font (fontFamily, size, style, unit, charSet, vertical);
227                 }
228
229                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
230                 {
231                         return true;
232                 }
233
234                 public override PropertyDescriptorCollection GetProperties
235                         (ITypeDescriptorContext context,
236                         object value, Attribute [] attributes)
237                 {
238                         if (value is Font)
239                                 return TypeDescriptor.GetProperties (value, attributes);
240
241                         return base.GetProperties (context, value, attributes);
242                 }
243
244                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
245                 {
246                         return true;
247                 }
248
249                 public sealed class FontNameConverter : TypeConverter
250 #if NET_2_0
251                 , IDisposable           
252 #endif
253                 {
254                         public FontNameConverter ()
255                         {
256                         }       
257 #if NET_2_0                     
258                         void IDisposable.Dispose ()\r
259                         {
260
261                         }\r
262 #endif
263                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
264                         {
265                                 if (sourceType == typeof (string))
266                                         return true;
267
268                                 return base.CanConvertFrom (context, sourceType);
269                         }
270
271                         [MonoTODO]
272                         public override object ConvertFrom (ITypeDescriptorContext context,
273                                 CultureInfo culture,
274                                 object value)
275                         {
276                                 throw new NotImplementedException ();
277                         }
278
279                         [MonoTODO]
280                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
281                         {
282                                 throw new NotImplementedException ();
283                         }
284
285                         [MonoTODO]
286                         public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
287                         {
288                                 throw new NotImplementedException ();
289                         }
290
291                         public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
292                         {
293                                 return true;
294                         }
295                 }
296 \r
297                 public class FontUnitConverter : EnumConverter\r
298                 {\r
299                         public FontUnitConverter () : base (typeof (GraphicsUnit))\r
300                         {\r
301                         }\r
302
303                         [MonoTODO]\r
304                         public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)\r
305                         {\r
306                                 throw new NotImplementedException ();
307                         }\r
308                 }\r
309         }\r
310 }\r