diff options
author | Havoc Pennington <hp@pobox.com> | 2000-06-22 15:36:12 +0000 |
---|---|---|
committer | Havoc Pennington <hp@src.gnome.org> | 2000-06-22 15:36:12 +0000 |
commit | 3a422541e314126987c24fc1752f94962fad78d5 (patch) | |
tree | 9692c443f5df4063c64c4b72cc9482cb1b342a4b /gdk-pixbuf | |
parent | 7b6c38f544dd6d55123b1a21d6a45da957ee6396 (diff) | |
download | gdk-pixbuf-3a422541e314126987c24fc1752f94962fad78d5.tar.gz |
Convert GdkPixbuf to GObject, leaving it opaque (i.e. derivation is not
2000-06-21 Havoc Pennington <hp@pobox.com>
* gdk-pixbuf.c: Convert GdkPixbuf to GObject, leaving it opaque
(i.e. derivation is not allowed, and there are no virtual methods
anyway).
(gdk_pixbuf_preinit): Call g_type_init() here.
(gdk_pixbuf_init): Add a user-friendly init function, for users
of standalone gdk-pixbuf
* gdk-pixbuf-animation.c: Convert to GObject, in the same
way GdkPixbufAnimation was done.
* gdk-pixbuf.h: Remove gdk_pixbuf_set_unref_handler() and
gdk_pixbuf_finalize() from API, since these are broken and don't
make sense with new GObject stuff.
2000-06-21 Havoc Pennington <hp@pobox.com>
* gdk/gdkwindow.c (_gdk_window_destroy_hierarchy): Fix bug where
we didn't check window->bg_pixmap != GDK_NO_BG.
* gtk/gdk-pixbuf-loader.c: Change to reflect GObject-ification of
gdk-pixbuf
Diffstat (limited to 'gdk-pixbuf')
-rw-r--r-- | gdk-pixbuf/ChangeLog | 16 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-animation.c | 106 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-data.c | 4 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-io.c | 3 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-loader.c | 4 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-private.h | 35 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf.c | 131 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf.h | 27 | ||||
-rw-r--r-- | gdk-pixbuf/io-gif.c | 4 | ||||
-rw-r--r-- | gdk-pixbuf/test-gdk-pixbuf.c | 4 |
10 files changed, 203 insertions, 131 deletions
diff --git a/gdk-pixbuf/ChangeLog b/gdk-pixbuf/ChangeLog index 5dfaaa848..c014e6b93 100644 --- a/gdk-pixbuf/ChangeLog +++ b/gdk-pixbuf/ChangeLog @@ -1,3 +1,19 @@ +2000-06-21 Havoc Pennington <hp@pobox.com> + + * gdk-pixbuf.c: Convert GdkPixbuf to GObject, leaving it opaque + (i.e. derivation is not allowed, and there are no virtual methods + anyway). + (gdk_pixbuf_preinit): Call g_type_init() here. + (gdk_pixbuf_init): Add a user-friendly init function, for users + of standalone gdk-pixbuf + + * gdk-pixbuf-animation.c: Convert to GObject, in the same + way GdkPixbufAnimation was done. + + * gdk-pixbuf.h: Remove gdk_pixbuf_set_unref_handler() and + gdk_pixbuf_finalize() from API, since these are broken and don't + make sense with new GObject stuff. + Wed Jun 21 16:02:48 2000 Owen Taylor <otaylor@redhat.com> * gdk-pixbuf-io.c (gdk_pixbuf_load_module): Add feature to diff --git a/gdk-pixbuf/gdk-pixbuf-animation.c b/gdk-pixbuf/gdk-pixbuf-animation.c index beb5b2939..fa6108b89 100644 --- a/gdk-pixbuf/gdk-pixbuf-animation.c +++ b/gdk-pixbuf/gdk-pixbuf-animation.c @@ -25,6 +25,68 @@ #include "gdk-pixbuf-io.h" #include "gdk-pixbuf-private.h" +static void gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass); +static void gdk_pixbuf_animation_finalize (GObject *object); + + + +static gpointer parent_class; + +GType +gdk_pixbuf_animation_get_type (void) +{ + static GType object_type = 0; + + if (!object_type) { + static const GTypeInfo object_info = { + sizeof (GdkPixbufAnimationClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) gdk_pixbuf_animation_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (GdkPixbufAnimation), + 0, /* n_preallocs */ + (GInstanceInitFunc) NULL, + }; + + object_type = g_type_register_static (G_TYPE_OBJECT, + "GdkPixbufAnimation", + &object_info); + } + + return object_type; +} + +static void +gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = gdk_pixbuf_animation_finalize; +} + +static void +gdk_pixbuf_animation_finalize (GObject *object) +{ + GdkPixbufAnimation *animation = GDK_PIXBUF_ANIMATION (object); + + GList *l; + GdkPixbufFrame *frame; + + for (l = animation->frames; l; l = l->next) { + frame = l->data; + gdk_pixbuf_unref (frame->pixbuf); + g_free (frame); + } + + g_list_free (animation->frames); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + /** @@ -87,10 +149,8 @@ gdk_pixbuf_animation_new_from_file (const char *filename) pixbuf = (* image_module->load) (f); fclose (f); - if (pixbuf) - g_assert (pixbuf->ref_count > 0); - else - return NULL; + if (pixbuf == NULL) + return NULL; frame = g_new (GdkPixbufFrame, 1); frame->pixbuf = pixbuf; @@ -99,8 +159,8 @@ gdk_pixbuf_animation_new_from_file (const char *filename) frame->delay_time = -1; frame->action = GDK_PIXBUF_FRAME_RETAIN; - animation = g_new0 (GdkPixbufAnimation, 1); - animation->ref_count = 1; + animation = GDK_PIXBUF_ANIMATION (g_type_create_instance (GDK_TYPE_PIXBUF_ANIMATION)); + animation->n_frames = 1; animation->frames = g_list_prepend (NULL, frame); animation->width = gdk_pixbuf_get_width (pixbuf); @@ -118,50 +178,28 @@ gdk_pixbuf_animation_new_from_file (const char *filename) * gdk_pixbuf_animation_ref: * @animation: An animation. * - * Adds a reference to an animation. It must be released afterwards using - * gdk_pixbuf_animation_unref(). + * Adds a reference to an animation. Deprecated; use + * g_object_ref(). The reference must be released afterwards using + * g_object_unref(). * * Return value: The same as the @animation argument. **/ GdkPixbufAnimation * gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation) { - g_return_val_if_fail (animation != NULL, NULL); - g_return_val_if_fail (animation->ref_count > 0, NULL); - - animation->ref_count++; - return animation; + return (GdkPixbufAnimation*) g_object_ref (G_OBJECT (animation)); } /** * gdk_pixbuf_animation_unref: * @animation: An animation. * - * Removes a reference from an animation. It will be destroyed when the - * reference count drops to zero. At that point, all the frames in the - * animation will be freed and their corresponding pixbufs will be unreferenced. + * Removes a reference from an animation. Deprecated; use g_object_unref(). **/ void gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation) { - g_return_if_fail (animation != NULL); - g_return_if_fail (animation->ref_count > 0); - - animation->ref_count--; - - if (animation->ref_count == 0) { - GList *l; - GdkPixbufFrame *frame; - - for (l = animation->frames; l; l = l->next) { - frame = l->data; - gdk_pixbuf_unref (frame->pixbuf); - g_free (frame); - } - - g_list_free (animation->frames); - g_free (animation); - } + g_object_unref (G_OBJECT (animation)); } /** diff --git a/gdk-pixbuf/gdk-pixbuf-data.c b/gdk-pixbuf/gdk-pixbuf-data.c index 1ad122bc1..f03d869ba 100644 --- a/gdk-pixbuf/gdk-pixbuf-data.c +++ b/gdk-pixbuf/gdk-pixbuf-data.c @@ -60,8 +60,8 @@ gdk_pixbuf_new_from_data (const guchar *data, GdkColorspace colorspace, gboolean g_return_val_if_fail (width > 0, NULL); g_return_val_if_fail (height > 0, NULL); - pixbuf = g_new0 (GdkPixbuf, 1); - pixbuf->ref_count = 1; + pixbuf = GDK_PIXBUF (g_type_create_instance (GDK_TYPE_PIXBUF)); + pixbuf->colorspace = colorspace; pixbuf->n_channels = has_alpha ? 4 : 3; pixbuf->bits_per_sample = bits_per_sample; diff --git a/gdk-pixbuf/gdk-pixbuf-io.c b/gdk-pixbuf/gdk-pixbuf-io.c index ca7f980ea..599101039 100644 --- a/gdk-pixbuf/gdk-pixbuf-io.c +++ b/gdk-pixbuf/gdk-pixbuf-io.c @@ -451,9 +451,6 @@ gdk_pixbuf_new_from_file (const char *filename) pixbuf = (* image_module->load) (f); fclose (f); - if (pixbuf) - g_assert (pixbuf->ref_count > 0); - return pixbuf; } diff --git a/gdk-pixbuf/gdk-pixbuf-loader.c b/gdk-pixbuf/gdk-pixbuf-loader.c index 884ce4cc8..9c59a423f 100644 --- a/gdk-pixbuf/gdk-pixbuf-loader.c +++ b/gdk-pixbuf/gdk-pixbuf-loader.c @@ -270,9 +270,9 @@ gdk_pixbuf_loader_frame_done (GdkPixbufFrame *frame, gpointer loader) priv->pixbuf = NULL; if (priv->animation == NULL) { - priv->animation = g_new0 (GdkPixbufAnimation, 1); + priv->animation = GDK_PIXBUF_ANIMATION (g_type_create_instance (GDK_TYPE_PIXBUF_ANIMATION)); + priv->animation->n_frames = 0; - priv->animation->ref_count = 1; priv->animation->width = gdk_pixbuf_get_width (frame->pixbuf) + frame->x_offset; priv->animation->height = gdk_pixbuf_get_height (frame->pixbuf) + frame->y_offset; } else { diff --git a/gdk-pixbuf/gdk-pixbuf-private.h b/gdk-pixbuf/gdk-pixbuf-private.h index 4595785a0..2aecb6623 100644 --- a/gdk-pixbuf/gdk-pixbuf-private.h +++ b/gdk-pixbuf/gdk-pixbuf-private.h @@ -30,10 +30,15 @@ +typedef struct _GdkPixbufClass GdkPixbufClass; + +#define GDK_PIXBUF_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF, GdkPixbufClass)) +#define GDK_IS_PIXBUF_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF)) +#define GDK_PIXBUF_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF, GdkPixbufClass)) + /* Private part of the GdkPixbuf structure */ struct _GdkPixbuf { - /* Reference count */ - int ref_count; + GObject parent_instance; /* Color space */ GdkColorspace colorspace; @@ -59,16 +64,15 @@ struct _GdkPixbuf { /* User data for the destroy notification function */ gpointer destroy_fn_data; - /* Last unref handler, determines whether the pixbuf should be finalized */ - GdkPixbufLastUnref last_unref_fn; - - /* User data for the last unref handler */ - gpointer last_unref_fn_data; - /* Do we have an alpha channel? */ guint has_alpha : 1; }; +struct _GdkPixbufClass { + GObjectClass parent_class; + +}; + /* Private part of the GdkPixbufFrame structure */ struct _GdkPixbufFrame { /* The pixbuf with this frame's image data */ @@ -85,10 +89,15 @@ struct _GdkPixbufFrame { GdkPixbufFrameAction action; }; +typedef struct _GdkPixbufAnimationClass GdkPixbufAnimationClass; + +#define GDK_PIXBUF_ANIMATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_ANIMATION, GdkPixbufAnimationClass)) +#define GDK_IS_PIXBUF_ANIMATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_ANIMATION)) +#define GDK_PIXBUF_ANIMATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_ANIMATION, GdkPixbufAnimationClass)) + /* Private part of the GdkPixbufAnimation structure */ struct _GdkPixbufAnimation { - /* Reference count */ - int ref_count; + GObject parent_instance; /* Number of frames */ int n_frames; @@ -100,6 +109,12 @@ struct _GdkPixbufAnimation { int width, height; }; +struct _GdkPixbufAnimationClass { + GObjectClass parent_class; + + +}; + #endif diff --git a/gdk-pixbuf/gdk-pixbuf.c b/gdk-pixbuf/gdk-pixbuf.c index d42cfd159..50947ba8e 100644 --- a/gdk-pixbuf/gdk-pixbuf.c +++ b/gdk-pixbuf/gdk-pixbuf.c @@ -28,97 +28,86 @@ #include "gdk-pixbuf.h" #include "gdk-pixbuf-private.h" +static void gdk_pixbuf_class_init (GdkPixbufClass *klass); +static void gdk_pixbuf_finalize (GObject *object); + -/* Reference counting */ +static gpointer parent_class; + +GType +gdk_pixbuf_get_type (void) +{ + static GType object_type = 0; + + if (!object_type) { + static const GTypeInfo object_info = { + sizeof (GdkPixbufClass), + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) gdk_pixbuf_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (GdkPixbuf), + 0, /* n_preallocs */ + (GInstanceInitFunc) NULL, + }; + + object_type = g_type_register_static (G_TYPE_OBJECT, + "GdkPixbuf", + &object_info); + } + + return object_type; +} + +static void +gdk_pixbuf_class_init (GdkPixbufClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = gdk_pixbuf_finalize; +} + +static void +gdk_pixbuf_finalize (GObject *object) +{ + GdkPixbuf *pixbuf = GDK_PIXBUF (object); + + if (pixbuf->destroy_fn) + (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} /** * gdk_pixbuf_ref: * @pixbuf: A pixbuf. * - * Adds a reference to a pixbuf. It must be released afterwards using - * gdk_pixbuf_unref(). + * Adds a reference to a pixbuf. Deprecated; use g_object_ref(). * * Return value: The same as the @pixbuf argument. **/ GdkPixbuf * gdk_pixbuf_ref (GdkPixbuf *pixbuf) { - g_return_val_if_fail (pixbuf != NULL, NULL); - g_return_val_if_fail (pixbuf->ref_count > 0, NULL); - - pixbuf->ref_count++; - return pixbuf; + return (GdkPixbuf *) g_object_ref (G_OBJECT(pixbuf)); } /** * gdk_pixbuf_unref: * @pixbuf: A pixbuf. * - * Removes a reference from a pixbuf. If this is the last reference for the - * @pixbuf, then its last unref handler function will be called; if no handler - * has been defined, then the pixbuf will be finalized. + * Removes a reference from a pixbuf. Deprecated; use + * g_object_unref(). * - * See also: gdk_pixbuf_set_last_unref_handler(). **/ void gdk_pixbuf_unref (GdkPixbuf *pixbuf) { - g_return_if_fail (pixbuf != NULL); - g_return_if_fail (pixbuf->ref_count > 0); - - if (pixbuf->ref_count > 1) - pixbuf->ref_count--; - else if (pixbuf->last_unref_fn) - (* pixbuf->last_unref_fn) (pixbuf, pixbuf->last_unref_fn_data); - else - gdk_pixbuf_finalize (pixbuf); -} - -/** - * gdk_pixbuf_set_last_unref_handler: - * @pixbuf: A pixbuf. - * @last_unref_fn: Handler function for the last unref. - * @last_unref_fn_data: Closure data to pass to the last unref handler function. - * - * Sets the handler function for the @pixbuf's last unref handler. When - * gdk_pixbuf_unref() is called on this pixbuf and it has a reference count of - * 1, i.e. its last reference, then the last unref handler will be called. This - * function should determine whether to finalize the pixbuf or just continue. - * If it wishes to finalize the pixbuf, it should do so by calling - * gdk_pixbuf_finalize(). - * - * See also: gdk_pixbuf_finalize(). - **/ -void -gdk_pixbuf_set_last_unref_handler (GdkPixbuf *pixbuf, GdkPixbufLastUnref last_unref_fn, - gpointer last_unref_fn_data) -{ - g_return_if_fail (pixbuf != NULL); - - pixbuf->last_unref_fn = last_unref_fn; - pixbuf->last_unref_fn_data = last_unref_fn_data; -} - -/** - * gdk_pixbuf_finalize: - * @pixbuf: A pixbuf with a reference count of 1. - * - * Finalizes a pixbuf by calling its destroy notification function to free the - * pixel data and freeing the pixbuf itself. This function is meant to be - * called only from within a #GdkPixbufLastUnref handler function, and the - * specified @pixbuf must have a reference count of 1, i.e. its last reference. - **/ -void -gdk_pixbuf_finalize (GdkPixbuf *pixbuf) -{ - g_return_if_fail (pixbuf != NULL); - g_return_if_fail (pixbuf->ref_count == 1); - - if (pixbuf->destroy_fn) - (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data); - - g_free (pixbuf); + g_object_unref (G_OBJECT (pixbuf)); } @@ -357,9 +346,17 @@ const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION; void gdk_pixbuf_preinit (gpointer app, gpointer modinfo) { + g_type_init (); } void gdk_pixbuf_postinit (gpointer app, gpointer modinfo) { } + +void +gdk_pixbuf_init (void) +{ + gdk_pixbuf_preinit (NULL, NULL); + gdk_pixbuf_postinit (NULL, NULL); +} diff --git a/gdk-pixbuf/gdk-pixbuf.h b/gdk-pixbuf/gdk-pixbuf.h index 08087ccce..04756b5b8 100644 --- a/gdk-pixbuf/gdk-pixbuf.h +++ b/gdk-pixbuf/gdk-pixbuf.h @@ -32,6 +32,7 @@ extern "C" { #include <glib.h> #include <gdk-pixbuf/gdk-pixbuf-features.h> +#include <gobject/gobject.h> @@ -42,28 +43,30 @@ typedef enum { /* All of these are opaque structures */ typedef struct _GdkPixbuf GdkPixbuf; -typedef struct _GdkPixbufFrame GdkPixbufFrame; typedef struct _GdkPixbufAnimation GdkPixbufAnimation; +typedef struct _GdkPixbufFrame GdkPixbufFrame; + +#define GDK_TYPE_PIXBUF (gdk_pixbuf_get_type ()) +#define GDK_PIXBUF(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF, GdkPixbuf)) +#define GDK_IS_PIXBUF(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF)) + +#define GDK_TYPE_PIXBUF_ANIMATION (gdk_pixbuf_animation_get_type ()) +#define GDK_PIXBUF_ANIMATION(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_ANIMATION, GdkPixbufAnimation)) +#define GDK_IS_PIXBUF_ANIMATION(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_ANIMATION)) + /* Handler that must free the pixel array */ typedef void (* GdkPixbufDestroyNotify) (guchar *pixels, gpointer data); -/* Handler for the last unref operation */ -typedef void (* GdkPixbufLastUnref) (GdkPixbuf *pixbuf, gpointer data); - +GType gdk_pixbuf_get_type (void); + /* Reference counting */ GdkPixbuf *gdk_pixbuf_ref (GdkPixbuf *pixbuf); void gdk_pixbuf_unref (GdkPixbuf *pixbuf); -void gdk_pixbuf_set_last_unref_handler (GdkPixbuf *pixbuf, - GdkPixbufLastUnref last_unref_fn, - gpointer last_unref_fn_data); - -void gdk_pixbuf_finalize (GdkPixbuf *pixbuf); - /* GdkPixbuf accessors */ GdkColorspace gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf); @@ -192,6 +195,8 @@ typedef enum { GDK_PIXBUF_FRAME_REVERT } GdkPixbufFrameAction; +GType gdk_pixbuf_animation_get_type (void); + GdkPixbufAnimation *gdk_pixbuf_animation_new_from_file (const char *filename); GdkPixbufAnimation *gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation); @@ -214,6 +219,8 @@ GdkPixbufFrameAction gdk_pixbuf_frame_get_action (GdkPixbufFrame *frame); /* General (presently empty) initialization hooks, primarily for gnome-libs */ void gdk_pixbuf_preinit (gpointer app, gpointer modinfo); void gdk_pixbuf_postinit (gpointer app, gpointer modinfo); +/* A more user-friendly init function */ +void gdk_pixbuf_init (void); diff --git a/gdk-pixbuf/io-gif.c b/gdk-pixbuf/io-gif.c index 4f3bc8878..7f4299969 100644 --- a/gdk-pixbuf/io-gif.c +++ b/gdk-pixbuf/io-gif.c @@ -1214,8 +1214,8 @@ gdk_pixbuf__gif_image_load_animation (FILE *file) g_return_val_if_fail (file != NULL, NULL); context = new_context (); - context->animation = g_new (GdkPixbufAnimation, 1); - context->animation->ref_count = 1; + context->animation = GDK_PIXBUF_ANIMATION (g_type_create_instance (GDK_TYPE_PIXBUF_ANIMATION)); + context->animation->n_frames = 0; context->animation->frames = NULL; context->animation->width = 0; diff --git a/gdk-pixbuf/test-gdk-pixbuf.c b/gdk-pixbuf/test-gdk-pixbuf.c index 182703c97..b4110731d 100644 --- a/gdk-pixbuf/test-gdk-pixbuf.c +++ b/gdk-pixbuf/test-gdk-pixbuf.c @@ -226,9 +226,11 @@ int main (int argc, char **argv) { int result; - + result = EXIT_SUCCESS; + gdk_pixbuf_init (); + /* Run some tests. */ if (!simple_composite_test ()) { result = EXIT_FAILURE; |