home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Tk / DropSite.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  4.2 KB  |  243 lines

  1. package Tk::DropSite;
  2. require Tk::DragDrop::Common;
  3. require Tk::DragDrop::Rect;
  4. @ISA = qw(Tk::DragDrop::Common Tk::DragDrop::Rect);
  5.  
  6. Construct Tk::Widget 'DropSite';
  7.  
  8. use strict;
  9. use vars qw(%type @types);
  10.  
  11. Tk::DragDrop->Tk::DragDrop::Common::Type('Local');
  12.  
  13. my @toplevels;
  14.  
  15. BEGIN                  
  16. {
  17.  my $name;
  18.  no strict 'refs';
  19.  foreach $name (qw(x y X Y width height widget))
  20.   {
  21.    my $key = $name;
  22.    *{"$key"} = sub { shift->{$key} };
  23.   }
  24. }
  25.  
  26. sub CheckSites
  27. {
  28.  my ($class,$token) = @_;
  29. }
  30.  
  31. sub SiteList
  32. {
  33.  my ($class,$widget) = @_;
  34.  my $t;
  35.  my @list;
  36.  foreach $t (@toplevels)
  37.   {
  38.    my $sites = $t->{'DropSites'};
  39.    if ($sites)
  40.     {
  41.      $sites = $sites->{'Local'};
  42.      push(@list,@{$sites}) if ($sites);
  43.     }
  44.   }
  45.  return @list;
  46. }
  47.  
  48. sub XY
  49. {
  50.  my ($site,$event) = @_;
  51.  return ($event->X - $site->X,$event->Y - $site->Y);
  52. }
  53.  
  54. sub Apply
  55. {
  56.  my $site = shift;
  57.  my $name = shift;
  58.  my $cb   = $site->{$name};
  59.  if ($cb)
  60.   {
  61.    my $event = shift;
  62.    $cb->Call(@_,$site->XY($event));
  63.   }
  64. }
  65.  
  66. sub Drop
  67. {
  68.  my ($site,$win,$seln,$event) = @_;
  69.  $site->SUPER::Drop($win,$seln,$event);
  70.  $site->Apply(-dropcommand => $event, $seln);
  71. }
  72.  
  73. sub Enter
  74. {
  75.  my ($site,$token,$event) = @_;
  76.  $site->SUPER::Enter($token,$event);
  77.  $site->Apply(-entercommand => $event, 1);
  78. }
  79.  
  80. sub Leave
  81. {
  82.  my ($site,$token,$event) = @_;
  83.  $site->SUPER::Leave($token,$event);
  84.  $site->Apply(-entercommand => $event, 0);
  85. }
  86.  
  87. sub Motion
  88. {
  89.  my ($site,$token,$event) = @_;
  90.  $site->SUPER::Motion($token,$event);
  91.  $site->Apply(-motioncommand => $event);
  92. }
  93.  
  94. sub NoteSites
  95. {
  96.  my ($class,$t,$sites) = @_;
  97.  unless (grep($_ == $t,@toplevels))
  98.   {
  99.    $Tk::DragDrop::types{'Local'} = $class if (@$sites);
  100.    push(@toplevels,$t);
  101.    $t->OnDestroy(sub { @toplevels = grep($_ != $t,@toplevels) });
  102.   }
  103. }
  104.  
  105. sub UpdateDropSites
  106. {
  107.  my ($t) = @_;
  108.  my $type;
  109.  foreach $type (@types)
  110.   {
  111.    my $sites = $t->{'DropSites'}->{$type};
  112.    if ($sites && @$sites)
  113.     {
  114.      my $class = $type{$type};
  115.      $class->NoteSites($t,$sites);
  116.     }
  117.   }
  118. }
  119.  
  120. sub QueueDropSiteUpdate
  121. {
  122.  my $obj = shift;
  123.  my $class = ref($obj);
  124.  my $t   = $obj->widget->toplevel;
  125.  unless ($t->{'DropUpdate'})
  126.   {
  127.    $t->afterIdle(sub { UpdateDropSites($t) });
  128.   }
  129. }
  130.  
  131. sub delete
  132. {
  133.  my ($obj) = @_;
  134.  my $w = $obj->widget;
  135.  $w->bindtags([grep($_ ne $obj,$w->bindtags)]);
  136.  my $t = $w->toplevel;
  137.  my $type;
  138.  foreach $type (@{$obj->{'-droptypes'}})
  139.   {
  140.    my $a = $t->{'DropSites'}->{$type};
  141.    @$a    = grep($_ ne $obj,@$a);
  142.   }
  143.  $obj->QueueDropSiteUpdate;
  144. }
  145.  
  146. sub DropSiteUpdate
  147. {
  148.  my $obj = shift;
  149.  my $w   = $obj->widget;
  150.  $obj->{'x'}      = $w->X;
  151.  $obj->{'y'}      = $w->Y;
  152.  $obj->{'X'}      = $w->rootx;
  153.  $obj->{'Y'}      = $w->rooty;
  154.  $obj->{'width'}  = $w->Width;
  155.  $obj->{'height'} = $w->Height;
  156.  $obj->QueueDropSiteUpdate;
  157. }
  158.  
  159. sub TopSiteUpdate
  160. {
  161.  my ($t) = @_;
  162.  my $type;
  163.  foreach $type (@types)
  164.   {
  165.    my $sites = $t->{'DropSites'}->{$type};
  166.    if ($sites && @$sites)
  167.     {
  168.      my $site;
  169.      foreach $site (@$sites)
  170.       {
  171.        $site->DropSiteUpdate;
  172.       }
  173.     }
  174.   }
  175. }
  176.  
  177. sub Callback
  178. {
  179.  my $obj = shift;
  180.  my $key = shift;
  181.  my $cb  = $obj->{$key};
  182.  $cb->Call(@_) if (defined $cb);
  183. }
  184.  
  185. sub InitSite
  186. {
  187.  my ($class,$site) = @_;
  188.  # Tk::DragDrop->Type('Local');
  189. }
  190.  
  191. sub new
  192. {
  193.  my ($class,$w,%args) = @_;
  194.  my $t = $w->toplevel;
  195.  $args{'widget'} = $w;
  196.  if (exists $args{'-droptypes'})
  197.   {
  198.    $args{'-droptypes'} = [$args{'-droptypes'}] unless (ref $args{'-droptypes'});
  199.   }
  200.  else
  201.   {
  202.    $args{'-droptypes'} = \@types; 
  203.   }
  204.  my ($key,$val);
  205.  while (($key,$val) = each %args)
  206.   {
  207.    if ($key =~ /command$/)
  208.     {
  209.      $val = Tk::Callback->new($val);
  210.     }
  211.   }
  212.  my $obj = bless \%args,$class;
  213.  unless (exists $t->{'DropSites'})
  214.   {
  215.    $t->{'DropSites'} = {}; 
  216.    $t->{'DropUpdate'} = 0; 
  217.   }
  218.  my $type;
  219.  foreach $type (@{$args{'-droptypes'}})
  220.   {
  221.    Tk::DropSite->import($type) unless (exists $type{$type});
  222.    my $class = $type{$type};
  223.    $class->InitSite($obj);
  224.    unless (exists $t->{'DropSites'}->{$type})
  225.     {
  226.      $t->{'DropSites'}->{$type}  = [];
  227.     }
  228.    push(@{$t->{'DropSites'}->{$type}},$obj);
  229.   }
  230.  $w->OnDestroy([$obj,'delete']);
  231.  $obj->DropSiteUpdate;
  232.  $w->bindtags([$w->bindtags,$obj]);
  233.  $w->Tk::bind($obj,'<Configure>',[$obj,'DropSiteUpdate']);
  234.  $t->Tk::bind($class,'<Configure>',[\&TopSiteUpdate,$t]);
  235.  unless (grep($_ eq $class,$t->bindtags))
  236.   {
  237.    $t->bindtags([$t->bindtags,$class]);
  238.   }
  239.  return $obj;
  240. }
  241.  
  242. 1;
  243.