Debian Codegraph @

Tixish/DirTree.pm

raw
1 package Tk::DirTree;
2 # DirTree -- TixDirTree widget
3 #
4 # Derived from DirTree.tcl in Tix 4.1
5 #
6 # Chris Dean <[email protected]>
7
8 use strict;
9 use vars qw($VERSION);
10 $VERSION = '4.022';
11
12 use Tk;
13 use Tk::Derived;
14 use Tk::Tree;
15 use Cwd;
16 use DirHandle;
17 use File::Spec qw();
18
19 use base qw(Tk::Derived Tk::Tree);
20 use strict;
21
22 Construct Tk::Widget 'DirTree';
23
24 my $sep = $^O eq 'MSWin32' ? '\\' : '/';
25
26 *_fs_encode = eval { require Encode; 1 } ? sub { Encode::encode("iso-8859-1", $_[0]) } : sub { $_[0] };
27
28 sub Populate {
29 my( $cw, $args ) = @_;
30
31 $cw->SUPER::Populate( $args );
32
33 $cw->ConfigSpecs(
34 -dircmd => [qw/CALLBACK dirCmd DirCmd DirCmd/],
35 -showhidden => [qw/PASSIVE showHidden ShowHidden 0/],
36 -image => [qw/PASSIVE image Image folder/],
37 -directory => [qw/SETMETHOD directory Directory ./],
38 -value => '-directory' );
39
40 $cw->configure( -separator => $sep,
41 -itemtype => 'imagetext',
42 );
43 }
44
45 sub DirCmd {
46 my( $w, $dir, $showhidden ) = @_;
47 $dir .= $sep if $dir =~ /^[a-z]:$/i and $^O eq 'MSWin32';
48 my $h = DirHandle->new( $dir ) or return();
49 my @names = grep( $_ ne '.' && $_ ne '..', $h->read );
50 @names = grep( ! /^[.]/, @names ) unless $showhidden;
51 return( @names );
52 }
53
54 *dircmd = \&DirCmd;
55
56 sub fullpath
57 {
58 my ($path) = @_;
59 my $cwd = getcwd();
60 if (CORE::chdir($path))
61 {
62 $path = getcwd();
63 CORE::chdir($cwd) || die "Cannot cd back to $cwd:$!";
64 }
65 else
66 {
67 warn "Cannot cd to $path:$!"
68 }
69 $path = File::Spec->canonpath($path);
70 return $path;
71 }
72
73 sub directory
74 {
75 my ($w,$key,$val) = @_;
76 # We need a value for -image, so its being undefined
77 # is probably caused by order of handling config defaults
78 # so defer it.
79 $w->afterIdle([$w, 'set_dir' => $val]);
80 }
81
82 sub set_dir {
83 my( $w, $val ) = @_;
84 my $fulldir = fullpath( $val );
85
86 my $parent = $sep;
87 if ($^O eq 'MSWin32')
88 {
89 if ($fulldir =~ s/^([a-z]:)//i)
90 {
91 $parent = $1;
92 }
93 }
94 $w->add_to_tree( $parent, $parent) unless $w->infoExists($parent);
95
96 my @dirs = ($parent);
97 foreach my $name (split( /\Q$sep\E/, $fulldir )) {
98 next unless length $name;
99 push @dirs, $name;
100 my $dir = File::Spec->catfile( @dirs );
101 $w->add_to_tree( $dir, $name, $parent )
102 unless $w->infoExists( $dir );
103 $parent = $dir;
104 }
105
106 $w->OpenCmd( $parent );
107 $w->setmode( $parent, 'close' );
108 }
109 *chdir = \&set_dir;
110
111
112 sub OpenCmd {
113 my( $w, $dir ) = @_;
114
115 my $parent = $dir;
116 foreach my $name ($w->dirnames( $parent )) {
117 next if ($name eq '.' || $name eq '..');
118 my $subdir = File::Spec->catfile( $dir, $name );
119 $subdir = _fs_encode($subdir);
120 next unless -d $subdir;
121 if( $w->infoExists( $subdir ) ) {
122 $w->show( -entry => $subdir );
123 } else {
124 $w->add_to_tree( $subdir, $name, $parent );
125 }
126 }
127 }
128
129 *opencmd = \&OpenCmd;
130
131 sub add_to_tree {
132 my( $w, $dir, $name, $parent ) = @_;
133
134 my $dir8 = _fs_encode($dir);
135 my $image = $w->cget('-image');
136 if ( !UNIVERSAL::isa($image, 'Tk::Image') ) {
137 $image = $w->Getimage( $image );
138 }
139 my $mode = 'none';
140 $mode = 'open' if $w->has_subdir( $dir );
141
142 my @args = (-image => $image, -text => $name);
143 if( $parent ) { # Add in alphabetical order.
144 foreach my $sib ($w->infoChildren( $parent )) {
145 use if $^O ne 'MSWin32', "locale"; # dumps core under Windows under some (japanese?) locales, see http://www.nntp.perl.org/group/perl.cpan.testers/2008/11/msg2550386.html
146 my $sib8 = _fs_encode($sib);
147 if ($sib8 gt $dir8) {
148 push @args, (-before => $sib);
149 last;
150 }
151 }
152 }
153
154 $w->add( $dir, @args );
155 $w->setmode( $dir, $mode );
156 }
157
158 sub has_subdir {
159 my( $w, $dir ) = @_;
160 foreach my $name ($w->dirnames( $dir )) {
161 next if ($name eq '.' || $name eq '..');
162 next if ($name =~ /^\.+$/);
163 return( 1 ) if -d File::Spec->catfile( $dir, $name );
164 }
165 return( 0 );
166 }
167
168 sub dirnames {
169 my( $w, $dir ) = @_;
170 my @names = $w->Callback( '-dircmd', $dir, $w->cget( '-showhidden' ) );
171 return( @names );
172 }
173
174 {
175 package Tk::DirTreeDialog;
176 use base qw(Tk::Toplevel);
177 Construct Tk::Widget 'DirTreeDialog';
178
179 sub Populate {
180 my($w, $args) = @_;
181 $w->{curr_dir} = delete $args->{-initialdir};
182 if (!defined $w->{curr_dir}) {
183 require Cwd;
184 $w->{curr_dir} = Cwd::getcwd();
185 }
186 if (defined $args->{-mustexist}) {
187 die "-mustexist is not yet implemented";
188 }
189 my $title = $args->{-title} || "Choose directory:";
190 delete $args->{-popover};
191
192 $w->title($title);
193 $w->{ok} = 0; # flag: "1" means OK, "-1" means cancelled
194
195 $w->transient($w->Parent->toplevel);
196
197 # Create Frame widget before the DirTree widget, so it's always visible
198 # if the window gets resized.
199 my $f = $w->Frame->pack(-fill => "x", -side => "bottom");
200
201 my $d;
202 $d = $w->Scrolled('DirTree',
203 -scrollbars => 'osoe',
204 -width => 35,
205 -height => 20,
206 -selectmode => 'browse',
207 -exportselection => 1,
208 -browsecmd => sub {
209 $w->{curr_dir} = shift;
210 },
211
212 # With this version of -command a double-click will
213 # select the directory
214 -command => sub { $w->{ok} = 1 },
215
216 # With this version of -command a double-click will
217 # open a directory. Selection is only possible with
218 # the Ok button.
219 #-command => sub { $d->opencmd($_[0]) },
220 )->pack(-fill => "both", -expand => 1);
221 # Set the initial directory
222 $d->set_dir($w->{curr_dir});
223
224 $f->Button(-text => 'Ok',
225 -command => sub { $w->{ok} = 1 })->pack(-side => 'left');
226 $f->Button(-text => 'Cancel',
227 -command => sub { $w->{ok} = -1 })->pack(-side => 'left');
228 $w->OnDestroy(sub { $w->{ok} = -1 });
229 }
230
231 sub Show {
232 my $w = shift;
233 my $old_focus = $w->focusSave;
234 my $old_grab = $w->grabSave;
235 Tk::catch {
236 $w->grab;
237 };
238 $w->waitVariable(\$w->{ok});
239 my $ret = $w->{ok} == 1 ? $w->{curr_dir} : undef;
240 $w->grabRelease if Tk::Exists($w);
241 &$old_focus;
242 &$old_grab;
243 $w->destroy if Tk::Exists($w);
244 $ret;
245 }
246 }
247
248 1;
249
250 __END__
251
252 # Copyright (c) 1996, Expert Interface Technologies
253 # See the file "license.terms" for information on usage and redistribution
254 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
255 #
256 # The file man.macros and some of the macros used by this file are
257 # copyrighted: (c) 1990 The Regents of the University of California.
258 # (c) 1994-1995 Sun Microsystems, Inc.
259 # The license terms of the Tcl/Tk distrobution are in the file
260 # license.tcl.
261