Skip to content

Tutorial 4

vmagnin edited this page Jun 17, 2021 · 11 revisions

How-to use Glade3 and gtkf-sketcher

This how-to is for GTK 3 only, Glade being not yet ported to GTK 4 and our gtkf-sketcher being not (yet?) fully ported to GTK 4.

The GtkBuilder API

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

Using GtkBuilder maybe be useful if you use many widgets in your API.

The gtk-fortran code sketcher

gtkf-sketcher aims to be a tool for rapid application development (RAD) and is inspired by the Freebasic code sketcher GTKtobac2. It can be used to generate/update Fortran source code for an existing Glade3 GUI-XML file via gtk-fortran to implement cross-platform GUI widgets.

The design of the GUI can be completely accomplished using the UI Designer Glade3, where the GUI is saved as an GktBuilder XML-file. gtkf-sketcher can load and analyze this file and generates cross platform Fortran source code to show and handle the GUI-widgets.

This is a very basic manual for gtkf-sketcher:

  • 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.
  • Compile and execute the example application:
$ gfortran example.f90 -o example $(pkg-config --cflags --libs gtk-3-fortran)
$ ./example

You can then add code to manage each event of your GUI into the specified files.

Clone this wiki locally