AppWindow/Ext/SideBars.pm untested

filter: Statementstmt 0.0% 0 / 213 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 338 CC 27 SCAR 25.2
CC
27
Cov
0%
pageAdd
42.0
nbMaximize
30.0
pageImage
20.0
CRAP 12.4
1 package Tk::AppWindow::Ext::SideBars;
2  
3 =head1 NAME
4  
5 Tk::AppWindow::Ext::SideBars - Basic functionality for side bars.
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use vars qw($VERSION);
12 $VERSION = "0.20";
13 use Tk;
14 require Tk::YANoteBook;
15  
16 use base qw( Tk::AppWindow::BaseClasses::Extension );
17  
18 =head1 SYNOPSIS
19  
20 my $app = new Tk::AppWindow(@options,
21 -extensions => ['SideBars'],
22 );
23 $app->MainLoop;
24  
25 =head1 DESCRIPTION
26  
27 Provides a primer for panels that contain a resizable YANoteBook for
28 selecting various tools.
29  
30 It inherits L<Tk::AppWindow::BaseClasses::Extension>
31  
32 =head1 CONFIG VARIABLES
33  
34 =over 4
35  
36 =item Switch: B<-sidebariconsize> I<hookable>
37  
38 Default value 32.
39  
40 =back
41  
42 =cut
43  
44 sub new {
45 my $class = shift;
46 my $self = $class->SUPER::new(@_);
47 $self->Require('Panels');
48 $self->{ICONSIZE} = 22;
49 $self->{INITIALSIZES} = {};
50 $self->{LASTSIZE} = {};
51 $self->{PAGES} = {};
52 $self->{PANELS} = {};
53 $self->{SELECTCALLS} = {};
54 $self->{UNSELECTCALLS} = {};
55 $self->{TABSIDES} = {};
56 $self->{TEXTSIDES} = {};
57 $self->{TEXTROTATES} = {};
58  
59 $self->configInit(
60 '-sidebariconsize' => ['IconSize', $self, 22]
61 );
62 return $self;
63 }
64  
65 =head1 METHODS
66  
67 =over 4
68  
69 =item B<canRotateText>
70  
71 Returns true if facilities for rotating text are in place.
72 On linux this means the command 'fc-list' works.
73  
74 =cut
75  
76 sub canRotateText {
77 my $self = shift;
78 my $art = $self->extGet('Art');
79 return 0 unless defined $art;
80 return $art->canRotateText;
81 }
82  
83 =item B<IconSize>I<(?$size?)>
84  
85 Set and return the iconsize in the tabs of the notebooks
86  
87 =cut
88  
89 sub IconSize {
90 my $self = shift;
91 $self->{ICONSIZE} = shift if @_;
92 return $self->{ICONSIZE};
93 }
94  
95 =item B<nbAdd>I<($name, $panel, $tabside)>
96  
97 Creates a new notebook widget and assigns it to I<$panel>.
98 If you do not specify I<$tabside> it is set to 'top'.
99  
100 =cut
101  
102 sub nbAdd {
103 my ($self, $name, $panel, $tabside) = @_;
104 $tabside = 'top' unless defined $tabside;
105 my $nb = $self->Subwidget($panel)->YANoteBook(
106 -onlyselect => 0,
107 -rigid => 0,
108 -selecttabcall => ['TabSelect', $self, $name],
109 -tabside => $tabside,
110 -unselecttabcall => ['TabUnselect', $self, $name],
111 )->pack(-expand => 1, -fill=> 'both', -padx => 2, -pady => 2);
112 $self->{TABSIDES}->{$name} = $tabside;
113 $self->after(200, sub {
114 $nb->Subwidget('MoreButton')->configure(-image => $self->getArt('document-multiple', 16))
115 });
116 $self->Advertise($name . 'NB', $nb);
117 my $pn = $self->extGet('Panels');
118 $pn->adjusterWidget($panel, $nb);
119 $pn->adjusterActive($panel, 0);
120 $pn->panelAssign($name, $panel);
121 $pn->panelShow($panel);
122 }
123  
124 =item B<nbDelete>I<($name)>
125  
126 Destroys notebook I<$name>.
127  
128 =cut
129  
130 sub nbDelete {
131 my ($self, $name) = @_;
132 my $pn = $self->extGet('Panels');
133 my $panel = $pn->panelGet($name);
134 # $self->geoDeleteCall($panel);
135 $pn->panelDelete($name);
136 delete $self->textsides->{$name};
137 $name = $name . 'NB';
138 $self->Subwidget($name)->destroy;
139 }
140  
141 sub nbExists {
142 my ($self, $name) = @_;
143 $name = $name . 'NB';
144 my $book = $self->Subwidget($name);
145 return Exists $book if defined $book;
146 return undef
147 }
148  
149 =item B<nbGet>I($name)>
150  
151 Returns a reference to notebook widget I<$name>.
152  
153 =cut
154  
155 sub nbGet {
156 my ($self, $name) = @_;
157 $name = $name . 'NB';
158 return $self->Subwidget($name);
159 }
160  
161 =item B<nbMaximize>I<($name, $tab)>
162  
163 Maximizes notebook I<$name> at the requested size of I<$tab>
164  
165 =cut
166  
167 sub nbMaximize {
168 my ($self, $notebook, $tab) = @_;
169 my $nb = $self->nbGet($notebook);
170 my $pf = $nb->Subwidget('PageFrame');
171 my $tf = $nb->Subwidget('TabFrame');
172 my $panel = $self->extGet('Panels')->panelAssign($notebook);
173 $panel = $self->Subwidget($panel);
174 my $offset = $self->nbOffset($notebook);
175 my $height = $panel->height;;
176 my $width = $panel->width;
177 my $ls = $self->{LASTSIZE}->{$tab};
178 $ls = $self->{INITIALSIZES}->{$tab} unless defined $ls;
179 my $ts = $self->{TABSIDES}->{$notebook};
180 if (defined $ls) {
181 my ($w, $h) = @$ls;
182 if (($ts eq 'top') or ($ts eq 'bottom')) {
183 $height = $h
184 } else {
185 $width = $w
186 }
187 } else {
188 if (($ts eq 'top') or ($ts eq 'bottom')) {
189 $height = 150;
190 } else {
191 $width = 300;
192 }
193 }
194 $nb->configure(-width => $width);
195 $nb->configure(-height => $height);
196 }
197  
198 =item B<nbMinimize>I<($name, $tab)>
199  
200 Minimizes notebook I<$name> and saves the size of I<$tab>.
201  
202 =cut
203  
204 sub nbMinimize {
205 my ($self, $notebook, $tab) = @_;
206 my $nb = $self->nbGet($notebook);
207 my $tf = $nb->Subwidget('TabFrame');
208 $self->{LASTSIZE}->{$tab} = [$nb->width, $nb->height];
209 my $ts = $self->{TABSIDES}->{$notebook};
210 my $offset = $self->nbOffset($notebook);
211 my ($width, $height);
212 if (($ts eq 'top') or ($ts eq 'bottom')) {
213 $width = $nb->width + $offset;
214 $height = $tf->height + $offset;
215 } else {
216 $width = $tf->width + $offset;
217 $height = $nb->height + $offset;
218 }
219 $nb->configure(-width => $width);
220 $nb->configure(-height => $height);
221 }
222  
223 sub nbOffset {
224 my ($self, $notebook) = @_;
225 my $nb = $self->nbGet($notebook);
226 my $tf = $nb->Subwidget('TabFrame');
227 return (($tf->cget('-borderwidth') + $nb->cget('-borderwidth')) * 2) +1
228 }
229  
230 =item B<nbTextSide>I($name, $side)>
231  
232 Specifies where the text should be shown relative to the image in the
233 page tags of sidebar $name. Posible values are 'left', 'right', 'top', 'bottom'
234 and 'none'. If you do not call this method for a notebook it will be treated as
235 'none'.
236  
237 =cut
238  
239 sub nbTextSide {
240 my ($self, $name, $side) = @_;
241 $self->textsides->{$name} = $side if defined $side;
242 return $self->textsides->{$name}
243 }
244  
245 =item B<nbTextRotate>I($name, $degrees)>
246  
247 Specifies with how many degrees text on tags of sidebar $name
248 should be rotated. If you do not call this method for a notebook
249 it will be treated as 0 degrees.
250  
251 =cut
252  
253 sub nbTextRotate {
254 my ($self, $name, $rotate) = @_;
255 $self->textrotates->{$name} = $rotate if defined $rotate;
256 return $self->textrotates->{$name}
257 }
258  
259 =item B<pageAdd>I<($notebook, $name, $image, $text, $statustext, $initialsize)>
260  
261 Adds a page to a notebook.
262  
263 =cut
264  
265 sub pageAdd {
266 my ($self, $notebook, $name, $image, $text, $statustext, $initialsize) = @_;
267 $text = $name, unless defined $text;
268  
269 $initialsize = 200 unless defined $initialsize;
270 $self->{INITIALSIZES}->{$name} = [$initialsize, $initialsize];
271
272 my $nb = $self->nbGet($notebook);
273  
274 my @opt = ();
275 my $art = $self->extGet('Art');
276 my $icon = $self->pageImage($notebook, $image, $text);
277 @opt = (-title => $text) unless defined $icon;
278 @opt = (-titleimg => $icon) if defined $icon;
279 my $page = $nb->addPage($name, @opt);
280 $self->pages->{$name} = [$notebook, $image, $text];
281  
282 my $l = $nb->getTab($name)->Subwidget('Label');
283 $self->StatusAttach($l, $statustext) if defined $statustext;
284 $self->BalloonAttach($l, $text);
285 $self->after(500, sub { $nb->UpdateTabs });
286  
287 return $page;
288 }
289  
290 =item B<pageCount>I<($notebook)>
291  
292 Returns the number of pages in $notebook.
293  
294 =cut
295  
296 sub pageCount {
297 my ($self, $notebook) = @_;
298 my $book = $self->nbGet($notebook);
299 return $book->pageCount
300 }
301  
302 =item B<pageDelete>I<($notebook, $name)>
303  
304 Deletes a page from a notebook.
305  
306 =cut
307  
308 sub pageDelete {
309 my ($self, $notebook, $name) = @_;
310 $self->nbGet($notebook)->deletePage($name);
311 delete $self->{INITIALSIZES}->{$name};
312 delete $self->{LASTSIZE}->{$name};
313 delete $self->{SELECTCALLS}->{$name};
314 delete $self->{UNSELECTCALLS}->{$name};
315 delete $self->pages->{$name}
316 }
317  
318 =item B<pageExists>I<($notebook, $name)>
319  
320 Returns true if I<$name> exists in I<$notebook>.
321  
322 =cut
323  
324 sub pageExists {
325 my ($self, $notebook, $name) = @_;
326 my $book = $self->nbGet($notebook);
327 return $book->pageExists($name);
328 }
329  
330 sub pageImage {
331 my ($self, $nb, $icon, $text) = @_;
332 my $art = $self->extGet('Art');
333 return undef unless defined $icon;
334 my $img;
335 if (defined $art) {
336 my $image = $art->getIcon($icon, $self->IconSize);
337 my $side = $self->textsides->{$nb};
338 $side = 'none' unless defined $side;
339 my $rotate = $self->textrotates->{$nb};
340  
341 $img = $art->createCompound(
342 -textside => $side,
343 -image => $image,
344 -text => $text,
345 -textrotate => $rotate,
346 );
347 }
348 return $img;
349 }
350  
351 sub pages { return $_[0]->{PAGES} }
352  
353 =item B<pageSelectCall>I<($page, @callback)>
354  
355 Creates a callback called when $page is selected.
356  
357 =cut
358  
359 sub pageSelectCall {
360 my $self = shift;
361 my $page = shift;
362 $self->{SELECTCALLS}->{$page} = $self->CreateCallback(@_);
363 }
364  
365 =item B<pageUnselectCall>I<($page, @callback)>
366  
367 Creates a callback called when $page is unselected.
368  
369 =cut
370  
371 sub pageUnselectCall {
372 my $self = shift;
373 my $page = shift;
374 $self->{UNSELECTCALLS}->{$page} = $self->CreateCallback(@_);
375 }
376  
377 sub panelOffset {
378 my ($self, $notebook) = @_;
379 my $nb = $self->nbGet($notebook);
380 my $border = $nb->cget('-borderwidth');
381 my $pad = 0;
382 my %pi = $nb->packInfo;
383 $pad = $pi{'-padx'} if exists $pi{'-padx'};
384 $pad = $pi{'-pady'} if exists $pi{'-pady'};
385 return ($border + $pad) * 2;
386 }
387  
388 sub ReConfigure {
389 my $self = shift;
390 my $pgs = $self->pages;
391 for (keys %$pgs) {
392 my $page = $_;
393 my $val = $pgs->{$page};
394 my ($nb, $img, $text) = @$val;
395 my $book = $self->nbGet($nb);
396 my $tab = $book->getTab($page);
397  
398 my $icon = $self->pageImage($nb, $img, $text);
399 $tab->configure(-titleimg => $icon) if defined $icon
400 }
401 }
402  
403 sub TabSelect {
404 my ($self, $notebook, $tab) = @_;
405 return if $self->configMode;
406 # $self->geoBlock(1);
407 my $pn = $self->extGet('Panels');
408 $self->after(1, sub {
409 $self->nbMaximize($notebook, $tab);
410 my $p = $pn->panelAssign($notebook);
411 $pn->adjusterSet($p);
412 $pn->adjusterActive($p, 1);
413 my $call = $self->{SELECTCALLS}->{$tab};
414 $call->execute if defined $call;
415 });
416 # $self->after(200, ['geoBlock', $self, 0]);
417 }
418  
419 sub TabUnselect {
420 my ($self, $notebook, $tab) = @_;
421 return if $self->configMode;
422 my $pn = $self->extGet('Panels');
423 my $p = $pn->panelAssign($notebook);
424 $pn->adjusterClear($p);
425 $pn->adjusterActive($p, 0);
426 # $self->geoBlock(1);
427 $self->nbMinimize($notebook, $tab);
428 my $call = $self->{UNSELECTCALLS}->{$tab};
429 $call->execute if defined $call;
430 # $self->after(400, ['geoBlock', $self, 0]);
431 }
432  
433 sub textsides { return $_[0]->{TEXTSIDES} }
434  
435 sub textrotates { return $_[0]->{TEXTROTATES} }
436  
437 =back
438  
439 =head1 AUTHOR
440  
441 Hans Jeuken (hanje at cpan dot org)
442  
443 =head1 BUGS
444  
445 Unknown. If you find any, please contact the author.
446  
447 =head1 SEE ALSO
448  
449 =over 4
450  
451 =item L<Tk::AppWindow::BaseClasses::Extension>
452  
453 =item L<Tk::AppWindow::BaseClasses::PanelExtension>
454  
455 =item L<Tk::AppWindow>
456  
457  
458 =back
459  
460 =cut
461  
462 1;
463 __END__