package Tk::MyBrowseEntry; use Tk::BrowseEntry; @ISA = qw(Tk::BrowseEntry); Construct Tk::Widget 'MyBrowseEntry'; #公開したいサブルーチン #@EXPORT = qw(); use strict; #use base qw(Tk::Widget); use File::DosGlob 'glob'; use Tk::Listbox; sub Populate { my($cw, $args) = @_; $cw->SUPER::Populate($args); return; # my $b = $cw->Entry(); # $b->pack(-expand => 1, -fill => 'both'); # $cw->Advertise('MyEntry', => $b); # $cw->ConfigSpecs(DEFAULT => [$b]); # $cw->Delegates(DEFAULT => $b); } sub ClassInit { my ($class, $mw) = @_; return $class; } BEGIN {} sub new { my $class = shift; my ($parent,@args) = @_; my $IsReadOnly = 0; my @newargs; push(@newargs, $_[0]); my $TextVar; my $pSelections; my $SelIndex; for(my $i = 0 ; $i < @args ; $i += 2) { #print "new:arg$i: $args[$i] => $args[$i+1]\n"; if($args[$i] eq '-variable') { my $p = $args[$i+1]; $TextVar = $p; # $TextVar = $$p; next; } if($args[$i] eq '-state') { $IsReadOnly = 1 if($args[$i] eq 'readonly'); } if($args[$i] eq '-Selections') { $pSelections = $args[$i+1]; next; } if($args[$i] eq '-SelIndex') { $SelIndex = $args[$i+1]; next; } push(@newargs, $args[$i]); push(@newargs, $args[$i+1]); } my $self = Tk::BrowseEntry->new(@newargs); if(defined $TextVar) { $self->{variable} = $TextVar; $self->configure(-variable => $self->{variable} ); } else { my $var = ''; $self->{variable} = \$var; #''; $self->configure(-variable => $self->{variable} ); # $self->{variable} = ''; # $self->configure(-variable => \($self->{variable}) ); } # $self->configure(-variable => $self->{variable} ) if(defined $self->{variable}); # $self->configure(-variable => \($self->{variable}) ) if(defined $self->{variable}); if($IsReadOnly) { $self->Subwidget("entry")->configure( -fg => 'red', -background => 'white', -labelBackground => 'white', ); $self->Subwidget("choices")->configure( -background => 'white' ); $self->Subwidget("slistbox")->configure( -background => 'white', ); } my $lb = $self->Subwidget("slistbox"); $lb->bind('', [ sub { $_[0]->yviewScroll(-($_[1]/120)*3, 'units'); }, Tk::Ev('D') ] ); my $this = bless $self, ref($class) || $class; if($pSelections) { for(my $i = 0 ; $i < @$pSelections ; $i++) { $this->AddItem($pSelections->[$i]); } } if(defined $SelIndex) { $this->SetCurSel($SelIndex); } return $this; } sub DESTROY { my $this = shift; $this->SUPER::DESTROY(@_); } sub SetTitle { my ($this, $title) = @_; #print "SetTitle: $title\n"; return ${$this->{variable}} = $title; # return $this->{variable} = $title; } sub GetTitle { my ($this) = @_; return ${$this->{variable}}; # return $this->{variable}; } sub SetText { my ($this, $title) = @_; return $this->SetTitle($title); } sub GetText { my ($this) = @_; return $this->GetTitle(); } sub GetItemNumber { my ($this) = @_; return $this->size(); } #$indexの項目が見えるようにスクロールする sub ShowIndexItem { my ($this, $index) = @_; return $this->see($index); } sub GetCurSelItem { my ($this) = @_; return $this->get("active"); } sub GetCurSel { my ($this) = @_; # my @selections = $this->Listbox->curselection(); # return $selections[0] if(@selections > 0); # return -1; # my $sel = $this->get("active"); my $sel = $this->GetTitle(); for(my $i = 0 ; ; $i++) { my $s = $this->get($i); last if(!defined $s); return $i if($s eq $sel); } return -1; # my $ret = $this->Listbox->index("active"); # my $ret = $this->index("anchor"); #print "ret=$ret\n"; # return $ret; } sub GetSel { my ($this, $index) = @_; return $this->get($index); # return $this->ListboxWidget->selectionIncludes($index); } sub FindText { my ($this, @Texts) = @_; for(my $i = 0 ; $i < @Texts ; $i++) { my $text = $Texts[$i]; for(my $i = 0 ; ; $i++) { my $s = $this->get($i); last if(!defined $s); #print "s: $s [$text]\n"; return $i if($s =~ /^$text$/i); } } return -1; } sub SetCurSel { my ($this, @labels) = @_; my $text; my $index; for(my $i = 0 ; $i < @labels ; $i++) { if($labels[$i] =~ /^\d+$/) { $text = $this->GetSel($labels[$i]); $index = $labels[$i]; last if(defined $text and $text ne ''); } else { $index = $this->FindText($labels[$i]); if($index >= 0) { $text = $this->get($index); #Get$labels[$i]; last; } } } #print "text: $text,$index\n"; # if(defined $index) { # $this->SetText($text); ## $this->set($index); # } # elsif(defined $text) { if(defined $text) { $this->SetText($text); } return $text; # return $this->activate($index); } sub SetSel { my ($this, $index) = @_; return $this->SetCurSel($index); } sub DeleteItem { my ($this, $s, $e) = @_; if(!$e) { return $this->delete($s); } return $this->delete($s, $e); } sub DeleteAllItem { my ($this) = @_; return $this->delete(0, 'end'); } sub GetAllItem { my ($this) = @_; return $this->get(0, 'end'); } sub GetItem { my ($this, $s, $e) = @_; if(!$e) { return $this->get($s); } return $this->get($s, $e); } sub InsertItem { my ($this, $index, @lists) = @_; return $this->insert($index, @lists); } sub AddItemIfNew { my ($this, @lists) = @_; my $n = @lists; for(my $i = 0 ; $i < $n ; $i++) { next if($this->FindText($lists[$i]) >= 0); $this->AddItem($lists[$i]); } } sub AddItem { my ($this, @lists) = @_; return $this->insert("end", @lists); } # specify: 'file', 'dir', 'all' sub ReadFileList { my ($this, $dir, $RegFileMask, $specify, $IncludeDirPath, $DeleteAll, $DoSort) = @_; $DoSort = 0 if(!defined $DoSort); $this->DeleteAllItem() if($DeleteAll); $dir = Deps::ExtractDirectory($dir); my $path = Deps::MakePath($dir, '*'); my @list = glob($path); if($DoSort) { @list = sort(@list); } $RegFileMask = ".*" unless($RegFileMask); for my $f (@list) { next if($specify eq 'file' and -d $f); next if($specify eq 'dir' and -f $f); my $filename = Deps::ExtractFileName($f); #print "f=$filename r=$RegFileMask\n"; next unless($filename =~ /$RegFileMask/i); $f = $filename unless($IncludeDirPath); $this->AddItem($f); } $this->SetSel(0); return @list; } 1;