#!/bin/sh
# treeforpass.tcl -*- TCL -*- Created: ramsan Nov-1999, Modified: ramsan Nov-1999
# 	$Id: treeforpass.tcl,v 1.13 2003/02/06 19:12:59 jsperez Exp $

#----------------------TREEFORPASS ALONE---------------------------
# the next line restarts using wish \
	exec wish8.0 "$0" "$@"

set TreeRunningAlone 0
catch {
    if {$argv0 == [info script] } {
	set TreeRunningAlone 1
	set tcl_interactive 0
	set tk_strictMotif 1
	wm withdraw .

	if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
	    set GIDDEFAULTTCL $GIDDEFAULT
	    catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
	    set auto_path "$GIDDEFAULTTCL"
	    source $GIDDEFAULTTCL/init.tcl
	    source $GIDDEFAULTTCL/tk.tcl
	}
    }
}

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------

proc PressingButOne { tree what node } {
    global GidPriv

    if { $what == "Double-1" && [$tree nodes $node] != "" } {
	if { [$tree itemcget $node -open] == 0 } {
	    $tree itemconfigure $node -open 1
	    TreeModifyDir 1 $tree $node
	} else {
	    $tree itemconfigure $node -open 0
	    TreeModifyDir 0 $tree $node
	}
	return
    }
    if { [$tree nodes $node] != "" } { return }
    switch -exact $what {
	1 {
	    $tree selection set $node
	}
	Control-1 {
	    if { [lsearch -exact [$tree selection get] $node] != -1 } {
		$tree selection remove $node
	    } else {
		$tree selection add $node
	    }
	}
    }
}

proc RetFreeNode { tree node } {
    if { ![$tree exists $node] } { return $node }
    set inum 1
    while { [$tree exists $node$inum] } { incr inum }
    return $node$inum
}

proc SetProblemTypeTreeForDir { dir tree treeparent } {
    global GidPriv

    set inum 0
    set files ""
    foreach i [glob -nocomplain [file join $dir *]] {
	lappend files [file tail $i]
    }
    foreach i [lsort $files] {
	set ifull [file join $dir $i]
	if { [file extension $i] == ".gid" } {
	    set ishort [file root $i]
	    set node [RetFreeNode $tree $ishort]
	    $tree insert end $treeparent $node -text $ishort \
		    -image $GidPriv(image_GiDdirectory)
	    incr inum
	} elseif { [file isdir $ifull] } {
	    #set ishort [file root $i]
	    set ishort $i
	    set node [RetFreeNode $tree $ishort]
	    $tree insert end $treeparent $node -text $ishort \
		    -image $GidPriv(image_folder)
	    set inumin [SetProblemTypeTreeForDir $ifull $tree $ishort]
	    if { $inumin == 0 } {
		$tree delete $node
	    } else { incr inum $inumin }
	}
    }
    return $inum
}

proc TreeModifyDir { idx tree node } {
    global GidPriv
    
    if { $idx && [$tree itemcget $node -drawcross] == "allways" } {
        getdir $tree $node [$tree itemcget $node -data]
        if { [llength [$tree nodes $node]] } {
            $tree itemconfigure $node -image $GidPriv(image_openfold)
        } else {
            $tree itemconfigure $node -image $GidPriv(image_folder)
        }
    } else {
        $tree itemconfigure $node -image \
		$GidPriv(image_[lindex {folder openfold} $idx])
    }
}


proc SetProblemtypeForTree { w } {
    global GidPriv GIDDEFAULT

    foreach img {openfold folder GiDdirectory} {
	if { ![info exists GidPriv(image_$img)] } {
	    set GidPriv(image_$img) [image create photo -file \
					 [file join $GIDDEFAULT bitmaps ${img}.gif]]
	}
    }

    set sw    [ScrolledWindow $w.sw \
	    -relief sunken -borderwidth 2]
    set tree  [Tree $w.sw.tree \
	    -relief flat -borderwidth 0 -width 15 -highlightthickness 0 \
	    -highlightcolor red -selectforeground black -selectbackground green -highlightbackground yellow -width 30 \
	    -redraw 1 -dropenabled 1 -dragenabled 1 \
	    -dragevent 3 \
	    -droptypes {
	TREE_NODE    {copy {} move {} link {}}
	LISTBOX_ITEM {copy {} move {} link {}}
    } \
	    -opencmd   "TreeModifyDir 1 $w.sw.tree" \
	    -closecmd  "TreeModifyDir 0 $w.sw.tree"]

	$sw setwidget $tree

	$tree configure -selectbackground yellow

	foreach i "Double-1 1 Control-1" {
	    $tree bindImage <$i> "PressingButOne $tree $i"
	    $tree bindText  <$i> "PressingButOne $tree $i"
	}
	pack $sw    -side top  -expand yes -fill both


    set files ""
    foreach i [glob -nocomplain [file join $GIDDEFAULT *.gid]] {
	lappend files [file tail [file root $i]]
    }
    foreach i [lsort $files] {
	# the tree doesn't support spaces in node names
	regsub -all { } $i {_} tree_node 
	set node [RetFreeNode $tree $tree_node]
	$tree insert end root $tree_node -text $i -image $GidPriv(image_GiDdirectory)
    }
    
    SetProblemTypeTreeForDir [file join $GIDDEFAULT problemtypes] $tree root
    return $tree
}

proc ChooseProblemtype { w } {
    global GidPriv GIDDEFAULT
    set t [toplevel $w.t]
    wm title $t [_ "Choose one module"]

    wm geom $t +[expr [winfo x [winfo toplevel $w]]+20]+[expr [winfo y [winfo toplevel $w]]+20]

    set tree [SetProblemtypeForTree $t]
    pack [ label $t.l -text [_ "Press Control-mouse1 to select several problem types"] \
	    -font SmallFont]
    pack [frame $t.buts]
    button $t.buts.ok -text [_ "OK"] -width 8 -command { set GidPriv(selectmodules) 1 }
    button $t.buts.cancel -text [_ "Cancel"] -width 8 -command { set GidPriv(selectmodules) 0 }
    pack $t.buts.ok $t.buts.cancel -side left -padx 3 -pady 3
    set GidPriv(selectmodules) 0
    grab $t
    tkwait variable GidPriv(selectmodules)
    set retval "--CANCEL--"
    if { $GidPriv(selectmodules) == 1 } {
	set retval ""
	foreach i [$tree selection get] {
	    set node $i
	    set nodename [$tree itemcget $i -text]
	    while { [$tree parent $node] != "root" } {
		set node [$tree parent $node]
		set nodename [file join [$tree itemcget $node -text] $nodename]
	    }
	    if { [file isdir [file join $GIDDEFAULT $nodename.gid]] } {
		set val [file join $GIDDEFAULT $nodename]
	    } elseif { [file isdir [file join $GIDDEFAULT problemtypes $nodename.gid]] } {
		set val [file join $GIDDEFAULT problemtypes $nodename]
	    } else {
		WarnWin [_ "error -%s-" $nodename]
	    }
	    lappend retval $val
	}
    }
    destroy $t
    return $retval
}

#----------------------TREEFORPASS ALONE---------------------------
if { $TreeRunningAlone == 1 } {
    wm withdraw .
    toplevel .t
    SetProblemtypeForTree .t
} 
#-----------------------------------------------------------------


