Re: TreeIters again




On Feb 21, 2008, at 9:23 AM, Dave Howorth wrote:

The problem is that $selected_row_iter is never equal to $iter. $iter is
a new TreeIter for every sibling.

Yes. That's because the objects are intended to be allocated on the stack. See below.


<rant>TreeIters seem like a very badly thought out feature!</rant>

GtkTreeIter is designed to be very fast in C code, using stack allocation and short object lifetimes. This makes binding GtkTreeIter to a dynamic language like perl rather, ahem, challenging, and Gtk2::TreeIter is not very natural in perl. Sorry. Gtk2::ListStore is natural for wrapping up in tie() magic to masquerade as an array, but Gtk2::TreeStore is not so simple. Therefore, there is no canonical perlish iteration interface. (But Gtk2::Ex::Simple::TiedList is quite handy for Gtk2::ListStores.)

So, not a badly thought-out feature in and of themselves, but not very fun for language bindings.


I know I could use iter_nth_child() to iterate using an index, but I
haven't found a way to discover the index of the selected child.
Something like:

   my $selected_index =
           $tree_store->???child_index???($parent, $selected_iter);

You can get a path from an iter, and you can convert a path to indices. You can build a path from indices. From the docs:

A path is essentially a potential node. It is a location on a model that may or may not actually correspond to a node on a specific model. The Gtk2::TreePath object can be converted into either an array of unsigned integers or a string. The string form is a list of numbers separated by a colon. Each number refers to the offset at that level. Thus, the path '0' refers to the root node and the path '2:4' refers
       to the fifth child of the third node.



So,

    my $selected_index =
        ($tree_store->get_path ($selected_iter)->get_indices)[-1];



--
It's all very complicated and would take a scientist to explain it.
  -- MST3K





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]