4

I am using the Tk::Text module.

I want that whenever the user changes the position of the cursor inside the Tk::Text module, it should act as a trigger to call a subroutine which I have written.

How do I go about implementing this?

EDIT:

As answered by Donal Fellows, I somehow need to find if the insert mark is changed when a call is made to the markSet routine. I have searched the net extensively to find a solution to this problem, but to no avail. Now I need you guys to help me with it. Thanks!

Liam Willis
  • 661
  • 6
  • 17

2 Answers2

2

There isn't a predefined callback for when the location of the insert mark changes (that's the terminology you're looking for) but it is always set via the markSet method. Maybe you can put something in to intercept calls to that method, see if they're being applied to insert, and do your callback? (That's certainly how I'd do it in Tcl/Tk; I don't know how easy it is to intercept methods on the Perl side of things but surely it must be possible?)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
1

This is what https://stackoverflow.com/a/22356444/2335842 is talking about, see http://p3rl.org/perlobj and http://p3rl.org/Tk::Widget and http://p3rl.org/require for details

#!/usr/bin/perl --
use strict; use warnings;
use Tk;
Main( @ARGV );
exit( 0 );

BEGIN {
    package Tk::TText;
    $INC{q{Tk/TText.pm}}=__FILE__;
    use parent qw[ Tk::Text ];
    Tk::Widget->Construct( q{TText} );
    sub markSet {
        warn qq{@_};
        my( $self, @args ) = @_;
        $self->SUPER::markSet( @args );
    }
}

sub Main {
    my $mw = tkinit();
    $mw->TText->pack;
    use Tk::WidgetDump; $mw->WidgetDump; ## helps you Tk your Tk
    $mw->MainLoop;
}
__END__
Tk::TText=HASH(0x10f7a74) insert @347,218 at - line 13.
Tk::TText=HASH(0x10f7a74) anchor insert at - line 13.
Community
  • 1
  • 1
optional
  • 2,061
  • 12
  • 16
  • Can you expain what `BEGIN { ... }` is doing in a bit more detail? – Liam Willis Apr 12 '14 at 15:32
  • the BEGIN block hook on `markSet` method to allow you to inject code when it is called. This is known as monkey patching. The proposed solution alter the package TText and will hook on every subsequently created intances. For per instance monkey patching, you might give a glance at http://stackoverflow.com/questions/449690/how-can-i-monkey-patch-an-instance-method-in-perl – FabienAndre Apr 12 '14 at 22:28
  • not monkeypatching, its a right proper subclass ... the links explain the details – optional Apr 29 '14 at 03:50