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 developed since 1998, written in C, distributed under GNU GPL 3 license and available for Linux, Unix, Windows and Mac OS X. And gtk-fortran offers an interface to around 10000 GTK+ 2 and GTK+ 3 functions (GTK, GDK, GdkPixbuf, Cairo, Pango, ATK, GLib, GObject, GIO). Although gtk-fortran development is mainly focused on Linux, some users have also built it successfully under BSD, Mac OS X, Windows 7. 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.
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:
- GTK+ includes the crossplatform GLib library which offers a lot of generic functions (regular expressions, random numbers, hash, strings, input/output...),
- and gtk-fortran offers also an interface to PLplot.
julia_pixbuf.f90 example with gtk-fortran on Kubuntu 16.04 with GTK+ 3.18.9
You will find more Screenshots on the wiki and even more in the screenshots directory (especially in the gtk3 branch). See also jtappin's RPN Scientific calculator, a good example of what can be done with gtk-fortran.
- 07-18-2016: gtk3 changes backported to the master (gtk2) branch. CMake files have been unified.
- 07-04-2016: gtk3 branch has been updated.
- 06-25-2016: The code of the heart of gtk-fortran, the cfwrapper.py script, has been refactored and improved in order to ease maintenance. The new version is in the gtk3 branch, with the latest versions of gtk-fortran files.
- 06-15-2016: gtk-fortran 16.04 released.
- 06-11-2016: the test.sh script has been updated and can be used as a primitive alternative to compile gtk-fortran and its examples.
- 03-02-2016: a bug in GCC 5 prevents CMake to build two examples of gtk-fortran. The workaround (see Issue 89) is simply to type
make -iinstead of
make, to ignore errors during the build.
- 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
gtk-fortran is a three-stage rocket:
- The Python cfwrapper.py script parses the GTK+ .h files and generates several .f90 files containing interfaces to the functions of the GTK+ libraries. Normally, you do not need to use cfwrapper.py except when you need a new GTK+ function whose interface is not available in the gtk-fortran branches available on this site.
- The gtk.f90 and the *-auto.f90 files constitute the source of the gtk-fortran library which you can use directly in your own programs.
- But you can also decide to use the high-level-interface-for-Gtk-Fortran (hl-*.f90 files) which offers a simplified interface to some GTK+ functions.
Installation and building
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. Most recent Linux distributions come with gfortran 5.3 or higher.
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. The files 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).
On your system, you need the GTK+ development files and CMake.
You will find the latest versions of gtk-fortran in the master branch for GTK+ 2 and the gtk3 branch for GTK+ 3. You can also download a tagged release by choosing one in the "Switch branches/tags" selection window:
- v16.04.gtk2.24.30: compiled with GTK+ 2.24.30, GLib 2.48.0 (Kubuntu 16.04),
- v16.04.gtk3.18.9: compiled with GTK+ 3.18.9, GLib 2.48.0 (Kubuntu 16.04),
- v13.10.gtk2.24.22: compiled with GTK+ 2.24.22, GLib 2.38.1 (October 2013),
- v13.10.gtk3.10.1: compiled with GTK+ 3.10.1, GLib 2.38.1 (October 2013).
If you have an older version of GTK+ 2 or GTK+ 3, you should have no problem because deprecated functions will be removed only in the next major version. But with GLib, deprecated functions are sometimes removed from one minor version to another, which can prevent building gtk-fortran on your machine. In that case, you can also test the older branches gtk2-old and gtk3-old (August 2012).
First, you need to install the GTK+ development files (Ubuntu example):
sudo apt-get install gfortran libgtk2.0-dev libgtk-3-dev
Depending on your system, you may also need other packages.
The best way to build gtk-fortran and install its libraries on your system is to use CMake: follow the instructions of the "INSTALL" file.
But if you do not have CMake, you can alternatively use the test.sh bash script to compile all the gtk-fortran files and all examples, using gfortran by default. To use another compiler, use the following syntax:
And if you know which gtk-fortran files are needed, you can also simply compile and run each example as follow, from the src/ directory:
$ gfortran gtk.f90 ../examples/julia_pixbuf.f90
pkg-config --cflags --libs gtk+-3.0
Mac OS X
Search in the Issues tab to find message from Mac OS X users. 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 MS Windows page. 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).
You can begin by studying the examples available in the
examples/ directory, beginning by gtkhello2.f90. And the following book is a good way to learn GTK+:
- 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.
More links to learn and use GTK+ are given at the end of this page.
Some external docs about 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.
Projects using gtk-fortran
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. The list of all the functions available in gtk-fortran, with informations on the required files and the C prototype of the function, is in the src/gtk-fortran-index.csv file.
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
Parsing more than 700 .h files is not an easy task. So gtk-fortran does not implement 100% of GTK+ functions and have some limitations:
- gstdio.h and giochannel.h were excluded because some functions are declared twice (UNIX and non UNIX systems for example).
- Around 90 functions with optional arguments (variadic fuctions) are not implemented. But 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.
History and Roadmap
This project is based on Tobias Burnus' post on comp.lang.fortran newsgroup (2nd May 2007 13:10) where he gave an 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...
- Planning a 0.1 release and publish packets for some Linux distributions ?
How can I help ?
- We need people compiling and running the examples under various OS (particularly Windows and Mac OS), architectures (32/64 bits...) and with various Fortran compilers. You can report bugs and problems in the Issues tab.
- 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 (
- 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.
- The GTK+ Project
- Drawing with GTK+:
- Related projects:
- Fortran ressources:
- Fortran 2008 Standard Draft (WD 1539-1)
- 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.
- Richard J. Hanson, Tim Hopkins, Numerical Computing with Modern Fortran. Philadelphia: SIAM, 2013, ISBN 978-1-611973-11-2
- Markus, Arjen, Modern Fortran in Practice. 1st ed. Cambridge: Cambridge University Press, 2012. http://dx.doi.org/10.1017/CBO9781139084796.
- Michael Metcalf, John Reid, Malcolm Cohen, fortran 95/2003 explained. Oxford: Oxford University Press, 2004, ISBN 978-0-19-852693-3.
- Arjen Markus, Modern Fortran in Practice. New York: Cambridge University Press, 2012, ISBN 978-1-107-60347-9.
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.
git, the fast version control system.
- Scott Chacon, Pro Git, APress, 2009, a Creative Commons book.
CMake, the cross-platform, open-source build system.
- Ken Martin, Bill Hoffm, Mastering CMake - A Cross-Platform Build System. Kitware, 2010, ISBN 978-1930934313.
- Meld, a visual diff and merge tool.
- Scrot: a command line screen capturing tool.
- Linux Ubuntu, Debian, Fedora, Arch Linux, Pardus...
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.