Forum: Ruby-Gnome 2 n_children cannot be used to get number of toplevel nodes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B985bf8029d396259f579c70f809269a?d=identicon&s=25 Simon Arnaud (sarnaud)
on 2009-03-25 18:51
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 Arnaud
46a8ebdfabc2485407f89a2db5bdafb7?d=identicon&s=25 Igor Pirnovar (rooby)
on 2009-03-25 22:09
Simon Arnaud 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
B985bf8029d396259f579c70f809269a?d=identicon&s=25 Simon Arnaud (sarnaud)
on 2009-03-26 14:30
Igor Pirnovar wrote:
> Simon Arnaud 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.
8616a8bf5c9ac0db67ac26f89d3b06b6?d=identicon&s=25 Mike Charlton (Guest)
on 2009-03-26 15:12
(Received via mailing list)
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
B985bf8029d396259f579c70f809269a?d=identicon&s=25 Simon Arnaud (sarnaud)
on 2009-03-26 16:11
Mike Charlton 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
:).
This topic is locked and can not be replied to.