En aquest programa d'aprenentatge aprendreu a:
Some basic concepts of C++/GObject programming
How to write a Gtk application in C++
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
Trieu
Make sure that
Feu clic a
#include
#include "config.h">]]>
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
Press
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
:
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.
We will now define the signal handler for the clicked signal or the
button we mentioned before. Add this code before the main
method.
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 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
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 dialog.run
will return the value Gtk::RESPONSE_ACCEPT
(it would return Gtk::RESPONSE_CANCEL
if the user clicked switch
statement tests for this.
We hide the
Assuming that the user did click Gtk::Image
so that it is displayed.
All of the code should now be ready to go. Click
If you haven't already done so, choose the
Si teniu algun problema amb el programa d'aprenentatge, compareu el codi amb el codi de referència.
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 transfers and the like, and GNOME Scan to handle scanning.