| 1 | package Tk::SimpleFileSelect; | ||
| 2 | $VERSION=0.67; | ||
| 3 | use vars qw($VERSION @EXPORT_OK); | ||
| 4 | @EXPORT_OK = qw(glob_to_re); | ||
| 5 | |||
| 6 | use Tk qw(Ev); | ||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | use base qw(Tk::Toplevel); | ||
| 10 | use Tk::widgets qw(LabEntry Button Frame Listbox Scrollbar); | ||
| 11 | use File::Basename; | ||
| 12 | |||
| 13 | Construct Tk::Widget 'SimpleFileSelect'; | ||
| 14 | |||
| 15 | sub Cancel { | ||
| 16 | my ($cw) = @_; | ||
| 17 | $cw -> withdraw; | ||
| 18 | return ''; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub Accept_dir { | ||
| 22 | my ($cw,$new) = @_; | ||
| 23 | my $dir = $cw->cget('-directory'); | ||
| 24 | $cw->configure(-directory => "$dir/$new", -title => "$dir/$new" ); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub Open { | ||
| 28 | my ($cw) = @_; | ||
| 29 | my ($entry,$path); | ||
| 30 | $entry = $cw -> Subwidget('file_entry') -> get; | ||
| 31 | $path = $cw -> {'Configure'}{'-directory'}; | ||
| 32 | if (defined $entry and length($entry)) { | ||
| 33 | if ( -d "$path/$entry" ) { | ||
| 34 | $cw -> directory( "$path/$entry" ); | ||
| 35 | $cw -> Accept_dir; | ||
| 36 | } else { | ||
| 37 | $cw -> {Selected} = "$path/$entry"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | sub Populate { | ||
| 43 | my ($w, $args) = @_; | ||
| 44 | require Tk::Listbox; | ||
| 45 | require Tk::Button; | ||
| 46 | require Tk::Dialog; | ||
| 47 | require Tk::DialogBox; | ||
| 48 | require Tk::Toplevel; | ||
| 49 | require Tk::LabEntry; | ||
| 50 | require Cwd; | ||
| 51 | $w->SUPER::Populate($args); | ||
| 52 | |||
| 53 | $w->ConfigSpecs( | ||
| 54 | -font => ['CHILDREN',undef,undef, | ||
| 55 | '*-helvetica-medium-r-*-*-12-*'], | ||
| 56 | -width => [ ['dir_list'], undef, undef, 30 ], | ||
| 57 | -height => [ ['dir_list'], undef, undef, 14 ], | ||
| 58 | -directory => [ 'METHOD', undef, undef, '.' ], | ||
| 59 | -initialdir => '-directory', | ||
| 60 | -files => [ 'PASSIVE', undef,undef,1 ], | ||
| 61 | -selectedfile => [ 'PASSIVE', undef,undef,''], | ||
| 62 | -dotfiles => [ 'PASSIVE', undef,undef,0 ], | ||
| 63 | -filter => [ 'METHOD', undef, undef, undef ], | ||
| 64 | '-accept' => [ 'CALLBACK',undef,undef, undef ], | ||
| 65 | -create => [ 'PASSIVE', undef, undef, 0 ], | ||
| 66 | -acceptlabel => [ 'PASSIVE', undef, undef, 'Accept' ], | ||
| 67 | -initialtext => [ 'PASSIVE', undef, undef, '' ], | ||
| 68 | DEFAULT => [ 'dir_list' ], | ||
| 69 | ); | ||
| 70 | |||
| 71 | $w->protocol('WM_DELETE_WINDOW' => ['Cancel', $w ]); | ||
| 72 | $w->{'reread'} = 0; | ||
| 73 | my $l = $w -> Component( Label => 'entry_label',-text => 'File Name: '); | ||
| 74 | $l -> grid( -column => 1, -row => 3, -padx => 5, -pady => 5 ); | ||
| 75 | my $e = $w -> Component(Entry => 'file_entry', | ||
| 76 | -textvariable=>\$w->{Configure}{-initialtext}); | ||
| 77 | $e->grid(-column => 2, -columnspan => 1, -padx => 5, -pady => 5, | ||
| 78 | -row => 3, -sticky => 'e,w' ); | ||
| 79 | $e->bind('<Return>' => [$w => 'Open', Ev(['getSelected'])]); | ||
| 80 | my $lb = $w->Component( ScrlListbox => 'dir_list', | ||
| 81 | -scrollbars => 'se', -width => \$w -> {Configure}{-width}, | ||
| 82 | -height => \$w -> {Configure}{-height} ); | ||
| 83 | $lb -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 84 | $lb -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 85 | $lb->grid( -column => 2, -row => 1, -rowspan => 2, -padx => 5, | ||
| 86 | -pady => 5, -sticky => 'nsew' ); | ||
| 87 | $lb->bind('<Double-Button-1>' => [$w => 'Open', Ev(['getSelected'])]); | ||
| 88 | $lb->bind('<Button-1>', sub {($w->{Configure}{-initialtext}= | ||
| 89 | $lb->get($lb->curselection))}); | ||
| 90 | $b = $w -> Button(-textvariable => \$w->{'Configure'}{'-acceptlabel'}, | ||
| 91 | -underline => 0,-command => [$w => 'Open', Ev(['getSelected']) ]); | ||
| 92 | $b->grid(-column=>1,-row=>1,-padx=>5,-pady=>5,-sticky=>'sew'); | ||
| 93 | $b = $w->Button( -text => 'Cancel', -underline => 0, | ||
| 94 | -command => [ 'Cancel', $w ]); | ||
| 95 | $b->grid( -column => 1, -row => 2, -padx => 5, -pady => 5, | ||
| 96 | -sticky => 'new' ); | ||
| 97 | $w -> bind( '<Alt-c>', [$w => 'Cancel', $w]); | ||
| 98 | $w -> Subwidget('file_entry') -> focus; | ||
| 99 | $w -> eventAdd( '<<Accept>>', '<Alt-a>'); | ||
| 100 | $w -> bind('<<Accept>>', [$w => 'Open', Ev(['getSelected']) ]); | ||
| 101 | $w->Delegates(DEFAULT => 'dir_list'); | ||
| 102 | return $w; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub translate { | ||
| 106 | my ($bs,$ch) = @_; | ||
| 107 | return "\\$ch" if (length $bs); | ||
| 108 | return '.*' if ($ch eq '*'); | ||
| 109 | return '.' if ($ch eq '?'); | ||
| 110 | return "\\." if ($ch eq '.'); | ||
| 111 | return "\\/" if ($ch eq '/'); | ||
| 112 | return "\\\\" if ($ch eq '\\'); | ||
| 113 | return $ch; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub glob_to_re { | ||
| 117 | my $regex = shift; | ||
| 118 | $regex =~ s/(\\?)(.)/&translate($1,$2)/ge; | ||
| 119 | return sub { shift =~ /^${regex}$/ }; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub filter { | ||
| 123 | my ($cw,$val) = @_; | ||
| 124 | my $var = \$cw->{Configure}{'-filter'}; | ||
| 125 | if (@_ > 1 || !defined($$var)) { | ||
| 126 | $val = '*' unless defined $val; | ||
| 127 | $$var = $val; | ||
| 128 | $cw->{'match'} = glob_to_re($val) unless defined $cw->{'match'}; | ||
| 129 | unless ($cw->{'reread'}++) { | ||
| 130 | $cw->Busy; | ||
| 131 | if( ( $cw -> cget( '-connected' ) ) =~ /1/ ) { | ||
| 132 | $cw->afterIdle(['rereadRemote',$cw,$cw->cget('-directory')]) | ||
| 133 | } else { | ||
| 134 | $cw->afterIdle(['reread',$cw,$cw->cget('-directory')]); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | return $$var; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub defaultextension { | ||
| 142 | my ($cw,$val) = @_; | ||
| 143 | if (@_ > 1) { | ||
| 144 | $val = ".$val" if ($val !~ /^\./); | ||
| 145 | $cw->filter("*$val"); | ||
| 146 | } else { | ||
| 147 | $val = $cw->filter; | ||
| 148 | my ($ext) = $val =~ /(\.[^\.]*)$/; | ||
| 149 | return $ext; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | sub directory { | ||
| 154 | my ($cw,$dir) = @_; | ||
| 155 | my $var = \$cw->{Configure}{'-directory'}; | ||
| 156 | if (@_ > 1 && defined $dir) { | ||
| 157 | if (substr($dir,0,1) eq '~') { | ||
| 158 | if (substr($dir,1,1) eq '/') { | ||
| 159 | $dir = $ENV{'HOME'} . substr($dir,1); | ||
| 160 | } else { | ||
| 161 | my ($uid,$rest) = ($dir =~ m#^~([^/]+)(/.*$)#); | ||
| 162 | $dir = (getpwnam($uid))[7] . $rest; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | $dir =~ s#([^/\\])[\\/]+$#$1#; | ||
| 166 | if (-d $dir) { | ||
| 167 | unless (Tk::tainting()) { | ||
| 168 | my $pwd = Cwd::getcwd(); | ||
| 169 | if (chdir( (defined($dir) ? $dir : '') ) ) { | ||
| 170 | my $new = Cwd::getcwd(); | ||
| 171 | if ($new) { | ||
| 172 | $dir = $new; | ||
| 173 | } else { | ||
| 174 | carp "Cannot getcwd in '$dir'"; | ||
| 175 | } | ||
| 176 | chdir($pwd) || carp "Cannot chdir($pwd) : $!"; | ||
| 177 | $cw->{Configure}{'-directory'} = $dir; | ||
| 178 | $cw->configure(-title=>$dir); | ||
| 179 | } else { | ||
| 180 | $cw->BackTrace("Cannot chdir($dir) :$!"); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | $$var = $dir; | ||
| 184 | unless ($cw->{'reread'}++) { | ||
| 185 | $cw->Busy; | ||
| 186 | $cw->afterIdle(['reread',$cw]); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | return $$var; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | sub reread { | ||
| 196 | my ($w) = @_; | ||
| 197 | my $dir = $w->cget('-directory'); | ||
| 198 | my ($f, $seen); | ||
| 199 | if (defined $dir) { | ||
| 200 | if (!defined $w->cget('-filter') or $w->cget('-filter') eq '') { | ||
| 201 | $w->configure('-filter', '*'); | ||
| 202 | } | ||
| 203 | my $dl = $w->Subwidget('dir_list'); | ||
| 204 | $dl->delete(0, 'end'); | ||
| 205 | local *DIR; | ||
| 206 | my $h; | ||
| 207 | if (opendir(DIR, $dir)) { | ||
| 208 | my $file = $w->cget('-selectedfile'); | ||
| 209 | my $seen = 0; | ||
| 210 | my $accept = $w->cget('-accept'); | ||
| 211 | foreach $f (sort(readdir(DIR))) { | ||
| 212 | next if ($f eq '.'); | ||
| 213 | next if $f =~ /^\.[^\.]/ and ! $w -> {Configure}{-dotfiles} ; | ||
| 214 | my $path = "$dir/$f"; | ||
| 215 | if (-d $path) { | ||
| 216 | $dl->insert('end', $f.'/'); | ||
| 217 | } elsif ($w -> cget('-files')) { | ||
| 218 | if (&{$w->{match}}($f)) { | ||
| 219 | if (!defined($accept) || $accept->Call($path)) { | ||
| 220 | $seen = $dl->index('end') | ||
| 221 | if ($file && $f eq $file); | ||
| 222 | $dl->insert('end', $f); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | closedir(DIR); | ||
| 228 | if ($seen) { | ||
| 229 | $dl->selectionSet($seen); | ||
| 230 | $dl->see($seen); | ||
| 231 | } else { | ||
| 232 | $w->configure(-selectedfile => '') | ||
| 233 | unless $w->cget('-create'); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | $w->{DirectoryString} = $dir . '/' . $w->cget('-filter'); | ||
| 237 | $w->{'reread'} = 0; | ||
| 238 | $w->Unbusy; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | sub Error { | ||
| 243 | my $cw = shift; | ||
| 244 | my $msg = shift; | ||
| 245 | my $dlg = $cw->Subwidget('dialog'); | ||
| 246 | $dlg->configure(-text => $msg); | ||
| 247 | $dlg->Show; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub Show { | ||
| 251 | my ($cw,@args) = @_; | ||
| 252 | my $accel = lc(substr $cw->cget('-acceptlabel'),0,1); | ||
| 253 | $cw -> eventAdd('<<Accept>>', "\<Alt-$accel\>"); | ||
| 254 | $cw -> waitVariable(\$cw->{Selected}); | ||
| 255 | $cw -> withdraw; | ||
| 256 | return $cw -> {Selected}; | ||
| 257 | } | ||
| 258 | |||
| 259 | 1; | ||
| 260 | |||
| 261 | __END__ |