
#########################################
# Create the procedures for the "File" menu         #
#########################################
source but_small_popup.tcl
source but_small_popup2.tcl
source slidebar_popup.tcl
source slidebar_popup2.tcl
source light_menu.tcl
source light_menu2.tcl

source compass_popup.tcl
source compass_popup2.tcl
source vehicle_popup.tcl
source elem_prop.tcl
source elem_prop2.tcl
source depthbar.tcl
source depthbar2.tcl
#source bar_detail.tcl
#source bar_detail2.tcl
source bar.tcl
source bar2.tcl
source annotate_box.tcl
source annotate_box.tcl
source overlay.tcl
source overlay2.tcl
source tms_popup.tcl
source tms_popup2.tcl
source submenu/log_field.tcl
source submenu/log_field2.tcl



#-----------------------------------
# Initialize variables
#-----------------------------------

set filename ""
set backup_filename ""
set just_saved 1
set vefile .ventana.editor


#-----------------------------------
# This procedure will be called when we want to
# create a new file
#-----------------------------------
proc newFile { } {
 	

	destroy_all_graphic

      	destroy .ventana
      	destroy .ventana2
 	
	global env
	
	set env(DISPLAY) :0.1
 	source test2.tcl
	set env(DISPLAY) :0
 	source test.tcl
 	
 	# Delete every object in database
	clean_up_screen


	
}




#-----------------------------------
# This procedure will be called when we want to
# Open a new file
#-----------------------------------

set typelist {
	{{Config Files} {.cfg}}
	{{All Files} {.*}}
}

proc openFile {} {
   global filename backup_filename
   global just_saved
   global typelist

   if { $just_saved == 1 } {


     set filename [tk_getOpenFile -initialdir ./config -filetypes $typelist ]
     	
      # If the user press "Cancel"
      if {$filename == "" } {
       	return
      }

      # Destroying all graphic
      destroy_all_graphic
      clean_up_screen
      opening $filename

      destroy .ventana
      destroy .ventana2

      global env
      set env(DISPLAY) :0.1	
      source test2.tcl
      set env(DISPLAY) :0
      source test.tcl



      display_all_graphic




   } else {

     set status [save_config_or_not]
     puts "status is $status"
     switch $status {
     
 	 1 {saveFile $filename}
      	 2 {
      	 	set filename [tk_getOpenFile -initialdir ./config -filetypes $typelist]
      	 		
      	 	destroy_all_graphic
      	 	clean_up_screen
      	     	opening $filename
      	     	
      		destroy .ventana
      		destroy .ventana2
      		global env
      		set env(DISPLAY) :0.1	
      		source test2.tcl
      		set env(DISPLAY) :0
      		source test.tcl

      		for {set i  10000} {$i > 0} {incr i -1} {
      			puts "counting down : $i"
      		}

      	     	display_all_graphic
     	
      	
  	   	set just_saved 0
  	   }
  	
      	 #{puts "NOT OPENING FILE"; return}
      }

  # end else
  }

# end of proc "openFile"
}


proc saveFile {} {

   global fileNameExt
   global filename  backup_filename
   global just_saved
   global typelist
   global justExt
   # Kenny - Nov 18
   global splitFile	
   global fileSplit

   if { $filename != "" } {

     #--------------------------------------------------------------
      #exec [mv $filename $filename.bak.cfg]
     #puts "copying to file $filename.bak.cfg"

     #---------------------------------------------------------------

     saving $filename
     return
   } else {
   	set fileNameExt ""
   	while { $fileNameExt == "" } {
   		
   		
     		set fileNameExt [tk_getSaveFile -initialdir \
				 ./config -filetypes $typelist \
				 -defaultextension ".cfg"]
		
		
     		#-----------------------------------------------
     		# do some error checking, check for duplicates
		# Kenny - added the following Nov.18
     		#-----------------------------------------------
               
		set splitFile $fileNameExt
		set fileSplit [file split $splitFile]

                # invariant: doubleClk is 0
                set doubleClk 0
		set searchSplit $fileSplit

		puts "======================"	
		foreach x $fileSplit {
     			set searchSplit [lreplace $searchSplit 0 0]	
	           	set ix [lsearch -exact $searchSplit $x]
                	#puts $searchSplit 
			#puts $x
			set doubleClk 0
                	if {$ix >= 0} {
				puts $x
                		set doubleClk 1
                		break	
                	}
                	
                }
		puts "======================"	
                
		if {$doubleClk == 1} {
                	puts "double click"
			set fileNameExt ""
			set fileName ""	
			continue
                }


	
		
		if {$fileNameExt == ""} {
			return
		}

     		set justExt [file extension $fileNameExt]
 
    		if {($justExt != ".cfg") && ($justExt != "")} {
        		set statusAck [notCfgExt]
    			switch $statusAck {
       				1 {set fileNameExt ""
				   continue
				  } 
       				2 return
     			}
		}
		
	}

    }

     
    if {$justExt == ".cfg"} {
      set filename $fileNameExt
    }

   if { $filename != "" } {

   #----------------------------------- Adding code here -----------------------
    #mv $filename $filename.bak.cfg
     #puts "copying to file $filename.bak.cfg"

     #----------------------------------------------------------------------------

     saving $filename
     set just_saved 1
   }





}


