michaelg has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I would like to use the Tk package of tree or dirtree , but I can't see files icons when I use it , also if and when I'll be able to see file icons what is the best way to get the whole path when clicking it ( event or somthin .. ) . for bow I use the 'getOpenfile' Thx in advance . MichaelG

Replies are listed 'Best First'.
Re: using TK tree or dirtree
by michaelg (Beadle) on Sep 01, 2003 at 15:51 UTC
    Hi , I'm using this code that I found on the web . but I cna't find the event of doubleClicking a file icon . That Is the reason I can't extract the path of the requested file . so for now I'm using getopenfile .
    #!/usr/bin/perl -w # # Perl/Tk version of Tix4.1.0/demos/samples/DynTree.tcl. # # Chris Dean <ctdean@cogit.com> use lib ".."; use strict; use Tk; use Tk::Tree; my $top = new MainWindow( -title => "Tree (Dynamic)" ); my $tree = $top->ScrlTree( qw(-separator / -width 35 -height 25 -scrollbars osoe) ); $tree->pack( qw/-expand yes -fill both -padx 10 -pady 10 -side top/ ); $tree->configure( -opencmd => sub { dyntree_opendir( $tree, @_ ); } ); # Add the root directory the TixTree widget dyntree_adddir( $tree, "/" ); # The / directory is added in the "open" mode. The user can open it # and then browse its subdirectories ... my $ok = $top->Button( qw/-text Ok -underline 0 -width 6/, -command => sub { exit } ); my $cancel = $top->Button( qw/-text Cancel -underline 0 -width 6/, -command => sub { exit } ); $ok->pack( qw/-side left -padx 10 -pady 10/ ); $cancel->pack( qw/-side right -padx 10 -pady 10/ ); MainLoop(); sub dyntree_adddir { my( $tree, $dir ) = @_; my $text = $dir; $text = (split( "/", $dir ))[-1] unless $text eq "/"; $tree->add( $dir, -text => $text, -image => $tree->Getimage("folde +r") ); $tree->setmode( $dir, -d $dir ? "open" : "none" ); } # This command is called whenever the user presses the (+) indicator o +r # double clicks on a directory whose mode is "open". It loads the file +s # inside that directory into the Tree widget. # # Note we didn't specify the -closecmd option for the Tree widget, so +it # performs the default action when the user presses the (-) indicator +or # double clicks on a directory whose mode is "close": hide all of its +child # entries # sub dyntree_opendir { my( $tree, $dir ) = @_; if( my @kids = $tree->infoChildren( $dir ) ) { # We have already loaded this directory. Let's just # show all the child entries # # Note: since we load the directory only once, it will not be # refreshed if the you add or remove files from this # directory. # foreach my $kid (@kids) { $tree->show( -entry => $kid ); } return; } my $image = $tree->Getimage("folder"); opendir D, $dir or return; foreach my $file (sort readdir( D )) { next if $file =~ /^\./; my $path = "$dir/$file"; $path = "/$file" if $dir eq "/"; if( -d $path ) { dyntree_adddir( $tree, $path ); } else { $tree->add( $path, -text => $file, -image => $image ); } } closedir D; }

    edited: Mon Sep 1 15:53:57 2003 by jeffa - code tags

      To find out what item the user double-clicked on, add something along the lines of:

      $tree->bind('<Double-1>' => sub { print "Got:\n"; print " $_\n" for $tree->info('selection'); });

      just after the line where you configure the -opencmd option. You'll have to modify the sub {...} to suit your purposes, of course.

      As for showing the file icons, it's a little unclear exactly what you want. If you mean that you simply want a different icon for a file than for a directory, then you can simply change the image requested in the dyntree_adddir sub depending on whether the item being added tests as a directory or not (-d).

      If, however, you mean that you want, say, a different icon for each different file type (eg, the way MS's Explorer shows different icons depending on the extension of the file), that is rather more difficult. First of all, you'll have to decide how you want to determine which file gets which icon. Then you need to decided where you want to get all those icons from (eg, do you want to try and extract them from the OS, or do you want to carry around a bunch of images files with your program). Once you've decided those things, then you can start figuring out how to add those icons in to the program.

      bbfu
      Black flowers blossom
      Fearless on my breath

Re: using TK tree or dirtree
by Anonymous Monk on Sep 01, 2003 at 15:36 UTC
    code please