dumping an entire treeview into a HoH


I'm hoping someone can save me some hacking by showing me
a generic code snippet for walking an entire tree and getting
all it's values.

This is code from the Novell Study Guide, and I added a button,
with a callback called "clicked", in which I want to automatically
walk the tree and collect all values without selecting them all.

Is this possible? Or is it better to create a data structure first,
then populate the tree from the data structure?

#! /usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;

#Create a constant to differentiate columns in the model
#(With more than once column this will become more clear)
use constant MODEL_FIRST_COLUMN       => 0;

#Create a constant to differentiate different renderers;
#This makes things easy to discover which renderer in the view
#was changed
use constant RENDERER_FIRST_TEXT       => 0;

#standard window creation, placement, and signal connecting
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });

#this vbox will geturn the bulk of the gui
my $vbox = &ret_vbox();

#add and show the vbox

#our main event-loop

sub ret_vbox {

my $vbox = Gtk2::VBox->new(FALSE,5);
$vbox->set_size_request (300, 300);

        #this is one of the provided base Gtk2::TreeModel classes.
        my $tree_store = Gtk2::TreeStore->new(qw/Glib::String/);

        #fill it with arbitry data
        foreach (1..3) {
                my $parent_nr = $_;
                my $iter = $tree_store->append(undef);
                $tree_store->set ($iter,MODEL_FIRST_COLUMN, "Parent $parent_nr");
                 foreach (1..3){
                        my $iter_child = $tree_store->append($iter);
                        $tree_store->set ($iter_child,MODEL_FIRST_COLUMN, "Child $_ of Parent $parent_nr");
        #this will create a treeview, specify $tree_store as its model
        my $tree_view = Gtk2::TreeView->new($tree_store);

                #create a Gtk2::TreeViewColumn to add to $tree_view
                my $tree_column = Gtk2::TreeViewColumn->new();
                $tree_column->set_title ("Click to sort");
                        my $renderer = Gtk2::CellRendererText->new;
                        $renderer->set_property('editable' => TRUE);
            #Attach a 'renderer_number' value to the renderer.
            #This can be used to differentiate between renderers
            #when we have a few renderers which can be edited
            $renderer->{'renderer_number'} = RENDERER_FIRST_TEXT;
                        $renderer->signal_connect (edited => \&cell_edited, $tree_store);
                $tree_column->pack_start ($renderer, FALSE);
                $tree_column->add_attribute($renderer, text => MODEL_FIRST_COLUMN);
        #add $tree_column to the treeview
        $tree_view->append_column ($tree_column);

my $button = Gtk2::Button->new ('Get all values');
$button->signal_connect (clicked => \&clicked, $tree_view );

return $vbox;

sub clicked {
  print "@_\n";
  # I want to dump the tree into a HoH structure, or arrays


sub cell_edited {
  my ($cell, $path_string, $new_text, $model) = @_;
  my $path = Gtk2::TreePath->new_from_string ($path_string);

    #Get the renderer number;
        my $column_of_model;
    if ($cell->{'renderer_number'} == RENDERER_FIRST_TEXT) {

        $column_of_model = MODEL_FIRST_COLUMN;

        my $iter = $model->get_iter ($path);
  $model->set_value ($iter, $column_of_model, $new_text); 



I'm not really a human, but I play one on earth.

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