| zentara 2007-06-07, 10:04 pm |
| On Thu, 07 Jun 2007 04:20:28 -0700, nishant <nishant.031@gmail.com>
wrote:
>Hi ,
>How can I find out the line no of the text over which the mouse cursor
>is placed ?
Here is a generalized example, that should show you the way.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::Balloon;
my @last = ('','');
my @word = ('','');
my $msg;
my $mw = tkinit;
my $t = $mw->Text->pack;
my $balloon = $mw->Balloon(
-initwait => 0,
-balloonposition => 'mouse',
-postcommand => \&post_com,
-motioncommand => \&motion_com
);
$t->insert( 'end', "Balloon over words is easy, eh?\n" );
$t->insert( 'end', "Adding position over words is easy also, eh?\n" );
$balloon->attach( $t, -balloonmsg => \$msg );
MainLoop;
########################################
################
sub post_com {
if ( $word[0] eq $word[1] ) {
0; # No word under mouse - don't post the balloon.
}
else {
# Have a word under mouse - change the message:
my $word = $t->get( $word[0], $word[1] );
# Skip it if it contains non-word chars:
return 0 if $word =~ /\W/;
$msg = "The word under the mouse is: $word @last";
$t->tag( 'add', 'sel', $word[0] => $word[1] );
# Find a good place to put the balloon (right below the last charin
the word):
my $i = $t->index("$word[1] - 1 chars");
my @p = $t->bbox($i);
my $x = $t->rootx + $p[0] + $p[2] - 4;
my $y = $t->rooty + $p[1] + $p[3] + 2;
"$x,$y";
}
}
########################################
#######
sub motion_com {
my $x = $t->pointerx - $t->rootx;
my $y = $t->pointery - $t->rooty;
@word = ( $t->index("\@$x,$y wordstart"), $t->index("\@$x,$y
wordend") );
if ( $word[0] eq $last[0] and $word[1] eq $last[1] ) {
0;# Same word - don't cancel the balloon.
}
else {
# New word under mouse - cancel it so a new balloon will
beposted.
$t->SelectionClear;
@last = @word;
1;
}
}
########################################
##############
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|