From 58f6f01d458267b44f7cadea791d34c59a669849 Mon Sep 17 00:00:00 2001 From: Vladimir Vukicevic Date: Sun, 10 Nov 2002 10:09:05 +0000 Subject: [PATCH] * glib/Object.cs: add needs_ref boolean that controls whether we need to ref this object once we have a pointer to it or not. By default this is set to true -- constructors and other functions where we do own the object need to set this to false before setting the "Raw" property. Also added Unref and RefCount methods. * glue/object.c, glue/type.c: some utility functions for refcounting support * gdk/Pixbuf.custom: manually wrap a few functions so that the refcount ends up being correct at the end (need an extra Unref) * api/gdk-api.xml, sources/Gdk.metadata: metadata updates for hiding manually-wrapped Pixbuf stuff svn path=/trunk/gtk-sharp/; revision=8913 --- ChangeLog | 17 ++++++++++ api/gdk-api.xml | 29 ---------------- gdk/Pixbuf.custom | 81 +++++++++++++++++++++++++++++++------------- glib/Object.cs | 41 ++++++++++++++++++++-- glue/object.c | 11 ++++++ glue/type.c | 2 +- sources/Gdk.metadata | 16 +++++++++ 7 files changed, 142 insertions(+), 55 deletions(-) diff --git a/ChangeLog b/ChangeLog index c214fb1e9..c54edcf44 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2002-11-10 Vladimir Vukicevic + + * glib/Object.cs: add needs_ref boolean that controls whether + we need to ref this object once we have a pointer to it or not. + By default this is set to true -- constructors and other functions + where we do own the object need to set this to false before setting + the "Raw" property. Also added Unref and RefCount methods. + + * glue/object.c, glue/type.c: some utility functions for + refcounting support + + * gdk/Pixbuf.custom: manually wrap a few functions so that + the refcount ends up being correct at the end (need an extra Unref) + + * api/gdk-api.xml, sources/Gdk.metadata: metadata updates for + hiding manually-wrapped Pixbuf stuff + 2002-11-10 Vladimir Vukicevic * generator/StructBase.cs: create a Zero static member for diff --git a/api/gdk-api.xml b/api/gdk-api.xml index ed8df566c..7b6a05e50 100644 --- a/api/gdk-api.xml +++ b/api/gdk-api.xml @@ -2411,15 +2411,6 @@ - - - - - - - - - @@ -2460,18 +2451,6 @@ - - - - - - - - - - - - @@ -2721,14 +2700,6 @@ - - - - - - - - diff --git a/gdk/Pixbuf.custom b/gdk/Pixbuf.custom index ac209186d..6e299700a 100644 --- a/gdk/Pixbuf.custom +++ b/gdk/Pixbuf.custom @@ -1,29 +1,64 @@ -IntPtr LoadFromStream (System.IO.Stream input) -{ - PixbufLoader loader = new PixbufLoader (); - byte [] buffer = new byte [8192]; + IntPtr 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 (); + + return loader.Pixbuf.Handle; + } + + public Pixbuf (System.IO.Stream input) + { + Raw = LoadFromStream (input); + } + + public Pixbuf (System.Reflection.Assembly assembly, string resource) + { + if (assembly == null) + assembly = System.Reflection.Assembly.GetCallingAssembly (); + + System.IO.Stream s; + Pixbuf p = null; + using (s = assembly.GetManifestResourceStream (resource)) + Raw = LoadFromStream (s); + } - int n; + // scale_simple, composite_color_simple, and addalpha do a gdk_pixbuf_new on the + // return first, and we also ref it + // in the GetObject. So get rid of one extra reference. - while ((n = input.Read (buffer, 0, 8192)) != 0) - loader.Write (buffer, (uint) n); - loader.Close (); + + [DllImport("gdk_pixbuf-2.0")] + static extern IntPtr gdk_pixbuf_scale_simple(IntPtr raw, int dest_width, int dest_height, int interp_type); - return loader.Pixbuf.Handle; -} + public Gdk.Pixbuf ScaleSimple(int dest_width, int dest_height, Gdk.InterpType interp_type) { + IntPtr raw_ret = gdk_pixbuf_scale_simple(Handle, dest_width, dest_height, (int) interp_type); + Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret); + ret.Unref (); + return ret; + } -public Pixbuf (System.IO.Stream input) -{ - Raw = LoadFromStream (input); -} + [DllImport("gdk_pixbuf-2.0")] + static extern IntPtr gdk_pixbuf_composite_color_simple(IntPtr raw, int dest_width, int dest_height, int interp_type, int overall_alpha, int check_size, uint color1, uint color2); -public Pixbuf (System.Reflection.Assembly assembly, string resource) -{ - if (assembly == null) - assembly = System.Reflection.Assembly.GetCallingAssembly (); + public Gdk.Pixbuf CompositeColorSimple(int dest_width, int dest_height, Gdk.InterpType interp_type, int overall_alpha, int check_size, uint color1, uint color2) { + IntPtr raw_ret = gdk_pixbuf_composite_color_simple(Handle, dest_width, dest_height, (int) interp_type, overall_alpha, check_size, color1, color2); + Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret); + ret.Unref (); + return ret; + } - System.IO.Stream s; - Pixbuf p = null; - using (s = assembly.GetManifestResourceStream (resource)) - Raw = LoadFromStream (s); -} \ No newline at end of file + [DllImport("gdk_pixbuf-2.0")] + static extern IntPtr gdk_pixbuf_add_alpha(IntPtr raw, bool substitute_color, byte r, byte g, byte b); + + public Gdk.Pixbuf AddAlpha(bool substitute_color, byte r, byte g, byte b) { + IntPtr raw_ret = gdk_pixbuf_add_alpha(Handle, substitute_color, r, g, b); + Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret); + ret.Unref (); + return ret; + } diff --git a/glib/Object.cs b/glib/Object.cs index 85a821388..f6ed52e6d 100644 --- a/glib/Object.cs +++ b/glib/Object.cs @@ -24,6 +24,7 @@ namespace GLib { // Private class and instance members IntPtr _obj; + protected bool needs_ref = true; EventHandlerList _events; bool disposed = false; Hashtable Data; @@ -87,6 +88,24 @@ namespace GLib { g_object_ref (_obj); } + + /// + /// Unref Method + /// + /// + /// + /// Decreases the reference count on the native object. + /// This method is used by generated classes and structs, + /// and should not be used in user code. + /// + protected virtual void Unref () + { + if (_obj == IntPtr.Zero) + return; + + g_object_unref (_obj); + } + /// /// GetObject Shared Method @@ -121,6 +140,7 @@ namespace GLib { /// public Object () { + needs_ref = false; } /// @@ -147,15 +167,20 @@ namespace GLib { /// Handle property. /// - protected IntPtr Raw { + [DllImport("libgobject-2.0.so")] + private static extern string g_type_name (uint gtype); + + protected virtual IntPtr Raw { get { return _obj; } set { + if (needs_ref) + g_object_ref (value); Objects [value] = new WeakReference (this); _obj = value; } - } + } /// /// GType Property @@ -165,6 +190,9 @@ namespace GLib { /// The type associated with this object class. /// + [DllImport("libgtksharpglue.so")] + private static extern uint gtksharp_get_type_id (IntPtr obj); + public static int GType { get { return 0; @@ -290,5 +318,14 @@ namespace GLib { { return gtksharp_is_object (obj); } + + [DllImport("gtksharpglue")] + static extern int gtksharp_object_get_ref_count (IntPtr obj); + + public int RefCount { + get { + return gtksharp_object_get_ref_count (Handle); + } + } } } diff --git a/glue/object.c b/glue/object.c index cecc68644..1050bcdad 100644 --- a/glue/object.c +++ b/glue/object.c @@ -15,3 +15,14 @@ gtksharp_object_unref_if_floating (GObject *obj) g_object_unref (obj); } +gboolean +gtksharp_object_is_floating (GObject *obj) +{ + return GTK_OBJECT_FLOATING (obj); +} + +int +gtksharp_object_get_ref_count (GObject *obj) +{ + return obj->ref_count; +} diff --git a/glue/type.c b/glue/type.c index ec22e49b6..18744f9f2 100644 --- a/glue/type.c +++ b/glue/type.c @@ -1,4 +1,4 @@ -/* value.c : Glue to allocate GValues on the heap. +/* type.c : GType utilities * * Author: Mike Kestner * diff --git a/sources/Gdk.metadata b/sources/Gdk.metadata index ea48390a0..373ab2032 100644 --- a/sources/Gdk.metadata +++ b/sources/Gdk.metadata @@ -121,4 +121,20 @@ + + + + AddAlpha + ScaleSimple + CompositeColorSimple + + + + hidden + 1 + + + + +