ListBrowser/FilterEntry.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 120 CC 10 SCAR 16.7
CC
10
Cov
0%
KeyRelease
12.0
activate
6.0
Button1
6.0
CRAP 5.3
1 package Tk::ListBrowser::FilterEntry;
2  
3 use strict;
4 use warnings;
5 use vars qw ($VERSION);
6 $VERSION = 0.11;
7  
8 use base qw(Tk::Derived Tk::Entry);
9  
10 Construct Tk::Widget 'FilterEntry';
11 use Tk;
12  
13 sub Populate {
14 my ($self,$args) = @_;
15 $self->SUPER::Populate($args);
16 $self->{FILTERINIT} = 1;
17 $self->bind('<Button-1>', [$self, 'Button1']);
18 $self->bind('<KeyRelease>', [$self, 'KeyRelease', Ev('A')]);
19 $self->ConfigSpecs(
20 -command => ['CALLBACK'],
21 -filterdelay => ['PASSIVE', undef, undef, 300],
22 -initforeground => ['PASSIVE', undef, undef, '#808080'],
23 DEFAULT => [$self],
24 );
25 $self->after(1, sub {
26 $self->{'fg_sav'} = $self->cget('-foreground');
27 $self->reset;
28 });
29 }
30  
31 sub activate {
32 my $self = shift;
33 my $filter_id = $self->{'filter_id'};
34 if (defined $filter_id) {
35 $self->afterCancel($filter_id);
36 }
37 $filter_id = $self->after($self->cget('-filterdelay'), ['filter', $self]);
38 $self->{'filter_id'} = $filter_id;
39 }
40  
41 sub Button1 {
42 my $self = shift;
43 $self->icursor(0) unless $self->filterinit;
44 $self->focus;
45 }
46  
47 sub filter {
48 my $self= shift;
49 $self->Callback('-command')
50 }
51  
52 sub filterinit { return $_[0]->{FILTERINIT} };
53  
54 sub get {
55 my $self = shift;
56 return '' if $self->filterinit;
57 return $self->SUPER::get(@_);
58 }
59  
60 sub initialize {
61 my $self = shift;
62 return unless $self->filterinit;
63 $self->delete(0, 'end');
64 my $fg = $self->{'fg_sav'};
65 $self->configure(-foreground => $fg);
66 $self->{FILTERINIT} = 0;
67 }
68  
69 sub Delete {
70 my $self = shift;
71 $self->initialize if $self->filterinit;
72 $self->SUPER::Delete(@_);
73 }
74  
75 sub Backspace {
76 my $self = shift;
77 $self->initialize if $self->filterinit;
78 $self->SUPER::Backspace(@_);
79 }
80  
81 sub Insert {
82 my $self = shift;
83 $self->initialize if $self->filterinit;
84 $self->SUPER::Insert(@_);
85 }
86  
87 sub KeyRelease {
88 my ($self, $key) = @_;
89 if ($self->filterinit) {
90 $self->icursor(0);
91 } else {
92 $self->activate if $key ne ''
93 }
94 }
95  
96 sub reset {
97 my $self = shift;
98 $self->delete(0, 'end');
99 $self->insert('end', 'Filter');
100 $self->icursor(0);
101 $self->configure(-foreground => $self->cget('-initforeground'));
102 $self->{FILTERINIT} = 1;
103 }
104  
105 1;