Blame docs/reference/gobject/tut_gsignal.xml

Packit ae235b
Packit ae235b
Packit ae235b
               "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
Packit ae235b
]>
Packit ae235b
<chapter id="chapter-signal">
Packit ae235b
  <title>The GObject messaging system</title>
Packit ae235b
Packit ae235b
  <sect1 id="closure">
Packit ae235b
    <title>Closures</title>
Packit ae235b
Packit ae235b
    <para>
Packit ae235b
      Closures are central to the concept of asynchronous signal delivery
Packit ae235b
      which is widely used throughout GTK+ and GNOME applications. A closure is an 
Packit ae235b
      abstraction, a generic representation of a callback. It is a small structure
Packit ae235b
      which contains three objects:
Packit ae235b
      <itemizedlist>
Packit ae235b
        <listitem><para>a function pointer (the callback itself) whose prototype looks like:
Packit ae235b
<informalexample><programlisting>
Packit ae235b
return_type function_callback (… , gpointer user_data);
Packit ae235b
</programlisting></informalexample>
Packit ae235b
        </para></listitem>
Packit ae235b
        <listitem><para>
Packit ae235b
           the <parameter>user_data</parameter> pointer which is passed to the callback upon invocation of the closure
Packit ae235b
          </para></listitem>
Packit ae235b
        <listitem><para>
Packit ae235b
           a function pointer which represents the destructor of the closure: whenever the
Packit ae235b
           closure's refcount reaches zero, this function will be called before the closure
Packit ae235b
           structure is freed.
Packit ae235b
          </para></listitem>
Packit ae235b
      </itemizedlist>
Packit ae235b
    </para>
Packit ae235b
Packit ae235b
    <para>
Packit ae235b
      The <link linkend="GClosure"><type>GClosure</type></link> structure represents the common functionality of all
Packit ae235b
      closure implementations: there exists a different closure implementation for
Packit ae235b
      each separate runtime which wants to use the GObject type system.
Packit ae235b
      <footnote><para>
Packit ae235b
        In practice, closures sit at the boundary of language runtimes: if you are
Packit ae235b
        writing Python code and one of your Python callbacks receives a signal from
Packit ae235b
        a GTK+ widget, the C code in GTK+ needs to execute your Python
Packit ae235b
        code. The closure invoked by the GTK+ object invokes the Python callback:
Packit ae235b
        it behaves as a normal C object for GTK+ and as a normal Python object for
Packit ae235b
        Python code.
Packit ae235b
      </para></footnote>
Packit ae235b
      The GObject library provides a simple <link linkend="GCClosure"><type>GCClosure</type></link> type which
Packit ae235b
      is a specific implementation of closures to be used with C/C++ callbacks.
Packit ae235b
    </para>
Packit ae235b
    <para>
Packit ae235b
      A <link linkend="GClosure"><type>GClosure</type></link> provides simple services:
Packit ae235b
      <itemizedlist>
Packit ae235b
        <listitem><para>
Packit ae235b
          Invocation (<function><link linkend="g-closure-invoke">g_closure_invoke</link></function>): this is what closures 
Packit ae235b
          were created for: they hide the details of callback invocation from the
Packit ae235b
          callback invoker.</para>
Packit ae235b
        </listitem>
Packit ae235b
        <listitem><para>
Packit ae235b
          Notification: the closure notifies listeners of certain events such as
Packit ae235b
          closure invocation, closure invalidation and closure finalization. Listeners
Packit ae235b
          can be registered with <function><link linkend="g-closure-add-finalize-notifier">g_closure_add_finalize_notifier</link></function>
Packit ae235b
          (finalization notification), <function><link linkend="g-closure-add-invalidate-notifier">g_closure_add_invalidate_notifier</link></function> 
Packit ae235b
          (invalidation notification) and 
Packit ae235b
          <function><link linkend="g-closure-add-marshal-guards">g_closure_add_marshal_guards</link></function> (invocation notification).
