kikuchiyo has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a Gtk2-Perl app with a Gtk2::Ex::Simple::List (which is derived from TreeView, hence the title).

I want to set up one of the columns in a special way, specifically, I want this column to hold values from a short list, and each click on a cell in this column should toggle the next value from the pre-specified list. Run the attached code for clarification.

Obviously, for this I need to know which column did the user click into.

I found TreeView's get_path_at_pos method, which, according to the documentation:
"In scalar context, returns the Gtk2::TreePath, in array context, adds the Gtk2::TreeViewColumn, and $x and $y translated to be relative to the cell."

On Linux, this works fine and I can get the column number. However, on Windows (with newest Camelbox), get_path_at_pos doesn't seem to work in array context! All it gives back is the path object.

How can I remedy this? How can I
a) either fix get_path_at_pos's behavior, or
b) get the column number of the clicked cell in some other way?

Thanks in advance for any insight.

The code, which demonstrates both my intent (on Linux, where it works) and the problem (on Windows, where it doesn't) is below:
################################################ #!/usr/bin/perl use strict; use warnings; use Gtk2 -init; use Gtk2::Ex::Simple::List; use Glib ':constants'; my %activity_list = ( O => '<span color="#7f7f00"><b>One</b></span>', M => '<span color="#7f007f"><b>Two</b></span>', U => '<span color="#007f7f"><b>Five</b></span>', E => '<span color="#00cf00"><b>Three, Sir, Three!</b></span>', A => '<span color="#ff0000"><b>BOOM</b></span>' ); my %activity_order = ('O' => 'M', 'M' => 'U', 'U' => 'E', 'E' => 'A', +'A' => 'O'); my %activities_rev = reverse %activity_list; my $win = Gtk2::Window->new; $win->set_border_width (6); $win->signal_connect (delete_event => sub { Gtk2->main_quit; }); my $tslist = Gtk2::Ex::Simple::List->new ( "Special column" => 'markup', "Normal column" => 'text' ); $win->add ($tslist); $tslist->get_selection->set_mode ('single'); $tslist->set_reorderable(FALSE); $tslist->set_column_editable (0, FALSE); $tslist->set_column_editable (1, TRUE); my $dtslist = $tslist->{data}; push @$dtslist, [ $activity_list{M}, "text" ], [ $activity_list{O}, "t +ext2" ]; my @columns = $tslist->get_columns; $tslist->signal_connect('button-release-event' => sub { my ($self, $event) = @_; my ($path, $column, $cell_x, $cell_y) = $tslist->get_path_a +t_pos ($event->x, $event->y ); # <------- !!!!! if ($path) { if ($column == $columns[0]) { my $row = $path->to_string; my $value = $dtslist->[$row][0]; $dtslist->[$row][0] = $activity_list{$activity_order{$ +activities_rev{$value}}}; } } }); $win->show_all; Gtk2->main; ################################################

PS. Please direct me to the right place if this is not the right place to ask this.

Replies are listed 'Best First'.
Re: Gtk2::TreeView get_path_at_pos doesn't return array on Windows
by kikuchiyo (Hermit) on Jul 15, 2009 at 07:12 UTC
    I've found a workaround. I simply query the column widths for each column (these may have changed, because the user could resize them), make an array that holds the cumulative column boundary positions, then check if the click was between the borders of the special column.

    The relevant part to be replaced in the code above follows.
    $tslist->signal_connect('button-release-event' => sub { my ($self, $event) = @_; my ($path, $column, $cell_x, $cell_y) = $tslist->get_path_a +t_pos ($event->x, $event->y ); # <------- !!!!! my @widths; $widths[0] = $columns[0]->get_width(); for my $i (1..$#columns) { $widths[$i] = $widths[$i-1] + $columns[$i]->get_width( +); } if ($path) { if ($event->x > 0 and $event->x < $widths[0]) { my $row = $path->to_string; my $value = $dtslist->[$row][0]; $dtslist->[$row][0] = $activity_list{$activity_order{$ +activities_rev{$value}}}; } } });
Re: Gtk2::TreeView get_path_at_pos doesn't return array on Windows
by zentara (Cardinal) on Jul 15, 2009 at 17:17 UTC
    A better place to ask this is probably on the Gtk2-Perl maillist. It sounds like one of the many tiny bugs they are always fixing. I'm glad you found a workaround. Just sign up for the list, and put your code and fix in the post.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
        Sorry, I'm on vacation ( the funemployed :-) ) and I don't have Assistant installed. Once again, best advice is to ask on the maillist.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku