snapshot: Turn into GObject
authorBenjamin Otte <otte@redhat.com>
Sun, 18 Mar 2018 18:21:33 +0000 (19:21 +0100)
committerBenjamin Otte <otte@redhat.com>
Sun, 18 Mar 2018 18:21:33 +0000 (19:21 +0100)
This makes GdkSnapshot the base class for GtkSnapshot and hopefully
stops confusing bindings.

C code should see no difference to before.

gdk/gdk.h
gdk/gdkpaintable.c
gdk/gdkpaintable.h
gdk/gdksnapshot.c [new file with mode: 0644]
gdk/gdksnapshot.h [new file with mode: 0644]
gdk/gdksnapshotprivate.h [new file with mode: 0644]
gdk/gdktypes.h
gdk/meson.build
gtk/gtksnapshot.c
gtk/gtksnapshot.h
gtk/gtksnapshotprivate.h

index b07a00e19f2b5a5da26763ec8fb1416df259d839..6bb48e004a89bd855b472209e9be1f2fa6891fb8 100644 (file)
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -63,6 +63,7 @@
 #include <gdk/gdkrectangle.h>
 #include <gdk/gdkrgba.h>
 #include <gdk/gdkseat.h>
+#include <gdk/gdksnapshot.h>
 #include <gdk/gdktexture.h>
 #include <gdk/gdktypes.h>
 #include <gdk/gdkvulkancontext.h>
index eb317a82346d023c76072ec58a9c26788daafb64..dd6422654a0486395c99dec87d9c3cb42fb8732e 100644 (file)
@@ -21,6 +21,8 @@
 
 #include "gdkpaintable.h"
 
+#include "gdksnapshotprivate.h"
+
 /**
  * SECTION:paintable
  * @Title: GdkPaintable
index 99e4e83ff9ab30a2e088e13d4b08526365140bb9..d38f0511e5521de637d62c708c00d22fa6529059 100644 (file)
 #error "Only <gdk/gdk.h> can be included directly."
 #endif
 
-#include <cairo.h>
-#include <glib-object.h>
-
+#include <gdk/gdktypes.h>
 #include <gdk/gdkversionmacros.h>
 
 G_BEGIN_DECLS
 
-#define GDK_TYPE_PAINTABLE            (gdk_paintable_get_type ())
-
-typedef struct _GdkSnapshot         GdkSnapshot; /* Forward declaration */
+#define GDK_TYPE_PAINTABLE               (gdk_paintable_get_type ())
 
 GDK_AVAILABLE_IN_ALL
 G_DECLARE_INTERFACE (GdkPaintable, gdk_paintable, GDK, PAINTABLE, GObject)
