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
|
# 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, 2012-2023 -- leonerd@leonerd.org.uk
use v5.20;
use warnings;
use Object::Pad 0.807;
package Tickit::Widget::Button 0.42;
class Tickit::Widget::Button :strict(params);
inherit Tickit::Widget;
use Tickit::Style;
use Tickit::RenderBuffer qw( LINE_SINGLE LINE_DOUBLE LINE_THICK );
use Tickit::Utils qw( textwidth );
use constant CAN_FOCUS => 1;
=head1 NAME
C<Tickit::Widget::Button> - a widget displaying a clickable button
=head1 SYNOPSIS
use Tickit;
use Tickit::Widget::Button;
my $button = Tickit::Widget::Button->new(
label => "Click Me!",
on_click => sub {
my ( $self ) = @_;
# Do something!
},
);
Tickit->new( root => $button )->run;
=head1 DESCRIPTION
This class provides a widget which displays a clickable area with a label.
When the area is clicked, a callback is invoked.
=head1 STYLE
The default style pen is used as the widget pen. The following style keys are
used:
=over 4
=item linetype => STRING
What kind of border to draw around the button; one of
none single double thick
=item marker_left => STRING
A two-character string to place just before the button label
=item marker_right => STRING
A two-character string to place just after the button label
=back
The following style tags are used:
=over 4
=item :active
Set when the mouse is being held over the button, before it is released
=back
The following style actions are used:
=over 4
=item click
The main action to activate the C<on_click> handler.
=back
=cut
style_definition base =>
fg => "black",
bg => "blue",
linetype => "single",
marker_left => "> ",
marker_right => " <",
'<Enter>' => "click";
style_definition ':focus' =>
marker_left => ">>",
marker_right => "<<";
style_definition ':active' =>
rv => 1;
style_reshape_keys qw( linetype );
style_redraw_keys qw( marker_left marker_right );
use constant WIDGET_PEN_FROM_STYLE => 1;
use constant KEYPRESSES_FROM_STYLE => 1;
=head1 CONSTRUCTOR
=cut
=head2 new
$button = Tickit::Widget::Button->new( %args );
Constructs a new C<Tickit::Widget::Button> object.
Takes the following named arguments:
=over 8
=item label => STR
Text to display in the button area
=item on_click => CODE
Optional. Callback function to invoke when the button is clicked.
=back
=cut
field $_label :reader :param = undef;
field $_on_click :reader :writer :param = undef;
field $_active;
field $_dragging_on_self;
ADJUST :params (
:$align = 0.5,
:$valign = 0.5,
) {
$self->set_align( $align );
$self->set_valign( $valign );
}
method lines
{
my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none";
return 1 + 2*$has_border;
}
method cols
{
return 4 + textwidth( $self->label ) + 2;
}
=head1 ACCESSORS
=cut
=head2 label
$label = $button->label;
=cut
# generated accessor
=head2 set_label
$button->set_label( $label );
Return or set the text to display in the button area.
=cut
method set_label
{
( $_label ) = @_;
$self->redraw;
}
=head2 on_click
$on_click = $button->on_click;
=cut
# generated accessor
=head2 set_on_click
$button->set_on_click( $on_click );
Return or set the CODE reference to be called when the button area is clicked.
$on_click->( $button );
=cut
# generated accessor
=head2 click
$button->click;
Behave as if the button has been clicked; running its C<on_click> handler.
This is provided for convenience of activating its handler programmatically
via other parts of code.
=cut
method click
{
$_on_click->( $self );
}
# Activation by key should "flash" the button briefly on the screen as a
# visual feedback
method key_click
{
$self->click;
if( my $window = $self->window ) {
$self->set_style_tag( active => 1 );
$window->tickit->timer( after => 0.1, sub { $self->set_style_tag( active => 0 ) } );
}
return 1;
}
method _activate
{
my ( $active ) = @_;
$_active = $active;
$self->set_style_tag( active => $active );
}
=head2 align
=head2 set_align
=head2 valign
=head2 set_valign
$align = $button->align;
$button->set_align( $align );
$valign = $button->valign;
$button->set_valign( $valign );
Accessors for the horizontal and vertical alignment of the label text within
the button area. See also L<Tickit::WidgetRole::Alignable>.
=cut
use Tickit::WidgetRole::Alignable name => "align", style => "h";
use Tickit::WidgetRole::Alignable name => "valign", style => "v";
field $_label_line;
field $_label_col;
field $_label_end;
method reshape
{
my $win = $self->window or return;
my $lines = $win->lines;
my $cols = $win->cols;
my $width = textwidth $self->label;
my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none";
my ( $lines_before, undef, $lines_after ) = $self->_valign_allocation( 1, $lines - (2 * $has_border) );
my ( $cols_before, undef, $cols_after ) = $self->_align_allocation( $width + 2, $cols - 2 );
$_label_line = $lines_before + $has_border;
$_label_col = $cols_before + 2;
$_label_end = $cols_before + $width + 2;
$win->cursor_at( $_label_line, $_label_col );
}
method render_to_rb
{
my ( $rb, $rect ) = @_;
my $win = $self->window or return;
my $lines = $win->lines;
my $cols = $win->cols;
my ( $linetype, $marker_left, $marker_right ) =
$self->get_style_values(qw( linetype marker_left marker_right ));
my $linestyle = $linetype eq "single" ? LINE_SINGLE :
$linetype eq "double" ? LINE_DOUBLE :
$linetype eq "thick" ? LINE_THICK :
undef;
if( defined $linestyle ) {
$rb->hline_at( 0, 0, $cols-1, $linestyle );
$rb->hline_at( $lines-1, 0, $cols-1, $linestyle );
$rb->vline_at( 0, $lines-1, 0, $linestyle );
$rb->vline_at( 0, $lines-1, $cols-1, $linestyle );
foreach my $line ( $rect->linerange( 1, $lines-2 ) ) {
$rb->erase_at( $line, 1, $cols-2 );
}
}
else {
foreach my $line ( $rect->linerange( 0, $lines-1 ) ) {
$rb->erase_at( $line, 0, $cols );
}
$rb->text_at( $_label_line, 0, "[" );
$rb->text_at( $_label_line, $cols-1, "]" );
}
$rb->text_at( $_label_line, $_label_col - 2, $marker_left );
$rb->text_at( $_label_line, $_label_end, $marker_right );
$rb->text_at( $_label_line, $_label_col, $self->label );
}
method on_mouse
{
my ( $args ) = @_;
my $type = $args->type;
my $button = $args->button;
return if $type eq "wheel" or $button != 1;
if( $type eq "press" ) {
$self->_activate( 1 );
}
elsif( $type eq "drag_start" ) {
$_dragging_on_self = 1;
}
elsif( $type eq "drag_stop" ) {
$_dragging_on_self = 0;
}
elsif( $type eq "drag" ) {
# TODO: This could be neater with an $arg->srcwin
$self->_activate( 1 ) if $_dragging_on_self and !$_active;
}
elsif( $type eq "drag_outside" ) {
$self->_activate( 0 ) if $_active;
}
elsif( $type eq "release" ) {
if( $_active ) {
$self->_activate( 0 );
$self->click;
}
}
return 1;
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|