Zum Inhalt springen

Fortran: Gtk-fortran

Aus Wikibooks
<< zur Fortran-Startseite
< f03gl BLAS und ATLAS >




Eine relativ neue Möglichkeit GUIs zu erstellen ist gtk-fortran. Dieses Paket stellt das C-Binding für die  GTK-Bibliothek zur Verfügung. Es verwendet den Fortran 2008-Standard.

  • Für die Homepage generell siehe [1].
  • Eine Downloadmöglichkeit ist unter [2] gegeben.
  • Für die Installationsanweisungen siehe [3].
  • Für die Tutorials siehe [4].

Ein Beispiel wird nachfolgend aus dem gtk-fortran-Paket, gekürzt und geringfügig überarbeitet, zitiert. Der Code steht unter der  GNU GPLv3, Copyright (C) 2020 The gtk-fortran team. Der Originalcode findet sich im heruntergeladenen gtk-fortran-Paket unter dem Verzeichnis ..../gtk-fortran-gtk4/examples in der Datei gtkhello.f90.

Fortran 2003 (oder neuer)-Code
module handlers
  use, intrinsic :: iso_c_binding, only: c_ptr, c_int, c_funloc, c_null_char
  use gtk, only: FALSE, gtk_window_set_default_size,  &
               & gtk_window_set_title, gtk_window_destroy, &
               & g_signal_connect, g_signal_connect_swapped, &
               & gtk_widget_show, gtk_application_window_new, &
               & gtk_box_new, gtk_box_append, gtk_window_set_child, &
               & GTK_ORIENTATION_VERTICAL, GTK_ORIENTATION_HORIZONTAL, &
               & gtk_button_new_with_label, gtk_button_new_with_mnemonic
  implicit none

contains
  subroutine activate(app, gdata) bind(c)
    type(c_ptr), value, intent(in)  :: app, gdata
    type(c_ptr) :: window
    type(c_ptr) :: box
    type(c_ptr) :: button1, button2

    window = gtk_application_window_new(app)
    call gtk_window_set_title(window, "Hello GLib & GTK world!"//c_null_char)

    box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10_c_int)

    call gtk_window_set_child(window, box)

    button1 = gtk_button_new_with_label("I say hello"//c_null_char)
    call gtk_box_append(box, button1)
    call g_signal_connect(button1, "clicked"//c_null_char, &
                        & c_funloc(button1clicked))
    call g_signal_connect(button1, "clicked"//c_null_char, c_funloc(hello))

    button2 = gtk_button_new_with_mnemonic("I don't know why you say _goodbye"&
                                          &//c_null_char)
    call gtk_box_append(box, button2)
    call g_signal_connect_swapped(button2, "clicked"//c_null_char, &
                                & c_funloc(gtk_window_destroy), window)
    call gtk_widget_show(window)
  end subroutine activate

  subroutine hello(widget, gdata) bind(c)
    type(c_ptr), value, intent(in)  :: widget, gdata

    print *, "So I say Hello GTK World!"
  end subroutine hello

  subroutine button1clicked(widget, gdata) bind(c)
    type(c_ptr), value, intent(in)  :: widget, gdata

    print *, "Button 1 clicked!"
  end subroutine button1clicked
end module handlers

program bsp
  use, intrinsic :: iso_c_binding, only: c_ptr, c_funloc, c_null_char, c_null_ptr
  use gtk, only: gtk_application_new, G_APPLICATION_FLAGS_NONE
  use g, only: g_application_run, g_object_unref
  use handlers

  implicit none
  integer(c_int)     :: status
  type(c_ptr)        :: app

  app = gtk_application_new("gtk-fortran.examples.gtkhello"//c_null_char, &
                            & G_APPLICATION_FLAGS_NONE)
  call g_signal_connect(app, "activate"//c_null_char, c_funloc(activate), &
                      & c_null_ptr)
  status = g_application_run(app, 0_c_int, [c_null_ptr])

  print *, "You have exited the GLib main loop, bye, bye..."

  call g_object_unref(app)
end program bsp

Compiliert und gelinkt wird das Ganze so:

gfortran bsp.f90 $(pkg-config --cflags --libs gtk-4-fortran plplot plplot-fortran)

Nach dem Programmstart

./a.out

öffnet sich ein Fenster mit zwei Buttons. Der eine Button schreibt etwas in die Konsole, der andere schließt das Programm.


<< zur Fortran-Startseite
< f03gl BLAS und ATLAS >