From 475b44e7ba7f6f0d5c205958146acf831e051f66 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 1 Apr 2005 14:54:12 +0000 Subject: [PATCH] * gdk/PixbufLoader.custom: Implement System.IO.Stream and resource-file constructors (using code formerly in Pixbuf.custom) * gdk/Pixbuf.custom: Redo the stream and resource ctors in terms of the PixbufLoader ones. * gdk/PixbufAnimation.custom: Add stream and resource ctors * gtk/Image.custom: Add stream and resource ctors svn path=/trunk/gtk-sharp/; revision=42463 --- ChangeLog | 12 +++++ doc/ChangeLog | 7 +++ doc/en/Gdk/Pixbuf.xml | 10 ++-- doc/en/Gdk/PixbufAnimation.xml | 85 ++++++++++++++++++++++++++++++++++ doc/en/Gdk/PixbufLoader.xml | 55 +++++++++++++++++++++- doc/en/Gtk/Image.xml | 79 +++++++++++++++++++++++++++---- gdk/Makefile.am | 1 + gdk/Pixbuf.custom | 38 +-------------- gdk/PixbufAnimation.custom | 33 +++++++++++++ gdk/PixbufLoader.custom | 48 ++++++++++++++++++- gtk/Image.custom | 34 ++++++++++++++ 11 files changed, 353 insertions(+), 49 deletions(-) create mode 100644 gdk/PixbufAnimation.custom diff --git a/ChangeLog b/ChangeLog index e1aa673ae..efba64157 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2005-03-31 Dan Winship + + * gdk/PixbufLoader.custom: Implement System.IO.Stream and + resource-file constructors (using code formerly in Pixbuf.custom) + + * gdk/Pixbuf.custom: Redo the stream and resource ctors in terms + of the PixbufLoader ones. + + * gdk/PixbufAnimation.custom: Add stream and resource ctors + + * gtk/Image.custom: Add stream and resource ctors + 2005-03-31 Mike Kestner * glib/ManagedValue.cs : add null/NULL guarding to Copy, Free, diff --git a/doc/ChangeLog b/doc/ChangeLog index adc4a4b63..412a32984 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2005-04-01 Dan Winship + + * en/Gdk/Pixbuf.xml: + * en/Gdk/PixbufAnimation.xml: + * en/Gdk/PixbufLoader.xml: + * en/Gtk/Image.xml: document new/updated stream/resource constructors + 2005-03-15 Dan Winship * en/Gtk/NodeStore.xml (AddNode): diff --git a/doc/en/Gdk/Pixbuf.xml b/doc/en/Gdk/Pixbuf.xml index 9b2cda2bd..5b535fe2b 100644 --- a/doc/en/Gdk/Pixbuf.xml +++ b/doc/en/Gdk/Pixbuf.xml @@ -1215,10 +1215,14 @@ mcs -resource:image.jpg sample.cs - Loads a pixbuf from a resource string. - a + Loads a pixbuf from a resource file. + the name of the resource a - TODO: Clarify. + + This loads a pixbuf from a resource in the calling assembly. This is equivalent to + using the + constructor with a assembly. + diff --git a/doc/en/Gdk/PixbufAnimation.xml b/doc/en/Gdk/PixbufAnimation.xml index 2207e02e1..a6f0f3067 100644 --- a/doc/en/Gdk/PixbufAnimation.xml +++ b/doc/en/Gdk/PixbufAnimation.xml @@ -245,5 +245,90 @@ To be added + + + Method + + Gdk.PixbufAnimation + + + + + + Loads a pixbuf from a resource file. + the name of the resource + a + + This loads an animation from a resource in the calling assembly. This is equivalent to + using the + constructor with a assembly. + + + + + + Constructor + + + + + + Makes a new animation object from a . + a + a + Useful for creating an animation from an image file that resides in memory. + + + +/* buffer containing an image */ +System.Byte[] buffer = new System.Byte[256]; + +/* create a memory stream to the buffer */ +System.IO.MemoryStream memorystream = new System.IO.MemoryStream(buffer); + +/* create an animation from the stream as if it was a file */ +Gdk.PixbufAnimation pba = new Gdk.PixbufAnimation(memorystream); + + + + + + + Constructor + + + + + + + Constructor for images embedded in an assembly + The that contains the image. + +If the value is , the image will be looked up on the calling assembly. + The name given as the resource in the assembly + The created from the resource stream + + + This method is used to construct a from an embedded resource in an assembly. + + + Typically this is used when application developers want to distribute images in a single executable. + + +If the assembly parameter is , the image will be looked up on the calling assembly. + + +For example: + + +Gdk.PixbufAnimation p = new PixbufAnimation (null, "anim.gif"); + + Compile with: + +mcs -resource:anim.gif sample.cs + + + + \ No newline at end of file diff --git a/doc/en/Gdk/PixbufLoader.xml b/doc/en/Gdk/PixbufLoader.xml index 35d82080c..56f3df919 100644 --- a/doc/en/Gdk/PixbufLoader.xml +++ b/doc/en/Gdk/PixbufLoader.xml @@ -450,5 +450,58 @@ namespace GtkDemo To be added + + + Method + + Gdk.PixbufLoader + + + + + + Loads a pixbuf from a resource file. + the name of the resource + a + + This creates a pixbuf loader to load from a resource in the calling assembly. + This is equivalent to using the + + constructor with a assembly. + + + + + + Constructor + + + + + + Loads a Pixbuf from a . + a + a + See also + + + + + Constructor + + + + + + + Loads a Pixbuf embedded in an assembly + The that contains the image. + +If the value is , the image will be looked up on the calling assembly. + The name given as the resource in the assembly + a for the resource stream + See also + + - + \ No newline at end of file diff --git a/doc/en/Gtk/Image.xml b/doc/en/Gtk/Image.xml index 91064a1c1..8dd7ab038 100644 --- a/doc/en/Gtk/Image.xml +++ b/doc/en/Gtk/Image.xml @@ -445,7 +445,7 @@ masked by the given . The type of representation being used by the - + to store image data. If the Image has no image data, the return value will be . @@ -580,10 +580,11 @@ masked by the given . - Same functionality as . + Same functionality as . a a - + + @@ -598,15 +599,15 @@ masked by the given . - Gets the and mask being - displayed by the . + Gets the and mask being + displayed by the . a a The storage type of the image must be - . - or . + . + or . (see ). @@ -683,5 +684,67 @@ masked by the given . + + + Method + + Gtk.Image + + + + + + Loads an image from a resource file. + the name of the resource + a + + This load an image from a resource in the calling assembly. This is + equivalent to using the + + constructor with a assembly. + + + + + + Constructor + + + + + + Loads an image from a . + a + a + + This is equivalent to calling + or and then creating + an from the resulting pixbuf. + + + + + + Constructor + + + + + + + Loads an image embedded in an assembly + The that contains the image. + +If the value is , the image will be looked up on the calling assembly. + The name given as the resource in the assembly + a + + This is equivalent to calling + or + + and then creating an from the resulting pixbuf. + + + - + \ No newline at end of file diff --git a/gdk/Makefile.am b/gdk/Makefile.am index fabf39ca9..bd40b6a06 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -41,6 +41,7 @@ customs = \ Keymap.custom \ Pixmap.custom \ Pixbuf.custom \ + PixbufAnimation.custom \ PixbufLoader.custom \ Pixdata.custom \ Point.custom \ diff --git a/gdk/Pixbuf.custom b/gdk/Pixbuf.custom index 9a4c38acc..d1c867b3e 100644 --- a/gdk/Pixbuf.custom +++ b/gdk/Pixbuf.custom @@ -92,54 +92,20 @@ gdk_pixbuf_render_to_drawable_alpha(Handle, drawable.Handle, src_x, src_y, dest_x, dest_y, width, height, (int) alpha_mode, alpha_threshold, (int) dither, x_dither, y_dither); } - [DllImport("libgobject-2.0-0.dll")] - static extern void g_object_ref (IntPtr handle); - - private void LoadFromStream (System.IO.Stream input) - { - PixbufLoader loader = new PixbufLoader (); - byte [] buffer = new byte [8192]; - int n; - - while ((n = input.Read (buffer, 0, 8192)) != 0) - loader.Write (buffer, (uint) n); - - loader.Close (); - Raw = loader.PixbufHandle; - if (Raw == IntPtr.Zero) - throw new ArgumentException ("Unable to load pixbuf from stream"); - g_object_ref (Raw); - } - - public Pixbuf (System.IO.Stream stream) : base (IntPtr.Zero) - { - LoadFromStream (stream); - } + public Pixbuf (System.IO.Stream stream) : base (new PixbufLoader (stream).PixbufHandle) {} public Pixbuf (System.Reflection.Assembly assembly, string resource) : base (IntPtr.Zero) { if (assembly == null) assembly = System.Reflection.Assembly.GetCallingAssembly (); - - if (resource == null) - throw new ArgumentNullException ("resource"); - - System.IO.Stream s = assembly.GetManifestResourceStream (resource); - if (s == null) - throw new ArgumentException ("resource must be a valid resource name of 'assembly'."); - - LoadFromStream (s); + Raw = new PixbufLoader (assembly, resource).PixbufHandle; } static public Pixbuf LoadFromResource (string resource) { - if (resource == null) - throw new ArgumentNullException ("resource"); - return new Pixbuf (System.Reflection.Assembly.GetCallingAssembly (), resource); } - [DllImport("libgdk_pixbuf-2.0-0.dll")] static extern IntPtr gdk_pixbuf_scale_simple(IntPtr raw, int dest_width, int dest_height, int interp_type); diff --git a/gdk/PixbufAnimation.custom b/gdk/PixbufAnimation.custom new file mode 100644 index 000000000..c7a9774ad --- /dev/null +++ b/gdk/PixbufAnimation.custom @@ -0,0 +1,33 @@ +// PixbufAnimation.custom - GdkPixbufAnimation class customizations +// +// Copyright (c) 2005 Novell, Inc. +// +// This code is inserted after the automatically generated code. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + + public PixbufAnimation (System.IO.Stream stream) : base (new PixbufLoader (stream).AnimationHandle) {} + + public PixbufAnimation (System.Reflection.Assembly assembly, string resource) : base (IntPtr.Zero) + { + if (assembly == null) + assembly = System.Reflection.Assembly.GetCallingAssembly (); + Raw = new PixbufLoader (assembly, resource).AnimationHandle; + } + + static public PixbufAnimation LoadFromResource (string resource) + { + return new PixbufAnimation (System.Reflection.Assembly.GetCallingAssembly (), resource); + } diff --git a/gdk/PixbufLoader.custom b/gdk/PixbufLoader.custom index 3b0b04039..e973329c7 100644 --- a/gdk/PixbufLoader.custom +++ b/gdk/PixbufLoader.custom @@ -21,9 +21,18 @@ // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. + [DllImport("libgobject-2.0-0.dll")] + static extern IntPtr g_object_ref (IntPtr handle); + internal IntPtr PixbufHandle { get { - return gdk_pixbuf_loader_get_pixbuf (Handle); + return g_object_ref (gdk_pixbuf_loader_get_pixbuf (Handle)); + } + } + + internal IntPtr AnimationHandle { + get { + return g_object_ref (gdk_pixbuf_loader_get_animation (Handle)); } } @@ -32,3 +41,40 @@ return this.Write (bytes, (uint) bytes.Length); } + private void LoadFromStream (System.IO.Stream input) + { + byte [] buffer = new byte [8192]; + int n; + + while ((n = input.Read (buffer, 0, 8192)) != 0) + Write (buffer, (uint) n); + Close (); + } + + public PixbufLoader (System.IO.Stream stream) : this () + { + LoadFromStream (stream); + } + + public PixbufLoader (System.Reflection.Assembly assembly, string resource) : this () + { + if (assembly == null) + assembly = System.Reflection.Assembly.GetCallingAssembly (); + + if (resource == null) + throw new ArgumentNullException ("resource"); + + System.IO.Stream s = assembly.GetManifestResourceStream (resource); + if (s == null) + throw new ArgumentException ("'" + resource + "' is not a valid resource name of assembly '" + assembly + "'."); + + LoadFromStream (s); + } + + static public PixbufLoader LoadFromResource (string resource) + { + if (resource == null) + throw new ArgumentNullException ("resource"); + + return new PixbufLoader (System.Reflection.Assembly.GetCallingAssembly (), resource); + } diff --git a/gtk/Image.custom b/gtk/Image.custom index 7bf5a7211..f1d6b344c 100644 --- a/gtk/Image.custom +++ b/gtk/Image.custom @@ -54,3 +54,37 @@ GLib.Marshaller.Free (native); } + void LoadFromStream (System.IO.Stream stream) + { + try { + Gdk.PixbufAnimation anim = new Gdk.PixbufAnimation (stream); + if (anim.IsStaticImage) + Pixbuf = anim.StaticImage; + else + PixbufAnimation = anim; + } catch { + Stock = Gtk.Stock.MissingImage; + } + } + + public Image (System.IO.Stream stream) : this () + { + LoadFromStream (stream); + } + + public Image (System.Reflection.Assembly assembly, string resource) : this () + { + if (assembly == null) + assembly = System.Reflection.Assembly.GetCallingAssembly (); + + System.IO.Stream s = assembly.GetManifestResourceStream (resource); + if (s == null) + throw new ArgumentException ("'" + resource + "' is not a valid resource name of assembly '" + assembly + "'."); + + LoadFromStream (s); + } + + static public Image LoadFromResource (string resource) + { + return new Image (System.Reflection.Assembly.GetCallingAssembly (), resource); + }