proc saveAs {} {
  global filename
  global typelist



  set filename ""
  saveFile

  set just_saved 1

}


proc exit_program { } {

  global just_saved

  set just_saved 0
  if {  $just_saved != 1 } {
     set status [save_config_or_not]
     switch $status {
       1 saveFile
       2 exit
       3 return
     }
  } else {
  	exit
  }


}


proc notCfgExt { } {
   global buttonPressed
   global vefile

# Build "Would you like to save your configuration?" dialog
# Toplevel
    toplevel .wrong_ext -class Dialog
    wm title .wrong_ext "NOT a ventana .cfg extension"
    wm geometry .wrong_ext 275x100

# Buttons
    button .wrong_ext.ackBtn1 \
	    -padx 10 -pady 3 -text {Acknowledge} \
	    -command {set buttonPressed 1}
    button .wrong_ext.ackBtn2 \
	    -padx 10 -pady 3 -text {Cancel} -command {set buttonPressed 2}

# Message
    message .wrong_ext.badMessage \
        -borderwidth 1 -padx 10 -pady 3 \
        -justify center -width 7c \
        -text {The extension you have entered is invalid. Ventana configuration files must be .cfg}

# Set the geometry of this dialog box
    pack .wrong_ext.badMessage -side top -fill x -expand 1 \
         -padx 2m -pady 2m
    pack .wrong_ext.ackBtn1 \
	-side left -padx 2m -pady 2m

    pack .wrong_ext.ackBtn2 \
	-side right -padx 2m -pady 2m


    bind .wrong_ext <Return> \
        { .wrong_ext.ackBtn1 flash; set buttonPressed 1 }


# Shift the control to this dialog box.

    set oldFocus [focus]
    focus .wrong_ext

# Register the user's reply.

    tkwait variable buttonPressed

# Destroy it if the user has not cancel it.

    destroy .wrong_ext
    focus $oldFocus


# Return the user's reply.

    return $buttonPressed

}



proc save_config_or_not { } {
   global buttonPressed
   global vefile
   global env
   set env(DISPLAY) :0

# Toplevel
    toplevel .save_or_not

    wm title .save_or_not "Quit The Program ?"
    wm geometry .save_or_not 275x120+200+200

# Buttons

    button .save_or_not.saveConfBtn \
	    -text Save -command {set buttonPressed 1} \
	    -width 5 -height 3

    button .save_or_not.doNotSaveBtn \
	    -text {Exit} -command {set buttonPressed 2} \
	    -width 5 -height 3

    button .save_or_not.cancelBtn \
        -text Cancel -command {set buttonPressed 3} \
        -width 5 -height 3
# Message

    message .save_or_not.saveMessage \
        -borderwidth 1 \
        -justify center -width 7c \
        -text {Are you sure you want to quit ? Would you like to save your configuration?}

# Set the geometry of this dialog box

    pack .save_or_not.saveMessage -side top  \
         -padx 2m -pady 2m
    pack .save_or_not.saveConfBtn \
         .save_or_not.doNotSaveBtn \
         .save_or_not.cancelBtn -side left -padx 5m -pady 2m

    bind .save_or_not <Return> \
        { .save_or_not.saveConfBtn flash; set buttonPressed 1 }


# Shift the control to this dialog box.

    set oldFocus [focus]
    #grab set .save_or_not
    focus .save_or_not

# Register the user's reply.

    tkwait variable buttonPressed

# Destroy it if the user has not cancel it.

    destroy .save_or_not
    focus $oldFocus


# Return the user's reply.

    return $buttonPressed

}




proc display_all_graphic { } {

	display_button
	display_slidebar
   	display_light
      	display_compass
      	display_vehicle_graphic
      	display_bar_and_depth
      	display_camera_values      	
      	display_arm_values      	
      	display_auto_pilot_values
        display_tms
        display_tms2



      #----------------------------------------------------------#
      # Recreate the sub output channel #49 -#64 again           #
      # becauuse when we open a file, the element get destroyed  #
      #----------------------------------------------------------#

      subout_make

      #-------------------------------------------------#
      # Set the status of polling to be true, so       	#
      # we can get updated digital channels when       	#
      # we reestablish telemetry			#
      #-------------------------------------------------#
  	stop_poll

}

