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

Commit

Permalink
Merge pull request #351 from mwawra/mw/nativefilechooser
Browse files Browse the repository at this point in the history
Add GtkFileChooserNative
  • Loading branch information
tknopp authored May 25, 2018
2 parents 9f32c9a + 21cd6a7 commit 19c77cd
Show file tree
Hide file tree
Showing 15 changed files with 4,843 additions and 4,327 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,15 @@ We can create a canvas that, when right clicked, reveals a context menu:
#### File dialogs
Gtk.jl supports the `GtkFileChooserDialog`.
It also provides two functions, `open_dialog` and `save_dialog`, making this functionality easier to use.
The syntax of these two functions are as follows:
Gtk.jl supports the `GtkFileChooserDialog` and the `GtkFileChooserNative`.
It also provides four functions, `open_dialog` and `save_dialog` as well as `open_dialog_native` and `save_dialog_native`, making this functionality easier to use.
The syntax of these four functions are as follows:
```jl
open_dialog(title, GtkNullContainer(), ASCIIString[])
save_dialog(title, GtkNullContainer(), ASCIIString[])
open_dialog(title, GtkNullContainer(), String[])
save_dialog(title, GtkNullContainer(), String[])
open_dialog_native(title, GtkNullContainer(), String[])
save_dialog_native(title, GtkNullContainer(), String[])
```
If you are using these functions in the context of a GUI, you should set the parent to be the top-level window.
Expand All @@ -518,7 +520,7 @@ You can specify multiple match types for a single filter by separating the patte
You can alternatively specify MIME types, or if no specification is provided it defaults to types supported by `GdkPixbuf`.
The generic specification of a filter is
```jl
@FileFilter(; name = nothing, pattern = "", mimetype = "")
FileFilter(; name = nothing, pattern = "", mimetype = "")
```
Here are some examples:
Expand All @@ -527,11 +529,11 @@ open_dialog("Pick a file")
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)
open_dialog("Pick a file", Null(), (@FileFilter(mimetype="text/csv"),))
open_dialog("Pick an image file", GtkNullContainer(), ("*.png", "*.jpg", @FileFilter("*.png,*.jpg", name="All supported formats")))
open_dialog("Pick an image file", GtkNullContainer(), (@FileFilter(name="Supported image formats"),))
open_dialog("Pick a file", Null(), (FileFilter(mimetype="text/csv"),))
open_dialog("Pick an image file", GtkNullContainer(), ("*.png", "*.jpg", FileFilter("*.png,*.jpg", name="All supported formats")))
open_dialog("Pick an image file", GtkNullContainer(), (FileFilter(name="Supported image formats"),))