Packit ae235b
          There exist symmetric deregistration functions for finalization and invalidation
Packit ae235b
          events (<function><link linkend="g-closure-remove-finalize-notifier">g_closure_remove_finalize_notifier</link></function> and
Packit ae235b
          <function><link linkend="g-closure-remove-invalidate-notifier">g_closure_remove_invalidate_notifier</link></function>) but not for the invocation 
Packit ae235b
          process.
Packit ae235b
          <footnote><para>
Packit ae235b
            Closures are reference counted and notify listeners of their destruction in a two-stage
Packit ae235b
            process: the invalidation notifiers are invoked before the finalization notifiers.
Packit ae235b
          </para></footnote></para>
Packit ae235b
        </listitem>
Packit ae235b
      </itemizedlist>
Packit ae235b
    </para>
Packit ae235b
Packit ae235b
    <sect2>
Packit ae235b
      <title>C Closures</title>
Packit ae235b
Packit ae235b
      <para>
Packit ae235b
        If you are using C or C++
Packit ae235b
        to connect a callback to a given event, you will either use simple <link linkend="GCClosure"><type>GCClosure</type></link>s
Packit ae235b
        which have a pretty minimal API or the even simpler <function><link linkend="g-signal-connect">g_signal_connect</link></function> 
Packit ae235b
        functions (which will be presented a bit later).
Packit ae235b
      </para>
Packit ae235b
Packit ae235b
      <para>
Packit ae235b
        <function><link linkend="g-cclosure-new">g_cclosure_new</link></function> will create a new closure which can invoke the
Packit ae235b
        user-provided callback_func with the user-provided
Packit ae235b
        <parameter>user_data</parameter> as its last parameter. When the closure
Packit ae235b
        is finalized (second stage of the destruction process), it will invoke
Packit ae235b
        the <parameter>destroy_data</parameter> function if the user has
Packit ae235b
        supplied one.
Packit ae235b
      </para>
Packit ae235b
  
Packit ae235b
      <para>
Packit ae235b
        <function><link linkend="g-cclosure-new-swap">g_cclosure_new_swap</link></function> will create a new closure which can invoke the
Packit ae235b
        user-provided <parameter>callback_func</parameter> with the
Packit ae235b
        user-provided <parameter>user_data</parameter> as its first parameter
Packit ae235b
        (instead of being the 
Packit ae235b
        last parameter as with <function><link linkend="g-cclosure-new">g_cclosure_new</link></function>). When the closure
Packit ae235b
        is finalized (second stage of the destruction process), it will invoke
Packit ae235b
        the <parameter>destroy_data</parameter> function if the user has
Packit ae235b
        supplied one.
Packit ae235b
      </para>
Packit ae235b
    </sect2>
Packit ae235b
Packit ae235b
    <sect2>
Packit ae235b
      <title>Non-C closures (for the fearless)</title>
Packit ae235b
Packit ae235b
      <para>
Packit ae235b
        As was explained above, closures hide the details of callback invocation. In C,
Packit ae235b
        callback invocation is just like function invocation: it is a matter of creating
Packit ae235b
        the correct stack frame for the called function and executing a <emphasis>call</emphasis>
Packit ae235b
        assembly instruction.
Packit ae235b
      </para>
Packit ae235b
  
Packit ae235b
      <para>
Packit ae235b
        C closure marshallers transform the array of GValues which represent 
Packit ae235b
        the parameters to the target function into a C-style function parameter list, invoke
Packit ae235b
        the user-supplied C function with this new parameter list, get the return value of the
Packit ae235b
        function, transform it into a GValue and return this GValue to the marshaller caller.
Packit ae235b
      </para>
Packit ae235b
Packit ae235b
      <para>
Packit ae235b
        A generic C closure marshaller is available as
Packit ae235b
        <link linkend="g-cclosure-marshal-generic"><function>g_cclosure_marshal_generic</function></link>
