-
Notifications
You must be signed in to change notification settings - Fork 46
Tutorial 4
This tutorial is for GTK 3 only. Concerning GTK 4, be patient, a new rapid application development (RAD) tool is under active development: the Cambalache project was released in 0.10.0 version in June 2022. Note also that our gtkf-sketcher tool was not fully ported to GTK 4.
You have three solutions to create a GUI with gtk-fortran:
- hard-coding the GUI in Fortran, inspiring yourself with the gtk-fortran examples. In my opinion, it's better to first learn this way, in order to understand the GTK concepts.
- Using Glade to create a
.ui
XML file and use the GtkBuilder API, - or write yourself your
.ui
file and use the GtkBuilder API.
This tutorial is about the solutions 2) and 3): you will discover how our gtkf-sketcher tool can help.
Using GtkBuilder maybe be useful if you use many widgets in your API: you can design your GUI using the UI Designer Glade3, then save it to a GktBuilder XML-file. In your gtk-fortran application, you will use the gtk_builder_new_from_file() function to build the GUI on the fly. Of course, the callback functions of your GUI will not be in the XML file: you will have to code them in Fortran, using the same names as defined in the XML-file. Our gtkf-sketcher
tool (inspired by the Freebasic code sketcher GTKtobac2) can ease the pain by automatically generating sketches of those Fortran callback functions.
gtkf-sketcher comes with an example XML file named example.glade
. You can learn to use gtkf-sketcher by playing with it:
- go into the
gtk-fortran/build/sketcher/
directory and run./gtkf-sketcher
- From its File>Open menu, load the
example.glade
file available in the same directory (do not choose thegtkf-sketcher.glade
which is the file used to build the GUI of gtkf-sketcher itself!). - In the Info tab, the content of the
example.glade
file is analyzed. - Keep the default options available in the Options tab and go directly to the Output tab.
- Click on the Write Files button.
- In the terminal, go inside the
gtk-fortran/build/sketcher/example
directory where gtkf-sketcher has generated the Fortran files of the example application:
gtk-fortran/build/sketcher/example$ ls -1 *.f90
example.f90
handler_button2clicked.f90
handler_delete_event.f90
handler_destroy.f90
handler_hello.f90
- Compile and execute the example application:
$ gfortran example.f90 -o example $(pkg-config --cflags --libs gtk-3-fortran)
$ ./example
- You can see a basic application with three buttons: Button1, Button2, Exit. Of course, you will see some warnings like
(example:12807): Gtk-WARNING **: 15:06:45.542: Could not find signal handler 'destroy'. Did you compile with -rdynamic?
and the GUI does nothing because you now need to add code to manage each event into the specified files.
Now you can start your application starting from this example!
- Installation
- My first gtk-fortran application
- Drawing an image in a PNG file (without GUI)
- A program also usable without GUI
- Using Glade3 and gtkf-sketcher (GTK 3)
- Using gtk-fortran as a fpm dependency
- Debugging with GtkInspector
- Learning from examples
- Video tutorials
- How to start my own project from a gtk-fortran example
- git basics
- CMake basics
- Alternatives to CMake
- How to migrate to GTK 4
- How to contribute to gtk-fortran
- How to hack the cfwrapper with other C libraries