Hi!
use Tk;
my $top = new MainWindow;
my $c = $top->Canvas()->pack();
my ($x,$y);
# left mouse-button -> create text
$top->bind('<1>' => [\&click_left, Ev('x'),Ev('y')]);
# shift and left mouse-button -> edit text
$top->bind('<Shift-Button-1>' => [\&edit, Ev('x'),Ev('y')]);
# right mouse-button -> move the text
$top->bind('<3>' => [\&move_it, Ev('x'),Ev('y')]);
MainLoop;
sub click_left {
my $e = shift;
($x,$y) = @_;
my $t = $top->Toplevel();
my $entry = $t->Entry()->pack();
$entry->focus();
$t->bind('<Return>' =>sub{
my $text = $entry->get();
$c->createText($x,$y,-text=>$text);
$t->destroy();
});
}
sub move_it {
my $e = shift;
($x,$y) = @_;
my $id = $c->find('closest',$x,$y);
$top->bind('<Button3-Motion>' => [\&move, Ev('x'),Ev('y'),$id]);
}
sub move {
my ($e,$xm,$ym,$id) = @_;
my $dx = $xm - $x;
my $dy = $ym - $y;
($x,$y) = ($x+$dx, $y+$dy);
$c->move($id,$dx,$dy);
}
sub edit {
my $e = shift;
($x,$y) = @_;
my $id = $c->find('closest',$x,$y);
my $text = $c->itemcget($id,'text');
my $t = $top->Toplevel(-title=>"edit");
my $entry = $t->Entry()->pack();
$entry->insert(0,$text);
$entry->focus();
$t->bind('<Return>' => sub {
my $text = $entry->get();
$c->delete($id);
$c->createText($x,$y,-text=>$text);
$t->destroy();
});
}
I'm sure this is not the best way, but it should do what you want :-)
mawe