| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Perl/Tk version of Tix4.1.0/demos/samples/DynTree.tcl. |
| 4 |
# |
| 5 |
# Chris Dean <[email protected]> |
| 6 |
|
| 7 |
use lib ".."; |
| 8 |
|
| 9 |
use strict; |
| 10 |
use Tk; |
| 11 |
use Tk::Tree; |
| 12 |
|
| 13 |
my $top = new MainWindow( -title => "Tree (Dynamic)" ); |
| 14 |
|
| 15 |
my $tree = $top->ScrlTree( qw(-separator / -width 35 -height 25 |
| 16 |
-scrollbars osoe) ); |
| 17 |
$tree->pack( qw/-expand yes -fill both -padx 10 -pady 10 -side top/ ); |
| 18 |
|
| 19 |
$tree->configure( -opencmd => sub { dyntree_opendir( $tree, @_ ); } ); |
| 20 |
|
| 21 |
# Add the root directory the TixTree widget |
| 22 |
dyntree_adddir( $tree, "/" ); |
| 23 |
|
| 24 |
# The / directory is added in the "open" mode. The user can open it |
| 25 |
# and then browse its subdirectories ... |
| 26 |
|
| 27 |
my $ok = $top->Button( qw/-text Ok -underline 0 -width 6/, |
| 28 |
-command => sub { exit } ); |
| 29 |
my $cancel = $top->Button( qw/-text Cancel -underline 0 -width 6/, |
| 30 |
-command => sub { exit } ); |
| 31 |
|
| 32 |
$ok->pack( qw/-side left -padx 10 -pady 10/ ); |
| 33 |
$cancel->pack( qw/-side right -padx 10 -pady 10/ ); |
| 34 |
|
| 35 |
MainLoop(); |
| 36 |
|
| 37 |
sub dyntree_adddir { |
| 38 |
my( $tree, $dir ) = @_; |
| 39 |
|
| 40 |
my $text = $dir; |
| 41 |
$text = (split( "/", $dir ))[-1] unless $text eq "/"; |
| 42 |
|
| 43 |
$tree->add( $dir, -text => $text, -image => $tree->Getimage("folder") ); |
| 44 |
$tree->setmode( $dir, -d $dir ? "open" : "none" ); |
| 45 |
} |
| 46 |
|
| 47 |
# This command is called whenever the user presses the (+) indicator or |
| 48 |
# double clicks on a directory whose mode is "open". It loads the files |
| 49 |
# inside that directory into the Tree widget. |
| 50 |
# |
| 51 |
# Note we didn't specify the -closecmd option for the Tree widget, so it |
| 52 |
# performs the default action when the user presses the (-) indicator or |
| 53 |
# double clicks on a directory whose mode is "close": hide all of its child |
| 54 |
# entries |
| 55 |
# |
| 56 |
sub dyntree_opendir { |
| 57 |
my( $tree, $dir ) = @_; |
| 58 |
|
| 59 |
if( my @kids = $tree->infoChildren( $dir ) ) { |
| 60 |
# We have already loaded this directory. Let's just |
| 61 |
# show all the child entries |
| 62 |
# |
| 63 |
# Note: since we load the directory only once, it will not be |
| 64 |
# refreshed if the you add or remove files from this |
| 65 |
# directory. |
| 66 |
# |
| 67 |
foreach my $kid (@kids) { |
| 68 |
$tree->show( -entry => $kid ); |
| 69 |
} |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
my $image = $tree->Getimage("folder"); |
| 74 |
|
| 75 |
opendir D, $dir or return; |
| 76 |
foreach my $file (sort readdir( D )) { |
| 77 |
next if $file =~ /^\./; |
| 78 |
my $path = "$dir/$file"; |
| 79 |
$path = "/$file" if $dir eq "/"; |
| 80 |
if( -d $path ) { |
| 81 |
dyntree_adddir( $tree, $path ); |
| 82 |
} else { |
| 83 |
$tree->add( $path, -text => $file, -image => $image ); |
| 84 |
} |
| 85 |
} |
| 86 |
closedir D; |
| 87 |
} |
| 88 |
|