| 1 | package Tk::FilterEntry; | ||
| 2 | |||
| 3 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | $VERSION = '0.02'; | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Tk; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Tk::Entry; | ||
| 9 | |||
| 10 | use base qw(Tk::Derived Tk::Entry); | ||
| 11 | |||
| 12 | Construct Tk::Widget 'FilterEntry'; | ||
| 13 | |||
| 14 | sub ClassInit { | ||
| 15 | my ($class, $mw) = @_; | ||
| 16 | |||
| 17 | $class->SUPER::ClassInit($mw); | ||
| 18 | |||
| 19 | $mw->bind($class, '<FocusIn>' => \&FocusIn); | ||
| 20 | $mw->bind($class, '<FocusOut>' => \&FocusOut); | ||
| 21 | $mw->bind($class, '<Visibility>' => \&FocusOut); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub Populate { | ||
| 25 | my ($self, $args) = @_; | ||
| 26 | |||
| 27 | $self->SUPER::Populate($args); | ||
| 28 | |||
| 29 | $self->ConfigSpecs( | ||
| 30 | -background => ['PASSIVE', 'background', undef, 'white'], | ||
| 31 | -fg_valid => ['PASSIVE', 'fg_valid', undef, 'black'], | ||
| 32 | -fg_invalid => ['PASSIVE', 'fg_invalid', undef, 'red'], | ||
| 33 | -filter => ['PASSIVE', 'filter', undef, '.*'], | ||
| 34 | -anchors => ['PASSIVE', 'anchors', undef, 1], | ||
| 35 | -trim => ['PASSIVE', 'trim', undef, 1], | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub FocusIn { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | my $fg = $self->cget(-fg_valid); | ||
| 43 | $self->Tk::Entry::configure(-foreground => $fg); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub FocusOut { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | my $filter = $self->cget(-filter); | ||
| 50 | my $trim = $self->cget(-trim); | ||
| 51 | my $anchors = $self->cget(-anchors); | ||
| 52 | $filter = '\s*' . $filter . '\s*' if ($trim); | ||
| 53 | $filter = '^' . $filter . '$' if ($anchors); | ||
| 54 | my $value = $self->get(); | ||
| 55 | my $r_vcmd = $self->cget(-validatecommand); | ||
| 56 | my (@vcmd, $vcmd); | ||
| 57 | if (defined $r_vcmd) { | ||
| 58 | @vcmd = @{$r_vcmd}; | ||
| 59 | $vcmd = shift @vcmd; | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( ( defined $vcmd and ! &{$vcmd}($self, @vcmd) ) | ||
| 63 | or ( defined $filter and ( $value !~ /$filter/ ) ) ) { | ||
| 64 | my $fg = $self->cget(-fg_invalid); | ||
| 65 | $self->Tk::Entry::configure(-foreground => $fg); | ||
| 66 | my $r_invcmd = $self->cget(-invalidcommand); | ||
| 67 | if (defined $r_invcmd) { | ||
| 68 | my @invcmd = @{$r_invcmd}; | ||
| 69 | my $invcmd = shift @invcmd; | ||
| 70 | &{$invcmd}($self, @invcmd); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub validate { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | my $r_vcmd = $self->cget(-validatecommand); | ||
| 79 | if (defined $r_vcmd) { | ||
| 80 | my @vcmd = @{$r_vcmd}; | ||
| 81 | my $vcmd = shift @vcmd; | ||
| 82 | return &{$vcmd}($self, @vcmd); | ||
| 83 | } | ||
| 84 | |||
| 85 | my $filter = $self->cget(-filter); | ||
| 86 | my $trim = $self->cget(-trim); | ||
| 87 | my $anchors = $self->cget(-anchors); | ||
| 88 | $filter = '\s*' . $filter . '\s*' if ($trim); | ||
| 89 | $filter = '^' . $filter . '$' if ($anchors); | ||
| 90 | my $value = $self->get(); | ||
| 91 | return ( $value =~ /$filter/ ) if (defined $filter); | ||
| 92 | |||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | __END__ | ||