Skip to content

Commit

Permalink
get multiple file selection working in open_dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed May 28, 2023
1 parent cb993ba commit 5cbe708
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
8 changes: 8 additions & 0 deletions docs/src/manual/dialogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ open_dialog("Pick a file to open", parent) do filename
# call a function here to do something with the file
end
```
Multiple files can be opened by setting the `multiple` keyword argument:
```julia
open_dialog("Pick files to open", parent; multiple = true) do filenames
# call a function here to do something with files
end
```
In this case `filenames` is a list of paths.

The dialog can be preset to a particular directory using the optional argument `start_folder`:
```julia
open_dialog(f, "Pick a file to open", parent; start_folder = "/data")
Expand Down
13 changes: 13 additions & 0 deletions examples/dialogs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ end

signal_connect(open_file_open_dialog,file_open_dialog_button,"clicked")

## File open dialog (multiple)

files_open_dialog_button = GtkButton("Multiple file open dialog")
push!(box,files_open_dialog_button)

function open_files_open_dialog(b)
open_dialog("Select file(s) to open",main_window; multiple = true) do filenames
@async println("selection was ", filenames)
end
end

signal_connect(open_files_open_dialog,files_open_dialog_button,"clicked")

## File open dialog (with a *.png filter)

png_open_dialog_button = GtkButton("PNG open dialog")
Expand Down
28 changes: 12 additions & 16 deletions src/windows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,27 +342,27 @@ function makefilters!(dlgp::GtkFileChooser, filters::Union{AbstractVector, Tuple
end
end

function file_chooser_get_selection(dlg::Union{GtkFileChooserDialog,GtkFileChooserNative}, response_id)

function selection(dlg::Union{GtkFileChooserDialog,GtkFileChooserNative}, response_id)
dlgp = GtkFileChooser(dlg)
multiple = get_gtk_property(dlg, :select_multiple, Bool)
local selection
if response_id == ResponseType_ACCEPT
multiple = G_.get_select_multiple(dlgp)
local sel
if ResponseType(unsafe_trunc(UInt16, response_id)) == ResponseType_ACCEPT
if multiple
filename_list = G_.get_files(dlgp)
selection = String[GLib.G_.get_path(GFile(f)) for f in GListModel(filename_list)]
sel = String[GLib.G_.get_path(GFile(f)) for f in GListModel(filename_list)]
else
gfile = G_.get_file(dlgp)
selection = GLib.G_.get_path(GFile(gfile))
sel = GLib.G_.get_path(GFile(gfile))
end
else
if multiple
selection = String[]
sel = String[]
else
selection = ""
sel = ""
end
end
destroy(dlg)
return selection
return sel
end

## Native file dialogs
Expand Down Expand Up @@ -398,6 +398,7 @@ function open_dialog(callback::Function, title::AbstractString, parent = nothing
action = select_folder ? FileChooserAction_SELECT_FOLDER : FileChooserAction_OPEN
dlg = GtkFileChooserNative(title, parent, action, "Open", "Cancel")
dlgp = GtkFileChooser(dlg)
multiple && G_.set_select_multiple(dlgp, true)
if !isempty(filters)
makefilters!(dlgp, filters)
end
Expand All @@ -407,12 +408,7 @@ function open_dialog(callback::Function, title::AbstractString, parent = nothing
end

function on_response(dlg, response_id)
if ResponseType(unsafe_trunc(UInt16, response_id)) == ResponseType_ACCEPT
file = G_.get_file(dlgp)
sel = GLib.G_.get_path(GFile(file))
else
sel = ""
end
sel = selection(dlg, response_id)
callback(sel)
destroy(dlg)
end
Expand Down

0 comments on commit 5cbe708

Please sign in to comment.