#! /opt/blt2.3/bin/bltwish -f

set FLXSHOME $env(FLXSHOME)
set pdbPath $FLXSHOME
source $pdbPath/Params/pdb
source $pdbPath/Params/dialog
source $pdbPath/Params/Sdialog

table .

wm title . "Property Data Base Browser"

# creates edit, eval and plot buttons in row0 col0
#**********************************************************************
frame .mbar1 
button .mbar1.edit -text "Edit" -command {edit}
button .mbar1.plot -text "Plot" -command {plot}
button .mbar1.eval -text "Eval" -command {evaluate}
pack .mbar1.edit .mbar1.eval .mbar1.plot -side left -padx 1 -fill x -expand 1

table . \
  .mbar1 0,0
table configure . .mbar1 -anchor w -padx 3 -pady 1 -fill x
table configure . r0 -height 0.5i -resize none
#***********************************************************************

# creates the home, back and forward buttons in row0 col1
#********************************************************************** 
frame .mbar2
pack .mbar2
  
    button .mbar2.home -text "Home" -command {home}
    button .mbar2.back -text "Back" -command {backlevel}
    button .mbar2.forwrd -text "Forward" -command {enterlevel}
  pack .mbar2.home .mbar2.back .mbar2.forwrd -side left -padx 1 -fill x -expand 1
  
    button .mbar2.help -text "Help"
    button .mbar2.quit -text "Quit" -command {exit}
  pack .mbar2.help .mbar2.quit -side left -padx 1 


table . \
  .mbar2 0,1
table configure . .mbar2 -anchor e -fill x 
#**********************************************************************

# creates the listbox and scroll bar in row1 col0
#**********************************************************************
frame .box
listbox .box.lbox -yscroll ".box.vscroll set" -relief sunken -width 25 -height 35 -setgrid yes -selectmode single
scrollbar .box.vscroll -command ".box.lbox yview"
pack .box.lbox -side left
pack .box.vscroll -side left -fill y

table . \
  .box 1,0 
table configure . .box -padx 3 -pady 2
table configure . r1 -height 6i -resize none
table configure . .box -reqheight {6i 8i}
#**********************************************************************


#create a frame for the status display and graph
#**********************************************************************
frame .c  
pack .c 
#**********************************************************************


# creates the arrayname, value, evaluated value, and type display
#**********************************************************************
frame .c.status 
	
	frame .c.status.array 
	label .c.status.array.array -text "Array: " 
	label .c.status.array.arrayname -textvariable ArrayName -width 45 -anchor w
	pack .c.status.array.array -side left -fill x
	pack .c.status.array.arrayname -side left -fill x 
	
	frame .c.status.type
	label .c.status.type.type -text "Type: "
	label .c.status.type.typename -textvariable TypeName -width 35 -anchor w
	pack .c.status.type.type -side left -anchor w -fill x
	pack .c.status.type.typename -side left -fill x 
	
	frame .c.status.value 
	label .c.status.value.value -text "Value: "
	message .c.status.value.valuename -textvariable ValueName -width 7c -anchor w
	pack .c.status.value.value -side left -fill x
	pack .c.status.value.valuename -side left -fill x 
	
	frame .c.status.evalue
	label .c.status.evalue.evalue -text "Evaluated Value : "
	message .c.status.evalue.evaluename -textvariable EvalVal -width 7c -anchor w
	pack .c.status.evalue.evalue -side left
	pack .c.status.evalue.evaluename -side left -fill x
	
	pack .c.status.array .c.status.type .c.status.value .c.status.evalue -anchor w

pack .c.status -anchor nw -expand 1
#**********************************************************************	

#creates the graph 
#**********************************************************************	

  graph .c.graph -plotbackground black 
  vector xVec yVec
    xVec set {700 700 700 700 700}
    yVec set {0 0 0 0 0}
    .c.graph axis configure y -logscale yes
    .c.graph axis configure x -title "1/Temp" -titlecolor white -titlefont Helvetica
    .c.graph axis configure y -title "log value" -titlecolor white -titlefont Helvetica
 pack .c.graph -anchor s
#**********************************************************************	
 
#put frame of status display and graph in col1 row1 of the table
#**********************************************************************	    
table . \
  .c 1,1
table configure . .c -anchor nw -pady 2 -fill both -padx 2
table configure . c1 -resize none
#**********************************************************************	


# initiallization of the commands
#**********************************************************************
set ArrayName "Params"
__pdbReadParam Params ; #read the params directory, create one if it does not exsist
#**********************************************************************


#fill in the list box with top level commands
#**********************************************************************
proc __pdbFillList {Arr} {
  global $Arr
  .box.lbox delete 0 end
  set sid [array startsearch $Arr]
  while {[array anymore $Arr $sid]} {
    set val [array nextelement $Arr $sid]
    if { [string compare $val Modify] } {.box.lbox insert end $val}
  }
  array donesearch $Arr $sid
}
#**********************************************************************

