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
|
<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>Chapter 3. Basics</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-packages-windows.html" title="Microsoft Windows">
<link rel="next" href="sec-headers-and-linking.html" title="Headers and Linking">
</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">Chapter 3. Basics</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-packages-windows.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-headers-and-linking.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-basics"></a>Chapter 3. Basics</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc">
<li><span class="section"><a href="chapter-basics.html#sec-basics-simple-example">Simple Example</a></span></li>
<li><span class="section"><a href="sec-headers-and-linking.html">Headers and Linking</a></span></li>
<li><span class="section"><a href="sec-widgets-overview.html">Widgets</a></span></li>
<li><span class="section"><a href="sec-signals-overview.html">Signals</a></span></li>
<li><span class="section"><a href="sec-basics-ustring.html">Glib::ustring</a></span></li>
<li><span class="section"><a href="sec-basics-gobj-and-wrap.html">Mixing C and C++ APIs</a></span></li>
<li><span class="section"><a href="sec-helloworld.html">Hello World in <span class="application">gtkmm</span></a></span></li>
</ul>
</div>
<p>
This chapter will introduce some of the most important aspects of <span class="application">gtkmm</span> coding. These will be demonstrated with simple working example code. However, this is just a taster, so you need to look at the other chapters for more substantial information.
</p>
<p>
Your existing knowledge of C++ will help you with <span class="application">gtkmm</span> as it would with any library. Unless we state otherwise, you can expect <span class="application">gtkmm</span> classes to behave like any other C++ class, and you can expect to use your existing C++ techniques with <span class="application">gtkmm</span> classes.
</p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-basics-simple-example"></a>Simple Example</h2></div></div></div>
<p>
To begin our introduction to <span class="application">gtkmm</span>, we'll start with the simplest
program possible. This program will create an empty 200 x 200 pixel window.
</p>
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/base" target="_top">Source Code</a></p>
<p>File: <code class="filename">base.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include <gtkmm.h>
class MyWindow : public Gtk::Window
{
public:
MyWindow();
};
MyWindow::MyWindow()
{
set_title("Basic application");
set_default_size(200, 200);
}
int main(int argc, char* argv[])
{
auto app = Gtk::Application::create("org.gtkmm.examples.base");
return app->make_window_and_run<MyWindow>(argc, argv);
}
</code></pre>
<p>We will now explain each part of the example</p>
<pre class="programlisting"><code class="code">#include <gtkmm.h></code></pre>
<p>
All <span class="application">gtkmm</span> programs must include certain <span class="application">gtkmm</span> headers; <code class="literal">gtkmm.h</code>
includes the entire <span class="application">gtkmm</span> kit. This is usually not a good idea, because
it includes a megabyte or so of headers, but for simple programs, it
suffices.
</p>
<p>The next part of the program:</p>
<pre class="programlisting"><code class="code">class MyWindow : public Gtk::Window
{
public:
MyWindow();
};
MyWindow::MyWindow()
{
set_title("Basic application");
set_default_size(200, 200);
}</code></pre>
<p>
defines the <code class="classname">MyWindow</code> class. Its default constructor sets the
window's title and default (initial) size.
</p>
<p>The <code class="function">main()</code> function's first statement:</p>
<pre class="programlisting"><code class="code">auto app = Gtk::Application::create("org.gtkmm.examples.base");</code></pre>
<p>
creates a <code class="classname">Gtk::Application</code> object, stored in a <code class="classname">Glib::RefPtr</code> smartpointer.
This is needed in all <span class="application">gtkmm</span> applications. The <code class="methodname">create()</code> method for this object initializes <span class="application">gtkmm</span>.
</p>
<p>
The last line creates and shows a window and enters the <span class="application">gtkmm</span> main processing loop, which will finish when the window is closed.
Your <code class="function">main()</code> function will then return with an appropriate success or error code.
The <em class="parameter"><code>argc</code></em> and <em class="parameter"><code>argv</code></em> arguments, passed to your application on the command line,
can be checked when <code class="methodname">make_window_and_run()</code> is called, but this simple application does not use those arguments.
</p>
<pre class="programlisting"><code class="code">return app->make_window_and_run<MyWindow>(argc, argv);</code></pre>
<p>
After putting the source code in <code class="literal">simple.cc</code> you can compile
the above program with <span class="application">gcc</span> using:
</p>
<pre class="programlisting"><code class="code">g++ simple.cc -o simple `pkg-config --cflags --libs gtkmm-4.0` -std=c++17</code></pre>
<p>
Note that you must surround the <code class="literal">pkg-config</code> invocation with backquotes.
Backquotes cause the shell to execute the command inside them, and to use
the command's output as part of the command line.
Note also that <code class="literal">simple.cc</code> must come before the <code class="literal">pkg-config</code>
invocation on the command line. <code class="literal">-std=c++17</code> is necessary only if
your compiler is not C++17 compliant by default.
</p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-packages-windows.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-headers-and-linking.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Microsoft Windows </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"> Headers and Linking</td>
</tr>
</table>
</div>
</body>
</html>
|