Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SelectImageDialog.cs « editor « libstetic « MonoDevelop.GtkCore « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c4f6330cb6e0aacf73e3a4a4463cd54476bd8141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

using System;
using System.Collections;
using System.IO;
using Gtk;

namespace Stetic.Editor
{
	public class SelectImageDialog: IDisposable
	{
		[Glade.Widget] Gtk.TreeView resourceList;
		[Glade.Widget] Gtk.FileChooserWidget fileChooser;
		[Glade.Widget] Gtk.Entry iconNameEntry;
		[Glade.Widget] Gtk.Notebook notebook;
		[Glade.Widget] Gtk.ScrolledWindow iconScrolledwindow;
		[Glade.Widget] Gtk.Image previewIcon;
		[Glade.Widget] Gtk.Image previewResource;
		[Glade.Widget] Gtk.ComboBox iconSizeCombo;
		[Glade.Widget] Gtk.Entry resourceNameEntry;
		[Glade.Widget] Gtk.Button okButton;
		[Glade.Widget] Gtk.Button buttonAdd;
		[Glade.Widget] Gtk.Button buttonRemove;
		[Glade.Widget ("SelectImageDialog")] Gtk.Dialog dialog;
		
		ThemedIconList iconList;
		
		Gtk.ListStore resourceListStore;
		Gtk.Window parent;
		
		int thumbnailSize = 30;
		Hashtable resources = new Hashtable ();	// Stores resourceName -> thumbnail pixbuf
		Gdk.Pixbuf missingThumbnail;
		IResourceProvider resourceProvider;
		string importedImageFile;
		Stetic.IProject project;
		
		public SelectImageDialog (Gtk.Window parent, Stetic.IProject project)
		{
			this.parent = parent;
			this.project = project;
			Glade.XML xml = new Glade.XML (null, "stetic.glade", "SelectImageDialog", null);
			xml.Autoconnect (this);
			
			// Stock icon list
			
			iconList = new ThemedIconList ();
			iconList.SelectionChanged += new EventHandler (OnIconSelectionChanged);
			iconScrolledwindow.AddWithViewport (iconList);
			
			// Icon Sizes
			
			foreach (IconSize s in Enum.GetValues (typeof(Gtk.IconSize))) {
				if (s != IconSize.Invalid)
					iconSizeCombo.AppendText (s.ToString ());
			}
			iconSizeCombo.Active = 0;
			
			// Resource list
			
			resourceListStore = new Gtk.ListStore (typeof(Gdk.Pixbuf), typeof(string), typeof(string));
			resourceList.Model = resourceListStore;
			
			Gtk.TreeViewColumn col = new Gtk.TreeViewColumn ();
			
			Gtk.CellRendererPixbuf pr = new Gtk.CellRendererPixbuf ();
			pr.Xpad = 3;
			col.PackStart (pr, false);
			col.AddAttribute (pr, "pixbuf", 0);
			
			Gtk.CellRendererText crt = new Gtk.CellRendererText ();
			col.PackStart (crt, true);
			col.AddAttribute (crt, "markup", 1);
			
			resourceList.AppendColumn (col);
			resourceProvider = project.ResourceProvider;
			if (resourceProvider == null) {
				buttonAdd.Sensitive = false;
				buttonRemove.Sensitive = false;
			}
			FillResources ();
			resourceList.Selection.Changed += OnResourceSelectionChanged;
			
			if (project.FileName != null)
				fileChooser.SetCurrentFolder (project.ImagesRootPath);

			fileChooser.SelectionChanged += delegate (object s, EventArgs a) {
				UpdateButtons ();
			};

			fileChooser.FileActivated += delegate (object s, EventArgs a) {
				if (Icon != null) {
					if (Validate ())
						dialog.Respond (Gtk.ResponseType.Ok);
				}
			};
			
			okButton.Clicked += OnOkClicked;

			UpdateButtons ();
		}
		
		public int Run ()
		{
			dialog.ShowAll ();
			dialog.TransientFor = parent;
			return dialog.Run ();
		}
		
		public void Dispose ()
		{
			dialog.Destroy ();
		}
		
		public ImageInfo Icon {
			get {
				if (notebook.Page == 0) {
					if (iconNameEntry.Text.Length == 0)
						return null;
					return ImageInfo.FromTheme (iconNameEntry.Text, SelectedIconSize);
				} else if (notebook.Page == 1) {
					if (resourceNameEntry.Text.Length == 0)
						return null;
					return ImageInfo.FromResource (resourceNameEntry.Text);
				} else {
					if (importedImageFile != null)
						return ImageInfo.FromFile (importedImageFile);
					if (fileChooser.Filename == null || fileChooser.Filename.Length == 0 || !File.Exists (fileChooser.Filename))
						return null;
					return ImageInfo.FromFile (fileChooser.Filename);
				}
			}
			set {
				if (value == null)
					return;
				if (value.Source == ImageSource.Theme) {
					iconNameEntry.Text = value.Name;
					SelectedIconSize = value.ThemeIconSize;
					notebook.Page = 0;
				} else if (value.Source == ImageSource.Resource) {
					notebook.Page = 1;
					resourceNameEntry.Text = value.Name;
				} else {
					fileChooser.SetFilename (value.Name);
					notebook.Page = 2;
				}
			}
		}
		
		Gtk.IconSize SelectedIconSize {
			get { return (IconSize) iconSizeCombo.Active + 1; }
			set { iconSizeCombo.Active = ((int) value) - 1; }
		}
		
		void UpdateButtons ()
		{
			okButton.Sensitive = Icon != null;
		}
		