diff --git a/gdk/gdksnapshot.c b/gdk/gdksnapshot.c
new file mode 100644 (file)
index 0000000..1589428
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#include "config.h"
+
+#include "gdksnapshotprivate.h"
+
+G_DEFINE_ABSTRACT_TYPE (GdkSnapshot, gdk_snapshot, G_TYPE_OBJECT)
+
+static void
+gdk_snapshot_class_init (GdkSnapshotClass *klass)
+{
+}
+
+static void
+gdk_snapshot_init (GdkSnapshot *self)
+{
+}
+
diff --git a/gdk/gdksnapshot.h b/gdk/gdksnapshot.h
new file mode 100644 (file)
index 0000000..5f282f6
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GDK_SNAPSHOT_H__
+#define __GDK_SNAPSHOT_H__
+
+#if !defined (__GDK_H_INSIDE__) && !defined (GDK_COMPILATION)
+#error "Only <gdk/gdk.h> can be included directly."
+#endif
+
+#include <gdk/gdktypes.h>
+#include <gdk/gdkversionmacros.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GdkSnapshotClass       GdkSnapshotClass;
+
+#define GDK_TYPE_SNAPSHOT               (gdk_snapshot_get_type ())
+
+#define GDK_SNAPSHOT(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDK_TYPE_SNAPSHOT, GdkSnapshot))
+#define GDK_IS_SNAPSHOT(obj)            (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDK_TYPE_SNAPSHOT))
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkSnapshot, g_object_unref)
+
+GDK_AVAILABLE_IN_ALL
+GType           gdk_snapshot_get_type   (void) G_GNUC_CONST;
+
+
+G_END_DECLS
+
+#endif /* __GDK_SNAPSHOT_H__ */
diff --git a/gdk/gdksnapshotprivate.h b/gdk/gdksnapshotprivate.h
new file mode 100644 (file)
index 0000000..b7cdc76
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef __GDK_SNAPSHOT_PRIVATE_H__
+#define __GDK_SNAPSHOT_PRIVATE_H__
+
+#include "gdksnapshot.h"
+
+G_BEGIN_DECLS
+
+#define GDK_SNAPSHOT_CLASS(klass)            (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_SNAPSHOT, GdkSnapshotClass))
+#define GDK_IS_SNAPSHOT_CLASS(klass)         (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_SNAPSHOT))
+#define GDK_SNAPSHOT_GET_CLASS(obj)          (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_SNAPSHOT, GdkSnapshotClass))
+
+struct _GdkSnapshot
+{
+  GObject parent_instance;
+};
+
+struct _GdkSnapshotClass {
+  GObjectClass parent_class;
+};
+
+G_END_DECLS
+
+#endif /* __GDK_SNAPSHOT_PRIVATE_H__ */
index 6b2778373d89805f7e61816bbb67d90aba31b22c..2622d0561954189f540c6107e2236fb9b956d29a 100644 (file)
@@ -132,6 +132,7 @@ typedef struct _GdkWindow             GdkWindow;
 typedef struct _GdkKeymap             GdkKeymap;
 typedef struct _GdkAppLaunchContext   GdkAppLaunchContext;
 typedef struct _GdkSeat               GdkSeat;
+typedef struct _GdkSnapshot           GdkSnapshot;
 
 typedef struct _GdkDrawingContext     GdkDrawingContext;
 typedef struct _GdkDrawContext        GdkDrawContext;
