| 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, 2012-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::Button 0.42; | ||
| 11 | class Tickit::Widget::Button :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget; | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | use Tickit::RenderBuffer qw( LINE_SINGLE LINE_DOUBLE LINE_THICK ); | ||
| 17 | |||
| 18 | use Tickit::Utils qw( textwidth ); | ||
| 19 | |||
| 20 | use constant CAN_FOCUS => 1; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | C<Tickit::Widget::Button> - a widget displaying a clickable button | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Tickit; | ||
| 29 | use Tickit::Widget::Button; | ||
| 30 | |||
| 31 | my $button = Tickit::Widget::Button->new( | ||
| 32 | label => "Click Me!", | ||
| 33 | on_click => sub { | ||
| 34 | my ( $self ) = @_; | ||
| 35 | |||
| 36 | # Do something! | ||
| 37 | }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | Tickit->new( root => $button )->run; | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This class provides a widget which displays a clickable area with a label. | ||
| 45 | When the area is clicked, a callback is invoked. | ||
| 46 | |||
| 47 | =head1 STYLE | ||
| 48 | |||
| 49 | The default style pen is used as the widget pen. The following style keys are | ||
| 50 | used: | ||
| 51 | |||
| 52 | =over 4 | ||
| 53 | |||
| 54 | =item linetype => STRING | ||
| 55 | |||
| 56 | What kind of border to draw around the button; one of | ||
| 57 | |||
| 58 | none single double thick | ||
| 59 | |||
| 60 | =item marker_left => STRING | ||
| 61 | |||
| 62 | A two-character string to place just before the button label | ||
| 63 | |||
| 64 | =item marker_right => STRING | ||
| 65 | |||
| 66 | A two-character string to place just after the button label | ||
| 67 | |||
| 68 | =back | ||
| 69 | |||
| 70 | The following style tags are used: | ||
| 71 | |||
| 72 | =over 4 | ||
| 73 | |||
| 74 | =item :active | ||
| 75 | |||
| 76 | Set when the mouse is being held over the button, before it is released | ||
| 77 | |||
| 78 | =back | ||
| 79 | |||
| 80 | The following style actions are used: | ||
| 81 | |||
| 82 | =over 4 | ||
| 83 | |||
| 84 | =item click | ||
| 85 | |||
| 86 | The main action to activate the C<on_click> handler. | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | style_definition base => | ||
| 93 | fg => "black", | ||
| 94 | bg => "blue", | ||
| 95 | linetype => "single", | ||
| 96 | marker_left => "> ", | ||
| 97 | marker_right => " <", | ||
| 98 | '<Enter>' => "click"; | ||
| 99 | |||
| 100 | style_definition ':focus' => | ||
| 101 | marker_left => ">>", | ||
| 102 | marker_right => "<<"; | ||
| 103 | |||
| 104 | style_definition ':active' => | ||
| 105 | rv => 1; | ||
| 106 | |||
| 107 | style_reshape_keys qw( linetype ); | ||
| 108 | style_redraw_keys qw( marker_left marker_right ); | ||
| 109 | |||
| 110 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 111 | use constant KEYPRESSES_FROM_STYLE => 1; | ||
| 112 | |||
| 113 | =head1 CONSTRUCTOR | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | =head2 new | ||
| 118 | |||
| 119 | $button = Tickit::Widget::Button->new( %args ); | ||
| 120 | |||
| 121 | Constructs a new C<Tickit::Widget::Button> object. | ||
| 122 | |||
| 123 | Takes the following named arguments: | ||
| 124 | |||
| 125 | =over 8 | ||
| 126 | |||
| 127 | =item label => STR | ||
| 128 | |||
| 129 | Text to display in the button area | ||
| 130 | |||
| 131 | =item on_click => CODE | ||
| 132 | |||
| 133 | Optional. Callback function to invoke when the button is clicked. | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | field $_label :reader :param = undef; | ||
| 140 | field $_on_click :reader :writer :param = undef; | ||
| 141 | |||
| 142 | field $_active; | ||
| 143 | field $_dragging_on_self; | ||
| 144 | |||
| 145 | |||
| 146 | ADJUST :params ( | ||
| 147 | :$align = 0.5, | ||
| 148 | :$valign = 0.5, | ||
| 149 | ) { | ||
| 150 | $self->set_align( $align ); | ||
| 151 | $self->set_valign( $valign ); | ||
| 152 | } | ||
| 153 | |||
| 154 | method lines | ||
| 155 | { | ||
| 156 | my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none"; | ||
| 157 | return 1 + 2*$has_border; | ||
| 158 | } | ||
| 159 | |||
| 160 | method cols | ||
| 161 | { | ||
| 162 | return 4 + textwidth( $self->label ) + 2; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head1 ACCESSORS | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | =head2 label | ||
| 170 | |||
| 171 | $label = $button->label; | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | # generated accessor | ||
| 176 | |||
| 177 | =head2 set_label | ||
| 178 | |||
| 179 | $button->set_label( $label ); | ||
| 180 | |||
| 181 | Return or set the text to display in the button area. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | method set_label | ||
| 186 | { | ||
| 187 | ( $_label ) = @_; | ||
| 188 | $self->redraw; | ||
| 189 | } | ||
| 190 | |||
| 191 | =head2 on_click | ||
| 192 | |||
| 193 | $on_click = $button->on_click; | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | # generated accessor | ||
| 198 | |||
| 199 | =head2 set_on_click | ||
| 200 | |||
| 201 | $button->set_on_click( $on_click ); | ||
| 202 | |||
| 203 | Return or set the CODE reference to be called when the button area is clicked. | ||
| 204 | |||
| 205 | $on_click->( $button ); | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | # generated accessor | ||
| 210 | |||
| 211 | =head2 click | ||
| 212 | |||
| 213 | $button->click; | ||
| 214 | |||
| 215 | Behave as if the button has been clicked; running its C<on_click> handler. | ||
| 216 | This is provided for convenience of activating its handler programmatically | ||
| 217 | via other parts of code. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | method click | ||
| 222 | { | ||
| 223 | $_on_click->( $self ); | ||
| 224 | } | ||
| 225 | |||
| 226 | # Activation by key should "flash" the button briefly on the screen as a | ||
| 227 | # visual feedback | ||
| 228 | method key_click | ||
| 229 | { | ||
| 230 | $self->click; | ||
| 231 | if( my $window = $self->window ) { | ||
| 232 | $self->set_style_tag( active => 1 ); | ||
| 233 | $window->tickit->timer( after => 0.1, sub { $self->set_style_tag( active => 0 ) } ); | ||
| 234 | } | ||
| 235 | return 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | method _activate | ||
| 239 | { | ||
| 240 | my ( $active ) = @_; | ||
| 241 | $_active = $active; | ||
| 242 | $self->set_style_tag( active => $active ); | ||
| 243 | } | ||
| 244 | |||
| 245 | =head2 align | ||
| 246 | |||
| 247 | =head2 set_align | ||
| 248 | |||
| 249 | =head2 valign | ||
| 250 | |||
| 251 | =head2 set_valign | ||
| 252 | |||
| 253 | $align = $button->align; | ||
| 254 | |||
| 255 | $button->set_align( $align ); | ||
| 256 | |||
| 257 | $valign = $button->valign; | ||
| 258 | |||
| 259 | $button->set_valign( $valign ); | ||
| 260 | |||
| 261 | Accessors for the horizontal and vertical alignment of the label text within | ||
| 262 | the button area. See also L<Tickit::WidgetRole::Alignable>. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | use Tickit::WidgetRole::Alignable name => "align", style => "h"; | ||
| 267 | use Tickit::WidgetRole::Alignable name => "valign", style => "v"; | ||
| 268 | |||
| 269 | field $_label_line; | ||
| 270 | field $_label_col; | ||
| 271 | field $_label_end; | ||
| 272 | |||
| 273 | method reshape | ||
| 274 | { | ||
| 275 | my $win = $self->window or return; | ||
| 276 | my $lines = $win->lines; | ||
| 277 | my $cols = $win->cols; | ||
| 278 | |||
| 279 | my $width = textwidth $self->label; | ||
| 280 | |||
| 281 | my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none"; | ||
| 282 | |||
| 283 | my ( $lines_before, undef, $lines_after ) = $self->_valign_allocation( 1, $lines - (2 * $has_border) ); | ||
| 284 | my ( $cols_before, undef, $cols_after ) = $self->_align_allocation( $width + 2, $cols - 2 ); | ||
| 285 | |||
| 286 | $_label_line = $lines_before + $has_border; | ||
| 287 | $_label_col = $cols_before + 2; | ||
| 288 | $_label_end = $cols_before + $width + 2; | ||
| 289 | |||
| 290 | $win->cursor_at( $_label_line, $_label_col ); | ||
| 291 | } | ||
| 292 | |||
| 293 | method render_to_rb | ||
| 294 | { | ||
| 295 | my ( $rb, $rect ) = @_; | ||
| 296 | |||
| 297 | my $win = $self->window or return; | ||
| 298 | my $lines = $win->lines; | ||
| 299 | my $cols = $win->cols; | ||
| 300 | |||
| 301 | my ( $linetype, $marker_left, $marker_right ) = | ||
| 302 | $self->get_style_values(qw( linetype marker_left marker_right )); | ||
| 303 | |||
| 304 | my $linestyle = $linetype eq "single" ? LINE_SINGLE : | ||
| 305 | $linetype eq "double" ? LINE_DOUBLE : | ||
| 306 | $linetype eq "thick" ? LINE_THICK : | ||
| 307 | undef; | ||
| 308 | |||
| 309 | if( defined $linestyle ) { | ||
| 310 | $rb->hline_at( 0, 0, $cols-1, $linestyle ); | ||
| 311 | $rb->hline_at( $lines-1, 0, $cols-1, $linestyle ); | ||
| 312 | $rb->vline_at( 0, $lines-1, 0, $linestyle ); | ||
| 313 | $rb->vline_at( 0, $lines-1, $cols-1, $linestyle ); | ||
| 314 | |||
| 315 | foreach my $line ( $rect->linerange( 1, $lines-2 ) ) { | ||
| 316 | $rb->erase_at( $line, 1, $cols-2 ); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | else { | ||
| 320 | foreach my $line ( $rect->linerange( 0, $lines-1 ) ) { | ||
| 321 | $rb->erase_at( $line, 0, $cols ); | ||
| 322 | } | ||
| 323 | $rb->text_at( $_label_line, 0, "[" ); | ||
| 324 | $rb->text_at( $_label_line, $cols-1, "]" ); | ||
| 325 | } | ||
| 326 | |||
| 327 | $rb->text_at( $_label_line, $_label_col - 2, $marker_left ); | ||
| 328 | $rb->text_at( $_label_line, $_label_end, $marker_right ); | ||
| 329 | |||
| 330 | $rb->text_at( $_label_line, $_label_col, $self->label ); | ||
| 331 | } | ||
| 332 | |||
| 333 | method on_mouse | ||
| 334 | { | ||
| 335 | my ( $args ) = @_; | ||
| 336 | |||
| 337 | my $type = $args->type; | ||
| 338 | my $button = $args->button; | ||
| 339 | |||
| 340 | return if $type eq "wheel" or $button != 1; | ||
| 341 | |||
| 342 | if( $type eq "press" ) { | ||
| 343 | $self->_activate( 1 ); | ||
| 344 | } | ||
| 345 | elsif( $type eq "drag_start" ) { | ||
| 346 | $_dragging_on_self = 1; | ||
| 347 | } | ||
| 348 | elsif( $type eq "drag_stop" ) { | ||
| 349 | $_dragging_on_self = 0; | ||
| 350 | } | ||
| 351 | elsif( $type eq "drag" ) { | ||
| 352 | # TODO: This could be neater with an $arg->srcwin | ||
| 353 | $self->_activate( 1 ) if $_dragging_on_self and !$_active; | ||
| 354 | } | ||
| 355 | elsif( $type eq "drag_outside" ) { | ||
| 356 | $self->_activate( 0 ) if $_active; | ||
| 357 | } | ||
| 358 | elsif( $type eq "release" ) { | ||
| 359 | if( $_active ) { | ||
| 360 | $self->_activate( 0 ); | ||
| 361 | $self->click; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | return 1; | ||
| 366 | } | ||
| 367 | |||
| 368 | =head1 AUTHOR | ||
| 369 | |||
| 370 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 371 | |||
| 372 | =cut | ||
| 373 | |||
| 374 | 0x55AA; |