Skip to content

Commit

Permalink
reduce GObject boilerplate, remove some useless constructor functiona…
Browse files Browse the repository at this point in the history
…lity

How would one use GResource in Julia code?
  • Loading branch information
jwahlstrand committed Jan 3, 2023
1 parent e16632b commit 8da6f6a
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 425 deletions.
4 changes: 1 addition & 3 deletions GI/src/giimport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,7 @@ function gobject_decl(objectinfo)
local kwargs
function $leafname(args...; kwargs...)
w = $oname(args...)
for (kw, val) in kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[$(QuoteNode(oname))] = $leafname
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Gtk4"
uuid = "9db2cae5-386f-4011-9d63-a5602296539b"
version = "0.2.0"
version = "0.3.0"

[deps]
BitFlags = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35"
Expand All @@ -23,7 +23,7 @@ gdk_pixbuf_jll = "da03df04-f53b-5353-a52f-6a8b0620ced0"
hicolor_icon_theme_jll = "059c91fe-1bad-52ad-bddd-f7b78713c282"

[compat]
Glib_jll = "2.68.0"
Glib_jll = "2.74.0"
GTK4_jll = "4.6.0"
Xorg_xkeyboard_config_jll = "2.27.0"
adwaita_icon_theme_jll = "3.33.92"
Expand Down
2 changes: 1 addition & 1 deletion src/GLib/GLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export signal_handler_block, signal_handler_unblock
export add_action, add_stateful_action

export get_gtk_property, set_gtk_property!, gtk_propertynames, bind_property, unbind_property
export bytestring, nothing_to_null
export bytestring, nothing_to_null, setproperties!
export length_zt, err_buf, check_err
export gtkdoc_const_url, gtkdoc_enum_url, gtkdoc_flags_url, gtkdoc_method_url,
gtkdoc_func_url, gtkdoc_struc_url
Expand Down
6 changes: 6 additions & 0 deletions src/GLib/gobject.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function setproperties!(obj::GObject; kwargs...)
for (kw, val) = kwargs
set_gtk_property!(obj, kw, val)
end
end

# converts the string output by `g_value_get_string` to Julia equivalent
function gvalue_string_convert(str)
value = (str == C_NULL ? "nothing" : GLib.bytestring(str))
Expand Down
72 changes: 15 additions & 57 deletions src/GdkPixbufLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,58 +201,21 @@ convert(::Type{P}, a::MatrixStrided) where {P <: Ptr} = convert(P, a.p)
bstride(a::MatrixStrided, i) = (i == 1 ? sizeof(eltype(a)) : (i == 2 ? a.rowstride : 0))
bstride(a, i) = stride(a, i) * sizeof(eltype(a))

# Example constructors:
#GdkPixbuf(filename = "", width = -1, height = -1, preserve_aspect_ratio = true)
#GdkPixbuf(resource_path = "", width = -1, height = -1, preserve_aspect_ratio = true)
#GdkPixbuf(stream = "", width = -1, height = -1, preserve_aspect_ratio = true)
#GdkPixbuf(xpm_data = [...])
#GdkPixbuf(data = [...], has_alpha = true)
#GdkPixbuf(width = 1, height = 1, has_alpha = true)
function GdkPixbuf(; stream = nothing, resource_path = nothing, filename = nothing, xpm_data = nothing, inline_data = nothing, data = nothing,
width = -1, height = -1, preserve_aspect_ratio = true, has_alpha = nothing)
source_count = (stream !== nothing) + (resource_path !== nothing) + (filename !== nothing) +
(xpm_data !== nothing) + (inline_data !== nothing) + (data !== nothing)
@assert(source_count <= 1,
"GdkPixbuf must have at most one stream, resource_path, filename, xpm_data, inline_data, or data argument")
@assert(source_count == 0 || data !== nothing || has_alpha === nothing,
"GdkPixbuf can only set the has-alpha property for new buffers")
function GdkPixbuf(width::Integer, height::Integer, has_alpha = true)
G_.Pixbuf_new(Colorspace_RGB, has_alpha, 8, width, height)
end

function GdkPixbuf(filename::AbstractString, width = -1, height = -1, preserve_aspect_ratio = true)
if width == -1 && height == -1
G_.Pixbuf_new_from_file(filename)
else
G_.Pixbuf_new_from_file_at_scale(filename, width, height, preserve_aspect_ratio)
end
end

