Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > e40b0b2b839eccdb3c85993a7b738115 > files > 6

gtkmm-documentation-3.24.0-1.mga7.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 3. Basics</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="Programming with gtkmm 3">
<link rel="up" href="index.html" title="Programming with gtkmm 3">
<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="sect1"><a href="chapter-basics.html#sec-basics-simple-example">Simple Example</a></span></li>
<li><span class="sect1"><a href="sec-headers-and-linking.html">Headers and Linking</a></span></li>
<li><span class="sect1"><a href="sec-widgets-overview.html">Widgets</a></span></li>
<li><span class="sect1"><a href="sec-signals-overview.html">Signals</a></span></li>
<li><span class="sect1"><a href="sec-basics-ustring.html">Glib::ustring</a></span></li>
<li><span class="sect1"><a href="sec-intermediate-types.html">Intermediate types</a></span></li>
<li><span class="sect1"><a href="sec-basics-gobj-and-wrap.html">Mixing C and C++ APIs</a></span></li>
<li><span class="sect1"><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="sect1">
<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="http://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/base?h=gtkmm-3-24" target="_top">Source Code</a></p>
<p>File: <code class="filename">base.cc</code> (For use with gtkmm 3, not gtkmm 2)
</p>
<pre class="programlisting">
#include &lt;gtkmm.h&gt;

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

  Gtk::Window window;
  window.set_default_size(200, 200);

  return app-&gt;run(window);
}
</pre>
<p>We will now explain each line of the example</p>
<pre class="programlisting">#include &lt;gtkmm.h&gt;</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 statement:

</p>
<pre class="programlisting">Glib::RefPtr&lt;Gtk::Application&gt; app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");</pre>
<p>

creates a <code class="classname">Gtk::Application</code> object, stored in a <code class="classname">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>, and checks the
arguments passed to your application on the command line, looking for
standard options such as <code class="literal">--display</code>. It takes these from the argument list, leaving anything it does not
recognize for your application to parse or ignore. This ensures
that all <span class="application">gtkmm</span> applications accept the same set of standard arguments.
</p>
<p>
The next two lines of code create a window and set its default (initial) size:
</p>
<pre class="programlisting">Gtk::Window window;
window.set_default_size(200, 200);</pre>
<p>
The last line shows the 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.
</p>
<pre class="programlisting">return app-&gt;run(window);</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">g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`</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.
</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>