use strict; use Tk; require Tk::DirTree; require Tk::DragDrop; require Tk::DropSite; require Tk::LabFrame; my $CalTreeWidth = 30; my $CalTreeHeight = 20; my $selectionWidth = 30; my $selectionHeight = 8; my $p1 = MainWindow->new(); my ( $CalTree, $listbox, ); my $leftFrame = $p1->LabFrame(-label=>"System Configuration", -labelside=>"acrosstop") ->pack(-side=>"left",-fill=>'y', -anchor=>"nw"); { my $CalTreeScrollBarY = $leftFrame->Scrollbar(); my $CalTreeScrollBarX = $leftFrame->Scrollbar(-orient=>"horiz"); $CalTree = $leftFrame->Tree(-yscrollcommand=>['set',$CalTreeScrollBarY], -xscrollcommand=>['set',$CalTreeScrollBarX], -width=>$CalTreeWidth, -height=>$CalTreeHeight, -drawbranch=>1, -selectmode=>"extended", -command=>\&addSelected); $CalTreeScrollBarY->configure(-command=> [yview=>$CalTree]); $CalTreeScrollBarX->configure(-command=> [xview=>$CalTree]); $CalTree->DragDrop(-event => '', -text => "", -sitetypes => qw(Local)); $CalTree->DropSite(-droptypes => qw(Local), -dropcommand => [\&removeSelected]); $CalTreeScrollBarX->pack(-side=>"bottom",-fill=>"x"); $CalTree->pack(-side=>"left",-fill=>"both"); $CalTreeScrollBarY->pack(-side=>"left",-fill=>"y"); } { my $selFrame = $p1->LabFrame(-label=>"Selected Items", -labelside=>"acrosstop") ->pack(-fill=>"y", -expand=>1); my $listboxScrollBarY = $selFrame->Scrollbar(); my $listboxScrollBarX = $selFrame->Scrollbar(-orient=>"horiz"); $listbox = $selFrame->Tree(-width=>$selectionWidth,-height=>$selectionHeight,-drawbranch=>1, -command=>\&removeSelected,-selectmode=>"extended", -yscrollcommand=>['set',$listboxScrollBarY], -xscrollcommand=>['set',$listboxScrollBarX] ); $listboxScrollBarY->configure(-command=> [yview=>$listbox]); $listboxScrollBarX->configure(-command=> [xview=>$listbox]); $listbox->DropSite(-droptypes => qw(Local), -dropcommand => [\&addSelected]); $listbox->DragDrop(-event => '', -text => "", -sitetypes => qw(Local)); $listboxScrollBarX->pack(-side=>"bottom",-fill=>"x"); $listbox->pack(-side=>"left",-fill=>"both"); $listboxScrollBarY->pack(-side=>"right",-fill=>"y"); } $CalTree->add("parent", -text=>"parent1111"); $CalTree->addchild("parent", -text=>"child1"); $CalTree->addchild("parent.0", -text=>"grandchild1"); $CalTree->addchild("parent.0", -text=>"grandchild2"); $CalTree->addchild("parent", -text=>"child2"); $CalTree->addchild("parent.1", -text=>"grandchild1"); $CalTree->addchild("parent.1", -text=>"grandchild2"); $CalTree->autosetmode(); MainLoop; sub addSelected { print "InterncalCal.pm: addSelected()\n"; foreach ($CalTree->info("selection")) { print " Found selected: " . $CalTree->entrycget($_,"-text") . "\n"; my $item = $CalTree->entrycget($_,"-text"); if ($CalTree->info("parent",$_) eq ""){ # has no parent, so selection must have been 'root'. In other words, 'select all' foreach($CalTree->info("children", $_)){ my $parent = $CalTree->entrycget($_,"-text"); next if ($CalTree->entrycget($_,"-state") eq "disabled"); if (!$listbox->info("exists",$parent)){ $listbox->add("$parent", -text=>$parent); # if parent does not exist, create him first } foreach($CalTree->info("children", $_)){ my $no = 0; my $item = $CalTree->entrycget($_,"-text"); foreach($listbox->info("children", $parent)){ $no++ if ($listbox->entrycget($_,"-text") eq $item); #if both parent and child exist, no need to add again } if (!$no){ $listbox->addchild($parent,-text=>$item); # add item if does not already exists } } } } else { my $parent = $CalTree->entrycget($CalTree->info("parent",$_), "-text"); if ($parent =~ /\d{4}/){ # since we are not tracking from root (which contains 4 digits, this item should be at the root of the selection list if (!$listbox->info("exists",$item)){ # add item to list if does not exist $listbox->add("$item", -text=>$item); } foreach my $child ($CalTree->info('children',$_)){ #add all of its children as well my $no = 0; my $childname = $CalTree->entrycget($child,"-text"); foreach ($listbox->info('children',$item)){ $no++ if ($listbox->entrycget($_,"-text") eq $childname); } if (!$no){ $listbox->addchild($item,-text=>$childname); } } } else { if ($listbox->info("exists",$parent)){ foreach($listbox->info("children", $parent)){ return if ($listbox->entrycget($_,"-text") eq $item); #if both parent and child exist, no need to add again } } else { $listbox->add("$parent", -text=>$parent); # if parent does not exist, create him first } $listbox->addchild($parent,-text=>$item); # add item } } } $listbox->autosetmode(); } sub removeSelected { print "InterncalCal.pm: removeSelected()\n"; my @toplevel; foreach ($listbox->info("selection")){ print " Found selected: " . $listbox->entrycget($_,"-text") . "\n"; # don't remove the top level ones until all the selected items underneath have been removed # this is a hack !!! if ($listbox->entrycget($_,"-text") !~ /\s/){ push @toplevel, $_; next; } $listbox->delete("entry",$_); } $listbox->delete("entry",$_) foreach(@toplevel); $listbox->autosetmode(); }