Skip to content

Tutorial 4

Vincent Magnin edited this page May 8, 2024 · 11 revisions

Using Glade3 and gtkf-sketcher (GTK 3)

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:

  1. 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.
  2. Using Glade to create a .ui XML file and use the GtkBuilder API,
  3. 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.

The GtkBuilder API

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.

Be careful with the names of callback functions in the GktBuilder XML-file: remember that Fortran is not case sensitive but C is. Using lowercase names in the Fortran program is a simple way to avoid some headaches! Another solution is to use the name parameter to tell which name the Fortran compiler will expose to the C side, for example: bind(c, name="button2Clicked")

Playing with gtkf-sketcher

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 the gtkf-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!

Clone this wiki locally