Custom TreeModel



Hi,

I tried implementing a custom Gtk2::TreeModel in Perl and my results are not quite as expected. I followed the advice from the Gtk2-Perl recipes [1] and the example customlist.pl [2]. My intent is to create a mode that wraps a real tree and not a flat list.

My strategy was to use a DOM tree as the real source of my model and to it implement the methods of TreeModel on top of the existing xml node hierarchy (XML::LibXML DOM nodes). I've attached the code of my implementation to this email.

What I've noticed is that the data stored in the Gtk2::TreeIter is really volatile and that it doesn't survive too long after a method returns. Furthermore, only the 2 first elements of the TreeIter make it back to the TreeModel methods intact. I also noticed that setting references into the last 2 fields can yield strange situations.

So far the only way that I found in Perl for passing my elements into the TreeIters and to get them back is to pass a unique number that will help me find back the corresponding node. This way the code works but it's far from being optimal as each DOM node has to be assigned to an internal hash in order to persist between the subsequent method calls of the model. The main problem here is that I don't know when I can remove elements from the hash and I endup copying all DOM objects into the hash.

Here's how I pass the elements into the tree iter
ÂÂÂ my $address = refaddr $node;
ÂÂÂ $self->{iters}{$address} = $node;
ÂÂÂ return [ $self->{stamp}, $address, undef, undef ]; # The TreeIter

Ideally I would insert the XML node into the iter and get it back at each method, but this results a segmentation fault later on in the program:
ÂÂÂ return [ $self->{stamp}, 0, $node, undef ];Â # Crash latter on

If I try to pass to path to the node (XPath _expression_ as a string), I receive undef.
ÂÂÂ return [ $self->{stamp}, 0, $node->nodePath, undef ]; # The string is lost in the next calls

If I pass a reference just as customlist.pl does I endup with an reference too but pointing to the wrong place:
ÂÂÂ return [ $self->{stamp}, 0, [ $xpath ], undef ]; # Next calls receive [0,0,\$VAR1->[1],undef];

It seems that implementing a real tree model (and not a flat list) with Gtk2 is not so trivial. It's either that or I failed miserably :)

I implemented the same code in C and it works fine as the TreeIter is not losing the data that gets inserted and calls to the model methods get the proper values.

Is the Perl sample code wrong? Maybe I missed something..
Would it make sense to add it to the Gtk2-Perl examples, perhaps someone could find it useful.

[1] http://live.gnome.org/GTK2-Perl/Recipes#Why_not_cook_up_a_TreeStore_from_scratch_.3F_Like_in_olden_times...
[2] http://git.gnome.org/browse/perl-Gtk2/tree/examples/customlist.pl
--
Emmanuel Rodriguez

Attachment: dom.pl
Description: Perl program



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