gtk3freebasic

FreeBASIC and GTK Glade how work the glade Button?


a few months ago I started programming with FreeBASIC using the GTK libraries so that the applications are operational on both Windows and Linux without the need to rewrite the code, but being a beginner I only managed to create the mask but I don't know how to handle pressing the button.

Could someone tell me what I need to do to handle the button press?

    #INCLUDE ONCE "gtk/gtk.bi" 
    gtk_init(@__FB_ARGC__, @__FB_ARGV__)





DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR _
  mainWindow, Main_CloseButton

XML = gtk_builder_new()

SCOPE
DIM AS GError PTR meld
VAR GUISTR = SADD( _
"<?xml version=""1.0"" encoding=""UTF-8""?>" _
"<!-- Generated with glade 3.38.2 -->" _
"<interface>" _
"<requires lib=""gtk+"" version=""3.24""/>" _
"<object class=""GtkWindow"" id=""mainWindow"">" _
"<property name=""can-focus"">False</property>" _
"<child>" _
"<object class=""GtkLayout"">" _
"<property name=""visible"">True</property>" _
"<property name=""can-focus"">False</property>" _
"<child>" _
"<object class=""GtkButton"" id=""Main_CloseButton"">" _
"<property name=""label"" translatable=""yes"">Close</property>" _
"<property name=""width-request"">80</property>" _
"<property name=""height-request"">30</property>" _
"<property name=""visible"">True</property>" _
"<property name=""can-focus"">True</property>" _
"<property name=""receives-default"">True</property>" _
"</object>" _
"<packing>" _
"<property name=""x"">350</property>" _
"<property name=""y"">200</property>" _
"</packing>" _
"</child>" _
"</object>" _
"</child>" _
"</object>" _
"</interface>" _
!"\0")
IF 0 = gtk_builder_add_from_string(XML, GUISTR, -1, @meld) THEN
  WITH *meld
    ?"Error (GTK-Builder):"
    ?*.message
  END WITH
  g_error_free(meld)
  END 2
END IF
END SCOPE

mainWindow = gtk_builder_get_object(XML, "mainWindow")
Main_CloseButton = gtk_builder_get_object(XML, "Main_CloseButton")





    gtk_builder_connect_signals(XML, 0)
    g_object_unref(XML) 
    gtk_widget_show_all(GTK_WIDGET(mainWindow)) 
    gtk_main()

Solution

  • First of all you need to add a signal in the XML code that refers to pressing the button. Ex.

    "<property name=""receives-default"">True</property>" _
    "<signal name=""clicked"" handler=""on_Main_CloseButton_clicked"" swapped=""no""/>" _
    "</object>" _
    

    Create a new file by calling it (on_Main_CloseButton_clicked.bas) include it before the "gtk_builder_connect_signals(XML, 0)" Sometimes during the compilation if it is included later it can give some errors (perhaps a compiler bug or the code too messy)

    Main_CloseButton = gtk_builder_get_object(XML, "Main_CloseButton")
    
    #INCLUDE "on_Main_CloseButton_clicked.bas"
    
    
        gtk_builder_connect_signals(XML, 0)
        g_object_unref(XML) 
        gtk_widget_show_all(GTK_WIDGET(mainWindow)) 
        gtk_main()
    
    

    And inside the new file write a sub that is activated by a handler

    on_Main_CloseButton_clicked.bas

    SUB on_Main_CloseButton_clicked CDECL ALIAS "on_Main_CloseButton_clicked" ( _
      BYVAL widget AS GtkWidget PTR, _
      BYVAL user_data AS gpointer) EXPORT ' Standard-Parameterliste
    
    'Place your code here+
    end
    
    
    END SUB