Merge pull request #1156 from felfert/master
[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 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Text;
33 using System.IO;
34 using System.ComponentModel;
35 using System.Windows.Forms;
36
37 namespace System.Drawing.Design
38 {
39         public class ImageEditor : UITypeEditor
40         {
41
42                 private OpenFileDialog openDialog;
43
44                 public ImageEditor()
45                 {
46                 }
47
48                 public override object EditValue (ITypeDescriptorContext context,
49                         IServiceProvider provider, object value)
50                 {
51                         openDialog = new OpenFileDialog();
52                         openDialog.Title = Locale.GetText ("Open image file");
53                         openDialog.CheckFileExists = true;
54                         openDialog.CheckPathExists = true;
55                         openDialog.Filter = CreateFilterEntry (this);
56                         openDialog.Multiselect = false;
57
58                         // Show the dialog
59                         DialogResult result = openDialog.ShowDialog();
60
61                         // Check the result and create a new image from the file
62                         if (result == DialogResult.OK) {
63                                 return LoadFromStream (openDialog.OpenFile());
64                         }
65                         else
66                                 return value;   
67                 }
68
69                 public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
70                 {
71                         return UITypeEditorEditStyle.Modal;
72                 }
73
74                 public override bool GetPaintValueSupported (ITypeDescriptorContext context)
75                 {
76                         return true;
77                 }
78
79                 public override void PaintValue (PaintValueEventArgs e)
80                 {
81                         Graphics G = e.Graphics;
82
83                         if (e.Value != null) {
84                                 Image I = (Image) e.Value;
85                                 G.DrawImage (I, e.Bounds);
86                         }
87
88                         G.DrawRectangle (Pens.Black, e.Bounds);
89                 }
90
91                 protected static string CreateExtensionsString (string[] extensions, string sep)
92                 {
93                         if (extensions.Length > 0) {
94                                 StringBuilder sb = new StringBuilder();
95
96                                 sb.Append (extensions[0]);
97                                 for (int x = 1; x < extensions.Length - 1; x++) {
98                                         sb.Append (sep);
99                                         sb.Append (extensions[x]);
100                                 }
101                                 return sb.ToString();
102                         }
103                         else {
104                                 return string.Empty;
105                         }
106                 }
107
108                 protected static string CreateFilterEntry (ImageEditor e)
109                 {
110                         StringBuilder sb = new StringBuilder();
111                         string ExtStr = CreateExtensionsString (e.GetExtensions(), ";");
112
113                         sb.Append (e.GetFileDialogDescription());
114                         sb.Append (" (" + ExtStr + ")" + "|");
115                         sb.Append (ExtStr);
116                         return sb.ToString();
117                 }
118
119                 protected virtual string[] GetExtensions()
120                 {
121                         return new string[] {"*.bmp", "*.gif", "*.jpg", "*.jpeg", "*.png", "*.ico", "*.emf", "*.wmf"};
122                 }
123
124                 protected virtual string GetFileDialogDescription()
125                 {
126                         return Locale.GetText ("All image files");
127                 }
128
129                 protected virtual Image LoadFromStream (Stream stream)
130                 {
131                         return new Bitmap (stream);
132                 }
133                 [MonoTODO]
134                 protected virtual Type[] GetImageExtenders ()
135                 {
136                         throw new NotImplementedException ();
137                 }
138         }
139 }