__pdbFillList Params ; #fill in the list box
 

#define bindings for the list box
#**********************************************************************
bind .box.lbox <ButtonPress> {
  global TypeName ValueName EvalVal ArrayName IsArray
  regsub -all " " $ArrayName {\&} Arr
  global $Arr
  
  #where was the key pressed
  set y %y
  set indx [.box.lbox nearest $y]
  .box.lbox selection set $indx
  
  #get the selection point
  set point [.box.lbox get $indx]
  
  #get the current array value
  set tmp [lindex [array get $Arr $point] 1]
  
  #if the selected item contains more than 1 element  
  if {[llength $tmp] !=1} {
    set TypeName [lindex $tmp 0]
    set ValueName [lindex $tmp 1]
    set EvalVal ""
    .mbar2.forwrd configure -state disabled
    
    if {$TypeName == "Switch" || $TypeName =="Boolean"} {
    .mbar1.plot configure -state disabled
    } else {
              .mbar1.plot configure -state normal
      }
    
  }  else {
             set TypeName Array
             set ValueName $tmp
             set EvalVal ""
             .mbar2.forwrd configure -state normal
     	     .mbar1.plot configure -state disabled
     }
  
}
#**********************************************************************


#function for the enter button
#**********************************************************************
proc enterlevel {} {
  global ArrayName EvalVal ValueName TypeName
  
  
  #get the selected array name
  regsub -all " " $ArrayName {\&} Arr
  set Array ${Arr}&$ValueName
  global $Array
  
  
  #change the display variable
  set ArrayName "$ArrayName $ValueName"
  
  #if the array has not been read, then do so
  if {![array exists $Array]} {__pdbReadParam $Array}
  __pdbFillList $Array
  
   set error [.c.graph element exist line1]
  
  if { $error==1} {
      .c.graph element delete line1
  }
  
  .mbar2.back configure -state normal
  set TypeName ""
  set ValueName ""
  set EvalVal ""
}
#**********************************************************************


#function for the back button
#**********************************************************************
proc backlevel {} {
  global ArrayName TypeName EvalVal ValueName
  
  #get the selected array name
  set depth [regsub -all " " $ArrayName {\&} Arr]
  
  if {$depth==0} {
    __pdbFillList Params
    .mbar2.back configure -state disabled
  } else {
            set ArrayName [lrange $ArrayName 0 [expr [llength $ArrayName]-2]]
            set depth [regsub -all " " $ArrayName {\&} Arr]
            __pdbFillList $Arr
    }
  
  set error [.c.graph element exist line1]
  
  if { $error==1} {
      .c.graph element delete line1
  }
    
  set TypeName ""
  set ValueName ""
  set EvalVal ""
}
#**********************************************************************
  

#function for the home button
#**********************************************************************
proc home {} {
  global ArrayName TypeName EvalVal ValueName
  __pdbFillList Params ; #fill in the list box
  
  set ArrayName "Params"
  set TypeName ""
  set ValueName ""
  set EvalVal ""
  
  .mbar2.back configure -state disabled
  
  set error [.c.graph element exist line1]
  
  if { $error==1} {
      .c.graph element delete line1
  }
}
#**********************************************************************


#function for the Eval button
#**********************************************************************
proc evaluate {} {
  global EvalVal ValueName
  
  set EvalVal "[expr $ValueName]"
}
#**********************************************************************


#function for the edit button
#**********************************************************************
proc edit {} {
  global ArrayName TypeName ValueName OldValue point
  
  if { $TypeName =="Array"} {
    tk_dialog .diag {Warning!} {Only Double and Switch type values can be changed.} {} {0} {Ok}
  }
  
  if { $TypeName =="Double"} {
    dialog  {Double Edit Box} {Type in the new value}
    set OldValue $ValueName
  } 
  
  if { $TypeName =="Switch"} {
    Sdialog
    regsub -all " " $ArrayName {\&} Arr
    global $Arr
    set tmp [lindex [array get $Arr $point] 1]
    set tmp2 [lindex $tmp 2]
    set OldValue [lindex $tmp2 $ValueName]
  }
  

}
#**********************************************************************

#function for the plot button
#**********************************************************************
proc plot {} {
  global ArrayName TypeName ValueName Diffuse
  global xVec yVec

  puts "$ArrayName"
  puts "$TypeName"
  puts "$ValueName"
  
  if { $TypeName =="Double"} {
    
     .mbar1.plot configure -state normal
    set error [.c.graph element exist line1]
    
    if { $error==1} {
      .c.graph element delete line1
    }
        
    set i 0  

    foreach tmp {700.0 800.0 900.0 1000.0 1100.0} {
      simSetDouble Diffuse temp $tmp
      set v [evaluate]
      set T [expr 1 / $tmp]
      set xVec($i) $T
      set yVec($i) $v
      incr i
    }

    .c.graph element create line1 -label "" -xdata xVec -ydata yVec -symbol circle -pixel 3  -color red

  }
}
#**********************************************************************
