summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDanielle Madeley <danielle.madeley@collabora.co.uk>2009-08-25 13:00:32 +1000
committerDanielle Madeley <danielle.madeley@collabora.co.uk>2009-11-18 12:17:16 +1100
commit33f486548e9aa1b22df8c23d1cc451525c7d9988 (patch)
treefee4b358d5cba03862cda38d5bff32261dc3bea5 /examples
parent5306328fed9559f596fbf567f17b939eafe03603 (diff)
downloadclutter-gtk-33f486548e9aa1b22df8c23d1cc451525c7d9988.tar.gz
Refcounting testcase
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am5
-rw-r--r--examples/refcounting-test.c92
2 files changed, 97 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 8cfc2a2..a463e6a 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -11,6 +11,7 @@ noinst_PROGRAMS = \
gtk-clutter-viewport \
gtk-clutter-window-test \
gtk-clutter-window-test2 \
+ refcounting-test \
reparenting-test
INCLUDES = -I$(srcdir) -I$(top_srcdir)
@@ -57,6 +58,10 @@ gtk_clutter_window_test2_SOURCES = gtk-clutter-window-test2.c
gtk_clutter_window_test2_DEPENDENCIES = $(common_deps)
gtk_clutter_window_test2_LDADD = $(common_ldadd)
+refcounting_test_SOURCES = refcounting-test.c
+refcounting_test_DEPENDENCIES = $(common_deps)
+refcounting_test_LDADD = $(common_ldadd)
+
reparenting_test_SOURCES = reparenting-test.c
reparenting_test_DEPENDENCIES = $(common_deps)
reparenting_test_LDADD = $(common_ldadd)
diff --git a/examples/refcounting-test.c b/examples/refcounting-test.c
new file mode 100644
index 0000000..25d9e50
--- /dev/null
+++ b/examples/refcounting-test.c
@@ -0,0 +1,92 @@
+/* Clutter-Gtk Window Test
+ *
+ * (c) 2009, Collabora Ltd.
+ *
+ * Written by Davyd Madeley <davyd.madeley@collabora.co.uk>
+ */
+
+#include <gtk/gtk.h>
+#include <clutter/clutter.h>
+#include <clutter-gtk/clutter-gtk.h>
+
+#include <math.h>
+
+static gulong CLUTTER_COS_SQUARED = CLUTTER_ANIMATION_LAST;
+
+static ClutterActor *container = NULL;
+static ClutterActor *actor = NULL;
+static GtkWidget *bin = NULL;
+static GtkWidget *image = NULL;
+
+static void
+object_finalized (gpointer ptr, GObject *obj)
+{
+ g_print ("Object finalized: %p\n", obj);
+
+ *((GObject **)ptr) = NULL;
+}
+
+#define GET_REF_COUNT(p) (((p) != NULL) ? G_OBJECT ((p))->ref_count : -1)
+
+static void
+add_actor_cb (GtkToggleButton *toggle, gpointer user_data)
+{
+ if (gtk_toggle_button_get_active (toggle))
+ {
+ /* create children */
+ actor = gtk_clutter_actor_new ();
+ bin = gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (actor));
+ image = gtk_image_new_from_file ("redhand.png");
+
+ clutter_container_add_actor (CLUTTER_CONTAINER (container), actor);
+ gtk_container_add (GTK_CONTAINER (bin), image);
+ gtk_widget_show (image);
+
+ g_object_weak_ref (G_OBJECT (actor), object_finalized, &actor);
+ g_object_weak_ref (G_OBJECT (bin), object_finalized, &bin);
+ g_object_weak_ref (G_OBJECT (image), object_finalized, &image);
+ }
+ else
+ {
+ /* destroy children */
+ g_print ("Removing image from parent\n");
+ g_object_ref (image);
+ gtk_container_remove (GTK_CONTAINER (bin), image);
+ g_print ("Destroying actor\n");
+ clutter_actor_destroy (actor);
+ }
+
+ g_print ("\nRef Counts\n");
+ g_print ("----------\n");
+ g_print ("GtkClutterActor (%p): %i\n", actor, GET_REF_COUNT (actor));
+ g_print ("GtkClutterOffscreen (%p): %i\n", bin, GET_REF_COUNT (bin));
+ g_print ("GtkImage (%p): %i\n", image, GET_REF_COUNT (image));
+}
+
+int
+main (int argc, char **argv)
+{
+ gtk_clutter_init (&argc, &argv);
+
+ GtkWidget *window = gtk_clutter_window_new ();
+ GtkWidget *vbox = gtk_vbox_new (FALSE, 6);
+
+ gtk_container_add (GTK_CONTAINER (window), vbox);
+
+ container = clutter_group_new ();
+
+ GtkWidget *standin = gtk_clutter_standin_new (container);
+ gtk_box_pack_start (GTK_BOX (vbox), standin, TRUE, TRUE, 0);
+
+ GtkWidget *toggle = gtk_toggle_button_new_with_label ("Add actor");
+ gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, TRUE, 0);
+
+ g_signal_connect_swapped (window, "destroy",
+ G_CALLBACK (gtk_main_quit), NULL);
+
+ g_signal_connect (toggle, "toggled",
+ G_CALLBACK (add_actor_cb), NULL);
+
+ gtk_widget_show_all (window);
+ gtk_main ();
+}