App/Chart/Gtk2/Ex/MenuItem/EmptyInsensitive.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 108 CC 2 SCAR 9.2
CC
2
Cov
0%
_update_submenu_signals
6.0
new
2.0
_notify_submenu
2.0
CRAP 2.5
1 # a subclass of Gtk2::Menu can act on its attached parent item directly ...
2  
3  
4 # Copyright 2008, 2009, 2010 Kevin Ryde
5  
6 # This file is part of Chart.
7 #
8 # Chart is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as published
10 # by the Free Software Foundation; either version 3, or (at your option) any
11 # later version.
12 #
13 # Chart is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 # for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Chart. If not, see <http://www.gnu.org/licenses/>.
20  
21 package App::Chart::Gtk2::Ex::MenuItem::EmptyInsensitive;
22 use 5.008;
23 use strict;
24 use warnings;
25 use Gtk2;
26 use List::Util;
27  
28 use App::Chart::Glib::Ex::MoreUtils;
29 use Glib::Ex::SignalIds;
30  
31  
32  
33 # 'notify' is a no-hooks signal, so can't use an emission hook to have just
34 # one handler installed. The add and remove signals could be done that way
35 # though. It'd depend whether the speed lost to seeing every emission was
36 # was the memory saving of just one handler setup.
37 #
38  
39 # if nothing except tearoff and separators visible then consider empty
40 my @ignored_menuitem_classes = ('Gtk2::SeparatorMenuItem',
41 'Gtk2::TearoffMenuItem');
42  
43 sub new {
44 my ($class, $item) = @_;
45 my $self = bless {}, $class;
46 my $ref_weak_self = App::Chart::Glib::Ex::MoreUtils::ref_weak($self);
47  
48 $self->{'item_ids'} = Glib::Ex::SignalIds->new
49 ($item,
50 $item->signal_connect ('notify::submenu' => \&_notify_submenu,
51 $ref_weak_self));
52 _notify_submenu ($item, undef, $ref_weak_self);
53 return $self;
54 }
55  
56 # 'notify' of item "submenu" property, also called for initial setups
57 sub _notify_submenu {
58 my ($item, $pspec, $ref_weak_self) = @_;
59 my $submenu = $item->get_submenu;
60 ### SubmenuInsensitive _notify_submenu: $submenu
61 my $self = $$ref_weak_self || return;
62  
63 $self->{'submenu_ids'} = $submenu && do {
64 Glib::Ex::SignalIds->new
65 ($submenu,
66 $submenu->signal_connect (add => \&_add_or_remove_child,
67 $ref_weak_self),
68 $submenu->signal_connect (remove => \&_add_or_remove_child,
69 $ref_weak_self))
70 };
71 _update_submenu_signals ($self, $item, $ref_weak_self);
72 }
73 # 'add' or 'remove' signals on submenu
74 sub _add_or_remove_child {
75 my ($submenu, $subitem, $ref_weak_self) = @_;
76 ### SubmenuInsensitive _add_or_remove_child
77 my $self = $$ref_weak_self || return;
78 my $item = $submenu->get_attach_widget || return;
79 _update_submenu_signals ($self, $item, $ref_weak_self);
80 }
81  
82 sub _update_submenu_signals {
83 my ($self, $item, $ref_weak_self) = @_;
84 my $submenu = $item->get_submenu;
85  
86 # could add or remove for the changed child, but it's easier to just redo
87 # the lot the same as for initial setups
88 $self->{'subitem_ids_array'} = $submenu && do {
89 my @subitem_ids;
90 foreach my $subitem ($submenu->get_children) {
91 if (_is_ignored($subitem)) { next; }
92 push @subitem_ids, Glib::Ex::SignalIds->new
93 ($subitem,
94 $subitem->signal_connect ('notify::visible' => \&_notify_visible,
95 $ref_weak_self))
96 }
97 \@subitem_ids
98 };
99 _update_sensitive ($item);
100 }
101 # 'notify' of subitem "visible" property
102 sub _notify_visible {
103 my ($subitem) = @_;
104 ### SubmenuInsensitive _notify_visible
105 my $submenu = $subitem->get_parent || return;
106 my $item = $submenu->get_attach_widget || return;
107 _update_sensitive ($item);
108 }
109  
110 sub _update_sensitive {
111 my ($item) = @_;
112 ### SubmenuInsensitive _update_sensitive
113 $item->set_sensitive (_want_sensitive ($item));
114 }
115 sub _want_sensitive {
116 my ($item) = @_;
117 my $submenu = $item->get_submenu || return 0;
118 return List::Util::first { !_is_ignored($_)
119 && $_->visible } $submenu->get_children;
120 }
121 sub _is_ignored {
122 my ($subitem) = @_;
123 return List::Util::first {$subitem->isa($_)} @ignored_menuitem_classes;
124 }
125  
126 1;
127 __END__
128  
129 =for stopwords submenu Gtk ie
130  
131 =head1 NAME
132  
133 App::Chart::Gtk2::Ex::MenuItem::EmptyInsensitive -- menu item insensitive when submenu empty
134  
135 =head1 SYNOPSIS
136  
137 use App::Chart::Gtk2::Ex::MenuItem::EmptyInsensitive;
138  
139 my $item = Gtk2::MenuItem->new;
140 App::Chart::Gtk2::Ex::MenuItem::EmptyInsensitive->setup ($item);
141  
142 =head1 DESCRIPTION
143  
144 C<App::Chart::Gtk2::Ex::MenuItem::EmptyInsensitive> sets up a C<Gtk2::MenuItem> so that
145 it's insensitive if its submenu is empty, or all the items are hidden, or
146 there's no submenu set at all.
147  
148 Sensitivity updates with items added or removed from the submenu, or made
149 hidden or visible. In Gtk 2.12 and the sensitivity is updated for a new
150 submenu, ie. C<set_submenu>, but in prior versions there's no signal when
151 that happens and you must call C<setup> below again.
152  
153 =head1 FUNCTIONS
154  
155 =over 4
156  
157 =item C<< App::Chart::Gtk2::Ex::MenuItem::EmptyInsensitive->setup ($item) >>
158  
159 Setup C<$item> to be insensitive if its submenu is empty.
160  
161 =back
162  
163 =head1 SEE ALSO
164  
165 L<Gtk2::MenuItem>, L<Gtk2::Menu>
166  
167 =cut