File: sec-helloworld.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 (232 lines) | stat: -rw-r--r-- 7,924 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
<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>Hello World in gtkmm</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="chapter-basics.html" title="Chapter 3. Basics">
<link rel="prev" href="sec-basics-gobj-and-wrap.html" title="Mixing C and C++ APIs">
<link rel="next" href="changes-gtkmm3.html" title="Chapter 4. Changes in gtkmm 3">
</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">Hello World in <span class="application">gtkmm</span>
</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-basics-gobj-and-wrap.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 3. Basics</th>
<td width="20%" align="right"> <a accesskey="n" href="changes-gtkmm3.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-helloworld"></a>Hello World in <span class="application">gtkmm</span>
</h2></div></div></div>


<p>
We've now learned enough to look at a real example. In accordance with an ancient
tradition of computer science, we now introduce Hello World, a la <span class="application">gtkmm</span>:
</p>

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

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

#include &lt;gtkmm/button.h&gt;
#include &lt;gtkmm/window.h&gt;

class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  ~HelloWorld() override;

protected:
  //Signal handlers:
  void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
};

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

HelloWorld::HelloWorld()
: m_button("Hello World")   // creates a new button with label "Hello World".
{
  // Sets the margin around the button.
  m_button.set_margin(10);

  // When the button receives the "clicked" signal, it will call the
  // on_button_clicked() method defined below.
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
              &amp;HelloWorld::on_button_clicked));

  // This packs the button into the Window (a container).
  set_child(m_button);
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
  std::cout &lt;&lt; "Hello World" &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 "helloworld.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char* argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;HelloWorld&gt;(argc, argv);
}
</code></pre>


<p>
Try to compile and run it before going on. You should see something like this:
</p>

<div class="figure">
<a name="figure-helloworld"></a><p class="title"><b>Figure 3.1. Hello World</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/helloworld.png" alt="Hello World"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p>
Pretty thrilling, eh?  Let's examine the code. First, the
<code class="classname">HelloWorld</code> class:
</p>

<pre class="programlisting"><code class="code">class HelloWorld : public Gtk::Window
{
public:
  HelloWorld();
  ~HelloWorld() override;

protected:
  //Signal handlers:
  void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
};</code></pre>

<p>
This class implements the "Hello World" window. It's derived from
<code class="classname">Gtk::Window</code>, and has a single <code class="classname">Gtk::Button</code> as a member.
We've chosen to use the
constructor to do all of the initialization work for the window,
including setting up the signals. Here it is, with the comments
omitted:
</p>

<pre class="programlisting"><code class="code">HelloWorld::HelloWorld()
: m_button("Hello World")
{
  m_button.set_margin(10);
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
    &amp;HelloWorld::on_button_clicked));
  set_child(m_button);
}</code></pre>

<p>
Notice that we've used an initializer statement to give the <code class="literal">m_button</code>
object the label "Hello World".
</p>

<p>
Next we call the Button's <code class="methodname">set_margin()</code> method. This sets
the amount of space around the button.
</p>

<p>
We then hook up a signal handler to <code class="literal">m_button</code>'s <code class="literal">clicked</code> signal.
This prints our friendly greeting to <code class="literal">stdout</code>.
</p>

<p>
Next, we use the Window's <code class="methodname">set_child()</code> method to put
<code class="literal">m_button</code> in the Window. The <code class="methodname">set_child()</code>
method places the Widget in the Window.
</p>

<p>
Now let's look at our program's <code class="function">main()</code> function. Here it is,
without comments:
</p>

<pre class="programlisting"><code class="code">int main(int argc, char* argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");
  return app-&gt;make_window_and_run&lt;HelloWorld&gt;(argc, argv);
}</code></pre>

<p>
First we instantiate an object stored in a <code class="classname">Glib::RefPtr</code> smartpointer called <code class="literal">app</code>. This is of type
<code class="classname">Gtk::Application</code>. Every <span class="application">gtkmm</span> program must have one of these.
</p>

<p>
Next we call <code class="methodname">make_window_and_run()</code> which creates an object
of our <code class="classname">HelloWorld</code> class, shows that Window and starts the <span class="application">gtkmm</span>
<span class="emphasis"><em>event loop</em></span>. During the event loop <span class="application">gtkmm</span> idles, waiting for actions
from the user, and responding appropriately.
When the user closes the Window, <code class="methodname">make_window_and_run()</code> will return,
causing our <code class="function">main()</code> function to return. The application will then finish.
</p>

<p>
Like the simple example we showed earlier, this Hello World program does not use
the command-line parameters.
</p>

</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-basics-gobj-and-wrap.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-basics.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="changes-gtkmm3.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Mixing C and C++ APIs </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"> Chapter 4. Changes in <span class="application">gtkmm</span> 3</td>
</tr>
</table>
</div>
</body>
</html>