1// Image.custom - Customizations to Gtk.Image
2//
3// Authors: Mike Kestner  <mkestner@novell.com>
4// Authors: Stephane Delcroix  <sdelcroix@novell.com>
5//
6// Copyright (c) 2004-2008 Novell, Inc.
7//
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of version 2 of the Lesser GNU General
10// Public License as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15// Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public
18// License along with this program; if not, write to the
19// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20// Boston, MA 02111-1307, USA.
21
22		[DllImport("libgtk-win32-2.0-0.dll", CallingConvention=CallingConvention.Cdecl)]
23		static extern IntPtr gtk_image_new_from_icon_set(IntPtr icon_set, int size);
24
25		public Image (Gtk.IconSet icon_set, Gtk.IconSize size) : base (IntPtr.Zero)
26		{
27			if (GetType () != typeof (Image)) {
28				var vals = new GLib.Value[2];
29				var names = new IntPtr[2];
30				names[0] = GLib.Marshaller.StringToPtrGStrdup ("icon_set");
31				vals[0] = new GLib.Value (icon_set);
32				names[1] = GLib.Marshaller.StringToPtrGStrdup ("icon_size");
33				vals[1] = new GLib.Value ((int)size);
34				CreateNativeObject (names, vals, 2);
35				return;
36			}
37			owned = true;
38			Raw = gtk_image_new_from_icon_set(icon_set.Handle, (int) size);
39		}
40
41		[DllImport("libgtk-win32-2.0-0.dll", CallingConvention=CallingConvention.Cdecl)]
42		static extern IntPtr gtk_image_new_from_stock(IntPtr stock_id, int size);
43
44		public Image (string stock_id, Gtk.IconSize size) : base (IntPtr.Zero)
45		{
46			if (GetType () != typeof (Image)) {
47				var vals = new GLib.Value[2];
48				var names = new IntPtr[2];
49				names[0] = GLib.Marshaller.StringToPtrGStrdup ("stock");
50				vals[0] = new GLib.Value (stock_id);
51				names[1] = GLib.Marshaller.StringToPtrGStrdup ("icon_size");
52				vals[1] = new GLib.Value ((int)size);
53				CreateNativeObject (names, vals, 2);
54				return;
55			}
56			owned = true;
57			IntPtr native = GLib.Marshaller.StringToPtrGStrdup (stock_id);
58			Raw = gtk_image_new_from_stock(native, (int) size);
59			GLib.Marshaller.Free (native);
60		}
61
62		void LoadFromStream (System.IO.Stream stream)
63		{
64			try {
65				Gdk.PixbufAnimation anim = new Gdk.PixbufAnimation (stream);
66				if (anim.IsStaticImage)
67					Pixbuf = anim.StaticImage;
68				else
69					PixbufAnimation = anim;
70			} catch {
71				Stock = Gtk.Stock.MissingImage;
72			}
73		}
74
75		public Image (System.IO.Stream stream) : this ()
76		{
77			LoadFromStream (stream);
78		}
79
80		public Image (System.Reflection.Assembly assembly, string resource) : this ()
81		{
82			if (assembly == null)
83				assembly = System.Reflection.Assembly.GetCallingAssembly ();
84
85			System.IO.Stream s = assembly.GetManifestResourceStream (resource);
86			if (s == null)
87				throw new ArgumentException ("'" + resource + "' is not a valid resource name of assembly '" + assembly + "'.");
88
89			LoadFromStream (s);
90		}
91
92		static public Image LoadFromResource (string resource)
93		{
94			return new Image (System.Reflection.Assembly.GetCallingAssembly (), resource);
95		}
96
97		[Obsolete ("Use the Animation property instead")]
98		public Gdk.PixbufAnimation FromAnimation {
99			set {
100				gtk_image_set_from_animation(Handle, value == null ? IntPtr.Zero : value.Handle);
101			}
102		}
103
104		[Obsolete ("Use the File property instead")]
105		public string FromFile {
106			set {
107				IntPtr native_value = GLib.Marshaller.StringToPtrGStrdup (value);
108				gtk_image_set_from_file(Handle, native_value);
109				GLib.Marshaller.Free (native_value);
110			}
111		}
112
113		[Obsolete ("Use the Pixbuf property instead")]
114		public Gdk.Pixbuf FromPixbuf {
115			set {
116				gtk_image_set_from_pixbuf(Handle, value == null ? IntPtr.Zero : value.Handle);
117			}
118		}
119
120
121