| 1 | package Tk::FileTree; | ||
| 2 | |||
| 3 | # FileTree -- TixFileTree widget | ||
| 4 | # | ||
| 5 | # Derived from Tk::DirTree | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use vars qw($VERSION); | ||
| 9 | $VERSION = '1.01'; | ||
| 10 | |||
| 11 | use Tk; | ||
| 12 | use Tk::Derived; | ||
| 13 | use Tk::Tree; | ||
| 14 | use Tk::ItemStyle; | ||
| 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 'FileTree'; | ||
| 23 | |||
| 24 | sub ClassInit | ||
| 25 | { | ||
| 26 | my ($class,$mw) = @_; | ||
| 27 | |||
| 28 | $class->SUPER::ClassInit($mw); | ||
| 29 | $mw->bind($class,'<FocusIn>','focus'); | ||
| 30 | $mw->bind($class,'<FocusOut>','unfocus'); | ||
| 31 | $mw->bind($class,'<Double-1>' => ['MouseButton','Double1',Ev('index',Ev('@'))]); | ||
| 32 | $mw->bind($class,'<B1-Motion>',['MouseButton','Button1Motion',Ev('index',Ev('@'))]); | ||
| 33 | $mw->bind($class,'<ButtonPress-1>',['FileTree_ButtonPress1', Ev('index',Ev('@'))]); | ||
| 34 | $mw->bind($class,'<ButtonRelease-1>',['FileTree_ButtonRelease1']); | ||
| 35 | $mw->bind($class,'<Shift-ButtonPress-1>',['MouseButton','ShiftButton1',Ev('index',Ev('@'))]); #JWT:ADDED 20091020! | ||
| 36 | $mw->bind($class,'<Control-ButtonPress-1>',['MouseButton','CtrlButton1',Ev('index',Ev('@'))]); | ||
| 37 | $mw->bind($class,'<Alt-ButtonPress-1>',['MouseButton','Button1', Ev('index',Ev('@'))]); | ||
| 38 | #THESE NEEDED TO GET TAB-FOCUS CYCLING RIGHT: | ||
| 39 | $mw->bind($class,'<Tab>', sub { my $w = shift; $w->focusNext; }); | ||
| 40 | $mw->bind($class,'<<LeftTab>>', sub { my $w = shift; $w->focusPrev; }); | ||
| 41 | $mw->MouseWheelBind($class); # XXX Both needed? M$-Windows seems to ignore (bummer!) | ||
| 42 | $mw->YMouseWheelBind($class); | ||
| 43 | |||
| 44 | return $class; | ||
| 45 | } | ||
| 46 | |||
| 47 | my $bummer = ($^O eq 'MSWin32') ? 1 : 0; | ||
| 48 | my $sep = $bummer ? '\\' : '/'; | ||
| 49 | |||
| 50 | *_fs_encode = eval { require Encode; 1 } ? sub { Encode::encode("iso-8859-1", $_[0]) } : sub { $_[0] }; | ||
| 51 | |||
| 52 | sub Populate { | ||
| 53 | my($cw, $args) = @_; | ||
| 54 | |||
| 55 | $cw->toplevel->bind('<<setPalette>>' => [$cw => 'fixPalette']); | ||
| 56 | $cw->{'-showcursoralways'} = delete($args->{'-showcursoralways'}) if (defined $args->{'-showcursoralways'}); | ||
| 57 | $cw->{Configure}{'-font'} = $args->{'-font'} #MUST ACTUALLY SET THE HList "DEFAULT" FONT FOR BOTH PLATFORMS!: | ||
| 58 | || ($bummer ? "{MS Sans Serif} 8" : "Helvetica -12 bold"); | ||
| 59 | my $disableFG = (defined $args->{'-disabledforeground'}) | ||
| 60 | ? delete($args->{'-disabledforeground'}) : ($Tk::DISABLED_FG || '#a3a3a3'); | ||
| 61 | |||
| 62 | $cw->SUPER::Populate($args); | ||
| 63 | |||
| 64 | $cw->ConfigSpecs( | ||
| 65 | -dircmd => [qw/CALLBACK dirCmd DirCmd DirCmd/], | ||
| 66 | -dirtree => [qw/CALLBACK dirTree DirTree 0/], | ||
| 67 | -showhidden => [qw/PASSIVE showHidden ShowHidden 0/], | ||
| 68 | -image => [qw/PASSIVE image Image folder/], | ||
| 69 | -fileimage => [qw/PASSIVE image Image file/], | ||
| 70 | -font => [qw/METHOD font Font/], | ||
| 71 | -root => [qw/PASSIVE root Directory 0/], | ||
| 72 | -include => [qw/PASSIVE include Include undef/], | ||
| 73 | -exclude => [qw/PASSIVE exclude Exclude undef/], | ||
| 74 | -directory => [qw/SETMETHOD directory Directory/, '.'], | ||
| 75 | -showcursoralways => [qw/PASSIVE showcursoralways showcursoralways 0/], | ||
| 76 | -state => ['METHOD', 'state', 'State', 'normal'], | ||
| 77 | -disabledforeground => ['PASSIVE', 'disabledForeground', 'DisabledForeground', $disableFG], | ||
| 78 | -background => [qw/METHOD background Background/, ''], | ||
| 79 | -foreground => [qw/METHOD foreground Foreground/, ''], | ||
| 80 | -selectforeground => [qw/METHOD selectForeground SelectForeground/, ''], | ||
| 81 | -activeforeground => [qw/METHOD activeForeground ActiveForeground/, '#000000'], | ||
| 82 | -value => '-directory' | ||
| 83 | ); | ||
| 84 | |||
| 85 | $cw->configure( -separator => $sep, -itemtype => 'imagetext'); | ||
| 86 | |||
| 87 | my $Palette = $cw->Palette; | ||
| 88 | foreach my $tp (qw/text image imagetext/) { #CREATE "DEFAULT" STYLES FOR EACH itemtype: | ||
| 89 | $cw->{"_style$tp"} = $cw->ItemStyle($tp); | ||
| 90 | #FORCE "activebackground" := "background" TO AVOID UGLY PARTIAL BACKGROUND SHADING OF THE ACTIVE ENTRY!: | ||
| 91 | $cw->{"_style$tp"}->configure('-activebackground' => $Palette->{'background'}) if ($Palette->{'background'}); | ||
| 92 | $cw->{"_style$tp"}->configure('-font' => $cw->{Configure}{'-font'}) if ($tp ne 'image'); | ||
| 93 | } | ||
| 94 | $cw->configure('-activeforeground' => $args->{'-activeforeground'}) if ($args->{'-activeforeground'}); | ||
| 95 | $cw->configure('-selectforeground' => $args->{'-selectforeground'}) if ($args->{'-selectforeground'}); | ||
| 96 | $cw->{'_lastactive'} = 0; | ||
| 97 | $cw->{'_hasfocus'} = 0; | ||
| 98 | #NOTE: HList's DEFAULT FOCUS-MODEL ACTS AS -takefocus => 1 (not '') BUT '',0, AND 1 WORK AS WE EXPECT! | ||
| 99 | $cw->{'_savefocuscfg'} = (defined $args->{'-takefocus'}) ? $args->{'-takefocus'} : ''; | ||
| 100 | $cw->{Configure}{'-state'} = 'normal'; | ||
| 101 | #THESE NEEDED TO GET TAB-FOCUS CYCLING RIGHT: | ||
| 102 | $cw->parent->bind('<Tab>', sub { my $w = shift; $w->focusNext; }); | ||
| 103 | $cw->parent->bind('<FocusIn>', sub { my $w = shift; $w->focusNext; }); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub DirCmd { | ||
| 107 | my( $w, $dir, $showhidden ) = @_; | ||
| 108 | $dir .= $sep if $dir =~ /^[a-z]:$/io and $bummer; | ||
| 109 | my $h = DirHandle->new( $dir ) or return(); | ||
| 110 | my @names = grep( $_ ne '.' && $_ ne '..', $h->read ); | ||
| 111 | @names = grep( ! /^[.]/o, @names ) unless $showhidden; | ||
| 112 | return( @names ); | ||
| 113 | } | ||
| 114 | |||
| 115 | *dircmd = \&DirCmd; | ||
| 116 | |||
| 117 | sub fullpath | ||
| 118 | { | ||
| 119 | my ($path) = @_; | ||
| 120 | my $cwd = getcwd(); | ||
| 121 | $path ||= $cwd; if (CORE::chdir($path)) { | ||
| 122 | $path = getcwd(); | ||
| 123 | CORE::chdir($cwd) || die "Cannot cd back to $cwd:$!"; | ||
| 124 | } else { | ||
| 125 | warn "Cannot cd to $path:$!" | ||
| 126 | } | ||
| 127 | $path = File::Spec->canonpath($path); | ||
| 128 | return $path; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub directory | ||
| 132 | { | ||
| 133 | my ($w,$key,$val) = @_; | ||
| 134 | # We need a value for -image, so its being undefined | ||
| 135 | # is probably caused by order of handling config defaults | ||
| 136 | # so defer it. | ||
| 137 | } | ||
| 138 | |||
| 139 | sub set_dir { | ||
| 140 | my($w, $val, %ops) = @_; | ||
| 141 | my $fulldir = fullpath($val); | ||
| 142 | |||
| 143 | if (defined($ops{'-root'}) && $ops{'-root'}) { | ||
| 144 | $ops{'-root'} = $fulldir if ($ops{'-root'} !~ m#\Q$sep\E#); | ||
| 145 | $w->configure('-root' => $ops{'-root'}); | ||
| 146 | } | ||
| 147 | my $parent = ($bummer && $fulldir =~ s/^([a-z]:)//i) ? $1 : $sep; | ||
| 148 | $w->add_to_tree( $parent, $parent) unless $w->infoExists($parent); | ||
| 149 | |||
| 150 | my @dirs = ($parent); | ||
| 151 | foreach my $name (split( /\Q$sep\E/, $fulldir )) { | ||
| 152 | next unless length $name; | ||
| 153 | push @dirs, $name; | ||
| 154 | my $dir = File::Spec->catfile( @dirs ); | ||
| 155 | $w->add_to_tree($dir, $name, $parent) | ||
| 156 | unless $w->infoExists( $dir ); | ||
| 157 | $parent = $dir; | ||
| 158 | } | ||
| 159 | |||
| 160 | $w->OpenCmd($parent); | ||
| 161 | $w->setmode($parent, 'close'); | ||
| 162 | } | ||
| 163 | |||
| 164 | *chdir = \&set_dir; | ||
| 165 | |||
| 166 | |||
| 167 | sub OpenCmd { | ||
| 168 | my($w, $dir) = @_; | ||
| 169 | |||
| 170 | my $parent = $dir; | ||
| 171 | my $include = $w->cget('-include'); | ||
| 172 | my $checkInclude = (ref($include) =~ /ARRAY/o && $#{$include} >= 0 && $include->[0] =~ /\S/) ? 1 : 0; | ||
| 173 | my $exclude = $w->cget('-exclude'); | ||
| 174 | my $checkExclude = (ref($exclude) =~ /ARRAY/o && $#{$exclude} >= 0 && $exclude->[0] =~ /\S/) ? 1 : 0; | ||
| 175 | my $dirTreeOnly = $w->cget('-dirtree'); | ||
| 176 | |||
| 177 | NAME: foreach my $name ($w->dirnames($parent)) { | ||
| 178 | next if ($name eq '.' || $name eq '..'); | ||
| 179 | my $subdir = _fs_encode(File::Spec->catfile($dir, $name)); | ||
| 180 | next unless (!$dirTreeOnly || -d $subdir); | ||
| 181 | if (! -d $subdir) { | ||
| 182 | if ($checkInclude) { | ||
| 183 | my $included = 0; | ||
| 184 | foreach my $i (@{$include}) { | ||
| 185 | $included = 1 if ($subdir =~ /\.$i$/i); | ||
| 186 | } | ||
| 187 | next NAME unless ($included); | ||
| 188 | } | ||
| 189 | if ($checkExclude) { | ||
| 190 | foreach my $i (@{$exclude}) { | ||
| 191 | next NAME if ($subdir =~ /\.$i$/i); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | if ($w->infoExists($subdir)) { | ||
| 196 | $w->show( -entry => $subdir, -noexpand => 1); | ||
| 197 | } else { | ||
| 198 | $w->add_to_tree($subdir, $name, $parent); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | *opencmd = \&OpenCmd; | ||
| 204 | |||
| 205 | sub add_to_tree { | ||
| 206 | my($w, $dir, $name, $parent) = @_; | ||
| 207 | |||
| 208 | my $dir8 = _fs_encode($dir); | ||
| 209 | my $image = (-d $dir) ? $w->cget('-image') : $w->cget('-fileimage'); | ||
| 210 | $image = $w->Getimage($image) if (!UNIVERSAL::isa($image, 'Tk::Image')); | ||
| 211 | |||
| 212 | my $rootdir = $w->cget('-root'); | ||
| 213 | $rootdir =~ s#\Q$sep\E$##; #MUST REMOVE TRAILING SEPERTOR! | ||
| 214 | my $mode = (-d $dir && (!$rootdir || $dir =~ /^\Q$rootdir\E/)) ? 'open' : 'none'; | ||
| 215 | |||
| 216 | my @args = (-image => $image, -text => $name, -style => $w->{"_styleimagetext"}); | ||
| 217 | if ($parent) { # Add in alphabetical order. | ||
| 218 | foreach my $sib ($w->infoChildren( $parent )) { | ||
| 219 | use if !$bummer, "locale"; # dumps core under Windows under some (japanese?) locales, see http://www.nntp.perl.org/group/perl.cpan.testers/2008/11/msg2550386.html | ||
| 220 | my $sib8 = _fs_encode($sib); | ||
| 221 | if ($sib8 gt $dir8) { | ||
| 222 | push @args, (-before => $sib); | ||
| 223 | last; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | #TK doesn't LIKE THIS: $dir =~ s#\Q$sep\E#\/#go; | ||
| 229 | $w->add($dir, @args); | ||
| 230 | $w->setmode($dir, $mode); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub has_subdir { | ||
| 234 | my($w, $dir) = @_; | ||
| 235 | foreach my $name ($w->dirnames($dir)) { | ||
| 236 | next if ($name eq '.' || $name eq '..'); | ||
| 237 | next if ($name =~ /^\.+$/o); | ||
| 238 | return (1) if -d File::Spec->catfile($dir, $name); | ||
| 239 | } | ||
| 240 | return (0); | ||
| 241 | } | ||
| 242 | |||
| 243 | sub dirnames { | ||
| 244 | my ($w, $dir) = @_; | ||
| 245 | my @names = $w->Callback( '-dircmd', $dir, $w->cget('-showhidden')); | ||
| 246 | return (@names); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub _normalize #(INTERNAL) Convert Windowsey paths to standard (*nix) ones (\ => /), retains driveletters! | ||
| 250 | { | ||
| 251 | my $entry = shift; | ||
| 252 | |||
| 253 | $entry =~ s#\Q$sep\E#\/#go if ($bummer && $entry =~ /\:/o); | ||
| 254 | return $entry; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub _borgify #(INTERNAL) Convert standard (*nix) paths to Windows (/path => C:\path, A:/path => A:\path) | ||
| 258 | { #ALSO REMOVE ANY TRAILING '/' OR '\\' (unless path == '/'. | ||
| 259 | my $entry = shift; | ||
| 260 | |||
| 261 | if ($bummer) { | ||
| 262 | $entry =~ s#^([a-z]\:)#\U$1\E\:#o; | ||
| 263 | $entry =~ s#^\/#C\:\/#io if ($entry !~ /\:/o); | ||
| 264 | $entry =~ s#\/#$sep#go unless ($entry =~ m#\\#o); | ||
| 265 | $entry =~ s#\Q${sep}\E$##o; | ||
| 266 | } else { | ||
| 267 | $entry =~ s#${sep}$##o unless ($entry eq $sep); | ||
| 268 | } | ||
| 269 | return $entry; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub curselection #convenience method for JFM5 file-manager: | ||
| 273 | { | ||
| 274 | return shift->selectionGet; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub index #convenience method for JFM5 file-manager (returns psudo-normalized [C:]/paths): | ||
| 278 | { | ||
| 279 | my ($w, $idx) = @_; | ||
| 280 | |||
| 281 | return &_normalize($w->{'_lastactive'}) if ($idx =~ /^active$/o); | ||
| 282 | return &_normalize($w->info('anchor')) if ($idx =~ /^(?:active|anchor)$/o); | ||
| 283 | return &_normalize($idx); | ||
| 284 | } | ||
| 285 | |||
| 286 | sub expand #OPEN ALL NECESSARY DIRECTORIES TO MAKE $entry VISABLE AND SELECTABLE: | ||
| 287 | { | ||
| 288 | my ($w, $entry) = @_; | ||
| 289 | |||
| 290 | return unless (-d $entry); | ||
| 291 | my $rootdir = $w->cget('-root'); | ||
| 292 | my $dir = ''; | ||
| 293 | foreach my $name (split( /\Q$sep\E/, $entry )) { | ||
| 294 | next unless (length $name); | ||
| 295 | $dir .= $sep unless ($bummer && $dir eq ''); | ||
| 296 | $dir .= $name; | ||
| 297 | $w->open($dir, -noexpand => 1) if ((!$rootdir || $dir =~ /^\Q$rootdir\E/) && -d $dir && $dir ne $entry); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | sub anchorSet | ||
| 302 | { | ||
| 303 | my ($w, $entry) = @_; | ||
| 304 | |||
| 305 | $entry = &_borgify($entry); | ||
| 306 | my $showcursor = $w->{'-showcursoralways'} || ((defined($_[2]) && $_[2] == 1) ? 1 : $w->{'-showcursoralways'}); | ||
| 307 | $w->Tk::Tree::anchorClear; | ||
| 308 | if ($w->{Configure}{'-state'} !~ /d/o | ||
| 309 | && ($showcursor || $w->{'_hasfocus'})) { | ||
| 310 | eval "\$w->Tk::Tree::anchorSet(\$entry)"; #FAIL SILENTLY IF NOT OPEN & CAN'T SEE! | ||
| 311 | if ($@) { | ||
| 312 | $w->expand($entry); | ||
| 313 | eval "\$w->Tk::Tree::anchorSet(\$entry)"; #FAIL SILENTLY IF NOT THERE OR CAN'T SEE! | ||
| 314 | } | ||
| 315 | } | ||
| 316 | $w->{'_lastactive'} = $entry; #THIS HIDES CURSOR. | ||
| 317 | } | ||
| 318 | |||
| 319 | sub activate #convenience method for JFM5 file-manager (psudonym for anchorSet()): | ||
| 320 | { | ||
| 321 | return anchorSet(@_); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub getRow #convenience method for JFM5 file-manager (returns psudo-normalized [C:]/paths): | ||
| 325 | { | ||
| 326 | my ($w, $val) = @_; | ||
| 327 | |||
| 328 | $val = &_borgify($val); | ||
| 329 | wantarray ? ({'-filename' => $val}, &_normalize($val), ((-d $val) ? 'd' : '-')) : &_normalize($val); | ||
| 330 | } | ||
| 331 | |||
| 332 | #THESE METHODS WRAP THEIR Tk-Tree VERSIONS TO SILENCE ERRORS AND EITHER FAIL GRACEFULLY OR | ||
| 333 | #EXPAND COLLAPSED ENTRIES IF NEEDED TO AVOID ERRORS FROM Tk::Tree (WHICH IS RATHER "NOISY"), | ||
| 334 | #AND ALSO HANDLE M$-Windows PATH FORMAT TRANSLATIONS: | ||
| 335 | |||
| 336 | sub selectionSet | ||
| 337 | { | ||
| 338 | my ($w, $entry) = @_; | ||
| 339 | |||
| 340 | $entry = &_borgify($entry); | ||
| 341 | eval "\$w->Tk::Tree::selectionSet(\$entry)"; | ||
| 342 | if ($@) { | ||
| 343 | $w->expand($entry); #FAILS IF COLLAPSED (NOT DISPLAYED), SO EXPAND AND TRY AGAIN: | ||
| 344 | eval "\$w->Tk::Tree::selectionSet(\$entry)"; #FAIL SILENTLY IF NOT THERE! | ||
| 345 | } | ||
| 346 | return $@ || 0; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub selectionClear | ||
| 350 | { | ||
| 351 | my $w = shift; | ||
| 352 | my $entry = shift; | ||
| 353 | |||
| 354 | #IF SOMEONE GIVES US 2 ARGS, IE. "(0, 'end')", JUST CLEAR ALL (WE CAN'T DO RANGES)! | ||
| 355 | return $w->Tk::Tree::selectionClear if (!defined($entry) || defined($_[0])); | ||
| 356 | return $w->Tk::Tree::selectionClear(&_borgify($entry)); | ||
| 357 | } | ||
| 358 | |||
| 359 | sub selectionToggle #convenience method for JFM5 file-manager: | ||
| 360 | { | ||
| 361 | my ($w, $indx) = @_; | ||
| 362 | my @selected = $w->selectionGet; | ||
| 363 | |||
| 364 | foreach my $s (@selected) { | ||
| 365 | if ($s eq $indx) { | ||
| 366 | $w->selectionClear($indx); | ||
| 367 | return; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | $w->selectionSet($indx); | ||
| 371 | } | ||
| 372 | |||
| 373 | sub see | ||
| 374 | { | ||
| 375 | my $w = shift; | ||
| 376 | my $entry = shift; | ||
| 377 | |||
| 378 | $entry = &_borgify($entry); | ||
| 379 | eval "\$w->Tk::Tree::see(\$entry)"; | ||
| 380 | if ($@) { | ||
| 381 | $w->expand($entry); #FAILS IF COLLAPSED (NOT DISPLAYED), SO EXPAND AND TRY AGAIN: | ||
| 382 | eval "\$w->Tk::Tree::see(\$entry)"; #FAIL SILENTLY IF NOT THERE! | ||
| 383 | } | ||
| 384 | return $@ || 0; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub hide | ||
| 388 | { | ||
| 389 | my $w = shift; | ||
| 390 | my @args = @_; | ||
| 391 | |||
| 392 | unshift @args, '-entry' unless ($#args >= 1 && $args[0] =~ /^\-/o); | ||
| 393 | $args[1] = &_borgify($w->index(&_borgify($args[1]))); | ||
| 394 | eval "\$w->Tk::HList::hide(\@args)"; | ||
| 395 | return $@ || 0; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub show | ||
| 399 | { | ||
| 400 | my $w = shift; | ||
| 401 | my @args = @_; | ||
| 402 | |||
| 403 | unshift @args, '-entry' unless ($#args >= 1 && $args[0] =~ /^\-/o); | ||
| 404 | $args[1] = &_borgify($w->index(&_borgify($args[1]))); | ||
| 405 | eval "\$w->Tk::HList::show(\$args[0], \$args[1])"; | ||
| 406 | if ($@) { | ||
| 407 | my %ops = @args; | ||
| 408 | unless ($ops{'-noexpand'}) { | ||
| 409 | $w->expand($args[1]); #FAILS IF COLLAPSED (NOT DISPLAYED), SO EXPAND AND TRY AGAIN: | ||
| 410 | eval "\$w->Tk::HList::show(\$args[0], \$args[1])"; #FAIL SILENTLY IF NOT THERE! | ||
| 411 | } | ||
| 412 | } | ||
| 413 | return $@ || 0; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub selectionIncludes | ||
| 417 | { | ||
| 418 | my $w = shift; | ||
| 419 | my $entry = shift; | ||
| 420 | |||
| 421 | my $res = 0; | ||
| 422 | $entry = &_borgify($entry); | ||
| 423 | |||
| 424 | eval "\$res = \$w->Tk::Tree::selectionIncludes(\$entry)"; #FAIL SILENTLY IF NOT THERE OR CAN'T SEE! | ||
| 425 | if ($@) { | ||
| 426 | $w->expand($entry); #FAILS IF COLLAPSED (NOT DISPLAYED), SO EXPAND AND TRY AGAIN: | ||
| 427 | eval "\$res = \$w->Tk::Tree::selectionIncludes(\$entry)"; #FAIL SILENTLY IF NOT THERE! | ||
| 428 | } | ||
| 429 | return $@ ? 0 : $res; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub close | ||
| 433 | { | ||
| 434 | my $w = shift; | ||
| 435 | my $entry = shift; | ||
| 436 | |||
| 437 | $entry = &_borgify($entry); | ||
| 438 | eval "\$w->Tk::Tree::close(\$entry)"; #FAIL SILENTLY IF NOT THERE OR CAN'T SEE! | ||
| 439 | return $@ || 0; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub open | ||
| 443 | { | ||
| 444 | my $w = shift; | ||
| 445 | my $entry = shift; | ||
| 446 | my %ops = @_; | ||
| 447 | |||
| 448 | $entry = &_borgify($entry); | ||
| 449 | return "e:open:$entry Not a directory!" unless (-d $entry); | ||
| 450 | |||
| 451 | eval "\$w->Tk::Tree::open(\$entry)"; #FAIL SILENTLY IF NOT THERE OR CAN'T SEE! | ||
| 452 | if ($@ && !$ops{'-noexpand'}) { #FAILS IF COLLAPSED (NOT DISPLAYED), SO EXPAND AND TRY AGAIN: | ||
| 453 | $w->expand($entry); | ||
| 454 | eval "\$w->Tk::Tree::open(\$entry)"; #FAIL SILENTLY IF NOT THERE! | ||
| 455 | } | ||
| 456 | return $@ || 0; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub FileTree_ButtonPress1 | ||
| 460 | { | ||
| 461 | my $w = shift; | ||
| 462 | return if ($w->{Configure}{'-state'} =~ /d/o); | ||
| 463 | |||
| 464 | my $Ev = $w->XEvent; | ||
| 465 | my $mode = $w->cget('-selectmode'); | ||
| 466 | $w->Tk::HList::Button1($Ev); | ||
| 467 | my $force = ($mode !~ /^multiple/o && ! $w->{'_hasfocus'}) ? 1 : undef; | ||
| 468 | #FORCE ANCHOR-SET ON UNFOCUSED WIDGET SO DRAG-SELECT WILL WORK (HList USES ANCHOR FOR ACTIVE CURSOR)!: | ||
| 469 | my $ent = $w->Tk::HList::GetNearest($Ev->y, 1); | ||
| 470 | $w->anchorSet($ent, $force) if (defined($ent) && length($ent)); | ||
| 471 | } | ||
| 472 | |||
| 473 | sub FileTree_ButtonRelease1 | ||
| 474 | { | ||
| 475 | my $w = shift; | ||
| 476 | return if ($w->{Configure}{'-state'} =~ /d/o); | ||
| 477 | |||
| 478 | my $Ev = $w->XEvent; | ||
| 479 | my $mode = $w->cget('-selectmode'); | ||
| 480 | $w->Tk::HList::ButtonRelease_1($Ev); | ||
| 481 | if ($mode !~ /^multiple/o && ! $w->{'_hasfocus'}) { | ||
| 482 | #CLEAR FORCED VISIBLE ANCHOR SET BY FileTree_ButtonPress1 ON UNFOCUSED WIDGET!: | ||
| 483 | my $ent = $w->Tk::HList::GetNearest($Ev->y, 1); | ||
| 484 | $w->anchorSet($ent) if (defined($ent) && length($ent)); | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | #THIS ALLOWS US TO INTERVENE BEFORE EACH MOUSE-BUTTON FUNCTION IS REDIRECTED TO IT'S | ||
| 489 | #CORRESPONDING HList EQUIVALENT SO WE CAN BLOCK ACTIONS WHEN WIDGET IS DISABLED: | ||
| 490 | sub MouseButton | ||
| 491 | { | ||
| 492 | my $w = shift; | ||
| 493 | return if ($w->{Configure}{'-state'} =~ /d/o); | ||
| 494 | |||
| 495 | my $button = shift; | ||
| 496 | my $Ev = $w->XEvent; | ||
| 497 | |||
| 498 | eval "\$w->Tk::HList::$button(\$Ev)"; | ||
| 499 | } | ||
| 500 | |||
| 501 | sub state { | ||
| 502 | my ($w, $val) = @_; | ||
| 503 | |||
| 504 | return $w->{Configure}{'-state'} || undef unless (defined($val) && $val); | ||
| 505 | |||
| 506 | #THE STUPID HList WIDGET IS BROKEN: WON'T TAKE STATE CHANGE?! $w->Tk::HList::configure('-state' => $val); | ||
| 507 | #SO WE HAVE TO "EMULATE" IT OURSELVES MANUALLY - GRRRRRRRR!: | ||
| 508 | return if (defined($w->{'_prevstate'}) && $val eq $w->{'_prevstate'}); #DON'T DO TWICE IN A ROW! | ||
| 509 | |||
| 510 | $w->{'_statechg'} = 1; | ||
| 511 | if ($val =~ /d/o) { #WE'RE DISABLING (SAVE CURRENT ENABLED STATUS STUFF, THEN DISABLE USER-INTERACTION): | ||
| 512 | my $Palette = $w->Palette; | ||
| 513 | $w->{'_lastactive'} = $w->index('active') || 0 if ($w->{'_hasfocus'}); | ||
| 514 | @{$w->{'_savesel'}} = $w->curselection; #SAVE & CLEAR THE CURRENT SELECTION, FOCUS STATUS & COLORS: | ||
| 515 | $w->{'_savefocuscfg'} = $w->cget('-takefocus'); | ||
| 516 | $w->selectionClear; | ||
| 517 | $w->anchorClear; | ||
| 518 | $w->{'_foreground'} = $w->foreground; | ||
| 519 | $w->{Configure}{'-state'} = $val; | ||
| 520 | $w->foreground($w->cget('-disabledforeground') || $Palette->{'disabledForeground'}); | ||
| 521 | $w->focusNext if ($w->{'_hasfocus'}); #MOVE FOCUS OFF WIDGET IF IT HAS IT. | ||
| 522 | $w->Tk::HList::configure(-takefocus => 0); | ||
| 523 | $w->configure(-takefocus => 0); | ||
| 524 | } elsif ($w->{'_prevstate'}) { #ENABLING (BUT DON'T DO ALL THIS WHEN ENABLING *INITIALLY*)!: | ||
| 525 | my $fg = $w->{'_foreground'}; | ||
| 526 | $fg ||= $w->toplevel->cget('-foreground') || $w->toplevel->Palette->{'foreground'}; | ||
| 527 | $fg ||= $Tk::NORMAL_BG || 'black'; | ||
| 528 | $w->{Configure}{'-state'} = $val; | ||
| 529 | $w->Tk::HList::configure('-foreground' => $fg, -takefocus => $w->{'_savefocuscfg'}); | ||
| 530 | $w->configure(-takefocus => $w->{'_savefocuscfg'}); | ||
| 531 | if (ref $w->{'_savesel'}) { #RESTORE SELECTED LIST. | ||
| 532 | $w->selectionSet(shift @{$w->{'_savesel'}}) while (@{$w->{'_savesel'}}); | ||
| 533 | } | ||
| 534 | $w->activate($w->{'_lastactive'}) if ($w->{'_lastactive'}); | ||
| 535 | my $colr = 'foreground'; | ||
| 536 | (my $colr_ = $colr) =~ tr/A-Z/a-z/; | ||
| 537 | $fg = $w->{'_foreground'}; | ||
| 538 | $fg ||= $w->toplevel->cget('-foreground') || $w->toplevel->Palette->{'foreground'}; | ||
| 539 | $w->configure('-foreground' => $fg) if ($fg); #RESTORE FG COLOR. | ||
| 540 | } else { #INSTEAD, JUST DO THIS WHEN INITIALLY SET ENABLED!: | ||
| 541 | #NOTE: HList's DEFAULT FOCUS-MODEL ACTS AS -takefocus => 1 (not '') BUT '',0, AND 1 WORK AS WE EXPECT, | ||
| 542 | #SO WE FIX THAT HERE ('' IF NOT SPECIFIED IN INITIAL FLAGS)!: | ||
| 543 | $w->Tk::HList::configure(-takefocus => $w->{'_savefocuscfg'}); | ||
| 544 | } | ||
| 545 | $w->{'_prevstate'} = $w->{Configure}{'-state'}; | ||
| 546 | $w->{'_statechg'} = 0; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub focus | ||
| 550 | { | ||
| 551 | my $w = shift; | ||
| 552 | if ($w->{Configure}{'-state'} =~ /d/o) { | ||
| 553 | #HANDLE EDGE CASE DUE TO WRONG INITIALIZATION ORDER (-state=disabled, -takefocus=1)!: | ||
| 554 | #(WHEN INITIALIZING W/BOTH THESE FLAGS, ->state('disabled') GETS CALLED FIRST SETTING FOCUS:=0, | ||
| 555 | #BUT THEN FOCUS GETS SET TO 1(FLAG) CAUSING LEFT-TAB STRANDING)! | ||
| 556 | $w->configure(-takefocus => 0) if ($w->cget('-takefocus')); | ||
| 557 | $w->focusNext; | ||
| 558 | return; | ||
| 559 | } | ||
| 560 | $w->Tk::focus; | ||
| 561 | $w->{'_hasfocus'} = 1; | ||
| 562 | $w->parent->bind('<<LeftTab>>', sub { #THESE NEEDED TO GET FOCUS-CYCLING RIGHT: | ||
| 563 | $w->focusCurrent->parent->focusPrev; | ||
| 564 | Tk->break; | ||
| 565 | }); | ||
| 566 | return if ($w->{'-showcursoralways'}); | ||
| 567 | |||
| 568 | #RESTORE CURSOR WHEN FOCUS IS GAINED (FAIL SILENTLY IF NO LONGER VISIBLE!): | ||
| 569 | $w->activate($w->{'_lastactive'}) if ($w->{'_lastactive'}); | ||
| 570 | } | ||
| 571 | |||
| 572 | sub unfocus | ||
| 573 | { | ||
| 574 | my $w = shift; | ||
| 575 | if ($w->{'-showcursoralways'}) { | ||
| 576 | $w->{'_hasfocus'} = 0; | ||
| 577 | return; | ||
| 578 | } | ||
| 579 | |||
| 580 | $w->{'_lastactive'} = $w->index('active') unless ($w->{Configure}{'-state'} =~ /d/o); | ||
| 581 | $w->{'_hasfocus'} = 0; | ||
| 582 | $w->anchorClear unless ($w->{Configure}{'-state'} =~ /d/o); | ||
| 583 | } | ||
| 584 | |||
| 585 | sub fixPalette { #WITH OUR setPalette, WE CAN CATCH PALETTE CHANGES AND ADJUST EVERYTHING ACCORDINGLY!: | ||
| 586 | my $w = shift; #WE STILL PROVIDE THIS AS A USER-CALLABLE METHOD FOR THOSE WHO ARE NOT. | ||
| 587 | my $Palette = $w->Palette; | ||
| 588 | |||
| 589 | $w->background($Palette->{'background'}); | ||
| 590 | $w->foreground($Palette->{'foreground'}); | ||
| 591 | } | ||
| 592 | |||
| 593 | #WE NEED CONTROL OVER COLOR OR PALETTE CHANGES TO THESE COLOR ATTRIBUTES: | ||
| 594 | sub background { | ||
| 595 | my $w = shift; | ||
| 596 | my $val = shift; | ||
| 597 | return $w->{Configure}{'-background'} unless (defined($val) && $val); | ||
| 598 | |||
| 599 | my $dfltBG = $w->toplevel->cget('-background'); | ||
| 600 | #ALLOW BACKGROUND CHANGE IF FORCE OR VALUE APPEARS TO BE USER-SET: | ||
| 601 | $w->{'_backgroundset'} = 0 if ($val !~ /^${dfltBG}$/i); | ||
| 602 | ## Ensure that the base Frame, pane and columns (if any) get set | ||
| 603 | unless ($w->{'_backgroundset'}) { | ||
| 604 | Tk::configure($w, "-background", $val); | ||
| 605 | $w->{Configure}{'-background'} = $val; | ||
| 606 | foreach my $tp (qw/text image imagetext/) { | ||
| 607 | $w->{"_style$tp"}->configure('-background' => $val, '-activebackground' => $val); | ||
| 608 | } | ||
| 609 | $w->{'_backgroundset'} = 1 unless ($w->{'_statechg'} || $val =~ /^${dfltBG}$/i); | ||
| 610 | } | ||
| 611 | } | ||
| 612 | |||
| 613 | #PBM. IS THAT IF USER SETS A CUSTOM COLOR, WE WANT TO KEEP THAT THRU BOTH STATE AND PALETTE CHANGES, | ||
| 614 | #OTHERWISE, WE WANT TO PROPERLY CHANGE WHEN EITHER STATE OR PALETTE CHANGES, EVEN IF PALETTE CHANGES | ||
| 615 | #WHILE THE STATE IS DISABLED!: | ||
| 616 | sub foreground { #THIS CODE IS UGLY AS CRAP, BUT NECESSARY TO ACTUALLY WORK: | ||
| 617 | my $w = shift; | ||
| 618 | my $val = shift; | ||
| 619 | return $w->{Configure}{'-foreground'} unless (defined($val) && $val); | ||
| 620 | |||
| 621 | my $dfltFG = $w->toplevel->cget('-foreground'); | ||
| 622 | #ALLOW FOREGROUND CHANGE IF FORCE OR VALUE APPEARS TO BE USER-SET: | ||
| 623 | my $palettechg = ($val =~ /^${dfltFG}$/i) ? 1 : 0; | ||
| 624 | my $disabled = ($w->{Configure}{'-state'} =~ /d/o) ? 1 : 0; | ||
| 625 | my $allowed = 0; | ||
| 626 | if ($w->{'_statechg'}) { #ALWAYS ALLOW FORCE OR STATE-CHANGE TO SET FG: | ||
| 627 | $allowed = 1; #IF FORCE OR WE'RE CHANGING "STATE" (ie. "normal" to "disabled"): | ||
| 628 | } elsif ($w->{'_foregroundset'}) { #USER SPECIFIED A SPECIFIC FG COLOR. | ||
| 629 | $allowed = 1 unless ($palettechg); #PREVIOUSLY USER-SET, ALLOW USER BUT NOT setPalette TO CHANGE: | ||
| 630 | } elsif ($disabled) { | ||
| 631 | $allowed = 1 #NOT PREVIOUSLY USER-SET, ALLOW setPalette OR DISABLED STATE TO SET FG: | ||
| 632 | } elsif (!$w->{'_foregroundset'}) { #ALLOW CHANGE SINCE USER HASN'T SPECIFIED A COLOR: | ||
| 633 | $allowed = 1 | ||
| 634 | } | ||
| 635 | ## Ensure that the base Frame, pane and columns (if any) get set | ||
| 636 | if ($allowed) { #FG ALLOWED TO BE CHANGED: | ||
| 637 | if ($disabled) { #FORCE TO DISABLED FG, IF DISABLED: | ||
| 638 | my $Palette = $w->Palette; | ||
| 639 | $w->{'_foreground'} = $val | ||
| 640 | if ($w->{'_statechg'} eq '0'); #STATE CHANGED TO DIABLED BEFORE INITIALIZATION, SO SAVE SAVE USER-SPECIFIED ("normal") COLOR: | ||
| 641 | $w->{'_foreground'} = $w->{'_foregroundset'} || $Palette->{'foreground'} #NEED TO GET IT HERE, SINCE STARTED DISABLED & WE DIDN'T HAVE IT YET! | ||
| 642 | unless ($w->{'_foreground'}); #NO "normal" COLOR SAVED YET, SAVE USER'S FG IF SPECIFIED, OR THE PALETTE'S COLOR: | ||
| 643 | $val = $w->cget('-disabledforeground') || $Palette->{'disabledForeground'}; #NOW SWITCH TO THE PALETTE'S "DISABLED" FG COLOR. | ||
| 644 | } | ||
| 645 | Tk::configure($w, "-foreground", $val); | ||
| 646 | $w->{Configure}{'-foreground'} = $val; | ||
| 647 | foreach my $tp (qw/text image imagetext/) { | ||
| 648 | $w->{"_style$tp"}->configure('-foreground' => $val); | ||
| 649 | } | ||
| 650 | |||
| 651 | #FREEZE (SAVE) THE FG (IF ENABLED AND SET BY USER, NOT PALETTE & NOT CHANGING "STATE"): | ||
| 652 | if (!$disabled && !$palettechg && !$w->{'_statechg'}) { | ||
| 653 | $w->{'_foregroundset'} = $val if (!$disabled && !$palettechg && !$w->{'_statechg'}); | ||
| 654 | } else { | ||
| 655 | #WE MUST CONSIDER UPDATING ACTIVE AND SELECT FOREGROUND IF CHANGING STATES OR PALETTES: | ||
| 656 | $w->{'_propogateFG'} = 1; | ||
| 657 | $w->activeforeground($val); | ||
| 658 | $w->selectforeground($val); | ||
| 659 | $w->{'_propogateFG'} = 0; | ||
| 660 | } | ||
| 661 | } else { #NOT ALLOWED TO BE CHANGED: MUST RESET TO "USER-SET" FG, SINCE TK'S ALREADY "CHANGED" IT INTERNALLY: | ||
| 662 | if ($w->{'_foregroundset'}) { | ||
| 663 | Tk::configure($w, "-foreground", $w->{'_foregroundset'}); | ||
| 664 | $w->{Configure}{'-foreground'} = $w->{'_foregroundset'}; | ||
| 665 | } elsif ($palettechg && $disabled) { #IF WE'RE NOT "USER-SET" AND WE'RE DISABLED, SAVE NEW PALLETTE-SET FG FOR RESTORATION WHEN ENABLED: | ||
| 666 | $w->{'_foreground'} = $val; | ||
| 667 | } | ||
| 668 | } | ||
| 669 | } | ||
| 670 | |||
| 671 | sub activeforeground { | ||
| 672 | my ($w, $val) = @_; | ||
| 673 | return $w->{Configure}{'-activeforeground'} unless (defined($val) && $val); | ||
| 674 | |||
| 675 | my $dfltFG = $w->toplevel->cget('-foreground'); | ||
| 676 | my $palettechg = ($val =~ /^${dfltFG}$/i) ? 1 : 0; | ||
| 677 | my $disabled = ($w->{Configure}{'-state'} =~ /d/o) ? 1 : 0; | ||
| 678 | my $allowed = 0; | ||
| 679 | if ($w->{'_activeforegroundset'}) { | ||
| 680 | $allowed = 1 unless ($w->{'_propogateFG'} || $palettechg); #USER-SET: ALLOW USER, BUT NOT setPalette TO CHANGE: | ||
| 681 | } else { | ||
| 682 | $allowed = 1; #NOT PREVIOUSLY USER-SET, SO ALLOW setPalette IF ENABLED: | ||
| 683 | } | ||
| 684 | if ($allowed) { | ||
| 685 | $w->{Configure}{'-activeforeground'} = $val; | ||
| 686 | foreach my $tp (qw/text image imagetext/) { | ||
| 687 | $w->{"_style$tp"}->configure('-activeforeground' => $val); | ||
| 688 | } | ||
| 689 | $w->{'_activeforegroundset'} = $val unless ($disabled || $palettechg | ||
| 690 | || $w->{'_statechg'} || $w->{'_propogateFG'}); | ||
| 691 | } else { #NOT ALLOWED TO BE CHANGED: MUST RESET TO "USER-SET" FG, SINCE TK'S ALREADY "CHANGED" IT INTERNALLY: | ||
| 692 | if ($w->{'_activeforegroundset'}) { | ||
| 693 | eval { Tk::configure($w, "-activeforeground", $w->{'_activeforegroundset'}); }; | ||
| 694 | $w->{Configure}{'-activeforeground'} = $w->{'_activeforegroundset'}; | ||
| 695 | } | ||
| 696 | } | ||
| 697 | } | ||
| 698 | |||
| 699 | sub selectforeground { | ||
| 700 | my ($w, $val) = @_; | ||
| 701 | return $w->{Configure}{'-selectforeground'} unless (defined($val) && $val); | ||
| 702 | |||
| 703 | my $dfltFG = $w->toplevel->cget('-foreground'); | ||
| 704 | my $palettechg = ($val =~ /^${dfltFG}$/i) ? 1 : 0; | ||
| 705 | my $disabled = ($w->{Configure}{'-state'} =~ /d/o) ? 1 : 0; | ||
| 706 | my $allowed = 0; | ||
| 707 | if ($w->{'_selectforegroundset'}) { | ||
| 708 | $allowed = 1 unless ($w->{'_propogateFG'} || $palettechg); #FROZEN, ALLOW USER, BUT NOT setPalette TO CHANGE: | ||
| 709 | } else { | ||
| 710 | $allowed = 1; #NOT FROZEN, ALLOW setPalette IF ENABLED: | ||
| 711 | } | ||
| 712 | if ($allowed) { | ||
| 713 | Tk::configure($w, "-selectforeground", $val); | ||
| 714 | $w->{Configure}{'-selectforeground'} = $val; | ||
| 715 | foreach my $tp (qw/text image imagetext/) { | ||
| 716 | $w->{"_style$tp"}->configure('-selectforeground' => $val); | ||
| 717 | } | ||
| 718 | $w->{'_selectforegroundset'} = $val unless ($disabled || $palettechg | ||
| 719 | || $w->{'_statechg'} || $w->{'_propogateFG'}); | ||
| 720 | } else { #NOT ALLOWED TO BE CHANGED: MUST RESET TO "FROZEN" FG, SINCE TK'S ALREADY "CHANGED" IT INTERNALLY: | ||
| 721 | if ($w->{'_selectforegroundset'}) { | ||
| 722 | Tk::configure($w, "-selectforeground", $w->{'_selectforegroundset'}); | ||
| 723 | $w->{Configure}{'-selectforeground'} = $w->{'_selectforegroundset'}; | ||
| 724 | } | ||
| 725 | } | ||
| 726 | } | ||
| 727 | |||
| 728 | sub font { #SINCE WE CREATE "DEFAULT" STYLES FOR FG/BG PURPOSES, WE MUST SET THE FONT THERE TOO!: | ||
| 729 | my ($w, $val) = @_; | ||
| 730 | |||
| 731 | return $w->{Configure}{'-font'} || undef unless (defined($val) && $val); | ||
| 732 | |||
| 733 | $w->{Configure}{'-font'} = $val; | ||
| 734 | foreach my $tp (qw/text imagetext/) { | ||
| 735 | $w->{"_style$tp"}->configure('-font' => $val); | ||
| 736 | } | ||
| 737 | } | ||
| 738 | |||
| 739 | 1 | ||
| 740 | |||
| 741 | __END__ |