GTK-Fortran with Code::Blocks IDE
Gtk+ (http://www.gtk.org/) is a cross-platform toolkit for creating Graphical User Interfaces (GUI). Gtk-fortran (https://github.com/jerryd/gtk-fortran/wiki) enables creation of GUI from Fortran using this toolkit. I tried to build few gtk-fortran examples on Windows 7 64bit. This is my instructions / notes how to use gtk-fortran on Code::Blocks. I hope it will be useful for users of C::B.
How to use gtk-fortran on C::B IDE:
If you don't have gfortran on your system, install it. I used version 4.7 here (somewhat older version).
Download GTK+ prebuild binaries (http://www.gtk.org/download/win32.php). I tested version "GTK+ 2.24" but GTK+ 3.x should work too. Choose "all-in-one" bundle. Create directory where you like, and extract downloaded zip file. In Readme.txt is recommendation to avoid Winzip! Read README.txt which comes with GTK. Then add the GTK bin folder to your PATH environment variable. Verify that it works.
Download gtk-fortran (https://github.com/jerryd/gtk-fortran). You will find "Download ZIP" on the right side of the page. Unzip it where you like.
Download archived directory “test_gtk_fortran_1.zip”. Unzip it. It contains C::B test project. Source files are few files from "gtk-fortran/examples", "gtk-fortran/src" and “gtk-fortran/plplot” required to compile selected examples. Open the project with C::B. You will be asked to define global variable "gtk” with value pointing to the directory where GTK libraries were extracted (for help you: below this directory are “bin”, “etc”, “include” etc. directories). Also you will be asked to define global variable “plplot”. If you haven’t installed plplot library (yet?), point it to random folder.
Select one of the targets, compile it, run it. You may use this project file as a base for your own experiments. This project file contains files from "gtk-fortran/src" required to compile the selected examples. Other examples or your own work may require more files from "gtk-fortran/src". Copy them to “gtk_src” subdirectory and add to your project. Note: some of the gtk-fortran files are "includes" (they are included in other files). Include files don't need to be added into C::B project. They just should be beside other source files. Another possibility with "includes" is to add them into C::B project, but exclude them from the compilation (if you try to compile include file, compiler may or may not complain about it). And again: DON’T compile include files (you may end with strange error messages, when you try to start your app). To make file "uncompilable" by C::B, on the file properties dialog (right click on the filename in C::B Projects tree, and select "Properties...") unselect "Compile file" and "Link file". An advantage to have include files in the project is that you have the code-completion for names defined in those include files.
If you use GTK+2, the source files from gtk-fortran for GTK+2 should be included into your project. And if you use GTK+3, the source files from gtk-fortran for GTK+3 should be included into the project.
(Next steps are optional.)
If you like to have the visualization of your calculation results, you may use “PLplot” library. It allows you to create scientific plots directly in your gtk window. There are other visualization possibilities like “Gnuplot”, “Dislin”, but they allow you to show your plot in a separate non-gtk window only (please, correct me if I am wrong). Alternatively, MathGL library can be used too (how to use it with Fortran read here).
(optional) First, you should compile Plplot from sources: Download Plplot source code from “plplot.sourceforge.net”. In my case it was “plplot-5.10.0.tar.gz” file. Extract it.
(optional) For compilation of Plplot, “CMake” build system is used. Download it from “www.cmake.org” and install it. By installing CMake, ask to add CMake’s bin directory to your PATH.
-
Most instructions how to compile Plplot are for Linux world only. Therefore, below is my step-by-step instruction:
Create “build” directory where you extracted Plplot library.
Open “cmd” shell and navigate to this directory.
Type command in the shell: “cmake -G "MinGW Makefiles" -DDEFAULT_NO_BINDINGS=ON -DENABLE_f95=ON -DCMAKE_INSTALL_PREFIX=install .. “ (best, copy it from here)
Type commands: “mingw32-make” and then “mingw32-make install”
(here I assumed you have mingw32-make.exe on your PATH) Now you should have Plplot library installed on your system (<directory_with_plplot_source>\build\install)
(optional) Add "<directory_with_plplot_source>\build\install\bin" and "<directory_with_plplot_source>\build\install\lib\plplot5.10.0\driversd" to your PATH environment variable.
(optional) Open C::B. Set “plplot” global variable to “<directory_with_plplot_source>\build\install”. Open project from “test_gtk_fortran_1.zip”. Several targets “hl_plplot*” are examples from “gtk-fortran/plplot”. Compile and run them.
Some suggestions for Fortran developers how to use GTK
How to create GUI
I think, the easiest way to create GUI for your Fortran application is to use Glade (glade.gnome.org). In Glade by mouse only (almost) you can construct sophisticated windows/ dialogs without knowing many details of each GUI element. Later, you just need to load “*.glade" file and connect callback functions (signal handlers) in your Fortran code. Note that if you are using GTK+2, then you should install Glade 3.8, because Glade >3.8 is targeting for GTK+3 only.
About how to use Glade From https://glade.gnome.org/: "Glade is a RAD tool to enable quick & easy development of user interfaces for theGTK+ toolkit… The user interfaces designed in Glade are saved as XML, and by using the GtkBuilder GTK+ object these can be loaded by applications dynamically as needed."
Glade is a separate program which helps to build GUI. Users build dialogue using Glade and saves it in "*.glade" file. From Fortran (gtk-fortran binding) you load this "*.glade" file during program execution using "gtk_builder_*" functions. In Fortran you just need (a) toload this “*.glade” file, (b) obtain pointers to some GUI elements and (c) register callback functions. All three steps are done using GtkBuilder functions ( see link ). You should find a lot of tutorials how to use Glade on internet. You can try this: http://blog.borovsak.si/2009/09/glade3-tutorial-1-introduction.html .
There are two basic examples of using *.glade file which comes with gtk-fortran in "examples" subdirectory: gtkbuilder.f90 and gtkbuilder2.f90.
About the structure of the code
I would suggest you to follow a bit different code structuring style, than it is used in gtk-fortran examples, i.e. to use more object-oriented style. My suggestions: a) Create one separate module for each window / dialog. b) Create a derived type, which contains pointers to window, text fields, other relevant GUI elements of the window and additional data if required. c) Create constructor-like subroutine, in which “*.glade” file is loaded, pointers to GUI elements are referenced and callback functions are registered. d) Send c-pointer to the instance of your derived type during registrationof the callback functions as an “user_data” argument. Through this argument you can access to your instance of derived type in the callbackfunctions.
An example of such coding style is included in an archive file which contains Code::Blocks project file also.
Note for GTK+ 3.6.4 users on Windows
While I have used GTK+2, I don't see any reason, why not to use GTK+3. Just corresponding version of gtk-fortran should be used to compile your project. One note: by default, fonts are ugly in GTK+3.6.4 applications (just because some settings were overlooked during packaging). According to “http://forum.gtkd.org/groups/GtkD/thread/57/”, to correct how fonts are displayed, add following lines into “settings.ini” file:
File: <your_gtk_installation_dir>\etc\gtk-3.0\settings.ini
Add the following into the file:
[Settings] gtk-xft-antialias = 1 gtk-xft-rgba = rgb #You can also change the default font if needed with: gtk-font-name = Arial 10
Downloads
test_gtk_fortran_1.zip - Several examples from gtk-fortran/examples and C::B project.
test_say_hello_1.zip - An example, which uses Glade file.