Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Table matrix suspected selected cell discrepancy

by merrymonk (Hermit)
on Aug 17, 2011 at 15:14 UTC ( [id://920713]=perlquestion: print w/replies, xml ) Need Help??

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

I have something that I do understand about what is given for the selected cell in a table matrix.
The Perl below creates a table matrix and the sub brscmd is used whenever a cell is slected using either the left hand mouse key or the up/down and left/right keys on the key board.
The selected cell is obtained in two ways. These are
1. from the ‘standard’ argument to the browse sub;
2. using $t->curselection() where $t is the table matrix.
Both values are printed out.
When I use the left hand mouse key both values are the same.
When I use the keyboard keys, the value from curselection always seems to be that for the previous selection of a cell.
Is this how it should be or is there something I can do to ensure I always get the same values whatever method I use to select cells in a table matrix?
use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (10, 10); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $arrayVar->{"$row,$col"} = 2*$row + 3*$col; } } print "Creating Table...\n"; my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $t->pack(-expand => 1, -fill => 'both'); Tk::MainLoop; sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; my ($sel, $js); my $sel = $t->curselection(); foreach $js (@$sel) { print "\n[brscmd] actual index <$actual_index> from curselection < +$js>\n"; } }

Replies are listed 'Best First'.
Re: Table matrix suspected selected cell discrepancy
by zentara (Archbishop) on Aug 17, 2011 at 17:29 UTC
    Is this how it should be or is there something I can do to ensure I always get the same values whatever method I use to select cells in a table matrix?

    Just being practical, but it seems that calling

    my $sel = $t->curselection();
    in the browsecommand sub causes the keyboard binding to see the current selection as the previous one.

    Why not use the simpler

    sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; print "$row $col\n"; print "\n[brscmd] actual index <$actual_index> from curselection<$actu +al_index>\n"; }
    Is there a reason you need to call $t->curselection from the brwscmnd?

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      As you may well have guessed this is a 'test' from a bigger appliation.
      I called $t->curselection from the brwscmd because I also
      want to be able to select more than one cell using the
      windows explorer techique where with the left hand mouse key
      1. the Ctrl key allows you to select a number of cells;
      2. the Shift key allows you to select a rnge of cells.
      I have just looked in the documentation for the tablematrix.
      In the Default Bindings section it says that "The left, right, up and down arrows move the active cell".
      Therefore I wonder if there is problem with the basic table matrix code.
        It might be a little bug, but I added a button to show the current selected cell, and it works to call $t->curselection() from outside the browsecommand sub. I also included a hack discovered awhile ago to force the widget to update. You might find it handy. :-)

        This gets the current active cell correct, but from outside the browsecommand sub. There may be some tag to configure and set, to get the operation you want, but it eludes me.

        An interesting thing is that if the curselection is called from the button, it works. But I added an external sub call to the browsecommand, and it returns the wrong info!! There is something funny in the browsecommand sub maintaining cell state.

        #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (10, 10); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $arrayVar->{"$row,$col"} = 2*$row + 3*$col; } } print "Creating Table...\n"; my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $t->pack(-expand => 1, -fill => 'both'); $mw->Button( -text => "Show Selected", -command => sub{ print $t->curselection(),"\n"; })->pack(-expand => 1, -fill => 'x'); Tk::MainLoop; sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; my ($sel, $js); $sel = $t->curselection(); print "@$sel\n"; #didn't do much #$t->set('active',@$sel); &get_active(); &TMRefresh($t); foreach $js (@$sel) { print "\n[brscmd] actual index <$actual_index> from curselection < +$js>\n"; } } sub get_active{ &TMRefresh($t); my @active = @{$t->curselection()}; print "active-> @active\n"; } sub TMRefresh { #Required input TableMatrix object. #use to force matrix to update, a code trick return if (!$_[0]); $_[0]->configure(-padx =>($_[0]->cget(-padx))); }

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://920713]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found