Image viewer

In this tutorial, you will learn:

  • Some basic concepts of C++/GObject programming

  • How to write a Gtk application in C++

Create a project in Anjuta

Before you start coding, you'll need to set up a new project in Anjuta. This will create all of the files you need to build and run the code later on. It's also useful for keeping everything together.

  1. Start Anjuta and click File ▸ New ▸ Project to open the project wizard.

  2. Choose GTKmm (Simple) from the C++ tab, click Forward, and fill out your details on the next few pages. Use image-viewer as project name and directory.

  3. 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.

  4. Click Apply and the project will be created for you. Open src/main.cc from the Project or File tabs. You should see some code which starts with the lines:

    #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::Containers 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;
}
  1. 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.

  2. 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.

  3. 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.

  4. 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.

Build and run the application

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!

Reference Implementation

If you run into problems with the tutorial, compare your code with this reference code.