CompoundButton.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 99 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 372 CC 29 SCAR 51.3
CC
29
Cov
0%
create_compound
342.0
Populate
110.0
enter
6.0
CRAP 169.8
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for a Button with Text and image or bitmap
3 ##
4 ## Copyright (c) 2004-2009 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 : V0.1 22-Jul-2004 Class released. MK
9 ## History : V0.2 08-Mar-2007 Added side-default from Optionbase. Thx to hkuhlmann. MK
10 ## History : V0.3 05-Nov-2009 Complete ReImplemention based on Tk::Compound. MK
11 ##
12 ######################################## EOH ###########################################
13 package Tk::CompoundButton;
14  
15 ##############################################
16 ### Use
17 ##############################################
18 use Tk;
19 use Tk::Button;
20 use Tk::Frame;
21 use Tk::Compound;
22  
23 use strict;
24 use Carp;
25  
26 use vars qw ($VERSION);
27 $VERSION = '0.3';
28  
29 use base qw (Tk::Frame);
30  
31 ########################################################################
32 Tk::Widget->Construct ('CompoundButton');
33  
34 #---------------------------------------------
35 sub ClassInit
36 {
37 # Parameters
38 my ($class, $window) = @_;
39  
40 $class->SUPER::ClassInit($window);
41  
42 #-------------------------------------------------
43 # Setup proper bindings for handling the 'active' behavior
44 $window->bind ($class, '<Enter>', 'enter');
45 $window->bind ($class, '<Leave>', 'leave');
46 }
47  
48 #---------------------------------------------
49 sub Populate
50 {
51 # Parameters
52 my ($this, $args) = @_;
53 # Locals
54 my (%attrs, @sides, @txtjst_sides, $button, @text_ref_text);
55
56 @sides = qw(left right top bottom);
57 @txtjst_sides = qw(left right center none);
58 #-------------------------------------------------
59 # Retrieve special option (which are not feasible for configure
60 $attrs{side} = delete $args->{-side} || $this->optionGet( 'side', 'Side' ) || 'right';
61 croak "Error: Allowed values for '-side' are " . join(', ', @sides) . "!\n" unless grep /$attrs{side}/, @sides;
62 $attrs{gap} = delete $args->{-gap}; unless (defined $attrs{gap}) { $attrs{gap} = $this->optionGet( 'gap', 'Gap' ) || '3' }
63 $attrs{textjustify} = delete $args->{-textjustify} || $this->optionGet( 'textjustify', 'Textjustify' ) || 'none';
64 croak "Error: Allowed values for '-textjustify' are " . join(', ', @txtjst_sides) . "!\n" unless grep /$attrs{textjustify}/, @txtjst_sides;
65 #-------------------------------------------------
66 # We must remove padx/y otherwise during widget creation the padx/y is calc'd as padx/y PLUS optionbased padx/y
67 $attrs{padx} = delete $args->{-padx}; unless (defined $attrs{padx}) { $attrs{padx} = $this->optionGet( 'padx', 'padX' ) || '3m' }
68 $attrs{pady} = delete $args->{-pady}; unless (defined $attrs{pady}) { $attrs{pady} = $this->optionGet( 'pady', 'padY' ) || '1m' }
69 # similar problem for abchor
70 $attrs{anchor} = delete $args->{-anchor} || $this->optionGet( 'anchor', 'Anchor' ) || 'center';
71 #-------------------------------------------------
72 # The following 2 opts MUST also be deleted otherwise the Bttn gets filled ONLY with the image
73 $attrs{image} = delete $args->{-image};
74 $attrs{bitmap} = delete $args->{-bitmap};
75 #-------------------------------------------------
76  
77 #-------------------------------------------------
78 # Create a worker-button
79 $this->{_Button} = $button = $this->Button()->pack;
80 #-------------------------------------------------
81  
82 #-------------------------------------------------
83 # Walk through the BaseClass
84 $this->SUPER::Populate($args);
85 #-------------------------------------------------
86  
87 #-------------------------------------------------
88 # Prepare the minimum necessary options
89 $this->ConfigSpecs(
90 -padx => ['SELF', 'padx', 'padX', '-1'], # Mandatory to avoid 'doubled' padx insertion (1x Bttn, 1x Frm)
91 -pady => ['SELF', 'pady', 'padY', '-1'], # Mandatory to avoid 'doubled' pady insertion (1x Bttn, 1x Frm)
92 'DEFAULT' => [$button],
93 );
94 #-------------------------------------------------
95  
96 #-------------------------------------------------
97 # Redirect all operations to the 'main' button
98 $this->Delegates('DEFAULT' => $button);
99 #-------------------------------------------------
100  
101 #-------------------------------------------------
102 # Now apply the saved attrs to the button
103 $button->configure(-padx => $attrs{padx}, -pady => $attrs{pady}, -anchor => $attrs{anchor});
104 #-------------------------------------------------
105  
106 #-------------------------------------------------
107 # store values needed for building the Compount Element(s)
108 $attrs{width} = $args->{-width} || $this->optionGet( 'width', 'Width' ) || $this->cget('-Width');
109 $attrs{background} = $args->{-bg} || $args->{-background} || $this->optionGet( 'background', 'Background' ) || $this->cget('-background');
110 $attrs{foreground} = $args->{-fg} || $args->{-foreground} || $this->optionGet( 'foreground', 'Foreground' ) || $this->cget('-foreground');
111 $attrs{justify} = $args->{-justify} || $this->optionGet( 'justify', 'Justify' ) || $this->cget('-justify');
112 $attrs{underline} = $args->{-underline} || $this->optionGet( 'underline', 'Underline' ) || $this->cget('-underline');
113 $attrs{wraplength} = $args->{-wraplength} || $this->optionGet( 'wraplength', 'WrapLength' ) || $this->cget('-wraplength');
114 $attrs{font} = $args->{-font} || $this->optionGet( 'font', 'Font' ) || $this->cget('-font');
115 $attrs{text} = $args->{-text} || $this->optionGet( 'text', 'Text' ) || $this->cget('-text');
116 # store coloring for an 'active' looking Compount Element too
117 $attrs{activebackground} = $args->{-activebackground} || $this->optionGet( 'activebackground', 'ActiveBackground' ) || $this->cget('-activebackground');
118 $attrs{activeforeground} = $args->{-activeforeground} || $this->optionGet( 'activeforeground', 'ActiveForeground' ) || $this->cget('-activeforeground');
119  
120 #-------------------------------------------------
121 # Take care of text references
122 if ($args->{-textvariable}) {
123 # Setup a Tie to watch for changes on the textvar
124 use Tie::Watch;
125 my $st = [sub { my ($watch, $new_val) = @_;
126 my $argv= $watch->Args('-store');
127 $argv->[0]->create_all_compounds($button, %attrs, text => $new_val);
128 $watch->Store($new_val);
129 }, $this];
130  
131 $this->{_BPI_VRWatch} = Tie::Watch->new(-variable => $args->{-textvariable}, -store => $st);
132  
133  
134 # Preset will the current var-value
135 @text_ref_text = (text => ${$args->{-textvariable}});
136 }
137 #-------------------------------------------------
138  
139 #-------------------------------------------------
140 # Now create an initial set of compounds
141 $this->create_all_compounds($button, %attrs, @text_ref_text);
142  
143 #-------------------------------------------------
144 # Remove the Watchpoint after it's no more needed
145 $this->OnDestroy( sub { $this->{_BPI_VRWatch}->Unwatch if $this->{_BPI_VRWatch};
146 $this->{_BPI_CompNormal}->delete if $this->{_BPI_CompNormal};
147 $this->{_BPI_CompActive}->delete if $this->{_BPI_CompActive};
148 } );
149  
150 }
151  
152  
153 ########################################################################
154  
155 #---------------------------------------------
156 # create_all_compounds
157 # The procedure below is invoked to create ALL
158 # compounds needed for the current widget.
159 #---------------------------------------------
160 sub create_all_compounds
161 {
162 # Paremeters
163 my ($this, $button, %attrs) = @_;
164  
165 #-------------------------------------------------
166 # Create a 'normal' Compount Element and attach it
167 $this->{_BPI_CompNormal} = create_compound($button, %attrs);
168 $button->configure(-image => $this->{_BPI_CompNormal});
169  
170 #-------------------------------------------------
171 # Create an 'active' Compount Element and store it
172 $attrs{background} = $attrs{activebackground};
173 $attrs{foreground} = $attrs{activeforeground};
174 #
175 $this->{_BPI_CompActive} = create_compound($button, %attrs);
176 }
177  
178 #---------------------------------------------
179 # create_compound
180 # The procedure below is invoked to create a
181 # single compound according given attr-spec.
182 #---------------------------------------------
183 sub create_compound
184 {
185 # Paremeters
186 my ($button, %attrs) = @_;
187  
188 # Locals
189 my ($compound, %text_attrs);
190  
191 #-------------------------------------------------
192 # Create a new storage element
193 $compound = $button->Compound(-padx => $attrs{padx}, -pady => $attrs{pady}, -font => $attrs{font},
194 #-showbackground => 1, -relief => 'ridge', -borderwidth => 2, ## for debugging only
195 -background => $attrs{background}, -foreground => $attrs{foreground});
196 # a shortcut to save effort
197 %text_attrs = (-text => $attrs{text}, -font => $attrs{font}, -justify => $attrs{justify},
198 -underline => $attrs{underline}, -wraplength => $attrs{wraplength});
199  
200 #-------------------------------------------------
201 # Provide a dynamic TEXT justification (by calculating dynamic gap between image/bitmap and text
202 if ($attrs{textjustify} ne 'none' and $attrs{width}) {
203 my ($im_width, $txt_width, $delta);
204
205 $im_width = 0;
206 if ($attrs{image}) {
207 $im_width = $attrs{image}->width;
208 }
209 elsif ($attrs{bitmap}) {
210 croak "Error: Option '-textjustify' is NOT usable together with '-bitmap', use a pixmap via '-image' instead!\n";
211 }
212 $txt_width = $button->fontMeasure($attrs{font}, $attrs{text});
213 $delta = $attrs{width} - $im_width - $txt_width - $attrs{gap};
214  
215 if ($attrs{textjustify} eq 'center') {
216 $delta /= 2;
217 }
218 else {
219 if (($attrs{textjustify} eq 'right' and $attrs{anchor} =~ /e$/io)
220 or ($attrs{textjustify} eq 'left' and $attrs{anchor} =~ /w$/io)) {
221 $delta = 0;
222 }
223 }
224  
225 $delta = 0 if $delta < 0; # failsafe in case of multicolumn-text
226 $attrs{gap} += $delta;
227 }
228  
229 #-------------------------------------------------
230 # Differentiate build according side
231 if ($attrs{side} =~ /left|right/io) {
232 $compound->Line(-anchor => $attrs{anchor});
233 if ($attrs{side} =~ /left/io) {
234 if ($attrs{image}) {
235 $compound->Image(-image => $attrs{image})
236 }
237 elsif ($attrs{bitmap}) {
238 $compound->Bitmap(-bitmap => $attrs{bitmap})
239 }
240 $compound->Space(-width => $attrs{gap});
241 $compound->Text(%text_attrs);
242 }
243 else {
244 $compound->Text(%text_attrs);
245 $compound->Space(-width => $attrs{gap});
246 if ($attrs{image}) {
247 $compound->Image(-image => $attrs{image})
248 }
249 elsif ($attrs{bitmap}) {
250 $compound->Bitmap(-bitmap => $attrs{bitmap})
251 }
252 }
253 }
254 else { # Side Top or Bottom
255 $compound->Line(-anchor => $attrs{anchor});
256 if ($attrs{side} =~ /top/io) {
257 if ($attrs{image}) {
258 $compound->Image(-image => $attrs{image})
259 }
260 elsif ($attrs{bitmap}) {
261 $compound->Bitmap(-bitmap => $attrs{bitmap})
262 }
263 $compound->Line(-anchor => $attrs{anchor});
264 $compound->Space(-height => $attrs{gap});
265 $compound->Line(-anchor => $attrs{anchor});
266 $compound->Text(%text_attrs);
267 }
268 else {
269 $compound->Text(%text_attrs);
270 $compound->Line(-anchor => $attrs{anchor});
271 $compound->Space(-height => $attrs{gap});
272 $compound->Line(-anchor => $attrs{anchor});
273 if ($attrs{image}) {
274 $compound->Image(-image => $attrs{image})
275 }
276 elsif ($attrs{bitmap}) {
277 $compound->Bitmap(-bitmap => $attrs{bitmap})
278 }
279 }
280 }
281 return $compound;
282 }
283  
284 #---------------------------------------------
285 # enter
286 # The procedure below is invoked when the
287 # mouse pointer enters a button widget. It
288 # changes the 'visible' state of the button
289 # to active unless the button is disabled.
290 #---------------------------------------------
291 sub enter
292 {
293 my $this = shift;
294 $this->{_Button}->configure(
295 -image => $this->{_BPI_CompActive}) if $this->cget('-state') ne 'disabled'
296 }
297  
298 #---------------------------------------------
299 # leave
300 # The procedure below is invoked when the
301 # mouse pointer leaves a button widget. It
302 # changes the 'visible' state of the button
303 # back to normal.
304 #---------------------------------------------
305 sub leave
306 {
307 my $this = shift;
308 $this->{_Button}->configure(
309 -image => $this->{_BPI_CompNormal}) if $this->cget('-state') ne 'disabled'
310 }
311  
312  
313 ########################################################################
314 1;
315 __END__