#!../tree_wish -f
# -*-Tcl-*-
#
# dirtree7 - similar to dirtree6, but adds selection of nodes 
#            with mouse buton <1>

source util.tcl
 

# set the bindings for the tree canvas

proc SetBindings {canvas tree} {
    # bind mouse button <1> to select the label or bitmap
    $canvas bind text <1> "focus %W; SelectNode $canvas"
    $canvas bind bitmap <1> "SelectBitmap $canvas"
}


# select the current node's label

proc SelectNode {canvas} {
    $canvas select from current 0
    $canvas select to current [string length [$canvas itemcget current -text]]
    DeSelectBitmap $canvas
    puts "selected label: [GetPath $canvas]"
}


# de-select all node labels

proc DeSelectNode {canvas} {
    $canvas select clear
}


# highlight the node's bitmap

proc SelectBitmap {canvas} {
    catch {focus {}}
    set path [lindex [$canvas gettags current] 0]
    DeSelectNode $canvas
    DeSelectBitmap $canvas
    $canvas itemconfig current -background [$canvas cget -selectbackground] \
	-tags "[$canvas gettags current] selected" 
    puts "selected bitmap: [GetPath $canvas]"
}


# stop highlighting the node's bitmap

proc DeSelectBitmap {canvas} {
    $canvas itemconfig selected -background [$canvas cget -background]
    $canvas dtag selected
}



# return the pathname (dir) for the item currently selected (bitmap or text)

proc GetPath {canvas} {
    set id [$canvas select item]
    if {"$id" == ""} {
        return [lindex [$canvas gettags selected] 0]
    }
    return [lindex [$canvas gettags $id] 0]
}


# add the given dir to the tree
#
# Args: 
#  canvas  - tree's canvas
#  tree    - the tree
#  parent  - pathname of parent dir 
#  dir     - pathname of new dir being added
#  text    - text for tree node label (last component of dir)

proc AddDir {canvas tree parent dir text} {
    set bitmap @bitmaps/dir.xbm
    $canvas create text 0 0 -text $text -tags [list $dir text $dir:text]
    $canvas create bitmap 0 0 -bitmap $bitmap -tags [list $dir bitmap $dir:bitmap]
    set line [$canvas create line 0 0 0 0 -tag "line"]
    LayoutNode $canvas $tree $dir
    $tree addlink $parent $dir $line -border 1m
}


# main 

wm geometry . 400x275
set canvas [MakeCanvas . .c]
set tree [tree $canvas.t]

cd ..
set dir [pwd]
AddDir $canvas $tree {} $dir [file tail $dir]
ListDirsRec $canvas $tree $dir
SetBindings $canvas $tree
