Re: Using Tk::Text and '<<Modified>>'
by zentara (Cardinal) on Nov 20, 2004 at 14:01 UTC
|
Yeah, you need to reset the $text->editModified(0) flag. The "gotcha" comes when you try to reset the flag automatically at the end of the <<Modified>> callback. You will get an infinite loop. So you need to manually reset it somehow, maybe with a timer?
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::Text;
my $MW = MainWindow->new(
-title => "Tk::Text test",
-width => 200,
-height => 200
);
my $text = $MW->Text(
-height => 10,
-width => 40,
-wrap => 'word'
);
$text->pack( -side => 'top', -fill => 'both' );
$text->bind( '<<Modified>>' => sub {
getText( $text, @_ );
} );
$MW->Button(-text=>'Clear Modified Flag',
-command => sub{ $text->editModified(0) }
)->pack();
MainLoop;
sub getText {
my ( $t, @args ) = @_;
my $text = $t->get( '0.0', 'end' );
print "Got: $text\n";
return if ( !$text );
return 1;
}
I'm not really a human, but I play one on earth.
flash japh
| [reply] [d/l] |
|
|
my $allowed=1;
$text->bind('<<Modified>>' =>
sub {
getText($text, @_);
do {$allowed=0;$text->editModified(0);$allowed=1} if $
+allowed;
} );
unfortunately this is not simple, so not very Perlish.
addition: surprisingly, afterIdle(...) do not resolve that, and its usage restores initial problem...
Best regards,
Courage, the Cowardly Dog
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
"a consistent interface for all the Tk:: modules."
What is inconsistent here? I agree that it would be much better if they can add some examples here and there in Tk documents, so that we can reach the proper use more quickly. The entire Tk document is long but poor.
But your issue really does not point me to anything that is "inconsistent", but rather some sort of learning curve.
If you want me to point out the inconsistancy, I would say that they better support editModified() in widgets that are really similar to Tk::Text, for example Tk::Entry, but it does not.
use Tk;
use Tk::Dialog;
use warnings;
use strict;
my $MW = MainWindow->new(-title => "Tk::Text test",
-width => 200,
-height => 200);
my $text = $MW->Entry(
-width => 40);
$text->pack(-side => 'top', -fill => 'both');
$text->bind( '<FocusOut>' => \&callback);
my $text2 = $MW->Text(-height => 10,
-width => 40,
-wrap => 'word');
$text2->pack(-side => 'top', -fill => 'both');
MainLoop;
sub callback {
if ($text->editModified()) {
$text->Dialog(-title=>"Modified",-text=>"\$text has been modif
+ied")->Show();#or whatever you want
$text->editModified(0);
}
}
| [reply] [d/l] |
|
|
Re: Using Tk::Text and '<<Modified>>'
by rrwo (Friar) on Nov 20, 2004 at 13:18 UTC
|
If you want your routine to be called with every keystroke, then bind every keystroke.
Your callback was called once when it was modified. Since you now know it's been modified, there's no reason to call it again.
I don't have the Tk docs with me here, but I recall that you have to reset some kind of flag when modified has been called.
| [reply] |
|
|
use warnings;
use strict;
use Tk;
my $MW = MainWindow->new(-title => "Tk::Text test",
-width => 200,
-height => 200);
my $text = $MW->Text(-height => 10,
-width => 40,
-wrap => 'word');
$text->pack(-side => 'top', -fill => 'both');
$text->bind('<<Modified>>' =>
sub { getText($text, @_) } );
MainLoop;
sub getText
{
my ($t, @args) = @_;
my $str = $t->get('0.0', 'end');
print "Got: $str\n";
$text->bind('<<Modified>>' =>
sub { getText($str, @_) } );
return if(!$str);
return 1;
}
| [reply] [d/l] |
Re: Using Tk::Text and '<<Modified>>'
by zentara (Cardinal) on Nov 20, 2004 at 19:31 UTC
|
Like I said in my first reply, I like to check md5sums.
If you want to only call a save routine, if a modification is made, you have those 2 options. One, if the editModified flag is 1, call a save routine upon exiting. Or you can do a get('0.0','end') on loading the file, and do an md5sum on the string and store it away. Then have something like this in you code:
$SIG{__DIE__} = sub {&save_it(); exit};
$SIG{INT} = sub {&save_it(); exit};
#have your exit button call &save_it too
sub save_it{
# do another get('0.0','end')
# take an md5sum of the text and compare
# it to your original md5sum
# save your text if they differ
}
Otherwise you will be auto-saving on every keystroke. You can also work out an auto-save scheme if the text box goes out of focus.
I'm not really a human, but I play one on earth.
flash japh
| [reply] [d/l] |
Re: Using Tk::Text and '<<Modified>>'
by zentara (Cardinal) on Nov 20, 2004 at 10:58 UTC
|
| [reply] |
|
|
use Tk;
use Tk::Dialog;
use warnings;
use strict;
my $MW = MainWindow->new(-title => "Tk::Text test",
-width => 200,
-height => 200);
my $text = $MW->Text(-height => 10,
-width => 40,
-wrap => 'word');
$text->pack(-side => 'top', -fill => 'both');
$text->bind( '<FocusOut>' => \&callback);
my $text2 = $MW->Text(-height => 10,
-width => 40,
-wrap => 'word');
$text2->pack(-side => 'top', -fill => 'both');
MainLoop;
sub callback {
if ($text->editModified()) {
$text->Dialog(-title=>"Modified",-text=>"\$text has been modif
+ied")->Show();#or whatever you want
$text->editModified(0);
}
}
| [reply] [d/l] |