Skip to content

Commit d050920

Browse files
committed
use regards more consistently in dialogs (breaking), and other minor tweaks
1 parent f34005b commit d050920

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

src/selectors.jl

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414
#GtkFontSelectionDialog — A dialog box for selecting fonts
1515
#GtkInputDialog — Configure devices for the XInput extension
1616

17-
push!(widget::GtkDialog, text::String, response::Integer) =
17+
function push!(widget::GtkDialog, text::String, response::Integer)
1818
ccall((:gtk_dialog_add_button,libgtk), Ptr{GObject},
1919
(Ptr{GObject},Ptr{Uint8},Cint), widget, text, response)
20+
widget
21+
end
2022

21-
function GtkFileChooserDialogLeaf(title::String, parent::GtkContainer, action::Integer, button_text_response...)
22-
n = length(button_text_response)
23-
if !iseven(n)
24-
error("button_text_response must consist of text/response pairs")
25-
end
23+
#if VERSION >= v"0.4-"
24+
#GtkFileChooserDialogLeaf(title::String, parent::GtkContainer, action::Integer, button_text_response::=>...; kwargs...) =
25+
# GtkFileChooserDialogLeaf(title::String, parent, action, button_text_response; kwargs...)
26+
#end
27+
function GtkFileChooserDialogLeaf(title::String, parent::GtkContainer, action::Integer, button_text_response; kwargs...)
2628
w = GtkFileChooserDialogLeaf(ccall((:gtk_file_chooser_dialog_new,libgtk), Ptr{GObject},
2729
(Ptr{Uint8},Ptr{GObject},Cint,Ptr{Void}),
28-
title, parent, action, C_NULL))
29-
for i = 1:2:n
30-
push!(w, button_text_response[i], button_text_response[i+1])
30+
title, parent, action, C_NULL); kwargs...)
31+
for (k,v) in button_text_response
32+
push!(w, k, v)
3133
end
3234
w
3335
end
@@ -36,7 +38,7 @@ run(widget::GtkDialog) = ccall((:gtk_dialog_run,libgtk), Cint, (Ptr{GObject},),
3638

3739
const SingleComma = r"(?<!,),(?!,)"
3840
function GtkFileFilterLeaf(; name::Union(ByteString,Nothing) = nothing, pattern::ByteString = "", mimetype::ByteString = "")
39-
filt = ccall((:gtk_file_filter_new,libgtk), Ptr{GObject}, ())
41+
filt = GtkFileFilterLeaf(ccall((:gtk_file_filter_new,libgtk), Ptr{GObject}, ()))
4042
if !isempty(pattern)
4143
name == nothing && (name = pattern)
4244
for p in split(pattern, SingleComma)
@@ -57,24 +59,24 @@ function GtkFileFilterLeaf(; name::Union(ByteString,Nothing) = nothing, pattern:
5759
end
5860
GtkFileFilterLeaf(pattern::ByteString; name::Union(ByteString,Nothing) = nothing) = GtkFileFilterLeaf(; name=name, pattern=pattern)
5961

60-
GtkFileFilterLeaf(filter::GtkFileFilterLeaf) = filter
62+
GtkFileFilterLeaf(filter::GtkFileFilter) = filter
6163

62-
function makefilters(dlgp::GtkFileChooser, filters::Union(AbstractVector,Tuple))
64+
function makefilters!(dlgp::GtkFileChooser, filters::Union(AbstractVector,Tuple))
6365
for f in filters
6466
ccall((:gtk_file_chooser_add_filter,libgtk), Void, (Ptr{GObject}, Ptr{GObject}), dlgp, @GtkFileFilter(f))
6567
end
6668
end
6769

68-
function open_dialog(title::String; parent = GtkNullContainer(), filters::Union(AbstractVector,Tuple) = ASCIIString[], multiple::Bool = false)
70+
function open_dialog(title::String, parent = GtkNullContainer(), filters::Union(AbstractVector,Tuple) = ASCIIString[]; kwargs...)
6971
dlg = @GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.OPEN,
70-
"_Cancel", GConstants.GtkResponseType.CANCEL,
71-
"_Open", GConstants.GtkResponseType.ACCEPT)
72-
setproperty!(dlg, :select_multiple, multiple)
72+
(("_Cancel", GConstants.GtkResponseType.CANCEL),
73+
("_Open", GConstants.GtkResponseType.ACCEPT)); kwargs...)
7374
dlgp = GtkFileChooser(dlg)
7475
if !isempty(filters)
75-
makefilters(dlgp, filters)
76+
makefilters!(dlgp, filters)
7677
end
7778
response = run(dlg)
79+
multiple = getproperty(dlg, :select_multiple, Bool)
7880
local selection
7981
if response == GConstants.GtkResponseType.ACCEPT
8082
if multiple
@@ -96,13 +98,13 @@ function open_dialog(title::String; parent = GtkNullContainer(), filters::Union(
9698
selection
9799
end
98100

99-
function save_dialog(title::String; parent = GtkNullContainer(), filters::Union(AbstractVector,Tuple) = ASCIIString[])
101+
function save_dialog(title::String, parent = GtkNullContainer(), filters::Union(AbstractVector,Tuple) = ASCIIString[]; kwargs...)
100102
dlg = @GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.SAVE,
101-
"_Cancel", GConstants.GtkResponseType.CANCEL,
102-
"_Save", GConstants.GtkResponseType.ACCEPT)
103+
(("_Cancel", GConstants.GtkResponseType.CANCEL),
104+
("_Save", GConstants.GtkResponseType.ACCEPT)), kwargs...)
103105
dlgp = GtkFileChooser(dlg)
104106
if !isempty(filters)
105-
makefilters(dlgp, filters)
107+
makefilters!(dlgp, filters)
106108
end
107109
ccall((:gtk_file_chooser_set_do_overwrite_confirmation,libgtk), Void, (Ptr{GObject}, Cint), dlg, true)
108110
response = run(dlg)

