File: chapter-custom-signals.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (234 lines) | stat: -rw-r--r-- 7,528 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Appendix C. Creating your own signals</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="index.html" title="Programming with gtkmm 4">
<link rel="prev" href="sec-exceptions-in-signal-handlers.html" title="Exceptions in signal handlers">
<link rel="next" href="sec-signals-comparison.html" title="Appendix D. Comparison with other signalling systems">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Appendix C. Creating your own signals</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-exceptions-in-signal-handlers.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="sec-signals-comparison.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="appendix">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-custom-signals"></a>Appendix C. Creating your own signals</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc"><li><span class="section"><a href="chapter-custom-signals.html#chapter-custom-signals-example">Example</a></span></li></ul>
</div>


<p>
Now that you've seen signals and signal handlers in <span class="application">gtkmm</span>, you
might like to use the same technique to allow interaction between your
own classes. That's actually very simple by using the
<span class="application">libsigc++</span> library directly.
</p>
<p>
This isn't purely a <span class="application">gtkmm</span> or GUI issue. <span class="application">gtkmm</span> uses
<span class="application">libsigc++</span> to implement its proxy wrappers for the
<span class="application">GTK</span> signal system, but for new,
non-GTK signals, you can create pure C++ signals, using the
<code class="classname">sigc::signal&lt;&gt;</code> template.
</p>
<p>
For instance, to create a signal that sends 2 parameters, a <span class="type">bool</span>
and an <span class="type">int</span>, just declare a <code class="classname">sigc::signal</code>,
like so:
</p>
<pre class="programlisting"><code class="code">sigc::signal&lt;void(bool, int)&gt; signal_something;
</code></pre>
<p>
You could just declare that signal as a public member variable, but
some people find that distasteful and prefer to make it available via
an accessor method, like so:
</p>
<pre class="programlisting"><code class="code">class Server
{
public:
  //signal accessor:
  using type_signal_something = sigc::signal&lt;void(bool, int)&gt;;
  type_signal_something signal_something();

protected:
  type_signal_something m_signal_something;
};

Server::type_signal_something Server::signal_something()
{
  return m_signal_something;
}
</code></pre>

<p>
You can then connect to the signal using the same syntax used when
connecting to <span class="application">gtkmm</span> signals. For instance,
</p>
<pre class="programlisting"><code class="code">server.signal_something().connect(
  sigc::mem_fun(client, &amp;Client::on_server_something) );
</code></pre>

<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="chapter-custom-signals-example"></a>Example</h2></div></div></div>


<p>
This is a full working example that defines and uses custom signals.
</p>

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/signals/custom/" target="_top">Source Code</a></p>

<p>File: <code class="filename">client.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_CLIENT_H
#define GTKMM_EXAMPLE_CLIENT_H

#include &lt;sigc++/sigc++.h&gt;

//Client must inherit from sigc::trackable.
//because libsigc++ needs to keep track of the lifetime of signal handlers.
class Client : public sigc::trackable
{
public:
  Client();
  virtual ~Client();

  //Signal handler:
  void on_server_something(bool a, int b);
};

#endif //GTKMM_EXAMPLE_CLIENT_H
</code></pre>
<p>File: <code class="filename">server.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_SERVER_H
#define GTKMM_EXAMPLE_SERVER_H

#include &lt;sigc++/sigc++.h&gt;

class Server
{
public:
  Server();
  virtual ~Server();

  void do_something();

  //signal accessor:
  using type_signal_something = sigc::signal&lt;void(bool, int)&gt;;
  type_signal_something signal_something();

protected:
  type_signal_something m_signal_something;
};

#endif //GTKMM_EXAMPLE_SERVER_H
</code></pre>
<p>File: <code class="filename">client.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "client.h"
#include &lt;iostream&gt;

Client::Client()
{
}

Client::~Client()
{
}

void Client::on_server_something(bool a, int b)
{
  std::cout &lt;&lt; "Client::on_server_something() called with these parameters: "
      &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; std::endl;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "server.h"
#include "client.h"
#include &lt;iostream&gt;

int main(int, char**)
{
  Server server;
  Client client;

  //Connect a Server signal to the signal handler in Client.
  server.signal_something().connect(sigc::mem_fun(client,
              &amp;Client::on_server_something) );

  std::cout &lt;&lt; "Before Server::do_something()" &lt;&lt; std::endl;

  //Tell the server to do something that will eventually cause it to emit the
  //"something" signal.
  server.do_something();    // Client::on_server_something() will run before
                            // Server::do_something() has completed.

  std::cout &lt;&lt; "After Server::do_something()" &lt;&lt; std::endl;

  return 0;
}
</code></pre>
<p>File: <code class="filename">server.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "server.h"
#include &lt;iostream&gt;

Server::Server()
{
}

Server::~Server()
{
}

Server::type_signal_something Server::signal_something()
{
  return m_signal_something;
}

void Server::do_something()
{
  m_signal_something.emit(false, 5);
}

</code></pre>


</div>

</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-exceptions-in-signal-handlers.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="sec-signals-comparison.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Exceptions in signal handlers </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Appendix D. Comparison with other signalling systems</td>
</tr>
</table>
</div>
</body>
</html>