#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;
use vars qw($dr_reel $window);
$dr_reel="";    # surface de dssin
$window="";     # surface de dssin
use warnings;
package Image;
use Commun;
our @ISA=qw( Commun );
our %liste;
sub getListe {    # rewrite : try to put in Commun.pm if possible.
    my $self=shift;
    return %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});
    }
    bless($self, $classe);
    if (%param) {
        $self->configure(%param);
    }    # rewrite best manage param new/configure
    $self->draw();
    $liste{$self->id}=$self;
    return $self;
}
sub draw {
}
sub configure {
    my $self=shift;
    my %param=@_;
    $self->{X}=$param{"-x"} || 0;
    $self->{Y}=$param{"-y"} || 0;
    my $name=$self->{NAME}=$param{"-name"};
    if (-e $name) {
        my $image=$main::window->Photo(-file => $name);
        $self->{DRAW}=
          $::drawing->createImage($self->{X}, $self->{Y}, -image => $image);
        $::dr_reel->lower($self->{DRAW}, "grille");
    }    # fixme-m if not, erase old image. And also, delete object neither use
}
sub x {
    my $self=shift;
    if (@_) {$self->{X}=shift}
    $::drawing->coords($self->{DRAW}, $self->{X}, $self->{Y});
    return $self->{X};
}
sub y {
    my $self=shift;
    if (@_) {$self->{Y}=shift}
    $::drawing->coords($self->{DRAW}, $self->{X}, $self->{Y});
    return $self->{Y};
}
sub name {
    my $self=shift;
    if (@_) {$self->{NAME}=shift}
    return $self->{NAME};
}
sub hidden {
    foreach my $liste (%liste) {
        if (ref($liste) eq "Image") {
            $::drawing->itemconfigure($liste->{DRAW}, -state => "hidden");
        }
    }
}
sub normal {
    foreach my $liste (%liste) {
        if (ref($liste) eq "Image") {
            $::drawing->itemconfigure($liste->{DRAW}, -state => "normal");
        }
    }
}
sub deselectionne {
}
sub redraw {
}
sub supprime {
    my $self=shift;
    delete($liste{$self->id});
    delete($::liste{$self->{DRAW}});
    print "supprime de" . $self;
}
sub print {
    my $self=shift;
    return
"-type Image -id $self->{ID} -name $self->{NAME} -x $self->{X} -y $self->{Y}\n";
}
sub DESTROY {
    my $self=shift;
    $self->undoCreate;
    $self->SUPER::DESTROY();
    $self->deselectionne;
    $::drawing->delete($self->{DRAW});
    print "destroy de" . $self;
}
1;
