Gtk.jl - GTKFileChooserButton question

I have created a GtkWindow (with a GtkFixed grid on it) in Glade and place a GTKFileChooserButton on the grid and I give the GTKFileChooserButton object the ID - fileChooser. I saved as fileChooser.glade.

So, in Julia I do the following:

using Gtk

b = GtkBuilder(filename="fileChooser.glade")

win = b["MainWindow"]
fileChooser = b["fileChooser"]

function on_fileChooser_file_set(var)
    println("I'm here.")
end
id = signal_connect(on_fileChooser_file_set, fileChooser, "file-set")

showall(win)

This all works fine. I can click on the “file chooser” button, pick a file, and my callback gets executed and I see “I’m here.” in the terminal.

In the “C” world, if I wanted access to the file and directory, my callback might look like:

void on_fileChooser_file_set(GtkFileChooserButton *f) {
    printf("file name = %s\n", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(f)));
    printf("folder name = %s\n", gtk_file_chooser_get_current_folder(GTK_FILECHOOSER(f)));
}

Can I do this same functionality via the Gtk.jl package using my fileChooser widget I’m accessing via GtkBuilder? If so, how?

What I am doing (for exactly that purpose) is to generate the file chooser within the “clicked” callback of the button. Here is some example code

  signal_connect(btnChooser, "clicked") do widget
    dlg = FileChooserDialog("Select folder", Null(), GtkFileChooserAction.SELECT_FOLDER,
                             "gtk-cancel", GtkResponseType.CANCEL,
                             "gtk-open", GtkResponseType.ACCEPT)
    if ret == GtkResponseType.ACCEPT
      path = Gtk.bytestring(Gtk._.filename(dlg),true)

     # now do something with the path
    end
    destroy(dlg)
  end


end

But, this isn’t using what was designed in Glade with GtkFileChooserButton - is it? This looks like you are using a button connected to the “clicked” signal and you are generating a FileChoose Dialog inside the button callback.

This is not what the Gtk implementation looks like.

{
  GtkWidget *button;

  button = gtk_file_chooser_button_new (_("Select a file"),
                                        GTK_FILE_CHOOSER_ACTION_OPEN);
  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
                                       "/etc");
}

I am trying to be able to use Glade (as is) to design a GUI and use the builtin widget of Glade and the Gtk. Am I looking at this incorrectly?

yes, I also do most of the things in Glade but do some widgets on runtime.

If you want to stick to your code:
In

function on_fileChooser_file_set(var)
    println("I'm here.")
end

the var is certainly a file chooser object. I would try

function on_fileChooser_file_set(var)
   filename = Gtk.bytestring(Gtk._.filename(var),true)
   println("this is the filename: ", filename)
end

I don’t get anything printed in the terminal. Here’s my code:

using Gtk

b = GtkBuilder(filename="fileChooser.glade")

win = b["MainWindow"]
fileChooser = b["fileChooser"]

function on_fileChooser_file_set(var)
	filename = Gtk.bytestring(Gtk._.filename(var),true)
	println("this is the filename: ", filename)
	#println("I'm here.")
end

id = signal_connect(on_fileChooser_file_set, fileChooser, "file-set")

showall(win)

I do see the filename in the button on MainWindow, but it doesn’t print anything to the screen.

If I change my code to:

using Gtk

b = GtkBuilder(filename="fileChooser.glade")

win = b["MainWindow"]
fileChooser = b["fileChooser"]

function on_fileChooser_file_set(var)
	#filename = Gtk.bytestring(Gtk._.filename(var),true)
	#println("this is the filename: ", filename)
	println("I'm here.")
end

id = signal_connect(on_fileChooser_file_set, fileChooser, "file-set")

showall(win)

I do see “I’m here.” each time I click the button and choose another file.

function on_fileChooser_file_set(var)
	filename = Gtk.bytestring(Gtk.GAccessor.filename(var),true)
	println("this is the filename: ", filename)
	#println("I'm here.")
end

I cannot test because I don’t have the glade file. Best thing to test is that you provide a minimal working example (MWE). Instead of providing the glade file, you can also paste the content of the glade file into a string my_ui_string and call GtkBuilder(buffer=my_ui_string).

OK. I really do appreciate the help!

using Gtk

ui = """
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="MainWindow">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Main Window</property>
    <property name="window_position">center</property>
    <property name="default_width">800</property>
    <property name="default_height">600</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkFixed" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkFileChooserButton" id="fileChooser">
            <property name="width_request">270</property>
            <property name="height_request">42</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="title" translatable="yes"/>
            <signal name="file-set" handler="on_fileChooser_file_set" swapped="no"/>
          </object>
          <packing>
            <property name="x">80</property>
            <property name="y">70</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
"""

b = GtkBuilder(buffer=ui)

win = b["MainWindow"]
fileChooser = b["fileChooser"]

function on_fileChooser_file_set(var)
	filename = Gtk.bytestring(Gtk._.filename(var),true)
	println("this is the filename: ", filename)
	#println("I'm here.")
end

id = signal_connect(on_fileChooser_file_set, fileChooser, "file-set")

showall(win)

The issue is that GtkFileChooserButton is not yet implemented in Gtk.jl. One has to add this in few places (e.g. https://github.com/JuliaGraphics/Gtk.jl/blob/master/src/gtktypes.jl).

1 Like

OK. Thanks for tracking that down. So, are all of the ones with @gtktype … safe to use? If so, that’s a nice palette of controls to choose from.

Thanks again for all of your help.

Frank

yes that list is pretty safe to use. I have build larger applications with what is available in Gtk.jl. If you need more, simply extend it.