#!../tree_wish -f
# -*-Tcl-*-
#
# dirtree8 - same as dirtree7, but adds editing capabilities


source dirtree7

$canvas bind text <Double-Button-1> "ToggleChildren $canvas $tree"
$canvas bind bitmap <Double-Button-1> "ToggleParent $canvas $tree"


# If the current selection is a leaf, add its subnodes, otherwise
# remove them

proc ToggleChildren {canvas tree} {
    set path [GetPath $canvas]
    if [$tree isleaf $path] {
	ListDirs $canvas $tree $path
	$tree draw
    } else {
	$tree prune $path
    }
}


# If the selected node is the root of the tree, add its parent and siblings
# to the tree, otherwise make the selected node the new root of the tree.

proc ToggleParent {canvas tree} {
    global dirtree
    set path [GetPath $canvas]
    if [$tree isroot $path] {
	set dir [file dirname $path]
	if {$dir != $path} {
	    AddDir $canvas $tree "" $dir [dir_tail $dir]
	    set tail [file tail $path]
	    foreach i [exec ls $dir] {
		if {[file isdirectory $dir/$i]} {
		    if {$i == $tail} {
			$tree movelink $path $dir
		    } else {
			AddDir $canvas $tree $dir $dir/$i "$i"
		    }
		}
	    }
	    $tree draw
	}
    } else {
	$tree root $path
    }
}


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

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

