Skip to content

Commit 881fdce

Browse files
authored
Support indexing abstractions for containers (#677)
For objects that support `obj[i]`, also support `eachindex` and (in some cases) `axes` as mechanisms to query the valid indexes.
1 parent 97561bb commit 881fdce

File tree

9 files changed

+31
-1
lines changed

9 files changed

+31
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Gtk"
22
uuid = "4c0ca9eb-093a-5379-98c5-f87ac0bbbf44"
3-
version = "1.2.3"
3+
version = "1.3.0"
44

55
[deps]
66
Cairo = "159f3aea-2a34-519c-b102-8c37f9878175"

src/container.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Base.:|>(parent::GtkContainer, child::Union{GObject, AbstractString}) = push!(pa
4141
start_(w::GtkContainer) = glist_iter(ccall((:gtk_container_get_children, libgtk), Ptr{_GList{GObject}}, (Ptr{GObject},), w))
4242
iterate(w::GtkContainer, list=start_(w)) = iterate(list[1], list)
4343
length(w::GtkContainer) = length(start_(w)[1])
44+
Base.keys(w::GtkContainer) = Base.OneTo(length(w))
4445
getindex(w::GtkContainer, i::Integer) = convert(GtkWidget, start_(w)[1][i])::GtkWidget
4546

4647
function start_(w::GtkBin)

src/layout.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ function setindex!(pane::GtkPaned, child, i::Integer, resize::Bool, shrink::Bool
162162
end
163163
end
164164

165+
Base.keys(::GtkPaned) = Base.OneTo(2)
166+
165167
### GtkLayout
166168
function GtkLayoutLeaf(width::Real, height::Real)
167169
layout = ccall((:gtk_layout_new, libgtk), Ptr{GObject},

src/lists.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,24 @@ function length(listStore::GtkListStore)
194194
end
195195

196196
size(listStore::GtkListStore) = (length(listStore), ncolumns(GtkTreeModel(listStore)))
197+
Base.axes(listStore::GtkListStore) = Base.OneTo.(size(listStore))
198+
Base.axes(listStore::GtkListStore, d::Integer) = axes(listStore)[d]
199+
200+
Base.keys(listStore::GtkListStore) = CartesianIndices(size(listStore))
197201

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

201205
function setindex!(store::GtkListStore, value, index::Int, column::Integer)
202206
setindex!(store, value, Gtk.iter_from_index(store, index), column)
203207
end
208+
setindex!(store::GtkListStore, value, index::Union{Int,CartesianIndex{1}}, column::Union{Integer,CartesianIndex{1}}) =
209+
setindex!(store, value, _integer(index), _integer(column))
210+
setindex!(store::GtkListStore, value, index::CartesianIndex{2}) =
211+
setindex!(store, value, Tuple(index)...)
212+
213+
_integer(i::Integer) = i
214+
_integer(i::CartesianIndex{1}) = convert(Int, i)
204215

205216
### GtkTreeStore
206217

src/toolbar.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ end
3131

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

3536
### GtkToolButton
3637
GtkToolButtonLeaf(stock_id::AbstractString) = GtkToolButtonLeaf(

test/glist.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ end
6060
@test length(g2)==8
6161

6262
@test length(g)==10
63+
@test eachindex(g) == 1:10
6364

6465
insert!(g,8,string(25))
6566
@test length(g)==11

test/gui.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Tests
2+
using Test
23

34
using Gtk.ShortNames, Gtk.GConstants, Gtk.Graphics
45
import Gtk.deleteat!, Gtk.libgtk_version, Gtk.GtkToolbarStyle, Gtk.GtkFileChooserAction, Gtk.GtkResponseType
@@ -195,6 +196,8 @@ push!(w, pw)
195196
push!(pw, Button("one"))
196197
push!(pw, pw2)
197198
@test pw[2]==pw2
199+
@test length(pw) == 2
200+
@test eachindex(pw) == 1:2
198201
pw2[1]=Button("two")
199202
pw2[2,true,false]=Button("three")
200203
showall(w)
@@ -207,6 +210,7 @@ l = Layout(600,600)
207210
push!(w,l)
208211
l[300,300]=Button("Button")
209212
s=size(l)
213+
@test s == (600, 600)
210214
@test width(l)==600
211215
@test height(l)==600
212216
showall(w)
@@ -224,6 +228,8 @@ g2 = Gtk.GtkBox(:h)
224228
push!(f,g1)
225229
push!(f,g2)
226230
@test f[1]==g1
231+
@test length(f) == 2
232+
@test eachindex(f) == 1:2
227233

228234
b11 = Button("first")
229235
push!(g1, b11)
@@ -281,6 +287,7 @@ end
281287
grid[2,3] = Button("2,3")
282288
grid[1,1] = "grid"
283289
grid[3,1:3] = Button("Tall button")
290+
@test_broken eachindex(grid) == CartesianIndices(size(grid))
284291
insert!(grid,1,:top)
285292
insert!(grid,3,:bottom)
286293
insert!(grid,grid[1,2],:right)
@@ -499,11 +506,13 @@ for c in choices
499506
push!(combo, c)
500507
end
501508
c = cells(CellLayout(combo))
509+
@test eachindex(c) == 1:1
502510
set_gtk_property!(c[1],"max_width_chars", 5)
503511

504512
w = Window(combo, "ComboGtkBoxText")|>showall
505513
lsl = ListStoreLeaf(combo)
506514
@test length(lsl) == 3
515+
@test eachindex(lsl) == CartesianIndices(size(lsl))
507516
empty!(combo)
508517
@test length(lsl) == 0
509518

@@ -718,6 +727,8 @@ push!(ls,(33,true))
718727
pushfirst!(ls,(22,false))
719728
popfirst!(ls)
720729
@test size(ls)==(2,2)
730+
@test eachindex(ls) == CartesianIndices(size(ls))
731+
@test axes(ls, 1) == axes(ls, 2) == 1:2
721732
insert!(ls, 2, (35, false))
722733
tv=TreeView(TreeModel(ls))
723734
r1=CellRendererText()
@@ -796,6 +807,7 @@ push!(toolbar,tb3)
796807
push!(toolbar,SeparatorToolItem(), ToggleToolButton("gtk-open"), MenuToolButton("gtk-new"))
797808
@test toolbar[0]==tb2 # FIXME: uses zero based indexing
798809
@test length(toolbar)==6
810+
@test eachindex(toolbar) == 0:5 # FIXME zero-based indexing
799811
G_.style(toolbar,GtkToolbarStyle.BOTH)
800812
w = Window(toolbar, "Toolbar")|>showall
801813
destroy(w)

test/list.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Gtk
2+
using Test
23

34
@testset "list" begin
45

test/text.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ place_cursor(b, ite)
107107

108108
# GtkTextRange
109109
range=its:ite
110+
@test_broken eachindex(range) == 1:5
110111
@test range[1] == 'l'
111112
@test range[5] == '2'
112113
@test_throws BoundsError range[10]

0 commit comments

Comments
 (0)