Skip to content

Commit c7d802f

Browse files
committed
update lists.jl
This cleans up the interface for GtkListStore and GtkTreeStore a bit and fixes `selected` and adds `selected_rows`. This summarizes the changes (+ means added, - means dropped) ## store interface push!(listStore::GtkListStore, values::Tuple) unshift!(listStore::GtkListStore, values::Tuple) insert!(listStore::GtkListStoreLeaf, index::Int, values) + insert!(listStore::GtkListStoreLeaf, iter::TRI, values) empty!(listStore::GtkListStore) delete!(listStore::GtkListStore, iter::TRI) + deleteat!(store, iter::TRI) -splice!(listStore::GtkListStoreLeaf, index::Int) + deleteat!(listStore::GtkListStoreLeaf, index::Int) + pop! = deleteat!(store, length(store)) length(listStore::GtkListStore) + size(listStore) = (length(listStore, Gtk.ncolumns(GtkTreeModel(listStore)))) isvalid(listStore::GtkListStore, iter::TRI) ## indexing + getindex(listStore::GtkListStore, iter::TRI, column::Integer)) = getindex(GtkTreeModel(listStore), iter, column) + getindex(listStore::GtkListStore, iter::TRI) = getindex(GtkTreeModel(listStore), iter) + getindex(listStore, row::int, column) = getindex(listStore, iter_from_index(listStore, row), column) + getindex(listStore, row::int) = getindex(listStore, iter_from_index(listStore, row)) ## what of setindex! ## no symmetry here ## get store from a view getproperty(view, :model, Gtk.GtkListStore) ## set store for view, setproperty! fails Gtk.G_.model(view, store) ## Tree store push!(treeStore::GtkTreeStore, values::Tuple, parent=nothing) unshift!(treeStore::GtkTreeStore, values::Tuple, parent=nothing) insert!(treeStore::GtkTreeStoreLeaf, index::Vector{Int}, values; how::Symbol=:parent, where::Symbol=:after) delete!(treeStore::GtkTreeStore, iter::TRI) deleteat!(treeStore::GtkTreeStore, iter::TRI) = delete!(treeStore::GtkTreeStore, iter::TRI)) empty!(treeStore::GtkTreeStore) + deleteat!(treeStore::GtkTreeStoreLeaf, iter::TRI) ## delete at -splice!(treeStore::GtkTreeStoreLeaf, index::Vector{Int}) ## delete at deleteat!(treeStore::GtkTreeStoreLeaf, index::Vector{Int}) isvalid(treeStore::GtkTreeStore, iter::TRI) isancestor(treeStore::GtkTreeStore, iter::TRI, descendant::TRI) depth(treeStore::GtkTreeStore, iter::TRI) getindex(treeModel::GtkTreeModel, iter::TRI, column::Integer) getindex(treeModel::GtkTreeModel, iter::TRI) setindex!(treeModel::GtkTreeModel, value, iter::TRI, column::Integer) setindex!(treeModel::GtkTreeModel, values, iter::TRI) ## Selection - selected(GtkTreeSelection): returned (model, iter) but with error + selected(GtkTreeSelection): return GtkTreeIter or error if no selection select!(GtkTreeSelection, Iter) + selected_rows(GtkTreeSelection) return GtkTreeIter[] or error if no selection selectall!(GtkTreeSelection) unselect!(selection::GtkTreeSelection, iter::TRI unselectall!(GtkTreeSelection)
1 parent 51e0292 commit c7d802f

File tree

1 file changed

+73
-27
lines changed

1 file changed

+73
-27
lines changed

src/lists.jl

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function iter_from_string_index(store, index::String)
106106
if !isvalid(store, iter)
107107
error("invalid index: $index")
108108
end
109-
iter
109+
iter
110110
end
111111

112112
### GtkListStore
@@ -143,6 +143,13 @@ function unshift!(listStore::GtkListStore, values::Tuple)
143143
iter[]
144144
end
145145

