File: RadioButton.pm

package info (click to toggle)
libtickit-widgets-perl 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 568 kB
  • sloc: perl: 5,636; makefile: 2
file content (396 lines) | stat: -rw-r--r-- 7,456 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2013-2023 -- leonerd@leonerd.org.uk

use v5.20;
use warnings;
use Object::Pad 0.807;

package Tickit::Widget::RadioButton 0.42;
class Tickit::Widget::RadioButton :strict(params);

inherit Tickit::Widget;

use Tickit::Style;

use Carp;

use Tickit::Utils qw( textwidth );
use List::Util 1.33 qw( any );

use constant CAN_FOCUS => 1;

=head1 NAME

C<Tickit::Widget::RadioButton> - a widget allowing a selection from multiple
options

=head1 SYNOPSIS

   use Tickit;
   use Tickit::Widget::RadioButton;
   use Tickit::Widget::VBox;

   my $group = Tickit::Widget::RadioButton::Group->new;

   my $vbox = Tickit::Widget::VBox->new;
   $vbox->add( Tickit::Widget::RadioButton->new(
         caption => "Radio button $_",
         group   => $group,
   ) ) for 1 .. 5;

   Tickit->new( root => $vbox )->run;

=head1 DESCRIPTION

This class provides a widget which allows a selection of one value from a
group of related options. It provides a clickable area and a visual indication
of which button in the group is the one currently active. Selecting a new
button within a group will unselect the previously-selected one.

This widget is part of an experiment in evolving the design of the
L<Tickit::Style> widget integration code, and such is subject to change of
details.

=head1 STYLE

The default style pen is used as the widget pen. The following style pen 
prefixes are also used:

=over 4

=item tick => PEN

The pen used to render the tick marker

=back

The following style keys are used:

=over 4

=item tick => STRING

The text used to indicate the active button

=item spacing => INT

Number of columns of spacing between the tick mark and the caption text

=back

The following style tags are used:

=over 4

=item :active

Set when this button is the active one of the group.

=back

The following style actions are used:

=over 4

=item activate

The main action to activate the C<on_click> handler.

=back

=cut

style_definition base =>
   tick_fg => "hi-white",
   tick_b  => 1,
   tick    => "( )",
   spacing => 2,
   '<Space>' => "activate";

style_definition ':active' =>
   b        => 1,
   tick     => "(*)";

style_reshape_keys qw( spacing );

style_reshape_textwidth_keys qw( tick );

use constant WIDGET_PEN_FROM_STYLE => 1;
use constant KEYPRESSES_FROM_STYLE => 1;

=head1 CONSTRUCTOR

=cut

=head2 new

   $radiobutton = Tickit::Widget::RadioButton->new( %args );

Constructs a new C<Tickit::Widget::RadioButton> object.

Takes the following named argmuents

=over 8

=item label => STRING

The label text to display alongside this button.

=item group => Tickit::Widget::RadioButton::Group

Optional. If supplied, the group that the button should belong to. If not
supplied, a new group will be constructed that can be accessed using the
C<group> accessor.

=item value => SCALAR

Optional. If supplied, used to set the button's identification value, which
is passed to the group's C<on_changed> callback.

=back

=cut

field $_label     :reader         :param;
field $_on_toggle :reader :writer :param = undef;
field $_value     :reader :writer :param = undef;
field $_group     :reader         :param = Tickit::Widget::RadioButton::Group->new;

method lines
{
   return 1;
}

method cols
{
   return textwidth( $self->get_style_values( "tick" ) ) +
          $self->get_style_values( "spacing" ) +
          textwidth( $_label );
}

=head1 ACCESSORS

=cut

=head2 group

   $group = $radiobutton->group;

Returns the C<Tickit::Widget::RadioButton::Group> this button belongs to.

=cut

# generated accessor

=head2 label

=head2 set_label

   $label = $radiobutton->label;

   $radiobutton->set_label( $label );

Returns or sets the label text of the button.

=cut

# generated accessor

method set_label
{
   ( $_label ) = @_;
   $self->reshape;
   $self->redraw;
}

=head2 on_toggle

   $on_toggle = $radiobutton->on_toggle;

=cut

# generated accessor

=head2 set_on_toggle

   $radiobutton->set_on_toggle( $on_toggle );

Return or set the CODE reference to be called when the button state is
changed.

   $on_toggle->( $radiobutton, $active );

When the radio tick mark moves from one button to another, the old button is
marked unactive before the new one is marked active.

=cut

# generated accessor

=head2 value

   $value = $radiobutton->value;

=cut

# generated accessor

=head2 set_value

   $radiobutton->set_value( $value );

Return or set the scalar value used to identify the radio button to the
group's C<on_changed> callback. This can be any scalar value; it is simply
stored by the button and not otherwise used.

=cut

# generated accessor

=head1 METHODS

=cut

=head2 activate

   $radiobutton->activate;

Sets this button as the active member of the group, deactivating the previous
one.

=cut

*key_activate = \&activate;
method activate
{
   if( my $old = $_group->active ) {
      $old->set_style_tag( active => 0 );
      $old->on_toggle->( $old, 0 ) if $old->on_toggle;
   }

   $_group->set_active( $self );

   $self->set_style_tag( active => 1 );
   $_on_toggle->( $self, 1 ) if $_on_toggle;

   return 1;
}

=head2 is_active

   $active = $radiobutton->is_active;

Returns true if this button is the active button of the group.

=cut

method is_active
{
   return $self->group->active == $self;
}

method reshape
{
   my $win = $self->window or return;

   my $tick = $self->get_style_values( "tick" );

   $win->cursor_at( 0, ( textwidth( $tick )-1 ) / 2 );
}

method render_to_rb
{
   my ( $rb, $rect ) = @_;

   $rb->clear;

   return if $rect->top > 0;

   $rb->goto( 0, 0 );

   $rb->text( my $tick = $self->get_style_values( "tick" ), $self->get_style_pen( "tick" ) );
   $rb->erase( $self->get_style_values( "spacing" ) );
   $rb->text( $_label );
   $rb->erase_to( $rect->right );
}

method on_mouse
{
   my ( $args ) = @_;

   return unless $args->type eq "press" and $args->button == 1;
   return 1 unless $args->line == 0;

   $self->activate;
}

class Tickit::Widget::RadioButton::Group {
   use Scalar::Util qw( weaken refaddr );

=head1 GROUPS

Every C<Tickit::Widget::RadioButton> belongs to a group. Only one button can
be active in a group at any one time. The C<group> accessor returns the group
the button is a member of. The following methods are available on it.

A group can be explicitly created to pass to a button's constructor, or one
will be implicitly created for a button if none is passed.

=cut

=head2 new

   $group = Tickit::Widget::RadioButton::Group->new;

Returns a new group.

=cut

   field $_active :reader;
   field $_on_changed :reader :writer;

=head2 active

   $radiobutton = $group->active;

Returns the button which is currently active in the group

=cut

   method set_active
   {
      ( $_active ) = @_;
      $_on_changed->( $self->active, $self->active->value ) if $_on_changed;
   }

=head2 on_changed

   $on_changed = $group->on_changed;

=cut

=head2 set_on_changed

   $group->set_on_changed( $on_changed );

Return or set the CODE reference to be called when the active member of the
group changes. This may be more convenient than setting the C<on_toggle>
callback of each button in the group.

The callback is passed the currently-active button, and its C<value>.

   $on_changed->( $active, $value );

=cut

}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;