tarball import: glib-2.25.9.tar.bz2
[mirror-ossqm-glib2.git] / docs / reference / gobject / html / howto-signals.html
blobac44ecbeb05c9b16deee86dc7e9ef540b53b5813
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>How to create and use signals</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="pt02.html" title="Part IV. Tutorial">
9 <link rel="prev" href="howto-interface-properties.html" title="Interface Properties">
10 <link rel="next" href="pt03.html" title="Part V. Related Tools">
11 <meta name="generator" content="GTK-Doc V1.15 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 </head>
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="howto-interface-properties.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="pt02.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GObject Reference Manual</th>
20 <td><a accesskey="n" href="pt03.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
21 </tr></table>
22 <div class="chapter" title="How to create and use signals">
23 <div class="titlepage"><div><div><h2 class="title">
24 <a name="howto-signals"></a>How to create and use signals</h2></div></div></div>
25 <div class="toc"><dl><dt><span class="sect1"><a href="howto-signals.html#howto-simple-signals">Simple use of signals</a></span></dt></dl></div>
26 <p>
27 The signal system which was built in GType is pretty complex and
28 flexible: it is possible for its users to connect at runtime any
29 number of callbacks (implemented in any language for which a binding
30 exists)
31 <sup>[<a name="id740741" href="#ftn.id740741" class="footnote">14</a>]</sup>
32 to any signal and to stop the emission of any signal at any
33 state of the signal emission process. This flexibility makes it
34 possible to use GSignal for much more than just emit signals which
35 can be received by numerous clients.
36 </p>
37 <div class="sect1" title="Simple use of signals">
38 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
39 <a name="howto-simple-signals"></a>Simple use of signals</h2></div></div></div>
40 <p>
41 The most basic use of signals is to implement simple event
42 notification: for example, if we have a MamanFile object, and
43 if this object has a write method, we might wish to be notified
44 whenever someone has changed something via our MamanFile instance.
45 The code below shows how the user can connect a callback to the
46 "changed" signal.
47 </p>
48 <pre class="programlisting">
49 file = g_object_new (MAMAN_FILE_TYPE, NULL);
51 g_signal_connect (file, "changed", G_CALLBACK (changed_event), NULL);
53 maman_file_write (file, buffer, strlen (buffer));
54 </pre>
55 <p>
56 </p>
57 <p>
58 The <span class="type">MamanFile</span> signal is registered in the class_init
59 function:
60 </p>
61 <pre class="programlisting">
62 file_signals[CHANGED] =
63 g_signal_newv ("changed",
64 G_TYPE_FROM_CLASS (gobject_class),
65 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
66 NULL /* closure */,
67 NULL /* accumulator */,
68 NULL /* accumulator data */,
69 g_cclosure_marshal_VOID__VOID,
70 G_TYPE_NONE /* return_type */,
71 0 /* n_params */,
72 NULL /* param_types */);
73 </pre>
74 <p>
75 and the signal is emitted in <code class="function">maman_file_write</code>:
76 </p>
77 <pre class="programlisting">
78 void
79 maman_file_write (MamanFile *self,
80 const guchar *buffer,
81 gssize size)
83 /* First write data. */
85 /* Then, notify user of data written. */
86 g_signal_emit (self, file_signals[CHANGED], 0 /* details */);
88 </pre>
89 <p>
90 As shown above, you can safely set the details parameter to zero if
91 you do not know what it can be used for. For a discussion of what you
92 could used it for, see <a class="xref" href="signal.html#signal-detail" title="The detail argument">the section called “The <span class="emphasis"><em>detail</em></span> argument”</a>
93 </p>
94 <p>
95 The signature of the signal handler in the above example is defined as
96 <code class="function">g_cclosure_marshal_VOID__VOID</code>. Its name follows
97 a simple convention which encodes the function parameter and return value
98 types in the function name. Specifically, the value in front of the
99 double underscore is the type of the return value, while the value(s)
100 after the double underscore denote the parameter types.
101 </p>
103 The header <code class="filename">gobject/gmarshal.h</code> defines a set of
104 commonly needed closures that one can use. If you want to have complex
105 marshallers for your signals you should probably use glib-genmarshal
106 to autogenerate them from a file containing their return and
107 parameter types.
108 </p>
109 </div>
110 <div class="footnotes">
111 <br><hr width="100" align="left">
112 <div class="footnote"><p><sup>[<a name="ftn.id740741" href="#id740741" class="para">14</a>] </sup>A Python callback can be connected to any signal on any
113 C-based GObject.
114 </p></div>
115 </div>
116 </div>
117 <div class="footer">
118 <hr>
119 Generated by GTK-Doc V1.15</div>
120 </body>
121 </html>