proc display_button { } {                       	
 	
	set bcount [get_button_count]
	
	for { set i 0 } { $i < $bcount } { incr i } {
	 	
	 	global but_name      	 	
   		global sp_xentry
		global sp_yentry      		
             	global color
             	global varname
             	global clicked
             	global vbut
  		global conn_name
             	global state
             	global varname
             	global board
             	global elem_num                   	
             	global but_name2
             	global conn_name2
	 	global sp_xentry2
		global sp_yentry2
	 	global varname2
	 	global color2           	                                                     	 	
	 	 	
	 	set bstring [get_button_string $i]	 	
	 	set binfo [split $bstring |]                                                          	 	
	 	set but_name [lindex $binfo 0]
	 	set varname [lindex $binfo 1]
	 	set board [lindex $binfo 2]
	 	set elem_num [lindex $binfo 3]
		set sp_xentry [lindex $binfo 4]
	 	set sp_yentry [lindex $binfo 5]
	 	set color [lindex $binfo 6]
	 	set state 0
	 	set sp_xentry2 [lindex $binfo 8]
	 	set sp_yentry2 [lindex $binfo 9]
	 	set but_name2 [lindex $binfo 0]
	 	set varname2 [lindex $binfo 1]
	 	set board2 [lindex $binfo 2]
	 	set elem_num2 [lindex $binfo 3]
	 	set color2 [lindex $binfo 6]
	 	
	 	#puts "--------for i $i button string is $bstring-----------" 	
	 	#puts "but_name $but_name"
	 	#puts "type $varname"
	 	#puts "board $board"
	 	#puts "elem_num $elem_num"
	 	#puts "x2 $sp_xentry2"
	 	#puts "y2 $sp_yentry2"
	 	#puts "state $state"
	 	
	 	if { $board == 0 } {
	 		set conn_name "Dig Gesout $elem_num"
	 		set conn_name2 $conn_name
	 	}
	 	
	 	if { $board == 1 } {
	 		set conn_name "Dig MPL $elem_num"
	 		set conn_name2 $conn_name
	 	}
	 	
	 	if {$sp_xentry != -1 || $sp_yentry != -1}  {
	 		global $conn_name
	 		set $conn_name $state
	 		
	 		#-------------------------------# 	
	 		# Display it on main screen   	#
	 		#-------------------------------# 	
	 		
	 		but_small_popup
			button_add_output
			button_list_output $conn_name
			creating_button                              				
			#add_button
			destroy .but_small_popup
		}
		
		
		 if {$sp_xentry2 != -1 || $sp_yentry2 != -1}  {
	 	
			global $conn_name2
			set $conn_name2 $state
	 	
	 	        #-------------------------------# 	
	 		# Display it on second screen   #
	 		#-------------------------------# 	
	 		
			but_small_popup2                    	 		
			button_add_output2
			button_list_output2 $conn_name2
			creating_button2                            				
			#add_button2
			destroy .but_small_popup2
   		}
	 	
	 	
	 	
	}
 	
}


proc display_slidebar { } {
 	
 	#--------------------------------------
 	# SLIDEBAR
 	#--------------------------------------
 	
	# slidebar count
	set scount [get_slidebar_count]
	#puts "slide count is $scount"
	for { set i 0 } { $i < $scount } { incr i } {
	 	global slide_name
   		global slidex
		global slidey
             	global color                	
             	global vsl
              	global value
             	global slide_board
             	global slide_elem_num
             	global slide_conn                                                     	
	 	global slide2_name
   		global slide2x
		global slide2y
             	global color2               	
             	global v2sl                                                                  	
             	global slide2_board
             	global slide2_elem_num
             	global slide2_conn                                                    	
             	
	 	set sstring [get_slidebar_string $i]                   	 	
	 	#puts "--------for i $i slidebar string is $sstring-----------"
	 	set sinfo [split $sstring |]                                	 	
	 	set slide_name [lindex $sinfo 0] 	 	
	 	set slide_board [lindex $sinfo 1]
	 	set slide_elem_num [lindex $sinfo 2]
	 	set value 0
		set slidex [lindex $sinfo 4]
	 	set slidey [lindex $sinfo 5]
	 	set color [lindex $sinfo 6]                              	 	
	 	set slide2_name [lindex $sinfo 0] 	 	
	 	set slide2_board [lindex $sinfo 1]
	 	set slide2_elem_num [lindex $sinfo 2]
	 	set color2 [lindex $sinfo 6]
		set slide2x [lindex $sinfo 7]
	 	set slide2y [lindex $sinfo 8]
	 	
	 	# slide_board and slide2_board will always be 2 -> Ana Gesdac
	 	set slide_conn "Ana Gesdac $slide_elem_num"
	 	set slide2_conn $slide_conn
	 	
	 	#puts "file_proc.tcl : printing out the value : $value"	 	
	 	if { $slidex != -1 || $slidey != -1 } {
	 		global $slide_conn
	 		set $slide_conn $value
	 	 	
	 		# display it on main screen
	 		slidebar_popup
			slide_add_output
			slide_list_output $slide_conn
		
			# Create the actual button
			creating_slidebar                            			
			destroy .slide_popup
   		}
   		
   		if { $slide2x != -1 || $slide2y != -1 } {
	 		global $slide2_conn
	 		set $slide2_conn $value
	 	 	
	 		# display it on main screen
	 		slidebar_popup2
			slide_add_output2
			slide_list_output2 $slide2_conn
		
			# Create the actual button
			creating_slidebar2                          			
			destroy .slide_popup2
   		}         	                                                    	 	
	}
}