Packit ae235b
        which implements marshalling for all function types using libffi. Custom
Packit ae235b
        marshallers for different types are not needed apart from performance
Packit ae235b
        critical code where the libffi-based marshaller may be too slow.
Packit ae235b
      </para>
Packit ae235b
Packit ae235b
      <para>
Packit ae235b
        An example of a custom marshaller is given below, illustrating how
Packit ae235b
        <type>GValue</type>s can be converted to a C function call. The
Packit ae235b
        marshaller is for a C function which takes an integer as its first
Packit ae235b
        parameter and returns void.
Packit ae235b
<informalexample><programlisting>
Packit ae235b
g_cclosure_marshal_VOID__INT (GClosure     *closure,
Packit ae235b
                              GValue       *return_value,
Packit ae235b
                              guint         n_param_values,
Packit ae235b
                              const GValue *param_values,
Packit ae235b
                              gpointer      invocation_hint,
Packit ae235b
                              gpointer      marshal_data)
Packit ae235b
{
Packit ae235b
  typedef void (*GMarshalFunc_VOID__INT) (gpointer     data1,
Packit ae235b
                                          gint         arg_1,
Packit ae235b
                                          gpointer     data2);
Packit ae235b
  register GMarshalFunc_VOID__INT callback;
Packit ae235b
  register GCClosure *cc = (GCClosure*) closure;
Packit ae235b
  register gpointer data1, data2;
Packit ae235b
Packit ae235b
  g_return_if_fail (n_param_values == 2);
Packit ae235b
Packit ae235b
  data1 = g_value_peek_pointer (param_values + 0);
Packit ae235b
  data2 = closure->data;
Packit ae235b
Packit ae235b
  callback = (GMarshalFunc_VOID__INT) (marshal_data ? marshal_data : cc->callback);
Packit ae235b
Packit ae235b
  callback (data1,
Packit ae235b
            g_marshal_value_peek_int (param_values + 1),
Packit ae235b
            data2);
Packit ae235b
}
Packit ae235b
</programlisting></informalexample>
Packit ae235b
      </para>
Packit ae235b
  
Packit ae235b
      <para>
Packit ae235b
        There exist other kinds of marshallers, for example there is a generic
Packit ae235b
        Python marshaller which is used by all Python closures (a Python closure
Packit ae235b
        is used to invoke a callback written in Python). This Python marshaller
Packit ae235b
        transforms the input GValue list representing the function parameters
Packit ae235b
        into a Python tuple which is the equivalent structure in Python.
Packit ae235b
      </para>
Packit ae235b
Packit ae235b
    </sect2>
Packit ae235b
  </sect1>
Packit ae235b
Packit ae235b
  <sect1 id="signal">
Packit ae235b
    <title>Signals</title>
Packit ae235b
Packit ae235b
    <para>
Packit ae235b
      GObject's signals have nothing to do with standard UNIX signals: they connect 
Packit ae235b
      arbitrary application-specific events with any number of listeners.
Packit ae235b
      For example, in GTK+, every user event (keystroke or mouse move) is received
Packit ae235b
      from the windowing system and generates a GTK+ event in the form of a signal emission
Packit ae235b
      on the widget object instance.
Packit ae235b
    </para>
Packit ae235b
Packit ae235b
    <para>
Packit ae235b
      Each signal is registered in the type system together with the type on which
Packit ae235b
      it can be emitted: users of the type are said to <emphasis>connect</emphasis>
Packit ae235b
      to the signal on a given type instance when they register a closure to be 
Packit ae235b
      invoked upon the signal emission. Users can also emit the signal by themselves 
Packit ae235b
      or stop the emission of the signal from within one of the closures connected 
Packit ae235b
      to the signal.
Packit ae235b
    </para>
Packit ae235b
Packit ae235b
    <para>
Packit ae235b
      When a signal is emitted on a given type instance, all the closures
