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. We chose GTK+ rather than Qt or wxWidgets because it is written in C, so the binding is easier than with C++.
GTK+ is a free software cross-platform graphical library, distributed under GNU GPL 3 license and available for Linux, Unix, Windows and Mac OS X. And gtk-fortran offers currently an interface to more than 9400 GTK+ 2 and GTK+ 3 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. Although gtk-fortran development is mainly focused on Linux, some users have also built it successfully under BSD, Mac OS X, Windows 7. It has also been tested on 32 bits and 64 bits machines and on Big-endian and Little-endian machines.
GTK+ being a huge library, only some hundreds of its functions have been tested. That is the reason why gtk-fortran must be considered as in the alpha development stage. See the list of Tested functions and the Status page for more informations. Note also that gtk-fortran goes beyond programming GUI because GTK+ includes the crossplatform GLib library which offers a lot of generic functions (regular expressions, random numbers, hash, strings, input/output...)
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. Every good will is welcome.
julia_pixbuf.f90 example with gtk-fortran on Ubuntu
- master is for GTK+ 2.24.22, GLib 2.38.1
- gtk3 is for GTK+ 3.10.1, GLib 2.38.1
- gtk2-old is for gtk+ 2.24 and glib 2.28.
- gtk3-old is for gtk+ 3.2 and glib 2.30.
If you have an older version of GTK+ 2 or GTK+ 3, you will have no problem because deprecated functions will be removed only in the next major version. With glib, deprecated functions are sometimes removed from one minor version to another.
- 03-02-2016: a bug in GCC 5 prevents at the moment the building of some files in gtk-fortran but a workaround is proposed in the Issue 89.
- 01-22-2013: Arch PKGBUILDs submitted to the AUR for Arch and Arch-derived distros. Packages are
- 08-19-2012: Reorganize branches.
- 08-10-2012: Plplot integration with gtk-fortran is now automatically built by the
cmakebuild system if plplot libraries and module files are found on the system, a tutorial on using plplot and gtk-fortran is under construction. Plplot integration
- 07-24-2012: PLplot functionalities have been added to gtk-fortran ! (see the graphics/plplot directory).
- 05-18-2012: NULL, CNULL, FNULL parameters replaced by the corresponding iso_c_binding names (c_null_ptr...) in all the files of the project (master and gtk3 branch).
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): you need gfortran 4.6 or higher. Recent Linux distributions come with it.
To download all the project files, you can simply clone the repository using git (see git-basics). Alternatively you can 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.pl perl script can be useful to find which GTK+ functions are used in your programs (see advices to lower compilation time).
You also need to install the GTK+ development files (Ubuntu example):
sudo apt-get install gfortran libgtk2.0-dev libgtk-3-dev
sudo tar -xzvf gfortran-4.6-20110107-linux-i686.tar.gz -C /
The path to the compiler is then
- 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).
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.
You will need:
- Fortran compiler (recommended is gfortran 4.6 or higher: included in MinGW http://sourceforge.net/projects/mingw)
- CMake http://www.cmake.org/cmake/resources/software.html
- sed http://sourceforge.net/projects/gnuwin32
- PLplot http://sourceforge.net/projects/plplot (optionally)
You can install the dependencies manually or you can use the win32_install.bat script (only in the testwin32gtk* branches) for a semiautomatic installation (except PLplot). The latter requires only wget (http://gnuwin32.sourceforge.net/packages/wget.htm) and PLplot to be installed manually.
Windows binaries of PLplot are not distributed. Therefore you have to build your own binaries (after installation of MinGW and Cmake):
- download from http://sourceforge.net/projects/plplot (latest version is 5.9.9)
- extract the archive
- cd to the extracted archive folder
cmake -DCMAKE_INSTALL_PREFIX=<installation directory> -DCMAKE_C_COMPILER=gcc -G "MinGW Makefiles" ..(be careful to use NO space in
<installation directory>, e.g. C:\Program Files\PLplot will not work!!!)
<installation directory>\lib\plplot5.9.9\driversdto PATH
<installation directory>\lib\pkgconfigto PKG_CONFIG_PATH
Known Problems and solutions
- GTK libraries not found after installation: Do you have spaces in the installation directory name? (e.g. C:\Program Files\GTK will not work!!!) - Reinstall GTK+ in a directory without spaces.
- Some GTK+ symbols are not found by the linker: Do you have older libgtk versions in PATH before the current version (installed by some other software, e.g. Graphviz)? - Prepend the directory with the current GTK+ libraries to PATH.
gtk-fortran was successfully tested on the following Windows platforms:
- Windows 7 (32 bits) with gfortran 4.6.0 20110115 (experimental) and the dll of GTK+ 2.22.1, put in the src directory.
- Windows XP SP3 (32 bits) with gfortran 4.7.2-1 and GTK+ 2.24
- Windows XP SP3 (32 bits) with gfortran 4.7.2-1 and GTK+ 3.6.1
You can also use the (unmaintained) test.bat script to compile all the libraries and examples.
The gtk-fortran developers are mainly working under linux, so do not expect excessive support. If you are a Windows developer, your help would be highly appreciated. You can also read this interesting page about using "GTK-Fortran with Code::Blocks IDE" under Windows 7 64bits, and also Creating a GUI using GTK3 and gtk-fortran. See also the Fortran Tools document in the Useful links section.
The best way is to use CMake: follow the instructions of the "INSTALL" file. If you do not have CMake, you can alternatively use the test.sh bash script to compile all the library and all examples using gfortran (there is also test95.sh for g95, but it is not maintained).
Use CMake via win32_install.bat or alternatively the (unmaintained) test.bat script to compile all the libraries and examples (more details in the installation section).
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 9000 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.pl perl script can scan your Fortran source files for GTK+ and related library functions and generate the required USE statements. For more informations type:
perl usemodules.pl --help
Note also that the compilation time with gfortran 4.6 under Windows is ten times longer than under Linux (why ?)
Known issues or limitations
- gstdio.h and giochannel.h were excluded because some functions are declared twice (UNIX and non UNIX systems for example).
- 90 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
- Very few GTK+ types are not yet implemented.
- Macro functions (#define) are not implemented.
- Structures (struct) are not implemented.
- Some enums are still missing, like GdkPixdataType.
- All enums are in the gtk module. It would perhaps 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).
- Fortran has no unsigned integers, so problems could occur if you pass great values between Fortran and GTK+. See the tests.f90 file.
- Missing deallocate in gtk_init() ? (g95 message when the program is quited).
See also the Issues page.
How can I help ?
- We need people compiling and running the examples under various OS (particularly Windows), 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 (
- 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.
History and Roadmap
- 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...
- The first phase (proof concept) is over.
- Second phase: writing more examples to test a maximum of widgets, with GTK+ 2 & 3.
- Planning a 0.1 release and publish packets for some Linux distributions.
Gtk-fortran is licensed under GNU General Public License version 3.
The wiki documentation is under the GNU Free Documentation License 1.3. The gtk-fortran logo uses the GTK+ logo (by Andreas Nilsson) and so is also under GNU Free Documentation License Version 1.2 or later & Creative Commons Attribution-Share Alike 3.0 Unported license. The font is DejaVu Sans, a font under a free license.
The GTK+ Project
- GTK+ 3:
- GTK+ 2:
- 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.
- GTK+ Programming Forum.
- A french GTK+ forum (developpez.com)
- Drawing with GTK+:
- Related projects:
- Fortran ressources:
Projects using gtk-fortran
- Fortran Tools - Chapter 15: GTK (p.117-140): this document edited in 2014 by The Fortran Company contains a long chapter about setting up gtk-fortran under Windows, and about programming GTK+ GUI in Fortran.
- How to use GTK-Fortran with Code::Blocks IDE
- gtk-fortran is referenced in the following book: Markus, Arjen. Modern Fortran in Practice. 1st ed. Cambridge: Cambridge University Press, 2012. http://dx.doi.org/10.1017/CBO9781139084796.
- ForGE - Fortran GUI Environment
- RPN Scientific calculator
Tools used in this project
- GNU Fortran (gfortran). You can download the last builds of gfortran (i686).
- Scripting languages: Python, Perl.
- To test regular expressions: regexxer and Kodos. See also the Regular Expression Library site, with its online tester, and REGEXPER.
- 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.
- CMake, the cross-platform, open-source build system.
- Meld, a visual diff and merge tool.
- Linux Ubuntu, Debian, Fedora, Arch Linux, Pardus...