Skip to content

Allow specifying current folder and suggesting filenames in file dialogs #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/src/manual/filedialogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ if isdir(dir)
end
```

The open and save dialogs (incl. native versions) support the keyword argument `current_folder`
to set the current folder shown when the dialog opens. Both save dialog variants support an additional
keyword argument, `current_name`, to allow suggesting a filename for saving.

Here are some examples:
```julia
open_dialog("Pick a file")
open_dialog("Pick a file"; current_folder=homedir())
open_dialog("Pick some files", select_multiple=true)
open_dialog("Pick a file", Null(), ("*.jl",))
open_dialog("Pick some text files", GtkNullContainer(), ("*.txt, *.csv",), select_multiple=true)
Expand All @@ -44,6 +49,8 @@ open_dialog("Pick an image file", GtkNullContainer(), ("*.png", "*.jpg", GtkFile
open_dialog("Pick an image file", GtkNullContainer(), (GtkFileFilter(name="Supported image formats"),))

save_dialog("Save as...", Null(), (GtkFileFilter("*.png, *.jpg", name="All supported formats"), "*.png", "*.jpg"))
save_dialog("Save as...", Null(), (GtkFileFilter("*.png, *.jpg", name="All supported formats"), "*.png", "*.jpg");
current_name="Profile.jpg")
```


Expand Down
32 changes: 28 additions & 4 deletions src/selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ function makefilters!(dlgp::GtkFileChooser, filters::Union{AbstractVector, Tuple
end
end

function open_dialog(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
function open_dialog(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[];
current_folder::Union{AbstractString, Nothing} = nothing, kwargs...)
dlg = GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.OPEN,
(("_Cancel", GConstants.GtkResponseType.CANCEL),
("_Open", 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
response = run(dlg)
multiple = get_gtk_property(dlg, :select_multiple, Bool)
local selection
Expand All @@ -116,12 +120,16 @@ function open_dialog(title::AbstractString, parent = GtkNullContainer(), filters
return selection
end

function open_dialog_native(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
function open_dialog_native(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[];
current_folder::Union{AbstractString, Nothing} = nothing, kwargs...)
dlg = GtkFileChooserNative(title, parent, GConstants.GtkFileChooserAction.OPEN,"_Open","_Cancel"; 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
response = run(dlg)
multiple = get_gtk_property(dlg, :select_multiple, Bool)
local selection
Expand All @@ -143,14 +151,22 @@ function open_dialog_native(title::AbstractString, parent = GtkNullContainer(),
return selection
end

function save_dialog(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
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
Expand All @@ -162,12 +178,20 @@ function save_dialog(title::AbstractString, parent = GtkNullContainer(), filters
return selection
end

function save_dialog_native(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
function save_dialog_native(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[];
current_folder::Union{AbstractString, Nothing} = nothing,
current_name::Union{AbstractString, Nothing} = nothing, kwargs...)
dlg = GtkFileChooserNative(title, parent, GConstants.GtkFileChooserAction.SAVE,"_Save","_Cancel"; 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
Expand Down