| 1 | package Tk::ProgressBarPlus; | ||
| 2 | |||
| 3 | use vars qw($VERSION); | ||
| 4 | $VERSION = '3.0141'; # $Id: //depot/Tk8/Tk/ProgressBar.pm#14 $ | ||
| 5 | |||
| 6 | use Tk; | ||
| 7 | use Tk::Canvas; | ||
| 8 | use Carp; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | use base qw(Tk::Derived Tk::Canvas); | ||
| 12 | |||
| 13 | Construct Tk::Widget 'ProgressBarPlus'; | ||
| 14 | |||
| 15 | sub ClassInit { | ||
| 16 | my ($class,$mw) = @_; | ||
| 17 | |||
| 18 | $class->SUPER::ClassInit($mw); | ||
| 19 | |||
| 20 | $mw->bind($class,'<Configure>', ['_layoutRequest',1]); | ||
| 21 | } | ||
| 22 | |||
| 23 | |||
| 24 | sub Populate { | ||
| 25 | my($c,$args) = @_; | ||
| 26 | |||
| 27 | $c->ConfigSpecs( | ||
| 28 | -width => [PASSIVE => undef, undef, 0], | ||
| 29 | '-length' => [PASSIVE => undef, undef, 0], | ||
| 30 | -from => [PASSIVE => undef, undef, 0], | ||
| 31 | -to => [PASSIVE => undef, undef, 100], | ||
| 32 | -blocks => [PASSIVE => undef, undef, 10], | ||
| 33 | -padx => [PASSIVE => 'padX', 'Pad', 0], | ||
| 34 | -pady => [PASSIVE => 'padY', 'Pad', 0], | ||
| 35 | -gap => [PASSIVE => undef, undef, 1], | ||
| 36 | -colors => [PASSIVE => undef, undef, undef], | ||
| 37 | -relief => [SELF => 'relief', 'Relief', 'sunken'], | ||
| 38 | -value => [METHOD => undef, undef, undef], | ||
| 39 | -variable => [METHOD => undef, undef, undef], | ||
| 40 | -anchor => [METHOD => 'anchor', 'Anchor', 'w'], | ||
| 41 | -resolution | ||
| 42 | => [PASSIVE => undef, undef, 1.0], | ||
| 43 | -highlightthickness | ||
| 44 | => [SELF => 'highlightThickness','HighlightThickness',0], | ||
| 45 | -troughcolor | ||
| 46 | => [PASSIVE => 'troughColor', 'Background', 'grey55'], | ||
| 47 | # for Value-Display | ||
| 48 | -showvalue => [PASSIVE => undef, undef, 0], | ||
| 49 | -valueformat => [PASSIVE => undef, undef, '%s%%'], | ||
| 50 | -valuecolor => [PASSIVE => undef, undef, 'white'], | ||
| 51 | -font => [PASSIVE => 'font', 'Font', undef], | ||
| 52 | ); | ||
| 53 | _layoutRequest($c,1); | ||
| 54 | $c->OnDestroy(['Destroyed' => $c]); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub anchor { | ||
| 58 | my $c = shift; | ||
| 59 | my $var = \$c->{Configure}{'-anchor'}; | ||
| 60 | my $old = $$var; | ||
| 61 | |||
| 62 | if(@_) { | ||
| 63 | my $new = shift; | ||
| 64 | croak "bad anchor position \"$new\": must be n, s, w or e" | ||
| 65 | unless $new =~ /^[news]$/; | ||
| 66 | $$var = $new; | ||
| 67 | } | ||
| 68 | |||
| 69 | $old; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _layoutRequest { | ||
| 73 | my $c = shift; | ||
| 74 | my $why = shift; | ||
| 75 | $c->afterIdle(['_arrange',$c]) unless $c->{'layout_pending'}; | ||
| 76 | $c->{'layout_pending'} |= $why; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _arrange { | ||
| 80 | my $c = shift; | ||
| 81 | my $why = $c->{'layout_pending'}; | ||
| 82 | |||
| 83 | $c->{'layout_pending'} = 0; | ||
| 84 | |||
| 85 | my $w = $c->Width; | ||
| 86 | my $h = $c->Height; | ||
| 87 | my $bw = $c->cget('-borderwidth') + $c->cget('-highlightthickness'); | ||
| 88 | my $x = abs(int($c->{Configure}{'-padx'})) + $bw; | ||
| 89 | my $y = abs(int($c->{Configure}{'-pady'})) + $bw; | ||
| 90 | my $value = $c->value; | ||
| 91 | my $realvalue = $value; | ||
| 92 | |||
| 93 | my $from = $c->{Configure}{'-from'}; | ||
| 94 | my $to = $c->{Configure}{'-to'}; | ||
| 95 | my $horz = $c->{Configure}{'-anchor'} =~ /[ew]/i ? 1 : 0; | ||
| 96 | my $dir = $c->{Configure}{'-anchor'} =~ /[ne]/i ? -1 : 1; | ||
| 97 | |||
| 98 | my($minv,$maxv) = $from < $to ? ($from,$to) : ($to,$from); | ||
| 99 | |||
| 100 | if($w == 1 && $h == 1) { | ||
| 101 | my $bw = $c->cget('-borderwidth'); | ||
| 102 | my $defw = 10 + $y*2 + $bw *2; | ||
| 103 | my $defl = ($maxv - $minv) + $x*2 + $bw*2; | ||
| 104 | |||
| 105 | $h = $c->pixels($c->{Configure}{'-length'}) || $defl; | ||
| 106 | $w = $c->pixels($c->{Configure}{'-width'}) || $defw; | ||
| 107 | |||
| 108 | ($w,$h) = ($h,$w) if $horz; | ||
| 109 | $c->GeometryRequest($w,$h); | ||
| 110 | $c->parent->update; | ||
| 111 | $c->update; | ||
| 112 | |||
| 113 | $w = $c->Width; | ||
| 114 | $h = $c->Height; | ||
| 115 | } | ||
| 116 | |||
| 117 | $w -= $x*2; | ||
| 118 | $h -= $y*2; | ||
| 119 | |||
| 120 | my $length = $horz ? $w : $h; | ||
| 121 | my $width = $horz ? $h : $w; | ||
| 122 | |||
| 123 | my $blocks = int($c->{Configure}{'-blocks'}); | ||
| 124 | my $gap = int($c->{Configure}{'-gap'}); | ||
| 125 | |||
| 126 | $blocks = 1 if $blocks < 1; | ||
| 127 | |||
| 128 | my $gwidth = $gap * ( $blocks - 1); | ||
| 129 | my $bwidth = ($length - $gwidth) / $blocks; | ||
| 130 | |||
| 131 | if($bwidth < 3 || $blocks <= 1 || $gap <= 0) { | ||
| 132 | $blocks = 1; | ||
| 133 | $bwidth = $length; | ||
| 134 | $gap = 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | if($why & 1) { | ||
| 138 | my $colors = $c->{Configure}{'-colors'} || []; | ||
| 139 | my $bdir = $from < $to ? $dir : 0 - $dir; | ||
| 140 | |||
| 141 | $c->delete($c->find('all')); | ||
| 142 | |||
| 143 | $c->createRectangle(0,0,$w+$x*2,$h+$y*2, | ||
| 144 | -fill => $c->{Configure}{'-troughcolor'}, | ||
| 145 | -width => 0, | ||
| 146 | -outline => undef, | ||
| 147 | -tags => 'rectangle'); | ||
| 148 | |||
| 149 | $c->{'cover'} = $c->createRectangle($x,$y,$w,$h, | ||
| 150 | -fill => $c->{Configure}{'-troughcolor'}, | ||
| 151 | -width => 0, | ||
| 152 | -outline => undef, | ||
| 153 | -tags => 'cover'); | ||
| 154 | |||
| 155 | if ($c->cget('-showvalue')) { | ||
| 156 | my $text = sprintf($c->cget('-valueformat'), $value); | ||
| 157 | my @font; @font = (-font => $c->cget('-font') ) if defined $c->cget('-font'); | ||
| 158 | $c->createText($x+$w/2, $y+$h/2, | ||
| 159 | -text => $text, | ||
| 160 | -tags => 'percentage', | ||
| 161 | -fill => $c->cget('-valuecolor'), | ||
| 162 | @font, | ||
| 163 | ); | ||
| 164 | } | ||
| 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 | my $cover = $c->{'cover'}; | ||
| 241 | my $ddir = $from > $to ? 1 : -1; | ||
| 242 | |||
| 243 | if(($value <=> $to) == (0-$ddir)) { | ||
| 244 | $c->lower($cover); | ||
| 245 | } | ||
| 246 | elsif(($value <=> $from) == $ddir) { | ||
| 247 | $c->raise($cover); | ||
| 248 | my $x1 = $horz ? $x + $length : $x + $width; | ||
| 249 | my $y1 = $horz ? $y + $width : $y + $length; | ||
| 250 | $c->coords($cover,$x,$y,$x1,$y1); | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | my $step; | ||
| 254 | $value = int($value / $step) * $step | ||
| 255 | if(defined($step = $c->{Configure}{'-resolution'}) && $step > 0); | ||
| 256 | |||
| 257 | $maxv = $minv+1 | ||
| 258 | if $minv == $maxv; | ||
| 259 | |||
| 260 | my $range = $maxv - $minv; | ||
| 261 | my $bval = $range / $blocks; | ||
| 262 | my $offset = abs($value - $from); | ||
| 263 | my $ioff = int($offset / $bval); | ||
| 264 | my $start = $ioff * ($bwidth + $gap); | ||
| 265 | $start += ($offset - ($ioff * $bval)) / $bval * $bwidth; | ||
| 266 | |||
| 267 | my($x0,$x1,$y0,$y1); | ||
| 268 | |||
| 269 | if($horz) { | ||
| 270 | $y0 = $y; | ||
| 271 | $y1 = $y + $h; | ||
| 272 | if($dir > 0) { | ||
| 273 | $x0 = $x + $start; | ||
| 274 | $x1 = $x + $w; | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | $x0 = $x; | ||
| 278 | $x1 = $w + $x - $start; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | $x0 = $x; | ||
| 283 | $x1 = $x + $w; | ||
| 284 | if($dir > 0) { | ||
| 285 | $y0 = $y + $start; | ||
| 286 | $y1 = $y + $h; | ||
| 287 | } | ||
| 288 | else { | ||
| 289 | $y0 = $y; | ||
| 290 | $y1 = $h + $y - $start; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | $c->raise($cover); | ||
| 296 | $c->coords($cover,$x0,$y0,$x1,$y1); | ||
| 297 | } | ||
| 298 | |||
| 299 | # Addon to display the percentage as a text | ||
| 300 | if ($c->cget('-showvalue')) { | ||
| 301 | my ($min, $max); | ||
| 302 | if ($c->{Configure}{'-to'} > $c->{Configure}{'-from'}) { | ||
| 303 | $min = $c->{Configure}{'-from'}; | ||
| 304 | $max = $c->{Configure}{'-to'}; | ||
| 305 | } | ||
| 306 | else { | ||
| 307 | $min = $c->{Configure}{'-to'}; | ||
| 308 | $max = $c->{Configure}{'-from'}; | ||
| 309 | } | ||
| 310 | $realvalue = $min if $realvalue < $min; | ||
| 311 | $realvalue = $max if $realvalue > $max; | ||
| 312 | my $text = sprintf($c->cget('-valueformat'), $realvalue); | ||
| 313 | $c->dchars('percentage', 0, 'end'); | ||
| 314 | $c->insert('percentage', 0, $text); | ||
| 315 | $c->raise('percentage'); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | sub value { | ||
| 320 | my $c = shift; | ||
| 321 | my $val = defined($c->{'-variable'}) | ||
| 322 | ? $c->{'-variable'} | ||
| 323 | : \$c->{'-value'}; | ||
| 324 | my $old = defined($$val) ? $$val : $c->{Configure}{'-from'}; | ||
| 325 | |||
| 326 | if(@_) { | ||
| 327 | my $value = shift; | ||
| 328 | $$val = defined($value) ? $value : $c->{Configure}{'-from'}; | ||
| 329 | _layoutRequest($c,2); | ||
| 330 | } | ||
| 331 | |||
| 332 | $old; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub variable { | ||
| 336 | my $c = shift; | ||
| 337 | my $val = \$c->{'-variable'}; | ||
| 338 | my $old = $$val; | ||
| 339 | if(@_) { | ||
| 340 | my $value = shift; | ||
| 341 | if (ref $old) | ||
| 342 | { | ||
| 343 | $c->{'-value'} = $$old; | ||
| 344 | untie $$old if tied($$old); | ||
| 345 | } | ||
| 346 | tie $$value,'Tk::Configure',$c,'-value'; | ||
| 347 | $$val = $value; | ||
| 348 | _layoutRequest($c,2); | ||
| 349 | } | ||
| 350 | $old; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub Destroyed | ||
| 354 | { | ||
| 355 | my $c = shift; | ||
| 356 | my $var = delete $c->{'-variable'}; | ||
| 357 | untie $$var if (defined($var) && ref($var)) | ||
| 358 | } | ||
| 359 | |||
| 360 | ######################################################################## | ||
| 361 | 1; | ||
| 362 | __END__ |