diff options
-rw-r--r-- | Perl/ZincText.pm | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/Perl/ZincText.pm b/Perl/ZincText.pm new file mode 100644 index 0000000..2785b39 --- /dev/null +++ b/Perl/ZincText.pm @@ -0,0 +1,93 @@ + +package ZincText; + + +sub new { + my $proto = shift; + my $type = ref($proto) || $proto; + my ($zinc) = @_; + my $self = {}; + + $zinc->bind('text', '<1>' => sub {startSel($zinc)}); + $zinc->bind('text', '<B1-Motion>' => sub {extendSel($zinc)}); + $zinc->bind('text', '<Shift-B1-Motion>' => sub {extendSel($zinc)}); + $zinc->bind('text', '<Shift-1>' => sub { + my $e = $zinc->XEvent(); + my($x, $y) = ($e->x, $e->y); + $zinc->select('adjust', 'current', "\@$x,$y"); }); + $zinc->bind('text', '<Left>' => sub {moveCur($zinc, -1);}); + $zinc->bind('text', '<Right>' => sub {moveCur($zinc, 1);}); + $zinc->bind('text', '<Control-a>' => sub {setCur($zinc, 0);}); + $zinc->bind('text', '<Home>' => sub {setCur($zinc, 0);}); + $zinc->bind('text', '<Control-e>' => sub {setCur($zinc, 'end');}); + $zinc->bind('text', '<End>' => sub {setCur($zinc, 'end');}); + $zinc->bind('text', '<KeyPress>' => sub {insertChar($zinc);}); + $zinc->bind('text', '<Shift-KeyPress>' => sub {insertChar($zinc);}); + $zinc->bind('text', '<Return>' => sub { + $zinc->insert($zinc->focus(), 'insert', "\n"); }); + $zinc->bind('text', '<BackSpace>' => sub {textDel($zinc, -1)}); + $zinc->bind('text', '<Delete>' => sub {textDel($zinc, 0)}); + $zinc->bind('text', '<Control-d>' => sub { + $zinc->dchars($zinc->focus(), 'sel.first', 'sel.last'); }); + $zinc->bind('text', '<Control-y>' => sub { + $zinc->insert($zinc->focus(), 'insert', Tk::selection('get')); }); + + bless ($self, $type); + return $self; +} + + +sub insertChar { + my ($w) = @_; + my $c = $w->XEvent->A(); + + $w->insert($w->focus(), 'insert', $c); +} + + +sub setCur { + my ($w, $where) = @_; + my $it = $w->focus(); + + $w->cursor($it, $where); +} + + +sub moveCur { + my ($w, $dir) = @_; + my $it = $w->focus(); + my $index = $w->index($it, 'insert'); + + $w->cursor($it, $index + $dir); +} + + +sub startSel { + my($w) = @_; + my $e = $w->XEvent; + my($x, $y) = ($e->x, $e->y); + + $w->cursor('current', "\@$x,$y"); + $w->focus('current'); + $w->Tk::focus; + $w->select('from', 'current', "\@$x,$y"); +} + + +sub extendSel { + my($w) = @_; + my $e = $w->XEvent; + my($x, $y) = ($e->x, $e->y); + $w->select('to', 'current', "\@$x,$y"); +} + + +sub textDel { + my($w, $dir) = @_; + my $it = $w->focus(); + my $ind = $w->index($it, 'insert') + $dir; + + $w->dchars($it, $ind) if ($ind >= 0); +} + +1; |