blib/lib/Tk/SimpleFileSelect.pm

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