| 1 | package Tk::ITree; | ||
| 2 | # Tree -- TixTree widget | ||
| 3 | # | ||
| 4 | # Derived from Tree.tcl in Tix 4.1 | ||
| 5 | # | ||
| 6 | # Chris Dean <ctdean@cogit.com> | ||
| 7 | # Changes: Renee Baecker <module@renee-baecker.de> | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Tk::ITree - Shamelessly copied Tk::Tree widget | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use vars qw($VERSION); | ||
| 16 | $VERSION = '0.03'; | ||
| 17 | |||
| 18 | use Tk (); | ||
| 19 | use Tk::Derived; | ||
| 20 | use Tk::HList; | ||
| 21 | use base qw(Tk::Derived Tk::HList); | ||
| 22 | use strict; | ||
| 23 | |||
| 24 | Construct Tk::Widget 'ITree'; | ||
| 25 | |||
| 26 | sub Tk::Widget::ScrlTree { shift->Scrolled('ITree' => @_) } | ||
| 27 | |||
| 28 | my $minusimg = '#define indicatorclose_width 11 | ||
| 29 | #define indicatorclose_height 11 | ||
| 30 | static unsigned char indicatorclose_bits[] = { | ||
| 31 | 0xff, 0x07, 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0xfd, 0x05, | ||
| 32 | 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0xff, 0x07 }; | ||
| 33 | '; | ||
| 34 | |||
| 35 | my $plusimg = '#define indicatoropen_width 11 | ||
| 36 | #define indicatoropen_height 11 | ||
| 37 | static unsigned char indicatoropen_bits[] = { | ||
| 38 | 0xff, 0x07, 0x01, 0x04, 0x21, 0x04, 0x21, 0x04, 0x21, 0x04, 0xfd, 0x05, | ||
| 39 | 0x21, 0x04, 0x21, 0x04, 0x21, 0x04, 0x01, 0x04, 0xff, 0x07 }; | ||
| 40 | '; | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | require Tk::ITree; | ||
| 45 | my $tree= $window->ITree(@options)->pack; | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | B<Tk::ITree> is a an almost exact copy of L<Tk::Tree>. This one performs | ||
| 50 | a bit better under a dark desktop theme. | ||
| 51 | |||
| 52 | The reason for copying instead of inheriting is that just the method i | ||
| 53 | needed to change is a hidden one. | ||
| 54 | |||
| 55 | All cudos go to the original authors. | ||
| 56 | |||
| 57 | You can use all config variables and methods of the Tree widget. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | =head1 B<CONFIG VARIABLES> | ||
| 62 | |||
| 63 | =over 4 | ||
| 64 | |||
| 65 | =item Switch: B<-indicatorminusimg> | ||
| 66 | |||
| 67 | Set or get the I<minus> indicator image. By default it is set | ||
| 68 | to an internal bitmap in this package. | ||
| 69 | |||
| 70 | =item Switch: B<-indicatorplusimg> | ||
| 71 | |||
| 72 | Set or get the I<plus> indicator image. By default it is set | ||
| 73 | to an internal bitmap in this package. | ||
| 74 | |||
| 75 | =item Switch: B<-leftrightcall> | ||
| 76 | |||
| 77 | This callback is executed when the user presses the right or left arrow key. | ||
| 78 | The call gets 'left' or 'right' and the name of the selected entry as parameter. | ||
| 79 | |||
| 80 | =back | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub Populate | ||
| 85 | { | ||
| 86 | my( $w, $args ) = @_; | ||
| 87 | |||
| 88 | $w->SUPER::Populate( $args ); | ||
| 89 | |||
| 90 | my $l = $w->Label; | ||
| 91 | my $fg = $l->cget('-foreground'); | ||
| 92 | $l->destroy; | ||
| 93 | $w->ConfigSpecs( | ||
| 94 | -closecmd => ['CALLBACK', 'closeCmd', 'CloseCmd', 'CloseCmd'], | ||
| 95 | -indicatorminusimg => ['PASSIVE', undef, undef, $w->Bitmap( | ||
| 96 | -data => $minusimg, | ||
| 97 | -foreground => $fg, | ||
| 98 | )], | ||
| 99 | -indicatorplusimg => ['PASSIVE', undef, undef, $w->Bitmap( | ||
| 100 | -data => $plusimg, | ||
| 101 | -foreground => $fg, | ||
| 102 | )], | ||
| 103 | -indent => ['SELF', 'indent', 'Indent', 20], | ||
| 104 | -indicator => ['SELF', 'indicator', 'Indicator', 1], | ||
| 105 | -indicatorcmd => ['CALLBACK', 'indicatorCmd', 'IndicatorCmd', 'IndicatorCmd'], | ||
| 106 | -ignoreinvoke => ['PASSIVE', 'ignoreInvoke', 'IgnoreInvoke', 0], | ||
| 107 | -itemtype => ['SELF', 'itemtype', 'Itemtype', 'imagetext'], | ||
| 108 | -leftrightcall => ['CALLBACK', undef, undef, sub {}], | ||
| 109 | -opencmd => ['CALLBACK', 'openCmd', 'OpenCmd', 'OpenCmd' ], | ||
| 110 | -width => ['SELF', 'width', 'Width', 20], | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub autosetmode | ||
| 115 | { | ||
| 116 | my( $w ) = @_; | ||
| 117 | $w->setmode(); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub add_pathimage | ||
| 121 | { | ||
| 122 | my ($w,$path,$imgopen,$imgclose) = @_; | ||
| 123 | $imgopen ||= "minusarm"; | ||
| 124 | $imgclose ||= "plusarm"; | ||
| 125 | |||
| 126 | my $separator = $w->cget(-separator); | ||
| 127 | |||
| 128 | $path =~ s/([\.?()|])/\\$1/g; | ||
| 129 | $path =~ s/\$/\\\$/g; | ||
| 130 | $path =~ s/\\\$$/\$/; | ||
| 131 | $path =~ s/\*/[^$separator]+/g; | ||
| 132 | |||
| 133 | push(@{$w->{Images}},[$path,$imgopen,$imgclose]); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub child_entries | ||
| 137 | { | ||
| 138 | my ($w,$path,$depth) = @_; | ||
| 139 | |||
| 140 | my $level = 1; | ||
| 141 | $depth ||= 1; | ||
| 142 | $path ||= ''; | ||
| 143 | |||
| 144 | my @children = $w->_get_childinfos($depth,$level,$path); | ||
| 145 | |||
| 146 | return wantarray ? @children : scalar(@children); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _get_childinfos | ||
| 150 | { | ||
| 151 | my ($w,$maxdepth,$level,$path) = @_; | ||
| 152 | my @children = $w->infoChildren($path); | ||
| 153 | my @tmp; | ||
| 154 | |||
| 155 | if($level < $maxdepth) | ||
| 156 | { | ||
| 157 | for my $child(@children) | ||
| 158 | { | ||
| 159 | push(@tmp,$w->_get_childinfos($maxdepth,$level +1,$child)); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | push(@children,@tmp); | ||
| 164 | |||
| 165 | return @children; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub IndicatorCmd | ||
| 169 | { | ||
| 170 | my( $w, $ent, $event ) = @_; | ||
| 171 | |||
| 172 | my $mode = $w->getmode( $ent ); | ||
| 173 | |||
| 174 | if ( $event eq '<Arm>' ) | ||
| 175 | { | ||
| 176 | if ($mode eq 'open' ) | ||
| 177 | { | ||
| 178 | #$w->_indicator_image( $ent, 'plusarm' ); | ||
| 179 | $w->_open($ent); | ||
| 180 | } | ||
| 181 | else | ||
| 182 | { | ||
| 183 | #$w->_indicator_image( $ent, 'minusarm' ); | ||
| 184 | $w->_close($ent); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | elsif ( $event eq '<Disarm>' ) | ||
| 188 | { | ||
| 189 | if ($mode eq 'open' ) | ||
| 190 | { | ||
| 191 | #$w->_indicator_image( $ent, 'plus' ); | ||
| 192 | $w->_open($ent); | ||
| 193 | } | ||
| 194 | else | ||
| 195 | { | ||
| 196 | #$w->_indicator_image( $ent, 'minus' ); | ||
| 197 | $w->_close($ent); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | elsif( $event eq '<Activate>' ) | ||
| 201 | { | ||
| 202 | $w->Activate( $ent, $mode ); | ||
| 203 | $w->Callback( -browsecmd => $ent ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | sub close | ||
| 208 | { | ||
| 209 | my( $w, $ent ) = @_; | ||
| 210 | my $mode = $w->getmode( $ent ); | ||
| 211 | $w->Activate( $ent, $mode ) if( $mode eq 'close' ); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub open | ||
| 215 | { | ||
| 216 | my( $w, $ent ) = @_; | ||
| 217 | my $mode = $w->getmode( $ent ); | ||
| 218 | $w->Activate( $ent, $mode ) if( $mode eq 'open' ); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub getmode | ||
| 222 | { | ||
| 223 | my( $w, $ent ) = @_; | ||
| 224 | |||
| 225 | return( 'none' ) unless $w->indicatorExists( $ent ); | ||
| 226 | |||
| 227 | my $img = $w->_indicator_image( $ent ); | ||
| 228 | if ($img eq "plus" || $img eq "plusarm" || grep{$img eq $_->[2]}@{$w->{Images}}) | ||
| 229 | { | ||
| 230 | return( 'open' ); | ||
| 231 | } | ||
| 232 | return( 'close' ); | ||
| 233 | } | ||
| 234 | |||
| 235 | sub setmode | ||
| 236 | { | ||
| 237 | my ($w,$ent,$mode) = @_; | ||
| 238 | unless (defined $mode) | ||
| 239 | { | ||
| 240 | $mode = 'none'; | ||
| 241 | my @args; | ||
| 242 | push(@args,$ent) if defined $ent; | ||
| 243 | my @children = $w->infoChildren( @args ); | ||
| 244 | if ( @children ) | ||
| 245 | { | ||
| 246 | $mode = 'close'; | ||
| 247 | foreach my $c (@children) | ||
| 248 | { | ||
| 249 | $mode = 'open' if $w->infoHidden( $c ); | ||
| 250 | $w->setmode( $c ); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | if (defined $ent) | ||
| 256 | { | ||
| 257 | if ( $mode eq 'open' ) | ||
| 258 | { | ||
| 259 | #$w->_indicator_image( $ent, 'plus' ); | ||
| 260 | $w->_open($ent); | ||
| 261 | } | ||
| 262 | elsif ( $mode eq 'close' ) | ||
| 263 | { | ||
| 264 | #$w->_indicator_image( $ent, 'minus' ); | ||
| 265 | $w->_close($ent); | ||
| 266 | } | ||
| 267 | elsif( $mode eq 'none' ) | ||
| 268 | { | ||
| 269 | $w->_indicator_image( $ent, undef ); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _open | ||
| 275 | { | ||
| 276 | my ($w,$ent) = @_; | ||
| 277 | $w->_indicator_image( $ent, "plus" ); | ||
| 278 | for my $entry (@{$w->{Images}}) | ||
| 279 | { | ||
| 280 | if($ent =~ $entry->[0]) | ||
| 281 | { | ||
| 282 | $w->_indicator_image( $ent, $entry->[2] ); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | sub _close | ||
| 288 | { | ||
| 289 | my ($w,$ent) = @_; | ||
| 290 | $w->_indicator_image( $ent, "minus" ); | ||
| 291 | for my $entry (@{$w->{Images}}) | ||
| 292 | { | ||
| 293 | if($ent =~ $entry->[0]) | ||
| 294 | { | ||
| 295 | $w->_indicator_image( $ent, $entry->[1] ); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | sub Activate | ||
| 301 | { | ||
| 302 | my( $w, $ent, $mode ) = @_; | ||
| 303 | if ( $mode eq 'open' ) | ||
| 304 | { | ||
| 305 | $w->Callback( -opencmd => $ent ); | ||
| 306 | #$w->_indicator_image( $ent, 'minus' ); | ||
| 307 | $w->_close($ent); | ||
| 308 | } | ||
| 309 | elsif ( $mode eq 'close' ) | ||
| 310 | { | ||
| 311 | $w->Callback( -closecmd => $ent ); | ||
| 312 | #$w->_indicator_image( $ent, 'plus' ); | ||
| 313 | $w->_open($ent); | ||
| 314 | } | ||
| 315 | else | ||
| 316 | { | ||
| 317 | |||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | sub OpenCmd | ||
| 322 | { | ||
| 323 | my( $w, $ent ) = @_; | ||
| 324 | # The default action | ||
| 325 | foreach my $kid ($w->infoChildren( $ent )) | ||
| 326 | { | ||
| 327 | $w->show( -entry => $kid ); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | sub CloseCmd | ||
| 332 | { | ||
| 333 | my( $w, $ent ) = @_; | ||
| 334 | |||
| 335 | # The default action | ||
| 336 | foreach my $kid ($w->infoChildren( $ent )) | ||
| 337 | { | ||
| 338 | $w->hide( -entry => $kid ); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | sub Command | ||
| 343 | { | ||
| 344 | my( $w, $ent ) = @_; | ||
| 345 | |||
| 346 | return if $w->{Configure}{-ignoreInvoke}; | ||
| 347 | |||
| 348 | $w->Activate( $ent, $w->getmode( $ent ) ) if $w->indicatorExists( $ent ); | ||
| 349 | } | ||
| 350 | |||
| 351 | sub _indicator_image | ||
| 352 | { | ||
| 353 | my( $w, $ent, $image ) = @_; | ||
| 354 | my $data = $w->privateData(); | ||
| 355 | if (@_ > 2) | ||
| 356 | { | ||
| 357 | if (defined $image) | ||
| 358 | { | ||
| 359 | $w->indicatorCreate( $ent, -itemtype => 'image' ) | ||
| 360 | unless $w->indicatorExists($ent); | ||
| 361 | $data->{$ent} = $image; | ||
| 362 | my $bmp; | ||
| 363 | if ($image eq 'minus') { | ||
| 364 | $bmp = $w->cget('-indicatorminusimg'); | ||
| 365 | } elsif ($image eq 'plus') { | ||
| 366 | $bmp = $w->cget('-indicatorplusimg'); | ||
| 367 | } else { | ||
| 368 | $bmp = $w->$w->Getimage( $image ) | ||
| 369 | } | ||
| 370 | $w->indicatorConfigure( $ent, -image => $bmp ); | ||
| 371 | } | ||
| 372 | else | ||
| 373 | { | ||
| 374 | $w->indicatorDelete( $ent ) if $w->indicatorExists( $ent ); | ||
| 375 | delete $data->{$ent}; | ||
| 376 | } | ||
| 377 | } | ||
| 378 | return $data->{$ent}; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub LeftRight { | ||
| 382 | my ($self, $spec) = @_; | ||
| 383 | my ($sel) = $self->infoSelection; | ||
| 384 | if ((defined $sel) and $self->infoChildren($sel)) { | ||
| 385 | if ($spec eq 'left') { | ||
| 386 | $self->Activate($sel, 'close'); | ||
| 387 | } elsif ($spec eq 'right') { | ||
| 388 | $self->Activate($sel, 'open'); | ||
| 389 | } | ||
| 390 | $self->Callback('-leftrightcall', $spec, $sel); | ||
| 391 | } | ||
| 392 | $self->SUPER::LeftRight($spec) | ||
| 393 | } | ||
| 394 | |||
| 395 | =head1 AUTHOR | ||
| 396 | |||
| 397 | Hans Jeuken (hanje at cpan dot org) | ||
| 398 | |||
| 399 | =head1 LICENSE | ||
| 400 | |||
| 401 | Same as perl. | ||
| 402 | |||
| 403 | =head1 BUGS | ||
| 404 | |||
| 405 | Unknown. If you find any, please contact the author. | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | 1; | ||
| 410 | |||
| 411 | __END__ |