~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/libstetic/editor/SelectImageDialog.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Collections;
 
4
using System.IO;
 
5
using Gtk;
 
6
 
 
7
namespace Stetic.Editor
 
8
{
 
9
        public class SelectImageDialog: IDisposable
 
10
        {
 
11
                [Glade.Widget] Gtk.TreeView resourceList;
 
12
                [Glade.Widget] Gtk.FileChooserWidget fileChooser;
 
13
                [Glade.Widget] Gtk.Entry iconNameEntry;
 
14
                [Glade.Widget] Gtk.Notebook notebook;
 
15
                [Glade.Widget] Gtk.ScrolledWindow iconScrolledwindow;
 
16
                [Glade.Widget] Gtk.Image previewIcon;
 
17
                [Glade.Widget] Gtk.Image previewResource;
 
18
                [Glade.Widget] Gtk.ComboBox iconSizeCombo;
 
19
                [Glade.Widget] Gtk.Entry resourceNameEntry;
 
20
                [Glade.Widget] Gtk.Button okButton;
 
21
                [Glade.Widget] Gtk.Button buttonAdd;
 
22
                [Glade.Widget] Gtk.Button buttonRemove;
 
23
                [Glade.Widget ("SelectImageDialog")] Gtk.Dialog dialog;
 
24
                
 
25
                ThemedIconList iconList;
 
26
                
 
27
                Gtk.ListStore resourceListStore;
 
28
                Gtk.Window parent;
 
29
                
 
30
                int thumbnailSize = 30;
 
31
                Hashtable resources = new Hashtable (); // Stores resourceName -> thumbnail pixbuf
 
32
                Gdk.Pixbuf missingThumbnail;
 
33
                IResourceProvider resourceProvider;
 
34
                string importedImageFile;
 
35
                Stetic.IProject project;
 
36
                
 
37
                public SelectImageDialog (Gtk.Window parent, Stetic.IProject project)
 
38
                {
 
39
                        this.parent = parent;
 
40
                        this.project = project;
 
41
                        Glade.XML xml = new Glade.XML (null, "stetic.glade", "SelectImageDialog", null);
 
42
                        xml.Autoconnect (this);
 
43
                        
 
44
                        // Stock icon list
 
45
                        
 
46
                        iconList = new ThemedIconList ();
 
47
                        iconList.SelectionChanged += new EventHandler (OnIconSelectionChanged);
 
48
                        iconScrolledwindow.AddWithViewport (iconList);
 
49
                        
 
50
                        // Icon Sizes
 
51
                        
 
52
                        foreach (IconSize s in Enum.GetValues (typeof(Gtk.IconSize))) {
 
53
                                if (s != IconSize.Invalid)
 
54
                                        iconSizeCombo.AppendText (s.ToString ());
 
55
                        }
 
56
                        iconSizeCombo.Active = 0;
 
57
                        
 
58
                        // Resource list
 
59
                        
 
60
                        resourceListStore = new Gtk.ListStore (typeof(Gdk.Pixbuf), typeof(string), typeof(string));
 
61
                        resourceList.Model = resourceListStore;
 
62
                        
 
63
                        Gtk.TreeViewColumn col = new Gtk.TreeViewColumn ();
 
64
                        
 
65
                        Gtk.CellRendererPixbuf pr = new Gtk.CellRendererPixbuf ();
 
66
                        pr.Xpad = 3;
 
67
                        col.PackStart (pr, false);
 
68
                        col.AddAttribute (pr, "pixbuf", 0);
 
69
                        
 
70
                        Gtk.CellRendererText crt = new Gtk.CellRendererText ();
 
71
                        col.PackStart (crt, true);
 
72
                        col.AddAttribute (crt, "markup", 1);
 
73
                        
 
74
                        resourceList.AppendColumn (col);
 
75
                        resourceProvider = project.ResourceProvider;
 
76
                        if (resourceProvider == null) {
 
77
                                buttonAdd.Sensitive = false;
 
78
                                buttonRemove.Sensitive = false;
 
79
                        }
 
80
                        FillResources ();
 
81
                        resourceList.Selection.Changed += OnResourceSelectionChanged;
 
82
                        
 
83
                        if (project.FileName != null)
 
84
                                fileChooser.SetCurrentFolder (project.ImagesRootPath);
 
85
 
 
86
                        fileChooser.SelectionChanged += delegate (object s, EventArgs a) {
 
87
                                UpdateButtons ();
 
88
                        };
 
89
 
 
90
                        fileChooser.FileActivated += delegate (object s, EventArgs a) {
 
91
                                if (Icon != null) {
 
92
                                        if (Validate ())
 
93
                                                dialog.Respond (Gtk.ResponseType.Ok);
 
94
                                }
 
95
                        };
 
96
                        
 
97
                        okButton.Clicked += OnOkClicked;
 
98
 
 
99
                        UpdateButtons ();
 
100
                }
 
101
                
 
102
                public int Run ()
 
103
                {
 
104
                        dialog.ShowAll ();
 
105
                        dialog.TransientFor = parent;
 
106
                        return dialog.Run ();
 
107
                }
 
108
                
 
109
                public void Dispose ()
 
110
                {
 
111
                        dialog.Destroy ();
 
112
                }
 
113
                
 
114
                public ImageInfo Icon {
 
115
                        get {
 
116
                                if (notebook.Page == 0) {
 
117
                                        if (iconNameEntry.Text.Length == 0)
 
118
                                                return null;
 
119
                                        return ImageInfo.FromTheme (iconNameEntry.Text, SelectedIconSize);
 
120
                                } else if (notebook.Page == 1) {
 
121
                                        if (resourceNameEntry.Text.Length == 0)
 
122
                                                return null;
 
123
                                        return ImageInfo.FromResource (resourceNameEntry.Text);
 
124
                                } else {
 
125
                                        if (importedImageFile != null)
 
126
                                                return ImageInfo.FromFile (importedImageFile);
 
127
                                        if (fileChooser.Filename == null || fileChooser.Filename.Length == 0 || !File.Exists (fileChooser.Filename))
 
128
                                                return null;
 
129
                                        return ImageInfo.FromFile (fileChooser.Filename);
 
130
                                }
 
131
                        }
 
132
                        set {
 
133
                                if (value == null)
 
134
                                        return;
 
135
                                if (value.Source == ImageSource.Theme) {
 
136
                                        iconNameEntry.Text = value.Name;
 
137
                                        SelectedIconSize = value.ThemeIconSize;
 
138
                                        notebook.Page = 0;
 
139
                                } else if (value.Source == ImageSource.Resource) {
 
140
                                        notebook.Page = 1;
 
141
                                        resourceNameEntry.Text = value.Name;
 
142
                                } else {
 
143
                                        fileChooser.SetFilename (value.Name);
 
144
                                        notebook.Page = 2;
 
145
                                }
 
146
                        }
 
147
                }
 
148
                
 
149
                Gtk.IconSize SelectedIconSize {
 
150
                        get { return (IconSize) iconSizeCombo.Active + 1; }
 
151
                        set { iconSizeCombo.Active = ((int) value) - 1; }
 
152
                }
 
153
                
 
154
                void UpdateButtons ()
 
155
                {
 
156
                        okButton.Sensitive = Icon != null;
 
157
                }
 
158
                
 
159
                protected void OnCurrentPageChanged (object s, Gtk.SwitchPageArgs args)
 
160
                {
 
161
                        UpdateButtons ();
 
162
                }
 
163
                
 
164
                void OnIconSelectionChanged (object s, EventArgs args)
 
165
                {
 
166
                        if (iconList.Selection != null) {
 
167
                                iconNameEntry.Text = iconList.Selection;
 
168
                        }
 
169
                }
 
170
                
 
171
                void UpdateIconSelection ()
 
172
                {
 
173
                        Gdk.Pixbuf icon = null;
 
174
                        if (iconNameEntry.Text.Length > 0) {
 
175
                                icon = WidgetUtils.LoadIcon (iconNameEntry.Text, SelectedIconSize);
 
176
                        }
 
177
                        if (icon == null)
 
178
                                icon = WidgetUtils.MissingIcon;
 
179
                        previewIcon.Pixbuf = icon;
 
180
                }
 
181
                
 
182
                protected void OnIconSizeChanged (object ob, EventArgs args)
 
183
                {
 
184
                        UpdateIconSelection ();
 
185
                }
 
186
                
 
187
                protected void OnIconNameChanged (object ob, EventArgs args)
 
188
                {
 
189
                        UpdateIconSelection ();
 
190
                        UpdateButtons ();
 
191
                }
 
192
                
 
193
                void FillResources ()
 
194
                {
 
195
                        resourceListStore.Clear ();
 
196
                        resources.Clear ();
 
197
                        if (resourceProvider != null) {
 
198
                                foreach (ResourceInfo res in resourceProvider.GetResources ()) {
 
199
                                        if (res.MimeType.StartsWith ("image/")) {
 
200
                                                AppendResource (resourceProvider.GetResourceStream (res.Name), res.Name);
 
201
                                        }
 
202
                                }
 
203
                        }
 
204
                }
 
205
                
 
206
                void AppendResource (Stream stream, string name)
 
207
                {
 
208
                        try {
 
209
                                Gdk.Pixbuf pix = new Gdk.Pixbuf (stream);
 
210
                                stream.Close ();
 
211
                                string txt = name + "\n<span foreground='darkgrey' size='x-small'>" + pix.Width + " x " + pix.Height + "</span>";
 
212
                                pix = GetThumbnail (pix);
 
213
                                resourceListStore.AppendValues (pix, txt, name);
 
214
                                resources [name] = pix;
 
215
                        } catch {
 
216
                                // Doesn't look like a valid image. Just ignore it.
 
217
                        }
 
218
                }
 
219
                
 
220
                Gdk.Pixbuf GetThumbnail (Gdk.Pixbuf pix)
 
221
                {
 
222
                        if (pix.Width > pix.Height) {
 
223
                                if (pix.Width > thumbnailSize) {
 
224
                                        float prop = (float) pix.Height / (float) pix.Width;
 
225
                                        return pix.ScaleSimple (thumbnailSize, (int)(thumbnailSize * prop), Gdk.InterpType.Bilinear);
 
226
                                }
 
227
                        } else {
 
228
                                if (pix.Height > thumbnailSize) {
 
229
                                        float prop = (float) pix.Width / (float) pix.Height;
 
230
                                        return pix.ScaleSimple ((int)(thumbnailSize * prop), thumbnailSize, Gdk.InterpType.Bilinear);
 
231
                                }
 
232
                        }
 
233
                        return pix;
 
234
                }
 
235
                
 
236
                void OnResourceSelectionChanged (object obj, EventArgs args)
 
237
                {
 
238
                        Gtk.TreeIter iter;
 
239
                        Gtk.TreeModel model;
 
240
                        if (!resourceList.Selection.GetSelected (out model, out iter)) {
 
241
                                resourceNameEntry.Text = "";
 
242
                        } else {
 
243
                                resourceNameEntry.Text = (string) resourceListStore.GetValue (iter, 2);
 
244
                        }
 
245
                }
 
246
                
 
247
                protected void OnResourceNameChanged (object ob, EventArgs args)
 
248
                {
 
249
                        Gdk.Pixbuf pix = (Gdk.Pixbuf) resources [resourceNameEntry.Text];
 
250
                        if (pix != null)
 
251
                                previewResource.Pixbuf = pix;
 
252
                        else {
 
253
                                if (missingThumbnail == null)
 
254
                                        missingThumbnail = WidgetUtils.MissingIcon;
 
255
                                previewResource.Pixbuf = missingThumbnail;
 
256
                        }
 
257
                        UpdateButtons ();
 
258
                }
 
259
                
 
260
                protected void OnAddResource (object ob, EventArgs args)
 
261
                {
 
262
                        FileChooserDialog dialog =
 
263
                                new FileChooserDialog ("Open File", null, FileChooserAction.Open,
 
264
                                                       Gtk.Stock.Cancel, Gtk.ResponseType.Cancel,
 
265
                                                       Gtk.Stock.Open, Gtk.ResponseType.Ok);
 
266
                        int response = dialog.Run ();
 
267
                        if (response == (int)Gtk.ResponseType.Ok) {
 
268
                                ResourceInfo rinfo = resourceProvider.AddResource (dialog.Filename);
 
269
                                AppendResource (resourceProvider.GetResourceStream (rinfo.Name), rinfo.Name);
 
270
                                resourceNameEntry.Text = rinfo.Name;
 
271
                        }
 
272
                        dialog.Destroy ();
 
273
                }
 
274
                
 
275
                protected void OnRemoveResource (object ob, EventArgs args)
 
276
                {
 
277
                        Gtk.TreeIter iter;
 
278
                        Gtk.TreeModel model;
 
279
                        if (resourceList.Selection.GetSelected (out model, out iter)) {
 
280
                                string res = (string) resourceListStore.GetValue (iter, 2);
 
281
                                Gtk.MessageDialog msg = new Gtk.MessageDialog (dialog, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, "Are you sure you want to delete the resource '{0}'?", res);
 
282
                                if (msg.Run () == (int) ResponseType.Yes) {
 
283
                                        resourceProvider.RemoveResource (res);
 
284
                                        resourceListStore.Remove (ref iter);
 
285
                                }
 
286
                                msg.Destroy ();
 
287
                        }
 
288
                }
 
289
                
 
290
                bool Validate ()
 
291
                {
 
292
                        if (notebook.Page == 2) {
 
293
                                if (fileChooser.Filename == null || fileChooser.Filename.Length == 0 || !File.Exists (fileChooser.Filename))
 
294
                                        return true;
 
295
                                
 
296
                                importedImageFile = project.ImportFile (fileChooser.Filename);
 
297
                                if (importedImageFile != null)
 
298
                                        importedImageFile = WidgetUtils.AbsoluteToRelativePath (project.ImagesRootPath, importedImageFile);
 
299
                                return importedImageFile != null;
 
300
                        }
 
301
                        return true;
 
302
                }
 
303
                
 
304
                void OnOkClicked (object s, EventArgs args)
 
305
                {
 
306
                        if (Validate ())
 
307
                                dialog.Respond (Gtk.ResponseType.Ok);
 
308
                }
 
309
        }
 
310
}