Debian Codegraph @

Tk/ProgressBar.pm

raw
1 package Tk::ProgressBar;
2
3 use vars qw($VERSION);
4 $VERSION = '4.015'; # was: sprintf '4.%03d', q$Revision: #10 $ =~ /\D(\d+)\s*$/;
5
6 use Tk;
7 use Tk::Canvas;
8 use Tk::Trace;
9 use Carp;
10 use strict;
11
12 use base qw(Tk::Derived Tk::Canvas);
13
14 Construct Tk::Widget 'ProgressBar';
15
16 sub ClassInit {
17 my ($class,$mw) = @_;
18
19 $class->SUPER::ClassInit($mw);
20
21 $mw->bind($class,'<Configure>', ['_layoutRequest',1]);
22 }
23
24 my $LAYOUT_REQUEST_NONE = 0;
25 my $LAYOUT_REQUEST_COLORS = 1;
26 my $LAYOUT_REQUEST_VALUE = 2;
27 my $LAYOUT_REQUEST_GEOM = 4;
28
29 sub Populate {
30 my($c,$args) = @_;
31
32 $c->ConfigSpecs(
33 -width => [PASSIVE => undef, undef, 0],
34 '-length' => [PASSIVE => undef, undef, 0],
35 -from => [PASSIVE => undef, undef, 0],
36 -to => [PASSIVE => undef, undef, 100],
37 -blocks => [PASSIVE => undef, undef, 10],
38 -padx => [PASSIVE => 'padX', 'Pad', 0],
39 -pady => [PASSIVE => 'padY', 'Pad', 0],
40 -gap => [PASSIVE => undef, undef, 1],
41 -colors => [PASSIVE => undef, undef, undef],
42 -relief => [SELF => 'relief', 'Relief', 'sunken'],
43 -value => [METHOD => undef, undef, undef],
44 -variable => [METHOD => undef, undef, undef],
45 -anchor => [METHOD => 'anchor', 'Anchor', 'w'],
46 -resolution
47 => [PASSIVE => undef, undef, 1.0],
48 -highlightthickness
49 => [SELF => 'highlightThickness','HighlightThickness',0],
50 -troughcolor
51 => [PASSIVE => 'troughColor', 'Background', 'grey55'],
52 );
53 $c->_layoutRequest($LAYOUT_REQUEST_COLORS);
54 $c->OnDestroy(['Destroyed' => $c]);
55 }
56
57 sub ConfigChanged {
58 my($c, $changed) = @_;
59 for my $k (qw(from to blocks padx pady gap colors resolution troughcolor)) {
60 if (exists $changed->{"-$k"}) {
61 $c->_layoutRequest($LAYOUT_REQUEST_COLORS);
62 last;
63 }
64 }
65 for my $k (qw(borderwidth length width)) {
66 if (exists $changed->{"-$k"}) {
67 $c->_layoutRequest($LAYOUT_REQUEST_GEOM);
68 last;
69 }
70 }
71 }
72
73 sub anchor {
74 my $c = shift;
75 my $var = \$c->{Configure}{'-anchor'};
76 my $old = $$var;
77
78 if(@_) {
79 my $new = shift;
80 croak "bad anchor position \"$new\": must be n, s, w or e"
81 unless $new =~ /^[news]$/;
82 $$var = $new;
83 }
84
85 $old;
86 }
87
88 sub _layoutRequest {
89 my $c = shift;
90 my $why = shift;
91 $c->afterIdle(['_arrange',$c]) unless $c->{'layout_pending'};
92 $c->{'layout_pending'} |= $why;
93 }
94
95 sub _arrange {
96 my $c = shift;
97 my $why = $c->{'layout_pending'};
98
99 $c->{'layout_pending'} = 0;
100
101 my $w = $c->Width;
102 my $h = $c->Height;
103 my $bw = $c->cget('-borderwidth') + $c->cget('-highlightthickness');
104 my $x = abs(int($c->{Configure}{'-padx'})) + $bw;
105 my $y = abs(int($c->{Configure}{'-pady'})) + $bw;
106 my $value = $c->value;
107 my $from = $c->{Configure}{'-from'};
108 my $to = $c->{Configure}{'-to'};
109 my $horz = $c->{Configure}{'-anchor'} =~ /[ew]/i ? 1 : 0;
110 my $dir = $c->{Configure}{'-anchor'} =~ /[se]/i ? -1 : 1;
111
112 my($minv,$maxv) = $from < $to ? ($from,$to) : ($to,$from);
113
114 if(($w == 1 && $h == 1) || ($why & $LAYOUT_REQUEST_GEOM)) {
115 my $bw = $c->cget('-borderwidth');
116 my $defw = 10 + $y*2 + $bw *2;
117 my $defl = ($maxv - $minv) + $x*2 + $bw*2;
118
119 $h = $c->pixels($c->{Configure}{'-length'}) || $defl;
120 $w = $c->pixels($c->{Configure}{'-width'}) || $defw;
121
122 ($w,$h) = ($h,$w) if $horz;
123 $c->GeometryRequest($w,$h);
124 $c->update;
125
126 $w = $c->Width;
127 $h = $c->Height;
128 }
129
130 $w -= $x*2;
131 $h -= $y*2;
132
133 my $length = $horz ? $w : $h;
134 my $width = $horz ? $h : $w;
135
136 my $blocks = int($c->{Configure}{'-blocks'});
137 my $gap = int($c->{Configure}{'-gap'});
138
139 $blocks = 1 if $blocks < 1;
140
141 my $gwidth = $gap * ( $blocks - 1);
142 my $bwidth = ($length - $gwidth) / $blocks;
143
144 if($bwidth < 3 || $blocks <= 1 || $gap <= 0) {
145 $blocks = 1;
146 $bwidth = $length;
147 $gap = 0;
148 }
149
150 if($why & $LAYOUT_REQUEST_COLORS) {
151 my $colors = $c->{Configure}{'-colors'} || [];
152 my $bdir = $from < $to ? $dir : 0 - $dir;
153
154 $c->delete('all');
155
156 $c->createRectangle(0,0,$w+$x*2,$h+$y*2,
157 -fill => $c->{Configure}{'-troughcolor'},
158 -width => 0,
159 -outline => undef);
160
161 $c->{'cover'} = $c->createRectangle($x,$y,$w,$h,
162 -fill => $c->{Configure}{'-troughcolor'},
163 -width => 0,
164 -outline => undef);
165
166 my($x0,$y0,$x1,$y1);
167
168 if($horz) {
169 if($bdir > 0) {
170 ($x0,$y0) = ($x - $gap,$y);
171 }
172 else {
173 ($x0,$y0) = ($length + $x + $gap,$y);
174 }
175 ($x1,$y1) = ($x0,$y0 + $width);
176 }
177 else {
178 if($bdir > 0) {
179 ($x0,$y0) = ($x,$y - $gap);
180 }
181 else {
182 ($x0,$y0) = ($x,$length + $y + $gap);
183 }
184 ($x1,$y1) = ($x0 + $width,$y0);
185 }
186
187 my $blks = $blocks;
188 my $dval = ($maxv - $minv) / $blocks;
189 my $color = $c->cget('-foreground');
190 my $pos = 0;
191 my $val = $minv;
192
193 while($val < $maxv) {
194 my($bw,$nval);
195
196 while(($pos < @$colors) && $colors->[$pos] <= $val) {
197 $color = $colors->[$pos+1];
198 $pos += 2;
199 }
200
201 if($blocks == 1) {
202 $nval = defined($colors->[$pos])
203 ? $colors->[$pos] : $maxv;
204 $bw = (($nval - $val) / ($maxv - $minv)) * $length;
205 }
206 else {
207 $bw = $bwidth;
208 $nval = $val + $dval if($blocks > 1);
209 }
210
211 if($horz) {
212 if($bdir > 0) {
213 $x0 = $x1 + $gap;
214 $x1 = $x0 + $bw;
215 }
216 else {
217 $x1 = $x0 - $gap;
218 $x0 = $x1 - $bw;
219 }
220 }
221 else {
222 if($bdir > 0) {
223 $y0 = $y1 + $gap;
224 $y1 = $y0 + $bw;
225 }
226 else {
227 $y1 = $y0 - $gap;
228 $y0 = $y1 - $bw;
229 }
230 }
231
232 $c->createRectangle($x0,$y0,$x1,$y1,
233 -fill => $color,
234 -width => 0,
235 -outline => undef
236 );
237 $val = $nval;
238 }
239 }
240
241 my $cover = $c->{'cover'};
242 my $ddir = $from > $to ? 1 : -1;
243
244 if(($value <=> $to) == (0-$ddir)) {
245 $c->lower($cover);
246 }
247 elsif(($value <=> $from) == $ddir) {
248 $c->raise($cover);
249 my $x1 = $horz ? $x + $length : $x + $width;
250 my $y1 = $horz ? $y + $width : $y + $length;
251 $c->coords($cover,$x,$y,$x1,$y1);
252 }
253 else {
254 my $step;
255 $value = int($value / $step) * $step
256 if(defined($step = $c->{Configure}{'-resolution'}) && $step > 0);
257
258 $maxv = $minv+1
259 if $minv == $maxv;
260
261 my $range = $maxv - $minv;
262 my $bval = $range / $blocks;
263 my $offset = abs($value - $from);
264 my $ioff = int($offset / $bval);
265 my $start = $ioff * ($bwidth + $gap);
266 $start += ($offset - ($ioff * $bval)) / $bval * $bwidth;
267
268 my($x0,$x1,$y0,$y1);
269
270 if($horz) {
271 $y0 = $y;
272 $y1 = $y + $h;
273 if($dir > 0) {
274 $x0 = $x + $start;
275 $x1 = $x + $w;
276 }
277 else {
278 $x0 = $x;
279 $x1 = $w + $x - $start;
280 }
281 }
282 else {
283 $x0 = $x;
284 $x1 = $x + $w;
285 if($dir > 0) {
286 $y0 = $y + $start;
287 $y1 = $y + $h;
288 }
289 else {
290 $y0 = $y;
291 $y1 = $h + $y - $start;
292 }
293 }
294
295
296 $c->raise($cover);
297 $c->coords($cover,$x0,$y0,$x1,$y1);
298 }
299 }
300
301 sub value {
302 my $c = shift;
303 my $val = defined($c->{'-variable'})
304 ? $c->{'-variable'}
305 : \$c->{'-value'};
306 my $old = defined($$val) ? $$val : $c->{Configure}{'-from'};
307
308 if(@_) {
309 my $value = shift;
310 $$val = defined($value) ? $value : $c->{Configure}{'-from'};
311 $c->_layoutRequest($LAYOUT_REQUEST_VALUE);
312 }
313
314 $old;
315 }
316
317 sub variable {
318 my $c = shift;
319 my $oldvarref = $c->{'-variable'};
320 my $oldval; $oldval = $$oldvarref if $oldvarref;
321 if(@_) {
322 my $varref = shift;
323 if ($oldvarref)
324 {
325 $c->traceVdelete($oldvarref);
326 }
327 $c->{'-variable'} = $varref;
328 $c->traceVariable($varref, 'w', sub { $c->value($_[1]) });
329 $$varref = $oldval;
330 $c->_layoutRequest($LAYOUT_REQUEST_VALUE);
331 }
332 $oldval;
333 }
334
335 sub Destroyed
336 {
337 my $c = shift;
338 my $var = delete $c->{'-variable'};
339 $c->traceVdelete($var);
340 }
341
342 1;
343 __END__
344
345 =head1 NAME
346
347 Tk::ProgressBar - A graphical progress bar
348
349 =for category Derived Widgets
350
351 =head1 SYNOPSIS
352
353 use Tk::ProgressBar;
354
355 $progress = $parent->ProgressBar(
356 -width => 20,
357 -length => 200,
358 -anchor => 's',
359 -from => 0,
360 -to => 100,
361 -blocks => 10,
362 -colors => [0, 'green', 50, 'yellow' , 80, 'red'],
363 -variable => \$percent_done
364 );
365
366 $progress->value($position);
367
368 =head1 DESCRIPTION
369
370 B<Tk::ProgressBar> provides a widget which will show a graphical representation
371 of a value, given maximum and minimum reference values.
372
373 =head1 STANDARD OPTIONS
374
375 The following standard widget options are supported:
376
377 =over 4
378
379 =item B<-borderwidth>
380
381 =item B<-highlightthickness>
382
383 Defaults to 0.
384
385 =item B<-padx>
386
387 Defaults to 0.
388
389 =item B<-pady>
390
391 Defaults to 0.
392
393 =item B<-relief>
394
395 Defaults to C<sunken>
396
397 =item B<-troughcolor>
398
399 The color to be used for the background (trough) of the progress bar.
400 Default is to use grey55.
401
402 =back
403
404 =head1 WIDGET-SPECIFIC OPTIONS
405
406 =over 4
407
408 =item B<-anchor>
409
410 This can be used to position the start point of the bar. Default
411 is 'w' (horizontal bar starting from the left). A vertical bar can be
412 configured by using either 's' or 'n'.
413
414 =item B<-blocks>
415
416 This controls the number of blocks to be used to construct the progress
417 bar. The default is to break the bar into 10 blocks.
418
419 =item B<-colors>
420
421 Controls the colors to be used for different positions of the progress bar.
422 The colors should be supplied as a reference to an array containing pairs
423 of positions and colors.
424
425 -colors => [ 0, 'green', 50, 'red' ]
426
427 means that for the range 0 to 50 the progress bar should be green
428 and for higher values it should be red.
429
430
431 =item B<-from>
432
433 This sets the lower limit of the progress bar. If the bar is set to a
434 value below the lower limt no bar will be displayed. Defaults to 0.
435 See the C<-to> description for more information.
436
437 =item B<-gap>
438
439 This is the spacing (in pixels) between each block. Defaults to 1.
440 Use 0 to get a continuous bar.
441
442
443 =item B<-length>
444
445 Specifies the desired long dimension of the ProgressBar in screen
446 units (i.e. any of the forms acceptable to Tk_GetPixels). For vertical
447 ProgressBars this is the ProgressBars height; for horizontal scales it
448 is the ProgressBars width. The default length is calculated from the
449 values of C<-padx>, C<-borderwidth>, C<-highlightthickness> and the
450 difference between C<-from> and C<-to>.
451
452
453 =item B<-resolution>
454
455 A real value specifying the resolution for the scale. If this value is greater
456 than zero then the scale's value will always be rounded to an even multiple of
457 this value, as will tick marks and the endpoints of the scale. If the value is
458 less than zero then no rounding occurs. Defaults to 1 (i.e., the value will be
459 integral).
460
461 =item B<-to>
462
463 This sets the upper limit of the progress bar. If a value is specified
464 (for example, using the C<value> method) that lies above this value the
465 full progress bar will be displayed. Defaults to 100.
466
467
468
469 =item B<-variable>
470
471 Specifies the reference to a scalar variable to link to the ProgressBar.
472 Whenever the value of the variable changes, the ProgressBar will update
473 to reflect this value. (See also the B<value> method below.)
474
475 =item B<-value>
476
477 This can be used to set the current position of the progress bar
478 when used in conjunction with the standard C<configure>. It is
479 usually recommended to use the B<value> method instead.
480
481
482 =item B<-width>
483
484 Specifies the desired narrow dimension of the ProgressBar in screen
485 units (i.e. any of the forms acceptable to Tk_GetPixels). For
486 vertical ProgressBars this is the ProgressBars width; for horizontal
487 bars this is the ProgressBars height. The default width is derived
488 from the values of C<-borderwidth> and C<-pady> and C<-highlightthickness>.
489
490 =back
491
492 =head1 WIDGET METHODS
493
494 =over 4
495
496 =item I<$ProgressBar>-E<gt>B<value>(?I<value>?)
497
498 If I<value> is omitted, returns the current value of the ProgressBar. If
499 I<value> is given, the value of the ProgressBar is set. If I<$value> is
500 given but undefined the value of the option B<-from> is used.
501
502 =back
503
504
505 =head1 AUTHOR
506
507 Graham Barr E<lt>F<[email protected]>E<gt>
508
509 =head1 COPYRIGHT
510
511 Copyright (c) 1997-1998 Graham Barr. All rights reserved.
512 This program is free software; you can redistribute it and/or modify it
513 under the same terms as Perl itself.
514
515 =cut
516
517