Skip to content

Commit

Permalink
explore copying texture from clipboard and support GdkTextureDownloader
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed May 26, 2024
1 parent 250f392 commit ac17def
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 169 deletions.
28 changes: 15 additions & 13 deletions GI/src/giexport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ function struct_exprs!(exprs,exports,ns,structs=nothing;print_summary=true,exclu
fields=get_fields(ssi)
if only_opaque && length(fields)>0 && !in(name,import_as_opaque)
imported-=1
push!(struct_skiplist,name)
continue
end
if occursin("Private",String(name))
imported-=1
push!(struct_skiplist,name)
continue
end
if is_gtype_struct(ssi) # these are "class structures" and according to the documentation we probably don't need them in bindings
Expand Down Expand Up @@ -120,19 +122,24 @@ function struct_exprs!(exprs,exports,ns,structs=nothing;print_summary=true,exclu
length(fields)>0 && push!(exports.args,get_struct_name(ssi,false))
end

for ss in structs
if print_summary
printstyled("Generated ",imported," structs out of ",length(structs),"\n";color=:green)
end

struct_skiplist
end

function struct_constructor_exprs!(exprs,ns;constructor_skiplist=[], struct_skiplist=[], exclude_deprecated=true,first_list=[])
s=get_non_skipped(ns,GIStructInfo,struct_skiplist,exclude_deprecated)
structs = get_name.(s)
for ss in vcat(first_list, structs)
println("struct constructor: ",ss)
ssi=gi_find_by_name(ns,ss)
constructors = get_constructors(ssi;skiplist=constructor_skiplist, struct_skiplist=struct_skiplist, exclude_deprecated=exclude_deprecated)
if !isempty(constructors)
append!(exprs,constructors)
end
end

if print_summary
printstyled("Generated ",imported," structs out of ",length(structs),"\n";color=:green)
end

struct_skiplist
end

function all_struct_exprs!(exprs,exports,ns;print_summary=true,excludelist=[],constructor_skiplist=[],import_as_opaque=Symbol[],output_cache_init=true,only_opaque=false,exclude_deprecated=true)
Expand Down Expand Up @@ -171,13 +178,7 @@ function all_struct_exprs!(exprs,exports,ns;print_summary=true,excludelist=[],co
end
push!(exprs,unblock(gboxed_types_init))
end


for ssi in ss
constructors = get_constructors(ssi;skiplist=constructor_skiplist, struct_skiplist=struct_skiplist, exclude_deprecated=exclude_deprecated)
isempty(constructors) || append!(exprs,constructors)
end

if print_summary
printstyled("Generated ",imported," structs out of ",length(ss),"\n";color=:green)
end
Expand Down Expand Up @@ -219,6 +220,7 @@ function export_struct_exprs!(ns,path,prefix, struct_skiplist, import_as_opaque;
end
all_interfaces!(exprs,exports,ns; skiplist = interface_skiplist, exclude_deprecated = exclude_deprecated)
c = all_objects!(exprs,exports,ns;handled=[:Object],skiplist=object_skiplist,output_cache_init = output_object_cache_init, output_cache_define = output_object_cache_define, constructor_skiplist = object_constructor_skiplist,exclude_deprecated=exclude_deprecated)
struct_constructor_exprs!(exprs,ns;constructor_skiplist=constructor_skiplist,exclude_deprecated=exclude_deprecated,struct_skiplist=struct_skiplist,first_list=first_list)
if doc_xml !== nothing
append_object_docs!(exprs, doc_prefix, doc_xml, c, ns; skiplist = doc_skiplist)
end
Expand Down
32 changes: 28 additions & 4 deletions examples/show_image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,42 @@ win=GtkWindow("Image")
box = GtkBox(:v)

img = testimage("mandrill")
t = GdkMemoryTexture(img)
p = GdkPaintable(t)
t = Ref(GdkMemoryTexture(img))
pic = GtkPicture(GdkPaintable(t[]))

button = GtkButton("Copy image to clipboard")
signal_connect(button, "clicked") do _
b = Gtk4.G_.save_to_png_bytes(t)
b = Gtk4.G_.save_to_png_bytes(t[])
cp = GdkContentProvider("image/png", b)
c = Gtk4.clipboard(GdkDisplay())
Gtk4.content(c, cp)
end

push!(box, GtkPicture(p))
button2 = GtkButton("Copy image from clipboard")

function cb(clipboard, resobj)
try
t[] = Gtk4.G_.read_texture_finish(clipboard, Gtk4.GLib.GAsyncResult(resobj))
Gtk4.paintable(pic, GdkPaintable(t[]))
catch e
if e isa Gtk4.GLib.GErrorException
info_dialog(e.message, win) do
end
else
println(e)
end
end
nothing
end

signal_connect(button2, "clicked") do _
c = Gtk4.clipboard(GdkDisplay())
cf = Gtk4.formats(c)
Gtk4.G_.read_texture_async(c, nothing, cb)
end

push!(box, pic)
push!(box, button)
push!(box, button2)

win[] = box
6 changes: 2 additions & 4 deletions gen/gen_gdk4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ toplevel, exprs, exports = GI.output_exprs()

# These are marked as "disguised" and what this means is not documented AFAICT.
disguised = Symbol[]
struct_skiplist=vcat(disguised, [:ToplevelSize,:TextureDownloader])
struct_skiplist=vcat(disguised, [:ToplevelSize])

object_skiplist=Symbol[]

GI.export_struct_exprs!(ns,path, "gdk4", struct_skiplist, [:TimeCoord]; object_constructor_skiplist=[:new_from_resource],doc_xml = d)

push!(struct_skiplist,:ContentFormats)

object_method_skiplist=[:begin,:put_event]
object_method_skiplist=[:begin,:put_event,:download]
interface_method_skiplist=[:inhibit_system_shortcuts,:show_window_menu]

GI.export_methods!(ns,path,"gdk4"; interface_method_skiplist = interface_method_skiplist, object_method_skiplist = object_method_skiplist, object_skiplist = object_skiplist, struct_skiplist = struct_skiplist)
Expand Down
2 changes: 1 addition & 1 deletion gen/gen_gsk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GI.export_consts!(ns, path, "gsk4"; doc_xml = d, export_constants = false)

# These are marked as "disguised" and what this means is not documentated AFAICT.
disguised = Symbol[]
struct_skiplist=vcat(disguised, Symbol[:ShaderArgsBuilder])
struct_skiplist=disguised

GI.export_struct_exprs!(ns,path, "gsk4", struct_skiplist, [:RoundedRect]; doc_xml = d, output_boxed_cache_init = false, output_boxed_types_def = false, output_object_cache_define = false, output_object_cache_init = false, output_callbacks = false)
GI.export_methods!(ns,path,"gsk4"; struct_skiplist = struct_skiplist)
Expand Down
34 changes: 33 additions & 1 deletion src/Gdk4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ const color_formats = Dict(ColorTypes.RGB{N0f8}=>Gtk4.MemoryFormat_R8G8B8,
ColorTypes.Gray{N0f16}=>Gtk4.MemoryFormat_G16,
ColorTypes.GrayA{N0f8}=>Gtk4.MemoryFormat_G8A8,
ColorTypes.GrayA{N0f16}=>Gtk4.MemoryFormat_G16A16,
)
)

const color_formats_premultiplied = Dict(ColorTypes.RGBA{N0f8}=>Gtk4.MemoryFormat_R8G8B8A8_PREMULTIPLIED,
ColorTypes.ARGB{N0f8}=>Gtk4.MemoryFormat_A8R8G8B8_PREMULTIPLIED,
ColorTypes.BGRA{N0f8}=>Gtk4.MemoryFormat_B8G8R8A8_PREMULTIPLIED,
ColorTypes.RGBA{N0f16}=>Gtk4.MemoryFormat_R16G16B16A16_PREMULTIPLIED,
# Available since GTK 4.12
ColorTypes.GrayA{N0f8}=>Gtk4.MemoryFormat_G8A8_PREMULTIPLIED,
ColorTypes.GrayA{N0f16}=>Gtk4.MemoryFormat_G16A16_PREMULTIPLIED,
)

imgformatsupported(img) = eltype(img) in keys(color_formats)

Expand All @@ -100,6 +109,29 @@ function GdkMemoryTexture(img::AbstractArray, tp = true)
GdkMemoryTexture(size(img)[1], size(img)[2], f, b, sizeof(eltype(img))*size(img)[1])
end

function toarray(::Type{T}, t::GdkTexture, td::GdkTextureDownloader) where T
ua = Vector{UInt8}(undef,sizeof(T)*size(t)[1]*size(t)[2])
G_.download_into(td,ua,sizeof(T)*size(t)[1])
arr = collect(reshape(reinterpret(T, ua),reverse(size(t))))
arr'
end

function toarray(t::GdkTexture)
td = GdkTextureDownloader(t)
f = G_.get_format(td)
for (k,v) in color_formats
if v == f
return toarray(k,t,td)
end
end
for (k,v) in color_formats_premultiplied
if v == f
return toarray(k,t,td)
end
end
error("no suitable format found")
end

function glib_ref(x::Ptr{GdkEvent})
ccall((:gdk_event_ref, libgtk4), Nothing, (Ptr{GdkEvent},), x)
end
Expand Down
142 changes: 137 additions & 5 deletions src/gen/gdk4_methods
Original file line number Diff line number Diff line change
@@ -1,5 +1,98 @@
quote
$(Expr(:toplevel, quote
function ContentFormats_new(_mime_types)
_mime_types_maybe = nothing_to_null(_mime_types)
_n_mime_types = length(_mime_types)
ret = ccall(("gdk_content_formats_new", libgtk4), Ptr{GdkContentFormats}, (Ptr{Cstring}, UInt32), _mime_types_maybe, _n_mime_types)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function ContentFormats_new_for_gtype(_type::Integer)
ret = ccall(("gdk_content_formats_new_for_gtype", libgtk4), Ptr{GdkContentFormats}, (UInt64,), _type)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function contain_gtype(instance::GdkContentFormats, _type::Integer)
ret = ccall(("gdk_content_formats_contain_gtype", libgtk4), Cint, (Ptr{GdkContentFormats}, UInt64), instance, _type)
ret2 = convert(Bool, ret)
ret2
end
function contain_mime_type(instance::GdkContentFormats, _mime_type::Union{AbstractString, Symbol})
ret = ccall(("gdk_content_formats_contain_mime_type", libgtk4), Cint, (Ptr{GdkContentFormats}, Cstring), instance, _mime_type)
ret2 = convert(Bool, ret)
ret2
end
function get_gtypes(instance::GdkContentFormats)
m_n_gtypes = Ref{UInt64}()
ret = ccall(("gdk_content_formats_get_gtypes", libgtk4), Ptr{UInt64}, (Ptr{GdkContentFormats}, Ptr{UInt64}), instance, m_n_gtypes)
ret2 = collect(unsafe_wrap(Vector{UInt64}, ret, m_n_gtypes[]))
_n_gtypes = m_n_gtypes[]
ret2
end
function get_mime_types(instance::GdkContentFormats)
m_n_mime_types = Ref{UInt64}()
ret = ccall(("gdk_content_formats_get_mime_types", libgtk4), Ptr{Cstring}, (Ptr{GdkContentFormats}, Ptr{UInt64}), instance, m_n_mime_types)
ret2 = bytestring.(unsafe_wrap(Vector{Cstring}, ret, m_n_mime_types[]))
_n_mime_types = m_n_mime_types[]
ret2
end
function match(instance::GdkContentFormats, _second::GdkContentFormats)
ret = ccall(("gdk_content_formats_match", libgtk4), Cint, (Ptr{GdkContentFormats}, Ptr{GdkContentFormats}), instance, _second)
ret2 = convert(Bool, ret)
ret2
end
function match_gtype(instance::GdkContentFormats, _second::GdkContentFormats)
ret = ccall(("gdk_content_formats_match_gtype", libgtk4), UInt64, (Ptr{GdkContentFormats}, Ptr{GdkContentFormats}), instance, _second)
ret
end
function match_mime_type(instance::GdkContentFormats, _second::GdkContentFormats)
ret = ccall(("gdk_content_formats_match_mime_type", libgtk4), Cstring, (Ptr{GdkContentFormats}, Ptr{GdkContentFormats}), instance, _second)
ret2 = string_or_nothing(ret, false)
ret2
end
function print(instance::GdkContentFormats, _string::Union{GString, Ref{_GString}})
ret = ccall(("gdk_content_formats_print", libgtk4), Nothing, (Ptr{GdkContentFormats}, Ptr{_GString}), instance, _string)
nothing
end
function ref(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_ref", libgtk4), Ptr{GdkContentFormats}, (Ptr{GdkContentFormats},), instance)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function to_string(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_to_string", libgtk4), Cstring, (Ptr{GdkContentFormats},), instance)
ret2 = string_or_nothing(ret, true)
ret2
end
function union(instance::GdkContentFormats, _second::GdkContentFormats)
ret = ccall(("gdk_content_formats_union", libgtk4), Ptr{GdkContentFormats}, (Ptr{GdkContentFormats}, Ptr{GdkContentFormats}), instance, _second)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function union_deserialize_gtypes(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_union_deserialize_gtypes", libgtk4), Ptr{GdkContentFormats}, (Ptr{GdkContentFormats},), instance)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function union_deserialize_mime_types(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_union_deserialize_mime_types", libgtk4), Ptr{GdkContentFormats}, (Ptr{GdkContentFormats},), instance)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function union_serialize_gtypes(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_union_serialize_gtypes", libgtk4), Ptr{GdkContentFormats}, (Ptr{GdkContentFormats},), instance)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function union_serialize_mime_types(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_union_serialize_mime_types", libgtk4), Ptr{GdkContentFormats}, (Ptr{GdkContentFormats},), instance)
ret2 = convert(GdkContentFormats, ret, true)
ret2
end
function unref(instance::GdkContentFormats)
ret = ccall(("gdk_content_formats_unref", libgtk4), Nothing, (Ptr{GdkContentFormats},), instance)
nothing
end
function ContentFormatsBuilder_new()
ret = ccall(("gdk_content_formats_builder_new", libgtk4), Ptr{GdkContentFormatsBuilder}, ())
ret2 = convert(GdkContentFormatsBuilder, ret, true)
Expand Down Expand Up @@ -235,6 +328,50 @@ $(Expr(:toplevel, quote
_dest = m_dest[]
_dest
end
function TextureDownloader_new(_texture::GdkTexture)
ret = ccall(("gdk_texture_downloader_new", libgtk4), Ptr{GdkTextureDownloader}, (Ptr{GObject},), _texture)
ret2 = convert(GdkTextureDownloader, ret, true)
ret2
end
function copy(instance::GdkTextureDownloader)
ret = ccall(("gdk_texture_downloader_copy", libgtk4), Ptr{GdkTextureDownloader}, (Ptr{GdkTextureDownloader},), instance)
ret2 = convert(GdkTextureDownloader, ret, true)
ret2
end
function download_bytes(instance::GdkTextureDownloader)
m_out_stride = Ref{UInt64}()
ret = ccall(("gdk_texture_downloader_download_bytes", libgtk4), Ptr{GBytes}, (Ptr{GdkTextureDownloader}, Ptr{UInt64}), instance, m_out_stride)
ret2 = convert(GBytes, ret, true)
_out_stride = m_out_stride[]
(ret2, _out_stride)
end
function download_into(instance::GdkTextureDownloader, _data, _stride::Integer)
_data_arr = convert(Vector{UInt8}, _data)
ret = ccall(("gdk_texture_downloader_download_into", libgtk4), Nothing, (Ptr{GdkTextureDownloader}, Ptr{UInt8}, UInt64), instance, _data_arr, _stride)
nothing
end
function free(instance::GdkTextureDownloader)
ret = ccall(("gdk_texture_downloader_free", libgtk4), Nothing, (Ptr{GdkTextureDownloader},), instance)
nothing
end
function get_format(instance::GdkTextureDownloader)
ret = ccall(("gdk_texture_downloader_get_format", libgtk4), UInt32, (Ptr{GdkTextureDownloader},), instance)
ret2 = MemoryFormat(ret)
ret2
end
function get_texture(instance::GdkTextureDownloader)
ret = ccall(("gdk_texture_downloader_get_texture", libgtk4), Ptr{GObject}, (Ptr{GdkTextureDownloader},), instance)
ret2 = convert(GdkTexture, ret, false)
ret2
end
function set_format(instance::GdkTextureDownloader, _format)
ret = ccall(("gdk_texture_downloader_set_format", libgtk4), Nothing, (Ptr{GdkTextureDownloader}, UInt32), instance, _format)
nothing
end
function set_texture(instance::GdkTextureDownloader, _texture::GdkTexture)
ret = ccall(("gdk_texture_downloader_set_texture", libgtk4), Nothing, (Ptr{GdkTextureDownloader}, Ptr{GObject}), instance, _texture)
nothing
end
function ToplevelLayout_new()
ret = ccall(("gdk_toplevel_layout_new", libgtk4), Ptr{GdkToplevelLayout}, ())
ret2 = convert(GdkToplevelLayout, ret, true)
Expand Down Expand Up @@ -1947,11 +2084,6 @@ $(Expr(:toplevel, quote
ret2 = convert(GdkTexture, ret, true)
ret2
end
function download(instance::GdkTexture, _data, _stride::Integer)
_data_arr = convert(Vector{UInt8}, _data)
ret = ccall(("gdk_texture_download", libgtk4), Nothing, (Ptr{GObject}, Ptr{UInt8}, UInt64), instance, _data_arr, _stride)
nothing
end
function get_format(instance::GdkTexture)
ret = ccall(("gdk_texture_get_format", libgtk4), UInt32, (Ptr{GObject},), instance)
ret2 = MemoryFormat(ret)
Expand Down
Loading

0 comments on commit ac17def

Please sign in to comment.