Statusbox.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 156 CC 12 SCAR 24.8
CC
12
Cov
0%
variable
30.0
Populate
20.0
color
12.0
CRAP 11.9
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for a (colored) status display
3 ##
4 ## Copyright (c) 2002 Michael Krause. All rights reserved.
5 ## This program is free software; you can redistribute it and/or modify it
6 ## under the same terms as Perl itself.
7 ##
8 ## History : V1.0 01-Oct-2002 Class released. MK
9 ## V1.01 05-Dec-2002 Bugfix/Enhancement for Set-Reset '-variable'
10 ##
11 ######################################## EOH ###########################################
12 package Tk::Statusbox;
13  
14 ##############################################
15 ### Use
16 ##############################################
17 use Tk;
18 use Tk::Button;
19 use Tk::Frame;
20  
21 use strict;
22 use Carp;
23  
24 use vars qw ($VERSION);
25 $VERSION = '1.00';
26  
27 use base qw (Tk::Derived Tk::Frame);
28  
29 ########################################################################
30 Tk::Widget->Construct ('Statusbox');
31  
32 #---------------------------------------------
33 sub ClassInit {
34 my ($class, $window) = (@_);
35  
36 $class->SUPER::ClassInit($window);
37 }
38  
39 #---------------------------------------------
40 sub Populate {
41 my ($this, $args) = @_;
42  
43 # Retrieve standard background
44 if (defined ($args->{-bg})) {
45 $args->{-background} = $args->{-bg};
46 }
47 my $common_background = (defined($args->{-background})) ?
48 $args->{-background} :
49 $this->cget ('-background');
50
51 # Create a Closure for saving the current value,
52 # if there is no variable spec'd
53 unless (defined $args->{-variable}) {
54 my $gen = $common_background;
55 my $var = \$gen;
56 $args->{-variable} = $var;
57 }
58  
59 $this->SUPER::Populate ($args);
60
61  
62 #Widget Creation
63 my $canvas = $this->Canvas(
64 -highlightthickness => '0',
65 -relief => 'flat',
66 -borderwidth => '0',
67 )->pack(
68 -fill => 'both',
69 -expand => '1',
70 -anchor => 'center',
71 );
72  
73 $this->Advertise( 'canvas' => $canvas );
74 $this->ConfigSpecs(
75 -background => [['DESCENDANTS'], 'background', 'Background', 'white'],
76 -foreground => [['SELF', 'PASSIVE'], 'foreground', 'Foreground', 'black'],
77 -activebackground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'activebackground', 'ActiveBackground', '#ececec'],
78 -disabledforeground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'disabledForeground', 'DisabledForeground', '#a3a3a3'],
79 -disabledbackground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'disabledBackground', 'DisabledBackground', '#AE00B200C300'],
80 -borderwidth => [['SELF', 'PASSIVE'], 'borderwidth', 'BorderWidth', 2],
81 -color => [['SELF', 'PASSIVE'], 'color', 'Color', 'red'],
82 -relief => [['SELF', 'PASSIVE'], 'relief', 'Relief', 'groove'],
83 -height => ['DESCENDANTS', 'height', 'Height', 15],
84 -width => ['DESCENDANTS', 'width', 'Width', 15],
85 -flashintervall => [['SELF', 'PASSIVE'], 'flashintervall', 'FlashIntervall', 500],
86 -command => ['CALLBACK',undef,undef, undef],
87 -variable => ['METHOD', 'variable', 'Variable', undef],
88 );
89 # Preset the internal Saver
90 $this->{m_Normalbackground} = $common_background;
91 }
92  
93 #---------------------------------------------
94 # Ties the scalar variable to the widget
95 #---------------------------------------------
96 sub variable {
97 use Tie::Watch;
98 # Parameters
99 my ($this, $vref) = @_;
100 #
101 #print "Reached Statusbox::variable. with >@_<, caller", caller ," args\n";
102 #print "this = $this, vref = >", (defined $vref) ? "$vref" : "undef" ,"<\n";
103  
104 my $st = [sub { my ($watch, $new_val) = @_;
105 my $argv= $watch->Args('-store');
106 $argv->[0]->color($new_val);
107 $watch->Store($new_val);
108 }, $this];
109  
110 if (defined $vref) {
111 #assign a new one
112 $this->{watch} = Tie::Watch->new(-variable => $vref, -store => $st);
113 $this->{watch_variable} = $vref;
114 # Remove the Watchpoint after it's no more needed
115 $this->OnDestroy( sub { $this->{watch}->Unwatch if $this->{watch} } );
116  
117 # Preset will the current var-value
118 $this->color($$vref);
119 }
120 elsif ( (scalar @_ ) > 1 ) {
121 $this->{watch}->Unwatch if $this->{watch};
122 }
123
124 # Return sth useful for -cget questionares
125 return $this->{watch_variable};
126 } # end variable
127  
128  
129  
130 #---------------------------------------------
131 # Set Value by Variable or by -color Parm
132 #---------------------------------------------
133 sub color {
134 # Parameters
135 my ($this, $newvalue) = @_;
136 #print "color: >@_<, caller = ", caller, "\n";
137 return unless defined $newvalue;
138 my ($canvas) = $this->Subwidget('canvas');
139 #
140 my $fetch = $this->{watch}->Fetch() || '-';
141 if ($newvalue ne $fetch) {
142 $this->{watch}->Store($newvalue);
143 }
144 #Prepare the button background
145 $canvas->configure (-background => $newvalue );
146 # invoke callback
147 my @args = ( $newvalue );
148 $this->Callback(-command => @args);
149 }
150  
151  
152 #---------------------------------------------
153 # Clear Value,
154 #---------------------------------------------
155 sub clear {
156 my $this = shift;
157
158 $this->color($this->{m_Normalbackground});
159 }
160  
161 #---------------------------------------------
162 # Set Value by Variable or by -color Parm
163 #---------------------------------------------
164 sub flash
165 {
166 # Parameters
167 my ($this, $startstop) = @_;
168
169 # Locals
170 if ($startstop =~ /start/i) {
171 my ($canvas, $bgcolor, $fgcolor, $flashintervall);
172 $canvas = $this->Subwidget('canvas');
173 #
174 $bgcolor = $this->{m_Normalbackground};
175 $fgcolor = $this->{watch}->Fetch;
176 $flashintervall = $this->cget ('-flashintervall');
177 $this->{flash} = 'true';
178 $this->flash_widget($canvas, '-background', $fgcolor, $bgcolor, $flashintervall);
179 }
180 else {
181 $this->{flash} = 'false';
182 }
183  
184 }
185 sub flash_widget {
186 # Flash a widget attribute periodically.
187 my ($this, $w, $opt, $val1, $val2, $interval) = @_;
188 my $mw = $w->toplevel;
189
190 $w->configure($opt => $val1);
191 if ($this->{flash} eq 'true') {
192 $mw->after($interval, [\&flash_widget, $this, $w, $opt,
193 $val2, $val1, $interval]);
194 }
195 else {
196 $this->clear();
197 }
198 }
199  
200 ########################################################################
201 1;
202 __END__