treeview and column indices



Hi, I've been trying to get the complex
TreeView model into my head, and ran into
a little roadblock, which I can't seem to find the answer to.

The script below is from the novell-study-guide examples,
modified to have some child indices and a second column.

My problem comes when editing column 1. In the original script's
callback &cell_edited, he derived the column from the $cell. 

my $column = $cell->get_data('column');

This works for column 0 and it's descendants, but when I click
on column 1, it always returns 0.

So I had to resort to adding a data-rider to the cellrenderer, to
mark it as column 0 or column 1.  

$renderer1->{ 'column' } = 1;

So is this the way to go in this type of situation, or am
I missing the way to get the right indices when I click on 
column 1 ?

The $cell being passed to the callback is different for
column 0 and column 1, so why would
my $column = $cell->get_data('column');
return 0 for column1 ?

############################################################################
#! /usr/bin/perl -w
use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;
use Data::Dumper;

my $window = Gtk2::Window->new( 'toplevel' );
$window->signal_connect( 'delete_event' => sub { Gtk2->main_quit; } );
$window->set_border_width( 5 );
$window->set_position( 'center_always' );

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

#add and show the vbox
$window->add( $vbox );
$window->show();

#our main event-loop
Gtk2->main();

sub ret_vbox {

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

   my $sw = Gtk2::ScrolledWindow->new( undef, undef );
   $sw->set_shadow_type( 'etched-out' );
   $sw->set_policy( 'automatic', 'automatic' );
   $sw->set_size_request( 300, 300 );
   $sw->set_border_width( 5 );

   my $tree_store = Gtk2::TreeStore->new( qw/Glib::String  Glib::String / );

#fill it with arbitry data
   foreach ( 1 .. 30 ) {
      my $parent_nr = $_;
#the iter is a pointer in the treestore. We
#use to add data.
      my $iter = $tree_store->append( undef );
      $tree_store->set( $iter, 0 => "Parent $parent_nr" );
      $tree_store->set( $iter, 1 => "----------" );

      foreach ( 1 .. 3 ) {
         my $iter_child = $tree_store->append( $iter );
         $tree_store->set( $iter_child, 0 => "Child $_ of Parent $parent_nr" );
      }
   }

   my $tree_view = Gtk2::TreeView->new( $tree_store );
   $tree_view->set_reorderable( 1 );    #drag and drop reordering

   my $tree_column = Gtk2::TreeViewColumn->new();
   $tree_column->set_title( "Click to sort" );

   my $renderer = Gtk2::CellRendererText->new;
   #add a data rider to store column
   $renderer->{ 'column' } = 0;
   $renderer->set_property( 'editable', TRUE );
   $renderer->signal_connect( edited => \&cell_edited, $tree_store );

   $tree_column->pack_start( $renderer, FALSE );
   $tree_column->add_attribute( $renderer, text => 0 );
   $tree_view->append_column( $tree_column );

#second column
   my $tree_column1 = Gtk2::TreeViewColumn->new();
   $tree_column1->set_title( "-----------------" );

   my $renderer1 = Gtk2::CellRendererText->new;

   #add a data rider to store column
   $renderer1->{ 'column' } = 1;

   $renderer1->set_property( 'editable', TRUE );
   $renderer1->signal_connect( edited => \&cell_edited, $tree_store );
   $tree_column1->pack_end( $renderer1, FALSE );
   $tree_column1->add_attribute( $renderer1, text => 1 );
   $tree_view->append_column( $tree_column1 );
   $tree_view->set_search_column( 0 );
   $tree_column1->set_sort_column_id( 0 );

   $tree_view->set_reorderable( TRUE );
   $sw->add( $tree_view );

   $vbox->pack_start( $sw, TRUE, TRUE, 0 );
   $vbox->show_all();
   return $vbox;
}
#######################################################################
sub cell_edited {
   print "@_\n";
   my ( $cell, $path_string, $new_text, $model ) = @_;

#   print Dumper( [ \$cell ] ), "\n";

#   my @props = $cell->list_properties;
#   print join "\n",@props;
#   print "@props\n\n";
   
#   foreach my $prop( @props ){
#         print Dumper([\$prop]),"\n";
#         }

  my $path = Gtk2::TreePath->new_from_string( $path_string );
  print "$path_string\n";
  
  my @indices = $path->get_indices;
  print "indices->@indices\n";

# the original get column, which returns 0 for column 1
#my $column = $cell->get_data('column');

# this is what I did to get the column number
   my $column = $cell->{ 'column' };
   print "column->$column\n";

   my $iter = $model->get_iter( $path );
   $model->set_value( $iter, $column, $new_text );
   return FALSE;
}

__END__

Thanks.

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



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