| 1 |
<html> |
| 2 |
<head> |
| 3 |
<meta charset="UTF-8"> |
| 4 |
<link rel="stylesheet" href="highlight.min.css"> |
| 5 |
<script src="highlight.min.js"></script><script> |
| 6 |
hljs.configure({languages: ['cpp']}); |
| 7 |
hljs.highlightAll(); |
| 8 |
</script><title>Writing signal handlers</title> |
| 9 |
<link rel="stylesheet" type="text/css" href="style.css"> |
| 10 |
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"> |
| 11 |
<link rel="home" href="index.html" title="Programming with gtkmm 4"> |
| 12 |
<link rel="up" href="chapter-signals.html" title="Appendix B. Signals"> |
| 13 |
<link rel="prev" href="chapter-signals.html" title="Appendix B. Signals"> |
| 14 |
<link rel="next" href="sec-disconnecting-signal-handlers.html" title="Disconnecting signal handlers"> |
| 15 |
</head> |
| 16 |
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| 17 |
<div class="navheader"> |
| 18 |
<table width="100%" summary="Navigation header"> |
| 19 |
<tr><th colspan="3" align="center">Writing signal handlers</th></tr> |
| 20 |
<tr> |
| 21 |
<td width="20%" align="left"> |
| 22 |
<a accesskey="p" href="chapter-signals.html"><img src="icons/prev.png" alt="Prev"></a> </td> |
| 23 |
<th width="60%" align="center">Appendix B. Signals</th> |
| 24 |
<td width="20%" align="right"> <a accesskey="n" href="sec-disconnecting-signal-handlers.html"><img src="icons/next.png" alt="Next"></a> |
| 25 |
</td> |
| 26 |
</tr> |
| 27 |
</table> |
| 28 |
<hr> |
| 29 |
</div> |
| 30 |
<div class="section"> |
| 31 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 32 |
<a name="sec-writing-signal-handlers"></a>Writing signal handlers</h2></div></div></div> |
| 33 |
|
| 34 |
|
| 35 |
<p> |
| 36 |
To find out what type of signal handler you can connect to a signal, you can |
| 37 |
look it up in the reference documentation or the header file. Here's an example of a signal declaration you |
| 38 |
might see in the <span class="application">gtkmm</span> headers: |
| 39 |
</p> |
| 40 |
|
| 41 |
<pre class="programlisting"><code class="code">Glib::SignalProxy<bool(Gtk::DirectionType)> signal_focus() |
| 42 |
</code></pre> |
| 43 |
|
| 44 |
<p> |
| 45 |
Other than the signal's name (<code class="literal">focus</code>), the template arguments are |
| 46 |
important to note here. The first argument, <span class="type">bool</span>, is the type that |
| 47 |
the signal handler should return; and the type within parentheses, |
| 48 |
<span class="type">Gtk::DirectionType</span>, is the type of this signal's first, and only, |
| 49 |
argument. By looking at the reference documentation, you can |
| 50 |
see the names of the arguments too. |
| 51 |
</p> |
| 52 |
|
| 53 |
<p> |
| 54 |
The same principles apply for signals which have more arguments. Here's one |
| 55 |
with three (taken from <code class="filename"><gtkmm/textbuffer.h></code>): |
| 56 |
</p> |
| 57 |
|
| 58 |
<pre class="programlisting"><code class="code">Glib::SignalProxy<void(TextBuffer::iterator&, const Glib::ustrin&, int)> signal_insert(); |
| 59 |
</code></pre> |
| 60 |
|
| 61 |
<p> |
| 62 |
It follows the same form. The first type is <span class="type">void</span>, so that should be |
| 63 |
our signal handler's return type. |
| 64 |
The following three types are the argument types, in order. Our signal |
| 65 |
handler's prototype could look like this: |
| 66 |
</p> |
| 67 |
|
| 68 |
<pre class="programlisting"><code class="code">void on_insert(TextBuffer::iterator& pos, const Glib::ustring& text, int bytes) |
| 69 |
</code></pre> |
| 70 |
</div> |
| 71 |
<div class="navfooter"> |
| 72 |
<hr> |
| 73 |
<table width="100%" summary="Navigation footer"> |
| 74 |
<tr> |
| 75 |
<td width="40%" align="left"> |
| 76 |
<a accesskey="p" href="chapter-signals.html"><img src="icons/prev.png" alt="Prev"></a> </td> |
| 77 |
<td width="20%" align="center"><a accesskey="u" href="chapter-signals.html"><img src="icons/up.png" alt="Up"></a></td> |
| 78 |
<td width="40%" align="right"> <a accesskey="n" href="sec-disconnecting-signal-handlers.html"><img src="icons/next.png" alt="Next"></a> |
| 79 |
</td> |
| 80 |
</tr> |
| 81 |
<tr> |
| 82 |
<td width="40%" align="left" valign="top">Appendix B. Signals </td> |
| 83 |
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> |
| 84 |
<td width="40%" align="right" valign="top"> Disconnecting signal handlers</td> |
| 85 |
</tr> |
| 86 |
</table> |
| 87 |
</div> |
| 88 |
</body> |
| 89 |
</html> |