Creare una finestra mediante le funzioni del API di GTK+3

Da Gambas-it.org - Wikipedia.

GTK+3 è una libreria per la creazione di interfacce grafiche utente. Una importante funzione di tale libreria è quella della realizzazione di finestre comprensive di oggetti, operando con i quali l'utente può generare degli eventi.

E' innanzitutto necessario avere installata nel sistema e richiamare in Gambas la libreria dinamica condivisa: "libgtk-3.so.0.2404.29 ".


Mostriamo di seguito un semplice esempio, nel quale verrà creata una finestra contenente un Button avente un'immagine. Cliccando su tale tasto, si causerà la chiusura della finestra prima creata e la scrittura di un avviso nella console.

All'interno del codice la funzione gtk_main() pone in attesa l'applicazione, passando il controllo degli eventi ad una specifica funzione. La sollevazione di evento genera un segnale che verrà gestito attraverso la chiamata di un'apposita funzione dell'evento. [Nota 1]

Tale gestore del segnale dell'evento è rappresentato dalla funzione esterna g_signal_connect_data(), nella quale la funzione specificata come terzo argomento è una funzione callback, mentre il quarto argomento, invece, rappresenta gli eventuali dati da passare alla funzione callback.
Il primo degli parametri della funzione di ritorno (callback) rappresenta l'oggetto che scatena l'evento (in questo esempio il Button).

Private cu as Pointer


Library "libgtk-3:0.2404.29"

Private Enum GTK_WINDOW_TOPLEVEL = 0, GTK_WINDOW_POPUP
Private Enum GTK_RELIEF_NORMAL = 0, GTK_RELIEF_NONE = 2

' void gtk_init (int *argc, char ***argv)
' Call this function before using any other GTK+ functions in your GUI applications.
Private Extern gtk_init(argc As Pointer, argv As Pointer)

' GtkWidget * gtk_window_new (GtkWindowType type)
' Creates a new GtkWindow, which is a toplevel window that can contain other widgets.
Private Extern gtk_window_new(type As Integer) As Pointer

' void gtk_widget_set_size_request (GtkWidget *widget, gint width, gint height)
' Sets the minimum size of a widget; that is, the widget’s size request will be at least width by height.
Private Extern gtk_widget_set_size_request(widget As Pointer, width As Integer, height As Integer)

' void gtk_window_set_title (GtkWindow *self, gchar* title)
' Sets the title of the GtkWindow.
Private Extern gtk_window_set_title(self As Pointer, title As String)

' void gtk_container_set_border_width (GtkContainer *container, guint border_width)
' Sets the border width of the container.
Private Extern gtk_container_set_border_width(contain As Pointer, border_width As Integer)

' GtkWidget * gtk_button_new_with_label (const gchar *label)
' Creates a GtkButton widget with a GtkLabel child containing the given text.
Private Extern gtk_button_new_with_label(etichetta As String) As Pointer

' GtkSettings * gtk_settings_get_default (void)
' Gets the GtkSettings object for the default GDK screen, creating it if necessary.
Private Extern gtk_settings_get_default() As Pointer

' g_object_set (gpointer object, const gchar *first_property_name, ...)
' Sets properties on an object.
Private Extern g_object_set(GTKObject As Pointer, first_property_name As String, displayed As Boolean, aliud As Pointer)

' GtkWidget * gtk_image_new (void)
' Creates a new empty GtkImage widget.
Private Extern gtk_image_new() As Pointer

' void gtk_image_set_from_file (GtkImage *image, const gchar *filename)
' Creates a new GtkImage displaying the file filename.
Private Extern gtk_image_set_from_file(GTKImage As Pointer, filename As String)

' void gtk_button_set_relief (GtkButton *button, GtkReliefStyle relief)
' Sets the relief style of the edges of the given GtkButton widget.
Private Extern gtk_button_set_relief(GtkButton As Pointer, relief As Integer)

' void gtk_button_set_image (GtkButton *button, GtkWidget *image)
' Set the image of button to the given widget.
Private Extern gtk_button_set_image(GtkButton As Pointer, GTKImage As Pointer)

