Yes, a module would be the way to go. You could go writing a generic Tk::History package, but if you want to deal just with entries, this is what a derived HistEntry package looks like, based on your post (and Tk::LabEntry):
package Tk::HistEntry; use vars qw($VERSION); $VERSION = '0.001'; use base qw(Tk::Frame); use Tk::widgets qw(Frame Entry); Construct Tk::Widget 'HistEntry'; sub Populate { require Tk::Entry; # HistEntry constructor. # my($cw, $args) = @_; $cw->SUPER::Populate($args); # Advertised subwidgets: entry. my $e = $cw->Entry(); $e->pack('-expand' => 1, '-fill' => 'both'); $cw->Advertise('entry' => $e ); $cw->ConfigSpecs(DEFAULT => [$e]); $cw->Delegates(DEFAULT => $e); $cw->AddScrollbars($e) if (exists $args->{-scrollbars}); $e->{history} = { hlist => [], place => 0, }; $e->bind("<Key-Return>"=> [ \&addHistory ]); $e->bind("<Key-Up>" => [ \&upHistory ]); $e->bind("<Key-Down>" => [ \&downHistory ]); } sub addHistory { my $entry = shift; my $hist = $entry->{history}; push @{$hist->{hlist}}, $entry->get() ; $hist->{place}++; } sub upHistory { my $entry = shift; my $hist = $entry->{history}; if ($hist->{place} <= 0) { $hist->{place} = $#{$hist->{hlist}} } else { $hist->{place}--; } $entry->delete('0','end'); $entry->insert('1',$hist->{hlist}[$hist->{place}]); } sub downHistory { my $entry = shift; my $hist = $entry->{history}; $entry->delete('0','end'); if ($hist->{place} == $#{$hist->{hlist}}) { $hist->{place} = -1; return } $hist->{place}++; $entry->insert('1',$hist->{hlist}[$hist->{place}]); } 1;
In your Code, you can now use HistEntry instead of Entry.
In reply to Re: What to do with my "Tk entry command-history" function
by shmem
in thread What to do with my "Tk entry command-history" function
by halfcountplus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |