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

I am working with the Tk::LineNumberText widget. As an argument it takes the type of widget (has to be derived from Tk::Text) that you want the text part to be. I've been experimenting with Tk::CodeText and the results where good. Line numbers and highlighting was all working as it should. Then I saw the oh so nifty autoindent feature and turned that on for the CodeText widget. Now the CodeText widget binds to 'return' to do the indenting, and the LineNumberText wants to overright that binding to see that the user is on a new line. I've been tweaking LineNumberText to see if i can get it to play nice but i just don't seem to grasp the order of events with bindings. I had hoped that if i bound the 'return' event of the frame parenting the text widget, that it would get called after the text widgets own event. This is not the case, or at least doesn't appear to be.

Is there some way to have two bindings for a single event on a single widget OR to somehow use inheritance/parenting to acheive that same end. Obviously I could just hand modify both modules to play nice with the other, but i was hoping for some more general method that could be used to patch one or the other in hopes of future prosperity.

Thanks in advance for any assitance you can provide.


___________
Eric Hodges

Replies are listed 'Best First'.
Re: Tk bindings
by eserte (Deacon) on May 19, 2004 at 08:56 UTC
    A possible solution is to get the old value of the binding and fire this callback in the new callback. E.g.
    #!/usr/bin/perl use Tk; $mw = tkinit; $mw->bind("<Return>" => sub { warn "Old binding" }); my $old_binding = $mw->bind("<Return>"); $mw->bind("<Return>" => sub { $old_binding->Call(); warn "New binding" }); MainLoop; __END__
    Note that the return value of bind is not a CODE reference, but a Tk::Callback object, so you have to use the Call() method.

    It's also possible to add a new bindtag via the bindtags() method:

    #!/usr/bin/perl use Tk; $mw = tkinit; $mw->bind("<Return>" => sub { warn "Old binding" }); $mw->bind("anotherbindtag", "<Return>" => sub { warn "New binding" }); $mw->bindtags(["anotherbindtag", $mw->bindtags]); MainLoop; __END__

      Thanks your second solution was great. Thats a great way for controls that have subwidgets to bind stuff on those subwidgets without messing up the original bindings. I've fired off an email to the author so hopefully it will all get fixed up! Thanks!


      ___________
      Eric Hodges
Re: Tk bindings
by bbfu (Curate) on May 19, 2004 at 09:09 UTC

    Also note that it's possible to bind to an entire class using something like $mw->bind('Tk::CodeText', $callback);, so you may need to save/change that callback as well.

    Update: See Re: Tk::Tree double click on + for an example of saving and calling this type of binding.

    bbfu
    Black flowers blossom
    Fearless on my breath