Packit ae235b
      connected to this signal on this type instance will be invoked. All the closures
Packit ae235b
      connected to such a signal represent callbacks whose signature looks like:
Packit ae235b
<informalexample><programlisting>
Packit ae235b
return_type function_callback (gpointer instance, …, gpointer user_data);
Packit ae235b
</programlisting></informalexample>
Packit ae235b
    </para>
Packit ae235b
Packit ae235b
    <sect2 id="signal-registration">
Packit ae235b
      <title>Signal registration</title>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		To register a new signal on an existing type, we can use any of <function><link linkend="g-signal-newv">g_signal_newv</link></function>,
Packit ae235b
		<function><link linkend="g-signal-new-valist">g_signal_new_valist</link></function> or <function><link linkend="g-signal-new">g_signal_new</link></function> functions:
Packit ae235b
<informalexample><programlisting>
Packit ae235b
guint g_signal_newv (const gchar        *signal_name,
Packit ae235b
                     GType               itype,
Packit ae235b
                     GSignalFlags        signal_flags,
Packit ae235b
                     GClosure           *class_closure,
Packit ae235b
                     GSignalAccumulator  accumulator,
Packit ae235b
                     gpointer            accu_data,
Packit ae235b
                     GSignalCMarshaller  c_marshaller,
Packit ae235b
                     GType               return_type,
Packit ae235b
                     guint               n_params,
Packit ae235b
                     GType              *param_types);
Packit ae235b
</programlisting></informalexample>
Packit ae235b
		The number of parameters to these functions is a bit intimidating but they are relatively
Packit ae235b
		simple:
Packit ae235b
		<itemizedlist>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>signal_name</parameter>: is a string which can be used to uniquely identify a given signal.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>itype</parameter>: is the instance type on which this signal can be emitted.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>signal_flags</parameter>: partly defines the order in which closures which were connected to the
Packit ae235b
			  signal are invoked.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>class_closure</parameter>: this is the default closure for the signal: if it is not NULL upon
Packit ae235b
			  the signal emission, it will be invoked upon this emission of the signal. The 
Packit ae235b
			  moment where this closure is invoked compared to other closures connected to that 
Packit ae235b
			  signal depends partly on the signal_flags.
Packit ae235b
			</para></listitem>
Packit ae235b
			<listitem><para>
Packit ae235b
			  <parameter>accumulator</parameter>: this is a function pointer which is invoked after each closure
Packit ae235b
			  has been invoked. If it returns FALSE, signal emission is stopped. If it returns
Packit ae235b
			  TRUE, signal emission proceeds normally. It is also used to compute the return
Packit ae235b
			  value of the signal based on the return value of all the invoked closures.
Packit ae235b
			  For example, an accumulator could ignore
Packit ae235b
			  <literal>NULL</literal> returns from closures; or it
Packit ae235b
			  could build a list of the values returned by the
Packit ae235b
			  closures.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>accu_data</parameter>: this pointer will be passed down to each invocation of the
Packit ae235b
			  accumulator during emission.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>c_marshaller</parameter>: this is the default C marshaller for any closure which is connected to
Packit ae235b
			this signal.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>return_type</parameter>: this is the type of the return value of the signal.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>n_params</parameter>: this is the number of parameters this signal takes.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			  <parameter>param_types</parameter>: this is an array of GTypes which indicate the type of each parameter
Packit ae235b
			  of the signal. The length of this array is indicated by n_params.
Packit ae235b
			</para></listitem>
Packit ae235b
		</itemizedlist>
Packit ae235b
      </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		As you can see from the above definition, a signal is basically a description
Packit ae235b
		of the closures which can be connected to this signal and a description of the
Packit ae235b
		order in which the closures connected to this signal will be invoked.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	</sect2>
Packit ae235b
Packit ae235b
	<sect2 id="signal-connection">
Packit ae235b
	  <title>Signal connection</title>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		If you want to connect to a signal with a closure, you have three possibilities:
