The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/local/bin/perl -w
use Tk;
use strict;
use Tk::DropSite;
use vars qw($kind);           
BEGIN  { $kind = ($^O eq 'MSWin32') ? ['Win32'] : ['Sun','XDND','KDE'] }
use Tk::DropSite @$kind;
use Tk::DragDrop @$kind;
use Tk::Menubar;  

sub enter_leave
{          
 my ($ds,$flag) = @_;
 $ds->configure(-relief => ($flag) ? 'sunken' : 'ridge');
 return 1;
}

my $top = MainWindow->new();
my $mb  = $top->Menubar;
$mb->Menubutton(-text => '~File', -menuitems => [
                [ Button => 'E~xit', -command => [ destroy => $top]],
                ]);

my $but = $top->Frame->pack;

my $lb = $top->Scrolled('Listbox',-width => 40)->pack(-side => 'bottom');

$but->Button('-text' => "Primary Targets", '-command' => [\&ShowTargets,$lb,'PRIMARY'])->pack('-side'=>'left');
$but->Button('-text' => "Quit", '-command' => ['destroy',$top])->pack('-side'=>'right');

my $filename = "/tmp/SomethingSilly";

open(FOO,">$filename");
print FOO "Hi There\n";
close(FOO);

my $src = $top->Frame->pack;
foreach my $k ('Local',@$kind,'Any')
 {
  # next if $k eq 'KDE';
  my $thing = $src->Message('-text' => "$k Source")->pack(-side => 'left');
  my @list;
  @list = (-sitetypes => $k) unless $k eq 'Any';
  $thing->DragDrop(-event => '<B1-Motion>', @list,
                 -handlers => [
                   [-type => 'text/plain',[\&string_handler,"This is text/plain"]],
                   [-type => 'FILE_NAME',[\&string_handler,$filename]],
                   [[\&string_handler,"This is STRING"]]
                 ]);
 }


my $dst = $top->Frame->pack;
foreach my $k ('Local',@$kind,'Any')
 {
  my @list;
  @list = (-droptypes => $k) unless $k eq 'Any';
  my $sun = $dst->Message('-text' => "$k Drops here", '-relief' => 'ridge' )->pack(-side => 'left');
  $sun->DropSite(-dropcommand => [\&ShowTargets,$lb],
                 -entercommand => [\&enter_leave,$sun], 
                 @list);
 }


sub string_handler
{
 my ($text,$offset,$max) = @_;
 return substr($text,$offset,$max);
}

MainLoop;

use Data::Dumper;

sub ShowTargets
{
 my $lb = shift;
 my $seln = shift;
 my $own =  $lb->SelectionExists('-selection'=>$seln);
 printf "owner of $seln is %x\n",$own;
 my @targ = $lb->SelectionGet('-selection'=>$seln,'TARGETS');
 $lb->delete(0,'end');
 $lb->insert('end',@targ);
 foreach (@targ)
  {
   if (/FILE_NAME/)
    {
     print "$_:",$lb->SelectionGet('-selection'=>$seln,'FILE_NAME'),"\n";
    }
   if (/_SUN/ && !/(END|ACK|DONE|YIELD)/)
    {       
     my @info;
     Tk::catch { @info  = $lb->SelectionGet('-selection'=>$seln,$_) };
     if ($@)
      {
       print "Cannot get $_\n";
      }
     else
      {
       print "$_:",Dumper(\@info);
      }
    }
   if ($^O eq 'MSWin32' && /STRING/)
    {
     print "$_:",$lb->SelectionGet('-selection'=>$seln,$_),"\n";
    } 
  }
}