Skip to content

Allow setting current_name/folder in save_dialog #500

@halleysfifthinc

Description

@halleysfifthinc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions