#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Tk;
require Tk::HList;
my $top = MainWindow->new(-title=>"FileSelector");
$top->geometry("300x500");
my $h = $top->Scrolled('HList',
-drawbranch => 1,
-separator => '/',
-indent => 15,
-command => \&show_or_hide_dir,
-scrollbars => 'osoe',
)
->pack(-fill => 'both',
-expand => 'y',
);
my $xpm_p = <<'ENDE';
/* XPM */
static char *test[]={
"12 12 2 1",
". c None",
"# c #000000",
".....##.....",
".....##.....",
".....##.....",
".....##.....",
".....##.....",
"############",
"############",
".....##.....",
".....##.....",
".....##.....",
".....##.....",
".....##....."};
ENDE
my $xpm_m = <<'ENDE';
/* XPM */
static char *test[]={
"12 12 2 1",
". c None",
"# c #000000",
"............",
"............",
"............",
"............",
"............",
"############",
"############",
"............",
"............",
"............",
"............",
"............"};
ENDE
my %icons;
#$icons{"open"} = $top->Bitmap(-file => './open_folder.xbm');
#$icons{"closed"} = $top->Bitmap(-file => './folder.xbm');
#$icons{"open"} = $top->Pixmap(-data => $xpm);
#$icons{"closed"} = $top->Pixmap(-data => $xpm);
$icons{"open"} = $top->Pixmap(-data => $xpm_m);
$icons{"closed"} = $top->Pixmap(-data => $xpm_p);
#show_or_hide_dir("c:\\programme");
show_or_hide_dir(@ARGV);
#show_or_hide_dir("p:");
#show_or_hide_dir("/temp");
MainLoop();
#-----------------------------------------------------------------------
sub show_or_hide_dir {
my $path = $_[0];
print "PATH:".$path;
return if (! -d $path);
if ($h->info('exists', $path)) {
my $next_entry = $h->info('next', $path);
if (!$next_entry || (index ($next_entry, "$path/") == -1)) {
$h->entryconfigure($path, '-image' => $icons{"open"});
add_dir_contents($path);
my $path1 = $path;
my $path2 = $path;
my @du = ("du", "-b", "-h", $path1, $path2);
my $dirSize = system(@du) == 0 or die "system @du failed: $?";
}
else {
$h->entryconfigure($path, '-image' => $icons{"closed"});
$h->delete('offsprings', $path);
}
}
else {
die "'$path' is not a directory\n" if (! -d $path);
$h->add($path,
-itemtype => 'imagetext',
-image => $icons{"open"},
-text => $path,
);
add_dir_contents($path);
}
}
sub add_dir_contents {
my $path = $_[0];
my $oldcursor = $top->cget('-cursor');
$top->configure(-cursor => 'watch');
$top->update();
my @files = <$path/*>;
foreach my $file (@files) {
$file =~ s|//|/|g;
#$file =~ /^\s*$/;
print "FILE:".$file."\n\n";
(my $text = $file) =~ s|^.*/||g;
if (-d $file) {
$h->add($file,
-itemtype => 'imagetext',
-image => $icons{"closed"},
-text => $text,
);
}
else {
$h->add($file,
-itemtype => 'text',
-text => $text,
);
}
}
$top->configure(-cursor => $oldcursor);
} # sub add_dir_contents