<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 3. Basics</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.73.2"> <link rel="start" href="index.html" title="Programming with gtkmm"> <link rel="up" href="index.html" title="Programming with gtkmm"> <link rel="prev" href="ch02s03.html" title="Microsoft Windows"> <link rel="next" href="ch03s02.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="ch02s03.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="ch03s02.html"><img src="../icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="chapter" lang="en"> <div class="titlepage"><div><div><h2 class="title"> <a name="sec-basics"></a>Chapter 3. Basics</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <ul> <li><span class="sect1"><a href="ch03.html#id2569752">Simple Example</a></span></li> <li><span class="sect1"><a href="ch03s02.html">Headers and Linking</a></span></li> <li><span class="sect1"><a href="ch03s03.html">Widgets</a></span></li> <li><span class="sect1"><a href="ch03s04.html">Signals</a></span></li> <li><span class="sect1"><a href="ch03s05.html">Glib::ustring</a></span></li> <li><span class="sect1"><a href="ch03s06.html">Intermediate types</a></span></li> <li><span class="sect1"><a href="ch03s07.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" lang="en"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="id2569752"></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> Source location: examples/base/base.cc </p> <pre class="programlisting"> #include <gtkmm.h> int main(int argc, char *argv[]) { Gtk::Main kit(argc, argv); Gtk::Window window; Gtk::Main::run(window); return 0; } </pre> <p>We will now explain each line of the example</p> <pre class="programlisting">#include <gtkmm.h></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 line: </p> <pre class="programlisting">Gtk::Main kit(argc, argv);</pre> <p> creates a <code class="classname">Gtk::Main</code> object. This is needed in all <span class="application">gtkmm</span> applications. The constructor 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 and display a window: </p> <pre class="programlisting">Gtk::Window window;</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. </p> <pre class="programlisting">Gtk::Main::run(window);</pre> <p> After putting the source code in <code class="literal">simple.cc</code> you can compile the above program with gcc using: </p> <pre class="programlisting">g++ simple.cc -o simple `pkg-config gtkmm-2.4 --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. </p> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="ch02s03.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="ch03s02.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>