		protected void OnCurrentPageChanged (object s, Gtk.SwitchPageArgs args)
		{
			UpdateButtons ();
		}
		
		void OnIconSelectionChanged (object s, EventArgs args)
		{
			if (iconList.Selection != null) {
				iconNameEntry.Text = iconList.Selection;
			}
		}
		
		void UpdateIconSelection ()
		{
			Gdk.Pixbuf icon = null;
			if (iconNameEntry.Text.Length > 0) {
				icon = WidgetUtils.LoadIcon (iconNameEntry.Text, SelectedIconSize);
			}
			if (icon == null)
				icon = WidgetUtils.MissingIcon;
			previewIcon.Pixbuf = icon;
		}
		
		protected void OnIconSizeChanged (object ob, EventArgs args)
		{
			UpdateIconSelection ();
		}
		
		protected void OnIconNameChanged (object ob, EventArgs args)
		{
			UpdateIconSelection ();
			UpdateButtons ();
		}
		
		void FillResources ()
		{
			resourceListStore.Clear ();
			resources.Clear ();
			if (resourceProvider != null) {
				foreach (ResourceInfo res in resourceProvider.GetResources ()) {
					if (res.MimeType.StartsWith ("image/")) {
						AppendResource (resourceProvider.GetResourceStream (res.Name), res.Name);
					}
				}
			}
		}
		
		void AppendResource (Stream stream, string name)
		{
			try {
				Gdk.Pixbuf pix = new Gdk.Pixbuf (stream);
				string txt = name + "\n<span foreground='darkgrey' size='x-small'>" + pix.Width + " x " + pix.Height + "</span>";
				pix = GetThumbnail (pix);
				resourceListStore.AppendValues (pix, txt, name);
				resources [name] = pix;
			} catch {
				// Doesn't look like a valid image. Just ignore it.
			} finally {
				stream.Dispose ();
			}
		}
		
		Gdk.Pixbuf GetThumbnail (Gdk.Pixbuf pix)
		{
			if (pix.Width > pix.Height) {
				if (pix.Width > thumbnailSize) {
					float prop = (float) pix.Height / (float) pix.Width;
					return pix.ScaleSimple (thumbnailSize, (int)(thumbnailSize * prop), Gdk.InterpType.Bilinear);
				}
			} else {
				if (pix.Height > thumbnailSize) {
					float prop = (float) pix.Width / (float) pix.Height;
					return pix.ScaleSimple ((int)(thumbnailSize * prop), thumbnailSize, Gdk.InterpType.Bilinear);
				}
			}
			return pix;
		}
		
		void OnResourceSelectionChanged (object obj, EventArgs args)
		{
			Gtk.TreeIter iter;
			Gtk.TreeModel model;
			if (!resourceList.Selection.GetSelected (out model, out iter)) {
				resourceNameEntry.Text = "";
			} else {
				resourceNameEntry.Text = (string) resourceListStore.GetValue (iter, 2);
			}
		}
		
		protected void OnResourceNameChanged (object ob, EventArgs args)
		{
			Gdk.Pixbuf pix = (Gdk.Pixbuf) resources [resourceNameEntry.Text];
			if (pix != null)
				previewResource.Pixbuf = pix;
			else {
				if (missingThumbnail == null)
					missingThumbnail = WidgetUtils.MissingIcon;
				previewResource.Pixbuf = missingThumbnail;
			}
			UpdateButtons ();
		}
		
		protected void OnAddResource (object ob, EventArgs args)
		{
			FileChooserDialog dialog =
				new FileChooserDialog ("Open File", null, FileChooserAction.Open,
						       Gtk.Stock.Cancel, Gtk.ResponseType.Cancel,
						       Gtk.Stock.Open, Gtk.ResponseType.Ok);
			if (parent != null)
				dialog.TransientFor = parent.Toplevel as Gtk.Window;
			int response = dialog.Run ();
			if (response == (int)Gtk.ResponseType.Ok) {
				ResourceInfo rinfo = resourceProvider.AddResource (dialog.Filename);
				AppendResource (resourceProvider.GetResourceStream (rinfo.Name), rinfo.Name);
				resourceNameEntry.Text = rinfo.Name;
			}
			dialog.Destroy ();
		}
		
		protected void OnRemoveResource (object ob, EventArgs args)
		{
			Gtk.TreeIter iter;
			Gtk.TreeModel model;
			if (resourceList.Selection.GetSelected (out model, out iter)) {
				string res = (string) resourceListStore.GetValue (iter, 2);
				Gtk.MessageDialog msg = new Gtk.MessageDialog (dialog, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, "Are you sure you want to delete the resource '{0}'?", res);
				if (parent != null)
					msg.TransientFor = parent.Toplevel as Gtk.Window;
				if (msg.Run () == (int) ResponseType.Yes) {
					resourceProvider.RemoveResource (res);
					resourceListStore.Remove (ref iter);
				}
				msg.Destroy ();
			}
		}
		
		bool Validate ()
		{
			if (notebook.Page == 2) {
				if (fileChooser.Filename == null || fileChooser.Filename.Length == 0 || !File.Exists (fileChooser.Filename))
					return true;
				
				importedImageFile = project.ImportFile (fileChooser.Filename);
				if (importedImageFile != null)
					importedImageFile = WidgetUtils.AbsoluteToRelativePath (project.ImagesRootPath, importedImageFile);
				return importedImageFile != null;
			}
			return true;
		}
		
		void OnOkClicked (object s, EventArgs args)
		{
			if (Validate ())
				dialog.Respond (Gtk.ResponseType.Ok);
		}
	}
}