Search code examples
perltreeviewgtk2

Perl Gtk2 CellRendererCombo: get selected item index


I have Gtk2::TreeView, some of columns are using CellRendererCombo. I have everything work ok, except of I can't get integer index of selected item in CellRendererCombo. Not a text, but the integer value.
As a workaround, it could be useful to associate somehow a hash with combo_model, but I don't know how. Could somebody help me? Trying and googling for a couple of days already.

Here's the code:

#!/usr/bin/perl

package uform;

use utf8;
use warnings;
use strict;
use Gtk2 -init;
use Glib qw/TRUE FALSE/;
use Glib ':constants';
use Data::Dumper;

use constant col0 => 0;
use constant col1 => 1;
use constant col2 => 2;
use constant colC => 3;

binmode(STDOUT,':utf8');

my $model; my $treeview; my @attr_models;

sub create_window ($$) {
    my ($width,$height)=(shift,shift);
    my $window = Gtk2::Window->new( "toplevel" );
    $window->signal_connect( "destroy", sub { Gtk2->main_quit(); } );
    $window->set_default_size($width,$height);

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

    $model = Gtk2::ListStore->new(
                    "Gtk2::Gdk::Pixbuf", #0 pic
                    "Glib::String", #1 product
                    "Glib::String", #2 attr
                    "Gtk2::ListStore"   #combo values list
                  );



    #sample_data
    #some combo lists
    foreach my $d (
    [qw(fast medium slow)],
    [qw(greay orange black white rainbow)],
    [qw(fat with_wholes molded)],
    [qw(long short jeans)]
    )
    {
    my $cmodel = Gtk2::ListStore->new('Glib::String');
    foreach my $str (@$d) {$cmodel->set($cmodel->append,0,$str);}
    push @attr_models,$cmodel;
    }

    #some pixbufs to play with
    my $pixbuf2 = Gtk2::Button->new->render_icon ('gtk-info', 'large-toolbar');



    #add some rows
    my @data = (
    [$pixbuf2,'Shirt',1,$attr_models[0]],
    [$pixbuf2,'Pants',0,$attr_models[0]],
    [$pixbuf2,'Cheese',2,$attr_models[1]],
    [$pixbuf2,'Cola',1,$attr_models[2]],
    [$pixbuf2,'Laptop',0,$attr_models[3]]
    );

    foreach my $st(@data) {
    $st->[2]=$st->[3]->get($st->[3]->iter_nth_child(undef,$st->[2]), 0);
        $model->set( $model->append, 
        0, $st->[0],
        1, $st->[1],
        2, $st->[2],
        3, $st->[3],
    );
    }


#Column0 setup
my $combo_model0 = Gtk2::ListStore->new( 'Gtk2::Gdk::Pixbuf' );
my $renderer_0 = Gtk2::CellRendererPixbuf->new;

my $column_0 = Gtk2::TreeViewColumn->new_with_attributes(
            "Pic",
            $renderer_0,
            pixbuf => col0
            );



#Column1 setup
my $renderer_1 = Gtk2::CellRendererText->new;
$renderer_1->set( editable => FALSE );

my $column_1 = Gtk2::TreeViewColumn->new_with_attributes(
            "Product",
            $renderer_1,
            text => col1
            );




#Column2 setup
my $renderer_2 = Gtk2::CellRendererCombo->new;
$renderer_2->set(
            editable    => TRUE,
            text_column => 0,
            has_entry   => FALSE
        );

$renderer_2->signal_connect (changed => sub {
    my ($renderer, $str, $iter)=@_;
    print Dumper (@_) . "\n";
    }
);

$renderer_2->signal_connect (edited => 
    sub {
    my ($renderer, $text_path, $new_text) = @_;
        my $combo_model = $renderer->get("model");
    $model->set ($model->get_iter_from_string ($text_path), col2, $new_text);
    }
);

my $column_2 = Gtk2::TreeViewColumn->new_with_attributes(
            "Attr",
            $renderer_2,
            text => col2,
            model => colC
            );







# main program starts
$treeview = Gtk2::TreeView->new( $model );

$treeview->get_selection->set_mode ('single');
$treeview->set_rules_hint( TRUE );

$treeview->append_column( $column_0 );
$treeview->append_column( $column_1 );
$treeview->append_column( $column_2 );


my $sw = Gtk2::ScrolledWindow->new( undef, undef );
$sw->set_shadow_type( "etched-in" );
$sw->set_policy( "never", "always" );

$sw->add( $treeview );

$vbox->pack_start( $sw, 1, 1, 0 );

$window->add( $vbox );
$window->show_all;
}

Solution

  • So, looks like there isn't any direct answer for this question. As a workaround, you may create array of hashes. Each element corresponds to one TreeView's row and have e.g. fields like 'combo_hash' and 'current_index'.
    'current_index' is self-explained, 'combo_hash' is array which consists of hashes with some fields like 'text' and e.g. 'index' (or other id).
    On CellRendererCombo's 'edited' signal you get current TreeView's index by $treeview->get_selection()->get_selected_rows()->get_indices() (it's simple int), and search for selected in CellRendererCombo's 'text' field through 'combo_hash' array. Don't forget to store finded 'index' to 'current_index'.
    This workaround allows non-unique text in different TreeView's rows, but can't handle non-unique text in one CellRendererCombo's ListStore.
    Another workaround is to inherite subclass from CellRenderer and embed ComboBox, which provides simple integer index. Looks better and have no limitations by non-unique data, but if array of hashes is unavoidable by design (you need to store lots of other info not visible in TreeView), first workaround should be more relevant.