ColorEntry.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 178 CC 14 SCAR 19.9
CC
14
Cov
0%
EntryUpdate
12.0
OnEscape
12.0
popBlock
12.0
CRAP 7.3
1 package Tk::ColorEntry;
2  
3 use strict;
4 use warnings;
5 use vars qw($VERSION);
6 $VERSION = '0.12';
7 use Tk;
8  
9 use base qw(Tk::Derived Tk::Frame);
10  
11 Construct Tk::Widget 'ColorEntry';
12  
13 require Tk::PopColor;
14  
15 =head1 NAME
16  
17 Tk::ColorEntry - Entry widget with a color selection facilities.
18  
19 =head1 SYNOPSIS
20  
21 use Tk::ColorEntry;
22 my $entry = $window->ColorEntry->pack;
23  
24 =head1 DESCRIPTION
25  
26 Megawidget, inherits L<Tk::Frame>
27 Tk::ColorEntry is an entry widget with a label packed to it's right.
28 The background color of the label is used as indicator for the current color.
29 Clicking the entry widget pops a L<Tk::ColorPop> widget.
30  
31 Pressing escape causes the ColorPop to widthdraw. If a pick operation is active
32 cancels the pick operation instead.
33  
34 =head1 OPTIONS
35  
36 You can use many options of L<Tk::ColorPicker>.
37  
38 =over 4
39  
40 =item Switch: B<-command>
41  
42 Callback to be executed when a color is selected. The color is given as parameter.
43  
44 =item Switch: B<-entryerrorcolor>
45  
46 Default value '#FF0000' (red). Foreground color of the entry
47 when it's content is not a valid color.
48  
49 =item Switch: B<-indborderwidth>
50  
51 Default value 2. Borderwidth of the indicator label.
52  
53 =item Switch: B<-indicatorwidth>
54  
55 Default value 4. Width of the indicator label.
56  
57 =item Switch: B<-indrelief>
58  
59 Default value 'sunken'. Relief of the indicator label.
60  
61 =item Switch: B<-popcolor>
62  
63 Sets and returns the reference to the PopColor widget to be used.
64  
65 =item Switch: B<-variable>
66  
67 Reference to the variable where the current value is held.
68  
69 =back
70  
71 =head1 METHODS
72  
73 You can use many methods of L<Tk::ColorPicker>.
74  
75 =over 4
76  
77 =cut
78  
79 sub Populate {
80 my ($self,$args) = @_;
81  
82 my $pop = delete $args->{'-popcolor'};
83
84 $self->SUPER::Populate($args);
85
86 my $entry = $self->Entry(
87 )->pack(
88 -side => 'left',
89 -expand => 1,
90 -fill => 'x',
91 # -pady => 2,
92 );
93 my $indicator = $self->Label->pack(
94 -side => 'left',
95 -fill => 'y',
96 -padx => 2,
97 # -pady => 2,
98 );
99 $self->Advertise('Display', $indicator);
100 $self->Advertise('Entry', $entry);
101 $pop = $self->PopColor(
102 -updatecall => sub {
103 $self->put(shift);
104 },
105 -widget => $self,
106 ) unless defined $pop;
107 $self->Advertise('Pop', $pop);
108  
109 $entry->bind('<Button-1>', [$self, 'popBlock']);
110 $entry->bind('<ButtonRelease-1>', [$self, 'popFlip']);
111 $entry->bind('<Return>', [$self, 'popFlip']);
112 # $entry->bind('<FocusOut>', [$self, 'popDown']);
113 $entry->bind('<Key>', [$self, 'OnKey']);
114 $entry->bind('<Escape>', [$self, 'OnEscape']);
115  
116 $self->{POPBLOCK} = 0;
117 my $var = '';
118 $self->ConfigSpecs(
119 -background => ['SELF', 'DESCENDANTS'],
120 -command => ['CALLBACK', undef, undef, sub {}],
121 -entryerrorcolor => ['PASSIVE', undef, undef, '#FF0000'],
122 -entryforeground => ['PASSIVE', undef, undef, $self->Subwidget('Entry')->cget('-foreground')],
123 -font => [$entry],
124 -foreground => [$entry],
125 -historyfile => [$pop->Subwidget('Picker')],
126 -indborderwidth => [{-borderwidth => $indicator}, undef, undef, 2],
127 -indicatorwidth => [{-width => $indicator}, undef, undef, 4],
128 -indrelief => [{-relief => $indicator}, undef, undef, 'sunken'],
129 -justify => [$entry],
130 -popborderwidth => [{-borderwidth => $pop}, undef, undef, 1],
131 -popcolor => ['PASSIVE', undef, undef, $pop],
132 -poprelief => [{-relief => $pop}, undef, undef, 'raised'],
133 -state => [$entry],
134 -variable => [{-textvariable => $entry}, undef, undef, \$var],
135 -width => [$entry],
136 DEFAULT => [ $pop ],
137 );
138  
139 $self->Delegates(
140 DEFAULT => $pop,
141 );
142 }
143  
144 sub EntryUpdate {
145 my $self = shift;
146 my $entry = $self->Subwidget('Entry');
147 my $display = $self->Subwidget('Display');
148 my $val = $entry->get;
149 if ($self->validate($val)) {
150 my $pop = $self->Subwidget('Pop');
151 my $current = $pop->getHEX;
152 $display->configure(-background => $current) if $self->validate($current);
153 $entry->configure(-foreground => $self->cget('-entryforeground'));
154 } else {
155 $display->configure(-background => $self->cget('-background'));
156 $entry->configure(-foreground => $self->cget('-entryerrorcolor'));
157 }
158 }
159  
160 =item B<get>
161  
162 Returns the contents of the entry widget if it is a valid color.
163  
164 =cut
165  
166 sub get {
167 my $self = shift;
168 my $color = $self->Subwidget('Entry')->get;
169 return $color if $self->validate($color);
170 }
171  
172 sub OnEscape {
173 my $self = shift;
174 if ($self->pickInProgress) {
175 $self->pickCancel
176 } else {
177 my $save = delete $self->{'e_save'};
178 $self->put($save) if defined $save;
179 $self->popCancel;
180 }
181 }
182  
183 sub OnKey {
184 my $self = shift;
185 my $color = $self->Subwidget('Entry')->get;
186 $self->put($color) if $self->validate($color);
187 $self->EntryUpdate;
188 }
189  
190 sub popCancel {
191 my $self = shift;
192 delete $self->{'e_save'};
193 $self->cget('-popcolor')->popCancel;
194 }
195  
196 sub popBlock {
197 my $self = shift;
198 if ($self->cget('-popcolor')->ismapped) {
199 my $color = $self->Subwidget('Entry')->get;
200 $self->Callback('-command', $color) if $self->validate($color);
201 $self->{POPBLOCK} = 1;
202 $self->after(400, sub { $self->{POPBLOCK} = 0});
203 }
204 }
205  
206 sub popDown {
207 my $self = shift;
208 delete $self->{'e_save'};
209 my $color = $self->Subwidget('Entry')->get;
210 $self->Callback('-command', $color) if $self->validate($color);
211 $self->cget('-popcolor')->popDown;
212 }
213  
214 sub popFlip {
215 my $self = shift;
216 if ($self->cget('-popcolor')->ismapped) {
217 $self->popDown
218 } else {
219 $self->popUp unless $self->{POPBLOCK}
220 }
221 }
222  
223 sub popUp {
224 my $self = shift;
225 my $save = $self->Subwidget('Entry')->get;
226 $self->{'e_save'} = $save;
227 my $pop = $self->cget('-popcolor');
228 $pop->configure(-widget => $self->Subwidget('Entry'));
229 $pop->configure(-updatecall => ['put', $self]);
230 $pop->put($save);
231
232 $pop->popUp;
233 }
234  
235 =item B<put>(I<$color>)
236  
237 $color becomes the content of the entry widget.
238 Adjusts the sliders if $color is a valid color.
239  
240 =cut;
241  
242 sub put {
243 my ($self, $color) = @_;
244 unless (defined($color)) {
245 warn "color is not defined";
246 return
247 }
248 my $var = $self->Subwidget('Entry')->cget('-textvariable');
249 $$var = $color;
250 my $pop = $self->Subwidget('Pop');
251 $pop->put($color);
252 $self->EntryUpdate;
253 }
254  
255 =back
256  
257 =head1 LICENSE
258  
259 Same as Perl.
260  
261 =head1 AUTHOR
262  
263 Hans Jeuken (hanje at cpan dot org)
264  
265 =cut
266  
267 =head1 BUGS
268  
269 Unknown. If you find any, please contact the author.
270  
271 =head1 SEE ALSO
272  
273 =over 4
274  
275 =item L<Tk::Poplevel>
276  
277 =item L<Tk::PopColor>
278  
279 =item L<Tk::ColorPicker>
280  
281 =back
282  
283 =cut
284  
285 1;
286 __END__