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.
#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); |
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); |
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); |
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; |
loop.run (); |
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.