# menuunit.tcl --
#
#        Este fichero implementa el código Tcl de un control unidad
#        utilizado en GID. Se compone de un entry y un menucanvas.
#        
#
# Copyright (c) 2000 CIMNE
#

package provide MenuUnits 0.1

namespace eval MenuUnits {
    namespace export CreateInstance

    variable LastInstance -1   
}

proc MenuUnits::CreateInstance {} { 
    variable LastInstance

    incr LastInstance

    set _MenuUnits ::MenuUnits$LastInstance
    namespace eval $_MenuUnits {
	namespace export Create GetStrUnit
	
	variable  VarInfo      ""
	variable  UInfoList    ""
	variable  MagTable     ""
	variable  MagName      ""
	variable  UnitsList    ""
	variable  IdxUnit      -1
	variable  MCanvas      ""
	variable  MaxWidth     0
    }

    proc $_MenuUnits\::InitUnitsList {} {
	variable UInfoList
	variable MagTable
	variable MagName
	variable IdxUnit

	if { [info commands .central.s] == "" } {
	    variable UnitsList   $::Units::Magnitudes($MagName)
	    for { set i 0 } { $i < [llength $UnitsList] } { incr i } {
		lappend UInfoList [list $MagName $i 0]
	    }
	} else {
	    variable UnitsList   [.central.s info magnitudes 1 $MagName units]
	    for { set i 0 } { $i < [llength $UnitsList] } { incr i } {
		lappend UInfoList [list $MagName $i 1]
	    }
	    set ugl [.central.s info magnitudes 0 $MagName units]
	    if { $MagTable } {
		set IdxNeedUpdate 0
		set gidx 0
	    } else {
		set IdxNeedUpdate 1
		set gidx [llength $UnitsList]
		incr gidx -1
	    }
	    set lidx 0
	    foreach u $ugl {
		set N [lindex $u 1]
		set D [lindex $u 2]
		if { [FindUnitInList $N $D $UnitsList] == {} } {
		    lappend UnitsList $u
		    incr gidx
		    lappend UInfoList [list $MagName $lidx 0]
		}
		if { $IdxNeedUpdate && $lidx == $IdxUnit } {
		    set IdxUnit $gidx
		    set IdxNeedUpdate 0
		}
		incr lidx
	    }
	}
    }

    proc $_MenuUnits\::Create { w _table _mag _idx _varinfo } {
	variable VarInfo      $_varinfo
	variable MagTable     $_table
	variable MagName      $_mag
	variable IdxUnit      $_idx
	variable MCanvas

	InitUnitsList

	set MCanvas [MenuCanvas::CreateInstance]
	$MCanvas\::Create $w \
	    [namespace code OnChangeUnit] \
	    [namespace code FillMenuUnit] \
	    [namespace code PopupEntry]
	InitMenu

	bind $w <Destroy> +[namespace code "Destroy %W"]
    }

    proc $_MenuUnits\::Destroy { W } {
	
	#incr MenuUnits::LastInstance -1
	namespace delete [namespace current]
    }

    proc $_MenuUnits\::GetStrUnit {} {
	variable MagName
	variable UnitsList
	variable IdxUnit

	return [lindex [lindex $UnitsList $IdxUnit] end]
    }

    proc $_MenuUnits\::InitMenu {} {
	variable UnitsList
	variable IdxUnit
	variable MCanvas

	#calculo el tamaño del boton adecuado para que no salte al seleccionar
	# una opcion de mayor geometria
	set xmax 0
	set ymax 0
	set c [canvas .__tmpcanvas__ -width 0 -height 0]
	foreach ui $UnitsList {
	    set cgeo [GidDrawUnit [lindex $ui 3] [lindex $ui 4] black $c]
	    foreach {x y} $cgeo {}
	    if { $x > $xmax } {
		set xmax $x
	    }
	    if { $y > $ymax } {
		set ymax $y
	    }
	}
	destroy $c

	set Button [$MCanvas\::GetButton]
	set unitinfo [lindex $UnitsList $IdxUnit]
	set cgeo [GidDrawUnit [lindex $unitinfo 3] [lindex $unitinfo 4] black $Button]
# 	$Button configure -width [lindex $cgeo 0] -height [lindex $cgeo 1]
 	$Button configure -width $xmax -height $ymax
    }
    
    proc $_MenuUnits\::OnChangeUnit { menu idx } {
	variable UInfoList
	variable UnitsList
	variable IdxUnit
	variable VarInfo

	if { $idx == $IdxUnit } {
	    return
	}
	set IdxUnit $idx
	set ucambio [lindex $UnitsList $idx]
	set Button [$menu\::GetButton]
	$Button delete all
	set cgeo [GidDrawUnit [lindex $ucambio 3] [lindex $ucambio 4] black $Button]
	#$Button configure -width [lindex $cgeo 0] -height [lindex $cgeo 1]

	upvar \#0 $VarInfo var
	set var [lindex $UInfoList $IdxUnit]
    }

    proc $_MenuUnits\::FillMenuUnit { bmenu } {
	variable MagName
	variable UnitsList
	variable IdxUnit

	set i 0
	foreach unitinfo $UnitsList {
	    if { $i == $IdxUnit } {
		set color red
	    } else {
		set color black
	    }
	    $bmenu\::AddEntry "GidDrawUnit [list [lindex $unitinfo 3]] [list [lindex $unitinfo 4]] $color "
	    incr i
	}
    }

    proc $_MenuUnits\::PopupEntry { c i } {
	variable IdxUnit

	if { $i == $IdxUnit } {
	    $c itemconfigure todo -fill red
	} else {
	    $c itemconfigure todo -fill black
	}
    }

    return $_MenuUnits
}