package MyText; use Tk::TextUndo; use Tk::TextEdit; use MyTk::TkCommon; @ISA = qw(Tk::TextUndo TkCommon); Construct Tk::Widget 'MyText'; use Tk::ROText; #公開したいサブルーチン #@EXPORT = qw(); use strict; #use base qw(Tk::Widget); use JFile; sub Populate { my($cw, $args) = @_; $cw->SUPER::Populate($args); return; # my $b = $cw->Text(); # $b->pack(-expand => 1, -fill => 'both'); # $cw->Advertise('MyText', => $b); # $cw->ConfigSpecs(DEFAULT => [$b]); # $cw->Delegates(DEFAULT => $b); } sub ClassInit { my ($class, $mw) = @_; return $class; } sub new { my $class = shift; my ($parent,@args) = @_; # print "new: parent=$parent\n"; my @newargs; push(@newargs, $_[0]); my $pText; my $readonly = 0; for(my $i = 0 ; $i < @args ; $i += 2) { # print "new:arg$i: $args[$i] => $args[$i+1]\n"; if($args[$i] eq '-text' or $args[$i] eq '-textvariable') { $pText = $args[$i+1]; next; } if($args[$i] eq '-readonly') { $readonly = $args[$i+1]; next; } push(@newargs, $args[$i]); push(@newargs, $args[$i+1]); } my $self; if($readonly) { $self = Tk::ROText->new(@newargs); } else { $self = Tk::Text->new(@newargs); } my $this = bless $self, ref($class) || $class; if(!$pText) { my $v = ''; $pText = \$v;; } $self->{textvariable} = $pText; $this->SetText($$pText); $this->Initialize(); $this->bind('', [ sub { $_[0]->yviewScroll(-($_[1]/120)*3, 'units'); }, Tk::Ev('D') ] ); if ( $Tk::platform eq 'unix' ) { $this->bind('<4>', sub { $_[0]->yviewScroll(-3, 'units') unless $Tk::strictMotif; } ); $this->bind('<5>', sub { $_[0]->yviewScroll( 3, 'units') unless $Tk::strictMotif; } ); } # $this->bind("", [\&KeyIn, Tk::Ev('k'), Tk::Ev('K'), Tk::Ev('A') ] ); return $this; } sub DESTROY { my $this = shift; $this->SUPER::DESTROY(@_); } sub Initialize { my ($this) = @_; $this->{'IsModified'} = 0; } sub KeyIn { my ($this, $key, $Key, $ASCII) = @_; my $str = "$key: $Key: $ASCII"; my ($line, $col) = split(/\./, $this->index('insert')); $this->mw()->WriteStatusBar("($line : $col)", 'left'); if($ASCII ne '' or $Key =~ /delete/i) { $this->mw()->WriteStatusBar("$str:ModifiedKey", 'right'); $this->{'IsModified'} = 1; } else { $this->mw()->WriteStatusBar("$str:NonModifiedKey", 'right'); } } sub IsModified { return shift->{'IsModified'}; } sub SetModified { my ($this,$f)=@_; return shift->{'IsModified'} = $f; } sub SetTitle { my ($this, $title) = @_; $this->delete('1.0', 'end'); $this->insert('end', $title); return ${$this->{textvariable}} = $title; } sub GetTitle { my ($this) = @_; return ${$this->{textvariable}} = $this->get('1.0', 'end'); } sub SetText { my ($this, $title) = @_; return $this->SetTitle($title); } sub GetText { my ($this) = @_; return $this->GetTitle(); } sub ClearText { my ($this, $title) = @_; $this->delete('1.0', 'end'); } sub AddText { my ($this, $text) = @_; $this->insert('end', $text); return $text; } sub GetFirstLines { my ($this, $start, $end) = @_; $start = 0 if(!defined $start); $end = $start if(!defined $end); return $this->get("0.0 + $start lines linestart", "0.0 + $end lines lineend"); } sub GetLastLines { my ($this, $start, $end) = @_; $start = 0 if(!defined $start); $end = $start if(!defined $end); return $this->get("end - $start lines linestart", "end - $end lines lineend"); } sub SetFont { my ($this, $FontName, $FontSize, $FontStyle) = @_; my $font = [$FontName, $FontSize, $FontStyle]; return $this->configure(-font, $font); } sub UpdateFont { my ($this, $FontName, $FontSize, $FontStyle) = @_; $this->SetFont($FontName,$FontSize,$FontStyle); $this->Update(); } sub Update { return 1; } sub ReadFile { my ($this, $path, $charcode) = @_; $this->ClearText(); my $infile = new JFile; my $ret = $infile->Open($path, "r", $charcode); unless($ret) { print "Can not open [" . $this->IniFile() . "].\n"; return undef; } my $line; while($line = $infile->ReadLine()) { $this->AddText($line); } $infile->Close(); return $path; } sub ClipboardCopy { my ($this) = @_; if($this->mw()->SelectionOwner() eq $this) { # my $sel = $this->SelectionGet(-selection => 'PRIMARY'); my $sel = $this->get('sel.first', 'sel.last'); $this->clipboardClear(); $this->delete('sel.first', 'sel.last'); $this->clipboardAppend($sel); } } sub ClipboardCopyAndCut { my ($this) = @_; if($this->mw()->SelectionOwner() eq $this) { # my $sel = $this->SelectionGet(-selection => 'PRIMARY'); my $sel = $this->get('sel.first', 'sel.last'); $this->clipboardClear(); $this->delete('sel.first', 'sel.last'); $this->clipboardAppend($sel); } } sub ClipboardPaste { my ($this) = @_; if($this->mw()->SelectionOwner() eq $this) { my $sel = $this->SelectionGet(-selection => 'PRIMARY'); $this->clipboardClear(); $this->delete('sel.first', 'sel.last'); my $idx = $this->index('insert'); my $contents = $this->SelectionGet(-selection => 'CLIPBOARD'); $this->insert($idx, $contents); } } 1;