proc display_light { } {

        global light_out
        global light_xpos  light_ypos
	global light2_xpos  light2_ypos                                   	
	global xl1 xl2 xl3 xl4 xl5 xl6 xl7 xl8 xl9 xl10
	global yl1 yl2 yl3 yl4 yl5 yl6 yl7 yl8 yl9 yl10
        global x2l1 x2l2 x2l3 x2l4 x2l5 x2l6 x2l7 x2l8 x2l9 x2l10
	global y2l1 y2l2 y2l3 y2l4 y2l5 y2l6 y2l7 y2l8 y2l9 y2l10

	#----------------
	# Get Connection
	#----------------
	
	set conn [split [get_light_conn_db] | ]
	
	set light_out(1) "Digital [lindex $conn 0]"   	
	set light_out(2) "Digital [lindex $conn 1]"
	set light_out(3) "Digital [lindex $conn 2]"
	set light_out(4) "Digital [lindex $conn 3]"
	set light_out(5) "Digital [lindex $conn 4]"
	set light_out(6) "Digital [lindex $conn 5]"
	set light_out(7) "Digital [lindex $conn 6]"
	set light_out(8) "Digital [lindex $conn 7]"
	set light_out(9) "Digital [lindex $conn 8]"
	set light_out(10) "Digital [lindex $conn 9]"
	
	
	# ----------------- SCREEN 1 ---------------------
 	set temp1 [ split [get_light_string 1] | ]                            	
 	set light_xpos [lindex $temp1 0 ]
 	set light_ypos [lindex $temp1 1 ]
 	set xl1  [lindex $temp1 2 ]
 	set xl2  [lindex $temp1 3 ]
 	set xl3  [lindex $temp1 4 ]
 	set xl4  [lindex $temp1 5 ]
 	set xl5  [lindex $temp1 6 ]
 	set xl6  [lindex $temp1 7 ]
 	set xl7  [lindex $temp1 8 ]
 	set xl8  [lindex $temp1 9 ]
 	set xl9  [lindex $temp1 10 ]
 	set xl10  [lindex $temp1 11 ]
 	set yl1  [lindex $temp1 12 ]
 	set yl2  [lindex $temp1 13 ]
 	set yl3  [lindex $temp1 14 ]
 	set yl4  [lindex $temp1 15 ]
 	set yl5  [lindex $temp1 16 ]
 	set yl6  [lindex $temp1 17 ]
 	set yl7  [lindex $temp1 18 ]
 	set yl8  [lindex $temp1 19 ]
 	set yl9  [lindex $temp1 20 ]
 	set yl10  [lindex $temp1 21 ]
 	
 	if { $light_xpos != -1 || $light_ypos != -1 } {
 	
 		set_light_pos $light_xpos $light_ypos
             	light $xl1 $xl2 $xl3 $xl4 $xl5 $xl6 $xl7 $xl8 $xl9 $xl10 $yl1 $yl2 $yl3 $yl4 $yl5 $yl6 $yl7 $yl8 $yl9 $yl10
           }

        # ----------------- SCREEN 2 ---------------------
 	
 	set temp2 [ split [get_light_string 2] | ]                            	
 	set light2_xpos [lindex $temp2 0 ]
 	set light2_ypos [lindex $temp2 1 ]
 	set x2l1  [lindex $temp2 2 ]
 	set x2l2  [lindex $temp2 3 ]
 	set x2l3  [lindex $temp2 4 ]
 	set x2l4  [lindex $temp2 5 ]
 	set x2l5  [lindex $temp2 6 ]
 	set x2l6  [lindex $temp2 7 ]
 	set x2l7  [lindex $temp2 8 ]
 	set x2l8  [lindex $temp2 9 ]
 	set x2l9  [lindex $temp2 10 ]
 	set x2l10  [lindex $temp2 11 ]
 	set y2l1  [lindex $temp2 12 ]
 	set y2l2  [lindex $temp2 13 ]
 	set y2l3  [lindex $temp2 14 ]
 	set y2l4  [lindex $temp2 15 ]
 	set y2l5  [lindex $temp2 16 ]
 	set y2l6  [lindex $temp2 17 ]
 	set y2l7  [lindex $temp2 18 ]
 	set y2l8  [lindex $temp2 19 ]
 	set y2l9  [lindex $temp2 20 ]
 	set y2l10  [lindex $temp2 21 ]
 	
 	if { $light2_xpos != -1 || $light2_ypos != -1 } {
 	
 		set_light2_pos $light2_xpos $light2_ypos
             	light2 $x2l1 $x2l2 $x2l3 $x2l4 $x2l5 $x2l6 $x2l7 $x2l8 $x2l9 $x2l10 $y2l1 $y2l2 $y2l3 $y2l4 $y2l5 $y2l6 $y2l7 $y2l8 $y2l9 $y2l10
             }
}


