f



Text bind command not selecting text

I am putting together a small report editor and need some help with
the following problem. I would like the user to type in some text and
then later be able to select either all of it or a portion of it for
formatting (ie. centering, bold, italic,etc.) by typing in a command
like Ctrl-A. The following code is what I came up for this process but
for some reason the text does not get selected. Can someone tell why
this code doesn't work?


################################################################
#

use strict ;
use Tk ;
use Tk::NoteBook ;


my $fTab ;
my $mw = MainWindow->new;
$fTab  = $mw->Frame()->pack( -anchor => 'n', -fill => 'both', -expand
=> 1 ) ;

my $nb  = $fTab->NoteBook()->pack( -anchor => 'n', -fill=>'both', -
expand=>1 );
my $rpt = $nb->add("Report", -label => "Report");
my $hdr = $nb->add("Header", -label => "Header" );
my $tle = $nb->add("Title",  -label => "Title");
my $dtl = $nb->add("Detail", -label => "Detail");
my $ftr = $nb->add("Footer", -label => "Footer");
my $fin = $nb->add("Final",  -label => "Final");

my $rpt_txt = $rpt->Text( -width => 80
                        , -height => 20
                        , -state => 'disable'
                        , -relief => 'flat'
                        )->pack( -anchor => 'n' ) ;

my $hdr_txt = $hdr->Text( -width => 80
                        , -height => 6
                        )->pack( -anchor => 'n' ) ;
$hdr_txt->tagConfigure( 'center', justify => 'center', -foreground =>
'red' ) ;
$hdr_txt->bind( '<Control-Key-a>', sub { print "In sub\n" ;
                                         my $t = shift ;
                                         $t->tagAdd( 'center',
'sel.first', 'sel.last') if( $t->tagRanges( 'sel' ) ) ;
                                       } ) ;

my $tle_txt = $tle->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;
my $dtl_txt = $dtl->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;
my $ftr_txt = $ftr->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;
my $fin_txt = $fin->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;



MainLoop;

0
sajohn
7/10/2007 12:46:32 AM
comp.lang.perl.tk 4721 articles. 0 followers. pharrendorf (19) is leader. Post Follow

2 Replies
1008 Views

Similar Articles

[PageSpeed] 53

"sajohn" <sajohn52@yahoo.com> wrote in message 
news:1184028392.176358.236900@q75g2000hsh.googlegroups.com...
>I am putting together a small report editor and need some help with
> the following problem. I would like the user to type in some text and
> then later be able to select either all of it or a portion of it for
> formatting (ie. centering, bold, italic,etc.) by typing in a command
> like Ctrl-A. The following code is what I came up for this process but
> for some reason the text does not get selected. Can someone tell why
> this code doesn't work?
>
>
> ################################################################
> #
>
> use strict ;
> use Tk ;
> use Tk::NoteBook ;
>
>
> my $fTab ;
> my $mw = MainWindow->new;
> $fTab  = $mw->Frame()->pack( -anchor => 'n', -fill => 'both', -expand
> => 1 ) ;
>
> my $nb  = $fTab->NoteBook()->pack( -anchor => 'n', -fill=>'both', -
> expand=>1 );
> my $rpt = $nb->add("Report", -label => "Report");
> my $hdr = $nb->add("Header", -label => "Header" );
> my $tle = $nb->add("Title",  -label => "Title");
> my $dtl = $nb->add("Detail", -label => "Detail");
> my $ftr = $nb->add("Footer", -label => "Footer");
> my $fin = $nb->add("Final",  -label => "Final");
>
> my $rpt_txt = $rpt->Text( -width => 80
>                        , -height => 20
>                        , -state => 'disable'
>                        , -relief => 'flat'
>                        )->pack( -anchor => 'n' ) ;
>
> my $hdr_txt = $hdr->Text( -width => 80
>                        , -height => 6
>                        )->pack( -anchor => 'n' ) ;
> $hdr_txt->tagConfigure( 'center', justify => 'center', -foreground =>
> 'red' ) ;
> $hdr_txt->bind( '<Control-Key-a>', sub { print "In sub\n" ;
>                                         my $t = shift ;
>                                         $t->tagAdd( 'center',

#Make sure you bind to a cApitAl A too...
$hdr_txt->bind( '<Control-Key-a>', [$hdr_txt => 'selectAll']);
$hdr_txt->bind( '<Control-Key-A>', [$hdr_txt => 'selectAll']);



> 'sel.first', 'sel.last') if( $t->tagRanges( 'sel' ) ) ;
>                                       } ) ;
>
> my $tle_txt = $tle->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
> my $dtl_txt = $dtl->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
> my $ftr_txt = $ftr->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
> my $fin_txt = $fin->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
>
>
>
> MainLoop;
>

Jack 


0
Jack
7/11/2007 2:49:46 AM
sajohn wrote:
> I am putting together a small report editor and need some help with
> the following problem. I would like the user to type in some text and
> then later be able to select either all of it or a portion of it for
> formatting (ie. centering, bold, italic,etc.) by typing in a command
> like Ctrl-A. The following code is what I came up for this process but
> for some reason the text does not get selected. Can someone tell why
> this code doesn't work?
> 

Hello,

the Ctrl-A event is processed by a Class binding first. Therefore your 'sel' tag 
does no longer exist at the time your instance-binding is invoked.
So you have to bind to the Text class, change the bindtags order or derive a 
Class from Tk::Text which provides the behaviour that you need.

The first approach is the simplest.
The drawback here is, that all Text widgets in your application will respond to 
the binding:

use warnings;
use strict;
use Tk;



my $fTab ;
my $mw = MainWindow->new;
$fTab  = $mw->Frame()->pack( -anchor => 'n',
                              -fill   => 'both',
                              -expand => 1 ) ;



my $hdr_txt = $fTab->Text( -width => 80,
                            -height => 6
                         )->pack( -anchor => 'n' ) ;
$hdr_txt->tagConfigure( 'center',
                         -justify    => 'center',
                         -foreground => 'red' ) ;
$mw->bind( 'Tk::Text','<Control-Key-a>',\&event_);

MainLoop;

sub event_{
     my $t = shift ;
     $t->tagAdd( 'center',
                 'sel.first',
                 'sel.last') if( $t->tagRanges( 'sel' ) ) ;
}


-- 
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
0
Ch
7/11/2007 8:45:10 PM
Reply: