File: NumEntryPlain.pm

package info (click to toggle)
libtk-gbarr-perl 2.07-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 212 kB
  • ctags: 199
  • sloc: perl: 1,875; makefile: 48
file content (307 lines) | stat: -rw-r--r-- 6,701 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# $Id: NumEntryPlain.pm,v 1.10 2003/10/22 21:32:59 eserte Exp $

package Tk::NumEntryPlain;

use Tk ();
use Tk::Derived;
use Tk::Entry;
use strict;

use vars qw(@ISA $VERSION);
@ISA = qw(Tk::Derived Tk::Entry);
$VERSION = sprintf("%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/);

Construct Tk::Widget 'NumEntryPlain';

sub ClassInit {
    my ($class,$mw) = @_;

    $class->SUPER::ClassInit($mw);

    $mw->bind($class,'<Leave>', 'Leave');
    $mw->bind($class,'<FocusOut>', 'Leave');
    $mw->bind($class,'<Return>', 'Return');
    $mw->bind($class,'<Up>', 'Up');
    $mw->bind($class,'<Down>', 'Down');
    $mw->bind($class,'<Home>', 'Home');
    $mw->bind($class,'<End>', 'End');
    $mw->bind($class,'<Prior>', 'Prior');
    $mw->bind($class,'<Next>', 'Next');
}


## Bindings callbacks

sub Leave {
    my $e = shift;
    $e->incdec(0);  # range check
}

sub Return {
    my $e = shift;

    my $v = $e->value; # range check

    $e->Callback(-command => $v);
}

sub Up {
    my $e = shift;
    $e->incdec($e->cget(-increment));
}

sub Down {
    my $e = shift;
    $e->incdec(-$e->cget(-increment));
}

sub Prior {
    my $e = shift;
    $e->incdec($e->cget(-bigincrement) || 1);
}

sub Next {
    my $e = shift;
    $e->incdec(-($e->cget(-bigincrement) || 1));
}

sub Insert {
    my($e,$c) = @_;

    my $dot = ($e->cget(-increment) =~ /\./ ? '.' : '');

    if($c =~ /^[-0-9$dot]$/) {
	$e->SUPER::Insert($c);
    }
    elsif(defined($c) && length($c)) {
	$e->_ringBell;
    }
}

sub Home {
    my $e = shift;
    my $min_val = $e->cget(-minvalue);
    return unless defined $min_val;
    $e->value($min_val);
}

sub End {
    my $e = shift;
    my $max_val = $e->cget(-maxvalue);
    return unless defined $max_val;
    $e->value($max_val);
}

## Widget constructor

sub Populate {
    my ($e, $args) = @_;

#    $e->SUPER::Populate($args);


    $e->ConfigSpecs(
        -value       => [METHOD   => undef,         undef,         "0"       ],
        -defaultvalue
		     => [PASSIVE  => undef,         undef,         undef     ],
        -maxvalue    => [PASSIVE  => undef,         undef,         undef     ],
        -minvalue    => [PASSIVE  => undef,         undef,         undef     ],
        -bell        => [PASSIVE  => "bell",        "Bell",        1         ],
        -command     => [CALLBACK => undef,         undef,         undef     ],
	-browsecmd   => [CALLBACK => undef,         undef,         undef     ],
        -increment    => [PASSIVE => undef,         undef,         1         ],
        -bigincrement => [PASSIVE => undef,         undef,         undef     ],
        ($Tk::platform eq 'MSWin32' ?
          (-background => ['PASSIVE', qw/background Background/, undef]) :
          ()),
        DEFAULT      => [$e],
    );
}

## Options implementation

sub value {
    my $e = shift;
    my $old;

    if(@_) {
        my $new = 0 + shift;
        my $pos = $e->index('insert');

        $old = $e->get;

        $e->delete(0,'end');
        $e->insert(0,$new);
        $e->icursor($pos);
    }
    else {
        $e->incdec(0); # range check
        $old = $e->get;
    }

    # Do a range check after all configuration has finished,
    # as we may not yet know the range

    $e->afterIdle([ $e => 'incdec', 0]);

    length($old) ? $old + 0 : $e->{Configure}{'-defaultvalue'};
}

sub _ringBell {
    my $e = shift;
    my $v;
    return
        unless defined($v = $e->{Configure}{'-bell'});
    $e->bell
        if(($v =~ /^\d+$/ && $v) || $v =~ /^true$/i);
}


sub incdec {
    my($e,$inc) = @_;
    my $val = $e->get;

    if($inc == 0 && $val =~ /^-?$/) {
        $val = "";
    }
    else {
        my $min = $e->{Configure}{'-minvalue'};
        my $max = $e->{Configure}{'-maxvalue'};

	$val = 0 if !$val;
        $val += $inc;
        my $limit = undef;

        $limit = $val = $min
            if(defined($min) && $val < $min);

        $limit = $val = $max
            if(defined($max) && $val > $max);

        if(defined $limit) {
            $e->_ringBell
                if $inc;
        }
    }

    my $pos = $e->index('insert');
    $e->delete(0,'end');
    $e->insert(0,$val);
    $e->icursor($pos);
    $e->Callback(-browsecmd) if $inc;
}


1;

__END__

=head1 NAME

Tk::NumEntryPlain - A numeric entry widget

=head1 SYNOPSIS

S<    >B<use Tk::NumEntryPlain>;

=head1 DESCRIPTION

B<Tk::NumEntryPlain> defines a widget for entering integer numbers.

B<Tk::NumEntryPlain> supports all the options and methods that a normal
L<Entry|Tk::Entry> widget provides, plus the following options

=head1 STANDARD OPTIONS

B<-repeatdelay>
B<-repeatinterval>

=head1 WIDGET-SPECIFIC OPTIONS

=over 4

=item -minvalue

Defines the minimum legal value that the widget can hold. If this
value is C<undef> then there is no minimum value (default = undef).

=item -maxvalue

Defines the maximum legal value that the widget can hold. If this
value is C<undef> then there is no maximum value (default = undef).

=item -bell

Specifies a boolean value. If true then a bell will ring if the user
attempts to enter an illegal character into the entry widget, and
when the user reaches the upper or lower limits when using the
up/down buttons for keys (default = true).

=item -textvariable

Reference to a scalar variable that contains the value currently
in the B<NumEntry>.  Use the variable only for reading (see
L<"CAVEATS"> below).

=item -value

Specifies the value to be inserted into the entry widget. Similar
to the standard B<-text> option, but will perform a range
check on the value.

=item -command

A callback which is called if <Return> is pressed.

=item -browsecmd

A callback which is called every time an increment or decrement
happens in the entry widget.

=back

=head1 WIDGET METHODS

=over 4

=item I<$numentry>->B<incdec>(I<increment>)

Increment the value of the entry widget by the specified increment. If
increment is 0, then perform a range check.

=back

=head1 CAVEATS

=over 4

=item -textvariable

B<-textvariable> should only be used to read out the current
value in the B<NumEntry>.

Values set via B<-textvariable> are not valided. Therefore
it's possible to insert, e.g., 'abc', into the B<NumEntry>.

=back

=head1 SEE ALSO

L<Tk::NumEntry|Tk::NumEntry>
L<Tk::Entry|Tk::Entry>

=head1 HISTORY

The code was extracted from B<Tk::NumEntry> and slightly modified
by Achim Bohnet E<lt>ach@mpe.mpg.deE<gt>.  B<Tk::NumEntry>'s author
is Graham Barr E<lt>gbarr@pobox.comE<gt>.

Current maintainer is Slaven Rezic E<lt>slaven@rezic.deE<gt>.

=head1 COPYRIGHT

Copyright (c) 1997-1998 Graham Barr. All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut