# moduniwin.tcl --
#
#        Este fichero implementa el código Tcl que muestra la informcaión de las unidades
#        de trabajo para un modelo.
#
# Copyright (c) 2000 CIMNE
#

catch {
    LoadDebugger
}

package provide ModUnitWin 0.1
#package require MenuUnits

namespace eval ModUnitWin {
    namespace export Clear Init Show IsOpen Raise InitInfoUnits

    variable win             ""
    variable cb_sys          ""
    variable f_udef          ""
    variable Systems         ""
    variable IdxSys          -1
    variable _IdxSys         -1
    variable CurSysName      ""
    variable LengthInfo      ""
    variable _LengthInfo     ""
    variable bUserDef        0
    variable _bUserDef       0
    variable UserDefSysList  ""
    variable _UserDefSysList
    variable UserDefSysArray
    variable UniSysArray
    variable SystemsList     ""
    variable EnableUserDef   1
}


proc ModUnitWin::Clear {} {
    variable win        ""
    variable cb_sys     ""
    variable f_udef     ""
    variable Systems    ""
    variable IdxSys     -1
    variable CurSysName ""
    variable LengthInfo ""
    variable bUserDef   0
    variable UserDefSysList ""
    variable UserDefSysArray 
    variable SystemsList ""

    catch {
	unset UserDefSysArray
	unset UniSysArray
    }
}

proc ModUnitWin::BuildSysArray {} {
    variable UserDefSysList
    variable UserDefSysArray
    variable UniSysArray
    variable bUserDef
    variable CurSysName
    variable SystemsList
    variable IdxSys

    if $bUserDef {
	upvar \#0 ::ModUnitWin::UserDefSysList  theList
	upvar \#0 ::ModUnitWin::UserDefSysArray theArray
    } else {
	set CurSysName [lindex $SystemsList $IdxSys]
	set theList [::Units::GetSystem $CurSysName]
	upvar \#0 ::ModUnitWin::UniSysArray theArray
    }

    catch {
	unset theArray
    }

    if { $bUserDef && ![string length $CurSysName] } {
	set ApplyFilter 0
    } else {
	set ApplyFilter 1
    }

    set MagsFilter [.central.s info unitssystems magused]
    
    foreach u [.central.s info unitssystems $CurSysName prj] {
	if { [lsearch $MagsFilter [set mag [lindex $u 0]]] == -1 } {
	    lappend MagsFilter $mag
	}
    }

    set i 0
    foreach u $theList {
	set mag [lindex $u 0]
	if $ApplyFilter {
	    if { $mag == "LENGTH" || [lsearch $MagsFilter $mag] != -1 } {
		set theArray($i) $u
		incr i
	    }
	} else {
	    set theArray($i) $u
	    incr i
	}
    }
}

proc ModUnitWin::InitInfoUnits {} {
    variable LengthInfo
    variable _LengthInfo
    variable bUserDef
    variable _bUserDef
    variable UserDefSysList
    variable _UserDefSysList
    variable UserDefSysArray
    variable SystemsList
    variable CurSysName
    variable IdxSys
    variable _IdxSys

    variable EnableUserDef

    catch { 
	unset UserDefSysArray
    }
    if { [info commands .central.s] == "" } {
	set LengthInfo     $::Units::ModelUnit
	set UserDefSysList $::Units::OwnerSystem
	set SystemsList    $::Units::SysNames
	if [string compare $::Units::ModelSysName "USER-DEFINED"] {
	    set CurSysName $::Units::ModelSysName
	} else {
	    set CurSysName ""
	}
	set EnableUserDef $::Units::UserDefinedState
    } else {
	set LengthInfo     [.central.s info unitssystems modunit]
	set UserDefSysList [.central.s info unitssystems usersys]
	set SystemsList [::Units::GetUnitsSystems]
	set CurSysName [.central.s info unitssystems prbsys]
	set EnableUserDef [.central.s info unitssystems udstate]
    }
  

    if [llength $UserDefSysList] {
	set bUserDef 1
	BuildSysArray
    }

    if { $CurSysName == "" } {
	set bUserDef 1
	set IdxSys 0
	BuildSysArray
    } else {
	set bUserDef 0
	set IdxSys [lsearch $SystemsList $CurSysName]
    }

    set _UserDefSysList $UserDefSysList
    set _LengthInfo $LengthInfo
    set _bUserDef   $bUserDef
    set _IdxSys     $IdxSys

    if { ![llength $LengthInfo] } {
	if ![llength $SystemsList] {
	    return 0
	}
	set IdxSys 0
	set bUserDef 0
	set sname [lindex $SystemsList 0]
	set CurSysName $sname
	if { [info commands .central.s] == "" } {
	    set lm $::Units::Systems($sname)
	} else {

	    set lm [::Units::GetSystem $sname]
	}

	set il [::Units::FindMagnitud $lm LENGTH]
	set LengthInfo [lindex $lm $il]
    }

    return 1
}

