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
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter3.Basics</title><meta name="generator" content="DocBook XSL Stylesheets V1.64.1"><link rel="home" href="index.html" title="Programming with gtkmm2"><link rel="up" href="index.html" title="Programming with gtkmm2"><link rel="previous" href="ch02s03.html" title="Microsoft Windows"><link rel="next" href="ch03s02.html" title="Widgets"></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">Chapter3.Basics</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s03.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="ch03s02.html">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>Chapter3.Basics</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch03.html#id2443444">Simple Example</a></span></dt><dt><span class="sect1"><a href="ch03s02.html">Widgets</a></span></dt><dt><span class="sect1"><a href="ch03s03.html">Signals</a></span></dt><dt><span class="sect1"><a href="ch03s04.html">Glib::ustring</a></span></dt><dt><span class="sect1"><a href="ch03s05.html">Intermediate types</a></span></dt><dt><span class="sect1"><a href="ch03s06.html">Hello World in gtkmm</a></span></dt></dl></div><p>
This chapter will introduce some of the most important aspects of gtkmm 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 be help you with gtkmm as it would with any library. Unless we state otherwise, you can expect gtkmm classes to behave like any other C++ class, and you can expect to use your existing C++ techniques with gtkmm classes.
</p><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2443444"></a>Simple Example</h2></div></div><div></div></div><p>
To begin our introduction to gtkmm, 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>
</p><p>We will now explain each line of the example</p><p>
</p><pre class="programlisting">
#include <gtkmm.h>
</pre><p>
</p><p>
All gtkmm programs must include certain gtkmm headers; <tt class="literal">gtkmm.h</tt>
includes the entire gtkmm kit, which is usually not a good idea, since
it includes a megabyte or so of headers, but for simple programs, it
suffices.
</p><p>
The next line:
</p><p>
</p><pre class="programlisting">
Gtk::Main kit(argc, argv);
</pre><p>
</p><p>
creates a <tt class="literal">Gtk::Main</tt> object. This is needed in all gtkmm
applications. The constructor for this object initializes gtkmm, and checks the
arguments passed to your application on the command line, looking for
standard options such as <tt class="literal">-display</tt>. It takes these from the argument list, leaving anything it does not
recognize for your application to parse or ignore. This ensures
that all gtkmm applications accept the same set of standard arguments.
</p><p>
The next two lines of code create and display a window:
</p><p>
</p><pre class="programlisting">
Gtk::Window window;
</pre><p>
</p><p>
The last line shows the window and enters the gtkmm main processing loop, which will finish when the window is closed.
</p><p>
</p><pre class="programlisting">
Gtk::Main::run(window);
</pre><p>
</p><p>
After putting the source code in <tt class="literal">simple.cc</tt> you can compile the above program with gcc using:
</p><pre class="programlisting">
g++ simple.cc -o simple `pkg-config gtkmm-2.0 --cflags --libs`
</pre><p>
Note that you must surround
the <tt class="literal">pkg-config</tt> 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><p>
Although we have shown the compilation command for this simple example, you really should use the automake and autoconf tools, as described in "Autoconf, Automake, Libtool", by G. V. Vaughan et al. The examples used in this book are included in the gtkmm package, with appropriate build files, so we won't show the build commands in future. You'll just need to find the appropriate directory and type <tt class="literal">make</tt>.
</p><p>
To simplify compilation, we use <tt class="literal">pkg-config</tt>, which
is present in all (properly installed) gtkmm installations. This
program 'knows' what compiler switches are needed to compile programs
that use gtkmm. The <tt class="literal">--cflags</tt> option causes
<tt class="literal">pkg-config</tt> to output a list of include directories for the
compiler to look in; the <tt class="literal">--libs</tt> option requests the
list of libraries for the compiler to link with and the directories to
find them in. Try running it from your shell-prompt to see the results on your system.
</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">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch03s02.html">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">Home</a></td><td width="40%" align="right" valign="top">Widgets</td></tr></table></div></body></html>
|