Checkbox.pm untested

filter: Statementstmt 0.0% 0 / 215 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 343 CC 43 SCAR 38.2
CC
43
Cov
0%
set
110.0
Populate
90.0
state
56.0
CRAP 45.8
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for a nicer Checkbutton
3 ##
4 ## Copyright (c) 2002-2007 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 : V2.1 24-Oct-2002 Class released. MK
9 ## V2.2 12-Nov-2002 Small Bugfix. MK
10 ## V2.3 30-Sep-2004 Enhanced state disabled w/o highlightthickness. MK
11 ## V2.4 03-May-2005 Added variable size option. MK
12 ## V2.5 19-Jul-2005 Bugfix for noInitialCallback option - executed never if set. MK
13 ## V2.6 03-Apr-2007 Added activeForeground option. MK
14 ## V2.7 14-Feb-2008 Added -variable retrieval. MK
15 ## V2.8 21-Sep-2011 Added -background update during set for catching unchanged BGs due to earlier invisibility. MK
16 ##
17 ######################################## EOH ###########################################
18 package Tk::Checkbox;
19  
20 ##############################################
21 ### Use
22 ##############################################
23 use Tk;
24 use Tk::Canvas;
25 use Tk::Frame;
26  
27 use strict;
28 use Carp;
29  
30 use vars qw ($VERSION);
31 $VERSION = '2.7';
32  
33 use base qw (Tk::Derived Tk::Frame);
34  
35 ########################################################################
36 Tk::Widget->Construct ('Checkbox');
37  
38 #---------------------------------------------
39 sub ClassInit {
40 my ($class, $window) = (@_);
41 $class->SUPER::ClassInit($window);
42  
43 # Note these keyboard-Keys are only usable, if the widget gets 'focus'
44 $window->bind ($class, '<ButtonPress-1>', 'set');
45 $window->bind ($class, '<space>', 'set');
46 $window->bind ($class, '<Control-Tab>','focusNext');
47 $window->bind ($class, '<Control-Shift-Tab>','focusPrev');
48 $window->bind ($class, '<Tab>', 'focus');
49 }
50  
51 #---------------------------------------------
52 sub Populate {
53 my ($this, $args) = @_;
54  
55 # Retrieve standard background
56 if (defined ($args->{-bg})) {
57 $args->{-background} = $args->{-bg};
58 }
59 # Retrieve standard foreground
60 if (defined ($args->{-fg})) {
61 $args->{-foreground} = $args->{-fg};
62 }
63 my $common_background = (defined($args->{-background})) ?
64 $args->{-background} :
65 $this->cget ('-background');
66 my $common_foreground = (defined($args->{-foreground})) ?
67 $args->{-foreground} :
68 $this->parent->cget('-foreground');
69  
70 # retrieve extra option
71 $this->{m_noInitialCallback} = delete $args->{-noinitialcallback};
72 my $size = delete $args->{-size} || 15;
73
74 # Create a Closure for saving the current value,
75 # if there is no variable spec'd
76 unless (defined $args->{-variable}) {
77 my $gen = (defined ($args->{-offvalue})) ? $args->{-offvalue} : 0;
78 my $var = \$gen;
79 $args->{-variable} = $var;
80 }
81 # retrieve default values
82 $this->{m_OffValue} = (defined($args->{-offvalue})) ? delete $args->{-offvalue} : 0;
83 $this->{m_OnValue} = (defined($args->{-onvalue} )) ? delete $args->{-onvalue} : 1;
84
85  
86 $this->SUPER::Populate ($args);
87  
88 #Widget Creation
89 my $canvas = $this->Canvas(
90 -height => $size,
91 -width => $size,
92 )->pack(
93 -fill => 'both',
94 -expand => '1',
95 );
96
97 my @points = ( 0, 7, 3, 12, 4, 14, 4, 15, 11, 4, 15, 0, 4, 11, 0, 7);
98 $_ = int($_ * $size / 15) foreach @points;
99 $this->{m_CheckMark} = $canvas->createPolygon(@points);
100
101 $canvas->Tk::bind ('<ButtonPress-1>' => sub { $this->set(); } );
102 $canvas->Tk::bind ('<Enter>' => sub { $this->enter(); } );
103 $canvas->Tk::bind ('<Leave>' => sub { $this->leave(); } );
104 $this->bind ('<Tab>' => sub { $this->focus(); } );
105  
106 $this->Advertise( 'canvas' => $canvas );
107
108 $this->ConfigSpecs(
109 -background => ['METHOD', 'background', 'Background', 'grey'],
110 -foreground => ['METHOD', 'foreground', 'Foreground', 'black'],
111 -activebackground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'activeBackground', 'ActiveBackground', '#ececec'],
112 -activeforeground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'activeForeground', 'ActiveForeground', 'Black'],
113 -disabledforeground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'disabledForeground', 'DisabledForeground', '#a3a3a3'],
114 -disabledbackground => [['SELF', 'DESCENDANTS', 'PASSIVE'], 'disabledBackground', 'DisabledBackground', '#AE00B200C300'],
115 -borderwidth => [['SELF', 'PASSIVE'], 'borderwidth', 'BorderWidth', 2],
116 -relief => [['SELF', 'PASSIVE'], 'relief', 'Relief', 'sunken'],
117 -onvalue => ['METHOD', 'onvalue', 'OnValue', '1'],
118 -offvalue => ['METHOD', 'offvalue', 'OffValue', '0'],
119 -state => ['METHOD', 'state', 'State', 'normal'],
120 -command => ['CALLBACK',undef,undef, undef],
121 -variable => ['METHOD', 'variable', 'Variable', undef],
122 -takefocus => [$canvas , 'takeFocus','TakeFocus',1],
123 -highlightthickness => [['SELF'], 'highlightThickness','HighlightThickness', 1]
124 );
125  
126 # Preset the internal Savers
127 $this->{m_Normalbackground} = $common_background;
128 $this->{m_Normalforeground} = $common_foreground;
129 $this->{m_State} = 'normal';
130 $this->{m_Entered} = 'false';
131 }
132  
133 #---------------------------------------------
134 # promote the new background color to the canvas
135 # and the checkmark
136 #---------------------------------------------
137 sub background
138 {
139 # Parameters
140 my ($this, $arg) = @_;
141  
142 if ($arg) {
143 if ($this->viewable) {
144 # Prepare the background
145 my $canvas = $this->Subwidget('canvas');
146 $canvas->configure( -background => $arg);
147 # Apply Checker-Color
148 if ($this->{watch}->Fetch ne $this->{m_OnValue}) {
149 $canvas->itemconfigure(
150 $this->{m_CheckMark},
151 -outline => $arg,
152 -fill => $arg,
153 );
154 }
155 }
156 # Store it
157 $this->{m_Normalbackground} = $arg;
158 }
159 return $this->{m_Normalbackground};
160 }
161  
162 #---------------------------------------------
163 # promote the new foreground color to the
164 # canvas - checkmark
165 #---------------------------------------------
166 sub foreground
167 {
168 # Parameters
169 my ($this, $arg) = @_;
170  
171 if ($arg) {
172 if ($this->viewable) {
173 # Prepare the foreground
174 my $canvas = $this->Subwidget('canvas');
175 #$canvas->configure( -foreground => $arg);# wrong n/a in canvas !!
176 # Apply Checker-Color
177 if ($this->{watch}->Fetch eq $this->{m_OnValue}) {
178 $canvas->itemconfigure(
179 $this->{m_CheckMark},
180 -outline => $arg,
181 -fill => $arg,
182 );
183 }
184 }
185 # Store it
186 $this->{m_Normalforeground} = $arg;
187 }
188 return $this->{m_Normalforeground};
189 }
190  
191 #---------------------------------------------
192 # switch to 'normal', 'disabled'
193 #---------------------------------------------
194 sub state {
195 # Parameters
196 my ($this, $arg) = @_;
197 #print "reached state with >@_<\n";
198  
199 if (defined($arg)) {
200 if ($this->viewable) {
201 my ($canvas, $var, $color);
202 $canvas = $this->Subwidget('canvas');
203 $var = $this->{watch}->Fetch;
204 #
205 if ($arg eq 'disabled') {
206 # Prepare the background
207 $canvas->configure( -background => $this->cget ('-disabledbackground') );
208 # Store/Delete HLT
209 $this->{m_Highlightthickness} = $canvas->cget ('-highlightthickness');
210 $canvas->configure(-highlightthickness => '0');
211  
212 #Prepare the checkmark foreground
213 if ($var eq $this->{m_OnValue}) {
214 $color = $this->cget ('-disabledforeground');
215 }
216 else {
217 $color = $this->cget ('-disabledbackground');
218 }
219 }
220 else {
221 # Prepare the background
222 $canvas->configure( -background => $this->{m_Normalbackground} );
223 # Restore HLT
224 $canvas->configure(-highlightthickness => ($this->{m_Highlightthickness} || '1'));
225  
226 #Prepare the checkmark foreground
227 if ($var eq $this->{m_OnValue}) {
228 $color = $this->{m_Normalforeground};
229 }
230 else {
231 $color = $this->{m_Normalbackground};
232 }
233 }
234 # Apply Checker-Color
235 $canvas->itemconfigure(
236 $this->{m_CheckMark},
237 -outline => $color,
238 -fill => $color,
239 );
240
241 }
242 else {
243 my ($canvas, $var, $color);
244 $canvas = $this->Subwidget('canvas');
245  
246 #Prepare the checkmark foreground
247 $color = $this->{m_Normalbackground};
248  
249 # Prepare the background
250 $canvas->configure( -background => $this->{m_Normalbackground} );
251 # Apply Checker-Color
252 $canvas->itemconfigure(
253 $this->{m_CheckMark},
254 -outline => $color,
255 -fill => $color,
256 );
257 if ($arg eq 'disabled') {
258 # Store/Delete HLT
259 $this->{m_Highlightthickness} = $canvas->cget ('-highlightthickness');
260 $canvas->configure(-highlightthickness => '0');
261 }
262 else {
263 # Restore HLT
264 $canvas->configure(-highlightthickness => ($this->{m_Highlightthickness} || '0') );
265 }
266 }
267 # Store it
268 $this->{m_State} = $arg;
269 }
270 return $this->{m_State};
271 }
272  
273 #---------------------------------------------
274 # internal function to trace any on/off-value updates
275 #---------------------------------------------
276 sub onvalue
277 {
278 # Parameters
279 my ($this, $onvalue) = @_;
280
281 # if we're not in a cget we might consider changing the value
282 if (defined $onvalue) {
283 if ($this->viewable) {
284 $this->{m_OnValue} = $onvalue;
285 }
286 else {
287 # avoid misleading resetting to the default '1'
288 if ($onvalue ne '1') {
289 $this->{m_OnValue} = $onvalue;
290 }
291 }
292 }
293 # needed for cget
294 return $this->{m_OnValue};
295 }
296 #---------------------------------------------
297 sub offvalue
298 {
299 # Parameters
300 my ($this, $offvalue) = @_;
301
302 # if we're not in a cget we might consider changing the value
303 if (defined $offvalue) {
304 if ($this->viewable) {
305 $this->{m_OffValue} = $offvalue;
306 }
307 else {
308 # avoid misleading resetting to the default '0'
309 if ($offvalue ne '0') {
310 $this->{m_OffValue} = $offvalue;
311 }
312 }
313 }
314 # needed for cget
315 return $this->{m_OffValue};
316 }
317  
318  
319 #---------------------------------------------
320 # Setting 'active' look after getting the focus
321 #---------------------------------------------
322 sub enter {
323 # Parameters
324 my $this = shift;
325  
326 if ($this->viewable) {
327 my ($canvas, $color, $fgcolor);
328 $canvas = $this->Subwidget('canvas');
329 #
330 if ($this->{m_State} ne 'disabled') {
331 $this->{m_Entered} = 'true';
332 $this->{m_Lastbackground} = $canvas->cget ('-background');
333 $color = $this->cget ('-activebackground');
334 $fgcolor = $this->cget ('-activeforeground');
335 # Prepare the background
336 $canvas->configure( -background => $color );
337
338 #Prepare the checkmark foreground
339 if ($this->{watch}->Fetch eq $this->{m_OnValue}) {
340 $canvas->itemconfigure(
341 $this->{m_CheckMark},
342 #-outline => $color,
343 #-fill => $color,
344 -outline => $fgcolor,
345 -fill => $fgcolor,
346 );
347 }
348 else {
349 $canvas->itemconfigure(
350 $this->{m_CheckMark},
351 -outline => $color,
352 -fill => $color,
353 );
354 }
355 }
356 }
357 }
358  
359 #---------------------------------------------
360 # Restore the default look after leaving the focus
361 #---------------------------------------------
362 sub leave {
363 my $this = shift;
364  
365 if ($this->{m_Entered} eq 'true') {
366 my ($canvas, $color);
367 $canvas = $this->Subwidget('canvas');
368 $this->{m_Entered} = 'false';
369
370 # Prepare the background
371 $canvas->configure( -background => $this->{m_Lastbackground} );
372
373 #Prepare the checkmark foreground
374 if ($this->{watch}->Fetch eq $this->{m_OnValue}) {
375 #$color = $this->cget ('-foreground');
376 $color = $this->{m_Normalforeground};
377 }
378 else {
379 $color = $canvas->cget ('-background');
380 }
381 $canvas->itemconfigure(
382 $this->{m_CheckMark},
383 -outline => $color,
384 -fill => $color,
385 );
386 }
387 }
388  
389 #---------------------------------------------
390 # Ties the scalar variable to the widget
391 #---------------------------------------------
392 sub variable {
393 use Tie::Watch;
394  
395 # Parameters
396 my ($this, $vref) = @_;
397 #
398 if ($vref) {
399 my $st = [sub { my ($watch, $new_val) = @_;
400 my $argv= $watch->Args('-store');
401 $argv->[0]->set($new_val);
402 $watch->Store($new_val);
403 }, $this];
404  
405 $this->{watch} = Tie::Watch->new(-variable => $vref, -store => $st);
406  
407 # Remove the Watchpoint after it's no more needed
408 $this->OnDestroy( sub { $this->{watch}->Unwatch if $this->{watch} } );
409  
410 # Preset will the current var-value
411 $this->set($$vref);
412 # Store internally
413 $this->{m_VarRef} = $vref;
414 }
415 return $this->{m_VarRef};
416 } # end variable
417  
418  
419 #---------------------------------------------
420 # Set Value by Pressing the Mouse Bttn or via Parm
421 #---------------------------------------------
422 sub set {
423 # Parameters
424 my ($this, $newvalue) = @_;
425  
426 if ( $this->{m_State} ne 'disabled') {
427 my ($canvas, $color);
428 $canvas = $this->Subwidget('canvas');
429 #
430 #toggle stored value, if no external value was given
431 unless (defined($newvalue)) {
432 $newvalue = $this->{watch}->Fetch;
433 if (defined ($newvalue)) {
434 if ($newvalue eq $this->{m_OnValue}) {
435 $newvalue = $this->{m_OffValue};
436 }
437 else {
438 $newvalue = $this->{m_OnValue};
439 }
440 }
441 else {
442 $newvalue = $this->{m_OffValue};
443 }
444 }
445 #safety check for illegal values
446 if ($newvalue ne $this->{m_OnValue} && $newvalue ne $this->{m_OffValue}) {
447 if ($newvalue eq '1') {
448 $newvalue = $this->{m_OnValue};
449 }
450 else {
451 $newvalue = $this->{m_OffValue};
452 }
453 }
454  
455 $this->{watch}->Store($newvalue);
456  
457 #setup the coloring
458 if ($newvalue eq $this->{m_OnValue}) {
459 $color = $this->{m_Normalforeground};
460 }
461 else {
462 $color = $this->cget ('-background');
463 if ($this->{m_Entered} eq 'true') {
464 $color = $this->cget ('-activebackground');
465 }
466 }
467 # Refresh background in case not yet applied due to not yet viewable
468 $canvas->configure( -background => $this->{m_Normalbackground});
469 #Prepare the checkmark foreground
470 $canvas->itemconfigure(
471 $this->{m_CheckMark},
472 -outline => $color,
473 -fill => $color,
474 );
475  
476 # invoke callback for normal execution
477 my @args = ( $newvalue );
478 my $inhibit = delete $this->{m_noInitialCallback};
479 $this->Callback(-command => @args) unless $inhibit;
480 }
481 }
482  
483 ########################################################################
484 1;
485 __END__