Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Allow setting current_name/folder in save_dialog #500

Open
halleysfifthinc opened this issue Apr 14, 2020 · 1 comment · May be fixed by #501
Open

Allow setting current_name/folder in save_dialog #500

halleysfifthinc opened this issue Apr 14, 2020 · 1 comment · May be fixed by #501

Comments

@halleysfifthinc
Copy link

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.

@tknopp
Copy link
Collaborator

tknopp commented Apr 14, 2020

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants