use strict; use Tcl::Tk; my $status = ''; my $mw = Tcl::Tk::MainWindow("Simple Drag & Drop Demo..."); # label widget that will show status my $lab_stat = $mw->Label(-textvariable=>\$status, qw/-relief sunken -bd 1 -width 60 -anchor w/)->pack(qw/-side bottom -fill x/); # three more widgets that will be used for trying DND my $lab_source = $mw->Label(-text=>"source", qw/-relief groove -bd 2 -width 20/) ->pack(-pady=>5); my $lab_text_plain = $mw->Label(-text=>"drop plain text", qw/-relief raised -bd 1 -width 20/)->pack(-pady=>5); my $lab_t2 = $mw->Label(-text=>"drop color", qw/-relief raised -bd 1 -width 20 -bg lightyellow/)->pack(-pady=>5); # and a frame $mw->Frame(-height=>10)->pack(qw/-side bottom -fill x/); # following line will do 'package require tkdnd' in Tcl/Tk $mw->interp->need_tk('tkdnd'); # in order to make DND syntax shorter let's define following methods sub Tcl::Tk::Widget::dndBindsource { my $w = shift; $w->interp->call('dnd','bindsource',$w,@_); } sub Tcl::Tk::Widget::dndBindtarget { my $w = shift; $w->interp->call('dnd','bindtarget',$w,@_); } # could be done without above two methods that go to Tcl::Tk package: # $int->call('dnd','bindsource',$widget->path,.....); # but now we will be allowed to shorter # $widget->dndBindsource(....); #--------------------------------------------------------------------- # Before registering a drop target, we always have to make sure the # corresponding widget has been created: $mw->update('idle'); # tells the DND protocol source can deliver textual data $lab_source->dndBindsource('text/plain', q{return "testing DND"}); # bind the DND operation on left button $lab_source->bind('<1>', 'dnd drag %W'); # tells the DND protocol target can handle textual data $lab_text_plain->dndBindtarget('text/plain', '', \\'WDTA', sub { my (undef,$int,$sub) = (shift,shift,shift); my ($W,$D,$T,$A) = (@_); # Ev vars my $receiver_widget = $int->widget($W); $receiver_widget->configure(-text=>$D); $status = "[target1] type='$T', action='$A'"; $int->after(2000, sub { $receiver_widget->configure(-text=>"drop plain text") }); }); # defines an other type on source $lab_source->dndBindsource('TK_COLOR', q{return "pink"}); # tells the DND protocol target can handle color data $lab_t2->dndBindtarget('TK_COLOR', '', , \\'WDTA', sub { my (undef,$int,$sub) = (shift,shift,shift); my ($W,$D,$T,$A) = (@_); # Ev vars $lab_t2->configure(-bg=>$D); $status = "[target1] type='$T', action='$A'"; $int->after(2000, sub {$lab_t2->configure(-bg=>'lightyellow')}); }); Tcl::Tk::MainLoop;