save_dialog("Save as...", Null(), (@FileFilter("*.png,*.jpg", name="All supported formats"), "*.png", "*.jpg"))
save_dialog("Save as...", Null(), (FileFilter("*.png,*.jpg", name="All supported formats"), "*.png", "*.jpg"))
```
#### Message dialogs
Expand Down
2 changes: 2 additions & 0 deletions doc/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ all elements in the enum.
. . . +- GtkSpinner
. . . +- GtkSwitch
. . . +- GtkTextView
. . +- GtkNativeDialog
. . . +- GtkFileChooserNative
. +- GParamSpec
. +- GSList{T}
. +- GValue
Expand Down
8 changes: 5 additions & 3 deletions docs/src/manual/filedialogs.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# File Dialogs

Gtk.jl supports the `GtkFileChooserDialog`.
It also provides two functions, `open_dialog` and `save_dialog`, making this functionality easier to use.
The syntax of these two functions are as follows:
Gtk.jl supports the `GtkFileChooserDialog` and the `GtkFileChooserNative`.
It also provides four functions, `open_dialog` and `save_dialog` as well as `open_dialog_native` and `save_dialog_native`, making this functionality easier to use.
The syntax of these four functions are as follows:

```julia
open_dialog(title, GtkNullContainer(), String[])
save_dialog(title, GtkNullContainer(), String[])
open_dialog_native(title, GtkNullContainer(), String[])
save_dialog_native(title, GtkNullContainer(), String[])
```

If you are using these functions in the context of a GUI, you should set the parent to be the top-level window.
Expand Down
4,465 changes: 2,303 additions & 2,162 deletions gen/gbox3

Large diffs are not rendered by default.

4,575 changes: 2,427 additions & 2,148 deletions gen/gconsts3

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion gen/gtk_auto_gen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ let gtk_version = Gtk.gtk_version
end
end
isfile(header) || error("gtk.h not found, please specify path")
args = ASCIIString[split(readstring(`pkg-config --cflags gtk+-$gtk_version.0`),' ')...,cppargs...]
args = readstring(`pkg-config --cflags gtk+-$gtk_version.0`)
if args[end] == '\n'
args = args[1:end-1]
end
args = ASCIIString[split(args,' ')...,cppargs...]
global gtk_h, gtk_macro_h
cd(JULIA_HOME) do
gtk_h = cindex.parse_header(header, diagnostics=true, args=args, flags=0x41)
Expand Down
2 changes: 1 addition & 1 deletion gen/gtk_consts_gen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function gen_consts(body, gtk_h)
name = cindex.spelling(mdecl)
if ismatch(r"^G\w*[A-Za-z]$", name)
tokens = cindex.tokenize(mdecl)
if length(tokens) == 2 && isa(tokens[2], cindex.Literal)
if length(tokens) == 3 && isa(tokens[2], cindex.Literal)
tok2 = Clang.wrap_c.handle_macro_exprn(tokens, 2)[1]
tok2 = replace(tok2, "\$", "\\\$")
push!(body.args, Expr(:const, Expr(:(=), Symbol(name), parse(tok2))))
Expand Down
2 changes: 2 additions & 0 deletions gen/gtk_get_set_gen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const GtkTypeMap = Set{Symbol}([
:GtkEntryCompletion,
:GtkExpander,
:GtkFileChooserDialog,
:GtkFileChooserNative,
:GtkFontButton,
:GtkFrame,
:GtkIconView,
Expand All @@ -47,6 +48,7 @@ const GtkTypeMap = Set{Symbol}([
:GtkMenuItem,
:GtkMenuToolButton,
:GtkMessageDialog,
:GtkNativeDialog,
:GtkNotebook,
:GtkPaned,
:GtkProgressBar,
Expand Down
2 changes: 1 addition & 1 deletion src/basic_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export new, width, height, #minsize, maxsize
#property, margin, padding, align
#raise, focus, destroy, enabled

export open_dialog, save_dialog
export open_dialog, open_dialog_native, save_dialog, save_dialog_native
export info_dialog, ask_dialog, warn_dialog, error_dialog, input_dialog

# GLib-imported event handling
Expand Down
17 changes: 17 additions & 0 deletions src/gtktypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,20 @@ type GtkGLArea end
:( GtkGLAreaLeaf($(args...)) )
end
end

if libgtk_version >= v"3.20.0"
@gtktype GtkNativeDialog
@gtktype GtkFileChooserNative
else
type GtkNativeDialog end
GtkNativeDialogLeaf(x...) = error("GtkNativeDialog is not available until Gtk3.20")
macro GtkNativeDialog(args...)
:( GtkNativeDialogLeaf($(args...)) )
end

type GtkFileChooserNative end
GtkFileChooserNativeLeaf(x...) = error("GtkFileChooserNative is not available until Gtk3.20")
macro GtkFileChooserNative(args...)
:( GtkFileChooserNativeLeaf($(args...)) )
end
end
2 changes: 2 additions & 0 deletions src/long_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export GObject,
GtkEntryCompletion,
GtkExpander,
GtkFileChooserDialog,
GtkFileChooserNative,
GtkFileFilter,
GtkFontButton,
GtkFrame,
Expand All @@ -40,6 +41,7 @@ export GObject,
GtkMenuItem,
GtkMenuToolButton,
GtkMessageDialog,
GtkNativeDialog,
GtkNotebook,
GtkNullContainer,
GtkOverlay,
Expand Down
3 changes: 3 additions & 0 deletions src/long_leaf_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export
@GtkEntryCompletion,
@GtkExpander,
@GtkFileChooserDialog,
@GtkFileChooserNative,
@GtkFileFilter,
@GtkFontButton,
@GtkFrame,
Expand All @@ -40,6 +41,7 @@ export
@GtkMenuItem,
@GtkMenuToolButton,
@GtkMessageDialog,
@GtkNativeDialog,
@GtkNotebook,
@GtkNullContainer,
@GtkOverlay,
Expand Down Expand Up @@ -103,6 +105,7 @@ export
GtkEntryCompletionLeaf,
GtkExpanderLeaf,
GtkFileChooserDialogLeaf,
GtkFileChooserNativeLeaf,
GtkFileFilterLeaf,
GtkFontButtonLeaf,
GtkFrameLeaf,
Expand Down
58 changes: 57 additions & 1 deletion src/selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if libgtk_version >= v"3" ### should work with v >= 2.4, but there is a bug
#GtkHSV — A 'color wheel' widget
#GtkFileChooser — File chooser interface used by GtkFileChooserWidget and GtkFileChooserDialog
#GtkFileChooserButton — A button to launch a file selection dialog
#GtkFileChooserNative — A native file chooser dialog, suitable for “File/Open” or “File/Save” commands
#GtkFileChooserDialog — A file chooser dialog, suitable for "File/Open" or "File/Save" commands
#GtkFileChooserWidget — File chooser widget that can be embedded in other widgets
#GtkFileFilter — A filter for selecting a file subset
Expand Down Expand Up @@ -36,10 +37,21 @@ if libgtk_version >= v"3" ### should work with v >= 2.4, but there is a bug
return w
end

function GtkFileChooserNativeLeaf(title::AbstractString, parent::GtkContainer, action::Integer, open::AbstractString, cancel::AbstractString; kwargs...)
w = GtkFileChooserNativeLeaf(ccall((:gtk_file_chooser_native_new, libgtk), Ptr{GObject},
(Ptr{UInt8}, Ptr{GObject}, Cint, Ptr{UInt8}, Ptr{UInt8}),
title, parent, action, open, cancel); kwargs...)
return w
end

run(widget::GtkDialog) = GLib.g_sigatom() do
ccall((:gtk_dialog_run, libgtk), Cint, (Ptr{GObject},), widget)
end

run(widget::GtkNativeDialog) = GLib.g_sigatom() do
ccall((:gtk_native_dialog_run, libgtk), Cint, (Ptr{GObject},), widget)
end

const SingleComma = r"(?<!,), (?!,)"
function GtkFileFilterLeaf(; name::Union{AbstractString, Void} = nothing, pattern::AbstractString = "", mimetype::AbstractString = "")
filt = GtkFileFilterLeaf(ccall((:gtk_file_filter_new, libgtk), Ptr{GObject}, ()))
Expand Down Expand Up @@ -100,10 +112,37 @@ if libgtk_version >= v"3" ### should work with v >= 2.4, but there is a bug
return selection
end

function open_dialog_native(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
dlg = GtkFileChooserNative(title, parent, GConstants.GtkFileChooserAction.OPEN,"_Open","_Cancel"; kwargs...)
dlgp = GtkFileChooser(dlg)
if !isempty(filters)
makefilters!(dlgp, filters)
end
response = run(dlg)
multiple = getproperty(dlg, :select_multiple, Bool)
local selection
if response == GConstants.GtkResponseType.ACCEPT
if multiple
filename_list = ccall((:gtk_file_chooser_get_filenames, libgtk), Ptr{_GSList{String}}, (Ptr{GObject},), dlgp)
selection = String[f for f in GList(filename_list, #=transfer-full=#true)]
else
selection = bytestring(GAccessor.filename(dlgp))
end
else
if multiple
selection = String[]
else
selection = GLib.utf8("")
end
end
GLib.gc_unref(dlg) #destroy(dlg)
return selection
end

function save_dialog(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
dlg = GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.SAVE,
(("_Cancel", GConstants.GtkResponseType.CANCEL),
("_Save", GConstants.GtkResponseType.ACCEPT)), kwargs...)
("_Save", GConstants.GtkResponseType.ACCEPT)); kwargs...)
dlgp = GtkFileChooser(dlg)
if !isempty(filters)
makefilters!(dlgp, filters)
Expand All @@ -118,4 +157,21 @@ if libgtk_version >= v"3" ### should work with v >= 2.4, but there is a bug
destroy(dlg)
return selection
end

function save_dialog_native(title::AbstractString, parent = GtkNullContainer(), filters::Union{AbstractVector, Tuple} = String[]; kwargs...)
dlg = GtkFileChooserNative(title, parent, GConstants.GtkFileChooserAction.SAVE,"_Save","_Cancel"; kwargs...)
dlgp = GtkFileChooser(dlg)
if !isempty(filters)
makefilters!(dlgp, filters)
end
ccall((:gtk_file_chooser_set_do_overwrite_confirmation, libgtk), Void, (Ptr{GObject}, Cint), dlg, true)
response = run(dlg)
if response == GConstants.GtkResponseType.ACCEPT
selection = bytestring(GAccessor.filename(dlgp))
else
selection = GLib.utf8("")
end
GLib.gc_unref(dlg) #destroy(dlg)
return selection
end
end
4 changes: 4 additions & 0 deletions src/short_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Entry = GtkEntry
const EntryCompletion = GtkEntryCompletion
const Expander = GtkExpander
const FileChooserDialog = GtkFileChooserDialog
const FileChooserNative = GtkFileChooserNative
const FileFilter = GtkFileFilter
const FontButton = GtkFontButton
const Frame = GtkFrame
Expand All @@ -39,6 +40,7 @@ const MenuBar = GtkMenuBar
const MenuItem = GtkMenuItem
const MenuToolButton = GtkMenuToolButton
const MessageDialog = GtkMessageDialog
const NativeDialog = GtkNativeDialog
const Notebook = GtkNotebook
const Null = GtkNullContainer
const Overlay = GtkOverlay
Expand Down Expand Up @@ -103,6 +105,7 @@ export G_, GObject,
EntryCompletion,
Expander,
FileChooserDialog,
FileChooserNative,
FileFilter,
FontButton,
Frame,
Expand All @@ -116,6 +119,7 @@ export G_, GObject,
MenuItem,
MenuToolButton,
MessageDialog,
NativeDialog,
Notebook,
Null,
Overlay,
Expand Down
2 changes: 2 additions & 0 deletions src/short_leaf_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
@g_type_delegate EntryCompletion = GtkEntryCompletion
@g_type_delegate Expander = GtkExpander
@g_type_delegate FileChooserDialog = GtkFileChooserDialog
@g_type_delegate FileChooserNative = GtkFileChooserNative
@g_type_delegate FileFilter = GtkFileFilter
@g_type_delegate FontButton = GtkFontButton
@g_type_delegate Frame = GtkFrame
Expand Down Expand Up @@ -101,6 +102,7 @@ export @G_,
@EntryCompletion,
@Expander,
@FileChooserDialog,
@FileChooserNative,
@FileFilter,
@FontButton,
@Frame,
Expand Down

0 comments on commit 19c77cd

Please sign in to comment.