Chapter 2. Getting Started with the Inti::Gtk Module

Table of Contents
Simple Hello, World
Larger Hello, World
Review of Hello, World Key Points
Containers
Gtk::Widget Base Class

The Inti::Gtk namespace contains Inti's GUI toolkit interface. This chapter introduces Inti::Gtk. There are two "Hello, World" programs in the chapter; if you've never used a GUI toolkit, you might want to look at the section called Simple Hello, World, if you're more experienced with GUI toolkits and C++ you could jump straight to the section called Larger Hello, World.

Simple Hello, World

#include <inti/gtk/window.h>
#include <inti/gtk/button.h>
#include <inti/main.h>
#include <iostream>

using namespace Inti;

void
on_button_clicked ()
{
  cout << "Button was clicked!" << endl;
}

int
main (int argc, char **argv)
{
  using namespace Gtk;
  
  Main::Loop loop;
  
  init (&argc, &argv);

  Window * win = new Window;
  Button * button = new Button ("Hello, World!");

  win->add (button);
  
  win->sig_destroy ().connect (&loop, &Main::Loop::quit);
  button->sig_clicked ().connect (on_button_clicked);
  
  win->show_all ();
  
  loop.run ();

  return 0;
}
    

The first step in any Inti::Gtk application is to initialize the library; this is done with the Inti::Gtk::init() function.
  init (&argc, &argv);
    
Initializing the library involves connecting to the user's display, and parsing command line arguments that Inti::Gtk understands.

Next the program creates some widgets. Widgets are simply GUI objects on the screen, in this case a window and a push button.
  Window * win = new Window;
  Button * button = new Button ("Hello, World!");
    

To make the button appear inside the window, use the add() method:
  win->add (button);
    
Without this line, the window would be empty, and the button wouldn't appear on the screen (because all widgets must be inside a window before they can be displayed).

Widgets should cause something to happen when the user interacts with them. Signals allow you to execute code in response to user actions. You can think of a signal as a list of functions or methods to be invoked when some interesting event occurs. Adding a function or method to be invoked is called connecting to the signal. Invoking the list of functions is referred to as emitting the signal.

This short "Hello, World" connects to two signals.
  win->sig_destroy ().connect (&loop, &Main::Loop::quit);
  button->sig_clicked ().connect (on_button_clicked);    
    
First it connects the quit() method of Main::Loop to the destroy signal of the window; this causes the application to exit when the window is closed. Second, it connects the function on_button_clicked() to the clicked signal; when the user clicks the button, on_button_clicked() will be called.

There are two final steps to take, after connecting signals. First, the show_all() method of Gtk::Widget recursively shows a widget and each of its children. So the following code makes the window and button appear on the screen:
  win->show_all ();

Finally, the program enters the main loop. The main loop is simply an infinite loop that sleeps when nothing is happening, and wakes up if the user interacts with the application. A Main::Loop object was created at the start of main():
  Main::Loop loop;
The run() method enters the main loop:
  
  loop.run ();
Remember that the Main::Loop::quit() method was connected to the window's destroy signal. The quit() method causes the run() method to return, and at that point the end of main() will be reached and the program will exit.

That's all there is to a simple "Hello, World." The next section introduces some more of Inti::Gtk's features, and goes into more detail about the features described so far.