Hello World in gtkmm
Prev | Chapter 3. Basics | Next

## Hello World in gtkmm

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 gtkmm:

Source Code

File: helloworld.h

    #ifndef GTKMM_EXAMPLE_HELLOWORLD_H
    #define GTKMM_EXAMPLE_HELLOWORLD_H

    #include <gtkmm/button.h>
    #include <gtkmm/window.h>

    class HelloWorld : public Gtk::Window
    {

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

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

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

    #endif // GTKMM_EXAMPLE_HELLOWORLD_H

File: helloworld.cc

    #include "helloworld.h"
    #include <iostream>

    HelloWorld::HelloWorld()
    : m_button("Hello World")   // creates a new button with the 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
      // hello() method. The hello() method is defined below.
      m_button.signal_clicked().connect(sigc::mem_fun(*this, &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 << "Hello World" << std::endl;
    }

File: main.cc

    #include <gtkmm/main.h>
    #include "helloworld.h"

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

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

      return 0;
    }

Try to compile and run it before going on. You should see something like this:

Figure 3.1. Hello World

Pretty thrilling, eh? Let's examine the code. First, the `HelloWorld` class:

    class HelloWorld : public Gtk::Window
    {

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

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

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

This class implements the "Hello World" window. It's derived from `Gtk::Window`, and has a single `Gtk::Button` 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:

    HelloWorld::HelloWorld()
    :
      m_button ("Hello World")
    {
      set_border_width(10);
      m_button.signal_clicked().connect(sigc::mem_fun(*this, &HelloWorld::on_button_clicked));
      add(m_button);.
      m_button.show();
    }

Notice that we've used an initialiser statement to give the `m_button` object the label "Hello World".

Next we call the Window's `set_border_width()` method. This sets the amount of space between the sides of the window and the widget it contains.

We then hook up a signal handler to `m_button`'s `clicked` signal. This prints our friendly greeting to `stdout`.

Next, we use the Window's `add()` method to put `m_button` in the Window. (`add()` comes from `Gtk::Container`, which is described in the chapter on container widgets.) The `add()` method places the Widget in the Window, but it doesn't display the widget. gtkmm widgets are always invisible when you create them - to display them, you must call their `show()` method, which is what we do in the next line.

Now let's look at our program's `main()` function. Here it is, without comments:

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

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

      return 0;
    }

First we instantiate an object called `kit`. This is of type `Gtk::Main`. Every gtkmm 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.

Next we make an object of our `HelloWorld` 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 gtkmm event loop. During the event loop gtkmm 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.

Prev | Up | Next
Intermediate types | Home | Chapter 4. Buttons