File: seed-gtkbuilder.c

package info (click to toggle)
seed 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,900 kB
  • sloc: ansic: 24,336; sh: 11,196; makefile: 773; xml: 187; python: 173
file content (105 lines) | stat: -rw-r--r-- 3,033 bytes parent folder | download
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
/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */

/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed 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.
 * Seed 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 Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */

#include <seed-module.h>

#include <gtk/gtk.h>

typedef struct _builder_ud {
  SeedContext ctx;
  SeedObject obj;
  SeedObject user_data;
} builder_ud;

static void
seed_builder_connect_func (GtkBuilder *builder,
			   GObject *object,
			   const gchar *signal_name,
			   const gchar *handler_name,
			   GObject *connect_object,
			   GConnectFlags flags,
			   gpointer user_data)
{
  SeedContext ctx;
  SeedObject obj, func;
  builder_ud *priv = (builder_ud *)user_data;
  GClosure *closure;

  ctx = priv->ctx;
  obj = priv->obj;

  func = seed_object_get_property (ctx, obj, handler_name);
  if (!seed_value_is_object (ctx, func) || !seed_value_is_function (ctx, func))
    return;

  closure = seed_closure_new (ctx, func, priv->user_data,
                              "signal handler (GtkBuilder)");
  if (connect_object != NULL)
    g_object_watch_closure (connect_object, closure);

  g_signal_connect_closure (object, signal_name, closure, FALSE);
}

SeedValue
seed_gtk_builder_connect_signals(SeedContext ctx,
				 SeedObject function,
				 SeedObject this_object,
				 gsize argument_count,
				 const SeedValue arguments[],
				 SeedException *exception)
{
  builder_ud ud;
  GtkBuilder *b;
  
  CHECK_ARG_COUNT("GtkBuilder.connect_signals", 1);

  if (!seed_value_is_object (ctx, arguments[0]))
    {
      seed_make_exception (ctx, exception, "TypeError",
			   "connect_signals expects one object as the first argument");
      return seed_make_undefined (ctx);
    }

  b = GTK_BUILDER (seed_value_to_object (ctx, this_object, exception));
  ud.ctx = ctx;
  ud.obj = arguments[0];
  if (argument_count == 2)
    ud.user_data = arguments[1];
  else
    ud.user_data = NULL;
  gtk_builder_connect_signals_full(b, seed_builder_connect_func, &ud);

  return seed_make_undefined (ctx);
}

SeedObject
seed_module_init(SeedEngine *eng)
{
  SeedObject gtkbuilder_proto;

  gtkbuilder_proto = seed_simple_evaluate (eng->context,
					   "imports.gi.Gtk.Builder.prototype",
					   NULL);
  seed_create_function (eng->context,
			"connect_signals",
			seed_gtk_builder_connect_signals,
			gtkbuilder_proto);

  return seed_make_object (eng->context, NULL, NULL);
}