function GdkPixbuf(data::AbstractArray, has_alpha = nothing)
local pixbuf::Ptr{GObject}
if stream !== nothing
@assert(false, "not implemented yet")
elseif resource_path !== nothing
GError() do error_check
if width == -1 && height == -1
pixbuf = ccall((:gdk_pixbuf_new_from_resource, libgdkpixbuf), Ptr{GObject}, (Ptr{UInt8}, Ptr{Ptr{GError}}), bytestring(resource_path), error_check)
else
pixbuf = ccall((:gdk_pixbuf_new_from_resource_at_scale, libgdkpixbuf), Ptr{GObject},
(Ptr{UInt8}, Cint, Cint, Cint, Ptr{Ptr{GError}}), bytestring(resource_path), width, height, preserve_aspect_ratio, error_check)
end
return pixbuf !== C_NULL
end
elseif filename !== nothing
GError() do error_check
if width == -1 && height == -1
pixbuf = ccall((:gdk_pixbuf_new_from_file, libgdkpixbuf), Ptr{GObject}, (Ptr{UInt8}, Ptr{Ptr{GError}}), bytestring(filename), error_check)
else
pixbuf = ccall((:gdk_pixbuf_new_from_file_at_scale, libgdkpixbuf), Ptr{GObject},
(Ptr{UInt8}, Cint, Cint, Cint, Ptr{Ptr{GError}}), bytestring(filename), width, height, preserve_aspect_ratio, error_check)
end
return pixbuf !== C_NULL
end
elseif xpm_data !== nothing
@assert(width == -1 && height == -1, "GdkPixbuf cannot set the width/height of a image from xpm_data")
GError() do error_check
pixbuf = ccall((:gdk_pixbuf_new_from_xpm_data, libgdkpixbuf), Ptr{GObject}, (Ptr{Ptr{Nothing}},), xpm_data)
return pixbuf !== C_NULL
end
elseif inline_data !== nothing
@assert(width == -1 && height == -1, "GdkPixbuf cannot set the width/height of a image from inline_data")
GError() do error_check
pixbuf = ccall((:gdk_pixbuf_new_from_inline, libgdkpixbuf), Ptr{GObject}, (Cint, Ptr{Nothing}, Cint, Ptr{Ptr{GError}}), sizeof(inline_data), inline_data, true, error_check)
return pixbuf !== C_NULL
end
elseif data !== nothing # RGB or RGBA array, packed however you wish
@assert(width == -1 && height == -1, "GdkPixbuf cannot set the width/height of a image from data")
if data !== nothing # RGB or RGBA array, packed however you wish
alpha = convert(Bool, has_alpha)
width = size(data, 1) * bstride(data, 1)/(3 + Int(alpha))
height = size(data, 2)
Expand All @@ -261,14 +224,9 @@ function GdkPixbuf(; stream = nothing, resource_path = nothing, filename = nothi
(Ptr{Nothing}, Cint, Cint, Cint, Cint, Cint, Cint, Ptr{Nothing}, Ptr{Nothing}),
data, 0, alpha, 8, width, height, bstride(data, 2),
deref_data, ref_data)
else
@assert(width != -1 && height != -1 && has_alpha !== nothing, "GdkPixbuf requires a width, height, and has_alpha to create an uninitialized pixbuf")
return G_.Pixbuf_new(Colorspace_RGB, has_alpha, 8, width, height)
end
return GdkPixbuf(pixbuf)
return convert(GdkPixbuf, pixbuf, true)
end
#GdkPixbufLoader for new with type/mimetype
#GdkPixbuf(callback, stream, width = -1, height = -1, preserve_aspect_ratio = true)

slice(img::GdkPixbuf, x, y) = G_.new_subpixbuf(img, first(x)-1, first(y)-1, length(x), length(y))
size(a::GdkPixbuf, i::Integer) = (i == 1 ? width(a) : (i == 2 ? height(a) : 1))
Expand Down
12 changes: 5 additions & 7 deletions src/builder.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@

function GtkBuilder(; buffer = nothing, filename = nothing, resource = nothing)
function GtkBuilder(; buffer = nothing, filename = nothing)
builder = G_.Builder_new()
push!(builder, buffer = buffer, filename = filename, resource = resource)
push!(builder, buffer = buffer, filename = filename)
builder
end