Packit ae235b
		<itemizedlist>
Packit ae235b
		  <listitem><para>
Packit ae235b
		  You can register a class closure at signal registration: this is a
Packit ae235b
		  system-wide operation. i.e.: the class closure will be invoked during each emission
Packit ae235b
		  of a given signal on <emphasis>any</emphasis> of the instances of the type which supports that signal.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
		  You can use <function><link linkend="g-signal-override-class-closure">g_signal_override_class_closure</link></function> which
Packit ae235b
		  overrides the class closure of a given type. It is possible to call this function
Packit ae235b
		  only on a derived type of the type on which the signal was registered.
Packit ae235b
		  This function is of use only to language bindings.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
		  You can register a closure with the <function><link linkend="g-signal-connect">g_signal_connect</link></function>
Packit ae235b
		  family of functions. This is an instance-specific operation: the closure
Packit ae235b
		  will be invoked only during emission of a given signal on a given instance.
Packit ae235b
			</para></listitem>
Packit ae235b
		</itemizedlist>
Packit ae235b
		It is also possible to connect a different kind of callback on a given signal: 
Packit ae235b
		emission hooks are invoked whenever a given signal is emitted whatever the instance on 
Packit ae235b
		which it is emitted. Emission hooks are used for example to get all mouse_clicked
Packit ae235b
		emissions in an application to be able to emit the small mouse click sound.
Packit ae235b
		Emission hooks are connected with <function><link linkend="g-signal-add-emission-hook">g_signal_add_emission_hook</link></function>
Packit ae235b
		and removed with <function><link linkend="g-signal-remove-emission-hook">g_signal_remove_emission_hook</link></function>.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	</sect2>
Packit ae235b
Packit ae235b
	<sect2 id="signal-emission">
Packit ae235b
	  <title>Signal emission</title>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		Signal emission is done through the use of the <function><link linkend="g-signal-emit">g_signal_emit</link></function> family 
Packit ae235b
		of functions.
Packit ae235b
<informalexample><programlisting>
Packit ae235b
void g_signal_emitv (const GValue *instance_and_params,
Packit ae235b
                     guint         signal_id,
Packit ae235b
                     GQuark        detail,
Packit ae235b
                     GValue       *return_value);
Packit ae235b
</programlisting></informalexample>
Packit ae235b
		<itemizedlist>
Packit ae235b
		  <listitem><para>
Packit ae235b
			The <parameter>instance_and_params</parameter> array of GValues contains the list of input
Packit ae235b
			parameters to the signal. The first element of the array is the 
Packit ae235b
			instance pointer on which to invoke the signal. The following elements of
Packit ae235b
			the array contain the list of parameters to the signal.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<parameter>signal_id</parameter> identifies the signal to invoke.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<parameter>detail</parameter> identifies the specific detail of the signal to invoke. A detail is a kind of 
Packit ae235b
			magic token/argument which is passed around during signal emission and which is used
Packit ae235b
			by closures connected to the signal to filter out unwanted signal emissions. In most 
Packit ae235b
			cases, you can safely set this value to zero. See <xref linkend="signal-detail"/> for
Packit ae235b
			more details about this parameter.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<parameter>return_value</parameter> holds the return value of the last closure invoked during emission if
Packit ae235b
			no accumulator was specified. If an accumulator was specified during signal creation,
Packit ae235b
			this accumulator is used to calculate the return value as a function of the return
Packit ae235b
			values of all the closures invoked during emission. 
Packit ae235b
			If no closure is invoked during
Packit ae235b
			emission, the <parameter>return_value</parameter> is nonetheless initialized to zero/null.
Packit ae235b
			</para></listitem>
Packit ae235b
		  </itemizedlist>
Packit ae235b
		</para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		Signal emission can be decomposed in 5 steps:
Packit ae235b
		<orderedlist>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<literal>RUN_FIRST</literal>: if the