' void gtk_container_add (GtkContainer *container, GtkWidget *widget)
' Adds widget to container.
Private Extern gtk_container_add(contain As Pointer, widget As Pointer)

' gulong g_signal_connect_data (gpointer instance, const gchar *detailed_signal, GCallback c_handler, gpointer data, GClosureNotify destroy_data, GConnectFlags connect_flags)
' Connects a GCallback function to a signal for a particular object.
Private Extern g_signal_connect_data(instance As Pointer, detailed_signal As String, c_handler As Pointer, data As Pointer, destroy_data As Pointer, connect_flags As Integer) As Long

' void gtk_widget_show_all (GtkWidget *widget)
' Recursively shows a widget, and any child widgets (if the widget is a container).
Private Extern gtk_widget_show_all(widget As Pointer)

' GdkDisplay * gdk_display_get_default (void)
' Gets the default GdkDisplay.
Private Extern gdk_display_get_default() As Pointer

' GdkCursor * gdk_cursor_new_for_display (GdkDisplay *display, GdkCursorType cursor_type)
' Creates a new cursor from the set of builtin cursors.
Private Extern gdk_cursor_new_for_display(display As Pointer, cursor_type As Integer) As Pointer

' GdkWindow * gtk_widget_get_window (GtkWidget *widget)
' Returns the widget’s window if it is realized.
 Private Extern gtk_widget_get_window(widget As Pointer) As Pointer

' void gdk_window_set_cursor (GdkWindow *window, GdkCursor *cursor)
' Sets the default mouse pointer for a GdkWindow.
Private Extern gdk_window_set_cursor(GdkWindow As Pointer, cursor As Pointer)

' void gtk_main()
' Runs the main loop until gtk_main_quit() is called.
Private Extern gtk_main()

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(object As Pointer)

' void gtk_widget_destroy (GtkWidget *widget)
' Destroys a widget.
Private Extern gtk_widget_destroy(widget As Pointer)

' void gtk_main_quit(void)
' Makes the innermost invocation of the main loop return when it regains control.
Private Extern gtk_main_quit()


Public Sub Main()

 Dim win, button  As Pointer
 Dim default_settings, buttonImage As Pointer
 
 gtk_init(0, 0)
   
' Crea la finestra:
 win = gtk_window_new(GTK_WINDOW_TOPLEVEL)
 If win == 0 Then Error.Raise("Errore !")
   
 gtk_widget_set_size_request(win, 450, 300)
 gtk_window_set_title(win, "Intestazione della finestra")
   
' Imposta la larghezza del bordo della finestra.
' La larghezza del bordo di un contenitore è la distanza esistente per uscire dal contenitore.
' L'ambito dei valori validi è: 0 - 65535 pixel.
 gtk_container_set_border_width(win, 140)
 
 default_settings = gtk_settings_get_default()
 g_object_set(default_settings, "gtk-button-images", 0, Null)
 
 buttonImage = gtk_image_new()
 gtk_image_set_from_file(buttonImage, "/usr/share/icons/gnome/48x48/actions/gtk-delete.png")
 button = gtk_button_new_with_label(Null)
 gtk_button_set_relief(button, GTK_RELIEF_NORMAL)
 gtk_button_set_image(button, buttonImage)

 gtk_container_add(win, button)
   
' Funzione di gestione del segnale dell'evento del "Button":
 g_signal_connect_data(button, "button-release-event", Distrugge_finestra, 0, 0, 0)

' Mostra la finestra:
 gtk_widget_show_all(win)
  
' Imposta il tipo di cursore:
 cu = gdk_cursor_new_for_display(gdk_display_get_default(), 16)
 If cu == 0 Then Error.Raise("Errore !")
  
 gdk_window_set_cursor(gtk_widget_get_window(win), cu)
   
' Resta in attesa di un evento:
 gtk_main()

End

Private Procedure Distrugge_finestra(widget As Pointer, data As Pointer)

 g_object_unref(cu)
 gtk_main_quit()
 gtk_widget_destroy(widget)
 Print "Finestra distrutta !"
 
End


Note

[1] La tipologia degli eventi è indicata in questa pagina web di GNOME.


Riferimenti