<!doctype article PUBLIC "-//OASIS//DTD DocBook V3.1//EN" [
]>

<article id="index">

  <artheader>
    <title>Writing Language Bindings for GTK+ 2.0</title>

    <authorgroup>
      <author>
	<firstname>James</firstname>
	<surname>Henstridge</surname>
	<affiliation>
	  <address><email>james@daa.com.au</email></address>
	</affiliation>
      </author>
    </authorgroup>
    <copyright>
      <year>2000</year>
      <holder>James Henstridge</holder>
    </copyright>

    <abstract>
      <para>GTK+ 2.0 is a versatile toolkit for creating graphical
      user interfaces, and is available for the X Window System, the
      Linux Framebuffer console and Microsoft Windows, among others.
      Unlike many other toolkits, GTK+ was designed so that it is
      fairly easy to write bindings to other languages.</para>

      <para>This document is designed to help language binding authors
      understand how to bind GTK+ 2.0</para>
    </abstract>
  </artheader>

  <sect1 id="overview">
    <title>Overview</title>

    <para>GTK+ 2.0 is an object oriented graphical toolkit.  It's
    object model is based on top of the GObject library, which is part
    of glib.  As the object system is implemented in C code,
    it is not constrained by what is provided by the language.</para>

    <para>One of the aims of the GObject system was to make it easy to
    use from a language binding.  Some of the benefits are:</para>

    <itemizedlist>
      <listitem>
	<para>Written in C.  Most scripting languages are written in
	C, and have well defined interfaces for wrapping C code for
	use in the language.  Many compiled languages also have the
	ability to call C functions as well, so this makes binding GTK
	easier</para>
      </listitem>
      <listitem>
	<para>Runtime introspection.  The object model provides access
	to a lot of information about the type system at runtime.
	This is very useful for language bindings.</para>
      </listitem>
      <listitem>
	<para>more to come ...</para>
      </listitem>
    </itemizedlist>

    <sect2>
      <title>The GObject object model</title>

      <para>As you might expect, to write a good GTK language binding,
      a good understanding of GObject is necessary.  There are a
      number of parts of GObject that you will need to look at:</para>

      <itemizedlist>
	<listitem>
	  <para><emphasis>GType</emphasis>: the type registry.  Every
            type used in GTK has (or should have) a type registered
            with the type system.  From integers to enumerations to
            GObjects.  This registry is used for runtime introspection
            at various parts of the binding.</para>
	</listitem>
	<listitem>
	  <para><emphasis>GValue</emphasis>: used for marshalling
	  arbitrary data types to a function.  A GValue is similar to
	  a CORBA::Any (XXXX - is this true?).  It consists of a GType
	  typecode, and type specific data.  The actual value can be
	  read with type specific accessor functions.</para>

	  <para>A language binding will probably need some code to
	  marshal.</para>
	</listitem>
	<listitem>
	  <para><emphasis>GClosure</emphasis>: a data structure
	  representing a function.  It consists of a function and a
	  marshaller that will marshal an array of GValues to a form
	  that can be passed to the function.</para>

	  <para>For a language binding, you will need to implement a
	  new closure type and an appropriate marshaller that can call
	  functions written in the scripting language.</para>

	  <para>GClosures are used to implement signals for GObjects,
	  and are used for various other callbacks in GTK+ (XXXX - at
	  the moment, there aren't any other uses).</para>
	</listitem>
	<listitem>
	  <para><emphasis>GSignal</emphasis>: a notification
	  mechanism.  XXXX - put in description of signals here</para>
	</listitem>
	<listitem>
	  <para><emphasis>GObject</emphasis>: object and class system.
	  These are the objects provided by the gobject library.  A
	  GObject class can be subclassed, can have a number of
	  signals and may implement a number of interfaces.  The
	  programmer can store arbitrary data with a GObject instance,
	  which can be accessed with a string key.</para>

	  <para>All widgets in the GTK+ library are GObjects.  From
	  the point of view of a language binding author,
	  understanding GObjects is vital to writing a good binding.</para>
	</listitem>
	<listitem>
	  <para><emphasis>GInterface</emphasis>: interfaces for
	  GObjects.  These are similar to C++ signatures (XXXX - well,
	  that is what timj said).</para>
	</listitem>
      </itemizedlist>

      <para>Each of these things will be explained in later
      sections.</para>
    </sect2>
  </sect1>

  <sect1 id="types-and-values">
    <title>GTypes and GValues</title>

    <sect2>
      <title>GTypes</title>

      <para>As a language binding author, mostly you will be querying
      the GType system for information, rather than registering new
      types.  One exception would be creating new GObjects subclasses
      from the language the binding is for.  Discussion of GObject
      subclassing will be covered in a later section.</para>

      <para>Another is if you wish to create an equivalent of the GTK+
      1.2 <parameter>GTK_TYPE_FOREIGN</parameter> type.
      <parameter>GTK_TYPE_FOREIGN</parameter> was removed in GTK 2.0,
      in favour of bindings registering their own type.  You will
      probably want to use a boxed type for this purpose, as seen in
      the following example:</para>

      <programlisting lang="C">static gpointer
pyobject_copy(gpointer boxed)
{
    PyObject *obj = (PyObject *)boxed;

    /* we use referencing as our copying semantics */
    Py_INCREF(obj);
    return obj;
}

static void
pyobject_free(gpointer boxed)
{
    PyObject *obj = (PyObject *)boxed;

    Py_DECREF(obj);
}

...
/* in binding initialisation function */
g_boxed_type_register_static("PyObject", pyobject_copy,
pyobject_free);</programlisting>

      <para>A type such as this may be used in the definitions of new
      signals created for new GObjects created by your binding.</para>
    </sect2>

    <sect2>
      <title>GValues</title>

      <para>For a language binding, you will need some way of
      converting between <type>GValue</type>s and the native types in
      the language you are writing the binding for.  You will probably
      need functions for converting to and from <type>GValue</type>s.
      In pygtk, these are <function>pyg_value_from_pyobject</function>
      and <function>pyg_value_as_pyobject</function>:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>int
	    <function>pyg_value_from_pyobject</function></funcdef>
	  <paramdef>GValue *<parameter>value</parameter></paramdef>
	  <paramdef>PyObject *<parameter>obj</parameter></paramdef>
	</funcprototype>
	<funcprototype>
	  <funcdef>PyObject *
	    <function>pyg_value_as_pyobject</function></funcdef>
	  <paramdef>const GValue *<parameter>value</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>The first function
      <function>pyg_value_from_pyobject</function> takes a
      <type>GValue</type> that has had its type set and a PyObject
      which is the python object we wish to marshal into the
      <parameter>value</parameter>.  If the <type>PyObject</type> has
      the wrong type, then an error is is returned.  The second
      function <function>pyg_value_as_pyobject</function> takes a
      <type>GValue</type>, and returns the equivalent python
      object.</para>

      <para>In both of these functions, there is a big if statement
      calling the various <function>G_VALUE_HOLDS_*</function> macros
      to check the type of the value.</para>
    </sect2>
  </sect1>

  <sect1 id="gclosure">
    <title>GClosure</title>

    <para>In GTK 2.0, <type>GClosure</type>s are used to represent
    callbacks supplied by the programmer.  It will generally comprise
    a function of some kind and a marshaller used to call it.  In the
    case of C programs, a closure usually just holds a pointer to a
    function and maybe a data argument.  For a language binding, you
    will need to develop one or more closure types.</para>

    <para>When a <type>GClosure</type> is invoked, its marshal
    function will be called.  As arguments, it will be passed a GValue
    that the return value should be written into, an array of GValues
    representing arguments to the function, a context dependent
    "invocation hint" and an extra data argument:</para>

    <funcsynopsis lang="C">
      <funcprototype>
	<funcdef>void <function>marshal</function></funcdef>
	<paramdef>GClosure *<parameter>closure</parameter></paramdef>
	<paramdef>GValue *<parameter>return_value</parameter></paramdef>
	<paramdef>guint <parameter>n_param_values</parameter></paramdef>
	<paramdef>const GValue *<parameter>param_values</parameter></paramdef>
	<paramdef>gpointer <parameter>invocation_hint</parameter></paramdef>
	<paramdef>gpointer <parameter>marshal_data</parameter></paramdef>
      </funcprototype>
    </funcsynopsis>

    <para>Unlike in GTK 1.2, <type>GClosure</type>s are extensible and
    can hold an arbitrary ammount of information, so you won't have to
    use tricks like passing a pointer to a structure as the user data
    for a callback.</para>

    <para>The most common closure type you will use is one that just
    wraps a function object from the language you are binding as a
    <type>GClosure</type> (for use as a signal handler, etc).  In the
    case of interpreted languages, you will probably be able to write
    a generic closure marshal that will be able to construct arbitrary
    argument vector when invoking the closure.</para>

    <para>The closure marshal would most likely use the
    <type>GVale</type> -&gt; native type routine written earlier to
    convert the <parameter>param_values</parameter> array into an
    argument vector suitable for calling the function wrapped by the
    closure.  After calling the closure, the return value can then be
    converted back to a <type>GValue</type> and stored in
    <parameter>return_value</parameter>.</para>

    <sect2>
      <title>Creating a New GClosure Type</title>

      <para>It is trivial to create a new closure type.  First of all
      you need to decide if you need to store more information in the
      closure than that provided by the <type>GClosure</type>
      structure.  If so, define a structure to hold the extra data,
      with a <type>GClosure</type> as the first member.  Something
      like this:</para>

      <programlisting lang="C">typedef struct _MyClosure MyClosure;
struct _MyClosure {
    GClosure closure;
    /* my extra data goes here */
};</programlisting>

      <para>Note that this structure does not need to be in any header
      files -- it is an implementation detail of your closure.  Now to
      create a closure of this type, use the following
      statement:</para>

      <programlisting lang="C">GClosure *closure;

closure = g_closure_new_simple(sizeof(MyClosure), data);</programlisting>

      <para>The second argument to
      <function>g_closure_new_simple</function> gets put in the data
      member of the <type>GClosure</type> structure.  After creating
      the closure, you can set the extra data members in your custom
      closure any way you want.  If those data members need to be
      finalised when the closure is freed, then you can add a
      finaliser to the closure:</para>

      <programlisting lang="C">static void
my_closure_finalize(gpointer notify_data, GClosure *closure)
{
    MyClosure *myclosure = (MyClosure *)closure;

    /* free the extra data in myclosure */
}

...

g_closure_add_finalize_notifier(closure, notify_data,
                                my_closure_finalize);</programlisting>

      <para>If you don't need to pass any extra data to the finaliser,
      <parameter>notify_data</parameter> can be <type>NULL</type>.
      You can associate a marshal function with the closure with the
      following command:</para>

      <programlisting lang="C">g_closure_set_marshal(closure, my_closure_marshal);</programlisting>

      <para>In the case where you don't need to store any extra data
      with the closure, you can simply pass the size of the
      <type>GClosure</type> structure to
      <function>g_closure_new_simple</function> and not add any
      finalize notifiers.</para>

      <para>For examples of some closure implementations, see the
      <filename>gobjectmodule.c</filename> file in recent pygtk
      snapshots.</para>

    </sect2>

  </sect1>

  <sect1 id="gobject">
    <title>GObject</title>

    <para>The new base class for the object system is now
    <type>GObject</type> (<type>GtkObject</type> is a subclass of
    <type>GObject</type> now).  GObjects are one example of
    instantiable types in the GType type system.  In practice, they
    are the only instantiable types you will use.</para>

    <para>It is important to make the wrapper for this type as good as
    possible -- it will be the most used wrapper in the binding.  The
    better the GObject wrapper, the more natural the binding will
    feel.</para>

    <para>GObjects are reference counted, so your wrapper should hold
    a reference to the object, and release it when the wrapper is
    destroyed.  Manipulation of the reference count is done with the
    following functions:</para>

    <funcsynopsis lang="C">
      <funcprototype>
	<funcdef>gpointer <function>g_object_ref</function></funcdef>
	<paramdef>gpointer <parameter>object</parameter></paramdef>
      </funcprototype>
      <funcprototype>
	<funcdef>void <function>g_object_unref</function></funcdef>
	<paramdef>gpointer <parameter>object</parameter></paramdef>
      </funcprototype>
    </funcsynopsis>

    <para>If you need to associate some data with a GObject, you can
    use the data APIs:</para>

    <funcsynopsis lang="C">
      <funcprototype>
	<funcdef>gpointer <function>g_object_get_data</function></funcdef>
	<paramdef>GObject *<parameter>object</parameter></paramdef>
	<paramdef>const gchar *<parameter>key</parameter></paramdef>
      </funcprototype>
      <funcprototype>
	<funcdef>void <function>g_object_set_data</function></funcdef>
	<paramdef>GObject *<parameter>object</parameter></paramdef>
	<paramdef>const gchar *<parameter>key</parameter></paramdef>
	<paramdef>gpointer <parameter>data</parameter></paramdef>
      </funcprototype>
      <funcprototype>
	<funcdef>void <function>g_object_set_data_full</function></funcdef>
	<paramdef>GObject *<parameter>object</parameter></paramdef>
	<paramdef>const gchar *<parameter>key</parameter></paramdef>
	<paramdef>gpointer <parameter>data</parameter></paramdef>
	<paramdef>GDestroyNotify <parameter>destroy</parameter></paramdef>
      </funcprototype>
    </funcsynopsis>

    <para>If you are writing a binding for an object oriented
    language, you will probably want to make a class heirachy of
    wrappers that correspond to the various GObject subclasses.  The
    functions for each particular GObject type can be mapped to
    methods on the corresponding wrapper.</para>

    <para>At times, you may need to get the type of a particular
    GObject instance.  This can be done with the
    <function>G_OBJECT_TYPE</function> macro, which effectively has
    the following prototype:</para>

    <funcsynopsis lang="C">
      <funcprototype>
	<funcdef>GType <function>G_OBJECT_TYPE</function></funcdef>
	<paramdef>gpointer <parameter>object</parameter></paramdef>
      </funcprototype>
    </funcsynopsis>

    <para>As well as creating the various GObject subclasses with
    their <function>*_new</function> constructors, there is also a
    generic constructor that can be used for any GObject type.  It
    takes the GType of the object as the first argument, followed by
    (parameter name, value) pairs, and closed off with a NULL:</para>

    <funcsynopsis lang="C">
      <funcprototype>
	<funcdef>gpointer <function>g_object_new</function></funcdef>
	<paramdef>const gchar *<parameter>first_property_name</parameter></paramdef>
	<paramdef>...</paramdef>
      </funcprototype>
    </funcsynopsis>

    <para>It's use might be something like this:</para>

    <programlisting lang="C">GtkWidget *widget;

widget = g_object_new(GTK_TYPE_WINDOW,
                      "x", 42,
                      "y", 20,
                      "visible", TRUE,
                      NULL);</programlisting>

    <para>The use of <function>g_object_new</function> is the only way
    to set <type>G_PARAM_CONSTRUCT_ONLY</type> properties on an
    object.  Unfortunately there doesn't seem to be a version of this
    function that doesn't use C varargs at the moment, which limits
    its usefulness to language bindings.</para>

    <sect2>
      <title>Creating GObject Subclasses</title>

      <para>In some cases, you may want to create new GObject
      subclasses from the language binding.  For this, you will want
      to use the <function>g_type_register_static</function>
      function:</para>

      <funcsynopsis>
	<funcprototype>
	  <funcdef>GType <function>g_type_register_static</function></funcdef>
	  <paramdef>GType <parameter>parent</parameter></paramdef>
	  <paramdef>const gchar *<parameter>type_name</parameter></paramdef>
	  <paramdef>const GTypeInfo *<parameter>info</parameter></paramdef>
	  <paramdef>GTypeFlags <parameter>flags</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>The <type>GTypeInfo</type> structure passed has the
      following methods:</para>

      <programlisting lang="C">typedef struct _GTypeInfo GTypeInfo;
struct _GTypeInfo
{
  /* interface types, classed types, instantiated types */
  guint16                class_size;

  GBaseInitFunc          base_init;
  GBaseFinalizeFunc      base_finalize;

  /* classed types, instantiated types */
  GClassInitFunc         class_init;
  GClassFinalizeFunc     class_finalize;
  gconstpointer          class_data;

  /* instantiated types */
  guint16                instance_size;
  guint16                n_preallocs;
  GInstanceInitFunc      instance_init;

  /* value handling */
  const GTypeValueTable *value_table;
};</programlisting>

      <para>In most cases, you will just want to create a new GObject
      with the same size structure as the parent (or maybe with space
      for an additional pointer or two).  The values to put in the
      <parameter>class_size</parameter> and
      <parameter>instance_size</parameter> members can be derived with
      the <function>g_type_query</function> function:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>void <function>g_type_query</function></funcdef>
	  <paramdef>GType <parameter>type</parameter></paramdef>
	  <paramdef>GTypeQuery *<parameter>query</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>Which returns its data in a <type>GTypeQuery</type>
      structure:</para>

      <programlisting lang="C">typedef struct _GTypeQuery GTypeQuery;
struct _GTypeQuery
{
  GType         type;
  const gchar  *type_name;
  guint         class_size;
  guint         instance_size;
};</programlisting>

      <para>The <parameter>base_init</parameter> and
      <parameter>base_finalize</parameter> members can be left NULL.
      The <parameter>class_{init,finalize,data}</parameter> members
      can be left NULL in most cases as well.  For language bindings,
      it is probably easier to just initialise signals and properties
      after registering the type.</para>

      <para>What you use for the <parameter>instance_init</parameter>
      function depends on how new types will be instantiated.  If the
      GObject will be created from a wrapper's constructor, then you
      probably don't want to do anything in the
      <parameter>instance_init</parameter> function, so can leave it
      as NULL.  If you want the wrapper to be created from the
      GObject's constructor, then you can set
      <parameter>instance_init</parameter> to a function that will
      construct the wrapper for the object.  The first case is what
      happens in the python bindings.</para>

      <para>Putting this all together, the code to derive a GType for
      a new subclass of GtkWidget might look something like
      this:</para>

      <programlisting lang="C">GType typecode;
GTypeInfo type_info = {
    0,    /* class_size */

    (GBaseInitFunc) NULL,
    (GBaseFinalizeFunc) NULL,

    (GClassInitFunc) NULL,
    (GClassFinalizeFunc) NULL,
    NULL, /* class_data */

    0,    /* instance_size */
    0,    /* n_preallocs */
    (GInstanceInitFunc) NULL
};
GTypeQuery query;

g_type_query(GTK_TYPE_WIDGET, &amp;query);
type_info.class_size = query.class_size;
type_info.instance_size = query.instance_size;

/* increase the size of the class or instance structs here if necessary */

typecode = g_type_register_static(GTK_TYPE_WIDGET, "NameOfNewWidget",
                                  &amp;type_info, 0);</programlisting>

      <para>It should now be possible to instantiate objects of the
      new type with <function>g_object_new</function>:</para>

      <programlisting lang="C">GtkWidget *widget;

widget = g_object_new(typecode, NULL);</programlisting>

    </sect2>

  </sect1>

  <sect1 id="gsignal">
    <title>GSignal</title>

    <para>The replacement for GtkSignals in
    <filename>libgobject</filename> are GSignals.  As with most things
    in this new library, GSignals are a lot more versatile than
    GtkSignals.  Signals are a general purpose notification framework.
    A type may have one or more signals, each of which may have an
    argument list and return value.  Handlers can then be
    <emphasis>connected</emphasis> to instances of the type.  When the
    signal is <emphasis>emitted</emphasis> on an instance, each of the
    connected handlers will be called.</para>

    <para>Inside <filename>libgobject</filename>, signals are used for
    property change notification.  In GTK+, signals are used for event
    notification and state change notification among other
    things.</para>

    <para>Unlike GTK+ 1.2, instead of directly connecting C functions
    as signal handlers, <type>GClosure</type>s are used in glib 2.0
    (there are convenience wrappers included for the benefit of C
    programmers).</para>

    <sect2>
      <title>Connecting Handlers to GSignals</title>

      <para>For a language binding, you will probably want to use
      either <function>g_signal_connect_closure</function> or
      <function>g_signal_connect_closure_by_id</function>:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>guint <function>g_signal_connect_closure</function></funcdef>
	  <paramdef>gpointer <parameter>instance</parameter></paramdef>
	  <paramdef>const gchar *<parameter>detailed_signal</parameter></paramdef>
	  <paramdef>GClosure *<parameter>closure</parameter></paramdef>
	  <paramdef>gboolean <parameter>after</parameter></paramdef>
	</funcprototype>
	<funcprototype>
	  <funcdef>guint <function>g_signal_connect_closure_by_id</function></funcdef>
	  <paramdef>gpointer <parameter>instance</parameter></paramdef>
	  <paramdef>guint <parameter>signal_id</parameter></paramdef>
	  <paramdef>GQuark <parameter>detail</parameter></paramdef>
	  <paramdef>GClosure *<parameter>closure</parameter></paramdef>
	  <paramdef>gboolean <parameter>after</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>Note that the only difference between the two functions is
      that <function>g_signal_connect_closure</function> converts its
      <parameter>detailed_signal</parameter> argument to
      <parameter>signal_id</parameter> and
      <parameter>detail</parameter> arguments using:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>gboolean <function>g_signal_parse_name</function></funcdef>
	  <paramdef>const gchar *<parameter>detailed_signal</parameter></paramdef>
	  <paramdef>GType <parameter>itype</parameter></paramdef>
	  <paramdef>guint *<parameter>signal_id_p</parameter></paramdef>
	  <paramdef>GQuark *<parameter>detail_p</parameter></paramdef>
	  <paramdef>gboolean <parameter>force_detail_quark</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>The <parameter>detailed_signal</parameter> or
      <parameter>signal_id</parameter> and
      <parameter>detail</parameter> arguments identify which signal to
      call on the instance.  The <parameter>closure</parameter>
      argument says what function to call as a handler for the signal.
      The <parameter>after</parameter> argument says whether the
      handler should be called before or after the <emphasis>class
      closure</emphasis> of the signal (described in the next
      section).</para>

      <para>The closure will be passed all the arguments that should
      be passed to the function it wraps (unlike the
      GtkCallbackMarshal used in GTK+ 1.2, which didn't include the
      actual instance in the arg list).  The
      <parameter>invocation_hint</parameter> argument passed to the
      closure's marshal function will be a pointer to a
      <type>GSignalInvocationHint</type> structure:</para>

      <programlisting lang="C">typedef struct _GSignalInvocationHint GSignalInvocationHint;
struct _GSignalInvocationHint
{
  guint         signal_id;
  GQuark        detail;
  GSignalFlags  run_type;
};</programlisting>

      <para>For those who have used the GTK+ 1.2 signal system, the
      above functions would only seem to replace
      <function>gtk_signal_connect</function> and
      <function>gtk_signal_connect_after</function> -- not
      <function>gtk_signal_connect_object</function> and
      <function>gtk_signal_connect_object_after</function>.  The
      behaviour of the second two functions is handled by the closure
      in the glib 2.0 model.</para>

      <para>To get the <function>*_object</function> behaviour, you
      will need to pass in a closure that swaps the first argument
      passed to the closure and the data argument.  For example, there
      are two constructors for "C Closures" --
      <function>g_cclosure_new</function> and
      <function>g_cclosure_new_swap</function>, which performs the
      argument swapping.  If you want to provide the argument swapping
      behaviour in your language binding, your custom closure type
      must support it.</para>

      <para>To put it all together, the code to connect a function
      from the language you are binding to a signal of a particular
      object may look something like this pseudo C code:</para>

      <programlisting lang="C">static guint
connect_signal(ObjectWrapper *wrapper,
               const gchar *detailed_signal,
               Function *function,
               gboolean swap_data,
               gboolean after)
{
    GObject *instance = get_object_from_wrapper(Wrapper);
    GClosure *closure = create_closure_for_function(function, swap_data);

    return g_signal_connect_closure(instance, detailed_signal, closure, after);
}</programlisting>

    </sect2>

    <sect2>
      <title>Emitting Signals</title>

      <para>Once you have handlers connected to a signal, you need to
      wait for the signal to be <emphasis>emitted</emphasis>.  For
      most of the signals in GTK+, the emission is handled by the
      library.  In some cases though, you may want to emit signals
      from the language you are writing the binding for.  In these
      cases, you will need a wrapper for
      <function>g_signal_emitv</function>:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>void <function>g_signal_emitv</function></funcdef>
	  <paramdef>const GValue *<parameter>instance_and_params</parameter></paramdef>
	  <paramdef>guint <parameter>signal_id</parameter></paramdef>
	  <paramdef>GQuark <parameter>detail</parameter></paramdef>
	  <paramdef>GValue *<parameter>return_value</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>The first argument to <function>g_signal_emitv</function>
      is the argument list for the signal emission.  The first element
      in the array is a GValue for the instance the signal is being
      emitted on.  The rest are any arguments to be passed to the
      signal.  The types of the arguments can be found with the
      <function>g_signal_query</function> function:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>void <function>g_signal_query</function></funcdef>
	  <paramdef>guint <parameter>signal_id</parameter></paramdef>
	  <paramdef>GSignalQuery *<parameter>query</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>This function will fill in a <type>GSignalQuery</type>
      structure passed in as the second argument.  The structure has
      the following members:</para>

      <programlisting lang="C">typedef struct _GSignalQuery GSignalQuery;
struct _GSignalQuery
{
  guint         signal_id;
  const gchar  *signal_name;
  GType         itype;       /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
  GSignalFlags  signal_flags;
  GType         return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
  guint         n_params;
  const GType  *param_types;
};</programlisting>

      <para>Using the param types, you should be able to convert an
      argument vector in the native format of the language binding to
      a GValue array (using the functions for converting from native
      formats to GValues written earlier). </para>

      <para>The <parameter>signal_id</parameter> and
      <parameter>detail</parameter> arguments can be deduced with the
      <function>g_signal_parse_name</function> function.</para>

      <para>The <parameter>return_value</parameter> is a
      <type>GValue</type> to store the return value of the signal
      emission.  It should be initialised before the
      <function>g_signal_emitv</function> with code something like
      this:</para>

      <programlisting lang="C">GValue return_type = { 0, };
GSignalQuery query;

g_signal_query(signal_id, &amp;query);
if (query.return_type != G_TYPE_NONE)
    g_value_init(&amp;return_type, query.return_type);</programlisting>

      <para>Signal emission is wrapped in the
      <function>pygobject_emit</function> function in pygtk.  The
      interface provided to python is a
      <function>GObject.emit</function> method that takes a detailed
      signal argument as the first argument, followed by any arguments
      for the signal.  The method returns the return value of the
      signal (using the function to convert from a GValue to native
      types).</para>

    </sect2>

    <sect2>
      <title>Creating New Signals</title>

      <para>It is easy to add new signals to any instantiable type
      such as a <type>GObject</type>.  This is done with the
      <function>g_signal_newv</function> function:</para>

      <funcsynopsis lang="C">
	<funcprototype>
	  <funcdef>guint <function>g_signal_newv</function></funcdef>
	  <paramdef>const gchar *<parameter>signal_name</parameter></paramdef>
	  <paramdef>GType <parameter>itype</parameter></paramdef>
	  <paramdef>GSignalFlags <parameter>signal_flags</parameter></paramdef>
	  <paramdef>GClosure *<parameter>class_closure</parameter></paramdef>
	  <paramdef>GSignalAccumulator <parameter>accumulator</parameter></paramdef>
	  <paramdef>gpointer <parameter>accu_data</parameter></paramdef>
	  <paramdef>GSignalCMarsahller <parameter>c_marshaller</parameter></paramdef>
	  <paramdef>GType <parameter>return_type</parameter></paramdef>
	  <paramdef>guint <parameter>n_params</parameter></paramdef>
	  <paramdef>GType *<parameter>param_types</parameter></paramdef>
	</funcprototype>
      </funcsynopsis>

      <para>The <parameter>signal_name</parameter> argument is the
      name of the signal being created.  The
      <parameter>itype</parameter> argument is the GType of instances
      this signal should apply to.  The
      <parameter>signal_flags</parameter> gives the flags for the
      signal (whether to run the class closure before or after
      handlers, etc).  The accumulator related arguments can be
      ignored (pass NULL), and so can the
      <parameter>c_marshaller</parameter> argument (it is only needed
      if you intend to connect C handlers to the signal).  The last
      three arguments give the GType's of the return value and
      parameters for the signal.</para>

      <para>The <parameter>class_closure</parameter> argument takes a
      closure that represents the `class handler' for the signal.
      That is, a handler that will always be called when the signal is
      emitted.  Usually, this would be a different type of closure to
      that passed to <function>g_signal_connect_closure</function>.
      Instead of just wrapping a function, it will usually call a
      virtual function of the GObject wrapper (so that the class
      handler can be overidden in subclasses).  The naming convention
      used for the class handler used in PyGTK and Inti is "do_"
      prepended to the signal name (XXXX - what do other bindings
      do?).  It is recommended that other language bindings use
      something similar.</para>

      <para>In the case of bindings for languages with good
      introspective capabilities, you may be able to call a method if
      you know only its name.  Since the name of the class handler is
      derived from the signal name (which can be derived from the
      signal id passed in the invocation hint for the marshaller), you
      can write a class closure marshal function that can handle all
      possible signals.  This aproach is used in the python bindings
      where a single closure is used as the class closure for all
      signals created in python.  See the
      <function>pyg_signal_class_closure_get</function> function in
      <filename>gobjectmodule.c</filename> file in pygtk for
      details.</para>

    </sect2>
  </sect1>

</article>