src/windows.jl

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,58 @@ GtkScrolledWindowLeaf() = GtkScrolledWindowLeaf(
3333
ccall((:gtk_scrolled_window_new,libgtk),Ptr{GObject},(Ptr{GObject},Ptr{GObject}),
3434
C_NULL,C_NULL))
3535

36-
function GtkDialogLeaf(title::StringLike, parent::GtkContainer, flags::Integer, button_text_response...)
37-
n = length(button_text_response)
38-
if !iseven(n)
39-
error("button_text_response must consist of text/response pairs")
40-
end
36+
#if VERSION >= v"0.4-"
37+
#GtkDialogLeaf(title::StringLike, parent::GtkContainer, flags::Integer, buttons::=>...; kwargs...) =
38+
# GtkDialogLeaf(title, parent, flags, buttons; kwargs...)
39+
#end
40+
function GtkDialogLeaf(title::StringLike, parent::GtkContainer, flags::Integer, buttons; kwargs...)
4141
w = GtkDialogLeaf(ccall((:gtk_dialog_new_with_buttons,libgtk), Ptr{GObject},
4242
(Ptr{Uint8},Ptr{GObject},Cint,Ptr{Void}),
43-
title, parent, flags, C_NULL))
44-
for i = 1:2:n
45-
push!(w, button_text_response[i], button_text_response[i+1])
43+
title, parent, flags, C_NULL); kwargs...)
44+
for (k,v) in buttons
45+
push!(w, k, v)
4646
end
4747
w
4848
end
4949

5050
GtkAboutDialogLeaf() = GtkAboutDialogLeaf(
5151
ccall((:gtk_about_dialog_new,libgtk),Ptr{GObject},()))
5252

53-
function GtkMessageDialogLeaf(parent::GtkContainer, flags::Integer, typ::Integer,
54-
message::StringLike, button_text_response...)
55-
n = length(button_text_response)
56-
if !iseven(n)
57-
error("button_text_response must consist of text/response pairs")
58-
end
53+
function GtkMessageDialogLeaf(message::StringLike, buttons, flags::Integer, typ::Integer, parent = GtkNullContainer(); kwargs...)
5954
w = GtkMessageDialogLeaf(ccall((:gtk_message_dialog_new,libgtk), Ptr{GObject},
60-
(Ptr{GObject},Cint,Cint,Cint,Ptr{Uint8}),
61-
parent, flags, typ, 0, bytestring(message) ))
62-
for i = 1:2:n
63-
push!(w, button_text_response[i], button_text_response[i+1])
55+
(Ptr{GObject},Cint,Cint,Cint,Ptr{Uint8}),
56+
parent, flags, typ, GtkButtonsType.NONE, C_NULL); kwargs...)
57+
setproperty!(w, :text, message)
58+
for (k,v) in buttons
59+
push!(w, k, v)
6460
end
6561
w
6662
end
6763

68-
#GtkSeparator — A separator widget
64+
ask_dialog(message::String, parent = GtkNullContainer()) =
65+
ask_dialog(message, "No", "Yes", parent)
66+
67+
function ask_dialog(message::String, no_text, yes_text, parent = GtkNullContainer())
68+
dlg = @GtkMessageDialog(message, ((no_text,0), (yes_text,1)),
69+
GtkDialogFlags.DESTROY_WITH_PARENT, GtkMessageType.QUESTION, parent)
70+
response = run(dlg)
71+
destroy(dlg)
72+
response == 1
73+
end
74+
75+
for (func, flag) in (
76+
(:info_dialog, :(GtkMessageType.INFO)),
77+
(:warn_dialog, :(GtkMessageType.WARNING)),
78+
(:error_dialog, :(GtkMessageType.ERROR)))
79+
@eval function $func(message::String, parent = GtkNullContainer())
80+
w = GtkMessageDialogLeaf(ccall((:gtk_message_dialog_new,libgtk), Ptr{GObject},
81+
(Ptr{GObject},Cint,Cint,Cint,Ptr{Uint8}),
82+
parent, GtkDialogFlags.DESTROY_WITH_PARENT,
83+
$flag, GtkButtonsType.CLOSE, C_NULL))
84+
setproperty!(w, :text, message)
85+
run(w)
86+
destroy(w)
87+
end
88+
end
89+
90+
#TODO: GtkSeparator — A separator widget

test/gui.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ destroy(win)
443443
## Selectors
444444
import Gtk.GtkFileChooserAction, Gtk.GtkResponseType
445445
dlg = @FileChooserDialog("Select file", @Null(), GtkFileChooserAction.OPEN,
446-
"_Cancel", GtkResponseType.CANCEL,
447-
"_Open", GtkResponseType.ACCEPT)
446+
(("_Cancel", GtkResponseType.CANCEL),
447+
("_Open", GtkResponseType.ACCEPT)))
448448
destroy(dlg)
449449

450450
## List view

0 commit comments

Comments
 (0)