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

sorry if my title is confusing, but I am confused. I'm working with the Tk::TableMatrix widget to edit tabular data, though my problem may not be specific to that widget. Apparently in Tk::TableMatrix the event <Control-Key-v> has a default binding to a callback that performs a paste operation. I don't know if this is an inherited binding or what: the documentation for Tk::TableMatrix makes no mention of it and I am, as they say, a total Tk newbie. That "built-in" paste function works great and I don't want to have to write my own. But I need to know when a paste operation begins and then when it is finished. Idealy, I think, I would create my own callback bound to <Control-Key-v> within which I could call the "built-in" paste function. But I can't find any documentation on this paste function, nor can I figure out any other way to call it other than generating the event: <Control-Key-v>. I know you can have multiple callbacks bound to the same event. And that the callbacks are executed in a certain order according to their 'specicifity'. So maybe I could construct two bindings such that one would run right before the default <Control-Key-v> callback and the other one right afterwards, but that seems messy to me. I think it wreaks. So does anybody know if there is some sort of $widget->paste() function that Tk::TableMatrix is calling that I could call too? Or am I thinking about this the wrong way? Or do I need to be more specific? (by the way, the reason I want to know when a paste operation is in progress is so that my I can capture mulitple cell edits. I'm trying to build some Undo functionality into Tk::TableMatrix, and I want a multiple cell edit (such as with "paste", "cut", or "replace all") to be an atomic undo.) Thank you so much, I hope this isn't a topic that's been broached and answered previsouly, I searched a little bit and really didn't see anything that seemed to match.
  • Comment on how to modify a built-in event (capturing a paste operation in Tk::TableMatrix)

Replies are listed 'Best First'.
Re: how to modify a built-in event (capturing a paste operation in Tk::TableMatrix)
by zentara (Cardinal) on Nov 09, 2007 at 13:16 UTC
    Here is a start. If you look at the TableMatrix.pm code, you will find the sub called Paste. You can modify it and put a start and end into it, like
    sub Paste{ print "start\n"; my $w = shift; my $cell = shift || ''; ## Perltk not sure if translated correctly my $data; if ($cell ne '') { eval{ $data = $w->GetSelection(); }; return if($@); } else { eval{ $data = $w->GetSelection('CLIPBOARD'); }; return if($@); $cell = 'active'; } $w->PasteHandler($w->index($cell),$data); $w->focus if ($w->cget('-state') eq 'normal'); print "end\n"; }

    Now the question becomes how to make that change part of your code, without permanently altering the module for everyone.

    The easiest method( but clunkiest) is to copy TableMatrix.pm to a subdir called Tk in your script's working directory. Then you can modify your TableMatrix.pm's Paste sub, and put

    use lib '.';
    at the top of your script.

    The better way, is to just redefine the sub, but you get a redefine warning.

    #!/usr/bin/perl use warnings; no warnings "redefine"; use Tk; use Tk::TableMatrix; sub Tk::TableMatrix::Paste { print "start paste\n"; my $w = shift; my $cell = shift || ''; ## Perltk not sure if translated correctly my $data; if ($cell ne '') { eval{ $data = $w->GetSelection(); }; return if($@); } else { eval{ $data = $w->GetSelection('CLIPBOARD'); }; return if($@); $cell = 'active'; } $w->PasteHandler($w->index($cell),$data); $w->focus if ($w->cget('-state') eq 'normal'); print "end paste\n"; } my $top = MainWindow->new; my $arrayVar = {}; foreach my $row (0..20){ foreach my $col (0..10){ $arrayVar->{"$row,$col"} = "r$row, c$col"; } } my $t = $top->Scrolled('TableMatrix', -rows => 21, -cols => 11, -width => 6, -height => 6, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -selectmode => 'extended', -resizeborders => 'both', -titlerows => 1, -titlecols => 1, -bg => 'white', # -state => 'disabled' # -colseparator => "\t", # -rowseparator => "\n" ); $t->tagConfigure('active', -bg => 'gray90', -relief => 'sunken'); $t->tagConfigure( 'title', -bg => 'gray85', -fg => 'black', -relief => + 'sunken'); # $t->bind("<Any-Enter>", sub { $t->focus }); $t->pack(-expand => 1, -fill => 'both'); Tk::MainLoop;

    The best way is to subclass TableMatrix.pm, but you can search how to do that yourself. Search for making a "Tk megawidget". It's basically the same thing as I shown above, but you create a "MyTableMatrix" widget, based on TableMatrix.pm. This avoids the "redefined" warnings.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      OK, yes, thanks, that sounds like a possible good solution. I like the idea of subclassing the widget, and then redefining the Paste sub with a minor modification.

      I guess I was sort of afraid to look in the TableMatrix.pm file because I didn't think I would understand it. But it looks like TableMatrix.pm is just the default bindings plus some useful interface functions such as Paste. Not too scary afterall. (though I definitely don't "get" everything.)

      Also, I sort of assumed if a sub wasn't mentioned in the documentation that it was considered "private" and I wasn't supposed to use it unless I really know what I'm doing. But I guess I could say now that I know that's what I need to do.

      (I do think making a subclass is the best way to go (I love OO), but another possible solution it seems, (now that I know there is a Paste sub I can call) would be to just rebind <Control-v> to my own sub and I could do my 'before and after' code with a call to Paste in between. Though I would also need to rebind <ButtonRelease-2>.)

      back to your your example: in the Paste() redefine above, you could instead of writing all the original Paste code in between

      print "start paste\n"; my $w = shift; my $cell = shift || ''; ## Perltk not sure if translated correctly
      and print "end paste\n";, you could just call $w::SUPER->Paste($cell); Is that correct? I read a couple of the tutorials on OO with Perl, but I haven't really put it to use yet.

      Thanks again so much for the reply. You were very helpful.