proc display_compass { } {

 	global  compass_xpos compass_ypos  compass_type compass_type2
 	global  compass2_xpos compass2_ypos
 	
 	
 	set temp [ split [get_compass_string] | ]
 	
 	                 	
 	#------------------------------#
 	# For Heading comfing from "FOG"           #
 	#------------------------------#
 	set compass_xpos [lindex $temp 0 ]
 	set compass_ypos [lindex $temp 1 ]
 	set compass2_xpos  [lindex $temp 2 ]
 	set compass2_ypos  [lindex $temp 3 ]
 	
 	# Both compass on screen 1 and 2 (if displayed on both)
 	# are the same type
 	 	
 	set compass_type  [lindex $temp 4 ]
 	#puts "file_proc.tcl: What's the compass type here : $compass_type"
 	
        if { $compass_xpos != -1 || $compass_ypos != -1 } {
              	compass_popup
              	add_compass
              	destroy .compass_popup
        }                                   	

        if { $compass2_xpos != -1 || $compass2_ypos != -1 } {             	
              	compass2_popup
              	add_compass2
              	destroy .compass2_popup
        }
 	

        #------------------------------#
 	# For Heading comfing from "BACKUP"    #
 	#------------------------------#
 	set compass_xpos [lindex $temp 5 ]
 	set compass_ypos [lindex $temp 6 ]
 	set compass2_xpos  [lindex $temp 7 ]
 	set compass2_ypos  [lindex $temp 8 ]
 	
 	
 	set compass_type2  [lindex $temp 9 ]
# 	puts "file_proc.tcl: What's the compass type here : $compass_type2"
 	
             if { $compass_xpos != -1 || $compass_ypos != -1 } {
              	compass_popup
              	add_compass
              	destroy .compass_popup
             }                                   	

             if { $compass2_xpos != -1 || $compass2_ypos != -1 } {             	
              	compass2_popup
              	add_compass2
              	destroy .compass2_popup
             }
}
 	

proc display_vehicle_graphic { } {

 	global  vehicle_xpos vehicle_ypos
 	global  vehicle2_xpos vehicle2_ypos
 	
 	set temp [ split [get_vehicle_string] | ]                         	
 	set vehicle_xpos [lindex $temp 0 ]
 	set vehicle_ypos [lindex $temp 1 ]
 	set vehicle2_xpos  [lindex $temp 2 ]
 	set vehicle2_ypos  [lindex $temp 3 ]
 	
             if { $vehicle_xpos != -1 || $vehicle_ypos != -1 } {
             	vehicle_popup
             	add_vehicle
             	destroy .vehicle_popup
             }                                   	

             if { $vehicle2_xpos != -1 || $vehicle2_ypos != -1 } {
             	vehicle2_popup
             	add_vehicle2
             	destroy .vehicle2_popup
             	
             }
 	
}

