1
votes

My concern is that I created a callback function, which should display a Gtk_Entry when we click on the Gtk_Button but is that when I click on the button nothing happens. I don't understand.

File.ads

Package Test is

    Type T_Test is record
        Conteneur : Gtk_Fixe;
        L_Entree : Gtk_Entry;
    end Record;

    Procedure Le_Callback (Emetteur : access Gtk_Button_Record'Class);

    Package P is new Gtk.Handlers.Callback (Gtk_Button_Record);

    Use P;

end Test;

File.adb

Package body Test is

    Procedure Initialise_Conteneur (Object : T_Test) is
    begin
        Gtk_New (Object.Conteneur);

    end Initialise_Conteneur;

    Procedure Le_Callback (Emetteur : access Gtk_Button_Record'Classs) is
        V : T_Test;

    begin
        Initialise_Conteneur (Object => V);
        Gtk_New (V.L_Entree);
        V.Conteneur.Add (V.L_Entree);

        V.L_Entree.Show;

    end Le_Callback;
end Test;

Main.adb

Procedure Main is
    Win : Gtk_Window;
    Button : Gtk_Button;
    Posix : T_Test;

begin
    Init;
    Initialize (object => Posix);

    1
    Gtk_New (Win);
    Win.Set_Default_Size (600,400);

    Gtk_New (Button,"Bouton");

    Test.P.Connect (Widget => Button,
                    Name => Signal_Clicked,
                    Marsh => P.To_Marshaller (Le_Test'Access),
                    After => true);

    Posix.Conteneur.Add (Button);
    Win.Add (Posix.Conteneur);

    Win.Show_All;
    Main;

end Main;
1
I have an answer written in Vala, as I can't read ada even remotely. Shall I post it? - JCWasmx86

1 Answers

0
votes

Slightly hacked ... to export the Initialize method called in Main.

with Gtk; use Gtk;
with Gtk.Button; use Gtk.Button;
with Gtk.Handlers; use Gtk.Handlers;
with Gtk.Fixed; use Gtk.Fixed;
with Gtk.GEntry; use Gtk.GEntry;


Package Test is

    Type T_Test is record
        Conteneur : Gtk_Fixed;
        L_Entree : Gtk_Entry;
    end Record;
    
    procedure Initialize (Object : out T_Test);

    Procedure Le_Callback (Emetteur : access Gtk_Button_Record'Class);

    Package P is new Gtk.Handlers.Callback (Gtk_Button_Record);

    Use P;

end Test;

And to add logging (goes to the console if you started the program from command line) ... also note the parameter passing mode in the Initialize methods.

With Ada.Text_IO; Use Ada.Text_IO;

Package body Test is

    Procedure Initialise_Conteneur (Object : out T_Test) is
    begin
        Gtk_New (Object.Conteneur);
    end Initialise_Conteneur;
    
    procedure Initialize (Object : out T_Test) renames Initialise_Conteneur;

    Procedure Le_Callback (Emetteur : access Gtk_Button_Record'Class) is
        V : T_Test;

    begin
        put_line("Got here");
        Initialise_Conteneur (Object => V);
        Gtk_New (V.L_Entree);
        V.Conteneur.Add (V.L_Entree);

        V.L_Entree.Show;

    end Le_Callback;
end Test;

And the main program...

with Gtk.Button; use Gtk.Button;
with Gtk.Window; use Gtk.Window;
with Gtk.Main;
with test; use test;


Procedure Main is
    Win : Gtk_Window;
    Button : Gtk_Button;
    Posix : T_Test;

begin
    Gtk.Main.Init;
    Initialize (object => Posix);

    Gtk_New (Win);
    Win.Set_Default_Size (600,400);

    Gtk_New (Button,"Bouton");

    Test.P.Connect (Widget => Button,
                    Name => Signal_Clicked,
                    Marsh => P.To_Marshaller (Le_Callback'Access),
                    After => true);

    Posix.Conteneur.Add (Button);
    Win.Add (Posix.Conteneur);

    Win.Show_All;
    GTK.Main.Main;

end Main;

and my GPR file for it.

with "gtkada";
-- with "gtkada_gl";
project Test is
   for Main use ("main.adb");
   for Source_Dirs use (".");
   for Object_Dir use "obj";
   for Exec_Dir use ".";

   package Compiler is
      for Default_Switches ("Ada") use ("-g", "-O1", "-gnatafo");
   end Compiler;
   package Binder is
      for Default_Switches ("Ada") use ("-E");
   end Binder;
   package Linker is
--      for Default_Switches ("Ada") use ("-lgtkglada");
   end Linker;
end Test;

Now it builds (in future, PLEASE make the example code buildable! would have saved a good chunk of time) and I get to see a button...

Press the button and I can see "Got here" in the console, so we know the handler is connected to the button, and receiving button press messages.

I'll stop here, but (having added something to the button) do you need to resize it before the new entry will show? Or do you need to re-show the entire button? As debugging, why not add the entry first, and hide it when you press the button?