146+
## insert before
147+
function Base.insert!(listStore::GtkListStoreLeaf, iter::TRI, values)
148+
newiter = Gtk.mutable(GtkTreeIter)
149+
ccall((:gtk_list_store_insert_before,Gtk.libgtk),Void,(Ptr{GObject},Ptr{GtkTreeIter},Ptr{GtkTreeIter}), listStore, newiter, iter)
150+
list_store_set_values(listStore, newiter, values)
151+
newiter[]
152+
end
146153

147154

148155
function delete!(listStore::GtkListStore, iter::TRI)
@@ -151,28 +158,26 @@ function delete!(listStore::GtkListStore, iter::TRI)
151158
listStore
152159
end
153160

161+
Base.deleteat!(listStore::GtkListStore, iter::TRI) = delete!(listStore, iter)
162+
163+
154164
empty!(listStore::GtkListStore) =
155165
ccall((:gtk_list_store_clear,libgtk), Void, (Ptr{GObject},),listStore)
156166

157167
## by index
158168

159169
## insert into a list store after index
160170
function Base.insert!(listStore::GtkListStoreLeaf, index::Int, values)
161-
index < 1 && return(unshift!(listStore, values))
162-
163-
index = min(index, length(listStore))
164-
iter = Gtk.mutable(GtkTreeIter)
165-
siter = iter_from_index(listStore, index)
166-
ccall((:gtk_list_store_insert_after,Gtk.libgtk),Void,(Ptr{GObject},Ptr{GtkTreeIter},Ptr{GtkTreeIter}), listStore, iter, siter)
167-
list_store_set_values(listStore, iter, values)
168-
iter
169-
end
171+
index > length(listStore) && return(push!(listStore, values))
170172

171-
function Base.splice!(listStore::GtkListStoreLeaf, index::Int)
172173
iter = iter_from_index(listStore, index)
173-
delete!(listStore, iter)
174+
insert!(listStore, iter, values)
174175
end
175176

177+
Base.deleteat!(listStore::GtkListStoreLeaf, index::Int) = delete!(listStore, iter_from_index(listStore, index))
178+
Base.pop!(listStore::GtkListStoreLeaf) = deleteat!(listStore, length(listStore))
179+
Base.shift!(listSTore::GtkListStoreLeaf) = deleteat!(listStore, 1)
180+
176181

177182

178183

@@ -183,6 +188,15 @@ isvalid(listStore::GtkListStore, iter::TRI) =
183188
length(listStore::GtkListStore) =
184189
ccall((:gtk_tree_model_iter_n_children,libgtk), Cint, (Ptr{GObject},Ptr{GtkTreeIter}),listStore, C_NULL)
185190

191+
size(listStore::GtkListStore) = (length(listStore), ncolumns(GtkTreeModel(listStore)))
192+
193+
Base.getindex(listStore::GtkListStore, iter::TRI, column::Integer) = getindex(GtkTreeModel(listStore), iter, column)
194+
Base.getindex(listStore::GtkListStore, iter::TRI) = getindex(GtkTreeModel(listStore), iter)
195+
Base.getindex(listStore::GtkListStore, row::Int, column) = getindex(listStore, iter_from_index(listStore, row), column)
196+
Base.getindex(listStore::GtkListStore, row::Int) = getindex(listStore, iter_from_index(listStore, row))
197+
198+
199+
186200
### GtkTreeStore
187201

188202
function GtkTreeStoreLeaf(types::Type...)
@@ -226,18 +240,10 @@ function unshift!(treeStore::GtkTreeStore, values::Tuple, parent=nothing)
226240
iter[]
227241
end
228242