proc ModUnitWin::IsOpen {} {
    variable win

    return [winfo exists $win]
}

proc ModUnitWin::Raise {} {
    variable win

    raise $win
}

proc ModUnitWin::Init { w } {
    if [IsOpen] Raise
    
    variable win $w
    
    return [InitInfoUnits]
}

proc ModUnitWin::Show {} {
    variable win
    variable cb_sys     
    variable f_udef
    variable LengthInfo
    variable SystemsList
    variable IdxSys
    variable EnableUserDef

    if [IsOpen] Raise

    .gid configure -cursor watch

    ::GiDTeXSymbols::ForceLoadFont

    .gid configure -cursor ""

    if { [info commands .central.s] == "" } {
	toplevel $win
    } else {
	InitWindow $win [_ "Problem units"] PrbUnitWindowGeom
    }

    set frame1 [frame $win.frame1]

    label $frame1.l_munit -text [_ "Model Unit: "]
    set mu [MenuUnits::CreateInstance]
    $mu\::Create $frame1.b_munit [lindex $LengthInfo 2] LENGTH [lindex $LengthInfo 1] \
	::ModUnitWin::LengthInfo

    grid $frame1.l_munit -row 0 -column 0 -sticky "w"
    grid $frame1.b_munit -row 0 -column 1 -sticky "w"
    
    TitleFrame $win.f_system -text [_ "Units System"]
    #frame $win.f_system -bd 1 -relief sunken

    set f [$win.f_system getframe]
    #set f $win.f_system
    
    set frame2 [frame $f.frame2]

    label $frame2.l_usys  -text [_ "Base System: "]
    set f_udef [frame $f.f_system -bd 1 -relief sunken]

    if $EnableUserDef {
	checkbutton $f.chk_userdef -text [_ "User defined"] -variable ::ModUnitWin::bUserDef \
	    -command [namespace code CheckUserDef]
    }

    set cb_sys $frame2.c_usys
    ComboBox $cb_sys -editable 0 -values $::ModUnitWin::SystemsList \
	-modifycmd [namespace code OnChangeSystem]

    grid $frame2.l_usys -row 0 -column 0 -sticky "w"
    grid $cb_sys        -row 0 -column 1 -sticky "w"

    $cb_sys setvalue @$IdxSys

    frame $win.fbut -bg [CCColorActivo [$win cget -background]]

    button $win.fbut.b_accept -text [_ "Accept"] -command [namespace code OnAccept]
    button $win.fbut.b_cancel -text [_ "Cancel"] -command [namespace code OnCancel]

    #grid $win.l_munit   -column 0 -row 0 -sticky "w"
    #grid $win.b_munit   -column 1 -row 0 -sticky "w"

    grid $frame1 -row 0 -column 0 -sticky "nw"

    set row 0

    if $EnableUserDef {
	grid $f.chk_userdef -column 0 -row $row -sticky "w"
	incr row
    }
    #grid $f.l_usys      -column 0 -row $row -sticky "w"
    #grid $f.c_usys      -column 1 -row $row -sticky "e"
    grid $frame2 -row $row -column 0 -sticky "w"
    incr row

    grid $f_udef        -column 0 -row $row -sticky "snwe" -pady 2
    grid rowconfigure $f $row -weight 1
    grid columnconfigure $f 0 -weight 1


    grid $win.f_system  -column 0 -row 1 -sticky "snwe"

    grid $win.fbut.b_accept  -column 0 -row 1 -padx 5 -pady 5
    grid $win.fbut.b_cancel  -column 1 -row 1 -padx 5 -pady 5
    grid $win.fbut -column 0 -row 2 -columnspan 2 -sticky snew

    grid rowconfigure $win 1 -weight 1
    grid columnconfigure $win 0 -weight 1

    CheckUserDef
}

