Clone this wiki locally
The gtk-fortran project aims to offer scientists programming in Fortran a cross-platform library to build Graphical User Interfaces (GUI). Gtk-fortran is a partial GTK+ / Fortran binding 100% written in Fortran, thanks to the ISO_C_BINDING module for interoperability between C and Fortran, which is a part of the Fortran 2003 standard. GTK+ is a free software cross-platform graphical library available for Linux, Unix, Windows and Mac OS X.
gtk-fortran offers currently an interface to around 8800 GTK+ 2.24 and 8450 GTK+ 3.0 functions (GTK, GDK, GdkPixbuf, Cairo, Pango, ATK, GLib, GObject, GIO). These interfaces were automatically created by a python script which parses the C header files of the GTK+ libraries. gtk-fortran has already been tested under Linux, Mac OS X, Windows 7, on 32 bits and 64 bits machines, and on Big-endian and Little-endian machines. See the list of Tested functions and the Status page for more informations.
The second phase of the project is now concentrating on testing and writing examples. You can join our small community to share knowledge, skills and code: just create a github account and you will be able to post messages.
julia_pixbuf.f90 example with gtk-fortran on Ubuntu 11.04
- 05-19-2011: Added first version of the GTK+ Fortran code sketcher gtkf-sketcher.
- 05-07-2011: Added tree widget and also table and notebook containers to
- 04-24-2011: Added multi column list to
gtk-hl.f90, also demo in
- 04-19-2011: Added Wiki page documenting
- 04-19-2011: gtk-fortran is now listed on the GTK+ Language Bindings page.
- 04-18-2011: Added File Choosers to
src/gtk-hl.f90, and demo to
- 04-18-2011: all examples successfully tested under Windows 7 & GTK+ 2.22.1, except gtkbuilder2.f90 which needs GTK+ 2.24.
- 04-18-2011: gtk-fortran has been successfully tested on Mac OS X & GTK+ 2.24.4 !
- 04-13-2011: Added ComboBox interface to
- 04-12-2011: Added high-level interfaces for sliders, spin boxes and text view (and example codes).
- 04-11-2011: gtk-fortran is now GTK+ 3.0 ready ! All the examples compile and run under Linux. See the Gtk3 page.
First, you need a Fortran compiler with the ISO_C_BINDING module. If your compiler is compliant with the Fortran 2003 standard, this module should be present. Else you can download gfortran, a free software compiler (see the links at the bottom of this page).
To download all the project files, click on Source (top left), then just below Switch branches to "master" if you use GTK+ 2 or "gtk3" for GTK+ 3, then click on the Downloads button on the right, then choose the .tar.gz or .zip archive depending on your system. The only files you absolutely need are gtk.f90 and the *-auto.f90 files. You can copy them in the same directory as your program or in a specific directory.
gtk-sup.f90 and gtk-hl.f90 are needed for list_demo.f90 and the hl-*.f90 files (see gtk_hl:-A-high-level-interface-for-Gtk-Fortran).
The usemodules.py python script can be useful to find which GTK+ functions are used in your programs (see advices to lower compilation time). Python scripts have been tested under Linux with python 2.6.6 and 2.7.1.
If you use the GNU Fortran compiler, you need gfortran 4.6.0 or higher. After downloading the install file, you can install it by typing something like:
sudo tar -xzvf gfortran-4.6-20110107-linux-i686.tar.gz -C /
The path to the compiler is then
You also need the GTK+ development files:
sudo apt-get install libgtk2.0-dev
- You can also use Fedora 15 which is one of the first distributions to come natively with gfortran 4.6.0 and GTK+ 3.0.9.
- depending on your installation, you may also need the packages libmpc2 and libmpfr1ldbl.
- gtk-fortran has also been tested with the compilers:
- Intel Fortran 12.0.0 on x86-ubuntu-maverick (bdsatish, 02-14-2011).
- g95 version 0.93, Ubuntu x86_64 (bonanza, 02-17-2011).
- g95 (2010-06-16 debian version), Ubuntu 10.10, 32 bits (vmagnin).
All the examples were tested under Windows 7 (32 bits) using gfortran 4.6.0 20110115 (experimental) and the dll of GTK+ 2.22.1, put in the src directory. You can use the test.bat batch file.
You can download the all-in-one GTK+ 2.22 bundle at http://www.gtk.org/download-windows.html. More recent versions of some individual libraries (GTK+ 2.24) can be found at http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/.
Note that for high-level examples you need GTK+ 2.18 or higher, and 2.24 for gtkbuilder2.f90 and julia_pixbuf.f90.
Mac OS X
The examples were tested under Mac OS X 10.6.7 (Darwin 10.7.0 on x86_64), with the quartz version of GTK2. gtk2 and gfortran 4.6 were built using macports.
See the test.sh bash script to compile all the library and all examples using gfortran (test95.sh for g95).
See the test.bat script to compile all the library and all examples.
Advices to lower compilation time
Concerning the names of the modules you need, you can generally just look at the beginning of the names of the functions you use: gtk_window_new is in the gtk module, cairo_curve_to is in cairo... Note that all enumerators are currently in the gtk module.
gtk-fortran modules offering interfaces to more than 8000 functions, you should add to your USE statements the ONLY option, e.g.:
use gtk, only: gtk_init, gtk_window_new, GTK_WINDOW_TOPLEVEL, gtk_window_set_title, & & gtk_container_set_border_width, g_signal_connect, gtk_hbox_new, gtk_container_add, & & gtk_button_new_with_label, gtk_box_pack_start, gtk_widget_show, gtk_main, FALSE, & & CNULL, TRUE
The compilation time will be ten times faster than with a simple "use gtk" statement ! To help you, the usemodules.py script can analyze your programs and generate the usemodules.txt file with the USE statements you need. Simply type for example:
python usemodules.py ../examples
But the script does not find all used symbols and you will generally need to complete the USE statements by hand. Note also that the compilation time with gfortran under Windows is ten times longer than under Linux (why ?)
Known issues or limitations
- Very few GTK+ types are not yet implemented.
- Macro functions (#define) are not implemented.
- Structures (struct) are not implemented.
- gstdio.h and giochannel.h were excluded because some functions are declared twice (UNIX and non UNIX systems for example).
- 85 functions with optional arguments (variadic fuctions) are not implemented. Some of these functions are also available with an array argument to solve binding problems (for example gtk_list_store_new / gtk_list_store_newv), but not all. See http://live.gnome.org/GObjectIntrospection/WritingBindingableAPIs
- Some enums are still missing, like GdkPixdataType.
- All enums are in the gtk module. It would be better either to dispatch them in the right modules or to create an enums module.
- g_signal_connect: should have been be declared as a function returning a c_long (handler_id).
- Missing deallocate in gtk_init() ? (g95 message when the program is quited).
See also the Issues page.
- The first phase (proof concept) is over.
- Second phase: writing more examples to test a maximum of widgets, with GTK+ 2 & 3.
- Future: instead of parsing the C header files with python & regular expressions, we should use the GObject Introspection project. The XML ".gir" files in /usr/share/gir-1.0/ could be parsed by a python script to generate a more complete binding. GTK, GDK, GdkPixbuf, Pango and Atk are supported, but not Cairo (cfwrapper.py would still be needed). See also http://mkestner.blogspot.com/2011/02/signs-of-life.html on the gtk-sharp binding. After installing the gobject-introspection package under Ubuntu, we have the python file /usr/lib/gobject-introspection/giscanner/girparser.py which we could perhaps use to parse the gir files.
How can I help ?
- We need people compiling and running the examples under various OS, architectures (32/64 bits...) and with various Fortran compilers. You can signal bugs.
- You can write new examples or improve existing examples and share them with the community. Note that C examples are available in the applications gtk-demo and gtk3-demo (
- If you have knowledge about GIR format or XML parsing with python, your help can be precious for future versions.
- You can simply talk to your colleagues of this new possibility of creating Graphical User Interfaces for their Fortran programs.
- Put a link on your site to https://github.com/jerryd/gtk-fortran/wiki, and include the "gui" and "graphical user interface" keywords. It will improve the pagerank of the project in search engines.
This project is based on Tobias Burnus' post on comp.lang.fortran newsgroup (2nd May 2007 13:10) where he gave a little example of a Fortran 2003 program creating a minimalist GUI (an empty GTK window !) using ISO_C_BINDING. Vincent Magnin posted a message on the 27th December 2010 and got into contact with Jerry DeLisle on the 29th. They launched together the project in January 2011. Other contributors then joined: James Tappin, Jens Hunger...
Gtk-fortran is licensed under GNU General Public License version 3.
- Why GTK+ rather than Qt or wxWidgets ? We chose GTK+ because it is written in C, so the binding is easier than with C++.
- Is gtk-fortran only useful for GUI ? No because GTK+ includes the crossplatform GLib library which offers a lot of generic functions (regular expressions, random numbers, hash, strings, input/output...)
- The GTK+ Project
- GTK+ Reference Manual (version 2.24)
- GTK+ 3.0:
- GTK+ 2.0 Tutorial in C language.
- GTK+ Programming Forum.
- A french GTK+ forum (developpez.com)
- Cairo documentation
- The Cairo graphics tutorial in C language.
- Andrew Krause, Foundations of GTK+ Development, Apress, 2007, ISBN-13: 978-1-59059-793-4, 630pp, $49.99. You can download the C examples here. And you can have a look at the book on Google Books.
- Pilib: this GTK+ / Fortran 9x project is not maintained anymore by Martin Hierholzer but its code can be inspiring.
- gtk-sharp: a Mono/.NET binding to GTK+, with examples, on github.
- go-gtk: a Go binding to GTK+, with examples, on github.
- John Reid, The New Features of Fortran 2003, ISO/IEC JTC1/SC22/WG5 N1579.
- John Reid, Interoperability with C, an introduction to ISO_C_BINDING.
Tools used in this project
- GNU Fortran (gfortran). You can download the last builds of gfortran (i686).
- Python programming language.
- To test regular expressions:
- Geany, a small and fast IDE for Linux and Windows.
git, the fast version control system.
- Scott Chacon, Pro Git, APress, 2009, a Creative Commons book.
- Meld, a visual diff and merge tool.
- Linux Ubuntu.