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

add more tests, fix a few issues with GList, GtkGrid, GtkNotebook, GtkOverlay #603

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/GLib/glist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ popfirst!(list::GList) = splice!(list, nth_first(list))
pop!(list::GList) = splice!(list, nth_last(list))
deleteat!(list::GList, i::Integer) = deleteat!(list, nth(list, i))

function splice!(list::GList, item::Ptr)
function splice!(list::GList, item::Ptr)
x = deref(item)
deleteat!(list, item)
x
Expand All @@ -93,8 +93,7 @@ length(list::LList{L}) where {L <: _GList} = Int(ccall((:g_list_length, libglib)
copy(list::GList{L}) where {L <: _GSList} = typeof(list)(ccall((:g_slist_copy, libglib), Ptr{L}, (Ptr{L},), list), false)
copy(list::GList{L}) where {L <: _GList} = typeof(list)(ccall((:g_list_copy, libglib), Ptr{L}, (Ptr{L},), list), false)
check_undefref(p::Ptr) = (p == C_NULL ? error(UndefRefError()) : p)
nth_first(list::LList{L}) where {L <: _GSList} =
check_undefref(ccall((:g_slist_first, libglib), Ptr{L}, (Ptr{L},), list))
nth_first(list::LList{L}) where {L <: _GSList} = unsafe_convert(Ptr{L}, list)
nth_first(list::LList{L}) where {L <: _GList} =
check_undefref(ccall((:g_list_first, libglib), Ptr{L}, (Ptr{L},), list))
nth_last(list::LList{L}) where {L <: _GSList} =
Expand Down Expand Up @@ -154,13 +153,15 @@ function empty!(list::GList{L}) where L <: _GList
return list
end
function append!(l1::GList{L}, l2::GList{L}) where L <: _GSList
(l1.transfer_full & l2.transfer_full) && error("cannot combine two lists with transfer_full = true")
(l1.transfer_full | l2.transfer_full) && error("cannot combine lists with transfer_full = true")
l1.handle = ccall((:g_slist_concat, libglib), Ptr{L}, (Ptr{L}, Ptr{L}), l1, l2)
l2.handle = C_NULL
return l1
end
function append!(l1::GList{L}, l2::GList{L}) where L <: _GList
(l1.transfer_full & l2.transfer_full) && error("cannot combine two lists with transfer_full = true")
(l1.transfer_full | l2.transfer_full) && error("cannot combine lists with transfer_full = true")
l1.handle = ccall((:g_list_concat, libglib), Ptr{L}, (Ptr{L}, Ptr{L}), l1, l2)
l2.handle = C_NULL
return l1
end
function reverse!(list::GList{L}) where L <: _GSList
Expand Down Expand Up @@ -261,14 +262,26 @@ empty!(li::Ptr{_GSList{Ptr{N}}}) where {N <: Number} = g_free(unsafe_load(li).da
empty!(li::Ptr{_GList{Ptr{N}}}) where {N <: Number} = g_free(unsafe_load(li).data)

### Store (byte)strings as pointers
deref_to(::Type{S}, p::Ptr) where {S <: String} = bytestring(convert(Ptr{UInt8}, p))
function deref_to(::Type{S}, p::Ptr) where {S <: String}
p==C_NULL && return ""
bytestring(convert(Ptr{UInt8}, p))
end
function ref_to(::Type{S}, x) where S <: String
s = bytestring(x)
l = sizeof(s)
p = convert(Ptr{UInt8}, g_malloc(l + 1))
unsafe_copyto!(p, convert(Ptr{UInt8}, pointer(s)), l)
unsafe_store!(p, '\0', l + 1)
p = ccall((:g_strdup, libglib), Ptr{UInt8}, (Cstring,), s)
return p
end
empty!(li::Ptr{_GSList{S}}) where {S <: String} = g_free(unsafe_load(li).data)
empty!(li::Ptr{_GList{S}}) where {S <: String} = g_free(unsafe_load(li).data)

function empty!(li::Ptr{_GSList{S}}) where {S <: String}
gl=unsafe_load(li)
g_free(gl.data)
gl = _GSList{S}(C_NULL, gl.next) # set to NULL to prevent Julia from trying to access this while finalizing
unsafe_store!(li, gl)
end

function empty!(li::Ptr{_GList{S}}) where {S <: String}
gl=unsafe_load(li)
g_free(gl.data)
gl = _GList{S}(C_NULL, gl.next, gl.prev) # set to NULL to prevent Julia from trying to access this while finalizing
unsafe_store!(li, gl)
end
7 changes: 4 additions & 3 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function deleteat!(grid::GtkGrid, i::Integer, side::Symbol)
end

function insert!(grid::GtkGrid, sibling, side::Symbol)
ccall((:gtk_grid_insert_next_to, libgtk), Nothing, (Ptr{GObject}, Ptr{GObject}, Cint), grid, sibling, GtkPositionType.(side))
pos=getfield(GtkPositionType,Symbol(uppercase(string(side))))
ccall((:gtk_grid_insert_next_to, libgtk), Nothing, (Ptr{GObject}, Ptr{GObject}, Cint), grid, sibling, pos)
end

if libgtk_version >= v"3.16.0"
Expand Down Expand Up @@ -211,11 +212,11 @@ function splice!(w::GtkNotebook, i::Integer)
end

pagenumber(w::GtkNotebook, child::GtkWidget) =
ccall((:gtk_notebook_page_num, libgtk), Cint, (Ptr{GObject}, Ptr{GObject}), w, child)
ccall((:gtk_notebook_page_num, libgtk), Cint, (Ptr{GObject}, Ptr{GObject}), w, child) + 1

### GtkOverlay
GtkOverlayLeaf() = GtkOverlayLeaf(ccall((:gtk_overlay_new, libgtk), Ptr{GObject}, () ))
GtkOverlayLeaf(w::GtkWidget) = invoke(push!, (GtkContainer,), GtkOverlayLeaf(), w)
GtkOverlayLeaf(w::GtkWidget) = invoke(push!, Tuple{GtkContainer,Any}, GtkOverlayLeaf(), w)
function push!(w::GtkOverlay, x::GtkWidget)
ccall((:gtk_overlay_add_overlay, libgtk), Cint,
(Ptr{GObject}, Ptr{GObject}), w, x)
Expand Down
26 changes: 26 additions & 0 deletions test/glist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ for (i,item) = enumerate(g2)
@test string(i)==item
end

@test popfirst!(g2)==string(1)
@test length(g2)==9
@test pop!(g2)==string(10)
@test length(g2)==8

@test length(g)==10

insert!(g,8,string(25))
@test length(g)==11
@test g[8]==string(25)
Expand All @@ -74,6 +81,11 @@ reverse!(g)
g[2]="test"
@test g[2]=="test"

g3=copy(g)

append!(g2,g3)
@test length(g2)==20

empty!(g)
@test isempty(g)

Expand Down Expand Up @@ -106,6 +118,13 @@ for (i,item) = enumerate(g2)
@test string(i)==item
end

@test popfirst!(g2)==string(1)
@test length(g2)==9
@test pop!(g2)==string(10)
@test length(g2)==8

@test length(g)==10

insert!(g,8,string(25))
@test length(g)==11
@test g[8]==string(25)
Expand All @@ -126,6 +145,11 @@ reverse!(g)
g[2]="test"
@test g[2]=="test"

g3=copy(g)

append!(g2,g3)
@test length(g2)==20

empty!(g)
@test isempty(g)

Expand Down Expand Up @@ -198,4 +222,6 @@ empty!(g)

end

GC.gc()

end
49 changes: 42 additions & 7 deletions test/gui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,26 @@ end
w = Window(
Frame(),
"Frame", 400, 400)
widgets=[f for f in w] # test iteration over GtkBin
@test length(widgets)==1
showall(w)
destroy(w)
end

@testset "Initially Hidden Canvas" begin
nb = Notebook()
@test hasparent(nb)==false
vbox = Gtk.GtkBox(:v)
c = Canvas()
push!(nb, vbox, "A")
push!(nb, c, "B")
insert!(nb, 2, Label("Something in the middle"), "A*")
pushfirst!(nb, Label("Something at the beginning"), "First")
splice!(nb, 3)
w = Window("TestDataViewer",600,600)
@test pagenumber(nb,c)==3
push!(w,nb)
@test parent(nb)==w
showall(w)
destroy(w)
end
Expand Down Expand Up @@ -175,8 +183,21 @@ pw2 = Paned(:v)
push!(w, pw)
push!(pw, Button("one"))
push!(pw, pw2)
push!(pw2,Button("two"))
push!(pw2,Button("three"))
@test pw[2]==pw2
pw2[1]=Button("two")
pw2[2,true,false]=Button("three")
showall(w)
destroy(w)
end

@testset "Layout" begin
w = Window("Layout", 400, 400)
l = Layout(600,600)
push!(w,l)
l[300,300]=Button("Button")
s=size(l)
@test width(l)==600
@test height(l)==600
showall(w)
destroy(w)
end
Expand Down Expand Up @@ -224,8 +245,7 @@ bb = ButtonBox(:h)
w = Window(bb, "ButtonBox")
cancel = Button("Cancel")
ok = Button("OK")
push!(bb, cancel)
push!(bb, ok)
append!(bb, [cancel,ok])

# Expander
delete!(w, bb)
Expand All @@ -238,12 +258,18 @@ end
@testset "Grid" begin
grid = Grid()
w = Window(grid, "Grid", 400, 400)
grid[2,2] = Button("2,2")
b=Button("2,2")
grid[2,2] = b
@test grid[2,2] == b
grid[2,3] = Button("2,3")
grid[1,1] = "grid"
grid[3,1:3] = Button("Tall button")
insert!(grid,1,:top)
insert!(grid,3,:bottom)
insert!(grid,grid[1,2],:right)
libgtk_version >= v"3.10.0" && deleteat!(grid,1,:row)
showall(w)
empty!(grid)
destroy(w)
end

Expand Down Expand Up @@ -289,7 +315,12 @@ end
icon = Matrix{Gtk.RGB}(undef, 40, 20)
fill!(icon, Gtk.RGB(0,0xff,0))
icon[5:end-5, 3:end-3] .= Ref(Gtk.RGB(0,0,0xff))
b = Button(Image(Pixbuf(data=icon, has_alpha=false)))
pb=Pixbuf(data=icon, has_alpha=false)
@test eltype(pb) == Gtk.RGB
@test size(pb) == (40, 20)
@test pb[1,1].g==0xff
pb[10,10]=Gtk.RGB(0,0,0)
b = Button(Image(pb))
w = Window(b, "Icon button", 60, 40)
showall(w)
destroy(w)
Expand Down Expand Up @@ -699,6 +730,8 @@ push!(toolbar,tb1)
pushfirst!(toolbar,tb2)
push!(toolbar,tb3)
push!(toolbar,SeparatorToolItem(), ToggleToolButton("gtk-open"), MenuToolButton("gtk-new"))
@test toolbar[0]==tb2 # FIXME: uses zero based indexing
@test length(toolbar)==6
G_.style(toolbar,GtkToolbarStyle.BOTH)
w = Window(toolbar, "Toolbar")|>showall
destroy(w)
Expand Down Expand Up @@ -773,7 +806,9 @@ destroy(w)
end

@testset "overlay" begin
o = Overlay()
c = Canvas()
o = Overlay(c)
push!(o,Button("Button"))
w = Window(o, "overlay")|>showall
destroy(w)
end
Expand Down