aboutsummaryrefslogtreecommitdiff
path: root/src/MTools/MSwitch.pm
blob: 8d94fa97d285e6fb344550d86e53d0c8823f0439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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 set a 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;