| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2013-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::RadioButton 0.42; | ||
| 11 | class Tickit::Widget::RadioButton :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget; | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | |||
| 19 | use Tickit::Utils qw( textwidth ); | ||
| 20 | use List::Util 1.33 qw( any ); | ||
| 21 | |||
| 22 | use constant CAN_FOCUS => 1; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | C<Tickit::Widget::RadioButton> - a widget allowing a selection from multiple | ||
| 27 | options | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Tickit; | ||
| 32 | use Tickit::Widget::RadioButton; | ||
| 33 | use Tickit::Widget::VBox; | ||
| 34 | |||
| 35 | my $group = Tickit::Widget::RadioButton::Group->new; | ||
| 36 | |||
| 37 | my $vbox = Tickit::Widget::VBox->new; | ||
| 38 | $vbox->add( Tickit::Widget::RadioButton->new( | ||
| 39 | caption => "Radio button $_", | ||
| 40 | group => $group, | ||
| 41 | ) ) for 1 .. 5; | ||
| 42 | |||
| 43 | Tickit->new( root => $vbox )->run; | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | This class provides a widget which allows a selection of one value from a | ||
| 48 | group of related options. It provides a clickable area and a visual indication | ||
| 49 | of which button in the group is the one currently active. Selecting a new | ||
| 50 | button within a group will unselect the previously-selected one. | ||
| 51 | |||
| 52 | This widget is part of an experiment in evolving the design of the | ||
| 53 | L<Tickit::Style> widget integration code, and such is subject to change of | ||
| 54 | details. | ||
| 55 | |||
| 56 | =head1 STYLE | ||
| 57 | |||
| 58 | The default style pen is used as the widget pen. The following style pen | ||
| 59 | prefixes are also used: | ||
| 60 | |||
| 61 | =over 4 | ||
| 62 | |||
| 63 | =item tick => PEN | ||
| 64 | |||
| 65 | The pen used to render the tick marker | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | The following style keys are used: | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | =item tick => STRING | ||
| 74 | |||
| 75 | The text used to indicate the active button | ||
| 76 | |||
| 77 | =item spacing => INT | ||
| 78 | |||
| 79 | Number of columns of spacing between the tick mark and the caption text | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | The following style tags are used: | ||
| 84 | |||
| 85 | =over 4 | ||
| 86 | |||
| 87 | =item :active | ||
| 88 | |||
| 89 | Set when this button is the active one of the group. | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | The following style actions are used: | ||
| 94 | |||
| 95 | =over 4 | ||
| 96 | |||
| 97 | =item activate | ||
| 98 | |||
| 99 | The main action to activate the C<on_click> handler. | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | style_definition base => | ||
| 106 | tick_fg => "hi-white", | ||
| 107 | tick_b => 1, | ||
| 108 | tick => "( )", | ||
| 109 | spacing => 2, | ||
| 110 | '<Space>' => "activate"; | ||
| 111 | |||
| 112 | style_definition ':active' => | ||
| 113 | b => 1, | ||
| 114 | tick => "(*)"; | ||
| 115 | |||
| 116 | style_reshape_keys qw( spacing ); | ||
| 117 | |||
| 118 | style_reshape_textwidth_keys qw( tick ); | ||
| 119 | |||
| 120 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 121 | use constant KEYPRESSES_FROM_STYLE => 1; | ||
| 122 | |||
| 123 | =head1 CONSTRUCTOR | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | =head2 new | ||
| 128 | |||
| 129 | $radiobutton = Tickit::Widget::RadioButton->new( %args ); | ||
| 130 | |||
| 131 | Constructs a new C<Tickit::Widget::RadioButton> object. | ||
| 132 | |||
| 133 | Takes the following named argmuents | ||
| 134 | |||
| 135 | =over 8 | ||
| 136 | |||
| 137 | =item label => STRING | ||
| 138 | |||
| 139 | The label text to display alongside this button. | ||
| 140 | |||
| 141 | =item group => Tickit::Widget::RadioButton::Group | ||
| 142 | |||
| 143 | Optional. If supplied, the group that the button should belong to. If not | ||
| 144 | supplied, a new group will be constructed that can be accessed using the | ||
| 145 | C<group> accessor. | ||
| 146 | |||
| 147 | =item value => SCALAR | ||
| 148 | |||
| 149 | Optional. If supplied, used to set the button's identification value, which | ||
| 150 | is passed to the group's C<on_changed> callback. | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | field $_label :reader :param; | ||
| 157 | field $_on_toggle :reader :writer :param = undef; | ||
| 158 | field $_value :reader :writer :param = undef; | ||
| 159 | field $_group :reader :param = Tickit::Widget::RadioButton::Group->new; | ||
| 160 | |||
| 161 | method lines | ||
| 162 | { | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | method cols | ||
| 167 | { | ||
| 168 | return textwidth( $self->get_style_values( "tick" ) ) + | ||
| 169 | $self->get_style_values( "spacing" ) + | ||
| 170 | textwidth( $_label ); | ||
| 171 | } | ||
| 172 | |||
| 173 | =head1 ACCESSORS | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | =head2 group | ||
| 178 | |||
| 179 | $group = $radiobutton->group; | ||
| 180 | |||
| 181 | Returns the C<Tickit::Widget::RadioButton::Group> this button belongs to. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | # generated accessor | ||
| 186 | |||
| 187 | =head2 label | ||
| 188 | |||
| 189 | =head2 set_label | ||
| 190 | |||
| 191 | $label = $radiobutton->label; | ||
| 192 | |||
| 193 | $radiobutton->set_label( $label ); | ||
| 194 | |||
| 195 | Returns or sets the label text of the button. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | # generated accessor | ||
| 200 | |||
| 201 | method set_label | ||
| 202 | { | ||
| 203 | ( $_label ) = @_; | ||
| 204 | $self->reshape; | ||
| 205 | $self->redraw; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 on_toggle | ||
| 209 | |||
| 210 | $on_toggle = $radiobutton->on_toggle; | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | # generated accessor | ||
| 215 | |||
| 216 | =head2 set_on_toggle | ||
| 217 | |||
| 218 | $radiobutton->set_on_toggle( $on_toggle ); | ||
| 219 | |||
| 220 | Return or set the CODE reference to be called when the button state is | ||
| 221 | changed. | ||
| 222 | |||
| 223 | $on_toggle->( $radiobutton, $active ); | ||
| 224 | |||
| 225 | When the radio tick mark moves from one button to another, the old button is | ||
| 226 | marked unactive before the new one is marked active. | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | # generated accessor | ||
| 231 | |||
| 232 | =head2 value | ||
| 233 | |||
| 234 | $value = $radiobutton->value; | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | # generated accessor | ||
| 239 | |||
| 240 | =head2 set_value | ||
| 241 | |||
| 242 | $radiobutton->set_value( $value ); | ||
| 243 | |||
| 244 | Return or set the scalar value used to identify the radio button to the | ||
| 245 | group's C<on_changed> callback. This can be any scalar value; it is simply | ||
| 246 | stored by the button and not otherwise used. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | # generated accessor | ||
| 251 | |||
| 252 | =head1 METHODS | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | =head2 activate | ||
| 257 | |||
| 258 | $radiobutton->activate; | ||
| 259 | |||
| 260 | Sets this button as the active member of the group, deactivating the previous | ||
| 261 | one. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | *key_activate = \&activate; | ||
| 266 | method activate | ||
| 267 | { | ||
| 268 | if( my $old = $_group->active ) { | ||
| 269 | $old->set_style_tag( active => 0 ); | ||
| 270 | $old->on_toggle->( $old, 0 ) if $old->on_toggle; | ||
| 271 | } | ||
| 272 | |||
| 273 | $_group->set_active( $self ); | ||
| 274 | |||
| 275 | $self->set_style_tag( active => 1 ); | ||
| 276 | $_on_toggle->( $self, 1 ) if $_on_toggle; | ||
| 277 | |||
| 278 | return 1; | ||
| 279 | } | ||
| 280 | |||
| 281 | =head2 is_active | ||
| 282 | |||
| 283 | $active = $radiobutton->is_active; | ||
| 284 | |||
| 285 | Returns true if this button is the active button of the group. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | method is_active | ||
| 290 | { | ||
| 291 | return $self->group->active == $self; | ||
| 292 | } | ||
| 293 | |||
| 294 | method reshape | ||
| 295 | { | ||
| 296 | my $win = $self->window or return; | ||
| 297 | |||
| 298 | my $tick = $self->get_style_values( "tick" ); | ||
| 299 | |||
| 300 | $win->cursor_at( 0, ( textwidth( $tick )-1 ) / 2 ); | ||
| 301 | } | ||
| 302 | |||
| 303 | method render_to_rb | ||
| 304 | { | ||
| 305 | my ( $rb, $rect ) = @_; | ||
| 306 | |||
| 307 | $rb->clear; | ||
| 308 | |||
| 309 | return if $rect->top > 0; | ||
| 310 | |||
| 311 | $rb->goto( 0, 0 ); | ||
| 312 | |||
| 313 | $rb->text( my $tick = $self->get_style_values( "tick" ), $self->get_style_pen( "tick" ) ); | ||
| 314 | $rb->erase( $self->get_style_values( "spacing" ) ); | ||
| 315 | $rb->text( $_label ); | ||
| 316 | $rb->erase_to( $rect->right ); | ||
| 317 | } | ||
| 318 | |||
| 319 | method on_mouse | ||
| 320 | { | ||
| 321 | my ( $args ) = @_; | ||
| 322 | |||
| 323 | return unless $args->type eq "press" and $args->button == 1; | ||
| 324 | return 1 unless $args->line == 0; | ||
| 325 | |||
| 326 | $self->activate; | ||
| 327 | } | ||
| 328 | |||
| 329 | class Tickit::Widget::RadioButton::Group { | ||
| 330 | use Scalar::Util qw( weaken refaddr ); | ||
| 331 | |||
| 332 | =head1 GROUPS | ||
| 333 | |||
| 334 | Every C<Tickit::Widget::RadioButton> belongs to a group. Only one button can | ||
| 335 | be active in a group at any one time. The C<group> accessor returns the group | ||
| 336 | the button is a member of. The following methods are available on it. | ||
| 337 | |||
| 338 | A group can be explicitly created to pass to a button's constructor, or one | ||
| 339 | will be implicitly created for a button if none is passed. | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | =head2 new | ||
| 344 | |||
| 345 | $group = Tickit::Widget::RadioButton::Group->new; | ||
| 346 | |||
| 347 | Returns a new group. | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | field $_active :reader; | ||
| 352 | field $_on_changed :reader :writer; | ||
| 353 | |||
| 354 | =head2 active | ||
| 355 | |||
| 356 | $radiobutton = $group->active; | ||
| 357 | |||
| 358 | Returns the button which is currently active in the group | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | method set_active | ||
| 363 | { | ||
| 364 | ( $_active ) = @_; | ||
| 365 | $_on_changed->( $self->active, $self->active->value ) if $_on_changed; | ||
| 366 | } | ||
| 367 | |||
| 368 | =head2 on_changed | ||
| 369 | |||
| 370 | $on_changed = $group->on_changed; | ||
| 371 | |||
| 372 | =cut | ||
| 373 | |||
| 374 | =head2 set_on_changed | ||
| 375 | |||
| 376 | $group->set_on_changed( $on_changed ); | ||
| 377 | |||
| 378 | Return or set the CODE reference to be called when the active member of the | ||
| 379 | group changes. This may be more convenient than setting the C<on_toggle> | ||
| 380 | callback of each button in the group. | ||
| 381 | |||
| 382 | The callback is passed the currently-active button, and its C<value>. | ||
| 383 | |||
| 384 | $on_changed->( $active, $value ); | ||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | } | ||
| 389 | |||
| 390 | =head1 AUTHOR | ||
| 391 | |||
| 392 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 393 | |||
| 394 | =cut | ||
| 395 | |||
| 396 | 0x55AA; |