Skip to content

Learning from examples

vmagnin edited this page May 25, 2023 · 9 revisions

Once you will have created your first gtk-fortran toy applications, you will need to use more widgets and GTK advanced features. Learning by reading and playing with gtk-fortran and GTK examples is a good way.

The gtk-fortran examples

Let's have a look at some gtk-fortran examples available in the examples/ and plplot/ directories or the gtk-fortran-extra repository, starting from the simplest ones and moving progressively toward more complex examples.

The most basic examples

Take gtkzero_gapp.f90 as a starting point. It just creates an empty GTK window with around 30 lines of Fortran code:

An empty gtk-fortran window

Then have a look at gtkhello.f90 which opens a window with two buttons. You will see how you can create buttons, arrange them in a box container and link them to callback functions.

A gtk-fortran window with two buttons

These two examples are heavily commented in order to help you learn the GTK basics.

Bitmap drawing examples

If you want to draw progressively a picture pixel by pixel during a long scientific computation, you should first work on the Drawing an image in a PNG file (without GUI) short tutorial to discover the GdkPixbuf library. Then the mandelbrot_pixbuf.f90 will show you how a GdkPixbuf can be drawn on a GtkDrawingArea via Cairo.

Mandelbrot set plotted with gtk-fortran on macOS

The mandelbrot_pixbuf_zoom.f90 is more elaborated as it shows how to manage the mouse and keyboard to zoom in the Mandelbrot set.

Cairo vectorial drawing examples

The simplest example to learn to make vector graphics is the cairo-basics.f90 example, which just open a window and draws lines, circles, bezier curves and text using the Cairo library. It draws on screen but also in a SVG file and a PDF file:

gtk-fortran Cairo basics example (Ubuntu)

You can also study the Unknown Pleasures example in the gtk-fortran-extra repository (MIT license).

The cairo-tests.f90 example demonstrates it is possible to mix bitmap and vectorial drawing on the same GtkDrawingArea.

Graphical User Interface

When you will feel ready to work on more complicated graphical user interfaces, the julia_pixbuf.f90 will be a good starting point. It combines bitmap drawing, buttons, combo-boxes, spin buttons, grid layout, text view, tabs, etc.

The julia_pixbuf gtk-fortran example

You can also study the Saville Code example in the gtk-fortran-extra repository (MIT license).

The menubar.f90 and notebooks.f90 examples explore specific widgets and show how to create a menu and how to manage notebooks with tabs.

Problems specific to scientific computing

In the gtk-fortran-extra repository (MIT license), you will find:

  • my_fast_app: an example for a fast computation (ideally < 1 second). In that case, you can use the GtkApplication API.
  • my_long_app: an example for long computations, with a picture that is updated regularly. In that case you must manage the events of the GLib main loop to keep the GUI reactive during the computation.
  • parallel_app/ demonstrates how you can use parallel Fortran 2008 and 2018 features (coarrays, collective routines...) in a gtk-fortran application.

The GTK examples

You can also run the gtk4-demo command (gtk-4-examples package in Ubuntu) to launch each official GTK example and read directly its C source code.

Clone this wiki locally