function push!(builder::GtkBuilder; buffer = nothing, filename = nothing, resource = nothing)
source_count = (buffer !== nothing) + (filename !== nothing) + (resource !== nothing)
function push!(builder::GtkBuilder; buffer = nothing, filename = nothing)
source_count = (buffer !== nothing) + (filename !== nothing)
@assert(source_count == 1,
"push!(GtkBuilder) must have exactly one buffer, filename, or resource argument")
"push!(GtkBuilder) must have exactly one buffer or filename argument")
if buffer !== nothing
G_.add_from_string(builder, buffer, -1)
elseif filename !== nothing
G_.add_from_file(builder, filename)
elseif resource !== nothing
G_.add_from_resource(builder, resource)
end
return builder
end
Expand Down
24 changes: 8 additions & 16 deletions src/displays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ end
GtkImage(pixbuf::GdkPixbuf) = G_.Image_new_from_pixbuf(pixbuf)
GtkImage(filename::AbstractString) = G_.Image_new_from_file(filename)

function GtkImage(; resource_path = nothing, filename = nothing, icon_name = nothing)
source_count = (resource_path !== nothing) + (filename !== nothing) + (icon_name !== nothing)
function GtkImage(; filename = nothing, icon_name = nothing)
source_count = (filename !== nothing) + (icon_name !== nothing)
@assert(source_count <= 1,
"GdkPixbuf must have at most one resource_path, filename, or icon_name argument")
if resource_path !== nothing
img = G_.Image_new_from_resource(resource_path)
elseif filename !== nothing
"GdkPixbuf must have at most one filename or icon_name argument")
if filename !== nothing
img = G_.Image_new_from_file(filename)
elseif icon_name !== nothing
img = G_.Image_new_from_icon_name(icon_name)
Expand All @@ -49,18 +47,12 @@ GtkPicture(pixbuf::GdkPixbuf) = G_.Picture_new_for_pixbuf(pixbuf)
GtkPicture(p::GdkPaintable) = G_.Picture_new_for_paintable(p)
GtkPicture(gfile::GFile) = G_.Picture_new_for_file(gfile)

function GtkPicture(; resource_path = nothing, filename = nothing)
source_count = (resource_path !== nothing) + (filename !== nothing)
@assert(source_count <= 1,
"GdkPixbuf must have at most one resource_path or filename argument")
if resource_path !== nothing
img = G_.Picture_new_for_resource(resource_path)
elseif filename !== nothing
img = G_.Picture_new_for_filename(filename)
function GtkPicture(filename = nothing)
if filename !== nothing
G_.Picture_new_for_filename(filename)
else
img = G_.Picture_new()
G_.Picture_new()
end
return img
end

## GtkLevelBar
Expand Down
28 changes: 7 additions & 21 deletions src/gen/gdkpixbuf_structs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufLeaf(args...; kwargs...)
w = GdkPixbuf(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbuf] = GdkPixbufLeaf
Expand All @@ -99,9 +97,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufAnimationLeaf(args...; kwargs...)
w = GdkPixbufAnimation(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbufAnimation] = GdkPixbufAnimationLeaf
Expand All @@ -124,9 +120,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufAnimationIterLeaf(args...; kwargs...)
w = GdkPixbufAnimationIter(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbufAnimationIter] = GdkPixbufAnimationIterLeaf
Expand All @@ -149,9 +143,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufLoaderLeaf(args...; kwargs...)
w = GdkPixbufLoader(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbufLoader] = GdkPixbufLoaderLeaf
Expand All @@ -174,9 +166,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufNonAnimLeaf(args...; kwargs...)
w = GdkPixbufNonAnim(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbufNonAnim] = GdkPixbufNonAnimLeaf
Expand All @@ -199,9 +189,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufSimpleAnimLeaf(args...; kwargs...)
w = GdkPixbufSimpleAnim(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbufSimpleAnim] = GdkPixbufSimpleAnimLeaf
Expand All @@ -224,9 +212,7 @@ $(Expr(:toplevel, quote
local kwargs
function GdkPixbufSimpleAnimIterLeaf(args...; kwargs...)
w = GdkPixbufSimpleAnimIter(args...)
for (kw, val) = kwargs
set_gtk_property!(w, kw, val)
end
setproperties!(w; kwargs...)
w
end
gtype_wrapper_cache[:GdkPixbufSimpleAnimIter] = GdkPixbufSimpleAnimIterLeaf
Expand Down
Loading

0 comments on commit 8da6f6a

Please sign in to comment.