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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
# 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, 2011-2023 -- leonerd@leonerd.org.uk
use v5.20;
use warnings;
use Object::Pad 0.807;
package Tickit::Widget::Entry 0.42;
class Tickit::Widget::Entry :strict(params);
inherit Tickit::Widget;
use Carp;
use Tickit::Style;
Tickit::Window->VERSION( '0.39' ); # expose_after_scroll default on
use Tickit::Utils qw( textwidth chars2cols cols2chars substrwidth );
use constant CAN_FOCUS => 1;
# Positions in this code can get complicated. The following conventions apply:
# $pos_ch = a position in CHaracters within a Unicode string (length, substr,..)
# $pos_co = a position in screen COlumns counted from the start of the string
# $pos_x = a position in screen columns from the start of the window ($win positions)
=head1 NAME
C<Tickit::Widget::Entry> - a widget for entering text
=head1 SYNOPSIS
use Tickit;
use Tickit::Widget::Entry;
my $entry = Tickit::Widget::Entry->new(
on_enter => sub {
my ( $self, $line ) = @_;
# process $line somehow
$self->set_text( "" );
},
);
Tickit->new( root => $entry )->run;
=head1 DESCRIPTION
This class provides a widget which allows the user to enter a line of text.
=head1 STYLE
The default style pen is used as the widget pen. The following style pen
prefixes are also used:
=over 4
=item more => PEN
The pen used for the "more" scroll markers
=back
The following style keys are used:
=over 4
=item more_left => STRING
=item more_right => STRING
The text used to indicate that there is more content scrolled to the left or
right, respectively
=back
=cut
style_definition base =>
more_fg => "cyan",
more_left => "<..",
more_right => "..>";
use constant WIDGET_PEN_FROM_STYLE => 1;
=head1 KEYBINDINGS
The following keys are bound by default
=over 2
=item * Ctrl-K
Delete the entire line
=item * Ctrl-U
Delete to the start of the line
=item * Ctrl-W or Ctrl-Backspace
Delete one word backwards
=item * Backspace
Delete one character backwards
=item * Delete
Delete one character forwards
=item * Ctrl-Delete
Delete one word forwards
=item * End or Ctrl-E
Move the cursor to the end of the input line
=item * Enter
Accept a line of input by running the C<on_enter> action
=item * Home or Ctrl-A
Move the cursor to the beginning of the input line
=item * Insert
Toggle between overwrite and insert mode
=item * Left
Move the cursor one character left
=item * Ctrl-Left or Alt-B
Move the cursor one word left
=item * Right
Move the cursor one character right
=item * Ctrl-Right or Alt-F
Move the cursor one word right
=back
=cut
=head1 CONSTRUCTOR
=cut
=head2 new
$entry = Tickit::Widget::Entry->new( %args );
Constructs a new C<Tickit::Widget::Entry> object.
Takes the following named arguments:
=over 8
=item text => STR
Optional. Initial text to display in the box
=item position => INT
Optional. Initial position of the cursor within the text.
=item on_enter => CODE
Optional. Callback function to invoke when the C<< <Enter> >> key is pressed.
=back
=cut
field $_text :reader :param //= "";
field $_pos_ch :reader(position) :param(position) //= 0;
field $_scrolloffs_co = 0;
field $_overwrite = 0;
field %_keybindings = (
'C-a' => "key_beginning_of_line",
'C-e' => "key_end_of_line",
'C-k' => "key_delete_line",
'C-u' => "key_backward_delete_line",
'C-w' => "key_backward_delete_word",
'M-b' => "key_backward_word",
'M-f' => "key_forward_word",
'Backspace' => "key_backward_delete_char",
'C-Backspace' => "key_backward_delete_word",
'Delete' => "key_forward_delete_char",
'C-Delete' => "key_forward_delete_word",
'End' => "key_end_of_line",
'Enter' => "key_enter_line",
'Home' => "key_beginning_of_line",
'Insert' => "key_overwrite_mode",
'Left' => "key_backward_char",
'C-Left' => "key_backward_word",
'Right' => "key_forward_char",
'C-Right' => "key_forward_word",
);
field $_on_enter :reader :param = undef;
ADJUST
{
my $textlen = length $_text;
$_pos_ch = $textlen if $_pos_ch > $textlen;
# Since we take keyboard input we almost certainly want to take focus here
$self->take_focus;
}
method lines { 1 }
method cols { 5 }
method char2col
{
my ( $ch ) = @_;
return scalar chars2cols $_text, $ch;
}
method pretext_width
{
return 0 if $_scrolloffs_co == 0;
return textwidth( $self->get_style_values( "more_left" ) );
}
method pretext_render
{
my ( $rb ) = @_;
$rb->text_at( 0, 0, $self->get_style_values( "more_left" ), $self->get_style_pen( "more" ) );
}
method posttext_width
{
return 0 if textwidth( $self->text ) <= $_scrolloffs_co + $self->window->cols;
return textwidth( $self->get_style_values( "more_right" ) );
}
method posttext_render
{
my ( $rb ) = @_;
$rb->text_at( 0, 0, $self->get_style_values( "more_right" ), $self->get_style_pen( "more" ) );
}
method render_to_rb
{
my ( $rb, $rect ) = @_;
my $cols = $self->window->cols;
if( $rect->top == 0 ) {
my $text = substrwidth( $self->text, $_scrolloffs_co, $cols );
$rb->goto( 0, 0 );
$rb->text( $text ) if length $text;
$rb->erase_to( $cols );
if( my $pretext_width = $self->pretext_width ) {
$rb->save;
$rb->clip( Tickit::Rect->new( top => 0, left => 0, lines => 1, cols => $pretext_width ) );
$self->pretext_render( $rb );
$rb->restore;
}
if( my $posttext_width = $self->posttext_width ) {
$rb->save;
$rb->translate( 0, $cols - $posttext_width );
$rb->clip( Tickit::Rect->new( top => 0, left => 0, lines => 1, cols => $posttext_width ) );
$self->posttext_render( $rb );
$rb->restore;
}
}
foreach my $line ( $rect->linerange( 1, undef ) ) {
$rb->erase_at( $line, 0, $cols );
}
$self->reposition_cursor;
}
method _recalculate_scroll
{
my ( $pos_ch ) = @_;
my $pos_co = $self->char2col( $pos_ch );
my $off_co = $_scrolloffs_co;
my $pos_x = $pos_co - $off_co;
my $width = $self->window->cols;
my $halfwidth = int( $width / 2 );
# Don't even try unless we have at least 2 columns
return unless $halfwidth;
# Try to keep the cursor within 5 columns of the window edge
while( $pos_x < 5 and $off_co >= 5 ) {
$off_co -= $halfwidth;
$off_co = 0 if $off_co < 0;
$pos_x = $pos_co - $off_co;
}
while( $pos_x > ( $width - 5 ) ) {
$off_co += $halfwidth;
$pos_x = $pos_co - $off_co;
}
return $off_co if $off_co != $_scrolloffs_co;
return undef;
}
method reposition_cursor
{
my ( $pos_ch ) = @_;
$_pos_ch = $pos_ch if defined $pos_ch;
my $win = $self->window or return;
my $new_scrolloffs = $self->_recalculate_scroll( $_pos_ch );
if( defined $new_scrolloffs ) {
$_scrolloffs_co = $new_scrolloffs;
$self->redraw;
}
my $pos_x = $self->char2col( $_pos_ch ) - $_scrolloffs_co;
$win->cursor_at( 0, $pos_x );
}
method _text_spliced
{
my ( $pos_ch, $deleted, $inserted, $at_end ) = @_;
my $win = $self->window;
my $width = $win->cols;
my $insertedlen_co = textwidth $inserted;
my $deletedlen_co = textwidth $deleted;
my $delta_co = $insertedlen_co - $deletedlen_co;
my $pos_co = $self->char2col( $pos_ch );
my $pos_x = $pos_co - $_scrolloffs_co;
# Don't bother at all if the affected range is scrolled off the right
return if $pos_x >= $width;
if( $pos_x < 0 ) {
die "TODO: text_splice before window - what to do??\n";
}
my $need_reprint = 0;
# No point doing a scrollrect if there's nothing after here
if( $delta_co != 0 and !$at_end ) {
$win->scrollrect( 0, $pos_x, 1, $win->cols - $pos_x, 0, -$delta_co ) or
$need_reprint = 1;
}
if( $need_reprint ) {
# ICH/DCH failed; we'll have to reprint the entire rest of the line from
# here
$win->expose(
Tickit::Rect->new( top => 0, left => $pos_x, lines => 1, right => $width )
);
return;
}
if( $insertedlen_co > 0 ) {
$win->expose(
Tickit::Rect->new( top => 0, left => $pos_x, lines => 1, cols => $insertedlen_co )
);
if( my $posttext_width = $self->posttext_width ) {
$win->expose(
Tickit::Rect->new( top => 0, left => $width - $posttext_width, lines => 1, right => $width )
);
}
}
if( $delta_co < 0 and $_scrolloffs_co + $width < textwidth $self->text ) {
# Add extra damage to redraw the trashed posttext marker
my $rhs_x = -$delta_co + $self->posttext_width;
$win->expose(
Tickit::Rect->new( top => 0, left => $width - $rhs_x, lines => 1, right => $width )
);
}
}
method on_key
{
my ( $args ) = @_;
return 0 unless $self->window->is_focused;
my $type = $args->type;
my $str = $args->str;
if( $type eq "key" and my $code = $_keybindings{$str} ) {
$self->$code( $str );
return 1;
}
if( $type eq "text" ) {
$self->on_text( $str );
return 1;
}
return 0;
}
method on_text
{
my ( $text ) = @_;
$self->text_splice( $_pos_ch, $_overwrite ? 1 : 0, $text );
}
method on_mouse
{
my ( $args ) = @_;
return unless $args->type eq "press" and $args->button == 1;
my $pos_ch = scalar cols2chars $_text, $args->col + $_scrolloffs_co;
$self->set_position( $pos_ch );
}
=head1 ACCESSORS
=cut
=head2 on_enter
$on_enter = $entry->on_enter;
=head2 set_on_enter
$entry->set_on_enter( $on_enter );
Return or set the CODE reference to be called when the C<key_enter_line>
action is invoked; usually bound to the C<Enter> key.
$on_enter->( $entry, $line );
=cut
# generated accessor
method set_on_enter
{
( $_on_enter ) = @_;
}
=head2 position
$offset = $entry->position;
Returns the current entry position, in terms of characters within the text.
=cut
# generated accessor
=head2 set_position
$entry->set_position( $position );
Set the text entry position, moving the cursor
=cut
method set_position
{
my ( $pos_ch ) = @_;
$pos_ch = 0 if $pos_ch < 0;
$pos_ch = length $_text if $pos_ch > length $_text;
$self->reposition_cursor( $pos_ch );
}
=head1 METHODS
=cut
=head2 bind_keys
$entry->bind_keys( $keystr => $value, ... );
Associate methods or CODE references with keypresses. On receipt of a the key
the method or CODE reference will be invoked, being passed the stringified key
representation and the underlying C<Term::TermKey::Key> structure.
$ret = $entry->method( $keystr, $key );
$ret = $coderef->( $entry, $keystr, $key );
This method takes a hash of keystring/value pairs. Binding a value of C<undef>
will remove it.
=cut
method bind_keys
{
while( @_ ) {
my $str = shift;
my $value = shift;
if( defined $value ) {
$_keybindings{$str} = $value;
}
else {
delete $_keybindings{$str};
}
}
}
=head2 make_popup_at_cursor
$win = $entry->make_popup_at_cursor( $top_offset, $left_offset, $lines, $cols );
I<Since version 0.33.>
Creates a new popup window, as if calling L<Tickit::Window/make_popup> on the
widget's main window, but with an offset relative to the current cursor
position.
An offet of (0, 0) will position the popup window's top left corner exactly over
the cursor; this is likely not what you want.
To position the popup just below the widget, use a top offset of +1:
$win = $entry->make_popup_at_cursor( +1, 0, $lines, $cols );
To position the popup just above the widget, use a top offset of negative the
number of lines:
$win = $entry->make_popup_at_cursor( -$lines, 0, $lines, $cols );
=cut
method make_popup_at_cursor
{
my ( $topoff, $leftoff, $lines, $cols ) = @_;
$self->window or
croak "Cannot ->make_popup_at_cursor on an Entry widget with no window";
my $pos_x = $self->char2col( $_pos_ch ) - $_scrolloffs_co;
return $self->window->make_popup( $topoff, $pos_x + $leftoff, $lines, $cols );
}
=head1 TEXT MODEL METHODS
These methods operate on the text input buffer directly, updating the stored
text and changing the rendered display to reflect the changes. They can be
used by a program to directly manipulate the text.
=cut
=head2 text
$text = $entry->text;
Returns the currently entered text.
=cut
# generated accessor
=head2 set_text
$entry->set_text( $text );
Replace the text in the entry box. This completely redraws the widget's
window. It is largely provided for initialisation; for normal edits (such as
from keybindings), it is preferable to use C<text_insert>, C<text_delete> or
C<text_splice>.
=cut
method set_text
{
my ( $text ) = @_;
$_text = $text;
$_pos_ch = length $text if $_pos_ch > length $text;
$self->redraw;
}
=head2 text_insert
$entry->text_insert( $text, $pos_ch );
Insert the given text at the given character position.
=cut
method text_insert
{
my ( $text, $pos_ch ) = @_;
$self->text_splice( $pos_ch, 0, $text );
}
=head2 text_delete
$deleted = $entry->text_delete( $pos_ch, $len_ch );
Delete the given section of text. Returns the deleted text.
=cut
method text_delete
{
my ( $pos_ch, $len_ch ) = @_;
return $self->text_splice( $pos_ch, $len_ch, "" );
}
=head2 text_splice
$deleted = $entry->text_splice( $pos_ch, $len_ch, $text );
Replace the given section of text with the given replacement. Returns the
text deleted from the section.
=cut
method text_splice
{
my ( $pos_ch, $len_ch, $text ) = @_;
my $textlen_ch = length($text);
my $delta_ch = $textlen_ch - $len_ch;
my $at_end = ( $pos_ch == length $_text );
my $deleted = substr( $_text, $pos_ch, $len_ch, $text );
my $new_pos_ch;
if( $_pos_ch >= $pos_ch + $len_ch ) {
# Cursor after splice; move to suit
$new_pos_ch = $self->position + $delta_ch;
}
elsif( $_pos_ch >= $pos_ch ) {
# Cursor within splice; move to end
$new_pos_ch = $pos_ch + $textlen_ch;
}
# else { ignore }
# No point incrementally updating as we'll have to scroll anyway
unless( defined $new_pos_ch and defined $self->_recalculate_scroll( $new_pos_ch ) ) {
$self->_text_spliced( $pos_ch, $deleted, $text, $at_end );
}
$self->reposition_cursor( $new_pos_ch ) if defined $new_pos_ch and $new_pos_ch != $_pos_ch;
return $deleted;
}
=head2 find_bow_forward
$pos = $entry->find_bow_forward( $initial, $else );
Search forward in the string, returning the character position of the next
beginning of word from the initial position. If none is found, returns
C<$else>.
=cut
method find_bow_forward
{
my ( $pos, $else ) = @_;
my $posttext = substr( $self->text, $pos );
return $posttext =~ m/(?<=\s)\S/ ? $pos + $-[0] : $else;
}
=head2 find_eow_forward
$pos = $entry->find_eow_forward( $initial )
Search forward in the string, returning the character position of the next
end of word from the initial position. If none is found, returns the length of
the string.
=cut
method find_eow_forward
{
my ( $pos ) = @_;
my $posttext = substr( $self->text, $pos );
$posttext =~ m/(?<=\S)\s|$/;
return $pos + $-[0];
}
=head2 find_bow_backward
$pos = $entry->find_bow_backward( $initial );
Search backward in the string, returning the character position of the
previous beginning of word from the initial position. If none is found,
returns 0.
=cut
method find_bow_backward
{
my ( $pos ) = @_;
my $pretext = substr( $self->text, 0, $pos );
return $pretext =~ m/.*\s(?=\S)/ ? $+[0] : 0;
}
=head2 find_eow_backward
$pos = $entry->find_eow_backward( $initial );
Search backward in the string, returning the character position of the
previous end of word from the initial position. If none is found, returns
C<undef>.
=cut
method find_eow_backward
{
my ( $pos ) = @_;
my $pretext = substr( $self->text, 0, $pos + 1 ); # +1 to allow if cursor is on the space
return $pretext =~ m/.*\S(?=\s)/ ? $+[0] : undef;
}
## Key binding methods
method key_backward_char
{
if( $_pos_ch > 0 ) {
$self->set_position( $_pos_ch - 1 );
}
}
method key_backward_delete_char
{
if( $_pos_ch > 0 ) {
$self->text_delete( $_pos_ch - 1, 1 );
}
}
method key_backward_delete_line
{
$self->text_delete( 0, $_pos_ch );
}
method key_backward_delete_word
{
my $bow = $self->find_bow_backward( $_pos_ch );
$self->text_delete( $bow, $_pos_ch - $bow );
}
method key_backward_word
{
if( $_pos_ch > 0 ) {
$self->set_position( $self->find_bow_backward( $_pos_ch ) );
}
}
method key_beginning_of_line
{
$self->set_position( 0 );
}
method key_delete_line
{
$self->text_delete( 0, length $self->text );
}
method key_end_of_line
{
$self->set_position( length $_text );
}
method key_enter_line
{
my $text = $self->text;
return unless length $text;
$_on_enter->( $self, $text ) if $_on_enter;
}
method key_forward_char
{
if( $_pos_ch < length $_text ) {
$self->set_position( $_pos_ch + 1 );
}
}
# Renamed from readline's "delete-char" because this one doesn't have the EOF
# behaviour if input line is empty
method key_forward_delete_char
{
if( $_pos_ch < length $_text ) {
$self->text_delete( $_pos_ch, 1 );
}
}
method key_forward_delete_word
{
my $bow = $self->find_bow_forward( $_pos_ch, length $self->text );
$self->text_delete( $_pos_ch, $bow - $_pos_ch );
}
method key_forward_word
{
my $bow = $self->find_bow_forward( $_pos_ch, length $self->text );
$self->set_position( $bow );
}
method key_overwrite_mode
{
$_overwrite = !$_overwrite;
}
=head1 TODO
=over 4
=item * Plugin ability
Try to find a nice way to allow loaded plugins, possibly per-instance if not
just globally or per-class. See how many of these TODO items can be done using
plugins.
=item * More readline behaviours
History. Isearch. History replay. Transpose. Transcase. Yank ring. Numeric
prefixes.
=item * Visual selection behaviour
Shift-movement, or vim-style. Mouse.
=back
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|