3
using System.Collections;
7
namespace Stetic.Editor
9
public class SelectImageDialog: IDisposable
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;
25
ThemedIconList iconList;
27
Gtk.ListStore resourceListStore;
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;
37
public SelectImageDialog (Gtk.Window parent, Stetic.IProject project)
40
this.project = project;
41
Glade.XML xml = new Glade.XML (null, "stetic.glade", "SelectImageDialog", null);
42
xml.Autoconnect (this);
46
iconList = new ThemedIconList ();
47
iconList.SelectionChanged += new EventHandler (OnIconSelectionChanged);
48
iconScrolledwindow.AddWithViewport (iconList);
52
foreach (IconSize s in Enum.GetValues (typeof(Gtk.IconSize))) {
53
if (s != IconSize.Invalid)
54
iconSizeCombo.AppendText (s.ToString ());
56
iconSizeCombo.Active = 0;
60
resourceListStore = new Gtk.ListStore (typeof(Gdk.Pixbuf), typeof(string), typeof(string));
61
resourceList.Model = resourceListStore;
63
Gtk.TreeViewColumn col = new Gtk.TreeViewColumn ();
65
Gtk.CellRendererPixbuf pr = new Gtk.CellRendererPixbuf ();
67
col.PackStart (pr, false);
68
col.AddAttribute (pr, "pixbuf", 0);
70
Gtk.CellRendererText crt = new Gtk.CellRendererText ();
71
col.PackStart (crt, true);
72
col.AddAttribute (crt, "markup", 1);
74
resourceList.AppendColumn (col);
75
resourceProvider = project.ResourceProvider;
76
if (resourceProvider == null) {
77
buttonAdd.Sensitive = false;
78
buttonRemove.Sensitive = false;
81
resourceList.Selection.Changed += OnResourceSelectionChanged;
83
if (project.FileName != null)
84
fileChooser.SetCurrentFolder (project.ImagesRootPath);
86
fileChooser.SelectionChanged += delegate (object s, EventArgs a) {
90
fileChooser.FileActivated += delegate (object s, EventArgs a) {
93
dialog.Respond (Gtk.ResponseType.Ok);
97
okButton.Clicked += OnOkClicked;
105
dialog.TransientFor = parent;
106
return dialog.Run ();
109
public void Dispose ()
114
public ImageInfo Icon {
116
if (notebook.Page == 0) {
117
if (iconNameEntry.Text.Length == 0)
119
return ImageInfo.FromTheme (iconNameEntry.Text, SelectedIconSize);
120
} else if (notebook.Page == 1) {
121
if (resourceNameEntry.Text.Length == 0)
123
return ImageInfo.FromResource (resourceNameEntry.Text);
125
if (importedImageFile != null)
126
return ImageInfo.FromFile (importedImageFile);
127
if (fileChooser.Filename == null || fileChooser.Filename.Length == 0 || !File.Exists (fileChooser.Filename))
129
return ImageInfo.FromFile (fileChooser.Filename);
135
if (value.Source == ImageSource.Theme) {
136
iconNameEntry.Text = value.Name;
137
SelectedIconSize = value.ThemeIconSize;
139
} else if (value.Source == ImageSource.Resource) {
141
resourceNameEntry.Text = value.Name;
143
fileChooser.SetFilename (value.Name);
149
Gtk.IconSize SelectedIconSize {
150
get { return (IconSize) iconSizeCombo.Active + 1; }
151
set { iconSizeCombo.Active = ((int) value) - 1; }
154
void UpdateButtons ()
156
okButton.Sensitive = Icon != null;
159
protected void OnCurrentPageChanged (object s, Gtk.SwitchPageArgs args)
164
void OnIconSelectionChanged (object s, EventArgs args)
166
if (iconList.Selection != null) {
167
iconNameEntry.Text = iconList.Selection;
171
void UpdateIconSelection ()
173
Gdk.Pixbuf icon = null;
174
if (iconNameEntry.Text.Length > 0) {
175
icon = WidgetUtils.LoadIcon (iconNameEntry.Text, SelectedIconSize);
178
icon = WidgetUtils.MissingIcon;
179
previewIcon.Pixbuf = icon;
182
protected void OnIconSizeChanged (object ob, EventArgs args)
184
UpdateIconSelection ();
187
protected void OnIconNameChanged (object ob, EventArgs args)
189
UpdateIconSelection ();
193
void FillResources ()
195
resourceListStore.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);
206
void AppendResource (Stream stream, string name)
209
Gdk.Pixbuf pix = new Gdk.Pixbuf (stream);
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;
216
// Doesn't look like a valid image. Just ignore it.
220
Gdk.Pixbuf GetThumbnail (Gdk.Pixbuf pix)
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);
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);
236
void OnResourceSelectionChanged (object obj, EventArgs args)
240
if (!resourceList.Selection.GetSelected (out model, out iter)) {
241
resourceNameEntry.Text = "";
243
resourceNameEntry.Text = (string) resourceListStore.GetValue (iter, 2);
247
protected void OnResourceNameChanged (object ob, EventArgs args)
249
Gdk.Pixbuf pix = (Gdk.Pixbuf) resources [resourceNameEntry.Text];
251
previewResource.Pixbuf = pix;
253
if (missingThumbnail == null)
254
missingThumbnail = WidgetUtils.MissingIcon;
255
previewResource.Pixbuf = missingThumbnail;
260
protected void OnAddResource (object ob, EventArgs args)
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;
275
protected void OnRemoveResource (object ob, EventArgs args)
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);
292
if (notebook.Page == 2) {
293
if (fileChooser.Filename == null || fileChooser.Filename.Length == 0 || !File.Exists (fileChooser.Filename))
296
importedImageFile = project.ImportFile (fileChooser.Filename);
297
if (importedImageFile != null)
298
importedImageFile = WidgetUtils.AbsoluteToRelativePath (project.ImagesRootPath, importedImageFile);
299
return importedImageFile != null;
304
void OnOkClicked (object s, EventArgs args)
307
dialog.Respond (Gtk.ResponseType.Ok);