index 7808ab20eac88d5cfa89628f56af76b393ccebb9..8f69e1ca24e80112848998d390ca871ba134ec14 100644 (file)
@@ -39,6 +39,7 @@ gdk_public_sources = files([
   'gdkseat.c',
   'gdkseatdefault.c',
   'gdkselection.c',
+  'gdksnapshot.c',
   'gdktexture.c',
   'gdkvulkancontext.c',
   'gdkwindow.c',
@@ -81,6 +82,7 @@ gdk_public_headers = files([
   'gdkrectangle.h',
   'gdkrgba.h',
   'gdkseat.h',
+  'gdksnapshot.h',
   'gdktexture.h',
   'gdktypes.h',
   'gdkvulkancontext.h',
index a74dba038b31d8c42e3243554ee5b951a998c4f5..c9ebf0b5a0dd6b1af6946c4d01ee35525283ff26 100644 (file)
  * use gtk_snapshot_new().
  */
 
-G_DEFINE_BOXED_TYPE (GtkSnapshot, gtk_snapshot, gtk_snapshot_ref, gtk_snapshot_unref)
+G_DEFINE_TYPE (GtkSnapshot, gtk_snapshot, GDK_TYPE_SNAPSHOT)
 
-/**
- * gtk_snapshot_ref:
- * @snapshot: a #GtkSnapshot
- *
- * Increase the reference count of @snapshot by 1.
- *
- * Returns: the @snapshot
- */
-GtkSnapshot *
-gtk_snapshot_ref (GtkSnapshot *snapshot)
-{
-  g_assert (snapshot->ref_count > 0);
-
-  snapshot->ref_count += 1;
-
-  return snapshot;
-}
-
-/**
- * gtk_snapshot_unref:
- * @snapshot: a #GtkSnapshot
- *
- * Decrease the reference count of @snapshot by 1 and
- * free the object if the count drops to 0.
- */
-void
-gtk_snapshot_unref (GtkSnapshot *snapshot)
+static void
+gtk_snapshot_dispose (GObject *object)
 {
-  g_assert (snapshot->ref_count > 0);
-
-  snapshot->ref_count -= 1;
-
-  if (snapshot->ref_count > 0)
-    return;
+  GtkSnapshot *snapshot = GTK_SNAPSHOT (object);
 
   if (snapshot->state_stack)
     gsk_render_node_unref (gtk_snapshot_to_node (snapshot));
@@ -94,7 +64,20 @@ gtk_snapshot_unref (GtkSnapshot *snapshot)
   g_assert (snapshot->state_stack == NULL);
   g_assert (snapshot->nodes == NULL);
 
-  g_free (snapshot);
+  G_OBJECT_CLASS (gtk_snapshot_parent_class)->dispose (object);
+}
+
+static void
+gtk_snapshot_class_init (GtkSnapshotClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->dispose = gtk_snapshot_dispose;
+}
+
+static void
+gtk_snapshot_init (GtkSnapshot *self)
+{
 }
 
 static GskRenderNode *
@@ -205,8 +188,7 @@ gtk_snapshot_new (GskRenderer          *renderer,
   else
     str = NULL;
 
-  snapshot = g_new (GtkSnapshot, 1);
-  snapshot->ref_count = 1;
+  snapshot = g_object_new (GTK_TYPE_SNAPSHOT, NULL);
 
   snapshot->record_names = record_names;
   snapshot->renderer = renderer;
@@ -224,8 +206,8 @@ gtk_snapshot_new (GskRenderer          *renderer,
 }
 
 /**
- * gtk_snapshot_free_to_node:
- * @snapshot: a #GtkSnapshot
+ * gtk_snapshot_free_to_node: (skip)
+ * @snapshot: (transfer full): a #GtkSnapshot
  *
  * Returns the node that was constructed by @snapshot
  * and frees @snapshot.
@@ -238,7 +220,7 @@ gtk_snapshot_free_to_node (GtkSnapshot *snapshot)
   GskRenderNode *result;
 
   result = gtk_snapshot_to_node (snapshot);
-  gtk_snapshot_unref (snapshot);
+  g_object_unref (snapshot);
 
   return result;
 }
index f908a3dcdfd0bfad38041b5b0d1cf93ae2e0f00e..e9852f8b96a314e65598874bbbbe2c3e4232cdae 100644 (file)
 
 G_BEGIN_DECLS
 
+typedef GdkSnapshot                    GtkSnapshot;
+typedef struct _GtkSnapshotClass       GtkSnapshotClass;
+
+#define GTK_TYPE_SNAPSHOT               (gtk_snapshot_get_type ())
+
+#define GTK_SNAPSHOT(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SNAPSHOT, GtkSnapshot))
+#define GTK_IS_SNAPSHOT(obj)            (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SNAPSHOT))
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkSnapshot, g_object_unref)
+
+
 
 GDK_AVAILABLE_IN_ALL
 GType           gtk_snapshot_get_type                   (void) G_GNUC_CONST;
-GDK_AVAILABLE_IN_ALL
-GtkSnapshot *   gtk_snapshot_ref                        (GtkSnapshot            *snapshot);
-GDK_AVAILABLE_IN_ALL
-void            gtk_snapshot_unref                      (GtkSnapshot            *snapshot);
 
 GDK_AVAILABLE_IN_ALL
 GtkSnapshot *   gtk_snapshot_new                        (GskRenderer            *renderer,
index 370784e27df2e1e6ab63b5276f5b2c171db9d3db..d6e534deef93dc6670a30f8c237f2c285713a931 100644 (file)
@@ -80,14 +80,24 @@ struct _GtkSnapshotState {
   } data;
 };
 
+/* This is a nasty little hack. We typedef GtkSnapshot to the fake object GdkSnapshot
+ * so that we don't need to typecast between them.
+ * After all, the GdkSnapshot only exist so poor language bindings don't trip. Hardcore
+ * C code can just blatantly ignore such layering violations with a typedef.
+ */
 struct _GdkSnapshot {
-  int ref_count;
+  GObject                parent_instance; /* it's really GdkSnapshot, but don't tell anyone! */
+
   gboolean               record_names;
   GskRenderer           *renderer;
   GArray                *state_stack;
   GPtrArray             *nodes;
 };
 
+struct _GtkSnapshotClass {
+  GObjectClass           parent_class; /* it's really GdkSnapshotClass, but don't tell anyone! */
+};
+
 G_END_DECLS
 
 #endif /* __GTK_SNAPSHOT_PRIVATE_H__ */