Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 345aa895e80053137c21f8693106c3a0 > files > 150

gtkmm2.4-documentation-2.17.4-1mdv2010.0.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World in gtkmm</title>
<link rel="stylesheet" href="style.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="index.html" title="Programming with gtkmm">
<link rel="up" href="chapter-basics.html" title="Chapter 3. Basics">
<link rel="prev" href="sec-intermediate-types.html" title="Intermediate types">
<link rel="next" href="chapter-button-widget.html" title="Chapter 4. Buttons">
</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-intermediate-types.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="chapter-button-widget.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Hello World in gtkmm">
<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="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/helloworld" target="_top">Source Code</a></p>
<p>File: <code class="filename">helloworld.h</code>
</p>
<pre class="programlisting">
#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();
  virtual ~HelloWorld();

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

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

#endif // GTKMM_EXAMPLE_HELLOWORLD_H
</pre>
<p>File: <code class="filename">main.cc</code>
</p>
<pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#include "helloworld.h"

int main (int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  HelloWorld helloworld;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(helloworld);

  return 0;
}
</pre>
<p>File: <code class="filename">helloworld.cc</code>
</p>
<pre class="programlisting">
#include "helloworld.h"
#include &lt;iostream&gt;

HelloWorld::HelloWorld()
: m_button("Hello World")   // creates a new button with label "Hello World".
{
  // Sets the border width of the window.
  set_border_width(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).
  add(m_button);

  // The final step is to display this newly created widget...
  m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
  std::cout &lt;&lt; "Hello World" &lt;&lt; std::endl;
}
</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><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">class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

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

  //Member widgets:
  Gtk::Button m_button;
};</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 initialisation work for the window,
including setting up the signals.  Here it is, with the comments
omitted:
</p>
<pre class="programlisting">HelloWorld::HelloWorld()
:
  m_button ("Hello World")
{
  set_border_width(10);
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
    &amp;HelloWorld::on_button_clicked));
  add(m_button);.
  m_button.show();
}</pre>
<p>
Notice that we've used an initialiser statement to give the <code class="literal">m_button</code>
object the label "Hello World".
</p>
<p>
Next we call the Window's <code class="methodname">set_border_width()</code> method.  This sets
the amount of space between the sides of the window and the widget it
contains.
</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">add()</code> method to put <code class="literal">m_button</code> in
the Window.  (<code class="methodname">add()</code> comes from <code class="classname">Gtk::Container</code>, which is
described in the chapter on container widgets.)  The <code class="methodname">add()</code> method
places the Widget in the Window, but it doesn't display
the widget.  <span class="application">gtkmm</span> widgets are always invisible when you create them - to display them, you must call their <code class="methodname">show()</code> method, which
is what we do in the next line.
</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">int main(int argc, char** argv)
{
  Gtk::Main kit(argc, argv);

  HelloWorld helloworld;
  Gtk::Main::run(helloworld);

  return 0;
}</pre>
<p>
First we instantiate an object called <code class="literal">kit</code>. This is of type
<code class="classname">Gtk::Main</code>.  Every <span class="application">gtkmm</span> program must have one of these.  We pass
our command-line arguments to its constructor. It takes the arguments
it wants, and leaves you the rest, as we described earlier.
</p>
<p>
Next we make an object of our <code class="classname">HelloWorld</code> class, whose constructor
takes no arguments, but it isn't visible yet. When we call Gtk::Main::run(), giving it the helloworld Window, it shows the 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, run() will return, causing the final line of our main() function be to executed. The application will then finish.
</p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-intermediate-types.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="chapter-button-widget.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Intermediate types </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. Buttons</td>
</tr>
</table>
</div>
</body>
</html>