#!/usr/bin/perl -- use strict; use warnings; use Tk; Main( @ARGV ); exit( 0 ); sub Main { tkgui(0, 'who appears selected ? (with exportselection only)'); tkgui(1, 'who appears selected ? (with exportselection and callbacks)'); } sub tkgui { my ( $bind_callbacks , $message ) = @_; my $exportselection = 0; my $mw = MainWindow->new; my $filename = $message; my $f = $mw->Frame->pack( -side => 'top', -fill => 'x' ); my $e = $f->Entry( -textvariable => \$filename, -exportselection => $exportselection, )->pack( -side => 'left', -anchor => 'w', -fill => 'x', -expand => 1, ); my $t = $mw->Scrolled( "Text", -exportselection => $exportselection, )->pack( -side => 'bottom', -fill => 'both', -expand => 1, ); $t->insert( end => join "\n", qw' to hilite highlight selection without w/o focus tagAdd tagname @range tagConfigure foreground and background ', 1 .. 10, ); if( $bind_callbacks ){ $t->bind( '<>', \&on_selection ); $t->bind( '', sub { my ($t) = @_; }, ); } $mw->withdraw; $mw->deiconify; $mw->raise; $mw->focusForce; $mw->update; for( 1 .. 5 ){ $filename = "$message [I HAVE FOCUS]"; $e->selectionRange(0, length($filename) - 8 ); $e->focus ; $e->update; $mw->update; warn 'focusCurrent? ', eval { $mw->focusCurrent } || 'none'; sleep 1; $filename = "$message [I LOST FOCUS]"; $t->selectAll; $t->focus; $t->update; $mw->update; warn 'focusCurrent? ', eval { $mw->focusCurrent } || 'none'; sleep 1; } $mw->withdraw; return $mw; } sub on_selection { my( $t ) = @_; $t->tagDelete('ssel'); if ( my (@range) = $t->tagRanges('sel') ) { $t->tagAdd( ssel => @range ); for my $opt (qw' foreground background ') { $t->tagConfigure( ssel => "-$opt" => $t->cget("select$opt") ); } } return; }