package MTools::MSwitch; # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU LGPL Libray General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program 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 Library General Public License for more details. # # You should have received a copy of the GNU Library General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, # or refer to http://www.gnu.org/copyleft/lgpl.html # ################################################################## # Le composant MSwitch permet de mat?rialiser et contr?ler des "?tat" actif de l'application. # Le principe est d'associer des composants (graphiques, abstraits, comportements, objets complexes) ? un ?tat. # Le composant MSwitch contr?le un ?tat actif de l'application et g?re l'option "-visible" des composants associ?s ? l'?tat. # # Les param?tres : # * $parent : parent de l'objet MSwitch # * %etats : table de hash d?finissant les ?tats de l'application # Ex : # etat1 => [], # ?tat ne comportant aucun composant # etat2 => 'fichier_svg.svg#tag_svg', # l'?tat va instancier un objet issu d'un fichier svg # etat3 => ['fichier_svg.svg#tag_svg', $obj], # l'?tat va contenir deux objets: l'un instancier par le MSwitch l'autre d?j? cr?? pr?alablement # Les propi?t?s : # * state : contr?le l'?tat actif du MSwitch # * -visible : permettre de rendre le MSwitch visible ou invisible et en cons?quence de rendre # "visible ou invisible" les composants de l'?tat actif du switch # Les fonctions public : # * setState : $switch -> setState ('etat') ?quivalent ? $swicth -> mconfigure (state => 'etat') use strict; use MTools::SVG::SVGLoader; use vars qw /@ISA/; BEGIN { @ISA = qw /MTools::GUI::MGroup/; } sub new { my ($class, $parent, %etats) = @_; my $self = new MTools::GUI::MGroup ($parent); bless $self, $class; $self -> recordProperty ('state', ''); $self -> recordProperty ('-visible', 1); $self -> {__last} = ''; $self -> plisten ('state', [$self, '__setState']); my $first = ''; while ( my ($etat, $val) = each (%etats) ) { if ($first eq '') { $first = $etat; } my @tb; if (ref ($val) eq 'ARRAY') { @tb = @{$val}; } else { $tb[0] = $val; } for (my $i = 0; $i < @tb; $i ++) { if ( !defined $self -> {"__instance_".$tb [$i]}) { if ( ref ($tb [$i]) eq '') { $self -> {"__instance_".$tb [$i]} = MTools::minstance ($tb [$i], $self); MTools::mconfigure ($self -> {"__instance_".$tb [$i]}, -visible => 0, -sensitive => 0); } else { $self -> {"__instance_".$tb [$i]} = $tb [$i]; $self -> {"__instance_".$tb [$i]} -> mconfigure ( -visible => 0, -sensitive => 0); } } } $self -> {"__etat_$etat"} = \@tb; } if ($first ne '') { $self -> mconfigure ('state' => $first); } return $self; } sub setState { my ($self, $etat) = @_; $self -> mconfigure ('state', $etat); } sub __setState { my ($self, $src, $key, $etat) = @_; my $curr = $self -> {__last}; $self -> {__last} = $etat; if(!$self -> mget ('-visible')) { return; } if ($curr ne '') { if (defined $self -> {"__etat_".$curr}) { my @tb = @{$self -> {"__etat_".$curr}}; for (my $i = 0; $i < @tb; $i ++) { if ( ref ($tb [$i]) eq '') { MTools::mconfigure ($self -> {"__instance_".$tb [$i]}, -visible => 0, -sensitive => 0); } else { $self -> {"__instance_".$tb [$i]} -> mconfigure (-visible => 0, -sensitive => 0); } } } else { die "MTools::MSwitch::__setState : you are trying to unset an unknown state \"$curr\"\n"; } } if (defined $self -> {"__etat_$etat"}) { my @tb = @{$self -> {"__etat_$etat"}}; for (my $i = 0; $i < @tb; $i ++) { if ( ref ($tb [$i]) eq '') { MTools::mconfigure ($self -> {"__instance_".$tb [$i]}, -visible => 1, -sensitive => 1); } else { $self -> {"__instance_".$tb [$i]} -> mconfigure (-visible => 1, -sensitive => 1); } MTools::raise ($self -> {"__instance_".$tb [$i]}); } } else { if ($etat ne '') { die "MTools::MSwitch::__setState : you are trying to set a unknown state \"$etat\"\n"; } } } sub mconfigure { my ($self, %options) = @_; if (defined $options {-visible}) { my $curr = ::mget ($self, 'state'); if ($curr ne '') { if (defined $self -> {"__etat_".$curr}) { my @tb = @{$self -> {"__etat_".$curr}}; my $v = $options {-visible}; for (my $i = 0; $i < @tb; $i ++) { if ( ref ($tb [$i]) eq '') { MTools::mconfigure ($self -> {"__instance_".$tb [$i]}, -visible => $v, -sensitive => $v); } else { $self -> {"__instance_".$tb [$i]} -> mconfigure (-visible => $v, -sensitive => $v); } if ($v) { MTools::raise ($self -> {"__instance_".$tb [$i]}); } } } else { die "MTools::MSwitch::mconfigure : you are trying to set a unknown state \"$curr\"\n"; } } } MTools::mconfigure ($self, %options); } 1;