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', '' => sub {extendSel($zinc)}); $zinc->bind('text', '' => sub {extendSel($zinc)}); $zinc->bind('text', '' => sub { my $e = $zinc->XEvent(); my($x, $y) = ($e->x, $e->y); $zinc->select('adjust', 'current', "\@$x,$y"); }); $zinc->bind('text', '' => sub {moveCur($zinc, -1);}); $zinc->bind('text', '' => sub {moveCur($zinc, 1);}); $zinc->bind('text', '' => sub {setCur($zinc, 0);}); $zinc->bind('text', '' => sub {setCur($zinc, 0);}); $zinc->bind('text', '' => sub {setCur($zinc, 'end');}); $zinc->bind('text', '' => sub {setCur($zinc, 'end');}); $zinc->bind('text', '' => sub {insertChar($zinc);}); $zinc->bind('text', '' => sub {insertChar($zinc);}); $zinc->bind('text', '' => sub { $zinc->insert($zinc->focus(), 'insert', "\n"); }); $zinc->bind('text', '' => sub {textDel($zinc, -1)}); $zinc->bind('text', '' => sub {textDel($zinc, 0)}); $zinc->bind('text', '' => sub { $zinc->dchars($zinc->focus(), 'sel.first', 'sel.last'); }); $zinc->bind('text', '' => 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;