Skip to content

Commit

Permalink
Support indexing abstractions for containers (#677)
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 Dec 27, 2022
1 parent 97561bb commit 881fdce
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Gtk"
uuid = "4c0ca9eb-093a-5379-98c5-f87ac0bbbf44"
version = "1.2.3"
version = "1.3.0"

[deps]
Cairo = "159f3aea-2a34-519c-b102-8c37f9878175"
Expand Down
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
11 changes: 11 additions & 0 deletions src/lists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,24 @@ 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{1}}, column::Union{Integer,CartesianIndex{1}}) =
setindex!(store, value, _integer(index), _integer(column))
setindex!(store::GtkListStore, value, index::CartesianIndex{2}) =
setindex!(store, value, Tuple(index)...)

_integer(i::Integer) = i
_integer(i::CartesianIndex{1}) = 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

2 comments on commit 881fdce

@timholy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/74689

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.0 -m "<description of version>" 881fdce8eb5a8bce2f1a846f79ac7142177e5b2a
git push origin v1.3.0

Please sign in to comment.