#!../tree_wish -f
# -*-Tcl-*-
#
# dirtree10 - same as dirtree9, but adds key binding for the cursor keys
#             to navigate the tree.


source dirtree9

bind $canvas <Left> "SelectNext $canvas $tree %K"
bind $canvas <Right> "SelectNext $canvas $tree %K"
bind $canvas <Down> "SelectNext $canvas $tree %K"
bind $canvas <Up> "SelectNext $canvas $tree %K"


# select the current node's parent, child or sibling
# depending on the value of direction (Left, Right, Up or Down)

proc SelectNext {canvas tree direction} {
    set id [$canvas select item]
    set path [lindex [$canvas gettags $id] 0]
    
    # for vertical trees its different...
    if {[$tree cget -layout] == "vertical"} {
	case $direction in {
	    Left	{set direction Up}
	    Right	{set direction Down}
	    Up	        {set direction Left}
	    Down	{set direction Right}
	}
    }
    
    case $direction in {
        Left {
	    set node [$tree parent $path]
	    if {"$node" == ""} {
		ToggleParent $canvas $tree
		set node [$tree parent $path]
	    }
       	}
        Right {
	    set node [$tree child $path]
	    if {"$node" == ""} {
		ListDirs $canvas $tree $path
		set node [$tree child $path]
		$tree draw
	    }
	}
        Down {
	    set node [$tree sibling $path]
	    if {"$node" == ""} {
		set node [$tree child [$tree parent $path]]
	    }
	}
        Up {
	    set next [$tree child [$tree parent $path]]
	    while {"$next" != ""} {
		set node $next
		set next [$tree sibling $next]
		if {"$next" == "$path"} {
		    break;
		}
	    }
	}
        default {return}
    }

    if {"$node" != ""} {
        set next [$canvas find withtag $node:text]
        $canvas select from $next 0
        $canvas select to $next [string length [$canvas itemcget $next -text]]
    }
}

