Retrieving the filename from a GtkFileChooserDialog

I’m looking to get the filename from a GtkFileChooserDialog and I feel like I’m close but I can’t figure out what I’m missing. I’ve read over:

And that helped, but I’m getting:

ERROR: LoadError: MethodError: no method matching filename(::GtkFileChooserDialogLeaf)
Closest candidates are:
  filename(::GtkFileChooser) at /home/me/.julia/packages/Gtk/aP55V/gen/gbox3:3064
  filename(::GtkFileChooser, ::Any) at /home/me/.julia/packages/Gtk/aP55V/gen/gbox3:3067

When trying to call: Gtk.bytestring(Gtk.GAccessor.filename(widget), true).

I’ve simplified an example down to the following two files, I hope it’s small enough to not annoy people.

First is help.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkFileChooserDialog" id="dialog">
    <property name="can_focus">False</property>
    <property name="type_hint">dialog</property>
    <property name="create_folders">False</property>
    <property name="preview_widget_active">False</property>
    <property name="use_preview_label">False</property>
    <child>
      <placeholder/>
    </child>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="cancel">
                <property name="label">gtk-cancel</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_underline">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="open">
                <property name="label">gtk-open</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_underline">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="main">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="border_width">6</property>
    <property name="title" translatable="yes">Help</property>
    <property name="default_width">800</property>
    <property name="default_height">600</property>
    <property name="has_resize_grip">True</property>
    <signal name="destroy" handler="ui_destroy" swapped="no"/>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_File</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="add">
                        <property name="label">gtk-add</property>
                        <property name="name">menu_new</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="quit">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>

Next the julia file:

#using Pkg
#Pkg.add("Gtk")

using Gtk

function display_ui()::GtkBuilder
    GtkBuilder(filename="help.glade")
end

function run(builder::GtkBuilder)::Channel

    local connectors  = [
        ("quit", "activate", "quit"),
        ("add", "activate", "add"),
        ("dialog", "file-activated", "file"),
        ("dialog", "selection-changed", "file")
    ]

    Channel() do chan
        for (element, event, message) in connectors
            signal_connect(builder[element], event) do widget
                put!(chan, (message, widget))
            end
        end

        local c = Condition()
        signal_connect(builder["main"], :destroy) do widget
            notify(c)
        end
        wait(c)

    end
end

builder = display_ui()
channel = run(builder)

for (msg, widget) in channel
    println("Msg: ", msg)

    if msg == "add"
        showall(builder["dialog"])
    end

    if msg == "file"
        local filename = Gtk.bytestring(Gtk.GAccessor.filename(widget), true)
        println("Filename: ", filename)
    end

end

Figured it out if anyone runs across this later, ended up just needing to cast the widget to the correct type:

    if msg == "file"
        local val = Gtk.GAccessor.filename(Gtk.GtkFileChooser(widget))
        if val != C_NULL
            local filename = Gtk.bytestring(val)
            println("Filename: ", filename)
        end
    end
2 Likes