Gtk.jl icon indicating copy to clipboard operation
Gtk.jl copied to clipboard

Allow setting current_name/folder in `save_dialog`

Open halleysfifthinc opened this issue 4 years ago • 1 comments

I recently needed to set the current folder and suggest a save name in the save_dialog/open_dialog functions (this is done with the Gtk functions gtk_file_chooser_set_current_folder and gtk_file_chooser_set_current_name), but there is currently no way to do this with save_dialog and open_dialog.

I've just started using Gtk.jl, so I'm not sure what the stylistic conventions for this package are. Keyword arguments would be the most Julian solution, and this is a monkey-patch that works for me:

@eval Gtk function save_dialog(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[];
                     current_folder::Union{AbstractString, Nothing}=nothing, current_name::Union{AbstractString, Nothing}=nothing,
                     kwargs...)
    dlg = GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.SAVE,
                                (("_Cancel", GConstants.GtkResponseType.CANCEL),
                                 ("_Save",   GConstants.GtkResponseType.ACCEPT)); kwargs...)
    dlgp = GtkFileChooser(dlg)
    if !isempty(filters)
        makefilters!(dlgp, filters)
    end
    if !isnothing(current_folder)
        ccall((:gtk_file_chooser_set_current_folder, libgtk), Nothing, (Ptr{GObject}, Ptr{UInt8}), dlgp, current_folder)
    end
    if !isnothing(current_name)
        ccall((:gtk_file_chooser_set_current_name, libgtk), Nothing, (Ptr{GObject}, Ptr{UInt8}), dlgp, current_name)
    end
    ccall((:gtk_file_chooser_set_do_overwrite_confirmation, libgtk), Nothing, (Ptr{GObject}, Cint), dlg, true)
    response = run(dlg)
    if response == GConstants.GtkResponseType.ACCEPT
        selection = bytestring(GAccessor.filename(dlgp))
    else
        selection = ""
    end
    destroy(dlg)
    return selection
end

Is there any interest in enabling this functionality? I'm more than happy to submit a PR if given direction regarding desired style and/or functionality.

halleysfifthinc avatar Apr 14 '20 17:04 halleysfifthinc

Yes this looks good. The save / open dialog is somewhat special in the Gtk.jl context since it is more of a high-level API, while most of the other parts try to keep as close to the C API. If you prepare a pull request, I am happy to review that.

tknopp avatar Apr 14 '20 19:04 tknopp