This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
base.jl
137 lines (118 loc) · 5.81 KB
/
base.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
unsafe_convert(::Type{Ptr{GObject}}, w::AbstractStringLike) = unsafe_convert(Ptr{GObject}, GtkLabelLeaf(w))
destroy(w::GtkWidget) = @sigatom ccall((:gtk_widget_destroy, libgtk), Nothing, (Ptr{GObject},), w)
parent(w::GtkWidget) = convert(GtkWidget, ccall((:gtk_widget_get_parent, libgtk), Ptr{GObject}, (Ptr{GObject},), w))
hasparent(w::GtkWidget) = ccall((:gtk_widget_get_parent, libgtk), Ptr{Nothing}, (Ptr{GObject},), w) != C_NULL
toplevel(w::GtkWidget) = convert(GtkWidget, ccall((:gtk_widget_get_toplevel, libgtk), Ptr{GObject}, (Ptr{GObject},), w))
function allocation(widget::Gtk.GtkWidget)
allocation_ = Ref{GdkRectangle}()
ccall((:gtk_widget_get_allocation, libgtk), Nothing, (Ptr{GObject}, Ptr{GdkRectangle}), widget, allocation_)
return allocation_[]
end
width(w::GtkWidget) = ccall((:gtk_widget_get_allocated_width, libgtk), Cint, (Ptr{GObject},), w)
height(w::GtkWidget) = ccall((:gtk_widget_get_allocated_height, libgtk), Cint, (Ptr{GObject},), w)
size(w::GtkWidget) = (width(w), height(w))
gdk_window(w::GtkWidget) = ccall((:gtk_widget_get_window, libgtk), Ptr{Nothing}, (Ptr{GObject},), w)
screen_size(w::GtkWindowLeaf) = screen_size(Gtk.GAccessor.screen(w))
### Functions and methods common to all GtkWidget objects
visible(w::GtkWidget) = Bool(ccall((:gtk_widget_get_visible, libgtk), Cint, (Ptr{GObject},), w))
visible(w::GtkWidget, state::Bool) = @sigatom ccall((:gtk_widget_set_visible, libgtk), Nothing, (Ptr{GObject}, Cint), w, state)
const shown_widgets = WeakKeyDict()
function handle_auto_idle(w::GtkWidget)
if auto_idle[]
signal_connect(w, :realize) do w
enable_eventloop(true)
shown_widgets[w] = nothing
signal_connect(w, :destroy, #= after =# true) do w
delete!(shown_widgets, w)
isempty(shown_widgets) && enable_eventloop(false)
end
end
@static Sys.iswindows() && yield() # issue #610
end
end
function show(w::GtkWidget)
handle_auto_idle(w)
@sigatom ccall((:gtk_widget_show, libgtk), Nothing, (Ptr{GObject},), w)
w
end
"""
showall(w::Gtk.GtkWidget)
Recursively show a widget, and any child widgets (if the widget is a container).
This function overrides the `visible` property in all widgets in the hierarchy. Widgets can be
protected from `showall` by setting the `no_show_all` property on the object to `true`.
"""
function showall(w::GtkWidget)
handle_auto_idle(w)
@sigatom ccall((:gtk_widget_show_all, libgtk), Nothing, (Ptr{GObject},), w)
w
end
hide(w::GtkWidget) = (@sigatom ccall((:gtk_widget_hide , libgtk),Cvoid,(Ptr{GObject},),w); w)
grab_focus(w::GtkWidget) = (@sigatom ccall((:gtk_widget_grab_focus , libgtk), Cvoid, (Ptr{GObject},), w); w)
# TODO Use Pango type PangoFontDescription once it is wrapped
modifyfont(w::GtkWidget, font_desc::Ptr{Nothing}) =
ccall((:gtk_widget_modify_font, libgtk), Nothing, (Ptr{GObject}, Ptr{Nothing}), w, font_desc)
function get_gtk_property(w::GtkContainer, name::AbstractStringLike, child::GtkWidget, ::Type{T}) where T
v = gvalue(T)
ccall((:gtk_container_child_get_property, libgtk), Nothing,
(Ptr{GObject}, Ptr{GObject}, Ptr{UInt8}, Ptr{GValue}), w, child, bytestring(name), v)
val = v[T]
ccall((:g_value_unset, libgobject), Nothing, (Ptr{GValue},), v)
return val
end
#property(w::GtkContainer, value, child::GtkWidget, ::Type{T}) = error("missing Gtk property-name to set")
set_gtk_property!(w::GtkContainer, name::AbstractStringLike, child::GtkWidget, ::Type{T}, value) where {T} = set_gtk_property!(w, name, child, convert(T, value))
function set_gtk_property!(w::GtkContainer, name::AbstractStringLike, child::GtkWidget, value)
ccall((:gtk_container_child_set_property, libgtk), Nothing,
(Ptr{GObject}, Ptr{GObject}, Ptr{UInt8}, Ptr{GValue}), w, child, bytestring(name), gvalue(value))
w
end
# Shortcut for creating callbacks that don't corrupt Gtk state if
# there's an error
macro guarded(ex...)
retval = nothing
if length(ex) == 2
retval = ex[1]
ex = ex[2]
else
length(ex) == 1 || error("@guarded requires 1 or 2 arguments")
ex = ex[1]
end
# do-block syntax
if ex.head == :do && length(ex.args) >= 2 && ex.args[2].head == :->
newbody = _guarded(ex.args[2], retval)
ret = deepcopy(ex)
ret.args[2] = Expr(ret.args[2].head, ret.args[2].args[1], newbody)
return esc(ret)
end
newbody = _guarded(ex, retval)
esc(Expr(ex.head, ex.args[1], newbody))
end
function _guarded(ex, retval)
isa(ex, Expr) && (
ex.head == :-> ||
(ex.head == :(=) && isa(ex.args[1], Expr) && ex.args[1].head == :call) ||
ex.head == :function
) || error("@guarded requires an expression defining a function")
quote
begin
try
$(ex.args[2])
catch err
@warn("Error in @guarded callback", exception=(err, catch_backtrace()))
$retval
end
end
end
end
@deprecate getindex(w::GtkContainer, child::GtkWidget, name::AbstractStringLike, T::Type) get_gtk_property(w, name, child, T)
@deprecate setindex!(w::GtkContainer, value, child::GtkWidget, name::AbstractStringLike, T::Type) set_gtk_property!(w, name, child, T, value)
@deprecate setindex!(w::GtkContainer, value, child::GtkWidget, name::AbstractStringLike) set_gtk_property!(w, name, child, value)
GtkAccelGroupLeaf() = GtkAccelGroupLeaf(
ccall((:gtk_accel_group_new, libgtk), Ptr{GObject}, ()))
function push!(w::GtkWidget, accel_signal::AbstractStringLike, accel_group::GtkAccelGroup,
accel_key::Integer, accel_mods::Integer, accel_flags::Integer)
ccall((:gtk_widget_add_accelerator, libgtk), Nothing,
(Ptr{GObject}, Ptr{UInt8}, Ptr{GObject}, Cuint, Cint, Cint),
w, bytestring(accel_signal), accel_group, accel_key, accel_mods, accel_flags)
w
end