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

View: New views
3 Messages — Rating Filter:   Alert me  

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

by Peter Juhasz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

(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 .
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

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

by Dave Howorth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Juhasz wrote:

> (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.

Thanks for the nice example, which works for me on Linux. I don't have a
 M$ box so can't help you with why it doesn't work there. But maybe
there's a neater other way. Have you tried:

 my ($path, $column) = $tslist->get_cursor;

instead of get_path_at_pos() ?

Cheers, Dave
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Parent Message unknown Re: Gtk2::TreeView get_path_at_pos doesn't return array on Windows

by Dave Howorth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Juhasz wrote:
> On Thu, Jul 16, 2009 at 11:22 AM, Dave
> Howorth<dhoworth@...> wrote:
>> Peter Juhasz wrote:
>>> (I was instructed on Perlmonks to post this problem here.)

And it's best to keep all replies on-list as well :)

> Yes, in fact I've found the get_cursor method because I needed it for
> a different trick.
>
> Simply put, I want to enable some rudimentary keyboard functionality
> in my Gtk2::Ex::Simple::List.
> The cells of this list are editable if the user double-clicks on them
> but double-clicking on every cell, every time quickly becomes tedious.
> So I want to set it up so that pressing Tab will set the focus on the
> next cell and also open it for editing.
>
> I can do this by hijacking the key-pressed-event of the list, getting
> the coordinates of the current cell, calculating the coordinates of
> the next cell, then finally setting the cursor on it with
> set_cursor($new_path, $new_column, TRUE).

As a rule of thumb, I've found that it's not necessary to mess around
with co-ordinates. There's always some way to discover the appropriate
widget's identity but the 'fascination' in the game is discovering just
how in each case ;/

> There is only one problem with this: I have to press Enter to get the
> edited contents of the cell registered. If I just press Tab the cell
> forgets what I've just written into it. How could I force the current
> cell to update itself before I move the cursor to the next cell?

Running your example program, if I edit the text in a cell then move to
another cell with a cursor arrow, it saves the edit. If I use TAB it
saves the edit but doesn't move the cursor (probably need to set up tab
navigation?). I need to press enter or space to start to edit a cell. In
short, it doesn't behave exactly like yours.

I don't know much about this aspect but I think the behaviour is linked
to your platform and perhaps themes. Others will know more (hence
keeping it on the list). But be careful changing behaviour away from
whatever standard people expect.

> I've tried to find answers in the documentation but frankly, I can't
> find my way in the POD.

You'll probably find some old mails from me in the archive with a
similar issue :( The Perl POD generally doesn't repeat stuff that's
already in the C docs, to reduce the maintenance tuits needed. So you
definitely need to look at the C docs. I've also found it helpful to
look at the Python docs, since they include a bit more info sometimes
while the binding is similar to Perl. There are also various tutorials
about trees in particular and gtk in general. You should find links in
the archives of this list.

Cheers, Dave
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list