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

Greetings.

Tk::Text. Is there a way to find out whether character at specified index is visible at the moment?

Thanks,
Artem A. Avetisyan.
  • Comment on Tk::Text. How to find out if char is visible?

Replies are listed 'Best First'.
Re: Tk::Text. How to find out if char is visible?
by liverpole (Monsignor) on Jan 23, 2007 at 18:45 UTC
    Hi artemave,

    zentara is correct; you could easily do this by using the bbox method of a Tk::Text object.

    Here's an example program:

    #!/usr/bin/perl -w + use strict; use warnings; use Tk; + # Globals my @filler = qw( just a bunch of filler words ... ); + # Main program my $mw = new MainWindow(-title => 'Detect_if_text_visible'); my @opts = (-bg => 'peachpuff', -width => 32, -height => 16); push @opts, -wrap => "none"; my $txt = $mw->Scrolled('Text', @opts, -scrollbars => "osoe"); $txt->tagConfigure('white', -background => 'white'); $txt->tagConfigure('target', -background => 'red'); $txt->pack(-expand => 1, -fill => 'both'); insert_filler(20); $txt->insert("end", 'TARGET is to the right ==============> ', 'white' +); $txt->insert("end", "X", 'target'); $txt->insert("end", ' <=============== TARGET is to the left', 'white' +); $txt->insert("end", "\n"); insert_filler(20); $mw->repeat(1000 => \&idle_loop); $mw->bind("<Escape>" => sub { $mw->destroy() }); MainLoop; + + # Subroutines sub insert_filler { my ($nlines) = @_; for (my $i = 0; $i < $nlines; $i++) { my $line = (join(" ", @filler) . " ") x 4; my $word = shift @filler; push @filler, $word; $txt->insert("end", "$line\n"); } } + + sub idle_loop { my $index = $txt->index("target.first"); my $bbox = $txt->bbox($index); if ($bbox) { print "Index '$index' is VISIBLE\n"; } else { print "Index '$index' NOT visible\n"; } }

    It creates a Tk::Text window filled with a bunch of lines.  The middle line is highlighted in white to make it easier to see, and approximately in the middle of the line is a red 'X'.

    When you first run the program, the 'X' is NOT visible, but you can scroll in either direction to make the 'X' come into view.

    Every second the idle_loop is called, which will report whether the 'X' is visible or not, depending on the results of bbox.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Tk::Text. How to find out if char is visible?
by zentara (Cardinal) on Jan 23, 2007 at 17:15 UTC
    I don't have an example handy, but you could try a few things. One is to get the bbox of the index in question, and see if it's a non-zero area? Or maybe tag the item you are looking for, then get the positions for those tags?

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Tk::Text. How to find out if char is visible?
by Melly (Chaplain) on Jan 23, 2007 at 15:51 UTC

    I can't really help in any event, but can you clarify - "visible" as in "a different colour than the background" or "visible" as in "on screen"?

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk