| 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::CheckButton 0.42; | ||
| 11 | class Tickit::Widget::CheckButton :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::CheckButton> - a widget allowing a toggle true/false option | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Tickit; | ||
| 31 | use Tickit::Widget::CheckButton; | ||
| 32 | use Tickit::Widget::VBox; | ||
| 33 | |||
| 34 | my $vbox = Tickit::Widget::VBox->new; | ||
| 35 | $vbox->add( Tickit::Widget::CheckButton->new( | ||
| 36 | label => "Check button $_", | ||
| 37 | ) ) for 1 .. 5; | ||
| 38 | |||
| 39 | Tickit->new( root => $vbox )->run; | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | This class provides a widget which allows a true/false selection. It displays | ||
| 44 | a clickable indication of status and a caption. Clicking on the status or | ||
| 45 | caption inverts the status of the widget. | ||
| 46 | |||
| 47 | This widget is part of an experiment in evolving the design of the | ||
| 48 | L<Tickit::Style> widget integration code, and such is subject to change of | ||
| 49 | details. | ||
| 50 | |||
| 51 | =head1 STYLE | ||
| 52 | |||
| 53 | The default style pen is used as the widget pen. The following style pen | ||
| 54 | prefixes are also used: | ||
| 55 | |||
| 56 | =over 4 | ||
| 57 | |||
| 58 | =item check => PEN | ||
| 59 | |||
| 60 | The pen used to render the check marker | ||
| 61 | |||
| 62 | =back | ||
| 63 | |||
| 64 | The following style keys are used: | ||
| 65 | |||
| 66 | =over 4 | ||
| 67 | |||
| 68 | =item check => STRING | ||
| 69 | |||
| 70 | The text used to indicate the active status | ||
| 71 | |||
| 72 | =item spacing => INT | ||
| 73 | |||
| 74 | Number of columns of spacing between the check mark and the caption text | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | The following style tags are used: | ||
| 79 | |||
| 80 | =over 4 | ||
| 81 | |||
| 82 | =item :active | ||
| 83 | |||
| 84 | Set when this button's status is true | ||
| 85 | |||
| 86 | =back | ||
| 87 | |||
| 88 | The following style actions are used: | ||
| 89 | |||
| 90 | =over 4 | ||
| 91 | |||
| 92 | =item toggle | ||
| 93 | |||
| 94 | The main action to activate the C<on_click> handler. | ||
| 95 | |||
| 96 | =back | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | style_definition base => | ||
| 101 | check_fg => "hi-white", | ||
| 102 | check_b => 1, | ||
| 103 | check => "[ ]", | ||
| 104 | spacing => 2, | ||
| 105 | '<Space>' => "toggle"; | ||
| 106 | |||
| 107 | style_definition ':active' => | ||
| 108 | b => 1, | ||
| 109 | check => "[X]"; | ||
| 110 | |||
| 111 | style_reshape_keys qw( spacing ); | ||
| 112 | |||
| 113 | style_reshape_textwidth_keys qw( check ); | ||
| 114 | |||
| 115 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 116 | use constant KEYPRESSES_FROM_STYLE => 1; | ||
| 117 | |||
| 118 | =head1 CONSTRUCTOR | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | =head2 new | ||
| 123 | |||
| 124 | $checkbutton = Tickit::Widget::CheckButton->new( %args ); | ||
| 125 | |||
| 126 | Constructs a new C<Tickit::Widget::CheckButton> object. | ||
| 127 | |||
| 128 | Takes the following named argmuents | ||
| 129 | |||
| 130 | =over 8 | ||
| 131 | |||
| 132 | =item label => STRING | ||
| 133 | |||
| 134 | The label text to display alongside this button. | ||
| 135 | |||
| 136 | =item on_toggle => CODE | ||
| 137 | |||
| 138 | Optional. Callback function to invoke when the check state is changed. | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | field $_label :reader :param = undef; | ||
| 145 | field $_on_toggle :reader :writer :param = undef; | ||
| 146 | |||
| 147 | field $_active; | ||
| 148 | |||
| 149 | method lines | ||
| 150 | { | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | method cols | ||
| 155 | { | ||
| 156 | return textwidth( $self->get_style_values( "check" ) ) + | ||
| 157 | $self->get_style_values( "spacing" ) + | ||
| 158 | textwidth( $_label ); | ||
| 159 | } | ||
| 160 | |||
| 161 | =head1 ACCESSORS | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | =head2 label | ||
| 166 | |||
| 167 | =head2 set_label | ||
| 168 | |||
| 169 | $label = $checkbutton->label; | ||
| 170 | |||
| 171 | $checkbutton->set_label( $label ); | ||
| 172 | |||
| 173 | Returns or sets the label text of the button. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | # generated accessor | ||
| 178 | |||
| 179 | method set_label | ||
| 180 | { | ||
| 181 | ( $_label ) = @_; | ||
| 182 | $self->reshape; | ||
| 183 | $self->redraw; | ||
| 184 | } | ||
| 185 | |||
| 186 | =head2 on_toggle | ||
| 187 | |||
| 188 | $on_toggle = $checkbutton->on_toggle; | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | # generated accessor | ||
| 193 | |||
| 194 | =head2 set_on_toggle | ||
| 195 | |||
| 196 | $checkbutton->set_on_toggle( $on_toggle ); | ||
| 197 | |||
| 198 | Return or set the CODE reference to be called when the button state is | ||
| 199 | changed. | ||
| 200 | |||
| 201 | $on_toggle->( $checkbutton, $active ); | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | # generated accessor | ||
| 206 | |||
| 207 | =head1 METHODS | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | =head2 activate | ||
| 212 | |||
| 213 | $checkbutton->activate; | ||
| 214 | |||
| 215 | Sets this button's active state to true. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | method activate | ||
| 220 | { | ||
| 221 | $_active = 1; | ||
| 222 | $self->set_style_tag( active => 1 ); | ||
| 223 | $_on_toggle->( $self, 1 ) if $_on_toggle; | ||
| 224 | } | ||
| 225 | |||
| 226 | =head2 deactivate | ||
| 227 | |||
| 228 | $checkbutton->deactivate; | ||
| 229 | |||
| 230 | Sets this button's active state to false. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | method deactivate | ||
| 235 | { | ||
| 236 | $_active = 0; | ||
| 237 | $self->set_style_tag( active => 0 ); | ||
| 238 | $_on_toggle->( $self, 0 ) if $_on_toggle; | ||
| 239 | } | ||
| 240 | |||
| 241 | *key_toggle = \&toggle; | ||
| 242 | method toggle | ||
| 243 | { | ||
| 244 | $self->is_active ? $self->deactivate : $self->activate; | ||
| 245 | return 1; | ||
| 246 | } | ||
| 247 | |||
| 248 | =head2 is_active | ||
| 249 | |||
| 250 | $active = $checkbutton->is_active; | ||
| 251 | |||
| 252 | Returns this button's active state. | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | method is_active { !!$_active } | ||
| 257 | |||
| 258 | method reshape | ||
| 259 | { | ||
| 260 | my $win = $self->window or return; | ||
| 261 | |||
| 262 | my $check = $self->get_style_values( "check" ); | ||
| 263 | |||
| 264 | $win->cursor_at( 0, ( textwidth( $check )-1 ) / 2 ); | ||
| 265 | } | ||
| 266 | |||
| 267 | method render_to_rb | ||
| 268 | { | ||
| 269 | my ( $rb, $rect ) = @_; | ||
| 270 | |||
| 271 | $rb->clear; | ||
| 272 | |||
| 273 | return if $rect->top > 0; | ||
| 274 | |||
| 275 | $rb->goto( 0, 0 ); | ||
| 276 | |||
| 277 | $rb->text( my $check = $self->get_style_values( "check" ), $self->get_style_pen( "check" ) ); | ||
| 278 | $rb->erase( $self->get_style_values( "spacing" ) ); | ||
| 279 | $rb->text( $self->label ); | ||
| 280 | $rb->erase_to( $rect->right ); | ||
| 281 | } | ||
| 282 | |||
| 283 | method on_mouse | ||
| 284 | { | ||
| 285 | my ( $args ) = @_; | ||
| 286 | |||
| 287 | return unless $args->type eq "press" and $args->button == 1; | ||
| 288 | return 1 unless $args->line == 0; | ||
| 289 | |||
| 290 | $self->toggle; | ||
| 291 | } | ||
| 292 | |||
| 293 | =head1 AUTHOR | ||
| 294 | |||
| 295 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | 0x55AA; |