Liam Willis
Liam Willis

Reputation: 681

Call subroutine automatically whenever cursor position changes in a Text widget

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!

Upvotes: 4

Views: 192

Answers (2)

optional
optional

Reputation: 2071

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.

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137807

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?)

Upvotes: 2

Related Questions