Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

If the text widget does not have the focus, the selection tag is not visible

by balee (Novice)
on Nov 14, 2009 at 19:23 UTC ( #807173=perlquestion: print w/ replies, xml ) Need Help??
balee has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a problem with text widget: if the text widget does not have the focus, the selection tag is not visible.
That is annoying, if I use the built in Find function: founded parts are not selected. How can I change this?
use Tk; $mw = MainWindow->new; # Create necessary widgets $f = $mw->Frame->pack(-side => 'top', -fill => 'x'); $f->Entry(-textvariable => \$filename)->pack(-side => 'left', -anchor => 'w', -fill => 'x', -expand => 1); $f->Button(-text => "Exit", -command => sub { exit; } )-> pack(-side => 'right'); $t = $mw->Scrolled("Text")->pack(-side => 'bottom', -fill => 'both', -expand => 1); MainLoop;
Google found this:
http://www.tcl.tk/cgi-bin/tct/tip/26.xml
Finally, a virtual event has been added that is triggered whenever the selection in the widget changes. At first it may seem not so useful, but there are a number of situations where this functionality is needed. A couple of examples where I ran into the need for this may clarify this. On Windows, if the text widget does not have the focus, the selection tag is not visible. This is consistent with other Windows applications. However, when implementing a search mechanism, the found string needs to be tagged with the selection tag. (You want it to be selected). The search (and replace) dialog box has the focus however, so this selection tag is invisible. To make it visible, another tag was used to duplicate the selection tag. This is very easy when the functionality described here is available. Otherwise it is very difficult to do this consistently. Another occasion was when I was implementing a rectangular cut and paste for the text widget. This was based on adding spaces on the fly, while selecting the rectangle. If for some reason the selection changes (for instance on Unix another application gets the selection) these spaces need to be removed again. Doing this is virtually impossible without this functionality. With it, it becomes trivial. The functionality itself adds little or no overhead to the text widget. I do not understand this...

Comment on If the text widget does not have the focus, the selection tag is not visible
Download Code
Re: If the text widget does not have the focus, the selection tag is not visible
by Anonymous Monk on Nov 14, 2009 at 20:13 UTC
    Use strict warnings and diagnostics or die :)
    #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = MainWindow->new; my $filename; # Create necessary widgets my $f = $mw->Frame->pack( -side => 'top', -fill => 'x' ); $f->Entry( -textvariable => \$filename )->pack( -side => 'left', -anchor => 'w', -fill => 'x', -expand => 1 ); $f->Button( -text => "Exit", -command => sub { exit; } ) ->pack( -side => 'right' ); my $t = $mw->Scrolled("Text")->pack( -side => 'bottom', -fill => 'both', -expand => 1, ); $t->insert( end => join "\n", 1 .. 10 ); $t->focus; $t->bind( '<<Selection>>', sub { #~ warn @_; 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") ); } } ## end if ( my (@range) = $t->tagRanges...) return; } ); $t->bind( '<FocusOut>', sub { my ($t) = @_; #~ print $t->getSelected; #~ use Data::Dumper; print Dumper([ $t->tagConfigure('ssel') ]); #~ use Data::Dumper; print Dumper([ $t->tagNames ]); #~ use Data::Dumper; print Dumper([ $t->tagRanges('sel') ]); return; } ); MainLoop;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://807173]
Approved by zwon
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-08-27 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls