in reply to Tk::Text. How to find out if char is visible?
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.
|
|---|