| 1 | package Tk::DocumentTree; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::DocumentTree - Tk::ListBrowser based document list | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw($VERSION); | ||
| 11 | $VERSION = '0.11'; | ||
| 12 | |||
| 13 | use base qw(Tk::Derived Tk::Frame); | ||
| 14 | |||
| 15 | require Tk::ListBrowser; | ||
| 16 | use File::Basename; | ||
| 17 | use Tk; | ||
| 18 | use Config; | ||
| 19 | |||
| 20 | Construct Tk::Widget 'DocumentTree'; | ||
| 21 | |||
| 22 | my $save_pixmap = ' | ||
| 23 | /* XPM */ | ||
| 24 | static char *save[]={ | ||
| 25 | "16 16 4 1", | ||
| 26 | ". c None", | ||
| 27 | "# c #000000", | ||
| 28 | "a c #808080", | ||
| 29 | "b c #ffff00", | ||
| 30 | "................", | ||
| 31 | "..############..", | ||
| 32 | ".#aaaaaaaaaaaa#.", | ||
| 33 | ".#aaaaaaaaaaaa#.", | ||
| 34 | ".#aaaaaaaaaaaa#.", | ||
| 35 | ".#aaaaaaaaaaaa#.", | ||
| 36 | ".#aaaaaaaaaaaa#.", | ||
| 37 | ".#aaaaaaaaaaaa#.", | ||
| 38 | ".#aa########aa#.", | ||
| 39 | ".#aa########aa#.", | ||
| 40 | ".#aa########aa#.", | ||
| 41 | ".#aa########aa#.", | ||
| 42 | ".#aa#bbbbbb#aa#.", | ||
| 43 | ".#aa#bbbbbb#aa#.", | ||
| 44 | "..############..", | ||
| 45 | "................"}; | ||
| 46 | '; | ||
| 47 | |||
| 48 | =head1 SYNOPSIS | ||
| 49 | |||
| 50 | require Tk::DocumentTree; | ||
| 51 | my $tree= $window->DocumentTree(@options)->pack; | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | B<Tk::DocumentTree> is a Tree like megawidget. It consists of a Label and a ListBrowser widget. | ||
| 56 | |||
| 57 | The Label on top displays the path all added entries have in commom. | ||
| 58 | It automatically creates a folder tree as entries are added. | ||
| 59 | |||
| 60 | Entries can have the status I<file> or I<untracked> | ||
| 61 | An entry is untracked when it does not exist as a file. | ||
| 62 | |||
| 63 | =head1 CONFIG VARIABLES | ||
| 64 | |||
| 65 | =over 4 | ||
| 66 | |||
| 67 | =item Switch: B<-contextmenu> | ||
| 68 | |||
| 69 | Specify menuitems for a menu that pops on Button-3. | ||
| 70 | By default it has entries for collapseAll and expandAll, | ||
| 71 | |||
| 72 | =item Switch: B<-diriconcall> | ||
| 73 | |||
| 74 | Callback for obtaining the dir icon. By default it is set | ||
| 75 | to a call that returns the default folder.xpm in the Perl/Tk | ||
| 76 | distribution. | ||
| 77 | |||
| 78 | =item Switch: B<-entryselect> | ||
| 79 | |||
| 80 | Callback to execute when the user clicks (selects) and entry. | ||
| 81 | |||
| 82 | =item Switch: B<-fileiconcall> | ||
| 83 | |||
| 84 | Callback for obtaining the file icon. By default it is set | ||
| 85 | to a call that returns the default file.xpm in the Perl/Tk | ||
| 86 | distribution. | ||
| 87 | |||
| 88 | =item Switch: B<-saveiconcall> | ||
| 89 | |||
| 90 | Callback for obtaining the save icon. By default it is set | ||
| 91 | to a call that returns save icon embedded in this package. | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =head1 METHODS | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub Populate { | ||
| 102 | my ($self,$args) = @_; | ||
| 103 | $self->SUPER::Populate($args); | ||
| 104 | |||
| 105 | my $sep = '/'; | ||
| 106 | $sep = '\\' if $Config{osname} eq 'MSWin32'; | ||
| 107 | |||
| 108 | $self->{HAVECELLSIZE} = 0; | ||
| 109 | my $topbar = $self->CreatePathBar; | ||
| 110 | $self->Advertise(PATH => $topbar); | ||
| 111 | my $tree = $self->ListBrowser( | ||
| 112 | -arrange => 'tree', | ||
| 113 | -autorefresh => 0, | ||
| 114 | -browsecmd => ['EntryClick', $self], | ||
| 115 | -filterforce => 1, | ||
| 116 | -itemtype => 'imagetext', | ||
| 117 | -selectmode => 'exclusive', | ||
| 118 | -separator => $sep, | ||
| 119 | -textanchor => 'w', | ||
| 120 | -textside => 'right', | ||
| 121 | -width => 250, | ||
| 122 | )->pack( | ||
| 123 | -padx => 2, | ||
| 124 | -pady => 2, | ||
| 125 | -expand => 1, | ||
| 126 | -fill => 'both', | ||
| 127 | ); | ||
| 128 | $self->Advertise(Tree => $tree); | ||
| 129 | my $c = $tree->Subwidget('Canvas'); | ||
| 130 | $c->Tk::bind('<ButtonRelease-3>', sub { $self->lmPost(Ev('X'), Ev('Y')) }); | ||
| 131 | |||
| 132 | my (@contextmenu) = ( | ||
| 133 | ['command' => 'Collapse All', -command => ['collapseAll', $self]], | ||
| 134 | ['command' => 'Expand All', -command => ['expandAll', $self]], | ||
| 135 | ); | ||
| 136 | $self->ConfigSpecs( | ||
| 137 | -background => ['SELF', 'DESCENDANTS'], | ||
| 138 | -contextmenu => ['PASSIVE', undef, undef, \@contextmenu], | ||
| 139 | -diriconcall => ['CALLBACK', undef, undef, ['DefaultDirIcon', $self]], | ||
| 140 | -entryselect => ['CALLBACK', undef, undef, sub {}], | ||
| 141 | -fileiconcall => ['CALLBACK', undef, undef, ['DefaultFileIcon', $self]], | ||
| 142 | -refreshdelay => ['PASSIVE', undef, undef, 300], | ||
| 143 | -saveiconcall => ['CALLBACK', undef, undef, ['DefaultSaveIcon', $self]], | ||
| 144 | DEFAULT => [$tree], | ||
| 145 | ); | ||
| 146 | $self->Delegates( | ||
| 147 | 'DEFAULT' => $tree, | ||
| 148 | ); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub Add { | ||
| 152 | my ($self, $new, $type) = @_; | ||
| 153 | if ($type eq 'untracked') { | ||
| 154 | my @peers = $self->infoRoot; | ||
| 155 | my @op = (-image => $self->GetFileIcon($new)); | ||
| 156 | for (@peers) { | ||
| 157 | if ($self->isDir($_)) { | ||
| 158 | push @op, -before => $_; | ||
| 159 | last; | ||
| 160 | } elsif ($self->isFile($_)) { | ||
| 161 | push @op, -before => $_; | ||
| 162 | last; | ||
| 163 | } elsif ($new lt $_) { | ||
| 164 | push @op, -before => $_; | ||
| 165 | last; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | my $n = $self->add($new, @op, | ||
| 169 | -text => $new, | ||
| 170 | -data => $type, | ||
| 171 | ); | ||
| 172 | } else { | ||
| 173 | my $sep = $self->cget('-separator'); | ||
| 174 | my $nsep = quotemeta($sep); | ||
| 175 | |||
| 176 | my $name = ''; | ||
| 177 | my @path = ($new); | ||
| 178 | @path = split /$nsep/, $new if $new =~ /$nsep/; | ||
| 179 | |||
| 180 | while (@path) { | ||
| 181 | my $title = shift @path; | ||
| 182 | my $data = 'file'; | ||
| 183 | $data = 'dir' if @path; | ||
| 184 | if ($name eq '') { | ||
| 185 | $name = $title; | ||
| 186 | } else { | ||
| 187 | $name = $name . $sep . $title; | ||
| 188 | } | ||
| 189 | unless ($self->infoExists($name)) { | ||
| 190 | my @op = ( | ||
| 191 | -data => $data, | ||
| 192 | ); | ||
| 193 | my @peers = $self->GetPeers($name); | ||
| 194 | |||
| 195 | #We want a sorted list, directories first | ||
| 196 | if ($data eq 'dir') { | ||
| 197 | for (@peers) { | ||
| 198 | my $peer = $_; | ||
| 199 | if ($self->isUntracked($peer)) { #ignore untracked entries | ||
| 200 | } elsif ($self->isFile($peer)) { #we arrived at the end of the directory section | ||
| 201 | push @op, -before => $peer; | ||
| 202 | last; | ||
| 203 | } elsif ($name lt $peer) { | ||
| 204 | push @op, -before => $peer; | ||
| 205 | last; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | push @op, -image => $self->GetDirIcon($self->GetFileName($name)); | ||
| 209 | } else { | ||
| 210 | for (@peers) { | ||
| 211 | my $peer = $_; | ||
| 212 | if ($self->isDir($peer) or $self->isUntracked($peer)) { #weed through the untracked and directory section of the list | ||
| 213 | } elsif ($name lt $peer) { | ||
| 214 | push @op, -before => $peer; | ||
| 215 | last; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | push @op, -image => $self->GetFileIcon($self->GetFileName($name)); | ||
| 219 | } | ||
| 220 | my $n = $self->add($name, @op, | ||
| 221 | -text => $title, | ||
| 222 | -data => $data, | ||
| 223 | ); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | $self->refreshList; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub collapse { | ||
| 231 | my ($self, $entry) = @_; | ||
| 232 | |||
| 233 | my @children; | ||
| 234 | if (defined $entry) { | ||
| 235 | @children = $self->GetChildren($entry); | ||
| 236 | } else { | ||
| 237 | @children = $self->infoRoot; | ||
| 238 | } | ||
| 239 | |||
| 240 | for (@children) { | ||
| 241 | if ($self->infoChildren($_)) { | ||
| 242 | $self->collapse($_); | ||
| 243 | } | ||
| 244 | $self->close($_); | ||
| 245 | $self->update; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | =item B<collapseAll> | ||
| 250 | |||
| 251 | Closes all folders in the Tree widget, except for the | ||
| 252 | path leading to the currently selected entry. | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub collapseAll { | ||
| 257 | my $self = shift; | ||
| 258 | my ($sel) = $self->infoSelection; | ||
| 259 | $self->collapse; | ||
| 260 | $self->update; | ||
| 261 | $self->entryShow($self->GetFileName($sel)) if defined $sel; | ||
| 262 | $self->refreshList; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub CreatePathBar { | ||
| 266 | return $_[0]->Label( | ||
| 267 | -anchor => 'w', | ||
| 268 | )->pack( | ||
| 269 | -fill => 'x', | ||
| 270 | -padx => 2, | ||
| 271 | -pady => 2, | ||
| 272 | ) | ||
| 273 | } | ||
| 274 | |||
| 275 | sub DefaultDirIcon { | ||
| 276 | return $_[0]->Pixmap(-file => Tk->findINC('folder.xpm')) | ||
| 277 | } | ||
| 278 | |||
| 279 | sub DefaultFileIcon { | ||
| 280 | return $_[0]->Pixmap(-file => Tk->findINC('file.xpm')) | ||
| 281 | } | ||
| 282 | |||
| 283 | sub DefaultSaveIcon { | ||
| 284 | return $_[0]->Pixmap(-data => $save_pixmap) | ||
| 285 | } | ||
| 286 | |||
| 287 | sub Delete { | ||
| 288 | my ($self, $entry) = @_; | ||
| 289 | my $par = $self->GetParent($entry); | ||
| 290 | $self->delete($entry); | ||
| 291 | if ($par ne '') { | ||
| 292 | my @peers = $self->infoChildren($par); | ||
| 293 | $self->Delete($par) unless @peers; | ||
| 294 | } | ||
| 295 | $self->refreshList; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub DirList { | ||
| 299 | my ($self, $path, $list) = @_; | ||
| 300 | $list = [] unless defined $list; | ||
| 301 | $path = '' unless defined $path; | ||
| 302 | my @children = $self->GetChildren($path); | ||
| 303 | for (@children) { | ||
| 304 | if ($self->isDir($_)) { | ||
| 305 | push @$list, $_; | ||
| 306 | $self->ItemList($_, $list); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | return @$list; | ||
| 310 | } | ||
| 311 | |||
| 312 | =item B<entryAdd>I<($filename)> | ||
| 313 | |||
| 314 | Adds the entry $filename. | ||
| 315 | $filename can also be an untracked entry. | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | sub entryAdd { | ||
| 320 | my ($self, $new) = @_; | ||
| 321 | |||
| 322 | if (-d $new) { | ||
| 323 | warn "You can not add a directory\n"; | ||
| 324 | return | ||
| 325 | } | ||
| 326 | my $untracked = not -e $new; | ||
| 327 | |||
| 328 | my $type = 'file'; | ||
| 329 | if ($untracked) { | ||
| 330 | $type = 'untracked'; | ||
| 331 | } else { | ||
| 332 | my $sep = quotemeta($self->cget('-separator')); | ||
| 333 | $new =~ s/^$sep// unless $Config{osname} eq 'MSWin32'; | ||
| 334 | my $compath = quotemeta($self->GetCommonPath($new)); | ||
| 335 | my $reg = $compath . $sep; | ||
| 336 | $new =~ s/^$reg// | ||
| 337 | } | ||
| 338 | # $self->pause(5500); | ||
| 339 | $self->Add($new, $type); | ||
| 340 | $self->refreshList; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub EntryClick { | ||
| 344 | my ($self, $entry) = @_; | ||
| 345 | return if $self->isDir($entry); | ||
| 346 | $entry = $self->GetFileName($entry) unless $self->isUntracked($entry); | ||
| 347 | $self->Callback('-entryselect', $entry); | ||
| 348 | } | ||
| 349 | |||
| 350 | =item B<entryDelete>I<($filename)> | ||
| 351 | |||
| 352 | Deletes the entry $filename. | ||
| 353 | $filename can also be an untracked entry. | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | sub entryDelete { | ||
| 358 | my ($self, $entry) = @_; | ||
| 359 | my $sep = $self->cget('-separator'); | ||
| 360 | $entry =~ s/^$sep// unless $Config{osname} eq 'MSWin32'; | ||
| 361 | $entry = $self->StripPath($entry); | ||
| 362 | |||
| 363 | if ($self->isDir($entry)) { | ||
| 364 | warn "You cannot delete a directory: $entry"; | ||
| 365 | return | ||
| 366 | } | ||
| 367 | |||
| 368 | $self->Delete($entry); | ||
| 369 | my @c = $self->infoRoot; | ||
| 370 | if (@c) { | ||
| 371 | $self->GetCommonPath | ||
| 372 | } else { | ||
| 373 | $self->SetPath(''); | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | =item B<entryModified>I<($filename)> | ||
| 378 | |||
| 379 | Sets the icon in the $filename entry to the save icon, indicating the entry is modified. | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | sub entryModified { | ||
| 384 | my ($self, $entry) = @_; | ||
| 385 | my $sep = $self->cget('-separator'); | ||
| 386 | $entry =~ s/^$sep// unless $Config{osname} eq 'MSWin32'; | ||
| 387 | $entry = $self->StripPath($entry); | ||
| 388 | $self->entryConfigure($entry, -image => $self->GetSaveIcon); | ||
| 389 | } | ||
| 390 | |||
| 391 | =item B<entrySaved>I<($filename)> | ||
| 392 | |||
| 393 | Sets the icon in the $filename entry to the default file icon, indicating the entry is saved. | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | sub entrySaved { | ||
| 398 | my ($self, $entry) = @_; | ||
| 399 | my $sep = $self->cget('-separator'); | ||
| 400 | $entry =~ s/^$sep// unless $Config{osname} eq 'MSWin32'; | ||
| 401 | $entry = $self->StripPath($entry); | ||
| 402 | $self->entryConfigure($entry, -image => $self->GetFileIcon($entry)); | ||
| 403 | } | ||
| 404 | |||
| 405 | =item B<entrySelect>I<($filename)> | ||
| 406 | |||
| 407 | Call this method if you want to change the selection. | ||
| 408 | |||
| 409 | =cut | ||
| 410 | |||
| 411 | sub entrySelect { | ||
| 412 | my ($self, $entry) = @_; | ||
| 413 | |||
| 414 | my $sep = $self->cget('-separator'); | ||
| 415 | my $sel = $entry; | ||
| 416 | $sel =~ s/^$sep// unless $Config{osname} eq 'MSWin32'; | ||
| 417 | $sel = $self->StripPath($sel); | ||
| 418 | |||
| 419 | $self->entryShow($entry); | ||
| 420 | $self->selectionClear; | ||
| 421 | $self->anchorClear; | ||
| 422 | $self->selectionSet($sel); | ||
| 423 | } | ||
| 424 | |||
| 425 | =item B<entryShow>I<($filename)> | ||
| 426 | |||
| 427 | Expands the parth to $filename so it becomes visible. | ||
| 428 | |||
| 429 | =cut | ||
| 430 | |||
| 431 | sub entryShow { | ||
| 432 | my ($self, $entry) = @_; | ||
| 433 | my $sep = $self->cget('-separator'); | ||
| 434 | $entry =~ s/^$sep// unless $Config{osname} eq 'MSWin32'; | ||
| 435 | $entry = $self->StripPath($entry); | ||
| 436 | my $parent = $self->infoParent($entry); | ||
| 437 | while (defined $parent) { | ||
| 438 | my $p = $self->get($parent); | ||
| 439 | unless ($p->opened) { | ||
| 440 | $self->open($parent); | ||
| 441 | } | ||
| 442 | $parent = $self->infoParent($parent); | ||
| 443 | } | ||
| 444 | $self->selectionSet($entry); | ||
| 445 | $self->see($entry); | ||
| 446 | $self->refreshList; | ||
| 447 | } | ||
| 448 | |||
| 449 | =item B<expandAll> | ||
| 450 | |||
| 451 | Opens all folders in the Tree widget. | ||
| 452 | |||
| 453 | =cut | ||
| 454 | |||
| 455 | sub expandAll { | ||
| 456 | my ($self, $entry) = @_; | ||
| 457 | my @children; | ||
| 458 | if (defined $entry) { | ||
| 459 | @children = $self->GetChildren($entry); | ||
| 460 | } else { | ||
| 461 | @children = $self->infoRoot; | ||
| 462 | } | ||
| 463 | for (@children) { | ||
| 464 | if ($self->infoChildren($_)) { | ||
| 465 | $self->open($_); | ||
| 466 | $self->expandAll($_); | ||
| 467 | } | ||
| 468 | } | ||
| 469 | $self->refreshList; | ||
| 470 | } | ||
| 471 | |||
| 472 | =item B<fileList> | ||
| 473 | |||
| 474 | Returns a list of all files in the document tree. | ||
| 475 | Untracked items are not included. | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | sub fileList { | ||
| 480 | my $self = shift; | ||
| 481 | my @list = $self->ItemList; | ||
| 482 | my @out = (); | ||
| 483 | for (@list) { | ||
| 484 | push @out, $self->GetFileName($_); | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | sub GetChildren { | ||
| 489 | my ($self, $name) = @_; | ||
| 490 | return $self->infoRoot if $name eq''; | ||
| 491 | return $self->infoChildren($name) | ||
| 492 | } | ||
| 493 | |||
| 494 | sub GetCommonPath { | ||
| 495 | my ($self, $new) = @_; | ||
| 496 | |||
| 497 | my @items = $self->ItemList; | ||
| 498 | my @files = (); | ||
| 499 | while (@items) { | ||
| 500 | my $item = shift @items; | ||
| 501 | push @files, $self->GetFullPath($item); | ||
| 502 | } | ||
| 503 | |||
| 504 | my @xfiles = @files; | ||
| 505 | push @xfiles, $new if defined $new; | ||
| 506 | |||
| 507 | my @ifiles = (); | ||
| 508 | my $sep = quotemeta($self->cget('-separator')); | ||
| 509 | for (@xfiles) { | ||
| 510 | my $file = $_; | ||
| 511 | $file = $self->GetParent($file); | ||
| 512 | my @p = split /$sep/, $file; | ||
| 513 | push @ifiles, \@p; | ||
| 514 | } | ||
| 515 | |||
| 516 | my $newpath = ''; | ||
| 517 | if (@ifiles) { | ||
| 518 | my $level = 0; | ||
| 519 | while ($level >= 0) { | ||
| 520 | my $equal = 1; | ||
| 521 | my $value = $ifiles[0]->[$level]; | ||
| 522 | unless (defined $value) { | ||
| 523 | $equal = 0; | ||
| 524 | $level = -1; | ||
| 525 | } else { | ||
| 526 | for (0 .. @ifiles - 1) { | ||
| 527 | if (defined $ifiles[$_]->[$level]) { | ||
| 528 | unless ($ifiles[$_]->[$level] eq $value) { | ||
| 529 | $equal = 0; | ||
| 530 | last; | ||
| 531 | } | ||
| 532 | } else { | ||
| 533 | $equal = 0; | ||
| 534 | last; | ||
| 535 | } | ||
| 536 | } | ||
| 537 | if ($equal) { | ||
| 538 | if ($newpath eq '') { | ||
| 539 | $newpath = $value | ||
| 540 | } else { | ||
| 541 | $newpath = $newpath . $self->cget('-separator') . $value; | ||
| 542 | } | ||
| 543 | $level ++; | ||
| 544 | } else { | ||
| 545 | $level = -1; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | } | ||
| 549 | } | ||
| 550 | |||
| 551 | my $oldpath = $self->GetPath; | ||
| 552 | if ($newpath ne $oldpath) { | ||
| 553 | $self->SetPath($newpath); | ||
| 554 | my @untrack = $self->untrackedList; | ||
| 555 | $self->deleteAll; | ||
| 556 | for (@untrack) { | ||
| 557 | $self->Add($_, 'untracked'); | ||
| 558 | } | ||
| 559 | for (@files) { | ||
| 560 | my $item = $_; | ||
| 561 | my $reg = quotemeta($newpath) . $sep; | ||
| 562 | $item =~ s/^$reg// unless $newpath eq ''; | ||
| 563 | $self->Add($item); | ||
| 564 | } | ||
| 565 | } | ||
| 566 | return $newpath | ||
| 567 | } | ||
| 568 | |||
| 569 | sub GetDirIcon { | ||
| 570 | my ($self, $name) = @_; | ||
| 571 | return $self->Callback('-diriconcall', $name); | ||
| 572 | } | ||
| 573 | |||
| 574 | sub GetFileIcon { | ||
| 575 | my ($self, $name) = @_; | ||
| 576 | return $self->Callback('-fileiconcall', $name); | ||
| 577 | } | ||
| 578 | |||
| 579 | sub GetFileName { | ||
| 580 | my ($self, $item) = @_; | ||
| 581 | $item = $self->GetFullPath($item); | ||
| 582 | unless ($Config{osname} eq 'MSWin32') { | ||
| 583 | $item = $self->cget('-separator') . $item; #unless ($Config{osname} eq 'MSWin32'); | ||
| 584 | } | ||
| 585 | return $item; | ||
| 586 | } | ||
| 587 | |||
| 588 | sub GetFullPath { | ||
| 589 | my ($self, $name) = @_; | ||
| 590 | my $commonpath = $self->GetPath; | ||
| 591 | my $sep = $self->cget('-separator'); | ||
| 592 | $name = $commonpath . $sep . $name if $commonpath ne''; | ||
| 593 | return $name; | ||
| 594 | } | ||
| 595 | |||
| 596 | sub GetParent { | ||
| 597 | my ($self, $name) = @_; | ||
| 598 | my $dir = dirname($name); | ||
| 599 | $dir = '' if $dir eq '.'; | ||
| 600 | return $dir | ||
| 601 | } | ||
| 602 | |||
| 603 | sub GetPath { | ||
| 604 | my $self = shift; | ||
| 605 | my $path = $self->Subwidget('PATH')->cget('-text'); | ||
| 606 | my $sep = quotemeta($self->cget('-separator')); | ||
| 607 | $path =~ s/^$sep//; | ||
| 608 | return $path; | ||
| 609 | } | ||
| 610 | |||
| 611 | sub GetPeers { | ||
| 612 | my ($self, $name) = @_; | ||
| 613 | my $par = $self->decodeParent($name); | ||
| 614 | return $self->infoRoot unless defined $par; | ||
| 615 | return $self->infoChildren($par) if $self->infoExists($par); | ||
| 616 | return () | ||
| 617 | } | ||
| 618 | |||
| 619 | sub GetSaveIcon { | ||
| 620 | my ($self, $name) = @_; | ||
| 621 | return $self->Callback('-saveiconcall', $name); | ||
| 622 | } | ||
| 623 | |||
| 624 | sub isDir { | ||
| 625 | my ($self, $item) = @_; | ||
| 626 | return 1 if $self->infoData($item) eq 'dir'; | ||
| 627 | return 0; | ||
| 628 | } | ||
| 629 | |||
| 630 | sub isFile { | ||
| 631 | my ($self, $item) = @_; | ||
| 632 | return 1 if $self->infoData($item) eq 'file'; | ||
| 633 | return 0; | ||
| 634 | } | ||
| 635 | |||
| 636 | sub isUntracked { | ||
| 637 | my ($self, $item) = @_; | ||
| 638 | return 1 if $self->infoData($item) eq 'untracked'; | ||
| 639 | return 0; | ||
| 640 | } | ||
| 641 | |||
| 642 | sub ItemList { | ||
| 643 | my ($self, $path, $list) = @_; | ||
| 644 | $list = [] unless defined $list; | ||
| 645 | $path = '' unless defined $path; | ||
| 646 | my @children = $self->GetChildren($path); | ||
| 647 | for (@children) { | ||
| 648 | if ($self->isUntracked($_)) { #ignoring untracked | ||
| 649 | } elsif ($self->isDir($_)) { | ||
| 650 | $self->ItemList($_, $list) | ||
| 651 | } else { | ||
| 652 | push @$list, $_ | ||
| 653 | } | ||
| 654 | } | ||
| 655 | return @$list; | ||
| 656 | } | ||
| 657 | |||
| 658 | sub lmPost { | ||
| 659 | my $self = shift; | ||
| 660 | my ($x, $y) = $self->pointerxy; | ||
| 661 | my $items = $self->cget('-contextmenu'); | ||
| 662 | if (@$items) { | ||
| 663 | my $menu = $self->Menu( | ||
| 664 | -menuitems => $items, | ||
| 665 | -tearoff => 0, | ||
| 666 | ); | ||
| 667 | $menu->bind('<Leave>', [$self, 'lmUnpost']); | ||
| 668 | $self->{'l_menu'} = $menu; | ||
| 669 | $menu->post($x - 6, $y - 6); | ||
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | sub lmUnpost { | ||
| 674 | my $self = shift; | ||
| 675 | my $menu = $self->{'l_menu'}; | ||
| 676 | if (defined $menu) { | ||
| 677 | delete $self->{'l_menu'}; | ||
| 678 | $menu->unpost; | ||
| 679 | $menu->destroy; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | sub PathCompare { | ||
| 684 | my ($self, $path1, $path2) = @_; | ||
| 685 | my @l1 = $self->PathList($path1); | ||
| 686 | my @l2 = $self->PathList($path2); | ||
| 687 | my $size1 = @l1; | ||
| 688 | my $size2 = @l2; | ||
| 689 | if ($size1 > $size2) { | ||
| 690 | return 1 | ||
| 691 | } elsif ($size1 eq $size2) { | ||
| 692 | return 0 | ||
| 693 | } else { | ||
| 694 | return -1 | ||
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | sub PathList { | ||
| 699 | my ($self, $path) = @_; | ||
| 700 | my $sep = $self->cget('-separator'); | ||
| 701 | return split /$sep/, $path | ||
| 702 | } | ||
| 703 | |||
| 704 | sub pause { | ||
| 705 | my ($self, $time) = @_; | ||
| 706 | my $var = 0; | ||
| 707 | $self->after($time, sub { $time = 1 }); | ||
| 708 | $self->waitVariable(\$time); | ||
| 709 | } | ||
| 710 | |||
| 711 | sub refreshList { | ||
| 712 | my ($self, $flag) = @_; | ||
| 713 | |||
| 714 | my $delay = $self->cget('-refreshdelay'); | ||
| 715 | unless (defined $flag) { | ||
| 716 | my $id = $self->{'timer_id'}; | ||
| 717 | $self->afterCancel($id) if defined $id; | ||
| 718 | |||
| 719 | $id = $self->after($delay, [refreshTimer => $self]); | ||
| 720 | $self->{'timer_id'} = $id; | ||
| 721 | |||
| 722 | return | ||
| 723 | } | ||
| 724 | |||
| 725 | $self->setCellSize; | ||
| 726 | my $c = $self->Subwidget('Tree')->Subwidget('Canvas'); | ||
| 727 | my ($view) = $c->yview; | ||
| 728 | $self->refreshPurge; | ||
| 729 | $self->after($delay, sub { $c->yview('moveto', $view) }); | ||
| 730 | |||
| 731 | } | ||
| 732 | |||
| 733 | sub refreshTimer { | ||
| 734 | my $self = shift; | ||
| 735 | delete $self->{'timer_id'}; | ||
| 736 | $self->refreshList(1); | ||
| 737 | } | ||
| 738 | |||
| 739 | sub setCellSize { | ||
| 740 | my ($self, $name) = @_; | ||
| 741 | my $t = $self->Subwidget('Tree'); | ||
| 742 | unless ($self->{HAVECELLSIZE}) { | ||
| 743 | # my $i = $t->get($name); | ||
| 744 | $t->cellSize; | ||
| 745 | $self->{HAVECELLSIZE} = 1 | ||
| 746 | } | ||
| 747 | # $t->refreshPurge($t->index($name)) | ||
| 748 | } | ||
| 749 | |||
| 750 | sub SetPath { | ||
| 751 | my ($self, $path) = @_; | ||
| 752 | $path = $self->cget('-separator') . $path unless ($Config{osname} eq 'MSWin32'); | ||
| 753 | $self->Subwidget('PATH')->configure(-text => $path); | ||
| 754 | } | ||
| 755 | |||
| 756 | sub StripPath { | ||
| 757 | my ($self, $name) = @_; | ||
| 758 | my $path = quotemeta($self->GetPath); | ||
| 759 | my $sep = quotemeta($self->cget('-separator')); | ||
| 760 | $name =~ s/^$path$sep// if $path ne ''; | ||
| 761 | return $name; | ||
| 762 | } | ||
| 763 | |||
| 764 | =item B<untrackedList> | ||
| 765 | |||
| 766 | =over 4 | ||
| 767 | |||
| 768 | Returns a list of all untracked items. | ||
| 769 | |||
| 770 | =back | ||
| 771 | |||
| 772 | =cut | ||
| 773 | |||
| 774 | sub untrackedList { | ||
| 775 | my $self = shift; | ||
| 776 | my @top = $self->infoRoot(''); | ||
| 777 | my @untracked = (); | ||
| 778 | for (@top) { | ||
| 779 | push @untracked, $_ if $self->infoData($_) eq 'untracked' | ||
| 780 | } | ||
| 781 | return @untracked | ||
| 782 | } | ||
| 783 | |||
| 784 | =back | ||
| 785 | |||
| 786 | =head1 LICENSE | ||
| 787 | |||
| 788 | Same as Perl. | ||
| 789 | |||
| 790 | =head1 AUTHOR | ||
| 791 | |||
| 792 | Hans Jeuken (hanje at cpan dot org) | ||
| 793 | |||
| 794 | =head1 BUGS | ||
| 795 | |||
| 796 | Unknown. If you find any, please contact the author. | ||
| 797 | |||
| 798 | =head1 SEE ALSO | ||
| 799 | |||
| 800 | =over 4 | ||
| 801 | |||
| 802 | =item L<Tk::ListBrowser> | ||
| 803 | |||
| 804 | =back | ||
| 805 | |||
| 806 | =cut | ||
| 807 | |||
| 808 | 1; | ||
| 809 | |||
| 810 | __END__ |