Poplevel.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« PopList.pm ↑ Summary
1 package Tk::Poplevel;
2  
3 =head1 NAME
4  
5 Tk::Poplevel - Popping a toplevel without decoration relative to a widget
6  
7 =cut
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 $VERSION = '0.14';
13  
14 1 ▶use base qw(Tk::Derived Tk::Toplevel);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 use Tk;
17  
18 Construct Tk::Widget 'Poplevel';
19  
20 =head1 SYNOPSIS
21  
22 require Tk::Poplevel;
23 my $pop = $window->Poplevel(@options,
24 -widget => $somewidget,
25 );
26 $pop->popUp;
27  
28 =head1 DESCRIPTION
29  
30 This widget pops a toplevel without ornaments relative to the widget specified in the B<-widget> option.
31 It aligns its size and position to the widget.
32  
33 Clicking outside the toplevel will pop it down.
34  
35 =head1 OPTIONS
36  
37 Accepts all the options of a Toplevel widget;
38  
39 =over 4
40  
41 =item B<-confine>
42  
43 Default value is 0.
44 If set the popup will have the equal with of the widget.
45  
46 =item B<-popalign>
47  
48 Default value 'left'.
49 Can be 'left' or 'right'.
50 This is the preferred horizontal alignment.
51 Does nothing when B<-confine> is set.
52  
53 =item B<-popdirection>
54  
55 Default value 'down'.
56 Can be 'up' or 'down'.
57 Specifies if the popup should be preferrably above or below the widget.
58 Does nothing when B<-confine> is set.
59  
60 =item B<-widget>
61  
62 Set and return a reference to the widget the Poplevel should pop relative to.
63  
64 =back
65  
66 =head1 METHODS
67  
68 =over 4
69  
70 =cut
71  
72 sub Populate {
73 my ($self,$args) = @_;
74
75 $self->SUPER::Populate($args);
76  
77 $self->{POPDIRECTION} = '';
78
79 $self->overrideredirect(1);
80 $self->withdraw;
81  
82 my $parent = $self->parent;
83 my $bindsub = $parent->bind('<Button-1>');
84 if ($bindsub) {
85 $parent->bind('<Button-1>', sub {
86 $bindsub->Call;
87 $self->popDown;
88 });
89 } else {
90 $parent->bind('<Button-1>', [$self, 'popDown'] );
91 }
92
93 $self->ConfigSpecs(
94 -borderwidth => [$self, 'borderWidth', 'BorderWidth', 1],
95 -confine => ['PASSIVE', undef, undef, 0],
96 -popalign => ['PASSIVE', undef, undef, 'left'],
97 -popdirection => ['PASSIVE', undef, undef, 'down'],
98 -relief => [$self, 'relief', 'Relief', 'raised'],
99 -widget => ['PASSIVE'],
100 DEFAULT => [ $self ],
101 );
102 }
103  
104 =item B<calculateHeight>
105  
106 For you to overwrite.
107 Returns the requested height of the B<Polevel>.
108  
109 =cut
110  
111 sub calculateHeight {
112 return $_[0]->reqheight;
113 }
114  
115 =item B<calculateWidth>
116  
117 For you to overwrite.
118 Returns the requested width of the B<Polevel>.
119  
120 =cut
121  
122 sub calculateWidth {
123 return $_[0]->reqwidth;
124 }
125  
126 sub ConfigureSizeAndPos {
127 my $self = shift;
128  
129 my $widget = $self->cget('-widget');
130 return unless (defined $widget) and (Exists $widget);
131 my $screenheight = $self->vrootheight;
132 my $screenwidth = $self->vrootwidth;
133 my $confine = $self->cget('-confine');
134 my $preferred = $self->cget('-popdirection');
135 my $align = $self->cget('-popalign');
136  
137 my $height = $self->calculateHeight;
138  
139 my $width;
140 if ($confine) {
141 $width = $widget->width;
142 } else {
143 $width = $self->calculateWidth;
144 }
145  
146 my $x = $widget->rootx;
147 unless ($confine) {
148 my $flag;
149 if ($align eq 'left') {
150 $flag = ($x + $width > $screenwidth);
151 } else {
152 $flag = ($x + $widget->width - $width > 0)
153 }
154 if ($flag) {
155 $x = $x - ($width - $widget->width);
156 }
157 }
158  
159 my $y = $widget->rooty;
160  
161 my $flag;
162 if ($preferred eq 'up') {
163 $flag = ($y - $height > 0)
164 } else {
165 $flag = ($y + $height + $widget->height > $screenheight)
166 }
167
168 if ($flag) {
169 $self->{POPDIRECTION} = 'up';
170 $y = $y - $height;
171 } else {
172 $self->{POPDIRECTION} = 'down';
173 $y = $y + $widget->height;
174 }
175 $self->geometry(sprintf('%dx%d+%d+%d', $width, $height, $x, $y));
176 }
177  
178 =item B<popDirection>
179  
180 Returns the direction of the popup. It is '' when not yet calculated. Can be 'up' or 'down'.
181  
182 =cut
183  
184 sub popDirection {
185 return $_[0]->{POPDIRECTION}
186 }
187  
188  
189 =item B<popDown>
190  
191 Hides the PopList.
192  
193 =cut
194  
195 sub popDown {
196 my $self = shift;
197 return unless $self->ismapped;
198 $self->withdraw;
199 $self->parent->grabRelease;
200 if (ref $self->{'_BE_grabinfo'} eq 'CODE') {
201 $self->{'_BE_grabinfo'}->();
202 delete $self->{'_BE_grabinfo'};
203 }
204 }
205  
206 =item B<popFlip>
207  
208 Hides the PopList if it shown. Shows the PopList if it is hidden.
209  
210 =cut
211  
212 sub popFlip {
213 my $self = shift;
214 if ($self->ismapped) {
215 $self->popDown
216 } else {
217 $self->popUp
218 }
219 }
220  
221 =item B<popUp>
222  
223 Shows the PopList.
224  
225 =cut
226  
227 sub popUp {
228 my $self = shift;
229  
230 return if $self->ismapped;
231  
232 $self->ConfigureSizeAndPos;
233 $self->deiconify;
234 $self->raise;
235 $self->{'_BE_grabinfo'} = $self->grabSave;
236 $self->parent->grabGlobal;
237 }
238  
239 =back
240  
241 =head1 LICENSE
242  
243 Same as Perl.
244  
245 =head1 AUTHOR
246  
247 =over 4
248  
249 =item Hans Jeuken (hanje at cpan dot org)
250  
251 =back
252  
253 =cut
254  
255 =head1 BUGS
256  
257 Unknown. If you find any, please contact the author.
258  
259 =head1 SEE ALSO
260  
261 =over 4
262  
263 =item L<Tk>
264  
265 =item L<Tk::Toplevel>
266  
267 =item L<Tk::Listbox>
268  
269 =back
270  
271 =cut
272  
273 1;
274 __END__
« PopList.pm ↑ Summary