2004-02-24 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / System.Drawing.Design / System.Drawing.Design / IconEditor.cs
1 //
2 // System.Drawing.Design.IconEditor.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
12 using System;
13 using System.Text;
14 using System.IO;
15 using System.Drawing;
16 using System.ComponentModel;
17 using System.Windows.Forms;
18
19 namespace System.Drawing.Design
20 {
21         // LAMESPEC: Why isn't this class inherited from ImageEditor?
22         public class IconEditor : UITypeEditor
23         {
24
25                 private OpenFileDialog openDialog;
26
27                 public IconEditor()
28                 {
29                 }
30
31                 protected static string CreateExtensionsString (string[] extensions, string sep)
32                 {
33                         if (extensions.Length > 0) {
34                                 StringBuilder sb = new StringBuilder();
35
36                                 sb.Append (extensions[0]);
37                                 for (int x = 1; x < extensions.Length - 1; x++) {
38                                         sb.Append (sep);
39                                         sb.Append (extensions[x]);
40                                 }
41                                 return sb.ToString();
42                         }
43                         else {
44                                 return string.Empty;
45                         }
46                 }
47
48                 protected static string CreateFilterEntry (IconEditor e)
49                 {
50                         StringBuilder sb = new StringBuilder();
51                         string ExtStr = CreateExtensionsString (e.GetExtensions(), ";");
52
53                         sb.Append (e.GetFileDialogDescription());
54                         sb.Append (" (" + ExtStr + ")" + "|");
55                         sb.Append (ExtStr);
56                         return sb.ToString();
57                 }
58
59                 public override object EditValue (ITypeDescriptorContext context,
60                         IServiceProvider provider, object value)
61                 {
62                         openDialog = new OpenFileDialog();
63                         openDialog.Title = Locale.GetText ("Open image file");
64                         openDialog.CheckFileExists = true;
65                         openDialog.CheckPathExists = true;
66                         openDialog.Filter = CreateFilterEntry (this);
67                         openDialog.Multiselect = false;
68
69                         // Show the dialog
70                         DialogResult result = openDialog.ShowDialog();
71
72                         // Check the result and create a new image from the file
73                         if (result == DialogResult.OK)
74                         {
75                                 return LoadFromStream (openDialog.OpenFile());
76                         }
77                         else
78                                 return value;
79                 }
80
81                 public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
82                 {
83                         return UITypeEditorEditStyle.Modal;
84                 }
85
86                 protected virtual string[] GetExtensions()
87                 {
88                         return new string[] {"*.ico"};
89                 }
90
91                 protected virtual string GetFileDialogDescription()
92                 {
93                         return Locale.GetText ("Icon files");
94                 }
95
96                 public override bool GetPaintValueSupported (ITypeDescriptorContext context)
97                 {
98                         return true;
99                 }
100
101                 protected virtual Icon LoadFromStream (Stream stream)
102                 {
103                         return new Icon (stream);
104                 }
105
106                 public override void PaintValue (PaintValueEventArgs e)
107                 {
108                         Graphics G = e.Graphics;
109
110                         if (e.Value != null) {
111                                 Image I = (Image) e.Value;
112                                 G.DrawImage (I, e.Bounds);
113                         }
114
115                         G.DrawRectangle (Pens.Black, e.Bounds);
116                 }
117         }
118 }