proc display_bar_and_depth { } {
 	
	global graph_type graph_type2
	global xpos ypos xpos2 ypos2
	global barx bary
	global bar_min bar_max bar_average bar_yrange bar_rrange
   	global bar2x bar2y
	global bar_min2 bar_max2 bar_average2 bar_yrange2 bar_rrange2

	set count [retSubAnaSize]
	for {set i 0} { $i < $count } { incr i } {
		 	
	 	set temp [ split [get_graph_info $i] | ]
	 	
	 	set graph_type [lindex $temp 0]
	 	set xpos [lindex $temp 1]
	 	set ypos [lindex $temp 2]
	 	set graph_type2 [lindex $temp 3]
	 	set xpos2 [lindex $temp 4]
	 	set ypos2 [lindex $temp 5]
	 	set elem_num [lindex $temp 6]
	 	set name [get_element_name 0 0 $elem_num]
	 		 	
	 	#puts "***************** elem_num is $elem_num *****************"
	 	
	 	#puts " graph type is $graph_type *****************"
	 	#puts "graph type2 is $graph_type2 *****************"
	 			
	 	if {$graph_type == "" || $graph_type == " " } {
	 	 	set graph_type "None"
	 	}
	 	
	 	if {$graph_type2 == "" || $graph_type2 == " " } {
	 	 	set graph_type2 "None"
	 	}
	 	
	 	
	 	if { $graph_type == "Bar" } {
	 		# caling c++ function to get the bar detail
	 		set bar_info [split [getting_bar_detail $elem_num] | ]
	 		# Index start from 1 not 0 because index 0 is the element number
	 		
			set bar_min($elem_num) [lindex $bar_info 1]
	 		set bar_max($elem_num) [lindex $bar_info 2]
	 		set bar_average($elem_num) [lindex $bar_info 3]
	 		set bar_yrange($elem_num) [lindex $bar_info 4]
	 		set bar_rrange($elem_num) [lindex $bar_info 5]
	 	          	 	
	 		# Display in on first screen and put it in database
	 		set barx($elem_num) $xpos
			set bary($elem_num) $ypos	
	 		display_bar_or_not 1 $elem_num
	 	}
	 	if { $graph_type2 == "Bar" } {
			# caling c++ function to get the bar detail
			set bar_info [split [getting_bar_detail $elem_num] | ]
	 		# Index start from 1 not 0 because index 0 is the element number
	 		set bar_min2($elem_num) [lindex $bar_info 1]
	 		set bar_max2($elem_num) [lindex $bar_info 2]
	 		set bar_average2($elem_num) [lindex $bar_info 3]
	 		set bar_yrange2($elem_num) [lindex $bar_info 4]
	 		set bar_rrange2($elem_num) [lindex $bar_info 5]
	 	
	 		
	 		# Display in on second screen and put it in database
			set bar2x($elem_num) $xpos2
			set bar2y($elem_num) $ypos2	
 			display_bar2_or_not 1 $elem_num
		} else {
	 		check_graph_type 0 0 $graph_type $xpos $ypos $name $elem_num
	 	 	check_graph_type2 0 0 $graph_type2 $xpos2 $ypos2 $name $elem_num
	 		destroy .bar_detail
			destroy .bar_detail2
	 	}  	  	                                           	 		 	
	 	                                    	 	
	}
	
}


proc display_camera_values { } { 	

    	global cam_shoulder_sine cam_shoulder_cosine cam_shoulder_offset cam_shoulder_command cam_shoulder_master_value cam_shoulder_fb_value cam_shoulder_servo cam_p_shoulder cam_i_shoulder cam_d_shoulder
    	global cam_pan_sine cam_pan_cosine cam_pan_offset cam_pan_command cam_pan_master_value cam_pan_fb_value cam_pan_servo cam_p_pan cam_i_pan cam_d_pan
    	global cam_tilt_sine cam_tilt_cosine cam_tilt_offset cam_tilt_command cam_tilt_master_value cam_tilt_fb_value cam_tilt_servo cam_p_tilt cam_i_tilt cam_d_tilt
	
     	set str [split [loading_cam_string] |]
     	#puts "file_proc.tcl : str : $str"
        if {$str == ""} { return }
	    	
	set cam_shoulder_sine "Analog Input [lindex $str 0]"
	set cam_shoulder_cosine "Analog Input [lindex $str 1]"
	set cam_shoulder_offset [lindex $str 2]	
	set cam_shoulder_command "Analog Input [lindex $str 3]"
	#set cam_shoulder_master_value [lindex $str 4]
	#set cam_shoulder_fb_value [lindex $str 5]
	set cam_shoulder_servo "Analog Output [lindex $str 6]"
	set cam_p_shoulder [lindex $str 7]
	set cam_i_shoulder [lindex $str 8]
	set cam_d_shoulder [lindex $str 9]

	set cam_pan_sine "Analog Input [lindex $str 10]"
	set cam_pan_cosine "Analog Input [lindex $str 11]"
	set cam_pan_offset [lindex $str 12]
	set cam_pan_command "Analog Input [lindex $str 13]"
	#set cam_pan_master_value [lindex $str 14]
	#set cam_pan_fb_value [lindex $str 15]	
	set cam_pan_servo "Analog Output [lindex $str 16]"
	set cam_p_pan  [lindex $str 17]
	set cam_i_pan  [lindex $str 18]
	set cam_d_pan  [lindex $str 19]

	set cam_tilt_sine "Analog Input [lindex $str 20]"
	set cam_tilt_cosine "Analog Input [lindex $str 21]"
	set cam_tilt_offset [lindex $str 22]
	set cam_tilt_command "Analog Input [lindex $str 23]"
	#set cam_tilt_master_value [lindex $str 24]
	#set cam_tilt_fb_value [lindex $str 25]
	set cam_tilt_servo "Analog Output [lindex $str 26]"
	set cam_p_tilt [lindex $str 27]
	set cam_i_tilt [lindex $str 28]
	set cam_d_tilt [lindex $str 29]
}


