checkin for Andreas Nahr <ClassDevelopment@A-SoftTech.com>, see ChangeLog for details
[mono.git] / mcs / class / System.Drawing.Design / System.Drawing.Design / ImageEditor.cs
1 //
2 // System.Drawing.Design.ImageEditor.cs
3 // 
4 // Authors:
5 //  Martin Willemoes Hansen (mwh@sysrq.dk)
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // 
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2003 Andreas Nahr
10 // 
11 using System;
12 using System.IO;
13 using System.Drawing;
14 using System.ComponentModel;
15 using System.Windows.Forms;
16
17 namespace System.Drawing.Design
18 {
19         public class ImageEditor : UITypeEditor
20         {
21
22                 private OpenFileDialog openDialog;
23
24                 public ImageEditor()
25                 {
26                 }
27
28                 public override object EditValue (ITypeDescriptorContext context,
29                         IServiceProvider provider, object value)
30                 {
31                         openDialog = new OpenFileDialog();
32                         // FIXME: Add multilanguage support
33                         openDialog.Title = "Open image file";
34                         openDialog.CheckFileExists = true;
35                         openDialog.CheckPathExists = true;
36                         openDialog.Filter = CreateFilterEntry (this);
37                         openDialog.Multiselect = false;
38
39                         // Show the dialog
40                         DialogResult result = openDialog.ShowDialog();
41
42                         // Check the result and create a new image from the file
43                         if (result == DialogResult.OK)
44                         {
45                                 return LoadFromStream (openDialog.OpenFile());
46                         }
47                         else
48                                 return value;   
49                 }
50
51                 public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
52                 {
53                         return UITypeEditorEditStyle.Modal;
54                 }
55
56                 public override bool GetPaintValueSupported (ITypeDescriptorContext context)
57                 {
58                         return true;
59                 }
60
61                 public override void PaintValue (PaintValueEventArgs e)
62                 {
63                         Graphics G = e.Graphics;
64                         if (e.Value != null)
65                         {
66                                 Image I = (Image) e.Value;
67                                 G.DrawImage (I, e.Bounds);
68                         }
69                         G.DrawRectangle (Pens.Black, e.Bounds);
70                 }
71
72                 protected static string CreateExtensionsString (string[] extensions, string sep)
73                 {
74                         if (extensions.Length > 0)
75                         {
76                                 string Ext = extensions[0];
77                                 for (int x = 1; x < extensions.Length - 1; x++)
78                                         Ext = string.Concat(Ext, sep, extensions[x]);
79                                 return Ext;
80                         }
81                         else
82                         {
83                                 return string.Empty;
84                         }
85                 }
86
87                 protected static string CreateFilterEntry (ImageEditor e)
88                 {
89                         string ExtStr = CreateExtensionsString (e.GetExtensions(), ";");
90                         string Desc = e.GetFileDialogDescription() + " (" + ExtStr + ")";
91                         return String.Concat (Desc, "|", ExtStr);
92                 }
93
94                 protected virtual string[] GetExtensions()
95                 {
96                         return new string[] {"*.bmp", "*.gif", "*.jpg", "*.jpeg", "*.png", "*.ico", "*.emf", "*.wmf"};
97                 }
98
99                 protected virtual string GetFileDialogDescription()
100                 {
101                         // FIXME: Add multilanguage support
102                         return "All image files";
103                 }
104
105                 protected virtual Image LoadFromStream (Stream stream)
106                 {
107                         return new Bitmap (stream);
108                 }
109         }
110 }