#!../treesh/treesh -f
#
# dirtree: demonstrate the tree widget by displaying
# a directory tree. 
#
# Double-Click on a node and see what happens...
#
# -----------------------------------------------------------------------------
# Copyright 1993 Allan Brighton.
# 
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies.  Allan
# Brighton make no representations about the suitability of this software
# for any purpose.  It is provided "as is" without express or implied
# warranty.
# -----------------------------------------------------------------------------


# return the last component of the directory name
# (/ is a special case)

proc dir_tail {dir} {
    if {"$dir" == "/"} {return $dir}
   return [file tail $dir]
}


# add a path (file or dir) to the tree under the given dir

proc add_node {tree dir path} {
    if {[file isdirectory $path]} {
	tree_addNode $tree $dir $path dir.xbm [dir_tail $path]
    } else {
	tree_addNode $tree $dir $path file.xbm [dir_tail $path]
    }
}


# return a list of the files under the given dir

proc list_files {dir} {
    if {"$dir" == "/"} {
	set expr "/*"
    } else {
	set expr "$dir/*"
    }
    return [lsort [glob -nocomplain $expr]]
}


# add the files and dirs under the given one to the tree

proc add_files_under_dir {tree dir} {
    foreach path [list_files $dir] {
	add_node $tree $dir $path
    }
}


# called when the text of a node is double-clicked

proc text_proc {tree} {
    set path [tree_getCurrent $tree]
    if [$tree isleaf $path] {	
	add_files_under_dir $tree $path
    } else {
	$tree prune $path
    }
    $tree draw
}


# called when bitmap of a node is double-clicked

proc bitmap_proc {tree} {
    set path [tree_getCurrent $tree]
    tree_deSelectBitmap $tree
    if [$tree isroot $path] {
       set dir [file dirname $path]
       if {"$dir" != "$path"} {
	   tree_addNode $tree "" $dir dir.xbm [dir_tail $dir]
	   foreach file [list_files $dir] {
	       if {"$file" == "$path"} {
		   $tree movelink $path $dir
	       } else {
		   add_node $tree $dir $file
	       }
	   }
       }
    } else {
	$tree root $path
    }
    $tree draw
}


# create the menu bar

proc menubar_create {tree} {
    set menubar [frame .menu -relief raised -bd 2]
    pack $menubar -side top -fill x -ipady 1m
    
    # file menu
    set mb  [menubutton $menubar.file -text "File" -menu $menubar.file.m]
    set m [menu $mb.m]
    pack $mb -side left -padx 1m -ipadx 1m
    $m add command -label "Exit" -command {destroy .}

    # view menu
    set mb  [menubutton $menubar.view -text "View" -menu $menubar.view.m]
    set m [menu $mb.m]
    pack $mb -side left -padx 1m -ipadx 1m
    $m add command -label "Toggle Tree Layout" \
	-command "tree_toggleLayout $tree"
}

lappend auto_path [pwd]
wm title . "Simple Directory Tree"
wm minsize . 10 10 

set canvas .canvas
set tree $canvas.tree
menubar_create $tree
canvas_create . $canvas
tree_create $tree "text_proc $tree" "bitmap_proc $tree"

# add the root node
set dir [pwd]
tree_addNode $tree "" $dir dir.xbm [dir_tail $dir]
add_files_under_dir $tree $dir

$tree draw
tkwait visibility $canvas
tree_center $tree
