1

The attached code compile & runs.

-I should want to retrieve & manipulate the file name of a GtkAda widget. I do see it's possible when we manage directly the object (How to create a file chooser with GtkAda?). But i'm unable to do the same thing, meaning to get the file name of GtkFileChooserButton from inside the xml file.

-Do you can help & explain how you did because even using GnatStudio with its methods i don't see how to do, please.

Thank you Mark

-- window_callbacks.ads

with Gtkada.Builder; use Gtkada.Builder;

package Window_Callbacks is
  
  procedure On_File1_File_Set (Builder : access Gtkada_Builder_Record'Class);

end Window_Callbacks;



-- window_callbacks.adb

-- units from GtkAda
with Gtk.Main;

-- units from GtkAda
with Gtk.File_Chooser;           use Gtk.File_Chooser;
with Gtk.File_Chooser_Button;    use Gtk.File_Chooser_Button;

with Gtkada.File_Selection;      use Gtkada.File_Selection;


-- units from Glib
with Glib;              use Glib;
with Glib.Object;       use Glib.Object;

-- Ada predefined units
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Exceptions;

package body Window_Callbacks  is

  ---------------------------------
  -- On_File1_File_Set  --
  ---------------------------------

  procedure On_File1_File_Set      (Builder : access Gtkada_Builder_Record'Class) is
                                                      
    Button : access Gtk_File_Chooser_Button_Record;

    begin
      null;
    --Button := Gtk.File_Button.Get_File_Name ( XXX, "file1"); --something like this..

     --Prints the file_name of the selection box.
    --Ada.Text_IO.Put_Line ("File selected " & Gtk.File_Chooser_Button.Get_File__Name (Button));

    end On_File1_File_Set;

end Window_Callbacks;



-- glade_8.adb

-- units from Gtk
with Gtk.Main;
with Glib.Error;     use Glib.Error;
with Gtk.Widget;     use Gtk.Widget;
with Gtk.Builder;    use Gtk.Builder;
with Gtkada.Builder; use Gtkada.Builder;

-- Ada predefined units
with Ada.Text_IO;       use Ada.Text_IO;

-- Application specific units
with Window_Callbacks; use Window_Callbacks;

procedure Glade_8 is

  Mon_Interface   : Constant String :=
    "<?xml version=""1.0"" encoding=""UTF-8""?>"
  & "<!-- Generated with glade 3.40.0 -->"
  & "<interface>"
  & "  <requires lib=""gtk+"" version=""3.20""/>"
  & "  <object class=""GtkAdjustment"" id=""adjustment1"">"
  & "    <property name=""upper"">100</property>"
  & "    <property name=""step-increment"">1</property>"
  & "    <property name=""page-increment"">10</property>"
  & "  </object>"
  & "  <object class=""GtkListStore"" id=""liststore1"">"
  & "    <columns>"
  & "      <!-- column-name gchararray1 -->"
  & "      <column type=""gchararray""/>"
  & "    </columns>"
  & "    <data>"
  & "      <row>"
  & "        <col id=""0"" translatable=""yes"">test1</col>"
  & "      </row>"
  & "      <row>"
  & "        <col id=""0"" translatable=""yes"">test2</col>"
  & "      </row>"
  & "      <row>"
  & "        <col id=""0"" translatable=""yes"">test3</col>"
  & "      </row>"
  & "    </data>"
  & "  </object>"
  & "  <object class=""GtkWindow"" id=""window"">"
  & "    <property name=""can-focus"">False</property>"
  & "    <child>"
  & "      <object class=""GtkFixed"" id=""fixed1"">"
  & "        <property name=""visible"">True</property>"
  & "        <property name=""can-focus"">False</property>"
  & "        <child>"
  & "          <object class=""GtkFileChooserButton"" id=""file1"">"
  & "            <property name=""width-request"">196</property>"
  & "            <property name=""visible"">True</property>"
  & "            <property name=""can-focus"">False</property>"
  & "            <property name=""title"" translatable=""yes""/>"
  & "            <signal name=""file-set"" handler=""on_file1_file_set"" swapped=""no""/>"
  & "          </object>"
  & "          <packing>"
  & "            <property name=""x"">9</property>"
  & "            <property name=""y"">234</property>"
  & "          </packing>"
  & "        </child>"
  & "      </object>"
  & "    </child>"
  & "  </object>"
  & "</interface>";
  
  Builder       : Gtkada_Builder;
  Error         : aliased Glib.Error.GError;
  use type Glib.Guint;
     
  begin
    Gtk.Main.Init;

    -- Etape 1 : créer un Builder
    Gtk_New (Builder);
    
    if Add_From_String (Gtk_Builder(Builder), Mon_Interface, Error'Access) = 0 then
      Put_Line ("Error : " & Get_Message (Error));
      Error_Free (Error);
      return;
    end if;
   
    -- Etape 2 : créer les handlers des events
    Register_Handler (Builder, "on_file1_file_set", On_File1_File_Set'Access);

    -- Etape 3 : Do_Connect connecte tous les handlers enregistrés en une fois.
    Do_Connect (Builder);

    -- Etape 4 : Afficher la fenetre avec ses dépendances
    Show_All (Gtk_Widget (Get_Object (GTK_Builder (Builder), "window")));

    -- Etape 5 : Lancer la boucle infinie.
    Gtk.Main.Main;

    -- Etape 6 : Unref pour libérer la memoire associée au Builder.
    Unref (Builder);

  end Glade_8;
 

1 Answer 1

1

You need to get the Gtk_File_Chooser_Button, then get the filename from the button. In window_callback.adb

 procedure On_File1_File_Set (Builder : access Gtkada_Builder_Record'Class) is
                                                      
    Button : access Gtk_File_Chooser_Button_Record;

 begin
    -- Get the file chooser button
    Button := Gtk_File_Chooser_Button(Get_Object(Builder, "file1"));

    -- Get the filename
    declare
       filename: string := Get_FileName(Button);
    begin
       Ada.Text_IO.Put_Line ("File selected " & filename);
    end;

 end On_File1_File_Set;

MISSING A callback handler is also needed for the X button when you close the dialog otherwise it never closes and remains in the background.

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for the answer. I was halted with this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.