
set tms .tms_popup
set tms_display .ventana.editor.tms_display


proc tms_popup { }  {

    global tms tms_display


    	
    #------------------------------------------
    # Create a new window
    #------------------------------------------

    if [winfo exists $tms] {destroy $tms}
    global env
    set env(DISPLAY) :0
    toplevel $tms  -width 65m -height 55m
    wm title $tms "TMS Display"

    #-------------------------------------------
    # Creating Widgets
    #-------------------------------------------

    label $tms.title -text "TMS Display" \
    	-fg blue -relief groove -width 23


    label $tms.xlabel -text x
    entry $tms.x -textvariable tmsx -bg white

    label $tms.ylabel -text y
    entry $tms.y -textvariable tmsy -bg white

    button $tms.add -text Display -command {set_tms_display $tmsx $tmsy 1; display_tms}
    button $tms.remove -text Remove -command {set_tms_display -1 -1 1; destroy $tms_display }
    button $tms.close -text Close -command {destroy $tms}

    place $tms.title -x 10 -y 10
    place $tms.xlabel -x 10 -y 35
    place $tms.ylabel -x 10 -y 55

    place $tms.x -x 30 -y 35
    place $tms.y -x 30 -y 55

    place $tms.add -x 20 -y 85 -width 70
    place $tms.remove -x 100 -y 85 -width 70
    place $tms.close -x 20 -y 115 -width 70



    bind .ventana <ButtonRelease-1> {
    	#global tms
     	set tmsx %x
     	set tmsy %y

    }
}



proc display_tms { } {
 	global tmsx tmsy  tms_display
 	global velocity tether temperature tension pressure torque grd_flt

	
 	if [winfo exists $tms_display] { destroy $tms_display }
 	
 	# Get the x y position
	set tmp_str [split [get_tms_display] | ]
 	set tmsx [lindex $tmp_str 0]
 	set tmsy [lindex $tmp_str 1]
 	
	
 	if {$tmsx == "" || $tmsx == " " } {
 		set tmsx 0
 	}
 	if {$tmsy == "" || $tmsy == " " } {
 		set tmsy 0
 	}
 	
 	if {$tmsx == -1 || $tmsy == -1 } {
 		return
 	}
 	
 	frame $tms_display -width 200 -height 100 -bg black
 	
 	# Cable Speed
 	label $tms_display.velocity -text "Velocity" -bg black -fg white              	
 	entry $tms_display.v_entry -bg black -fg white -textvariable velocity -width 7	
 	label $tms_display.v_unit -text "mtr/min" -bg black -fg white
 	
 	# Cable Out
 	label $tms_display.tether -text "Tether" -bg black -fg white
 	entry $tms_display.tet_entry -bg black -fg white -textvariable tether -width 7	
 	label $tms_display.tet_unit -text "meter" -bg black -fg white
 	 	
 	# Temperature 	
 	#label $tms_display.temp -text "Temp" -bg black -fg white
 	#entry $tms_display.temp_entry -bg black -fg white -textvariable temperature -width 7	
 	#label $tms_display.temp_unit -text "C" -bg black -fg white
 	
 	# Tension 	
 	label $tms_display.tension -text "Tension" -bg black -fg white
 	entry $tms_display.ten_entry -bg black -fg white -textvariable tension -width 7	
 	label $tms_display.ten_unit -text "lbs" -bg black -fg white
 	
 	# Pressure
 	label $tms_display.cyl -text "Pressure" -bg black -fg white
 	entry $tms_display.cyl_entry -bg black -fg white -textvariable pressure -width 7	
 	label $tms_display.cyl_unit -text "psi" -bg black -fg white
 	
 	#label $tms_display.torque -text "Torque" -bg black -fg white
 	#entry $tms_display.tor_entry -bg black -fg white -textvariable torque -width 7	
 	#label $tms_display.tor_unit -text "ft/lbs" -bg black -fg white
 	
 	#label $tms_display.grd -text "GRD FLT" -bg black -fg white
 	#entry $tms_display.grd_entry -bg black -fg white -textvariable grd_flt -width 7	
 	#label $tms_display.grd_unit -text "ohms" -bg black -fg white
 	
 	#--------------------#
 	# Setting Geometry   #
 	#--------------------#
 	
 	
 	place $tms_display.velocity -x 10 -y 10
 	place $tms_display.v_entry -x 80 -y 10
 	place $tms_display.v_unit -x 145 -y 10
 	
 	place $tms_display.tether -x 10 -y 30
 	place $tms_display.tet_entry -x 80 -y 30
 	place $tms_display.tet_unit -x 145 -y 30
 	 	
 	#place $tms_display.temp -x 10 -y 50
 	#place $tms_display.temp_entry -x 80 -y 50
 	#place $tms_display.temp_unit -x 145 -y 50
 	 	
 	place $tms_display.tension -x 10 -y 50
 	place $tms_display.ten_entry -x 80 -y 50
 	place $tms_display.ten_unit -x 145 -y 50
 	
 	place $tms_display.cyl -x 10 -y 70
 	place $tms_display.cyl_entry  -x 80 -y 70
 	place $tms_display.cyl_unit -x 145 -y 70
 	
 	#place $tms_display.torque -x 10 -y 110
 	#place $tms_display.tor_entry -x 80 -y 110
 	#place $tms_display.tor_unit -x 145 -y 110
 	
	#place $tms_display.grd -x 10 -y 130
 	#place $tms_display.grd_entry -x 80 -y 130
 	#place $tms_display.grd_unit -x 145 -y 130
 	
 	place $tms_display -x $tmsx -y $tmsy
 	
 	
 	update_tms
 	
}



proc update_tms { } {
        global tms_display
	global velocity tether temperature tension pressure torque grd_flt

	if ![winfo exists $tms_display] {
	 	return
	}

        set temp_str [get_tms_info]

       	set tether [lindex $temp_str 0]
        set velocity [lindex $temp_str 1]
        set tension [lindex $temp_str 2]
	set pressure [lindex $temp_str 3]
	


 	after 300 [list update_tms]
}
