blib/lib/Tk/TipEntry.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Tk::TipEntry;
2
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Tk;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Tk::Entry;
8
9 our $VERSION = '0.06';
10
11 use base qw(Tk::Derived Tk::Entry);
12
13 Construct Tk::Widget 'TipEntry';
14
15 =head1 NAME
16
17 Tk::TipEntry - An entry with tooltip in the entry if it's empty
18
19 =head1 SYNOPSIS
20
21 use strict;
22 use Tk::TipEntry;
23
24 my $entry = $parent->FilterEntry(
25 -tip => 'Search...', # will be the default hint text when entry is empty
26 );
27 $entry->pack();
28
29 =head1 DESCRIPTION
30
31 This widget is derived from L<Tk::Entry>. It implements an other kind of
32 tooltip, that is displayed inside the entry when it's empty.
33 The tooltip will be removed, if the entry gets the focus and reinserted, if
34 the entry loses the focus and it's value is empty (C<$entry-E<gt>get() eq ''>).
35
36 In addition, the entry evaluates the escape key. If the entry has the focus
37 and the escape key is pressed, the original input will be restored. If there
38 is no previous input, the tooltip will be displayed again.
39
40
41 =head1 OPTIONS
42
43 Any option exept the C<-tip> will be passed to the construktor of the
44 L<Tk::Entry>. The -text option is altered minimally.
45
46
47 =head2 -tip
48 (new option)
49
50 Specify the tooltip, that will be displayed.
51
52 The default value is 'Search...'.
53
54
55 =head2 -text
56 (altered option)
57
58 If there is no C<-text> attribute for the Entry, the tooltip will be set initially
59 as default text. Specify C<-text> if you want another initial input.
60
61 The default value is the same as for -tip.
62
63
64 =head1 METHODS
65
66 =cut
67
68 # ClassInit( $class, $mw )
69 #
70 # Bind FocusIn, FocusOut and Escape to events.
71
72 sub ClassInit {
73 my ($class, $mw) = @_;
74
75 $class->SUPER::ClassInit($mw);
76
77 $mw->bind($class, '<FocusIn>' => \&FocusIn);
78 $mw->bind($class, '<FocusOut>' => \&FocusOut);
79 $mw->bind($class, '<Escape>' => \&Escape);
80 } # /ClassInit
81
82
83
84
85 # Populate( %args )
86 #
87 # Sets default for -tip, unless specified. Set -text initially to -tip,
88 # if there is -tip, but no -text.
89
90 sub Populate {
91 my ($self, $args) = @_;
92
93 # -- check for undef tip value
94 my $default_tip = 'Search...';
95 if( !exists($args->{-tip}) or ( exists($args->{-tip}) and !defined($args->{-tip})) ) {
96 $args->{-tip} = $default_tip;
97 }
98
99 unless( exists $args->{-text} ) {
100 $args->{-text} = $args->{-tip};
101 }
102
103 $self->SUPER::Populate($args);
104
105 $self->ConfigSpecs(
106 -tip => ['PASSIVE', 'tip', 'Tip', $default_tip],
107 -previous => ['PASSIVE', 'previous', undef, undef],
108 );
109 } # /Populate
110
111
112
113
114 =head2 FocusIn()
115
116 When the entry gets the focus, the tooltip will be removed.
117
118 =cut
119
120 sub FocusIn {
121 my $self = shift;
122
123 my $default_text = $self->cget('-tip');
124
125 if( $self->get() eq $default_text ) {
126 $self->delete(0, 'end');
127 $self->configure(-previous => undef);
128 }else{
129 $self->configure(-previous => $self->get());
130 }
131
132 return 1;
133 } # /FocusIn
134
135
136
137
138 =head2 FocusOut()
139
140 When the entry loses the focus and if it's empty, the tooltip will be inserted.
141
142 =cut
143
144 sub FocusOut {
145 my $self = shift;
146
147 my $default_text = $self->cget('-tip');
148
149 if( $self->get() eq '' ) {
150 $self->insert(0, $default_text);
151 $self->configure(-previous => undef);
152 }elsif( $self->get() eq $default_text ) {
153 $self->configure(-previous => undef);
154 }else{
155 $self->configure(-previous => $self->get());
156 }
157
158 return 1;
159 } # /FocusOut
160
161
162
163
164 =head2 Escape()
165
166 If the escape key is pressed, the current input will be discarded. The
167 previous input will be inserted. If there is no previous input, the -tip will
168 be the new input.
169
170 =cut
171
172 sub Escape {
173 my $self = shift;
174
175 my $default_text = $self->cget('-tip');
176 my $previous_input = $self->cget('-previous');
177
178 if( defined($previous_input) and $self->get() ne $previous_input ) {
179 $self->delete(0, 'end');
180 $self->insert(0, $previous_input);
181 }
182
183 $self->parent->focus();
184
185 return 1;
186 } # /Escape
187
188
189
190
191 =head1 SEE ALSO
192
193 L<Tk>, L<Tk::Entry>, L<Tk::FilterEntry>
194
195 =head1 CREDITS
196
197 POD for C<Populate()> partially taken from L<Tk::Wizard>.
198
199 =head1 LICENSE
200
201 This library is free software; you can redistribute it and/or modify it under
202 the same terms as Perl itself, either Perl version 5.12.2 or, at your option,
203 any later version of Perl 5 you may have available.
204
205 =head1 AUTHOR
206
207 This module was designed after L<Tk::FilterEntry>.
208
209 Alexander Becker, L<asb@cpan.org>
210
211 =cut
212
213 1; # /Tk::TipEntry
↑ Summary