summaryrefslogtreecommitdiff
path: root/tools/fake-scope.c
blob: 8234d577a8ba1b5dd2f236c0dcf801be9d2de9ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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: */