AppWindow/Ext/MenuBar.pm untested

filter: Statementstmt 0.0% 0 / 319 Branchbran 0.0% 0 / 112 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 508 CC 57 SCAR 31.4
CC
57
Cov
0%
DecodeMenuPath
56.0
CheckStackForImages
42.0
ConfMenuCheck
42.0
CRAP 23.2
1 package Tk::AppWindow::Ext::MenuBar;
2  
3 =head1 NAME
4  
5 Tk::AppWindow::Ext::MenuBar - handling menu's and stuff.
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use Tk;
12 use vars qw($VERSION);
13 $VERSION="0.08";
14  
15 use base qw( Tk::AppWindow::BaseClasses::Extension );
16  
17 =head1 SYNOPSIS
18  
19 my $app = new Tk::AppWindow(@options,
20 -extensions => ['MenuBar'],
21 );
22 $app->MainLoop;
23  
24 =head1 DESCRIPTION
25  
26 Adds a menu to your application.
27  
28 =head1 CONFIG VARIABLES
29  
30 =over 4
31  
32 =item Switch: B<-automenu>
33  
34 Default value 1.
35  
36 Specifies if the menu items of all extensions should be loaded automatically.
37  
38 =item Switch: B<-mainmenuitems>
39  
40 Default value [].
41  
42 Configure your menu here. See the section B<CONFIGURING MENUS> below.
43  
44 =item Switch: B<-menucolspace>
45  
46 Default value 3
47  
48 Space between the colums in a menu item.
49  
50 =item Switch: B<-menuiconsize>
51  
52 Default value 16
53  
54 =back
55  
56 =cut
57  
58 sub new {
59 my $class = shift;
60 my $self = $class->SUPER::new(@_);
61  
62 $self->{MENUPOST} = [];
63 $self->Require('Keyboard');
64 $self->addPreConfig(
65 -automenu => ['PASSIVE', undef, undef, 1],
66 -mainmenuitems => ['PASSIVE', undef, undef, []],
67 -menucolspace =>['PASSIVE', undef, undef, 3],
68 -menuiconsize =>['PASSIVE', undef, undef, 16],
69 );
70  
71 $self->addPostConfig('DoPostConfig', $self);
72 return $self;
73 }
74  
75 =head1 METHODS
76  
77 =over 4
78  
79 =cut
80  
81 sub AddKeyboardBinding {
82 my $self = shift;
83 $self->GetAppWindow->cmdExecute('addkeyboardbinding', @_);
84 }
85  
86 sub CheckStackForImages {
87 my ($self, $stack) = @_;
88 my $hasimages = 0;
89 my $name = $self->configGet('-appname');
90 my $art = $self->extGet('Art');
91 for (@$stack) {
92 my %conv = ( @$_ );
93 if (exists $conv{'cascade'}) {
94 $self->CheckStackForImages($conv{'-menuitems'})
95 }
96 if (exists $conv{'-image'}) {
97 $hasimages = 1;
98 }
99 $_->[1] =~ s/appname/$name/;
100
101 }
102 if ($hasimages) {
103 my $size = $self->configGet('-menuiconsize');
104 my $empty = $art->createEmptyImage($size) if defined $art;
105 for (@$stack) {
106 my %conv = ( @$_ );
107 unless ((exists $conv{'-image'}) and (defined $empty)) {
108 push @$_, -image => $empty, -compound => 'left';
109 }
110 }
111 }
112 }
113  
114 sub ConfDoConfig {
115 my ($self, $config, $item) = @_;
116 if (defined $config) {
117 my $val = '';
118 $self->MenuPostAdd(sub { $val = $self->configGet($config) });
119 push @$item, -variable => \$val, -command => sub { $self->configPut($config, $val) }
120 }
121 }
122  
123 sub ConfDoIcon {
124 my ($self, $icon, $item) = @_;
125 if (defined $icon) {
126 my $size = $self->configGet('-menuiconsize');
127 my $bmp = $self->getArt($icon, $size);
128 if (defined $bmp) {
129 push @$item, -image => $bmp, -compound => 'left'
130 }
131 }
132 }
133  
134 sub ConfDoKeyb {
135 my ($self, $command, $keyb, $item) = @_;
136 if (defined $keyb) {
137 if ((defined $command) and (not $keyb =~ s/^\*//)) {
138 my $kb = $self->GetAppWindow->extGet('Keyboard');
139 $kb->AddBinding($command, $keyb);
140 }
141 push @$item, -accelerator => $keyb;
142 }
143 }
144  
145 my %types = (
146 menu => \&ConfMenu,
147 menu_check => \&ConfMenuCheck,
148 menu_normal => \&ConfMenuNormal,
149 menu_radio => \&ConfMenuRadio,
150 menu_radio_s => \&ConfMenuRadioGroup,
151 menu_separator => \&ConfMenuSeparator,
152 );
153  
154 sub Configure {
155 my $self = shift;
156 my $w = $self->GetAppWindow;
157 my $menu = $w->cget('-menu');
158 unless (defined $menu) {
159 $menu = $w->Menu;
160 $w->configure(-menu => $menu);
161 } else {
162 $menu->delete(0, 'end');
163 }
164 my $stack = $self->menuStack(@_);
165 $self->FillMenu($menu, $stack);
166 }
167  
168 sub createMenu {
169 my $self = shift;
170 my $parent = shift;
171 my $mpsave = $self->{MENUPOST};
172 my $calls = [];
173 $self->{MENUPOST} = $calls;
174 my $menu = $parent->Menu(
175 -menuitems => $self->menuStack(@_),
176 -postcommand => sub {
177 for (@$calls) {
178 if (ref $_) {
179 &$_
180 } else {
181 $self->cmdExecute($_)
182 }
183 }
184 },
185 );
186 $self->{MENUPOST} = $mpsave;
187 return $menu
188 }
189  
190 sub ConfGetCommand {
191 my ($self, $cmd) = @_;
192 my $w = $self->GetAppWindow;
193 if (defined $cmd) {
194 if ($cmd =~ /^<.+>/) {
195 return -command => ['eventGenerate', $w, $cmd]
196 } else {
197 return -command => ['cmdExecute', $w, $cmd];
198 }
199 }
200 }
201  
202 sub ConfMenu {
203 my ($self, $stack, $path, $label, $cmd, $icon) = @_;
204 my ($menu, $insertpos) = $self->DecodeMenuPath($stack, $path);
205 return 0 unless defined $insertpos;
206 my @item = ('cascade', $label,
207 -menuitems => [],
208 -postcommand => sub { $self->MenuPost },
209 );
210 $self->ConfDoIcon($icon,\@item);
211 if (defined $cmd) {
212 $self->MenuPostAdd($cmd);
213 }
214 if ($insertpos <= @$menu) {
215 splice @$menu, $insertpos, 0, \@item
216 } else {
217 push @$menu, \@item
218 }
219 return 1
220 }
221  
222 sub ConfMenuNormal {
223 my ($self, $stack, $path, $label, $cmd, $icon, $keyb) = @_;
224 my ($menu, $insertpos) = $self->DecodeMenuPath($stack, $path);
225 return 0 unless defined $insertpos;
226 my @item = ('command', $label);
227 push @item, $self->ConfGetCommand($cmd) if defined $cmd;
228 $self->ConfDoKeyb($cmd, $keyb,\@item);
229 $self->ConfDoIcon($icon,\@item);
230 if ($insertpos <= @$menu) {
231 splice @$menu, $insertpos, 0, \@item
232 } else {
233 push @$menu, \@item
234 }
235 return 1
236 }
237  
238 sub ConfMenuCheck {
239 my ($self, $stack, $path, $label, $icon, $config, $cmd, $offvalue, $onvalue) = @_;
240 my ($menu, $insertpos) = $self->DecodeMenuPath($stack, $path);
241 return 0 unless defined $insertpos;
242 my $w = $self->GetAppWindow;
243  
244 my @item = ('checkbutton', $label);
245  
246 push @item, -offvalue => $offvalue if defined $offvalue;
247 push @item, -onvalue => $onvalue if defined $onvalue;
248 push @item, $self->ConfGetCommand($cmd) if defined $cmd;
249  
250  
251 $self->ConfDoIcon($icon,\@item);
252 $self->ConfDoConfig($config,\@item);
253 if ($insertpos <= @$menu) {
254 splice @$menu, $insertpos, 0, \@item
255 } else {
256 push @$menu, \@item
257 }
258 return 1
259 }
260  
261 sub ConfMenuRadio {
262 my ($self, $stack, $path, $label, $icon, $config, $cmd, $value) = @_;
263 my ($menu, $insertpos) = $self->DecodeMenuPath($stack, $path);
264 return 0 unless defined $insertpos;
265 my $w = $self->GetAppWindow;
266  
267 my @item = ('radiobutton', $label);
268  
269 push @item, -value => $value if defined $value;
270 push @item, $self->ConfGetCommand($cmd) if defined $cmd;
271  
272 $self->ConfDoIcon($icon,\@item);
273 $self->ConfDoConfig($config,\@item);
274 if ($insertpos <= @$menu) {
275 splice @$menu, $insertpos, 0, \@item
276 } else {
277 push @$menu, \@item
278 }
279 return 1
280 }
281  
282 sub ConfMenuRadioGroup {
283 my ($self, $stack, $path, $label, $values, $icon, $config, $cmd) = @_;
284 my ($menu, $insertpos) = $self->DecodeMenuPath($stack, $path);
285 return 0 unless defined $insertpos;
286 my @item = ();
287 my @group = ();
288 $self->ConfDoConfig($config,\@item);
289 for (@$values) {
290 my $value = $_;
291 my @i = @item;
292 unshift @i, $value;
293 unshift @i, 'radiobutton';
294 push @i, -value => $value;
295 push @i, -command => $cmd if defined $cmd;
296 push @group, \@i;
297 }
298 my @mnu = ('cascade' => $label,
299 -menuitems => \@group
300 );
301 $self->ConfDoIcon($icon,\@mnu);
302 if ($insertpos <= @$menu) {
303 splice @$menu, $insertpos, 0, \@mnu
304 } else {
305 push @$menu, \@mnu
306 }
307 return 1
308 }
309  
310 sub ConfMenuSeparator {
311 my ($self, $stack, $path, $label) = @_;
312 my ($menu, $insertpos) = $self->DecodeMenuPath($stack, $path);
313 return 0 unless defined $insertpos;
314 my @item = ('separator', $label);
315 if ($insertpos <= @$menu) {
316 splice @$menu, $insertpos, 0, \@item
317 } else {
318 push @$menu, \@item
319 }
320 return 1
321 }
322  
323 sub CreateMenu {
324 my $self = shift;
325 my $w = $self->GetAppWindow;
326 my @u = ();
327 if ($w->configGet('-automenu')) {
328 my @p = $self->extList;
329 my @l = ($w);
330 for (@p) { push @l, $self->extGet($_) }
331 for (@l) {
332 push @u, $_->MenuItems;
333 }
334 }
335 my $m = $w->configGet('-mainmenuitems');
336 push @u, @$m;
337 $self->Configure(@u);
338 }
339  
340 sub DecodeMenuPath {
341 my ($self, $stack, $path) = @_;
342 unless (defined $path) {
343 my $end = @$stack;
344 return ($stack, $end);
345 }
346 #first weed through the tree of submenu's, if any
347 while ($path =~ s/^([^\:]+)\:\://) {
348 my $item = $1;
349 my $size = @$stack;
350 my $count = 0;
351 my $found = 0;
352 while (($count < $size) and (not $found)) {
353 my $label = $stack->[$count]->[1];
354 $label =~ s/\~//g;
355 if ($label eq $item) {
356 $found = 1;
357 if ($stack->[$count]->[0] eq 'cascade') {
358 $stack = $stack->[$count]->[3];
359 if ($path eq '') {
360 my $s = @$stack;
361 return ($stack, $s)
362 }
363 } else {
364 return ($stack, undef)
365 }
366 } else {
367 $count ++;
368 }
369 }
370 }
371 # if the last character in the path is a |, the item should be inserted after the current one.
372 my $offset = 0;
373 $offset = 1 if $path =~ s/\|$//;
374  
375 my $size = @$stack;
376 my $count = 0;
377 while ($count < $size) {
378 my $label = $stack->[$count]->[1];
379 $label =~ s/\~//g;
380 if ($label eq $path) {
381 return ($stack, $count + $offset);
382 } else {
383 $count ++;
384 }
385 }
386 return ($stack, undef)
387 }
388  
389 sub DoPostConfig {
390 my $self = shift;
391 my $art = $self->extGet('Art');
392 if (defined $art) {
393 my $size = $self->configGet('-menuiconsize');
394 $size = $art->getAlternateSize($size);
395 $self->configPut(-menuiconsize => $size)
396 }
397 $self->CreateMenu;
398 }
399  
400 sub FillMenu {
401 my ($self, $menu, $items) = @_;
402 while (@$items) {
403 my $item = shift @$items;
404 my $type = shift @$item;
405 my $label = shift @$item;
406 my %opt = @$item;
407 if ($label =~ /^([^~]*)~([^~]*)/) {
408 $opt{'-underline'} = length($1);
409 $label = $1 . $2
410 }
411 $opt{'-label'} = $label;
412 if ($type eq 'cascade') {
413 my $menuitems = delete $opt{'-menuitems'};
414  
415 my %mnuopt = ();
416 for (qw/postcommand selectcolor tearoff tearoffcommand title type/) {
417 my $name = "-$_";
418 my $val = delete $opt{$name};
419 $mnuopt{$name} = $val if defined $val;
420 }
421  
422 my $submenu = $menu->Menu(%mnuopt);
423 $opt{'-menu'} = $submenu;
424 $menu->add('cascade', %opt);
425 $self->FillMenu($submenu, $menuitems) if defined $menuitems;
426 } elsif ($type eq 'separator') {
427 $menu->add('separator');
428 } else {
429 $menu->add($type, %opt);
430 }
431 }
432 }
433  
434 sub FindMenuEntry {
435 my ($self, $path) = @_;
436 my $menu = $self->configGet('-menu');
437 my $p = $path;
438 while ($p =~ s/([^\:]+)\:\://) {
439 my $item = $1;
440 for (1 .. $menu->index('last')) {
441 if ($item eq $menu->entrycget($_, '-label')) {
442 if ($menu->type($_) eq 'cascade') {
443 $menu = $menu->entrycget($_, '-menu');
444 }
445 last
446 }
447 }
448 }
449 for (1 .. $menu->index('last')) {
450 next if $menu->type($_) eq 'separator';
451 if ($p eq $menu->entrycget($_, '-label')) {
452 return ($menu, $_);
453 }
454 }
455 warn "Menu entry $path not found";
456 }
457  
458 =item B<menuContext>I<($parent, @menulist)>
459  
460 Same as B<menuCreate>, but binds it unpost at <Leave> basically turning
461 it into a context menu.
462  
463 =cut
464  
465 sub menuContext {
466 my $self = shift;
467 my $menu = $self->menuCreate(@_);
468 $menu->bind('<Leave>', sub { $menu->unpost });
469 return $menu
470 }
471  
472 =item B<menuCreate>I<($parent, @menulist)>
473  
474 Returns a Tk::Menu object. It's items in @menulist are according to the
475 section below CONFIGURING MENUS.
476  
477 =cut
478  
479 sub menuCreate {
480 my $self = shift;
481 my $parent = shift;
482 my $mpsave = $self->{MENUPOST};
483 my $calls = [];
484 $self->{MENUPOST} = $calls;
485 my $menu = $parent->Menu(
486 -menuitems => $self->menuStack(@_),
487 -postcommand => sub {
488 for (@$calls) {
489 if (ref $_) {
490 &$_
491 } else {
492 $self->cmdExecute($_)
493 }
494 }
495 },
496 );
497 $self->{MENUPOST} = $mpsave;
498 return $menu
499 }
500  
501 sub MenuPost {
502 my $self = shift;
503 my $calls = $self->{MENUPOST};
504 for (@$calls) {
505 if (ref $_) {
506 &$_
507 } else {
508 $self->cmdExecute($_)
509 }
510 }
511 }
512  
513 sub MenuPostAdd {
514 my ($self, $cmd) = @_;
515 my $calls = $self->{MENUPOST};
516 push @$calls, $cmd
517 }
518  
519 =item B<menuStack>I<(@menulist)>
520  
521 Returns a reference to a list that can be used as the B<-menuitems>
522 at menu creation. So:
523  
524 my $menu = $w->Menu(-menuitems => $menubar->menuStack(@menulist);
525
526  
527 Items in @menulist are according to the section below CONFIGURING MENUS.
528  
529 =cut
530  
531 sub menuStack {
532 my $self = shift;
533 my $stack = [];
534 my @menuitems = @_;
535 my $count = 0;
536 my $keeploop = 1;
537 while ((@menuitems) and $keeploop) {
538 my $i = shift @menuitems;
539 my @item = @$i;
540 my $type = shift @item;
541 if (exists $types{$type}) {
542 my $c = $types{$type};
543 unless (&$c($self, $stack, @item)) {
544 push @menuitems, $i
545 }
546 } else {
547 warn "undefined menu type $type"
548 }
549 $count ++;
550 if ($count > 1000) {
551 warn "Invalid menupath: " . $item[0];
552 $keeploop = 0;
553 }
554 }
555 $self->CheckStackForImages($stack);
556 return $stack
557 }
558  
559 sub ReConfigure {
560 my $self = shift;
561 $self->{MENUPOST} = [];
562 # $self->DeleteAll;
563 $self->CreateMenu;
564 return 0;
565 }
566  
567 =back
568  
569 =head1 CONFIGURING MENUS
570  
571 Feeding the B<-menuitems> switch and the B<MenuItems> methods of extensions is
572 done with a two dimensional list. In Perl:
573  
574 my $app = new Tk::AppWindow(@options,
575 -extensions => ['MenuBar'],
576 -menuitems => [
577 [ $type, $path, @options ],
578 [ $type2, $path2, @options2 ],
579 ],
580 );
581 $app->MainLoop;
582  
583 Variables used below have the following meaning:
584  
585 =over 4
586  
587 =item B<$path>
588  
589 Determines where the menu entry should be placed.
590 Lets document by example. Suppose you have a menu structure like this:
591  
592 ~File -> ~Open
593 -> ~Close
594  
595 Where ~File is a submenu on the main menu bar and ~Open and ~close are it's entries.
596  
597 =over 4
598  
599 =item $path = B<undef>
600  
601 Adds your item to the end of the main menubar.
602  
603 =item $path = 'B<File>'
604  
605 Inserts your item in the main menubar before ~File
606  
607 =item $path = 'B<File|>'
608  
609 Inserts your item in the main menubar after ~File
610  
611 =item $path = 'B<File::>'
612  
613 Adds your item to the end of the ~File menu
614  
615 =item $path = 'B<File::Close>'
616  
617 Inserts your item in the ~File menu before the ~Close entry
618  
619 =item $path = 'B<File::Open|>'
620  
621 Inserts your item in the ~File menu after the ~Open entry
622  
623 =back
624  
625 =item B<$label>
626  
627 The name of the menu entry. You can use the '~' to indicate a shortcut.
628  
629 =item B<$cmd>
630  
631 The command executed or event generated on selection of the menu item.
632 A command should be defined with B<cmdConfig>. You can set it to undef
633 if no command is needed.
634  
635 =item B<$icon>
636  
637 The name of an icon available through the B<getArt> method.
638 You can set it to undef if you do not want an icon.
639  
640 =item B<$config>
641  
642 For checkbutton and radiobutton entries. The config variable linked
643 to this entry. You can set it to undef if no config variable needs
644 to be involved.
645  
646 =item B<$keyboard>
647  
648 The name of the keyboard shortcut to be associated with this
649 entry. The notation is that of modern desktops. The keyboard
650 bindings will actually be created. If you don't want this
651 you can add a '*' in front. Examples:
652  
653 =over 4
654  
655 =item CTRL+A
656  
657 =item SHIFT+F10
658  
659 =item *CTRL+C
660  
661 =back
662  
663 =item B<$offvalue>
664  
665 =item B<$onvalue>
666  
667 For checkbutton entries. If you do not specify them they default to 0 and 1.
668  
669 =item B<$value>
670  
671 For radiobutton entries.
672  
673 =item B<$values>
674  
675 A reference to a list of values: ['value1', 'value2'].
676  
677 =back
678  
679 The following types are available:
680  
681 =over 4
682  
683 =item ['B<menu>', I<$path, $label, $cmd, $icon>],
684  
685 Creates a submenu.
686  
687 =item ['B<menu_normal>', I<$path, $label, $cmd, $icon, $keyboard>],
688  
689 Creates a command entry.
690  
691 =item ['B<menu_check>', I<$path, $label, $icon, $config, $cmd, $offvalue, $onvalue>],
692  
693 Creates a checkbutton entry.
694  
695 =item ['B<menu_radio>', I<$path, $label, $icon, $config, $cmd, $value>],
696  
697 Creates a radiobutton entry.
698  
699 =item ['B<menu_radio_s>', I<$path, $label, $values, $icon, $config, $cmd>],
700  
701 Creates a submenu with a group of radiobuttons.
702  
703 =item ['B<menu_separator>', I<$path, $label>],
704  
705 Creates a separator. Although separators do not need a label, we prefer
706 to still do so. They act as a path indicator for the path of other entries.
707  
708 =back
709  
710 =head1 AUTHOR
711  
712 Hans Jeuken (hanje at cpan dot org)
713  
714 =head1 BUGS
715  
716 Unknown. If you find any, please contact the author.
717  
718 =head1 SEE ALSO
719  
720 =over 4
721  
722 =item L<Tk::AppWindow>
723  
724 =item L<Tk::AppWindow::BaseClasses::Extension>
725  
726 =back
727  
728 =cut
729  
730 1;
731  
732  
733  
734