Gtk2::TreeView get_path_at_pos doesn't return array on Windows



(I was instructed on Perlmonks to post this problem here.)

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. (On Linux I
use the latest Perl 5.10 package that comes from the Ubuntu
repository, together with a reasonably recent Gtk2-Perl bundle from
CPAN. On Windows I installed the full Camelbox on a virgin machine.)

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="#007f00"><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{O}, "text" ], [ $activity_list{M}, "text2" ];
my @columns = $tslist->get_columns;

#### replace the workaround below from here...
$tslist->signal_connect('button-release-event' =>    sub {
        my ($self, $event) = @_;
                my ($path, $column, $cell_x, $cell_y) = $tslist->get_path_at_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}}};
                        }
                }
        });
#### ...to here

$win->show_all;
Gtk2->main;
################################################


I've since found a workaround:

I simply query the column widths for each column (these may have
changed, because the user could have resized 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_at_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}}};
                        }
                }
        });
####

(You may ask why I prefer this kind of interface to the popup-menu
style (e.g. as in cellrenderer_popup.pl in the examples).
Well, I felt that this interface, where the user just has to click a
few times, is better than the one where the user needs to examine the
popup list, select his choice and position the mouse on it. This is
especially true with *computerwise doubleplus-unenlightened* users who
have poor mouse control.)

For reference, I first posted this on Perlmonks at
http://www.perlmonks.org/?node_id=780082 .



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