229-
function delete!(treeStore::GtkTreeStore, iter::TRI)
230-
# not sure what to do with the return value here
231-
deleted = ccall((:gtk_tree_store_remove,libgtk),Cint,(Ptr{GObject},Ptr{GtkTreeIter}), treeStore, mutable(iter))
232-
treeStore
233-
end
234-
235-
236-
## insert by index
237243
## index can be :parent or :sibling
238244
## insertion can be :after or :before
239-
function Base.insert!(treeStore::GtkTreeStoreLeaf, index::Vector{Int}, values; how::Symbol=:parent, where::Symbol=:after)
240-
piter = iter_from_index(treeStore, index)
245+
function Base.insert!(treeStore::GtkTreeStoreLeaf, piter::TRI, values; how::Symbol=:parent, where::Symbol=:after)
246+
241247
iter = Gtk.mutable(GtkTreeIter)
242248
if how == :parent
243249
if where == :after
@@ -257,6 +263,21 @@ function Base.insert!(treeStore::GtkTreeStoreLeaf, index::Vector{Int}, values; h
257263
end
258264

259265

266+
function delete!(treeStore::GtkTreeStore, iter::TRI)
267+
# not sure what to do with the return value here
268+
deleted = ccall((:gtk_tree_store_remove,libgtk),Cint,(Ptr{GObject},Ptr{GtkTreeIter}), treeStore, mutable(iter))
269+
treeStore
270+
end
271+
272+
Base.deleteat!(treeStore::GtkTreeStore, iter::TRI) = delete!(treeStore, iter)
273+
274+
## insert by index
275+
function Base.insert!(treeStore::GtkTreeStoreLeaf, index::Vector{Int}, values; how::Symbol=:parent, where::Symbol=:after)
276+
piter = iter_from_index(treeStore, index)
277+
insert!(treeStore, iter, values; how=how, where=where)
278+
end
279+
280+
260281
function Base.splice!(treeStore::GtkTreeStoreLeaf, index::Vector{Int})
261282
iter = iter_from_index(treeStore, index)
262283
delete!(treeStore, iter)
@@ -409,24 +430,49 @@ add_attribute(treeColumn::GtkTreeViewColumn, renderer::GtkCellRenderer,
409430
(Ptr{GObject},Ptr{GObject},Ptr{Uint8},Cint),treeColumn,renderer,bytestring(attribute),column)
410431

411432
### GtkTreeSelection
412-
413433
function selected(selection::GtkTreeSelection)
434+
hasselection(selection) || error("No selection for GtkTreeSelection")
435+
414436
model = mutable(Ptr{GtkTreeModel})
415437
iter = mutable(GtkTreeIter)
438+
416439
ret = bool(ccall((:gtk_tree_selection_get_selected,libgtk),Cint,
417440
(Ptr{GObject},Ptr{Ptr{GtkTreeModel}},Ptr{GtkTreeIter}),selection,model,iter))
418-
if !ret
419-
error("No selection of GtkTreeSelection")
441+
442+
!ret && error("No selection of GtkTreeSelection")
443+
444+
iter[]
445+
end
446+
447+
function selected_rows(selection::GtkTreeSelection)
448+
hasselection(selection) || return GtkTreeIter[]
449+
450+
model = mutable(Ptr{GtkTreeModel})
451+
452+
paths = Gtk.GLib.GList(ccall((:gtk_tree_selection_get_selected_rows, Gtk.libgtk),
453+
Ptr{Gtk._GSList{Gtk.GtkTreePath}},
454+
(Ptr{GObject}, Ptr{GtkTreeModel}),
455+
selection, model))
456+
457+
iters = GtkTreeIter[]
458+
for path in paths
459+
it = mutable(GtkTreeIter)
460+
ret = bool( ccall((:gtk_tree_model_get_iter,libgtk), Cint, (Ptr{GObject},Ptr{GtkTreeIter},Ptr{GtkTreePath}),
461+
model,it,path))
462+
ret && push!(iters, it[])
420463
end
421-
convert(GtkTreeModel, model[]), iter[]
464+
465+
iters
466+
422467
end
423468

469+
424470
length(selection::GtkTreeSelection) =
425471
ccall((:gtk_tree_selection_count_selected_rows,libgtk), Cint, (Ptr{GObject},),selection)
426472

427473
hasselection(selection::GtkTreeSelection) = length(selection) > 0
428474

429-
select!(selection::GtkTreeSelection, iter::TRI) =
475+
Base.select!(selection::GtkTreeSelection, iter::TRI) =
430476
ccall((:gtk_tree_selection_select_iter,libgtk), Void,
431477
(Ptr{GObject},Ptr{GtkTreeIter}),selection, mutable(iter))
432478

0 commit comments

Comments
 (0)