proc ModUnitWin::FillFrameSystem {} {
    variable f_udef
    variable UserDefSysArray
    variable UniSysArray
    variable bUserDef

    if $bUserDef {
	upvar \#0 ::ModUnitWin::UserDefSysArray theArray
    } else {
	upvar \#0 ::ModUnitWin::UniSysArray theArray
    }

    foreach i [winfo children $f_udef] {
	grid forget $i
	destroy $i
    }
    set cols 2
    set ccol 0
    set crow 0

    grid rowconfigure $f_udef 0 -weight 0
    foreach i [array names theArray] {
	set uinfo $theArray($i)
	set mag [lindex $uinfo 0]
      if { [lsearch $::Units::MagHidden $mag] != -1 } continue
	set idx [lindex $uinfo 1]
	set tab [lindex $uinfo 2]
	set l [label $f_udef.l_mag$i -text "$mag :"]
	set b $f_udef.b_mag$i
	if $bUserDef {
	    set mu [MenuUnits::CreateInstance]
	    $mu\::Create $b $tab $mag $idx ::ModUnitWin::UserDefSysArray($i)
	} else {
	    canvas $b
	    set lunit [::Units::GetUnit $uinfo]
	    set cgeo [GidDrawUnit [lindex $lunit 3] [lindex $lunit 4] black $b]
	    $b configure -width [lindex $cgeo 0] -height [lindex $cgeo 1]
	}
	grid config $l -row $crow -column [expr $ccol*2]   -sticky "e"
	grid config $b -row $crow -column [expr $ccol*2+1] -sticky "w"

	incr ccol
	if { $ccol == 2 } {
	    set ccol 0
	    incr crow
	    grid rowconfigure $f_udef $crow -weight 0
	}
    }
    incr crow
    grid rowconfigure    $f_udef $crow -weight 1
    grid columnconfigure $f_udef 4     -weight 1
    grid columnconfigure $f_udef  0     -pad    4
    grid columnconfigure $f_udef  2     -pad    4
    
#     foreach i [winfo children $f_udef] {
# 	catch {
# 	    $i conf -state disabled
# 	}
#     }
}

proc ModUnitWin::CheckUserDef {} {
    variable IdxSys
    variable bUserDef
    variable UserDefSysList
    variable UserDefSysArray
    variable SystemsList
    variable f_udef
    variable cb_sys

    if { $bUserDef } {
	#grid config $f_udef

	if { ![llength $UserDefSysList] } {
	    if { $IdxSys != -1 } {
		set UserDefSysList [::Units::GetSystem [lindex $SystemsList $IdxSys]]
		BuildSysArray
	    } else {
		set bUserDef 1
		return
	    }
	}
	#FillFrameSystem
	if { $IdxSys != -1 } {
	    $cb_sys configure -state disabled
	}
    } else {
	#grid remove $f_udef
	set UserDefSysList {}
	catch {
	    unset UserDefSysArray
	}
	if ![info exists UniSysArray] {
	    BuildSysArray
	}
	$cb_sys configure -state normal
    }
    FillFrameSystem
}

proc ModUnitWin::OnChangeSystem {} {
    variable f_udef
    variable IdxSys
    variable cb_sys
    variable SystemsList

    set newidx [$cb_sys getvalue]
    if { $newidx == -1 || $newidx == $IdxSys } {
	return
    }
    set IdxSys $newidx
    BuildSysArray
    FillFrameSystem
}

proc ModUnitWin::BuildUserDefSystem {} {
    variable UserDefSysArray
    variable UserDefSysList {}

    foreach i [array names UserDefSysArray] {
	lappend UserDefSysList $UserDefSysArray($i)
    }
}

proc DumpUserDef {} {
    WarnWin $::ModUnitWin::UserDefSysList
}

proc ModUnitWin::GetSysName {} {
    variable bUserDef
    variable SystemsList
    variable IdxSys

    if { !$bUserDef && $IdxSys != -1 } {
	return [lindex $SystemsList $IdxSys]
    }
    return ""
}

proc ModUnitWin::HasChanged {} {
    variable LengthInfo
    variable _LengthInfo
    variable bUserDef
    variable _bUserDef
    variable UserDefSysList
    variable _UserDefSysList
    variable IdxSys
    variable _IdxSys
    variable CurSysName
    variable SystemsList

    BuildUserDefSystem
    
    if { $IdxSys != -1 } {
	set CurSysName [lindex $SystemsList $IdxSys]
    }

    if { $bUserDef != $_bUserDef } {
	return 1
    }

    if { $LengthInfo != $_LengthInfo } {
	return 1
    }

    if $bUserDef {
	
	if { $_UserDefSysList != $UserDefSysList } {
	    return 1
	}
    } elseif { $IdxSys != $_IdxSys } {
	return 1
    }

    return 0
 }

proc ModUnitWin::OnAccept {} {
    variable win

    if { [HasChanged] } {
	if { [info commands .central.s] == "" } {
	    puts "Los datos cambiaron"
	} else {
	    updmdlunits
	}
    }
    destroy $win
}

proc ModUnitWin::OnCancel {} {
    variable win

    destroy $win
}