in reply to Re^4: Table matrix suspected selected cell discrepancy
in thread Table matrix suspected selected cell discrepancy

If you want to look at the Tk::TableMatrix code, run

perldoc -m Tk::TableMatrix

It seems to do the arrow bindings correctly, but there is some xs we don't see.


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh
  • Comment on Re^5: Table matrix suspected selected cell discrepancy

Replies are listed 'Best First'.
Re^6: Table matrix suspected selected cell discrepancy
by merrymonk (Hermit) on Aug 18, 2011 at 07:34 UTC
    I thought that I would try trapping the button or key that was used to select a cell hoping that I would be able to tell in the browse command how the cell had been selected.
    Therefore I can tell if one of the arrow keys had been used and I would know which cell should be active.
    This is in the Perl code below.
    You will see that I used bind’s with Keypress and ButtonPress.
    The binds printed and set a variable to indicate which bind had been used.

    When a mouse button was used, the output was:
    print_button1 button 1 pressed
    brscmd entry - last_button_key <button 1>
    brscmd actual index <4,3> from curselection <4,3>
    brscmd exit

    When an arrow key was used, the output was:
    brscmd entry - last_button_key <button 1>
    brscmd actual index <5,3> from curselection <4,3>
    brscmd exit
    print_keysym Keysym = Down numeric 65364

    As can be seen, the bind for the mouse button happed before the browse command was entered but the bind for the arrow key happened after the browse command was finished.
    I guess this could be the reason for my original problem when I found that the selected cell was always the previous one when using an arrow key.
    Can I alter when a bind is used or is that something 'deep' in the tablematrix code?
    use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my $arrayVar = {}; my $selcou = 0; my $last_button_key; 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'); sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; my ($sel, $js); print "\n[brscmd] entry - last_button_key <$last_button_key>\n"; my $sel = $t->curselection(); $sel_cou += 1; #$t->curselection($sel_cou) &TMRefresh($t); foreach $js (@$sel) { print "\n[brscmd] actual index <$actual_index> from curselection < +$js>\n"; } print "\n[brscmd] exit\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))); } $t->bind('<KeyPress>' =>\&print_keysym); $t->bind('<ButtonPress-1>' =>\&print_button1); $t->bind('<ButtonPress-2>' =>\&print_button2); sub print_keysym{ my($widget) = @_; my $e = $widget->XEvent; my($keysym_text, $keysym_decimal) = ($e->K, $e->N); print "[print_keysym] Keysym = $keysym_text numeric $keysym_decim +al\n"; $last_button_key = 'key'; } sub print_button1{ my($widget) = @_; print "[print_button1] button 1 pressed\n"; $last_button_key = 'button 1'; } sub print_button2{ my($widget) = @_; print "[print_button2] button 2 pressed\n"; $last_button_key = 'button 2'; } Tk::MainLoop;
      Can I alter when a bind is used or is that something 'deep' in the tablematrix code?

      Yes, there are a couple of ways to do it. Some sample code is shown in the ReadMore below.

      The first method, is a bit crude, and requires a no warnings "redefine";. You just copy in the sub(s) you want to redefine, obtained from perldoc -m Tk::TableMatrix, into your script, and rewrite the sub(s) anyway you want.

      The second, better method, is to create a custom package widget, which allows you to redefine any sub, without generating warnings.

      I'm not sure which sub would need to be redefined, or if redefining it may break something else, like possibly the action of hitting the Enter key on a cell. A second package method using ClassInit is also shown, and may be needed to catch $mw bindings.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        Thanks for that. Clearly something I will try!