N_children cannot be used to get number of toplevel nodes


#1

Hi

I’m trying to get the numbers of rows in my liststore, but it seems I
lack a functionality.

In C, you can call :

gtk_tree_model_iter_n_children(GtkTreeModel *tree_model, GtkTreeIter
*iter)

with a NULL pointer to GtkTreeIter, and get the number of toplevel
nodes.

From the doc :
Returns the number of children that iter has. As a special case, if iter
is NULL, then the number of toplevel nodes is returned.

However, since we can only call n_children from an instanced
Gtk::TreeIter object, I can’t reproduce the C behavior.

Am I missing a method somewhere in the API, or should I do something
ugly like :

module Gtk
class ListStore
def n_rows
count = 0
each { count += 1 }
count
end
end
end

regards

Simon A.


#2

Simon A. wrote:

Hi

I’m trying to get the numbers of rows in my liststore, but it seems I
lack a functionality.

In C, you can call :

gtk_tree_model_iter_n_children(GtkTreeModel *tree_model, GtkTreeIter
*iter)

You want Gtk::TreeIter#n_children


#3

Igor P. wrote:

Simon A. wrote:

Hi

I’m trying to get the numbers of rows in my liststore, but it seems I
lack a functionality.

In C, you can call :

gtk_tree_model_iter_n_children(GtkTreeModel *tree_model, GtkTreeIter
*iter)

You want Gtk::TreeIter#n_children

Either I’m missing something, or I didn’t made myself clear enough.

irb(main):005:0> Gtk::TreeIter.singleton_methods
=> [“gtype”]

So, I can’t use Gtk::TreeIter.n_children(whatever arguments).

And, as I said : “we can only call n_children from an instanced
Gtk::TreeIter object”

The important word here is “instanced”. Because, I cannot instance a
pseudo root iter above the ones in the liststore. It would be the same
for the top level nodes in a treestore.

I should have pasted the doc of the C function, which clearly says :

Returns the number of children that iter has. As a special case, if iter
is NULL, then the number of toplevel nodes is returned.

“if iter is NULL” is not possible in the ruby gnome2 bindings, afaik.


#4

Mike C. wrote:

Perhaps I’m not understanding correctly. Would

i = tree_model.iter_first
num_children = i.n_children

do what you want?

I realize it isn’t exactly what you would do in C code,
but that may be a good thing. Something like

TreeIter::n_children_from_model(tree_model)

doesn’t make much sense to me (though pretty
easy to implement, I guess, if there is a reason
to do so).

          MikeC

No, it would not work. It would return the first iter, which has no
child.

require ‘gtk2’

model = Gtk::ListStore.new(Integer)
10.times { model.append }
first_iter = model.iter_first
puts first_iter.n_children
count = 0
model.each { count += 1 }
puts count

will output

0
10

and first_iter.parent.n_children wil error, since the parent of
first_iter is nil.

I agree, it would be silly in the TreeIter class.

However, it could prove usefull in TreeModel, like :

Gtk::TreeModel#root_n_children => Integer
or
Gtk::TreeModel#n_children(Gtk::TreeIter = nil) => Integer (juste like
the C function)
or
Gtk::TreeModel#nb_toplevel_iters

And it’s very easy to implement for ListStore, as in my first post, but
there is already a C function, I don’t see a good reason not to use it
:).


#5

Perhaps I’m not understanding correctly. Would

i = tree_model.iter_first
num_children = i.n_children

do what you want?

I realize it isn’t exactly what you would do in C code,
but that may be a good thing. Something like

TreeIter::n_children_from_model(tree_model)

doesn’t make much sense to me (though pretty
easy to implement, I guess, if there is a reason
to do so).

          MikeC