proc display_arm_values { } { 	

	global shoulder_sine shoulder_cosine shoulder_offset shoulder_command shoulder_master_value shoulder_fb_value shoulder_servo p_shoulder i_shoulder d_shoulder
 	global swing_sine swing_cosine swing_offset swing_command swing_master_value swing_fb_value swing_servo p_swing i_swing d_swing
 	global elbow_sine elbow_cosine elbow_offset elbow_command elbow_master_value elbow_fb_value elbow_servo p_elbow i_elbow d_elbow
  	global pitch_sine pitch_cosine pitch_offset pitch_command pitch_master_value pitch_fb_value pitch_servo p_pitch i_pitch d_pitch
  	global yaw_sine yaw_cosine yaw_offset yaw_command yaw_master_value yaw_fb_value yaw_servo p_yaw i_yaw d_yaw

        set str [split [loading_arm_string] | ]
        if {$str == ""} { return }
	
	set shoulder_sine "Analog Input [lindex $str 0]"
	set shoulder_cosine "Analog Input [lindex $str 1]"
	set shoulder_offset [lindex $str 2]
	set shoulder_command "Analog Input [lindex $str 3]"
	#set shoulder_master_value [lindex $str 4]
	#set shoulder_fb_value [lindex $str 5]	
	set shoulder_servo "Analog Output [lindex $str 6]"
	set p_shoulder [lindex $str 7]	
	set i_shoulder [lindex $str 8]	
	set d_shoulder [lindex $str 9]	
	set swing_sine "Analog Input [lindex $str 10]"
	set swing_cosine "Analog Input [lindex $str 11]"
	set swing_offset [lindex $str 12]
	set swing_command "Analog Input [lindex $str 13]"	
	#set swing_master_value [lindex $str 14]
	#set swing_fb_value [lindex $str 15]
	set swing_servo "Analog Output [lindex $str 16]"
	set p_swing [lindex $str 17]
	set i_swing [lindex $str 18]
	set d_swing [lindex $str 19]
	set elbow_sine "Analog Input [lindex $str 20]"
	set elbow_cosine "Analog Input [lindex $str 21]"
	set elbow_offset [lindex $str 22]
	set elbow_command "Analog Input [lindex $str 23]"
	#set elbow_master_value [lindex $str 24]
	#set elbow_fb_value [lindex $str 25]
	set elbow_servo "Analog Output [lindex $str 26]"
	set p_elbow [lindex $str 27]
	set i_elbow [lindex $str 28]
	set d_elbow [lindex $str 29]
	set pitch_sine "Analog Input [lindex $str 30]"
	set pitch_cosine "Analog Input [lindex $str 31]"
	set pitch_offset [lindex $str 32]
	set pitch_command "Analog Input [lindex $str 33]"	
	#set pitch_master_value [lindex $str 34]
	#set pitch_fb_value [lindex $str 35]
	set pitch_servo "Analog Output [lindex $str 36]"
	set p_pitch [lindex $str 37]
	set i_pitch [lindex $str 38]
	set d_pitch [lindex $str 39]
	set yaw_sine "Analog Input [lindex $str 40]"
	set yaw_cosine "Analog Input [lindex $str 41]"
	set yaw_offset [lindex $str 42]
	set yaw_command "Analog Input [lindex $str 43]"
	#set yaw_master_value [lindex $str 44]
	#set yaw_fb_value [lindex $str 45]
	set yaw_servo "Analog Output [lindex $str 46]"
	set p_yaw [lindex $str 47]
	set i_yaw [lindex $str 48]
	set d_yaw [lindex $str 49]
	                  	
}

