Skip to content

Commit

Permalink
Support indexing abstractions for containers
Browse files Browse the repository at this point in the history
For objects that support `obj[i]`, also support `eachindex`
and (in some cases) `axes` as mechanisms to query
the valid indexes.
  • Loading branch information
timholy committed Nov 30, 2022
1 parent 73421a8 commit 33e1797
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Base.:|>(parent::GtkContainer, child::Union{GObject, AbstractString}) = push!(pa
start_(w::GtkContainer) = glist_iter(ccall((:gtk_container_get_children, libgtk), Ptr{_GList{GObject}}, (Ptr{GObject},), w))
iterate(w::GtkContainer, list=start_(w)) = iterate(list[1], list)
length(w::GtkContainer) = length(start_(w)[1])
Base.keys(w::GtkContainer) = Base.OneTo(length(w))
getindex(w::GtkContainer, i::Integer) = convert(GtkWidget, start_(w)[1][i])::GtkWidget

function start_(w::GtkBin)
Expand Down
2 changes: 2 additions & 0 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ function setindex!(pane::GtkPaned, child, i::Integer, resize::Bool, shrink::Bool
end
end

Base.keys(::GtkPaned) = Base.OneTo(2)

### GtkLayout
function GtkLayoutLeaf(width::Real, height::Real)
layout = ccall((:gtk_layout_new, libgtk), Ptr{GObject},
Expand Down
9 changes: 9 additions & 0 deletions src/lists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,22 @@ function length(listStore::GtkListStore)
end

size(listStore::GtkListStore) = (length(listStore), ncolumns(GtkTreeModel(listStore)))
Base.axes(listStore::GtkListStore) = Base.OneTo.(size(listStore))
Base.axes(listStore::GtkListStore, d::Integer) = axes(listStore)[d]

Base.keys(listStore::GtkListStore) = CartesianIndices(size(listStore))

getindex(store::GtkListStore, row::Int, column) = getindex(store, iter_from_index(store, row), column)
getindex(store::GtkListStore, row::Int) = getindex(store, iter_from_index(store, row))

function setindex!(store::GtkListStore, value, index::Int, column::Integer)
setindex!(store, value, Gtk.iter_from_index(store, index), column)
end
setindex!(store::GtkListStore, value, index::Union{Int,CartesianIndex}, column::Union{Integer,CartesianIndex}) =
setindex!(store, value, _integer(index), _integer(column))

_integer(i::Integer) = i
_integer(i::CartesianIndex) = convert(Int, i)

### GtkTreeStore

Expand Down
1 change: 1 addition & 0 deletions src/toolbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ end

length(toolbar::GtkToolbar) =
ccall((:gtk_toolbar_get_n_items, libgtk), Cint, (Ptr{GObject},), toolbar)
Base.keys(toolbar::GtkToolbar) = 0:length(toolbar)-1 # FIXME zero-based indexing

### GtkToolButton
GtkToolButtonLeaf(stock_id::AbstractString) = GtkToolButtonLeaf(
Expand Down
1 change: 1 addition & 0 deletions test/glist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ end
@test length(g2)==8

@test length(g)==10
@test eachindex(g) == 1:10

insert!(g,8,string(25))
@test length(g)==11
Expand Down
12 changes: 12 additions & 0 deletions test/gui.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Tests
using Test

using Gtk.ShortNames, Gtk.GConstants, Gtk.Graphics
import Gtk.deleteat!, Gtk.libgtk_version, Gtk.GtkToolbarStyle, Gtk.GtkFileChooserAction, Gtk.GtkResponseType
Expand Down Expand Up @@ -195,6 +196,8 @@ push!(w, pw)
push!(pw, Button("one"))
push!(pw, pw2)
@test pw[2]==pw2
@test length(pw) == 2
@test eachindex(pw) == 1:2
pw2[1]=Button("two")
pw2[2,true,false]=Button("three")
showall(w)
Expand All @@ -207,6 +210,7 @@ l = Layout(600,600)
push!(w,l)
l[300,300]=Button("Button")
s=size(l)
@test s == (600, 600)
@test width(l)==600
@test height(l)==600
showall(w)
Expand All @@ -224,6 +228,8 @@ g2 = Gtk.GtkBox(:h)
push!(f,g1)
push!(f,g2)
@test f[1]==g1
@test length(f) == 2
@test eachindex(f) == 1:2

b11 = Button("first")
push!(g1, b11)
Expand Down Expand Up @@ -281,6 +287,7 @@ end
grid[2,3] = Button("2,3")
grid[1,1] = "grid"
grid[3,1:3] = Button("Tall button")
@test_broken eachindex(grid) == CartesianIndices(size(grid))
insert!(grid,1,:top)
insert!(grid,3,:bottom)
insert!(grid,grid[1,2],:right)
Expand Down Expand Up @@ -499,11 +506,13 @@ for c in choices
push!(combo, c)
end
c = cells(CellLayout(combo))
@test eachindex(c) == 1:1
set_gtk_property!(c[1],"max_width_chars", 5)

w = Window(combo, "ComboGtkBoxText")|>showall
lsl = ListStoreLeaf(combo)
@test length(lsl) == 3
@test eachindex(lsl) == CartesianIndices(size(lsl))
empty!(combo)
@test length(lsl) == 0

Expand Down Expand Up @@ -718,6 +727,8 @@ push!(ls,(33,true))
pushfirst!(ls,(22,false))
popfirst!(ls)
@test size(ls)==(2,2)
@test eachindex(ls) == CartesianIndices(size(ls))
@test axes(ls, 1) == axes(ls, 2) == 1:2
insert!(ls, 2, (35, false))
tv=TreeView(TreeModel(ls))
r1=CellRendererText()
Expand Down Expand Up @@ -796,6 +807,7 @@ 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
@test eachindex(toolbar) == 0:5 # FIXME zero-based indexing
G_.style(toolbar,GtkToolbarStyle.BOTH)
w = Window(toolbar, "Toolbar")|>showall
destroy(w)
Expand Down
1 change: 1 addition & 0 deletions test/list.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Gtk
using Test

@testset "list" begin

Expand Down
1 change: 1 addition & 0 deletions test/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ place_cursor(b, ite)

# GtkTextRange
range=its:ite
@test_broken eachindex(range) == 1:5
@test range[1] == 'l'
@test range[5] == '2'
@test_throws BoundsError range[10]
Expand Down

0 comments on commit 33e1797

Please sign in to comment.