Menu/EnumRadio.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 132 CC 9 SCAR 22.3
CC
9
Cov
0%
SET_PROPERTY
20.0
set_active_nick
20.0
GET_PROPERTY
6.0
CRAP 9.3
1 # Copyright 2010, 2011 Kevin Ryde
2  
3 # This file is part of Gtk2-Ex-ComboBoxBits.
4 #
5 # Gtk2-Ex-ComboBoxBits is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Gtk2-Ex-ComboBoxBits is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Gtk2-Ex-ComboBoxBits. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Gtk2::Ex::Menu::EnumRadio;
19 use 5.008;
20 use strict;
21 use warnings;
22 use Gtk2;
23 use Glib::Ex::FreezeNotify;
24 use Glib::Ex::SignalBits;
25 use Glib::Ex::EnumBits;
26  
27 use Gtk2::Ex::Menu::EnumRadio::Item;
28  
29 # uncomment this to run the ### lines
30 #use Smart::Comments;
31  
32 our $VERSION = 32;
33  
34 use Glib::Object::Subclass
35 'Gtk2::Menu',
36 signals => { 'nick-to-display'
37 => { param_types => ['Glib::String'],
38 return_type => 'Glib::String',
39 flags => ['action','run-last'],
40 class_closure => \&default_nick_to_display,
41 accumulator => \&Glib::Ex::SignalBits::accumulator_first_defined,
42 },
43 },
44 properties => [
45 # FIXME: default enum-type is undef but
46 # Glib::ParamSpec->gtype() comes out as 'Glib::Enum'
47 #
48 (Glib::Param->can('gtype')
49 ?
50 # gtype() new in Glib 2.10 and Perl-Glib 1.240
51 Glib::ParamSpec->gtype
52 ('enum-type',
53 'Enum type',
54 'The enum class to display.',
55 'Glib::Enum',
56 Glib::G_PARAM_READWRITE)
57 :
58 Glib::ParamSpec->string
59 ('enum-type',
60 'Enum type',
61 'The enum class to display.',
62 (eval {Glib->VERSION(1.240);1}
63 ? undef # default
64 : ''), # no undef/NULL before Perl-Glib 1.240
65 Glib::G_PARAM_READWRITE)),
66  
67 Glib::ParamSpec->string
68 ('active-nick',
69 'Active nick',
70 'The selected enum value, as its nick.',
71 (eval {Glib->VERSION(1.240);1}
72 ? undef # default
73 : ''), # no undef/NULL before Perl-Glib 1.240
74 Glib::G_PARAM_READWRITE),
75 ];
76  
77 # sub INIT_INSTANCE {
78 # my ($self) = @_;
79 # }
80  
81 sub GET_PROPERTY {
82 my ($self, $pspec) = @_;
83 my $pname = $pspec->get_name;
84 ### Enum GET_PROPERTY: $pname
85  
86 if ($pname eq 'active_nick') {
87 return $self->get_active_nick;
88 }
89 # $pname eq 'enum_type'
90 return $self->{$pname};
91 }
92  
93 sub SET_PROPERTY {
94 my ($self, $pspec, $newval) = @_;
95 my $pname = $pspec->get_name;
96 ### Enum SET_PROPERTY: $pname, $newval
97  
98 if ($pname eq 'enum_type') {
99 my $enum_type = $self->{$pname} = $newval;
100  
101 # preserve active-nick if the current active exists in the new
102 # enum-type, perhaps at a different position
103 $newval = $self->get('active-nick');
104  
105 # remove old
106 foreach my $item ($self->get_children) {
107 if ($item->isa ('Gtk2::Ex::Menu::EnumRadio::Item')) {
108 ### remove: "$item"
109 $self->remove ($item);
110 }
111 }
112 ### children now: "@{[$self->get_children]}"
113  
114 # add new
115 if (defined $enum_type && $enum_type ne '') {
116 my $item;
117 foreach my $info (Glib::Type->list_values($enum_type)) {
118 my $nick = $info->{'nick'};
119 my $str = $self->signal_emit ('nick-to-display', $nick);
120 $item = Gtk2::Ex::Menu::EnumRadio::Item->new_with_label
121 ($str);
122 $item->set (nick => $nick);
123 ### sig: $item->signal_connect (activate => sub { print "activate $item ",$item->get_active,"\n" })
124 $item->show;
125 $self->append ($item);
126 }
127 }
128 }
129  
130 # $pname eq 'active_nick', and also fall-through from 'enum_type' possibly
131 # preserving active nick
132 $self->set_active_nick ($newval);
133 ### actives now: map {$_->get_active} $self->get_children
134 }
135  
136 sub default_nick_to_display {
137 my ($self, $nick) = @_;
138 my $enum_type;
139 return (($enum_type = $self->{'enum_type'})
140 && Glib::Ex::EnumBits::to_display ($enum_type, $nick));
141 }
142  
143 sub get_active_nick {
144 my ($self) = @_;
145 foreach my $item ($self->get_children) {
146 if ($item->isa('Gtk2::Ex::Menu::EnumRadio::Item')
147 && $item->get_active) {
148 return $item->{'nick'};
149 }
150 }
151 return undef;
152 }
153  
154 # Each menuitem set_active() only does something if the new activeness is
155 # different. If different then its activate handler raises
156 # "notify::active-nick" back up here on the menu.
157 #
158 sub set_active_nick {
159 my ($self, $nick) = @_;
160  
161 # just one notify, not two for turning on and off
162 my $freezer = Glib::Ex::FreezeNotify->new ($self);
163  
164 foreach my $item ($self->get_children) {
165 if ($item->isa('Gtk2::Ex::Menu::EnumRadio::Item')) {
166 if (defined $nick) {
167 if ($item->{'nick'} eq $nick) {
168 $item->set_active (1);
169 last;
170 }
171 } else {
172 ### inactive: $item->{'nick'}
173 $item->set_active (0);
174 }
175 }
176 }
177 }
178  
179 1;
180 __END__