Packit ae235b
			<link linkend="G-SIGNAL-RUN-FIRST:CAPS"><literal>G_SIGNAL_RUN_FIRST</literal></link> flag was used
Packit ae235b
			during signal registration and if there exists a class closure for this signal,
Packit ae235b
			the class closure is invoked.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<literal>EMISSION_HOOK</literal>: if any emission hook was added to
Packit ae235b
			the signal, they are invoked from first to last added. Accumulate return values.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<literal>HANDLER_RUN_FIRST</literal>: if any closure were connected
Packit ae235b
			with the <function><link linkend="g-signal-connect">g_signal_connect</link></function> family of 
Packit ae235b
			functions, and if they are not blocked (with the <function><link linkend="g-signal-handler-block">g_signal_handler_block</link></function>
Packit ae235b
			family of functions) they are run here, from first to last connected.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<literal>RUN_LAST</literal>: if the <literal>G_SIGNAL_RUN_LAST</literal>
Packit ae235b
			flag was set during registration and if a class closure
Packit ae235b
			was set, it is invoked here.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<literal>HANDLER_RUN_LAST</literal>: if any closure were connected
Packit ae235b
			with the <function>g_signal_connect_after</function> family of 
Packit ae235b
			functions, if they were not invoked during <literal>HANDLER_RUN_FIRST</literal> and if they 
Packit ae235b
			are not blocked, they are run here, from first to last connected.
Packit ae235b
			</para></listitem>
Packit ae235b
		  <listitem><para>
Packit ae235b
			<literal>RUN_CLEANUP</literal>: if the <literal>G_SIGNAL_RUN_CLEANUP</literal> flag
Packit ae235b
			was set during registration and if a class closure was set,
Packit ae235b
			it is invoked here. Signal emission is completed here.
Packit ae235b
			</para></listitem>
Packit ae235b
		</orderedlist>
Packit ae235b
	  </para>
Packit ae235b
  
Packit ae235b
	  <para>
Packit ae235b
		If, at any point during emission (except in <literal>RUN_CLEANUP</literal> or
Packit ae235b
		<literal>EMISSION_HOOK</literal> state), one of the closures stops the signal emission with
Packit ae235b
		<function><link linkend="g-signal-stop-emission">g_signal_stop_emission</link></function>,
Packit ae235b
		emission jumps to <literal>RUN_CLEANUP</literal> state.
Packit ae235b
	  </para>
Packit ae235b
  
Packit ae235b
	  <para>
Packit ae235b
		If, at any point during emission, one of the closures or emission hook 
Packit ae235b
		emits the same signal on the same instance, emission is restarted from
Packit ae235b
		the <literal>RUN_FIRST</literal> state.
Packit ae235b
	  </para>
Packit ae235b
  
Packit ae235b
	  <para>
Packit ae235b
		The accumulator function is invoked in all states, after invocation
Packit ae235b
		of each closure (except in <literal>RUN_EMISSION_HOOK</literal> and
Packit ae235b
		<literal>RUN_CLEANUP</literal>). It accumulates
Packit ae235b
		the closure return value into the signal return value and returns TRUE or
Packit ae235b
		FALSE. If, at any point, it does not return TRUE, emission jumps
Packit ae235b
		to <literal>RUN_CLEANUP</literal> state.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		If no accumulator function was provided, the value returned by the last handler
Packit ae235b
		run will be returned by <function><link linkend="g-signal-emit">g_signal_emit</link></function>.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	</sect2>
Packit ae235b
Packit ae235b
Packit ae235b
	<sect2 id="signal-detail">
Packit ae235b
	  <title>The <emphasis>detail</emphasis> argument</title>
Packit ae235b
Packit ae235b
	  <para>All the functions related to signal emission or signal connection have a parameter
Packit ae235b
		named the <emphasis>detail</emphasis>. Sometimes, this parameter is hidden by the API