proc display_auto_pilot_values { } {

        global foreaft_input turns_input auto_head_input auto_depth_input
        global auto_altitude_input auto_cruise_input  depth_jog_up_input depth_jog_down_input
        global head_source_input altimeter_input
        global head_p head_i head_d depth_p depth_i depth_d altitude_p altitude_i altitude_d cruise_p cruise_i cruise_d
        global port_thrust_output stbd_thrust_output vert_thrust_output lat_thrust_output
	global tempAlt

	set str [split [loading_auto_pilot_string] | ]
	if {$str == ""} { return }
	
	set foreaft_input "Analog Input [lindex $str 0]"
	set turns_input "Analog Input [lindex $str 1]"
	set auto_head_input "Digital Input [lindex $str 2]"
	set auto_depth_input "Digital Input [lindex $str 3]"
	set auto_altitude_input "Digital Input [lindex $str 4]"
	set auto_cruise_input "Digital Input [lindex $str 5]"
	set depth_jog_up_input "Digital Input [lindex $str 6]"
	set depth_jog_down_input "Digital Input [lindex $str 7]"
	#set head_source_input "FOG GYRO"
	set depth_trans_input "Paroscrientific"
	# check altimeter number
	set tempAlt [lindex $str 10]
	if { $tempAlt == 0 } {
		set altimeter_input "MESOTECH"
	} elseif { $tempAlt == 1 } {
		set altimeter_input "TRITECH"
	} elseif { $tempAlt == 2 } {
		set altimeter_input "DVL"
	}
	set dvl_input "DVL"
	set head_p [lindex $str 12]
	set head_i [lindex $str 13]
	set head_d [lindex $str 14]
	set depth_p [lindex $str 15]
	set depth_i [lindex $str 16]
	set depth_d [lindex $str 17]
	set altitude_p [lindex $str 18]
	set altitude_i [lindex $str 19]
	set altitude_d [lindex $str 20]
	set cruise_p [lindex $str 21]
	set cruise_i [lindex $str 22]
	set cruise_d [lindex $str 23]
	set port_thrust_output  "Analog Output [lindex $str 24]"
	set stbd_thrust_output  "Analog Output [lindex $str 25]"
	set vert_thrust_output   "Analog Output [lindex $str 26]"
	set lat_thrust_output  "Analog Output [lindex $str 27]"


}




proc destroy_all_graphic { } {
 	
	
 	global head_check_var head2_check_var
 	global backup_head_check_var backup_head2_check_var
	global vd v2d graph_type graph_type2
 	global vframe vehicle_xpos vehicle_ypos
 	global compass_xpos compass_ypos compass2_xpos compass2_ypos
 	global tmsx tmsy tms_display tmsx2 tmsy2 tms_display2
 	global olay
 	
 	destroy .ventana.editor.fog.compass
 	destroy .ventana.editor.backup_comp.compass
 	destroy .ventana2.editor.fog.compass
 	destroy .ventana2.editor.backup_comp.compass
 	
 	for {set i 1} {$i <= 48} {incr i} {
 		destroy .ventana.editor.frame$i
 		destroy .ventana2.editor.frame$i
 	        destroy .ventana.editor.bframe$i
 	        destroy .ventana2.editor.bframe$i
 	
 	        set graph_type "None"
 		set graph_type2 "None"
 	}	
 	
 	destroy $vframe
 	destroy .ventana.editor.vframe
 	destroy $tms_display
 	destroy $tms_display2
 	destroy $olay
 	destroy .overlay_screen
 	
 	set vehicle_xpos -1
 	set vehicle_ypos -1
 	set compass_xpos -1
 	set compass_ypos -1
 	set compass2_xpos -1
 	set compass2_ypos  -1
 	set tmsx -1
	set tmsy -1
	
 	set_graphic_info "compass" fog -1 -1 1
 	set_graphic_info "compass" backup_comp -1 -1 1
 	set_graphic_info "compass" fog -1 -1 2
 	set_graphic_info "compass" backup_comp -1 -1 2
 	set_graphic_info "vehicle" "none" -1 -1 1
 	
 	set_light_db 1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
	set_light_db 2 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
	
	set_tms_display -1 -1 1
	set_tms_display -1 -1 2
	
	#-------------------------------------------------------#
	# This will actually stop all the graphic loops		#
	#-------------------------------------------------------#
	
	#---------------#
	# Compass	#
	#---------------#
	
	global fog_loop_id fog_loop_id2  backup_loop_id backup_loop_id2
	after cancel $fog_loop_id
	after cancel $fog_loop_id2
	after cancel $backup_loop_id
        after cancel $backup_loop_id2

	#---------------#
	# Vehicle	#
	#---------------#
	
	global veh_loop_id
	after cancel $veh_loop_id
	
	#---------------#
	# Depthbar	#
	#---------------#
	
	global depth_loop_id
	after cancel $depth_loop_id
	
	
	
}
