[gtk/builder-tool-scope: 3/6] buildertool: Add a builder scope implementation




commit c59e21f5005a0f94b1095c7949296ce6d7696593
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Oct 19 12:13:29 2022 -0400

    buildertool: Add a builder scope implementation
    
    This will be used in future commits.

 tools/fake-scope.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/fake-scope.h |   9 +++
 tools/meson.build  |   3 +-
 3 files changed, 224 insertions(+), 1 deletion(-)
---
diff --git a/tools/fake-scope.c b/tools/fake-scope.c
new file mode 100644
index 0000000000..8234d577a8
--- /dev/null
+++ b/tools/fake-scope.c
@@ -0,0 +1,213 @@
+/*  Copyright 2015 Red Hat, Inc.
+ *
+ * GTK+ 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 of the
+ * License, or (at your option) any later version.
+ *
+ * GLib 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 GTK+; see the file COPYING.  If not,
+ * see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Matthias Clasen
+ */
+
+#include "config.h"
+
+#include "fake-scope.h"
+#include "gtk-builder-tool.h"
+
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <glib/gi18n.h>
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
+
+/* {{{ Scope implementation */
+
+struct _FakeScope
+{
+  GtkBuilderCScope parent;
+
+  GPtrArray *types;
+  GPtrArray *callbacks;
+};
+
+static GtkBuilderScopeInterface *parent_scope_iface;
+
+static void
+dummy_cb (void)
+{
+}
+
+static GClosure *
+fake_scope_create_closure (GtkBuilderScope         *scope,
+                           GtkBuilder              *builder,
+                           const char              *function_name,
+                           GtkBuilderClosureFlags   flags,
+                           GObject                 *object,
+                           GError                 **error)
+{
+  FakeScope *self = FAKE_SCOPE (scope);
+  GClosure *closure;
+  gboolean swapped = flags & GTK_BUILDER_CLOSURE_SWAPPED;
+
+  g_ptr_array_add (self->callbacks, g_strdup (function_name));
+
+  if (object == NULL)
+    object = gtk_builder_get_current_object (builder);
+
+  if (object)
+    {
+      if (swapped)
+        closure = g_cclosure_new_object_swap (dummy_cb, object);
+      else
+        closure = g_cclosure_new_object (dummy_cb, object);
+    }
+  else
+    {
+      if (swapped)
+        closure = g_cclosure_new_swap (dummy_cb, NULL, NULL);
+      else
+        closure = g_cclosure_new (dummy_cb, NULL, NULL);
+    }
+
+  return closure;
+}
+
+static GType
+fake_scope_get_type_from_name (GtkBuilderScope *scope,
+                               GtkBuilder      *builder,
+                               const char      *type_name)
+{
+  FakeScope *self = FAKE_SCOPE (scope);
+  GType type;
+
+  type = parent_scope_iface->get_type_from_name (scope, builder, type_name);
+
+  g_ptr_array_add (self->types, g_strdup (type_name));
+
+  return type;
+}
+
+static GType
+fake_scope_get_type_from_function (GtkBuilderScope *scope,
+                                   GtkBuilder      *builder,
+                                   const char      *function_name)
+{
+  FakeScope *self = FAKE_SCOPE (scope);
+  GType type;
+
+  type = parent_scope_iface->get_type_from_function (scope, builder, function_name);
+
+  if (type != G_TYPE_INVALID)
+    g_ptr_array_add (self->types, g_strdup (g_type_name (type)));
+
+  return type;
+}
+
+static void
+fake_scope_scope_init (GtkBuilderScopeInterface *iface)
+{
+  parent_scope_iface = g_type_interface_peek_parent (iface);
+
+  iface->get_type_from_name = fake_scope_get_type_from_name;
+  iface->get_type_from_function = fake_scope_get_type_from_function;
+  iface->create_closure = fake_scope_create_closure;
+}
+
+G_DEFINE_TYPE_WITH_CODE (FakeScope, fake_scope, GTK_TYPE_BUILDER_CSCOPE,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDER_SCOPE,
+                                                fake_scope_scope_init))
+
+static void
+fake_scope_init (FakeScope *scope)
+{
+  scope->types = g_ptr_array_new_with_free_func (g_free);
+  scope->callbacks = g_ptr_array_new_with_free_func (g_free);
+}
+
+static void
+fake_scope_finalize (GObject *object)
+{
+  FakeScope *self = FAKE_SCOPE (object);
+
+  g_ptr_array_unref (self->types);
+  g_ptr_array_unref (self->callbacks);
+
+  G_OBJECT_CLASS (fake_scope_parent_class)->finalize (object);
+}
+
+static void
+fake_scope_class_init (FakeScopeClass *class)
+{
+  G_OBJECT_CLASS (class)->finalize = fake_scope_finalize;
+}
+
+/* }}} */
+/* {{{ API */
+
+FakeScope *
+fake_scope_new (void)
+{
+  return g_object_new (fake_scope_get_type (), NULL);
+}
+
+static int
+cmp_strings (gconstpointer a,
+             gconstpointer b)
+{
+  const char **aa = (const char **)a;
+  const char **bb = (const char **)b;
+
+  return strcmp (*aa, *bb);
+}
+
+static void
+g_ptr_array_unique (GPtrArray    *array,
+                    GCompareFunc  cmp)
+{
+  int i;
+
+  i = 1;
+  while (i < array->len)
+    {
+      gconstpointer *one = g_ptr_array_index (array, i - 1);
+      gconstpointer *two = g_ptr_array_index (array, i);
+
+      if (cmp (&one, &two) == 0)
+        g_ptr_array_remove_index (array, i);
+      else
+        i++;
+    }
+}
+
+GPtrArray *
+fake_scope_get_types (FakeScope *self)
+{
+  g_ptr_array_sort (self->types, cmp_strings);
+  g_ptr_array_unique (self->types, cmp_strings);
+
+  return self->types;
+}
+
+GPtrArray *
+fake_scope_get_callbacks (FakeScope *self)
+{
+  g_ptr_array_sort (self->callbacks, cmp_strings);
+  g_ptr_array_unique (self->callbacks, cmp_strings);
+
+  return self->callbacks;
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/tools/fake-scope.h b/tools/fake-scope.h
new file mode 100644
index 0000000000..05234129b8
--- /dev/null
+++ b/tools/fake-scope.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <gtk.h>
+
+G_DECLARE_FINAL_TYPE (FakeScope, fake_scope, FAKE, SCOPE, GtkBuilderCScope)
+
+FakeScope * fake_scope_new           (void);
+GPtrArray * fake_scope_get_types     (FakeScope *self);
+GPtrArray * fake_scope_get_callbacks (FakeScope *self);
diff --git a/tools/meson.build b/tools/meson.build
index 52afd5433d..0e90d88945 100644
--- a/tools/meson.build
+++ b/tools/meson.build
@@ -29,7 +29,8 @@ gtk_tools = [
                          'gtk-builder-tool-validate.c',
                          'gtk-builder-tool-enumerate.c',
                          'gtk-builder-tool-screenshot.c',
-                         'gtk-builder-tool-preview.c'], [libgtk_dep] ],
+                         'gtk-builder-tool-preview.c',
+                         'fake-scope.c'], [libgtk_dep] ],
   ['gtk4-update-icon-cache', ['updateiconcache.c'] + extra_update_icon_cache_objs, [ libgtk_static_dep ] ],
   ['gtk4-encode-symbolic-svg', ['encodesymbolic.c'], [ libgtk_static_dep ] ],
 ]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]