#!/usr/local/bin/perl -w
use Tk;
use strict;

my $mw = MainWindow->new();
my $seln;

my $tb = $mw->Frame->pack(-fill => 'x');

my $lb = $mw->Scrolled('Listbox',-width => 40, -exportselection => 0);


$tb->Button('-text' => "Primary Targets", '-command' => [\&ShowTargets,$lb,'PRIMARY'])->pack('-side'=>'left');
$tb->Button('-text' => "Clipboard Targets", '-command' => [\&ShowTargets,$lb,'CLIPBOARD'])->pack('-side'=>'left');
$tb->Label('-textvariable' => \$seln)->pack('-side'=>'left');
$tb->Button('-text' => "Quit", '-command' => ['destroy',$mw])->pack('-side'=>'right');

$lb->packAdjust(-expand => 1, -fill => 'both');
my $tx = $mw->Scrolled('Text',-width => 40, -exportselection => 0, -wrap => 'none')->pack(-expand => 1, -fill => 'both');

$lb->bind('<ButtonRelease-1>',[\&GetSelected,$tx]);

ShowTargets($lb,'PRIMARY');

MainLoop;

sub GetSelected
{
 my ($lb,$tx) = @_;
 my $name = $lb->Getselected;
 if (defined $name)
  {
   my @targ = $lb->SelectionGet('-selection'=>$seln,$name);
   foreach (@targ)
    {
     $tx->insert('end',"$_\n");
    }
   $tx->see('end');
  }
}

sub ShowTargets
{
 my $lb = shift;
 $seln = shift;
 my $own =  $lb->SelectionExists('-selection'=>$seln);
 if ($own)
  {
   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";
      }
    }
  }
}


