#KanDeLa.
#Copyright (C) 2011 Alain Barbason
#
#This file is part of KanDeLa.
#
#KanDeLa is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#KanDeLa is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with KanDeLa.  If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use warnings;
package Handle;
use Commun;
our @ISA=qw( Commun );
use Data::Dumper;
our %liste;
sub new {
    my $classe=shift;
    my $self=$classe->SUPER::new();
    my %param=@_;
    if ($param{"-id"}) {
        $self->{ID}=$param{"-id"};
        Commun::set_id($self->{ID});
    }
    my $x=$self->{X}=$param{"-x"};
    my $y=$self->{Y}=$param{"-y"};
    $self->{HANDLE}=
      $::drawing->createRectangle($x-5, $y-5, $x+5, $y+5, -fill => "grey",);
    bless($self, $classe);
    $liste{$self->id}=$self;
    $::liste{$self->{HANDLE}}=$self;
    #print Dumper $self;
    return $self;
}
sub configure {
    my $self=shift;
    my $key=uc(substr(shift, 1));
    $self->{$key}=shift;
}
sub tk_id {
    my $self=shift;
    return $self->{HANDLE};
}
sub x {
    my $self=shift;
    if (@_) {$self->{X}=shift}
    return $self->{X};
}
sub y {
    my $self=shift;
    if (@_) {$self->{Y}=shift}
    return $self->{Y};
}
sub select {
    my $self=shift;
    return $self->{SELECT};
}
sub draw {
    my $self=shift;
    $self->redraw(@_);
}
sub redraw {
    my $self=shift;
    my $x=$self->{X};
    my $y=$self->{Y};
    $::drawing->coords($self->{HANDLE}, $x-5, $y-5, $x+5, $y+5);
}
sub selectionne {
    my $self=shift;
    $self->{SELECT}=1;
    $self->redraw();
    # $::sc8->pack;
}
sub deselectionne {
    my $self=shift;
    if ($self->{SELECT}==1) {
        $self->{SELECT}=0;
        $self->redraw();
    }
}
sub print {
    my $self=shift;
    # handle  don't save
}
sub supprime {
    my $self=shift;
    delete($liste{$self->id});
    delete($::liste{$self->{HANDLE}});
}
sub DESTROY {
    my $self=shift;
    $self->SUPER::DESTROY();
    $self->deselectionne;
    $::drawing->delete($self->{HANDLE});
    #print "destroy de".$self;
}
1;
