Buttonplus.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 134 CC 8 SCAR 18.2
CC
8
Cov
0%
Enter
12.0
Leave
12.0
Populate
6.0
CRAP 6.2
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for a Button with Text and image
3 ##
4 ## Copyright (c) 2004-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 : 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 ##
11 ######################################## EOH ###########################################
12 package Tk::Buttonplus;
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 = '0.2';
26  
27 use base qw (Tk::Frame);
28  
29 ########################################################################
30 Tk::Widget->Construct ('Buttonplus');
31  
32 my ($buttonWindow, $relief);
33 #---------------------------------------------
34 sub ClassInit
35 {
36 my ($class, $window) = (@_);
37  
38 $class->SUPER::ClassInit($window);
39 }
40  
41 #---------------------------------------------
42 sub Populate
43 {
44 my ($this, $args) = @_;
45  
46 my ($side, $image, $button);
47  
48 # Retrieve special option
49 $side = delete $args->{-side} || $this->optionGet( 'side', 'Side' ) || 'right';
50  
51  
52 # Walk through the BaseClass
53 $this->SUPER::Populate ($args);
54  
55  
56 # Widget Creation - Button 1
57 if ($args->{-bitmap} || $args->{-image}) {
58 $image = $this->Button( %$args,
59 -highlightthickness => '0',
60 -relief => 'flat',
61 -borderwidth => '0',
62 )->pack(-side => $side, -fill => 'both');
63 $this->Advertise( 'image' => $image );
64 #
65 # Setup proper bindings for the image button
66 $image->Tk::bind ('<Enter>' => sub { $this->Enter() } );
67 $image->Tk::bind ('<Leave>' => sub { $this->Leave() } );
68 #
69 $image->Tk::bind ('<ButtonPress-1>' => sub { $this->butDown() } );
70 $image->Tk::bind ('<ButtonRelease-1>' => sub { $this->butUp() } );
71  
72 # Delete options that are not needed for the second button
73 delete $args->{-bitmap}; delete $args->{-image};
74 }
75 # Widget Creation - Button 2
76 $button = $this->Button(%$args,
77 -highlightthickness => '0',
78 -relief => 'flat',
79 -borderwidth => '0',
80 )->pack(-fill => 'both');
81 $this->Advertise( 'button' => $button );
82 #
83 # Setup proper bindings for the text button
84 $button->Tk::bind ('<Enter>' => sub { $this->Enter() } );
85 $button->Tk::bind ('<Leave>' => sub { $this->Leave() } );
86 #
87 $button->Tk::bind ('<ButtonPress-1>' => sub { $this->butDown() } );
88 $button->Tk::bind ('<ButtonRelease-1>' => sub { $this->butUp() } );
89  
90  
91 # Prepare the minimum necessary options
92 $this->ConfigSpecs(
93 -width => [['SELF', 'PASSIVE'], 'width', 'Width', 0],
94 -borderwidth => [['SELF', 'PASSIVE'], 'borderwidth', 'BorderWidth', 2],
95 -relief => [['SELF', 'PASSIVE'], 'relief', 'Relief', 'raised'],
96 -side => [['SELF', 'PASSIVE'], 'side', 'Side', 'right'],
97 -state => [['DESCENDANTS'], 'state', 'State', 'normal'],
98 'DEFAULT' => [$button],
99 );
100  
101 # Redirect all operations to the 'main' button
102 $this->Delegates(
103 'DEFAULT' => $button,
104 );
105 }
106  
107 #---------------------------------------------
108 # Enter --
109 # The procedure below is invoked when the
110 # mouse pointer enters a button widget. It
111 # changes the state of the button to active
112 # unless the button is disabled.
113 #
114 # Arguments:
115 # this - The name of the widget.
116 #---------------------------------------------
117 sub Enter
118 {
119 my $this = shift;
120  
121 if ($this->cget('-state') ne 'disabled') {
122 $this->Subwidget('image')->configure('-state' => 'active') if $this->Subwidget('image');
123 $this->Subwidget('button')->configure('-state' => 'active');
124 }
125 }
126  
127 #---------------------------------------------
128 # Leave --
129 # The procedure below is invoked when the
130 # mouse pointer leaves a button widget. It
131 # changes the state of the button back to
132 # inactive.
133 #
134 # Arguments:
135 # this - The name of the widget.
136 #---------------------------------------------
137 sub Leave
138 {
139 my $this = shift;
140  
141 if ($this->cget('-state') ne 'disabled') {
142 $this->Subwidget('image')->configure('-state' => 'normal') if $this->Subwidget('image');
143 $this->Subwidget('button')->configure('-state' => 'normal');
144 }
145 }
146  
147 #---------------------------------------------
148 # butDown --
149 # The procedure below is invoked when the
150 # mouse button is pressed in the button widget.
151 # It records the fact that the mouse is in the
152 # button, saves the button's relief so it can
153 # be restored later, and changes the relief
154 # to sunken.
155 #
156 # Arguments:
157 # this - The name of the widget.
158 #---------------------------------------------
159 sub butDown
160 {
161 my $this = shift;
162  
163 $relief = $this->cget('-relief');
164 if ($this->cget('-state') ne 'disabled') {
165 $buttonWindow = $this;
166 $this->configure('-relief' => 'sunken')
167 }
168 }
169  
170 #---------------------------------------------
171 # butUp --
172 # The procedure below is invoked when the
173 # mouse button is released in a button widget.
174 # It restores the button's relief and invokes
175 # the command as long as the mouse hasn't left
176 # the button.
177 #
178 # Arguments:
179 # this - The name of the widget.
180 #---------------------------------------------
181 sub butUp
182 {
183 my $this = shift;
184  
185 if (defined($buttonWindow) && $buttonWindow == $this) {
186 undef $buttonWindow;
187 $this->configure('-relief' => $relief);
188 }
189 }
190  
191 ########################################################################
192 1;
193 __END__