Packit ae235b
		but it is always there, in one form or another. 
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
	    Of the three main connection functions,
Packit ae235b
		only one has an explicit detail parameter as a <link linkend="GQuark"><type>GQuark</type></link>:
Packit ae235b
		<link linkend="g-signal-connect-closure-by-id"><function>g_signal_connect_closure_by_id</function></link>.
Packit ae235b
		<footnote>
Packit ae235b
		  <para>A GQuark is an integer which uniquely represents a string. It is possible to transform
Packit ae235b
		   back and forth between the integer and string representations with the functions 
Packit ae235b
		   <function><link linkend="g-quark-from-string">g_quark_from_string</link></function> and <function><link linkend="g-quark-to-string">g_quark_to_string</link></function>.
Packit ae235b
		  </para>
Packit ae235b
		</footnote>
Packit ae235b
	  </para>
Packit ae235b
	  <para>
Packit ae235b
	    The two other functions,
Packit ae235b
	    <link linkend="g-signal-connect-closure"><function>g_signal_connect_closure</function></link> and
Packit ae235b
	    <link linkend="g-signal-connect-data"><function>g_signal_connect_data</function></link>
Packit ae235b
	    hide the detail parameter in the signal name identification.
Packit ae235b
		Their <parameter>detailed_signal</parameter> parameter is a
Packit ae235b
		string which identifies the name of the signal to connect to.
Packit ae235b
		The format of this string should match
Packit ae235b
		<emphasis>signal_name::detail_name</emphasis>. For example,
Packit ae235b
		connecting to the signal named
Packit ae235b
		<emphasis>notify::cursor_position</emphasis> will actually
Packit ae235b
		connect to the signal named <emphasis>notify</emphasis> with the
Packit ae235b
		<emphasis>cursor_position</emphasis> detail.
Packit ae235b
		Internally, the detail string is transformed to a GQuark if it is present.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
	    Of the four main signal emission functions, one hides it in its
Packit ae235b
	    signal name parameter:
Packit ae235b
	    <link linkend="g-signal-connect"><function>g_signal_connect</function></link>.
Packit ae235b
	    The other three have an explicit detail parameter as a
Packit ae235b
	    <link linkend="GQuark"><type>GQuark</type></link> again:
Packit ae235b
	    <link linkend="g-signal-emit"><function>g_signal_emit</function></link>,
Packit ae235b
	    <link linkend="g-signal-emitv"><function>g_signal_emitv</function></link> and
Packit ae235b
	    <link linkend="g-signal-emit-valist"><function>g_signal_emit_valist</function></link>.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
        If a detail is provided by the user to the emission function, it is used during emission to match
Packit ae235b
        against the closures which also provide a detail.
Packit ae235b
        If a closure's detail does not match the detail provided by the user, it
Packit ae235b
        will not be invoked (even though it is connected to a signal which is
Packit ae235b
        being emitted).
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		This completely optional filtering mechanism is mainly used as an optimization for signals
Packit ae235b
		which are often emitted for many different reasons: the clients can filter out which events they are
Packit ae235b
		interested in before the closure's marshalling code runs. For example, this is used extensively
Packit ae235b
		by the <link linkend="GObject-notify"><structfield>notify</structfield></link> signal of GObject: whenever a property is modified on a GObject,
Packit ae235b
		instead of just emitting the <emphasis>notify</emphasis> signal, GObject associates as a detail to this
Packit ae235b
		signal emission the name of the property modified. This allows clients who wish to be notified of changes
Packit ae235b
		to only one property to filter most events before receiving them.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	  <para>
Packit ae235b
		As a simple rule, users can and should set the detail parameter to zero: this will disable completely
Packit ae235b
        this optional filtering for that signal.
Packit ae235b
	  </para>
Packit ae235b
Packit ae235b
	</sect2>
Packit ae235b
Packit ae235b
  </sect1>
Packit ae235b
</chapter>
Packit ae235b