A little bit more than a simple "Hello world" GTKmm application.
Projecte de documentació del GNOME
gnome-doc-list@gnome.org
Johannes Schmid
jhs@gnome.org
Marta Maria Casetti
mmcasetti@gmail.com 2013

# Image viewer

      En aquest programa d'aprenentatge aprendreu a:

        Some basic concepts of C++/GObject programming
        How to write a Gtk application in C++

# Creació d'un projecte a l'Anjuta

Abans de començar a programar, heu de crear un projecte nou a l'Anjuta. L'Anjuta crearà tots els fitxers necessaris per, més endavant, construir i executar el codi. És molt útil per així mantenir-ho tot junt.

Inicieu l'Anjuta i feu clic a Fitxer Nou Projecte per obrir l'auxiliar de projectes.

Trieu GTKmm (simple) a la pestanya C++, feu clic a Continua i empleneu les dades de les pàgines següents de l'auxiliar. Utilitzeu image-viewer com a nom de projecte i de directori.

Make sure that Use GtkBuilder for user interface is disabled as we will create the UI manually in this tutorial. Check the Guitar-Tuner tutorial if you want to learn how to use the interface builder.

Feu clic a Aplica i es crearà el projecte. Obriu el fitxer src/main.cc des de la pestanya de Projecte o de Fitxer. El fitxer ja conté unes línies de codi que comencen amb:

#include <gtkmm.h> #include <iostream>

#include "config.h">

# Build the code for the first time

This is a very basic C++ code setting up GTKmm. More details are given below; skip this list if you understand the basics:

The three `#include` lines at the top include the `config` (useful autoconf build defines), `gtkmm` (user interface) and `iostream` (C++-STL) libraries. Functions from these libraries are used in the rest of the code.

The `main` function creates a new (empty) window and sets the window title.

The `kit::run()` call starts the GTKmm main loop, which runs the user interface and starts listening for events (like clicks and key presses). As we give the window as an argument to that function, the application will automatically exit when that window is closed.

This code is ready to be used, so you can compile it by clicking Build Build Project (or press Shift F7).

Press Execute on the next window that appears to configure a debug build. You only need to do this once, for the first build.

# Creating the user interface

Now we will bring life into the empty window. GTKmm organizes the user interface with `Gtk::Container`s that can contain other widgets and even other containers. Here we will use the simplest available container, a `Gtk::Box`:

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

Gtk::Window main_win; main_win.set_title ("image-viewer-cpp");

Gtk::Box* box = Gtk::manage(new Gtk::Box()); box->set_orientation (Gtk::ORIENTATION_VERTICAL); box->set_spacing(6); main_win.add(*box);

image = Gtk::manage(new Gtk::Image()); box->pack_start (*image, true, true);

Gtk::Button* button = Gtk::manage(new Gtk::Button("Open Image…")); button->signal_clicked().connect ( sigc::ptr_fun(&on_open_image)); box->pack_start (*button, false, false);

main_win.show_all_children(); kit.run(main_win);

return 0; }

The first lines create the widgets we want to use: a button for opening up an image, the image view widget itself and the box we will use as a container.

The calls to `pack_start` add the two widgets to the box and define their behaviour. The image will expand into any available space while the button will just be as big as needed. You will notice that we don't set explicit sizes on the widgets. In GTKmm this is usually not needed as it makes it much easier to have a layout that looks good in different window sizes. Next, the box is added to the window.

We need to define what happens when the user clicks on the button. GTKmm uses the concept of signals. When the button is clicked, it fires the clicked signal, which we can connect to some action. This is done using the `signal_clicked().connect` method which tells GTKmm to call the `on_open_image` function when the button is clicked. We will define the callback in the next section.

The last step is to show all widgets in the window using `show_all_children()`. This is equivalent to using the `show()` method on all our child widgets.

# Showing the image

We will now define the signal handler for the clicked signal or the button we mentioned before. Add this code before the `main` method.

Gtk::Image* image = 0;

static void on_open_image () { Gtk::FileChooserDialog dialog("Open image", Gtk::FILE_CHOOSER_ACTION_OPEN); dialog.add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT); dialog.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);

Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create(); filter->add_pixbuf_formats(); filter->set_name("Images"); dialog.add_filter (filter);

const int response = dialog.run(); dialog.hide();

switch (response) { case Gtk::RESPONSE_ACCEPT: image->set(dialog.get_filename()); break; default: break; } }

This is a bit more complicated than anything we've attempted so far, so let's break it down:

The dialog for choosing the file is created using the `Gtk::FileChooserDialog` constructor. This takes the title and type of the dialog. In our case, it is an Open dialog.

The next two lines add an Open and a Close button to the dialog.

Notice that we are using stock button names from Gtk, instead of manually typing "Cancel" or "Open". The advantage of using stock names is that the button labels will already be translated into the user's language.

The second argument to the `add_button()` method is a value to identify the clicked button. We use predefined values provided by GTKmm here, too.

The next two lines restrict the Open dialog to only display files which can be opened by `Gtk::Image`. A filter object is created first; we then add all kinds of files supported by `Gdk::Pixbuf` (which includes most image formats like PNG and JPEG) to the filter. Finally, we set this filter to be the Open dialog's filter.

`Glib::RefPtr` is a smart pointer used here, that makes sure that the filter is destroyed when there is no reference to it anymore.

`dialog.run` displays the Open dialog. The dialog will wait for the user to choose an image; when they do, `dialog.run` will return the value `Gtk::RESPONSE_ACCEPT` (it would return `Gtk::RESPONSE_CANCEL` if the user clicked Cancel). The `switch` statement tests for this.

We hide the Open dialog because we don't need it any more. The dialog would be hidden later anyway, as it is only a local variable and is destroyed (and therefore hidden) when the scope ends.

Assuming that the user did click Open, the next line loads the file into the `Gtk::Image` so that it is displayed.

# Muntatge i execució de l'aplicació

All of the code should now be ready to go. Click Build Build Project to build everything again, and then Run Execute to start the application.

If you haven't already done so, choose the Debug/src/image-viewer application in the dialog that appears. Finally, hit Run and enjoy!

# Implementació de referència

Si teniu algun problema amb el programa d'aprenentatge, compareu el codi amb el codi de referència.

# Next steps

Here are some ideas for how you can extend this simple demonstration:

Have the user select a directory rather than a file, and provide controls to cycle through all of the images in a directory.

Apply random filters and effects to the image when it is loaded and allow the user to save the modified image.

GEGL provides powerful image manipulation capabilities.

Allow the user to load images from network shares, scanners, and other more complicated sources.

You can use GIO to handle network file tranfers and the like, and GNOME Scan to handle scanning.