File Coverage

blib/lib/UI/Various/Optionmenu.pm
Criterion Covered Total %
statement 58 58 100.0
branch 14 14 100.0
condition n/a
subroutine 17 17 100.0
pod 4 4 100.0
total 93 93 100.0


line stmt bran cond sub pod time code
1             package UI::Various::Optionmenu;
2              
3             # Author, Copyright and License: see end of file
4              
5             =head1 NAME
6              
7             UI::Various::Optionmenu - general option-menu widget of L
8              
9             =head1 SYNOPSIS
10              
11             use UI::Various;
12             my $main = UI::Various::main();
13             my @options = (0, [1st => 1], [2nd => 2], [3rd => 3]);
14             $main->window(...
15             UI::Various::Optionmenu->new(init => 2,
16             options => \@options),
17             ...);
18             $main->mainloop();
19              
20             =head1 ABSTRACT
21              
22             This module defines the general widget for a menu of options in an
23             application using L.
24              
25             =head1 DESCRIPTION
26              
27             Besides the common attributes inherited from C the
28             C widget knows the following additional attributes:
29              
30             =head2 Attributes
31              
32             =over
33              
34             =cut
35              
36             #########################################################################
37              
38 8     8   91 use v5.14;
  8         22  
39 8     8   38 use strictures;
  8         17  
  8         40  
40 8     8   1289 no indirect 'fatal';
  8         15  
  8         41  
41 8     8   404 no multidimensional;
  8         14  
  8         35  
42 8     8   240 use warnings 'once';
  8         10  
  8         407  
43              
44             our $VERSION = '0.24';
45              
46 8     8   57 use UI::Various::core;
  8         14  
  8         40  
47 8     8   42 use UI::Various::widget;
  8         15  
  8         440  
48 8     8   37 BEGIN { require 'UI/Various/' . UI::Various::core::using() . '/Optionmenu.pm'; }
49              
50             require Exporter;
51             our @ISA = qw(UI::Various::widget);
52             our @EXPORT_OK = qw();
53              
54             #########################################################################
55              
56             =item options [rw, fixed]
57              
58             a reference to a list of option pairs / options
59              
60             The list of options must be either an ARRAY of pairs (reference to an ARRAY
61             with two elements each) of menu entries with corresponding values or simple
62             scalar values, which will be mapped into the aforementioned pairs.
63              
64             =cut
65              
66             sub options($$)
67             {
68             return access('options',
69             sub{
70 5 100   5   20 unless (ref($_) eq 'ARRAY')
71             {
72 1         4 error('_1_attribute_must_be_a_2_reference',
73             'options', 'ARRAY');
74 1         13 return undef;
75             }
76 4         8 my $ra = $_;
77 4         12 foreach (@$ra)
78             {
79 10 100       86 if (ref($_) eq 'ARRAY')
80             {
81 8 100       21 error('invalid_pair_in__1_attribute', 'options')
82             if @$_ < 2;
83             }
84             else
85 2         6 { $_ = [ $_ => $_ ]; }
86             }
87 4         19 $_ = $ra;
88             },
89 6     6 1 63 @_);
90             }
91              
92             =item init [wo]
93              
94             option selected initially
95              
96             The option selected initially must be specified by its value, not by the
97             menu entry.
98              
99             =cut
100              
101             sub _init($$)
102             {
103 1     1   6 return set('_init', undef, @_);
104             }
105              
106             =item on_select [rw, optional]
107              
108             an optional callback called after changing the selection
109              
110             The callback routine is called with the selected value (not menu entry) as
111             parameter.
112              
113             Note that in L the callback is also called during initialisation.
114              
115             =cut
116              
117             sub on_select($;$)
118             {
119             return access('on_select',
120             sub{
121 2 100   2   13 unless (ref($_) eq 'CODE')
122             {
123 1         4 error('_1_attribute_must_be_a_2_reference',
124             'on_select', 'CODE');
125 1         9 return undef;
126             }
127             },
128 2     2 1 33 @_);
129             }
130              
131             #########################################################################
132             #
133             # internal constants and data:
134              
135 8         559 use constant ALLOWED_PARAMETERS =>
136 8     8   56 (UI::Various::widget::COMMON_PARAMETERS, qw(init options on_select));
  8         13  
137 8     8   44 use constant DEFAULT_ATTRIBUTES => ();
  8         13  
  8         2725  
138              
139             #########################################################################
140             #########################################################################
141              
142             =back
143              
144             =head1 METHODS
145              
146             Besides the accessors (attributes) described above and by
147             L and the methods
148             inherited from L only the
149             constructor is provided by the C class itself:
150              
151             =cut
152              
153             #########################################################################
154              
155             =head2 B - constructor
156              
157             see L
158             constructor for UI elements>
159              
160             =cut
161              
162             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
163              
164             sub new($;\[@$])
165             {
166 6     6 1 2678 debug(3, __PACKAGE__, '::new');
167 6         89 local $_ = construct({ DEFAULT_ATTRIBUTES },
168             '^(?:' . join('|', ALLOWED_PARAMETERS) . ')$',
169             @_);
170 6 100       34 unless (defined $_->{options})
171             {
172 2         9 error('mandatory_parameter__1_is_missing', 'options');
173 2         82 return undef;
174             }
175 4         15 $_->{_selected} = undef;
176 4         16 $_->{_selected_menu} = undef; # only used in *Term
177 4 100       16 if (defined $_->{_init})
178             {
179 1         2 foreach my $opt (@{$_->{options}})
  1         7  
180             {
181 4 100       14 next unless $opt->[1] eq $_->{_init};
182 1         3 $_->{_selected_menu} = $opt->[0];
183 1         4 $_->{_selected} = $opt->[1];
184             }
185             }
186 4         18 return $_;
187             }
188              
189              
190             #########################################################################
191              
192             =head2 B - get current value of the menu of options
193              
194             $selection = $optionmenu->selected();
195              
196             =head3 description:
197              
198             This method returns the value (not the menu entry) of the currently selected
199             option (or C in nothing has been selected or initialised).
200              
201             =head3 returns:
202              
203             selected value (or C)
204              
205             =cut
206              
207             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
208              
209             sub selected($)
210             {
211 3     3 1 3275 my ($self) = @_;
212 3         22 return $self->{_selected};
213             }
214              
215             1;
216              
217             #########################################################################
218             #########################################################################
219              
220             =head1 SEE ALSO
221              
222             L
223              
224             =head1 LICENSE
225              
226             Copyright (C) Thomas Dorner.
227              
228             This library is free software; you can redistribute it and/or modify it
229             under the same terms as Perl itself. See LICENSE file for more details.
230              
231             =head1 AUTHOR
232              
233             Thomas Dorner Edorner (at) cpan (dot) orgE
234              
235             =cut